[MTD] physmap: fix memory leak on physmap_flash_remove by using devres
[linux-2.6/mini2440.git] / sound / soc / soc-core.c
blob462e635dfc74bc084f2c140480c4e6fe8e8cd7cc
1 /*
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.
16 * TODO:
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>
27 #include <linux/pm.h>
28 #include <linux/bitops.h>
29 #include <linux/platform_device.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/initval.h>
37 /* debug */
38 #define SOC_DEBUG 0
39 #if SOC_DEBUG
40 #define dbg(format, arg...) printk(format, ## arg)
41 #else
42 #define dbg(format, arg...)
43 #endif
45 static DEFINE_MUTEX(pcm_mutex);
46 static DEFINE_MUTEX(io_mutex);
47 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50 * This is a timeout to do a DAPM powerdown after a stream is closed().
51 * It can be used to eliminate pops between different playback streams, e.g.
52 * between two audio tracks.
54 static int pmdown_time = 5000;
55 module_param(pmdown_time, int, 0);
56 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
59 * This function forces any delayed work to be queued and run.
61 static int run_delayed_work(struct delayed_work *dwork)
63 int ret;
65 /* cancel any work waiting to be queued. */
66 ret = cancel_delayed_work(dwork);
68 /* if there was any work waiting then we run it now and
69 * wait for it's completion */
70 if (ret) {
71 schedule_delayed_work(dwork, 0);
72 flush_scheduled_work();
74 return ret;
77 #ifdef CONFIG_SND_SOC_AC97_BUS
78 /* unregister ac97 codec */
79 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
81 if (codec->ac97->dev.bus)
82 device_unregister(&codec->ac97->dev);
83 return 0;
86 /* stop no dev release warning */
87 static void soc_ac97_device_release(struct device *dev){}
89 /* register ac97 codec to bus */
90 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
92 int err;
94 codec->ac97->dev.bus = &ac97_bus_type;
95 codec->ac97->dev.parent = NULL;
96 codec->ac97->dev.release = soc_ac97_device_release;
98 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
99 codec->card->number, 0, codec->name);
100 err = device_register(&codec->ac97->dev);
101 if (err < 0) {
102 snd_printk(KERN_ERR "Can't register ac97 bus\n");
103 codec->ac97->dev.bus = NULL;
104 return err;
106 return 0;
108 #endif
110 static inline const char *get_dai_name(int type)
112 switch (type) {
113 case SND_SOC_DAI_AC97_BUS:
114 case SND_SOC_DAI_AC97:
115 return "AC97";
116 case SND_SOC_DAI_I2S:
117 return "I2S";
118 case SND_SOC_DAI_PCM:
119 return "PCM";
121 return NULL;
125 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
126 * then initialized and any private data can be allocated. This also calls
127 * startup for the cpu DAI, platform, machine and codec DAI.
129 static int soc_pcm_open(struct snd_pcm_substream *substream)
131 struct snd_soc_pcm_runtime *rtd = substream->private_data;
132 struct snd_soc_device *socdev = rtd->socdev;
133 struct snd_pcm_runtime *runtime = substream->runtime;
134 struct snd_soc_dai_link *machine = rtd->dai;
135 struct snd_soc_platform *platform = socdev->platform;
136 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
137 struct snd_soc_dai *codec_dai = machine->codec_dai;
138 int ret = 0;
140 mutex_lock(&pcm_mutex);
142 /* startup the audio subsystem */
143 if (cpu_dai->ops.startup) {
144 ret = cpu_dai->ops.startup(substream);
145 if (ret < 0) {
146 printk(KERN_ERR "asoc: can't open interface %s\n",
147 cpu_dai->name);
148 goto out;
152 if (platform->pcm_ops->open) {
153 ret = platform->pcm_ops->open(substream);
154 if (ret < 0) {
155 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
156 goto platform_err;
160 if (codec_dai->ops.startup) {
161 ret = codec_dai->ops.startup(substream);
162 if (ret < 0) {
163 printk(KERN_ERR "asoc: can't open codec %s\n",
164 codec_dai->name);
165 goto codec_dai_err;
169 if (machine->ops && machine->ops->startup) {
170 ret = machine->ops->startup(substream);
171 if (ret < 0) {
172 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
173 goto machine_err;
177 /* Check that the codec and cpu DAI's are compatible */
178 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
179 runtime->hw.rate_min =
180 max(codec_dai->playback.rate_min,
181 cpu_dai->playback.rate_min);
182 runtime->hw.rate_max =
183 min(codec_dai->playback.rate_max,
184 cpu_dai->playback.rate_max);
185 runtime->hw.channels_min =
186 max(codec_dai->playback.channels_min,
187 cpu_dai->playback.channels_min);
188 runtime->hw.channels_max =
189 min(codec_dai->playback.channels_max,
190 cpu_dai->playback.channels_max);
191 runtime->hw.formats =
192 codec_dai->playback.formats & cpu_dai->playback.formats;
193 runtime->hw.rates =
194 codec_dai->playback.rates & cpu_dai->playback.rates;
195 } else {
196 runtime->hw.rate_min =
197 max(codec_dai->capture.rate_min,
198 cpu_dai->capture.rate_min);
199 runtime->hw.rate_max =
200 min(codec_dai->capture.rate_max,
201 cpu_dai->capture.rate_max);
202 runtime->hw.channels_min =
203 max(codec_dai->capture.channels_min,
204 cpu_dai->capture.channels_min);
205 runtime->hw.channels_max =
206 min(codec_dai->capture.channels_max,
207 cpu_dai->capture.channels_max);
208 runtime->hw.formats =
209 codec_dai->capture.formats & cpu_dai->capture.formats;
210 runtime->hw.rates =
211 codec_dai->capture.rates & cpu_dai->capture.rates;
214 snd_pcm_limit_hw_rates(runtime);
215 if (!runtime->hw.rates) {
216 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
217 codec_dai->name, cpu_dai->name);
218 goto machine_err;
220 if (!runtime->hw.formats) {
221 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
222 codec_dai->name, cpu_dai->name);
223 goto machine_err;
225 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
226 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
227 codec_dai->name, cpu_dai->name);
228 goto machine_err;
231 dbg("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
232 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
233 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
234 runtime->hw.channels_max);
235 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
236 runtime->hw.rate_max);
238 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
239 cpu_dai->playback.active = codec_dai->playback.active = 1;
240 else
241 cpu_dai->capture.active = codec_dai->capture.active = 1;
242 cpu_dai->active = codec_dai->active = 1;
243 cpu_dai->runtime = runtime;
244 socdev->codec->active++;
245 mutex_unlock(&pcm_mutex);
246 return 0;
248 machine_err:
249 if (machine->ops && machine->ops->shutdown)
250 machine->ops->shutdown(substream);
252 codec_dai_err:
253 if (platform->pcm_ops->close)
254 platform->pcm_ops->close(substream);
256 platform_err:
257 if (cpu_dai->ops.shutdown)
258 cpu_dai->ops.shutdown(substream);
259 out:
260 mutex_unlock(&pcm_mutex);
261 return ret;
265 * Power down the audio subsystem pmdown_time msecs after close is called.
266 * This is to ensure there are no pops or clicks in between any music tracks
267 * due to DAPM power cycling.
269 static void close_delayed_work(struct work_struct *work)
271 struct snd_soc_device *socdev =
272 container_of(work, struct snd_soc_device, delayed_work.work);
273 struct snd_soc_codec *codec = socdev->codec;
274 struct snd_soc_dai *codec_dai;
275 int i;
277 mutex_lock(&pcm_mutex);
278 for (i = 0; i < codec->num_dai; i++) {
279 codec_dai = &codec->dai[i];
281 dbg("pop wq checking: %s status: %s waiting: %s\n",
282 codec_dai->playback.stream_name,
283 codec_dai->playback.active ? "active" : "inactive",
284 codec_dai->pop_wait ? "yes" : "no");
286 /* are we waiting on this codec DAI stream */
287 if (codec_dai->pop_wait == 1) {
289 /* Reduce power if no longer active */
290 if (codec->active == 0) {
291 dbg("pop wq D1 %s %s\n", codec->name,
292 codec_dai->playback.stream_name);
293 snd_soc_dapm_set_bias_level(socdev,
294 SND_SOC_BIAS_PREPARE);
297 codec_dai->pop_wait = 0;
298 snd_soc_dapm_stream_event(codec,
299 codec_dai->playback.stream_name,
300 SND_SOC_DAPM_STREAM_STOP);
302 /* Fall into standby if no longer active */
303 if (codec->active == 0) {
304 dbg("pop wq D3 %s %s\n", codec->name,
305 codec_dai->playback.stream_name);
306 snd_soc_dapm_set_bias_level(socdev,
307 SND_SOC_BIAS_STANDBY);
311 mutex_unlock(&pcm_mutex);
315 * Called by ALSA when a PCM substream is closed. Private data can be
316 * freed here. The cpu DAI, codec DAI, machine and platform are also
317 * shutdown.
319 static int soc_codec_close(struct snd_pcm_substream *substream)
321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 struct snd_soc_device *socdev = rtd->socdev;
323 struct snd_soc_dai_link *machine = rtd->dai;
324 struct snd_soc_platform *platform = socdev->platform;
325 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
326 struct snd_soc_dai *codec_dai = machine->codec_dai;
327 struct snd_soc_codec *codec = socdev->codec;
329 mutex_lock(&pcm_mutex);
331 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
332 cpu_dai->playback.active = codec_dai->playback.active = 0;
333 else
334 cpu_dai->capture.active = codec_dai->capture.active = 0;
336 if (codec_dai->playback.active == 0 &&
337 codec_dai->capture.active == 0) {
338 cpu_dai->active = codec_dai->active = 0;
340 codec->active--;
342 /* Muting the DAC suppresses artifacts caused during digital
343 * shutdown, for example from stopping clocks.
345 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
346 snd_soc_dai_digital_mute(codec_dai, 1);
348 if (cpu_dai->ops.shutdown)
349 cpu_dai->ops.shutdown(substream);
351 if (codec_dai->ops.shutdown)
352 codec_dai->ops.shutdown(substream);
354 if (machine->ops && machine->ops->shutdown)
355 machine->ops->shutdown(substream);
357 if (platform->pcm_ops->close)
358 platform->pcm_ops->close(substream);
359 cpu_dai->runtime = NULL;
361 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
362 /* start delayed pop wq here for playback streams */
363 codec_dai->pop_wait = 1;
364 schedule_delayed_work(&socdev->delayed_work,
365 msecs_to_jiffies(pmdown_time));
366 } else {
367 /* capture streams can be powered down now */
368 snd_soc_dapm_stream_event(codec,
369 codec_dai->capture.stream_name,
370 SND_SOC_DAPM_STREAM_STOP);
372 if (codec->active == 0 && codec_dai->pop_wait == 0)
373 snd_soc_dapm_set_bias_level(socdev,
374 SND_SOC_BIAS_STANDBY);
377 mutex_unlock(&pcm_mutex);
378 return 0;
382 * Called by ALSA when the PCM substream is prepared, can set format, sample
383 * rate, etc. This function is non atomic and can be called multiple times,
384 * it can refer to the runtime info.
386 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
388 struct snd_soc_pcm_runtime *rtd = substream->private_data;
389 struct snd_soc_device *socdev = rtd->socdev;
390 struct snd_soc_dai_link *machine = rtd->dai;
391 struct snd_soc_platform *platform = socdev->platform;
392 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
393 struct snd_soc_dai *codec_dai = machine->codec_dai;
394 struct snd_soc_codec *codec = socdev->codec;
395 int ret = 0;
397 mutex_lock(&pcm_mutex);
399 if (machine->ops && machine->ops->prepare) {
400 ret = machine->ops->prepare(substream);
401 if (ret < 0) {
402 printk(KERN_ERR "asoc: machine prepare error\n");
403 goto out;
407 if (platform->pcm_ops->prepare) {
408 ret = platform->pcm_ops->prepare(substream);
409 if (ret < 0) {
410 printk(KERN_ERR "asoc: platform prepare error\n");
411 goto out;
415 if (codec_dai->ops.prepare) {
416 ret = codec_dai->ops.prepare(substream);
417 if (ret < 0) {
418 printk(KERN_ERR "asoc: codec DAI prepare error\n");
419 goto out;
423 if (cpu_dai->ops.prepare) {
424 ret = cpu_dai->ops.prepare(substream);
425 if (ret < 0) {
426 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
427 goto out;
431 /* we only want to start a DAPM playback stream if we are not waiting
432 * on an existing one stopping */
433 if (codec_dai->pop_wait) {
434 /* we are waiting for the delayed work to start */
435 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
436 snd_soc_dapm_stream_event(socdev->codec,
437 codec_dai->capture.stream_name,
438 SND_SOC_DAPM_STREAM_START);
439 else {
440 codec_dai->pop_wait = 0;
441 cancel_delayed_work(&socdev->delayed_work);
442 snd_soc_dai_digital_mute(codec_dai, 0);
444 } else {
445 /* no delayed work - do we need to power up codec */
446 if (codec->bias_level != SND_SOC_BIAS_ON) {
448 snd_soc_dapm_set_bias_level(socdev,
449 SND_SOC_BIAS_PREPARE);
451 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
452 snd_soc_dapm_stream_event(codec,
453 codec_dai->playback.stream_name,
454 SND_SOC_DAPM_STREAM_START);
455 else
456 snd_soc_dapm_stream_event(codec,
457 codec_dai->capture.stream_name,
458 SND_SOC_DAPM_STREAM_START);
460 snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
461 snd_soc_dai_digital_mute(codec_dai, 0);
463 } else {
464 /* codec already powered - power on widgets */
465 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
466 snd_soc_dapm_stream_event(codec,
467 codec_dai->playback.stream_name,
468 SND_SOC_DAPM_STREAM_START);
469 else
470 snd_soc_dapm_stream_event(codec,
471 codec_dai->capture.stream_name,
472 SND_SOC_DAPM_STREAM_START);
474 snd_soc_dai_digital_mute(codec_dai, 0);
478 out:
479 mutex_unlock(&pcm_mutex);
480 return ret;
484 * Called by ALSA when the hardware params are set by application. This
485 * function can also be called multiple times and can allocate buffers
486 * (using snd_pcm_lib_* ). It's non-atomic.
488 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
489 struct snd_pcm_hw_params *params)
491 struct snd_soc_pcm_runtime *rtd = substream->private_data;
492 struct snd_soc_device *socdev = rtd->socdev;
493 struct snd_soc_dai_link *machine = rtd->dai;
494 struct snd_soc_platform *platform = socdev->platform;
495 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
496 struct snd_soc_dai *codec_dai = machine->codec_dai;
497 int ret = 0;
499 mutex_lock(&pcm_mutex);
501 if (machine->ops && machine->ops->hw_params) {
502 ret = machine->ops->hw_params(substream, params);
503 if (ret < 0) {
504 printk(KERN_ERR "asoc: machine hw_params failed\n");
505 goto out;
509 if (codec_dai->ops.hw_params) {
510 ret = codec_dai->ops.hw_params(substream, params);
511 if (ret < 0) {
512 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
513 codec_dai->name);
514 goto codec_err;
518 if (cpu_dai->ops.hw_params) {
519 ret = cpu_dai->ops.hw_params(substream, params);
520 if (ret < 0) {
521 printk(KERN_ERR "asoc: interface %s hw params failed\n",
522 cpu_dai->name);
523 goto interface_err;
527 if (platform->pcm_ops->hw_params) {
528 ret = platform->pcm_ops->hw_params(substream, params);
529 if (ret < 0) {
530 printk(KERN_ERR "asoc: platform %s hw params failed\n",
531 platform->name);
532 goto platform_err;
536 out:
537 mutex_unlock(&pcm_mutex);
538 return ret;
540 platform_err:
541 if (cpu_dai->ops.hw_free)
542 cpu_dai->ops.hw_free(substream);
544 interface_err:
545 if (codec_dai->ops.hw_free)
546 codec_dai->ops.hw_free(substream);
548 codec_err:
549 if (machine->ops && machine->ops->hw_free)
550 machine->ops->hw_free(substream);
552 mutex_unlock(&pcm_mutex);
553 return ret;
557 * Free's resources allocated by hw_params, can be called multiple times
559 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
561 struct snd_soc_pcm_runtime *rtd = substream->private_data;
562 struct snd_soc_device *socdev = rtd->socdev;
563 struct snd_soc_dai_link *machine = rtd->dai;
564 struct snd_soc_platform *platform = socdev->platform;
565 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
566 struct snd_soc_dai *codec_dai = machine->codec_dai;
567 struct snd_soc_codec *codec = socdev->codec;
569 mutex_lock(&pcm_mutex);
571 /* apply codec digital mute */
572 if (!codec->active)
573 snd_soc_dai_digital_mute(codec_dai, 1);
575 /* free any machine hw params */
576 if (machine->ops && machine->ops->hw_free)
577 machine->ops->hw_free(substream);
579 /* free any DMA resources */
580 if (platform->pcm_ops->hw_free)
581 platform->pcm_ops->hw_free(substream);
583 /* now free hw params for the DAI's */
584 if (codec_dai->ops.hw_free)
585 codec_dai->ops.hw_free(substream);
587 if (cpu_dai->ops.hw_free)
588 cpu_dai->ops.hw_free(substream);
590 mutex_unlock(&pcm_mutex);
591 return 0;
594 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
596 struct snd_soc_pcm_runtime *rtd = substream->private_data;
597 struct snd_soc_device *socdev = rtd->socdev;
598 struct snd_soc_dai_link *machine = rtd->dai;
599 struct snd_soc_platform *platform = socdev->platform;
600 struct snd_soc_dai *cpu_dai = machine->cpu_dai;
601 struct snd_soc_dai *codec_dai = machine->codec_dai;
602 int ret;
604 if (codec_dai->ops.trigger) {
605 ret = codec_dai->ops.trigger(substream, cmd);
606 if (ret < 0)
607 return ret;
610 if (platform->pcm_ops->trigger) {
611 ret = platform->pcm_ops->trigger(substream, cmd);
612 if (ret < 0)
613 return ret;
616 if (cpu_dai->ops.trigger) {
617 ret = cpu_dai->ops.trigger(substream, cmd);
618 if (ret < 0)
619 return ret;
621 return 0;
624 /* ASoC PCM operations */
625 static struct snd_pcm_ops soc_pcm_ops = {
626 .open = soc_pcm_open,
627 .close = soc_codec_close,
628 .hw_params = soc_pcm_hw_params,
629 .hw_free = soc_pcm_hw_free,
630 .prepare = soc_pcm_prepare,
631 .trigger = soc_pcm_trigger,
634 #ifdef CONFIG_PM
635 /* powers down audio subsystem for suspend */
636 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
638 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
639 struct snd_soc_machine *machine = socdev->machine;
640 struct snd_soc_platform *platform = socdev->platform;
641 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
642 struct snd_soc_codec *codec = socdev->codec;
643 int i;
645 /* Due to the resume being scheduled into a workqueue we could
646 * suspend before that's finished - wait for it to complete.
648 snd_power_lock(codec->card);
649 snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
650 snd_power_unlock(codec->card);
652 /* we're going to block userspace touching us until resume completes */
653 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
655 /* mute any active DAC's */
656 for (i = 0; i < machine->num_links; i++) {
657 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
658 if (dai->dai_ops.digital_mute && dai->playback.active)
659 dai->dai_ops.digital_mute(dai, 1);
662 /* suspend all pcms */
663 for (i = 0; i < machine->num_links; i++)
664 snd_pcm_suspend_all(machine->dai_link[i].pcm);
666 if (machine->suspend_pre)
667 machine->suspend_pre(pdev, state);
669 for (i = 0; i < machine->num_links; i++) {
670 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
671 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
672 cpu_dai->suspend(pdev, cpu_dai);
673 if (platform->suspend)
674 platform->suspend(pdev, cpu_dai);
677 /* close any waiting streams and save state */
678 run_delayed_work(&socdev->delayed_work);
679 codec->suspend_bias_level = codec->bias_level;
681 for (i = 0; i < codec->num_dai; i++) {
682 char *stream = codec->dai[i].playback.stream_name;
683 if (stream != NULL)
684 snd_soc_dapm_stream_event(codec, stream,
685 SND_SOC_DAPM_STREAM_SUSPEND);
686 stream = codec->dai[i].capture.stream_name;
687 if (stream != NULL)
688 snd_soc_dapm_stream_event(codec, stream,
689 SND_SOC_DAPM_STREAM_SUSPEND);
692 if (codec_dev->suspend)
693 codec_dev->suspend(pdev, state);
695 for (i = 0; i < machine->num_links; i++) {
696 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
697 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
698 cpu_dai->suspend(pdev, cpu_dai);
701 if (machine->suspend_post)
702 machine->suspend_post(pdev, state);
704 return 0;
707 /* deferred resume work, so resume can complete before we finished
708 * setting our codec back up, which can be very slow on I2C
710 static void soc_resume_deferred(struct work_struct *work)
712 struct snd_soc_device *socdev = container_of(work,
713 struct snd_soc_device,
714 deferred_resume_work);
715 struct snd_soc_machine *machine = socdev->machine;
716 struct snd_soc_platform *platform = socdev->platform;
717 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
718 struct snd_soc_codec *codec = socdev->codec;
719 struct platform_device *pdev = to_platform_device(socdev->dev);
720 int i;
722 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
723 * so userspace apps are blocked from touching us
726 dev_info(socdev->dev, "starting resume work\n");
728 if (machine->resume_pre)
729 machine->resume_pre(pdev);
731 for (i = 0; i < machine->num_links; i++) {
732 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
733 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
734 cpu_dai->resume(pdev, cpu_dai);
737 if (codec_dev->resume)
738 codec_dev->resume(pdev);
740 for (i = 0; i < codec->num_dai; i++) {
741 char *stream = codec->dai[i].playback.stream_name;
742 if (stream != NULL)
743 snd_soc_dapm_stream_event(codec, stream,
744 SND_SOC_DAPM_STREAM_RESUME);
745 stream = codec->dai[i].capture.stream_name;
746 if (stream != NULL)
747 snd_soc_dapm_stream_event(codec, stream,
748 SND_SOC_DAPM_STREAM_RESUME);
751 /* unmute any active DACs */
752 for (i = 0; i < machine->num_links; i++) {
753 struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
754 if (dai->dai_ops.digital_mute && dai->playback.active)
755 dai->dai_ops.digital_mute(dai, 0);
758 for (i = 0; i < machine->num_links; i++) {
759 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
760 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
761 cpu_dai->resume(pdev, cpu_dai);
762 if (platform->resume)
763 platform->resume(pdev, cpu_dai);
766 if (machine->resume_post)
767 machine->resume_post(pdev);
769 dev_info(socdev->dev, "resume work completed\n");
771 /* userspace can access us now we are back as we were before */
772 snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
775 /* powers up audio subsystem after a suspend */
776 static int soc_resume(struct platform_device *pdev)
778 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
780 dev_info(socdev->dev, "scheduling resume work\n");
782 if (!schedule_work(&socdev->deferred_resume_work))
783 dev_err(socdev->dev, "work item may be lost\n");
785 return 0;
788 #else
789 #define soc_suspend NULL
790 #define soc_resume NULL
791 #endif
793 /* probes a new socdev */
794 static int soc_probe(struct platform_device *pdev)
796 int ret = 0, i;
797 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
798 struct snd_soc_machine *machine = socdev->machine;
799 struct snd_soc_platform *platform = socdev->platform;
800 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
802 if (machine->probe) {
803 ret = machine->probe(pdev);
804 if (ret < 0)
805 return ret;
808 for (i = 0; i < machine->num_links; i++) {
809 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
810 if (cpu_dai->probe) {
811 ret = cpu_dai->probe(pdev, cpu_dai);
812 if (ret < 0)
813 goto cpu_dai_err;
817 if (codec_dev->probe) {
818 ret = codec_dev->probe(pdev);
819 if (ret < 0)
820 goto cpu_dai_err;
823 if (platform->probe) {
824 ret = platform->probe(pdev);
825 if (ret < 0)
826 goto platform_err;
829 /* DAPM stream work */
830 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
831 #ifdef CONFIG_PM
832 /* deferred resume work */
833 INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
834 #endif
836 return 0;
838 platform_err:
839 if (codec_dev->remove)
840 codec_dev->remove(pdev);
842 cpu_dai_err:
843 for (i--; i >= 0; i--) {
844 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
845 if (cpu_dai->remove)
846 cpu_dai->remove(pdev, cpu_dai);
849 if (machine->remove)
850 machine->remove(pdev);
852 return ret;
855 /* removes a socdev */
856 static int soc_remove(struct platform_device *pdev)
858 int i;
859 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
860 struct snd_soc_machine *machine = socdev->machine;
861 struct snd_soc_platform *platform = socdev->platform;
862 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
864 run_delayed_work(&socdev->delayed_work);
866 if (platform->remove)
867 platform->remove(pdev);
869 if (codec_dev->remove)
870 codec_dev->remove(pdev);
872 for (i = 0; i < machine->num_links; i++) {
873 struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
874 if (cpu_dai->remove)
875 cpu_dai->remove(pdev, cpu_dai);
878 if (machine->remove)
879 machine->remove(pdev);
881 return 0;
884 /* ASoC platform driver */
885 static struct platform_driver soc_driver = {
886 .driver = {
887 .name = "soc-audio",
888 .owner = THIS_MODULE,
890 .probe = soc_probe,
891 .remove = soc_remove,
892 .suspend = soc_suspend,
893 .resume = soc_resume,
896 /* create a new pcm */
897 static int soc_new_pcm(struct snd_soc_device *socdev,
898 struct snd_soc_dai_link *dai_link, int num)
900 struct snd_soc_codec *codec = socdev->codec;
901 struct snd_soc_dai *codec_dai = dai_link->codec_dai;
902 struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
903 struct snd_soc_pcm_runtime *rtd;
904 struct snd_pcm *pcm;
905 char new_name[64];
906 int ret = 0, playback = 0, capture = 0;
908 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
909 if (rtd == NULL)
910 return -ENOMEM;
912 rtd->dai = dai_link;
913 rtd->socdev = socdev;
914 codec_dai->codec = socdev->codec;
916 /* check client and interface hw capabilities */
917 sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
918 get_dai_name(cpu_dai->type), num);
920 if (codec_dai->playback.channels_min)
921 playback = 1;
922 if (codec_dai->capture.channels_min)
923 capture = 1;
925 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
926 capture, &pcm);
927 if (ret < 0) {
928 printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
929 codec->name);
930 kfree(rtd);
931 return ret;
934 dai_link->pcm = pcm;
935 pcm->private_data = rtd;
936 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
937 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
938 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
939 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
940 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
941 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
942 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
944 if (playback)
945 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
947 if (capture)
948 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
950 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
951 if (ret < 0) {
952 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
953 kfree(rtd);
954 return ret;
957 pcm->private_free = socdev->platform->pcm_free;
958 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
959 cpu_dai->name);
960 return ret;
963 /* codec register dump */
964 static ssize_t codec_reg_show(struct device *dev,
965 struct device_attribute *attr, char *buf)
967 struct snd_soc_device *devdata = dev_get_drvdata(dev);
968 struct snd_soc_codec *codec = devdata->codec;
969 int i, step = 1, count = 0;
971 if (!codec->reg_cache_size)
972 return 0;
974 if (codec->reg_cache_step)
975 step = codec->reg_cache_step;
977 count += sprintf(buf, "%s registers\n", codec->name);
978 for (i = 0; i < codec->reg_cache_size; i += step) {
979 count += sprintf(buf + count, "%2x: ", i);
980 if (count >= PAGE_SIZE - 1)
981 break;
983 if (codec->display_register)
984 count += codec->display_register(codec, buf + count,
985 PAGE_SIZE - count, i);
986 else
987 count += snprintf(buf + count, PAGE_SIZE - count,
988 "%4x", codec->read(codec, i));
990 if (count >= PAGE_SIZE - 1)
991 break;
993 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
994 if (count >= PAGE_SIZE - 1)
995 break;
998 /* Truncate count; min() would cause a warning */
999 if (count >= PAGE_SIZE)
1000 count = PAGE_SIZE - 1;
1002 return count;
1004 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1007 * snd_soc_new_ac97_codec - initailise AC97 device
1008 * @codec: audio codec
1009 * @ops: AC97 bus operations
1010 * @num: AC97 codec number
1012 * Initialises AC97 codec resources for use by ad-hoc devices only.
1014 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1015 struct snd_ac97_bus_ops *ops, int num)
1017 mutex_lock(&codec->mutex);
1019 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1020 if (codec->ac97 == NULL) {
1021 mutex_unlock(&codec->mutex);
1022 return -ENOMEM;
1025 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1026 if (codec->ac97->bus == NULL) {
1027 kfree(codec->ac97);
1028 codec->ac97 = NULL;
1029 mutex_unlock(&codec->mutex);
1030 return -ENOMEM;
1033 codec->ac97->bus->ops = ops;
1034 codec->ac97->num = num;
1035 mutex_unlock(&codec->mutex);
1036 return 0;
1038 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1041 * snd_soc_free_ac97_codec - free AC97 codec device
1042 * @codec: audio codec
1044 * Frees AC97 codec device resources.
1046 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1048 mutex_lock(&codec->mutex);
1049 kfree(codec->ac97->bus);
1050 kfree(codec->ac97);
1051 codec->ac97 = NULL;
1052 mutex_unlock(&codec->mutex);
1054 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1057 * snd_soc_update_bits - update codec register bits
1058 * @codec: audio codec
1059 * @reg: codec register
1060 * @mask: register mask
1061 * @value: new value
1063 * Writes new register value.
1065 * Returns 1 for change else 0.
1067 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1068 unsigned short mask, unsigned short value)
1070 int change;
1071 unsigned short old, new;
1073 mutex_lock(&io_mutex);
1074 old = snd_soc_read(codec, reg);
1075 new = (old & ~mask) | value;
1076 change = old != new;
1077 if (change)
1078 snd_soc_write(codec, reg, new);
1080 mutex_unlock(&io_mutex);
1081 return change;
1083 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1086 * snd_soc_test_bits - test register for change
1087 * @codec: audio codec
1088 * @reg: codec register
1089 * @mask: register mask
1090 * @value: new value
1092 * Tests a register with a new value and checks if the new value is
1093 * different from the old value.
1095 * Returns 1 for change else 0.
1097 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1098 unsigned short mask, unsigned short value)
1100 int change;
1101 unsigned short old, new;
1103 mutex_lock(&io_mutex);
1104 old = snd_soc_read(codec, reg);
1105 new = (old & ~mask) | value;
1106 change = old != new;
1107 mutex_unlock(&io_mutex);
1109 return change;
1111 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1114 * snd_soc_new_pcms - create new sound card and pcms
1115 * @socdev: the SoC audio device
1117 * Create a new sound card based upon the codec and interface pcms.
1119 * Returns 0 for success, else error.
1121 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
1123 struct snd_soc_codec *codec = socdev->codec;
1124 struct snd_soc_machine *machine = socdev->machine;
1125 int ret = 0, i;
1127 mutex_lock(&codec->mutex);
1129 /* register a sound card */
1130 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1131 if (!codec->card) {
1132 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1133 codec->name);
1134 mutex_unlock(&codec->mutex);
1135 return -ENODEV;
1138 codec->card->dev = socdev->dev;
1139 codec->card->private_data = codec;
1140 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1142 /* create the pcms */
1143 for (i = 0; i < machine->num_links; i++) {
1144 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1145 if (ret < 0) {
1146 printk(KERN_ERR "asoc: can't create pcm %s\n",
1147 machine->dai_link[i].stream_name);
1148 mutex_unlock(&codec->mutex);
1149 return ret;
1153 mutex_unlock(&codec->mutex);
1154 return ret;
1156 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1159 * snd_soc_register_card - register sound card
1160 * @socdev: the SoC audio device
1162 * Register a SoC sound card. Also registers an AC97 device if the
1163 * codec is AC97 for ad hoc devices.
1165 * Returns 0 for success, else error.
1167 int snd_soc_register_card(struct snd_soc_device *socdev)
1169 struct snd_soc_codec *codec = socdev->codec;
1170 struct snd_soc_machine *machine = socdev->machine;
1171 int ret = 0, i, ac97 = 0, err = 0;
1173 for (i = 0; i < machine->num_links; i++) {
1174 if (socdev->machine->dai_link[i].init) {
1175 err = socdev->machine->dai_link[i].init(codec);
1176 if (err < 0) {
1177 printk(KERN_ERR "asoc: failed to init %s\n",
1178 socdev->machine->dai_link[i].stream_name);
1179 continue;
1182 if (socdev->machine->dai_link[i].codec_dai->type ==
1183 SND_SOC_DAI_AC97_BUS)
1184 ac97 = 1;
1186 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1187 "%s", machine->name);
1188 snprintf(codec->card->longname, sizeof(codec->card->longname),
1189 "%s (%s)", machine->name, codec->name);
1191 ret = snd_card_register(codec->card);
1192 if (ret < 0) {
1193 printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
1194 codec->name);
1195 goto out;
1198 mutex_lock(&codec->mutex);
1199 #ifdef CONFIG_SND_SOC_AC97_BUS
1200 if (ac97) {
1201 ret = soc_ac97_dev_register(codec);
1202 if (ret < 0) {
1203 printk(KERN_ERR "asoc: AC97 device register failed\n");
1204 snd_card_free(codec->card);
1205 mutex_unlock(&codec->mutex);
1206 goto out;
1209 #endif
1211 err = snd_soc_dapm_sys_add(socdev->dev);
1212 if (err < 0)
1213 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1215 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1216 if (err < 0)
1217 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1219 mutex_unlock(&codec->mutex);
1221 out:
1222 return ret;
1224 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1227 * snd_soc_free_pcms - free sound card and pcms
1228 * @socdev: the SoC audio device
1230 * Frees sound card and pcms associated with the socdev.
1231 * Also unregister the codec if it is an AC97 device.
1233 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1235 struct snd_soc_codec *codec = socdev->codec;
1236 #ifdef CONFIG_SND_SOC_AC97_BUS
1237 struct snd_soc_dai *codec_dai;
1238 int i;
1239 #endif
1241 mutex_lock(&codec->mutex);
1242 #ifdef CONFIG_SND_SOC_AC97_BUS
1243 for (i = 0; i < codec->num_dai; i++) {
1244 codec_dai = &codec->dai[i];
1245 if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
1246 soc_ac97_dev_unregister(codec);
1247 goto free_card;
1250 free_card:
1251 #endif
1253 if (codec->card)
1254 snd_card_free(codec->card);
1255 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1256 mutex_unlock(&codec->mutex);
1258 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1261 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1262 * @substream: the pcm substream
1263 * @hw: the hardware parameters
1265 * Sets the substream runtime hardware parameters.
1267 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1268 const struct snd_pcm_hardware *hw)
1270 struct snd_pcm_runtime *runtime = substream->runtime;
1271 runtime->hw.info = hw->info;
1272 runtime->hw.formats = hw->formats;
1273 runtime->hw.period_bytes_min = hw->period_bytes_min;
1274 runtime->hw.period_bytes_max = hw->period_bytes_max;
1275 runtime->hw.periods_min = hw->periods_min;
1276 runtime->hw.periods_max = hw->periods_max;
1277 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1278 runtime->hw.fifo_size = hw->fifo_size;
1279 return 0;
1281 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1284 * snd_soc_cnew - create new control
1285 * @_template: control template
1286 * @data: control private data
1287 * @lnng_name: control long name
1289 * Create a new mixer control from a template control.
1291 * Returns 0 for success, else error.
1293 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1294 void *data, char *long_name)
1296 struct snd_kcontrol_new template;
1298 memcpy(&template, _template, sizeof(template));
1299 if (long_name)
1300 template.name = long_name;
1301 template.index = 0;
1303 return snd_ctl_new1(&template, data);
1305 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1308 * snd_soc_info_enum_double - enumerated double mixer info callback
1309 * @kcontrol: mixer control
1310 * @uinfo: control element information
1312 * Callback to provide information about a double enumerated
1313 * mixer control.
1315 * Returns 0 for success.
1317 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1318 struct snd_ctl_elem_info *uinfo)
1320 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1322 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1323 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1324 uinfo->value.enumerated.items = e->max;
1326 if (uinfo->value.enumerated.item > e->max - 1)
1327 uinfo->value.enumerated.item = e->max - 1;
1328 strcpy(uinfo->value.enumerated.name,
1329 e->texts[uinfo->value.enumerated.item]);
1330 return 0;
1332 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1335 * snd_soc_get_enum_double - enumerated double mixer get callback
1336 * @kcontrol: mixer control
1337 * @uinfo: control element information
1339 * Callback to get the value of a double enumerated mixer.
1341 * Returns 0 for success.
1343 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1344 struct snd_ctl_elem_value *ucontrol)
1346 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1347 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1348 unsigned short val, bitmask;
1350 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1352 val = snd_soc_read(codec, e->reg);
1353 ucontrol->value.enumerated.item[0]
1354 = (val >> e->shift_l) & (bitmask - 1);
1355 if (e->shift_l != e->shift_r)
1356 ucontrol->value.enumerated.item[1] =
1357 (val >> e->shift_r) & (bitmask - 1);
1359 return 0;
1361 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1364 * snd_soc_put_enum_double - enumerated double mixer put callback
1365 * @kcontrol: mixer control
1366 * @uinfo: control element information
1368 * Callback to set the value of a double enumerated mixer.
1370 * Returns 0 for success.
1372 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1373 struct snd_ctl_elem_value *ucontrol)
1375 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1376 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1377 unsigned short val;
1378 unsigned short mask, bitmask;
1380 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1382 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1383 return -EINVAL;
1384 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1385 mask = (bitmask - 1) << e->shift_l;
1386 if (e->shift_l != e->shift_r) {
1387 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1388 return -EINVAL;
1389 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1390 mask |= (bitmask - 1) << e->shift_r;
1393 return snd_soc_update_bits(codec, e->reg, mask, val);
1395 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1398 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1399 * @kcontrol: mixer control
1400 * @uinfo: control element information
1402 * Callback to provide information about an external enumerated
1403 * single mixer.
1405 * Returns 0 for success.
1407 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_info *uinfo)
1410 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1412 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1413 uinfo->count = 1;
1414 uinfo->value.enumerated.items = e->max;
1416 if (uinfo->value.enumerated.item > e->max - 1)
1417 uinfo->value.enumerated.item = e->max - 1;
1418 strcpy(uinfo->value.enumerated.name,
1419 e->texts[uinfo->value.enumerated.item]);
1420 return 0;
1422 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1425 * snd_soc_info_volsw_ext - external single mixer info callback
1426 * @kcontrol: mixer control
1427 * @uinfo: control element information
1429 * Callback to provide information about a single external mixer control.
1431 * Returns 0 for success.
1433 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1434 struct snd_ctl_elem_info *uinfo)
1436 int max = kcontrol->private_value;
1438 if (max == 1)
1439 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1440 else
1441 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1443 uinfo->count = 1;
1444 uinfo->value.integer.min = 0;
1445 uinfo->value.integer.max = max;
1446 return 0;
1448 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1451 * snd_soc_info_volsw - single mixer info callback
1452 * @kcontrol: mixer control
1453 * @uinfo: control element information
1455 * Callback to provide information about a single mixer control.
1457 * Returns 0 for success.
1459 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1460 struct snd_ctl_elem_info *uinfo)
1462 struct soc_mixer_control *mc =
1463 (struct soc_mixer_control *)kcontrol->private_value;
1464 int max = mc->max;
1465 unsigned int shift = mc->min;
1466 unsigned int rshift = mc->rshift;
1468 if (max == 1)
1469 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1470 else
1471 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1473 uinfo->count = shift == rshift ? 1 : 2;
1474 uinfo->value.integer.min = 0;
1475 uinfo->value.integer.max = max;
1476 return 0;
1478 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1481 * snd_soc_get_volsw - single mixer get callback
1482 * @kcontrol: mixer control
1483 * @uinfo: control element information
1485 * Callback to get the value of a single mixer control.
1487 * Returns 0 for success.
1489 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1490 struct snd_ctl_elem_value *ucontrol)
1492 struct soc_mixer_control *mc =
1493 (struct soc_mixer_control *)kcontrol->private_value;
1494 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1495 unsigned int reg = mc->reg;
1496 unsigned int shift = mc->shift;
1497 unsigned int rshift = mc->rshift;
1498 int max = mc->max;
1499 unsigned int mask = (1 << fls(max)) - 1;
1500 unsigned int invert = mc->invert;
1502 ucontrol->value.integer.value[0] =
1503 (snd_soc_read(codec, reg) >> shift) & mask;
1504 if (shift != rshift)
1505 ucontrol->value.integer.value[1] =
1506 (snd_soc_read(codec, reg) >> rshift) & mask;
1507 if (invert) {
1508 ucontrol->value.integer.value[0] =
1509 max - ucontrol->value.integer.value[0];
1510 if (shift != rshift)
1511 ucontrol->value.integer.value[1] =
1512 max - ucontrol->value.integer.value[1];
1515 return 0;
1517 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1520 * snd_soc_put_volsw - single mixer put callback
1521 * @kcontrol: mixer control
1522 * @uinfo: control element information
1524 * Callback to set the value of a single mixer control.
1526 * Returns 0 for success.
1528 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1529 struct snd_ctl_elem_value *ucontrol)
1531 struct soc_mixer_control *mc =
1532 (struct soc_mixer_control *)kcontrol->private_value;
1533 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1534 unsigned int reg = mc->reg;
1535 unsigned int shift = mc->shift;
1536 unsigned int rshift = mc->rshift;
1537 int max = mc->max;
1538 unsigned int mask = (1 << fls(max)) - 1;
1539 unsigned int invert = mc->invert;
1540 unsigned short val, val2, val_mask;
1542 val = (ucontrol->value.integer.value[0] & mask);
1543 if (invert)
1544 val = max - val;
1545 val_mask = mask << shift;
1546 val = val << shift;
1547 if (shift != rshift) {
1548 val2 = (ucontrol->value.integer.value[1] & mask);
1549 if (invert)
1550 val2 = max - val2;
1551 val_mask |= mask << rshift;
1552 val |= val2 << rshift;
1554 return snd_soc_update_bits(codec, reg, val_mask, val);
1556 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1559 * snd_soc_info_volsw_2r - double mixer info callback
1560 * @kcontrol: mixer control
1561 * @uinfo: control element information
1563 * Callback to provide information about a double mixer control that
1564 * spans 2 codec registers.
1566 * Returns 0 for success.
1568 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_info *uinfo)
1571 struct soc_mixer_control *mc =
1572 (struct soc_mixer_control *)kcontrol->private_value;
1573 int max = mc->max;
1575 if (max == 1)
1576 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1577 else
1578 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1580 uinfo->count = 2;
1581 uinfo->value.integer.min = 0;
1582 uinfo->value.integer.max = max;
1583 return 0;
1585 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1588 * snd_soc_get_volsw_2r - double mixer get callback
1589 * @kcontrol: mixer control
1590 * @uinfo: control element information
1592 * Callback to get the value of a double mixer control that spans 2 registers.
1594 * Returns 0 for success.
1596 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1597 struct snd_ctl_elem_value *ucontrol)
1599 struct soc_mixer_control *mc =
1600 (struct soc_mixer_control *)kcontrol->private_value;
1601 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1602 unsigned int reg = mc->reg;
1603 unsigned int reg2 = mc->rreg;
1604 unsigned int shift = mc->shift;
1605 int max = mc->max;
1606 unsigned int mask = (1<<fls(max))-1;
1607 unsigned int invert = mc->invert;
1609 ucontrol->value.integer.value[0] =
1610 (snd_soc_read(codec, reg) >> shift) & mask;
1611 ucontrol->value.integer.value[1] =
1612 (snd_soc_read(codec, reg2) >> shift) & mask;
1613 if (invert) {
1614 ucontrol->value.integer.value[0] =
1615 max - ucontrol->value.integer.value[0];
1616 ucontrol->value.integer.value[1] =
1617 max - ucontrol->value.integer.value[1];
1620 return 0;
1622 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1625 * snd_soc_put_volsw_2r - double mixer set callback
1626 * @kcontrol: mixer control
1627 * @uinfo: control element information
1629 * Callback to set the value of a double mixer control that spans 2 registers.
1631 * Returns 0 for success.
1633 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
1634 struct snd_ctl_elem_value *ucontrol)
1636 struct soc_mixer_control *mc =
1637 (struct soc_mixer_control *)kcontrol->private_value;
1638 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1639 unsigned int reg = mc->reg;
1640 unsigned int reg2 = mc->rreg;
1641 unsigned int shift = mc->shift;
1642 int max = mc->max;
1643 unsigned int mask = (1 << fls(max)) - 1;
1644 unsigned int invert = mc->invert;
1645 int err;
1646 unsigned short val, val2, val_mask;
1648 val_mask = mask << shift;
1649 val = (ucontrol->value.integer.value[0] & mask);
1650 val2 = (ucontrol->value.integer.value[1] & mask);
1652 if (invert) {
1653 val = max - val;
1654 val2 = max - val2;
1657 val = val << shift;
1658 val2 = val2 << shift;
1660 err = snd_soc_update_bits(codec, reg, val_mask, val);
1661 if (err < 0)
1662 return err;
1664 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
1665 return err;
1667 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
1670 * snd_soc_info_volsw_s8 - signed mixer info callback
1671 * @kcontrol: mixer control
1672 * @uinfo: control element information
1674 * Callback to provide information about a signed mixer control.
1676 * Returns 0 for success.
1678 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_info *uinfo)
1681 struct soc_mixer_control *mc =
1682 (struct soc_mixer_control *)kcontrol->private_value;
1683 int max = mc->max;
1684 int min = mc->min;
1686 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1687 uinfo->count = 2;
1688 uinfo->value.integer.min = 0;
1689 uinfo->value.integer.max = max-min;
1690 return 0;
1692 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
1695 * snd_soc_get_volsw_s8 - signed mixer get callback
1696 * @kcontrol: mixer control
1697 * @uinfo: control element information
1699 * Callback to get the value of a signed mixer control.
1701 * Returns 0 for success.
1703 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_value *ucontrol)
1706 struct soc_mixer_control *mc =
1707 (struct soc_mixer_control *)kcontrol->private_value;
1708 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1709 unsigned int reg = mc->reg;
1710 int min = mc->min;
1711 int val = snd_soc_read(codec, reg);
1713 ucontrol->value.integer.value[0] =
1714 ((signed char)(val & 0xff))-min;
1715 ucontrol->value.integer.value[1] =
1716 ((signed char)((val >> 8) & 0xff))-min;
1717 return 0;
1719 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
1722 * snd_soc_put_volsw_sgn - signed mixer put callback
1723 * @kcontrol: mixer control
1724 * @uinfo: control element information
1726 * Callback to set the value of a signed mixer control.
1728 * Returns 0 for success.
1730 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1733 struct soc_mixer_control *mc =
1734 (struct soc_mixer_control *)kcontrol->private_value;
1735 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1736 unsigned int reg = mc->reg;
1737 int min = mc->min;
1738 unsigned short val;
1740 val = (ucontrol->value.integer.value[0]+min) & 0xff;
1741 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
1743 return snd_soc_update_bits(codec, reg, 0xffff, val);
1745 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
1748 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
1749 * @dai: DAI
1750 * @clk_id: DAI specific clock ID
1751 * @freq: new clock frequency in Hz
1752 * @dir: new clock direction - input/output.
1754 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
1756 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1757 unsigned int freq, int dir)
1759 if (dai->dai_ops.set_sysclk)
1760 return dai->dai_ops.set_sysclk(dai, clk_id, freq, dir);
1761 else
1762 return -EINVAL;
1764 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
1767 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
1768 * @dai: DAI
1769 * @clk_id: DAI specific clock divider ID
1770 * @div: new clock divisor.
1772 * Configures the clock dividers. This is used to derive the best DAI bit and
1773 * frame clocks from the system or master clock. It's best to set the DAI bit
1774 * and frame clocks as low as possible to save system power.
1776 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
1777 int div_id, int div)
1779 if (dai->dai_ops.set_clkdiv)
1780 return dai->dai_ops.set_clkdiv(dai, div_id, div);
1781 else
1782 return -EINVAL;
1784 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
1787 * snd_soc_dai_set_pll - configure DAI PLL.
1788 * @dai: DAI
1789 * @pll_id: DAI specific PLL ID
1790 * @freq_in: PLL input clock frequency in Hz
1791 * @freq_out: requested PLL output clock frequency in Hz
1793 * Configures and enables PLL to generate output clock based on input clock.
1795 int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
1796 int pll_id, unsigned int freq_in, unsigned int freq_out)
1798 if (dai->dai_ops.set_pll)
1799 return dai->dai_ops.set_pll(dai, pll_id, freq_in, freq_out);
1800 else
1801 return -EINVAL;
1803 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
1806 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
1807 * @dai: DAI
1808 * @clk_id: DAI specific clock ID
1809 * @fmt: SND_SOC_DAIFMT_ format value.
1811 * Configures the DAI hardware format and clocking.
1813 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1815 if (dai->dai_ops.set_fmt)
1816 return dai->dai_ops.set_fmt(dai, fmt);
1817 else
1818 return -EINVAL;
1820 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
1823 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
1824 * @dai: DAI
1825 * @mask: DAI specific mask representing used slots.
1826 * @slots: Number of slots in use.
1828 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
1829 * specific.
1831 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
1832 unsigned int mask, int slots)
1834 if (dai->dai_ops.set_sysclk)
1835 return dai->dai_ops.set_tdm_slot(dai, mask, slots);
1836 else
1837 return -EINVAL;
1839 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
1842 * snd_soc_dai_set_tristate - configure DAI system or master clock.
1843 * @dai: DAI
1844 * @tristate: tristate enable
1846 * Tristates the DAI so that others can use it.
1848 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
1850 if (dai->dai_ops.set_sysclk)
1851 return dai->dai_ops.set_tristate(dai, tristate);
1852 else
1853 return -EINVAL;
1855 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
1858 * snd_soc_dai_digital_mute - configure DAI system or master clock.
1859 * @dai: DAI
1860 * @mute: mute enable
1862 * Mutes the DAI DAC.
1864 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
1866 if (dai->dai_ops.digital_mute)
1867 return dai->dai_ops.digital_mute(dai, mute);
1868 else
1869 return -EINVAL;
1871 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
1873 static int __devinit snd_soc_init(void)
1875 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
1876 return platform_driver_register(&soc_driver);
1879 static void snd_soc_exit(void)
1881 platform_driver_unregister(&soc_driver);
1884 module_init(snd_soc_init);
1885 module_exit(snd_soc_exit);
1887 /* Module information */
1888 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
1889 MODULE_DESCRIPTION("ALSA SoC Core");
1890 MODULE_LICENSE("GPL");
1891 MODULE_ALIAS("platform:soc-audio");