[ALSA] ASoC - Bit clock matching error
[linux-2.6/cjktty.git] / sound / soc / soc-core.c
blob90e8841e7e33856c149ec8cf0d9d52aad53762e8
1 /*
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.
13 * Revision history
14 * 12th Aug 2005 Initial version.
15 * 25th Oct 2005 Working Codec, Interface and Platform registration.
17 * TODO:
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>
28 #include <linux/pm.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>
39 /* debug */
40 #define SOC_DEBUG 0
41 #if SOC_DEBUG
42 #define dbg(format, arg...) printk(format, ## arg)
43 #else
44 #define dbg(format, arg...)
45 #endif
46 /* debug DAI capabilities matching */
47 #define SOC_DEBUG_DAI 0
48 #if SOC_DEBUG_DAI
49 #define dbgc(format, arg...) printk(format, ## arg)
50 #else
51 #define dbgc(format, arg...)
52 #endif
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 struct work_struct soc_stream_work;
60 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
62 /* supported sample rates */
63 /* ATTENTION: these values depend on the definition in pcm.h! */
64 static const unsigned int rates[] = {
65 5512, 8000, 11025, 16000, 22050, 32000, 44100,
66 48000, 64000, 88200, 96000, 176400, 192000
70 * This is a timeout to do a DAPM powerdown after a stream is closed().
71 * It can be used to eliminate pops between different playback streams, e.g.
72 * between two audio tracks.
74 static int pmdown_time = 5000;
75 module_param(pmdown_time, int, 0);
76 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
78 #ifdef CONFIG_SND_SOC_AC97_BUS
79 /* unregister ac97 codec */
80 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
82 if (codec->ac97->dev.bus)
83 device_unregister(&codec->ac97->dev);
84 return 0;
87 /* stop no dev release warning */
88 static void soc_ac97_device_release(struct device *dev){}
90 /* register ac97 codec to bus */
91 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
93 int err;
95 codec->ac97->dev.bus = &ac97_bus_type;
96 codec->ac97->dev.parent = NULL;
97 codec->ac97->dev.release = soc_ac97_device_release;
99 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
100 codec->card->number, 0, codec->name);
101 err = device_register(&codec->ac97->dev);
102 if (err < 0) {
103 snd_printk(KERN_ERR "Can't register ac97 bus\n");
104 codec->ac97->dev.bus = NULL;
105 return err;
107 return 0;
109 #endif
111 static inline const char* get_dai_name(int type)
113 switch(type) {
114 case SND_SOC_DAI_AC97:
115 return "AC97";
116 case SND_SOC_DAI_I2S:
117 return "I2S";
118 case SND_SOC_DAI_PCM:
119 return "PCM";
121 return NULL;
124 /* get rate format from rate */
125 static inline int soc_get_rate_format(int rate)
127 int i;
129 for (i = 0; i < ARRAY_SIZE(rates); i++) {
130 if (rates[i] == rate)
131 return 1 << i;
133 return 0;
136 /* gets the audio system mclk/sysclk for the given parameters */
137 static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
138 struct snd_soc_clock_info *info)
140 struct snd_soc_device *socdev = rtd->socdev;
141 struct snd_soc_machine *machine = socdev->machine;
142 int i;
144 /* find the matching machine config and get it's mclk for the given
145 * sample rate and hardware format */
146 for(i = 0; i < machine->num_links; i++) {
147 if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
148 machine->dai_link[i].config_sysclk)
149 return machine->dai_link[i].config_sysclk(rtd, info);
151 return 0;
154 /* changes a bitclk multiplier mask to a divider mask */
155 static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
156 unsigned int pcmfmt, unsigned int chn)
158 int i, j;
159 u64 bfs_ = 0;
160 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
162 if (size <= 0)
163 return 0;
165 /* the minimum bit clock that has enough bandwidth */
166 min = size * rate * chn;
167 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
169 for (i = 0; i < 64; i++) {
170 if ((bfs >> i) & 0x1) {
171 j = min * (i + 1);
172 bfs_ |= SND_SOC_FSBD(mclk/j);
173 dbgc("rcw --> div support mult %d\n",
174 SND_SOC_FSBD_REAL(1<<i));
178 return bfs_;
181 /* changes a bitclk divider mask to a multiplier mask */
182 static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
183 unsigned int pcmfmt, unsigned int chn)
185 int i, j;
186 u64 bfs_ = 0;
188 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
190 if (size <= 0)
191 return 0;
193 /* the minimum bit clock that has enough bandwidth */
194 min = size * rate * chn;
195 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
197 for (i = 0; i < 64; i++) {
198 if ((bfs >> i) & 0x1) {
199 j = mclk / (i + 1);
200 if (j >= min) {
201 bfs_ |= SND_SOC_FSBW(j/min);
202 dbgc("div --> rcw support div %d\n",
203 SND_SOC_FSBW_REAL(1<<i));
208 return bfs_;
211 /* changes a constant bitclk to a multiplier mask */
212 static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
213 unsigned int pcmfmt, unsigned int chn)
215 unsigned int bfs_ = rate * bfs;
216 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
218 if (size <= 0)
219 return 0;
221 /* the minimum bit clock that has enough bandwidth */
222 min = size * rate * chn;
223 dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
225 if (bfs_ < min)
226 return 0;
227 else {
228 bfs_ = SND_SOC_FSBW(bfs_/min);
229 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
230 return bfs_;
234 /* changes a bitclk multiplier mask to a divider mask */
235 static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
236 unsigned int pcmfmt, unsigned int chn)
238 unsigned int bfs_ = rate * bfs;
239 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
241 if (size <= 0)
242 return 0;
244 /* the minimum bit clock that has enough bandwidth */
245 min = size * rate * chn;
246 dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
248 if (bfs_ < min)
249 return 0;
250 else {
251 bfs_ = SND_SOC_FSBW(mclk/bfs_);
252 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
253 return bfs_;
257 /* Matches codec DAI and SoC CPU DAI hardware parameters */
258 static int soc_hw_match_params(struct snd_pcm_substream *substream,
259 struct snd_pcm_hw_params *params)
261 struct snd_soc_pcm_runtime *rtd = substream->private_data;
262 struct snd_soc_dai_mode *codec_dai_mode = NULL;
263 struct snd_soc_dai_mode *cpu_dai_mode = NULL;
264 struct snd_soc_clock_info clk_info;
265 unsigned int fs, mclk, rate = params_rate(params),
266 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
267 u16 fmt = 0;
268 u64 codec_bfs, cpu_bfs;
270 dbg("asoc: match version %s\n", SND_SOC_VERSION);
271 clk_info.rate = rate;
272 pcmrate = soc_get_rate_format(rate);
274 /* try and find a match from the codec and cpu DAI capabilities */
275 for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
276 for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
277 codec_dai_mode = &rtd->codec_dai->caps.mode[j];
278 cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
280 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
281 pcmrate)) {
282 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
283 continue;
286 fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
287 if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
288 dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
289 continue;
292 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
293 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
294 j, k);
295 continue;
298 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
299 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
300 continue;
303 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
304 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
305 continue;
308 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
309 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
310 continue;
313 /* todo - still need to add tdm selection */
314 rtd->cpu_dai->dai_runtime.fmt =
315 rtd->codec_dai->dai_runtime.fmt =
316 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
317 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
318 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
319 clk_info.bclk_master =
320 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
322 /* make sure the ratio between rate and master
323 * clock is acceptable*/
324 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
325 if (fs == 0) {
326 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
327 continue;
329 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
330 rtd->codec_dai->dai_runtime.fs = fs;
332 /* calculate audio system clocking using slowest clocks possible*/
333 mclk = soc_get_mclk(rtd, &clk_info);
334 if (mclk == 0) {
335 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
336 dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
337 clk_info.bclk_master);
338 continue;
341 /* calculate word size (per channel) and frame size */
342 rtd->codec_dai->dai_runtime.pcmfmt =
343 rtd->cpu_dai->dai_runtime.pcmfmt =
344 1 << params_format(params);
346 chn = params_channels(params);
347 /* i2s always has left and right */
348 if (params_channels(params) == 1 &&
349 rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
350 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
351 chn <<= 1;
353 /* Calculate bfs - the ratio between bitclock and the sample rate
354 * We must take into consideration the dividers and multipliers
355 * used in the codec and cpu DAI modes. We always choose the
356 * lowest possible clocks to reduce power.
358 switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
359 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
360 /* cpu & codec bfs dividers */
361 rtd->cpu_dai->dai_runtime.bfs =
362 rtd->codec_dai->dai_runtime.bfs =
363 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
364 break;
365 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
366 /* normalise bfs codec divider & cpu rcw mult */
367 codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
368 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
369 rtd->cpu_dai->dai_runtime.bfs =
370 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
371 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
372 rtd->codec_dai->dai_runtime.pcmfmt, chn);
373 rtd->codec_dai->dai_runtime.bfs =
374 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
375 break;
376 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
377 /* normalise bfs codec rcw mult & cpu divider */
378 codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
379 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
380 rtd->cpu_dai->dai_runtime.bfs =
381 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
382 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
383 rtd->codec_dai->dai_runtime.pcmfmt, chn);
384 rtd->codec_dai->dai_runtime.bfs =
385 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
386 break;
387 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
388 /* codec & cpu bfs rate rcw multipliers */
389 rtd->cpu_dai->dai_runtime.bfs =
390 rtd->codec_dai->dai_runtime.bfs =
391 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
392 break;
393 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
394 /* normalise cpu bfs rate const multiplier & codec div */
395 cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
396 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
397 if(codec_dai_mode->bfs & cpu_bfs) {
398 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
399 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
400 } else
401 rtd->cpu_dai->dai_runtime.bfs = 0;
402 break;
403 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
404 /* normalise cpu bfs rate const multiplier & codec rcw mult */
405 cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
406 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
407 if(codec_dai_mode->bfs & cpu_bfs) {
408 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
409 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
410 } else
411 rtd->cpu_dai->dai_runtime.bfs = 0;
412 break;
413 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
414 /* normalise cpu bfs rate rcw multiplier & codec const mult */
415 codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
416 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
417 if(cpu_dai_mode->bfs & codec_bfs) {
418 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
419 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
420 } else
421 rtd->cpu_dai->dai_runtime.bfs = 0;
422 break;
423 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
424 /* normalise cpu bfs div & codec const mult */
425 codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
426 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
427 if(cpu_dai_mode->bfs & codec_bfs) {
428 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
429 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
430 } else
431 rtd->cpu_dai->dai_runtime.bfs = 0;
432 break;
433 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
434 /* cpu & codec constant mult */
435 if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
436 rtd->cpu_dai->dai_runtime.bfs =
437 rtd->codec_dai->dai_runtime.bfs =
438 codec_dai_mode->bfs;
439 else
440 rtd->cpu_dai->dai_runtime.bfs =
441 rtd->codec_dai->dai_runtime.bfs = 0;
442 break;
445 /* make sure the bit clock speed is acceptable */
446 if (!rtd->cpu_dai->dai_runtime.bfs ||
447 !rtd->codec_dai->dai_runtime.bfs) {
448 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
449 dbgc("asoc: cpu_dai %llu codec %llu\n",
450 rtd->cpu_dai->dai_runtime.bfs,
451 rtd->codec_dai->dai_runtime.bfs);
452 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
453 continue;
456 goto found;
459 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
460 return -EINVAL;
462 found:
463 /* we have matching DAI's, so complete the runtime info */
464 rtd->codec_dai->dai_runtime.pcmrate =
465 rtd->cpu_dai->dai_runtime.pcmrate =
466 soc_get_rate_format(rate);
468 rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
469 rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
470 rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
471 rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
473 /* for debug atm */
474 dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
475 if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
476 codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
477 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
478 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
479 rtd->codec_dai->dai_runtime.fs, mclk,
480 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
481 } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
482 codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
483 dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
484 rtd->codec_dai->dai_runtime.fs, mclk,
485 rtd->codec_dai->dai_runtime.bfs, codec_bclk);
486 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
487 codec_bclk = params_rate(params) * params_channels(params) *
488 snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
489 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
490 dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
491 rtd->codec_dai->dai_runtime.fs, mclk,
492 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
493 } else
494 codec_bclk = 0;
496 if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
497 cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
498 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
499 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
500 rtd->cpu_dai->dai_runtime.fs, mclk,
501 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
502 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
503 cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
504 dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
505 rtd->cpu_dai->dai_runtime.fs, mclk,
506 rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
507 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
508 cpu_bclk = params_rate(params) * params_channels(params) *
509 snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
510 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
511 dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
512 rtd->cpu_dai->dai_runtime.fs, mclk,
513 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
514 } else
515 cpu_bclk = 0;
518 * Check we have matching bitclocks. If we don't then it means the
519 * sysclock returned by either the codec or cpu DAI (selected by the
520 * machine sysclock function) is wrong compared with the supported DAI
521 * modes for the codec or cpu DAI.
523 if (cpu_bclk != codec_bclk && cpu_bclk){
524 printk(KERN_ERR
525 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
527 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
530 switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
531 case SND_SOC_DAIFMT_CBM_CFM:
532 dbg("asoc: DAI codec BCLK master, LRC master\n");
533 break;
534 case SND_SOC_DAIFMT_CBS_CFM:
535 dbg("asoc: DAI codec BCLK slave, LRC master\n");
536 break;
537 case SND_SOC_DAIFMT_CBM_CFS:
538 dbg("asoc: DAI codec BCLK master, LRC slave\n");
539 break;
540 case SND_SOC_DAIFMT_CBS_CFS:
541 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
542 break;
544 dbg("asoc: mode %x, invert %x\n",
545 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
546 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
547 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
548 params_channels(params), params_format(params));
550 return 0;
553 static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
555 int i;
556 u32 rates = 0;
558 for(i = 0; i < nmodes; i++)
559 rates |= modes[i].pcmrate;
561 return rates;
564 static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
566 int i;
567 u64 formats = 0;
569 for(i = 0; i < nmodes; i++)
570 formats |= modes[i].pcmfmt;
572 return formats;
576 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
577 * then initialized and any private data can be allocated. This also calls
578 * startup for the cpu DAI, platform, machine and codec DAI.
580 static int soc_pcm_open(struct snd_pcm_substream *substream)
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct snd_soc_device *socdev = rtd->socdev;
584 struct snd_pcm_runtime *runtime = substream->runtime;
585 struct snd_soc_machine *machine = socdev->machine;
586 struct snd_soc_platform *platform = socdev->platform;
587 struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
588 struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
589 int ret = 0;
591 mutex_lock(&pcm_mutex);
593 /* startup the audio subsystem */
594 if (rtd->cpu_dai->ops.startup) {
595 ret = rtd->cpu_dai->ops.startup(substream);
596 if (ret < 0) {
597 printk(KERN_ERR "asoc: can't open interface %s\n",
598 rtd->cpu_dai->name);
599 goto out;
603 if (platform->pcm_ops->open) {
604 ret = platform->pcm_ops->open(substream);
605 if (ret < 0) {
606 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
607 goto platform_err;
611 if (machine->ops && machine->ops->startup) {
612 ret = machine->ops->startup(substream);
613 if (ret < 0) {
614 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
615 goto machine_err;
619 if (rtd->codec_dai->ops.startup) {
620 ret = rtd->codec_dai->ops.startup(substream);
621 if (ret < 0) {
622 printk(KERN_ERR "asoc: can't open codec %s\n",
623 rtd->codec_dai->name);
624 goto codec_dai_err;
628 /* create runtime params from DMA, codec and cpu DAI */
629 if (runtime->hw.rates)
630 runtime->hw.rates &=
631 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
632 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
633 else
634 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);
637 if (runtime->hw.formats)
638 runtime->hw.formats &=
639 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
640 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
641 else
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 /* Check that the codec and cpu DAI's are compatible */
647 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
648 runtime->hw.rate_min =
649 max(rtd->codec_dai->playback.rate_min,
650 rtd->cpu_dai->playback.rate_min);
651 runtime->hw.rate_max =
652 min(rtd->codec_dai->playback.rate_max,
653 rtd->cpu_dai->playback.rate_max);
654 runtime->hw.channels_min =
655 max(rtd->codec_dai->playback.channels_min,
656 rtd->cpu_dai->playback.channels_min);
657 runtime->hw.channels_max =
658 min(rtd->codec_dai->playback.channels_max,
659 rtd->cpu_dai->playback.channels_max);
660 } else {
661 runtime->hw.rate_min =
662 max(rtd->codec_dai->capture.rate_min,
663 rtd->cpu_dai->capture.rate_min);
664 runtime->hw.rate_max =
665 min(rtd->codec_dai->capture.rate_max,
666 rtd->cpu_dai->capture.rate_max);
667 runtime->hw.channels_min =
668 max(rtd->codec_dai->capture.channels_min,
669 rtd->cpu_dai->capture.channels_min);
670 runtime->hw.channels_max =
671 min(rtd->codec_dai->capture.channels_max,
672 rtd->cpu_dai->capture.channels_max);
675 snd_pcm_limit_hw_rates(runtime);
676 if (!runtime->hw.rates) {
677 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
678 rtd->codec_dai->name, rtd->cpu_dai->name);
679 goto codec_dai_err;
681 if (!runtime->hw.formats) {
682 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
683 rtd->codec_dai->name, rtd->cpu_dai->name);
684 goto codec_dai_err;
686 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
687 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
688 rtd->codec_dai->name, rtd->cpu_dai->name);
689 goto codec_dai_err;
692 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
693 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
694 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
695 runtime->hw.channels_max);
696 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
697 runtime->hw.rate_max);
700 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
701 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
702 else
703 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
704 rtd->cpu_dai->active = rtd->codec_dai->active = 1;
705 rtd->cpu_dai->runtime = runtime;
706 socdev->codec->active++;
707 mutex_unlock(&pcm_mutex);
708 return 0;
710 codec_dai_err:
711 if (machine->ops && machine->ops->shutdown)
712 machine->ops->shutdown(substream);
714 machine_err:
715 if (platform->pcm_ops->close)
716 platform->pcm_ops->close(substream);
718 platform_err:
719 if (rtd->cpu_dai->ops.shutdown)
720 rtd->cpu_dai->ops.shutdown(substream);
721 out:
722 mutex_unlock(&pcm_mutex);
723 return ret;
727 * Power down the audio subsytem pmdown_time msecs after close is called.
728 * This is to ensure there are no pops or clicks in between any music tracks
729 * due to DAPM power cycling.
731 static void close_delayed_work(void *data)
733 struct snd_soc_device *socdev = data;
734 struct snd_soc_codec *codec = socdev->codec;
735 struct snd_soc_codec_dai *codec_dai;
736 int i;
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
769 * shutdown.
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;
783 else
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;
790 codec->active--;
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, &soc_stream_work,
809 msecs_to_jiffies(pmdown_time));
810 } else {
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);
822 return 0;
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;
836 int ret = 0;
838 mutex_lock(&pcm_mutex);
839 if (platform->pcm_ops->prepare) {
840 ret = platform->pcm_ops->prepare(substream);
841 if (ret < 0) {
842 printk(KERN_ERR "asoc: platform prepare error\n");
843 goto out;
847 if (rtd->codec_dai->ops.prepare) {
848 ret = rtd->codec_dai->ops.prepare(substream);
849 if (ret < 0) {
850 printk(KERN_ERR "asoc: codec DAI prepare error\n");
851 goto out;
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);
866 else {
867 rtd->codec_dai->pop_wait = 0;
868 cancel_delayed_work(&soc_stream_work);
869 if (rtd->codec_dai->digital_mute)
870 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
872 } else {
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);
883 else
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);
893 } else {
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);
899 else
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);
908 out:
909 mutex_unlock(&pcm_mutex);
910 return ret;
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;
925 int ret = 0;
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);
932 if (ret < 0)
933 goto out;
934 } else {
935 struct snd_soc_clock_info clk_info;
936 clk_info.rate = params_rate(params);
937 ret = soc_get_mclk(rtd, &clk_info);
938 if (ret < 0)
939 goto out;
942 if (rtd->codec_dai->ops.hw_params) {
943 ret = rtd->codec_dai->ops.hw_params(substream, params);
944 if (ret < 0) {
945 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
946 rtd->codec_dai->name);
947 goto out;
951 if (rtd->cpu_dai->ops.hw_params) {
952 ret = rtd->cpu_dai->ops.hw_params(substream, params);
953 if (ret < 0) {
954 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
955 rtd->cpu_dai->name);
956 goto interface_err;
960 if (platform->pcm_ops->hw_params) {
961 ret = platform->pcm_ops->hw_params(substream, params);
962 if (ret < 0) {
963 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
964 platform->name);
965 goto platform_err;
969 if (machine->ops && machine->ops->hw_params) {
970 ret = machine->ops->hw_params(substream, params);
971 if (ret < 0) {
972 printk(KERN_ERR "asoc: machine hw_params failed\n");
973 goto machine_err;
977 out:
978 mutex_unlock(&pcm_mutex);
979 return ret;
981 machine_err:
982 if (platform->pcm_ops->hw_free)
983 platform->pcm_ops->hw_free(substream);
985 platform_err:
986 if (rtd->cpu_dai->ops.hw_free)
987 rtd->cpu_dai->ops.hw_free(substream);
989 interface_err:
990 if (rtd->codec_dai->ops.hw_free)
991 rtd->codec_dai->ops.hw_free(substream);
993 mutex_unlock(&pcm_mutex);
994 return ret;
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);
1030 return 0;
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;
1038 int ret;
1040 if (rtd->codec_dai->ops.trigger) {
1041 ret = rtd->codec_dai->ops.trigger(substream, cmd);
1042 if (ret < 0)
1043 return ret;
1046 if (platform->pcm_ops->trigger) {
1047 ret = platform->pcm_ops->trigger(substream, cmd);
1048 if (ret < 0)
1049 return ret;
1052 if (rtd->cpu_dai->ops.trigger) {
1053 ret = rtd->cpu_dai->ops.trigger(substream, cmd);
1054 if (ret < 0)
1055 return ret;
1057 return 0;
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,
1070 #ifdef CONFIG_PM
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;
1079 int i;
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;
1105 if (stream != NULL)
1106 snd_soc_dapm_stream_event(codec, stream,
1107 SND_SOC_DAPM_STREAM_SUSPEND);
1108 stream = codec->dai[i].capture.stream_name;
1109 if (stream != NULL)
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);
1126 return 0;
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;
1137 int i;
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;
1153 if (stream != NULL)
1154 snd_soc_dapm_stream_event(codec, stream,
1155 SND_SOC_DAPM_STREAM_RESUME);
1156 stream = codec->dai[i].capture.stream_name;
1157 if (stream != NULL)
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);
1180 return 0;
1183 #else
1184 #define soc_suspend NULL
1185 #define soc_resume NULL
1186 #endif
1188 /* probes a new socdev */
1189 static int soc_probe(struct platform_device *pdev)
1191 int ret = 0, i;
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);
1199 if(ret < 0)
1200 return ret;
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);
1207 if(ret < 0)
1208 goto cpu_dai_err;
1212 if (codec_dev->probe) {
1213 ret = codec_dev->probe(pdev);
1214 if(ret < 0)
1215 goto cpu_dai_err;
1218 if (platform->probe) {
1219 ret = platform->probe(pdev);
1220 if(ret < 0)
1221 goto platform_err;
1224 /* DAPM stream work */
1225 soc_workq = create_workqueue("kdapm");
1226 if (soc_workq == NULL)
1227 goto work_err;
1228 INIT_WORK(&soc_stream_work, close_delayed_work, socdev);
1229 return 0;
1231 work_err:
1232 if (platform->remove)
1233 platform->remove(pdev);
1235 platform_err:
1236 if (codec_dev->remove)
1237 codec_dev->remove(pdev);
1239 cpu_dai_err:
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);
1249 return ret;
1252 /* removes a socdev */
1253 static int soc_remove(struct platform_device *pdev)
1255 int i;
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;
1261 if (soc_workq)
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);
1279 return 0;
1282 /* ASoC platform driver */
1283 static struct platform_driver soc_driver = {
1284 .driver = {
1285 .name = "soc-audio",
1287 .probe = soc_probe,
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;
1302 char new_name[64];
1303 int ret = 0, playback = 0, capture = 0;
1305 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1306 if (rtd == NULL)
1307 return -ENOMEM;
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)
1317 playback = 1;
1318 if (codec_dai->capture.channels_min)
1319 capture = 1;
1321 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1322 capture, &pcm);
1323 if (ret < 0) {
1324 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1325 kfree(rtd);
1326 return ret;
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;
1338 if (playback)
1339 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1341 if (capture)
1342 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1344 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
1345 if (ret < 0) {
1346 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1347 kfree(rtd);
1348 return ret;
1351 pcm->private_free = socdev->platform->pcm_free;
1352 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1353 cpu_dai->name);
1354 return ret;
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)
1366 return 0;
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));
1375 return count;
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);
1395 return -ENOMEM;
1398 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1399 if (codec->ac97->bus == NULL) {
1400 kfree(codec->ac97);
1401 codec->ac97 = NULL;
1402 mutex_unlock(&codec->mutex);
1403 return -ENOMEM;
1406 codec->ac97->bus->ops = ops;
1407 codec->ac97->num = num;
1408 mutex_unlock(&codec->mutex);
1409 return 0;
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);
1423 kfree(codec->ac97);
1424 codec->ac97 = NULL;
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
1434 * @value: new value
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)
1443 int change;
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;
1450 if (change)
1451 snd_soc_write(codec, reg, new);
1453 mutex_unlock(&io_mutex);
1454 return change;
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
1463 * @value: new value
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)
1473 int change;
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);
1482 return change;
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))
1497 return 0;
1498 return rates[rate];
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;
1514 int ret = 0, i;
1516 mutex_lock(&codec->mutex);
1518 /* register a sound card */
1519 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1520 if (!codec->card) {
1521 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1522 codec->name);
1523 mutex_unlock(&codec->mutex);
1524 return -ENODEV;
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);
1534 if (ret < 0) {
1535 printk(KERN_ERR "asoc: can't create pcm %s\n",
1536 machine->dai_link[i].stream_name);
1537 mutex_unlock(&codec->mutex);
1538 return ret;
1542 mutex_unlock(&codec->mutex);
1543 return ret;
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);
1566 if (err < 0) {
1567 printk(KERN_ERR "asoc: failed to init %s\n",
1568 socdev->machine->dai_link[i].stream_name);
1569 continue;
1572 if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1573 ac97 = 1;
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);
1581 if (ret < 0) {
1582 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1583 codec->name);
1584 goto out;
1587 #ifdef CONFIG_SND_SOC_AC97_BUS
1588 if (ac97) {
1589 ret = soc_ac97_dev_register(codec);
1590 if (ret < 0) {
1591 printk(KERN_ERR "asoc: AC97 device register failed\n");
1592 snd_card_free(codec->card);
1593 goto out;
1596 #endif
1598 err = snd_soc_dapm_sys_add(socdev->dev);
1599 if (err < 0)
1600 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1602 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1603 if (err < 0)
1604 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1605 out:
1606 mutex_unlock(&codec->mutex);
1607 return ret;
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
1624 if (codec->ac97)
1625 soc_ac97_dev_unregister(codec);
1626 #endif
1628 if (codec->card)
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;
1654 return 0;
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));
1674 if (long_name)
1675 template.name = long_name;
1676 template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1677 template.index = 0;
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
1689 * mixer control.
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]);
1706 return 0;
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);
1734 return 0;
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;
1752 unsigned short val;
1753 unsigned short mask, bitmask;
1755 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1757 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1758 return -EINVAL;
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)
1763 return -EINVAL;
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
1778 * single mixer.
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;
1788 uinfo->count = 1;
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]);
1795 return 0;
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;
1813 uinfo->type =
1814 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1815 uinfo->count = 1;
1816 uinfo->value.integer.min = 0;
1817 uinfo->value.integer.max = mask;
1818 return 0;
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;
1835 uinfo->count = 1;
1836 uinfo->value.integer.min = 0;
1837 uinfo->value.integer.max = 1;
1838 return 0;
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;
1858 uinfo->type =
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;
1863 return 0;
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;
1891 if (invert) {
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];
1899 return 0;
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;
1921 int err;
1922 unsigned short val, val2, val_mask;
1924 val = (ucontrol->value.integer.value[0] & mask);
1925 if (invert)
1926 val = mask - val;
1927 val_mask = mask << shift;
1928 val = val << shift;
1929 if (shift != rshift) {
1930 val2 = (ucontrol->value.integer.value[1] & mask);
1931 if (invert)
1932 val2 = mask - val2;
1933 val_mask |= mask << rshift;
1934 val |= val2 << rshift;
1936 err = snd_soc_update_bits(codec, reg, val_mask, val);
1937 return err;
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;
1956 uinfo->type =
1957 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1958 uinfo->count = 2;
1959 uinfo->value.integer.min = 0;
1960 uinfo->value.integer.max = mask;
1961 return 0;
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;
1988 if (invert) {
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];
1995 return 0;
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;
2017 int err;
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);
2024 if (invert) {
2025 val = mask - val;
2026 val2 = mask - val2;
2029 val = val << shift;
2030 val2 = val2 << shift;
2032 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
2033 return err;
2035 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2036 return err;
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");