2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
8 * with code, comments and ideas from :-
9 * Richard Purdie <richard@openedhand.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 * o Add hw rules to enforce rates, etc.
18 * o More testing with other codecs/machines.
19 * o Add more codecs and platforms to ensure good API coverage.
20 * o Support TDM on PCM and I2S
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
28 #include <linux/bitops.h>
29 #include <linux/debugfs.h>
30 #include <linux/platform_device.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/soc-dapm.h>
36 #include <sound/initval.h>
38 static DEFINE_MUTEX(pcm_mutex);
39 static DEFINE_MUTEX(io_mutex);
40 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
42 #ifdef CONFIG_DEBUG_FS
43 static struct dentry *debugfs_root;
46 static DEFINE_MUTEX(client_mutex);
47 static LIST_HEAD(card_list);
48 static LIST_HEAD(dai_list);
49 static LIST_HEAD(platform_list);
51 static int snd_soc_register_card(struct snd_soc_card *card);
52 static int snd_soc_unregister_card(struct snd_soc_card *card);
55 * This is a timeout to do a DAPM powerdown after a stream is closed().
56 * It can be used to eliminate pops between different playback streams, e.g.
57 * between two audio tracks.
59 static int pmdown_time = 5000;
60 module_param(pmdown_time, int, 0);
61 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
64 * This function forces any delayed work to be queued and run.
66 static int run_delayed_work(struct delayed_work *dwork)
70 /* cancel any work waiting to be queued. */
71 ret = cancel_delayed_work(dwork);
73 /* if there was any work waiting then we run it now and
74 * wait for it's completion */
76 schedule_delayed_work(dwork, 0);
77 flush_scheduled_work();
82 #ifdef CONFIG_SND_SOC_AC97_BUS
83 /* unregister ac97 codec */
84 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
86 if (codec->ac97->dev.bus)
87 device_unregister(&codec->ac97->dev);
91 /* stop no dev release warning */
92 static void soc_ac97_device_release(struct device *dev){}
94 /* register ac97 codec to bus */
95 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
99 codec->ac97->dev.bus = &ac97_bus_type;
100 codec->ac97->dev.parent = NULL;
101 codec->ac97->dev.release = soc_ac97_device_release;
103 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
104 codec->card->number, 0, codec->name);
105 err = device_register(&codec->ac97->dev);
107 snd_printk(KERN_ERR "Can't register ac97 bus\n");
108 codec->ac97->dev.bus = NULL;
116 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
117 * then initialized and any private data can be allocated. This also calls
118 * startup for the cpu DAI, platform, machine and codec DAI.
120 static int soc_pcm_open(struct snd_pcm_substream *substream)
122 struct snd_soc_pcm_runtime *rtd = substream->private_data;
123 struct snd_soc_device *socdev = rtd->socdev;
124 struct snd_soc_card *card = socdev->card;
125 struct snd_pcm_runtime *runtime = substream->runtime;
126 struct snd_soc_dai_link *machine = rtd->dai;
127 struct snd_soc_platform *platform = card->platform;
128 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
129 struct snd_soc_dai *codec_dai = machine->codec_dai;
132 mutex_lock(&pcm_mutex);
134 /* startup the audio subsystem */
135 if (cpu_dai->ops.startup) {
136 ret = cpu_dai->ops.startup(substream, cpu_dai);
138 printk(KERN_ERR "asoc: can't open interface %s\n",
144 if (platform->pcm_ops->open) {
145 ret = platform->pcm_ops->open(substream);
147 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
152 if (codec_dai->ops.startup) {
153 ret = codec_dai->ops.startup(substream, codec_dai);
155 printk(KERN_ERR "asoc: can't open codec %s\n",
161 if (machine->ops && machine->ops->startup) {
162 ret = machine->ops->startup(substream);
164 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
169 /* Check that the codec and cpu DAI's are compatible */
170 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
171 runtime->hw.rate_min =
172 max(codec_dai->playback.rate_min,
173 cpu_dai->playback.rate_min);
174 runtime->hw.rate_max =
175 min(codec_dai->playback.rate_max,
176 cpu_dai->playback.rate_max);
177 runtime->hw.channels_min =
178 max(codec_dai->playback.channels_min,
179 cpu_dai->playback.channels_min);
180 runtime->hw.channels_max =
181 min(codec_dai->playback.channels_max,
182 cpu_dai->playback.channels_max);
183 runtime->hw.formats =
184 codec_dai->playback.formats & cpu_dai->playback.formats;
186 codec_dai->playback.rates & cpu_dai->playback.rates;
188 runtime->hw.rate_min =
189 max(codec_dai->capture.rate_min,
190 cpu_dai->capture.rate_min);
191 runtime->hw.rate_max =
192 min(codec_dai->capture.rate_max,
193 cpu_dai->capture.rate_max);
194 runtime->hw.channels_min =
195 max(codec_dai->capture.channels_min,
196 cpu_dai->capture.channels_min);
197 runtime->hw.channels_max =
198 min(codec_dai->capture.channels_max,
199 cpu_dai->capture.channels_max);
200 runtime->hw.formats =
201 codec_dai->capture.formats & cpu_dai->capture.formats;
203 codec_dai->capture.rates & cpu_dai->capture.rates;
206 snd_pcm_limit_hw_rates(runtime);
207 if (!runtime->hw.rates) {
208 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
209 codec_dai->name, cpu_dai->name);
212 if (!runtime->hw.formats) {
213 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
214 codec_dai->name, cpu_dai->name);
217 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
218 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
219 codec_dai->name, cpu_dai->name);
223 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
224 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
225 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
226 runtime->hw.channels_max);
227 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
228 runtime->hw.rate_max);
230 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
231 cpu_dai->playback.active = codec_dai->playback.active = 1;
233 cpu_dai->capture.active = codec_dai->capture.active = 1;
234 cpu_dai->active = codec_dai->active = 1;
235 cpu_dai->runtime = runtime;
236 socdev->codec->active++;
237 mutex_unlock(&pcm_mutex);
241 if (machine->ops && machine->ops->shutdown)
242 machine->ops->shutdown(substream);
245 if (platform->pcm_ops->close)
246 platform->pcm_ops->close(substream);
249 if (cpu_dai->ops.shutdown)
250 cpu_dai->ops.shutdown(substream, cpu_dai);
252 mutex_unlock(&pcm_mutex);
257 * Power down the audio subsystem pmdown_time msecs after close is called.
258 * This is to ensure there are no pops or clicks in between any music tracks
259 * due to DAPM power cycling.
261 static void close_delayed_work(struct work_struct *work)
263 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
265 struct snd_soc_device *socdev = card->socdev;
266 struct snd_soc_codec *codec = socdev->codec;
267 struct snd_soc_dai *codec_dai;
270 mutex_lock(&pcm_mutex);
271 for (i = 0; i < codec->num_dai; i++) {
272 codec_dai = &codec->dai[i];
274 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
275 codec_dai->playback.stream_name,
276 codec_dai->playback.active ? "active" : "inactive",
277 codec_dai->pop_wait ? "yes" : "no");
279 /* are we waiting on this codec DAI stream */
280 if (codec_dai->pop_wait == 1) {
282 /* Reduce power if no longer active */
283 if (codec->active == 0) {
284 pr_debug("pop wq D1 %s %s\n", codec->name,
285 codec_dai->playback.stream_name);
286 snd_soc_dapm_set_bias_level(socdev,
287 SND_SOC_BIAS_PREPARE);
290 codec_dai->pop_wait = 0;
291 snd_soc_dapm_stream_event(codec,
292 codec_dai->playback.stream_name,
293 SND_SOC_DAPM_STREAM_STOP);
295 /* Fall into standby if no longer active */
296 if (codec->active == 0) {
297 pr_debug("pop wq D3 %s %s\n", codec->name,
298 codec_dai->playback.stream_name);
299 snd_soc_dapm_set_bias_level(socdev,
300 SND_SOC_BIAS_STANDBY);
304 mutex_unlock(&pcm_mutex);
308 * Called by ALSA when a PCM substream is closed. Private data can be
309 * freed here. The cpu DAI, codec DAI, machine and platform are also
312 static int soc_codec_close(struct snd_pcm_substream *substream)
314 struct snd_soc_pcm_runtime *rtd = substream->private_data;
315 struct snd_soc_device *socdev = rtd->socdev;
316 struct snd_soc_card *card = socdev->card;
317 struct snd_soc_dai_link *machine = rtd->dai;
318 struct snd_soc_platform *platform = card->platform;
319 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
320 struct snd_soc_dai *codec_dai = machine->codec_dai;
321 struct snd_soc_codec *codec = socdev->codec;
323 mutex_lock(&pcm_mutex);
325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
326 cpu_dai->playback.active = codec_dai->playback.active = 0;
328 cpu_dai->capture.active = codec_dai->capture.active = 0;
330 if (codec_dai->playback.active == 0 &&
331 codec_dai->capture.active == 0) {
332 cpu_dai->active = codec_dai->active = 0;
336 /* Muting the DAC suppresses artifacts caused during digital
337 * shutdown, for example from stopping clocks.
339 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
340 snd_soc_dai_digital_mute(codec_dai, 1);
342 if (cpu_dai->ops.shutdown)
343 cpu_dai->ops.shutdown(substream, cpu_dai);
345 if (codec_dai->ops.shutdown)
346 codec_dai->ops.shutdown(substream, codec_dai);
348 if (machine->ops && machine->ops->shutdown)
349 machine->ops->shutdown(substream);
351 if (platform->pcm_ops->close)
352 platform->pcm_ops->close(substream);
353 cpu_dai->runtime = NULL;
355 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
356 /* start delayed pop wq here for playback streams */
357 codec_dai->pop_wait = 1;
358 schedule_delayed_work(&card->delayed_work,
359 msecs_to_jiffies(pmdown_time));
361 /* capture streams can be powered down now */
362 snd_soc_dapm_stream_event(codec,
363 codec_dai->capture.stream_name,
364 SND_SOC_DAPM_STREAM_STOP);
366 if (codec->active == 0 && codec_dai->pop_wait == 0)
367 snd_soc_dapm_set_bias_level(socdev,
368 SND_SOC_BIAS_STANDBY);
371 mutex_unlock(&pcm_mutex);
376 * Called by ALSA when the PCM substream is prepared, can set format, sample
377 * rate, etc. This function is non atomic and can be called multiple times,
378 * it can refer to the runtime info.
380 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
382 struct snd_soc_pcm_runtime *rtd = substream->private_data;
383 struct snd_soc_device *socdev = rtd->socdev;
384 struct snd_soc_card *card = socdev->card;
385 struct snd_soc_dai_link *machine = rtd->dai;
386 struct snd_soc_platform *platform = card->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, codec_dai);
413 printk(KERN_ERR "asoc: codec DAI prepare error\n");
418 if (cpu_dai->ops.prepare) {
419 ret = cpu_dai->ops.prepare(substream, cpu_dai);
421 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
426 /* cancel any delayed stream shutdown that is pending */
427 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
428 codec_dai->pop_wait) {
429 codec_dai->pop_wait = 0;
430 cancel_delayed_work(&card->delayed_work);
433 /* do we need to power up codec */
434 if (codec->bias_level != SND_SOC_BIAS_ON) {
435 snd_soc_dapm_set_bias_level(socdev,
436 SND_SOC_BIAS_PREPARE);
438 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
439 snd_soc_dapm_stream_event(codec,
440 codec_dai->playback.stream_name,
441 SND_SOC_DAPM_STREAM_START);
443 snd_soc_dapm_stream_event(codec,
444 codec_dai->capture.stream_name,
445 SND_SOC_DAPM_STREAM_START);
447 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
448 snd_soc_dai_digital_mute(codec_dai, 0);
451 /* codec already powered - power on widgets */
452 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
453 snd_soc_dapm_stream_event(codec,
454 codec_dai->playback.stream_name,
455 SND_SOC_DAPM_STREAM_START);
457 snd_soc_dapm_stream_event(codec,
458 codec_dai->capture.stream_name,
459 SND_SOC_DAPM_STREAM_START);
461 snd_soc_dai_digital_mute(codec_dai, 0);
465 mutex_unlock(&pcm_mutex);
470 * Called by ALSA when the hardware params are set by application. This
471 * function can also be called multiple times and can allocate buffers
472 * (using snd_pcm_lib_* ). It's non-atomic.
474 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
475 struct snd_pcm_hw_params *params)
477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
478 struct snd_soc_device *socdev = rtd->socdev;
479 struct snd_soc_dai_link *machine = rtd->dai;
480 struct snd_soc_card *card = socdev->card;
481 struct snd_soc_platform *platform = card->platform;
482 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
483 struct snd_soc_dai *codec_dai = machine->codec_dai;
486 mutex_lock(&pcm_mutex);
488 if (machine->ops && machine->ops->hw_params) {
489 ret = machine->ops->hw_params(substream, params);
491 printk(KERN_ERR "asoc: machine hw_params failed\n");
496 if (codec_dai->ops.hw_params) {
497 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
499 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
505 if (cpu_dai->ops.hw_params) {
506 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
508 printk(KERN_ERR "asoc: interface %s hw params failed\n",
514 if (platform->pcm_ops->hw_params) {
515 ret = platform->pcm_ops->hw_params(substream, params);
517 printk(KERN_ERR "asoc: platform %s hw params failed\n",
524 mutex_unlock(&pcm_mutex);
528 if (cpu_dai->ops.hw_free)
529 cpu_dai->ops.hw_free(substream, cpu_dai);
532 if (codec_dai->ops.hw_free)
533 codec_dai->ops.hw_free(substream, codec_dai);
536 if (machine->ops && machine->ops->hw_free)
537 machine->ops->hw_free(substream);
539 mutex_unlock(&pcm_mutex);
544 * Free's resources allocated by hw_params, can be called multiple times
546 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
548 struct snd_soc_pcm_runtime *rtd = substream->private_data;
549 struct snd_soc_device *socdev = rtd->socdev;
550 struct snd_soc_dai_link *machine = rtd->dai;
551 struct snd_soc_card *card = socdev->card;
552 struct snd_soc_platform *platform = card->platform;
553 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
554 struct snd_soc_dai *codec_dai = machine->codec_dai;
555 struct snd_soc_codec *codec = socdev->codec;
557 mutex_lock(&pcm_mutex);
559 /* apply codec digital mute */
561 snd_soc_dai_digital_mute(codec_dai, 1);
563 /* free any machine hw params */
564 if (machine->ops && machine->ops->hw_free)
565 machine->ops->hw_free(substream);
567 /* free any DMA resources */
568 if (platform->pcm_ops->hw_free)
569 platform->pcm_ops->hw_free(substream);
571 /* now free hw params for the DAI's */
572 if (codec_dai->ops.hw_free)
573 codec_dai->ops.hw_free(substream, codec_dai);
575 if (cpu_dai->ops.hw_free)
576 cpu_dai->ops.hw_free(substream, cpu_dai);
578 mutex_unlock(&pcm_mutex);
582 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
584 struct snd_soc_pcm_runtime *rtd = substream->private_data;
585 struct snd_soc_device *socdev = rtd->socdev;
586 struct snd_soc_card *card= socdev->card;
587 struct snd_soc_dai_link *machine = rtd->dai;
588 struct snd_soc_platform *platform = card->platform;
589 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
590 struct snd_soc_dai *codec_dai = machine->codec_dai;
593 if (codec_dai->ops.trigger) {
594 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
599 if (platform->pcm_ops->trigger) {
600 ret = platform->pcm_ops->trigger(substream, cmd);
605 if (cpu_dai->ops.trigger) {
606 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
613 /* ASoC PCM operations */
614 static struct snd_pcm_ops soc_pcm_ops = {
615 .open = soc_pcm_open,
616 .close = soc_codec_close,
617 .hw_params = soc_pcm_hw_params,
618 .hw_free = soc_pcm_hw_free,
619 .prepare = soc_pcm_prepare,
620 .trigger = soc_pcm_trigger,
624 /* powers down audio subsystem for suspend */
625 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
627 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
628 struct snd_soc_card *card = socdev->card;
629 struct snd_soc_platform *platform = card->platform;
630 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
631 struct snd_soc_codec *codec = socdev->codec;
634 /* Due to the resume being scheduled into a workqueue we could
635 * suspend before that's finished - wait for it to complete.
637 snd_power_lock(codec->card);
638 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
639 snd_power_unlock(codec->card);
641 /* we're going to block userspace touching us until resume completes */
642 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
644 /* mute any active DAC's */
645 for (i = 0; i < card->num_links; i++) {
646 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
647 if (dai->ops.digital_mute && dai->playback.active)
648 dai->ops.digital_mute(dai, 1);
651 /* suspend all pcms */
652 for (i = 0; i < card->num_links; i++)
653 snd_pcm_suspend_all(card->dai_link[i].pcm);
655 if (card->suspend_pre)
656 card->suspend_pre(pdev, state);
658 for (i = 0; i < card->num_links; i++) {
659 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
660 if (cpu_dai->suspend && !cpu_dai->ac97_control)
661 cpu_dai->suspend(cpu_dai);
662 if (platform->suspend)
663 platform->suspend(cpu_dai);
666 /* close any waiting streams and save state */
667 run_delayed_work(&card->delayed_work);
668 codec->suspend_bias_level = codec->bias_level;
670 for (i = 0; i < codec->num_dai; i++) {
671 char *stream = codec->dai[i].playback.stream_name;
673 snd_soc_dapm_stream_event(codec, stream,
674 SND_SOC_DAPM_STREAM_SUSPEND);
675 stream = codec->dai[i].capture.stream_name;
677 snd_soc_dapm_stream_event(codec, stream,
678 SND_SOC_DAPM_STREAM_SUSPEND);
681 if (codec_dev->suspend)
682 codec_dev->suspend(pdev, state);
684 for (i = 0; i < card->num_links; i++) {
685 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
686 if (cpu_dai->suspend && cpu_dai->ac97_control)
687 cpu_dai->suspend(cpu_dai);
690 if (card->suspend_post)
691 card->suspend_post(pdev, state);
696 /* deferred resume work, so resume can complete before we finished
697 * setting our codec back up, which can be very slow on I2C
699 static void soc_resume_deferred(struct work_struct *work)
701 struct snd_soc_card *card = container_of(work,
703 deferred_resume_work);
704 struct snd_soc_device *socdev = card->socdev;
705 struct snd_soc_platform *platform = card->platform;
706 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
707 struct snd_soc_codec *codec = socdev->codec;
708 struct platform_device *pdev = to_platform_device(socdev->dev);
711 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
712 * so userspace apps are blocked from touching us
715 dev_dbg(socdev->dev, "starting resume work\n");
717 if (card->resume_pre)
718 card->resume_pre(pdev);
720 for (i = 0; i < card->num_links; i++) {
721 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
722 if (cpu_dai->resume && cpu_dai->ac97_control)
723 cpu_dai->resume(cpu_dai);
726 if (codec_dev->resume)
727 codec_dev->resume(pdev);
729 for (i = 0; i < codec->num_dai; i++) {
730 char *stream = codec->dai[i].playback.stream_name;
732 snd_soc_dapm_stream_event(codec, stream,
733 SND_SOC_DAPM_STREAM_RESUME);
734 stream = codec->dai[i].capture.stream_name;
736 snd_soc_dapm_stream_event(codec, stream,
737 SND_SOC_DAPM_STREAM_RESUME);
740 /* unmute any active DACs */
741 for (i = 0; i < card->num_links; i++) {
742 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
743 if (dai->ops.digital_mute && dai->playback.active)
744 dai->ops.digital_mute(dai, 0);
747 for (i = 0; i < card->num_links; i++) {
748 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
749 if (cpu_dai->resume && !cpu_dai->ac97_control)
750 cpu_dai->resume(cpu_dai);
751 if (platform->resume)
752 platform->resume(cpu_dai);
755 if (card->resume_post)
756 card->resume_post(pdev);
758 dev_dbg(socdev->dev, "resume work completed\n");
760 /* userspace can access us now we are back as we were before */
761 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
764 /* powers up audio subsystem after a suspend */
765 static int soc_resume(struct platform_device *pdev)
767 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
768 struct snd_soc_card *card = socdev->card;
770 dev_dbg(socdev->dev, "scheduling resume work\n");
772 if (!schedule_work(&card->deferred_resume_work))
773 dev_err(socdev->dev, "resume work item may be lost\n");
779 #define soc_suspend NULL
780 #define soc_resume NULL
783 static void snd_soc_instantiate_card(struct snd_soc_card *card)
785 struct platform_device *pdev = container_of(card->dev,
786 struct platform_device,
788 struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
789 struct snd_soc_platform *platform;
790 struct snd_soc_dai *dai;
791 int i, found, ret, ac97;
793 if (card->instantiated)
797 list_for_each_entry(platform, &platform_list, list)
798 if (card->platform == platform) {
803 dev_dbg(card->dev, "Platform %s not registered\n",
804 card->platform->name);
809 for (i = 0; i < card->num_links; i++) {
811 list_for_each_entry(dai, &dai_list, list)
812 if (card->dai_link[i].cpu_dai == dai) {
817 dev_dbg(card->dev, "DAI %s not registered\n",
818 card->dai_link[i].cpu_dai->name);
822 if (card->dai_link[i].cpu_dai->ac97_control)
826 /* If we have AC97 in the system then don't wait for the
827 * codec. This will need revisiting if we have to handle
828 * systems with mixed AC97 and non-AC97 parts. Only check for
829 * DAIs currently; we can't do this per link since some AC97
830 * codecs have non-AC97 DAIs.
833 for (i = 0; i < card->num_links; i++) {
835 list_for_each_entry(dai, &dai_list, list)
836 if (card->dai_link[i].codec_dai == dai) {
841 dev_dbg(card->dev, "DAI %s not registered\n",
842 card->dai_link[i].codec_dai->name);
847 /* Note that we do not current check for codec components */
849 dev_dbg(card->dev, "All components present, instantiating\n");
851 /* Found everything, bring it up */
853 ret = card->probe(pdev);
858 for (i = 0; i < card->num_links; i++) {
859 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
860 if (cpu_dai->probe) {
861 ret = cpu_dai->probe(pdev, cpu_dai);
867 if (codec_dev->probe) {
868 ret = codec_dev->probe(pdev);
873 if (platform->probe) {
874 ret = platform->probe(pdev);
879 /* DAPM stream work */
880 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
882 /* deferred resume work */
883 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
886 card->instantiated = 1;
891 if (codec_dev->remove)
892 codec_dev->remove(pdev);
895 for (i--; i >= 0; i--) {
896 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
898 cpu_dai->remove(pdev, cpu_dai);
906 * Attempt to initialise any uninitalised cards. Must be called with
909 static void snd_soc_instantiate_cards(void)
911 struct snd_soc_card *card;
912 list_for_each_entry(card, &card_list, list)
913 snd_soc_instantiate_card(card);
916 /* probes a new socdev */
917 static int soc_probe(struct platform_device *pdev)
920 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
921 struct snd_soc_card *card = socdev->card;
923 /* Bodge while we push things out of socdev */
924 card->socdev = socdev;
926 /* Bodge while we unpick instantiation */
927 card->dev = &pdev->dev;
928 ret = snd_soc_register_card(card);
930 dev_err(&pdev->dev, "Failed to register card\n");
937 /* removes a socdev */
938 static int soc_remove(struct platform_device *pdev)
941 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
942 struct snd_soc_card *card = socdev->card;
943 struct snd_soc_platform *platform = card->platform;
944 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
946 run_delayed_work(&card->delayed_work);
948 if (platform->remove)
949 platform->remove(pdev);
951 if (codec_dev->remove)
952 codec_dev->remove(pdev);
954 for (i = 0; i < card->num_links; i++) {
955 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
957 cpu_dai->remove(pdev, cpu_dai);
963 snd_soc_unregister_card(card);
968 /* ASoC platform driver */
969 static struct platform_driver soc_driver = {
972 .owner = THIS_MODULE,
975 .remove = soc_remove,
976 .suspend = soc_suspend,
977 .resume = soc_resume,
980 /* create a new pcm */
981 static int soc_new_pcm(struct snd_soc_device *socdev,
982 struct snd_soc_dai_link *dai_link, int num)
984 struct snd_soc_codec *codec = socdev->codec;
985 struct snd_soc_card *card = socdev->card;
986 struct snd_soc_platform *platform = card->platform;
987 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
988 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
989 struct snd_soc_pcm_runtime *rtd;
992 int ret = 0, playback = 0, capture = 0;
994 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
999 rtd->socdev = socdev;
1000 codec_dai->codec = socdev->codec;
1002 /* check client and interface hw capabilities */
1003 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
1006 if (codec_dai->playback.channels_min)
1008 if (codec_dai->capture.channels_min)
1011 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1014 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
1020 dai_link->pcm = pcm;
1021 pcm->private_data = rtd;
1022 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
1023 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
1024 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
1025 soc_pcm_ops.copy = platform->pcm_ops->copy;
1026 soc_pcm_ops.silence = platform->pcm_ops->silence;
1027 soc_pcm_ops.ack = platform->pcm_ops->ack;
1028 soc_pcm_ops.page = platform->pcm_ops->page;
1031 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1034 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1036 ret = platform->pcm_new(codec->card, codec_dai, pcm);
1038 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1043 pcm->private_free = platform->pcm_free;
1044 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1049 /* codec register dump */
1050 static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
1052 struct snd_soc_codec *codec = devdata->codec;
1053 int i, step = 1, count = 0;
1055 if (!codec->reg_cache_size)
1058 if (codec->reg_cache_step)
1059 step = codec->reg_cache_step;
1061 count += sprintf(buf, "%s registers\n", codec->name);
1062 for (i = 0; i < codec->reg_cache_size; i += step) {
1063 count += sprintf(buf + count, "%2x: ", i);
1064 if (count >= PAGE_SIZE - 1)
1067 if (codec->display_register)
1068 count += codec->display_register(codec, buf + count,
1069 PAGE_SIZE - count, i);
1071 count += snprintf(buf + count, PAGE_SIZE - count,
1072 "%4x", codec->read(codec, i));
1074 if (count >= PAGE_SIZE - 1)
1077 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
1078 if (count >= PAGE_SIZE - 1)
1082 /* Truncate count; min() would cause a warning */
1083 if (count >= PAGE_SIZE)
1084 count = PAGE_SIZE - 1;
1088 static ssize_t codec_reg_show(struct device *dev,
1089 struct device_attribute *attr, char *buf)
1091 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1092 return soc_codec_reg_show(devdata, buf);
1095 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1097 #ifdef CONFIG_DEBUG_FS
1098 static int codec_reg_open_file(struct inode *inode, struct file *file)
1100 file->private_data = inode->i_private;
1104 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1105 size_t count, loff_t *ppos)
1108 struct snd_soc_codec *codec = file->private_data;
1109 struct device *card_dev = codec->card->dev;
1110 struct snd_soc_device *devdata = card_dev->driver_data;
1111 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1114 ret = soc_codec_reg_show(devdata, buf);
1116 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1121 static ssize_t codec_reg_write_file(struct file *file,
1122 const char __user *user_buf, size_t count, loff_t *ppos)
1127 unsigned long reg, value;
1129 struct snd_soc_codec *codec = file->private_data;
1131 buf_size = min(count, (sizeof(buf)-1));
1132 if (copy_from_user(buf, user_buf, buf_size))
1136 if (codec->reg_cache_step)
1137 step = codec->reg_cache_step;
1139 while (*start == ' ')
1141 reg = simple_strtoul(start, &start, 16);
1142 if ((reg >= codec->reg_cache_size) || (reg % step))
1144 while (*start == ' ')
1146 if (strict_strtoul(start, 16, &value))
1148 codec->write(codec, reg, value);
1152 static const struct file_operations codec_reg_fops = {
1153 .open = codec_reg_open_file,
1154 .read = codec_reg_read_file,
1155 .write = codec_reg_write_file,
1158 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
1160 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
1161 debugfs_root, codec,
1163 if (!codec->debugfs_reg)
1165 "ASoC: Failed to create codec register debugfs file\n");
1167 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
1170 if (!codec->debugfs_pop_time)
1172 "Failed to create pop time debugfs file\n");
1175 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
1177 debugfs_remove(codec->debugfs_pop_time);
1178 debugfs_remove(codec->debugfs_reg);
1183 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
1187 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
1193 * snd_soc_new_ac97_codec - initailise AC97 device
1194 * @codec: audio codec
1195 * @ops: AC97 bus operations
1196 * @num: AC97 codec number
1198 * Initialises AC97 codec resources for use by ad-hoc devices only.
1200 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1201 struct snd_ac97_bus_ops *ops, int num)
1203 mutex_lock(&codec->mutex);
1205 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1206 if (codec->ac97 == NULL) {
1207 mutex_unlock(&codec->mutex);
1211 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1212 if (codec->ac97->bus == NULL) {
1215 mutex_unlock(&codec->mutex);
1219 codec->ac97->bus->ops = ops;
1220 codec->ac97->num = num;
1221 mutex_unlock(&codec->mutex);
1224 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1227 * snd_soc_free_ac97_codec - free AC97 codec device
1228 * @codec: audio codec
1230 * Frees AC97 codec device resources.
1232 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1234 mutex_lock(&codec->mutex);
1235 kfree(codec->ac97->bus);
1238 mutex_unlock(&codec->mutex);
1240 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1243 * snd_soc_update_bits - update codec register bits
1244 * @codec: audio codec
1245 * @reg: codec register
1246 * @mask: register mask
1249 * Writes new register value.
1251 * Returns 1 for change else 0.
1253 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1254 unsigned short mask, unsigned short value)
1257 unsigned short old, new;
1259 mutex_lock(&io_mutex);
1260 old = snd_soc_read(codec, reg);
1261 new = (old & ~mask) | value;
1262 change = old != new;
1264 snd_soc_write(codec, reg, new);
1266 mutex_unlock(&io_mutex);
1269 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1272 * snd_soc_test_bits - test register for change
1273 * @codec: audio codec
1274 * @reg: codec register
1275 * @mask: register mask
1278 * Tests a register with a new value and checks if the new value is
1279 * different from the old value.
1281 * Returns 1 for change else 0.
1283 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1284 unsigned short mask, unsigned short value)
1287 unsigned short old, new;
1289 mutex_lock(&io_mutex);
1290 old = snd_soc_read(codec, reg);
1291 new = (old & ~mask) | value;
1292 change = old != new;
1293 mutex_unlock(&io_mutex);
1297 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1300 * snd_soc_new_pcms - create new sound card and pcms
1301 * @socdev: the SoC audio device
1303 * Create a new sound card based upon the codec and interface pcms.
1305 * Returns 0 for success, else error.
1307 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1309 struct snd_soc_codec *codec = socdev->codec;
1310 struct snd_soc_card *card = socdev->card;
1313 mutex_lock(&codec->mutex);
1315 /* register a sound card */
1316 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1318 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1320 mutex_unlock(&codec->mutex);
1324 codec->card->dev = socdev->dev;
1325 codec->card->private_data = codec;
1326 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1328 /* create the pcms */
1329 for (i = 0; i < card->num_links; i++) {
1330 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1332 printk(KERN_ERR "asoc: can't create pcm %s\n",
1333 card->dai_link[i].stream_name);
1334 mutex_unlock(&codec->mutex);
1339 mutex_unlock(&codec->mutex);
1342 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1345 * snd_soc_init_card - register sound card
1346 * @socdev: the SoC audio device
1348 * Register a SoC sound card. Also registers an AC97 device if the
1349 * codec is AC97 for ad hoc devices.
1351 * Returns 0 for success, else error.
1353 int snd_soc_init_card(struct snd_soc_device *socdev)
1355 struct snd_soc_codec *codec = socdev->codec;
1356 struct snd_soc_card *card = socdev->card;
1357 int ret = 0, i, ac97 = 0, err = 0;
1359 for (i = 0; i < card->num_links; i++) {
1360 if (card->dai_link[i].init) {
1361 err = card->dai_link[i].init(codec);
1363 printk(KERN_ERR "asoc: failed to init %s\n",
1364 card->dai_link[i].stream_name);
1368 if (card->dai_link[i].codec_dai->ac97_control)
1371 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1373 snprintf(codec->card->longname, sizeof(codec->card->longname),
1374 "%s (%s)", card->name, codec->name);
1376 ret = snd_card_register(codec->card);
1378 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1383 mutex_lock(&codec->mutex);
1384 #ifdef CONFIG_SND_SOC_AC97_BUS
1386 ret = soc_ac97_dev_register(codec);
1388 printk(KERN_ERR "asoc: AC97 device register failed\n");
1389 snd_card_free(codec->card);
1390 mutex_unlock(&codec->mutex);
1396 err = snd_soc_dapm_sys_add(socdev->dev);
1398 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1400 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1402 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1404 soc_init_codec_debugfs(socdev->codec);
1405 mutex_unlock(&codec->mutex);
1410 EXPORT_SYMBOL_GPL(snd_soc_init_card);
1413 * snd_soc_free_pcms - free sound card and pcms
1414 * @socdev: the SoC audio device
1416 * Frees sound card and pcms associated with the socdev.
1417 * Also unregister the codec if it is an AC97 device.
1419 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1421 struct snd_soc_codec *codec = socdev->codec;
1422 #ifdef CONFIG_SND_SOC_AC97_BUS
1423 struct snd_soc_dai *codec_dai;
1427 mutex_lock(&codec->mutex);
1428 soc_cleanup_codec_debugfs(socdev->codec);
1429 #ifdef CONFIG_SND_SOC_AC97_BUS
1430 for (i = 0; i < codec->num_dai; i++) {
1431 codec_dai = &codec->dai[i];
1432 if (codec_dai->ac97_control && codec->ac97) {
1433 soc_ac97_dev_unregister(codec);
1441 snd_card_free(codec->card);
1442 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1443 mutex_unlock(&codec->mutex);
1445 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1448 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1449 * @substream: the pcm substream
1450 * @hw: the hardware parameters
1452 * Sets the substream runtime hardware parameters.
1454 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1455 const struct snd_pcm_hardware *hw)
1457 struct snd_pcm_runtime *runtime = substream->runtime;
1458 runtime->hw.info = hw->info;
1459 runtime->hw.formats = hw->formats;
1460 runtime->hw.period_bytes_min = hw->period_bytes_min;
1461 runtime->hw.period_bytes_max = hw->period_bytes_max;
1462 runtime->hw.periods_min = hw->periods_min;
1463 runtime->hw.periods_max = hw->periods_max;
1464 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1465 runtime->hw.fifo_size = hw->fifo_size;
1468 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1471 * snd_soc_cnew - create new control
1472 * @_template: control template
1473 * @data: control private data
1474 * @lnng_name: control long name
1476 * Create a new mixer control from a template control.
1478 * Returns 0 for success, else error.
1480 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1481 void *data, char *long_name)
1483 struct snd_kcontrol_new template;
1485 memcpy(&template, _template, sizeof(template));
1487 template.name = long_name;
1490 return snd_ctl_new1(&template, data);
1492 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1495 * snd_soc_info_enum_double - enumerated double mixer info callback
1496 * @kcontrol: mixer control
1497 * @uinfo: control element information
1499 * Callback to provide information about a double enumerated
1502 * Returns 0 for success.
1504 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1505 struct snd_ctl_elem_info *uinfo)
1507 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1509 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1510 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1511 uinfo->value.enumerated.items = e->max;
1513 if (uinfo->value.enumerated.item > e->max - 1)
1514 uinfo->value.enumerated.item = e->max - 1;
1515 strcpy(uinfo->value.enumerated.name,
1516 e->texts[uinfo->value.enumerated.item]);
1519 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1522 * snd_soc_get_enum_double - enumerated double mixer get callback
1523 * @kcontrol: mixer control
1524 * @uinfo: control element information
1526 * Callback to get the value of a double enumerated mixer.
1528 * Returns 0 for success.
1530 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1531 struct snd_ctl_elem_value *ucontrol)
1533 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1534 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1535 unsigned short val, bitmask;
1537 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1539 val = snd_soc_read(codec, e->reg);
1540 ucontrol->value.enumerated.item[0]
1541 = (val >> e->shift_l) & (bitmask - 1);
1542 if (e->shift_l != e->shift_r)
1543 ucontrol->value.enumerated.item[1] =
1544 (val >> e->shift_r) & (bitmask - 1);
1548 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1551 * snd_soc_put_enum_double - enumerated double mixer put callback
1552 * @kcontrol: mixer control
1553 * @uinfo: control element information
1555 * Callback to set the value of a double enumerated mixer.
1557 * Returns 0 for success.
1559 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1562 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1563 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1565 unsigned short mask, bitmask;
1567 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1569 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1571 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1572 mask = (bitmask - 1) << e->shift_l;
1573 if (e->shift_l != e->shift_r) {
1574 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1576 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1577 mask |= (bitmask - 1) << e->shift_r;
1580 return snd_soc_update_bits(codec, e->reg, mask, val);
1582 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1585 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1586 * @kcontrol: mixer control
1587 * @uinfo: control element information
1589 * Callback to provide information about an external enumerated
1592 * Returns 0 for success.
1594 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_info *uinfo)
1597 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1599 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1601 uinfo->value.enumerated.items = e->max;
1603 if (uinfo->value.enumerated.item > e->max - 1)
1604 uinfo->value.enumerated.item = e->max - 1;
1605 strcpy(uinfo->value.enumerated.name,
1606 e->texts[uinfo->value.enumerated.item]);
1609 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1612 * snd_soc_info_volsw_ext - external single mixer info callback
1613 * @kcontrol: mixer control
1614 * @uinfo: control element information
1616 * Callback to provide information about a single external mixer control.
1618 * Returns 0 for success.
1620 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1621 struct snd_ctl_elem_info *uinfo)
1623 int max = kcontrol->private_value;
1626 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1631 uinfo->value.integer.min = 0;
1632 uinfo->value.integer.max = max;
1635 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1638 * snd_soc_info_volsw - single mixer info callback
1639 * @kcontrol: mixer control
1640 * @uinfo: control element information
1642 * Callback to provide information about a single mixer control.
1644 * Returns 0 for success.
1646 int snd_soc_info_volsw(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;
1652 unsigned int shift = mc->shift;
1653 unsigned int rshift = mc->rshift;
1656 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1658 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1660 uinfo->count = shift == rshift ? 1 : 2;
1661 uinfo->value.integer.min = 0;
1662 uinfo->value.integer.max = max;
1665 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1668 * snd_soc_get_volsw - single mixer get callback
1669 * @kcontrol: mixer control
1670 * @uinfo: control element information
1672 * Callback to get the value of a single mixer control.
1674 * Returns 0 for success.
1676 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol)
1679 struct soc_mixer_control *mc =
1680 (struct soc_mixer_control *)kcontrol->private_value;
1681 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1682 unsigned int reg = mc->reg;
1683 unsigned int shift = mc->shift;
1684 unsigned int rshift = mc->rshift;
1686 unsigned int mask = (1 << fls(max)) - 1;
1687 unsigned int invert = mc->invert;
1689 ucontrol->value.integer.value[0] =
1690 (snd_soc_read(codec, reg) >> shift) & mask;
1691 if (shift != rshift)
1692 ucontrol->value.integer.value[1] =
1693 (snd_soc_read(codec, reg) >> rshift) & mask;
1695 ucontrol->value.integer.value[0] =
1696 max - ucontrol->value.integer.value[0];
1697 if (shift != rshift)
1698 ucontrol->value.integer.value[1] =
1699 max - ucontrol->value.integer.value[1];
1704 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1707 * snd_soc_put_volsw - single mixer put callback
1708 * @kcontrol: mixer control
1709 * @uinfo: control element information
1711 * Callback to set the value of a single mixer control.
1713 * Returns 0 for success.
1715 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_value *ucontrol)
1718 struct soc_mixer_control *mc =
1719 (struct soc_mixer_control *)kcontrol->private_value;
1720 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1721 unsigned int reg = mc->reg;
1722 unsigned int shift = mc->shift;
1723 unsigned int rshift = mc->rshift;
1725 unsigned int mask = (1 << fls(max)) - 1;
1726 unsigned int invert = mc->invert;
1727 unsigned short val, val2, val_mask;
1729 val = (ucontrol->value.integer.value[0] & mask);
1732 val_mask = mask << shift;
1734 if (shift != rshift) {
1735 val2 = (ucontrol->value.integer.value[1] & mask);
1738 val_mask |= mask << rshift;
1739 val |= val2 << rshift;
1741 return snd_soc_update_bits(codec, reg, val_mask, val);
1743 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1746 * snd_soc_info_volsw_2r - double mixer info callback
1747 * @kcontrol: mixer control
1748 * @uinfo: control element information
1750 * Callback to provide information about a double mixer control that
1751 * spans 2 codec registers.
1753 * Returns 0 for success.
1755 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_info *uinfo)
1758 struct soc_mixer_control *mc =
1759 (struct soc_mixer_control *)kcontrol->private_value;
1763 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1765 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1768 uinfo->value.integer.min = 0;
1769 uinfo->value.integer.max = max;
1772 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1775 * snd_soc_get_volsw_2r - double mixer get callback
1776 * @kcontrol: mixer control
1777 * @uinfo: control element information
1779 * Callback to get the value of a double mixer control that spans 2 registers.
1781 * Returns 0 for success.
1783 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1784 struct snd_ctl_elem_value *ucontrol)
1786 struct soc_mixer_control *mc =
1787 (struct soc_mixer_control *)kcontrol->private_value;
1788 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1789 unsigned int reg = mc->reg;
1790 unsigned int reg2 = mc->rreg;
1791 unsigned int shift = mc->shift;
1793 unsigned int mask = (1<<fls(max))-1;
1794 unsigned int invert = mc->invert;
1796 ucontrol->value.integer.value[0] =
1797 (snd_soc_read(codec, reg) >> shift) & mask;
1798 ucontrol->value.integer.value[1] =
1799 (snd_soc_read(codec, reg2) >> shift) & mask;
1801 ucontrol->value.integer.value[0] =
1802 max - ucontrol->value.integer.value[0];
1803 ucontrol->value.integer.value[1] =
1804 max - ucontrol->value.integer.value[1];
1809 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1812 * snd_soc_put_volsw_2r - double mixer set callback
1813 * @kcontrol: mixer control
1814 * @uinfo: control element information
1816 * Callback to set the value of a double mixer control that spans 2 registers.
1818 * Returns 0 for success.
1820 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1821 struct snd_ctl_elem_value *ucontrol)
1823 struct soc_mixer_control *mc =
1824 (struct soc_mixer_control *)kcontrol->private_value;
1825 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1826 unsigned int reg = mc->reg;
1827 unsigned int reg2 = mc->rreg;
1828 unsigned int shift = mc->shift;
1830 unsigned int mask = (1 << fls(max)) - 1;
1831 unsigned int invert = mc->invert;
1833 unsigned short val, val2, val_mask;
1835 val_mask = mask << shift;
1836 val = (ucontrol->value.integer.value[0] & mask);
1837 val2 = (ucontrol->value.integer.value[1] & mask);
1845 val2 = val2 << shift;
1847 err = snd_soc_update_bits(codec, reg, val_mask, val);
1851 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1854 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1857 * snd_soc_info_volsw_s8 - signed mixer info callback
1858 * @kcontrol: mixer control
1859 * @uinfo: control element information
1861 * Callback to provide information about a signed mixer control.
1863 * Returns 0 for success.
1865 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1866 struct snd_ctl_elem_info *uinfo)
1868 struct soc_mixer_control *mc =
1869 (struct soc_mixer_control *)kcontrol->private_value;
1873 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1875 uinfo->value.integer.min = 0;
1876 uinfo->value.integer.max = max-min;
1879 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1882 * snd_soc_get_volsw_s8 - signed mixer get callback
1883 * @kcontrol: mixer control
1884 * @uinfo: control element information
1886 * Callback to get the value of a signed mixer control.
1888 * Returns 0 for success.
1890 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_value *ucontrol)
1893 struct soc_mixer_control *mc =
1894 (struct soc_mixer_control *)kcontrol->private_value;
1895 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1896 unsigned int reg = mc->reg;
1898 int val = snd_soc_read(codec, reg);
1900 ucontrol->value.integer.value[0] =
1901 ((signed char)(val & 0xff))-min;
1902 ucontrol->value.integer.value[1] =
1903 ((signed char)((val >> 8) & 0xff))-min;
1906 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1909 * snd_soc_put_volsw_sgn - signed mixer put callback
1910 * @kcontrol: mixer control
1911 * @uinfo: control element information
1913 * Callback to set the value of a signed mixer control.
1915 * Returns 0 for success.
1917 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1920 struct soc_mixer_control *mc =
1921 (struct soc_mixer_control *)kcontrol->private_value;
1922 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1923 unsigned int reg = mc->reg;
1927 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1928 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1930 return snd_soc_update_bits(codec, reg, 0xffff, val);
1932 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1935 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1937 * @clk_id: DAI specific clock ID
1938 * @freq: new clock frequency in Hz
1939 * @dir: new clock direction - input/output.
1941 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1943 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1944 unsigned int freq, int dir)
1946 if (dai->ops.set_sysclk)
1947 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
1951 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1954 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1956 * @clk_id: DAI specific clock divider ID
1957 * @div: new clock divisor.
1959 * Configures the clock dividers. This is used to derive the best DAI bit and
1960 * frame clocks from the system or master clock. It's best to set the DAI bit
1961 * and frame clocks as low as possible to save system power.
1963 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1964 int div_id, int div)
1966 if (dai->ops.set_clkdiv)
1967 return dai->ops.set_clkdiv(dai, div_id, div);
1971 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1974 * snd_soc_dai_set_pll - configure DAI PLL.
1976 * @pll_id: DAI specific PLL ID
1977 * @freq_in: PLL input clock frequency in Hz
1978 * @freq_out: requested PLL output clock frequency in Hz
1980 * Configures and enables PLL to generate output clock based on input clock.
1982 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1983 int pll_id, unsigned int freq_in, unsigned int freq_out)
1985 if (dai->ops.set_pll)
1986 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
1990 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1993 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1995 * @fmt: SND_SOC_DAIFMT_ format value.
1997 * Configures the DAI hardware format and clocking.
1999 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2001 if (dai->ops.set_fmt)
2002 return dai->ops.set_fmt(dai, fmt);
2006 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2009 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2011 * @mask: DAI specific mask representing used slots.
2012 * @slots: Number of slots in use.
2014 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2017 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2018 unsigned int mask, int slots)
2020 if (dai->ops.set_sysclk)
2021 return dai->ops.set_tdm_slot(dai, mask, slots);
2025 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2028 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2030 * @tristate: tristate enable
2032 * Tristates the DAI so that others can use it.
2034 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2036 if (dai->ops.set_sysclk)
2037 return dai->ops.set_tristate(dai, tristate);
2041 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2044 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2046 * @mute: mute enable
2048 * Mutes the DAI DAC.
2050 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2052 if (dai->ops.digital_mute)
2053 return dai->ops.digital_mute(dai, mute);
2057 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2060 * snd_soc_register_card - Register a card with the ASoC core
2062 * @param card Card to register
2064 * Note that currently this is an internal only function: it will be
2065 * exposed to machine drivers after further backporting of ASoC v2
2066 * registration APIs.
2068 static int snd_soc_register_card(struct snd_soc_card *card)
2070 if (!card->name || !card->dev)
2073 INIT_LIST_HEAD(&card->list);
2074 card->instantiated = 0;
2076 mutex_lock(&client_mutex);
2077 list_add(&card->list, &card_list);
2078 snd_soc_instantiate_cards();
2079 mutex_unlock(&client_mutex);
2081 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2087 * snd_soc_unregister_card - Unregister a card with the ASoC core
2089 * @param card Card to unregister
2091 * Note that currently this is an internal only function: it will be
2092 * exposed to machine drivers after further backporting of ASoC v2
2093 * registration APIs.
2095 static int snd_soc_unregister_card(struct snd_soc_card *card)
2097 mutex_lock(&client_mutex);
2098 list_del(&card->list);
2099 mutex_unlock(&client_mutex);
2101 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2107 * snd_soc_register_dai - Register a DAI with the ASoC core
2109 * @param dai DAI to register
2111 int snd_soc_register_dai(struct snd_soc_dai *dai)
2116 /* The device should become mandatory over time */
2118 printk(KERN_WARNING "No device for DAI %s\n", dai->name);
2120 INIT_LIST_HEAD(&dai->list);
2122 mutex_lock(&client_mutex);
2123 list_add(&dai->list, &dai_list);
2124 snd_soc_instantiate_cards();
2125 mutex_unlock(&client_mutex);
2127 pr_debug("Registered DAI '%s'\n", dai->name);
2131 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2134 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2136 * @param dai DAI to unregister
2138 void snd_soc_unregister_dai(struct snd_soc_dai *dai)
2140 mutex_lock(&client_mutex);
2141 list_del(&dai->list);
2142 mutex_unlock(&client_mutex);
2144 pr_debug("Unregistered DAI '%s'\n", dai->name);
2146 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2149 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2151 * @param dai Array of DAIs to register
2152 * @param count Number of DAIs
2154 int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
2158 for (i = 0; i < count; i++) {
2159 ret = snd_soc_register_dai(&dai[i]);
2167 for (i--; i >= 0; i--)
2168 snd_soc_unregister_dai(&dai[i]);
2172 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2175 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2177 * @param dai Array of DAIs to unregister
2178 * @param count Number of DAIs
2180 void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
2184 for (i = 0; i < count; i++)
2185 snd_soc_unregister_dai(&dai[i]);
2187 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2190 * snd_soc_register_platform - Register a platform with the ASoC core
2192 * @param platform platform to register
2194 int snd_soc_register_platform(struct snd_soc_platform *platform)
2196 if (!platform->name)
2199 INIT_LIST_HEAD(&platform->list);
2201 mutex_lock(&client_mutex);
2202 list_add(&platform->list, &platform_list);
2203 snd_soc_instantiate_cards();
2204 mutex_unlock(&client_mutex);
2206 pr_debug("Registered platform '%s'\n", platform->name);
2210 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
2213 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
2215 * @param platform platform to unregister
2217 void snd_soc_unregister_platform(struct snd_soc_platform *platform)
2219 mutex_lock(&client_mutex);
2220 list_del(&platform->list);
2221 mutex_unlock(&client_mutex);
2223 pr_debug("Unregistered platform '%s'\n", platform->name);
2225 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
2227 static int __devinit snd_soc_init(void)
2229 #ifdef CONFIG_DEBUG_FS
2230 debugfs_root = debugfs_create_dir("asoc", NULL);
2231 if (IS_ERR(debugfs_root) || !debugfs_root) {
2233 "ASoC: Failed to create debugfs directory\n");
2234 debugfs_root = NULL;
2238 return platform_driver_register(&soc_driver);
2241 static void __exit snd_soc_exit(void)
2243 #ifdef CONFIG_DEBUG_FS
2244 debugfs_remove_recursive(debugfs_root);
2246 platform_driver_unregister(&soc_driver);
2249 module_init(snd_soc_init);
2250 module_exit(snd_soc_exit);
2252 /* Module information */
2253 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
2254 MODULE_DESCRIPTION("ALSA SoC Core");
2255 MODULE_LICENSE("GPL");
2256 MODULE_ALIAS("platform:soc-audio");