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/jack.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/initval.h>
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/asoc.h>
47 static DEFINE_MUTEX(pcm_mutex
);
48 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
50 #ifdef CONFIG_DEBUG_FS
51 struct dentry
*snd_soc_debugfs_root
;
52 EXPORT_SYMBOL_GPL(snd_soc_debugfs_root
);
55 static DEFINE_MUTEX(client_mutex
);
56 static LIST_HEAD(card_list
);
57 static LIST_HEAD(dai_list
);
58 static LIST_HEAD(platform_list
);
59 static LIST_HEAD(codec_list
);
61 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
);
64 * This is a timeout to do a DAPM powerdown after a stream is closed().
65 * It can be used to eliminate pops between different playback streams, e.g.
66 * between two audio tracks.
68 static int pmdown_time
= 5000;
69 module_param(pmdown_time
, int, 0);
70 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
72 /* returns the minimum number of bytes needed to represent
73 * a particular given value */
74 static int min_bytes_needed(unsigned long val
)
79 for (i
= (sizeof val
* 8) - 1; i
>= 0; --i
, ++c
)
82 c
= (sizeof val
* 8) - c
;
90 /* codec register dump */
91 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
)
93 int ret
, i
, step
= 1, count
= 0;
94 int wordsize
, regsize
;
96 wordsize
= codec
->driver
->reg_word_size
* 2;
97 regsize
= min_bytes_needed(codec
->driver
->reg_cache_size
) * 2;
99 if (!codec
->driver
->reg_cache_size
)
102 if (codec
->driver
->reg_cache_step
)
103 step
= codec
->driver
->reg_cache_step
;
105 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
106 if (codec
->readable_register
&& !codec
->readable_register(codec
, i
))
109 count
+= sprintf(buf
+ count
, "%.*x: ", regsize
, i
);
110 if (count
>= PAGE_SIZE
- 1)
113 if (codec
->driver
->display_register
) {
114 count
+= codec
->driver
->display_register(codec
, buf
+ count
,
115 PAGE_SIZE
- count
, i
);
117 /* If the read fails it's almost certainly due to
118 * the register being volatile and the device being
121 ret
= snd_soc_read(codec
, i
);
123 count
+= snprintf(buf
+ count
,
125 "%.*x", wordsize
, ret
);
127 count
+= snprintf(buf
+ count
,
129 "<no data: %d>", ret
);
132 if (count
>= PAGE_SIZE
- 1)
135 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
136 if (count
>= PAGE_SIZE
- 1)
140 /* Truncate count; min() would cause a warning */
141 if (count
>= PAGE_SIZE
)
142 count
= PAGE_SIZE
- 1;
146 static ssize_t
codec_reg_show(struct device
*dev
,
147 struct device_attribute
*attr
, char *buf
)
149 struct snd_soc_pcm_runtime
*rtd
=
150 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
152 return soc_codec_reg_show(rtd
->codec
, buf
);
155 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
157 static ssize_t
pmdown_time_show(struct device
*dev
,
158 struct device_attribute
*attr
, char *buf
)
160 struct snd_soc_pcm_runtime
*rtd
=
161 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
163 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
166 static ssize_t
pmdown_time_set(struct device
*dev
,
167 struct device_attribute
*attr
,
168 const char *buf
, size_t count
)
170 struct snd_soc_pcm_runtime
*rtd
=
171 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
174 ret
= strict_strtol(buf
, 10, &rtd
->pmdown_time
);
181 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
183 #ifdef CONFIG_DEBUG_FS
184 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
186 file
->private_data
= inode
->i_private
;
190 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
191 size_t count
, loff_t
*ppos
)
194 struct snd_soc_codec
*codec
= file
->private_data
;
195 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
198 ret
= soc_codec_reg_show(codec
, buf
);
200 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
205 static ssize_t
codec_reg_write_file(struct file
*file
,
206 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
211 unsigned long reg
, value
;
213 struct snd_soc_codec
*codec
= file
->private_data
;
215 buf_size
= min(count
, (sizeof(buf
)-1));
216 if (copy_from_user(buf
, user_buf
, buf_size
))
220 if (codec
->driver
->reg_cache_step
)
221 step
= codec
->driver
->reg_cache_step
;
223 while (*start
== ' ')
225 reg
= simple_strtoul(start
, &start
, 16);
226 if ((reg
>= codec
->driver
->reg_cache_size
) || (reg
% step
))
228 while (*start
== ' ')
230 if (strict_strtoul(start
, 16, &value
))
233 /* Userspace has been fiddling around behind the kernel's back */
234 add_taint(TAINT_USER
);
236 snd_soc_write(codec
, reg
, value
);
240 static const struct file_operations codec_reg_fops
= {
241 .open
= codec_reg_open_file
,
242 .read
= codec_reg_read_file
,
243 .write
= codec_reg_write_file
,
244 .llseek
= default_llseek
,
247 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
249 struct dentry
*debugfs_card_root
= codec
->card
->debugfs_card_root
;
251 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
253 if (!codec
->debugfs_codec_root
) {
255 "ASoC: Failed to create codec debugfs directory\n");
259 debugfs_create_bool("cache_sync", 0444, codec
->debugfs_codec_root
,
261 debugfs_create_bool("cache_only", 0444, codec
->debugfs_codec_root
,
264 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
265 codec
->debugfs_codec_root
,
266 codec
, &codec_reg_fops
);
267 if (!codec
->debugfs_reg
)
269 "ASoC: Failed to create codec register debugfs file\n");
271 codec
->dapm
.debugfs_dapm
= debugfs_create_dir("dapm",
272 codec
->debugfs_codec_root
);
273 if (!codec
->dapm
.debugfs_dapm
)
275 "Failed to create DAPM debugfs directory\n");
277 snd_soc_dapm_debugfs_init(&codec
->dapm
);
280 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
282 debugfs_remove_recursive(codec
->debugfs_codec_root
);
285 static ssize_t
codec_list_read_file(struct file
*file
, char __user
*user_buf
,
286 size_t count
, loff_t
*ppos
)
288 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
289 ssize_t len
, ret
= 0;
290 struct snd_soc_codec
*codec
;
295 list_for_each_entry(codec
, &codec_list
, list
) {
296 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
300 if (ret
> PAGE_SIZE
) {
307 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
314 static const struct file_operations codec_list_fops
= {
315 .read
= codec_list_read_file
,
316 .llseek
= default_llseek
,/* read accesses f_pos */
319 static ssize_t
dai_list_read_file(struct file
*file
, char __user
*user_buf
,
320 size_t count
, loff_t
*ppos
)
322 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
323 ssize_t len
, ret
= 0;
324 struct snd_soc_dai
*dai
;
329 list_for_each_entry(dai
, &dai_list
, list
) {
330 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n", dai
->name
);
333 if (ret
> PAGE_SIZE
) {
339 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
346 static const struct file_operations dai_list_fops
= {
347 .read
= dai_list_read_file
,
348 .llseek
= default_llseek
,/* read accesses f_pos */
351 static ssize_t
platform_list_read_file(struct file
*file
,
352 char __user
*user_buf
,
353 size_t count
, loff_t
*ppos
)
355 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
356 ssize_t len
, ret
= 0;
357 struct snd_soc_platform
*platform
;
362 list_for_each_entry(platform
, &platform_list
, list
) {
363 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
, "%s\n",
367 if (ret
> PAGE_SIZE
) {
373 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
380 static const struct file_operations platform_list_fops
= {
381 .read
= platform_list_read_file
,
382 .llseek
= default_llseek
,/* read accesses f_pos */
385 static void soc_init_card_debugfs(struct snd_soc_card
*card
)
387 card
->debugfs_card_root
= debugfs_create_dir(card
->name
,
388 snd_soc_debugfs_root
);
389 if (!card
->debugfs_card_root
) {
391 "ASoC: Failed to create codec debugfs directory\n");
395 card
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0644,
396 card
->debugfs_card_root
,
398 if (!card
->debugfs_pop_time
)
400 "Failed to create pop time debugfs file\n");
403 static void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
405 debugfs_remove_recursive(card
->debugfs_card_root
);
410 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
414 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
418 static inline void soc_init_card_debugfs(struct snd_soc_card
*card
)
422 static inline void soc_cleanup_card_debugfs(struct snd_soc_card
*card
)
427 #ifdef CONFIG_SND_SOC_AC97_BUS
428 /* unregister ac97 codec */
429 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
431 if (codec
->ac97
->dev
.bus
)
432 device_unregister(&codec
->ac97
->dev
);
436 /* stop no dev release warning */
437 static void soc_ac97_device_release(struct device
*dev
){}
439 /* register ac97 codec to bus */
440 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
444 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
445 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
446 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
448 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
449 codec
->card
->snd_card
->number
, 0, codec
->name
);
450 err
= device_register(&codec
->ac97
->dev
);
452 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
453 codec
->ac97
->dev
.bus
= NULL
;
460 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
462 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
463 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
464 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
467 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
468 rtd
->dai_link
->symmetric_rates
) {
469 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
472 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
473 SNDRV_PCM_HW_PARAM_RATE
,
478 "Unable to apply rate symmetry constraint: %d\n", ret
);
487 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
488 * then initialized and any private data can be allocated. This also calls
489 * startup for the cpu DAI, platform, machine and codec DAI.
491 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
493 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
494 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
495 struct snd_soc_platform
*platform
= rtd
->platform
;
496 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
497 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
498 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
499 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
502 mutex_lock(&pcm_mutex
);
504 /* startup the audio subsystem */
505 if (cpu_dai
->driver
->ops
->startup
) {
506 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
508 printk(KERN_ERR
"asoc: can't open interface %s\n",
514 if (platform
->driver
->ops
->open
) {
515 ret
= platform
->driver
->ops
->open(substream
);
517 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
522 if (codec_dai
->driver
->ops
->startup
) {
523 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
525 printk(KERN_ERR
"asoc: can't open codec %s\n",
531 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
532 ret
= rtd
->dai_link
->ops
->startup(substream
);
534 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
539 /* Check that the codec and cpu DAIs are compatible */
540 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
541 runtime
->hw
.rate_min
=
542 max(codec_dai_drv
->playback
.rate_min
,
543 cpu_dai_drv
->playback
.rate_min
);
544 runtime
->hw
.rate_max
=
545 min(codec_dai_drv
->playback
.rate_max
,
546 cpu_dai_drv
->playback
.rate_max
);
547 runtime
->hw
.channels_min
=
548 max(codec_dai_drv
->playback
.channels_min
,
549 cpu_dai_drv
->playback
.channels_min
);
550 runtime
->hw
.channels_max
=
551 min(codec_dai_drv
->playback
.channels_max
,
552 cpu_dai_drv
->playback
.channels_max
);
553 runtime
->hw
.formats
=
554 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
556 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
557 if (codec_dai_drv
->playback
.rates
558 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
559 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
560 if (cpu_dai_drv
->playback
.rates
561 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
562 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
564 runtime
->hw
.rate_min
=
565 max(codec_dai_drv
->capture
.rate_min
,
566 cpu_dai_drv
->capture
.rate_min
);
567 runtime
->hw
.rate_max
=
568 min(codec_dai_drv
->capture
.rate_max
,
569 cpu_dai_drv
->capture
.rate_max
);
570 runtime
->hw
.channels_min
=
571 max(codec_dai_drv
->capture
.channels_min
,
572 cpu_dai_drv
->capture
.channels_min
);
573 runtime
->hw
.channels_max
=
574 min(codec_dai_drv
->capture
.channels_max
,
575 cpu_dai_drv
->capture
.channels_max
);
576 runtime
->hw
.formats
=
577 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
579 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
580 if (codec_dai_drv
->capture
.rates
581 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
582 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
583 if (cpu_dai_drv
->capture
.rates
584 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
585 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
588 snd_pcm_limit_hw_rates(runtime
);
589 if (!runtime
->hw
.rates
) {
590 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
591 codec_dai
->name
, cpu_dai
->name
);
594 if (!runtime
->hw
.formats
) {
595 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
596 codec_dai
->name
, cpu_dai
->name
);
599 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
600 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
601 codec_dai
->name
, cpu_dai
->name
);
605 /* Symmetry only applies if we've already got an active stream. */
606 if (cpu_dai
->active
|| codec_dai
->active
) {
607 ret
= soc_pcm_apply_symmetry(substream
);
612 pr_debug("asoc: %s <-> %s info:\n",
613 codec_dai
->name
, cpu_dai
->name
);
614 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
615 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
616 runtime
->hw
.channels_max
);
617 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
618 runtime
->hw
.rate_max
);
620 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
621 cpu_dai
->playback_active
++;
622 codec_dai
->playback_active
++;
624 cpu_dai
->capture_active
++;
625 codec_dai
->capture_active
++;
629 rtd
->codec
->active
++;
630 mutex_unlock(&pcm_mutex
);
634 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
635 rtd
->dai_link
->ops
->shutdown(substream
);
638 if (codec_dai
->driver
->ops
->shutdown
)
639 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
642 if (platform
->driver
->ops
->close
)
643 platform
->driver
->ops
->close(substream
);
646 if (cpu_dai
->driver
->ops
->shutdown
)
647 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
649 mutex_unlock(&pcm_mutex
);
654 * Power down the audio subsystem pmdown_time msecs after close is called.
655 * This is to ensure there are no pops or clicks in between any music tracks
656 * due to DAPM power cycling.
658 static void close_delayed_work(struct work_struct
*work
)
660 struct snd_soc_pcm_runtime
*rtd
=
661 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
662 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
664 mutex_lock(&pcm_mutex
);
666 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
667 codec_dai
->driver
->playback
.stream_name
,
668 codec_dai
->playback_active
? "active" : "inactive",
669 codec_dai
->pop_wait
? "yes" : "no");
671 /* are we waiting on this codec DAI stream */
672 if (codec_dai
->pop_wait
== 1) {
673 codec_dai
->pop_wait
= 0;
674 snd_soc_dapm_stream_event(rtd
,
675 codec_dai
->driver
->playback
.stream_name
,
676 SND_SOC_DAPM_STREAM_STOP
);
679 mutex_unlock(&pcm_mutex
);
683 * Called by ALSA when a PCM substream is closed. Private data can be
684 * freed here. The cpu DAI, codec DAI, machine and platform are also
687 static int soc_codec_close(struct snd_pcm_substream
*substream
)
689 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
690 struct snd_soc_platform
*platform
= rtd
->platform
;
691 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
692 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
693 struct snd_soc_codec
*codec
= rtd
->codec
;
695 mutex_lock(&pcm_mutex
);
697 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
698 cpu_dai
->playback_active
--;
699 codec_dai
->playback_active
--;
701 cpu_dai
->capture_active
--;
702 codec_dai
->capture_active
--;
709 /* Muting the DAC suppresses artifacts caused during digital
710 * shutdown, for example from stopping clocks.
712 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
713 snd_soc_dai_digital_mute(codec_dai
, 1);
715 if (cpu_dai
->driver
->ops
->shutdown
)
716 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
718 if (codec_dai
->driver
->ops
->shutdown
)
719 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
721 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
722 rtd
->dai_link
->ops
->shutdown(substream
);
724 if (platform
->driver
->ops
->close
)
725 platform
->driver
->ops
->close(substream
);
726 cpu_dai
->runtime
= NULL
;
728 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
729 /* start delayed pop wq here for playback streams */
730 codec_dai
->pop_wait
= 1;
731 schedule_delayed_work(&rtd
->delayed_work
,
732 msecs_to_jiffies(rtd
->pmdown_time
));
734 /* capture streams can be powered down now */
735 snd_soc_dapm_stream_event(rtd
,
736 codec_dai
->driver
->capture
.stream_name
,
737 SND_SOC_DAPM_STREAM_STOP
);
740 mutex_unlock(&pcm_mutex
);
745 * Called by ALSA when the PCM substream is prepared, can set format, sample
746 * rate, etc. This function is non atomic and can be called multiple times,
747 * it can refer to the runtime info.
749 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
751 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
752 struct snd_soc_platform
*platform
= rtd
->platform
;
753 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
754 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
757 mutex_lock(&pcm_mutex
);
759 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
760 ret
= rtd
->dai_link
->ops
->prepare(substream
);
762 printk(KERN_ERR
"asoc: machine prepare error\n");
767 if (platform
->driver
->ops
->prepare
) {
768 ret
= platform
->driver
->ops
->prepare(substream
);
770 printk(KERN_ERR
"asoc: platform prepare error\n");
775 if (codec_dai
->driver
->ops
->prepare
) {
776 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
778 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
783 if (cpu_dai
->driver
->ops
->prepare
) {
784 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
786 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
791 /* cancel any delayed stream shutdown that is pending */
792 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
793 codec_dai
->pop_wait
) {
794 codec_dai
->pop_wait
= 0;
795 cancel_delayed_work(&rtd
->delayed_work
);
798 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
799 snd_soc_dapm_stream_event(rtd
,
800 codec_dai
->driver
->playback
.stream_name
,
801 SND_SOC_DAPM_STREAM_START
);
803 snd_soc_dapm_stream_event(rtd
,
804 codec_dai
->driver
->capture
.stream_name
,
805 SND_SOC_DAPM_STREAM_START
);
807 snd_soc_dai_digital_mute(codec_dai
, 0);
810 mutex_unlock(&pcm_mutex
);
815 * Called by ALSA when the hardware params are set by application. This
816 * function can also be called multiple times and can allocate buffers
817 * (using snd_pcm_lib_* ). It's non-atomic.
819 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
820 struct snd_pcm_hw_params
*params
)
822 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
823 struct snd_soc_platform
*platform
= rtd
->platform
;
824 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
825 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
828 mutex_lock(&pcm_mutex
);
830 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
831 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
833 printk(KERN_ERR
"asoc: machine hw_params failed\n");
838 if (codec_dai
->driver
->ops
->hw_params
) {
839 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
841 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
847 if (cpu_dai
->driver
->ops
->hw_params
) {
848 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
850 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
856 if (platform
->driver
->ops
->hw_params
) {
857 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
859 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
865 rtd
->rate
= params_rate(params
);
868 mutex_unlock(&pcm_mutex
);
872 if (cpu_dai
->driver
->ops
->hw_free
)
873 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
876 if (codec_dai
->driver
->ops
->hw_free
)
877 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
880 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
881 rtd
->dai_link
->ops
->hw_free(substream
);
883 mutex_unlock(&pcm_mutex
);
888 * Frees resources allocated by hw_params, can be called multiple times
890 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
892 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
893 struct snd_soc_platform
*platform
= rtd
->platform
;
894 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
895 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
896 struct snd_soc_codec
*codec
= rtd
->codec
;
898 mutex_lock(&pcm_mutex
);
900 /* apply codec digital mute */
902 snd_soc_dai_digital_mute(codec_dai
, 1);
904 /* free any machine hw params */
905 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
906 rtd
->dai_link
->ops
->hw_free(substream
);
908 /* free any DMA resources */
909 if (platform
->driver
->ops
->hw_free
)
910 platform
->driver
->ops
->hw_free(substream
);
912 /* now free hw params for the DAIs */
913 if (codec_dai
->driver
->ops
->hw_free
)
914 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
916 if (cpu_dai
->driver
->ops
->hw_free
)
917 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
919 mutex_unlock(&pcm_mutex
);
923 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
925 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
926 struct snd_soc_platform
*platform
= rtd
->platform
;
927 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
928 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
931 if (codec_dai
->driver
->ops
->trigger
) {
932 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
937 if (platform
->driver
->ops
->trigger
) {
938 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
943 if (cpu_dai
->driver
->ops
->trigger
) {
944 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
952 * soc level wrapper for pointer callback
953 * If cpu_dai, codec_dai, platform driver has the delay callback, than
954 * the runtime->delay will be updated accordingly.
956 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
958 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
959 struct snd_soc_platform
*platform
= rtd
->platform
;
960 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
961 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
962 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
963 snd_pcm_uframes_t offset
= 0;
964 snd_pcm_sframes_t delay
= 0;
966 if (platform
->driver
->ops
->pointer
)
967 offset
= platform
->driver
->ops
->pointer(substream
);
969 if (cpu_dai
->driver
->ops
->delay
)
970 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
972 if (codec_dai
->driver
->ops
->delay
)
973 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
975 if (platform
->driver
->delay
)
976 delay
+= platform
->driver
->delay(substream
, codec_dai
);
978 runtime
->delay
= delay
;
983 /* ASoC PCM operations */
984 static struct snd_pcm_ops soc_pcm_ops
= {
985 .open
= soc_pcm_open
,
986 .close
= soc_codec_close
,
987 .hw_params
= soc_pcm_hw_params
,
988 .hw_free
= soc_pcm_hw_free
,
989 .prepare
= soc_pcm_prepare
,
990 .trigger
= soc_pcm_trigger
,
991 .pointer
= soc_pcm_pointer
,
994 #ifdef CONFIG_PM_SLEEP
995 /* powers down audio subsystem for suspend */
996 int snd_soc_suspend(struct device
*dev
)
998 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
999 struct snd_soc_codec
*codec
;
1002 /* If the initialization of this soc device failed, there is no codec
1003 * associated with it. Just bail out in this case.
1005 if (list_empty(&card
->codec_dev_list
))
1008 /* Due to the resume being scheduled into a workqueue we could
1009 * suspend before that's finished - wait for it to complete.
1011 snd_power_lock(card
->snd_card
);
1012 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1013 snd_power_unlock(card
->snd_card
);
1015 /* we're going to block userspace touching us until resume completes */
1016 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
1018 /* mute any active DACs */
1019 for (i
= 0; i
< card
->num_rtd
; i
++) {
1020 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1021 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1023 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1026 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1027 drv
->ops
->digital_mute(dai
, 1);
1030 /* suspend all pcms */
1031 for (i
= 0; i
< card
->num_rtd
; i
++) {
1032 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1035 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
1038 if (card
->suspend_pre
)
1039 card
->suspend_pre(card
);
1041 for (i
= 0; i
< card
->num_rtd
; i
++) {
1042 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1043 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1045 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1048 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
1049 cpu_dai
->driver
->suspend(cpu_dai
);
1050 if (platform
->driver
->suspend
&& !platform
->suspended
) {
1051 platform
->driver
->suspend(cpu_dai
);
1052 platform
->suspended
= 1;
1056 /* close any waiting streams and save state */
1057 for (i
= 0; i
< card
->num_rtd
; i
++) {
1058 flush_delayed_work_sync(&card
->rtd
[i
].delayed_work
);
1059 card
->rtd
[i
].codec
->dapm
.suspend_bias_level
= card
->rtd
[i
].codec
->dapm
.bias_level
;
1062 for (i
= 0; i
< card
->num_rtd
; i
++) {
1063 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1065 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1068 if (driver
->playback
.stream_name
!= NULL
)
1069 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1070 SND_SOC_DAPM_STREAM_SUSPEND
);
1072 if (driver
->capture
.stream_name
!= NULL
)
1073 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1074 SND_SOC_DAPM_STREAM_SUSPEND
);
1077 /* suspend all CODECs */
1078 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
1079 /* If there are paths active then the CODEC will be held with
1080 * bias _ON and should not be suspended. */
1081 if (!codec
->suspended
&& codec
->driver
->suspend
) {
1082 switch (codec
->dapm
.bias_level
) {
1083 case SND_SOC_BIAS_STANDBY
:
1084 case SND_SOC_BIAS_OFF
:
1085 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
1086 codec
->suspended
= 1;
1089 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
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
->suspend
&& cpu_dai
->driver
->ac97_control
)
1102 cpu_dai
->driver
->suspend(cpu_dai
);
1105 if (card
->suspend_post
)
1106 card
->suspend_post(card
);
1110 EXPORT_SYMBOL_GPL(snd_soc_suspend
);
1112 /* deferred resume work, so resume can complete before we finished
1113 * setting our codec back up, which can be very slow on I2C
1115 static void soc_resume_deferred(struct work_struct
*work
)
1117 struct snd_soc_card
*card
=
1118 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
1119 struct snd_soc_codec
*codec
;
1122 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
1123 * so userspace apps are blocked from touching us
1126 dev_dbg(card
->dev
, "starting resume work\n");
1128 /* Bring us up into D2 so that DAPM starts enabling things */
1129 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
1131 if (card
->resume_pre
)
1132 card
->resume_pre(card
);
1134 /* resume AC97 DAIs */
1135 for (i
= 0; i
< card
->num_rtd
; i
++) {
1136 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1138 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1141 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
1142 cpu_dai
->driver
->resume(cpu_dai
);
1145 list_for_each_entry(codec
, &card
->codec_dev_list
, card_list
) {
1146 /* If the CODEC was idle over suspend then it will have been
1147 * left with bias OFF or STANDBY and suspended so we must now
1148 * resume. Otherwise the suspend was suppressed.
1150 if (codec
->driver
->resume
&& codec
->suspended
) {
1151 switch (codec
->dapm
.bias_level
) {
1152 case SND_SOC_BIAS_STANDBY
:
1153 case SND_SOC_BIAS_OFF
:
1154 codec
->driver
->resume(codec
);
1155 codec
->suspended
= 0;
1158 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1164 for (i
= 0; i
< card
->num_rtd
; i
++) {
1165 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1167 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1170 if (driver
->playback
.stream_name
!= NULL
)
1171 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1172 SND_SOC_DAPM_STREAM_RESUME
);
1174 if (driver
->capture
.stream_name
!= NULL
)
1175 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1176 SND_SOC_DAPM_STREAM_RESUME
);
1179 /* unmute any active DACs */
1180 for (i
= 0; i
< card
->num_rtd
; i
++) {
1181 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1182 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1184 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1187 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1188 drv
->ops
->digital_mute(dai
, 0);
1191 for (i
= 0; i
< card
->num_rtd
; i
++) {
1192 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1193 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1195 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1198 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1199 cpu_dai
->driver
->resume(cpu_dai
);
1200 if (platform
->driver
->resume
&& platform
->suspended
) {
1201 platform
->driver
->resume(cpu_dai
);
1202 platform
->suspended
= 0;
1206 if (card
->resume_post
)
1207 card
->resume_post(card
);
1209 dev_dbg(card
->dev
, "resume work completed\n");
1211 /* userspace can access us now we are back as we were before */
1212 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1215 /* powers up audio subsystem after a suspend */
1216 int snd_soc_resume(struct device
*dev
)
1218 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
1221 /* AC97 devices might have other drivers hanging off them so
1222 * need to resume immediately. Other drivers don't have that
1223 * problem and may take a substantial amount of time to resume
1224 * due to I/O costs and anti-pop so handle them out of line.
1226 for (i
= 0; i
< card
->num_rtd
; i
++) {
1227 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1228 if (cpu_dai
->driver
->ac97_control
) {
1229 dev_dbg(dev
, "Resuming AC97 immediately\n");
1230 soc_resume_deferred(&card
->deferred_resume_work
);
1232 dev_dbg(dev
, "Scheduling resume work\n");
1233 if (!schedule_work(&card
->deferred_resume_work
))
1234 dev_err(dev
, "resume work item may be lost\n");
1240 EXPORT_SYMBOL_GPL(snd_soc_resume
);
1242 #define snd_soc_suspend NULL
1243 #define snd_soc_resume NULL
1246 static struct snd_soc_dai_ops null_dai_ops
= {
1249 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1251 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1252 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1253 struct snd_soc_codec
*codec
;
1254 struct snd_soc_platform
*platform
;
1255 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1259 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1261 /* do we already have the CPU DAI for this link ? */
1265 /* no, then find CPU DAI from registered DAIs*/
1266 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1267 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1269 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1272 rtd
->cpu_dai
= cpu_dai
;
1276 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1277 dai_link
->cpu_dai_name
);
1280 /* do we already have the CODEC for this link ? */
1285 /* no, then find CODEC from registered CODECs*/
1286 list_for_each_entry(codec
, &codec_list
, list
) {
1287 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1290 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1291 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1292 if (codec
->dev
== codec_dai
->dev
&&
1293 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1294 rtd
->codec_dai
= codec_dai
;
1298 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1299 dai_link
->codec_dai_name
);
1304 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1305 dai_link
->codec_name
);
1308 /* do we already have the CODEC DAI for this link ? */
1309 if (rtd
->platform
) {
1312 /* no, then find CPU DAI from registered DAIs*/
1313 list_for_each_entry(platform
, &platform_list
, list
) {
1314 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1315 rtd
->platform
= platform
;
1320 dev_dbg(card
->dev
, "platform %s not registered\n",
1321 dai_link
->platform_name
);
1325 /* mark rtd as complete if we found all 4 of our client devices */
1326 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1333 static void soc_remove_codec(struct snd_soc_codec
*codec
)
1337 if (codec
->driver
->remove
) {
1338 err
= codec
->driver
->remove(codec
);
1341 "asoc: failed to remove %s: %d\n",
1345 /* Make sure all DAPM widgets are freed */
1346 snd_soc_dapm_free(&codec
->dapm
);
1348 soc_cleanup_codec_debugfs(codec
);
1350 list_del(&codec
->card_list
);
1351 module_put(codec
->dev
->driver
->owner
);
1354 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1356 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1357 struct snd_soc_codec
*codec
= rtd
->codec
;
1358 struct snd_soc_platform
*platform
= rtd
->platform
;
1359 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1362 /* unregister the rtd device */
1363 if (rtd
->dev_registered
) {
1364 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1365 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1366 device_unregister(&rtd
->dev
);
1367 rtd
->dev_registered
= 0;
1370 /* remove the CODEC DAI */
1371 if (codec_dai
&& codec_dai
->probed
) {
1372 if (codec_dai
->driver
->remove
) {
1373 err
= codec_dai
->driver
->remove(codec_dai
);
1375 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1377 codec_dai
->probed
= 0;
1378 list_del(&codec_dai
->card_list
);
1381 /* remove the platform */
1382 if (platform
&& platform
->probed
) {
1383 if (platform
->driver
->remove
) {
1384 err
= platform
->driver
->remove(platform
);
1386 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1388 platform
->probed
= 0;
1389 list_del(&platform
->card_list
);
1390 module_put(platform
->dev
->driver
->owner
);
1393 /* remove the CODEC */
1394 if (codec
&& codec
->probed
)
1395 soc_remove_codec(codec
);
1397 /* remove the cpu_dai */
1398 if (cpu_dai
&& cpu_dai
->probed
) {
1399 if (cpu_dai
->driver
->remove
) {
1400 err
= cpu_dai
->driver
->remove(cpu_dai
);
1402 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1404 cpu_dai
->probed
= 0;
1405 list_del(&cpu_dai
->card_list
);
1406 module_put(cpu_dai
->dev
->driver
->owner
);
1410 static void soc_set_name_prefix(struct snd_soc_card
*card
,
1411 struct snd_soc_codec
*codec
)
1415 if (card
->codec_conf
== NULL
)
1418 for (i
= 0; i
< card
->num_configs
; i
++) {
1419 struct snd_soc_codec_conf
*map
= &card
->codec_conf
[i
];
1420 if (map
->dev_name
&& !strcmp(codec
->name
, map
->dev_name
)) {
1421 codec
->name_prefix
= map
->name_prefix
;
1427 static int soc_probe_codec(struct snd_soc_card
*card
,
1428 struct snd_soc_codec
*codec
)
1433 codec
->dapm
.card
= card
;
1434 soc_set_name_prefix(card
, codec
);
1436 if (!try_module_get(codec
->dev
->driver
->owner
))
1439 if (codec
->driver
->probe
) {
1440 ret
= codec
->driver
->probe(codec
);
1443 "asoc: failed to probe CODEC %s: %d\n",
1449 soc_init_codec_debugfs(codec
);
1451 /* mark codec as probed and add to card codec list */
1453 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1454 list_add(&codec
->dapm
.list
, &card
->dapm_list
);
1459 module_put(codec
->dev
->driver
->owner
);
1464 static void rtd_release(struct device
*dev
) {}
1466 static int soc_post_component_init(struct snd_soc_card
*card
,
1467 struct snd_soc_codec
*codec
,
1468 int num
, int dailess
)
1470 struct snd_soc_dai_link
*dai_link
= NULL
;
1471 struct snd_soc_aux_dev
*aux_dev
= NULL
;
1472 struct snd_soc_pcm_runtime
*rtd
;
1473 const char *temp
, *name
;
1477 dai_link
= &card
->dai_link
[num
];
1478 rtd
= &card
->rtd
[num
];
1479 name
= dai_link
->name
;
1481 aux_dev
= &card
->aux_dev
[num
];
1482 rtd
= &card
->rtd_aux
[num
];
1483 name
= aux_dev
->name
;
1486 /* machine controls, routes and widgets are not prefixed */
1487 temp
= codec
->name_prefix
;
1488 codec
->name_prefix
= NULL
;
1490 /* do machine specific initialization */
1491 if (!dailess
&& dai_link
->init
)
1492 ret
= dai_link
->init(rtd
);
1493 else if (dailess
&& aux_dev
->init
)
1494 ret
= aux_dev
->init(&codec
->dapm
);
1496 dev_err(card
->dev
, "asoc: failed to init %s: %d\n", name
, ret
);
1499 codec
->name_prefix
= temp
;
1501 /* Make sure all DAPM widgets are instantiated */
1502 snd_soc_dapm_new_widgets(&codec
->dapm
);
1504 /* register the rtd device */
1507 rtd
->dev
.parent
= card
->dev
;
1508 rtd
->dev
.release
= rtd_release
;
1509 rtd
->dev
.init_name
= name
;
1510 ret
= device_register(&rtd
->dev
);
1513 "asoc: failed to register runtime device: %d\n", ret
);
1516 rtd
->dev_registered
= 1;
1518 /* add DAPM sysfs entries for this codec */
1519 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1522 "asoc: failed to add codec dapm sysfs entries: %d\n",
1525 /* add codec sysfs entries */
1526 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1529 "asoc: failed to add codec sysfs files: %d\n", ret
);
1534 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1536 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1537 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1538 struct snd_soc_codec
*codec
= rtd
->codec
;
1539 struct snd_soc_platform
*platform
= rtd
->platform
;
1540 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1543 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1545 /* config components */
1546 codec_dai
->codec
= codec
;
1547 cpu_dai
->platform
= platform
;
1548 codec_dai
->card
= card
;
1549 cpu_dai
->card
= card
;
1551 /* set default power off timeout */
1552 rtd
->pmdown_time
= pmdown_time
;
1554 /* probe the cpu_dai */
1555 if (!cpu_dai
->probed
) {
1556 if (cpu_dai
->driver
->probe
) {
1557 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1559 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1564 cpu_dai
->probed
= 1;
1565 /* mark cpu_dai as probed and add to card cpu_dai list */
1566 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1569 /* probe the CODEC */
1570 if (!codec
->probed
) {
1571 ret
= soc_probe_codec(card
, codec
);
1576 /* probe the platform */
1577 if (!platform
->probed
) {
1578 if (!try_module_get(platform
->dev
->driver
->owner
))
1581 if (platform
->driver
->probe
) {
1582 ret
= platform
->driver
->probe(platform
);
1584 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1586 module_put(platform
->dev
->driver
->owner
);
1590 /* mark platform as probed and add to card platform list */
1591 platform
->probed
= 1;
1592 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1595 /* probe the CODEC DAI */
1596 if (!codec_dai
->probed
) {
1597 if (codec_dai
->driver
->probe
) {
1598 ret
= codec_dai
->driver
->probe(codec_dai
);
1600 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1606 /* mark cpu_dai as probed and add to card cpu_dai list */
1607 codec_dai
->probed
= 1;
1608 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1611 /* DAPM dai link stream work */
1612 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1614 ret
= soc_post_component_init(card
, codec
, num
, 0);
1618 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1620 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1622 /* create the pcm */
1623 ret
= soc_new_pcm(rtd
, num
);
1625 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1629 /* add platform data for AC97 devices */
1630 if (rtd
->codec_dai
->driver
->ac97_control
)
1631 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1636 #ifdef CONFIG_SND_SOC_AC97_BUS
1637 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1641 /* Only instantiate AC97 if not already done by the adaptor
1642 * for the generic AC97 subsystem.
1644 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1646 * It is possible that the AC97 device is already registered to
1647 * the device subsystem. This happens when the device is created
1648 * via snd_ac97_mixer(). Currently only SoC codec that does so
1649 * is the generic AC97 glue but others migh emerge.
1651 * In those cases we don't try to register the device again.
1653 if (!rtd
->codec
->ac97_created
)
1656 ret
= soc_ac97_dev_register(rtd
->codec
);
1658 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1662 rtd
->codec
->ac97_registered
= 1;
1667 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1669 if (codec
->ac97_registered
) {
1670 soc_ac97_dev_unregister(codec
);
1671 codec
->ac97_registered
= 0;
1676 static int soc_probe_aux_dev(struct snd_soc_card
*card
, int num
)
1678 struct snd_soc_aux_dev
*aux_dev
= &card
->aux_dev
[num
];
1679 struct snd_soc_codec
*codec
;
1682 /* find CODEC from registered CODECs*/
1683 list_for_each_entry(codec
, &codec_list
, list
) {
1684 if (!strcmp(codec
->name
, aux_dev
->codec_name
)) {
1685 if (codec
->probed
) {
1687 "asoc: codec already probed");
1694 /* codec not found */
1695 dev_err(card
->dev
, "asoc: codec %s not found", aux_dev
->codec_name
);
1699 ret
= soc_probe_codec(card
, codec
);
1703 ret
= soc_post_component_init(card
, codec
, num
, 1);
1709 static void soc_remove_aux_dev(struct snd_soc_card
*card
, int num
)
1711 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd_aux
[num
];
1712 struct snd_soc_codec
*codec
= rtd
->codec
;
1714 /* unregister the rtd device */
1715 if (rtd
->dev_registered
) {
1716 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1717 device_unregister(&rtd
->dev
);
1718 rtd
->dev_registered
= 0;
1721 if (codec
&& codec
->probed
)
1722 soc_remove_codec(codec
);
1725 static int snd_soc_init_codec_cache(struct snd_soc_codec
*codec
,
1726 enum snd_soc_compress_type compress_type
)
1730 if (codec
->cache_init
)
1733 /* override the compress_type if necessary */
1734 if (compress_type
&& codec
->compress_type
!= compress_type
)
1735 codec
->compress_type
= compress_type
;
1736 ret
= snd_soc_cache_init(codec
);
1738 dev_err(codec
->dev
, "Failed to set cache compression type: %d\n",
1742 codec
->cache_init
= 1;
1746 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1748 struct snd_soc_codec
*codec
;
1749 struct snd_soc_codec_conf
*codec_conf
;
1750 enum snd_soc_compress_type compress_type
;
1753 mutex_lock(&card
->mutex
);
1755 if (card
->instantiated
) {
1756 mutex_unlock(&card
->mutex
);
1761 for (i
= 0; i
< card
->num_links
; i
++)
1762 soc_bind_dai_link(card
, i
);
1764 /* bind completed ? */
1765 if (card
->num_rtd
!= card
->num_links
) {
1766 mutex_unlock(&card
->mutex
);
1770 /* initialize the register cache for each available codec */
1771 list_for_each_entry(codec
, &codec_list
, list
) {
1772 if (codec
->cache_init
)
1774 /* by default we don't override the compress_type */
1776 /* check to see if we need to override the compress_type */
1777 for (i
= 0; i
< card
->num_configs
; ++i
) {
1778 codec_conf
= &card
->codec_conf
[i
];
1779 if (!strcmp(codec
->name
, codec_conf
->dev_name
)) {
1780 compress_type
= codec_conf
->compress_type
;
1781 if (compress_type
&& compress_type
1782 != codec
->compress_type
)
1786 ret
= snd_soc_init_codec_cache(codec
, compress_type
);
1788 mutex_unlock(&card
->mutex
);
1793 /* card bind complete so register a sound card */
1794 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1795 card
->owner
, 0, &card
->snd_card
);
1797 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1799 mutex_unlock(&card
->mutex
);
1802 card
->snd_card
->dev
= card
->dev
;
1804 #ifdef CONFIG_PM_SLEEP
1805 /* deferred resume work */
1806 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1809 /* initialise the sound card only once */
1811 ret
= card
->probe(card
);
1813 goto card_probe_error
;
1816 for (i
= 0; i
< card
->num_links
; i
++) {
1817 ret
= soc_probe_dai_link(card
, i
);
1819 pr_err("asoc: failed to instantiate card %s: %d\n",
1825 for (i
= 0; i
< card
->num_aux_devs
; i
++) {
1826 ret
= soc_probe_aux_dev(card
, i
);
1828 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1830 goto probe_aux_dev_err
;
1834 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1836 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1839 ret
= snd_card_register(card
->snd_card
);
1841 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1842 goto probe_aux_dev_err
;
1845 #ifdef CONFIG_SND_SOC_AC97_BUS
1846 /* register any AC97 codecs */
1847 for (i
= 0; i
< card
->num_rtd
; i
++) {
1848 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1850 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1852 soc_unregister_ac97_dai_link(card
->rtd
[i
].codec
);
1853 goto probe_aux_dev_err
;
1858 card
->instantiated
= 1;
1859 mutex_unlock(&card
->mutex
);
1863 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1864 soc_remove_aux_dev(card
, i
);
1867 for (i
= 0; i
< card
->num_links
; i
++)
1868 soc_remove_dai_link(card
, i
);
1874 snd_card_free(card
->snd_card
);
1876 mutex_unlock(&card
->mutex
);
1880 * Attempt to initialise any uninitialised cards. Must be called with
1883 static void snd_soc_instantiate_cards(void)
1885 struct snd_soc_card
*card
;
1886 list_for_each_entry(card
, &card_list
, list
)
1887 snd_soc_instantiate_card(card
);
1890 /* probes a new socdev */
1891 static int soc_probe(struct platform_device
*pdev
)
1893 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1897 * no card, so machine driver should be registering card
1898 * we should not be here in that case so ret error
1903 /* Bodge while we unpick instantiation */
1904 card
->dev
= &pdev
->dev
;
1906 ret
= snd_soc_register_card(card
);
1908 dev_err(&pdev
->dev
, "Failed to register card\n");
1915 static int soc_cleanup_card_resources(struct snd_soc_card
*card
)
1919 /* make sure any delayed work runs */
1920 for (i
= 0; i
< card
->num_rtd
; i
++) {
1921 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1922 flush_delayed_work_sync(&rtd
->delayed_work
);
1925 /* remove auxiliary devices */
1926 for (i
= 0; i
< card
->num_aux_devs
; i
++)
1927 soc_remove_aux_dev(card
, i
);
1929 /* remove and free each DAI */
1930 for (i
= 0; i
< card
->num_rtd
; i
++)
1931 soc_remove_dai_link(card
, i
);
1933 soc_cleanup_card_debugfs(card
);
1935 /* remove the card */
1940 snd_card_free(card
->snd_card
);
1945 /* removes a socdev */
1946 static int soc_remove(struct platform_device
*pdev
)
1948 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1950 snd_soc_unregister_card(card
);
1954 int snd_soc_poweroff(struct device
*dev
)
1956 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
1959 if (!card
->instantiated
)
1962 /* Flush out pmdown_time work - we actually do want to run it
1963 * now, we're shutting down so no imminent restart. */
1964 for (i
= 0; i
< card
->num_rtd
; i
++) {
1965 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1966 flush_delayed_work_sync(&rtd
->delayed_work
);
1969 snd_soc_dapm_shutdown(card
);
1973 EXPORT_SYMBOL_GPL(snd_soc_poweroff
);
1975 const struct dev_pm_ops snd_soc_pm_ops
= {
1976 .suspend
= snd_soc_suspend
,
1977 .resume
= snd_soc_resume
,
1978 .poweroff
= snd_soc_poweroff
,
1981 /* ASoC platform driver */
1982 static struct platform_driver soc_driver
= {
1984 .name
= "soc-audio",
1985 .owner
= THIS_MODULE
,
1986 .pm
= &snd_soc_pm_ops
,
1989 .remove
= soc_remove
,
1992 /* create a new pcm */
1993 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1995 struct snd_soc_codec
*codec
= rtd
->codec
;
1996 struct snd_soc_platform
*platform
= rtd
->platform
;
1997 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1998 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1999 struct snd_pcm
*pcm
;
2001 int ret
= 0, playback
= 0, capture
= 0;
2003 /* check client and interface hw capabilities */
2004 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
2005 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
2007 if (codec_dai
->driver
->playback
.channels_min
)
2009 if (codec_dai
->driver
->capture
.channels_min
)
2012 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
2013 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
2014 num
, playback
, capture
, &pcm
);
2016 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
2021 pcm
->private_data
= rtd
;
2022 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
2023 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
2024 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
2025 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
2026 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
2027 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
2028 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
2031 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
2034 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
2036 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
2038 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
2042 pcm
->private_free
= platform
->driver
->pcm_free
;
2043 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
2049 * snd_soc_codec_volatile_register: Report if a register is volatile.
2051 * @codec: CODEC to query.
2052 * @reg: Register to query.
2054 * Boolean function indiciating if a CODEC register is volatile.
2056 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
,
2059 if (codec
->volatile_register
)
2060 return codec
->volatile_register(codec
, reg
);
2064 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
2067 * snd_soc_new_ac97_codec - initailise AC97 device
2068 * @codec: audio codec
2069 * @ops: AC97 bus operations
2070 * @num: AC97 codec number
2072 * Initialises AC97 codec resources for use by ad-hoc devices only.
2074 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
2075 struct snd_ac97_bus_ops
*ops
, int num
)
2077 mutex_lock(&codec
->mutex
);
2079 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
2080 if (codec
->ac97
== NULL
) {
2081 mutex_unlock(&codec
->mutex
);
2085 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
2086 if (codec
->ac97
->bus
== NULL
) {
2089 mutex_unlock(&codec
->mutex
);
2093 codec
->ac97
->bus
->ops
= ops
;
2094 codec
->ac97
->num
= num
;
2097 * Mark the AC97 device to be created by us. This way we ensure that the
2098 * device will be registered with the device subsystem later on.
2100 codec
->ac97_created
= 1;
2102 mutex_unlock(&codec
->mutex
);
2105 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
2108 * snd_soc_free_ac97_codec - free AC97 codec device
2109 * @codec: audio codec
2111 * Frees AC97 codec device resources.
2113 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
2115 mutex_lock(&codec
->mutex
);
2116 #ifdef CONFIG_SND_SOC_AC97_BUS
2117 soc_unregister_ac97_dai_link(codec
);
2119 kfree(codec
->ac97
->bus
);
2122 codec
->ac97_created
= 0;
2123 mutex_unlock(&codec
->mutex
);
2125 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
2127 unsigned int snd_soc_read(struct snd_soc_codec
*codec
, unsigned int reg
)
2131 ret
= codec
->read(codec
, reg
);
2132 dev_dbg(codec
->dev
, "read %x => %x\n", reg
, ret
);
2133 trace_snd_soc_reg_read(codec
, reg
, ret
);
2137 EXPORT_SYMBOL_GPL(snd_soc_read
);
2139 unsigned int snd_soc_write(struct snd_soc_codec
*codec
,
2140 unsigned int reg
, unsigned int val
)
2142 dev_dbg(codec
->dev
, "write %x = %x\n", reg
, val
);
2143 trace_snd_soc_reg_write(codec
, reg
, val
);
2144 return codec
->write(codec
, reg
, val
);
2146 EXPORT_SYMBOL_GPL(snd_soc_write
);
2149 * snd_soc_update_bits - update codec register bits
2150 * @codec: audio codec
2151 * @reg: codec register
2152 * @mask: register mask
2155 * Writes new register value.
2157 * Returns 1 for change, 0 for no change, or negative error code.
2159 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
2160 unsigned int mask
, unsigned int value
)
2163 unsigned int old
, new;
2166 ret
= snd_soc_read(codec
, reg
);
2171 new = (old
& ~mask
) | value
;
2172 change
= old
!= new;
2174 ret
= snd_soc_write(codec
, reg
, new);
2181 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
2184 * snd_soc_update_bits_locked - update codec register bits
2185 * @codec: audio codec
2186 * @reg: codec register
2187 * @mask: register mask
2190 * Writes new register value, and takes the codec mutex.
2192 * Returns 1 for change else 0.
2194 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
2195 unsigned short reg
, unsigned int mask
,
2200 mutex_lock(&codec
->mutex
);
2201 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
2202 mutex_unlock(&codec
->mutex
);
2206 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
2209 * snd_soc_test_bits - test register for change
2210 * @codec: audio codec
2211 * @reg: codec register
2212 * @mask: register mask
2215 * Tests a register with a new value and checks if the new value is
2216 * different from the old value.
2218 * Returns 1 for change else 0.
2220 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
2221 unsigned int mask
, unsigned int value
)
2224 unsigned int old
, new;
2226 old
= snd_soc_read(codec
, reg
);
2227 new = (old
& ~mask
) | value
;
2228 change
= old
!= new;
2232 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
2235 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2236 * @substream: the pcm substream
2237 * @hw: the hardware parameters
2239 * Sets the substream runtime hardware parameters.
2241 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
2242 const struct snd_pcm_hardware
*hw
)
2244 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
2245 runtime
->hw
.info
= hw
->info
;
2246 runtime
->hw
.formats
= hw
->formats
;
2247 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
2248 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
2249 runtime
->hw
.periods_min
= hw
->periods_min
;
2250 runtime
->hw
.periods_max
= hw
->periods_max
;
2251 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
2252 runtime
->hw
.fifo_size
= hw
->fifo_size
;
2255 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
2258 * snd_soc_cnew - create new control
2259 * @_template: control template
2260 * @data: control private data
2261 * @long_name: control long name
2263 * Create a new mixer control from a template control.
2265 * Returns 0 for success, else error.
2267 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
2268 void *data
, char *long_name
)
2270 struct snd_kcontrol_new
template;
2272 memcpy(&template, _template
, sizeof(template));
2274 template.name
= long_name
;
2277 return snd_ctl_new1(&template, data
);
2279 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
2282 * snd_soc_add_controls - add an array of controls to a codec.
2283 * Convienience function to add a list of controls. Many codecs were
2284 * duplicating this code.
2286 * @codec: codec to add controls to
2287 * @controls: array of controls to add
2288 * @num_controls: number of elements in the array
2290 * Return 0 for success, else error.
2292 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
2293 const struct snd_kcontrol_new
*controls
, int num_controls
)
2295 struct snd_card
*card
= codec
->card
->snd_card
;
2296 char prefixed_name
[44], *name
;
2299 for (i
= 0; i
< num_controls
; i
++) {
2300 const struct snd_kcontrol_new
*control
= &controls
[i
];
2301 if (codec
->name_prefix
) {
2302 snprintf(prefixed_name
, sizeof(prefixed_name
), "%s %s",
2303 codec
->name_prefix
, control
->name
);
2304 name
= prefixed_name
;
2306 name
= control
->name
;
2308 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, name
));
2310 dev_err(codec
->dev
, "%s: Failed to add %s: %d\n",
2311 codec
->name
, name
, err
);
2318 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
2321 * snd_soc_info_enum_double - enumerated double mixer info callback
2322 * @kcontrol: mixer control
2323 * @uinfo: control element information
2325 * Callback to provide information about a double enumerated
2328 * Returns 0 for success.
2330 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
2331 struct snd_ctl_elem_info
*uinfo
)
2333 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2335 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2336 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
2337 uinfo
->value
.enumerated
.items
= e
->max
;
2339 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2340 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2341 strcpy(uinfo
->value
.enumerated
.name
,
2342 e
->texts
[uinfo
->value
.enumerated
.item
]);
2345 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
2348 * snd_soc_get_enum_double - enumerated double mixer get callback
2349 * @kcontrol: mixer control
2350 * @ucontrol: control element information
2352 * Callback to get the value of a double enumerated mixer.
2354 * Returns 0 for success.
2356 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
2357 struct snd_ctl_elem_value
*ucontrol
)
2359 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2360 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2361 unsigned int val
, bitmask
;
2363 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2365 val
= snd_soc_read(codec
, e
->reg
);
2366 ucontrol
->value
.enumerated
.item
[0]
2367 = (val
>> e
->shift_l
) & (bitmask
- 1);
2368 if (e
->shift_l
!= e
->shift_r
)
2369 ucontrol
->value
.enumerated
.item
[1] =
2370 (val
>> e
->shift_r
) & (bitmask
- 1);
2374 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
2377 * snd_soc_put_enum_double - enumerated double mixer put callback
2378 * @kcontrol: mixer control
2379 * @ucontrol: control element information
2381 * Callback to set the value of a double enumerated mixer.
2383 * Returns 0 for success.
2385 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
2386 struct snd_ctl_elem_value
*ucontrol
)
2388 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2389 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2391 unsigned int mask
, bitmask
;
2393 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
2395 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2397 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
2398 mask
= (bitmask
- 1) << e
->shift_l
;
2399 if (e
->shift_l
!= e
->shift_r
) {
2400 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2402 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
2403 mask
|= (bitmask
- 1) << e
->shift_r
;
2406 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2408 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2411 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2412 * @kcontrol: mixer control
2413 * @ucontrol: control element information
2415 * Callback to get the value of a double semi enumerated mixer.
2417 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2418 * used for handling bitfield coded enumeration for example.
2420 * Returns 0 for success.
2422 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2423 struct snd_ctl_elem_value
*ucontrol
)
2425 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2426 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2427 unsigned int reg_val
, val
, mux
;
2429 reg_val
= snd_soc_read(codec
, e
->reg
);
2430 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2431 for (mux
= 0; mux
< e
->max
; mux
++) {
2432 if (val
== e
->values
[mux
])
2435 ucontrol
->value
.enumerated
.item
[0] = mux
;
2436 if (e
->shift_l
!= e
->shift_r
) {
2437 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2438 for (mux
= 0; mux
< e
->max
; mux
++) {
2439 if (val
== e
->values
[mux
])
2442 ucontrol
->value
.enumerated
.item
[1] = mux
;
2447 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2450 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2451 * @kcontrol: mixer control
2452 * @ucontrol: control element information
2454 * Callback to set the value of a double semi enumerated mixer.
2456 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2457 * used for handling bitfield coded enumeration for example.
2459 * Returns 0 for success.
2461 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2462 struct snd_ctl_elem_value
*ucontrol
)
2464 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2465 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2469 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2471 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2472 mask
= e
->mask
<< e
->shift_l
;
2473 if (e
->shift_l
!= e
->shift_r
) {
2474 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2476 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2477 mask
|= e
->mask
<< e
->shift_r
;
2480 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2482 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2485 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2486 * @kcontrol: mixer control
2487 * @uinfo: control element information
2489 * Callback to provide information about an external enumerated
2492 * Returns 0 for success.
2494 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2495 struct snd_ctl_elem_info
*uinfo
)
2497 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2499 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2501 uinfo
->value
.enumerated
.items
= e
->max
;
2503 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2504 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2505 strcpy(uinfo
->value
.enumerated
.name
,
2506 e
->texts
[uinfo
->value
.enumerated
.item
]);
2509 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2512 * snd_soc_info_volsw_ext - external single mixer info callback
2513 * @kcontrol: mixer control
2514 * @uinfo: control element information
2516 * Callback to provide information about a single external mixer control.
2518 * Returns 0 for success.
2520 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2521 struct snd_ctl_elem_info
*uinfo
)
2523 int max
= kcontrol
->private_value
;
2525 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2526 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2528 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2531 uinfo
->value
.integer
.min
= 0;
2532 uinfo
->value
.integer
.max
= max
;
2535 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2538 * snd_soc_info_volsw - single mixer info callback
2539 * @kcontrol: mixer control
2540 * @uinfo: control element information
2542 * Callback to provide information about a single mixer control.
2544 * Returns 0 for success.
2546 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2547 struct snd_ctl_elem_info
*uinfo
)
2549 struct soc_mixer_control
*mc
=
2550 (struct soc_mixer_control
*)kcontrol
->private_value
;
2552 unsigned int shift
= mc
->shift
;
2553 unsigned int rshift
= mc
->rshift
;
2555 if (!mc
->platform_max
)
2556 mc
->platform_max
= mc
->max
;
2557 platform_max
= mc
->platform_max
;
2559 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2560 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2562 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2564 uinfo
->count
= shift
== rshift
? 1 : 2;
2565 uinfo
->value
.integer
.min
= 0;
2566 uinfo
->value
.integer
.max
= platform_max
;
2569 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2572 * snd_soc_get_volsw - single mixer get callback
2573 * @kcontrol: mixer control
2574 * @ucontrol: control element information
2576 * Callback to get the value of a single mixer control.
2578 * Returns 0 for success.
2580 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2581 struct snd_ctl_elem_value
*ucontrol
)
2583 struct soc_mixer_control
*mc
=
2584 (struct soc_mixer_control
*)kcontrol
->private_value
;
2585 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2586 unsigned int reg
= mc
->reg
;
2587 unsigned int shift
= mc
->shift
;
2588 unsigned int rshift
= mc
->rshift
;
2590 unsigned int mask
= (1 << fls(max
)) - 1;
2591 unsigned int invert
= mc
->invert
;
2593 ucontrol
->value
.integer
.value
[0] =
2594 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2595 if (shift
!= rshift
)
2596 ucontrol
->value
.integer
.value
[1] =
2597 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2599 ucontrol
->value
.integer
.value
[0] =
2600 max
- ucontrol
->value
.integer
.value
[0];
2601 if (shift
!= rshift
)
2602 ucontrol
->value
.integer
.value
[1] =
2603 max
- ucontrol
->value
.integer
.value
[1];
2608 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2611 * snd_soc_put_volsw - single mixer put callback
2612 * @kcontrol: mixer control
2613 * @ucontrol: control element information
2615 * Callback to set the value of a single mixer control.
2617 * Returns 0 for success.
2619 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2620 struct snd_ctl_elem_value
*ucontrol
)
2622 struct soc_mixer_control
*mc
=
2623 (struct soc_mixer_control
*)kcontrol
->private_value
;
2624 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2625 unsigned int reg
= mc
->reg
;
2626 unsigned int shift
= mc
->shift
;
2627 unsigned int rshift
= mc
->rshift
;
2629 unsigned int mask
= (1 << fls(max
)) - 1;
2630 unsigned int invert
= mc
->invert
;
2631 unsigned int val
, val2
, val_mask
;
2633 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2636 val_mask
= mask
<< shift
;
2638 if (shift
!= rshift
) {
2639 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2642 val_mask
|= mask
<< rshift
;
2643 val
|= val2
<< rshift
;
2645 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2647 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2650 * snd_soc_info_volsw_2r - double mixer info callback
2651 * @kcontrol: mixer control
2652 * @uinfo: control element information
2654 * Callback to provide information about a double mixer control that
2655 * spans 2 codec registers.
2657 * Returns 0 for success.
2659 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2660 struct snd_ctl_elem_info
*uinfo
)
2662 struct soc_mixer_control
*mc
=
2663 (struct soc_mixer_control
*)kcontrol
->private_value
;
2666 if (!mc
->platform_max
)
2667 mc
->platform_max
= mc
->max
;
2668 platform_max
= mc
->platform_max
;
2670 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2671 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2673 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2676 uinfo
->value
.integer
.min
= 0;
2677 uinfo
->value
.integer
.max
= platform_max
;
2680 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2683 * snd_soc_get_volsw_2r - double mixer get callback
2684 * @kcontrol: mixer control
2685 * @ucontrol: control element information
2687 * Callback to get the value of a double mixer control that spans 2 registers.
2689 * Returns 0 for success.
2691 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2692 struct snd_ctl_elem_value
*ucontrol
)
2694 struct soc_mixer_control
*mc
=
2695 (struct soc_mixer_control
*)kcontrol
->private_value
;
2696 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2697 unsigned int reg
= mc
->reg
;
2698 unsigned int reg2
= mc
->rreg
;
2699 unsigned int shift
= mc
->shift
;
2701 unsigned int mask
= (1 << fls(max
)) - 1;
2702 unsigned int invert
= mc
->invert
;
2704 ucontrol
->value
.integer
.value
[0] =
2705 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2706 ucontrol
->value
.integer
.value
[1] =
2707 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2709 ucontrol
->value
.integer
.value
[0] =
2710 max
- ucontrol
->value
.integer
.value
[0];
2711 ucontrol
->value
.integer
.value
[1] =
2712 max
- ucontrol
->value
.integer
.value
[1];
2717 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2720 * snd_soc_put_volsw_2r - double mixer set callback
2721 * @kcontrol: mixer control
2722 * @ucontrol: control element information
2724 * Callback to set the value of a double mixer control that spans 2 registers.
2726 * Returns 0 for success.
2728 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2729 struct snd_ctl_elem_value
*ucontrol
)
2731 struct soc_mixer_control
*mc
=
2732 (struct soc_mixer_control
*)kcontrol
->private_value
;
2733 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2734 unsigned int reg
= mc
->reg
;
2735 unsigned int reg2
= mc
->rreg
;
2736 unsigned int shift
= mc
->shift
;
2738 unsigned int mask
= (1 << fls(max
)) - 1;
2739 unsigned int invert
= mc
->invert
;
2741 unsigned int val
, val2
, val_mask
;
2743 val_mask
= mask
<< shift
;
2744 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2745 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2753 val2
= val2
<< shift
;
2755 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2759 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2762 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2765 * snd_soc_info_volsw_s8 - signed mixer info callback
2766 * @kcontrol: mixer control
2767 * @uinfo: control element information
2769 * Callback to provide information about a signed mixer control.
2771 * Returns 0 for success.
2773 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2774 struct snd_ctl_elem_info
*uinfo
)
2776 struct soc_mixer_control
*mc
=
2777 (struct soc_mixer_control
*)kcontrol
->private_value
;
2781 if (!mc
->platform_max
)
2782 mc
->platform_max
= mc
->max
;
2783 platform_max
= mc
->platform_max
;
2785 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2787 uinfo
->value
.integer
.min
= 0;
2788 uinfo
->value
.integer
.max
= platform_max
- min
;
2791 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2794 * snd_soc_get_volsw_s8 - signed mixer get callback
2795 * @kcontrol: mixer control
2796 * @ucontrol: control element information
2798 * Callback to get the value of a signed mixer control.
2800 * Returns 0 for success.
2802 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2803 struct snd_ctl_elem_value
*ucontrol
)
2805 struct soc_mixer_control
*mc
=
2806 (struct soc_mixer_control
*)kcontrol
->private_value
;
2807 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2808 unsigned int reg
= mc
->reg
;
2810 int val
= snd_soc_read(codec
, reg
);
2812 ucontrol
->value
.integer
.value
[0] =
2813 ((signed char)(val
& 0xff))-min
;
2814 ucontrol
->value
.integer
.value
[1] =
2815 ((signed char)((val
>> 8) & 0xff))-min
;
2818 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2821 * snd_soc_put_volsw_sgn - signed mixer put callback
2822 * @kcontrol: mixer control
2823 * @ucontrol: control element information
2825 * Callback to set the value of a signed mixer control.
2827 * Returns 0 for success.
2829 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2830 struct snd_ctl_elem_value
*ucontrol
)
2832 struct soc_mixer_control
*mc
=
2833 (struct soc_mixer_control
*)kcontrol
->private_value
;
2834 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2835 unsigned int reg
= mc
->reg
;
2839 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2840 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2842 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2844 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2847 * snd_soc_limit_volume - Set new limit to an existing volume control.
2849 * @codec: where to look for the control
2850 * @name: Name of the control
2851 * @max: new maximum limit
2853 * Return 0 for success, else error.
2855 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2856 const char *name
, int max
)
2858 struct snd_card
*card
= codec
->card
->snd_card
;
2859 struct snd_kcontrol
*kctl
;
2860 struct soc_mixer_control
*mc
;
2864 /* Sanity check for name and max */
2865 if (unlikely(!name
|| max
<= 0))
2868 list_for_each_entry(kctl
, &card
->controls
, list
) {
2869 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2875 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2876 if (max
<= mc
->max
) {
2877 mc
->platform_max
= max
;
2883 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2886 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2887 * mixer info callback
2888 * @kcontrol: mixer control
2889 * @uinfo: control element information
2891 * Returns 0 for success.
2893 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2894 struct snd_ctl_elem_info
*uinfo
)
2896 struct soc_mixer_control
*mc
=
2897 (struct soc_mixer_control
*)kcontrol
->private_value
;
2901 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2903 uinfo
->value
.integer
.min
= 0;
2904 uinfo
->value
.integer
.max
= max
-min
;
2908 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2911 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2912 * mixer get callback
2913 * @kcontrol: mixer control
2914 * @uinfo: control element information
2916 * Returns 0 for success.
2918 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2919 struct snd_ctl_elem_value
*ucontrol
)
2921 struct soc_mixer_control
*mc
=
2922 (struct soc_mixer_control
*)kcontrol
->private_value
;
2923 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2924 unsigned int mask
= (1<<mc
->shift
)-1;
2926 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2927 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2929 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2930 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2933 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2936 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2937 * mixer put callback
2938 * @kcontrol: mixer control
2939 * @uinfo: control element information
2941 * Returns 0 for success.
2943 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2944 struct snd_ctl_elem_value
*ucontrol
)
2946 struct soc_mixer_control
*mc
=
2947 (struct soc_mixer_control
*)kcontrol
->private_value
;
2948 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2949 unsigned int mask
= (1<<mc
->shift
)-1;
2952 unsigned int val
, valr
, oval
, ovalr
;
2954 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2956 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2959 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2960 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2964 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2968 if (ovalr
!= valr
) {
2969 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2976 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2979 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2981 * @clk_id: DAI specific clock ID
2982 * @freq: new clock frequency in Hz
2983 * @dir: new clock direction - input/output.
2985 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2987 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2988 unsigned int freq
, int dir
)
2990 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2991 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2995 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2998 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3000 * @div_id: DAI specific clock divider ID
3001 * @div: new clock divisor.
3003 * Configures the clock dividers. This is used to derive the best DAI bit and
3004 * frame clocks from the system or master clock. It's best to set the DAI bit
3005 * and frame clocks as low as possible to save system power.
3007 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
3008 int div_id
, int div
)
3010 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
3011 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
3015 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
3018 * snd_soc_dai_set_pll - configure DAI PLL.
3020 * @pll_id: DAI specific PLL ID
3021 * @source: DAI specific source for the PLL
3022 * @freq_in: PLL input clock frequency in Hz
3023 * @freq_out: requested PLL output clock frequency in Hz
3025 * Configures and enables PLL to generate output clock based on input clock.
3027 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
3028 unsigned int freq_in
, unsigned int freq_out
)
3030 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
3031 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
3036 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
3039 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3041 * @fmt: SND_SOC_DAIFMT_ format value.
3043 * Configures the DAI hardware format and clocking.
3045 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
3047 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
3048 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
3052 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
3055 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3057 * @tx_mask: bitmask representing active TX slots.
3058 * @rx_mask: bitmask representing active RX slots.
3059 * @slots: Number of slots in use.
3060 * @slot_width: Width in bits for each slot.
3062 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3065 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
3066 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
3068 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
3069 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
3074 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
3077 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3079 * @tx_num: how many TX channels
3080 * @tx_slot: pointer to an array which imply the TX slot number channel
3082 * @rx_num: how many RX channels
3083 * @rx_slot: pointer to an array which imply the RX slot number channel
3086 * configure the relationship between channel number and TDM slot number.
3088 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
3089 unsigned int tx_num
, unsigned int *tx_slot
,
3090 unsigned int rx_num
, unsigned int *rx_slot
)
3092 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
3093 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
3098 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
3101 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3103 * @tristate: tristate enable
3105 * Tristates the DAI so that others can use it.
3107 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
3109 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
3110 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
3114 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
3117 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3119 * @mute: mute enable
3121 * Mutes the DAI DAC.
3123 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
3125 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
3126 return dai
->driver
->ops
->digital_mute(dai
, mute
);
3130 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
3133 * snd_soc_register_card - Register a card with the ASoC core
3135 * @card: Card to register
3138 int snd_soc_register_card(struct snd_soc_card
*card
)
3142 if (!card
->name
|| !card
->dev
)
3145 snd_soc_initialize_card_lists(card
);
3147 soc_init_card_debugfs(card
);
3149 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) *
3150 (card
->num_links
+ card
->num_aux_devs
),
3152 if (card
->rtd
== NULL
)
3154 card
->rtd_aux
= &card
->rtd
[card
->num_links
];
3156 for (i
= 0; i
< card
->num_links
; i
++)
3157 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
3159 INIT_LIST_HEAD(&card
->list
);
3160 card
->instantiated
= 0;
3161 mutex_init(&card
->mutex
);
3163 mutex_lock(&client_mutex
);
3164 list_add(&card
->list
, &card_list
);
3165 snd_soc_instantiate_cards();
3166 mutex_unlock(&client_mutex
);
3168 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
3172 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
3175 * snd_soc_unregister_card - Unregister a card with the ASoC core
3177 * @card: Card to unregister
3180 int snd_soc_unregister_card(struct snd_soc_card
*card
)
3182 if (card
->instantiated
)
3183 soc_cleanup_card_resources(card
);
3184 mutex_lock(&client_mutex
);
3185 list_del(&card
->list
);
3186 mutex_unlock(&client_mutex
);
3187 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
3191 EXPORT_SYMBOL_GPL(snd_soc_unregister_card
);
3194 * Simplify DAI link configuration by removing ".-1" from device names
3195 * and sanitizing names.
3197 static char *fmt_single_name(struct device
*dev
, int *id
)
3199 char *found
, name
[NAME_SIZE
];
3202 if (dev_name(dev
) == NULL
)
3205 strlcpy(name
, dev_name(dev
), NAME_SIZE
);
3207 /* are we a "%s.%d" name (platform and SPI components) */
3208 found
= strstr(name
, dev
->driver
->name
);
3211 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
3213 /* discard ID from name if ID == -1 */
3215 found
[strlen(dev
->driver
->name
)] = '\0';
3219 /* I2C component devices are named "bus-addr" */
3220 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
3221 char tmp
[NAME_SIZE
];
3223 /* create unique ID number from I2C addr and bus */
3224 *id
= ((id1
& 0xffff) << 16) + id2
;
3226 /* sanitize component name for DAI link creation */
3227 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
3228 strlcpy(name
, tmp
, NAME_SIZE
);
3233 return kstrdup(name
, GFP_KERNEL
);
3237 * Simplify DAI link naming for single devices with multiple DAIs by removing
3238 * any ".-1" and using the DAI name (instead of device name).
3240 static inline char *fmt_multiple_name(struct device
*dev
,
3241 struct snd_soc_dai_driver
*dai_drv
)
3243 if (dai_drv
->name
== NULL
) {
3244 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
3249 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
3253 * snd_soc_register_dai - Register a DAI with the ASoC core
3255 * @dai: DAI to register
3257 int snd_soc_register_dai(struct device
*dev
,
3258 struct snd_soc_dai_driver
*dai_drv
)
3260 struct snd_soc_dai
*dai
;
3262 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
3264 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3268 /* create DAI component name */
3269 dai
->name
= fmt_single_name(dev
, &dai
->id
);
3270 if (dai
->name
== NULL
) {
3276 dai
->driver
= dai_drv
;
3277 if (!dai
->driver
->ops
)
3278 dai
->driver
->ops
= &null_dai_ops
;
3280 mutex_lock(&client_mutex
);
3281 list_add(&dai
->list
, &dai_list
);
3282 snd_soc_instantiate_cards();
3283 mutex_unlock(&client_mutex
);
3285 pr_debug("Registered DAI '%s'\n", dai
->name
);
3289 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
3292 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3294 * @dai: DAI to unregister
3296 void snd_soc_unregister_dai(struct device
*dev
)
3298 struct snd_soc_dai
*dai
;
3300 list_for_each_entry(dai
, &dai_list
, list
) {
3301 if (dev
== dai
->dev
)
3307 mutex_lock(&client_mutex
);
3308 list_del(&dai
->list
);
3309 mutex_unlock(&client_mutex
);
3311 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
3315 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
3318 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3320 * @dai: Array of DAIs to register
3321 * @count: Number of DAIs
3323 int snd_soc_register_dais(struct device
*dev
,
3324 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
3326 struct snd_soc_dai
*dai
;
3329 dev_dbg(dev
, "dai register %s #%Zu\n", dev_name(dev
), count
);
3331 for (i
= 0; i
< count
; i
++) {
3333 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
3339 /* create DAI component name */
3340 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
3341 if (dai
->name
== NULL
) {
3348 dai
->driver
= &dai_drv
[i
];
3349 if (dai
->driver
->id
)
3350 dai
->id
= dai
->driver
->id
;
3353 if (!dai
->driver
->ops
)
3354 dai
->driver
->ops
= &null_dai_ops
;
3356 mutex_lock(&client_mutex
);
3357 list_add(&dai
->list
, &dai_list
);
3358 mutex_unlock(&client_mutex
);
3360 pr_debug("Registered DAI '%s'\n", dai
->name
);
3363 mutex_lock(&client_mutex
);
3364 snd_soc_instantiate_cards();
3365 mutex_unlock(&client_mutex
);
3369 for (i
--; i
>= 0; i
--)
3370 snd_soc_unregister_dai(dev
);
3374 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
3377 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3379 * @dai: Array of DAIs to unregister
3380 * @count: Number of DAIs
3382 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
3386 for (i
= 0; i
< count
; i
++)
3387 snd_soc_unregister_dai(dev
);
3389 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
3392 * snd_soc_register_platform - Register a platform with the ASoC core
3394 * @platform: platform to register
3396 int snd_soc_register_platform(struct device
*dev
,
3397 struct snd_soc_platform_driver
*platform_drv
)
3399 struct snd_soc_platform
*platform
;
3401 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
3403 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
3404 if (platform
== NULL
)
3407 /* create platform component name */
3408 platform
->name
= fmt_single_name(dev
, &platform
->id
);
3409 if (platform
->name
== NULL
) {
3414 platform
->dev
= dev
;
3415 platform
->driver
= platform_drv
;
3417 mutex_lock(&client_mutex
);
3418 list_add(&platform
->list
, &platform_list
);
3419 snd_soc_instantiate_cards();
3420 mutex_unlock(&client_mutex
);
3422 pr_debug("Registered platform '%s'\n", platform
->name
);
3426 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3429 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3431 * @platform: platform to unregister
3433 void snd_soc_unregister_platform(struct device
*dev
)
3435 struct snd_soc_platform
*platform
;
3437 list_for_each_entry(platform
, &platform_list
, list
) {
3438 if (dev
== platform
->dev
)
3444 mutex_lock(&client_mutex
);
3445 list_del(&platform
->list
);
3446 mutex_unlock(&client_mutex
);
3448 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3449 kfree(platform
->name
);
3452 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3454 static u64 codec_format_map
[] = {
3455 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3456 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3457 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3458 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3459 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3460 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3461 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3462 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3463 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3464 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3465 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3466 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3467 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3468 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3469 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3470 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3473 /* Fix up the DAI formats for endianness: codecs don't actually see
3474 * the endianness of the data but we're using the CPU format
3475 * definitions which do need to include endianness so we ensure that
3476 * codec DAIs always have both big and little endian variants set.
3478 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3482 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3483 if (stream
->formats
& codec_format_map
[i
])
3484 stream
->formats
|= codec_format_map
[i
];
3488 * snd_soc_register_codec - Register a codec with the ASoC core
3490 * @codec: codec to register
3492 int snd_soc_register_codec(struct device
*dev
,
3493 const struct snd_soc_codec_driver
*codec_drv
,
3494 struct snd_soc_dai_driver
*dai_drv
,
3498 struct snd_soc_codec
*codec
;
3501 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3503 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3507 /* create CODEC component name */
3508 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3509 if (codec
->name
== NULL
) {
3514 if (codec_drv
->compress_type
)
3515 codec
->compress_type
= codec_drv
->compress_type
;
3517 codec
->compress_type
= SND_SOC_FLAT_COMPRESSION
;
3519 codec
->write
= codec_drv
->write
;
3520 codec
->read
= codec_drv
->read
;
3521 codec
->volatile_register
= codec_drv
->volatile_register
;
3522 codec
->readable_register
= codec_drv
->readable_register
;
3523 codec
->dapm
.bias_level
= SND_SOC_BIAS_OFF
;
3524 codec
->dapm
.dev
= dev
;
3525 codec
->dapm
.codec
= codec
;
3526 codec
->dapm
.seq_notifier
= codec_drv
->seq_notifier
;
3528 codec
->driver
= codec_drv
;
3529 codec
->num_dai
= num_dai
;
3530 mutex_init(&codec
->mutex
);
3532 /* allocate CODEC register cache */
3533 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3534 reg_size
= codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
;
3535 codec
->reg_size
= reg_size
;
3536 /* it is necessary to make a copy of the default register cache
3537 * because in the case of using a compression type that requires
3538 * the default register cache to be marked as __devinitconst the
3539 * kernel might have freed the array by the time we initialize
3542 if (codec_drv
->reg_cache_default
) {
3543 codec
->reg_def_copy
= kmemdup(codec_drv
->reg_cache_default
,
3544 reg_size
, GFP_KERNEL
);
3545 if (!codec
->reg_def_copy
) {
3552 if (codec_drv
->reg_access_size
&& codec_drv
->reg_access_default
) {
3553 if (!codec
->volatile_register
)
3554 codec
->volatile_register
= snd_soc_default_volatile_register
;
3555 if (!codec
->readable_register
)
3556 codec
->readable_register
= snd_soc_default_readable_register
;
3559 for (i
= 0; i
< num_dai
; i
++) {
3560 fixup_codec_formats(&dai_drv
[i
].playback
);
3561 fixup_codec_formats(&dai_drv
[i
].capture
);
3564 /* register any DAIs */
3566 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3571 mutex_lock(&client_mutex
);
3572 list_add(&codec
->list
, &codec_list
);
3573 snd_soc_instantiate_cards();
3574 mutex_unlock(&client_mutex
);
3576 pr_debug("Registered codec '%s'\n", codec
->name
);
3580 kfree(codec
->reg_def_copy
);
3581 codec
->reg_def_copy
= NULL
;
3586 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3589 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3591 * @codec: codec to unregister
3593 void snd_soc_unregister_codec(struct device
*dev
)
3595 struct snd_soc_codec
*codec
;
3598 list_for_each_entry(codec
, &codec_list
, list
) {
3599 if (dev
== codec
->dev
)
3606 for (i
= 0; i
< codec
->num_dai
; i
++)
3607 snd_soc_unregister_dai(dev
);
3609 mutex_lock(&client_mutex
);
3610 list_del(&codec
->list
);
3611 mutex_unlock(&client_mutex
);
3613 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3615 snd_soc_cache_exit(codec
);
3616 kfree(codec
->reg_def_copy
);
3620 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3622 static int __init
snd_soc_init(void)
3624 #ifdef CONFIG_DEBUG_FS
3625 snd_soc_debugfs_root
= debugfs_create_dir("asoc", NULL
);
3626 if (IS_ERR(snd_soc_debugfs_root
) || !snd_soc_debugfs_root
) {
3628 "ASoC: Failed to create debugfs directory\n");
3629 snd_soc_debugfs_root
= NULL
;
3632 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root
, NULL
,
3634 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3636 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root
, NULL
,
3638 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
3640 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root
, NULL
,
3641 &platform_list_fops
))
3642 pr_warn("ASoC: Failed to create platform list debugfs file\n");
3645 return platform_driver_register(&soc_driver
);
3647 module_init(snd_soc_init
);
3649 static void __exit
snd_soc_exit(void)
3651 #ifdef CONFIG_DEBUG_FS
3652 debugfs_remove_recursive(snd_soc_debugfs_root
);
3654 platform_driver_unregister(&soc_driver
);
3656 module_exit(snd_soc_exit
);
3658 /* Module information */
3659 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3660 MODULE_DESCRIPTION("ALSA SoC Core");
3661 MODULE_LICENSE("GPL");
3662 MODULE_ALIAS("platform:soc-audio");