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);
43 * This is a timeout to do a DAPM powerdown after a stream is closed().
44 * It can be used to eliminate pops between different playback streams, e.g.
45 * between two audio tracks.
47 static int pmdown_time = 5000;
48 module_param(pmdown_time, int, 0);
49 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
52 * This function forces any delayed work to be queued and run.
54 static int run_delayed_work(struct delayed_work *dwork)
58 /* cancel any work waiting to be queued. */
59 ret = cancel_delayed_work(dwork);
61 /* if there was any work waiting then we run it now and
62 * wait for it's completion */
64 schedule_delayed_work(dwork, 0);
65 flush_scheduled_work();
70 #ifdef CONFIG_SND_SOC_AC97_BUS
71 /* unregister ac97 codec */
72 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
74 if (codec->ac97->dev.bus)
75 device_unregister(&codec->ac97->dev);
79 /* stop no dev release warning */
80 static void soc_ac97_device_release(struct device *dev){}
82 /* register ac97 codec to bus */
83 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
87 codec->ac97->dev.bus = &ac97_bus_type;
88 codec->ac97->dev.parent = NULL;
89 codec->ac97->dev.release = soc_ac97_device_release;
91 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
92 codec->card->number, 0, codec->name);
93 err = device_register(&codec->ac97->dev);
95 snd_printk(KERN_ERR "Can't register ac97 bus\n");
96 codec->ac97->dev.bus = NULL;
104 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
105 * then initialized and any private data can be allocated. This also calls
106 * startup for the cpu DAI, platform, machine and codec DAI.
108 static int soc_pcm_open(struct snd_pcm_substream *substream)
110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
111 struct snd_soc_device *socdev = rtd->socdev;
112 struct snd_pcm_runtime *runtime = substream->runtime;
113 struct snd_soc_dai_link *machine = rtd->dai;
114 struct snd_soc_platform *platform = socdev->platform;
115 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
116 struct snd_soc_dai *codec_dai = machine->codec_dai;
119 mutex_lock(&pcm_mutex);
121 /* startup the audio subsystem */
122 if (cpu_dai->ops.startup) {
123 ret = cpu_dai->ops.startup(substream, cpu_dai);
125 printk(KERN_ERR "asoc: can't open interface %s\n",
131 if (platform->pcm_ops->open) {
132 ret = platform->pcm_ops->open(substream);
134 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
139 if (codec_dai->ops.startup) {
140 ret = codec_dai->ops.startup(substream, codec_dai);
142 printk(KERN_ERR "asoc: can't open codec %s\n",
148 if (machine->ops && machine->ops->startup) {
149 ret = machine->ops->startup(substream);
151 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
156 /* Check that the codec and cpu DAI's are compatible */
157 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
158 runtime->hw.rate_min =
159 max(codec_dai->playback.rate_min,
160 cpu_dai->playback.rate_min);
161 runtime->hw.rate_max =
162 min(codec_dai->playback.rate_max,
163 cpu_dai->playback.rate_max);
164 runtime->hw.channels_min =
165 max(codec_dai->playback.channels_min,
166 cpu_dai->playback.channels_min);
167 runtime->hw.channels_max =
168 min(codec_dai->playback.channels_max,
169 cpu_dai->playback.channels_max);
170 runtime->hw.formats =
171 codec_dai->playback.formats & cpu_dai->playback.formats;
173 codec_dai->playback.rates & cpu_dai->playback.rates;
175 runtime->hw.rate_min =
176 max(codec_dai->capture.rate_min,
177 cpu_dai->capture.rate_min);
178 runtime->hw.rate_max =
179 min(codec_dai->capture.rate_max,
180 cpu_dai->capture.rate_max);
181 runtime->hw.channels_min =
182 max(codec_dai->capture.channels_min,
183 cpu_dai->capture.channels_min);
184 runtime->hw.channels_max =
185 min(codec_dai->capture.channels_max,
186 cpu_dai->capture.channels_max);
187 runtime->hw.formats =
188 codec_dai->capture.formats & cpu_dai->capture.formats;
190 codec_dai->capture.rates & cpu_dai->capture.rates;
193 snd_pcm_limit_hw_rates(runtime);
194 if (!runtime->hw.rates) {
195 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
196 codec_dai->name, cpu_dai->name);
199 if (!runtime->hw.formats) {
200 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
201 codec_dai->name, cpu_dai->name);
204 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
205 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
206 codec_dai->name, cpu_dai->name);
210 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
211 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
212 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
213 runtime->hw.channels_max);
214 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
215 runtime->hw.rate_max);
217 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
218 cpu_dai->playback.active = codec_dai->playback.active = 1;
220 cpu_dai->capture.active = codec_dai->capture.active = 1;
221 cpu_dai->active = codec_dai->active = 1;
222 cpu_dai->runtime = runtime;
223 socdev->codec->active++;
224 mutex_unlock(&pcm_mutex);
228 if (machine->ops && machine->ops->shutdown)
229 machine->ops->shutdown(substream);
232 if (platform->pcm_ops->close)
233 platform->pcm_ops->close(substream);
236 if (cpu_dai->ops.shutdown)
237 cpu_dai->ops.shutdown(substream, cpu_dai);
239 mutex_unlock(&pcm_mutex);
244 * Power down the audio subsystem pmdown_time msecs after close is called.
245 * This is to ensure there are no pops or clicks in between any music tracks
246 * due to DAPM power cycling.
248 static void close_delayed_work(struct work_struct *work)
250 struct snd_soc_device *socdev =
251 container_of(work, struct snd_soc_device, delayed_work.work);
252 struct snd_soc_codec *codec = socdev->codec;
253 struct snd_soc_dai *codec_dai;
256 mutex_lock(&pcm_mutex);
257 for (i = 0; i < codec->num_dai; i++) {
258 codec_dai = &codec->dai[i];
260 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
261 codec_dai->playback.stream_name,
262 codec_dai->playback.active ? "active" : "inactive",
263 codec_dai->pop_wait ? "yes" : "no");
265 /* are we waiting on this codec DAI stream */
266 if (codec_dai->pop_wait == 1) {
268 /* Reduce power if no longer active */
269 if (codec->active == 0) {
270 pr_debug("pop wq D1 %s %s\n", codec->name,
271 codec_dai->playback.stream_name);
272 snd_soc_dapm_set_bias_level(socdev,
273 SND_SOC_BIAS_PREPARE);
276 codec_dai->pop_wait = 0;
277 snd_soc_dapm_stream_event(codec,
278 codec_dai->playback.stream_name,
279 SND_SOC_DAPM_STREAM_STOP);
281 /* Fall into standby if no longer active */
282 if (codec->active == 0) {
283 pr_debug("pop wq D3 %s %s\n", codec->name,
284 codec_dai->playback.stream_name);
285 snd_soc_dapm_set_bias_level(socdev,
286 SND_SOC_BIAS_STANDBY);
290 mutex_unlock(&pcm_mutex);
294 * Called by ALSA when a PCM substream is closed. Private data can be
295 * freed here. The cpu DAI, codec DAI, machine and platform are also
298 static int soc_codec_close(struct snd_pcm_substream *substream)
300 struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 struct snd_soc_device *socdev = rtd->socdev;
302 struct snd_soc_dai_link *machine = rtd->dai;
303 struct snd_soc_platform *platform = socdev->platform;
304 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
305 struct snd_soc_dai *codec_dai = machine->codec_dai;
306 struct snd_soc_codec *codec = socdev->codec;
308 mutex_lock(&pcm_mutex);
310 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
311 cpu_dai->playback.active = codec_dai->playback.active = 0;
313 cpu_dai->capture.active = codec_dai->capture.active = 0;
315 if (codec_dai->playback.active == 0 &&
316 codec_dai->capture.active == 0) {
317 cpu_dai->active = codec_dai->active = 0;
321 /* Muting the DAC suppresses artifacts caused during digital
322 * shutdown, for example from stopping clocks.
324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
325 snd_soc_dai_digital_mute(codec_dai, 1);
327 if (cpu_dai->ops.shutdown)
328 cpu_dai->ops.shutdown(substream, cpu_dai);
330 if (codec_dai->ops.shutdown)
331 codec_dai->ops.shutdown(substream, codec_dai);
333 if (machine->ops && machine->ops->shutdown)
334 machine->ops->shutdown(substream);
336 if (platform->pcm_ops->close)
337 platform->pcm_ops->close(substream);
338 cpu_dai->runtime = NULL;
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
341 /* start delayed pop wq here for playback streams */
342 codec_dai->pop_wait = 1;
343 schedule_delayed_work(&socdev->delayed_work,
344 msecs_to_jiffies(pmdown_time));
346 /* capture streams can be powered down now */
347 snd_soc_dapm_stream_event(codec,
348 codec_dai->capture.stream_name,
349 SND_SOC_DAPM_STREAM_STOP);
351 if (codec->active == 0 && codec_dai->pop_wait == 0)
352 snd_soc_dapm_set_bias_level(socdev,
353 SND_SOC_BIAS_STANDBY);
356 mutex_unlock(&pcm_mutex);
361 * Called by ALSA when the PCM substream is prepared, can set format, sample
362 * rate, etc. This function is non atomic and can be called multiple times,
363 * it can refer to the runtime info.
365 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
367 struct snd_soc_pcm_runtime *rtd = substream->private_data;
368 struct snd_soc_device *socdev = rtd->socdev;
369 struct snd_soc_dai_link *machine = rtd->dai;
370 struct snd_soc_platform *platform = socdev->platform;
371 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
372 struct snd_soc_dai *codec_dai = machine->codec_dai;
373 struct snd_soc_codec *codec = socdev->codec;
376 mutex_lock(&pcm_mutex);
378 if (machine->ops && machine->ops->prepare) {
379 ret = machine->ops->prepare(substream);
381 printk(KERN_ERR "asoc: machine prepare error\n");
386 if (platform->pcm_ops->prepare) {
387 ret = platform->pcm_ops->prepare(substream);
389 printk(KERN_ERR "asoc: platform prepare error\n");
394 if (codec_dai->ops.prepare) {
395 ret = codec_dai->ops.prepare(substream, codec_dai);
397 printk(KERN_ERR "asoc: codec DAI prepare error\n");
402 if (cpu_dai->ops.prepare) {
403 ret = cpu_dai->ops.prepare(substream, cpu_dai);
405 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
410 /* cancel any delayed stream shutdown that is pending */
411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
412 codec_dai->pop_wait) {
413 codec_dai->pop_wait = 0;
414 cancel_delayed_work(&socdev->delayed_work);
417 /* do we need to power up codec */
418 if (codec->bias_level != SND_SOC_BIAS_ON) {
419 snd_soc_dapm_set_bias_level(socdev,
420 SND_SOC_BIAS_PREPARE);
422 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
423 snd_soc_dapm_stream_event(codec,
424 codec_dai->playback.stream_name,
425 SND_SOC_DAPM_STREAM_START);
427 snd_soc_dapm_stream_event(codec,
428 codec_dai->capture.stream_name,
429 SND_SOC_DAPM_STREAM_START);
431 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
432 snd_soc_dai_digital_mute(codec_dai, 0);
435 /* codec already powered - power on widgets */
436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437 snd_soc_dapm_stream_event(codec,
438 codec_dai->playback.stream_name,
439 SND_SOC_DAPM_STREAM_START);
441 snd_soc_dapm_stream_event(codec,
442 codec_dai->capture.stream_name,
443 SND_SOC_DAPM_STREAM_START);
445 snd_soc_dai_digital_mute(codec_dai, 0);
449 mutex_unlock(&pcm_mutex);
454 * Called by ALSA when the hardware params are set by application. This
455 * function can also be called multiple times and can allocate buffers
456 * (using snd_pcm_lib_* ). It's non-atomic.
458 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
459 struct snd_pcm_hw_params *params)
461 struct snd_soc_pcm_runtime *rtd = substream->private_data;
462 struct snd_soc_device *socdev = rtd->socdev;
463 struct snd_soc_dai_link *machine = rtd->dai;
464 struct snd_soc_platform *platform = socdev->platform;
465 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
466 struct snd_soc_dai *codec_dai = machine->codec_dai;
469 mutex_lock(&pcm_mutex);
471 if (machine->ops && machine->ops->hw_params) {
472 ret = machine->ops->hw_params(substream, params);
474 printk(KERN_ERR "asoc: machine hw_params failed\n");
479 if (codec_dai->ops.hw_params) {
480 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
482 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
488 if (cpu_dai->ops.hw_params) {
489 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
491 printk(KERN_ERR "asoc: interface %s hw params failed\n",
497 if (platform->pcm_ops->hw_params) {
498 ret = platform->pcm_ops->hw_params(substream, params);
500 printk(KERN_ERR "asoc: platform %s hw params failed\n",
507 mutex_unlock(&pcm_mutex);
511 if (cpu_dai->ops.hw_free)
512 cpu_dai->ops.hw_free(substream, cpu_dai);
515 if (codec_dai->ops.hw_free)
516 codec_dai->ops.hw_free(substream, codec_dai);
519 if (machine->ops && machine->ops->hw_free)
520 machine->ops->hw_free(substream);
522 mutex_unlock(&pcm_mutex);
527 * Free's resources allocated by hw_params, can be called multiple times
529 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
531 struct snd_soc_pcm_runtime *rtd = substream->private_data;
532 struct snd_soc_device *socdev = rtd->socdev;
533 struct snd_soc_dai_link *machine = rtd->dai;
534 struct snd_soc_platform *platform = socdev->platform;
535 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
536 struct snd_soc_dai *codec_dai = machine->codec_dai;
537 struct snd_soc_codec *codec = socdev->codec;
539 mutex_lock(&pcm_mutex);
541 /* apply codec digital mute */
543 snd_soc_dai_digital_mute(codec_dai, 1);
545 /* free any machine hw params */
546 if (machine->ops && machine->ops->hw_free)
547 machine->ops->hw_free(substream);
549 /* free any DMA resources */
550 if (platform->pcm_ops->hw_free)
551 platform->pcm_ops->hw_free(substream);
553 /* now free hw params for the DAI's */
554 if (codec_dai->ops.hw_free)
555 codec_dai->ops.hw_free(substream, codec_dai);
557 if (cpu_dai->ops.hw_free)
558 cpu_dai->ops.hw_free(substream, cpu_dai);
560 mutex_unlock(&pcm_mutex);
564 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
566 struct snd_soc_pcm_runtime *rtd = substream->private_data;
567 struct snd_soc_device *socdev = rtd->socdev;
568 struct snd_soc_dai_link *machine = rtd->dai;
569 struct snd_soc_platform *platform = socdev->platform;
570 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
571 struct snd_soc_dai *codec_dai = machine->codec_dai;
574 if (codec_dai->ops.trigger) {
575 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
580 if (platform->pcm_ops->trigger) {
581 ret = platform->pcm_ops->trigger(substream, cmd);
586 if (cpu_dai->ops.trigger) {
587 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
594 /* ASoC PCM operations */
595 static struct snd_pcm_ops soc_pcm_ops = {
596 .open = soc_pcm_open,
597 .close = soc_codec_close,
598 .hw_params = soc_pcm_hw_params,
599 .hw_free = soc_pcm_hw_free,
600 .prepare = soc_pcm_prepare,
601 .trigger = soc_pcm_trigger,
605 /* powers down audio subsystem for suspend */
606 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
608 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
609 struct snd_soc_card *card = socdev->card;
610 struct snd_soc_platform *platform = socdev->platform;
611 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
612 struct snd_soc_codec *codec = socdev->codec;
615 /* Due to the resume being scheduled into a workqueue we could
616 * suspend before that's finished - wait for it to complete.
618 snd_power_lock(codec->card);
619 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
620 snd_power_unlock(codec->card);
622 /* we're going to block userspace touching us until resume completes */
623 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
625 /* mute any active DAC's */
626 for (i = 0; i < card->num_links; i++) {
627 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
628 if (dai->ops.digital_mute && dai->playback.active)
629 dai->ops.digital_mute(dai, 1);
632 /* suspend all pcms */
633 for (i = 0; i < card->num_links; i++)
634 snd_pcm_suspend_all(card->dai_link[i].pcm);
636 if (card->suspend_pre)
637 card->suspend_pre(pdev, state);
639 for (i = 0; i < card->num_links; i++) {
640 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
641 if (cpu_dai->suspend && !cpu_dai->ac97_control)
642 cpu_dai->suspend(pdev, cpu_dai);
643 if (platform->suspend)
644 platform->suspend(pdev, cpu_dai);
647 /* close any waiting streams and save state */
648 run_delayed_work(&socdev->delayed_work);
649 codec->suspend_bias_level = codec->bias_level;
651 for (i = 0; i < codec->num_dai; i++) {
652 char *stream = codec->dai[i].playback.stream_name;
654 snd_soc_dapm_stream_event(codec, stream,
655 SND_SOC_DAPM_STREAM_SUSPEND);
656 stream = codec->dai[i].capture.stream_name;
658 snd_soc_dapm_stream_event(codec, stream,
659 SND_SOC_DAPM_STREAM_SUSPEND);
662 if (codec_dev->suspend)
663 codec_dev->suspend(pdev, state);
665 for (i = 0; i < card->num_links; i++) {
666 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
667 if (cpu_dai->suspend && cpu_dai->ac97_control)
668 cpu_dai->suspend(pdev, cpu_dai);
671 if (card->suspend_post)
672 card->suspend_post(pdev, state);
677 /* deferred resume work, so resume can complete before we finished
678 * setting our codec back up, which can be very slow on I2C
680 static void soc_resume_deferred(struct work_struct *work)
682 struct snd_soc_device *socdev = container_of(work,
683 struct snd_soc_device,
684 deferred_resume_work);
685 struct snd_soc_card *card = socdev->card;
686 struct snd_soc_platform *platform = socdev->platform;
687 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
688 struct snd_soc_codec *codec = socdev->codec;
689 struct platform_device *pdev = to_platform_device(socdev->dev);
692 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
693 * so userspace apps are blocked from touching us
696 dev_dbg(socdev->dev, "starting resume work\n");
698 if (card->resume_pre)
699 card->resume_pre(pdev);
701 for (i = 0; i < card->num_links; i++) {
702 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
703 if (cpu_dai->resume && cpu_dai->ac97_control)
704 cpu_dai->resume(pdev, cpu_dai);
707 if (codec_dev->resume)
708 codec_dev->resume(pdev);
710 for (i = 0; i < codec->num_dai; i++) {
711 char *stream = codec->dai[i].playback.stream_name;
713 snd_soc_dapm_stream_event(codec, stream,
714 SND_SOC_DAPM_STREAM_RESUME);
715 stream = codec->dai[i].capture.stream_name;
717 snd_soc_dapm_stream_event(codec, stream,
718 SND_SOC_DAPM_STREAM_RESUME);
721 /* unmute any active DACs */
722 for (i = 0; i < card->num_links; i++) {
723 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
724 if (dai->ops.digital_mute && dai->playback.active)
725 dai->ops.digital_mute(dai, 0);
728 for (i = 0; i < card->num_links; i++) {
729 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
730 if (cpu_dai->resume && !cpu_dai->ac97_control)
731 cpu_dai->resume(pdev, cpu_dai);
732 if (platform->resume)
733 platform->resume(pdev, cpu_dai);
736 if (card->resume_post)
737 card->resume_post(pdev);
739 dev_dbg(socdev->dev, "resume work completed\n");
741 /* userspace can access us now we are back as we were before */
742 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
745 /* powers up audio subsystem after a suspend */
746 static int soc_resume(struct platform_device *pdev)
748 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
750 dev_dbg(socdev->dev, "scheduling resume work\n");
752 if (!schedule_work(&socdev->deferred_resume_work))
753 dev_err(socdev->dev, "resume work item may be lost\n");
759 #define soc_suspend NULL
760 #define soc_resume NULL
763 /* probes a new socdev */
764 static int soc_probe(struct platform_device *pdev)
767 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
768 struct snd_soc_card *card = socdev->card;
769 struct snd_soc_platform *platform = socdev->platform;
770 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
773 ret = card->probe(pdev);
778 for (i = 0; i < card->num_links; i++) {
779 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
780 if (cpu_dai->probe) {
781 ret = cpu_dai->probe(pdev, cpu_dai);
787 if (codec_dev->probe) {
788 ret = codec_dev->probe(pdev);
793 if (platform->probe) {
794 ret = platform->probe(pdev);
799 /* DAPM stream work */
800 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
802 /* deferred resume work */
803 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
809 if (codec_dev->remove)
810 codec_dev->remove(pdev);
813 for (i--; i >= 0; i--) {
814 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
816 cpu_dai->remove(pdev, cpu_dai);
825 /* removes a socdev */
826 static int soc_remove(struct platform_device *pdev)
829 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
830 struct snd_soc_card *card = socdev->card;
831 struct snd_soc_platform *platform = socdev->platform;
832 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
834 run_delayed_work(&socdev->delayed_work);
836 if (platform->remove)
837 platform->remove(pdev);
839 if (codec_dev->remove)
840 codec_dev->remove(pdev);
842 for (i = 0; i < card->num_links; i++) {
843 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
845 cpu_dai->remove(pdev, cpu_dai);
854 /* ASoC platform driver */
855 static struct platform_driver soc_driver = {
858 .owner = THIS_MODULE,
861 .remove = soc_remove,
862 .suspend = soc_suspend,
863 .resume = soc_resume,
866 /* create a new pcm */
867 static int soc_new_pcm(struct snd_soc_device *socdev,
868 struct snd_soc_dai_link *dai_link, int num)
870 struct snd_soc_codec *codec = socdev->codec;
871 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
872 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
873 struct snd_soc_pcm_runtime *rtd;
876 int ret = 0, playback = 0, capture = 0;
878 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
883 rtd->socdev = socdev;
884 codec_dai->codec = socdev->codec;
886 /* check client and interface hw capabilities */
887 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
890 if (codec_dai->playback.channels_min)
892 if (codec_dai->capture.channels_min)
895 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
898 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
905 pcm->private_data = rtd;
906 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
907 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
908 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
909 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
910 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
911 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
912 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
915 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
918 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
920 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
922 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
927 pcm->private_free = socdev->platform->pcm_free;
928 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
933 /* codec register dump */
934 static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
936 struct snd_soc_codec *codec = devdata->codec;
937 int i, step = 1, count = 0;
939 if (!codec->reg_cache_size)
942 if (codec->reg_cache_step)
943 step = codec->reg_cache_step;
945 count += sprintf(buf, "%s registers\n", codec->name);
946 for (i = 0; i < codec->reg_cache_size; i += step) {
947 count += sprintf(buf + count, "%2x: ", i);
948 if (count >= PAGE_SIZE - 1)
951 if (codec->display_register)
952 count += codec->display_register(codec, buf + count,
953 PAGE_SIZE - count, i);
955 count += snprintf(buf + count, PAGE_SIZE - count,
956 "%4x", codec->read(codec, i));
958 if (count >= PAGE_SIZE - 1)
961 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
962 if (count >= PAGE_SIZE - 1)
966 /* Truncate count; min() would cause a warning */
967 if (count >= PAGE_SIZE)
968 count = PAGE_SIZE - 1;
972 static ssize_t codec_reg_show(struct device *dev,
973 struct device_attribute *attr, char *buf)
975 struct snd_soc_device *devdata = dev_get_drvdata(dev);
976 return soc_codec_reg_show(devdata, buf);
979 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
981 #ifdef CONFIG_DEBUG_FS
982 static int codec_reg_open_file(struct inode *inode, struct file *file)
984 file->private_data = inode->i_private;
988 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
989 size_t count, loff_t *ppos)
992 struct snd_soc_device *devdata = file->private_data;
993 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
996 ret = soc_codec_reg_show(devdata, buf);
998 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1003 static ssize_t codec_reg_write_file(struct file *file,
1004 const char __user *user_buf, size_t count, loff_t *ppos)
1009 unsigned long reg, value;
1011 struct snd_soc_device *devdata = file->private_data;
1012 struct snd_soc_codec *codec = devdata->codec;
1014 buf_size = min(count, (sizeof(buf)-1));
1015 if (copy_from_user(buf, user_buf, buf_size))
1019 if (codec->reg_cache_step)
1020 step = codec->reg_cache_step;
1022 while (*start == ' ')
1024 reg = simple_strtoul(start, &start, 16);
1025 if ((reg >= codec->reg_cache_size) || (reg % step))
1027 while (*start == ' ')
1029 if (strict_strtoul(start, 16, &value))
1031 codec->write(codec, reg, value);
1035 static const struct file_operations codec_reg_fops = {
1036 .open = codec_reg_open_file,
1037 .read = codec_reg_read_file,
1038 .write = codec_reg_write_file,
1041 static void soc_init_debugfs(struct snd_soc_device *socdev)
1043 struct dentry *root, *file;
1044 struct snd_soc_codec *codec = socdev->codec;
1045 root = debugfs_create_dir(dev_name(socdev->dev), NULL);
1046 if (IS_ERR(root) || !root)
1049 file = debugfs_create_file("codec_reg", 0644,
1050 root, socdev, &codec_reg_fops);
1054 file = debugfs_create_u32("dapm_pop_time", 0744,
1055 root, &codec->pop_time);
1058 socdev->debugfs_root = root;
1061 debugfs_remove_recursive(root);
1063 dev_err(socdev->dev, "debugfs is not available\n");
1066 static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1068 debugfs_remove_recursive(socdev->debugfs_root);
1069 socdev->debugfs_root = NULL;
1074 static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1078 static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1084 * snd_soc_new_ac97_codec - initailise AC97 device
1085 * @codec: audio codec
1086 * @ops: AC97 bus operations
1087 * @num: AC97 codec number
1089 * Initialises AC97 codec resources for use by ad-hoc devices only.
1091 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1092 struct snd_ac97_bus_ops *ops, int num)
1094 mutex_lock(&codec->mutex);
1096 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1097 if (codec->ac97 == NULL) {
1098 mutex_unlock(&codec->mutex);
1102 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1103 if (codec->ac97->bus == NULL) {
1106 mutex_unlock(&codec->mutex);
1110 codec->ac97->bus->ops = ops;
1111 codec->ac97->num = num;
1112 mutex_unlock(&codec->mutex);
1115 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1118 * snd_soc_free_ac97_codec - free AC97 codec device
1119 * @codec: audio codec
1121 * Frees AC97 codec device resources.
1123 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1125 mutex_lock(&codec->mutex);
1126 kfree(codec->ac97->bus);
1129 mutex_unlock(&codec->mutex);
1131 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1134 * snd_soc_update_bits - update codec register bits
1135 * @codec: audio codec
1136 * @reg: codec register
1137 * @mask: register mask
1140 * Writes new register value.
1142 * Returns 1 for change else 0.
1144 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1145 unsigned short mask, unsigned short value)
1148 unsigned short old, new;
1150 mutex_lock(&io_mutex);
1151 old = snd_soc_read(codec, reg);
1152 new = (old & ~mask) | value;
1153 change = old != new;
1155 snd_soc_write(codec, reg, new);
1157 mutex_unlock(&io_mutex);
1160 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1163 * snd_soc_test_bits - test register for change
1164 * @codec: audio codec
1165 * @reg: codec register
1166 * @mask: register mask
1169 * Tests a register with a new value and checks if the new value is
1170 * different from the old value.
1172 * Returns 1 for change else 0.
1174 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1175 unsigned short mask, unsigned short value)
1178 unsigned short old, new;
1180 mutex_lock(&io_mutex);
1181 old = snd_soc_read(codec, reg);
1182 new = (old & ~mask) | value;
1183 change = old != new;
1184 mutex_unlock(&io_mutex);
1188 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1191 * snd_soc_new_pcms - create new sound card and pcms
1192 * @socdev: the SoC audio device
1194 * Create a new sound card based upon the codec and interface pcms.
1196 * Returns 0 for success, else error.
1198 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1200 struct snd_soc_codec *codec = socdev->codec;
1201 struct snd_soc_card *card = socdev->card;
1204 mutex_lock(&codec->mutex);
1206 /* register a sound card */
1207 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1209 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1211 mutex_unlock(&codec->mutex);
1215 codec->card->dev = socdev->dev;
1216 codec->card->private_data = codec;
1217 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1219 /* create the pcms */
1220 for (i = 0; i < card->num_links; i++) {
1221 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1223 printk(KERN_ERR "asoc: can't create pcm %s\n",
1224 card->dai_link[i].stream_name);
1225 mutex_unlock(&codec->mutex);
1230 mutex_unlock(&codec->mutex);
1233 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1236 * snd_soc_register_card - register sound card
1237 * @socdev: the SoC audio device
1239 * Register a SoC sound card. Also registers an AC97 device if the
1240 * codec is AC97 for ad hoc devices.
1242 * Returns 0 for success, else error.
1244 int snd_soc_register_card(struct snd_soc_device *socdev)
1246 struct snd_soc_codec *codec = socdev->codec;
1247 struct snd_soc_card *card = socdev->card;
1248 int ret = 0, i, ac97 = 0, err = 0;
1250 for (i = 0; i < card->num_links; i++) {
1251 if (card->dai_link[i].init) {
1252 err = card->dai_link[i].init(codec);
1254 printk(KERN_ERR "asoc: failed to init %s\n",
1255 card->dai_link[i].stream_name);
1259 if (card->dai_link[i].codec_dai->ac97_control)
1262 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1264 snprintf(codec->card->longname, sizeof(codec->card->longname),
1265 "%s (%s)", card->name, codec->name);
1267 ret = snd_card_register(codec->card);
1269 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1274 mutex_lock(&codec->mutex);
1275 #ifdef CONFIG_SND_SOC_AC97_BUS
1277 ret = soc_ac97_dev_register(codec);
1279 printk(KERN_ERR "asoc: AC97 device register failed\n");
1280 snd_card_free(codec->card);
1281 mutex_unlock(&codec->mutex);
1287 err = snd_soc_dapm_sys_add(socdev->dev);
1289 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1291 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1293 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1295 soc_init_debugfs(socdev);
1296 mutex_unlock(&codec->mutex);
1301 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1304 * snd_soc_free_pcms - free sound card and pcms
1305 * @socdev: the SoC audio device
1307 * Frees sound card and pcms associated with the socdev.
1308 * Also unregister the codec if it is an AC97 device.
1310 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1312 struct snd_soc_codec *codec = socdev->codec;
1313 #ifdef CONFIG_SND_SOC_AC97_BUS
1314 struct snd_soc_dai *codec_dai;
1318 mutex_lock(&codec->mutex);
1319 soc_cleanup_debugfs(socdev);
1320 #ifdef CONFIG_SND_SOC_AC97_BUS
1321 for (i = 0; i < codec->num_dai; i++) {
1322 codec_dai = &codec->dai[i];
1323 if (codec_dai->ac97_control && codec->ac97) {
1324 soc_ac97_dev_unregister(codec);
1332 snd_card_free(codec->card);
1333 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1334 mutex_unlock(&codec->mutex);
1336 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1339 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1340 * @substream: the pcm substream
1341 * @hw: the hardware parameters
1343 * Sets the substream runtime hardware parameters.
1345 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1346 const struct snd_pcm_hardware *hw)
1348 struct snd_pcm_runtime *runtime = substream->runtime;
1349 runtime->hw.info = hw->info;
1350 runtime->hw.formats = hw->formats;
1351 runtime->hw.period_bytes_min = hw->period_bytes_min;
1352 runtime->hw.period_bytes_max = hw->period_bytes_max;
1353 runtime->hw.periods_min = hw->periods_min;
1354 runtime->hw.periods_max = hw->periods_max;
1355 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1356 runtime->hw.fifo_size = hw->fifo_size;
1359 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1362 * snd_soc_cnew - create new control
1363 * @_template: control template
1364 * @data: control private data
1365 * @lnng_name: control long name
1367 * Create a new mixer control from a template control.
1369 * Returns 0 for success, else error.
1371 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1372 void *data, char *long_name)
1374 struct snd_kcontrol_new template;
1376 memcpy(&template, _template, sizeof(template));
1378 template.name = long_name;
1381 return snd_ctl_new1(&template, data);
1383 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1386 * snd_soc_info_enum_double - enumerated double mixer info callback
1387 * @kcontrol: mixer control
1388 * @uinfo: control element information
1390 * Callback to provide information about a double enumerated
1393 * Returns 0 for success.
1395 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1396 struct snd_ctl_elem_info *uinfo)
1398 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1400 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1401 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1402 uinfo->value.enumerated.items = e->max;
1404 if (uinfo->value.enumerated.item > e->max - 1)
1405 uinfo->value.enumerated.item = e->max - 1;
1406 strcpy(uinfo->value.enumerated.name,
1407 e->texts[uinfo->value.enumerated.item]);
1410 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1413 * snd_soc_get_enum_double - enumerated double mixer get callback
1414 * @kcontrol: mixer control
1415 * @uinfo: control element information
1417 * Callback to get the value of a double enumerated mixer.
1419 * Returns 0 for success.
1421 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1422 struct snd_ctl_elem_value *ucontrol)
1424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1425 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1426 unsigned short val, bitmask;
1428 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1430 val = snd_soc_read(codec, e->reg);
1431 ucontrol->value.enumerated.item[0]
1432 = (val >> e->shift_l) & (bitmask - 1);
1433 if (e->shift_l != e->shift_r)
1434 ucontrol->value.enumerated.item[1] =
1435 (val >> e->shift_r) & (bitmask - 1);
1439 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1442 * snd_soc_put_enum_double - enumerated double mixer put callback
1443 * @kcontrol: mixer control
1444 * @uinfo: control element information
1446 * Callback to set the value of a double enumerated mixer.
1448 * Returns 0 for success.
1450 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1451 struct snd_ctl_elem_value *ucontrol)
1453 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1454 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1456 unsigned short mask, bitmask;
1458 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1460 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1462 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1463 mask = (bitmask - 1) << e->shift_l;
1464 if (e->shift_l != e->shift_r) {
1465 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1467 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1468 mask |= (bitmask - 1) << e->shift_r;
1471 return snd_soc_update_bits(codec, e->reg, mask, val);
1473 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1476 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1477 * @kcontrol: mixer control
1478 * @uinfo: control element information
1480 * Callback to provide information about an external enumerated
1483 * Returns 0 for success.
1485 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1486 struct snd_ctl_elem_info *uinfo)
1488 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1490 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1492 uinfo->value.enumerated.items = e->max;
1494 if (uinfo->value.enumerated.item > e->max - 1)
1495 uinfo->value.enumerated.item = e->max - 1;
1496 strcpy(uinfo->value.enumerated.name,
1497 e->texts[uinfo->value.enumerated.item]);
1500 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1503 * snd_soc_info_volsw_ext - external single mixer info callback
1504 * @kcontrol: mixer control
1505 * @uinfo: control element information
1507 * Callback to provide information about a single external mixer control.
1509 * Returns 0 for success.
1511 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1512 struct snd_ctl_elem_info *uinfo)
1514 int max = kcontrol->private_value;
1517 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1519 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1522 uinfo->value.integer.min = 0;
1523 uinfo->value.integer.max = max;
1526 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1529 * snd_soc_info_volsw - single mixer info callback
1530 * @kcontrol: mixer control
1531 * @uinfo: control element information
1533 * Callback to provide information about a single mixer control.
1535 * Returns 0 for success.
1537 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_info *uinfo)
1540 struct soc_mixer_control *mc =
1541 (struct soc_mixer_control *)kcontrol->private_value;
1543 unsigned int shift = mc->shift;
1544 unsigned int rshift = mc->rshift;
1547 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1549 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1551 uinfo->count = shift == rshift ? 1 : 2;
1552 uinfo->value.integer.min = 0;
1553 uinfo->value.integer.max = max;
1556 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1559 * snd_soc_get_volsw - single mixer get callback
1560 * @kcontrol: mixer control
1561 * @uinfo: control element information
1563 * Callback to get the value of a single mixer control.
1565 * Returns 0 for success.
1567 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1570 struct soc_mixer_control *mc =
1571 (struct soc_mixer_control *)kcontrol->private_value;
1572 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1573 unsigned int reg = mc->reg;
1574 unsigned int shift = mc->shift;
1575 unsigned int rshift = mc->rshift;
1577 unsigned int mask = (1 << fls(max)) - 1;
1578 unsigned int invert = mc->invert;
1580 ucontrol->value.integer.value[0] =
1581 (snd_soc_read(codec, reg) >> shift) & mask;
1582 if (shift != rshift)
1583 ucontrol->value.integer.value[1] =
1584 (snd_soc_read(codec, reg) >> rshift) & mask;
1586 ucontrol->value.integer.value[0] =
1587 max - ucontrol->value.integer.value[0];
1588 if (shift != rshift)
1589 ucontrol->value.integer.value[1] =
1590 max - ucontrol->value.integer.value[1];
1595 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1598 * snd_soc_put_volsw - single mixer put callback
1599 * @kcontrol: mixer control
1600 * @uinfo: control element information
1602 * Callback to set the value of a single mixer control.
1604 * Returns 0 for success.
1606 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1607 struct snd_ctl_elem_value *ucontrol)
1609 struct soc_mixer_control *mc =
1610 (struct soc_mixer_control *)kcontrol->private_value;
1611 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1612 unsigned int reg = mc->reg;
1613 unsigned int shift = mc->shift;
1614 unsigned int rshift = mc->rshift;
1616 unsigned int mask = (1 << fls(max)) - 1;
1617 unsigned int invert = mc->invert;
1618 unsigned short val, val2, val_mask;
1620 val = (ucontrol->value.integer.value[0] & mask);
1623 val_mask = mask << shift;
1625 if (shift != rshift) {
1626 val2 = (ucontrol->value.integer.value[1] & mask);
1629 val_mask |= mask << rshift;
1630 val |= val2 << rshift;
1632 return snd_soc_update_bits(codec, reg, val_mask, val);
1634 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1637 * snd_soc_info_volsw_2r - double mixer info callback
1638 * @kcontrol: mixer control
1639 * @uinfo: control element information
1641 * Callback to provide information about a double mixer control that
1642 * spans 2 codec registers.
1644 * Returns 0 for success.
1646 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_info *uinfo)
1649 struct soc_mixer_control *mc =
1650 (struct soc_mixer_control *)kcontrol->private_value;
1654 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1656 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1659 uinfo->value.integer.min = 0;
1660 uinfo->value.integer.max = max;
1663 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1666 * snd_soc_get_volsw_2r - double mixer get callback
1667 * @kcontrol: mixer control
1668 * @uinfo: control element information
1670 * Callback to get the value of a double mixer control that spans 2 registers.
1672 * Returns 0 for success.
1674 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1675 struct snd_ctl_elem_value *ucontrol)
1677 struct soc_mixer_control *mc =
1678 (struct soc_mixer_control *)kcontrol->private_value;
1679 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1680 unsigned int reg = mc->reg;
1681 unsigned int reg2 = mc->rreg;
1682 unsigned int shift = mc->shift;
1684 unsigned int mask = (1<<fls(max))-1;
1685 unsigned int invert = mc->invert;
1687 ucontrol->value.integer.value[0] =
1688 (snd_soc_read(codec, reg) >> shift) & mask;
1689 ucontrol->value.integer.value[1] =
1690 (snd_soc_read(codec, reg2) >> shift) & mask;
1692 ucontrol->value.integer.value[0] =
1693 max - ucontrol->value.integer.value[0];
1694 ucontrol->value.integer.value[1] =
1695 max - ucontrol->value.integer.value[1];
1700 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1703 * snd_soc_put_volsw_2r - double mixer set callback
1704 * @kcontrol: mixer control
1705 * @uinfo: control element information
1707 * Callback to set the value of a double mixer control that spans 2 registers.
1709 * Returns 0 for success.
1711 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol)
1714 struct soc_mixer_control *mc =
1715 (struct soc_mixer_control *)kcontrol->private_value;
1716 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1717 unsigned int reg = mc->reg;
1718 unsigned int reg2 = mc->rreg;
1719 unsigned int shift = mc->shift;
1721 unsigned int mask = (1 << fls(max)) - 1;
1722 unsigned int invert = mc->invert;
1724 unsigned short val, val2, val_mask;
1726 val_mask = mask << shift;
1727 val = (ucontrol->value.integer.value[0] & mask);
1728 val2 = (ucontrol->value.integer.value[1] & mask);
1736 val2 = val2 << shift;
1738 err = snd_soc_update_bits(codec, reg, val_mask, val);
1742 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1745 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1748 * snd_soc_info_volsw_s8 - signed mixer info callback
1749 * @kcontrol: mixer control
1750 * @uinfo: control element information
1752 * Callback to provide information about a signed mixer control.
1754 * Returns 0 for success.
1756 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1757 struct snd_ctl_elem_info *uinfo)
1759 struct soc_mixer_control *mc =
1760 (struct soc_mixer_control *)kcontrol->private_value;
1764 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1766 uinfo->value.integer.min = 0;
1767 uinfo->value.integer.max = max-min;
1770 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1773 * snd_soc_get_volsw_s8 - signed mixer get callback
1774 * @kcontrol: mixer control
1775 * @uinfo: control element information
1777 * Callback to get the value of a signed mixer control.
1779 * Returns 0 for success.
1781 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_value *ucontrol)
1784 struct soc_mixer_control *mc =
1785 (struct soc_mixer_control *)kcontrol->private_value;
1786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1787 unsigned int reg = mc->reg;
1789 int val = snd_soc_read(codec, reg);
1791 ucontrol->value.integer.value[0] =
1792 ((signed char)(val & 0xff))-min;
1793 ucontrol->value.integer.value[1] =
1794 ((signed char)((val >> 8) & 0xff))-min;
1797 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1800 * snd_soc_put_volsw_sgn - signed mixer put callback
1801 * @kcontrol: mixer control
1802 * @uinfo: control element information
1804 * Callback to set the value of a signed mixer control.
1806 * Returns 0 for success.
1808 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_value *ucontrol)
1811 struct soc_mixer_control *mc =
1812 (struct soc_mixer_control *)kcontrol->private_value;
1813 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1814 unsigned int reg = mc->reg;
1818 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1819 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1821 return snd_soc_update_bits(codec, reg, 0xffff, val);
1823 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1826 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1828 * @clk_id: DAI specific clock ID
1829 * @freq: new clock frequency in Hz
1830 * @dir: new clock direction - input/output.
1832 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1834 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1835 unsigned int freq, int dir)
1837 if (dai->ops.set_sysclk)
1838 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
1842 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1845 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1847 * @clk_id: DAI specific clock divider ID
1848 * @div: new clock divisor.
1850 * Configures the clock dividers. This is used to derive the best DAI bit and
1851 * frame clocks from the system or master clock. It's best to set the DAI bit
1852 * and frame clocks as low as possible to save system power.
1854 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1855 int div_id, int div)
1857 if (dai->ops.set_clkdiv)
1858 return dai->ops.set_clkdiv(dai, div_id, div);
1862 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1865 * snd_soc_dai_set_pll - configure DAI PLL.
1867 * @pll_id: DAI specific PLL ID
1868 * @freq_in: PLL input clock frequency in Hz
1869 * @freq_out: requested PLL output clock frequency in Hz
1871 * Configures and enables PLL to generate output clock based on input clock.
1873 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1874 int pll_id, unsigned int freq_in, unsigned int freq_out)
1876 if (dai->ops.set_pll)
1877 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
1881 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1884 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1886 * @fmt: SND_SOC_DAIFMT_ format value.
1888 * Configures the DAI hardware format and clocking.
1890 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1892 if (dai->ops.set_fmt)
1893 return dai->ops.set_fmt(dai, fmt);
1897 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1900 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1902 * @mask: DAI specific mask representing used slots.
1903 * @slots: Number of slots in use.
1905 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1908 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1909 unsigned int mask, int slots)
1911 if (dai->ops.set_sysclk)
1912 return dai->ops.set_tdm_slot(dai, mask, slots);
1916 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1919 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1921 * @tristate: tristate enable
1923 * Tristates the DAI so that others can use it.
1925 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1927 if (dai->ops.set_sysclk)
1928 return dai->ops.set_tristate(dai, tristate);
1932 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1935 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1937 * @mute: mute enable
1939 * Mutes the DAI DAC.
1941 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1943 if (dai->ops.digital_mute)
1944 return dai->ops.digital_mute(dai, mute);
1948 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1950 static int __devinit snd_soc_init(void)
1952 return platform_driver_register(&soc_driver);
1955 static void snd_soc_exit(void)
1957 platform_driver_unregister(&soc_driver);
1960 module_init(snd_soc_init);
1961 module_exit(snd_soc_exit);
1963 /* Module information */
1964 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1965 MODULE_DESCRIPTION("ALSA SoC Core");
1966 MODULE_LICENSE("GPL");
1967 MODULE_ALIAS("platform:soc-audio");