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 struct workqueue_struct *soc_workq;
63 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
65 /* supported sample rates */
66 /* ATTENTION: these values depend on the definition in pcm.h! */
67 static const unsigned int rates[] = {
68 5512, 8000, 11025, 16000, 22050, 32000, 44100,
69 48000, 64000, 88200, 96000, 176400, 192000
73 * This is a timeout to do a DAPM powerdown after a stream is closed().
74 * It can be used to eliminate pops between different playback streams, e.g.
75 * between two audio tracks.
77 static int pmdown_time = 5000;
78 module_param(pmdown_time, int, 0);
79 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
81 #ifdef CONFIG_SND_SOC_AC97_BUS
82 /* unregister ac97 codec */
83 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
85 if (codec->ac97->dev.bus)
86 device_unregister(&codec->ac97->dev);
90 /* stop no dev release warning */
91 static void soc_ac97_device_release(struct device *dev){}
93 /* register ac97 codec to bus */
94 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
98 codec->ac97->dev.bus = &ac97_bus_type;
99 codec->ac97->dev.parent = NULL;
100 codec->ac97->dev.release = soc_ac97_device_release;
102 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
103 codec->card->number, 0, codec->name);
104 err = device_register(&codec->ac97->dev);
106 snd_printk(KERN_ERR "Can't register ac97 bus\n");
107 codec->ac97->dev.bus = NULL;
114 static inline const char* get_dai_name(int type)
117 case SND_SOC_DAI_AC97:
119 case SND_SOC_DAI_I2S:
121 case SND_SOC_DAI_PCM:
127 /* get rate format from rate */
128 static inline int soc_get_rate_format(int rate)
132 for (i = 0; i < ARRAY_SIZE(rates); i++) {
133 if (rates[i] == rate)
139 /* gets the audio system mclk/sysclk for the given parameters */
140 static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
141 struct snd_soc_clock_info *info)
143 struct snd_soc_device *socdev = rtd->socdev;
144 struct snd_soc_machine *machine = socdev->machine;
147 /* find the matching machine config and get it's mclk for the given
148 * sample rate and hardware format */
149 for(i = 0; i < machine->num_links; i++) {
150 if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
151 machine->dai_link[i].config_sysclk)
152 return machine->dai_link[i].config_sysclk(rtd, info);
157 /* changes a bitclk multiplier mask to a divider mask */
158 static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
159 unsigned int pcmfmt, unsigned int chn)
163 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
168 /* the minimum bit clock that has enough bandwidth */
169 min = size * rate * chn;
170 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
172 for (i = 0; i < 64; i++) {
173 if ((bfs >> i) & 0x1) {
175 bfs_ |= SND_SOC_FSBD(mclk/j);
176 dbgc("rcw --> div support mult %d\n",
177 SND_SOC_FSBD_REAL(1<<i));
184 /* changes a bitclk divider mask to a multiplier mask */
185 static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
186 unsigned int pcmfmt, unsigned int chn)
191 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
196 /* the minimum bit clock that has enough bandwidth */
197 min = size * rate * chn;
198 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
200 for (i = 0; i < 64; i++) {
201 if ((bfs >> i) & 0x1) {
204 bfs_ |= SND_SOC_FSBW(j/min);
205 dbgc("div --> rcw support div %d\n",
206 SND_SOC_FSBW_REAL(1<<i));
214 /* changes a constant bitclk to a multiplier mask */
215 static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
216 unsigned int pcmfmt, unsigned int chn)
218 unsigned int bfs_ = rate * bfs;
219 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
224 /* the minimum bit clock that has enough bandwidth */
225 min = size * rate * chn;
226 dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
231 bfs_ = SND_SOC_FSBW(bfs_/min);
232 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
237 /* changes a bitclk multiplier mask to a divider mask */
238 static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
239 unsigned int pcmfmt, unsigned int chn)
241 unsigned int bfs_ = rate * bfs;
242 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
247 /* the minimum bit clock that has enough bandwidth */
248 min = size * rate * chn;
249 dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
254 bfs_ = SND_SOC_FSBW(mclk/bfs_);
255 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
260 /* Matches codec DAI and SoC CPU DAI hardware parameters */
261 static int soc_hw_match_params(struct snd_pcm_substream *substream,
262 struct snd_pcm_hw_params *params)
264 struct snd_soc_pcm_runtime *rtd = substream->private_data;
265 struct snd_soc_dai_mode *codec_dai_mode = NULL;
266 struct snd_soc_dai_mode *cpu_dai_mode = NULL;
267 struct snd_soc_clock_info clk_info;
268 unsigned int fs, mclk, rate = params_rate(params),
269 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
271 u64 codec_bfs, cpu_bfs;
273 dbg("asoc: match version %s\n", SND_SOC_VERSION);
274 clk_info.rate = rate;
275 pcmrate = soc_get_rate_format(rate);
277 /* try and find a match from the codec and cpu DAI capabilities */
278 for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
279 for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
280 codec_dai_mode = &rtd->codec_dai->caps.mode[j];
281 cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
283 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
285 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
289 fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
290 if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
291 dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
295 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
296 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
301 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
302 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
306 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
307 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
311 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
312 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
316 /* todo - still need to add tdm selection */
317 rtd->cpu_dai->dai_runtime.fmt =
318 rtd->codec_dai->dai_runtime.fmt =
319 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
320 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
321 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
322 clk_info.bclk_master =
323 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
325 /* make sure the ratio between rate and master
326 * clock is acceptable*/
327 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
329 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
332 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
333 rtd->codec_dai->dai_runtime.fs = fs;
335 /* calculate audio system clocking using slowest clocks possible*/
336 mclk = soc_get_mclk(rtd, &clk_info);
338 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
339 dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
340 clk_info.bclk_master);
344 /* calculate word size (per channel) and frame size */
345 rtd->codec_dai->dai_runtime.pcmfmt =
346 rtd->cpu_dai->dai_runtime.pcmfmt =
347 1 << params_format(params);
349 chn = params_channels(params);
350 /* i2s always has left and right */
351 if (params_channels(params) == 1 &&
352 rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
353 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
356 /* Calculate bfs - the ratio between bitclock and the sample rate
357 * We must take into consideration the dividers and multipliers
358 * used in the codec and cpu DAI modes. We always choose the
359 * lowest possible clocks to reduce power.
361 switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
362 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
363 /* cpu & codec bfs dividers */
364 rtd->cpu_dai->dai_runtime.bfs =
365 rtd->codec_dai->dai_runtime.bfs =
366 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
368 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
369 /* normalise bfs codec divider & cpu rcw mult */
370 codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
371 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
372 rtd->cpu_dai->dai_runtime.bfs =
373 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
374 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
375 rtd->codec_dai->dai_runtime.pcmfmt, chn);
376 rtd->codec_dai->dai_runtime.bfs =
377 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
379 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
380 /* normalise bfs codec rcw mult & cpu divider */
381 codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
382 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
383 rtd->cpu_dai->dai_runtime.bfs =
384 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
385 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
386 rtd->codec_dai->dai_runtime.pcmfmt, chn);
387 rtd->codec_dai->dai_runtime.bfs =
388 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
390 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
391 /* codec & cpu bfs rate rcw multipliers */
392 rtd->cpu_dai->dai_runtime.bfs =
393 rtd->codec_dai->dai_runtime.bfs =
394 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
396 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
397 /* normalise cpu bfs rate const multiplier & codec div */
398 cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
399 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
400 if(codec_dai_mode->bfs & cpu_bfs) {
401 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
402 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
404 rtd->cpu_dai->dai_runtime.bfs = 0;
406 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
407 /* normalise cpu bfs rate const multiplier & codec rcw mult */
408 cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
409 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
410 if(codec_dai_mode->bfs & cpu_bfs) {
411 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
412 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
414 rtd->cpu_dai->dai_runtime.bfs = 0;
416 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
417 /* normalise cpu bfs rate rcw multiplier & codec const mult */
418 codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
419 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
420 if(cpu_dai_mode->bfs & codec_bfs) {
421 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
422 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
424 rtd->cpu_dai->dai_runtime.bfs = 0;
426 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
427 /* normalise cpu bfs div & codec const mult */
428 codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
429 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
430 if(cpu_dai_mode->bfs & codec_bfs) {
431 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
432 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
434 rtd->cpu_dai->dai_runtime.bfs = 0;
436 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
437 /* cpu & codec constant mult */
438 if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
439 rtd->cpu_dai->dai_runtime.bfs =
440 rtd->codec_dai->dai_runtime.bfs =
443 rtd->cpu_dai->dai_runtime.bfs =
444 rtd->codec_dai->dai_runtime.bfs = 0;
448 /* make sure the bit clock speed is acceptable */
449 if (!rtd->cpu_dai->dai_runtime.bfs ||
450 !rtd->codec_dai->dai_runtime.bfs) {
451 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
452 dbgc("asoc: cpu_dai %llu codec %llu\n",
453 rtd->cpu_dai->dai_runtime.bfs,
454 rtd->codec_dai->dai_runtime.bfs);
455 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
462 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
466 /* we have matching DAI's, so complete the runtime info */
467 rtd->codec_dai->dai_runtime.pcmrate =
468 rtd->cpu_dai->dai_runtime.pcmrate =
469 soc_get_rate_format(rate);
471 rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
472 rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
473 rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
474 rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
477 dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
478 if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
479 codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
480 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
481 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
482 rtd->codec_dai->dai_runtime.fs, mclk,
483 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
484 } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
485 codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
486 dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
487 rtd->codec_dai->dai_runtime.fs, mclk,
488 rtd->codec_dai->dai_runtime.bfs, codec_bclk);
489 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
490 codec_bclk = params_rate(params) * params_channels(params) *
491 snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
492 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
493 dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
494 rtd->codec_dai->dai_runtime.fs, mclk,
495 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
499 if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
500 cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
501 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
502 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
503 rtd->cpu_dai->dai_runtime.fs, mclk,
504 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
505 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
506 cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
507 dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
508 rtd->cpu_dai->dai_runtime.fs, mclk,
509 rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
510 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
511 cpu_bclk = params_rate(params) * params_channels(params) *
512 snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
513 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
514 dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
515 rtd->cpu_dai->dai_runtime.fs, mclk,
516 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
521 * Check we have matching bitclocks. If we don't then it means the
522 * sysclock returned by either the codec or cpu DAI (selected by the
523 * machine sysclock function) is wrong compared with the supported DAI
524 * modes for the codec or cpu DAI. Check your codec or CPU DAI
525 * config_sysclock() functions.
527 if (cpu_bclk != codec_bclk && cpu_bclk){
529 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
531 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
534 switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
535 case SND_SOC_DAIFMT_CBM_CFM:
536 dbg("asoc: DAI codec BCLK master, LRC master\n");
538 case SND_SOC_DAIFMT_CBS_CFM:
539 dbg("asoc: DAI codec BCLK slave, LRC master\n");
541 case SND_SOC_DAIFMT_CBM_CFS:
542 dbg("asoc: DAI codec BCLK master, LRC slave\n");
544 case SND_SOC_DAIFMT_CBS_CFS:
545 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
548 dbg("asoc: mode %x, invert %x\n",
549 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
550 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
551 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
552 params_channels(params), params_format(params));
557 static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
562 for(i = 0; i < nmodes; i++)
563 rates |= modes[i].pcmrate;
568 static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
573 for(i = 0; i < nmodes; i++)
574 formats |= modes[i].pcmfmt;
580 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
581 * then initialized and any private data can be allocated. This also calls
582 * startup for the cpu DAI, platform, machine and codec DAI.
584 static int soc_pcm_open(struct snd_pcm_substream *substream)
586 struct snd_soc_pcm_runtime *rtd = substream->private_data;
587 struct snd_soc_device *socdev = rtd->socdev;
588 struct snd_pcm_runtime *runtime = substream->runtime;
589 struct snd_soc_machine *machine = socdev->machine;
590 struct snd_soc_platform *platform = socdev->platform;
591 struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
592 struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
595 mutex_lock(&pcm_mutex);
597 /* startup the audio subsystem */
598 if (rtd->cpu_dai->ops.startup) {
599 ret = rtd->cpu_dai->ops.startup(substream);
601 printk(KERN_ERR "asoc: can't open interface %s\n",
607 if (platform->pcm_ops->open) {
608 ret = platform->pcm_ops->open(substream);
610 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
615 if (machine->ops && machine->ops->startup) {
616 ret = machine->ops->startup(substream);
618 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
623 if (rtd->codec_dai->ops.startup) {
624 ret = rtd->codec_dai->ops.startup(substream);
626 printk(KERN_ERR "asoc: can't open codec %s\n",
627 rtd->codec_dai->name);
632 /* create runtime params from DMA, codec and cpu DAI */
633 if (runtime->hw.rates)
635 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
636 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
639 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
640 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
641 if (runtime->hw.formats)
642 runtime->hw.formats &=
643 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
644 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
646 runtime->hw.formats =
647 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
648 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
650 /* Check that the codec and cpu DAI's are compatible */
651 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
652 runtime->hw.rate_min =
653 max(rtd->codec_dai->playback.rate_min,
654 rtd->cpu_dai->playback.rate_min);
655 runtime->hw.rate_max =
656 min(rtd->codec_dai->playback.rate_max,
657 rtd->cpu_dai->playback.rate_max);
658 runtime->hw.channels_min =
659 max(rtd->codec_dai->playback.channels_min,
660 rtd->cpu_dai->playback.channels_min);
661 runtime->hw.channels_max =
662 min(rtd->codec_dai->playback.channels_max,
663 rtd->cpu_dai->playback.channels_max);
665 runtime->hw.rate_min =
666 max(rtd->codec_dai->capture.rate_min,
667 rtd->cpu_dai->capture.rate_min);
668 runtime->hw.rate_max =
669 min(rtd->codec_dai->capture.rate_max,
670 rtd->cpu_dai->capture.rate_max);
671 runtime->hw.channels_min =
672 max(rtd->codec_dai->capture.channels_min,
673 rtd->cpu_dai->capture.channels_min);
674 runtime->hw.channels_max =
675 min(rtd->codec_dai->capture.channels_max,
676 rtd->cpu_dai->capture.channels_max);
679 snd_pcm_limit_hw_rates(runtime);
680 if (!runtime->hw.rates) {
681 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
682 rtd->codec_dai->name, rtd->cpu_dai->name);
685 if (!runtime->hw.formats) {
686 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
687 rtd->codec_dai->name, rtd->cpu_dai->name);
690 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
691 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
692 rtd->codec_dai->name, rtd->cpu_dai->name);
696 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
697 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
698 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
699 runtime->hw.channels_max);
700 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
701 runtime->hw.rate_max);
704 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
705 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
707 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
708 rtd->cpu_dai->active = rtd->codec_dai->active = 1;
709 rtd->cpu_dai->runtime = runtime;
710 socdev->codec->active++;
711 mutex_unlock(&pcm_mutex);
715 if (machine->ops && machine->ops->shutdown)
716 machine->ops->shutdown(substream);
719 if (platform->pcm_ops->close)
720 platform->pcm_ops->close(substream);
723 if (rtd->cpu_dai->ops.shutdown)
724 rtd->cpu_dai->ops.shutdown(substream);
726 mutex_unlock(&pcm_mutex);
731 * Power down the audio subsytem pmdown_time msecs after close is called.
732 * This is to ensure there are no pops or clicks in between any music tracks
733 * due to DAPM power cycling.
735 static void close_delayed_work(struct work_struct *work)
737 struct snd_soc_device *socdev =
738 container_of(work, struct snd_soc_device, delayed_work.work);
739 struct snd_soc_codec *codec = socdev->codec;
740 struct snd_soc_codec_dai *codec_dai;
743 mutex_lock(&pcm_mutex);
744 for(i = 0; i < codec->num_dai; i++) {
745 codec_dai = &codec->dai[i];
747 dbg("pop wq checking: %s status: %s waiting: %s\n",
748 codec_dai->playback.stream_name,
749 codec_dai->playback.active ? "active" : "inactive",
750 codec_dai->pop_wait ? "yes" : "no");
752 /* are we waiting on this codec DAI stream */
753 if (codec_dai->pop_wait == 1) {
755 codec_dai->pop_wait = 0;
756 snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
757 SND_SOC_DAPM_STREAM_STOP);
759 /* power down the codec power domain if no longer active */
760 if (codec->active == 0) {
761 dbg("pop wq D3 %s %s\n", codec->name,
762 codec_dai->playback.stream_name);
763 if (codec->dapm_event)
764 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
768 mutex_unlock(&pcm_mutex);
772 * Called by ALSA when a PCM substream is closed. Private data can be
773 * freed here. The cpu DAI, codec DAI, machine and platform are also
776 static int soc_codec_close(struct snd_pcm_substream *substream)
778 struct snd_soc_pcm_runtime *rtd = substream->private_data;
779 struct snd_soc_device *socdev = rtd->socdev;
780 struct snd_soc_machine *machine = socdev->machine;
781 struct snd_soc_platform *platform = socdev->platform;
782 struct snd_soc_codec *codec = socdev->codec;
784 mutex_lock(&pcm_mutex);
786 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
787 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
789 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
791 if (rtd->codec_dai->playback.active == 0 &&
792 rtd->codec_dai->capture.active == 0) {
793 rtd->cpu_dai->active = rtd->codec_dai->active = 0;
797 if (rtd->cpu_dai->ops.shutdown)
798 rtd->cpu_dai->ops.shutdown(substream);
800 if (rtd->codec_dai->ops.shutdown)
801 rtd->codec_dai->ops.shutdown(substream);
803 if (machine->ops && machine->ops->shutdown)
804 machine->ops->shutdown(substream);
806 if (platform->pcm_ops->close)
807 platform->pcm_ops->close(substream);
808 rtd->cpu_dai->runtime = NULL;
810 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
811 /* start delayed pop wq here for playback streams */
812 rtd->codec_dai->pop_wait = 1;
813 queue_delayed_work(soc_workq, &socdev->delayed_work,
814 msecs_to_jiffies(pmdown_time));
816 /* capture streams can be powered down now */
817 snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
818 SND_SOC_DAPM_STREAM_STOP);
820 if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){
821 if (codec->dapm_event)
822 codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
826 mutex_unlock(&pcm_mutex);
831 * Called by ALSA when the PCM substream is prepared, can set format, sample
832 * rate, etc. This function is non atomic and can be called multiple times,
833 * it can refer to the runtime info.
835 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
837 struct snd_soc_pcm_runtime *rtd = substream->private_data;
838 struct snd_soc_device *socdev = rtd->socdev;
839 struct snd_soc_platform *platform = socdev->platform;
840 struct snd_soc_codec *codec = socdev->codec;
843 mutex_lock(&pcm_mutex);
844 if (platform->pcm_ops->prepare) {
845 ret = platform->pcm_ops->prepare(substream);
847 printk(KERN_ERR "asoc: platform prepare error\n");
852 if (rtd->codec_dai->ops.prepare) {
853 ret = rtd->codec_dai->ops.prepare(substream);
855 printk(KERN_ERR "asoc: codec DAI prepare error\n");
860 if (rtd->cpu_dai->ops.prepare)
861 ret = rtd->cpu_dai->ops.prepare(substream);
863 /* we only want to start a DAPM playback stream if we are not waiting
864 * on an existing one stopping */
865 if (rtd->codec_dai->pop_wait) {
866 /* we are waiting for the delayed work to start */
867 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
868 snd_soc_dapm_stream_event(codec,
869 rtd->codec_dai->capture.stream_name,
870 SND_SOC_DAPM_STREAM_START);
872 rtd->codec_dai->pop_wait = 0;
873 cancel_delayed_work(&socdev->delayed_work);
874 if (rtd->codec_dai->digital_mute)
875 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
878 /* no delayed work - do we need to power up codec */
879 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
881 if (codec->dapm_event)
882 codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
884 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
885 snd_soc_dapm_stream_event(codec,
886 rtd->codec_dai->playback.stream_name,
887 SND_SOC_DAPM_STREAM_START);
889 snd_soc_dapm_stream_event(codec,
890 rtd->codec_dai->capture.stream_name,
891 SND_SOC_DAPM_STREAM_START);
893 if (codec->dapm_event)
894 codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
895 if (rtd->codec_dai->digital_mute)
896 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
899 /* codec already powered - power on widgets */
900 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
901 snd_soc_dapm_stream_event(codec,
902 rtd->codec_dai->playback.stream_name,
903 SND_SOC_DAPM_STREAM_START);
905 snd_soc_dapm_stream_event(codec,
906 rtd->codec_dai->capture.stream_name,
907 SND_SOC_DAPM_STREAM_START);
908 if (rtd->codec_dai->digital_mute)
909 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
914 mutex_unlock(&pcm_mutex);
919 * Called by ALSA when the hardware params are set by application. This
920 * function can also be called multiple times and can allocate buffers
921 * (using snd_pcm_lib_* ). It's non-atomic.
923 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
924 struct snd_pcm_hw_params *params)
926 struct snd_soc_pcm_runtime *rtd = substream->private_data;
927 struct snd_soc_device *socdev = rtd->socdev;
928 struct snd_soc_platform *platform = socdev->platform;
929 struct snd_soc_machine *machine = socdev->machine;
932 mutex_lock(&pcm_mutex);
934 /* we don't need to match any AC97 params */
935 if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) {
936 ret = soc_hw_match_params(substream, params);
940 struct snd_soc_clock_info clk_info;
941 clk_info.rate = params_rate(params);
942 ret = soc_get_mclk(rtd, &clk_info);
947 if (rtd->codec_dai->ops.hw_params) {
948 ret = rtd->codec_dai->ops.hw_params(substream, params);
950 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
951 rtd->codec_dai->name);
956 if (rtd->cpu_dai->ops.hw_params) {
957 ret = rtd->cpu_dai->ops.hw_params(substream, params);
959 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
965 if (platform->pcm_ops->hw_params) {
966 ret = platform->pcm_ops->hw_params(substream, params);
968 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
974 if (machine->ops && machine->ops->hw_params) {
975 ret = machine->ops->hw_params(substream, params);
977 printk(KERN_ERR "asoc: machine hw_params failed\n");
983 mutex_unlock(&pcm_mutex);
987 if (platform->pcm_ops->hw_free)
988 platform->pcm_ops->hw_free(substream);
991 if (rtd->cpu_dai->ops.hw_free)
992 rtd->cpu_dai->ops.hw_free(substream);
995 if (rtd->codec_dai->ops.hw_free)
996 rtd->codec_dai->ops.hw_free(substream);
998 mutex_unlock(&pcm_mutex);
1003 * Free's resources allocated by hw_params, can be called multiple times
1005 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1007 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1008 struct snd_soc_device *socdev = rtd->socdev;
1009 struct snd_soc_platform *platform = socdev->platform;
1010 struct snd_soc_codec *codec = socdev->codec;
1011 struct snd_soc_machine *machine = socdev->machine;
1013 mutex_lock(&pcm_mutex);
1015 /* apply codec digital mute */
1016 if (!codec->active && rtd->codec_dai->digital_mute)
1017 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1);
1019 /* free any machine hw params */
1020 if (machine->ops && machine->ops->hw_free)
1021 machine->ops->hw_free(substream);
1023 /* free any DMA resources */
1024 if (platform->pcm_ops->hw_free)
1025 platform->pcm_ops->hw_free(substream);
1027 /* now free hw params for the DAI's */
1028 if (rtd->codec_dai->ops.hw_free)
1029 rtd->codec_dai->ops.hw_free(substream);
1031 if (rtd->cpu_dai->ops.hw_free)
1032 rtd->cpu_dai->ops.hw_free(substream);
1034 mutex_unlock(&pcm_mutex);
1038 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1040 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1041 struct snd_soc_device *socdev = rtd->socdev;
1042 struct snd_soc_platform *platform = socdev->platform;
1045 if (rtd->codec_dai->ops.trigger) {
1046 ret = rtd->codec_dai->ops.trigger(substream, cmd);
1051 if (platform->pcm_ops->trigger) {
1052 ret = platform->pcm_ops->trigger(substream, cmd);
1057 if (rtd->cpu_dai->ops.trigger) {
1058 ret = rtd->cpu_dai->ops.trigger(substream, cmd);
1065 /* ASoC PCM operations */
1066 static struct snd_pcm_ops soc_pcm_ops = {
1067 .open = soc_pcm_open,
1068 .close = soc_codec_close,
1069 .hw_params = soc_pcm_hw_params,
1070 .hw_free = soc_pcm_hw_free,
1071 .prepare = soc_pcm_prepare,
1072 .trigger = soc_pcm_trigger,
1076 /* powers down audio subsystem for suspend */
1077 static int soc_suspend(struct platform_device *pdev, pm_message_t state)
1079 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1080 struct snd_soc_machine *machine = socdev->machine;
1081 struct snd_soc_platform *platform = socdev->platform;
1082 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1083 struct snd_soc_codec *codec = socdev->codec;
1086 /* mute any active DAC's */
1087 for(i = 0; i < machine->num_links; i++) {
1088 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1089 if (dai->digital_mute && dai->playback.active)
1090 dai->digital_mute(codec, dai, 1);
1093 if (machine->suspend_pre)
1094 machine->suspend_pre(pdev, state);
1096 for(i = 0; i < machine->num_links; i++) {
1097 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1098 if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
1099 cpu_dai->suspend(pdev, cpu_dai);
1100 if (platform->suspend)
1101 platform->suspend(pdev, cpu_dai);
1104 /* close any waiting streams and save state */
1105 flush_workqueue(soc_workq);
1106 codec->suspend_dapm_state = codec->dapm_state;
1108 for(i = 0; i < codec->num_dai; i++) {
1109 char *stream = codec->dai[i].playback.stream_name;
1111 snd_soc_dapm_stream_event(codec, stream,
1112 SND_SOC_DAPM_STREAM_SUSPEND);
1113 stream = codec->dai[i].capture.stream_name;
1115 snd_soc_dapm_stream_event(codec, stream,
1116 SND_SOC_DAPM_STREAM_SUSPEND);
1119 if (codec_dev->suspend)
1120 codec_dev->suspend(pdev, state);
1122 for(i = 0; i < machine->num_links; i++) {
1123 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1124 if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
1125 cpu_dai->suspend(pdev, cpu_dai);
1128 if (machine->suspend_post)
1129 machine->suspend_post(pdev, state);
1134 /* powers up audio subsystem after a suspend */
1135 static int soc_resume(struct platform_device *pdev)
1137 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1138 struct snd_soc_machine *machine = socdev->machine;
1139 struct snd_soc_platform *platform = socdev->platform;
1140 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1141 struct snd_soc_codec *codec = socdev->codec;
1144 if (machine->resume_pre)
1145 machine->resume_pre(pdev);
1147 for(i = 0; i < machine->num_links; i++) {
1148 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1149 if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
1150 cpu_dai->resume(pdev, cpu_dai);
1153 if (codec_dev->resume)
1154 codec_dev->resume(pdev);
1156 for(i = 0; i < codec->num_dai; i++) {
1157 char* stream = codec->dai[i].playback.stream_name;
1159 snd_soc_dapm_stream_event(codec, stream,
1160 SND_SOC_DAPM_STREAM_RESUME);
1161 stream = codec->dai[i].capture.stream_name;
1163 snd_soc_dapm_stream_event(codec, stream,
1164 SND_SOC_DAPM_STREAM_RESUME);
1167 /* unmute any active DAC's */
1168 for(i = 0; i < machine->num_links; i++) {
1169 struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
1170 if (dai->digital_mute && dai->playback.active)
1171 dai->digital_mute(codec, dai, 0);
1174 for(i = 0; i < machine->num_links; i++) {
1175 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1176 if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
1177 cpu_dai->resume(pdev, cpu_dai);
1178 if (platform->resume)
1179 platform->resume(pdev, cpu_dai);
1182 if (machine->resume_post)
1183 machine->resume_post(pdev);
1189 #define soc_suspend NULL
1190 #define soc_resume NULL
1193 /* probes a new socdev */
1194 static int soc_probe(struct platform_device *pdev)
1197 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1198 struct snd_soc_machine *machine = socdev->machine;
1199 struct snd_soc_platform *platform = socdev->platform;
1200 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1202 if (machine->probe) {
1203 ret = machine->probe(pdev);
1208 for (i = 0; i < machine->num_links; i++) {
1209 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1210 if (cpu_dai->probe) {
1211 ret = cpu_dai->probe(pdev);
1217 if (codec_dev->probe) {
1218 ret = codec_dev->probe(pdev);
1223 if (platform->probe) {
1224 ret = platform->probe(pdev);
1229 /* DAPM stream work */
1230 soc_workq = create_workqueue("kdapm");
1231 if (soc_workq == NULL)
1233 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
1237 if (platform->remove)
1238 platform->remove(pdev);
1241 if (codec_dev->remove)
1242 codec_dev->remove(pdev);
1245 for (i--; i > 0; i--) {
1246 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1247 if (cpu_dai->remove)
1248 cpu_dai->remove(pdev);
1251 if (machine->remove)
1252 machine->remove(pdev);
1257 /* removes a socdev */
1258 static int soc_remove(struct platform_device *pdev)
1261 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1262 struct snd_soc_machine *machine = socdev->machine;
1263 struct snd_soc_platform *platform = socdev->platform;
1264 struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
1267 destroy_workqueue(soc_workq);
1269 if (platform->remove)
1270 platform->remove(pdev);
1272 if (codec_dev->remove)
1273 codec_dev->remove(pdev);
1275 for (i = 0; i < machine->num_links; i++) {
1276 struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
1277 if (cpu_dai->remove)
1278 cpu_dai->remove(pdev);
1281 if (machine->remove)
1282 machine->remove(pdev);
1287 /* ASoC platform driver */
1288 static struct platform_driver soc_driver = {
1290 .name = "soc-audio",
1293 .remove = soc_remove,
1294 .suspend = soc_suspend,
1295 .resume = soc_resume,
1298 /* create a new pcm */
1299 static int soc_new_pcm(struct snd_soc_device *socdev,
1300 struct snd_soc_dai_link *dai_link, int num)
1302 struct snd_soc_codec *codec = socdev->codec;
1303 struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
1304 struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
1305 struct snd_soc_pcm_runtime *rtd;
1306 struct snd_pcm *pcm;
1308 int ret = 0, playback = 0, capture = 0;
1310 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1313 rtd->cpu_dai = cpu_dai;
1314 rtd->codec_dai = codec_dai;
1315 rtd->socdev = socdev;
1317 /* check client and interface hw capabilities */
1318 sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
1319 get_dai_name(cpu_dai->type), num);
1321 if (codec_dai->playback.channels_min)
1323 if (codec_dai->capture.channels_min)
1326 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1329 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1334 pcm->private_data = rtd;
1335 soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
1336 soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
1337 soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
1338 soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
1339 soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
1340 soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
1341 soc_pcm_ops.page = socdev->platform->pcm_ops->page;
1344 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1347 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1349 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
1351 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1356 pcm->private_free = socdev->platform->pcm_free;
1357 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1362 /* codec register dump */
1363 static ssize_t codec_reg_show(struct device *dev,
1364 struct device_attribute *attr, char *buf)
1366 struct snd_soc_device *devdata = dev_get_drvdata(dev);
1367 struct snd_soc_codec *codec = devdata->codec;
1368 int i, step = 1, count = 0;
1370 if (!codec->reg_cache_size)
1373 if (codec->reg_cache_step)
1374 step = codec->reg_cache_step;
1376 count += sprintf(buf, "%s registers\n", codec->name);
1377 for(i = 0; i < codec->reg_cache_size; i += step)
1378 count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
1382 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1385 * snd_soc_new_ac97_codec - initailise AC97 device
1386 * @codec: audio codec
1387 * @ops: AC97 bus operations
1388 * @num: AC97 codec number
1390 * Initialises AC97 codec resources for use by ad-hoc devices only.
1392 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1393 struct snd_ac97_bus_ops *ops, int num)
1395 mutex_lock(&codec->mutex);
1397 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1398 if (codec->ac97 == NULL) {
1399 mutex_unlock(&codec->mutex);
1403 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1404 if (codec->ac97->bus == NULL) {
1407 mutex_unlock(&codec->mutex);
1411 codec->ac97->bus->ops = ops;
1412 codec->ac97->num = num;
1413 mutex_unlock(&codec->mutex);
1416 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1419 * snd_soc_free_ac97_codec - free AC97 codec device
1420 * @codec: audio codec
1422 * Frees AC97 codec device resources.
1424 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1426 mutex_lock(&codec->mutex);
1427 kfree(codec->ac97->bus);
1430 mutex_unlock(&codec->mutex);
1432 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1435 * snd_soc_update_bits - update codec register bits
1436 * @codec: audio codec
1437 * @reg: codec register
1438 * @mask: register mask
1441 * Writes new register value.
1443 * Returns 1 for change else 0.
1445 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1446 unsigned short mask, unsigned short value)
1449 unsigned short old, new;
1451 mutex_lock(&io_mutex);
1452 old = snd_soc_read(codec, reg);
1453 new = (old & ~mask) | value;
1454 change = old != new;
1456 snd_soc_write(codec, reg, new);
1458 mutex_unlock(&io_mutex);
1461 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1464 * snd_soc_test_bits - test register for change
1465 * @codec: audio codec
1466 * @reg: codec register
1467 * @mask: register mask
1470 * Tests a register with a new value and checks if the new value is
1471 * different from the old value.
1473 * Returns 1 for change else 0.
1475 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1476 unsigned short mask, unsigned short value)
1479 unsigned short old, new;
1481 mutex_lock(&io_mutex);
1482 old = snd_soc_read(codec, reg);
1483 new = (old & ~mask) | value;
1484 change = old != new;
1485 mutex_unlock(&io_mutex);
1489 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1492 * snd_soc_get_rate - get int sample rate
1493 * @hwpcmrate: the hardware pcm rate
1495 * Returns the audio rate integaer value, else 0.
1497 int snd_soc_get_rate(int hwpcmrate)
1499 int rate = ffs(hwpcmrate) - 1;
1501 if (rate > ARRAY_SIZE(rates))
1505 EXPORT_SYMBOL_GPL(snd_soc_get_rate);
1508 * snd_soc_new_pcms - create new sound card and pcms
1509 * @socdev: the SoC audio device
1511 * Create a new sound card based upon the codec and interface pcms.
1513 * Returns 0 for success, else error.
1515 int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
1517 struct snd_soc_codec *codec = socdev->codec;
1518 struct snd_soc_machine *machine = socdev->machine;
1521 mutex_lock(&codec->mutex);
1523 /* register a sound card */
1524 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1526 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1528 mutex_unlock(&codec->mutex);
1532 codec->card->dev = socdev->dev;
1533 codec->card->private_data = codec;
1534 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1536 /* create the pcms */
1537 for(i = 0; i < machine->num_links; i++) {
1538 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1540 printk(KERN_ERR "asoc: can't create pcm %s\n",
1541 machine->dai_link[i].stream_name);
1542 mutex_unlock(&codec->mutex);
1547 mutex_unlock(&codec->mutex);
1550 EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1553 * snd_soc_register_card - register sound card
1554 * @socdev: the SoC audio device
1556 * Register a SoC sound card. Also registers an AC97 device if the
1557 * codec is AC97 for ad hoc devices.
1559 * Returns 0 for success, else error.
1561 int snd_soc_register_card(struct snd_soc_device *socdev)
1563 struct snd_soc_codec *codec = socdev->codec;
1564 struct snd_soc_machine *machine = socdev->machine;
1565 int ret = 0, i, ac97 = 0, err = 0;
1567 mutex_lock(&codec->mutex);
1568 for(i = 0; i < machine->num_links; i++) {
1569 if (socdev->machine->dai_link[i].init) {
1570 err = socdev->machine->dai_link[i].init(codec);
1572 printk(KERN_ERR "asoc: failed to init %s\n",
1573 socdev->machine->dai_link[i].stream_name);
1577 if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1580 snprintf(codec->card->shortname, sizeof(codec->card->shortname),
1581 "%s", machine->name);
1582 snprintf(codec->card->longname, sizeof(codec->card->longname),
1583 "%s (%s)", machine->name, codec->name);
1585 ret = snd_card_register(codec->card);
1587 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1592 #ifdef CONFIG_SND_SOC_AC97_BUS
1594 ret = soc_ac97_dev_register(codec);
1596 printk(KERN_ERR "asoc: AC97 device register failed\n");
1597 snd_card_free(codec->card);
1603 err = snd_soc_dapm_sys_add(socdev->dev);
1605 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1607 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1609 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1611 mutex_unlock(&codec->mutex);
1614 EXPORT_SYMBOL_GPL(snd_soc_register_card);
1617 * snd_soc_free_pcms - free sound card and pcms
1618 * @socdev: the SoC audio device
1620 * Frees sound card and pcms associated with the socdev.
1621 * Also unregister the codec if it is an AC97 device.
1623 void snd_soc_free_pcms(struct snd_soc_device *socdev)
1625 struct snd_soc_codec *codec = socdev->codec;
1627 mutex_lock(&codec->mutex);
1628 #ifdef CONFIG_SND_SOC_AC97_BUS
1630 soc_ac97_dev_unregister(codec);
1634 snd_card_free(codec->card);
1635 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1636 mutex_unlock(&codec->mutex);
1638 EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1641 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1642 * @substream: the pcm substream
1643 * @hw: the hardware parameters
1645 * Sets the substream runtime hardware parameters.
1647 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1648 const struct snd_pcm_hardware *hw)
1650 struct snd_pcm_runtime *runtime = substream->runtime;
1651 runtime->hw.info = hw->info;
1652 runtime->hw.formats = hw->formats;
1653 runtime->hw.period_bytes_min = hw->period_bytes_min;
1654 runtime->hw.period_bytes_max = hw->period_bytes_max;
1655 runtime->hw.periods_min = hw->periods_min;
1656 runtime->hw.periods_max = hw->periods_max;
1657 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1658 runtime->hw.fifo_size = hw->fifo_size;
1661 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1664 * snd_soc_cnew - create new control
1665 * @_template: control template
1666 * @data: control private data
1667 * @lnng_name: control long name
1669 * Create a new mixer control from a template control.
1671 * Returns 0 for success, else error.
1673 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1674 void *data, char *long_name)
1676 struct snd_kcontrol_new template;
1678 memcpy(&template, _template, sizeof(template));
1680 template.name = long_name;
1681 template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1684 return snd_ctl_new1(&template, data);
1686 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1689 * snd_soc_info_enum_double - enumerated double mixer info callback
1690 * @kcontrol: mixer control
1691 * @uinfo: control element information
1693 * Callback to provide information about a double enumerated
1696 * Returns 0 for success.
1698 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_info *uinfo)
1701 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1703 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1704 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1705 uinfo->value.enumerated.items = e->mask;
1707 if (uinfo->value.enumerated.item > e->mask - 1)
1708 uinfo->value.enumerated.item = e->mask - 1;
1709 strcpy(uinfo->value.enumerated.name,
1710 e->texts[uinfo->value.enumerated.item]);
1713 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1716 * snd_soc_get_enum_double - enumerated double mixer get callback
1717 * @kcontrol: mixer control
1718 * @uinfo: control element information
1720 * Callback to get the value of a double enumerated mixer.
1722 * Returns 0 for success.
1724 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
1727 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1728 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1729 unsigned short val, bitmask;
1731 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1733 val = snd_soc_read(codec, e->reg);
1734 ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
1735 if (e->shift_l != e->shift_r)
1736 ucontrol->value.enumerated.item[1] =
1737 (val >> e->shift_r) & (bitmask - 1);
1741 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1744 * snd_soc_put_enum_double - enumerated double mixer put callback
1745 * @kcontrol: mixer control
1746 * @uinfo: control element information
1748 * Callback to set the value of a double enumerated mixer.
1750 * Returns 0 for success.
1752 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1753 struct snd_ctl_elem_value *ucontrol)
1755 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1756 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1758 unsigned short mask, bitmask;
1760 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1762 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1764 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1765 mask = (bitmask - 1) << e->shift_l;
1766 if (e->shift_l != e->shift_r) {
1767 if (ucontrol->value.enumerated.item[1] > e->mask - 1)
1769 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1770 mask |= (bitmask - 1) << e->shift_r;
1773 return snd_soc_update_bits(codec, e->reg, mask, val);
1775 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1778 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1779 * @kcontrol: mixer control
1780 * @uinfo: control element information
1782 * Callback to provide information about an external enumerated
1785 * Returns 0 for success.
1787 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_info *uinfo)
1790 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1792 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1794 uinfo->value.enumerated.items = e->mask;
1796 if (uinfo->value.enumerated.item > e->mask - 1)
1797 uinfo->value.enumerated.item = e->mask - 1;
1798 strcpy(uinfo->value.enumerated.name,
1799 e->texts[uinfo->value.enumerated.item]);
1802 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1805 * snd_soc_info_volsw_ext - external single mixer info callback
1806 * @kcontrol: mixer control
1807 * @uinfo: control element information
1809 * Callback to provide information about a single external mixer control.
1811 * Returns 0 for success.
1813 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_info *uinfo)
1816 int mask = kcontrol->private_value;
1819 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1821 uinfo->value.integer.min = 0;
1822 uinfo->value.integer.max = mask;
1825 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1828 * snd_soc_info_bool_ext - external single boolean mixer info callback
1829 * @kcontrol: mixer control
1830 * @uinfo: control element information
1832 * Callback to provide information about a single boolean external mixer control.
1834 * Returns 0 for success.
1836 int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
1837 struct snd_ctl_elem_info *uinfo)
1839 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1841 uinfo->value.integer.min = 0;
1842 uinfo->value.integer.max = 1;
1845 EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
1848 * snd_soc_info_volsw - single mixer info callback
1849 * @kcontrol: mixer control
1850 * @uinfo: control element information
1852 * Callback to provide information about a single mixer control.
1854 * Returns 0 for success.
1856 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1857 struct snd_ctl_elem_info *uinfo)
1859 int mask = (kcontrol->private_value >> 16) & 0xff;
1860 int shift = (kcontrol->private_value >> 8) & 0x0f;
1861 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1864 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1865 uinfo->count = shift == rshift ? 1 : 2;
1866 uinfo->value.integer.min = 0;
1867 uinfo->value.integer.max = mask;
1870 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1873 * snd_soc_get_volsw - single mixer get callback
1874 * @kcontrol: mixer control
1875 * @uinfo: control element information
1877 * Callback to get the value of a single mixer control.
1879 * Returns 0 for success.
1881 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1882 struct snd_ctl_elem_value *ucontrol)
1884 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1885 int reg = kcontrol->private_value & 0xff;
1886 int shift = (kcontrol->private_value >> 8) & 0x0f;
1887 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1888 int mask = (kcontrol->private_value >> 16) & 0xff;
1889 int invert = (kcontrol->private_value >> 24) & 0x01;
1891 ucontrol->value.integer.value[0] =
1892 (snd_soc_read(codec, reg) >> shift) & mask;
1893 if (shift != rshift)
1894 ucontrol->value.integer.value[1] =
1895 (snd_soc_read(codec, reg) >> rshift) & mask;
1897 ucontrol->value.integer.value[0] =
1898 mask - ucontrol->value.integer.value[0];
1899 if (shift != rshift)
1900 ucontrol->value.integer.value[1] =
1901 mask - ucontrol->value.integer.value[1];
1906 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1909 * snd_soc_put_volsw - single mixer put callback
1910 * @kcontrol: mixer control
1911 * @uinfo: control element information
1913 * Callback to set the value of a single mixer control.
1915 * Returns 0 for success.
1917 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1920 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1921 int reg = kcontrol->private_value & 0xff;
1922 int shift = (kcontrol->private_value >> 8) & 0x0f;
1923 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1924 int mask = (kcontrol->private_value >> 16) & 0xff;
1925 int invert = (kcontrol->private_value >> 24) & 0x01;
1927 unsigned short val, val2, val_mask;
1929 val = (ucontrol->value.integer.value[0] & mask);
1932 val_mask = mask << shift;
1934 if (shift != rshift) {
1935 val2 = (ucontrol->value.integer.value[1] & mask);
1938 val_mask |= mask << rshift;
1939 val |= val2 << rshift;
1941 err = snd_soc_update_bits(codec, reg, val_mask, val);
1944 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1947 * snd_soc_info_volsw_2r - double mixer info callback
1948 * @kcontrol: mixer control
1949 * @uinfo: control element information
1951 * Callback to provide information about a double mixer control that
1952 * spans 2 codec registers.
1954 * Returns 0 for success.
1956 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
1959 int mask = (kcontrol->private_value >> 12) & 0xff;
1962 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1964 uinfo->value.integer.min = 0;
1965 uinfo->value.integer.max = mask;
1968 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1971 * snd_soc_get_volsw_2r - double mixer get callback
1972 * @kcontrol: mixer control
1973 * @uinfo: control element information
1975 * Callback to get the value of a double mixer control that spans 2 registers.
1977 * Returns 0 for success.
1979 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1982 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1983 int reg = kcontrol->private_value & 0xff;
1984 int reg2 = (kcontrol->private_value >> 24) & 0xff;
1985 int shift = (kcontrol->private_value >> 8) & 0x0f;
1986 int mask = (kcontrol->private_value >> 12) & 0xff;
1987 int invert = (kcontrol->private_value >> 20) & 0x01;
1989 ucontrol->value.integer.value[0] =
1990 (snd_soc_read(codec, reg) >> shift) & mask;
1991 ucontrol->value.integer.value[1] =
1992 (snd_soc_read(codec, reg2) >> shift) & mask;
1994 ucontrol->value.integer.value[0] =
1995 mask - ucontrol->value.integer.value[0];
1996 ucontrol->value.integer.value[1] =
1997 mask - ucontrol->value.integer.value[1];
2002 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2005 * snd_soc_put_volsw_2r - double mixer set callback
2006 * @kcontrol: mixer control
2007 * @uinfo: control element information
2009 * Callback to set the value of a double mixer control that spans 2 registers.
2011 * Returns 0 for success.
2013 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_value *ucontrol)
2016 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2017 int reg = kcontrol->private_value & 0xff;
2018 int reg2 = (kcontrol->private_value >> 24) & 0xff;
2019 int shift = (kcontrol->private_value >> 8) & 0x0f;
2020 int mask = (kcontrol->private_value >> 12) & 0xff;
2021 int invert = (kcontrol->private_value >> 20) & 0x01;
2023 unsigned short val, val2, val_mask;
2025 val_mask = mask << shift;
2026 val = (ucontrol->value.integer.value[0] & mask);
2027 val2 = (ucontrol->value.integer.value[1] & mask);
2035 val2 = val2 << shift;
2037 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
2040 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2043 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2045 static int __devinit snd_soc_init(void)
2047 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
2048 return platform_driver_register(&soc_driver);
2051 static void snd_soc_exit(void)
2053 platform_driver_unregister(&soc_driver);
2056 module_init(snd_soc_init);
2057 module_exit(snd_soc_exit);
2059 /* Module information */
2060 MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
2061 MODULE_DESCRIPTION("ALSA SoC Core");
2062 MODULE_LICENSE("GPL");