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 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
92 codec->card->number, 0, codec->name);
93 err = device_register(&codec->ac97->dev);
95 snd_printk(KERN_ERR "Can't register ac97 bus\n");
96 codec->ac97->dev.bus = NULL;
104 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
105 * then initialized and any private data can be allocated. This also calls
106 * startup for the cpu DAI, platform, machine and codec DAI.
108 static int soc_pcm_open(struct snd_pcm_substream *substream)
110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
111 struct snd_soc_device *socdev = rtd->socdev;
112 struct snd_soc_card *card = socdev->card;
113 struct snd_pcm_runtime *runtime = substream->runtime;
114 struct snd_soc_dai_link *machine = rtd->dai;
115 struct snd_soc_platform *platform = card->platform;
116 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
117 struct snd_soc_dai *codec_dai = machine->codec_dai;
120 mutex_lock(&pcm_mutex);
122 /* startup the audio subsystem */
123 if (cpu_dai->ops.startup) {
124 ret = cpu_dai->ops.startup(substream, cpu_dai);
126 printk(KERN_ERR "asoc: can't open interface %s\n",
132 if (platform->pcm_ops->open) {
133 ret = platform->pcm_ops->open(substream);
135 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
140 if (codec_dai->ops.startup) {
141 ret = codec_dai->ops.startup(substream, codec_dai);
143 printk(KERN_ERR "asoc: can't open codec %s\n",
149 if (machine->ops && machine->ops->startup) {
150 ret = machine->ops->startup(substream);
152 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
157 /* Check that the codec and cpu DAI's are compatible */
158 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
159 runtime->hw.rate_min =
160 max(codec_dai->playback.rate_min,
161 cpu_dai->playback.rate_min);
162 runtime->hw.rate_max =
163 min(codec_dai->playback.rate_max,
164 cpu_dai->playback.rate_max);
165 runtime->hw.channels_min =
166 max(codec_dai->playback.channels_min,
167 cpu_dai->playback.channels_min);
168 runtime->hw.channels_max =
169 min(codec_dai->playback.channels_max,
170 cpu_dai->playback.channels_max);
171 runtime->hw.formats =
172 codec_dai->playback.formats & cpu_dai->playback.formats;
174 codec_dai->playback.rates & cpu_dai->playback.rates;
176 runtime->hw.rate_min =
177 max(codec_dai->capture.rate_min,
178 cpu_dai->capture.rate_min);
179 runtime->hw.rate_max =
180 min(codec_dai->capture.rate_max,
181 cpu_dai->capture.rate_max);
182 runtime->hw.channels_min =
183 max(codec_dai->capture.channels_min,
184 cpu_dai->capture.channels_min);
185 runtime->hw.channels_max =
186 min(codec_dai->capture.channels_max,
187 cpu_dai->capture.channels_max);
188 runtime->hw.formats =
189 codec_dai->capture.formats & cpu_dai->capture.formats;
191 codec_dai->capture.rates & cpu_dai->capture.rates;
194 snd_pcm_limit_hw_rates(runtime);
195 if (!runtime->hw.rates) {
196 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
197 codec_dai->name, cpu_dai->name);
200 if (!runtime->hw.formats) {
201 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
202 codec_dai->name, cpu_dai->name);
205 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
206 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
207 codec_dai->name, cpu_dai->name);
211 pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
212 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
213 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
214 runtime->hw.channels_max);
215 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
216 runtime->hw.rate_max);
218 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
219 cpu_dai->playback.active = codec_dai->playback.active = 1;
221 cpu_dai->capture.active = codec_dai->capture.active = 1;
222 cpu_dai->active = codec_dai->active = 1;
223 cpu_dai->runtime = runtime;
224 socdev->codec->active++;
225 mutex_unlock(&pcm_mutex);
229 if (machine->ops && machine->ops->shutdown)
230 machine->ops->shutdown(substream);
233 if (platform->pcm_ops->close)
234 platform->pcm_ops->close(substream);
237 if (cpu_dai->ops.shutdown)
238 cpu_dai->ops.shutdown(substream, cpu_dai);
240 mutex_unlock(&pcm_mutex);
245 * Power down the audio subsystem pmdown_time msecs after close is called.
246 * This is to ensure there are no pops or clicks in between any music tracks
247 * due to DAPM power cycling.
249 static void close_delayed_work(struct work_struct *work)
251 struct snd_soc_card *card = container_of(work, struct snd_soc_card,
253 struct snd_soc_device *socdev = card->socdev;
254 struct snd_soc_codec *codec = socdev->codec;
255 struct snd_soc_dai *codec_dai;
258 mutex_lock(&pcm_mutex);
259 for (i = 0; i < codec->num_dai; i++) {
260 codec_dai = &codec->dai[i];
262 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
263 codec_dai->playback.stream_name,
264 codec_dai->playback.active ? "active" : "inactive",
265 codec_dai->pop_wait ? "yes" : "no");
267 /* are we waiting on this codec DAI stream */
268 if (codec_dai->pop_wait == 1) {
270 /* Reduce power if no longer active */
271 if (codec->active == 0) {
272 pr_debug("pop wq D1 %s %s\n", codec->name,
273 codec_dai->playback.stream_name);
274 snd_soc_dapm_set_bias_level(socdev,
275 SND_SOC_BIAS_PREPARE);
278 codec_dai->pop_wait = 0;
279 snd_soc_dapm_stream_event(codec,
280 codec_dai->playback.stream_name,
281 SND_SOC_DAPM_STREAM_STOP);
283 /* Fall into standby if no longer active */
284 if (codec->active == 0) {
285 pr_debug("pop wq D3 %s %s\n", codec->name,
286 codec_dai->playback.stream_name);
287 snd_soc_dapm_set_bias_level(socdev,
288 SND_SOC_BIAS_STANDBY);
292 mutex_unlock(&pcm_mutex);
296 * Called by ALSA when a PCM substream is closed. Private data can be
297 * freed here. The cpu DAI, codec DAI, machine and platform are also
300 static int soc_codec_close(struct snd_pcm_substream *substream)
302 struct snd_soc_pcm_runtime *rtd = substream->private_data;
303 struct snd_soc_device *socdev = rtd->socdev;
304 struct snd_soc_card *card = socdev->card;
305 struct snd_soc_dai_link *machine = rtd->dai;
306 struct snd_soc_platform *platform = card->platform;
307 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
308 struct snd_soc_dai *codec_dai = machine->codec_dai;
309 struct snd_soc_codec *codec = socdev->codec;
311 mutex_lock(&pcm_mutex);
313 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
314 cpu_dai->playback.active = codec_dai->playback.active = 0;
316 cpu_dai->capture.active = codec_dai->capture.active = 0;
318 if (codec_dai->playback.active == 0 &&
319 codec_dai->capture.active == 0) {
320 cpu_dai->active = codec_dai->active = 0;
324 /* Muting the DAC suppresses artifacts caused during digital
325 * shutdown, for example from stopping clocks.
327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
328 snd_soc_dai_digital_mute(codec_dai, 1);
330 if (cpu_dai->ops.shutdown)
331 cpu_dai->ops.shutdown(substream, cpu_dai);
333 if (codec_dai->ops.shutdown)
334 codec_dai->ops.shutdown(substream, codec_dai);
336 if (machine->ops && machine->ops->shutdown)
337 machine->ops->shutdown(substream);
339 if (platform->pcm_ops->close)
340 platform->pcm_ops->close(substream);
341 cpu_dai->runtime = NULL;
343 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
344 /* start delayed pop wq here for playback streams */
345 codec_dai->pop_wait = 1;
346 schedule_delayed_work(&card->delayed_work,
347 msecs_to_jiffies(pmdown_time));
349 /* capture streams can be powered down now */
350 snd_soc_dapm_stream_event(codec,
351 codec_dai->capture.stream_name,
352 SND_SOC_DAPM_STREAM_STOP);
354 if (codec->active == 0 && codec_dai->pop_wait == 0)
355 snd_soc_dapm_set_bias_level(socdev,
356 SND_SOC_BIAS_STANDBY);
359 mutex_unlock(&pcm_mutex);
364 * Called by ALSA when the PCM substream is prepared, can set format, sample
365 * rate, etc. This function is non atomic and can be called multiple times,
366 * it can refer to the runtime info.
368 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
370 struct snd_soc_pcm_runtime *rtd = substream->private_data;
371 struct snd_soc_device *socdev = rtd->socdev;
372 struct snd_soc_card *card = socdev->card;
373 struct snd_soc_dai_link *machine = rtd->dai;
374 struct snd_soc_platform *platform = card->platform;
375 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
376 struct snd_soc_dai *codec_dai = machine->codec_dai;
377 struct snd_soc_codec *codec = socdev->codec;
380 mutex_lock(&pcm_mutex);
382 if (machine->ops && machine->ops->prepare) {
383 ret = machine->ops->prepare(substream);
385 printk(KERN_ERR "asoc: machine prepare error\n");
390 if (platform->pcm_ops->prepare) {
391 ret = platform->pcm_ops->prepare(substream);
393 printk(KERN_ERR "asoc: platform prepare error\n");
398 if (codec_dai->ops.prepare) {
399 ret = codec_dai->ops.prepare(substream, codec_dai);
401 printk(KERN_ERR "asoc: codec DAI prepare error\n");
406 if (cpu_dai->ops.prepare) {
407 ret = cpu_dai->ops.prepare(substream, cpu_dai);
409 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
414 /* cancel any delayed stream shutdown that is pending */
415 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
416 codec_dai->pop_wait) {
417 codec_dai->pop_wait = 0;
418 cancel_delayed_work(&card->delayed_work);
421 /* do we need to power up codec */
422 if (codec->bias_level != SND_SOC_BIAS_ON) {
423 snd_soc_dapm_set_bias_level(socdev,
424 SND_SOC_BIAS_PREPARE);
426 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
427 snd_soc_dapm_stream_event(codec,
428 codec_dai->playback.stream_name,
429 SND_SOC_DAPM_STREAM_START);
431 snd_soc_dapm_stream_event(codec,
432 codec_dai->capture.stream_name,
433 SND_SOC_DAPM_STREAM_START);
435 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
436 snd_soc_dai_digital_mute(codec_dai, 0);
439 /* codec already powered - power on widgets */
440 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
441 snd_soc_dapm_stream_event(codec,
442 codec_dai->playback.stream_name,
443 SND_SOC_DAPM_STREAM_START);
445 snd_soc_dapm_stream_event(codec,
446 codec_dai->capture.stream_name,
447 SND_SOC_DAPM_STREAM_START);
449 snd_soc_dai_digital_mute(codec_dai, 0);
453 mutex_unlock(&pcm_mutex);
458 * Called by ALSA when the hardware params are set by application. This
459 * function can also be called multiple times and can allocate buffers
460 * (using snd_pcm_lib_* ). It's non-atomic.
462 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params *params)
465 struct snd_soc_pcm_runtime *rtd = substream->private_data;
466 struct snd_soc_device *socdev = rtd->socdev;
467 struct snd_soc_dai_link *machine = rtd->dai;
468 struct snd_soc_card *card = socdev->card;
469 struct snd_soc_platform *platform = card->platform;
470 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
471 struct snd_soc_dai *codec_dai = machine->codec_dai;
474 mutex_lock(&pcm_mutex);
476 if (machine->ops && machine->ops->hw_params) {
477 ret = machine->ops->hw_params(substream, params);
479 printk(KERN_ERR "asoc: machine hw_params failed\n");
484 if (codec_dai->ops.hw_params) {
485 ret = codec_dai->ops.hw_params(substream, params, codec_dai);
487 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
493 if (cpu_dai->ops.hw_params) {
494 ret = cpu_dai->ops.hw_params(substream, params, cpu_dai);
496 printk(KERN_ERR "asoc: interface %s hw params failed\n",
502 if (platform->pcm_ops->hw_params) {
503 ret = platform->pcm_ops->hw_params(substream, params);
505 printk(KERN_ERR "asoc: platform %s hw params failed\n",
512 mutex_unlock(&pcm_mutex);
516 if (cpu_dai->ops.hw_free)
517 cpu_dai->ops.hw_free(substream, cpu_dai);
520 if (codec_dai->ops.hw_free)
521 codec_dai->ops.hw_free(substream, codec_dai);
524 if (machine->ops && machine->ops->hw_free)
525 machine->ops->hw_free(substream);
527 mutex_unlock(&pcm_mutex);
532 * Free's resources allocated by hw_params, can be called multiple times
534 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
536 struct snd_soc_pcm_runtime *rtd = substream->private_data;
537 struct snd_soc_device *socdev = rtd->socdev;
538 struct snd_soc_dai_link *machine = rtd->dai;
539 struct snd_soc_card *card = socdev->card;
540 struct snd_soc_platform *platform = card->platform;
541 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
542 struct snd_soc_dai *codec_dai = machine->codec_dai;
543 struct snd_soc_codec *codec = socdev->codec;
545 mutex_lock(&pcm_mutex);
547 /* apply codec digital mute */
549 snd_soc_dai_digital_mute(codec_dai, 1);
551 /* free any machine hw params */
552 if (machine->ops && machine->ops->hw_free)
553 machine->ops->hw_free(substream);
555 /* free any DMA resources */
556 if (platform->pcm_ops->hw_free)
557 platform->pcm_ops->hw_free(substream);
559 /* now free hw params for the DAI's */
560 if (codec_dai->ops.hw_free)
561 codec_dai->ops.hw_free(substream, codec_dai);
563 if (cpu_dai->ops.hw_free)
564 cpu_dai->ops.hw_free(substream, cpu_dai);
566 mutex_unlock(&pcm_mutex);
570 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
572 struct snd_soc_pcm_runtime *rtd = substream->private_data;
573 struct snd_soc_device *socdev = rtd->socdev;
574 struct snd_soc_card *card= socdev->card;
575 struct snd_soc_dai_link *machine = rtd->dai;
576 struct snd_soc_platform *platform = card->platform;
577 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
578 struct snd_soc_dai *codec_dai = machine->codec_dai;
581 if (codec_dai->ops.trigger) {
582 ret = codec_dai->ops.trigger(substream, cmd, codec_dai);
587 if (platform->pcm_ops->trigger) {
588 ret = platform->pcm_ops->trigger(substream, cmd);
593 if (cpu_dai->ops.trigger) {
594 ret = cpu_dai->ops.trigger(substream, cmd, cpu_dai);
601 /* ASoC PCM operations */
602 static struct snd_pcm_ops soc_pcm_ops = {
603 .open = soc_pcm_open,
604 .close = soc_codec_close,
605 .hw_params = soc_pcm_hw_params,
606 .hw_free = soc_pcm_hw_free,
607 .prepare = soc_pcm_prepare,
608 .trigger = soc_pcm_trigger,
612 /* powers down audio subsystem for suspend */
613 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
615 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
616 struct snd_soc_card *card = socdev->card;
617 struct snd_soc_platform *platform = card->platform;
618 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
619 struct snd_soc_codec *codec = socdev->codec;
622 /* Due to the resume being scheduled into a workqueue we could
623 * suspend before that's finished - wait for it to complete.
625 snd_power_lock(codec->card);
626 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
627 snd_power_unlock(codec->card);
629 /* we're going to block userspace touching us until resume completes */
630 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
632 /* mute any active DAC's */
633 for (i = 0; i < card->num_links; i++) {
634 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
635 if (dai->ops.digital_mute && dai->playback.active)
636 dai->ops.digital_mute(dai, 1);
639 /* suspend all pcms */
640 for (i = 0; i < card->num_links; i++)
641 snd_pcm_suspend_all(card->dai_link[i].pcm);
643 if (card->suspend_pre)
644 card->suspend_pre(pdev, state);
646 for (i = 0; i < card->num_links; i++) {
647 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
648 if (cpu_dai->suspend && !cpu_dai->ac97_control)
649 cpu_dai->suspend(pdev, cpu_dai);
650 if (platform->suspend)
651 platform->suspend(pdev, cpu_dai);
654 /* close any waiting streams and save state */
655 run_delayed_work(&card->delayed_work);
656 codec->suspend_bias_level = codec->bias_level;
658 for (i = 0; i < codec->num_dai; i++) {
659 char *stream = codec->dai[i].playback.stream_name;
661 snd_soc_dapm_stream_event(codec, stream,
662 SND_SOC_DAPM_STREAM_SUSPEND);
663 stream = codec->dai[i].capture.stream_name;
665 snd_soc_dapm_stream_event(codec, stream,
666 SND_SOC_DAPM_STREAM_SUSPEND);
669 if (codec_dev->suspend)
670 codec_dev->suspend(pdev, state);
672 for (i = 0; i < card->num_links; i++) {
673 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
674 if (cpu_dai->suspend && cpu_dai->ac97_control)
675 cpu_dai->suspend(pdev, cpu_dai);
678 if (card->suspend_post)
679 card->suspend_post(pdev, state);
684 /* deferred resume work, so resume can complete before we finished
685 * setting our codec back up, which can be very slow on I2C
687 static void soc_resume_deferred(struct work_struct *work)
689 struct snd_soc_card *card = container_of(work,
691 deferred_resume_work);
692 struct snd_soc_device *socdev = card->socdev;
693 struct snd_soc_platform *platform = card->platform;
694 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
695 struct snd_soc_codec *codec = socdev->codec;
696 struct platform_device *pdev = to_platform_device(socdev->dev);
699 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
700 * so userspace apps are blocked from touching us
703 dev_dbg(socdev->dev, "starting resume work\n");
705 if (card->resume_pre)
706 card->resume_pre(pdev);
708 for (i = 0; i < card->num_links; i++) {
709 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
710 if (cpu_dai->resume && cpu_dai->ac97_control)
711 cpu_dai->resume(pdev, cpu_dai);
714 if (codec_dev->resume)
715 codec_dev->resume(pdev);
717 for (i = 0; i < codec->num_dai; i++) {
718 char *stream = codec->dai[i].playback.stream_name;
720 snd_soc_dapm_stream_event(codec, stream,
721 SND_SOC_DAPM_STREAM_RESUME);
722 stream = codec->dai[i].capture.stream_name;
724 snd_soc_dapm_stream_event(codec, stream,
725 SND_SOC_DAPM_STREAM_RESUME);
728 /* unmute any active DACs */
729 for (i = 0; i < card->num_links; i++) {
730 struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
731 if (dai->ops.digital_mute && dai->playback.active)
732 dai->ops.digital_mute(dai, 0);
735 for (i = 0; i < card->num_links; i++) {
736 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
737 if (cpu_dai->resume && !cpu_dai->ac97_control)
738 cpu_dai->resume(pdev, cpu_dai);
739 if (platform->resume)
740 platform->resume(pdev, cpu_dai);
743 if (card->resume_post)
744 card->resume_post(pdev);
746 dev_dbg(socdev->dev, "resume work completed\n");
748 /* userspace can access us now we are back as we were before */
749 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
752 /* powers up audio subsystem after a suspend */
753 static int soc_resume(struct platform_device *pdev)
755 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
756 struct snd_soc_card *card = socdev->card;
758 dev_dbg(socdev->dev, "scheduling resume work\n");
760 if (!schedule_work(&card->deferred_resume_work))
761 dev_err(socdev->dev, "resume work item may be lost\n");
767 #define soc_suspend NULL
768 #define soc_resume NULL
771 /* probes a new socdev */
772 static int soc_probe(struct platform_device *pdev)
775 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
776 struct snd_soc_card *card = socdev->card;
777 struct snd_soc_platform *platform = card->platform;
778 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
780 /* Bodge while we push things out of socdev */
781 card->socdev = socdev;
784 ret = card->probe(pdev);
789 for (i = 0; i < card->num_links; i++) {
790 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
791 if (cpu_dai->probe) {
792 ret = cpu_dai->probe(pdev, cpu_dai);
798 if (codec_dev->probe) {
799 ret = codec_dev->probe(pdev);
804 if (platform->probe) {
805 ret = platform->probe(pdev);
810 /* DAPM stream work */
811 INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
813 /* deferred resume work */
814 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
820 if (codec_dev->remove)
821 codec_dev->remove(pdev);
824 for (i--; i >= 0; i--) {
825 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
827 cpu_dai->remove(pdev, cpu_dai);
836 /* removes a socdev */
837 static int soc_remove(struct platform_device *pdev)
840 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
841 struct snd_soc_card *card = socdev->card;
842 struct snd_soc_platform *platform = card->platform;
843 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
845 run_delayed_work(&card->delayed_work);
847 if (platform->remove)
848 platform->remove(pdev);
850 if (codec_dev->remove)
851 codec_dev->remove(pdev);
853 for (i = 0; i < card->num_links; i++) {
854 struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
856 cpu_dai->remove(pdev, cpu_dai);
865 /* ASoC platform driver */
866 static struct platform_driver soc_driver = {
869 .owner = THIS_MODULE,
872 .remove = soc_remove,
873 .suspend = soc_suspend,
874 .resume = soc_resume,
877 /* create a new pcm */
878 static int soc_new_pcm(struct snd_soc_device *socdev,
879 struct snd_soc_dai_link *dai_link, int num)
881 struct snd_soc_codec *codec = socdev->codec;
882 struct snd_soc_card *card = socdev->card;
883 struct snd_soc_platform *platform = card->platform;
884 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
885 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
886 struct snd_soc_pcm_runtime *rtd;
889 int ret = 0, playback = 0, capture = 0;
891 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
896 rtd->socdev = socdev;
897 codec_dai->codec = socdev->codec;
899 /* check client and interface hw capabilities */
900 sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
903 if (codec_dai->playback.channels_min)
905 if (codec_dai->capture.channels_min)
908 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
911 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
918 pcm->private_data = rtd;
919 soc_pcm_ops.mmap = platform->pcm_ops->mmap;
920 soc_pcm_ops.pointer = platform->pcm_ops->pointer;
921 soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
922 soc_pcm_ops.copy = platform->pcm_ops->copy;
923 soc_pcm_ops.silence = platform->pcm_ops->silence;
924 soc_pcm_ops.ack = platform->pcm_ops->ack;
925 soc_pcm_ops.page = platform->pcm_ops->page;
928 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
931 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
933 ret = platform->pcm_new(codec->card, codec_dai, pcm);
935 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
940 pcm->private_free = platform->pcm_free;
941 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
946 /* codec register dump */
947 static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
949 struct snd_soc_codec *codec = devdata->codec;
950 int i, step = 1, count = 0;
952 if (!codec->reg_cache_size)
955 if (codec->reg_cache_step)
956 step = codec->reg_cache_step;
958 count += sprintf(buf, "%s registers\n", codec->name);
959 for (i = 0; i < codec->reg_cache_size; i += step) {
960 count += sprintf(buf + count, "%2x: ", i);
961 if (count >= PAGE_SIZE - 1)
964 if (codec->display_register)
965 count += codec->display_register(codec, buf + count,
966 PAGE_SIZE - count, i);
968 count += snprintf(buf + count, PAGE_SIZE - count,
969 "%4x", codec->read(codec, i));
971 if (count >= PAGE_SIZE - 1)
974 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
975 if (count >= PAGE_SIZE - 1)
979 /* Truncate count; min() would cause a warning */
980 if (count >= PAGE_SIZE)
981 count = PAGE_SIZE - 1;
985 static ssize_t codec_reg_show(struct device *dev,
986 struct device_attribute *attr, char *buf)
988 struct snd_soc_device *devdata = dev_get_drvdata(dev);
989 return soc_codec_reg_show(devdata, buf);
992 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
994 #ifdef CONFIG_DEBUG_FS
995 static int codec_reg_open_file(struct inode *inode, struct file *file)
997 file->private_data = inode->i_private;
1001 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
1002 size_t count, loff_t *ppos)
1005 struct snd_soc_device *devdata = file->private_data;
1006 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1009 ret = soc_codec_reg_show(devdata, buf);
1011 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1016 static ssize_t codec_reg_write_file(struct file *file,
1017 const char __user *user_buf, size_t count, loff_t *ppos)
1022 unsigned long reg, value;
1024 struct snd_soc_device *devdata = file->private_data;
1025 struct snd_soc_codec *codec = devdata->codec;
1027 buf_size = min(count, (sizeof(buf)-1));
1028 if (copy_from_user(buf, user_buf, buf_size))
1032 if (codec->reg_cache_step)
1033 step = codec->reg_cache_step;
1035 while (*start == ' ')
1037 reg = simple_strtoul(start, &start, 16);
1038 if ((reg >= codec->reg_cache_size) || (reg % step))
1040 while (*start == ' ')
1042 if (strict_strtoul(start, 16, &value))
1044 codec->write(codec, reg, value);
1048 static const struct file_operations codec_reg_fops = {
1049 .open = codec_reg_open_file,
1050 .read = codec_reg_read_file,
1051 .write = codec_reg_write_file,
1054 static void soc_init_debugfs(struct snd_soc_device *socdev)
1056 struct dentry *root, *file;
1057 struct snd_soc_codec *codec = socdev->codec;
1058 root = debugfs_create_dir(dev_name(socdev->dev), NULL);
1059 if (IS_ERR(root) || !root)
1062 file = debugfs_create_file("codec_reg", 0644,
1063 root, socdev, &codec_reg_fops);
1067 file = debugfs_create_u32("dapm_pop_time", 0744,
1068 root, &codec->pop_time);
1071 socdev->debugfs_root = root;
1074 debugfs_remove_recursive(root);
1076 dev_err(socdev->dev, "debugfs is not available\n");
1079 static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1081 debugfs_remove_recursive(socdev->debugfs_root);
1082 socdev->debugfs_root = NULL;
1087 static inline void soc_init_debugfs(struct snd_soc_device *socdev)
1091 static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
1097 * snd_soc_new_ac97_codec - initailise AC97 device
1098 * @codec: audio codec
1099 * @ops: AC97 bus operations
1100 * @num: AC97 codec number
1102 * Initialises AC97 codec resources for use by ad-hoc devices only.
1104 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1105 struct snd_ac97_bus_ops *ops, int num)
1107 mutex_lock(&codec->mutex);
1109 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1110 if (codec->ac97 == NULL) {
1111 mutex_unlock(&codec->mutex);
1115 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1116 if (codec->ac97->bus == NULL) {
1119 mutex_unlock(&codec->mutex);
1123 codec->ac97->bus->ops = ops;
1124 codec->ac97->num = num;
1125 mutex_unlock(&codec->mutex);
1128 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1131 * snd_soc_free_ac97_codec - free AC97 codec device
1132 * @codec: audio codec
1134 * Frees AC97 codec device resources.
1136 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1138 mutex_lock(&codec->mutex);
1139 kfree(codec->ac97->bus);
1142 mutex_unlock(&codec->mutex);
1144 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1147 * snd_soc_update_bits - update codec register bits
1148 * @codec: audio codec
1149 * @reg: codec register
1150 * @mask: register mask
1153 * Writes new register value.
1155 * Returns 1 for change else 0.
1157 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1158 unsigned short mask, unsigned short value)
1161 unsigned short old, new;
1163 mutex_lock(&io_mutex);
1164 old = snd_soc_read(codec, reg);
1165 new = (old & ~mask) | value;
1166 change = old != new;
1168 snd_soc_write(codec, reg, new);
1170 mutex_unlock(&io_mutex);
1173 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1176 * snd_soc_test_bits - test register for change
1177 * @codec: audio codec
1178 * @reg: codec register
1179 * @mask: register mask
1182 * Tests a register with a new value and checks if the new value is
1183 * different from the old value.
1185 * Returns 1 for change else 0.
1187 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1188 unsigned short mask, unsigned short value)
1191 unsigned short old, new;
1193 mutex_lock(&io_mutex);
1194 old = snd_soc_read(codec, reg);
1195 new = (old & ~mask) | value;
1196 change = old != new;
1197 mutex_unlock(&io_mutex);
1201 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1204 * snd_soc_new_pcms - create new sound card and pcms
1205 * @socdev: the SoC audio device
1207 * Create a new sound card based upon the codec and interface pcms.
1209 * Returns 0 for success, else error.
1211 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1213 struct snd_soc_codec *codec = socdev->codec;
1214 struct snd_soc_card *card = socdev->card;
1217 mutex_lock(&codec->mutex);
1219 /* register a sound card */
1220 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1222 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1224 mutex_unlock(&codec->mutex);
1228 codec->card->dev = socdev->dev;
1229 codec->card->private_data = codec;
1230 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1232 /* create the pcms */
1233 for (i = 0; i < card->num_links; i++) {
1234 ret = soc_new_pcm(socdev, &card->dai_link[i], i);
1236 printk(KERN_ERR "asoc: can't create pcm %s\n",
1237 card->dai_link[i].stream_name);
1238 mutex_unlock(&codec->mutex);
1243 mutex_unlock(&codec->mutex);
1246 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1249 * snd_soc_init_card - register sound card
1250 * @socdev: the SoC audio device
1252 * Register a SoC sound card. Also registers an AC97 device if the
1253 * codec is AC97 for ad hoc devices.
1255 * Returns 0 for success, else error.
1257 int snd_soc_init_card(struct snd_soc_device *socdev)
1259 struct snd_soc_codec *codec = socdev->codec;
1260 struct snd_soc_card *card = socdev->card;
1261 int ret = 0, i, ac97 = 0, err = 0;
1263 for (i = 0; i < card->num_links; i++) {
1264 if (card->dai_link[i].init) {
1265 err = card->dai_link[i].init(codec);
1267 printk(KERN_ERR "asoc: failed to init %s\n",
1268 card->dai_link[i].stream_name);
1272 if (card->dai_link[i].codec_dai->ac97_control)
1275 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1277 snprintf(codec->card->longname, sizeof(codec->card->longname),
1278 "%s (%s)", card->name, codec->name);
1280 ret = snd_card_register(codec->card);
1282 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1287 mutex_lock(&codec->mutex);
1288 #ifdef CONFIG_SND_SOC_AC97_BUS
1290 ret = soc_ac97_dev_register(codec);
1292 printk(KERN_ERR "asoc: AC97 device register failed\n");
1293 snd_card_free(codec->card);
1294 mutex_unlock(&codec->mutex);
1300 err = snd_soc_dapm_sys_add(socdev->dev);
1302 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1304 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1306 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1308 soc_init_debugfs(socdev);
1309 mutex_unlock(&codec->mutex);
1314 EXPORT_SYMBOL_GPL(snd_soc_init_card);
1317 * snd_soc_free_pcms - free sound card and pcms
1318 * @socdev: the SoC audio device
1320 * Frees sound card and pcms associated with the socdev.
1321 * Also unregister the codec if it is an AC97 device.
1323 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1325 struct snd_soc_codec *codec = socdev->codec;
1326 #ifdef CONFIG_SND_SOC_AC97_BUS
1327 struct snd_soc_dai *codec_dai;
1331 mutex_lock(&codec->mutex);
1332 soc_cleanup_debugfs(socdev);
1333 #ifdef CONFIG_SND_SOC_AC97_BUS
1334 for (i = 0; i < codec->num_dai; i++) {
1335 codec_dai = &codec->dai[i];
1336 if (codec_dai->ac97_control && codec->ac97) {
1337 soc_ac97_dev_unregister(codec);
1345 snd_card_free(codec->card);
1346 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1347 mutex_unlock(&codec->mutex);
1349 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1352 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1353 * @substream: the pcm substream
1354 * @hw: the hardware parameters
1356 * Sets the substream runtime hardware parameters.
1358 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1359 const struct snd_pcm_hardware *hw)
1361 struct snd_pcm_runtime *runtime = substream->runtime;
1362 runtime->hw.info = hw->info;
1363 runtime->hw.formats = hw->formats;
1364 runtime->hw.period_bytes_min = hw->period_bytes_min;
1365 runtime->hw.period_bytes_max = hw->period_bytes_max;
1366 runtime->hw.periods_min = hw->periods_min;
1367 runtime->hw.periods_max = hw->periods_max;
1368 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1369 runtime->hw.fifo_size = hw->fifo_size;
1372 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1375 * snd_soc_cnew - create new control
1376 * @_template: control template
1377 * @data: control private data
1378 * @lnng_name: control long name
1380 * Create a new mixer control from a template control.
1382 * Returns 0 for success, else error.
1384 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1385 void *data, char *long_name)
1387 struct snd_kcontrol_new template;
1389 memcpy(&template, _template, sizeof(template));
1391 template.name = long_name;
1394 return snd_ctl_new1(&template, data);
1396 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1399 * snd_soc_info_enum_double - enumerated double mixer info callback
1400 * @kcontrol: mixer control
1401 * @uinfo: control element information
1403 * Callback to provide information about a double enumerated
1406 * Returns 0 for success.
1408 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_info *uinfo)
1411 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1413 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1414 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1415 uinfo->value.enumerated.items = e->max;
1417 if (uinfo->value.enumerated.item > e->max - 1)
1418 uinfo->value.enumerated.item = e->max - 1;
1419 strcpy(uinfo->value.enumerated.name,
1420 e->texts[uinfo->value.enumerated.item]);
1423 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1426 * snd_soc_get_enum_double - enumerated double mixer get callback
1427 * @kcontrol: mixer control
1428 * @uinfo: control element information
1430 * Callback to get the value of a double enumerated mixer.
1432 * Returns 0 for success.
1434 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_value *ucontrol)
1437 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1438 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1439 unsigned short val, bitmask;
1441 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1443 val = snd_soc_read(codec, e->reg);
1444 ucontrol->value.enumerated.item[0]
1445 = (val >> e->shift_l) & (bitmask - 1);
1446 if (e->shift_l != e->shift_r)
1447 ucontrol->value.enumerated.item[1] =
1448 (val >> e->shift_r) & (bitmask - 1);
1452 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1455 * snd_soc_put_enum_double - enumerated double mixer put callback
1456 * @kcontrol: mixer control
1457 * @uinfo: control element information
1459 * Callback to set the value of a double enumerated mixer.
1461 * Returns 0 for success.
1463 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_value *ucontrol)
1466 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1467 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1469 unsigned short mask, bitmask;
1471 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1473 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1475 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1476 mask = (bitmask - 1) << e->shift_l;
1477 if (e->shift_l != e->shift_r) {
1478 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1480 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1481 mask |= (bitmask - 1) << e->shift_r;
1484 return snd_soc_update_bits(codec, e->reg, mask, val);
1486 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1489 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1490 * @kcontrol: mixer control
1491 * @uinfo: control element information
1493 * Callback to provide information about an external enumerated
1496 * Returns 0 for success.
1498 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1499 struct snd_ctl_elem_info *uinfo)
1501 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1503 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1505 uinfo->value.enumerated.items = e->max;
1507 if (uinfo->value.enumerated.item > e->max - 1)
1508 uinfo->value.enumerated.item = e->max - 1;
1509 strcpy(uinfo->value.enumerated.name,
1510 e->texts[uinfo->value.enumerated.item]);
1513 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1516 * snd_soc_info_volsw_ext - external single mixer info callback
1517 * @kcontrol: mixer control
1518 * @uinfo: control element information
1520 * Callback to provide information about a single external mixer control.
1522 * Returns 0 for success.
1524 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_info *uinfo)
1527 int max = kcontrol->private_value;
1530 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1532 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1535 uinfo->value.integer.min = 0;
1536 uinfo->value.integer.max = max;
1539 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1542 * snd_soc_info_volsw - single mixer info callback
1543 * @kcontrol: mixer control
1544 * @uinfo: control element information
1546 * Callback to provide information about a single mixer control.
1548 * Returns 0 for success.
1550 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1551 struct snd_ctl_elem_info *uinfo)
1553 struct soc_mixer_control *mc =
1554 (struct soc_mixer_control *)kcontrol->private_value;
1556 unsigned int shift = mc->shift;
1557 unsigned int rshift = mc->rshift;
1560 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1562 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1564 uinfo->count = shift == rshift ? 1 : 2;
1565 uinfo->value.integer.min = 0;
1566 uinfo->value.integer.max = max;
1569 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1572 * snd_soc_get_volsw - single mixer get callback
1573 * @kcontrol: mixer control
1574 * @uinfo: control element information
1576 * Callback to get the value of a single mixer control.
1578 * Returns 0 for success.
1580 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1583 struct soc_mixer_control *mc =
1584 (struct soc_mixer_control *)kcontrol->private_value;
1585 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1586 unsigned int reg = mc->reg;
1587 unsigned int shift = mc->shift;
1588 unsigned int rshift = mc->rshift;
1590 unsigned int mask = (1 << fls(max)) - 1;
1591 unsigned int invert = mc->invert;
1593 ucontrol->value.integer.value[0] =
1594 (snd_soc_read(codec, reg) >> shift) & mask;
1595 if (shift != rshift)
1596 ucontrol->value.integer.value[1] =
1597 (snd_soc_read(codec, reg) >> rshift) & mask;
1599 ucontrol->value.integer.value[0] =
1600 max - ucontrol->value.integer.value[0];
1601 if (shift != rshift)
1602 ucontrol->value.integer.value[1] =
1603 max - ucontrol->value.integer.value[1];
1608 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1611 * snd_soc_put_volsw - single mixer put callback
1612 * @kcontrol: mixer control
1613 * @uinfo: control element information
1615 * Callback to set the value of a single mixer control.
1617 * Returns 0 for success.
1619 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1620 struct snd_ctl_elem_value *ucontrol)
1622 struct soc_mixer_control *mc =
1623 (struct soc_mixer_control *)kcontrol->private_value;
1624 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1625 unsigned int reg = mc->reg;
1626 unsigned int shift = mc->shift;
1627 unsigned int rshift = mc->rshift;
1629 unsigned int mask = (1 << fls(max)) - 1;
1630 unsigned int invert = mc->invert;
1631 unsigned short val, val2, val_mask;
1633 val = (ucontrol->value.integer.value[0] & mask);
1636 val_mask = mask << shift;
1638 if (shift != rshift) {
1639 val2 = (ucontrol->value.integer.value[1] & mask);
1642 val_mask |= mask << rshift;
1643 val |= val2 << rshift;
1645 return snd_soc_update_bits(codec, reg, val_mask, val);
1647 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1650 * snd_soc_info_volsw_2r - double mixer info callback
1651 * @kcontrol: mixer control
1652 * @uinfo: control element information
1654 * Callback to provide information about a double mixer control that
1655 * spans 2 codec registers.
1657 * Returns 0 for success.
1659 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_info *uinfo)
1662 struct soc_mixer_control *mc =
1663 (struct soc_mixer_control *)kcontrol->private_value;
1667 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1669 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1672 uinfo->value.integer.min = 0;
1673 uinfo->value.integer.max = max;
1676 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1679 * snd_soc_get_volsw_2r - double mixer get callback
1680 * @kcontrol: mixer control
1681 * @uinfo: control element information
1683 * Callback to get the value of a double mixer control that spans 2 registers.
1685 * Returns 0 for success.
1687 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1688 struct snd_ctl_elem_value *ucontrol)
1690 struct soc_mixer_control *mc =
1691 (struct soc_mixer_control *)kcontrol->private_value;
1692 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1693 unsigned int reg = mc->reg;
1694 unsigned int reg2 = mc->rreg;
1695 unsigned int shift = mc->shift;
1697 unsigned int mask = (1<<fls(max))-1;
1698 unsigned int invert = mc->invert;
1700 ucontrol->value.integer.value[0] =
1701 (snd_soc_read(codec, reg) >> shift) & mask;
1702 ucontrol->value.integer.value[1] =
1703 (snd_soc_read(codec, reg2) >> shift) & mask;
1705 ucontrol->value.integer.value[0] =
1706 max - ucontrol->value.integer.value[0];
1707 ucontrol->value.integer.value[1] =
1708 max - ucontrol->value.integer.value[1];
1713 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1716 * snd_soc_put_volsw_2r - double mixer set callback
1717 * @kcontrol: mixer control
1718 * @uinfo: control element information
1720 * Callback to set the value of a double mixer control that spans 2 registers.
1722 * Returns 0 for success.
1724 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
1727 struct soc_mixer_control *mc =
1728 (struct soc_mixer_control *)kcontrol->private_value;
1729 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1730 unsigned int reg = mc->reg;
1731 unsigned int reg2 = mc->rreg;
1732 unsigned int shift = mc->shift;
1734 unsigned int mask = (1 << fls(max)) - 1;
1735 unsigned int invert = mc->invert;
1737 unsigned short val, val2, val_mask;
1739 val_mask = mask << shift;
1740 val = (ucontrol->value.integer.value[0] & mask);
1741 val2 = (ucontrol->value.integer.value[1] & mask);
1749 val2 = val2 << shift;
1751 err = snd_soc_update_bits(codec, reg, val_mask, val);
1755 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1758 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1761 * snd_soc_info_volsw_s8 - signed mixer info callback
1762 * @kcontrol: mixer control
1763 * @uinfo: control element information
1765 * Callback to provide information about a signed mixer control.
1767 * Returns 0 for success.
1769 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1770 struct snd_ctl_elem_info *uinfo)
1772 struct soc_mixer_control *mc =
1773 (struct soc_mixer_control *)kcontrol->private_value;
1777 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1779 uinfo->value.integer.min = 0;
1780 uinfo->value.integer.max = max-min;
1783 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1786 * snd_soc_get_volsw_s8 - signed mixer get callback
1787 * @kcontrol: mixer control
1788 * @uinfo: control element information
1790 * Callback to get the value of a signed mixer control.
1792 * Returns 0 for success.
1794 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1795 struct snd_ctl_elem_value *ucontrol)
1797 struct soc_mixer_control *mc =
1798 (struct soc_mixer_control *)kcontrol->private_value;
1799 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1800 unsigned int reg = mc->reg;
1802 int val = snd_soc_read(codec, reg);
1804 ucontrol->value.integer.value[0] =
1805 ((signed char)(val & 0xff))-min;
1806 ucontrol->value.integer.value[1] =
1807 ((signed char)((val >> 8) & 0xff))-min;
1810 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1813 * snd_soc_put_volsw_sgn - signed mixer put callback
1814 * @kcontrol: mixer control
1815 * @uinfo: control element information
1817 * Callback to set the value of a signed mixer control.
1819 * Returns 0 for success.
1821 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
1824 struct soc_mixer_control *mc =
1825 (struct soc_mixer_control *)kcontrol->private_value;
1826 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1827 unsigned int reg = mc->reg;
1831 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1832 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1834 return snd_soc_update_bits(codec, reg, 0xffff, val);
1836 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1839 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1841 * @clk_id: DAI specific clock ID
1842 * @freq: new clock frequency in Hz
1843 * @dir: new clock direction - input/output.
1845 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1847 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1848 unsigned int freq, int dir)
1850 if (dai->ops.set_sysclk)
1851 return dai->ops.set_sysclk(dai, clk_id, freq, dir);
1855 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1858 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1860 * @clk_id: DAI specific clock divider ID
1861 * @div: new clock divisor.
1863 * Configures the clock dividers. This is used to derive the best DAI bit and
1864 * frame clocks from the system or master clock. It's best to set the DAI bit
1865 * and frame clocks as low as possible to save system power.
1867 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1868 int div_id, int div)
1870 if (dai->ops.set_clkdiv)
1871 return dai->ops.set_clkdiv(dai, div_id, div);
1875 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1878 * snd_soc_dai_set_pll - configure DAI PLL.
1880 * @pll_id: DAI specific PLL ID
1881 * @freq_in: PLL input clock frequency in Hz
1882 * @freq_out: requested PLL output clock frequency in Hz
1884 * Configures and enables PLL to generate output clock based on input clock.
1886 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1887 int pll_id, unsigned int freq_in, unsigned int freq_out)
1889 if (dai->ops.set_pll)
1890 return dai->ops.set_pll(dai, pll_id, freq_in, freq_out);
1894 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1897 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1899 * @fmt: SND_SOC_DAIFMT_ format value.
1901 * Configures the DAI hardware format and clocking.
1903 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1905 if (dai->ops.set_fmt)
1906 return dai->ops.set_fmt(dai, fmt);
1910 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1913 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1915 * @mask: DAI specific mask representing used slots.
1916 * @slots: Number of slots in use.
1918 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1921 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1922 unsigned int mask, int slots)
1924 if (dai->ops.set_sysclk)
1925 return dai->ops.set_tdm_slot(dai, mask, slots);
1929 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1932 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1934 * @tristate: tristate enable
1936 * Tristates the DAI so that others can use it.
1938 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1940 if (dai->ops.set_sysclk)
1941 return dai->ops.set_tristate(dai, tristate);
1945 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1948 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1950 * @mute: mute enable
1952 * Mutes the DAI DAC.
1954 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1956 if (dai->ops.digital_mute)
1957 return dai->ops.digital_mute(dai, mute);
1961 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1963 static int __devinit snd_soc_init(void)
1965 return platform_driver_register(&soc_driver);
1968 static void __exit snd_soc_exit(void)
1970 platform_driver_unregister(&soc_driver);
1973 module_init(snd_soc_init);
1974 module_exit(snd_soc_exit);
1976 /* Module information */
1977 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1978 MODULE_DESCRIPTION("ALSA SoC Core");
1979 MODULE_LICENSE("GPL");
1980 MODULE_ALIAS("platform:soc-audio");