2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Liam Girdwood
8 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
9 * with code, comments and ideas from :-
10 * Richard Purdie <richard@openedhand.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * o Add hw rules to enforce rates, etc.
19 * o More testing with other codecs/machines.
20 * o Add more codecs and platforms to ensure good API coverage.
21 * o Support TDM on PCM and I2S
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
29 #include <linux/bitops.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>
41 #define dbg(format, arg...) printk(format, ## arg)
43 #define dbg(format, arg...)
46 static DEFINE_MUTEX(pcm_mutex
);
47 static DEFINE_MUTEX(io_mutex
);
48 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq
);
51 * This is a timeout to do a DAPM powerdown after a stream is closed().
52 * It can be used to eliminate pops between different playback streams, e.g.
53 * between two audio tracks.
55 static int pmdown_time
= 5000;
56 module_param(pmdown_time
, int, 0);
57 MODULE_PARM_DESC(pmdown_time
, "DAPM stream powerdown time (msecs)");
60 * This function forces any delayed work to be queued and run.
62 static int run_delayed_work(struct delayed_work
*dwork
)
66 /* cancel any work waiting to be queued. */
67 ret
= cancel_delayed_work(dwork
);
69 /* if there was any work waiting then we run it now and
70 * wait for it's completion */
72 schedule_delayed_work(dwork
, 0);
73 flush_scheduled_work();
78 #ifdef CONFIG_SND_SOC_AC97_BUS
79 /* unregister ac97 codec */
80 static int soc_ac97_dev_unregister(struct snd_soc_codec
*codec
)
82 if (codec
->ac97
->dev
.bus
)
83 device_unregister(&codec
->ac97
->dev
);
87 /* stop no dev release warning */
88 static void soc_ac97_device_release(struct device
*dev
){}
90 /* register ac97 codec to bus */
91 static int soc_ac97_dev_register(struct snd_soc_codec
*codec
)
95 codec
->ac97
->dev
.bus
= &ac97_bus_type
;
96 codec
->ac97
->dev
.parent
= NULL
;
97 codec
->ac97
->dev
.release
= soc_ac97_device_release
;
99 snprintf(codec
->ac97
->dev
.bus_id
, BUS_ID_SIZE
, "%d-%d:%s",
100 codec
->card
->number
, 0, codec
->name
);
101 err
= device_register(&codec
->ac97
->dev
);
103 snd_printk(KERN_ERR
"Can't register ac97 bus\n");
104 codec
->ac97
->dev
.bus
= NULL
;
111 static inline const char *get_dai_name(int type
)
114 case SND_SOC_DAI_AC97_BUS
:
115 case SND_SOC_DAI_AC97
:
117 case SND_SOC_DAI_I2S
:
119 case SND_SOC_DAI_PCM
:
126 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
127 * then initialized and any private data can be allocated. This also calls
128 * startup for the cpu DAI, platform, machine and codec DAI.
130 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
132 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
133 struct snd_soc_device
*socdev
= rtd
->socdev
;
134 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
135 struct snd_soc_dai_link
*machine
= rtd
->dai
;
136 struct snd_soc_platform
*platform
= socdev
->platform
;
137 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
138 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
141 mutex_lock(&pcm_mutex
);
143 /* startup the audio subsystem */
144 if (cpu_dai
->ops
.startup
) {
145 ret
= cpu_dai
->ops
.startup(substream
);
147 printk(KERN_ERR
"asoc: can't open interface %s\n",
153 if (platform
->pcm_ops
->open
) {
154 ret
= platform
->pcm_ops
->open(substream
);
156 printk(KERN_ERR
"asoc: can't open platform %s\n", platform
->name
);
161 if (codec_dai
->ops
.startup
) {
162 ret
= codec_dai
->ops
.startup(substream
);
164 printk(KERN_ERR
"asoc: can't open codec %s\n",
170 if (machine
->ops
&& machine
->ops
->startup
) {
171 ret
= machine
->ops
->startup(substream
);
173 printk(KERN_ERR
"asoc: %s startup failed\n", machine
->name
);
178 /* Check that the codec and cpu DAI's are compatible */
179 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
180 runtime
->hw
.rate_min
=
181 max(codec_dai
->playback
.rate_min
,
182 cpu_dai
->playback
.rate_min
);
183 runtime
->hw
.rate_max
=
184 min(codec_dai
->playback
.rate_max
,
185 cpu_dai
->playback
.rate_max
);
186 runtime
->hw
.channels_min
=
187 max(codec_dai
->playback
.channels_min
,
188 cpu_dai
->playback
.channels_min
);
189 runtime
->hw
.channels_max
=
190 min(codec_dai
->playback
.channels_max
,
191 cpu_dai
->playback
.channels_max
);
192 runtime
->hw
.formats
=
193 codec_dai
->playback
.formats
& cpu_dai
->playback
.formats
;
195 codec_dai
->playback
.rates
& cpu_dai
->playback
.rates
;
197 runtime
->hw
.rate_min
=
198 max(codec_dai
->capture
.rate_min
,
199 cpu_dai
->capture
.rate_min
);
200 runtime
->hw
.rate_max
=
201 min(codec_dai
->capture
.rate_max
,
202 cpu_dai
->capture
.rate_max
);
203 runtime
->hw
.channels_min
=
204 max(codec_dai
->capture
.channels_min
,
205 cpu_dai
->capture
.channels_min
);
206 runtime
->hw
.channels_max
=
207 min(codec_dai
->capture
.channels_max
,
208 cpu_dai
->capture
.channels_max
);
209 runtime
->hw
.formats
=
210 codec_dai
->capture
.formats
& cpu_dai
->capture
.formats
;
212 codec_dai
->capture
.rates
& cpu_dai
->capture
.rates
;
215 snd_pcm_limit_hw_rates(runtime
);
216 if (!runtime
->hw
.rates
) {
217 printk(KERN_ERR
"asoc: %s <-> %s No matching rates\n",
218 codec_dai
->name
, cpu_dai
->name
);
221 if (!runtime
->hw
.formats
) {
222 printk(KERN_ERR
"asoc: %s <-> %s No matching formats\n",
223 codec_dai
->name
, cpu_dai
->name
);
226 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
) {
227 printk(KERN_ERR
"asoc: %s <-> %s No matching channels\n",
228 codec_dai
->name
, cpu_dai
->name
);
232 dbg("asoc: %s <-> %s info:\n", codec_dai
->name
, cpu_dai
->name
);
233 dbg("asoc: rate mask 0x%x\n", runtime
->hw
.rates
);
234 dbg("asoc: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
235 runtime
->hw
.channels_max
);
236 dbg("asoc: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
237 runtime
->hw
.rate_max
);
239 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
240 cpu_dai
->playback
.active
= codec_dai
->playback
.active
= 1;
242 cpu_dai
->capture
.active
= codec_dai
->capture
.active
= 1;
243 cpu_dai
->active
= codec_dai
->active
= 1;
244 cpu_dai
->runtime
= runtime
;
245 socdev
->codec
->active
++;
246 mutex_unlock(&pcm_mutex
);
250 if (machine
->ops
&& machine
->ops
->shutdown
)
251 machine
->ops
->shutdown(substream
);
254 if (platform
->pcm_ops
->close
)
255 platform
->pcm_ops
->close(substream
);
258 if (cpu_dai
->ops
.shutdown
)
259 cpu_dai
->ops
.shutdown(substream
);
261 mutex_unlock(&pcm_mutex
);
266 * Power down the audio subsystem pmdown_time msecs after close is called.
267 * This is to ensure there are no pops or clicks in between any music tracks
268 * due to DAPM power cycling.
270 static void close_delayed_work(struct work_struct
*work
)
272 struct snd_soc_device
*socdev
=
273 container_of(work
, struct snd_soc_device
, delayed_work
.work
);
274 struct snd_soc_codec
*codec
= socdev
->codec
;
275 struct snd_soc_dai
*codec_dai
;
278 mutex_lock(&pcm_mutex
);
279 for (i
= 0; i
< codec
->num_dai
; i
++) {
280 codec_dai
= &codec
->dai
[i
];
282 dbg("pop wq checking: %s status: %s waiting: %s\n",
283 codec_dai
->playback
.stream_name
,
284 codec_dai
->playback
.active
? "active" : "inactive",
285 codec_dai
->pop_wait
? "yes" : "no");
287 /* are we waiting on this codec DAI stream */
288 if (codec_dai
->pop_wait
== 1) {
290 /* Reduce power if no longer active */
291 if (codec
->active
== 0) {
292 dbg("pop wq D1 %s %s\n", codec
->name
,
293 codec_dai
->playback
.stream_name
);
294 snd_soc_dapm_set_bias_level(socdev
,
295 SND_SOC_BIAS_PREPARE
);
298 codec_dai
->pop_wait
= 0;
299 snd_soc_dapm_stream_event(codec
,
300 codec_dai
->playback
.stream_name
,
301 SND_SOC_DAPM_STREAM_STOP
);
303 /* Fall into standby if no longer active */
304 if (codec
->active
== 0) {
305 dbg("pop wq D3 %s %s\n", codec
->name
,
306 codec_dai
->playback
.stream_name
);
307 snd_soc_dapm_set_bias_level(socdev
,
308 SND_SOC_BIAS_STANDBY
);
312 mutex_unlock(&pcm_mutex
);
316 * Called by ALSA when a PCM substream is closed. Private data can be
317 * freed here. The cpu DAI, codec DAI, machine and platform are also
320 static int soc_codec_close(struct snd_pcm_substream
*substream
)
322 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
323 struct snd_soc_device
*socdev
= rtd
->socdev
;
324 struct snd_soc_dai_link
*machine
= rtd
->dai
;
325 struct snd_soc_platform
*platform
= socdev
->platform
;
326 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
327 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
328 struct snd_soc_codec
*codec
= socdev
->codec
;
330 mutex_lock(&pcm_mutex
);
332 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
333 cpu_dai
->playback
.active
= codec_dai
->playback
.active
= 0;
335 cpu_dai
->capture
.active
= codec_dai
->capture
.active
= 0;
337 if (codec_dai
->playback
.active
== 0 &&
338 codec_dai
->capture
.active
== 0) {
339 cpu_dai
->active
= codec_dai
->active
= 0;
343 if (cpu_dai
->ops
.shutdown
)
344 cpu_dai
->ops
.shutdown(substream
);
346 if (codec_dai
->ops
.shutdown
)
347 codec_dai
->ops
.shutdown(substream
);
349 if (machine
->ops
&& machine
->ops
->shutdown
)
350 machine
->ops
->shutdown(substream
);
352 if (platform
->pcm_ops
->close
)
353 platform
->pcm_ops
->close(substream
);
354 cpu_dai
->runtime
= NULL
;
356 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
357 /* start delayed pop wq here for playback streams */
358 codec_dai
->pop_wait
= 1;
359 schedule_delayed_work(&socdev
->delayed_work
,
360 msecs_to_jiffies(pmdown_time
));
362 /* capture streams can be powered down now */
363 snd_soc_dapm_stream_event(codec
,
364 codec_dai
->capture
.stream_name
,
365 SND_SOC_DAPM_STREAM_STOP
);
367 if (codec
->active
== 0 && codec_dai
->pop_wait
== 0)
368 snd_soc_dapm_set_bias_level(socdev
,
369 SND_SOC_BIAS_STANDBY
);
372 mutex_unlock(&pcm_mutex
);
377 * Called by ALSA when the PCM substream is prepared, can set format, sample
378 * rate, etc. This function is non atomic and can be called multiple times,
379 * it can refer to the runtime info.
381 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
383 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
384 struct snd_soc_device
*socdev
= rtd
->socdev
;
385 struct snd_soc_dai_link
*machine
= rtd
->dai
;
386 struct snd_soc_platform
*platform
= socdev
->platform
;
387 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
388 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
389 struct snd_soc_codec
*codec
= socdev
->codec
;
392 mutex_lock(&pcm_mutex
);
394 if (machine
->ops
&& machine
->ops
->prepare
) {
395 ret
= machine
->ops
->prepare(substream
);
397 printk(KERN_ERR
"asoc: machine prepare error\n");
402 if (platform
->pcm_ops
->prepare
) {
403 ret
= platform
->pcm_ops
->prepare(substream
);
405 printk(KERN_ERR
"asoc: platform prepare error\n");
410 if (codec_dai
->ops
.prepare
) {
411 ret
= codec_dai
->ops
.prepare(substream
);
413 printk(KERN_ERR
"asoc: codec DAI prepare error\n");
418 if (cpu_dai
->ops
.prepare
) {
419 ret
= cpu_dai
->ops
.prepare(substream
);
421 printk(KERN_ERR
"asoc: cpu DAI prepare error\n");
426 /* we only want to start a DAPM playback stream if we are not waiting
427 * on an existing one stopping */
428 if (codec_dai
->pop_wait
) {
429 /* we are waiting for the delayed work to start */
430 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
431 snd_soc_dapm_stream_event(socdev
->codec
,
432 codec_dai
->capture
.stream_name
,
433 SND_SOC_DAPM_STREAM_START
);
435 codec_dai
->pop_wait
= 0;
436 cancel_delayed_work(&socdev
->delayed_work
);
437 snd_soc_dai_digital_mute(codec_dai
, 0);
440 /* no delayed work - do we need to power up codec */
441 if (codec
->bias_level
!= SND_SOC_BIAS_ON
) {
443 snd_soc_dapm_set_bias_level(socdev
,
444 SND_SOC_BIAS_PREPARE
);
446 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
447 snd_soc_dapm_stream_event(codec
,
448 codec_dai
->playback
.stream_name
,
449 SND_SOC_DAPM_STREAM_START
);
451 snd_soc_dapm_stream_event(codec
,
452 codec_dai
->capture
.stream_name
,
453 SND_SOC_DAPM_STREAM_START
);
455 snd_soc_dapm_set_bias_level(socdev
, SND_SOC_BIAS_ON
);
456 snd_soc_dai_digital_mute(codec_dai
, 0);
459 /* codec already powered - power on widgets */
460 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
461 snd_soc_dapm_stream_event(codec
,
462 codec_dai
->playback
.stream_name
,
463 SND_SOC_DAPM_STREAM_START
);
465 snd_soc_dapm_stream_event(codec
,
466 codec_dai
->capture
.stream_name
,
467 SND_SOC_DAPM_STREAM_START
);
469 snd_soc_dai_digital_mute(codec_dai
, 0);
474 mutex_unlock(&pcm_mutex
);
479 * Called by ALSA when the hardware params are set by application. This
480 * function can also be called multiple times and can allocate buffers
481 * (using snd_pcm_lib_* ). It's non-atomic.
483 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
484 struct snd_pcm_hw_params
*params
)
486 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
487 struct snd_soc_device
*socdev
= rtd
->socdev
;
488 struct snd_soc_dai_link
*machine
= rtd
->dai
;
489 struct snd_soc_platform
*platform
= socdev
->platform
;
490 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
491 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
494 mutex_lock(&pcm_mutex
);
496 if (machine
->ops
&& machine
->ops
->hw_params
) {
497 ret
= machine
->ops
->hw_params(substream
, params
);
499 printk(KERN_ERR
"asoc: machine hw_params failed\n");
504 if (codec_dai
->ops
.hw_params
) {
505 ret
= codec_dai
->ops
.hw_params(substream
, params
);
507 printk(KERN_ERR
"asoc: can't set codec %s hw params\n",
513 if (cpu_dai
->ops
.hw_params
) {
514 ret
= cpu_dai
->ops
.hw_params(substream
, params
);
516 printk(KERN_ERR
"asoc: interface %s hw params failed\n",
522 if (platform
->pcm_ops
->hw_params
) {
523 ret
= platform
->pcm_ops
->hw_params(substream
, params
);
525 printk(KERN_ERR
"asoc: platform %s hw params failed\n",
532 mutex_unlock(&pcm_mutex
);
536 if (cpu_dai
->ops
.hw_free
)
537 cpu_dai
->ops
.hw_free(substream
);
540 if (codec_dai
->ops
.hw_free
)
541 codec_dai
->ops
.hw_free(substream
);
544 if (machine
->ops
&& machine
->ops
->hw_free
)
545 machine
->ops
->hw_free(substream
);
547 mutex_unlock(&pcm_mutex
);
552 * Free's resources allocated by hw_params, can be called multiple times
554 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
556 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
557 struct snd_soc_device
*socdev
= rtd
->socdev
;
558 struct snd_soc_dai_link
*machine
= rtd
->dai
;
559 struct snd_soc_platform
*platform
= socdev
->platform
;
560 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
561 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
562 struct snd_soc_codec
*codec
= socdev
->codec
;
564 mutex_lock(&pcm_mutex
);
566 /* apply codec digital mute */
568 snd_soc_dai_digital_mute(codec_dai
, 1);
570 /* free any machine hw params */
571 if (machine
->ops
&& machine
->ops
->hw_free
)
572 machine
->ops
->hw_free(substream
);
574 /* free any DMA resources */
575 if (platform
->pcm_ops
->hw_free
)
576 platform
->pcm_ops
->hw_free(substream
);
578 /* now free hw params for the DAI's */
579 if (codec_dai
->ops
.hw_free
)
580 codec_dai
->ops
.hw_free(substream
);
582 if (cpu_dai
->ops
.hw_free
)
583 cpu_dai
->ops
.hw_free(substream
);
585 mutex_unlock(&pcm_mutex
);
589 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
591 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
592 struct snd_soc_device
*socdev
= rtd
->socdev
;
593 struct snd_soc_dai_link
*machine
= rtd
->dai
;
594 struct snd_soc_platform
*platform
= socdev
->platform
;
595 struct snd_soc_dai
*cpu_dai
= machine
->cpu_dai
;
596 struct snd_soc_dai
*codec_dai
= machine
->codec_dai
;
599 if (codec_dai
->ops
.trigger
) {
600 ret
= codec_dai
->ops
.trigger(substream
, cmd
);
605 if (platform
->pcm_ops
->trigger
) {
606 ret
= platform
->pcm_ops
->trigger(substream
, cmd
);
611 if (cpu_dai
->ops
.trigger
) {
612 ret
= cpu_dai
->ops
.trigger(substream
, cmd
);
619 /* ASoC PCM operations */
620 static struct snd_pcm_ops soc_pcm_ops
= {
621 .open
= soc_pcm_open
,
622 .close
= soc_codec_close
,
623 .hw_params
= soc_pcm_hw_params
,
624 .hw_free
= soc_pcm_hw_free
,
625 .prepare
= soc_pcm_prepare
,
626 .trigger
= soc_pcm_trigger
,
630 /* powers down audio subsystem for suspend */
631 static int soc_suspend(struct platform_device
*pdev
, pm_message_t state
)
633 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
634 struct snd_soc_machine
*machine
= socdev
->machine
;
635 struct snd_soc_platform
*platform
= socdev
->platform
;
636 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
637 struct snd_soc_codec
*codec
= socdev
->codec
;
640 /* Due to the resume being scheduled into a workqueue we could
641 * suspend before that's finished - wait for it to complete.
643 snd_power_lock(codec
->card
);
644 snd_power_wait(codec
->card
, SNDRV_CTL_POWER_D0
);
645 snd_power_unlock(codec
->card
);
647 /* we're going to block userspace touching us until resume completes */
648 snd_power_change_state(codec
->card
, SNDRV_CTL_POWER_D3hot
);
650 /* mute any active DAC's */
651 for (i
= 0; i
< machine
->num_links
; i
++) {
652 struct snd_soc_dai
*dai
= machine
->dai_link
[i
].codec_dai
;
653 if (dai
->dai_ops
.digital_mute
&& dai
->playback
.active
)
654 dai
->dai_ops
.digital_mute(dai
, 1);
657 /* suspend all pcms */
658 for (i
= 0; i
< machine
->num_links
; i
++)
659 snd_pcm_suspend_all(machine
->dai_link
[i
].pcm
);
661 if (machine
->suspend_pre
)
662 machine
->suspend_pre(pdev
, state
);
664 for (i
= 0; i
< machine
->num_links
; i
++) {
665 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
666 if (cpu_dai
->suspend
&& cpu_dai
->type
!= SND_SOC_DAI_AC97
)
667 cpu_dai
->suspend(pdev
, cpu_dai
);
668 if (platform
->suspend
)
669 platform
->suspend(pdev
, cpu_dai
);
672 /* close any waiting streams and save state */
673 run_delayed_work(&socdev
->delayed_work
);
674 codec
->suspend_bias_level
= codec
->bias_level
;
676 for (i
= 0; i
< codec
->num_dai
; i
++) {
677 char *stream
= codec
->dai
[i
].playback
.stream_name
;
679 snd_soc_dapm_stream_event(codec
, stream
,
680 SND_SOC_DAPM_STREAM_SUSPEND
);
681 stream
= codec
->dai
[i
].capture
.stream_name
;
683 snd_soc_dapm_stream_event(codec
, stream
,
684 SND_SOC_DAPM_STREAM_SUSPEND
);
687 if (codec_dev
->suspend
)
688 codec_dev
->suspend(pdev
, state
);
690 for (i
= 0; i
< machine
->num_links
; i
++) {
691 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
692 if (cpu_dai
->suspend
&& cpu_dai
->type
== SND_SOC_DAI_AC97
)
693 cpu_dai
->suspend(pdev
, cpu_dai
);
696 if (machine
->suspend_post
)
697 machine
->suspend_post(pdev
, state
);
702 /* deferred resume work, so resume can complete before we finished
703 * setting our codec back up, which can be very slow on I2C
705 static void soc_resume_deferred(struct work_struct
*work
)
707 struct snd_soc_device
*socdev
= container_of(work
,
708 struct snd_soc_device
,
709 deferred_resume_work
);
710 struct snd_soc_machine
*machine
= socdev
->machine
;
711 struct snd_soc_platform
*platform
= socdev
->platform
;
712 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
713 struct snd_soc_codec
*codec
= socdev
->codec
;
714 struct platform_device
*pdev
= to_platform_device(socdev
->dev
);
717 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
718 * so userspace apps are blocked from touching us
721 dev_info(socdev
->dev
, "starting resume work\n");
723 if (machine
->resume_pre
)
724 machine
->resume_pre(pdev
);
726 for (i
= 0; i
< machine
->num_links
; i
++) {
727 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
728 if (cpu_dai
->resume
&& cpu_dai
->type
== SND_SOC_DAI_AC97
)
729 cpu_dai
->resume(pdev
, cpu_dai
);
732 if (codec_dev
->resume
)
733 codec_dev
->resume(pdev
);
735 for (i
= 0; i
< codec
->num_dai
; i
++) {
736 char *stream
= codec
->dai
[i
].playback
.stream_name
;
738 snd_soc_dapm_stream_event(codec
, stream
,
739 SND_SOC_DAPM_STREAM_RESUME
);
740 stream
= codec
->dai
[i
].capture
.stream_name
;
742 snd_soc_dapm_stream_event(codec
, stream
,
743 SND_SOC_DAPM_STREAM_RESUME
);
746 /* unmute any active DACs */
747 for (i
= 0; i
< machine
->num_links
; i
++) {
748 struct snd_soc_dai
*dai
= machine
->dai_link
[i
].codec_dai
;
749 if (dai
->dai_ops
.digital_mute
&& dai
->playback
.active
)
750 dai
->dai_ops
.digital_mute(dai
, 0);
753 for (i
= 0; i
< machine
->num_links
; i
++) {
754 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
755 if (cpu_dai
->resume
&& cpu_dai
->type
!= SND_SOC_DAI_AC97
)
756 cpu_dai
->resume(pdev
, cpu_dai
);
757 if (platform
->resume
)
758 platform
->resume(pdev
, cpu_dai
);
761 if (machine
->resume_post
)
762 machine
->resume_post(pdev
);
764 dev_info(socdev
->dev
, "resume work completed\n");
766 /* userspace can access us now we are back as we were before */
767 snd_power_change_state(codec
->card
, SNDRV_CTL_POWER_D0
);
770 /* powers up audio subsystem after a suspend */
771 static int soc_resume(struct platform_device
*pdev
)
773 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
775 dev_info(socdev
->dev
, "scheduling resume work\n");
777 if (!schedule_work(&socdev
->deferred_resume_work
))
778 dev_err(socdev
->dev
, "work item may be lost\n");
784 #define soc_suspend NULL
785 #define soc_resume NULL
788 /* probes a new socdev */
789 static int soc_probe(struct platform_device
*pdev
)
792 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
793 struct snd_soc_machine
*machine
= socdev
->machine
;
794 struct snd_soc_platform
*platform
= socdev
->platform
;
795 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
797 if (machine
->probe
) {
798 ret
= machine
->probe(pdev
);
803 for (i
= 0; i
< machine
->num_links
; i
++) {
804 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
805 if (cpu_dai
->probe
) {
806 ret
= cpu_dai
->probe(pdev
, cpu_dai
);
812 if (codec_dev
->probe
) {
813 ret
= codec_dev
->probe(pdev
);
818 if (platform
->probe
) {
819 ret
= platform
->probe(pdev
);
824 /* DAPM stream work */
825 INIT_DELAYED_WORK(&socdev
->delayed_work
, close_delayed_work
);
827 /* deferred resume work */
828 INIT_WORK(&socdev
->deferred_resume_work
, soc_resume_deferred
);
834 if (codec_dev
->remove
)
835 codec_dev
->remove(pdev
);
838 for (i
--; i
>= 0; i
--) {
839 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
841 cpu_dai
->remove(pdev
, cpu_dai
);
845 machine
->remove(pdev
);
850 /* removes a socdev */
851 static int soc_remove(struct platform_device
*pdev
)
854 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
855 struct snd_soc_machine
*machine
= socdev
->machine
;
856 struct snd_soc_platform
*platform
= socdev
->platform
;
857 struct snd_soc_codec_device
*codec_dev
= socdev
->codec_dev
;
859 run_delayed_work(&socdev
->delayed_work
);
861 if (platform
->remove
)
862 platform
->remove(pdev
);
864 if (codec_dev
->remove
)
865 codec_dev
->remove(pdev
);
867 for (i
= 0; i
< machine
->num_links
; i
++) {
868 struct snd_soc_dai
*cpu_dai
= machine
->dai_link
[i
].cpu_dai
;
870 cpu_dai
->remove(pdev
, cpu_dai
);
874 machine
->remove(pdev
);
879 /* ASoC platform driver */
880 static struct platform_driver soc_driver
= {
883 .owner
= THIS_MODULE
,
886 .remove
= soc_remove
,
887 .suspend
= soc_suspend
,
888 .resume
= soc_resume
,
891 /* create a new pcm */
892 static int soc_new_pcm(struct snd_soc_device
*socdev
,
893 struct snd_soc_dai_link
*dai_link
, int num
)
895 struct snd_soc_codec
*codec
= socdev
->codec
;
896 struct snd_soc_dai
*codec_dai
= dai_link
->codec_dai
;
897 struct snd_soc_dai
*cpu_dai
= dai_link
->cpu_dai
;
898 struct snd_soc_pcm_runtime
*rtd
;
901 int ret
= 0, playback
= 0, capture
= 0;
903 rtd
= kzalloc(sizeof(struct snd_soc_pcm_runtime
), GFP_KERNEL
);
908 rtd
->socdev
= socdev
;
909 codec_dai
->codec
= socdev
->codec
;
911 /* check client and interface hw capabilities */
912 sprintf(new_name
, "%s %s-%s-%d", dai_link
->stream_name
, codec_dai
->name
,
913 get_dai_name(cpu_dai
->type
), num
);
915 if (codec_dai
->playback
.channels_min
)
917 if (codec_dai
->capture
.channels_min
)
920 ret
= snd_pcm_new(codec
->card
, new_name
, codec
->pcm_devs
++, playback
,
923 printk(KERN_ERR
"asoc: can't create pcm for codec %s\n",
930 pcm
->private_data
= rtd
;
931 soc_pcm_ops
.mmap
= socdev
->platform
->pcm_ops
->mmap
;
932 soc_pcm_ops
.pointer
= socdev
->platform
->pcm_ops
->pointer
;
933 soc_pcm_ops
.ioctl
= socdev
->platform
->pcm_ops
->ioctl
;
934 soc_pcm_ops
.copy
= socdev
->platform
->pcm_ops
->copy
;
935 soc_pcm_ops
.silence
= socdev
->platform
->pcm_ops
->silence
;
936 soc_pcm_ops
.ack
= socdev
->platform
->pcm_ops
->ack
;
937 soc_pcm_ops
.page
= socdev
->platform
->pcm_ops
->page
;
940 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &soc_pcm_ops
);
943 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &soc_pcm_ops
);
945 ret
= socdev
->platform
->pcm_new(codec
->card
, codec_dai
, pcm
);
947 printk(KERN_ERR
"asoc: platform pcm constructor failed\n");
952 pcm
->private_free
= socdev
->platform
->pcm_free
;
953 printk(KERN_INFO
"asoc: %s <-> %s mapping ok\n", codec_dai
->name
,
958 /* codec register dump */
959 static ssize_t
codec_reg_show(struct device
*dev
,
960 struct device_attribute
*attr
, char *buf
)
962 struct snd_soc_device
*devdata
= dev_get_drvdata(dev
);
963 struct snd_soc_codec
*codec
= devdata
->codec
;
964 int i
, step
= 1, count
= 0;
966 if (!codec
->reg_cache_size
)
969 if (codec
->reg_cache_step
)
970 step
= codec
->reg_cache_step
;
972 count
+= sprintf(buf
, "%s registers\n", codec
->name
);
973 for (i
= 0; i
< codec
->reg_cache_size
; i
+= step
) {
974 count
+= sprintf(buf
+ count
, "%2x: ", i
);
975 if (count
>= PAGE_SIZE
- 1)
978 if (codec
->display_register
)
979 count
+= codec
->display_register(codec
, buf
+ count
,
980 PAGE_SIZE
- count
, i
);
982 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
,
983 "%4x", codec
->read(codec
, i
));
985 if (count
>= PAGE_SIZE
- 1)
988 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "\n");
989 if (count
>= PAGE_SIZE
- 1)
993 /* Truncate count; min() would cause a warning */
994 if (count
>= PAGE_SIZE
)
995 count
= PAGE_SIZE
- 1;
999 static DEVICE_ATTR(codec_reg
, 0444, codec_reg_show
, NULL
);
1002 * snd_soc_new_ac97_codec - initailise AC97 device
1003 * @codec: audio codec
1004 * @ops: AC97 bus operations
1005 * @num: AC97 codec number
1007 * Initialises AC97 codec resources for use by ad-hoc devices only.
1009 int snd_soc_new_ac97_codec(struct snd_soc_codec
*codec
,
1010 struct snd_ac97_bus_ops
*ops
, int num
)
1012 mutex_lock(&codec
->mutex
);
1014 codec
->ac97
= kzalloc(sizeof(struct snd_ac97
), GFP_KERNEL
);
1015 if (codec
->ac97
== NULL
) {
1016 mutex_unlock(&codec
->mutex
);
1020 codec
->ac97
->bus
= kzalloc(sizeof(struct snd_ac97_bus
), GFP_KERNEL
);
1021 if (codec
->ac97
->bus
== NULL
) {
1024 mutex_unlock(&codec
->mutex
);
1028 codec
->ac97
->bus
->ops
= ops
;
1029 codec
->ac97
->num
= num
;
1030 mutex_unlock(&codec
->mutex
);
1033 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec
);
1036 * snd_soc_free_ac97_codec - free AC97 codec device
1037 * @codec: audio codec
1039 * Frees AC97 codec device resources.
1041 void snd_soc_free_ac97_codec(struct snd_soc_codec
*codec
)
1043 mutex_lock(&codec
->mutex
);
1044 kfree(codec
->ac97
->bus
);
1047 mutex_unlock(&codec
->mutex
);
1049 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec
);
1052 * snd_soc_update_bits - update codec register bits
1053 * @codec: audio codec
1054 * @reg: codec register
1055 * @mask: register mask
1058 * Writes new register value.
1060 * Returns 1 for change else 0.
1062 int snd_soc_update_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1063 unsigned short mask
, unsigned short value
)
1066 unsigned short old
, new;
1068 mutex_lock(&io_mutex
);
1069 old
= snd_soc_read(codec
, reg
);
1070 new = (old
& ~mask
) | value
;
1071 change
= old
!= new;
1073 snd_soc_write(codec
, reg
, new);
1075 mutex_unlock(&io_mutex
);
1078 EXPORT_SYMBOL_GPL(snd_soc_update_bits
);
1081 * snd_soc_test_bits - test register for change
1082 * @codec: audio codec
1083 * @reg: codec register
1084 * @mask: register mask
1087 * Tests a register with a new value and checks if the new value is
1088 * different from the old value.
1090 * Returns 1 for change else 0.
1092 int snd_soc_test_bits(struct snd_soc_codec
*codec
, unsigned short reg
,
1093 unsigned short mask
, unsigned short value
)
1096 unsigned short old
, new;
1098 mutex_lock(&io_mutex
);
1099 old
= snd_soc_read(codec
, reg
);
1100 new = (old
& ~mask
) | value
;
1101 change
= old
!= new;
1102 mutex_unlock(&io_mutex
);
1106 EXPORT_SYMBOL_GPL(snd_soc_test_bits
);
1109 * snd_soc_new_pcms - create new sound card and pcms
1110 * @socdev: the SoC audio device
1112 * Create a new sound card based upon the codec and interface pcms.
1114 * Returns 0 for success, else error.
1116 int snd_soc_new_pcms(struct snd_soc_device
*socdev
, int idx
, const char *xid
)
1118 struct snd_soc_codec
*codec
= socdev
->codec
;
1119 struct snd_soc_machine
*machine
= socdev
->machine
;
1122 mutex_lock(&codec
->mutex
);
1124 /* register a sound card */
1125 codec
->card
= snd_card_new(idx
, xid
, codec
->owner
, 0);
1127 printk(KERN_ERR
"asoc: can't create sound card for codec %s\n",
1129 mutex_unlock(&codec
->mutex
);
1133 codec
->card
->dev
= socdev
->dev
;
1134 codec
->card
->private_data
= codec
;
1135 strncpy(codec
->card
->driver
, codec
->name
, sizeof(codec
->card
->driver
));
1137 /* create the pcms */
1138 for (i
= 0; i
< machine
->num_links
; i
++) {
1139 ret
= soc_new_pcm(socdev
, &machine
->dai_link
[i
], i
);
1141 printk(KERN_ERR
"asoc: can't create pcm %s\n",
1142 machine
->dai_link
[i
].stream_name
);
1143 mutex_unlock(&codec
->mutex
);
1148 mutex_unlock(&codec
->mutex
);
1151 EXPORT_SYMBOL_GPL(snd_soc_new_pcms
);
1154 * snd_soc_register_card - register sound card
1155 * @socdev: the SoC audio device
1157 * Register a SoC sound card. Also registers an AC97 device if the
1158 * codec is AC97 for ad hoc devices.
1160 * Returns 0 for success, else error.
1162 int snd_soc_register_card(struct snd_soc_device
*socdev
)
1164 struct snd_soc_codec
*codec
= socdev
->codec
;
1165 struct snd_soc_machine
*machine
= socdev
->machine
;
1166 int ret
= 0, i
, ac97
= 0, err
= 0;
1168 for (i
= 0; i
< machine
->num_links
; i
++) {
1169 if (socdev
->machine
->dai_link
[i
].init
) {
1170 err
= socdev
->machine
->dai_link
[i
].init(codec
);
1172 printk(KERN_ERR
"asoc: failed to init %s\n",
1173 socdev
->machine
->dai_link
[i
].stream_name
);
1177 if (socdev
->machine
->dai_link
[i
].codec_dai
->type
==
1178 SND_SOC_DAI_AC97_BUS
)
1181 snprintf(codec
->card
->shortname
, sizeof(codec
->card
->shortname
),
1182 "%s", machine
->name
);
1183 snprintf(codec
->card
->longname
, sizeof(codec
->card
->longname
),
1184 "%s (%s)", machine
->name
, codec
->name
);
1186 ret
= snd_card_register(codec
->card
);
1188 printk(KERN_ERR
"asoc: failed to register soundcard for %s\n",
1193 mutex_lock(&codec
->mutex
);
1194 #ifdef CONFIG_SND_SOC_AC97_BUS
1196 ret
= soc_ac97_dev_register(codec
);
1198 printk(KERN_ERR
"asoc: AC97 device register failed\n");
1199 snd_card_free(codec
->card
);
1200 mutex_unlock(&codec
->mutex
);
1206 err
= snd_soc_dapm_sys_add(socdev
->dev
);
1208 printk(KERN_WARNING
"asoc: failed to add dapm sysfs entries\n");
1210 err
= device_create_file(socdev
->dev
, &dev_attr_codec_reg
);
1212 printk(KERN_WARNING
"asoc: failed to add codec sysfs files\n");
1214 mutex_unlock(&codec
->mutex
);
1219 EXPORT_SYMBOL_GPL(snd_soc_register_card
);
1222 * snd_soc_free_pcms - free sound card and pcms
1223 * @socdev: the SoC audio device
1225 * Frees sound card and pcms associated with the socdev.
1226 * Also unregister the codec if it is an AC97 device.
1228 void snd_soc_free_pcms(struct snd_soc_device
*socdev
)
1230 struct snd_soc_codec
*codec
= socdev
->codec
;
1231 #ifdef CONFIG_SND_SOC_AC97_BUS
1232 struct snd_soc_dai
*codec_dai
;
1236 mutex_lock(&codec
->mutex
);
1237 #ifdef CONFIG_SND_SOC_AC97_BUS
1238 for (i
= 0; i
< codec
->num_dai
; i
++) {
1239 codec_dai
= &codec
->dai
[i
];
1240 if (codec_dai
->type
== SND_SOC_DAI_AC97_BUS
&& codec
->ac97
) {
1241 soc_ac97_dev_unregister(codec
);
1249 snd_card_free(codec
->card
);
1250 device_remove_file(socdev
->dev
, &dev_attr_codec_reg
);
1251 mutex_unlock(&codec
->mutex
);
1253 EXPORT_SYMBOL_GPL(snd_soc_free_pcms
);
1256 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1257 * @substream: the pcm substream
1258 * @hw: the hardware parameters
1260 * Sets the substream runtime hardware parameters.
1262 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream
*substream
,
1263 const struct snd_pcm_hardware
*hw
)
1265 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1266 runtime
->hw
.info
= hw
->info
;
1267 runtime
->hw
.formats
= hw
->formats
;
1268 runtime
->hw
.period_bytes_min
= hw
->period_bytes_min
;
1269 runtime
->hw
.period_bytes_max
= hw
->period_bytes_max
;
1270 runtime
->hw
.periods_min
= hw
->periods_min
;
1271 runtime
->hw
.periods_max
= hw
->periods_max
;
1272 runtime
->hw
.buffer_bytes_max
= hw
->buffer_bytes_max
;
1273 runtime
->hw
.fifo_size
= hw
->fifo_size
;
1276 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams
);
1279 * snd_soc_cnew - create new control
1280 * @_template: control template
1281 * @data: control private data
1282 * @lnng_name: control long name
1284 * Create a new mixer control from a template control.
1286 * Returns 0 for success, else error.
1288 struct snd_kcontrol
*snd_soc_cnew(const struct snd_kcontrol_new
*_template
,
1289 void *data
, char *long_name
)
1291 struct snd_kcontrol_new
template;
1293 memcpy(&template, _template
, sizeof(template));
1295 template.name
= long_name
;
1298 return snd_ctl_new1(&template, data
);
1300 EXPORT_SYMBOL_GPL(snd_soc_cnew
);
1303 * snd_soc_info_enum_double - enumerated double mixer info callback
1304 * @kcontrol: mixer control
1305 * @uinfo: control element information
1307 * Callback to provide information about a double enumerated
1310 * Returns 0 for success.
1312 int snd_soc_info_enum_double(struct snd_kcontrol
*kcontrol
,
1313 struct snd_ctl_elem_info
*uinfo
)
1315 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1317 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1318 uinfo
->count
= e
->shift_l
== e
->shift_r
? 1 : 2;
1319 uinfo
->value
.enumerated
.items
= e
->mask
;
1321 if (uinfo
->value
.enumerated
.item
> e
->mask
- 1)
1322 uinfo
->value
.enumerated
.item
= e
->mask
- 1;
1323 strcpy(uinfo
->value
.enumerated
.name
,
1324 e
->texts
[uinfo
->value
.enumerated
.item
]);
1327 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double
);
1330 * snd_soc_get_enum_double - enumerated double mixer get callback
1331 * @kcontrol: mixer control
1332 * @uinfo: control element information
1334 * Callback to get the value of a double enumerated mixer.
1336 * Returns 0 for success.
1338 int snd_soc_get_enum_double(struct snd_kcontrol
*kcontrol
,
1339 struct snd_ctl_elem_value
*ucontrol
)
1341 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1342 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1343 unsigned short val
, bitmask
;
1345 for (bitmask
= 1; bitmask
< e
->mask
; bitmask
<<= 1)
1347 val
= snd_soc_read(codec
, e
->reg
);
1348 ucontrol
->value
.enumerated
.item
[0]
1349 = (val
>> e
->shift_l
) & (bitmask
- 1);
1350 if (e
->shift_l
!= e
->shift_r
)
1351 ucontrol
->value
.enumerated
.item
[1] =
1352 (val
>> e
->shift_r
) & (bitmask
- 1);
1356 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double
);
1359 * snd_soc_put_enum_double - enumerated double mixer put callback
1360 * @kcontrol: mixer control
1361 * @uinfo: control element information
1363 * Callback to set the value of a double enumerated mixer.
1365 * Returns 0 for success.
1367 int snd_soc_put_enum_double(struct snd_kcontrol
*kcontrol
,
1368 struct snd_ctl_elem_value
*ucontrol
)
1370 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1371 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1373 unsigned short mask
, bitmask
;
1375 for (bitmask
= 1; bitmask
< e
->mask
; bitmask
<<= 1)
1377 if (ucontrol
->value
.enumerated
.item
[0] > e
->mask
- 1)
1379 val
= ucontrol
->value
.enumerated
.item
[0] << e
->shift_l
;
1380 mask
= (bitmask
- 1) << e
->shift_l
;
1381 if (e
->shift_l
!= e
->shift_r
) {
1382 if (ucontrol
->value
.enumerated
.item
[1] > e
->mask
- 1)
1384 val
|= ucontrol
->value
.enumerated
.item
[1] << e
->shift_r
;
1385 mask
|= (bitmask
- 1) << e
->shift_r
;
1388 return snd_soc_update_bits(codec
, e
->reg
, mask
, val
);
1390 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double
);
1393 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1394 * @kcontrol: mixer control
1395 * @uinfo: control element information
1397 * Callback to provide information about an external enumerated
1400 * Returns 0 for success.
1402 int snd_soc_info_enum_ext(struct snd_kcontrol
*kcontrol
,
1403 struct snd_ctl_elem_info
*uinfo
)
1405 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
1407 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1409 uinfo
->value
.enumerated
.items
= e
->mask
;
1411 if (uinfo
->value
.enumerated
.item
> e
->mask
- 1)
1412 uinfo
->value
.enumerated
.item
= e
->mask
- 1;
1413 strcpy(uinfo
->value
.enumerated
.name
,
1414 e
->texts
[uinfo
->value
.enumerated
.item
]);
1417 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext
);
1420 * snd_soc_info_volsw_ext - external single mixer info callback
1421 * @kcontrol: mixer control
1422 * @uinfo: control element information
1424 * Callback to provide information about a single external mixer control.
1426 * Returns 0 for success.
1428 int snd_soc_info_volsw_ext(struct snd_kcontrol
*kcontrol
,
1429 struct snd_ctl_elem_info
*uinfo
)
1431 int max
= kcontrol
->private_value
;
1434 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1436 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1439 uinfo
->value
.integer
.min
= 0;
1440 uinfo
->value
.integer
.max
= max
;
1443 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext
);
1446 * snd_soc_info_volsw - single mixer info callback
1447 * @kcontrol: mixer control
1448 * @uinfo: control element information
1450 * Callback to provide information about a single mixer control.
1452 * Returns 0 for success.
1454 int snd_soc_info_volsw(struct snd_kcontrol
*kcontrol
,
1455 struct snd_ctl_elem_info
*uinfo
)
1457 int max
= (kcontrol
->private_value
>> 16) & 0xff;
1458 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1459 int rshift
= (kcontrol
->private_value
>> 12) & 0x0f;
1462 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1464 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1466 uinfo
->count
= shift
== rshift
? 1 : 2;
1467 uinfo
->value
.integer
.min
= 0;
1468 uinfo
->value
.integer
.max
= max
;
1471 EXPORT_SYMBOL_GPL(snd_soc_info_volsw
);
1474 * snd_soc_get_volsw - single mixer get callback
1475 * @kcontrol: mixer control
1476 * @uinfo: control element information
1478 * Callback to get the value of a single mixer control.
1480 * Returns 0 for success.
1482 int snd_soc_get_volsw(struct snd_kcontrol
*kcontrol
,
1483 struct snd_ctl_elem_value
*ucontrol
)
1485 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1486 int reg
= kcontrol
->private_value
& 0xff;
1487 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1488 int rshift
= (kcontrol
->private_value
>> 12) & 0x0f;
1489 int max
= (kcontrol
->private_value
>> 16) & 0xff;
1490 int mask
= (1 << fls(max
)) - 1;
1491 int invert
= (kcontrol
->private_value
>> 24) & 0x01;
1493 ucontrol
->value
.integer
.value
[0] =
1494 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
1495 if (shift
!= rshift
)
1496 ucontrol
->value
.integer
.value
[1] =
1497 (snd_soc_read(codec
, reg
) >> rshift
) & mask
;
1499 ucontrol
->value
.integer
.value
[0] =
1500 max
- ucontrol
->value
.integer
.value
[0];
1501 if (shift
!= rshift
)
1502 ucontrol
->value
.integer
.value
[1] =
1503 max
- ucontrol
->value
.integer
.value
[1];
1508 EXPORT_SYMBOL_GPL(snd_soc_get_volsw
);
1511 * snd_soc_put_volsw - single mixer put callback
1512 * @kcontrol: mixer control
1513 * @uinfo: control element information
1515 * Callback to set the value of a single mixer control.
1517 * Returns 0 for success.
1519 int snd_soc_put_volsw(struct snd_kcontrol
*kcontrol
,
1520 struct snd_ctl_elem_value
*ucontrol
)
1522 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1523 int reg
= kcontrol
->private_value
& 0xff;
1524 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1525 int rshift
= (kcontrol
->private_value
>> 12) & 0x0f;
1526 int max
= (kcontrol
->private_value
>> 16) & 0xff;
1527 int mask
= (1 << fls(max
)) - 1;
1528 int invert
= (kcontrol
->private_value
>> 24) & 0x01;
1529 unsigned short val
, val2
, val_mask
;
1531 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1534 val_mask
= mask
<< shift
;
1536 if (shift
!= rshift
) {
1537 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1540 val_mask
|= mask
<< rshift
;
1541 val
|= val2
<< rshift
;
1543 return snd_soc_update_bits(codec
, reg
, val_mask
, val
);
1545 EXPORT_SYMBOL_GPL(snd_soc_put_volsw
);
1548 * snd_soc_info_volsw_2r - double mixer info callback
1549 * @kcontrol: mixer control
1550 * @uinfo: control element information
1552 * Callback to provide information about a double mixer control that
1553 * spans 2 codec registers.
1555 * Returns 0 for success.
1557 int snd_soc_info_volsw_2r(struct snd_kcontrol
*kcontrol
,
1558 struct snd_ctl_elem_info
*uinfo
)
1560 int max
= (kcontrol
->private_value
>> 12) & 0xff;
1563 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
1565 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1568 uinfo
->value
.integer
.min
= 0;
1569 uinfo
->value
.integer
.max
= max
;
1572 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r
);
1575 * snd_soc_get_volsw_2r - double mixer get callback
1576 * @kcontrol: mixer control
1577 * @uinfo: control element information
1579 * Callback to get the value of a double mixer control that spans 2 registers.
1581 * Returns 0 for success.
1583 int snd_soc_get_volsw_2r(struct snd_kcontrol
*kcontrol
,
1584 struct snd_ctl_elem_value
*ucontrol
)
1586 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1587 int reg
= kcontrol
->private_value
& 0xff;
1588 int reg2
= (kcontrol
->private_value
>> 24) & 0xff;
1589 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1590 int max
= (kcontrol
->private_value
>> 12) & 0xff;
1591 int mask
= (1<<fls(max
))-1;
1592 int invert
= (kcontrol
->private_value
>> 20) & 0x01;
1594 ucontrol
->value
.integer
.value
[0] =
1595 (snd_soc_read(codec
, reg
) >> shift
) & mask
;
1596 ucontrol
->value
.integer
.value
[1] =
1597 (snd_soc_read(codec
, reg2
) >> shift
) & mask
;
1599 ucontrol
->value
.integer
.value
[0] =
1600 max
- ucontrol
->value
.integer
.value
[0];
1601 ucontrol
->value
.integer
.value
[1] =
1602 max
- ucontrol
->value
.integer
.value
[1];
1607 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r
);
1610 * snd_soc_put_volsw_2r - double mixer set callback
1611 * @kcontrol: mixer control
1612 * @uinfo: control element information
1614 * Callback to set the value of a double mixer control that spans 2 registers.
1616 * Returns 0 for success.
1618 int snd_soc_put_volsw_2r(struct snd_kcontrol
*kcontrol
,
1619 struct snd_ctl_elem_value
*ucontrol
)
1621 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1622 int reg
= kcontrol
->private_value
& 0xff;
1623 int reg2
= (kcontrol
->private_value
>> 24) & 0xff;
1624 int shift
= (kcontrol
->private_value
>> 8) & 0x0f;
1625 int max
= (kcontrol
->private_value
>> 12) & 0xff;
1626 int mask
= (1 << fls(max
)) - 1;
1627 int invert
= (kcontrol
->private_value
>> 20) & 0x01;
1629 unsigned short val
, val2
, val_mask
;
1631 val_mask
= mask
<< shift
;
1632 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
1633 val2
= (ucontrol
->value
.integer
.value
[1] & mask
);
1641 val2
= val2
<< shift
;
1643 err
= snd_soc_update_bits(codec
, reg
, val_mask
, val
);
1647 err
= snd_soc_update_bits(codec
, reg2
, val_mask
, val2
);
1650 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r
);
1653 * snd_soc_info_volsw_s8 - signed mixer info callback
1654 * @kcontrol: mixer control
1655 * @uinfo: control element information
1657 * Callback to provide information about a signed mixer control.
1659 * Returns 0 for success.
1661 int snd_soc_info_volsw_s8(struct snd_kcontrol
*kcontrol
,
1662 struct snd_ctl_elem_info
*uinfo
)
1664 int max
= (signed char)((kcontrol
->private_value
>> 16) & 0xff);
1665 int min
= (signed char)((kcontrol
->private_value
>> 24) & 0xff);
1667 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
1669 uinfo
->value
.integer
.min
= 0;
1670 uinfo
->value
.integer
.max
= max
-min
;
1673 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8
);
1676 * snd_soc_get_volsw_s8 - signed mixer get callback
1677 * @kcontrol: mixer control
1678 * @uinfo: control element information
1680 * Callback to get the value of a signed mixer control.
1682 * Returns 0 for success.
1684 int snd_soc_get_volsw_s8(struct snd_kcontrol
*kcontrol
,
1685 struct snd_ctl_elem_value
*ucontrol
)
1687 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1688 int reg
= kcontrol
->private_value
& 0xff;
1689 int min
= (signed char)((kcontrol
->private_value
>> 24) & 0xff);
1690 int val
= snd_soc_read(codec
, reg
);
1692 ucontrol
->value
.integer
.value
[0] =
1693 ((signed char)(val
& 0xff))-min
;
1694 ucontrol
->value
.integer
.value
[1] =
1695 ((signed char)((val
>> 8) & 0xff))-min
;
1698 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8
);
1701 * snd_soc_put_volsw_sgn - signed mixer put callback
1702 * @kcontrol: mixer control
1703 * @uinfo: control element information
1705 * Callback to set the value of a signed mixer control.
1707 * Returns 0 for success.
1709 int snd_soc_put_volsw_s8(struct snd_kcontrol
*kcontrol
,
1710 struct snd_ctl_elem_value
*ucontrol
)
1712 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1713 int reg
= kcontrol
->private_value
& 0xff;
1714 int min
= (signed char)((kcontrol
->private_value
>> 24) & 0xff);
1717 val
= (ucontrol
->value
.integer
.value
[0]+min
) & 0xff;
1718 val
|= ((ucontrol
->value
.integer
.value
[1]+min
) & 0xff) << 8;
1720 return snd_soc_update_bits(codec
, reg
, 0xffff, val
);
1722 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8
);
1725 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1727 * @clk_id: DAI specific clock ID
1728 * @freq: new clock frequency in Hz
1729 * @dir: new clock direction - input/output.
1731 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1733 int snd_soc_dai_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
1734 unsigned int freq
, int dir
)
1736 if (dai
->dai_ops
.set_sysclk
)
1737 return dai
->dai_ops
.set_sysclk(dai
, clk_id
, freq
, dir
);
1741 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk
);
1744 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1746 * @clk_id: DAI specific clock divider ID
1747 * @div: new clock divisor.
1749 * Configures the clock dividers. This is used to derive the best DAI bit and
1750 * frame clocks from the system or master clock. It's best to set the DAI bit
1751 * and frame clocks as low as possible to save system power.
1753 int snd_soc_dai_set_clkdiv(struct snd_soc_dai
*dai
,
1754 int div_id
, int div
)
1756 if (dai
->dai_ops
.set_clkdiv
)
1757 return dai
->dai_ops
.set_clkdiv(dai
, div_id
, div
);
1761 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv
);
1764 * snd_soc_dai_set_pll - configure DAI PLL.
1766 * @pll_id: DAI specific PLL ID
1767 * @freq_in: PLL input clock frequency in Hz
1768 * @freq_out: requested PLL output clock frequency in Hz
1770 * Configures and enables PLL to generate output clock based on input clock.
1772 int snd_soc_dai_set_pll(struct snd_soc_dai
*dai
,
1773 int pll_id
, unsigned int freq_in
, unsigned int freq_out
)
1775 if (dai
->dai_ops
.set_pll
)
1776 return dai
->dai_ops
.set_pll(dai
, pll_id
, freq_in
, freq_out
);
1780 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll
);
1783 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1785 * @clk_id: DAI specific clock ID
1786 * @fmt: SND_SOC_DAIFMT_ format value.
1788 * Configures the DAI hardware format and clocking.
1790 int snd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1792 if (dai
->dai_ops
.set_fmt
)
1793 return dai
->dai_ops
.set_fmt(dai
, fmt
);
1797 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt
);
1800 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1802 * @mask: DAI specific mask representing used slots.
1803 * @slots: Number of slots in use.
1805 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1808 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai
*dai
,
1809 unsigned int mask
, int slots
)
1811 if (dai
->dai_ops
.set_sysclk
)
1812 return dai
->dai_ops
.set_tdm_slot(dai
, mask
, slots
);
1816 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot
);
1819 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1821 * @tristate: tristate enable
1823 * Tristates the DAI so that others can use it.
1825 int snd_soc_dai_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
1827 if (dai
->dai_ops
.set_sysclk
)
1828 return dai
->dai_ops
.set_tristate(dai
, tristate
);
1832 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate
);
1835 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1837 * @mute: mute enable
1839 * Mutes the DAI DAC.
1841 int snd_soc_dai_digital_mute(struct snd_soc_dai
*dai
, int mute
)
1843 if (dai
->dai_ops
.digital_mute
)
1844 return dai
->dai_ops
.digital_mute(dai
, mute
);
1848 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute
);
1850 static int __devinit
snd_soc_init(void)
1852 printk(KERN_INFO
"ASoC version %s\n", SND_SOC_VERSION
);
1853 return platform_driver_register(&soc_driver
);
1856 static void snd_soc_exit(void)
1858 platform_driver_unregister(&soc_driver
);
1861 module_init(snd_soc_init
);
1862 module_exit(snd_soc_exit
);
1864 /* Module information */
1865 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
1866 MODULE_DESCRIPTION("ALSA SoC Core");
1867 MODULE_LICENSE("GPL");
1868 MODULE_ALIAS("platform:soc-audio");