2 * soc-core.c -- ALSA SoC Audio Layer
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 * 12th Aug 2005 Initial version.
15 * 25th Oct 2005 Working Codec, Interface and Platform registration.
18 * o Add hw rules to enforce rates, etc.
19 * o More testing with other codecs/machines.
20 * o Add more codecs and platforms to ensure good API coverage.
21 * o Support TDM on PCM and I2S
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
29 #include <linux/bitops.h>
30 #include <linux/platform_device.h>
31 #include <sound/driver.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
42 #define dbg(format, arg...) printk(format, ## arg)
44 #define dbg(format, arg...)
46 /* debug DAI capabilities matching */
47 #define SOC_DEBUG_DAI 0
49 #define dbgc(format, arg...) printk(format, ## arg)
51 #define dbgc(format, arg...)
54 #define CODEC_CPU(codec, cpu) ((codec << 4) | cpu)
56 static DEFINE_MUTEX(pcm_mutex);
57 static DEFINE_MUTEX(io_mutex);
58 static struct workqueue_struct *soc_workq;
59 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
61 /* supported sample rates */
62 /* ATTENTION: these values depend on the definition in pcm.h! */
63 static const unsigned int rates[] = {
64 5512, 8000, 11025, 16000, 22050, 32000, 44100,
65 48000, 64000, 88200, 96000, 176400, 192000
69 * This is a timeout to do a DAPM powerdown after a stream is closed().
70 * It can be used to eliminate pops between different playback streams, e.g.
71 * between two audio tracks.
73 static int pmdown_time = 5000;
74 module_param(pmdown_time, int, 0);
75 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
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);
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)
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);
102 snd_printk(KERN_ERR "Can't register ac97 bus\n");
103 codec->ac97->dev.bus = NULL;
110 static inline const char* get_dai_name(int type)
113 case SND_SOC_DAI_AC97:
115 case SND_SOC_DAI_I2S:
117 case SND_SOC_DAI_PCM:
123 /* get rate format from rate */
124 static inline int soc_get_rate_format(int rate)
128 for (i = 0; i < ARRAY_SIZE(rates); i++) {
129 if (rates[i] == rate)
135 /* gets the audio system mclk/sysclk for the given parameters */
136 static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
137 struct snd_soc_clock_info *info)
139 struct snd_soc_device *socdev = rtd->socdev;
140 struct snd_soc_machine *machine = socdev->machine;
143 /* find the matching machine config and get it's mclk for the given
144 * sample rate and hardware format */
145 for(i = 0; i < machine->num_links; i++) {
146 if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
147 machine->dai_link[i].config_sysclk)
148 return machine->dai_link[i].config_sysclk(rtd, info);
153 /* changes a bitclk multiplier mask to a divider mask */
154 static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
155 unsigned int pcmfmt, unsigned int chn)
159 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
164 /* the minimum bit clock that has enough bandwidth */
165 min = size * rate * chn;
166 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
168 for (i = 0; i < 64; i++) {
169 if ((bfs >> i) & 0x1) {
171 bfs_ |= SND_SOC_FSBD(mclk/j);
172 dbgc("rcw --> div support mult %d\n",
173 SND_SOC_FSBD_REAL(1<<i));
180 /* changes a bitclk divider mask to a multiplier mask */
181 static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
182 unsigned int pcmfmt, unsigned int chn)
187 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
192 /* the minimum bit clock that has enough bandwidth */
193 min = size * rate * chn;
194 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
196 for (i = 0; i < 64; i++) {
197 if ((bfs >> i) & 0x1) {
200 bfs_ |= SND_SOC_FSBW(j/min);
201 dbgc("div --> rcw support div %d\n",
202 SND_SOC_FSBW_REAL(1<<i));
210 /* changes a constant bitclk to a multiplier mask */
211 static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
212 unsigned int pcmfmt, unsigned int chn)
214 unsigned int bfs_ = rate * bfs;
215 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
220 /* the minimum bit clock that has enough bandwidth */
221 min = size * rate * chn;
222 dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
227 bfs_ = SND_SOC_FSBW(bfs_/min);
228 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
233 /* changes a bitclk multiplier mask to a divider mask */
234 static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
235 unsigned int pcmfmt, unsigned int chn)
237 unsigned int bfs_ = rate * bfs;
238 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
243 /* the minimum bit clock that has enough bandwidth */
244 min = size * rate * chn;
245 dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
250 bfs_ = SND_SOC_FSBW(mclk/bfs_);
251 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
256 /* Matches codec DAI and SoC CPU DAI hardware parameters */
257 static int soc_hw_match_params(struct snd_pcm_substream *substream,
258 struct snd_pcm_hw_params *params)
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_soc_dai_mode *codec_dai_mode = NULL;
262 struct snd_soc_dai_mode *cpu_dai_mode = NULL;
263 struct snd_soc_clock_info clk_info;
264 unsigned int fs, mclk, rate = params_rate(params),
265 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
267 u64 codec_bfs, cpu_bfs;
269 dbg("asoc: match version %s\n", SND_SOC_VERSION);
270 clk_info.rate = rate;
271 pcmrate = soc_get_rate_format(rate);
273 /* try and find a match from the codec and cpu DAI capabilities */
274 for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
275 for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
276 codec_dai_mode = &rtd->codec_dai->caps.mode[j];
277 cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
279 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
281 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
285 fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
286 if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
287 dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
291 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
292 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
297 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
298 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
302 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
303 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
307 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
308 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
312 /* todo - still need to add tdm selection */
313 rtd->cpu_dai->dai_runtime.fmt =
314 rtd->codec_dai->dai_runtime.fmt =
315 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
316 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
317 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
318 clk_info.bclk_master =
319 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
321 /* make sure the ratio between rate and master
322 * clock is acceptable*/
323 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
325 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
328 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
329 rtd->codec_dai->dai_runtime.fs = fs;
331 /* calculate audio system clocking using slowest clocks possible*/
332 mclk = soc_get_mclk(rtd, &clk_info);
334 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
335 dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
336 clk_info.bclk_master);
340 /* calculate word size (per channel) and frame size */
341 rtd->codec_dai->dai_runtime.pcmfmt =
342 rtd->cpu_dai->dai_runtime.pcmfmt =
343 1 << params_format(params);
345 chn = params_channels(params);
346 /* i2s always has left and right */
347 if (params_channels(params) == 1 &&
348 rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
349 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
352 /* Calculate bfs - the ratio between bitclock and the sample rate
353 * We must take into consideration the dividers and multipliers
354 * used in the codec and cpu DAI modes. We always choose the
355 * lowest possible clocks to reduce power.
357 switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
358 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
359 /* cpu & codec bfs dividers */
360 rtd->cpu_dai->dai_runtime.bfs =
361 rtd->codec_dai->dai_runtime.bfs =
362 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
364 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
365 /* normalise bfs codec divider & cpu rcw mult */
366 codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
367 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
368 rtd->cpu_dai->dai_runtime.bfs =
369 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
370 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
371 rtd->codec_dai->dai_runtime.pcmfmt, chn);
372 rtd->codec_dai->dai_runtime.bfs =
373 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
375 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
376 /* normalise bfs codec rcw mult & cpu divider */
377 codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
378 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
379 rtd->cpu_dai->dai_runtime.bfs =
380 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
381 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
382 rtd->codec_dai->dai_runtime.pcmfmt, chn);
383 rtd->codec_dai->dai_runtime.bfs =
384 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
386 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
387 /* codec & cpu bfs rate rcw multipliers */
388 rtd->cpu_dai->dai_runtime.bfs =
389 rtd->codec_dai->dai_runtime.bfs =
390 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
392 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
393 /* normalise cpu bfs rate const multiplier & codec div */
394 cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
395 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
396 if(codec_dai_mode->bfs & cpu_bfs) {
397 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
398 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
400 rtd->cpu_dai->dai_runtime.bfs = 0;
402 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
403 /* normalise cpu bfs rate const multiplier & codec rcw mult */
404 cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
405 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
406 if(codec_dai_mode->bfs & cpu_bfs) {
407 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
408 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
410 rtd->cpu_dai->dai_runtime.bfs = 0;
412 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
413 /* normalise cpu bfs rate rcw multiplier & codec const mult */
414 codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
415 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
416 if(cpu_dai_mode->bfs & codec_bfs) {
417 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
418 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
420 rtd->cpu_dai->dai_runtime.bfs = 0;
422 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
423 /* normalise cpu bfs div & codec const mult */
424 codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
425 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
426 if(cpu_dai_mode->bfs & codec_bfs) {
427 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
428 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
430 rtd->cpu_dai->dai_runtime.bfs = 0;
432 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
433 /* cpu & codec constant mult */
434 if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
435 rtd->cpu_dai->dai_runtime.bfs =
436 rtd->codec_dai->dai_runtime.bfs =
439 rtd->cpu_dai->dai_runtime.bfs =
440 rtd->codec_dai->dai_runtime.bfs = 0;
444 /* make sure the bit clock speed is acceptable */
445 if (!rtd->cpu_dai->dai_runtime.bfs ||
446 !rtd->codec_dai->dai_runtime.bfs) {
447 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
448 dbgc("asoc: cpu_dai %llu codec %llu\n",
449 rtd->cpu_dai->dai_runtime.bfs,
450 rtd->codec_dai->dai_runtime.bfs);
451 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
458 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
462 /* we have matching DAI's, so complete the runtime info */
463 rtd->codec_dai->dai_runtime.pcmrate =
464 rtd->cpu_dai->dai_runtime.pcmrate =
465 soc_get_rate_format(rate);
467 rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
468 rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
469 rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
470 rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
473 dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
474 if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
475 codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
476 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
477 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
478 rtd->codec_dai->dai_runtime.fs, mclk,
479 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
480 } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
481 codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
482 dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
483 rtd->codec_dai->dai_runtime.fs, mclk,
484 rtd->codec_dai->dai_runtime.bfs, codec_bclk);
485 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
486 codec_bclk = params_rate(params) * params_channels(params) *
487 snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
488 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
489 dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
490 rtd->codec_dai->dai_runtime.fs, mclk,
491 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
495 if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
496 cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
497 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
498 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
499 rtd->cpu_dai->dai_runtime.fs, mclk,
500 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
501 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
502 cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
503 dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
504 rtd->cpu_dai->dai_runtime.fs, mclk,
505 rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
506 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
507 cpu_bclk = params_rate(params) * params_channels(params) *
508 snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
509 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
510 dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
511 rtd->cpu_dai->dai_runtime.fs, mclk,
512 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
517 * Check we have matching bitclocks. If we don't then it means the
518 * sysclock returned by either the codec or cpu DAI (selected by the
519 * machine sysclock function) is wrong compared with the supported DAI
520 * modes for the codec or cpu DAI.
522 if (cpu_bclk != codec_bclk && cpu_bclk){
524 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
526 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
529 switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
530 case SND_SOC_DAIFMT_CBM_CFM:
531 dbg("asoc: DAI codec BCLK master, LRC master\n");
533 case SND_SOC_DAIFMT_CBS_CFM:
534 dbg("asoc: DAI codec BCLK slave, LRC master\n");
536 case SND_SOC_DAIFMT_CBM_CFS:
537 dbg("asoc: DAI codec BCLK master, LRC slave\n");
539 case SND_SOC_DAIFMT_CBS_CFS:
540 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
543 dbg("asoc: mode %x, invert %x\n",
544 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
545 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
546 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
547 params_channels(params), params_format(params));
552 static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
557 for(i = 0; i < nmodes; i++)
558 rates |= modes[i].pcmrate;
563 static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
568 for(i = 0; i < nmodes; i++)
569 formats |= modes[i].pcmfmt;
575 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
576 * then initialized and any private data can be allocated. This also calls
577 * startup for the cpu DAI, platform, machine and codec DAI.
579 static int soc_pcm_open(struct snd_pcm_substream *substream)
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct snd_soc_device *socdev = rtd->socdev;
583 struct snd_pcm_runtime *runtime = substream->runtime;
584 struct snd_soc_machine *machine = socdev->machine;
585 struct snd_soc_platform *platform = socdev->platform;
586 struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
587 struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
590 mutex_lock(&pcm_mutex);
592 /* startup the audio subsystem */
593 if (rtd->cpu_dai->ops.startup) {
594 ret = rtd->cpu_dai->ops.startup(substream);
596 printk(KERN_ERR "asoc: can't open interface %s\n",
602 if (platform->pcm_ops->open) {
603 ret = platform->pcm_ops->open(substream);
605 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
610 if (machine->ops && machine->ops->startup) {
611 ret = machine->ops->startup(substream);
613 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
618 if (rtd->codec_dai->ops.startup) {
619 ret = rtd->codec_dai->ops.startup(substream);
621 printk(KERN_ERR "asoc: can't open codec %s\n",
622 rtd->codec_dai->name);
627 /* create runtime params from DMA, codec and cpu DAI */
628 if (runtime->hw.rates)
630 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
631 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
634 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
635 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
636 if (runtime->hw.formats)
637 runtime->hw.formats &=
638 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
639 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
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 /* Check that the codec and cpu DAI's are compatible */
646 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
647 runtime->hw.rate_min =
648 max(rtd->codec_dai->playback.rate_min,
649 rtd->cpu_dai->playback.rate_min);
650 runtime->hw.rate_max =
651 min(rtd->codec_dai->playback.rate_max,
652 rtd->cpu_dai->playback.rate_max);
653 runtime->hw.channels_min =
654 max(rtd->codec_dai->playback.channels_min,
655 rtd->cpu_dai->playback.channels_min);
656 runtime->hw.channels_max =
657 min(rtd->codec_dai->playback.channels_max,
658 rtd->cpu_dai->playback.channels_max);
660 runtime->hw.rate_min =
661 max(rtd->codec_dai->capture.rate_min,
662 rtd->cpu_dai->capture.rate_min);
663 runtime->hw.rate_max =
664 min(rtd->codec_dai->capture.rate_max,
665 rtd->cpu_dai->capture.rate_max);
666 runtime->hw.channels_min =
667 max(rtd->codec_dai->capture.channels_min,
668 rtd->cpu_dai->capture.channels_min);
669 runtime->hw.channels_max =
670 min(rtd->codec_dai->capture.channels_max,
671 rtd->cpu_dai->capture.channels_max);
674 snd_pcm_limit_hw_rates(runtime);
675 if (!runtime->hw.rates) {
676 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
677 rtd->codec_dai->name, rtd->cpu_dai->name);
680 if (!runtime->hw.formats) {
681 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
682 rtd->codec_dai->name, rtd->cpu_dai->name);
685 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
686 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
687 rtd->codec_dai->name, rtd->cpu_dai->name);
691 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
692 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
693 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
694 runtime->hw.channels_max);
695 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
696 runtime->hw.rate_max);
699 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
700 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
702 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
703 rtd->cpu_dai->active = rtd->codec_dai->active = 1;
704 rtd->cpu_dai->runtime = runtime;
705 socdev->codec->active++;
706 mutex_unlock(&pcm_mutex);
710 if (machine->ops && machine->ops->shutdown)
711 machine->ops->shutdown(substream);
714 if (platform->pcm_ops->close)
715 platform->pcm_ops->close(substream);
718 if (rtd->cpu_dai->ops.shutdown)
719 rtd->cpu_dai->ops.shutdown(substream);
721 mutex_unlock(&pcm_mutex);
726 * Power down the audio subsytem pmdown_time msecs after close is called.
727 * This is to ensure there are no pops or clicks in between any music tracks
728 * due to DAPM power cycling.
730 static void close_delayed_work(struct work_struct *work)
732 struct snd_soc_device *socdev =
733 container_of(work, struct snd_soc_device, delayed_work.work);
734 struct snd_soc_codec *codec = socdev->codec;
735 struct snd_soc_codec_dai *codec_dai;
738 mutex_lock(&pcm_mutex);
739 for(i = 0; i < codec->num_dai; i++) {
740 codec_dai = &codec->dai[i];
742 dbg("pop wq checking: %s status: %s waiting: %s\n",
743 codec_dai->playback.stream_name,
744 codec_dai->playback.active ? "active" : "inactive",
745 codec_dai->pop_wait ? "yes" : "no");
747 /* are we waiting on this codec DAI stream */
748 if (codec_dai->pop_wait == 1) {
750 codec_dai->pop_wait = 0;
751 snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
752 SND_SOC_DAPM_STREAM_STOP);
754 /* power down the codec power domain if no longer active */
755 if (codec->active == 0) {
756 dbg("pop wq D3 %s %s\n", codec->name,
757 codec_dai->playback.stream_name);
758 if (codec->dapm_event)
759 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
763 mutex_unlock(&pcm_mutex);
767 * Called by ALSA when a PCM substream is closed. Private data can be
768 * freed here. The cpu DAI, codec DAI, machine and platform are also
771 static int soc_codec_close(struct snd_pcm_substream *substream)
773 struct snd_soc_pcm_runtime *rtd = substream->private_data;
774 struct snd_soc_device *socdev = rtd->socdev;
775 struct snd_soc_machine *machine = socdev->machine;
776 struct snd_soc_platform *platform = socdev->platform;
777 struct snd_soc_codec *codec = socdev->codec;
779 mutex_lock(&pcm_mutex);
781 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
782 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
784 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
786 if (rtd->codec_dai->playback.active == 0 &&
787 rtd->codec_dai->capture.active == 0) {
788 rtd->cpu_dai->active = rtd->codec_dai->active = 0;
792 if (rtd->cpu_dai->ops.shutdown)
793 rtd->cpu_dai->ops.shutdown(substream);
795 if (rtd->codec_dai->ops.shutdown)
796 rtd->codec_dai->ops.shutdown(substream);
798 if (machine->ops && machine->ops->shutdown)
799 machine->ops->shutdown(substream);
801 if (platform->pcm_ops->close)
802 platform->pcm_ops->close(substream);
803 rtd->cpu_dai->runtime = NULL;
805 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
806 /* start delayed pop wq here for playback streams */
807 rtd->codec_dai->pop_wait = 1;
808 queue_delayed_work(soc_workq, &socdev->delayed_work,
809 msecs_to_jiffies(pmdown_time));
811 /* capture streams can be powered down now */
812 snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
813 SND_SOC_DAPM_STREAM_STOP);
815 if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){
816 if (codec->dapm_event)
817 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
821 mutex_unlock(&pcm_mutex);
826 * Called by ALSA when the PCM substream is prepared, can set format, sample
827 * rate, etc. This function is non atomic and can be called multiple times,
828 * it can refer to the runtime info.
830 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
832 struct snd_soc_pcm_runtime *rtd = substream->private_data;
833 struct snd_soc_device *socdev = rtd->socdev;
834 struct snd_soc_platform *platform = socdev->platform;
835 struct snd_soc_codec *codec = socdev->codec;
838 mutex_lock(&pcm_mutex);
839 if (platform->pcm_ops->prepare) {
840 ret = platform->pcm_ops->prepare(substream);
842 printk(KERN_ERR "asoc: platform prepare error\n");
847 if (rtd->codec_dai->ops.prepare) {
848 ret = rtd->codec_dai->ops.prepare(substream);
850 printk(KERN_ERR "asoc: codec DAI prepare error\n");
855 if (rtd->cpu_dai->ops.prepare)
856 ret = rtd->cpu_dai->ops.prepare(substream);
858 /* we only want to start a DAPM playback stream if we are not waiting
859 * on an existing one stopping */
860 if (rtd->codec_dai->pop_wait) {
861 /* we are waiting for the delayed work to start */
862 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
863 snd_soc_dapm_stream_event(codec,
864 rtd->codec_dai->capture.stream_name,
865 SND_SOC_DAPM_STREAM_START);
867 rtd->codec_dai->pop_wait = 0;
868 cancel_delayed_work(&socdev->delayed_work);
869 if (rtd->codec_dai->digital_mute)
870 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
873 /* no delayed work - do we need to power up codec */
874 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
876 if (codec->dapm_event)
877 codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
879 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
880 snd_soc_dapm_stream_event(codec,
881 rtd->codec_dai->playback.stream_name,
882 SND_SOC_DAPM_STREAM_START);
884 snd_soc_dapm_stream_event(codec,
885 rtd->codec_dai->capture.stream_name,
886 SND_SOC_DAPM_STREAM_START);
888 if (codec->dapm_event)
889 codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
890 if (rtd->codec_dai->digital_mute)
891 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
894 /* codec already powered - power on widgets */
895 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
896 snd_soc_dapm_stream_event(codec,
897 rtd->codec_dai->playback.stream_name,
898 SND_SOC_DAPM_STREAM_START);
900 snd_soc_dapm_stream_event(codec,
901 rtd->codec_dai->capture.stream_name,
902 SND_SOC_DAPM_STREAM_START);
903 if (rtd->codec_dai->digital_mute)
904 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
909 mutex_unlock(&pcm_mutex);
914 * Called by ALSA when the hardware params are set by application. This
915 * function can also be called multiple times and can allocate buffers
916 * (using snd_pcm_lib_* ). It's non-atomic.
918 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
919 struct snd_pcm_hw_params *params)
921 struct snd_soc_pcm_runtime *rtd = substream->private_data;
922 struct snd_soc_device *socdev = rtd->socdev;
923 struct snd_soc_platform *platform = socdev->platform;
924 struct snd_soc_machine *machine = socdev->machine;
927 mutex_lock(&pcm_mutex);
929 /* we don't need to match any AC97 params */
930 if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) {
931 ret = soc_hw_match_params(substream, params);
935 struct snd_soc_clock_info clk_info;
936 clk_info.rate = params_rate(params);
937 ret = soc_get_mclk(rtd, &clk_info);
942 if (rtd->codec_dai->ops.hw_params) {
943 ret = rtd->codec_dai->ops.hw_params(substream, params);
945 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
946 rtd->codec_dai->name);
951 if (rtd->cpu_dai->ops.hw_params) {
952 ret = rtd->cpu_dai->ops.hw_params(substream, params);
954 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
960 if (platform->pcm_ops->hw_params) {
961 ret = platform->pcm_ops->hw_params(substream, params);
963 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
969 if (machine->ops && machine->ops->hw_params) {
970 ret = machine->ops->hw_params(substream, params);
972 printk(KERN_ERR "asoc: machine hw_params failed\n");
978 mutex_unlock(&pcm_mutex);
982 if (platform->pcm_ops->hw_free)
983 platform->pcm_ops->hw_free(substream);
986 if (rtd->cpu_dai->ops.hw_free)
987 rtd->cpu_dai->ops.hw_free(substream);
990 if (rtd->codec_dai->ops.hw_free)
991 rtd->codec_dai->ops.hw_free(substream);
993 mutex_unlock(&pcm_mutex);
998 * Free's resources allocated by hw_params, can be called multiple times
1000 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1002 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1003 struct snd_soc_device *socdev = rtd->socdev;
1004 struct snd_soc_platform *platform = socdev->platform;
1005 struct snd_soc_codec *codec = socdev->codec;
1006 struct snd_soc_machine *machine = socdev->machine;
1008 mutex_lock(&pcm_mutex);
1010 /* apply codec digital mute */
1011 if (!codec->active && rtd->codec_dai->digital_mute)
1012 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1);
1014 /* free any machine hw params */
1015 if (machine->ops && machine->ops->hw_free)
1016 machine->ops->hw_free(substream);
1018 /* free any DMA resources */
1019 if (platform->pcm_ops->hw_free)
1020 platform->pcm_ops->hw_free(substream);
1022 /* now free hw params for the DAI's */
1023 if (rtd->codec_dai->ops.hw_free)
1024 rtd->codec_dai->ops.hw_free(substream);
1026 if (rtd->cpu_dai->ops.hw_free)
1027 rtd->cpu_dai->ops.hw_free(substream);
1029 mutex_unlock(&pcm_mutex);
1033 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1035 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1036 struct snd_soc_device *socdev = rtd->socdev;
1037 struct snd_soc_platform *platform = socdev->platform;
1040 if (rtd->codec_dai->ops.trigger) {
1041 ret = rtd->codec_dai->ops.trigger(substream, cmd);
1046 if (platform->pcm_ops->trigger) {
1047 ret = platform->pcm_ops->trigger(substream, cmd);
1052 if (rtd->cpu_dai->ops.trigger) {
1053 ret = rtd->cpu_dai->ops.trigger(substream, cmd);
1060 /* ASoC PCM operations */
1061 static struct snd_pcm_ops soc_pcm_ops = {
1062 .open = soc_pcm_open,
1063 .close = soc_codec_close,
1064 .hw_params = soc_pcm_hw_params,
1065 .hw_free = soc_pcm_hw_free,
1066 .prepare = soc_pcm_prepare,
1067 .trigger = soc_pcm_trigger,
1071 /* powers down audio subsystem for suspend */
1072 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
1074 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1075 struct snd_soc_machine *machine = socdev->machine;
1076 struct snd_soc_platform *platform = socdev->platform;
1077 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1078 struct snd_soc_codec *codec = socdev->codec;
1081 /* mute any active DAC's */
1082 for(i = 0; i < machine->num_links; i++) {
1083 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1084 if (dai->digital_mute && dai->playback.active)
1085 dai->digital_mute(codec, dai, 1);
1088 if (machine->suspend_pre)
1089 machine->suspend_pre(pdev, state);
1091 for(i = 0; i < machine->num_links; i++) {
1092 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1093 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
1094 cpu_dai->suspend(pdev, cpu_dai);
1095 if (platform->suspend)
1096 platform->suspend(pdev, cpu_dai);
1099 /* close any waiting streams and save state */
1100 flush_workqueue(soc_workq);
1101 codec->suspend_dapm_state = codec->dapm_state;
1103 for(i = 0; i < codec->num_dai; i++) {
1104 char *stream = codec->dai[i].playback.stream_name;
1106 snd_soc_dapm_stream_event(codec, stream,
1107 SND_SOC_DAPM_STREAM_SUSPEND);
1108 stream = codec->dai[i].capture.stream_name;
1110 snd_soc_dapm_stream_event(codec, stream,
1111 SND_SOC_DAPM_STREAM_SUSPEND);
1114 if (codec_dev->suspend)
1115 codec_dev->suspend(pdev, state);
1117 for(i = 0; i < machine->num_links; i++) {
1118 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1119 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
1120 cpu_dai->suspend(pdev, cpu_dai);
1123 if (machine->suspend_post)
1124 machine->suspend_post(pdev, state);
1129 /* powers up audio subsystem after a suspend */
1130 static int soc_resume(struct platform_device *pdev)
1132 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1133 struct snd_soc_machine *machine = socdev->machine;
1134 struct snd_soc_platform *platform = socdev->platform;
1135 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1136 struct snd_soc_codec *codec = socdev->codec;
1139 if (machine->resume_pre)
1140 machine->resume_pre(pdev);
1142 for(i = 0; i < machine->num_links; i++) {
1143 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1144 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
1145 cpu_dai->resume(pdev, cpu_dai);
1148 if (codec_dev->resume)
1149 codec_dev->resume(pdev);
1151 for(i = 0; i < codec->num_dai; i++) {
1152 char* stream = codec->dai[i].playback.stream_name;
1154 snd_soc_dapm_stream_event(codec, stream,
1155 SND_SOC_DAPM_STREAM_RESUME);
1156 stream = codec->dai[i].capture.stream_name;
1158 snd_soc_dapm_stream_event(codec, stream,
1159 SND_SOC_DAPM_STREAM_RESUME);
1162 /* unmute any active DAC's */
1163 for(i = 0; i < machine->num_links; i++) {
1164 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1165 if (dai->digital_mute && dai->playback.active)
1166 dai->digital_mute(codec, dai, 0);
1169 for(i = 0; i < machine->num_links; i++) {
1170 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1171 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
1172 cpu_dai->resume(pdev, cpu_dai);
1173 if (platform->resume)
1174 platform->resume(pdev, cpu_dai);
1177 if (machine->resume_post)
1178 machine->resume_post(pdev);
1184 #define soc_suspend NULL
1185 #define soc_resume NULL
1188 /* probes a new socdev */
1189 static int soc_probe(struct platform_device *pdev)
1192 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1193 struct snd_soc_machine *machine = socdev->machine;
1194 struct snd_soc_platform *platform = socdev->platform;
1195 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1197 if (machine->probe) {
1198 ret = machine->probe(pdev);
1203 for (i = 0; i < machine->num_links; i++) {
1204 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1205 if (cpu_dai->probe) {
1206 ret = cpu_dai->probe(pdev);
1212 if (codec_dev->probe) {
1213 ret = codec_dev->probe(pdev);
1218 if (platform->probe) {
1219 ret = platform->probe(pdev);
1224 /* DAPM stream work */
1225 soc_workq = create_workqueue("kdapm");
1226 if (soc_workq == NULL)
1228 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
1232 if (platform->remove)
1233 platform->remove(pdev);
1236 if (codec_dev->remove)
1237 codec_dev->remove(pdev);
1240 for (i--; i > 0; i--) {
1241 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1242 if (cpu_dai->remove)
1243 cpu_dai->remove(pdev);
1246 if (machine->remove)
1247 machine->remove(pdev);
1252 /* removes a socdev */
1253 static int soc_remove(struct platform_device *pdev)
1256 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1257 struct snd_soc_machine *machine = socdev->machine;
1258 struct snd_soc_platform *platform = socdev->platform;
1259 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1262 destroy_workqueue(soc_workq);
1264 if (platform->remove)
1265 platform->remove(pdev);
1267 if (codec_dev->remove)
1268 codec_dev->remove(pdev);
1270 for (i = 0; i < machine->num_links; i++) {
1271 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1272 if (cpu_dai->remove)
1273 cpu_dai->remove(pdev);
1276 if (machine->remove)
1277 machine->remove(pdev);
1282 /* ASoC platform driver */
1283 static struct platform_driver soc_driver = {
1285 .name = "soc-audio",
1288 .remove = soc_remove,
1289 .suspend = soc_suspend,
1290 .resume = soc_resume,
1293 /* create a new pcm */
1294 static int soc_new_pcm(struct snd_soc_device *socdev,
1295 struct snd_soc_dai_link *dai_link, int num)
1297 struct snd_soc_codec *codec = socdev->codec;
1298 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
1299 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
1300 struct snd_soc_pcm_runtime *rtd;
1301 struct snd_pcm *pcm;
1303 int ret = 0, playback = 0, capture = 0;
1305 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1308 rtd->cpu_dai = cpu_dai;
1309 rtd->codec_dai = codec_dai;
1310 rtd->socdev = socdev;
1312 /* check client and interface hw capabilities */
1313 sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
1314 get_dai_name(cpu_dai->type), num);
1316 if (codec_dai->playback.channels_min)
1318 if (codec_dai->capture.channels_min)
1321 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1324 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1329 pcm->private_data = rtd;
1330 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
1331 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
1332 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
1333 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
1334 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
1335 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
1336 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
1339 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1342 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1344 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
1346 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1351 pcm->private_free = socdev->platform->pcm_free;
1352 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1357 /* codec register dump */
1358 static ssize_t codec_reg_show(struct device *dev,
1359 struct device_attribute *attr, char *buf)
1361 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1362 struct snd_soc_codec *codec = devdata->codec;
1363 int i, step = 1, count = 0;
1365 if (!codec->reg_cache_size)
1368 if (codec->reg_cache_step)
1369 step = codec->reg_cache_step;
1371 count += sprintf(buf, "%s registers\n", codec->name);
1372 for(i = 0; i < codec->reg_cache_size; i += step)
1373 count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
1377 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1380 * snd_soc_new_ac97_codec - initailise AC97 device
1381 * @codec: audio codec
1382 * @ops: AC97 bus operations
1383 * @num: AC97 codec number
1385 * Initialises AC97 codec resources for use by ad-hoc devices only.
1387 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1388 struct snd_ac97_bus_ops *ops, int num)
1390 mutex_lock(&codec->mutex);
1392 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1393 if (codec->ac97 == NULL) {
1394 mutex_unlock(&codec->mutex);
1398 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1399 if (codec->ac97->bus == NULL) {
1402 mutex_unlock(&codec->mutex);
1406 codec->ac97->bus->ops = ops;
1407 codec->ac97->num = num;
1408 mutex_unlock(&codec->mutex);
1411 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1414 * snd_soc_free_ac97_codec - free AC97 codec device
1415 * @codec: audio codec
1417 * Frees AC97 codec device resources.
1419 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1421 mutex_lock(&codec->mutex);
1422 kfree(codec->ac97->bus);
1425 mutex_unlock(&codec->mutex);
1427 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1430 * snd_soc_update_bits - update codec register bits
1431 * @codec: audio codec
1432 * @reg: codec register
1433 * @mask: register mask
1436 * Writes new register value.
1438 * Returns 1 for change else 0.
1440 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1441 unsigned short mask, unsigned short value)
1444 unsigned short old, new;
1446 mutex_lock(&io_mutex);
1447 old = snd_soc_read(codec, reg);
1448 new = (old & ~mask) | value;
1449 change = old != new;
1451 snd_soc_write(codec, reg, new);
1453 mutex_unlock(&io_mutex);
1456 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1459 * snd_soc_test_bits - test register for change
1460 * @codec: audio codec
1461 * @reg: codec register
1462 * @mask: register mask
1465 * Tests a register with a new value and checks if the new value is
1466 * different from the old value.
1468 * Returns 1 for change else 0.
1470 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1471 unsigned short mask, unsigned short value)
1474 unsigned short old, new;
1476 mutex_lock(&io_mutex);
1477 old = snd_soc_read(codec, reg);
1478 new = (old & ~mask) | value;
1479 change = old != new;
1480 mutex_unlock(&io_mutex);
1484 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1487 * snd_soc_get_rate - get int sample rate
1488 * @hwpcmrate: the hardware pcm rate
1490 * Returns the audio rate integaer value, else 0.
1492 int snd_soc_get_rate(int hwpcmrate)
1494 int rate = ffs(hwpcmrate) - 1;
1496 if (rate > ARRAY_SIZE(rates))
1500 EXPORT_SYMBOL_GPL(snd_soc_get_rate);
1503 * snd_soc_new_pcms - create new sound card and pcms
1504 * @socdev: the SoC audio device
1506 * Create a new sound card based upon the codec and interface pcms.
1508 * Returns 0 for success, else error.
1510 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
1512 struct snd_soc_codec *codec = socdev->codec;
1513 struct snd_soc_machine *machine = socdev->machine;
1516 mutex_lock(&codec->mutex);
1518 /* register a sound card */
1519 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1521 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1523 mutex_unlock(&codec->mutex);
1527 codec->card->dev = socdev->dev;
1528 codec->card->private_data = codec;
1529 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1531 /* create the pcms */
1532 for(i = 0; i < machine->num_links; i++) {
1533 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1535 printk(KERN_ERR "asoc: can't create pcm %s\n",
1536 machine->dai_link[i].stream_name);
1537 mutex_unlock(&codec->mutex);
1542 mutex_unlock(&codec->mutex);
1545 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1548 * snd_soc_register_card - register sound card
1549 * @socdev: the SoC audio device
1551 * Register a SoC sound card. Also registers an AC97 device if the
1552 * codec is AC97 for ad hoc devices.
1554 * Returns 0 for success, else error.
1556 int snd_soc_register_card(struct snd_soc_device *socdev)
1558 struct snd_soc_codec *codec = socdev->codec;
1559 struct snd_soc_machine *machine = socdev->machine;
1560 int ret = 0, i, ac97 = 0, err = 0;
1562 mutex_lock(&codec->mutex);
1563 for(i = 0; i < machine->num_links; i++) {
1564 if (socdev->machine->dai_link[i].init) {
1565 err = socdev->machine->dai_link[i].init(codec);
1567 printk(KERN_ERR "asoc: failed to init %s\n",
1568 socdev->machine->dai_link[i].stream_name);
1572 if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1575 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1576 "%s", machine->name);
1577 snprintf(codec->card->longname, sizeof(codec->card->longname),
1578 "%s (%s)", machine->name, codec->name);
1580 ret = snd_card_register(codec->card);
1582 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1587 #ifdef CONFIG_SND_SOC_AC97_BUS
1589 ret = soc_ac97_dev_register(codec);
1591 printk(KERN_ERR "asoc: AC97 device register failed\n");
1592 snd_card_free(codec->card);
1598 err = snd_soc_dapm_sys_add(socdev->dev);
1600 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1602 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1604 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1606 mutex_unlock(&codec->mutex);
1609 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1612 * snd_soc_free_pcms - free sound card and pcms
1613 * @socdev: the SoC audio device
1615 * Frees sound card and pcms associated with the socdev.
1616 * Also unregister the codec if it is an AC97 device.
1618 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1620 struct snd_soc_codec *codec = socdev->codec;
1622 mutex_lock(&codec->mutex);
1623 #ifdef CONFIG_SND_SOC_AC97_BUS
1625 soc_ac97_dev_unregister(codec);
1629 snd_card_free(codec->card);
1630 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1631 mutex_unlock(&codec->mutex);
1633 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1636 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1637 * @substream: the pcm substream
1638 * @hw: the hardware parameters
1640 * Sets the substream runtime hardware parameters.
1642 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1643 const struct snd_pcm_hardware *hw)
1645 struct snd_pcm_runtime *runtime = substream->runtime;
1646 runtime->hw.info = hw->info;
1647 runtime->hw.formats = hw->formats;
1648 runtime->hw.period_bytes_min = hw->period_bytes_min;
1649 runtime->hw.period_bytes_max = hw->period_bytes_max;
1650 runtime->hw.periods_min = hw->periods_min;
1651 runtime->hw.periods_max = hw->periods_max;
1652 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1653 runtime->hw.fifo_size = hw->fifo_size;
1656 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1659 * snd_soc_cnew - create new control
1660 * @_template: control template
1661 * @data: control private data
1662 * @lnng_name: control long name
1664 * Create a new mixer control from a template control.
1666 * Returns 0 for success, else error.
1668 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1669 void *data, char *long_name)
1671 struct snd_kcontrol_new template;
1673 memcpy(&template, _template, sizeof(template));
1675 template.name = long_name;
1676 template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1679 return snd_ctl_new1(&template, data);
1681 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1684 * snd_soc_info_enum_double - enumerated double mixer info callback
1685 * @kcontrol: mixer control
1686 * @uinfo: control element information
1688 * Callback to provide information about a double enumerated
1691 * Returns 0 for success.
1693 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1694 struct snd_ctl_elem_info *uinfo)
1696 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1698 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1699 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1700 uinfo->value.enumerated.items = e->mask;
1702 if (uinfo->value.enumerated.item > e->mask - 1)
1703 uinfo->value.enumerated.item = e->mask - 1;
1704 strcpy(uinfo->value.enumerated.name,
1705 e->texts[uinfo->value.enumerated.item]);
1708 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1711 * snd_soc_get_enum_double - enumerated double mixer get callback
1712 * @kcontrol: mixer control
1713 * @uinfo: control element information
1715 * Callback to get the value of a double enumerated mixer.
1717 * Returns 0 for success.
1719 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol)
1722 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1723 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1724 unsigned short val, bitmask;
1726 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1728 val = snd_soc_read(codec, e->reg);
1729 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1730 if (e->shift_l != e->shift_r)
1731 ucontrol->value.enumerated.item[1] =
1732 (val >> e->shift_r) & (bitmask - 1);
1736 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1739 * snd_soc_put_enum_double - enumerated double mixer put callback
1740 * @kcontrol: mixer control
1741 * @uinfo: control element information
1743 * Callback to set the value of a double enumerated mixer.
1745 * Returns 0 for success.
1747 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
1750 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1751 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1753 unsigned short mask, bitmask;
1755 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1757 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1759 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1760 mask = (bitmask - 1) << e->shift_l;
1761 if (e->shift_l != e->shift_r) {
1762 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1764 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1765 mask |= (bitmask - 1) << e->shift_r;
1768 return snd_soc_update_bits(codec, e->reg, mask, val);
1770 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1773 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1774 * @kcontrol: mixer control
1775 * @uinfo: control element information
1777 * Callback to provide information about an external enumerated
1780 * Returns 0 for success.
1782 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_info *uinfo)
1785 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1787 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1789 uinfo->value.enumerated.items = e->mask;
1791 if (uinfo->value.enumerated.item > e->mask - 1)
1792 uinfo->value.enumerated.item = e->mask - 1;
1793 strcpy(uinfo->value.enumerated.name,
1794 e->texts[uinfo->value.enumerated.item]);
1797 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1800 * snd_soc_info_volsw_ext - external single mixer info callback
1801 * @kcontrol: mixer control
1802 * @uinfo: control element information
1804 * Callback to provide information about a single external mixer control.
1806 * Returns 0 for success.
1808 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_info *uinfo)
1811 int mask = kcontrol->private_value;
1814 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1816 uinfo->value.integer.min = 0;
1817 uinfo->value.integer.max = mask;
1820 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1823 * snd_soc_info_bool_ext - external single boolean mixer info callback
1824 * @kcontrol: mixer control
1825 * @uinfo: control element information
1827 * Callback to provide information about a single boolean external mixer control.
1829 * Returns 0 for success.
1831 int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
1832 struct snd_ctl_elem_info *uinfo)
1834 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1836 uinfo->value.integer.min = 0;
1837 uinfo->value.integer.max = 1;
1840 EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
1843 * snd_soc_info_volsw - single mixer info callback
1844 * @kcontrol: mixer control
1845 * @uinfo: control element information
1847 * Callback to provide information about a single mixer control.
1849 * Returns 0 for success.
1851 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1852 struct snd_ctl_elem_info *uinfo)
1854 int mask = (kcontrol->private_value >> 16) & 0xff;
1855 int shift = (kcontrol->private_value >> 8) & 0x0f;
1856 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1859 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1860 uinfo->count = shift == rshift ? 1 : 2;
1861 uinfo->value.integer.min = 0;
1862 uinfo->value.integer.max = mask;
1865 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1868 * snd_soc_get_volsw - single mixer get callback
1869 * @kcontrol: mixer control
1870 * @uinfo: control element information
1872 * Callback to get the value of a single mixer control.
1874 * Returns 0 for success.
1876 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
1879 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1880 int reg = kcontrol->private_value & 0xff;
1881 int shift = (kcontrol->private_value >> 8) & 0x0f;
1882 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1883 int mask = (kcontrol->private_value >> 16) & 0xff;
1884 int invert = (kcontrol->private_value >> 24) & 0x01;
1886 ucontrol->value.integer.value[0] =
1887 (snd_soc_read(codec, reg) >> shift) & mask;
1888 if (shift != rshift)
1889 ucontrol->value.integer.value[1] =
1890 (snd_soc_read(codec, reg) >> rshift) & mask;
1892 ucontrol->value.integer.value[0] =
1893 mask - ucontrol->value.integer.value[0];
1894 if (shift != rshift)
1895 ucontrol->value.integer.value[1] =
1896 mask - ucontrol->value.integer.value[1];
1901 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1904 * snd_soc_put_volsw - single mixer put callback
1905 * @kcontrol: mixer control
1906 * @uinfo: control element information
1908 * Callback to set the value of a single mixer control.
1910 * Returns 0 for success.
1912 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1913 struct snd_ctl_elem_value *ucontrol)
1915 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1916 int reg = kcontrol->private_value & 0xff;
1917 int shift = (kcontrol->private_value >> 8) & 0x0f;
1918 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1919 int mask = (kcontrol->private_value >> 16) & 0xff;
1920 int invert = (kcontrol->private_value >> 24) & 0x01;
1922 unsigned short val, val2, val_mask;
1924 val = (ucontrol->value.integer.value[0] & mask);
1927 val_mask = mask << shift;
1929 if (shift != rshift) {
1930 val2 = (ucontrol->value.integer.value[1] & mask);
1933 val_mask |= mask << rshift;
1934 val |= val2 << rshift;
1936 err = snd_soc_update_bits(codec, reg, val_mask, val);
1939 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1942 * snd_soc_info_volsw_2r - double mixer info callback
1943 * @kcontrol: mixer control
1944 * @uinfo: control element information
1946 * Callback to provide information about a double mixer control that
1947 * spans 2 codec registers.
1949 * Returns 0 for success.
1951 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_info *uinfo)
1954 int mask = (kcontrol->private_value >> 12) & 0xff;
1957 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1959 uinfo->value.integer.min = 0;
1960 uinfo->value.integer.max = mask;
1963 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1966 * snd_soc_get_volsw_2r - double mixer get callback
1967 * @kcontrol: mixer control
1968 * @uinfo: control element information
1970 * Callback to get the value of a double mixer control that spans 2 registers.
1972 * Returns 0 for success.
1974 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1975 struct snd_ctl_elem_value *ucontrol)
1977 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1978 int reg = kcontrol->private_value & 0xff;
1979 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1980 int shift = (kcontrol->private_value >> 8) & 0x0f;
1981 int mask = (kcontrol->private_value >> 12) & 0xff;
1982 int invert = (kcontrol->private_value >> 20) & 0x01;
1984 ucontrol->value.integer.value[0] =
1985 (snd_soc_read(codec, reg) >> shift) & mask;
1986 ucontrol->value.integer.value[1] =
1987 (snd_soc_read(codec, reg2) >> shift) & mask;
1989 ucontrol->value.integer.value[0] =
1990 mask - ucontrol->value.integer.value[0];
1991 ucontrol->value.integer.value[1] =
1992 mask - ucontrol->value.integer.value[1];
1997 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2000 * snd_soc_put_volsw_2r - double mixer set callback
2001 * @kcontrol: mixer control
2002 * @uinfo: control element information
2004 * Callback to set the value of a double mixer control that spans 2 registers.
2006 * Returns 0 for success.
2008 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2009 struct snd_ctl_elem_value *ucontrol)
2011 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2012 int reg = kcontrol->private_value & 0xff;
2013 int reg2 = (kcontrol->private_value >> 24) & 0xff;
2014 int shift = (kcontrol->private_value >> 8) & 0x0f;
2015 int mask = (kcontrol->private_value >> 12) & 0xff;
2016 int invert = (kcontrol->private_value >> 20) & 0x01;
2018 unsigned short val, val2, val_mask;
2020 val_mask = mask << shift;
2021 val = (ucontrol->value.integer.value[0] & mask);
2022 val2 = (ucontrol->value.integer.value[1] & mask);
2030 val2 = val2 << shift;
2032 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
2035 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2038 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2040 static int __devinit snd_soc_init(void)
2042 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
2043 return platform_driver_register(&soc_driver);
2046 static void snd_soc_exit(void)
2048 platform_driver_unregister(&soc_driver);
2051 module_init(snd_soc_init);
2052 module_exit(snd_soc_exit);
2054 /* Module information */
2055 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
2056 MODULE_DESCRIPTION("ALSA SoC Core");
2057 MODULE_LICENSE("GPL");