2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Liam Girdwood
8 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
9 * with code, comments and ideas from :-
10 * Richard Purdie <richard@openedhand.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 * 12th Aug 2005 Initial version.
19 * 25th Oct 2005 Working Codec, Interface and Platform registration.
22 * o Add hw rules to enforce rates, etc.
23 * o More testing with other codecs/machines.
24 * o Add more codecs and platforms to ensure good API coverage.
25 * o Support TDM on PCM and I2S
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
33 #include <linux/bitops.h>
34 #include <linux/platform_device.h>
35 #include <sound/driver.h>
36 #include <sound/core.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/soc.h>
40 #include <sound/soc-dapm.h>
41 #include <sound/initval.h>
46 #define dbg(format, arg...) printk(format, ## arg)
48 #define dbg(format, arg...)
50 /* debug DAI capabilities matching */
51 #define SOC_DEBUG_DAI 0
53 #define dbgc(format, arg...) printk(format, ## arg)
55 #define dbgc(format, arg...)
58 #define CODEC_CPU(codec, cpu) ((codec << 4) | cpu)
60 static DEFINE_MUTEX(pcm_mutex);
61 static DEFINE_MUTEX(io_mutex);
62 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
64 /* supported sample rates */
65 /* ATTENTION: these values depend on the definition in pcm.h! */
66 static const unsigned int rates[] = {
67 5512, 8000, 11025, 16000, 22050, 32000, 44100,
68 48000, 64000, 88200, 96000, 176400, 192000
72 * This is a timeout to do a DAPM powerdown after a stream is closed().
73 * It can be used to eliminate pops between different playback streams, e.g.
74 * between two audio tracks.
76 static int pmdown_time = 5000;
77 module_param(pmdown_time, int, 0);
78 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
80 #ifdef CONFIG_SND_SOC_AC97_BUS
81 /* unregister ac97 codec */
82 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
84 if (codec->ac97->dev.bus)
85 device_unregister(&codec->ac97->dev);
89 /* stop no dev release warning */
90 static void soc_ac97_device_release(struct device *dev){}
92 /* register ac97 codec to bus */
93 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
97 codec->ac97->dev.bus = &ac97_bus_type;
98 codec->ac97->dev.parent = NULL;
99 codec->ac97->dev.release = soc_ac97_device_release;
101 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
102 codec->card->number, 0, codec->name);
103 err = device_register(&codec->ac97->dev);
105 snd_printk(KERN_ERR "Can't register ac97 bus\n");
106 codec->ac97->dev.bus = NULL;
113 static inline const char* get_dai_name(int type)
116 case SND_SOC_DAI_AC97:
118 case SND_SOC_DAI_I2S:
120 case SND_SOC_DAI_PCM:
126 /* get rate format from rate */
127 static inline int soc_get_rate_format(int rate)
131 for (i = 0; i < ARRAY_SIZE(rates); i++) {
132 if (rates[i] == rate)
138 /* gets the audio system mclk/sysclk for the given parameters */
139 static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
140 struct snd_soc_clock_info *info)
142 struct snd_soc_device *socdev = rtd->socdev;
143 struct snd_soc_machine *machine = socdev->machine;
146 /* find the matching machine config and get it's mclk for the given
147 * sample rate and hardware format */
148 for(i = 0; i < machine->num_links; i++) {
149 if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
150 machine->dai_link[i].config_sysclk)
151 return machine->dai_link[i].config_sysclk(rtd, info);
156 /* changes a bitclk multiplier mask to a divider mask */
157 static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
158 unsigned int pcmfmt, unsigned int chn)
162 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
167 /* the minimum bit clock that has enough bandwidth */
168 min = size * rate * chn;
169 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
171 for (i = 0; i < 64; i++) {
172 if ((bfs >> i) & 0x1) {
174 bfs_ |= SND_SOC_FSBD(mclk/j);
175 dbgc("rcw --> div support mult %d\n",
176 SND_SOC_FSBD_REAL(1<<i));
183 /* changes a bitclk divider mask to a multiplier mask */
184 static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
185 unsigned int pcmfmt, unsigned int chn)
190 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
195 /* the minimum bit clock that has enough bandwidth */
196 min = size * rate * chn;
197 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
199 for (i = 0; i < 64; i++) {
200 if ((bfs >> i) & 0x1) {
203 bfs_ |= SND_SOC_FSBW(j/min);
204 dbgc("div --> rcw support div %d\n",
205 SND_SOC_FSBW_REAL(1<<i));
213 /* changes a constant bitclk to a multiplier mask */
214 static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
215 unsigned int pcmfmt, unsigned int chn)
217 unsigned int bfs_ = rate * bfs;
218 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
223 /* the minimum bit clock that has enough bandwidth */
224 min = size * rate * chn;
225 dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
230 bfs_ = SND_SOC_FSBW(bfs_/min);
231 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
236 /* changes a bitclk multiplier mask to a divider mask */
237 static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
238 unsigned int pcmfmt, unsigned int chn)
240 unsigned int bfs_ = rate * bfs;
241 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
246 /* the minimum bit clock that has enough bandwidth */
247 min = size * rate * chn;
248 dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
253 bfs_ = SND_SOC_FSBW(mclk/bfs_);
254 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
259 /* Matches codec DAI and SoC CPU DAI hardware parameters */
260 static int soc_hw_match_params(struct snd_pcm_substream *substream,
261 struct snd_pcm_hw_params *params)
263 struct snd_soc_pcm_runtime *rtd = substream->private_data;
264 struct snd_soc_dai_mode *codec_dai_mode = NULL;
265 struct snd_soc_dai_mode *cpu_dai_mode = NULL;
266 struct snd_soc_clock_info clk_info;
267 unsigned int fs, mclk, rate = params_rate(params),
268 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
270 u64 codec_bfs, cpu_bfs;
272 dbg("asoc: match version %s\n", SND_SOC_VERSION);
273 clk_info.rate = rate;
274 pcmrate = soc_get_rate_format(rate);
276 /* try and find a match from the codec and cpu DAI capabilities */
277 for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
278 for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
279 codec_dai_mode = &rtd->codec_dai->caps.mode[j];
280 cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
282 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
284 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
288 fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
289 if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
290 dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
294 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
295 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
300 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
301 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
305 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
306 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
310 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
311 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
315 /* todo - still need to add tdm selection */
316 rtd->cpu_dai->dai_runtime.fmt =
317 rtd->codec_dai->dai_runtime.fmt =
318 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
319 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
320 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
321 clk_info.bclk_master =
322 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
324 /* make sure the ratio between rate and master
325 * clock is acceptable*/
326 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
328 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
331 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
332 rtd->codec_dai->dai_runtime.fs = fs;
334 /* calculate audio system clocking using slowest clocks possible*/
335 mclk = soc_get_mclk(rtd, &clk_info);
337 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
338 dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
339 clk_info.bclk_master);
343 /* calculate word size (per channel) and frame size */
344 rtd->codec_dai->dai_runtime.pcmfmt =
345 rtd->cpu_dai->dai_runtime.pcmfmt =
346 1 << params_format(params);
348 chn = params_channels(params);
349 /* i2s always has left and right */
350 if (params_channels(params) == 1 &&
351 rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
352 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
355 /* Calculate bfs - the ratio between bitclock and the sample rate
356 * We must take into consideration the dividers and multipliers
357 * used in the codec and cpu DAI modes. We always choose the
358 * lowest possible clocks to reduce power.
360 switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
361 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
362 /* cpu & codec bfs dividers */
363 rtd->cpu_dai->dai_runtime.bfs =
364 rtd->codec_dai->dai_runtime.bfs =
365 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
367 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
368 /* normalise bfs codec divider & cpu rcw mult */
369 codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
370 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
371 rtd->cpu_dai->dai_runtime.bfs =
372 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
373 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
374 rtd->codec_dai->dai_runtime.pcmfmt, chn);
375 rtd->codec_dai->dai_runtime.bfs =
376 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
378 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
379 /* normalise bfs codec rcw mult & cpu divider */
380 codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
381 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
382 rtd->cpu_dai->dai_runtime.bfs =
383 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
384 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
385 rtd->codec_dai->dai_runtime.pcmfmt, chn);
386 rtd->codec_dai->dai_runtime.bfs =
387 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
389 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
390 /* codec & cpu bfs rate rcw multipliers */
391 rtd->cpu_dai->dai_runtime.bfs =
392 rtd->codec_dai->dai_runtime.bfs =
393 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
395 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
396 /* normalise cpu bfs rate const multiplier & codec div */
397 cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
398 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
399 if(codec_dai_mode->bfs & cpu_bfs) {
400 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
401 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
403 rtd->cpu_dai->dai_runtime.bfs = 0;
405 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
406 /* normalise cpu bfs rate const multiplier & codec rcw mult */
407 cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
408 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
409 if(codec_dai_mode->bfs & cpu_bfs) {
410 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
411 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
413 rtd->cpu_dai->dai_runtime.bfs = 0;
415 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
416 /* normalise cpu bfs rate rcw multiplier & codec const mult */
417 codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
418 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
419 if(cpu_dai_mode->bfs & codec_bfs) {
420 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
421 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
423 rtd->cpu_dai->dai_runtime.bfs = 0;
425 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
426 /* normalise cpu bfs div & codec const mult */
427 codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
428 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
429 if(cpu_dai_mode->bfs & codec_bfs) {
430 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
431 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
433 rtd->cpu_dai->dai_runtime.bfs = 0;
435 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
436 /* cpu & codec constant mult */
437 if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
438 rtd->cpu_dai->dai_runtime.bfs =
439 rtd->codec_dai->dai_runtime.bfs =
442 rtd->cpu_dai->dai_runtime.bfs =
443 rtd->codec_dai->dai_runtime.bfs = 0;
447 /* make sure the bit clock speed is acceptable */
448 if (!rtd->cpu_dai->dai_runtime.bfs ||
449 !rtd->codec_dai->dai_runtime.bfs) {
450 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
451 dbgc("asoc: cpu_dai %llu codec %llu\n",
452 rtd->cpu_dai->dai_runtime.bfs,
453 rtd->codec_dai->dai_runtime.bfs);
454 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
461 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
465 /* we have matching DAI's, so complete the runtime info */
466 rtd->codec_dai->dai_runtime.pcmrate =
467 rtd->cpu_dai->dai_runtime.pcmrate =
468 soc_get_rate_format(rate);
470 rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
471 rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
472 rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
473 rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
476 dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
477 if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
478 codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
479 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
480 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
481 rtd->codec_dai->dai_runtime.fs, mclk,
482 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
483 } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
484 codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
485 dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
486 rtd->codec_dai->dai_runtime.fs, mclk,
487 rtd->codec_dai->dai_runtime.bfs, codec_bclk);
488 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
489 codec_bclk = params_rate(params) * params_channels(params) *
490 snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
491 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
492 dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
493 rtd->codec_dai->dai_runtime.fs, mclk,
494 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
498 if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
499 cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
500 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
501 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
502 rtd->cpu_dai->dai_runtime.fs, mclk,
503 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
504 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
505 cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
506 dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
507 rtd->cpu_dai->dai_runtime.fs, mclk,
508 rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
509 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
510 cpu_bclk = params_rate(params) * params_channels(params) *
511 snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
512 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
513 dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
514 rtd->cpu_dai->dai_runtime.fs, mclk,
515 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
520 * Check we have matching bitclocks. If we don't then it means the
521 * sysclock returned by either the codec or cpu DAI (selected by the
522 * machine sysclock function) is wrong compared with the supported DAI
523 * modes for the codec or cpu DAI. Check your codec or CPU DAI
524 * config_sysclock() functions.
526 if (cpu_bclk != codec_bclk && cpu_bclk){
528 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
530 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
533 switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
534 case SND_SOC_DAIFMT_CBM_CFM:
535 dbg("asoc: DAI codec BCLK master, LRC master\n");
537 case SND_SOC_DAIFMT_CBS_CFM:
538 dbg("asoc: DAI codec BCLK slave, LRC master\n");
540 case SND_SOC_DAIFMT_CBM_CFS:
541 dbg("asoc: DAI codec BCLK master, LRC slave\n");
543 case SND_SOC_DAIFMT_CBS_CFS:
544 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
547 dbg("asoc: mode %x, invert %x\n",
548 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
549 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
550 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
551 params_channels(params), params_format(params));
556 static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
561 for(i = 0; i < nmodes; i++)
562 rates |= modes[i].pcmrate;
567 static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
572 for(i = 0; i < nmodes; i++)
573 formats |= modes[i].pcmfmt;
579 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
580 * then initialized and any private data can be allocated. This also calls
581 * startup for the cpu DAI, platform, machine and codec DAI.
583 static int soc_pcm_open(struct snd_pcm_substream *substream)
585 struct snd_soc_pcm_runtime *rtd = substream->private_data;
586 struct snd_soc_device *socdev = rtd->socdev;
587 struct snd_pcm_runtime *runtime = substream->runtime;
588 struct snd_soc_machine *machine = socdev->machine;
589 struct snd_soc_platform *platform = socdev->platform;
590 struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
591 struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
594 mutex_lock(&pcm_mutex);
596 /* startup the audio subsystem */
597 if (rtd->cpu_dai->ops.startup) {
598 ret = rtd->cpu_dai->ops.startup(substream);
600 printk(KERN_ERR "asoc: can't open interface %s\n",
606 if (platform->pcm_ops->open) {
607 ret = platform->pcm_ops->open(substream);
609 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
614 if (machine->ops && machine->ops->startup) {
615 ret = machine->ops->startup(substream);
617 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
622 if (rtd->codec_dai->ops.startup) {
623 ret = rtd->codec_dai->ops.startup(substream);
625 printk(KERN_ERR "asoc: can't open codec %s\n",
626 rtd->codec_dai->name);
631 /* create runtime params from DMA, codec and cpu DAI */
632 if (runtime->hw.rates)
634 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
635 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
638 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
639 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
640 if (runtime->hw.formats)
641 runtime->hw.formats &=
642 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
643 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
645 runtime->hw.formats =
646 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
647 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
649 /* Check that the codec and cpu DAI's are compatible */
650 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
651 runtime->hw.rate_min =
652 max(rtd->codec_dai->playback.rate_min,
653 rtd->cpu_dai->playback.rate_min);
654 runtime->hw.rate_max =
655 min(rtd->codec_dai->playback.rate_max,
656 rtd->cpu_dai->playback.rate_max);
657 runtime->hw.channels_min =
658 max(rtd->codec_dai->playback.channels_min,
659 rtd->cpu_dai->playback.channels_min);
660 runtime->hw.channels_max =
661 min(rtd->codec_dai->playback.channels_max,
662 rtd->cpu_dai->playback.channels_max);
664 runtime->hw.rate_min =
665 max(rtd->codec_dai->capture.rate_min,
666 rtd->cpu_dai->capture.rate_min);
667 runtime->hw.rate_max =
668 min(rtd->codec_dai->capture.rate_max,
669 rtd->cpu_dai->capture.rate_max);
670 runtime->hw.channels_min =
671 max(rtd->codec_dai->capture.channels_min,
672 rtd->cpu_dai->capture.channels_min);
673 runtime->hw.channels_max =
674 min(rtd->codec_dai->capture.channels_max,
675 rtd->cpu_dai->capture.channels_max);
678 snd_pcm_limit_hw_rates(runtime);
679 if (!runtime->hw.rates) {
680 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
681 rtd->codec_dai->name, rtd->cpu_dai->name);
684 if (!runtime->hw.formats) {
685 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
686 rtd->codec_dai->name, rtd->cpu_dai->name);
689 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
690 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
691 rtd->codec_dai->name, rtd->cpu_dai->name);
695 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
696 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
697 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
698 runtime->hw.channels_max);
699 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
700 runtime->hw.rate_max);
703 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
704 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
706 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
707 rtd->cpu_dai->active = rtd->codec_dai->active = 1;
708 rtd->cpu_dai->runtime = runtime;
709 socdev->codec->active++;
710 mutex_unlock(&pcm_mutex);
714 if (machine->ops && machine->ops->shutdown)
715 machine->ops->shutdown(substream);
718 if (platform->pcm_ops->close)
719 platform->pcm_ops->close(substream);
722 if (rtd->cpu_dai->ops.shutdown)
723 rtd->cpu_dai->ops.shutdown(substream);
725 mutex_unlock(&pcm_mutex);
730 * Power down the audio subsytem pmdown_time msecs after close is called.
731 * This is to ensure there are no pops or clicks in between any music tracks
732 * due to DAPM power cycling.
734 static void close_delayed_work(struct work_struct *work)
736 struct snd_soc_device *socdev =
737 container_of(work, struct snd_soc_device, delayed_work.work);
738 struct snd_soc_codec *codec = socdev->codec;
739 struct snd_soc_codec_dai *codec_dai;
742 mutex_lock(&pcm_mutex);
743 for(i = 0; i < codec->num_dai; i++) {
744 codec_dai = &codec->dai[i];
746 dbg("pop wq checking: %s status: %s waiting: %s\n",
747 codec_dai->playback.stream_name,
748 codec_dai->playback.active ? "active" : "inactive",
749 codec_dai->pop_wait ? "yes" : "no");
751 /* are we waiting on this codec DAI stream */
752 if (codec_dai->pop_wait == 1) {
754 codec_dai->pop_wait = 0;
755 snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
756 SND_SOC_DAPM_STREAM_STOP);
758 /* power down the codec power domain if no longer active */
759 if (codec->active == 0) {
760 dbg("pop wq D3 %s %s\n", codec->name,
761 codec_dai->playback.stream_name);
762 if (codec->dapm_event)
763 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
767 mutex_unlock(&pcm_mutex);
771 * Called by ALSA when a PCM substream is closed. Private data can be
772 * freed here. The cpu DAI, codec DAI, machine and platform are also
775 static int soc_codec_close(struct snd_pcm_substream *substream)
777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
778 struct snd_soc_device *socdev = rtd->socdev;
779 struct snd_soc_machine *machine = socdev->machine;
780 struct snd_soc_platform *platform = socdev->platform;
781 struct snd_soc_codec *codec = socdev->codec;
783 mutex_lock(&pcm_mutex);
785 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
786 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
788 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
790 if (rtd->codec_dai->playback.active == 0 &&
791 rtd->codec_dai->capture.active == 0) {
792 rtd->cpu_dai->active = rtd->codec_dai->active = 0;
796 if (rtd->cpu_dai->ops.shutdown)
797 rtd->cpu_dai->ops.shutdown(substream);
799 if (rtd->codec_dai->ops.shutdown)
800 rtd->codec_dai->ops.shutdown(substream);
802 if (machine->ops && machine->ops->shutdown)
803 machine->ops->shutdown(substream);
805 if (platform->pcm_ops->close)
806 platform->pcm_ops->close(substream);
807 rtd->cpu_dai->runtime = NULL;
809 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
810 /* start delayed pop wq here for playback streams */
811 rtd->codec_dai->pop_wait = 1;
812 schedule_delayed_work(&socdev->delayed_work,
813 msecs_to_jiffies(pmdown_time));
815 /* capture streams can be powered down now */
816 snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
817 SND_SOC_DAPM_STREAM_STOP);
819 if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){
820 if (codec->dapm_event)
821 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
825 mutex_unlock(&pcm_mutex);
830 * Called by ALSA when the PCM substream is prepared, can set format, sample
831 * rate, etc. This function is non atomic and can be called multiple times,
832 * it can refer to the runtime info.
834 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
836 struct snd_soc_pcm_runtime *rtd = substream->private_data;
837 struct snd_soc_device *socdev = rtd->socdev;
838 struct snd_soc_platform *platform = socdev->platform;
839 struct snd_soc_codec *codec = socdev->codec;
842 mutex_lock(&pcm_mutex);
843 if (platform->pcm_ops->prepare) {
844 ret = platform->pcm_ops->prepare(substream);
846 printk(KERN_ERR "asoc: platform prepare error\n");
851 if (rtd->codec_dai->ops.prepare) {
852 ret = rtd->codec_dai->ops.prepare(substream);
854 printk(KERN_ERR "asoc: codec DAI prepare error\n");
859 if (rtd->cpu_dai->ops.prepare)
860 ret = rtd->cpu_dai->ops.prepare(substream);
862 /* we only want to start a DAPM playback stream if we are not waiting
863 * on an existing one stopping */
864 if (rtd->codec_dai->pop_wait) {
865 /* we are waiting for the delayed work to start */
866 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
867 snd_soc_dapm_stream_event(codec,
868 rtd->codec_dai->capture.stream_name,
869 SND_SOC_DAPM_STREAM_START);
871 rtd->codec_dai->pop_wait = 0;
872 cancel_delayed_work(&socdev->delayed_work);
873 if (rtd->codec_dai->digital_mute)
874 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
877 /* no delayed work - do we need to power up codec */
878 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
880 if (codec->dapm_event)
881 codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
883 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
884 snd_soc_dapm_stream_event(codec,
885 rtd->codec_dai->playback.stream_name,
886 SND_SOC_DAPM_STREAM_START);
888 snd_soc_dapm_stream_event(codec,
889 rtd->codec_dai->capture.stream_name,
890 SND_SOC_DAPM_STREAM_START);
892 if (codec->dapm_event)
893 codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
894 if (rtd->codec_dai->digital_mute)
895 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
898 /* codec already powered - power on widgets */
899 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
900 snd_soc_dapm_stream_event(codec,
901 rtd->codec_dai->playback.stream_name,
902 SND_SOC_DAPM_STREAM_START);
904 snd_soc_dapm_stream_event(codec,
905 rtd->codec_dai->capture.stream_name,
906 SND_SOC_DAPM_STREAM_START);
907 if (rtd->codec_dai->digital_mute)
908 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
913 mutex_unlock(&pcm_mutex);
918 * Called by ALSA when the hardware params are set by application. This
919 * function can also be called multiple times and can allocate buffers
920 * (using snd_pcm_lib_* ). It's non-atomic.
922 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
923 struct snd_pcm_hw_params *params)
925 struct snd_soc_pcm_runtime *rtd = substream->private_data;
926 struct snd_soc_device *socdev = rtd->socdev;
927 struct snd_soc_platform *platform = socdev->platform;
928 struct snd_soc_machine *machine = socdev->machine;
931 mutex_lock(&pcm_mutex);
933 /* we don't need to match any AC97 params */
934 if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) {
935 ret = soc_hw_match_params(substream, params);
939 struct snd_soc_clock_info clk_info;
940 clk_info.rate = params_rate(params);
941 ret = soc_get_mclk(rtd, &clk_info);
946 if (rtd->codec_dai->ops.hw_params) {
947 ret = rtd->codec_dai->ops.hw_params(substream, params);
949 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
950 rtd->codec_dai->name);
955 if (rtd->cpu_dai->ops.hw_params) {
956 ret = rtd->cpu_dai->ops.hw_params(substream, params);
958 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
964 if (platform->pcm_ops->hw_params) {
965 ret = platform->pcm_ops->hw_params(substream, params);
967 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
973 if (machine->ops && machine->ops->hw_params) {
974 ret = machine->ops->hw_params(substream, params);
976 printk(KERN_ERR "asoc: machine hw_params failed\n");
982 mutex_unlock(&pcm_mutex);
986 if (platform->pcm_ops->hw_free)
987 platform->pcm_ops->hw_free(substream);
990 if (rtd->cpu_dai->ops.hw_free)
991 rtd->cpu_dai->ops.hw_free(substream);
994 if (rtd->codec_dai->ops.hw_free)
995 rtd->codec_dai->ops.hw_free(substream);
997 mutex_unlock(&pcm_mutex);
1002 * Free's resources allocated by hw_params, can be called multiple times
1004 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1006 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1007 struct snd_soc_device *socdev = rtd->socdev;
1008 struct snd_soc_platform *platform = socdev->platform;
1009 struct snd_soc_codec *codec = socdev->codec;
1010 struct snd_soc_machine *machine = socdev->machine;
1012 mutex_lock(&pcm_mutex);
1014 /* apply codec digital mute */
1015 if (!codec->active && rtd->codec_dai->digital_mute)
1016 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1);
1018 /* free any machine hw params */
1019 if (machine->ops && machine->ops->hw_free)
1020 machine->ops->hw_free(substream);
1022 /* free any DMA resources */
1023 if (platform->pcm_ops->hw_free)
1024 platform->pcm_ops->hw_free(substream);
1026 /* now free hw params for the DAI's */
1027 if (rtd->codec_dai->ops.hw_free)
1028 rtd->codec_dai->ops.hw_free(substream);
1030 if (rtd->cpu_dai->ops.hw_free)
1031 rtd->cpu_dai->ops.hw_free(substream);
1033 mutex_unlock(&pcm_mutex);
1037 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1039 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1040 struct snd_soc_device *socdev = rtd->socdev;
1041 struct snd_soc_platform *platform = socdev->platform;
1044 if (rtd->codec_dai->ops.trigger) {
1045 ret = rtd->codec_dai->ops.trigger(substream, cmd);
1050 if (platform->pcm_ops->trigger) {
1051 ret = platform->pcm_ops->trigger(substream, cmd);
1056 if (rtd->cpu_dai->ops.trigger) {
1057 ret = rtd->cpu_dai->ops.trigger(substream, cmd);
1064 /* ASoC PCM operations */
1065 static struct snd_pcm_ops soc_pcm_ops = {
1066 .open = soc_pcm_open,
1067 .close = soc_codec_close,
1068 .hw_params = soc_pcm_hw_params,
1069 .hw_free = soc_pcm_hw_free,
1070 .prepare = soc_pcm_prepare,
1071 .trigger = soc_pcm_trigger,
1075 /* powers down audio subsystem for suspend */
1076 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
1078 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1079 struct snd_soc_machine *machine = socdev->machine;
1080 struct snd_soc_platform *platform = socdev->platform;
1081 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1082 struct snd_soc_codec *codec = socdev->codec;
1085 /* mute any active DAC's */
1086 for(i = 0; i < machine->num_links; i++) {
1087 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1088 if (dai->digital_mute && dai->playback.active)
1089 dai->digital_mute(codec, dai, 1);
1092 if (machine->suspend_pre)
1093 machine->suspend_pre(pdev, state);
1095 for(i = 0; i < machine->num_links; i++) {
1096 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1097 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
1098 cpu_dai->suspend(pdev, cpu_dai);
1099 if (platform->suspend)
1100 platform->suspend(pdev, cpu_dai);
1103 /* close any waiting streams and save state */
1104 flush_scheduled_work();
1105 codec->suspend_dapm_state = codec->dapm_state;
1107 for(i = 0; i < codec->num_dai; i++) {
1108 char *stream = codec->dai[i].playback.stream_name;
1110 snd_soc_dapm_stream_event(codec, stream,
1111 SND_SOC_DAPM_STREAM_SUSPEND);
1112 stream = codec->dai[i].capture.stream_name;
1114 snd_soc_dapm_stream_event(codec, stream,
1115 SND_SOC_DAPM_STREAM_SUSPEND);
1118 if (codec_dev->suspend)
1119 codec_dev->suspend(pdev, state);
1121 for(i = 0; i < machine->num_links; i++) {
1122 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1123 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
1124 cpu_dai->suspend(pdev, cpu_dai);
1127 if (machine->suspend_post)
1128 machine->suspend_post(pdev, state);
1133 /* powers up audio subsystem after a suspend */
1134 static int soc_resume(struct platform_device *pdev)
1136 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1137 struct snd_soc_machine *machine = socdev->machine;
1138 struct snd_soc_platform *platform = socdev->platform;
1139 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1140 struct snd_soc_codec *codec = socdev->codec;
1143 if (machine->resume_pre)
1144 machine->resume_pre(pdev);
1146 for(i = 0; i < machine->num_links; i++) {
1147 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1148 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
1149 cpu_dai->resume(pdev, cpu_dai);
1152 if (codec_dev->resume)
1153 codec_dev->resume(pdev);
1155 for(i = 0; i < codec->num_dai; i++) {
1156 char* stream = codec->dai[i].playback.stream_name;
1158 snd_soc_dapm_stream_event(codec, stream,
1159 SND_SOC_DAPM_STREAM_RESUME);
1160 stream = codec->dai[i].capture.stream_name;
1162 snd_soc_dapm_stream_event(codec, stream,
1163 SND_SOC_DAPM_STREAM_RESUME);
1166 /* unmute any active DAC's */
1167 for(i = 0; i < machine->num_links; i++) {
1168 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1169 if (dai->digital_mute && dai->playback.active)
1170 dai->digital_mute(codec, dai, 0);
1173 for(i = 0; i < machine->num_links; i++) {
1174 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1175 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
1176 cpu_dai->resume(pdev, cpu_dai);
1177 if (platform->resume)
1178 platform->resume(pdev, cpu_dai);
1181 if (machine->resume_post)
1182 machine->resume_post(pdev);
1188 #define soc_suspend NULL
1189 #define soc_resume NULL
1192 /* probes a new socdev */
1193 static int soc_probe(struct platform_device *pdev)
1196 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1197 struct snd_soc_machine *machine = socdev->machine;
1198 struct snd_soc_platform *platform = socdev->platform;
1199 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1201 if (machine->probe) {
1202 ret = machine->probe(pdev);
1207 for (i = 0; i < machine->num_links; i++) {
1208 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1209 if (cpu_dai->probe) {
1210 ret = cpu_dai->probe(pdev);
1216 if (codec_dev->probe) {
1217 ret = codec_dev->probe(pdev);
1222 if (platform->probe) {
1223 ret = platform->probe(pdev);
1228 /* DAPM stream work */
1229 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
1233 if (codec_dev->remove)
1234 codec_dev->remove(pdev);
1237 for (i--; i > 0; i--) {
1238 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1239 if (cpu_dai->remove)
1240 cpu_dai->remove(pdev);
1243 if (machine->remove)
1244 machine->remove(pdev);
1249 /* removes a socdev */
1250 static int soc_remove(struct platform_device *pdev)
1253 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1254 struct snd_soc_machine *machine = socdev->machine;
1255 struct snd_soc_platform *platform = socdev->platform;
1256 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1258 if (platform->remove)
1259 platform->remove(pdev);
1261 if (codec_dev->remove)
1262 codec_dev->remove(pdev);
1264 for (i = 0; i < machine->num_links; i++) {
1265 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1266 if (cpu_dai->remove)
1267 cpu_dai->remove(pdev);
1270 if (machine->remove)
1271 machine->remove(pdev);
1276 /* ASoC platform driver */
1277 static struct platform_driver soc_driver = {
1279 .name = "soc-audio",
1282 .remove = soc_remove,
1283 .suspend = soc_suspend,
1284 .resume = soc_resume,
1287 /* create a new pcm */
1288 static int soc_new_pcm(struct snd_soc_device *socdev,
1289 struct snd_soc_dai_link *dai_link, int num)
1291 struct snd_soc_codec *codec = socdev->codec;
1292 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
1293 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
1294 struct snd_soc_pcm_runtime *rtd;
1295 struct snd_pcm *pcm;
1297 int ret = 0, playback = 0, capture = 0;
1299 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1302 rtd->cpu_dai = cpu_dai;
1303 rtd->codec_dai = codec_dai;
1304 rtd->socdev = socdev;
1306 /* check client and interface hw capabilities */
1307 sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
1308 get_dai_name(cpu_dai->type), num);
1310 if (codec_dai->playback.channels_min)
1312 if (codec_dai->capture.channels_min)
1315 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1318 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1323 pcm->private_data = rtd;
1324 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
1325 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
1326 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
1327 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
1328 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
1329 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
1330 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
1333 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1336 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1338 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
1340 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1345 pcm->private_free = socdev->platform->pcm_free;
1346 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1351 /* codec register dump */
1352 static ssize_t codec_reg_show(struct device *dev,
1353 struct device_attribute *attr, char *buf)
1355 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1356 struct snd_soc_codec *codec = devdata->codec;
1357 int i, step = 1, count = 0;
1359 if (!codec->reg_cache_size)
1362 if (codec->reg_cache_step)
1363 step = codec->reg_cache_step;
1365 count += sprintf(buf, "%s registers\n", codec->name);
1366 for(i = 0; i < codec->reg_cache_size; i += step)
1367 count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
1371 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1374 * snd_soc_new_ac97_codec - initailise AC97 device
1375 * @codec: audio codec
1376 * @ops: AC97 bus operations
1377 * @num: AC97 codec number
1379 * Initialises AC97 codec resources for use by ad-hoc devices only.
1381 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1382 struct snd_ac97_bus_ops *ops, int num)
1384 mutex_lock(&codec->mutex);
1386 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1387 if (codec->ac97 == NULL) {
1388 mutex_unlock(&codec->mutex);
1392 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1393 if (codec->ac97->bus == NULL) {
1396 mutex_unlock(&codec->mutex);
1400 codec->ac97->bus->ops = ops;
1401 codec->ac97->num = num;
1402 mutex_unlock(&codec->mutex);
1405 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1408 * snd_soc_free_ac97_codec - free AC97 codec device
1409 * @codec: audio codec
1411 * Frees AC97 codec device resources.
1413 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1415 mutex_lock(&codec->mutex);
1416 kfree(codec->ac97->bus);
1419 mutex_unlock(&codec->mutex);
1421 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1424 * snd_soc_update_bits - update codec register bits
1425 * @codec: audio codec
1426 * @reg: codec register
1427 * @mask: register mask
1430 * Writes new register value.
1432 * Returns 1 for change else 0.
1434 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1435 unsigned short mask, unsigned short value)
1438 unsigned short old, new;
1440 mutex_lock(&io_mutex);
1441 old = snd_soc_read(codec, reg);
1442 new = (old & ~mask) | value;
1443 change = old != new;
1445 snd_soc_write(codec, reg, new);
1447 mutex_unlock(&io_mutex);
1450 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1453 * snd_soc_test_bits - test register for change
1454 * @codec: audio codec
1455 * @reg: codec register
1456 * @mask: register mask
1459 * Tests a register with a new value and checks if the new value is
1460 * different from the old value.
1462 * Returns 1 for change else 0.
1464 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1465 unsigned short mask, unsigned short value)
1468 unsigned short old, new;
1470 mutex_lock(&io_mutex);
1471 old = snd_soc_read(codec, reg);
1472 new = (old & ~mask) | value;
1473 change = old != new;
1474 mutex_unlock(&io_mutex);
1478 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1481 * snd_soc_get_rate - get int sample rate
1482 * @hwpcmrate: the hardware pcm rate
1484 * Returns the audio rate integaer value, else 0.
1486 int snd_soc_get_rate(int hwpcmrate)
1488 int rate = ffs(hwpcmrate) - 1;
1490 if (rate > ARRAY_SIZE(rates))
1494 EXPORT_SYMBOL_GPL(snd_soc_get_rate);
1497 * snd_soc_new_pcms - create new sound card and pcms
1498 * @socdev: the SoC audio device
1500 * Create a new sound card based upon the codec and interface pcms.
1502 * Returns 0 for success, else error.
1504 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
1506 struct snd_soc_codec *codec = socdev->codec;
1507 struct snd_soc_machine *machine = socdev->machine;
1510 mutex_lock(&codec->mutex);
1512 /* register a sound card */
1513 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1515 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1517 mutex_unlock(&codec->mutex);
1521 codec->card->dev = socdev->dev;
1522 codec->card->private_data = codec;
1523 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1525 /* create the pcms */
1526 for(i = 0; i < machine->num_links; i++) {
1527 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1529 printk(KERN_ERR "asoc: can't create pcm %s\n",
1530 machine->dai_link[i].stream_name);
1531 mutex_unlock(&codec->mutex);
1536 mutex_unlock(&codec->mutex);
1539 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1542 * snd_soc_register_card - register sound card
1543 * @socdev: the SoC audio device
1545 * Register a SoC sound card. Also registers an AC97 device if the
1546 * codec is AC97 for ad hoc devices.
1548 * Returns 0 for success, else error.
1550 int snd_soc_register_card(struct snd_soc_device *socdev)
1552 struct snd_soc_codec *codec = socdev->codec;
1553 struct snd_soc_machine *machine = socdev->machine;
1554 int ret = 0, i, ac97 = 0, err = 0;
1556 mutex_lock(&codec->mutex);
1557 for(i = 0; i < machine->num_links; i++) {
1558 if (socdev->machine->dai_link[i].init) {
1559 err = socdev->machine->dai_link[i].init(codec);
1561 printk(KERN_ERR "asoc: failed to init %s\n",
1562 socdev->machine->dai_link[i].stream_name);
1566 if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1569 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1570 "%s", machine->name);
1571 snprintf(codec->card->longname, sizeof(codec->card->longname),
1572 "%s (%s)", machine->name, codec->name);
1574 ret = snd_card_register(codec->card);
1576 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1581 #ifdef CONFIG_SND_SOC_AC97_BUS
1583 ret = soc_ac97_dev_register(codec);
1585 printk(KERN_ERR "asoc: AC97 device register failed\n");
1586 snd_card_free(codec->card);
1592 err = snd_soc_dapm_sys_add(socdev->dev);
1594 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1596 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1598 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1600 mutex_unlock(&codec->mutex);
1603 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1606 * snd_soc_free_pcms - free sound card and pcms
1607 * @socdev: the SoC audio device
1609 * Frees sound card and pcms associated with the socdev.
1610 * Also unregister the codec if it is an AC97 device.
1612 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1614 struct snd_soc_codec *codec = socdev->codec;
1616 mutex_lock(&codec->mutex);
1617 #ifdef CONFIG_SND_SOC_AC97_BUS
1619 soc_ac97_dev_unregister(codec);
1623 snd_card_free(codec->card);
1624 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1625 mutex_unlock(&codec->mutex);
1627 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1630 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1631 * @substream: the pcm substream
1632 * @hw: the hardware parameters
1634 * Sets the substream runtime hardware parameters.
1636 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1637 const struct snd_pcm_hardware *hw)
1639 struct snd_pcm_runtime *runtime = substream->runtime;
1640 runtime->hw.info = hw->info;
1641 runtime->hw.formats = hw->formats;
1642 runtime->hw.period_bytes_min = hw->period_bytes_min;
1643 runtime->hw.period_bytes_max = hw->period_bytes_max;
1644 runtime->hw.periods_min = hw->periods_min;
1645 runtime->hw.periods_max = hw->periods_max;
1646 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1647 runtime->hw.fifo_size = hw->fifo_size;
1650 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1653 * snd_soc_cnew - create new control
1654 * @_template: control template
1655 * @data: control private data
1656 * @lnng_name: control long name
1658 * Create a new mixer control from a template control.
1660 * Returns 0 for success, else error.
1662 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1663 void *data, char *long_name)
1665 struct snd_kcontrol_new template;
1667 memcpy(&template, _template, sizeof(template));
1669 template.name = long_name;
1670 template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1673 return snd_ctl_new1(&template, data);
1675 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1678 * snd_soc_info_enum_double - enumerated double mixer info callback
1679 * @kcontrol: mixer control
1680 * @uinfo: control element information
1682 * Callback to provide information about a double enumerated
1685 * Returns 0 for success.
1687 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1688 struct snd_ctl_elem_info *uinfo)
1690 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1692 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1693 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1694 uinfo->value.enumerated.items = e->mask;
1696 if (uinfo->value.enumerated.item > e->mask - 1)
1697 uinfo->value.enumerated.item = e->mask - 1;
1698 strcpy(uinfo->value.enumerated.name,
1699 e->texts[uinfo->value.enumerated.item]);
1702 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1705 * snd_soc_get_enum_double - enumerated double mixer get callback
1706 * @kcontrol: mixer control
1707 * @uinfo: control element information
1709 * Callback to get the value of a double enumerated mixer.
1711 * Returns 0 for success.
1713 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_value *ucontrol)
1716 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1717 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1718 unsigned short val, bitmask;
1720 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1722 val = snd_soc_read(codec, e->reg);
1723 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1724 if (e->shift_l != e->shift_r)
1725 ucontrol->value.enumerated.item[1] =
1726 (val >> e->shift_r) & (bitmask - 1);
1730 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1733 * snd_soc_put_enum_double - enumerated double mixer put callback
1734 * @kcontrol: mixer control
1735 * @uinfo: control element information
1737 * Callback to set the value of a double enumerated mixer.
1739 * Returns 0 for success.
1741 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1742 struct snd_ctl_elem_value *ucontrol)
1744 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1745 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1747 unsigned short mask, bitmask;
1749 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1751 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1753 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1754 mask = (bitmask - 1) << e->shift_l;
1755 if (e->shift_l != e->shift_r) {
1756 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1758 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1759 mask |= (bitmask - 1) << e->shift_r;
1762 return snd_soc_update_bits(codec, e->reg, mask, val);
1764 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1767 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1768 * @kcontrol: mixer control
1769 * @uinfo: control element information
1771 * Callback to provide information about an external enumerated
1774 * Returns 0 for success.
1776 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1777 struct snd_ctl_elem_info *uinfo)
1779 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1781 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1783 uinfo->value.enumerated.items = e->mask;
1785 if (uinfo->value.enumerated.item > e->mask - 1)
1786 uinfo->value.enumerated.item = e->mask - 1;
1787 strcpy(uinfo->value.enumerated.name,
1788 e->texts[uinfo->value.enumerated.item]);
1791 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1794 * snd_soc_info_volsw_ext - external single mixer info callback
1795 * @kcontrol: mixer control
1796 * @uinfo: control element information
1798 * Callback to provide information about a single external mixer control.
1800 * Returns 0 for success.
1802 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1803 struct snd_ctl_elem_info *uinfo)
1805 int mask = kcontrol->private_value;
1808 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1810 uinfo->value.integer.min = 0;
1811 uinfo->value.integer.max = mask;
1814 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1817 * snd_soc_info_bool_ext - external single boolean mixer info callback
1818 * @kcontrol: mixer control
1819 * @uinfo: control element information
1821 * Callback to provide information about a single boolean external mixer control.
1823 * Returns 0 for success.
1825 int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
1826 struct snd_ctl_elem_info *uinfo)
1828 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1830 uinfo->value.integer.min = 0;
1831 uinfo->value.integer.max = 1;
1834 EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
1837 * snd_soc_info_volsw - single mixer info callback
1838 * @kcontrol: mixer control
1839 * @uinfo: control element information
1841 * Callback to provide information about a single mixer control.
1843 * Returns 0 for success.
1845 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_info *uinfo)
1848 int mask = (kcontrol->private_value >> 16) & 0xff;
1849 int shift = (kcontrol->private_value >> 8) & 0x0f;
1850 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1853 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1854 uinfo->count = shift == rshift ? 1 : 2;
1855 uinfo->value.integer.min = 0;
1856 uinfo->value.integer.max = mask;
1859 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1862 * snd_soc_get_volsw - single mixer get callback
1863 * @kcontrol: mixer control
1864 * @uinfo: control element information
1866 * Callback to get the value of a single mixer control.
1868 * Returns 0 for success.
1870 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
1873 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1874 int reg = kcontrol->private_value & 0xff;
1875 int shift = (kcontrol->private_value >> 8) & 0x0f;
1876 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1877 int mask = (kcontrol->private_value >> 16) & 0xff;
1878 int invert = (kcontrol->private_value >> 24) & 0x01;
1880 ucontrol->value.integer.value[0] =
1881 (snd_soc_read(codec, reg) >> shift) & mask;
1882 if (shift != rshift)
1883 ucontrol->value.integer.value[1] =
1884 (snd_soc_read(codec, reg) >> rshift) & mask;
1886 ucontrol->value.integer.value[0] =
1887 mask - ucontrol->value.integer.value[0];
1888 if (shift != rshift)
1889 ucontrol->value.integer.value[1] =
1890 mask - ucontrol->value.integer.value[1];
1895 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1898 * snd_soc_put_volsw - single mixer put callback
1899 * @kcontrol: mixer control
1900 * @uinfo: control element information
1902 * Callback to set the value of a single mixer control.
1904 * Returns 0 for success.
1906 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_value *ucontrol)
1909 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1910 int reg = kcontrol->private_value & 0xff;
1911 int shift = (kcontrol->private_value >> 8) & 0x0f;
1912 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1913 int mask = (kcontrol->private_value >> 16) & 0xff;
1914 int invert = (kcontrol->private_value >> 24) & 0x01;
1916 unsigned short val, val2, val_mask;
1918 val = (ucontrol->value.integer.value[0] & mask);
1921 val_mask = mask << shift;
1923 if (shift != rshift) {
1924 val2 = (ucontrol->value.integer.value[1] & mask);
1927 val_mask |= mask << rshift;
1928 val |= val2 << rshift;
1930 err = snd_soc_update_bits(codec, reg, val_mask, val);
1933 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1936 * snd_soc_info_volsw_2r - double mixer info callback
1937 * @kcontrol: mixer control
1938 * @uinfo: control element information
1940 * Callback to provide information about a double mixer control that
1941 * spans 2 codec registers.
1943 * Returns 0 for success.
1945 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_info *uinfo)
1948 int mask = (kcontrol->private_value >> 12) & 0xff;
1951 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1953 uinfo->value.integer.min = 0;
1954 uinfo->value.integer.max = mask;
1957 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1960 * snd_soc_get_volsw_2r - double mixer get callback
1961 * @kcontrol: mixer control
1962 * @uinfo: control element information
1964 * Callback to get the value of a double mixer control that spans 2 registers.
1966 * Returns 0 for success.
1968 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1969 struct snd_ctl_elem_value *ucontrol)
1971 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1972 int reg = kcontrol->private_value & 0xff;
1973 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1974 int shift = (kcontrol->private_value >> 8) & 0x0f;
1975 int mask = (kcontrol->private_value >> 12) & 0xff;
1976 int invert = (kcontrol->private_value >> 20) & 0x01;
1978 ucontrol->value.integer.value[0] =
1979 (snd_soc_read(codec, reg) >> shift) & mask;
1980 ucontrol->value.integer.value[1] =
1981 (snd_soc_read(codec, reg2) >> shift) & mask;
1983 ucontrol->value.integer.value[0] =
1984 mask - ucontrol->value.integer.value[0];
1985 ucontrol->value.integer.value[1] =
1986 mask - ucontrol->value.integer.value[1];
1991 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1994 * snd_soc_put_volsw_2r - double mixer set callback
1995 * @kcontrol: mixer control
1996 * @uinfo: control element information
1998 * Callback to set the value of a double mixer control that spans 2 registers.
2000 * Returns 0 for success.
2002 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_value *ucontrol)
2005 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2006 int reg = kcontrol->private_value & 0xff;
2007 int reg2 = (kcontrol->private_value >> 24) & 0xff;
2008 int shift = (kcontrol->private_value >> 8) & 0x0f;
2009 int mask = (kcontrol->private_value >> 12) & 0xff;
2010 int invert = (kcontrol->private_value >> 20) & 0x01;
2012 unsigned short val, val2, val_mask;
2014 val_mask = mask << shift;
2015 val = (ucontrol->value.integer.value[0] & mask);
2016 val2 = (ucontrol->value.integer.value[1] & mask);
2024 val2 = val2 << shift;
2026 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
2029 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2032 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2034 static int __devinit snd_soc_init(void)
2036 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
2037 return platform_driver_register(&soc_driver);
2040 static void snd_soc_exit(void)
2042 platform_driver_unregister(&soc_driver);
2045 module_init(snd_soc_init);
2046 module_exit(snd_soc_exit);
2048 /* Module information */
2049 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
2050 MODULE_DESCRIPTION("ALSA SoC Core");
2051 MODULE_LICENSE("GPL");