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;
103 static inline const char *get_dai_name(int type)
106 case SND_SOC_DAI_AC97_BUS:
107 case SND_SOC_DAI_AC97:
109 case SND_SOC_DAI_I2S:
111 case SND_SOC_DAI_PCM:
118 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
119 * then initialized and any private data can be allocated. This also calls
120 * startup for the cpu DAI, platform, machine and codec DAI.
122 static int soc_pcm_open(struct snd_pcm_substream *substream)
124 struct snd_soc_pcm_runtime *rtd = substream->private_data;
125 struct snd_soc_device *socdev = rtd->socdev;
126 struct snd_pcm_runtime *runtime = substream->runtime;
127 struct snd_soc_dai_link *machine = rtd->dai;
128 struct snd_soc_platform *platform = socdev->platform;
129 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
130 struct snd_soc_dai *codec_dai = machine->codec_dai;
133 mutex_lock(&pcm_mutex);
135 /* startup the audio subsystem */
136 if (cpu_dai->ops.startup) {
137 ret = cpu_dai->ops.startup(substream);
139 printk(KERN_ERR "asoc: can't open interface %s\n",
145 if (platform->pcm_ops->open) {
146 ret = platform->pcm_ops->open(substream);
148 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
153 if (codec_dai->ops.startup) {
154 ret = codec_dai->ops.startup(substream);
156 printk(KERN_ERR "asoc: can't open codec %s\n",
162 if (machine->ops && machine->ops->startup) {
163 ret = machine->ops->startup(substream);
165 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
170 /* Check that the codec and cpu DAI's are compatible */
171 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
172 runtime->hw.rate_min =
173 max(codec_dai->playback.rate_min,
174 cpu_dai->playback.rate_min);
175 runtime->hw.rate_max =
176 min(codec_dai->playback.rate_max,
177 cpu_dai->playback.rate_max);
178 runtime->hw.channels_min =
179 max(codec_dai->playback.channels_min,
180 cpu_dai->playback.channels_min);
181 runtime->hw.channels_max =
182 min(codec_dai->playback.channels_max,
183 cpu_dai->playback.channels_max);
184 runtime->hw.formats =
185 codec_dai->playback.formats & cpu_dai->playback.formats;
187 codec_dai->playback.rates & cpu_dai->playback.rates;
189 runtime->hw.rate_min =
190 max(codec_dai->capture.rate_min,
191 cpu_dai->capture.rate_min);
192 runtime->hw.rate_max =
193 min(codec_dai->capture.rate_max,
194 cpu_dai->capture.rate_max);
195 runtime->hw.channels_min =
196 max(codec_dai->capture.channels_min,
197 cpu_dai->capture.channels_min);
198 runtime->hw.channels_max =
199 min(codec_dai->capture.channels_max,
200 cpu_dai->capture.channels_max);
201 runtime->hw.formats =
202 codec_dai->capture.formats & cpu_dai->capture.formats;
204 codec_dai->capture.rates & cpu_dai->capture.rates;
207 snd_pcm_limit_hw_rates(runtime);
208 if (!runtime->hw.rates) {
209 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
210 codec_dai->name, cpu_dai->name);
213 if (!runtime->hw.formats) {
214 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
215 codec_dai->name, cpu_dai->name);
218 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
219 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
220 codec_dai->name, cpu_dai->name);
224 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
225 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
226 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
227 runtime->hw.channels_max);
228 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
229 runtime->hw.rate_max);
231 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
232 cpu_dai->playback.active = codec_dai->playback.active = 1;
234 cpu_dai->capture.active = codec_dai->capture.active = 1;
235 cpu_dai->active = codec_dai->active = 1;
236 cpu_dai->runtime = runtime;
237 socdev->codec->active++;
238 mutex_unlock(&pcm_mutex);
242 if (machine->ops && machine->ops->shutdown)
243 machine->ops->shutdown(substream);
246 if (platform->pcm_ops->close)
247 platform->pcm_ops->close(substream);
250 if (cpu_dai->ops.shutdown)
251 cpu_dai->ops.shutdown(substream);
253 mutex_unlock(&pcm_mutex);
258 * Power down the audio subsystem pmdown_time msecs after close is called.
259 * This is to ensure there are no pops or clicks in between any music tracks
260 * due to DAPM power cycling.
262 static void close_delayed_work(struct work_struct *work)
264 struct snd_soc_device *socdev =
265 container_of(work, struct snd_soc_device, delayed_work.work);
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_dai_link *machine = rtd->dai;
317 struct snd_soc_platform *platform = socdev->platform;
318 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
319 struct snd_soc_dai *codec_dai = machine->codec_dai;
320 struct snd_soc_codec *codec = socdev->codec;
322 mutex_lock(&pcm_mutex);
324 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
325 cpu_dai->playback.active = codec_dai->playback.active = 0;
327 cpu_dai->capture.active = codec_dai->capture.active = 0;
329 if (codec_dai->playback.active == 0 &&
330 codec_dai->capture.active == 0) {
331 cpu_dai->active = codec_dai->active = 0;
335 /* Muting the DAC suppresses artifacts caused during digital
336 * shutdown, for example from stopping clocks.
338 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
339 snd_soc_dai_digital_mute(codec_dai, 1);
341 if (cpu_dai->ops.shutdown)
342 cpu_dai->ops.shutdown(substream);
344 if (codec_dai->ops.shutdown)
345 codec_dai->ops.shutdown(substream);
347 if (machine->ops && machine->ops->shutdown)
348 machine->ops->shutdown(substream);
350 if (platform->pcm_ops->close)
351 platform->pcm_ops->close(substream);
352 cpu_dai->runtime = NULL;
354 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
355 /* start delayed pop wq here for playback streams */
356 codec_dai->pop_wait = 1;
357 schedule_delayed_work(&socdev->delayed_work,
358 msecs_to_jiffies(pmdown_time));
360 /* capture streams can be powered down now */
361 snd_soc_dapm_stream_event(codec,
362 codec_dai->capture.stream_name,
363 SND_SOC_DAPM_STREAM_STOP);
365 if (codec->active == 0 && codec_dai->pop_wait == 0)
366 snd_soc_dapm_set_bias_level(socdev,
367 SND_SOC_BIAS_STANDBY);
370 mutex_unlock(&pcm_mutex);
375 * Called by ALSA when the PCM substream is prepared, can set format, sample
376 * rate, etc. This function is non atomic and can be called multiple times,
377 * it can refer to the runtime info.
379 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
381 struct snd_soc_pcm_runtime *rtd = substream->private_data;
382 struct snd_soc_device *socdev = rtd->socdev;
383 struct snd_soc_dai_link *machine = rtd->dai;
384 struct snd_soc_platform *platform = socdev->platform;
385 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
386 struct snd_soc_dai *codec_dai = machine->codec_dai;
387 struct snd_soc_codec *codec = socdev->codec;
390 mutex_lock(&pcm_mutex);
392 if (machine->ops && machine->ops->prepare) {
393 ret = machine->ops->prepare(substream);
395 printk(KERN_ERR "asoc: machine prepare error\n");
400 if (platform->pcm_ops->prepare) {
401 ret = platform->pcm_ops->prepare(substream);
403 printk(KERN_ERR "asoc: platform prepare error\n");
408 if (codec_dai->ops.prepare) {
409 ret = codec_dai->ops.prepare(substream);
411 printk(KERN_ERR "asoc: codec DAI prepare error\n");
416 if (cpu_dai->ops.prepare) {
417 ret = cpu_dai->ops.prepare(substream);
419 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
424 /* cancel any delayed stream shutdown that is pending */
425 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
426 codec_dai->pop_wait) {
427 codec_dai->pop_wait = 0;
428 cancel_delayed_work(&socdev->delayed_work);
431 /* do we need to power up codec */
432 if (codec->bias_level != SND_SOC_BIAS_ON) {
433 snd_soc_dapm_set_bias_level(socdev,
434 SND_SOC_BIAS_PREPARE);
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_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
446 snd_soc_dai_digital_mute(codec_dai, 0);
449 /* codec already powered - power on widgets */
450 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
451 snd_soc_dapm_stream_event(codec,
452 codec_dai->playback.stream_name,
453 SND_SOC_DAPM_STREAM_START);
455 snd_soc_dapm_stream_event(codec,
456 codec_dai->capture.stream_name,
457 SND_SOC_DAPM_STREAM_START);
459 snd_soc_dai_digital_mute(codec_dai, 0);
463 mutex_unlock(&pcm_mutex);
468 * Called by ALSA when the hardware params are set by application. This
469 * function can also be called multiple times and can allocate buffers
470 * (using snd_pcm_lib_* ). It's non-atomic.
472 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
473 struct snd_pcm_hw_params *params)
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_soc_device *socdev = rtd->socdev;
477 struct snd_soc_dai_link *machine = rtd->dai;
478 struct snd_soc_platform *platform = socdev->platform;
479 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
480 struct snd_soc_dai *codec_dai = machine->codec_dai;
483 mutex_lock(&pcm_mutex);
485 if (machine->ops && machine->ops->hw_params) {
486 ret = machine->ops->hw_params(substream, params);
488 printk(KERN_ERR "asoc: machine hw_params failed\n");
493 if (codec_dai->ops.hw_params) {
494 ret = codec_dai->ops.hw_params(substream, params);
496 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
502 if (cpu_dai->ops.hw_params) {
503 ret = cpu_dai->ops.hw_params(substream, params);
505 printk(KERN_ERR "asoc: interface %s hw params failed\n",
511 if (platform->pcm_ops->hw_params) {
512 ret = platform->pcm_ops->hw_params(substream, params);
514 printk(KERN_ERR "asoc: platform %s hw params failed\n",
521 mutex_unlock(&pcm_mutex);
525 if (cpu_dai->ops.hw_free)
526 cpu_dai->ops.hw_free(substream);
529 if (codec_dai->ops.hw_free)
530 codec_dai->ops.hw_free(substream);
533 if (machine->ops && machine->ops->hw_free)
534 machine->ops->hw_free(substream);
536 mutex_unlock(&pcm_mutex);
541 * Free's resources allocated by hw_params, can be called multiple times
543 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
545 struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 struct snd_soc_device *socdev = rtd->socdev;
547 struct snd_soc_dai_link *machine = rtd->dai;
548 struct snd_soc_platform *platform = socdev->platform;
549 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
550 struct snd_soc_dai *codec_dai = machine->codec_dai;
551 struct snd_soc_codec *codec = socdev->codec;
553 mutex_lock(&pcm_mutex);
555 /* apply codec digital mute */
557 snd_soc_dai_digital_mute(codec_dai, 1);
559 /* free any machine hw params */
560 if (machine->ops && machine->ops->hw_free)
561 machine->ops->hw_free(substream);
563 /* free any DMA resources */
564 if (platform->pcm_ops->hw_free)
565 platform->pcm_ops->hw_free(substream);
567 /* now free hw params for the DAI's */
568 if (codec_dai->ops.hw_free)
569 codec_dai->ops.hw_free(substream);
571 if (cpu_dai->ops.hw_free)
572 cpu_dai->ops.hw_free(substream);
574 mutex_unlock(&pcm_mutex);
578 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
580 struct snd_soc_pcm_runtime *rtd = substream->private_data;
581 struct snd_soc_device *socdev = rtd->socdev;
582 struct snd_soc_dai_link *machine = rtd->dai;
583 struct snd_soc_platform *platform = socdev->platform;
584 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
585 struct snd_soc_dai *codec_dai = machine->codec_dai;
588 if (codec_dai->ops.trigger) {
589 ret = codec_dai->ops.trigger(substream, cmd);
594 if (platform->pcm_ops->trigger) {
595 ret = platform->pcm_ops->trigger(substream, cmd);
600 if (cpu_dai->ops.trigger) {
601 ret = cpu_dai->ops.trigger(substream, cmd);
608 /* ASoC PCM operations */
609 static struct snd_pcm_ops soc_pcm_ops = {
610 .open = soc_pcm_open,
611 .close = soc_codec_close,
612 .hw_params = soc_pcm_hw_params,
613 .hw_free = soc_pcm_hw_free,
614 .prepare = soc_pcm_prepare,
615 .trigger = soc_pcm_trigger,
619 /* powers down audio subsystem for suspend */
620 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
622 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
623 struct snd_soc_card *card = socdev->card;
624 struct snd_soc_platform *platform = socdev->platform;
625 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
626 struct snd_soc_codec *codec = socdev->codec;
629 /* Due to the resume being scheduled into a workqueue we could
630 * suspend before that's finished - wait for it to complete.
632 snd_power_lock(codec->card);
633 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
634 snd_power_unlock(codec->card);
636 /* we're going to block userspace touching us until resume completes */
637 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
639 /* mute any active DAC's */
640 for (i = 0; i < machine->num_links; i++) {
641 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
642 if (dai->dai_ops.digital_mute && dai->playback.active)
643 dai->dai_ops.digital_mute(dai, 1);
646 /* suspend all pcms */
647 for (i = 0; i < card->num_links; i++)
648 snd_pcm_suspend_all(card->dai_link[i].pcm);
650 if (card->suspend_pre)
651 card->suspend_pre(pdev, state);
653 for (i = 0; i < card->num_links; i++) {
654 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
655 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
656 cpu_dai->suspend(pdev, cpu_dai);
657 if (platform->suspend)
658 platform->suspend(pdev, cpu_dai);
661 /* close any waiting streams and save state */
662 run_delayed_work(&socdev->delayed_work);
663 codec->suspend_bias_level = codec->bias_level;
665 for (i = 0; i < codec->num_dai; i++) {
666 char *stream = codec->dai[i].playback.stream_name;
668 snd_soc_dapm_stream_event(codec, stream,
669 SND_SOC_DAPM_STREAM_SUSPEND);
670 stream = codec->dai[i].capture.stream_name;
672 snd_soc_dapm_stream_event(codec, stream,
673 SND_SOC_DAPM_STREAM_SUSPEND);
676 if (codec_dev->suspend)
677 codec_dev->suspend(pdev, state);
679 for (i = 0; i < card->num_links; i++) {
680 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
681 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
682 cpu_dai->suspend(pdev, cpu_dai);
685 if (card->suspend_post)
686 card->suspend_post(pdev, state);
691 /* deferred resume work, so resume can complete before we finished
692 * setting our codec back up, which can be very slow on I2C
694 static void soc_resume_deferred(struct work_struct *work)
696 struct snd_soc_device *socdev = container_of(work,
697 struct snd_soc_device,
698 deferred_resume_work);
699 struct snd_soc_card *card = socdev->card;
700 struct snd_soc_platform *platform = socdev->platform;
701 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
702 struct snd_soc_codec *codec = socdev->codec;
703 struct platform_device *pdev = to_platform_device(socdev->dev);
706 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
707 * so userspace apps are blocked from touching us
710 dev_info(socdev->dev, "starting resume work\n");
712 if (card->resume_pre)
713 card->resume_pre(pdev);
715 for (i = 0; i < card->num_links; i++) {
716 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
717 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
718 cpu_dai->resume(pdev, cpu_dai);
721 if (codec_dev->resume)
722 codec_dev->resume(pdev);
724 for (i = 0; i < codec->num_dai; i++) {
725 char *stream = codec->dai[i].playback.stream_name;
727 snd_soc_dapm_stream_event(codec, stream,
728 SND_SOC_DAPM_STREAM_RESUME);
729 stream = codec->dai[i].capture.stream_name;
731 snd_soc_dapm_stream_event(codec, stream,
732 SND_SOC_DAPM_STREAM_RESUME);
735 /* unmute any active DACs */
736 for (i = 0; i < machine->num_links; i++) {
737 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
738 if (dai->dai_ops.digital_mute && dai->playback.active)
739 dai->dai_ops.digital_mute(dai, 0);
742 for (i = 0; i < card->num_links; i++) {
743 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
744 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
745 cpu_dai->resume(pdev, cpu_dai);
746 if (platform->resume)
747 platform->resume(pdev, cpu_dai);
750 if (card->resume_post)
751 card->resume_post(pdev);
753 dev_info(socdev->dev, "resume work completed\n");
755 /* userspace can access us now we are back as we were before */
756 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
759 /* powers up audio subsystem after a suspend */
760 static int soc_resume(struct platform_device *pdev)
762 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
764 dev_info(socdev->dev, "scheduling resume work\n");
766 if (!schedule_work(&socdev->deferred_resume_work))
767 dev_err(socdev->dev, "work item may be lost\n");
773 #define soc_suspend NULL
774 #define soc_resume NULL
777 /* probes a new socdev */
778 static int soc_probe(struct platform_device *pdev)
781 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
782 struct snd_soc_card *card = socdev->card;
783 struct snd_soc_platform *platform = socdev->platform;
784 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
787 ret = card->probe(pdev);
792 for (i = 0; i < card->num_links; i++) {
793 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
794 if (cpu_dai->probe) {
795 ret = cpu_dai->probe(pdev, cpu_dai);
801 if (codec_dev->probe) {
802 ret = codec_dev->probe(pdev);
807 if (platform->probe) {
808 ret = platform->probe(pdev);
813 /* DAPM stream work */
814 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
816 /* deferred resume work */
817 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
823 if (codec_dev->remove)
824 codec_dev->remove(pdev);
827 for (i--; i >= 0; i--) {
828 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
830 cpu_dai->remove(pdev, cpu_dai);
839 /* removes a socdev */
840 static int soc_remove(struct platform_device *pdev)
843 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
844 struct snd_soc_card *card = socdev->card;
845 struct snd_soc_platform *platform = socdev->platform;
846 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
848 run_delayed_work(&socdev->delayed_work);
850 if (platform->remove)
851 platform->remove(pdev);
853 if (codec_dev->remove)
854 codec_dev->remove(pdev);
856 for (i = 0; i < card->num_links; i++) {
857 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
859 cpu_dai->remove(pdev, cpu_dai);
868 /* ASoC platform driver */
869 static struct platform_driver soc_driver = {
872 .owner = THIS_MODULE,
875 .remove = soc_remove,
876 .suspend = soc_suspend,
877 .resume = soc_resume,
880 /* create a new pcm */
881 static int soc_new_pcm(struct snd_soc_device *socdev,
882 struct snd_soc_dai_link *dai_link, int num)
884 struct snd_soc_codec *codec = socdev->codec;
885 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
886 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
887 struct snd_soc_pcm_runtime *rtd;
890 int ret = 0, playback = 0, capture = 0;
892 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
897 rtd->socdev = socdev;
898 codec_dai->codec = socdev->codec;
900 /* check client and interface hw capabilities */
901 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
902 get_dai_name(cpu_dai->type), num);
904 if (codec_dai->playback.channels_min)
906 if (codec_dai->capture.channels_min)
909 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
912 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
919 pcm->private_data = rtd;
920 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
921 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
922 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
923 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
924 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
925 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
926 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
929 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
932 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
934 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
936 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
941 pcm->private_free = socdev->platform->pcm_free;
942 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
947 /* codec register dump */
948 static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
950 struct snd_soc_codec *codec = devdata->codec;
951 int i, step = 1, count = 0;
953 if (!codec->reg_cache_size)
956 if (codec->reg_cache_step)
957 step = codec->reg_cache_step;
959 count += sprintf(buf, "%s registers\n", codec->name);
960 for (i = 0; i < codec->reg_cache_size; i += step) {
961 count += sprintf(buf + count, "%2x: ", i);
962 if (count >= PAGE_SIZE - 1)
965 if (codec->display_register)
966 count += codec->display_register(codec, buf + count,
967 PAGE_SIZE - count, i);
969 count += snprintf(buf + count, PAGE_SIZE - count,
970 "%4x", codec->read(codec, i));
972 if (count >= PAGE_SIZE - 1)
975 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
976 if (count >= PAGE_SIZE - 1)
980 /* Truncate count; min() would cause a warning */
981 if (count >= PAGE_SIZE)
982 count = PAGE_SIZE - 1;
986 static ssize_t codec_reg_show(struct device *dev,
987 struct device_attribute *attr, char *buf)
989 struct snd_soc_device *devdata = dev_get_drvdata(dev);
990 return soc_codec_reg_show(devdata, buf);
993 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
995 #ifdef CONFIG_DEBUG_FS
996 static int codec_reg_open_file(struct inode *inode, struct file *file)
998 file->private_data = inode->i_private;
1002 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1003 size_t count, loff_t *ppos)
1006 struct snd_soc_device *devdata = file->private_data;
1007 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1010 ret = soc_codec_reg_show(devdata, buf);
1012 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1017 static ssize_t codec_reg_write_file(struct file *file,
1018 const char __user *user_buf, size_t count, loff_t *ppos)
1023 unsigned long reg, value;
1025 struct snd_soc_device *devdata = file->private_data;
1026 struct snd_soc_codec *codec = devdata->codec;
1028 buf_size = min(count, (sizeof(buf)-1));
1029 if (copy_from_user(buf, user_buf, buf_size))
1033 if (codec->reg_cache_step)
1034 step = codec->reg_cache_step;
1036 while (*start == ' ')
1038 reg = simple_strtoul(start, &start, 16);
1039 if ((reg >= codec->reg_cache_size) || (reg % step))
1041 while (*start == ' ')
1043 if (strict_strtoul(start, 16, &value))
1045 codec->write(codec, reg, value);
1049 static const struct file_operations codec_reg_fops = {
1050 .open = codec_reg_open_file,
1051 .read = codec_reg_read_file,
1052 .write = codec_reg_write_file,
1055 static void soc_init_debugfs(struct snd_soc_device *socdev)
1057 struct dentry *root, *file;
1058 struct snd_soc_codec *codec = socdev->codec;
1059 root = debugfs_create_dir(dev_name(socdev->dev), NULL);
1060 if (IS_ERR(root) || !root)
1063 file = debugfs_create_file("codec_reg", 0644,
1064 root, socdev, &codec_reg_fops);
1068 file = debugfs_create_u32("dapm_pop_time", 0744,
1069 root, &codec->pop_time);
1072 socdev->debugfs_root = root;
1075 debugfs_remove_recursive(root);
1077 dev_err(socdev->dev, "debugfs is not available\n");
1080 static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1082 debugfs_remove_recursive(socdev->debugfs_root);
1083 socdev->debugfs_root = NULL;
1088 static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1092 static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1098 * snd_soc_new_ac97_codec - initailise AC97 device
1099 * @codec: audio codec
1100 * @ops: AC97 bus operations
1101 * @num: AC97 codec number
1103 * Initialises AC97 codec resources for use by ad-hoc devices only.
1105 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1106 struct snd_ac97_bus_ops *ops, int num)
1108 mutex_lock(&codec->mutex);
1110 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1111 if (codec->ac97 == NULL) {
1112 mutex_unlock(&codec->mutex);
1116 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1117 if (codec->ac97->bus == NULL) {
1120 mutex_unlock(&codec->mutex);
1124 codec->ac97->bus->ops = ops;
1125 codec->ac97->num = num;
1126 mutex_unlock(&codec->mutex);
1129 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1132 * snd_soc_free_ac97_codec - free AC97 codec device
1133 * @codec: audio codec
1135 * Frees AC97 codec device resources.
1137 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1139 mutex_lock(&codec->mutex);
1140 kfree(codec->ac97->bus);
1143 mutex_unlock(&codec->mutex);
1145 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1148 * snd_soc_update_bits - update codec register bits
1149 * @codec: audio codec
1150 * @reg: codec register
1151 * @mask: register mask
1154 * Writes new register value.
1156 * Returns 1 for change else 0.
1158 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1159 unsigned short mask, unsigned short value)
1162 unsigned short old, new;
1164 mutex_lock(&io_mutex);
1165 old = snd_soc_read(codec, reg);
1166 new = (old & ~mask) | value;
1167 change = old != new;
1169 snd_soc_write(codec, reg, new);
1171 mutex_unlock(&io_mutex);
1174 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1177 * snd_soc_test_bits - test register for change
1178 * @codec: audio codec
1179 * @reg: codec register
1180 * @mask: register mask
1183 * Tests a register with a new value and checks if the new value is
1184 * different from the old value.
1186 * Returns 1 for change else 0.
1188 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1189 unsigned short mask, unsigned short value)
1192 unsigned short old, new;
1194 mutex_lock(&io_mutex);
1195 old = snd_soc_read(codec, reg);
1196 new = (old & ~mask) | value;
1197 change = old != new;
1198 mutex_unlock(&io_mutex);
1202 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1205 * snd_soc_new_pcms - create new sound card and pcms
1206 * @socdev: the SoC audio device
1208 * Create a new sound card based upon the codec and interface pcms.
1210 * Returns 0 for success, else error.
1212 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1214 struct snd_soc_codec *codec = socdev->codec;
1215 struct snd_soc_card *card = socdev->card;
1218 mutex_lock(&codec->mutex);
1220 /* register a sound card */
1221 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1223 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1225 mutex_unlock(&codec->mutex);
1229 codec->card->dev = socdev->dev;
1230 codec->card->private_data = codec;
1231 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1233 /* create the pcms */
1234 for (i = 0; i < card->num_links; i++) {
1235 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1237 printk(KERN_ERR "asoc: can't create pcm %s\n",
1238 card->dai_link[i].stream_name);
1239 mutex_unlock(&codec->mutex);
1244 mutex_unlock(&codec->mutex);
1247 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1250 * snd_soc_register_card - register sound card
1251 * @socdev: the SoC audio device
1253 * Register a SoC sound card. Also registers an AC97 device if the
1254 * codec is AC97 for ad hoc devices.
1256 * Returns 0 for success, else error.
1258 int snd_soc_register_card(struct snd_soc_device *socdev)
1260 struct snd_soc_codec *codec = socdev->codec;
1261 struct snd_soc_card *card = socdev->card;
1262 int ret = 0, i, ac97 = 0, err = 0;
1264 for (i = 0; i < card->num_links; i++) {
1265 if (card->dai_link[i].init) {
1266 err = card->dai_link[i].init(codec);
1268 printk(KERN_ERR "asoc: failed to init %s\n",
1269 card->dai_link[i].stream_name);
1273 if (card->dai_link[i].codec_dai->type ==
1274 SND_SOC_DAI_AC97_BUS)
1277 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1279 snprintf(codec->card->longname, sizeof(codec->card->longname),
1280 "%s (%s)", card->name, codec->name);
1282 ret = snd_card_register(codec->card);
1284 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1289 mutex_lock(&codec->mutex);
1290 #ifdef CONFIG_SND_SOC_AC97_BUS
1292 ret = soc_ac97_dev_register(codec);
1294 printk(KERN_ERR "asoc: AC97 device register failed\n");
1295 snd_card_free(codec->card);
1296 mutex_unlock(&codec->mutex);
1302 err = snd_soc_dapm_sys_add(socdev->dev);
1304 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1306 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1308 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1310 soc_init_debugfs(socdev);
1311 mutex_unlock(&codec->mutex);
1316 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1319 * snd_soc_free_pcms - free sound card and pcms
1320 * @socdev: the SoC audio device
1322 * Frees sound card and pcms associated with the socdev.
1323 * Also unregister the codec if it is an AC97 device.
1325 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1327 struct snd_soc_codec *codec = socdev->codec;
1328 #ifdef CONFIG_SND_SOC_AC97_BUS
1329 struct snd_soc_dai *codec_dai;
1333 mutex_lock(&codec->mutex);
1334 soc_cleanup_debugfs(socdev);
1335 #ifdef CONFIG_SND_SOC_AC97_BUS
1336 for (i = 0; i < codec->num_dai; i++) {
1337 codec_dai = &codec->dai[i];
1338 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1339 soc_ac97_dev_unregister(codec);
1347 snd_card_free(codec->card);
1348 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1349 mutex_unlock(&codec->mutex);
1351 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1354 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1355 * @substream: the pcm substream
1356 * @hw: the hardware parameters
1358 * Sets the substream runtime hardware parameters.
1360 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1361 const struct snd_pcm_hardware *hw)
1363 struct snd_pcm_runtime *runtime = substream->runtime;
1364 runtime->hw.info = hw->info;
1365 runtime->hw.formats = hw->formats;
1366 runtime->hw.period_bytes_min = hw->period_bytes_min;
1367 runtime->hw.period_bytes_max = hw->period_bytes_max;
1368 runtime->hw.periods_min = hw->periods_min;
1369 runtime->hw.periods_max = hw->periods_max;
1370 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1371 runtime->hw.fifo_size = hw->fifo_size;
1374 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1377 * snd_soc_cnew - create new control
1378 * @_template: control template
1379 * @data: control private data
1380 * @lnng_name: control long name
1382 * Create a new mixer control from a template control.
1384 * Returns 0 for success, else error.
1386 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1387 void *data, char *long_name)
1389 struct snd_kcontrol_new template;
1391 memcpy(&template, _template, sizeof(template));
1393 template.name = long_name;
1396 return snd_ctl_new1(&template, data);
1398 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1401 * snd_soc_info_enum_double - enumerated double mixer info callback
1402 * @kcontrol: mixer control
1403 * @uinfo: control element information
1405 * Callback to provide information about a double enumerated
1408 * Returns 0 for success.
1410 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1411 struct snd_ctl_elem_info *uinfo)
1413 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1415 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1416 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1417 uinfo->value.enumerated.items = e->max;
1419 if (uinfo->value.enumerated.item > e->max - 1)
1420 uinfo->value.enumerated.item = e->max - 1;
1421 strcpy(uinfo->value.enumerated.name,
1422 e->texts[uinfo->value.enumerated.item]);
1425 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1428 * snd_soc_get_enum_double - enumerated double mixer get callback
1429 * @kcontrol: mixer control
1430 * @uinfo: control element information
1432 * Callback to get the value of a double enumerated mixer.
1434 * Returns 0 for success.
1436 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1437 struct snd_ctl_elem_value *ucontrol)
1439 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1440 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1441 unsigned short val, bitmask;
1443 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1445 val = snd_soc_read(codec, e->reg);
1446 ucontrol->value.enumerated.item[0]
1447 = (val >> e->shift_l) & (bitmask - 1);
1448 if (e->shift_l != e->shift_r)
1449 ucontrol->value.enumerated.item[1] =
1450 (val >> e->shift_r) & (bitmask - 1);
1454 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1457 * snd_soc_put_enum_double - enumerated double mixer put callback
1458 * @kcontrol: mixer control
1459 * @uinfo: control element information
1461 * Callback to set the value of a double enumerated mixer.
1463 * Returns 0 for success.
1465 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1466 struct snd_ctl_elem_value *ucontrol)
1468 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1469 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1471 unsigned short mask, bitmask;
1473 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1475 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1477 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1478 mask = (bitmask - 1) << e->shift_l;
1479 if (e->shift_l != e->shift_r) {
1480 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1482 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1483 mask |= (bitmask - 1) << e->shift_r;
1486 return snd_soc_update_bits(codec, e->reg, mask, val);
1488 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1491 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1492 * @kcontrol: mixer control
1493 * @uinfo: control element information
1495 * Callback to provide information about an external enumerated
1498 * Returns 0 for success.
1500 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_info *uinfo)
1503 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1505 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1507 uinfo->value.enumerated.items = e->max;
1509 if (uinfo->value.enumerated.item > e->max - 1)
1510 uinfo->value.enumerated.item = e->max - 1;
1511 strcpy(uinfo->value.enumerated.name,
1512 e->texts[uinfo->value.enumerated.item]);
1515 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1518 * snd_soc_info_volsw_ext - external single mixer info callback
1519 * @kcontrol: mixer control
1520 * @uinfo: control element information
1522 * Callback to provide information about a single external mixer control.
1524 * Returns 0 for success.
1526 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_info *uinfo)
1529 int max = kcontrol->private_value;
1532 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1534 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1537 uinfo->value.integer.min = 0;
1538 uinfo->value.integer.max = max;
1541 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1544 * snd_soc_info_volsw - single mixer info callback
1545 * @kcontrol: mixer control
1546 * @uinfo: control element information
1548 * Callback to provide information about a single mixer control.
1550 * Returns 0 for success.
1552 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_info *uinfo)
1555 struct soc_mixer_control *mc =
1556 (struct soc_mixer_control *)kcontrol->private_value;
1558 unsigned int shift = mc->shift;
1559 unsigned int rshift = mc->rshift;
1562 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1564 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1566 uinfo->count = shift == rshift ? 1 : 2;
1567 uinfo->value.integer.min = 0;
1568 uinfo->value.integer.max = max;
1571 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1574 * snd_soc_get_volsw - single mixer get callback
1575 * @kcontrol: mixer control
1576 * @uinfo: control element information
1578 * Callback to get the value of a single mixer control.
1580 * Returns 0 for success.
1582 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1583 struct snd_ctl_elem_value *ucontrol)
1585 struct soc_mixer_control *mc =
1586 (struct soc_mixer_control *)kcontrol->private_value;
1587 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1588 unsigned int reg = mc->reg;
1589 unsigned int shift = mc->shift;
1590 unsigned int rshift = mc->rshift;
1592 unsigned int mask = (1 << fls(max)) - 1;
1593 unsigned int invert = mc->invert;
1595 ucontrol->value.integer.value[0] =
1596 (snd_soc_read(codec, reg) >> shift) & mask;
1597 if (shift != rshift)
1598 ucontrol->value.integer.value[1] =
1599 (snd_soc_read(codec, reg) >> rshift) & mask;
1601 ucontrol->value.integer.value[0] =
1602 max - ucontrol->value.integer.value[0];
1603 if (shift != rshift)
1604 ucontrol->value.integer.value[1] =
1605 max - ucontrol->value.integer.value[1];
1610 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1613 * snd_soc_put_volsw - single mixer put callback
1614 * @kcontrol: mixer control
1615 * @uinfo: control element information
1617 * Callback to set the value of a single mixer control.
1619 * Returns 0 for success.
1621 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
1624 struct soc_mixer_control *mc =
1625 (struct soc_mixer_control *)kcontrol->private_value;
1626 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1627 unsigned int reg = mc->reg;
1628 unsigned int shift = mc->shift;
1629 unsigned int rshift = mc->rshift;
1631 unsigned int mask = (1 << fls(max)) - 1;
1632 unsigned int invert = mc->invert;
1633 unsigned short val, val2, val_mask;
1635 val = (ucontrol->value.integer.value[0] & mask);
1638 val_mask = mask << shift;
1640 if (shift != rshift) {
1641 val2 = (ucontrol->value.integer.value[1] & mask);
1644 val_mask |= mask << rshift;
1645 val |= val2 << rshift;
1647 return snd_soc_update_bits(codec, reg, val_mask, val);
1649 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1652 * snd_soc_info_volsw_2r - double mixer info callback
1653 * @kcontrol: mixer control
1654 * @uinfo: control element information
1656 * Callback to provide information about a double mixer control that
1657 * spans 2 codec registers.
1659 * Returns 0 for success.
1661 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_info *uinfo)
1664 struct soc_mixer_control *mc =
1665 (struct soc_mixer_control *)kcontrol->private_value;
1669 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1671 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1674 uinfo->value.integer.min = 0;
1675 uinfo->value.integer.max = max;
1678 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1681 * snd_soc_get_volsw_2r - double mixer get callback
1682 * @kcontrol: mixer control
1683 * @uinfo: control element information
1685 * Callback to get the value of a double mixer control that spans 2 registers.
1687 * Returns 0 for success.
1689 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1690 struct snd_ctl_elem_value *ucontrol)
1692 struct soc_mixer_control *mc =
1693 (struct soc_mixer_control *)kcontrol->private_value;
1694 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1695 unsigned int reg = mc->reg;
1696 unsigned int reg2 = mc->rreg;
1697 unsigned int shift = mc->shift;
1699 unsigned int mask = (1<<fls(max))-1;
1700 unsigned int invert = mc->invert;
1702 ucontrol->value.integer.value[0] =
1703 (snd_soc_read(codec, reg) >> shift) & mask;
1704 ucontrol->value.integer.value[1] =
1705 (snd_soc_read(codec, reg2) >> shift) & mask;
1707 ucontrol->value.integer.value[0] =
1708 max - ucontrol->value.integer.value[0];
1709 ucontrol->value.integer.value[1] =
1710 max - ucontrol->value.integer.value[1];
1715 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1718 * snd_soc_put_volsw_2r - double mixer set callback
1719 * @kcontrol: mixer control
1720 * @uinfo: control element information
1722 * Callback to set the value of a double mixer control that spans 2 registers.
1724 * Returns 0 for success.
1726 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
1729 struct soc_mixer_control *mc =
1730 (struct soc_mixer_control *)kcontrol->private_value;
1731 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1732 unsigned int reg = mc->reg;
1733 unsigned int reg2 = mc->rreg;
1734 unsigned int shift = mc->shift;
1736 unsigned int mask = (1 << fls(max)) - 1;
1737 unsigned int invert = mc->invert;
1739 unsigned short val, val2, val_mask;
1741 val_mask = mask << shift;
1742 val = (ucontrol->value.integer.value[0] & mask);
1743 val2 = (ucontrol->value.integer.value[1] & mask);
1751 val2 = val2 << shift;
1753 err = snd_soc_update_bits(codec, reg, val_mask, val);
1757 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1760 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1763 * snd_soc_info_volsw_s8 - signed mixer info callback
1764 * @kcontrol: mixer control
1765 * @uinfo: control element information
1767 * Callback to provide information about a signed mixer control.
1769 * Returns 0 for success.
1771 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_info *uinfo)
1774 struct soc_mixer_control *mc =
1775 (struct soc_mixer_control *)kcontrol->private_value;
1779 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1781 uinfo->value.integer.min = 0;
1782 uinfo->value.integer.max = max-min;
1785 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1788 * snd_soc_get_volsw_s8 - signed mixer get callback
1789 * @kcontrol: mixer control
1790 * @uinfo: control element information
1792 * Callback to get the value of a signed mixer control.
1794 * Returns 0 for success.
1796 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_value *ucontrol)
1799 struct soc_mixer_control *mc =
1800 (struct soc_mixer_control *)kcontrol->private_value;
1801 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1802 unsigned int reg = mc->reg;
1804 int val = snd_soc_read(codec, reg);
1806 ucontrol->value.integer.value[0] =
1807 ((signed char)(val & 0xff))-min;
1808 ucontrol->value.integer.value[1] =
1809 ((signed char)((val >> 8) & 0xff))-min;
1812 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1815 * snd_soc_put_volsw_sgn - signed mixer put callback
1816 * @kcontrol: mixer control
1817 * @uinfo: control element information
1819 * Callback to set the value of a signed mixer control.
1821 * Returns 0 for success.
1823 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_value *ucontrol)
1826 struct soc_mixer_control *mc =
1827 (struct soc_mixer_control *)kcontrol->private_value;
1828 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1829 unsigned int reg = mc->reg;
1833 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1834 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1836 return snd_soc_update_bits(codec, reg, 0xffff, val);
1838 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1841 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1843 * @clk_id: DAI specific clock ID
1844 * @freq: new clock frequency in Hz
1845 * @dir: new clock direction - input/output.
1847 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1849 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1850 unsigned int freq, int dir)
1852 if (dai->dai_ops.set_sysclk)
1853 return dai->dai_ops.set_sysclk(dai, clk_id, freq, dir);
1857 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1860 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1862 * @clk_id: DAI specific clock divider ID
1863 * @div: new clock divisor.
1865 * Configures the clock dividers. This is used to derive the best DAI bit and
1866 * frame clocks from the system or master clock. It's best to set the DAI bit
1867 * and frame clocks as low as possible to save system power.
1869 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1870 int div_id, int div)
1872 if (dai->dai_ops.set_clkdiv)
1873 return dai->dai_ops.set_clkdiv(dai, div_id, div);
1877 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1880 * snd_soc_dai_set_pll - configure DAI PLL.
1882 * @pll_id: DAI specific PLL ID
1883 * @freq_in: PLL input clock frequency in Hz
1884 * @freq_out: requested PLL output clock frequency in Hz
1886 * Configures and enables PLL to generate output clock based on input clock.
1888 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1889 int pll_id, unsigned int freq_in, unsigned int freq_out)
1891 if (dai->dai_ops.set_pll)
1892 return dai->dai_ops.set_pll(dai, pll_id, freq_in, freq_out);
1896 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1899 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1901 * @clk_id: DAI specific clock ID
1902 * @fmt: SND_SOC_DAIFMT_ format value.
1904 * Configures the DAI hardware format and clocking.
1906 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1908 if (dai->dai_ops.set_fmt)
1909 return dai->dai_ops.set_fmt(dai, fmt);
1913 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1916 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1918 * @mask: DAI specific mask representing used slots.
1919 * @slots: Number of slots in use.
1921 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1924 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1925 unsigned int mask, int slots)
1927 if (dai->dai_ops.set_sysclk)
1928 return dai->dai_ops.set_tdm_slot(dai, mask, slots);
1932 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1935 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1937 * @tristate: tristate enable
1939 * Tristates the DAI so that others can use it.
1941 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1943 if (dai->dai_ops.set_sysclk)
1944 return dai->dai_ops.set_tristate(dai, tristate);
1948 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1951 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1953 * @mute: mute enable
1955 * Mutes the DAI DAC.
1957 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1959 if (dai->dai_ops.digital_mute)
1960 return dai->dai_ops.digital_mute(dai, mute);
1964 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1966 static int __devinit snd_soc_init(void)
1968 return platform_driver_register(&soc_driver);
1971 static void snd_soc_exit(void)
1973 platform_driver_unregister(&soc_driver);
1976 module_init(snd_soc_init);
1977 module_exit(snd_soc_exit);
1979 /* Module information */
1980 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1981 MODULE_DESCRIPTION("ALSA SoC Core");
1982 MODULE_LICENSE("GPL");
1983 MODULE_ALIAS("platform:soc-audio");