[ALSA] ak4114 - Use global workqueue
[linux-2.6-block.git] / sound / soc / soc-core.c
CommitLineData
db2a4165
FM
1/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
0664d888
LG
5 * Copyright 2005 Openedhand Ltd.
6 *
db2a4165
FM
7 * Author: Liam Girdwood
8 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
0664d888
LG
9 * with code, comments and ideas from :-
10 * Richard Purdie <richard@openedhand.com>
db2a4165
FM
11 *
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.
16 *
17 * Revision history
18 * 12th Aug 2005 Initial version.
19 * 25th Oct 2005 Working Codec, Interface and Platform registration.
20 *
21 * TODO:
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
26 */
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/delay.h>
32#include <linux/pm.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>
42
43/* debug */
44#define SOC_DEBUG 0
45#if SOC_DEBUG
46#define dbg(format, arg...) printk(format, ## arg)
47#else
48#define dbg(format, arg...)
49#endif
50/* debug DAI capabilities matching */
51#define SOC_DEBUG_DAI 0
52#if SOC_DEBUG_DAI
53#define dbgc(format, arg...) printk(format, ## arg)
54#else
55#define dbgc(format, arg...)
56#endif
57
a71a468a
LG
58#define CODEC_CPU(codec, cpu) ((codec << 4) | cpu)
59
db2a4165
FM
60static DEFINE_MUTEX(pcm_mutex);
61static DEFINE_MUTEX(io_mutex);
62static struct workqueue_struct *soc_workq;
db2a4165
FM
63static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
64
65/* supported sample rates */
66/* ATTENTION: these values depend on the definition in pcm.h! */
67static const unsigned int rates[] = {
68 5512, 8000, 11025, 16000, 22050, 32000, 44100,
69 48000, 64000, 88200, 96000, 176400, 192000
70};
71
72/*
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.
76 */
77static int pmdown_time = 5000;
78module_param(pmdown_time, int, 0);
79MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
80
81#ifdef CONFIG_SND_SOC_AC97_BUS
82/* unregister ac97 codec */
83static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
84{
85 if (codec->ac97->dev.bus)
86 device_unregister(&codec->ac97->dev);
87 return 0;
88}
89
90/* stop no dev release warning */
91static void soc_ac97_device_release(struct device *dev){}
92
93/* register ac97 codec to bus */
94static int soc_ac97_dev_register(struct snd_soc_codec *codec)
95{
96 int err;
97
98 codec->ac97->dev.bus = &ac97_bus_type;
99 codec->ac97->dev.parent = NULL;
100 codec->ac97->dev.release = soc_ac97_device_release;
101
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);
105 if (err < 0) {
106 snd_printk(KERN_ERR "Can't register ac97 bus\n");
107 codec->ac97->dev.bus = NULL;
108 return err;
109 }
110 return 0;
111}
112#endif
113
114static inline const char* get_dai_name(int type)
115{
116 switch(type) {
117 case SND_SOC_DAI_AC97:
118 return "AC97";
119 case SND_SOC_DAI_I2S:
120 return "I2S";
121 case SND_SOC_DAI_PCM:
122 return "PCM";
123 }
124 return NULL;
125}
126
127/* get rate format from rate */
128static inline int soc_get_rate_format(int rate)
129{
130 int i;
131
132 for (i = 0; i < ARRAY_SIZE(rates); i++) {
133 if (rates[i] == rate)
134 return 1 << i;
135 }
136 return 0;
137}
138
139/* gets the audio system mclk/sysclk for the given parameters */
140static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
141 struct snd_soc_clock_info *info)
142{
143 struct snd_soc_device *socdev = rtd->socdev;
144 struct snd_soc_machine *machine = socdev->machine;
145 int i;
146
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);
153 }
154 return 0;
155}
156
157/* changes a bitclk multiplier mask to a divider mask */
a71a468a 158static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
db2a4165
FM
159 unsigned int pcmfmt, unsigned int chn)
160{
161 int i, j;
a71a468a 162 u64 bfs_ = 0;
db2a4165
FM
163 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
164
165 if (size <= 0)
166 return 0;
167
168 /* the minimum bit clock that has enough bandwidth */
169 min = size * rate * chn;
a71a468a 170 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
db2a4165 171
a71a468a 172 for (i = 0; i < 64; i++) {
db2a4165 173 if ((bfs >> i) & 0x1) {
a71a468a
LG
174 j = min * (i + 1);
175 bfs_ |= SND_SOC_FSBD(mclk/j);
176 dbgc("rcw --> div support mult %d\n",
177 SND_SOC_FSBD_REAL(1<<i));
db2a4165
FM
178 }
179 }
180
181 return bfs_;
182}
183
184/* changes a bitclk divider mask to a multiplier mask */
a71a468a 185static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
db2a4165
FM
186 unsigned int pcmfmt, unsigned int chn)
187{
188 int i, j;
a71a468a 189 u64 bfs_ = 0;
db2a4165
FM
190
191 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
192
193 if (size <= 0)
194 return 0;
195
196 /* the minimum bit clock that has enough bandwidth */
197 min = size * rate * chn;
a71a468a 198 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
db2a4165 199
a71a468a 200 for (i = 0; i < 64; i++) {
db2a4165 201 if ((bfs >> i) & 0x1) {
a71a468a 202 j = mclk / (i + 1);
db2a4165 203 if (j >= min) {
a71a468a
LG
204 bfs_ |= SND_SOC_FSBW(j/min);
205 dbgc("div --> rcw support div %d\n",
206 SND_SOC_FSBW_REAL(1<<i));
db2a4165
FM
207 }
208 }
209 }
210
211 return bfs_;
212}
213
a71a468a
LG
214/* changes a constant bitclk to a multiplier mask */
215static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
216 unsigned int pcmfmt, unsigned int chn)
217{
218 unsigned int bfs_ = rate * bfs;
219 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
220
221 if (size <= 0)
222 return 0;
223
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);
227
228 if (bfs_ < min)
229 return 0;
230 else {
231 bfs_ = SND_SOC_FSBW(bfs_/min);
232 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
233 return bfs_;
234 }
235}
236
237/* changes a bitclk multiplier mask to a divider mask */
238static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
239 unsigned int pcmfmt, unsigned int chn)
240{
241 unsigned int bfs_ = rate * bfs;
242 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
243
244 if (size <= 0)
245 return 0;
246
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);
250
251 if (bfs_ < min)
252 return 0;
253 else {
254 bfs_ = SND_SOC_FSBW(mclk/bfs_);
255 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
256 return bfs_;
257 }
258}
259
db2a4165
FM
260/* Matches codec DAI and SoC CPU DAI hardware parameters */
261static int soc_hw_match_params(struct snd_pcm_substream *substream,
262 struct snd_pcm_hw_params *params)
263{
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;
a71a468a 268 unsigned int fs, mclk, rate = params_rate(params),
db2a4165
FM
269 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
270 u16 fmt = 0;
a71a468a 271 u64 codec_bfs, cpu_bfs;
db2a4165
FM
272
273 dbg("asoc: match version %s\n", SND_SOC_VERSION);
274 clk_info.rate = rate;
275 pcmrate = soc_get_rate_format(rate);
276
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];
282
283 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
284 pcmrate)) {
285 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
286 continue;
287 }
288
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);
292 continue;
293 }
294
295 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
296 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
297 j, k);
298 continue;
299 }
300
301 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
302 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
303 continue;
304 }
305
306 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
307 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
308 continue;
309 }
310
311 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
312 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
313 continue;
314 }
315
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;
324
325 /* make sure the ratio between rate and master
326 * clock is acceptable*/
327 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
328 if (fs == 0) {
329 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
330 continue;
331 }
332 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
333 rtd->codec_dai->dai_runtime.fs = fs;
334
335 /* calculate audio system clocking using slowest clocks possible*/
336 mclk = soc_get_mclk(rtd, &clk_info);
337 if (mclk == 0) {
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);
341 continue;
342 }
343
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);
348
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))
354 chn <<= 1;
355
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.
360 */
a71a468a
LG
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):
db2a4165
FM
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);
a71a468a
LG
367 break;
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,
db2a4165
FM
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);
a71a468a 374 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
db2a4165
FM
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);
a71a468a
LG
378 break;
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,
db2a4165
FM
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);
a71a468a 385 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
db2a4165
FM
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);
a71a468a
LG
389 break;
390 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
391 /* codec & cpu bfs rate rcw multipliers */
db2a4165
FM
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);
a71a468a
LG
395 break;
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;
403 } else
404 rtd->cpu_dai->dai_runtime.bfs = 0;
405 break;
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;
413 } else
414 rtd->cpu_dai->dai_runtime.bfs = 0;
415 break;
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;
423 } else
424 rtd->cpu_dai->dai_runtime.bfs = 0;
425 break;
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);
2e26e483 430 if(cpu_dai_mode->bfs & codec_bfs) {
a71a468a
LG
431 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
432 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
433 } else
434 rtd->cpu_dai->dai_runtime.bfs = 0;
435 break;
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 =
441 codec_dai_mode->bfs;
442 else
443 rtd->cpu_dai->dai_runtime.bfs =
444 rtd->codec_dai->dai_runtime.bfs = 0;
445 break;
db2a4165
FM
446 }
447
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);
a71a468a 452 dbgc("asoc: cpu_dai %llu codec %llu\n",
db2a4165
FM
453 rtd->cpu_dai->dai_runtime.bfs,
454 rtd->codec_dai->dai_runtime.bfs);
455 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
456 continue;
457 }
458
459 goto found;
460 }
461 }
462 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
463 return -EINVAL;
464
465found:
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);
470
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;
475
476 /* for debug atm */
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);
a71a468a
LG
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",
db2a4165 487 rtd->codec_dai->dai_runtime.fs, mclk,
a71a468a
LG
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);
496 } else
497 codec_bclk = 0;
498
db2a4165
FM
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);
a71a468a
LG
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",
db2a4165 508 rtd->cpu_dai->dai_runtime.fs, mclk,
a71a468a
LG
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);
517 } else
518 cpu_bclk = 0;
db2a4165
FM
519
520 /*
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
0664d888
LG
524 * modes for the codec or cpu DAI. Check your codec or CPU DAI
525 * config_sysclock() functions.
db2a4165 526 */
a71a468a 527 if (cpu_bclk != codec_bclk && cpu_bclk){
db2a4165
FM
528 printk(KERN_ERR
529 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
530 );
531 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
532 }
533
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");
537 break;
538 case SND_SOC_DAIFMT_CBS_CFM:
539 dbg("asoc: DAI codec BCLK slave, LRC master\n");
540 break;
541 case SND_SOC_DAIFMT_CBM_CFS:
542 dbg("asoc: DAI codec BCLK master, LRC slave\n");
543 break;
544 case SND_SOC_DAIFMT_CBS_CFS:
545 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
546 break;
547 }
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));
553
554 return 0;
555}
556
557static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
558{
559 int i;
560 u32 rates = 0;
561
562 for(i = 0; i < nmodes; i++)
563 rates |= modes[i].pcmrate;
564
565 return rates;
566}
567
568static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
569{
570 int i;
571 u64 formats = 0;
572
573 for(i = 0; i < nmodes; i++)
574 formats |= modes[i].pcmfmt;
575
576 return formats;
577}
578
579/*
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.
583 */
584static int soc_pcm_open(struct snd_pcm_substream *substream)
585{
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;
593 int ret = 0;
594
595 mutex_lock(&pcm_mutex);
596
597 /* startup the audio subsystem */
598 if (rtd->cpu_dai->ops.startup) {
599 ret = rtd->cpu_dai->ops.startup(substream);
600 if (ret < 0) {
601 printk(KERN_ERR "asoc: can't open interface %s\n",
602 rtd->cpu_dai->name);
603 goto out;
604 }
605 }
606
607 if (platform->pcm_ops->open) {
608 ret = platform->pcm_ops->open(substream);
609 if (ret < 0) {
610 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
611 goto platform_err;
612 }
613 }
614
615 if (machine->ops && machine->ops->startup) {
616 ret = machine->ops->startup(substream);
617 if (ret < 0) {
618 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
619 goto machine_err;
620 }
621 }
622
623 if (rtd->codec_dai->ops.startup) {
624 ret = rtd->codec_dai->ops.startup(substream);
625 if (ret < 0) {
626 printk(KERN_ERR "asoc: can't open codec %s\n",
627 rtd->codec_dai->name);
628 goto codec_dai_err;
629 }
630 }
631
632 /* create runtime params from DMA, codec and cpu DAI */
633 if (runtime->hw.rates)
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 else
638 runtime->hw.rates =
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);
645 else
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);
649
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);
664 } else {
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);
677 }
678
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);
683 goto codec_dai_err;
684 }
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);
688 goto codec_dai_err;
689 }
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);
693 goto codec_dai_err;
694 }
695
696 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
b5c5fd24
LG
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);
db2a4165
FM
702
703
704 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
705 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
706 else
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);
712 return 0;
713
714codec_dai_err:
715 if (machine->ops && machine->ops->shutdown)
716 machine->ops->shutdown(substream);
717
718machine_err:
719 if (platform->pcm_ops->close)
720 platform->pcm_ops->close(substream);
721
722platform_err:
723 if (rtd->cpu_dai->ops.shutdown)
724 rtd->cpu_dai->ops.shutdown(substream);
725out:
726 mutex_unlock(&pcm_mutex);
727 return ret;
728}
729
730/*
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.
734 */
4484bb2e 735static void close_delayed_work(struct work_struct *work)
db2a4165 736{
4484bb2e
AM
737 struct snd_soc_device *socdev =
738 container_of(work, struct snd_soc_device, delayed_work.work);
db2a4165
FM
739 struct snd_soc_codec *codec = socdev->codec;
740 struct snd_soc_codec_dai *codec_dai;
741 int i;
742
743 mutex_lock(&pcm_mutex);
744 for(i = 0; i < codec->num_dai; i++) {
745 codec_dai = &codec->dai[i];
746
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");
751
752 /* are we waiting on this codec DAI stream */
753 if (codec_dai->pop_wait == 1) {
754
755 codec_dai->pop_wait = 0;
756 snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
757 SND_SOC_DAPM_STREAM_STOP);
758
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);
765 }
766 }
767 }
768 mutex_unlock(&pcm_mutex);
769}
770
771/*
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
774 * shutdown.
775 */
776static int soc_codec_close(struct snd_pcm_substream *substream)
777{
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;
783
784 mutex_lock(&pcm_mutex);
785
786 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
787 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
788 else
789 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
790
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;
794 }
795 codec->active--;
796
797 if (rtd->cpu_dai->ops.shutdown)
798 rtd->cpu_dai->ops.shutdown(substream);
799
800 if (rtd->codec_dai->ops.shutdown)
801 rtd->codec_dai->ops.shutdown(substream);
802
803 if (machine->ops && machine->ops->shutdown)
804 machine->ops->shutdown(substream);
805
806 if (platform->pcm_ops->close)
807 platform->pcm_ops->close(substream);
808 rtd->cpu_dai->runtime = NULL;
809
810 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
811 /* start delayed pop wq here for playback streams */
812 rtd->codec_dai->pop_wait = 1;
4484bb2e 813 queue_delayed_work(soc_workq, &socdev->delayed_work,
db2a4165
FM
814 msecs_to_jiffies(pmdown_time));
815 } else {
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);
819
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);
823 }
824 }
825
826 mutex_unlock(&pcm_mutex);
827 return 0;
828}
829
830/*
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.
834 */
835static int soc_pcm_prepare(struct snd_pcm_substream *substream)
836{
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;
841 int ret = 0;
842
843 mutex_lock(&pcm_mutex);
844 if (platform->pcm_ops->prepare) {
845 ret = platform->pcm_ops->prepare(substream);
a71a468a
LG
846 if (ret < 0) {
847 printk(KERN_ERR "asoc: platform prepare error\n");
db2a4165 848 goto out;
a71a468a 849 }
db2a4165
FM
850 }
851
852 if (rtd->codec_dai->ops.prepare) {
853 ret = rtd->codec_dai->ops.prepare(substream);
a71a468a
LG
854 if (ret < 0) {
855 printk(KERN_ERR "asoc: codec DAI prepare error\n");
db2a4165 856 goto out;
a71a468a 857 }
db2a4165
FM
858 }
859
860 if (rtd->cpu_dai->ops.prepare)
861 ret = rtd->cpu_dai->ops.prepare(substream);
862
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);
871 else {
872 rtd->codec_dai->pop_wait = 0;
4484bb2e 873 cancel_delayed_work(&socdev->delayed_work);
db2a4165
FM
874 if (rtd->codec_dai->digital_mute)
875 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
876 }
877 } else {
878 /* no delayed work - do we need to power up codec */
879 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
880
881 if (codec->dapm_event)
882 codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
883
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);
888 else
889 snd_soc_dapm_stream_event(codec,
890 rtd->codec_dai->capture.stream_name,
891 SND_SOC_DAPM_STREAM_START);
892
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);
897
898 } else {
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);
904 else
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);
910 }
911 }
912
913out:
914 mutex_unlock(&pcm_mutex);
915 return ret;
916}
917
918/*
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.
922 */
923static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
924 struct snd_pcm_hw_params *params)
925{
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;
930 int ret = 0;
931
932 mutex_lock(&pcm_mutex);
933
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);
937 if (ret < 0)
938 goto out;
939 } else {
940 struct snd_soc_clock_info clk_info;
941 clk_info.rate = params_rate(params);
942 ret = soc_get_mclk(rtd, &clk_info);
943 if (ret < 0)
944 goto out;
945 }
946
947 if (rtd->codec_dai->ops.hw_params) {
948 ret = rtd->codec_dai->ops.hw_params(substream, params);
949 if (ret < 0) {
950 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
951 rtd->codec_dai->name);
952 goto out;
953 }
954 }
955
956 if (rtd->cpu_dai->ops.hw_params) {
957 ret = rtd->cpu_dai->ops.hw_params(substream, params);
958 if (ret < 0) {
959 printk(KERN_ERR "asoc: can't set interface %s hw params\n",
960 rtd->cpu_dai->name);
961 goto interface_err;
962 }
963 }
964
965 if (platform->pcm_ops->hw_params) {
966 ret = platform->pcm_ops->hw_params(substream, params);
967 if (ret < 0) {
968 printk(KERN_ERR "asoc: can't set platform %s hw params\n",
969 platform->name);
970 goto platform_err;
971 }
972 }
973
974 if (machine->ops && machine->ops->hw_params) {
975 ret = machine->ops->hw_params(substream, params);
976 if (ret < 0) {
977 printk(KERN_ERR "asoc: machine hw_params failed\n");
978 goto machine_err;
979 }
980 }
981
982out:
983 mutex_unlock(&pcm_mutex);
984 return ret;
985
986machine_err:
987 if (platform->pcm_ops->hw_free)
988 platform->pcm_ops->hw_free(substream);
989
990platform_err:
991 if (rtd->cpu_dai->ops.hw_free)
992 rtd->cpu_dai->ops.hw_free(substream);
993
994interface_err:
995 if (rtd->codec_dai->ops.hw_free)
996 rtd->codec_dai->ops.hw_free(substream);
997
998 mutex_unlock(&pcm_mutex);
999 return ret;
1000}
1001
1002/*
1003 * Free's resources allocated by hw_params, can be called multiple times
1004 */
1005static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1006{
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;
1012
1013 mutex_lock(&pcm_mutex);
1014
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);
1018
1019 /* free any machine hw params */
1020 if (machine->ops && machine->ops->hw_free)
1021 machine->ops->hw_free(substream);
1022
1023 /* free any DMA resources */
1024 if (platform->pcm_ops->hw_free)
1025 platform->pcm_ops->hw_free(substream);
1026
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);
1030
1031 if (rtd->cpu_dai->ops.hw_free)
1032 rtd->cpu_dai->ops.hw_free(substream);
1033
1034 mutex_unlock(&pcm_mutex);
1035 return 0;
1036}
1037
1038static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1039{
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;
1043 int ret;
1044
1045 if (rtd->codec_dai->ops.trigger) {
1046 ret = rtd->codec_dai->ops.trigger(substream, cmd);
1047 if (ret < 0)
1048 return ret;
1049 }
1050
1051 if (platform->pcm_ops->trigger) {
1052 ret = platform->pcm_ops->trigger(substream, cmd);
1053 if (ret < 0)
1054 return ret;
1055 }
1056
1057 if (rtd->cpu_dai->ops.trigger) {
1058 ret = rtd->cpu_dai->ops.trigger(substream, cmd);
1059 if (ret < 0)
1060 return ret;
1061 }
1062 return 0;
1063}
1064
1065/* ASoC PCM operations */
1066static 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,
1073};
1074
1075#ifdef CONFIG_PM
1076/* powers down audio subsystem for suspend */
1077static int soc_suspend(struct platform_device *pdev, pm_message_t state)
1078{
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;
1084 int i;
1085
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);
1091 }
1092
1093 if (machine->suspend_pre)
1094 machine->suspend_pre(pdev, state);
1095
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);
1102 }
1103
1104 /* close any waiting streams and save state */
1105 flush_workqueue(soc_workq);
1106 codec->suspend_dapm_state = codec->dapm_state;
1107
1108 for(i = 0; i < codec->num_dai; i++) {
1109 char *stream = codec->dai[i].playback.stream_name;
1110 if (stream != NULL)
1111 snd_soc_dapm_stream_event(codec, stream,
1112 SND_SOC_DAPM_STREAM_SUSPEND);
1113 stream = codec->dai[i].capture.stream_name;
1114 if (stream != NULL)
1115 snd_soc_dapm_stream_event(codec, stream,
1116 SND_SOC_DAPM_STREAM_SUSPEND);
1117 }
1118
1119 if (codec_dev->suspend)
1120 codec_dev->suspend(pdev, state);
1121
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);
1126 }
1127
1128 if (machine->suspend_post)
1129 machine->suspend_post(pdev, state);
1130
1131 return 0;
1132}
1133
1134/* powers up audio subsystem after a suspend */
1135static int soc_resume(struct platform_device *pdev)
1136{
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;
1142 int i;
1143
1144 if (machine->resume_pre)
1145 machine->resume_pre(pdev);
1146
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);
1151 }
1152
1153 if (codec_dev->resume)
1154 codec_dev->resume(pdev);
1155
1156 for(i = 0; i < codec->num_dai; i++) {
1157 char* stream = codec->dai[i].playback.stream_name;
1158 if (stream != NULL)
1159 snd_soc_dapm_stream_event(codec, stream,
1160 SND_SOC_DAPM_STREAM_RESUME);
1161 stream = codec->dai[i].capture.stream_name;
1162 if (stream != NULL)
1163 snd_soc_dapm_stream_event(codec, stream,
1164 SND_SOC_DAPM_STREAM_RESUME);
1165 }
1166
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);
1172 }
1173
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);
1180 }
1181
1182 if (machine->resume_post)
1183 machine->resume_post(pdev);
1184
1185 return 0;
1186}
1187
1188#else
1189#define soc_suspend NULL
1190#define soc_resume NULL
1191#endif
1192
1193/* probes a new socdev */
1194static int soc_probe(struct platform_device *pdev)
1195{
1196 int ret = 0, i;
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;
1201
1202 if (machine->probe) {
1203 ret = machine->probe(pdev);
1204 if(ret < 0)
1205 return ret;
1206 }
1207
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);
1212 if(ret < 0)
1213 goto cpu_dai_err;
1214 }
1215 }
1216
1217 if (codec_dev->probe) {
1218 ret = codec_dev->probe(pdev);
1219 if(ret < 0)
1220 goto cpu_dai_err;
1221 }
1222
1223 if (platform->probe) {
1224 ret = platform->probe(pdev);
1225 if(ret < 0)
1226 goto platform_err;
1227 }
1228
1229 /* DAPM stream work */
1230 soc_workq = create_workqueue("kdapm");
1231 if (soc_workq == NULL)
1232 goto work_err;
4484bb2e 1233 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
db2a4165
FM
1234 return 0;
1235
1236work_err:
1237 if (platform->remove)
1238 platform->remove(pdev);
1239
1240platform_err:
1241 if (codec_dev->remove)
1242 codec_dev->remove(pdev);
1243
1244cpu_dai_err:
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);
1249 }
1250
1251 if (machine->remove)
1252 machine->remove(pdev);
1253
1254 return ret;
1255}
1256
1257/* removes a socdev */
1258static int soc_remove(struct platform_device *pdev)
1259{
1260 int i;
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;
1265
1266 if (soc_workq)
1267 destroy_workqueue(soc_workq);
1268
1269 if (platform->remove)
1270 platform->remove(pdev);
1271
1272 if (codec_dev->remove)
1273 codec_dev->remove(pdev);
1274
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);
1279 }
1280
1281 if (machine->remove)
1282 machine->remove(pdev);
1283
1284 return 0;
1285}
1286
1287/* ASoC platform driver */
1288static struct platform_driver soc_driver = {
1289 .driver = {
1290 .name = "soc-audio",
1291 },
1292 .probe = soc_probe,
1293 .remove = soc_remove,
1294 .suspend = soc_suspend,
1295 .resume = soc_resume,
1296};
1297
1298/* create a new pcm */
1299static int soc_new_pcm(struct snd_soc_device *socdev,
1300 struct snd_soc_dai_link *dai_link, int num)
1301{
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;
1307 char new_name[64];
1308 int ret = 0, playback = 0, capture = 0;
1309
1310 rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
1311 if (rtd == NULL)
1312 return -ENOMEM;
1313 rtd->cpu_dai = cpu_dai;
1314 rtd->codec_dai = codec_dai;
1315 rtd->socdev = socdev;
1316
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);
1320
1321 if (codec_dai->playback.channels_min)
1322 playback = 1;
1323 if (codec_dai->capture.channels_min)
1324 capture = 1;
1325
1326 ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
1327 capture, &pcm);
1328 if (ret < 0) {
1329 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1330 kfree(rtd);
1331 return ret;
1332 }
1333
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;
1342
1343 if (playback)
1344 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1345
1346 if (capture)
1347 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1348
1349 ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
1350 if (ret < 0) {
1351 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1352 kfree(rtd);
1353 return ret;
1354 }
1355
1356 pcm->private_free = socdev->platform->pcm_free;
1357 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1358 cpu_dai->name);
1359 return ret;
1360}
1361
1362/* codec register dump */
1363static ssize_t codec_reg_show(struct device *dev,
1364 struct device_attribute *attr, char *buf)
1365{
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;
1369
1370 if (!codec->reg_cache_size)
1371 return 0;
1372
1373 if (codec->reg_cache_step)
1374 step = codec->reg_cache_step;
1375
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));
1379
1380 return count;
1381}
1382static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1383
1384/**
1385 * snd_soc_new_ac97_codec - initailise AC97 device
1386 * @codec: audio codec
1387 * @ops: AC97 bus operations
1388 * @num: AC97 codec number
1389 *
1390 * Initialises AC97 codec resources for use by ad-hoc devices only.
1391 */
1392int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1393 struct snd_ac97_bus_ops *ops, int num)
1394{
1395 mutex_lock(&codec->mutex);
1396
1397 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1398 if (codec->ac97 == NULL) {
1399 mutex_unlock(&codec->mutex);
1400 return -ENOMEM;
1401 }
1402
1403 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1404 if (codec->ac97->bus == NULL) {
1405 kfree(codec->ac97);
1406 codec->ac97 = NULL;
1407 mutex_unlock(&codec->mutex);
1408 return -ENOMEM;
1409 }
1410
1411 codec->ac97->bus->ops = ops;
1412 codec->ac97->num = num;
1413 mutex_unlock(&codec->mutex);
1414 return 0;
1415}
1416EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1417
1418/**
1419 * snd_soc_free_ac97_codec - free AC97 codec device
1420 * @codec: audio codec
1421 *
1422 * Frees AC97 codec device resources.
1423 */
1424void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1425{
1426 mutex_lock(&codec->mutex);
1427 kfree(codec->ac97->bus);
1428 kfree(codec->ac97);
1429 codec->ac97 = NULL;
1430 mutex_unlock(&codec->mutex);
1431}
1432EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1433
1434/**
1435 * snd_soc_update_bits - update codec register bits
1436 * @codec: audio codec
1437 * @reg: codec register
1438 * @mask: register mask
1439 * @value: new value
1440 *
1441 * Writes new register value.
1442 *
1443 * Returns 1 for change else 0.
1444 */
1445int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1446 unsigned short mask, unsigned short value)
1447{
1448 int change;
1449 unsigned short old, new;
1450
1451 mutex_lock(&io_mutex);
1452 old = snd_soc_read(codec, reg);
1453 new = (old & ~mask) | value;
1454 change = old != new;
1455 if (change)
1456 snd_soc_write(codec, reg, new);
1457
1458 mutex_unlock(&io_mutex);
1459 return change;
1460}
1461EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1462
1463/**
1464 * snd_soc_test_bits - test register for change
1465 * @codec: audio codec
1466 * @reg: codec register
1467 * @mask: register mask
1468 * @value: new value
1469 *
1470 * Tests a register with a new value and checks if the new value is
1471 * different from the old value.
1472 *
1473 * Returns 1 for change else 0.
1474 */
1475int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1476 unsigned short mask, unsigned short value)
1477{
1478 int change;
1479 unsigned short old, new;
1480
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);
1486
1487 return change;
1488}
1489EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1490
1491/**
1492 * snd_soc_get_rate - get int sample rate
1493 * @hwpcmrate: the hardware pcm rate
1494 *
1495 * Returns the audio rate integaer value, else 0.
1496 */
1497int snd_soc_get_rate(int hwpcmrate)
1498{
1499 int rate = ffs(hwpcmrate) - 1;
1500
1501 if (rate > ARRAY_SIZE(rates))
1502 return 0;
1503 return rates[rate];
1504}
1505EXPORT_SYMBOL_GPL(snd_soc_get_rate);
1506
1507/**
1508 * snd_soc_new_pcms - create new sound card and pcms
1509 * @socdev: the SoC audio device
1510 *
1511 * Create a new sound card based upon the codec and interface pcms.
1512 *
1513 * Returns 0 for success, else error.
1514 */
1515int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
1516{
1517 struct snd_soc_codec *codec = socdev->codec;
1518 struct snd_soc_machine *machine = socdev->machine;
1519 int ret = 0, i;
1520
1521 mutex_lock(&codec->mutex);
1522
1523 /* register a sound card */
1524 codec->card = snd_card_new(idx, xid, codec->owner, 0);
1525 if (!codec->card) {
1526 printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
1527 codec->name);
1528 mutex_unlock(&codec->mutex);
1529 return -ENODEV;
1530 }
1531
1532 codec->card->dev = socdev->dev;
1533 codec->card->private_data = codec;
1534 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1535
1536 /* create the pcms */
1537 for(i = 0; i < machine->num_links; i++) {
1538 ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
1539 if (ret < 0) {
1540 printk(KERN_ERR "asoc: can't create pcm %s\n",
1541 machine->dai_link[i].stream_name);
1542 mutex_unlock(&codec->mutex);
1543 return ret;
1544 }
1545 }
1546
1547 mutex_unlock(&codec->mutex);
1548 return ret;
1549}
1550EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1551
1552/**
1553 * snd_soc_register_card - register sound card
1554 * @socdev: the SoC audio device
1555 *
1556 * Register a SoC sound card. Also registers an AC97 device if the
1557 * codec is AC97 for ad hoc devices.
1558 *
1559 * Returns 0 for success, else error.
1560 */
1561int snd_soc_register_card(struct snd_soc_device *socdev)
1562{
1563 struct snd_soc_codec *codec = socdev->codec;
1564 struct snd_soc_machine *machine = socdev->machine;
12e74f7d 1565 int ret = 0, i, ac97 = 0, err = 0;
db2a4165
FM
1566
1567 mutex_lock(&codec->mutex);
1568 for(i = 0; i < machine->num_links; i++) {
12e74f7d
LG
1569 if (socdev->machine->dai_link[i].init) {
1570 err = socdev->machine->dai_link[i].init(codec);
1571 if (err < 0) {
1572 printk(KERN_ERR "asoc: failed to init %s\n",
1573 socdev->machine->dai_link[i].stream_name);
1574 continue;
1575 }
1576 }
db2a4165
FM
1577 if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1578 ac97 = 1;
1579 }
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);
1584
1585 ret = snd_card_register(codec->card);
1586 if (ret < 0) {
1587 printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
1588 codec->name);
12e74f7d 1589 goto out;
db2a4165
FM
1590 }
1591
1592#ifdef CONFIG_SND_SOC_AC97_BUS
12e74f7d
LG
1593 if (ac97) {
1594 ret = soc_ac97_dev_register(codec);
1595 if (ret < 0) {
1596 printk(KERN_ERR "asoc: AC97 device register failed\n");
1597 snd_card_free(codec->card);
1598 goto out;
1599 }
1600 }
db2a4165
FM
1601#endif
1602
12e74f7d
LG
1603 err = snd_soc_dapm_sys_add(socdev->dev);
1604 if (err < 0)
1605 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1606
1607 err = device_create_file(socdev->dev, &dev_attr_codec_reg);
1608 if (err < 0)
1609 printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
1610out:
db2a4165
FM
1611 mutex_unlock(&codec->mutex);
1612 return ret;
1613}
1614EXPORT_SYMBOL_GPL(snd_soc_register_card);
1615
1616/**
1617 * snd_soc_free_pcms - free sound card and pcms
1618 * @socdev: the SoC audio device
1619 *
1620 * Frees sound card and pcms associated with the socdev.
1621 * Also unregister the codec if it is an AC97 device.
1622 */
1623void snd_soc_free_pcms(struct snd_soc_device *socdev)
1624{
1625 struct snd_soc_codec *codec = socdev->codec;
1626
1627 mutex_lock(&codec->mutex);
1628#ifdef CONFIG_SND_SOC_AC97_BUS
1629 if (codec->ac97)
1630 soc_ac97_dev_unregister(codec);
1631#endif
1632
1633 if (codec->card)
1634 snd_card_free(codec->card);
1635 device_remove_file(socdev->dev, &dev_attr_codec_reg);
1636 mutex_unlock(&codec->mutex);
1637}
1638EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1639
1640/**
1641 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1642 * @substream: the pcm substream
1643 * @hw: the hardware parameters
1644 *
1645 * Sets the substream runtime hardware parameters.
1646 */
1647int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1648 const struct snd_pcm_hardware *hw)
1649{
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;
1659 return 0;
1660}
1661EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1662
1663/**
1664 * snd_soc_cnew - create new control
1665 * @_template: control template
1666 * @data: control private data
1667 * @lnng_name: control long name
1668 *
1669 * Create a new mixer control from a template control.
1670 *
1671 * Returns 0 for success, else error.
1672 */
1673struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1674 void *data, char *long_name)
1675{
1676 struct snd_kcontrol_new template;
1677
1678 memcpy(&template, _template, sizeof(template));
1679 if (long_name)
1680 template.name = long_name;
1681 template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1682 template.index = 0;
1683
1684 return snd_ctl_new1(&template, data);
1685}
1686EXPORT_SYMBOL_GPL(snd_soc_cnew);
1687
1688/**
1689 * snd_soc_info_enum_double - enumerated double mixer info callback
1690 * @kcontrol: mixer control
1691 * @uinfo: control element information
1692 *
1693 * Callback to provide information about a double enumerated
1694 * mixer control.
1695 *
1696 * Returns 0 for success.
1697 */
1698int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_info *uinfo)
1700{
1701 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1702
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;
1706
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]);
1711 return 0;
1712}
1713EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1714
1715/**
1716 * snd_soc_get_enum_double - enumerated double mixer get callback
1717 * @kcontrol: mixer control
1718 * @uinfo: control element information
1719 *
1720 * Callback to get the value of a double enumerated mixer.
1721 *
1722 * Returns 0 for success.
1723 */
1724int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1725 struct snd_ctl_elem_value *ucontrol)
1726{
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;
1730
1731 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1732 ;
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);
1738
1739 return 0;
1740}
1741EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1742
1743/**
1744 * snd_soc_put_enum_double - enumerated double mixer put callback
1745 * @kcontrol: mixer control
1746 * @uinfo: control element information
1747 *
1748 * Callback to set the value of a double enumerated mixer.
1749 *
1750 * Returns 0 for success.
1751 */
1752int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1753 struct snd_ctl_elem_value *ucontrol)
1754{
1755 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1756 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1757 unsigned short val;
1758 unsigned short mask, bitmask;
1759
1760 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1761 ;
1762 if (ucontrol->value.enumerated.item[0] > e->mask - 1)
1763 return -EINVAL;
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)
1768 return -EINVAL;
1769 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1770 mask |= (bitmask - 1) << e->shift_r;
1771 }
1772
1773 return snd_soc_update_bits(codec, e->reg, mask, val);
1774}
1775EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1776
1777/**
1778 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1779 * @kcontrol: mixer control
1780 * @uinfo: control element information
1781 *
1782 * Callback to provide information about an external enumerated
1783 * single mixer.
1784 *
1785 * Returns 0 for success.
1786 */
1787int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_info *uinfo)
1789{
1790 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1791
1792 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1793 uinfo->count = 1;
1794 uinfo->value.enumerated.items = e->mask;
1795
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]);
1800 return 0;
1801}
1802EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1803
1804/**
1805 * snd_soc_info_volsw_ext - external single mixer info callback
1806 * @kcontrol: mixer control
1807 * @uinfo: control element information
1808 *
1809 * Callback to provide information about a single external mixer control.
1810 *
1811 * Returns 0 for success.
1812 */
1813int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_info *uinfo)
1815{
1816 int mask = kcontrol->private_value;
1817
1818 uinfo->type =
1819 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1820 uinfo->count = 1;
1821 uinfo->value.integer.min = 0;
1822 uinfo->value.integer.max = mask;
1823 return 0;
1824}
1825EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1826
1827/**
1828 * snd_soc_info_bool_ext - external single boolean mixer info callback
1829 * @kcontrol: mixer control
1830 * @uinfo: control element information
1831 *
1832 * Callback to provide information about a single boolean external mixer control.
1833 *
1834 * Returns 0 for success.
1835 */
1836int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
1837 struct snd_ctl_elem_info *uinfo)
1838{
1839 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1840 uinfo->count = 1;
1841 uinfo->value.integer.min = 0;
1842 uinfo->value.integer.max = 1;
1843 return 0;
1844}
1845EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
1846
1847/**
1848 * snd_soc_info_volsw - single mixer info callback
1849 * @kcontrol: mixer control
1850 * @uinfo: control element information
1851 *
1852 * Callback to provide information about a single mixer control.
1853 *
1854 * Returns 0 for success.
1855 */
1856int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1857 struct snd_ctl_elem_info *uinfo)
1858{
1859 int mask = (kcontrol->private_value >> 16) & 0xff;
1860 int shift = (kcontrol->private_value >> 8) & 0x0f;
1861 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1862
1863 uinfo->type =
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;
1868 return 0;
1869}
1870EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1871
1872/**
1873 * snd_soc_get_volsw - single mixer get callback
1874 * @kcontrol: mixer control
1875 * @uinfo: control element information
1876 *
1877 * Callback to get the value of a single mixer control.
1878 *
1879 * Returns 0 for success.
1880 */
1881int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1882 struct snd_ctl_elem_value *ucontrol)
1883{
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;
1890
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;
1896 if (invert) {
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];
1902 }
1903
1904 return 0;
1905}
1906EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1907
1908/**
1909 * snd_soc_put_volsw - single mixer put callback
1910 * @kcontrol: mixer control
1911 * @uinfo: control element information
1912 *
1913 * Callback to set the value of a single mixer control.
1914 *
1915 * Returns 0 for success.
1916 */
1917int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
1919{
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;
1926 int err;
1927 unsigned short val, val2, val_mask;
1928
1929 val = (ucontrol->value.integer.value[0] & mask);
1930 if (invert)
1931 val = mask - val;
1932 val_mask = mask << shift;
1933 val = val << shift;
1934 if (shift != rshift) {
1935 val2 = (ucontrol->value.integer.value[1] & mask);
1936 if (invert)
1937 val2 = mask - val2;
1938 val_mask |= mask << rshift;
1939 val |= val2 << rshift;
1940 }
1941 err = snd_soc_update_bits(codec, reg, val_mask, val);
1942 return err;
1943}
1944EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1945
1946/**
1947 * snd_soc_info_volsw_2r - double mixer info callback
1948 * @kcontrol: mixer control
1949 * @uinfo: control element information
1950 *
1951 * Callback to provide information about a double mixer control that
1952 * spans 2 codec registers.
1953 *
1954 * Returns 0 for success.
1955 */
1956int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
1958{
1959 int mask = (kcontrol->private_value >> 12) & 0xff;
1960
1961 uinfo->type =
1962 mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1963 uinfo->count = 2;
1964 uinfo->value.integer.min = 0;
1965 uinfo->value.integer.max = mask;
1966 return 0;
1967}
1968EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1969
1970/**
1971 * snd_soc_get_volsw_2r - double mixer get callback
1972 * @kcontrol: mixer control
1973 * @uinfo: control element information
1974 *
1975 * Callback to get the value of a double mixer control that spans 2 registers.
1976 *
1977 * Returns 0 for success.
1978 */
1979int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
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;
1988
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;
1993 if (invert) {
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];
1998 }
1999
2000 return 0;
2001}
2002EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2003
2004/**
2005 * snd_soc_put_volsw_2r - double mixer set callback
2006 * @kcontrol: mixer control
2007 * @uinfo: control element information
2008 *
2009 * Callback to set the value of a double mixer control that spans 2 registers.
2010 *
2011 * Returns 0 for success.
2012 */
2013int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_value *ucontrol)
2015{
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;
2022 int err;
2023 unsigned short val, val2, val_mask;
2024
2025 val_mask = mask << shift;
2026 val = (ucontrol->value.integer.value[0] & mask);
2027 val2 = (ucontrol->value.integer.value[1] & mask);
2028
2029 if (invert) {
2030 val = mask - val;
2031 val2 = mask - val2;
2032 }
2033
2034 val = val << shift;
2035 val2 = val2 << shift;
2036
2037 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
2038 return err;
2039
2040 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2041 return err;
2042}
2043EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2044
2045static int __devinit snd_soc_init(void)
2046{
2047 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
2048 return platform_driver_register(&soc_driver);
2049}
2050
2051static void snd_soc_exit(void)
2052{
2053 platform_driver_unregister(&soc_driver);
2054}
2055
2056module_init(snd_soc_init);
2057module_exit(snd_soc_exit);
2058
2059/* Module information */
2060MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
2061MODULE_DESCRIPTION("ALSA SoC Core");
2062MODULE_LICENSE("GPL");