2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
28 #include <linux/bitops.h>
29 #include <linux/debugfs.h>
30 #include <linux/platform_device.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/soc-dapm.h>
36 #include <sound/initval.h>
38 static DEFINE_MUTEX(pcm_mutex
);
39 static DEFINE_MUTEX(io_mutex
);
40 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
42 #ifdef CONFIG_DEBUG_FS
43 static struct dentry
*debugfs_root
;
47 * This is a timeout to do a DAPM powerdown after a stream is closed().
48 * It can be used to eliminate pops between different playback streams, e.g.
49 * between two audio tracks.
51 static int pmdown_time
= 5000;
52 module_param(pmdown_time
, int, 0);
53 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
56 * This function forces any delayed work to be queued and run.
58 static int run_delayed_work(struct delayed_work
*dwork
)
62 /* cancel any work waiting to be queued. */
63 ret
= cancel_delayed_work(dwork
);
65 /* if there was any work waiting then we run it now and
66 * wait for it's completion */
68 schedule_delayed_work(dwork
, 0);
69 flush_scheduled_work();
74 #ifdef CONFIG_SND_SOC_AC97_BUS
75 /* unregister ac97 codec */
76 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
78 if (codec
->ac97
->dev
.bus
)
79 device_unregister(&codec
->ac97
->dev
);
83 /* stop no dev release warning */
84 static void soc_ac97_device_release(struct device
*dev
){}
86 /* register ac97 codec to bus */
87 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
91 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
92 codec
->ac97
->dev
.parent
= NULL
;
93 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
95 dev_set_name(&codec
->ac97
->dev
, "%d-%d:%s",
96 codec
->card
->number
, 0, codec
->name
);
97 err
= device_register(&codec
->ac97
->dev
);
99 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
100 codec
->ac97
->dev
.bus
= NULL
;
108 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
109 * then initialized and any private data can be allocated. This also calls
110 * startup for the cpu DAI, platform, machine and codec DAI.
112 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
114 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
115 struct snd_soc_device
*socdev
= rtd
->socdev
;
116 struct snd_soc_card
*card
= socdev
->card
;
117 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
118 struct snd_soc_dai_link
*machine
= rtd
->dai
;
119 struct snd_soc_platform
*platform
= card
->platform
;
120 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
121 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
124 mutex_lock(&pcm_mutex
);
126 /* startup the audio subsystem */
127 if (cpu_dai
->ops
.startup
) {
128 ret
= cpu_dai
->ops
.startup(substream
, cpu_dai
);
130 printk(KERN_ERR
"asoc: can't open interface %s\n",
136 if (platform
->pcm_ops
->open
) {
137 ret
= platform
->pcm_ops
->open(substream
);
139 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
144 if (codec_dai
->ops
.startup
) {
145 ret
= codec_dai
->ops
.startup(substream
, codec_dai
);
147 printk(KERN_ERR
"asoc: can't open codec %s\n",
153 if (machine
->ops
&& machine
->ops
->startup
) {
154 ret
= machine
->ops
->startup(substream
);
156 printk(KERN_ERR
"asoc: %s startup failed\n", machine
->name
);
161 /* Check that the codec and cpu DAI's are compatible */
162 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
163 runtime
->hw
.rate_min
=
164 max(codec_dai
->playback
.rate_min
,
165 cpu_dai
->playback
.rate_min
);
166 runtime
->hw
.rate_max
=
167 min(codec_dai
->playback
.rate_max
,
168 cpu_dai
->playback
.rate_max
);
169 runtime
->hw
.channels_min
=
170 max(codec_dai
->playback
.channels_min
,
171 cpu_dai
->playback
.channels_min
);
172 runtime
->hw
.channels_max
=
173 min(codec_dai
->playback
.channels_max
,
174 cpu_dai
->playback
.channels_max
);
175 runtime
->hw
.formats
=
176 codec_dai
->playback
.formats
& cpu_dai
->playback
.formats
;
178 codec_dai
->playback
.rates
& cpu_dai
->playback
.rates
;
180 runtime
->hw
.rate_min
=
181 max(codec_dai
->capture
.rate_min
,
182 cpu_dai
->capture
.rate_min
);
183 runtime
->hw
.rate_max
=
184 min(codec_dai
->capture
.rate_max
,
185 cpu_dai
->capture
.rate_max
);
186 runtime
->hw
.channels_min
=
187 max(codec_dai
->capture
.channels_min
,
188 cpu_dai
->capture
.channels_min
);
189 runtime
->hw
.channels_max
=
190 min(codec_dai
->capture
.channels_max
,
191 cpu_dai
->capture
.channels_max
);
192 runtime
->hw
.formats
=
193 codec_dai
->capture
.formats
& cpu_dai
->capture
.formats
;
195 codec_dai
->capture
.rates
& cpu_dai
->capture
.rates
;
198 snd_pcm_limit_hw_rates(runtime
);
199 if (!runtime
->hw
.rates
) {
200 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
201 codec_dai
->name
, cpu_dai
->name
);
204 if (!runtime
->hw
.formats
) {
205 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
206 codec_dai
->name
, cpu_dai
->name
);
209 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
210 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
211 codec_dai
->name
, cpu_dai
->name
);
215 pr_debug("asoc: %s <-> %s info:\n", codec_dai
->name
, cpu_dai
->name
);
216 pr_debug("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
217 pr_debug("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
218 runtime
->hw
.channels_max
);
219 pr_debug("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
220 runtime
->hw
.rate_max
);
222 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
223 cpu_dai
->playback
.active
= codec_dai
->playback
.active
= 1;
225 cpu_dai
->capture
.active
= codec_dai
->capture
.active
= 1;
226 cpu_dai
->active
= codec_dai
->active
= 1;
227 cpu_dai
->runtime
= runtime
;
228 socdev
->codec
->active
++;
229 mutex_unlock(&pcm_mutex
);
233 if (machine
->ops
&& machine
->ops
->shutdown
)
234 machine
->ops
->shutdown(substream
);
237 if (platform
->pcm_ops
->close
)
238 platform
->pcm_ops
->close(substream
);
241 if (cpu_dai
->ops
.shutdown
)
242 cpu_dai
->ops
.shutdown(substream
, cpu_dai
);
244 mutex_unlock(&pcm_mutex
);
249 * Power down the audio subsystem pmdown_time msecs after close is called.
250 * This is to ensure there are no pops or clicks in between any music tracks
251 * due to DAPM power cycling.
253 static void close_delayed_work(struct work_struct
*work
)
255 struct snd_soc_card
*card
= container_of(work
, struct snd_soc_card
,
257 struct snd_soc_device
*socdev
= card
->socdev
;
258 struct snd_soc_codec
*codec
= socdev
->codec
;
259 struct snd_soc_dai
*codec_dai
;
262 mutex_lock(&pcm_mutex
);
263 for (i
= 0; i
< codec
->num_dai
; i
++) {
264 codec_dai
= &codec
->dai
[i
];
266 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
267 codec_dai
->playback
.stream_name
,
268 codec_dai
->playback
.active
? "active" : "inactive",
269 codec_dai
->pop_wait
? "yes" : "no");
271 /* are we waiting on this codec DAI stream */
272 if (codec_dai
->pop_wait
== 1) {
274 /* Reduce power if no longer active */
275 if (codec
->active
== 0) {
276 pr_debug("pop wq D1 %s %s\n", codec
->name
,
277 codec_dai
->playback
.stream_name
);
278 snd_soc_dapm_set_bias_level(socdev
,
279 SND_SOC_BIAS_PREPARE
);
282 codec_dai
->pop_wait
= 0;
283 snd_soc_dapm_stream_event(codec
,
284 codec_dai
->playback
.stream_name
,
285 SND_SOC_DAPM_STREAM_STOP
);
287 /* Fall into standby if no longer active */
288 if (codec
->active
== 0) {
289 pr_debug("pop wq D3 %s %s\n", codec
->name
,
290 codec_dai
->playback
.stream_name
);
291 snd_soc_dapm_set_bias_level(socdev
,
292 SND_SOC_BIAS_STANDBY
);
296 mutex_unlock(&pcm_mutex
);
300 * Called by ALSA when a PCM substream is closed. Private data can be
301 * freed here. The cpu DAI, codec DAI, machine and platform are also
304 static int soc_codec_close(struct snd_pcm_substream
*substream
)
306 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
307 struct snd_soc_device
*socdev
= rtd
->socdev
;
308 struct snd_soc_card
*card
= socdev
->card
;
309 struct snd_soc_dai_link
*machine
= rtd
->dai
;
310 struct snd_soc_platform
*platform
= card
->platform
;
311 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
312 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
313 struct snd_soc_codec
*codec
= socdev
->codec
;
315 mutex_lock(&pcm_mutex
);
317 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
318 cpu_dai
->playback
.active
= codec_dai
->playback
.active
= 0;
320 cpu_dai
->capture
.active
= codec_dai
->capture
.active
= 0;
322 if (codec_dai
->playback
.active
== 0 &&
323 codec_dai
->capture
.active
== 0) {
324 cpu_dai
->active
= codec_dai
->active
= 0;
328 /* Muting the DAC suppresses artifacts caused during digital
329 * shutdown, for example from stopping clocks.
331 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
332 snd_soc_dai_digital_mute(codec_dai
, 1);
334 if (cpu_dai
->ops
.shutdown
)
335 cpu_dai
->ops
.shutdown(substream
, cpu_dai
);
337 if (codec_dai
->ops
.shutdown
)
338 codec_dai
->ops
.shutdown(substream
, codec_dai
);
340 if (machine
->ops
&& machine
->ops
->shutdown
)
341 machine
->ops
->shutdown(substream
);
343 if (platform
->pcm_ops
->close
)
344 platform
->pcm_ops
->close(substream
);
345 cpu_dai
->runtime
= NULL
;
347 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
348 /* start delayed pop wq here for playback streams */
349 codec_dai
->pop_wait
= 1;
350 schedule_delayed_work(&card
->delayed_work
,
351 msecs_to_jiffies(pmdown_time
));
353 /* capture streams can be powered down now */
354 snd_soc_dapm_stream_event(codec
,
355 codec_dai
->capture
.stream_name
,
356 SND_SOC_DAPM_STREAM_STOP
);
358 if (codec
->active
== 0 && codec_dai
->pop_wait
== 0)
359 snd_soc_dapm_set_bias_level(socdev
,
360 SND_SOC_BIAS_STANDBY
);
363 mutex_unlock(&pcm_mutex
);
368 * Called by ALSA when the PCM substream is prepared, can set format, sample
369 * rate, etc. This function is non atomic and can be called multiple times,
370 * it can refer to the runtime info.
372 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
374 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
375 struct snd_soc_device
*socdev
= rtd
->socdev
;
376 struct snd_soc_card
*card
= socdev
->card
;
377 struct snd_soc_dai_link
*machine
= rtd
->dai
;
378 struct snd_soc_platform
*platform
= card
->platform
;
379 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
380 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
381 struct snd_soc_codec
*codec
= socdev
->codec
;
384 mutex_lock(&pcm_mutex
);
386 if (machine
->ops
&& machine
->ops
->prepare
) {
387 ret
= machine
->ops
->prepare(substream
);
389 printk(KERN_ERR
"asoc: machine prepare error\n");
394 if (platform
->pcm_ops
->prepare
) {
395 ret
= platform
->pcm_ops
->prepare(substream
);
397 printk(KERN_ERR
"asoc: platform prepare error\n");
402 if (codec_dai
->ops
.prepare
) {
403 ret
= codec_dai
->ops
.prepare(substream
, codec_dai
);
405 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
410 if (cpu_dai
->ops
.prepare
) {
411 ret
= cpu_dai
->ops
.prepare(substream
, cpu_dai
);
413 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
418 /* cancel any delayed stream shutdown that is pending */
419 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
420 codec_dai
->pop_wait
) {
421 codec_dai
->pop_wait
= 0;
422 cancel_delayed_work(&card
->delayed_work
);
425 /* do we need to power up codec */
426 if (codec
->bias_level
!= SND_SOC_BIAS_ON
) {
427 snd_soc_dapm_set_bias_level(socdev
,
428 SND_SOC_BIAS_PREPARE
);
430 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
431 snd_soc_dapm_stream_event(codec
,
432 codec_dai
->playback
.stream_name
,
433 SND_SOC_DAPM_STREAM_START
);
435 snd_soc_dapm_stream_event(codec
,
436 codec_dai
->capture
.stream_name
,
437 SND_SOC_DAPM_STREAM_START
);
439 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_ON
);
440 snd_soc_dai_digital_mute(codec_dai
, 0);
443 /* codec already powered - power on widgets */
444 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
445 snd_soc_dapm_stream_event(codec
,
446 codec_dai
->playback
.stream_name
,
447 SND_SOC_DAPM_STREAM_START
);
449 snd_soc_dapm_stream_event(codec
,
450 codec_dai
->capture
.stream_name
,
451 SND_SOC_DAPM_STREAM_START
);
453 snd_soc_dai_digital_mute(codec_dai
, 0);
457 mutex_unlock(&pcm_mutex
);
462 * Called by ALSA when the hardware params are set by application. This
463 * function can also be called multiple times and can allocate buffers
464 * (using snd_pcm_lib_* ). It's non-atomic.
466 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
467 struct snd_pcm_hw_params
*params
)
469 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
470 struct snd_soc_device
*socdev
= rtd
->socdev
;
471 struct snd_soc_dai_link
*machine
= rtd
->dai
;
472 struct snd_soc_card
*card
= socdev
->card
;
473 struct snd_soc_platform
*platform
= card
->platform
;
474 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
475 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
478 mutex_lock(&pcm_mutex
);
480 if (machine
->ops
&& machine
->ops
->hw_params
) {
481 ret
= machine
->ops
->hw_params(substream
, params
);
483 printk(KERN_ERR
"asoc: machine hw_params failed\n");
488 if (codec_dai
->ops
.hw_params
) {
489 ret
= codec_dai
->ops
.hw_params(substream
, params
, codec_dai
);
491 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
497 if (cpu_dai
->ops
.hw_params
) {
498 ret
= cpu_dai
->ops
.hw_params(substream
, params
, cpu_dai
);
500 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
506 if (platform
->pcm_ops
->hw_params
) {
507 ret
= platform
->pcm_ops
->hw_params(substream
, params
);
509 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
516 mutex_unlock(&pcm_mutex
);
520 if (cpu_dai
->ops
.hw_free
)
521 cpu_dai
->ops
.hw_free(substream
, cpu_dai
);
524 if (codec_dai
->ops
.hw_free
)
525 codec_dai
->ops
.hw_free(substream
, codec_dai
);
528 if (machine
->ops
&& machine
->ops
->hw_free
)
529 machine
->ops
->hw_free(substream
);
531 mutex_unlock(&pcm_mutex
);
536 * Free's resources allocated by hw_params, can be called multiple times
538 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
540 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
541 struct snd_soc_device
*socdev
= rtd
->socdev
;
542 struct snd_soc_dai_link
*machine
= rtd
->dai
;
543 struct snd_soc_card
*card
= socdev
->card
;
544 struct snd_soc_platform
*platform
= card
->platform
;
545 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
546 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
547 struct snd_soc_codec
*codec
= socdev
->codec
;
549 mutex_lock(&pcm_mutex
);
551 /* apply codec digital mute */
553 snd_soc_dai_digital_mute(codec_dai
, 1);
555 /* free any machine hw params */
556 if (machine
->ops
&& machine
->ops
->hw_free
)
557 machine
->ops
->hw_free(substream
);
559 /* free any DMA resources */
560 if (platform
->pcm_ops
->hw_free
)
561 platform
->pcm_ops
->hw_free(substream
);
563 /* now free hw params for the DAI's */
564 if (codec_dai
->ops
.hw_free
)
565 codec_dai
->ops
.hw_free(substream
, codec_dai
);
567 if (cpu_dai
->ops
.hw_free
)
568 cpu_dai
->ops
.hw_free(substream
, cpu_dai
);
570 mutex_unlock(&pcm_mutex
);
574 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
576 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
577 struct snd_soc_device
*socdev
= rtd
->socdev
;
578 struct snd_soc_card
*card
= socdev
->card
;
579 struct snd_soc_dai_link
*machine
= rtd
->dai
;
580 struct snd_soc_platform
*platform
= card
->platform
;
581 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
582 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
585 if (codec_dai
->ops
.trigger
) {
586 ret
= codec_dai
->ops
.trigger(substream
, cmd
, codec_dai
);
591 if (platform
->pcm_ops
->trigger
) {
592 ret
= platform
->pcm_ops
->trigger(substream
, cmd
);
597 if (cpu_dai
->ops
.trigger
) {
598 ret
= cpu_dai
->ops
.trigger(substream
, cmd
, cpu_dai
);
605 /* ASoC PCM operations */
606 static struct snd_pcm_ops soc_pcm_ops
= {
607 .open
= soc_pcm_open
,
608 .close
= soc_codec_close
,
609 .hw_params
= soc_pcm_hw_params
,
610 .hw_free
= soc_pcm_hw_free
,
611 .prepare
= soc_pcm_prepare
,
612 .trigger
= soc_pcm_trigger
,
616 /* powers down audio subsystem for suspend */
617 static int soc_suspend(struct platform_device
*pdev
, pm_message_t state
)
619 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
620 struct snd_soc_card
*card
= socdev
->card
;
621 struct snd_soc_platform
*platform
= card
->platform
;
622 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
623 struct snd_soc_codec
*codec
= socdev
->codec
;
626 /* Due to the resume being scheduled into a workqueue we could
627 * suspend before that's finished - wait for it to complete.
629 snd_power_lock(codec
->card
);
630 snd_power_wait(codec
->card
, SNDRV_CTL_POWER_D0
);
631 snd_power_unlock(codec
->card
);
633 /* we're going to block userspace touching us until resume completes */
634 snd_power_change_state(codec
->card
, SNDRV_CTL_POWER_D3hot
);
636 /* mute any active DAC's */
637 for (i
= 0; i
< card
->num_links
; i
++) {
638 struct snd_soc_dai
*dai
= card
->dai_link
[i
].codec_dai
;
639 if (dai
->ops
.digital_mute
&& dai
->playback
.active
)
640 dai
->ops
.digital_mute(dai
, 1);
643 /* suspend all pcms */
644 for (i
= 0; i
< card
->num_links
; i
++)
645 snd_pcm_suspend_all(card
->dai_link
[i
].pcm
);
647 if (card
->suspend_pre
)
648 card
->suspend_pre(pdev
, state
);
650 for (i
= 0; i
< card
->num_links
; i
++) {
651 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
652 if (cpu_dai
->suspend
&& !cpu_dai
->ac97_control
)
653 cpu_dai
->suspend(pdev
, cpu_dai
);
654 if (platform
->suspend
)
655 platform
->suspend(cpu_dai
);
658 /* close any waiting streams and save state */
659 run_delayed_work(&card
->delayed_work
);
660 codec
->suspend_bias_level
= codec
->bias_level
;
662 for (i
= 0; i
< codec
->num_dai
; i
++) {
663 char *stream
= codec
->dai
[i
].playback
.stream_name
;
665 snd_soc_dapm_stream_event(codec
, stream
,
666 SND_SOC_DAPM_STREAM_SUSPEND
);
667 stream
= codec
->dai
[i
].capture
.stream_name
;
669 snd_soc_dapm_stream_event(codec
, stream
,
670 SND_SOC_DAPM_STREAM_SUSPEND
);
673 if (codec_dev
->suspend
)
674 codec_dev
->suspend(pdev
, state
);
676 for (i
= 0; i
< card
->num_links
; i
++) {
677 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
678 if (cpu_dai
->suspend
&& cpu_dai
->ac97_control
)
679 cpu_dai
->suspend(pdev
, cpu_dai
);
682 if (card
->suspend_post
)
683 card
->suspend_post(pdev
, state
);
688 /* deferred resume work, so resume can complete before we finished
689 * setting our codec back up, which can be very slow on I2C
691 static void soc_resume_deferred(struct work_struct
*work
)
693 struct snd_soc_card
*card
= container_of(work
,
695 deferred_resume_work
);
696 struct snd_soc_device
*socdev
= card
->socdev
;
697 struct snd_soc_platform
*platform
= card
->platform
;
698 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
699 struct snd_soc_codec
*codec
= socdev
->codec
;
700 struct platform_device
*pdev
= to_platform_device(socdev
->dev
);
703 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
704 * so userspace apps are blocked from touching us
707 dev_dbg(socdev
->dev
, "starting resume work\n");
709 if (card
->resume_pre
)
710 card
->resume_pre(pdev
);
712 for (i
= 0; i
< card
->num_links
; i
++) {
713 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
714 if (cpu_dai
->resume
&& cpu_dai
->ac97_control
)
715 cpu_dai
->resume(pdev
, cpu_dai
);
718 if (codec_dev
->resume
)
719 codec_dev
->resume(pdev
);
721 for (i
= 0; i
< codec
->num_dai
; i
++) {
722 char *stream
= codec
->dai
[i
].playback
.stream_name
;
724 snd_soc_dapm_stream_event(codec
, stream
,
725 SND_SOC_DAPM_STREAM_RESUME
);
726 stream
= codec
->dai
[i
].capture
.stream_name
;
728 snd_soc_dapm_stream_event(codec
, stream
,
729 SND_SOC_DAPM_STREAM_RESUME
);
732 /* unmute any active DACs */
733 for (i
= 0; i
< card
->num_links
; i
++) {
734 struct snd_soc_dai
*dai
= card
->dai_link
[i
].codec_dai
;
735 if (dai
->ops
.digital_mute
&& dai
->playback
.active
)
736 dai
->ops
.digital_mute(dai
, 0);
739 for (i
= 0; i
< card
->num_links
; i
++) {
740 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
741 if (cpu_dai
->resume
&& !cpu_dai
->ac97_control
)
742 cpu_dai
->resume(pdev
, cpu_dai
);
743 if (platform
->resume
)
744 platform
->resume(cpu_dai
);
747 if (card
->resume_post
)
748 card
->resume_post(pdev
);
750 dev_dbg(socdev
->dev
, "resume work completed\n");
752 /* userspace can access us now we are back as we were before */
753 snd_power_change_state(codec
->card
, SNDRV_CTL_POWER_D0
);
756 /* powers up audio subsystem after a suspend */
757 static int soc_resume(struct platform_device
*pdev
)
759 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
760 struct snd_soc_card
*card
= socdev
->card
;
762 dev_dbg(socdev
->dev
, "scheduling resume work\n");
764 if (!schedule_work(&card
->deferred_resume_work
))
765 dev_err(socdev
->dev
, "resume work item may be lost\n");
771 #define soc_suspend NULL
772 #define soc_resume NULL
775 /* probes a new socdev */
776 static int soc_probe(struct platform_device
*pdev
)
779 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
780 struct snd_soc_card
*card
= socdev
->card
;
781 struct snd_soc_platform
*platform
= card
->platform
;
782 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
784 /* Bodge while we push things out of socdev */
785 card
->socdev
= socdev
;
788 ret
= card
->probe(pdev
);
793 for (i
= 0; i
< card
->num_links
; i
++) {
794 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
795 if (cpu_dai
->probe
) {
796 ret
= cpu_dai
->probe(pdev
, cpu_dai
);
802 if (codec_dev
->probe
) {
803 ret
= codec_dev
->probe(pdev
);
808 if (platform
->probe
) {
809 ret
= platform
->probe(pdev
);
814 /* DAPM stream work */
815 INIT_DELAYED_WORK(&card
->delayed_work
, close_delayed_work
);
817 /* deferred resume work */
818 INIT_WORK(&card
->deferred_resume_work
, soc_resume_deferred
);
824 if (codec_dev
->remove
)
825 codec_dev
->remove(pdev
);
828 for (i
--; i
>= 0; i
--) {
829 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
831 cpu_dai
->remove(pdev
, cpu_dai
);
840 /* removes a socdev */
841 static int soc_remove(struct platform_device
*pdev
)
844 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
845 struct snd_soc_card
*card
= socdev
->card
;
846 struct snd_soc_platform
*platform
= card
->platform
;
847 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
849 run_delayed_work(&card
->delayed_work
);
851 if (platform
->remove
)
852 platform
->remove(pdev
);
854 if (codec_dev
->remove
)
855 codec_dev
->remove(pdev
);
857 for (i
= 0; i
< card
->num_links
; i
++) {
858 struct snd_soc_dai
*cpu_dai
= card
->dai_link
[i
].cpu_dai
;
860 cpu_dai
->remove(pdev
, cpu_dai
);
869 /* ASoC platform driver */
870 static struct platform_driver soc_driver
= {
873 .owner
= THIS_MODULE
,
876 .remove
= soc_remove
,
877 .suspend
= soc_suspend
,
878 .resume
= soc_resume
,
881 /* create a new pcm */
882 static int soc_new_pcm(struct snd_soc_device
*socdev
,
883 struct snd_soc_dai_link
*dai_link
, int num
)
885 struct snd_soc_codec
*codec
= socdev
->codec
;
886 struct snd_soc_card
*card
= socdev
->card
;
887 struct snd_soc_platform
*platform
= card
->platform
;
888 struct snd_soc_dai
*codec_dai
= dai_link
->codec_dai
;
889 struct snd_soc_dai
*cpu_dai
= dai_link
->cpu_dai
;
890 struct snd_soc_pcm_runtime
*rtd
;
893 int ret
= 0, playback
= 0, capture
= 0;
895 rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
), GFP_KERNEL
);
900 rtd
->socdev
= socdev
;
901 codec_dai
->codec
= socdev
->codec
;
903 /* check client and interface hw capabilities */
904 sprintf(new_name
, "%s %s-%d", dai_link
->stream_name
, codec_dai
->name
,
907 if (codec_dai
->playback
.channels_min
)
909 if (codec_dai
->capture
.channels_min
)
912 ret
= snd_pcm_new(codec
->card
, new_name
, codec
->pcm_devs
++, playback
,
915 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n",
922 pcm
->private_data
= rtd
;
923 soc_pcm_ops
.mmap
= platform
->pcm_ops
->mmap
;
924 soc_pcm_ops
.pointer
= platform
->pcm_ops
->pointer
;
925 soc_pcm_ops
.ioctl
= platform
->pcm_ops
->ioctl
;
926 soc_pcm_ops
.copy
= platform
->pcm_ops
->copy
;
927 soc_pcm_ops
.silence
= platform
->pcm_ops
->silence
;
928 soc_pcm_ops
.ack
= platform
->pcm_ops
->ack
;
929 soc_pcm_ops
.page
= platform
->pcm_ops
->page
;
932 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
935 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
937 ret
= platform
->pcm_new(codec
->card
, codec_dai
, pcm
);
939 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
944 pcm
->private_free
= platform
->pcm_free
;
945 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
950 /* codec register dump */
951 static ssize_t
soc_codec_reg_show(struct snd_soc_device
*devdata
, char *buf
)
953 struct snd_soc_codec
*codec
= devdata
->codec
;
954 int i
, step
= 1, count
= 0;
956 if (!codec
->reg_cache_size
)
959 if (codec
->reg_cache_step
)
960 step
= codec
->reg_cache_step
;
962 count
+= sprintf(buf
, "%s registers\n", codec
->name
);
963 for (i
= 0; i
< codec
->reg_cache_size
; i
+= step
) {
964 count
+= sprintf(buf
+ count
, "%2x: ", i
);
965 if (count
>= PAGE_SIZE
- 1)
968 if (codec
->display_register
)
969 count
+= codec
->display_register(codec
, buf
+ count
,
970 PAGE_SIZE
- count
, i
);
972 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
,
973 "%4x", codec
->read(codec
, i
));
975 if (count
>= PAGE_SIZE
- 1)
978 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
979 if (count
>= PAGE_SIZE
- 1)
983 /* Truncate count; min() would cause a warning */
984 if (count
>= PAGE_SIZE
)
985 count
= PAGE_SIZE
- 1;
989 static ssize_t
codec_reg_show(struct device
*dev
,
990 struct device_attribute
*attr
, char *buf
)
992 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
993 return soc_codec_reg_show(devdata
, buf
);
996 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
998 #ifdef CONFIG_DEBUG_FS
999 static int codec_reg_open_file(struct inode
*inode
, struct file
*file
)
1001 file
->private_data
= inode
->i_private
;
1005 static ssize_t
codec_reg_read_file(struct file
*file
, char __user
*user_buf
,
1006 size_t count
, loff_t
*ppos
)
1009 struct snd_soc_codec
*codec
= file
->private_data
;
1010 struct device
*card_dev
= codec
->card
->dev
;
1011 struct snd_soc_device
*devdata
= card_dev
->driver_data
;
1012 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1015 ret
= soc_codec_reg_show(devdata
, buf
);
1017 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1022 static ssize_t
codec_reg_write_file(struct file
*file
,
1023 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
1028 unsigned long reg
, value
;
1030 struct snd_soc_codec
*codec
= file
->private_data
;
1032 buf_size
= min(count
, (sizeof(buf
)-1));
1033 if (copy_from_user(buf
, user_buf
, buf_size
))
1037 if (codec
->reg_cache_step
)
1038 step
= codec
->reg_cache_step
;
1040 while (*start
== ' ')
1042 reg
= simple_strtoul(start
, &start
, 16);
1043 if ((reg
>= codec
->reg_cache_size
) || (reg
% step
))
1045 while (*start
== ' ')
1047 if (strict_strtoul(start
, 16, &value
))
1049 codec
->write(codec
, reg
, value
);
1053 static const struct file_operations codec_reg_fops
= {
1054 .open
= codec_reg_open_file
,
1055 .read
= codec_reg_read_file
,
1056 .write
= codec_reg_write_file
,
1059 static void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
1061 codec
->debugfs_reg
= debugfs_create_file("codec_reg", 0644,
1062 debugfs_root
, codec
,
1064 if (!codec
->debugfs_reg
)
1066 "ASoC: Failed to create codec register debugfs file\n");
1068 codec
->debugfs_pop_time
= debugfs_create_u32("dapm_pop_time", 0744,
1071 if (!codec
->debugfs_pop_time
)
1073 "Failed to create pop time debugfs file\n");
1076 static void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
1078 debugfs_remove(codec
->debugfs_pop_time
);
1079 debugfs_remove(codec
->debugfs_reg
);
1084 static inline void soc_init_codec_debugfs(struct snd_soc_codec
*codec
)
1088 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec
*codec
)
1094 * snd_soc_new_ac97_codec - initailise AC97 device
1095 * @codec: audio codec
1096 * @ops: AC97 bus operations
1097 * @num: AC97 codec number
1099 * Initialises AC97 codec resources for use by ad-hoc devices only.
1101 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1102 struct snd_ac97_bus_ops
*ops
, int num
)
1104 mutex_lock(&codec
->mutex
);
1106 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1107 if (codec
->ac97
== NULL
) {
1108 mutex_unlock(&codec
->mutex
);
1112 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1113 if (codec
->ac97
->bus
== NULL
) {
1116 mutex_unlock(&codec
->mutex
);
1120 codec
->ac97
->bus
->ops
= ops
;
1121 codec
->ac97
->num
= num
;
1122 mutex_unlock(&codec
->mutex
);
1125 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1128 * snd_soc_free_ac97_codec - free AC97 codec device
1129 * @codec: audio codec
1131 * Frees AC97 codec device resources.
1133 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1135 mutex_lock(&codec
->mutex
);
1136 kfree(codec
->ac97
->bus
);
1139 mutex_unlock(&codec
->mutex
);
1141 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1144 * snd_soc_update_bits - update codec register bits
1145 * @codec: audio codec
1146 * @reg: codec register
1147 * @mask: register mask
1150 * Writes new register value.
1152 * Returns 1 for change else 0.
1154 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1155 unsigned short mask
, unsigned short value
)
1158 unsigned short old
, new;
1160 mutex_lock(&io_mutex
);
1161 old
= snd_soc_read(codec
, reg
);
1162 new = (old
& ~mask
) | value
;
1163 change
= old
!= new;
1165 snd_soc_write(codec
, reg
, new);
1167 mutex_unlock(&io_mutex
);
1170 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1173 * snd_soc_test_bits - test register for change
1174 * @codec: audio codec
1175 * @reg: codec register
1176 * @mask: register mask
1179 * Tests a register with a new value and checks if the new value is
1180 * different from the old value.
1182 * Returns 1 for change else 0.
1184 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1185 unsigned short mask
, unsigned short value
)
1188 unsigned short old
, new;
1190 mutex_lock(&io_mutex
);
1191 old
= snd_soc_read(codec
, reg
);
1192 new = (old
& ~mask
) | value
;
1193 change
= old
!= new;
1194 mutex_unlock(&io_mutex
);
1198 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1201 * snd_soc_new_pcms - create new sound card and pcms
1202 * @socdev: the SoC audio device
1204 * Create a new sound card based upon the codec and interface pcms.
1206 * Returns 0 for success, else error.
1208 int snd_soc_new_pcms(struct snd_soc_device
*socdev
, int idx
, const char *xid
)
1210 struct snd_soc_codec
*codec
= socdev
->codec
;
1211 struct snd_soc_card
*card
= socdev
->card
;
1214 mutex_lock(&codec
->mutex
);
1216 /* register a sound card */
1217 codec
->card
= snd_card_new(idx
, xid
, codec
->owner
, 0);
1219 printk(KERN_ERR
"asoc: can't create sound card for codec %s\n",
1221 mutex_unlock(&codec
->mutex
);
1225 codec
->card
->dev
= socdev
->dev
;
1226 codec
->card
->private_data
= codec
;
1227 strncpy(codec
->card
->driver
, codec
->name
, sizeof(codec
->card
->driver
));
1229 /* create the pcms */
1230 for (i
= 0; i
< card
->num_links
; i
++) {
1231 ret
= soc_new_pcm(socdev
, &card
->dai_link
[i
], i
);
1233 printk(KERN_ERR
"asoc: can't create pcm %s\n",
1234 card
->dai_link
[i
].stream_name
);
1235 mutex_unlock(&codec
->mutex
);
1240 mutex_unlock(&codec
->mutex
);
1243 EXPORT_SYMBOL_GPL(snd_soc_new_pcms
);
1246 * snd_soc_init_card - register sound card
1247 * @socdev: the SoC audio device
1249 * Register a SoC sound card. Also registers an AC97 device if the
1250 * codec is AC97 for ad hoc devices.
1252 * Returns 0 for success, else error.
1254 int snd_soc_init_card(struct snd_soc_device
*socdev
)
1256 struct snd_soc_codec
*codec
= socdev
->codec
;
1257 struct snd_soc_card
*card
= socdev
->card
;
1258 int ret
= 0, i
, ac97
= 0, err
= 0;
1260 for (i
= 0; i
< card
->num_links
; i
++) {
1261 if (card
->dai_link
[i
].init
) {
1262 err
= card
->dai_link
[i
].init(codec
);
1264 printk(KERN_ERR
"asoc: failed to init %s\n",
1265 card
->dai_link
[i
].stream_name
);
1269 if (card
->dai_link
[i
].codec_dai
->ac97_control
)
1272 snprintf(codec
->card
->shortname
, sizeof(codec
->card
->shortname
),
1274 snprintf(codec
->card
->longname
, sizeof(codec
->card
->longname
),
1275 "%s (%s)", card
->name
, codec
->name
);
1277 ret
= snd_card_register(codec
->card
);
1279 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n",
1284 mutex_lock(&codec
->mutex
);
1285 #ifdef CONFIG_SND_SOC_AC97_BUS
1287 ret
= soc_ac97_dev_register(codec
);
1289 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1290 snd_card_free(codec
->card
);
1291 mutex_unlock(&codec
->mutex
);
1297 err
= snd_soc_dapm_sys_add(socdev
->dev
);
1299 printk(KERN_WARNING
"asoc: failed to add dapm sysfs entries\n");
1301 err
= device_create_file(socdev
->dev
, &dev_attr_codec_reg
);
1303 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1305 soc_init_codec_debugfs(socdev
->codec
);
1306 mutex_unlock(&codec
->mutex
);
1311 EXPORT_SYMBOL_GPL(snd_soc_init_card
);
1314 * snd_soc_free_pcms - free sound card and pcms
1315 * @socdev: the SoC audio device
1317 * Frees sound card and pcms associated with the socdev.
1318 * Also unregister the codec if it is an AC97 device.
1320 void snd_soc_free_pcms(struct snd_soc_device
*socdev
)
1322 struct snd_soc_codec
*codec
= socdev
->codec
;
1323 #ifdef CONFIG_SND_SOC_AC97_BUS
1324 struct snd_soc_dai
*codec_dai
;
1328 mutex_lock(&codec
->mutex
);
1329 soc_cleanup_codec_debugfs(socdev
->codec
);
1330 #ifdef CONFIG_SND_SOC_AC97_BUS
1331 for (i
= 0; i
< codec
->num_dai
; i
++) {
1332 codec_dai
= &codec
->dai
[i
];
1333 if (codec_dai
->ac97_control
&& codec
->ac97
) {
1334 soc_ac97_dev_unregister(codec
);
1342 snd_card_free(codec
->card
);
1343 device_remove_file(socdev
->dev
, &dev_attr_codec_reg
);
1344 mutex_unlock(&codec
->mutex
);
1346 EXPORT_SYMBOL_GPL(snd_soc_free_pcms
);
1349 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1350 * @substream: the pcm substream
1351 * @hw: the hardware parameters
1353 * Sets the substream runtime hardware parameters.
1355 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1356 const struct snd_pcm_hardware
*hw
)
1358 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1359 runtime
->hw
.info
= hw
->info
;
1360 runtime
->hw
.formats
= hw
->formats
;
1361 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1362 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1363 runtime
->hw
.periods_min
= hw
->periods_min
;
1364 runtime
->hw
.periods_max
= hw
->periods_max
;
1365 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1366 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1369 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1372 * snd_soc_cnew - create new control
1373 * @_template: control template
1374 * @data: control private data
1375 * @lnng_name: control long name
1377 * Create a new mixer control from a template control.
1379 * Returns 0 for success, else error.
1381 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1382 void *data
, char *long_name
)
1384 struct snd_kcontrol_new
template;
1386 memcpy(&template, _template
, sizeof(template));
1388 template.name
= long_name
;
1391 return snd_ctl_new1(&template, data
);
1393 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
1396 * snd_soc_info_enum_double - enumerated double mixer info callback
1397 * @kcontrol: mixer control
1398 * @uinfo: control element information
1400 * Callback to provide information about a double enumerated
1403 * Returns 0 for success.
1405 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
1406 struct snd_ctl_elem_info
*uinfo
)
1408 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1410 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1411 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
1412 uinfo
->value
.enumerated
.items
= e
->max
;
1414 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
1415 uinfo
->value
.enumerated
.item
= e
->max
- 1;
1416 strcpy(uinfo
->value
.enumerated
.name
,
1417 e
->texts
[uinfo
->value
.enumerated
.item
]);
1420 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
1423 * snd_soc_get_enum_double - enumerated double mixer get callback
1424 * @kcontrol: mixer control
1425 * @uinfo: control element information
1427 * Callback to get the value of a double enumerated mixer.
1429 * Returns 0 for success.
1431 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
1432 struct snd_ctl_elem_value
*ucontrol
)
1434 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1435 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1436 unsigned short val
, bitmask
;
1438 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1440 val
= snd_soc_read(codec
, e
->reg
);
1441 ucontrol
->value
.enumerated
.item
[0]
1442 = (val
>> e
->shift_l
) & (bitmask
- 1);
1443 if (e
->shift_l
!= e
->shift_r
)
1444 ucontrol
->value
.enumerated
.item
[1] =
1445 (val
>> e
->shift_r
) & (bitmask
- 1);
1449 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
1452 * snd_soc_put_enum_double - enumerated double mixer put callback
1453 * @kcontrol: mixer control
1454 * @uinfo: control element information
1456 * Callback to set the value of a double enumerated mixer.
1458 * Returns 0 for success.
1460 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
1461 struct snd_ctl_elem_value
*ucontrol
)
1463 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1464 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1466 unsigned short mask
, bitmask
;
1468 for (bitmask
= 1; bitmask
< e
->max
; bitmask
<<= 1)
1470 if (ucontrol
->value
.enumerated
.item
[0] > e
->max
- 1)
1472 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
1473 mask
= (bitmask
- 1) << e
->shift_l
;
1474 if (e
->shift_l
!= e
->shift_r
) {
1475 if (ucontrol
->value
.enumerated
.item
[1] > e
->max
- 1)
1477 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1478 mask
|= (bitmask
- 1) << e
->shift_r
;
1481 return snd_soc_update_bits(codec
, e
->reg
, mask
, val
);
1483 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
1486 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1487 * @kcontrol: mixer control
1488 * @uinfo: control element information
1490 * Callback to provide information about an external enumerated
1493 * Returns 0 for success.
1495 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
1496 struct snd_ctl_elem_info
*uinfo
)
1498 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1500 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1502 uinfo
->value
.enumerated
.items
= e
->max
;
1504 if (uinfo
->value
.enumerated
.item
> e
->max
- 1)
1505 uinfo
->value
.enumerated
.item
= e
->max
- 1;
1506 strcpy(uinfo
->value
.enumerated
.name
,
1507 e
->texts
[uinfo
->value
.enumerated
.item
]);
1510 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
1513 * snd_soc_info_volsw_ext - external single mixer info callback
1514 * @kcontrol: mixer control
1515 * @uinfo: control element information
1517 * Callback to provide information about a single external mixer control.
1519 * Returns 0 for success.
1521 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
1522 struct snd_ctl_elem_info
*uinfo
)
1524 int max
= kcontrol
->private_value
;
1527 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1529 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1532 uinfo
->value
.integer
.min
= 0;
1533 uinfo
->value
.integer
.max
= max
;
1536 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
1539 * snd_soc_info_volsw - single mixer info callback
1540 * @kcontrol: mixer control
1541 * @uinfo: control element information
1543 * Callback to provide information about a single mixer control.
1545 * Returns 0 for success.
1547 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
1548 struct snd_ctl_elem_info
*uinfo
)
1550 struct soc_mixer_control
*mc
=
1551 (struct soc_mixer_control
*)kcontrol
->private_value
;
1553 unsigned int shift
= mc
->shift
;
1554 unsigned int rshift
= mc
->rshift
;
1557 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1559 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1561 uinfo
->count
= shift
== rshift
? 1 : 2;
1562 uinfo
->value
.integer
.min
= 0;
1563 uinfo
->value
.integer
.max
= max
;
1566 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
1569 * snd_soc_get_volsw - single mixer get callback
1570 * @kcontrol: mixer control
1571 * @uinfo: control element information
1573 * Callback to get the value of a single mixer control.
1575 * Returns 0 for success.
1577 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
1578 struct snd_ctl_elem_value
*ucontrol
)
1580 struct soc_mixer_control
*mc
=
1581 (struct soc_mixer_control
*)kcontrol
->private_value
;
1582 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1583 unsigned int reg
= mc
->reg
;
1584 unsigned int shift
= mc
->shift
;
1585 unsigned int rshift
= mc
->rshift
;
1587 unsigned int mask
= (1 << fls(max
)) - 1;
1588 unsigned int invert
= mc
->invert
;
1590 ucontrol
->value
.integer
.value
[0] =
1591 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
1592 if (shift
!= rshift
)
1593 ucontrol
->value
.integer
.value
[1] =
1594 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
1596 ucontrol
->value
.integer
.value
[0] =
1597 max
- ucontrol
->value
.integer
.value
[0];
1598 if (shift
!= rshift
)
1599 ucontrol
->value
.integer
.value
[1] =
1600 max
- ucontrol
->value
.integer
.value
[1];
1605 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
1608 * snd_soc_put_volsw - single mixer put callback
1609 * @kcontrol: mixer control
1610 * @uinfo: control element information
1612 * Callback to set the value of a single mixer control.
1614 * Returns 0 for success.
1616 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
1617 struct snd_ctl_elem_value
*ucontrol
)
1619 struct soc_mixer_control
*mc
=
1620 (struct soc_mixer_control
*)kcontrol
->private_value
;
1621 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1622 unsigned int reg
= mc
->reg
;
1623 unsigned int shift
= mc
->shift
;
1624 unsigned int rshift
= mc
->rshift
;
1626 unsigned int mask
= (1 << fls(max
)) - 1;
1627 unsigned int invert
= mc
->invert
;
1628 unsigned short val
, val2
, val_mask
;
1630 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1633 val_mask
= mask
<< shift
;
1635 if (shift
!= rshift
) {
1636 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1639 val_mask
|= mask
<< rshift
;
1640 val
|= val2
<< rshift
;
1642 return snd_soc_update_bits(codec
, reg
, val_mask
, val
);
1644 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
1647 * snd_soc_info_volsw_2r - double mixer info callback
1648 * @kcontrol: mixer control
1649 * @uinfo: control element information
1651 * Callback to provide information about a double mixer control that
1652 * spans 2 codec registers.
1654 * Returns 0 for success.
1656 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
1657 struct snd_ctl_elem_info
*uinfo
)
1659 struct soc_mixer_control
*mc
=
1660 (struct soc_mixer_control
*)kcontrol
->private_value
;
1664 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1666 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1669 uinfo
->value
.integer
.min
= 0;
1670 uinfo
->value
.integer
.max
= max
;
1673 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
1676 * snd_soc_get_volsw_2r - double mixer get callback
1677 * @kcontrol: mixer control
1678 * @uinfo: control element information
1680 * Callback to get the value of a double mixer control that spans 2 registers.
1682 * Returns 0 for success.
1684 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
1685 struct snd_ctl_elem_value
*ucontrol
)
1687 struct soc_mixer_control
*mc
=
1688 (struct soc_mixer_control
*)kcontrol
->private_value
;
1689 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1690 unsigned int reg
= mc
->reg
;
1691 unsigned int reg2
= mc
->rreg
;
1692 unsigned int shift
= mc
->shift
;
1694 unsigned int mask
= (1<<fls(max
))-1;
1695 unsigned int invert
= mc
->invert
;
1697 ucontrol
->value
.integer
.value
[0] =
1698 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
1699 ucontrol
->value
.integer
.value
[1] =
1700 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
1702 ucontrol
->value
.integer
.value
[0] =
1703 max
- ucontrol
->value
.integer
.value
[0];
1704 ucontrol
->value
.integer
.value
[1] =
1705 max
- ucontrol
->value
.integer
.value
[1];
1710 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
1713 * snd_soc_put_volsw_2r - double mixer set callback
1714 * @kcontrol: mixer control
1715 * @uinfo: control element information
1717 * Callback to set the value of a double mixer control that spans 2 registers.
1719 * Returns 0 for success.
1721 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
1722 struct snd_ctl_elem_value
*ucontrol
)
1724 struct soc_mixer_control
*mc
=
1725 (struct soc_mixer_control
*)kcontrol
->private_value
;
1726 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1727 unsigned int reg
= mc
->reg
;
1728 unsigned int reg2
= mc
->rreg
;
1729 unsigned int shift
= mc
->shift
;
1731 unsigned int mask
= (1 << fls(max
)) - 1;
1732 unsigned int invert
= mc
->invert
;
1734 unsigned short val
, val2
, val_mask
;
1736 val_mask
= mask
<< shift
;
1737 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1738 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1746 val2
= val2
<< shift
;
1748 err
= snd_soc_update_bits(codec
, reg
, val_mask
, val
);
1752 err
= snd_soc_update_bits(codec
, reg2
, val_mask
, val2
);
1755 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
1758 * snd_soc_info_volsw_s8 - signed mixer info callback
1759 * @kcontrol: mixer control
1760 * @uinfo: control element information
1762 * Callback to provide information about a signed mixer control.
1764 * Returns 0 for success.
1766 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
1767 struct snd_ctl_elem_info
*uinfo
)
1769 struct soc_mixer_control
*mc
=
1770 (struct soc_mixer_control
*)kcontrol
->private_value
;
1774 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1776 uinfo
->value
.integer
.min
= 0;
1777 uinfo
->value
.integer
.max
= max
-min
;
1780 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
1783 * snd_soc_get_volsw_s8 - signed mixer get callback
1784 * @kcontrol: mixer control
1785 * @uinfo: control element information
1787 * Callback to get the value of a signed mixer control.
1789 * Returns 0 for success.
1791 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
1792 struct snd_ctl_elem_value
*ucontrol
)
1794 struct soc_mixer_control
*mc
=
1795 (struct soc_mixer_control
*)kcontrol
->private_value
;
1796 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1797 unsigned int reg
= mc
->reg
;
1799 int val
= snd_soc_read(codec
, reg
);
1801 ucontrol
->value
.integer
.value
[0] =
1802 ((signed char)(val
& 0xff))-min
;
1803 ucontrol
->value
.integer
.value
[1] =
1804 ((signed char)((val
>> 8) & 0xff))-min
;
1807 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
1810 * snd_soc_put_volsw_sgn - signed mixer put callback
1811 * @kcontrol: mixer control
1812 * @uinfo: control element information
1814 * Callback to set the value of a signed mixer control.
1816 * Returns 0 for success.
1818 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
1819 struct snd_ctl_elem_value
*ucontrol
)
1821 struct soc_mixer_control
*mc
=
1822 (struct soc_mixer_control
*)kcontrol
->private_value
;
1823 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1824 unsigned int reg
= mc
->reg
;
1828 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
1829 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
1831 return snd_soc_update_bits(codec
, reg
, 0xffff, val
);
1833 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
1836 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1838 * @clk_id: DAI specific clock ID
1839 * @freq: new clock frequency in Hz
1840 * @dir: new clock direction - input/output.
1842 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1844 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
1845 unsigned int freq
, int dir
)
1847 if (dai
->ops
.set_sysclk
)
1848 return dai
->ops
.set_sysclk(dai
, clk_id
, freq
, dir
);
1852 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
1855 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1857 * @clk_id: DAI specific clock divider ID
1858 * @div: new clock divisor.
1860 * Configures the clock dividers. This is used to derive the best DAI bit and
1861 * frame clocks from the system or master clock. It's best to set the DAI bit
1862 * and frame clocks as low as possible to save system power.
1864 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
1865 int div_id
, int div
)
1867 if (dai
->ops
.set_clkdiv
)
1868 return dai
->ops
.set_clkdiv(dai
, div_id
, div
);
1872 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
1875 * snd_soc_dai_set_pll - configure DAI PLL.
1877 * @pll_id: DAI specific PLL ID
1878 * @freq_in: PLL input clock frequency in Hz
1879 * @freq_out: requested PLL output clock frequency in Hz
1881 * Configures and enables PLL to generate output clock based on input clock.
1883 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
,
1884 int pll_id
, unsigned int freq_in
, unsigned int freq_out
)
1886 if (dai
->ops
.set_pll
)
1887 return dai
->ops
.set_pll(dai
, pll_id
, freq_in
, freq_out
);
1891 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
1894 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1896 * @fmt: SND_SOC_DAIFMT_ format value.
1898 * Configures the DAI hardware format and clocking.
1900 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1902 if (dai
->ops
.set_fmt
)
1903 return dai
->ops
.set_fmt(dai
, fmt
);
1907 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
1910 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1912 * @mask: DAI specific mask representing used slots.
1913 * @slots: Number of slots in use.
1915 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1918 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
1919 unsigned int mask
, int slots
)
1921 if (dai
->ops
.set_sysclk
)
1922 return dai
->ops
.set_tdm_slot(dai
, mask
, slots
);
1926 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
1929 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1931 * @tristate: tristate enable
1933 * Tristates the DAI so that others can use it.
1935 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
1937 if (dai
->ops
.set_sysclk
)
1938 return dai
->ops
.set_tristate(dai
, tristate
);
1942 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
1945 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1947 * @mute: mute enable
1949 * Mutes the DAI DAC.
1951 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
1953 if (dai
->ops
.digital_mute
)
1954 return dai
->ops
.digital_mute(dai
, mute
);
1958 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
1960 static int __devinit
snd_soc_init(void)
1962 #ifdef CONFIG_DEBUG_FS
1963 debugfs_root
= debugfs_create_dir("asoc", NULL
);
1964 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
1966 "ASoC: Failed to create debugfs directory\n");
1967 debugfs_root
= NULL
;
1971 return platform_driver_register(&soc_driver
);
1974 static void __exit
snd_soc_exit(void)
1976 #ifdef CONFIG_DEBUG_FS
1977 debugfs_remove_recursive(debugfs_root
);
1979 platform_driver_unregister(&soc_driver
);
1982 module_init(snd_soc_init
);
1983 module_exit(snd_soc_exit
);
1985 /* Module information */
1986 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1987 MODULE_DESCRIPTION("ALSA SoC Core");
1988 MODULE_LICENSE("GPL");
1989 MODULE_ALIAS("platform:soc-audio");