2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dapm.h>
40 #include <sound/initval.h>
44 static DEFINE_MUTEX(pcm_mutex
);
45 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
47 #ifdef CONFIG_DEBUG_FS
48 static struct dentry
*debugfs_root
;
51 static DEFINE_MUTEX(client_mutex
);
52 static LIST_HEAD(card_list
);
53 static LIST_HEAD(dai_list
);
54 static LIST_HEAD(platform_list
);
55 static LIST_HEAD(codec_list
);
57 static int snd_soc_register_card(struct snd_soc_card
*card
);
58 static int snd_soc_unregister_card(struct snd_soc_card
*card
);
59 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
);
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
66 static int pmdown_time
= 5000;
67 module_param(pmdown_time
, int, 0);
68 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
71 * This function forces any delayed work to be queued and run.
73 static int run_delayed_work(struct delayed_work
*dwork
)
77 /* cancel any work waiting to be queued. */
78 ret
= cancel_delayed_work(dwork
);
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
83 schedule_delayed_work(dwork
, 0);
84 flush_scheduled_work();
89 /* codec register dump */
90 static ssize_t
soc_codec_reg_show(struct snd_soc_codec
*codec
, char *buf
)
92 int ret
, i
, step
= 1, count
= 0;
94 if (!codec
->driver
->reg_cache_size
)
97 if (codec
->driver
->reg_cache_step
)
98 step
= codec
->driver
->reg_cache_step
;
100 count
+= sprintf(buf
, "%s registers\n", codec
->name
);
101 for (i
= 0; i
< codec
->driver
->reg_cache_size
; i
+= step
) {
102 if (codec
->driver
->readable_register
&& !codec
->driver
->readable_register(i
))
105 count
+= sprintf(buf
+ count
, "%2x: ", i
);
106 if (count
>= PAGE_SIZE
- 1)
109 if (codec
->driver
->display_register
) {
110 count
+= codec
->driver
->display_register(codec
, buf
+ count
,
111 PAGE_SIZE
- count
, i
);
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
117 ret
= codec
->driver
->read(codec
, i
);
119 count
+= snprintf(buf
+ count
,
123 count
+= snprintf(buf
+ count
,
125 "<no data: %d>", ret
);
128 if (count
>= PAGE_SIZE
- 1)
131 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
132 if (count
>= PAGE_SIZE
- 1)
136 /* Truncate count; min() would cause a warning */
137 if (count
>= PAGE_SIZE
)
138 count
= PAGE_SIZE
- 1;
142 static ssize_t
codec_reg_show(struct device
*dev
,
143 struct device_attribute
*attr
, char *buf
)
145 struct snd_soc_pcm_runtime
*rtd
=
146 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
148 return soc_codec_reg_show(rtd
->codec
, buf
);
151 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
153 static ssize_t
pmdown_time_show(struct device
*dev
,
154 struct device_attribute
*attr
, char *buf
)
156 struct snd_soc_pcm_runtime
*rtd
=
157 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
159 return sprintf(buf
, "%ld\n", rtd
->pmdown_time
);
162 static ssize_t
pmdown_time_set(struct device
*dev
,
163 struct device_attribute
*attr
,
164 const char *buf
, size_t count
)
166 struct snd_soc_pcm_runtime
*rtd
=
167 container_of(dev
, struct snd_soc_pcm_runtime
, dev
);
169 strict_strtol(buf
, 10, &rtd
->pmdown_time
);
174 static DEVICE_ATTR(pmdown_time
, 0644, pmdown_time_show
, pmdown_time_set
);
176 #ifdef CONFIG_DEBUG_FS
177 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
179 file
->private_data
= inode
->i_private
;
183 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
184 size_t count
, loff_t
*ppos
)
187 struct snd_soc_codec
*codec
= file
->private_data
;
188 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
191 ret
= soc_codec_reg_show(codec
, buf
);
193 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
198 static ssize_t
codec_reg_write_file(struct file
*file
,
199 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
204 unsigned long reg
, value
;
206 struct snd_soc_codec
*codec
= file
->private_data
;
208 buf_size
= min(count
, (sizeof(buf
)-1));
209 if (copy_from_user(buf
, user_buf
, buf_size
))
213 if (codec
->driver
->reg_cache_step
)
214 step
= codec
->driver
->reg_cache_step
;
216 while (*start
== ' ')
218 reg
= simple_strtoul(start
, &start
, 16);
219 if ((reg
>= codec
->driver
->reg_cache_size
) || (reg
% step
))
221 while (*start
== ' ')
223 if (strict_strtoul(start
, 16, &value
))
225 codec
->driver
->write(codec
, reg
, value
);
229 static const struct file_operations codec_reg_fops
= {
230 .open
= codec_reg_open_file
,
231 .read
= codec_reg_read_file
,
232 .write
= codec_reg_write_file
,
235 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
237 codec
->debugfs_codec_root
= debugfs_create_dir(codec
->name
,
239 if (!codec
->debugfs_codec_root
) {
241 "ASoC: Failed to create codec debugfs directory\n");
245 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
246 codec
->debugfs_codec_root
,
247 codec
, &codec_reg_fops
);
248 if (!codec
->debugfs_reg
)
250 "ASoC: Failed to create codec register debugfs file\n");
252 codec
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0744,
253 codec
->debugfs_codec_root
,
255 if (!codec
->debugfs_pop_time
)
257 "Failed to create pop time debugfs file\n");
259 codec
->debugfs_dapm
= debugfs_create_dir("dapm",
260 codec
->debugfs_codec_root
);
261 if (!codec
->debugfs_dapm
)
263 "Failed to create DAPM debugfs directory\n");
265 snd_soc_dapm_debugfs_init(codec
);
268 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
270 debugfs_remove_recursive(codec
->debugfs_codec_root
);
275 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
279 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
284 #ifdef CONFIG_SND_SOC_AC97_BUS
285 /* unregister ac97 codec */
286 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
288 if (codec
->ac97
->dev
.bus
)
289 device_unregister(&codec
->ac97
->dev
);
293 /* stop no dev release warning */
294 static void soc_ac97_device_release(struct device
*dev
){}
296 /* register ac97 codec to bus */
297 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
301 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
302 codec
->ac97
->dev
.parent
= codec
->card
->dev
;
303 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
305 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
306 codec
->card
->snd_card
->number
, 0, codec
->name
);
307 err
= device_register(&codec
->ac97
->dev
);
309 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
310 codec
->ac97
->dev
.bus
= NULL
;
317 static int soc_pcm_apply_symmetry(struct snd_pcm_substream
*substream
)
319 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
320 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
321 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
324 if (codec_dai
->driver
->symmetric_rates
|| cpu_dai
->driver
->symmetric_rates
||
325 rtd
->dai_link
->symmetric_rates
) {
326 dev_dbg(&rtd
->dev
, "Symmetry forces %dHz rate\n",
329 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
330 SNDRV_PCM_HW_PARAM_RATE
,
335 "Unable to apply rate symmetry constraint: %d\n", ret
);
344 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
345 * then initialized and any private data can be allocated. This also calls
346 * startup for the cpu DAI, platform, machine and codec DAI.
348 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
350 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
351 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
352 struct snd_soc_platform
*platform
= rtd
->platform
;
353 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
354 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
355 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
356 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
359 mutex_lock(&pcm_mutex
);
361 /* startup the audio subsystem */
362 if (cpu_dai
->driver
->ops
->startup
) {
363 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
365 printk(KERN_ERR
"asoc: can't open interface %s\n",
371 if (platform
->driver
->ops
->open
) {
372 ret
= platform
->driver
->ops
->open(substream
);
374 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
379 if (codec_dai
->driver
->ops
->startup
) {
380 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
382 printk(KERN_ERR
"asoc: can't open codec %s\n",
388 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
389 ret
= rtd
->dai_link
->ops
->startup(substream
);
391 printk(KERN_ERR
"asoc: %s startup failed\n", rtd
->dai_link
->name
);
396 /* Check that the codec and cpu DAI's are compatible */
397 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
398 runtime
->hw
.rate_min
=
399 max(codec_dai_drv
->playback
.rate_min
,
400 cpu_dai_drv
->playback
.rate_min
);
401 runtime
->hw
.rate_max
=
402 min(codec_dai_drv
->playback
.rate_max
,
403 cpu_dai_drv
->playback
.rate_max
);
404 runtime
->hw
.channels_min
=
405 max(codec_dai_drv
->playback
.channels_min
,
406 cpu_dai_drv
->playback
.channels_min
);
407 runtime
->hw
.channels_max
=
408 min(codec_dai_drv
->playback
.channels_max
,
409 cpu_dai_drv
->playback
.channels_max
);
410 runtime
->hw
.formats
=
411 codec_dai_drv
->playback
.formats
& cpu_dai_drv
->playback
.formats
;
413 codec_dai_drv
->playback
.rates
& cpu_dai_drv
->playback
.rates
;
414 if (codec_dai_drv
->playback
.rates
415 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
416 runtime
->hw
.rates
|= cpu_dai_drv
->playback
.rates
;
417 if (cpu_dai_drv
->playback
.rates
418 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
419 runtime
->hw
.rates
|= codec_dai_drv
->playback
.rates
;
421 runtime
->hw
.rate_min
=
422 max(codec_dai_drv
->capture
.rate_min
,
423 cpu_dai_drv
->capture
.rate_min
);
424 runtime
->hw
.rate_max
=
425 min(codec_dai_drv
->capture
.rate_max
,
426 cpu_dai_drv
->capture
.rate_max
);
427 runtime
->hw
.channels_min
=
428 max(codec_dai_drv
->capture
.channels_min
,
429 cpu_dai_drv
->capture
.channels_min
);
430 runtime
->hw
.channels_max
=
431 min(codec_dai_drv
->capture
.channels_max
,
432 cpu_dai_drv
->capture
.channels_max
);
433 runtime
->hw
.formats
=
434 codec_dai_drv
->capture
.formats
& cpu_dai_drv
->capture
.formats
;
436 codec_dai_drv
->capture
.rates
& cpu_dai_drv
->capture
.rates
;
437 if (codec_dai_drv
->capture
.rates
438 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
439 runtime
->hw
.rates
|= cpu_dai_drv
->capture
.rates
;
440 if (cpu_dai_drv
->capture
.rates
441 & (SNDRV_PCM_RATE_KNOT
| SNDRV_PCM_RATE_CONTINUOUS
))
442 runtime
->hw
.rates
|= codec_dai_drv
->capture
.rates
;
445 snd_pcm_limit_hw_rates(runtime
);
446 if (!runtime
->hw
.rates
) {
447 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
448 codec_dai
->name
, cpu_dai
->name
);
451 if (!runtime
->hw
.formats
) {
452 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
453 codec_dai
->name
, cpu_dai
->name
);
456 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
457 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
458 codec_dai
->name
, cpu_dai
->name
);
462 /* Symmetry only applies if we've already got an active stream. */
463 if (cpu_dai
->active
|| codec_dai
->active
) {
464 ret
= soc_pcm_apply_symmetry(substream
);
469 pr_debug("asoc: %s <-> %s info:\n",
470 codec_dai
->name
, cpu_dai
->name
);
471 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
472 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
473 runtime
->hw
.channels_max
);
474 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
475 runtime
->hw
.rate_max
);
477 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
478 cpu_dai
->playback_active
++;
479 codec_dai
->playback_active
++;
481 cpu_dai
->capture_active
++;
482 codec_dai
->capture_active
++;
486 rtd
->codec
->active
++;
487 mutex_unlock(&pcm_mutex
);
491 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
492 rtd
->dai_link
->ops
->shutdown(substream
);
495 if (codec_dai
->driver
->ops
->shutdown
)
496 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
499 if (platform
->driver
->ops
->close
)
500 platform
->driver
->ops
->close(substream
);
503 if (cpu_dai
->driver
->ops
->shutdown
)
504 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
506 mutex_unlock(&pcm_mutex
);
511 * Power down the audio subsystem pmdown_time msecs after close is called.
512 * This is to ensure there are no pops or clicks in between any music tracks
513 * due to DAPM power cycling.
515 static void close_delayed_work(struct work_struct
*work
)
517 struct snd_soc_pcm_runtime
*rtd
=
518 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
519 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
521 mutex_lock(&pcm_mutex
);
523 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
524 codec_dai
->driver
->playback
.stream_name
,
525 codec_dai
->playback_active
? "active" : "inactive",
526 codec_dai
->pop_wait
? "yes" : "no");
528 /* are we waiting on this codec DAI stream */
529 if (codec_dai
->pop_wait
== 1) {
530 codec_dai
->pop_wait
= 0;
531 snd_soc_dapm_stream_event(rtd
,
532 codec_dai
->driver
->playback
.stream_name
,
533 SND_SOC_DAPM_STREAM_STOP
);
536 mutex_unlock(&pcm_mutex
);
540 * Called by ALSA when a PCM substream is closed. Private data can be
541 * freed here. The cpu DAI, codec DAI, machine and platform are also
544 static int soc_codec_close(struct snd_pcm_substream
*substream
)
546 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
547 struct snd_soc_platform
*platform
= rtd
->platform
;
548 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
549 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
550 struct snd_soc_codec
*codec
= rtd
->codec
;
552 mutex_lock(&pcm_mutex
);
554 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
555 cpu_dai
->playback_active
--;
556 codec_dai
->playback_active
--;
558 cpu_dai
->capture_active
--;
559 codec_dai
->capture_active
--;
566 /* Muting the DAC suppresses artifacts caused during digital
567 * shutdown, for example from stopping clocks.
569 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
570 snd_soc_dai_digital_mute(codec_dai
, 1);
572 if (cpu_dai
->driver
->ops
->shutdown
)
573 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
575 if (codec_dai
->driver
->ops
->shutdown
)
576 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
578 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
579 rtd
->dai_link
->ops
->shutdown(substream
);
581 if (platform
->driver
->ops
->close
)
582 platform
->driver
->ops
->close(substream
);
583 cpu_dai
->runtime
= NULL
;
585 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
586 /* start delayed pop wq here for playback streams */
587 codec_dai
->pop_wait
= 1;
588 schedule_delayed_work(&rtd
->delayed_work
,
589 msecs_to_jiffies(rtd
->pmdown_time
));
591 /* capture streams can be powered down now */
592 snd_soc_dapm_stream_event(rtd
,
593 codec_dai
->driver
->capture
.stream_name
,
594 SND_SOC_DAPM_STREAM_STOP
);
597 mutex_unlock(&pcm_mutex
);
602 * Called by ALSA when the PCM substream is prepared, can set format, sample
603 * rate, etc. This function is non atomic and can be called multiple times,
604 * it can refer to the runtime info.
606 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
608 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
609 struct snd_soc_platform
*platform
= rtd
->platform
;
610 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
611 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
614 mutex_lock(&pcm_mutex
);
616 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
617 ret
= rtd
->dai_link
->ops
->prepare(substream
);
619 printk(KERN_ERR
"asoc: machine prepare error\n");
624 if (platform
->driver
->ops
->prepare
) {
625 ret
= platform
->driver
->ops
->prepare(substream
);
627 printk(KERN_ERR
"asoc: platform prepare error\n");
632 if (codec_dai
->driver
->ops
->prepare
) {
633 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
635 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
640 if (cpu_dai
->driver
->ops
->prepare
) {
641 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
643 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
648 /* cancel any delayed stream shutdown that is pending */
649 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
650 codec_dai
->pop_wait
) {
651 codec_dai
->pop_wait
= 0;
652 cancel_delayed_work(&rtd
->delayed_work
);
655 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
656 snd_soc_dapm_stream_event(rtd
,
657 codec_dai
->driver
->playback
.stream_name
,
658 SND_SOC_DAPM_STREAM_START
);
660 snd_soc_dapm_stream_event(rtd
,
661 codec_dai
->driver
->capture
.stream_name
,
662 SND_SOC_DAPM_STREAM_START
);
664 snd_soc_dai_digital_mute(codec_dai
, 0);
667 mutex_unlock(&pcm_mutex
);
672 * Called by ALSA when the hardware params are set by application. This
673 * function can also be called multiple times and can allocate buffers
674 * (using snd_pcm_lib_* ). It's non-atomic.
676 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
677 struct snd_pcm_hw_params
*params
)
679 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
680 struct snd_soc_platform
*platform
= rtd
->platform
;
681 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
682 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
685 mutex_lock(&pcm_mutex
);
687 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
688 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
690 printk(KERN_ERR
"asoc: machine hw_params failed\n");
695 if (codec_dai
->driver
->ops
->hw_params
) {
696 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
698 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
704 if (cpu_dai
->driver
->ops
->hw_params
) {
705 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
707 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
713 if (platform
->driver
->ops
->hw_params
) {
714 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
716 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
722 rtd
->rate
= params_rate(params
);
725 mutex_unlock(&pcm_mutex
);
729 if (cpu_dai
->driver
->ops
->hw_free
)
730 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
733 if (codec_dai
->driver
->ops
->hw_free
)
734 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
737 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
738 rtd
->dai_link
->ops
->hw_free(substream
);
740 mutex_unlock(&pcm_mutex
);
745 * Free's resources allocated by hw_params, can be called multiple times
747 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
749 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
750 struct snd_soc_platform
*platform
= rtd
->platform
;
751 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
752 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
753 struct snd_soc_codec
*codec
= rtd
->codec
;
755 mutex_lock(&pcm_mutex
);
757 /* apply codec digital mute */
759 snd_soc_dai_digital_mute(codec_dai
, 1);
761 /* free any machine hw params */
762 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
763 rtd
->dai_link
->ops
->hw_free(substream
);
765 /* free any DMA resources */
766 if (platform
->driver
->ops
->hw_free
)
767 platform
->driver
->ops
->hw_free(substream
);
769 /* now free hw params for the DAI's */
770 if (codec_dai
->driver
->ops
->hw_free
)
771 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
773 if (cpu_dai
->driver
->ops
->hw_free
)
774 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
776 mutex_unlock(&pcm_mutex
);
780 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
782 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
783 struct snd_soc_platform
*platform
= rtd
->platform
;
784 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
785 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
788 if (codec_dai
->driver
->ops
->trigger
) {
789 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
794 if (platform
->driver
->ops
->trigger
) {
795 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
800 if (cpu_dai
->driver
->ops
->trigger
) {
801 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
809 * soc level wrapper for pointer callback
810 * If cpu_dai, codec_dai, platform driver has the delay callback, than
811 * the runtime->delay will be updated accordingly.
813 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
815 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
816 struct snd_soc_platform
*platform
= rtd
->platform
;
817 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
818 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
819 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
820 snd_pcm_uframes_t offset
= 0;
821 snd_pcm_sframes_t delay
= 0;
823 if (platform
->driver
->ops
->pointer
)
824 offset
= platform
->driver
->ops
->pointer(substream
);
826 if (cpu_dai
->driver
->ops
->delay
)
827 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
829 if (codec_dai
->driver
->ops
->delay
)
830 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
832 if (platform
->driver
->delay
)
833 delay
+= platform
->driver
->delay(substream
, codec_dai
);
835 runtime
->delay
= delay
;
840 /* ASoC PCM operations */
841 static struct snd_pcm_ops soc_pcm_ops
= {
842 .open
= soc_pcm_open
,
843 .close
= soc_codec_close
,
844 .hw_params
= soc_pcm_hw_params
,
845 .hw_free
= soc_pcm_hw_free
,
846 .prepare
= soc_pcm_prepare
,
847 .trigger
= soc_pcm_trigger
,
848 .pointer
= soc_pcm_pointer
,
852 /* powers down audio subsystem for suspend */
853 static int soc_suspend(struct device
*dev
)
855 struct platform_device
*pdev
= to_platform_device(dev
);
856 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
859 /* If the initialization of this soc device failed, there is no codec
860 * associated with it. Just bail out in this case.
862 if (list_empty(&card
->codec_dev_list
))
865 /* Due to the resume being scheduled into a workqueue we could
866 * suspend before that's finished - wait for it to complete.
868 snd_power_lock(card
->snd_card
);
869 snd_power_wait(card
->snd_card
, SNDRV_CTL_POWER_D0
);
870 snd_power_unlock(card
->snd_card
);
872 /* we're going to block userspace touching us until resume completes */
873 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D3hot
);
875 /* mute any active DAC's */
876 for (i
= 0; i
< card
->num_rtd
; i
++) {
877 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
878 struct snd_soc_dai_driver
*drv
= dai
->driver
;
880 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
883 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
884 drv
->ops
->digital_mute(dai
, 1);
887 /* suspend all pcms */
888 for (i
= 0; i
< card
->num_rtd
; i
++) {
889 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
892 snd_pcm_suspend_all(card
->rtd
[i
].pcm
);
895 if (card
->suspend_pre
)
896 card
->suspend_pre(pdev
, PMSG_SUSPEND
);
898 for (i
= 0; i
< card
->num_rtd
; i
++) {
899 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
900 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
902 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
905 if (cpu_dai
->driver
->suspend
&& !cpu_dai
->driver
->ac97_control
)
906 cpu_dai
->driver
->suspend(cpu_dai
);
907 if (platform
->driver
->suspend
&& !platform
->suspended
) {
908 platform
->driver
->suspend(cpu_dai
);
909 platform
->suspended
= 1;
913 /* close any waiting streams and save state */
914 for (i
= 0; i
< card
->num_rtd
; i
++) {
915 run_delayed_work(&card
->rtd
[i
].delayed_work
);
916 card
->rtd
[i
].codec
->suspend_bias_level
= card
->rtd
[i
].codec
->bias_level
;
919 for (i
= 0; i
< card
->num_rtd
; i
++) {
920 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
922 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
925 if (driver
->playback
.stream_name
!= NULL
)
926 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
927 SND_SOC_DAPM_STREAM_SUSPEND
);
929 if (driver
->capture
.stream_name
!= NULL
)
930 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
931 SND_SOC_DAPM_STREAM_SUSPEND
);
934 /* suspend all CODECs */
935 for (i
= 0; i
< card
->num_rtd
; i
++) {
936 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
937 /* If there are paths active then the CODEC will be held with
938 * bias _ON and should not be suspended. */
939 if (!codec
->suspended
&& codec
->driver
->suspend
) {
940 switch (codec
->bias_level
) {
941 case SND_SOC_BIAS_STANDBY
:
942 case SND_SOC_BIAS_OFF
:
943 codec
->driver
->suspend(codec
, PMSG_SUSPEND
);
944 codec
->suspended
= 1;
947 dev_dbg(codec
->dev
, "CODEC is on over suspend\n");
953 for (i
= 0; i
< card
->num_rtd
; i
++) {
954 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
956 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
959 if (cpu_dai
->driver
->suspend
&& cpu_dai
->driver
->ac97_control
)
960 cpu_dai
->driver
->suspend(cpu_dai
);
963 if (card
->suspend_post
)
964 card
->suspend_post(pdev
, PMSG_SUSPEND
);
969 /* deferred resume work, so resume can complete before we finished
970 * setting our codec back up, which can be very slow on I2C
972 static void soc_resume_deferred(struct work_struct
*work
)
974 struct snd_soc_card
*card
=
975 container_of(work
, struct snd_soc_card
, deferred_resume_work
);
976 struct platform_device
*pdev
= to_platform_device(card
->dev
);
979 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
980 * so userspace apps are blocked from touching us
983 dev_dbg(card
->dev
, "starting resume work\n");
985 /* Bring us up into D2 so that DAPM starts enabling things */
986 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D2
);
988 if (card
->resume_pre
)
989 card
->resume_pre(pdev
);
991 /* resume AC97 DAIs */
992 for (i
= 0; i
< card
->num_rtd
; i
++) {
993 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
995 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
998 if (cpu_dai
->driver
->resume
&& cpu_dai
->driver
->ac97_control
)
999 cpu_dai
->driver
->resume(cpu_dai
);
1002 for (i
= 0; i
< card
->num_rtd
; i
++) {
1003 struct snd_soc_codec
*codec
= card
->rtd
[i
].codec
;
1004 /* If the CODEC was idle over suspend then it will have been
1005 * left with bias OFF or STANDBY and suspended so we must now
1006 * resume. Otherwise the suspend was suppressed.
1008 if (codec
->driver
->resume
&& codec
->suspended
) {
1009 switch (codec
->bias_level
) {
1010 case SND_SOC_BIAS_STANDBY
:
1011 case SND_SOC_BIAS_OFF
:
1012 codec
->driver
->resume(codec
);
1013 codec
->suspended
= 0;
1016 dev_dbg(codec
->dev
, "CODEC was on over suspend\n");
1022 for (i
= 0; i
< card
->num_rtd
; i
++) {
1023 struct snd_soc_dai_driver
*driver
= card
->rtd
[i
].codec_dai
->driver
;
1025 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1028 if (driver
->playback
.stream_name
!= NULL
)
1029 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->playback
.stream_name
,
1030 SND_SOC_DAPM_STREAM_RESUME
);
1032 if (driver
->capture
.stream_name
!= NULL
)
1033 snd_soc_dapm_stream_event(&card
->rtd
[i
], driver
->capture
.stream_name
,
1034 SND_SOC_DAPM_STREAM_RESUME
);
1037 /* unmute any active DACs */
1038 for (i
= 0; i
< card
->num_rtd
; i
++) {
1039 struct snd_soc_dai
*dai
= card
->rtd
[i
].codec_dai
;
1040 struct snd_soc_dai_driver
*drv
= dai
->driver
;
1042 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1045 if (drv
->ops
->digital_mute
&& dai
->playback_active
)
1046 drv
->ops
->digital_mute(dai
, 0);
1049 for (i
= 0; i
< card
->num_rtd
; i
++) {
1050 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1051 struct snd_soc_platform
*platform
= card
->rtd
[i
].platform
;
1053 if (card
->rtd
[i
].dai_link
->ignore_suspend
)
1056 if (cpu_dai
->driver
->resume
&& !cpu_dai
->driver
->ac97_control
)
1057 cpu_dai
->driver
->resume(cpu_dai
);
1058 if (platform
->driver
->resume
&& platform
->suspended
) {
1059 platform
->driver
->resume(cpu_dai
);
1060 platform
->suspended
= 0;
1064 if (card
->resume_post
)
1065 card
->resume_post(pdev
);
1067 dev_dbg(card
->dev
, "resume work completed\n");
1069 /* userspace can access us now we are back as we were before */
1070 snd_power_change_state(card
->snd_card
, SNDRV_CTL_POWER_D0
);
1073 /* powers up audio subsystem after a suspend */
1074 static int soc_resume(struct device
*dev
)
1076 struct platform_device
*pdev
= to_platform_device(dev
);
1077 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1080 /* AC97 devices might have other drivers hanging off them so
1081 * need to resume immediately. Other drivers don't have that
1082 * problem and may take a substantial amount of time to resume
1083 * due to I/O costs and anti-pop so handle them out of line.
1085 for (i
= 0; i
< card
->num_rtd
; i
++) {
1086 struct snd_soc_dai
*cpu_dai
= card
->rtd
[i
].cpu_dai
;
1087 if (cpu_dai
->driver
->ac97_control
) {
1088 dev_dbg(dev
, "Resuming AC97 immediately\n");
1089 soc_resume_deferred(&card
->deferred_resume_work
);
1091 dev_dbg(dev
, "Scheduling resume work\n");
1092 if (!schedule_work(&card
->deferred_resume_work
))
1093 dev_err(dev
, "resume work item may be lost\n");
1100 #define soc_suspend NULL
1101 #define soc_resume NULL
1104 static struct snd_soc_dai_ops null_dai_ops
= {
1107 static int soc_bind_dai_link(struct snd_soc_card
*card
, int num
)
1109 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1110 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1111 struct snd_soc_codec
*codec
;
1112 struct snd_soc_platform
*platform
;
1113 struct snd_soc_dai
*codec_dai
, *cpu_dai
;
1117 dev_dbg(card
->dev
, "binding %s at idx %d\n", dai_link
->name
, num
);
1119 /* do we already have the CPU DAI for this link ? */
1123 /* no, then find CPU DAI from registered DAIs*/
1124 list_for_each_entry(cpu_dai
, &dai_list
, list
) {
1125 if (!strcmp(cpu_dai
->name
, dai_link
->cpu_dai_name
)) {
1127 if (!try_module_get(cpu_dai
->dev
->driver
->owner
))
1130 rtd
->cpu_dai
= cpu_dai
;
1134 dev_dbg(card
->dev
, "CPU DAI %s not registered\n",
1135 dai_link
->cpu_dai_name
);
1138 /* do we already have the CODEC for this link ? */
1143 /* no, then find CODEC from registered CODECs*/
1144 list_for_each_entry(codec
, &codec_list
, list
) {
1145 if (!strcmp(codec
->name
, dai_link
->codec_name
)) {
1148 if (!try_module_get(codec
->dev
->driver
->owner
))
1151 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1152 list_for_each_entry(codec_dai
, &dai_list
, list
) {
1153 if (codec
->dev
== codec_dai
->dev
&&
1154 !strcmp(codec_dai
->name
, dai_link
->codec_dai_name
)) {
1155 rtd
->codec_dai
= codec_dai
;
1159 dev_dbg(card
->dev
, "CODEC DAI %s not registered\n",
1160 dai_link
->codec_dai_name
);
1165 dev_dbg(card
->dev
, "CODEC %s not registered\n",
1166 dai_link
->codec_name
);
1169 /* do we already have the CODEC DAI for this link ? */
1170 if (rtd
->platform
) {
1173 /* no, then find CPU DAI from registered DAIs*/
1174 list_for_each_entry(platform
, &platform_list
, list
) {
1175 if (!strcmp(platform
->name
, dai_link
->platform_name
)) {
1177 if (!try_module_get(platform
->dev
->driver
->owner
))
1180 rtd
->platform
= platform
;
1185 dev_dbg(card
->dev
, "platform %s not registered\n",
1186 dai_link
->platform_name
);
1190 /* mark rtd as complete if we found all 4 of our client devices */
1191 if (rtd
->codec
&& rtd
->codec_dai
&& rtd
->platform
&& rtd
->cpu_dai
) {
1198 static void soc_remove_dai_link(struct snd_soc_card
*card
, int num
)
1200 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1201 struct snd_soc_codec
*codec
= rtd
->codec
;
1202 struct snd_soc_platform
*platform
= rtd
->platform
;
1203 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1206 /* unregister the rtd device */
1207 if (rtd
->dev_registered
) {
1208 device_remove_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1209 device_unregister(&rtd
->dev
);
1210 rtd
->dev_registered
= 0;
1213 /* remove the CODEC DAI */
1214 if (codec_dai
&& codec_dai
->probed
) {
1215 if (codec_dai
->driver
->remove
) {
1216 err
= codec_dai
->driver
->remove(codec_dai
);
1218 printk(KERN_ERR
"asoc: failed to remove %s\n", codec_dai
->name
);
1220 codec_dai
->probed
= 0;
1221 list_del(&codec_dai
->card_list
);
1224 /* remove the platform */
1225 if (platform
&& platform
->probed
) {
1226 if (platform
->driver
->remove
) {
1227 err
= platform
->driver
->remove(platform
);
1229 printk(KERN_ERR
"asoc: failed to remove %s\n", platform
->name
);
1231 platform
->probed
= 0;
1232 list_del(&platform
->card_list
);
1233 module_put(platform
->dev
->driver
->owner
);
1236 /* remove the CODEC */
1237 if (codec
&& codec
->probed
) {
1238 if (codec
->driver
->remove
) {
1239 err
= codec
->driver
->remove(codec
);
1241 printk(KERN_ERR
"asoc: failed to remove %s\n", codec
->name
);
1244 /* Make sure all DAPM widgets are freed */
1245 snd_soc_dapm_free(codec
);
1247 soc_cleanup_codec_debugfs(codec
);
1248 device_remove_file(&rtd
->dev
, &dev_attr_codec_reg
);
1250 list_del(&codec
->card_list
);
1251 module_put(codec
->dev
->driver
->owner
);
1254 /* remove the cpu_dai */
1255 if (cpu_dai
&& cpu_dai
->probed
) {
1256 if (cpu_dai
->driver
->remove
) {
1257 err
= cpu_dai
->driver
->remove(cpu_dai
);
1259 printk(KERN_ERR
"asoc: failed to remove %s\n", cpu_dai
->name
);
1261 cpu_dai
->probed
= 0;
1262 list_del(&cpu_dai
->card_list
);
1263 module_put(cpu_dai
->dev
->driver
->owner
);
1267 static void rtd_release(struct device
*dev
) {}
1269 static int soc_probe_dai_link(struct snd_soc_card
*card
, int num
)
1271 struct snd_soc_dai_link
*dai_link
= &card
->dai_link
[num
];
1272 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[num
];
1273 struct snd_soc_codec
*codec
= rtd
->codec
;
1274 struct snd_soc_platform
*platform
= rtd
->platform
;
1275 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
, *cpu_dai
= rtd
->cpu_dai
;
1278 dev_dbg(card
->dev
, "probe %s dai link %d\n", card
->name
, num
);
1280 /* config components */
1281 codec_dai
->codec
= codec
;
1283 cpu_dai
->platform
= platform
;
1285 rtd
->dev
.parent
= card
->dev
;
1286 codec_dai
->card
= card
;
1287 cpu_dai
->card
= card
;
1289 /* set default power off timeout */
1290 rtd
->pmdown_time
= pmdown_time
;
1292 /* probe the cpu_dai */
1293 if (!cpu_dai
->probed
) {
1294 if (cpu_dai
->driver
->probe
) {
1295 ret
= cpu_dai
->driver
->probe(cpu_dai
);
1297 printk(KERN_ERR
"asoc: failed to probe CPU DAI %s\n",
1302 cpu_dai
->probed
= 1;
1303 /* mark cpu_dai as probed and add to card cpu_dai list */
1304 list_add(&cpu_dai
->card_list
, &card
->dai_dev_list
);
1307 /* probe the CODEC */
1308 if (!codec
->probed
) {
1309 if (codec
->driver
->probe
) {
1310 ret
= codec
->driver
->probe(codec
);
1312 printk(KERN_ERR
"asoc: failed to probe CODEC %s\n",
1318 soc_init_codec_debugfs(codec
);
1320 /* mark codec as probed and add to card codec list */
1322 list_add(&codec
->card_list
, &card
->codec_dev_list
);
1325 /* probe the platform */
1326 if (!platform
->probed
) {
1327 if (platform
->driver
->probe
) {
1328 ret
= platform
->driver
->probe(platform
);
1330 printk(KERN_ERR
"asoc: failed to probe platform %s\n",
1335 /* mark platform as probed and add to card platform list */
1336 platform
->probed
= 1;
1337 list_add(&platform
->card_list
, &card
->platform_dev_list
);
1340 /* probe the CODEC DAI */
1341 if (!codec_dai
->probed
) {
1342 if (codec_dai
->driver
->probe
) {
1343 ret
= codec_dai
->driver
->probe(codec_dai
);
1345 printk(KERN_ERR
"asoc: failed to probe CODEC DAI %s\n",
1351 /* mark cpu_dai as probed and add to card cpu_dai list */
1352 codec_dai
->probed
= 1;
1353 list_add(&codec_dai
->card_list
, &card
->dai_dev_list
);
1356 /* DAPM dai link stream work */
1357 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
1359 /* now that all clients have probed, initialise the DAI link */
1360 if (dai_link
->init
) {
1361 ret
= dai_link
->init(rtd
);
1363 printk(KERN_ERR
"asoc: failed to init %s\n", dai_link
->stream_name
);
1368 /* Make sure all DAPM widgets are instantiated */
1369 snd_soc_dapm_new_widgets(codec
);
1370 snd_soc_dapm_sync(codec
);
1372 /* register the rtd device */
1373 rtd
->dev
.init_name
= rtd
->dai_link
->stream_name
;
1374 rtd
->dev
.release
= rtd_release
;
1375 rtd
->dev
.init_name
= dai_link
->name
;
1376 ret
= device_register(&rtd
->dev
);
1378 printk(KERN_ERR
"asoc: failed to register DAI runtime device %d\n", ret
);
1382 rtd
->dev_registered
= 1;
1383 ret
= device_create_file(&rtd
->dev
, &dev_attr_pmdown_time
);
1385 printk(KERN_WARNING
"asoc: failed to add pmdown_time sysfs\n");
1387 /* add DAPM sysfs entries for this codec */
1388 ret
= snd_soc_dapm_sys_add(&rtd
->dev
);
1390 printk(KERN_WARNING
"asoc: failed to add codec dapm sysfs entries\n");
1392 /* add codec sysfs entries */
1393 ret
= device_create_file(&rtd
->dev
, &dev_attr_codec_reg
);
1395 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1397 /* create the pcm */
1398 ret
= soc_new_pcm(rtd
, num
);
1400 printk(KERN_ERR
"asoc: can't create pcm %s\n", dai_link
->stream_name
);
1404 /* add platform data for AC97 devices */
1405 if (rtd
->codec_dai
->driver
->ac97_control
)
1406 snd_ac97_dev_add_pdata(codec
->ac97
, rtd
->cpu_dai
->ac97_pdata
);
1411 #ifdef CONFIG_SND_SOC_AC97_BUS
1412 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime
*rtd
)
1416 /* Only instantiate AC97 if not already done by the adaptor
1417 * for the generic AC97 subsystem.
1419 if (rtd
->codec_dai
->driver
->ac97_control
&& !rtd
->codec
->ac97_registered
) {
1421 ret
= soc_ac97_dev_register(rtd
->codec
);
1423 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1427 rtd
->codec
->ac97_registered
= 1;
1432 static void soc_unregister_ac97_dai_link(struct snd_soc_codec
*codec
)
1434 if (codec
->ac97_registered
) {
1435 soc_ac97_dev_unregister(codec
);
1436 codec
->ac97_registered
= 0;
1441 static void snd_soc_instantiate_card(struct snd_soc_card
*card
)
1443 struct platform_device
*pdev
= to_platform_device(card
->dev
);
1446 mutex_lock(&card
->mutex
);
1448 if (card
->instantiated
) {
1449 mutex_unlock(&card
->mutex
);
1454 for (i
= 0; i
< card
->num_links
; i
++)
1455 soc_bind_dai_link(card
, i
);
1457 /* bind completed ? */
1458 if (card
->num_rtd
!= card
->num_links
) {
1459 mutex_unlock(&card
->mutex
);
1463 /* card bind complete so register a sound card */
1464 ret
= snd_card_create(SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
,
1465 card
->owner
, 0, &card
->snd_card
);
1467 printk(KERN_ERR
"asoc: can't create sound card for card %s\n",
1469 mutex_unlock(&card
->mutex
);
1472 card
->snd_card
->dev
= card
->dev
;
1475 /* deferred resume work */
1476 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
1479 /* initialise the sound card only once */
1481 ret
= card
->probe(pdev
);
1483 goto card_probe_error
;
1486 for (i
= 0; i
< card
->num_links
; i
++) {
1487 ret
= soc_probe_dai_link(card
, i
);
1489 printk(KERN_ERR
"asoc: failed to instanciate card %s\n", card
->name
);
1494 snprintf(card
->snd_card
->shortname
, sizeof(card
->snd_card
->shortname
),
1496 snprintf(card
->snd_card
->longname
, sizeof(card
->snd_card
->longname
),
1499 ret
= snd_card_register(card
->snd_card
);
1501 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n", card
->name
);
1505 #ifdef CONFIG_SND_SOC_AC97_BUS
1506 /* register any AC97 codecs */
1507 for (i
= 0; i
< card
->num_rtd
; i
++) {
1508 ret
= soc_register_ac97_dai_link(&card
->rtd
[i
]);
1510 printk(KERN_ERR
"asoc: failed to register AC97 %s\n", card
->name
);
1516 card
->instantiated
= 1;
1517 mutex_unlock(&card
->mutex
);
1521 for (i
= 0; i
< card
->num_links
; i
++)
1522 soc_remove_dai_link(card
, i
);
1528 snd_card_free(card
->snd_card
);
1530 mutex_unlock(&card
->mutex
);
1534 * Attempt to initialise any uninitialised cards. Must be called with
1537 static void snd_soc_instantiate_cards(void)
1539 struct snd_soc_card
*card
;
1540 list_for_each_entry(card
, &card_list
, list
)
1541 snd_soc_instantiate_card(card
);
1544 /* probes a new socdev */
1545 static int soc_probe(struct platform_device
*pdev
)
1547 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1550 /* Bodge while we unpick instantiation */
1551 card
->dev
= &pdev
->dev
;
1552 INIT_LIST_HEAD(&card
->dai_dev_list
);
1553 INIT_LIST_HEAD(&card
->codec_dev_list
);
1554 INIT_LIST_HEAD(&card
->platform_dev_list
);
1556 ret
= snd_soc_register_card(card
);
1558 dev_err(&pdev
->dev
, "Failed to register card\n");
1565 /* removes a socdev */
1566 static int soc_remove(struct platform_device
*pdev
)
1568 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1571 if (card
->instantiated
) {
1573 /* make sure any delayed work runs */
1574 for (i
= 0; i
< card
->num_rtd
; i
++) {
1575 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1576 run_delayed_work(&rtd
->delayed_work
);
1579 /* remove and free each DAI */
1580 for (i
= 0; i
< card
->num_rtd
; i
++)
1581 soc_remove_dai_link(card
, i
);
1583 /* remove the card */
1588 snd_card_free(card
->snd_card
);
1590 snd_soc_unregister_card(card
);
1594 static int soc_poweroff(struct device
*dev
)
1596 struct platform_device
*pdev
= to_platform_device(dev
);
1597 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
1600 if (!card
->instantiated
)
1603 /* Flush out pmdown_time work - we actually do want to run it
1604 * now, we're shutting down so no imminent restart. */
1605 for (i
= 0; i
< card
->num_rtd
; i
++) {
1606 struct snd_soc_pcm_runtime
*rtd
= &card
->rtd
[i
];
1607 run_delayed_work(&rtd
->delayed_work
);
1610 snd_soc_dapm_shutdown(card
);
1615 static const struct dev_pm_ops soc_pm_ops
= {
1616 .suspend
= soc_suspend
,
1617 .resume
= soc_resume
,
1618 .poweroff
= soc_poweroff
,
1621 /* ASoC platform driver */
1622 static struct platform_driver soc_driver
= {
1624 .name
= "soc-audio",
1625 .owner
= THIS_MODULE
,
1629 .remove
= soc_remove
,
1632 /* create a new pcm */
1633 static int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
1635 struct snd_soc_codec
*codec
= rtd
->codec
;
1636 struct snd_soc_platform
*platform
= rtd
->platform
;
1637 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
1638 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1639 struct snd_pcm
*pcm
;
1641 int ret
= 0, playback
= 0, capture
= 0;
1643 /* check client and interface hw capabilities */
1644 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
1645 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
1647 if (codec_dai
->driver
->playback
.channels_min
)
1649 if (codec_dai
->driver
->capture
.channels_min
)
1652 dev_dbg(rtd
->card
->dev
, "registered pcm #%d %s\n",num
,new_name
);
1653 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
,
1654 num
, playback
, capture
, &pcm
);
1656 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n", codec
->name
);
1661 pcm
->private_data
= rtd
;
1662 soc_pcm_ops
.mmap
= platform
->driver
->ops
->mmap
;
1663 soc_pcm_ops
.pointer
= platform
->driver
->ops
->pointer
;
1664 soc_pcm_ops
.ioctl
= platform
->driver
->ops
->ioctl
;
1665 soc_pcm_ops
.copy
= platform
->driver
->ops
->copy
;
1666 soc_pcm_ops
.silence
= platform
->driver
->ops
->silence
;
1667 soc_pcm_ops
.ack
= platform
->driver
->ops
->ack
;
1668 soc_pcm_ops
.page
= platform
->driver
->ops
->page
;
1671 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
1674 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
1676 ret
= platform
->driver
->pcm_new(rtd
->card
->snd_card
, codec_dai
, pcm
);
1678 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
1682 pcm
->private_free
= platform
->driver
->pcm_free
;
1683 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
1689 * snd_soc_codec_volatile_register: Report if a register is volatile.
1691 * @codec: CODEC to query.
1692 * @reg: Register to query.
1694 * Boolean function indiciating if a CODEC register is volatile.
1696 int snd_soc_codec_volatile_register(struct snd_soc_codec
*codec
, int reg
)
1698 if (codec
->driver
->volatile_register
)
1699 return codec
->driver
->volatile_register(reg
);
1703 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register
);
1706 * snd_soc_new_ac97_codec - initailise AC97 device
1707 * @codec: audio codec
1708 * @ops: AC97 bus operations
1709 * @num: AC97 codec number
1711 * Initialises AC97 codec resources for use by ad-hoc devices only.
1713 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1714 struct snd_ac97_bus_ops
*ops
, int num
)
1716 mutex_lock(&codec
->mutex
);
1718 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1719 if (codec
->ac97
== NULL
) {
1720 mutex_unlock(&codec
->mutex
);
1724 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1725 if (codec
->ac97
->bus
== NULL
) {
1728 mutex_unlock(&codec
->mutex
);
1732 codec
->ac97
->bus
->ops
= ops
;
1733 codec
->ac97
->num
= num
;
1734 mutex_unlock(&codec
->mutex
);
1737 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1740 * snd_soc_free_ac97_codec - free AC97 codec device
1741 * @codec: audio codec
1743 * Frees AC97 codec device resources.
1745 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1747 mutex_lock(&codec
->mutex
);
1748 #ifdef CONFIG_SND_SOC_AC97_BUS
1749 soc_unregister_ac97_dai_link(codec
);
1751 kfree(codec
->ac97
->bus
);
1754 mutex_unlock(&codec
->mutex
);
1756 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1759 * snd_soc_update_bits - update codec register bits
1760 * @codec: audio codec
1761 * @reg: codec register
1762 * @mask: register mask
1765 * Writes new register value.
1767 * Returns 1 for change else 0.
1769 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1770 unsigned int mask
, unsigned int value
)
1773 unsigned int old
, new;
1775 old
= snd_soc_read(codec
, reg
);
1776 new = (old
& ~mask
) | value
;
1777 change
= old
!= new;
1779 snd_soc_write(codec
, reg
, new);
1783 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1786 * snd_soc_update_bits_locked - update codec register bits
1787 * @codec: audio codec
1788 * @reg: codec register
1789 * @mask: register mask
1792 * Writes new register value, and takes the codec mutex.
1794 * Returns 1 for change else 0.
1796 int snd_soc_update_bits_locked(struct snd_soc_codec
*codec
,
1797 unsigned short reg
, unsigned int mask
,
1802 mutex_lock(&codec
->mutex
);
1803 change
= snd_soc_update_bits(codec
, reg
, mask
, value
);
1804 mutex_unlock(&codec
->mutex
);
1808 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked
);
1811 * snd_soc_test_bits - test register for change
1812 * @codec: audio codec
1813 * @reg: codec register
1814 * @mask: register mask
1817 * Tests a register with a new value and checks if the new value is
1818 * different from the old value.
1820 * Returns 1 for change else 0.
1822 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1823 unsigned int mask
, unsigned int value
)
1826 unsigned int old
, new;
1828 old
= snd_soc_read(codec
, reg
);
1829 new = (old
& ~mask
) | value
;
1830 change
= old
!= new;
1834 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1837 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1838 * @substream: the pcm substream
1839 * @hw: the hardware parameters
1841 * Sets the substream runtime hardware parameters.
1843 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1844 const struct snd_pcm_hardware
*hw
)
1846 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1847 runtime
->hw
.info
= hw
->info
;
1848 runtime
->hw
.formats
= hw
->formats
;
1849 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1850 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1851 runtime
->hw
.periods_min
= hw
->periods_min
;
1852 runtime
->hw
.periods_max
= hw
->periods_max
;
1853 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1854 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1857 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1860 * snd_soc_cnew - create new control
1861 * @_template: control template
1862 * @data: control private data
1863 * @long_name: control long name
1865 * Create a new mixer control from a template control.
1867 * Returns 0 for success, else error.
1869 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1870 void *data
, char *long_name
)
1872 struct snd_kcontrol_new
template;
1874 memcpy(&template, _template
, sizeof(template));
1876 template.name
= long_name
;
1879 return snd_ctl_new1(&template, data
);
1881 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
1884 * snd_soc_add_controls - add an array of controls to a codec.
1885 * Convienience function to add a list of controls. Many codecs were
1886 * duplicating this code.
1888 * @codec: codec to add controls to
1889 * @controls: array of controls to add
1890 * @num_controls: number of elements in the array
1892 * Return 0 for success, else error.
1894 int snd_soc_add_controls(struct snd_soc_codec
*codec
,
1895 const struct snd_kcontrol_new
*controls
, int num_controls
)
1897 struct snd_card
*card
= codec
->card
->snd_card
;
1900 for (i
= 0; i
< num_controls
; i
++) {
1901 const struct snd_kcontrol_new
*control
= &controls
[i
];
1902 err
= snd_ctl_add(card
, snd_soc_cnew(control
, codec
, NULL
));
1904 dev_err(codec
->dev
, "%s: Failed to add %s\n",
1905 codec
->name
, control
->name
);
1912 EXPORT_SYMBOL_GPL(snd_soc_add_controls
);
1915 * snd_soc_info_enum_double - enumerated double mixer info callback
1916 * @kcontrol: mixer control
1917 * @uinfo: control element information
1919 * Callback to provide information about a double enumerated
1922 * Returns 0 for success.
1924 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
1925 struct snd_ctl_elem_info
*uinfo
)
1927 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1929 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1930 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
1931 uinfo
->value
.enumerated
.items
= e
->max
;
1933 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
1934 uinfo
->value
.enumerated
.item
= e
->max
- 1;
1935 strcpy(uinfo
->value
.enumerated
.name
,
1936 e
->texts
[uinfo
->value
.enumerated
.item
]);
1939 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
1942 * snd_soc_get_enum_double - enumerated double mixer get callback
1943 * @kcontrol: mixer control
1944 * @ucontrol: control element information
1946 * Callback to get the value of a double enumerated mixer.
1948 * Returns 0 for success.
1950 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
1951 struct snd_ctl_elem_value
*ucontrol
)
1953 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1954 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1955 unsigned int val
, bitmask
;
1957 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1959 val
= snd_soc_read(codec
, e
->reg
);
1960 ucontrol
->value
.enumerated
.item
[0]
1961 = (val
>> e
->shift_l
) & (bitmask
- 1);
1962 if (e
->shift_l
!= e
->shift_r
)
1963 ucontrol
->value
.enumerated
.item
[1] =
1964 (val
>> e
->shift_r
) & (bitmask
- 1);
1968 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
1971 * snd_soc_put_enum_double - enumerated double mixer put callback
1972 * @kcontrol: mixer control
1973 * @ucontrol: control element information
1975 * Callback to set the value of a double enumerated mixer.
1977 * Returns 0 for success.
1979 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
1980 struct snd_ctl_elem_value
*ucontrol
)
1982 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1983 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1985 unsigned int mask
, bitmask
;
1987 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1989 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1991 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
1992 mask
= (bitmask
- 1) << e
->shift_l
;
1993 if (e
->shift_l
!= e
->shift_r
) {
1994 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1996 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1997 mask
|= (bitmask
- 1) << e
->shift_r
;
2000 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2002 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
2005 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2006 * @kcontrol: mixer control
2007 * @ucontrol: control element information
2009 * Callback to get the value of a double semi enumerated mixer.
2011 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2012 * used for handling bitfield coded enumeration for example.
2014 * Returns 0 for success.
2016 int snd_soc_get_value_enum_double(struct snd_kcontrol
*kcontrol
,
2017 struct snd_ctl_elem_value
*ucontrol
)
2019 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2020 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2021 unsigned int reg_val
, val
, mux
;
2023 reg_val
= snd_soc_read(codec
, e
->reg
);
2024 val
= (reg_val
>> e
->shift_l
) & e
->mask
;
2025 for (mux
= 0; mux
< e
->max
; mux
++) {
2026 if (val
== e
->values
[mux
])
2029 ucontrol
->value
.enumerated
.item
[0] = mux
;
2030 if (e
->shift_l
!= e
->shift_r
) {
2031 val
= (reg_val
>> e
->shift_r
) & e
->mask
;
2032 for (mux
= 0; mux
< e
->max
; mux
++) {
2033 if (val
== e
->values
[mux
])
2036 ucontrol
->value
.enumerated
.item
[1] = mux
;
2041 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double
);
2044 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2045 * @kcontrol: mixer control
2046 * @ucontrol: control element information
2048 * Callback to set the value of a double semi enumerated mixer.
2050 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2051 * used for handling bitfield coded enumeration for example.
2053 * Returns 0 for success.
2055 int snd_soc_put_value_enum_double(struct snd_kcontrol
*kcontrol
,
2056 struct snd_ctl_elem_value
*ucontrol
)
2058 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2059 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2063 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
2065 val
= e
->values
[ucontrol
->value
.enumerated
.item
[0]] << e
->shift_l
;
2066 mask
= e
->mask
<< e
->shift_l
;
2067 if (e
->shift_l
!= e
->shift_r
) {
2068 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
2070 val
|= e
->values
[ucontrol
->value
.enumerated
.item
[1]] << e
->shift_r
;
2071 mask
|= e
->mask
<< e
->shift_r
;
2074 return snd_soc_update_bits_locked(codec
, e
->reg
, mask
, val
);
2076 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double
);
2079 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2080 * @kcontrol: mixer control
2081 * @uinfo: control element information
2083 * Callback to provide information about an external enumerated
2086 * Returns 0 for success.
2088 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
2089 struct snd_ctl_elem_info
*uinfo
)
2091 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
2093 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2095 uinfo
->value
.enumerated
.items
= e
->max
;
2097 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
2098 uinfo
->value
.enumerated
.item
= e
->max
- 1;
2099 strcpy(uinfo
->value
.enumerated
.name
,
2100 e
->texts
[uinfo
->value
.enumerated
.item
]);
2103 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
2106 * snd_soc_info_volsw_ext - external single mixer info callback
2107 * @kcontrol: mixer control
2108 * @uinfo: control element information
2110 * Callback to provide information about a single external mixer control.
2112 * Returns 0 for success.
2114 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
2115 struct snd_ctl_elem_info
*uinfo
)
2117 int max
= kcontrol
->private_value
;
2119 if (max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2120 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2122 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2125 uinfo
->value
.integer
.min
= 0;
2126 uinfo
->value
.integer
.max
= max
;
2129 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
2132 * snd_soc_info_volsw - single mixer info callback
2133 * @kcontrol: mixer control
2134 * @uinfo: control element information
2136 * Callback to provide information about a single mixer control.
2138 * Returns 0 for success.
2140 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
2141 struct snd_ctl_elem_info
*uinfo
)
2143 struct soc_mixer_control
*mc
=
2144 (struct soc_mixer_control
*)kcontrol
->private_value
;
2146 unsigned int shift
= mc
->shift
;
2147 unsigned int rshift
= mc
->rshift
;
2149 if (!mc
->platform_max
)
2150 mc
->platform_max
= mc
->max
;
2151 platform_max
= mc
->platform_max
;
2153 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2154 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2156 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2158 uinfo
->count
= shift
== rshift
? 1 : 2;
2159 uinfo
->value
.integer
.min
= 0;
2160 uinfo
->value
.integer
.max
= platform_max
;
2163 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
2166 * snd_soc_get_volsw - single mixer get callback
2167 * @kcontrol: mixer control
2168 * @ucontrol: control element information
2170 * Callback to get the value of a single mixer control.
2172 * Returns 0 for success.
2174 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
2175 struct snd_ctl_elem_value
*ucontrol
)
2177 struct soc_mixer_control
*mc
=
2178 (struct soc_mixer_control
*)kcontrol
->private_value
;
2179 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2180 unsigned int reg
= mc
->reg
;
2181 unsigned int shift
= mc
->shift
;
2182 unsigned int rshift
= mc
->rshift
;
2184 unsigned int mask
= (1 << fls(max
)) - 1;
2185 unsigned int invert
= mc
->invert
;
2187 ucontrol
->value
.integer
.value
[0] =
2188 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2189 if (shift
!= rshift
)
2190 ucontrol
->value
.integer
.value
[1] =
2191 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
2193 ucontrol
->value
.integer
.value
[0] =
2194 max
- ucontrol
->value
.integer
.value
[0];
2195 if (shift
!= rshift
)
2196 ucontrol
->value
.integer
.value
[1] =
2197 max
- ucontrol
->value
.integer
.value
[1];
2202 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
2205 * snd_soc_put_volsw - single mixer put callback
2206 * @kcontrol: mixer control
2207 * @ucontrol: control element information
2209 * Callback to set the value of a single mixer control.
2211 * Returns 0 for success.
2213 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
2214 struct snd_ctl_elem_value
*ucontrol
)
2216 struct soc_mixer_control
*mc
=
2217 (struct soc_mixer_control
*)kcontrol
->private_value
;
2218 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2219 unsigned int reg
= mc
->reg
;
2220 unsigned int shift
= mc
->shift
;
2221 unsigned int rshift
= mc
->rshift
;
2223 unsigned int mask
= (1 << fls(max
)) - 1;
2224 unsigned int invert
= mc
->invert
;
2225 unsigned int val
, val2
, val_mask
;
2227 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2230 val_mask
= mask
<< shift
;
2232 if (shift
!= rshift
) {
2233 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2236 val_mask
|= mask
<< rshift
;
2237 val
|= val2
<< rshift
;
2239 return snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2241 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
2244 * snd_soc_info_volsw_2r - double mixer info callback
2245 * @kcontrol: mixer control
2246 * @uinfo: control element information
2248 * Callback to provide information about a double mixer control that
2249 * spans 2 codec registers.
2251 * Returns 0 for success.
2253 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
2254 struct snd_ctl_elem_info
*uinfo
)
2256 struct soc_mixer_control
*mc
=
2257 (struct soc_mixer_control
*)kcontrol
->private_value
;
2260 if (!mc
->platform_max
)
2261 mc
->platform_max
= mc
->max
;
2262 platform_max
= mc
->platform_max
;
2264 if (platform_max
== 1 && !strstr(kcontrol
->id
.name
, " Volume"))
2265 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
2267 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2270 uinfo
->value
.integer
.min
= 0;
2271 uinfo
->value
.integer
.max
= platform_max
;
2274 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
2277 * snd_soc_get_volsw_2r - double mixer get callback
2278 * @kcontrol: mixer control
2279 * @ucontrol: control element information
2281 * Callback to get the value of a double mixer control that spans 2 registers.
2283 * Returns 0 for success.
2285 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
2286 struct snd_ctl_elem_value
*ucontrol
)
2288 struct soc_mixer_control
*mc
=
2289 (struct soc_mixer_control
*)kcontrol
->private_value
;
2290 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2291 unsigned int reg
= mc
->reg
;
2292 unsigned int reg2
= mc
->rreg
;
2293 unsigned int shift
= mc
->shift
;
2295 unsigned int mask
= (1 << fls(max
)) - 1;
2296 unsigned int invert
= mc
->invert
;
2298 ucontrol
->value
.integer
.value
[0] =
2299 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
2300 ucontrol
->value
.integer
.value
[1] =
2301 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
2303 ucontrol
->value
.integer
.value
[0] =
2304 max
- ucontrol
->value
.integer
.value
[0];
2305 ucontrol
->value
.integer
.value
[1] =
2306 max
- ucontrol
->value
.integer
.value
[1];
2311 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
2314 * snd_soc_put_volsw_2r - double mixer set callback
2315 * @kcontrol: mixer control
2316 * @ucontrol: control element information
2318 * Callback to set the value of a double mixer control that spans 2 registers.
2320 * Returns 0 for success.
2322 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
2323 struct snd_ctl_elem_value
*ucontrol
)
2325 struct soc_mixer_control
*mc
=
2326 (struct soc_mixer_control
*)kcontrol
->private_value
;
2327 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2328 unsigned int reg
= mc
->reg
;
2329 unsigned int reg2
= mc
->rreg
;
2330 unsigned int shift
= mc
->shift
;
2332 unsigned int mask
= (1 << fls(max
)) - 1;
2333 unsigned int invert
= mc
->invert
;
2335 unsigned int val
, val2
, val_mask
;
2337 val_mask
= mask
<< shift
;
2338 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
2339 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
2347 val2
= val2
<< shift
;
2349 err
= snd_soc_update_bits_locked(codec
, reg
, val_mask
, val
);
2353 err
= snd_soc_update_bits_locked(codec
, reg2
, val_mask
, val2
);
2356 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
2359 * snd_soc_info_volsw_s8 - signed mixer info callback
2360 * @kcontrol: mixer control
2361 * @uinfo: control element information
2363 * Callback to provide information about a signed mixer control.
2365 * Returns 0 for success.
2367 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
2368 struct snd_ctl_elem_info
*uinfo
)
2370 struct soc_mixer_control
*mc
=
2371 (struct soc_mixer_control
*)kcontrol
->private_value
;
2375 if (!mc
->platform_max
)
2376 mc
->platform_max
= mc
->max
;
2377 platform_max
= mc
->platform_max
;
2379 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2381 uinfo
->value
.integer
.min
= 0;
2382 uinfo
->value
.integer
.max
= platform_max
- min
;
2385 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
2388 * snd_soc_get_volsw_s8 - signed mixer get callback
2389 * @kcontrol: mixer control
2390 * @ucontrol: control element information
2392 * Callback to get the value of a signed mixer control.
2394 * Returns 0 for success.
2396 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
2397 struct snd_ctl_elem_value
*ucontrol
)
2399 struct soc_mixer_control
*mc
=
2400 (struct soc_mixer_control
*)kcontrol
->private_value
;
2401 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2402 unsigned int reg
= mc
->reg
;
2404 int val
= snd_soc_read(codec
, reg
);
2406 ucontrol
->value
.integer
.value
[0] =
2407 ((signed char)(val
& 0xff))-min
;
2408 ucontrol
->value
.integer
.value
[1] =
2409 ((signed char)((val
>> 8) & 0xff))-min
;
2412 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
2415 * snd_soc_put_volsw_sgn - signed mixer put callback
2416 * @kcontrol: mixer control
2417 * @ucontrol: control element information
2419 * Callback to set the value of a signed mixer control.
2421 * Returns 0 for success.
2423 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
2424 struct snd_ctl_elem_value
*ucontrol
)
2426 struct soc_mixer_control
*mc
=
2427 (struct soc_mixer_control
*)kcontrol
->private_value
;
2428 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2429 unsigned int reg
= mc
->reg
;
2433 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
2434 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
2436 return snd_soc_update_bits_locked(codec
, reg
, 0xffff, val
);
2438 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
2441 * snd_soc_limit_volume - Set new limit to an existing volume control.
2443 * @codec: where to look for the control
2444 * @name: Name of the control
2445 * @max: new maximum limit
2447 * Return 0 for success, else error.
2449 int snd_soc_limit_volume(struct snd_soc_codec
*codec
,
2450 const char *name
, int max
)
2452 struct snd_card
*card
= codec
->card
->snd_card
;
2453 struct snd_kcontrol
*kctl
;
2454 struct soc_mixer_control
*mc
;
2458 /* Sanity check for name and max */
2459 if (unlikely(!name
|| max
<= 0))
2462 list_for_each_entry(kctl
, &card
->controls
, list
) {
2463 if (!strncmp(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
))) {
2469 mc
= (struct soc_mixer_control
*)kctl
->private_value
;
2470 if (max
<= mc
->max
) {
2471 mc
->platform_max
= max
;
2477 EXPORT_SYMBOL_GPL(snd_soc_limit_volume
);
2480 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2481 * mixer info callback
2482 * @kcontrol: mixer control
2483 * @uinfo: control element information
2485 * Returns 0 for success.
2487 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2488 struct snd_ctl_elem_info
*uinfo
)
2490 struct soc_mixer_control
*mc
=
2491 (struct soc_mixer_control
*)kcontrol
->private_value
;
2495 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
2497 uinfo
->value
.integer
.min
= 0;
2498 uinfo
->value
.integer
.max
= max
-min
;
2502 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx
);
2505 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2506 * mixer get callback
2507 * @kcontrol: mixer control
2508 * @uinfo: control element information
2510 * Returns 0 for success.
2512 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2513 struct snd_ctl_elem_value
*ucontrol
)
2515 struct soc_mixer_control
*mc
=
2516 (struct soc_mixer_control
*)kcontrol
->private_value
;
2517 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2518 unsigned int mask
= (1<<mc
->shift
)-1;
2520 int val
= snd_soc_read(codec
, mc
->reg
) & mask
;
2521 int valr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2523 ucontrol
->value
.integer
.value
[0] = ((val
& 0xff)-min
) & mask
;
2524 ucontrol
->value
.integer
.value
[1] = ((valr
& 0xff)-min
) & mask
;
2527 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx
);
2530 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2531 * mixer put callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2535 * Returns 0 for success.
2537 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol
*kcontrol
,
2538 struct snd_ctl_elem_value
*ucontrol
)
2540 struct soc_mixer_control
*mc
=
2541 (struct soc_mixer_control
*)kcontrol
->private_value
;
2542 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2543 unsigned int mask
= (1<<mc
->shift
)-1;
2546 unsigned int val
, valr
, oval
, ovalr
;
2548 val
= ((ucontrol
->value
.integer
.value
[0]+min
) & 0xff);
2550 valr
= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff);
2553 oval
= snd_soc_read(codec
, mc
->reg
) & mask
;
2554 ovalr
= snd_soc_read(codec
, mc
->rreg
) & mask
;
2558 ret
= snd_soc_write(codec
, mc
->reg
, val
);
2562 if (ovalr
!= valr
) {
2563 ret
= snd_soc_write(codec
, mc
->rreg
, valr
);
2570 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx
);
2573 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2575 * @clk_id: DAI specific clock ID
2576 * @freq: new clock frequency in Hz
2577 * @dir: new clock direction - input/output.
2579 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2581 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
2582 unsigned int freq
, int dir
)
2584 if (dai
->driver
&& dai
->driver
->ops
->set_sysclk
)
2585 return dai
->driver
->ops
->set_sysclk(dai
, clk_id
, freq
, dir
);
2589 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
2592 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2594 * @div_id: DAI specific clock divider ID
2595 * @div: new clock divisor.
2597 * Configures the clock dividers. This is used to derive the best DAI bit and
2598 * frame clocks from the system or master clock. It's best to set the DAI bit
2599 * and frame clocks as low as possible to save system power.
2601 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
2602 int div_id
, int div
)
2604 if (dai
->driver
&& dai
->driver
->ops
->set_clkdiv
)
2605 return dai
->driver
->ops
->set_clkdiv(dai
, div_id
, div
);
2609 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
2612 * snd_soc_dai_set_pll - configure DAI PLL.
2614 * @pll_id: DAI specific PLL ID
2615 * @source: DAI specific source for the PLL
2616 * @freq_in: PLL input clock frequency in Hz
2617 * @freq_out: requested PLL output clock frequency in Hz
2619 * Configures and enables PLL to generate output clock based on input clock.
2621 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
, int pll_id
, int source
,
2622 unsigned int freq_in
, unsigned int freq_out
)
2624 if (dai
->driver
&& dai
->driver
->ops
->set_pll
)
2625 return dai
->driver
->ops
->set_pll(dai
, pll_id
, source
,
2630 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
2633 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2635 * @fmt: SND_SOC_DAIFMT_ format value.
2637 * Configures the DAI hardware format and clocking.
2639 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
2641 if (dai
->driver
&& dai
->driver
->ops
->set_fmt
)
2642 return dai
->driver
->ops
->set_fmt(dai
, fmt
);
2646 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
2649 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2651 * @tx_mask: bitmask representing active TX slots.
2652 * @rx_mask: bitmask representing active RX slots.
2653 * @slots: Number of slots in use.
2654 * @slot_width: Width in bits for each slot.
2656 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2659 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
2660 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
2662 if (dai
->driver
&& dai
->driver
->ops
->set_tdm_slot
)
2663 return dai
->driver
->ops
->set_tdm_slot(dai
, tx_mask
, rx_mask
,
2668 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
2671 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2673 * @tx_num: how many TX channels
2674 * @tx_slot: pointer to an array which imply the TX slot number channel
2676 * @rx_num: how many RX channels
2677 * @rx_slot: pointer to an array which imply the RX slot number channel
2680 * configure the relationship between channel number and TDM slot number.
2682 int snd_soc_dai_set_channel_map(struct snd_soc_dai
*dai
,
2683 unsigned int tx_num
, unsigned int *tx_slot
,
2684 unsigned int rx_num
, unsigned int *rx_slot
)
2686 if (dai
->driver
&& dai
->driver
->ops
->set_channel_map
)
2687 return dai
->driver
->ops
->set_channel_map(dai
, tx_num
, tx_slot
,
2692 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map
);
2695 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2697 * @tristate: tristate enable
2699 * Tristates the DAI so that others can use it.
2701 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
2703 if (dai
->driver
&& dai
->driver
->ops
->set_tristate
)
2704 return dai
->driver
->ops
->set_tristate(dai
, tristate
);
2708 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
2711 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2713 * @mute: mute enable
2715 * Mutes the DAI DAC.
2717 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
2719 if (dai
->driver
&& dai
->driver
->ops
->digital_mute
)
2720 return dai
->driver
->ops
->digital_mute(dai
, mute
);
2724 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
2727 * snd_soc_register_card - Register a card with the ASoC core
2729 * @card: Card to register
2731 * Note that currently this is an internal only function: it will be
2732 * exposed to machine drivers after further backporting of ASoC v2
2733 * registration APIs.
2735 static int snd_soc_register_card(struct snd_soc_card
*card
)
2739 if (!card
->name
|| !card
->dev
)
2742 card
->rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
) * card
->num_links
,
2744 if (card
->rtd
== NULL
)
2747 for (i
= 0; i
< card
->num_links
; i
++)
2748 card
->rtd
[i
].dai_link
= &card
->dai_link
[i
];
2750 INIT_LIST_HEAD(&card
->list
);
2751 card
->instantiated
= 0;
2752 mutex_init(&card
->mutex
);
2754 mutex_lock(&client_mutex
);
2755 list_add(&card
->list
, &card_list
);
2756 snd_soc_instantiate_cards();
2757 mutex_unlock(&client_mutex
);
2759 dev_dbg(card
->dev
, "Registered card '%s'\n", card
->name
);
2765 * snd_soc_unregister_card - Unregister a card with the ASoC core
2767 * @card: Card to unregister
2769 * Note that currently this is an internal only function: it will be
2770 * exposed to machine drivers after further backporting of ASoC v2
2771 * registration APIs.
2773 static int snd_soc_unregister_card(struct snd_soc_card
*card
)
2775 mutex_lock(&client_mutex
);
2776 list_del(&card
->list
);
2777 mutex_unlock(&client_mutex
);
2778 dev_dbg(card
->dev
, "Unregistered card '%s'\n", card
->name
);
2784 * Simplify DAI link configuration by removing ".-1" from device names
2785 * and sanitizing names.
2787 static inline char *fmt_single_name(struct device
*dev
, int *id
)
2789 char *found
, name
[NAME_SIZE
];
2792 if (dev_name(dev
) == NULL
)
2795 strncpy(name
, dev_name(dev
), NAME_SIZE
);
2797 /* are we a "%s.%d" name (platform and SPI components) */
2798 found
= strstr(name
, dev
->driver
->name
);
2801 if (sscanf(&found
[strlen(dev
->driver
->name
)], ".%d", id
) == 1) {
2803 /* discard ID from name if ID == -1 */
2805 found
[strlen(dev
->driver
->name
)] = '\0';
2809 /* I2C component devices are named "bus-addr" */
2810 if (sscanf(name
, "%x-%x", &id1
, &id2
) == 2) {
2811 char tmp
[NAME_SIZE
];
2813 /* create unique ID number from I2C addr and bus */
2814 *id
= ((id1
&& 0xffff) << 16) + id2
;
2816 /* sanitize component name for DAI link creation */
2817 snprintf(tmp
, NAME_SIZE
, "%s.%s", dev
->driver
->name
, name
);
2818 strncpy(name
, tmp
, NAME_SIZE
);
2823 return kstrdup(name
, GFP_KERNEL
);
2827 * Simplify DAI link naming for single devices with multiple DAIs by removing
2828 * any ".-1" and using the DAI name (instead of device name).
2830 static inline char *fmt_multiple_name(struct device
*dev
,
2831 struct snd_soc_dai_driver
*dai_drv
)
2833 if (dai_drv
->name
== NULL
) {
2834 printk(KERN_ERR
"asoc: error - multiple DAI %s registered with no name\n",
2839 return kstrdup(dai_drv
->name
, GFP_KERNEL
);
2843 * snd_soc_register_dai - Register a DAI with the ASoC core
2845 * @dai: DAI to register
2847 int snd_soc_register_dai(struct device
*dev
,
2848 struct snd_soc_dai_driver
*dai_drv
)
2850 struct snd_soc_dai
*dai
;
2852 dev_dbg(dev
, "dai register %s\n", dev_name(dev
));
2854 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2858 /* create DAI component name */
2859 dai
->name
= fmt_single_name(dev
, &dai
->id
);
2860 if (dai
->name
== NULL
) {
2866 dai
->driver
= dai_drv
;
2867 if (!dai
->driver
->ops
)
2868 dai
->driver
->ops
= &null_dai_ops
;
2870 mutex_lock(&client_mutex
);
2871 list_add(&dai
->list
, &dai_list
);
2872 snd_soc_instantiate_cards();
2873 mutex_unlock(&client_mutex
);
2875 pr_debug("Registered DAI '%s'\n", dai
->name
);
2879 EXPORT_SYMBOL_GPL(snd_soc_register_dai
);
2882 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2884 * @dai: DAI to unregister
2886 void snd_soc_unregister_dai(struct device
*dev
)
2888 struct snd_soc_dai
*dai
;
2890 list_for_each_entry(dai
, &dai_list
, list
) {
2891 if (dev
== dai
->dev
)
2897 mutex_lock(&client_mutex
);
2898 list_del(&dai
->list
);
2899 mutex_unlock(&client_mutex
);
2901 pr_debug("Unregistered DAI '%s'\n", dai
->name
);
2905 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai
);
2908 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2910 * @dai: Array of DAIs to register
2911 * @count: Number of DAIs
2913 int snd_soc_register_dais(struct device
*dev
,
2914 struct snd_soc_dai_driver
*dai_drv
, size_t count
)
2916 struct snd_soc_dai
*dai
;
2919 dev_dbg(dev
, "dai register %s #%d\n", dev_name(dev
), count
);
2921 for (i
= 0; i
< count
; i
++) {
2923 dai
= kzalloc(sizeof(struct snd_soc_dai
), GFP_KERNEL
);
2927 /* create DAI component name */
2928 dai
->name
= fmt_multiple_name(dev
, &dai_drv
[i
]);
2929 if (dai
->name
== NULL
) {
2937 dai
->driver
= &dai_drv
[i
];
2938 if (!dai
->driver
->ops
)
2939 dai
->driver
->ops
= &null_dai_ops
;
2941 mutex_lock(&client_mutex
);
2942 list_add(&dai
->list
, &dai_list
);
2943 mutex_unlock(&client_mutex
);
2945 pr_debug("Registered DAI '%s'\n", dai
->name
);
2948 snd_soc_instantiate_cards();
2952 for (i
--; i
>= 0; i
--)
2953 snd_soc_unregister_dai(dev
);
2957 EXPORT_SYMBOL_GPL(snd_soc_register_dais
);
2960 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2962 * @dai: Array of DAIs to unregister
2963 * @count: Number of DAIs
2965 void snd_soc_unregister_dais(struct device
*dev
, size_t count
)
2969 for (i
= 0; i
< count
; i
++)
2970 snd_soc_unregister_dai(dev
);
2972 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais
);
2975 * snd_soc_register_platform - Register a platform with the ASoC core
2977 * @platform: platform to register
2979 int snd_soc_register_platform(struct device
*dev
,
2980 struct snd_soc_platform_driver
*platform_drv
)
2982 struct snd_soc_platform
*platform
;
2984 dev_dbg(dev
, "platform register %s\n", dev_name(dev
));
2986 platform
= kzalloc(sizeof(struct snd_soc_platform
), GFP_KERNEL
);
2987 if (platform
== NULL
)
2990 /* create platform component name */
2991 platform
->name
= fmt_single_name(dev
, &platform
->id
);
2992 if (platform
->name
== NULL
) {
2997 platform
->dev
= dev
;
2998 platform
->driver
= platform_drv
;
3000 mutex_lock(&client_mutex
);
3001 list_add(&platform
->list
, &platform_list
);
3002 snd_soc_instantiate_cards();
3003 mutex_unlock(&client_mutex
);
3005 pr_debug("Registered platform '%s'\n", platform
->name
);
3009 EXPORT_SYMBOL_GPL(snd_soc_register_platform
);
3012 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3014 * @platform: platform to unregister
3016 void snd_soc_unregister_platform(struct device
*dev
)
3018 struct snd_soc_platform
*platform
;
3020 list_for_each_entry(platform
, &platform_list
, list
) {
3021 if (dev
== platform
->dev
)
3027 mutex_lock(&client_mutex
);
3028 list_del(&platform
->list
);
3029 mutex_unlock(&client_mutex
);
3031 pr_debug("Unregistered platform '%s'\n", platform
->name
);
3032 kfree(platform
->name
);
3035 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform
);
3037 static u64 codec_format_map
[] = {
3038 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S16_BE
,
3039 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_U16_BE
,
3040 SNDRV_PCM_FMTBIT_S24_LE
| SNDRV_PCM_FMTBIT_S24_BE
,
3041 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_U24_BE
,
3042 SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S32_BE
,
3043 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_U32_BE
,
3044 SNDRV_PCM_FMTBIT_S24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3045 SNDRV_PCM_FMTBIT_U24_3LE
| SNDRV_PCM_FMTBIT_U24_3BE
,
3046 SNDRV_PCM_FMTBIT_S20_3LE
| SNDRV_PCM_FMTBIT_S20_3BE
,
3047 SNDRV_PCM_FMTBIT_U20_3LE
| SNDRV_PCM_FMTBIT_U20_3BE
,
3048 SNDRV_PCM_FMTBIT_S18_3LE
| SNDRV_PCM_FMTBIT_S18_3BE
,
3049 SNDRV_PCM_FMTBIT_U18_3LE
| SNDRV_PCM_FMTBIT_U18_3BE
,
3050 SNDRV_PCM_FMTBIT_FLOAT_LE
| SNDRV_PCM_FMTBIT_FLOAT_BE
,
3051 SNDRV_PCM_FMTBIT_FLOAT64_LE
| SNDRV_PCM_FMTBIT_FLOAT64_BE
,
3052 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3053 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
,
3056 /* Fix up the DAI formats for endianness: codecs don't actually see
3057 * the endianness of the data but we're using the CPU format
3058 * definitions which do need to include endianness so we ensure that
3059 * codec DAIs always have both big and little endian variants set.
3061 static void fixup_codec_formats(struct snd_soc_pcm_stream
*stream
)
3065 for (i
= 0; i
< ARRAY_SIZE(codec_format_map
); i
++)
3066 if (stream
->formats
& codec_format_map
[i
])
3067 stream
->formats
|= codec_format_map
[i
];
3071 * snd_soc_register_codec - Register a codec with the ASoC core
3073 * @codec: codec to register
3075 int snd_soc_register_codec(struct device
*dev
,
3076 struct snd_soc_codec_driver
*codec_drv
,
3077 struct snd_soc_dai_driver
*dai_drv
, int num_dai
)
3079 struct snd_soc_codec
*codec
;
3082 dev_dbg(dev
, "codec register %s\n", dev_name(dev
));
3084 codec
= kzalloc(sizeof(struct snd_soc_codec
), GFP_KERNEL
);
3088 /* create CODEC component name */
3089 codec
->name
= fmt_single_name(dev
, &codec
->id
);
3090 if (codec
->name
== NULL
) {
3095 /* allocate CODEC register cache */
3096 if (codec_drv
->reg_cache_size
&& codec_drv
->reg_word_size
) {
3098 if (codec_drv
->reg_cache_default
)
3099 codec
->reg_cache
= kmemdup(codec_drv
->reg_cache_default
,
3100 codec_drv
->reg_cache_size
* codec_drv
->reg_word_size
, GFP_KERNEL
);
3102 codec
->reg_cache
= kzalloc(codec_drv
->reg_cache_size
*
3103 codec_drv
->reg_word_size
, GFP_KERNEL
);
3105 if (codec
->reg_cache
== NULL
) {
3113 codec
->driver
= codec_drv
;
3114 codec
->bias_level
= SND_SOC_BIAS_OFF
;
3115 codec
->num_dai
= num_dai
;
3116 mutex_init(&codec
->mutex
);
3117 INIT_LIST_HEAD(&codec
->dapm_widgets
);
3118 INIT_LIST_HEAD(&codec
->dapm_paths
);
3120 for (i
= 0; i
< num_dai
; i
++) {
3121 fixup_codec_formats(&dai_drv
[i
].playback
);
3122 fixup_codec_formats(&dai_drv
[i
].capture
);
3126 ret
= snd_soc_register_dais(dev
, dai_drv
, num_dai
);
3130 mutex_lock(&client_mutex
);
3131 list_add(&codec
->list
, &codec_list
);
3132 snd_soc_instantiate_cards();
3133 mutex_unlock(&client_mutex
);
3135 pr_debug("Registered codec '%s'\n", codec
->name
);
3139 for (i
--; i
>= 0; i
--)
3140 snd_soc_unregister_dai(dev
);
3142 if (codec
->reg_cache
)
3143 kfree(codec
->reg_cache
);
3148 EXPORT_SYMBOL_GPL(snd_soc_register_codec
);
3151 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3153 * @codec: codec to unregister
3155 void snd_soc_unregister_codec(struct device
*dev
)
3157 struct snd_soc_codec
*codec
;
3160 list_for_each_entry(codec
, &codec_list
, list
) {
3161 if (dev
== codec
->dev
)
3167 for (i
= 0; i
< codec
->num_dai
; i
++)
3168 snd_soc_unregister_dai(dev
);
3170 mutex_lock(&client_mutex
);
3171 list_del(&codec
->list
);
3172 mutex_unlock(&client_mutex
);
3174 pr_debug("Unregistered codec '%s'\n", codec
->name
);
3176 if (codec
->reg_cache
)
3177 kfree(codec
->reg_cache
);
3180 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec
);
3182 static int __init
snd_soc_init(void)
3184 #ifdef CONFIG_DEBUG_FS
3185 debugfs_root
= debugfs_create_dir("asoc", NULL
);
3186 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3188 "ASoC: Failed to create debugfs directory\n");
3189 debugfs_root
= NULL
;
3193 return platform_driver_register(&soc_driver
);
3196 static void __exit
snd_soc_exit(void)
3198 #ifdef CONFIG_DEBUG_FS
3199 debugfs_remove_recursive(debugfs_root
);
3201 platform_driver_unregister(&soc_driver
);
3204 module_init(snd_soc_init
);
3205 module_exit(snd_soc_exit
);
3207 /* Module information */
3208 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3209 MODULE_DESCRIPTION("ALSA SoC Core");
3210 MODULE_LICENSE("GPL");
3211 MODULE_ALIAS("platform:soc-audio");