blob: 0bae14145a034b96150b27628b161ecc5cac2035 [file] [log] [blame]
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001/*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7 *
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.
12 *
13 * Revision history
14 * 12th Aug 2005 Initial version.
15 * 25th Oct 2005 Working Codec, Interface and Platform registration.
16 *
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
22 */
23
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>
38
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
53
Liam Girdwooda71a4682006-10-19 20:35:56 +020054#define CODEC_CPU(codec, cpu) ((codec << 4) | cpu)
55
Frank Mandarinodb2a4162006-10-06 18:31:09 +020056static DEFINE_MUTEX(pcm_mutex);
57static DEFINE_MUTEX(io_mutex);
58static struct workqueue_struct *soc_workq;
Frank Mandarinodb2a4162006-10-06 18:31:09 +020059static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
60
61/* supported sample rates */
62/* ATTENTION: these values depend on the definition in pcm.h! */
63static const unsigned int rates[] = {
64 5512, 8000, 11025, 16000, 22050, 32000, 44100,
65 48000, 64000, 88200, 96000, 176400, 192000
66};
67
68/*
69 * This is a timeout to do a DAPM powerdown after a stream is closed().
70 * It can be used to eliminate pops between different playback streams, e.g.
71 * between two audio tracks.
72 */
73static int pmdown_time = 5000;
74module_param(pmdown_time, int, 0);
75MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
76
77#ifdef CONFIG_SND_SOC_AC97_BUS
78/* unregister ac97 codec */
79static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
80{
81 if (codec->ac97->dev.bus)
82 device_unregister(&codec->ac97->dev);
83 return 0;
84}
85
86/* stop no dev release warning */
87static void soc_ac97_device_release(struct device *dev){}
88
89/* register ac97 codec to bus */
90static int soc_ac97_dev_register(struct snd_soc_codec *codec)
91{
92 int err;
93
94 codec->ac97->dev.bus = &ac97_bus_type;
95 codec->ac97->dev.parent = NULL;
96 codec->ac97->dev.release = soc_ac97_device_release;
97
98 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
99 codec->card->number, 0, codec->name);
100 err = device_register(&codec->ac97->dev);
101 if (err < 0) {
102 snd_printk(KERN_ERR "Can't register ac97 bus\n");
103 codec->ac97->dev.bus = NULL;
104 return err;
105 }
106 return 0;
107}
108#endif
109
110static inline const char* get_dai_name(int type)
111{
112 switch(type) {
113 case SND_SOC_DAI_AC97:
114 return "AC97";
115 case SND_SOC_DAI_I2S:
116 return "I2S";
117 case SND_SOC_DAI_PCM:
118 return "PCM";
119 }
120 return NULL;
121}
122
123/* get rate format from rate */
124static inline int soc_get_rate_format(int rate)
125{
126 int i;
127
128 for (i = 0; i < ARRAY_SIZE(rates); i++) {
129 if (rates[i] == rate)
130 return 1 << i;
131 }
132 return 0;
133}
134
135/* gets the audio system mclk/sysclk for the given parameters */
136static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
137 struct snd_soc_clock_info *info)
138{
139 struct snd_soc_device *socdev = rtd->socdev;
140 struct snd_soc_machine *machine = socdev->machine;
141 int i;
142
143 /* find the matching machine config and get it's mclk for the given
144 * sample rate and hardware format */
145 for(i = 0; i < machine->num_links; i++) {
146 if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
147 machine->dai_link[i].config_sysclk)
148 return machine->dai_link[i].config_sysclk(rtd, info);
149 }
150 return 0;
151}
152
153/* changes a bitclk multiplier mask to a divider mask */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200154static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200155 unsigned int pcmfmt, unsigned int chn)
156{
157 int i, j;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200158 u64 bfs_ = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200159 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
160
161 if (size <= 0)
162 return 0;
163
164 /* the minimum bit clock that has enough bandwidth */
165 min = size * rate * chn;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200166 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200167
Liam Girdwooda71a4682006-10-19 20:35:56 +0200168 for (i = 0; i < 64; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200169 if ((bfs >> i) & 0x1) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200170 j = min * (i + 1);
171 bfs_ |= SND_SOC_FSBD(mclk/j);
172 dbgc("rcw --> div support mult %d\n",
173 SND_SOC_FSBD_REAL(1<<i));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200174 }
175 }
176
177 return bfs_;
178}
179
180/* changes a bitclk divider mask to a multiplier mask */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200181static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200182 unsigned int pcmfmt, unsigned int chn)
183{
184 int i, j;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200185 u64 bfs_ = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200186
187 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
188
189 if (size <= 0)
190 return 0;
191
192 /* the minimum bit clock that has enough bandwidth */
193 min = size * rate * chn;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200194 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200195
Liam Girdwooda71a4682006-10-19 20:35:56 +0200196 for (i = 0; i < 64; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200197 if ((bfs >> i) & 0x1) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200198 j = mclk / (i + 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200199 if (j >= min) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200200 bfs_ |= SND_SOC_FSBW(j/min);
201 dbgc("div --> rcw support div %d\n",
202 SND_SOC_FSBW_REAL(1<<i));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200203 }
204 }
205 }
206
207 return bfs_;
208}
209
Liam Girdwooda71a4682006-10-19 20:35:56 +0200210/* changes a constant bitclk to a multiplier mask */
211static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
212 unsigned int pcmfmt, unsigned int chn)
213{
214 unsigned int bfs_ = rate * bfs;
215 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
216
217 if (size <= 0)
218 return 0;
219
220 /* the minimum bit clock that has enough bandwidth */
221 min = size * rate * chn;
222 dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
223
224 if (bfs_ < min)
225 return 0;
226 else {
227 bfs_ = SND_SOC_FSBW(bfs_/min);
228 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
229 return bfs_;
230 }
231}
232
233/* changes a bitclk multiplier mask to a divider mask */
234static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
235 unsigned int pcmfmt, unsigned int chn)
236{
237 unsigned int bfs_ = rate * bfs;
238 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
239
240 if (size <= 0)
241 return 0;
242
243 /* the minimum bit clock that has enough bandwidth */
244 min = size * rate * chn;
245 dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
246
247 if (bfs_ < min)
248 return 0;
249 else {
250 bfs_ = SND_SOC_FSBW(mclk/bfs_);
251 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
252 return bfs_;
253 }
254}
255
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200256/* Matches codec DAI and SoC CPU DAI hardware parameters */
257static int soc_hw_match_params(struct snd_pcm_substream *substream,
258 struct snd_pcm_hw_params *params)
259{
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_soc_dai_mode *codec_dai_mode = NULL;
262 struct snd_soc_dai_mode *cpu_dai_mode = NULL;
263 struct snd_soc_clock_info clk_info;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200264 unsigned int fs, mclk, rate = params_rate(params),
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200265 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
266 u16 fmt = 0;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200267 u64 codec_bfs, cpu_bfs;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200268
269 dbg("asoc: match version %s\n", SND_SOC_VERSION);
270 clk_info.rate = rate;
271 pcmrate = soc_get_rate_format(rate);
272
273 /* try and find a match from the codec and cpu DAI capabilities */
274 for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
275 for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
276 codec_dai_mode = &rtd->codec_dai->caps.mode[j];
277 cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
278
279 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
280 pcmrate)) {
281 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
282 continue;
283 }
284
285 fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
286 if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
287 dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
288 continue;
289 }
290
291 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
292 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
293 j, k);
294 continue;
295 }
296
297 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
298 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
299 continue;
300 }
301
302 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
303 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
304 continue;
305 }
306
307 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
308 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
309 continue;
310 }
311
312 /* todo - still need to add tdm selection */
313 rtd->cpu_dai->dai_runtime.fmt =
314 rtd->codec_dai->dai_runtime.fmt =
315 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
316 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
317 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
318 clk_info.bclk_master =
319 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
320
321 /* make sure the ratio between rate and master
322 * clock is acceptable*/
323 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
324 if (fs == 0) {
325 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
326 continue;
327 }
328 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
329 rtd->codec_dai->dai_runtime.fs = fs;
330
331 /* calculate audio system clocking using slowest clocks possible*/
332 mclk = soc_get_mclk(rtd, &clk_info);
333 if (mclk == 0) {
334 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
335 dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
336 clk_info.bclk_master);
337 continue;
338 }
339
340 /* calculate word size (per channel) and frame size */
341 rtd->codec_dai->dai_runtime.pcmfmt =
342 rtd->cpu_dai->dai_runtime.pcmfmt =
343 1 << params_format(params);
344
345 chn = params_channels(params);
346 /* i2s always has left and right */
347 if (params_channels(params) == 1 &&
348 rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
349 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
350 chn <<= 1;
351
352 /* Calculate bfs - the ratio between bitclock and the sample rate
353 * We must take into consideration the dividers and multipliers
354 * used in the codec and cpu DAI modes. We always choose the
355 * lowest possible clocks to reduce power.
356 */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200357 switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
358 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200359 /* cpu & codec bfs dividers */
360 rtd->cpu_dai->dai_runtime.bfs =
361 rtd->codec_dai->dai_runtime.bfs =
362 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200363 break;
364 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
365 /* normalise bfs codec divider & cpu rcw mult */
366 codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200367 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
368 rtd->cpu_dai->dai_runtime.bfs =
369 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200370 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200371 rtd->codec_dai->dai_runtime.pcmfmt, chn);
372 rtd->codec_dai->dai_runtime.bfs =
373 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200374 break;
375 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
376 /* normalise bfs codec rcw mult & cpu divider */
377 codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200378 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
379 rtd->cpu_dai->dai_runtime.bfs =
380 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200381 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200382 rtd->codec_dai->dai_runtime.pcmfmt, chn);
383 rtd->codec_dai->dai_runtime.bfs =
384 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200385 break;
386 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
387 /* codec & cpu bfs rate rcw multipliers */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200388 rtd->cpu_dai->dai_runtime.bfs =
389 rtd->codec_dai->dai_runtime.bfs =
390 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200391 break;
392 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
393 /* normalise cpu bfs rate const multiplier & codec div */
394 cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
395 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
396 if(codec_dai_mode->bfs & cpu_bfs) {
397 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
398 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
399 } else
400 rtd->cpu_dai->dai_runtime.bfs = 0;
401 break;
402 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
403 /* normalise cpu bfs rate const multiplier & codec rcw mult */
404 cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
405 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
406 if(codec_dai_mode->bfs & cpu_bfs) {
407 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
408 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
409 } else
410 rtd->cpu_dai->dai_runtime.bfs = 0;
411 break;
412 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
413 /* normalise cpu bfs rate rcw multiplier & codec const mult */
414 codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
415 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
416 if(cpu_dai_mode->bfs & codec_bfs) {
417 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
418 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
419 } else
420 rtd->cpu_dai->dai_runtime.bfs = 0;
421 break;
422 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
423 /* normalise cpu bfs div & codec const mult */
424 codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
425 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
Philipp Zabel2e26e482006-11-27 12:05:04 +0100426 if(cpu_dai_mode->bfs & codec_bfs) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200427 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
428 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
429 } else
430 rtd->cpu_dai->dai_runtime.bfs = 0;
431 break;
432 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
433 /* cpu & codec constant mult */
434 if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
435 rtd->cpu_dai->dai_runtime.bfs =
436 rtd->codec_dai->dai_runtime.bfs =
437 codec_dai_mode->bfs;
438 else
439 rtd->cpu_dai->dai_runtime.bfs =
440 rtd->codec_dai->dai_runtime.bfs = 0;
441 break;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200442 }
443
444 /* make sure the bit clock speed is acceptable */
445 if (!rtd->cpu_dai->dai_runtime.bfs ||
446 !rtd->codec_dai->dai_runtime.bfs) {
447 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200448 dbgc("asoc: cpu_dai %llu codec %llu\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200449 rtd->cpu_dai->dai_runtime.bfs,
450 rtd->codec_dai->dai_runtime.bfs);
451 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
452 continue;
453 }
454
455 goto found;
456 }
457 }
458 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
459 return -EINVAL;
460
461found:
462 /* we have matching DAI's, so complete the runtime info */
463 rtd->codec_dai->dai_runtime.pcmrate =
464 rtd->cpu_dai->dai_runtime.pcmrate =
465 soc_get_rate_format(rate);
466
467 rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
468 rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
469 rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
470 rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
471
472 /* for debug atm */
473 dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
474 if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
475 codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
476 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
477 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
478 rtd->codec_dai->dai_runtime.fs, mclk,
479 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200480 } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
481 codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
482 dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200483 rtd->codec_dai->dai_runtime.fs, mclk,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200484 rtd->codec_dai->dai_runtime.bfs, codec_bclk);
485 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
486 codec_bclk = params_rate(params) * params_channels(params) *
487 snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
488 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
489 dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
490 rtd->codec_dai->dai_runtime.fs, mclk,
491 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
492 } else
493 codec_bclk = 0;
494
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200495 if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
496 cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
497 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
498 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
499 rtd->cpu_dai->dai_runtime.fs, mclk,
500 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200501 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
502 cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
503 dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200504 rtd->cpu_dai->dai_runtime.fs, mclk,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200505 rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
506 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
507 cpu_bclk = params_rate(params) * params_channels(params) *
508 snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
509 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
510 dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
511 rtd->cpu_dai->dai_runtime.fs, mclk,
512 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
513 } else
514 cpu_bclk = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200515
516 /*
517 * Check we have matching bitclocks. If we don't then it means the
518 * sysclock returned by either the codec or cpu DAI (selected by the
519 * machine sysclock function) is wrong compared with the supported DAI
520 * modes for the codec or cpu DAI.
521 */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200522 if (cpu_bclk != codec_bclk && cpu_bclk){
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200523 printk(KERN_ERR
524 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
525 );
526 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
527 }
528
529 switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
530 case SND_SOC_DAIFMT_CBM_CFM:
531 dbg("asoc: DAI codec BCLK master, LRC master\n");
532 break;
533 case SND_SOC_DAIFMT_CBS_CFM:
534 dbg("asoc: DAI codec BCLK slave, LRC master\n");
535 break;
536 case SND_SOC_DAIFMT_CBM_CFS:
537 dbg("asoc: DAI codec BCLK master, LRC slave\n");
538 break;
539 case SND_SOC_DAIFMT_CBS_CFS:
540 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
541 break;
542 }
543 dbg("asoc: mode %x, invert %x\n",
544 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
545 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
546 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
547 params_channels(params), params_format(params));
548
549 return 0;
550}
551
552static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
553{
554 int i;
555 u32 rates = 0;
556
557 for(i = 0; i < nmodes; i++)
558 rates |= modes[i].pcmrate;
559
560 return rates;
561}
562
563static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
564{
565 int i;
566 u64 formats = 0;
567
568 for(i = 0; i < nmodes; i++)
569 formats |= modes[i].pcmfmt;
570
571 return formats;
572}
573
574/*
575 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
576 * then initialized and any private data can be allocated. This also calls
577 * startup for the cpu DAI, platform, machine and codec DAI.
578 */
579static int soc_pcm_open(struct snd_pcm_substream *substream)
580{
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct snd_soc_device *socdev = rtd->socdev;
583 struct snd_pcm_runtime *runtime = substream->runtime;
584 struct snd_soc_machine *machine = socdev->machine;
585 struct snd_soc_platform *platform = socdev->platform;
586 struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
587 struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
588 int ret = 0;
589
590 mutex_lock(&pcm_mutex);
591
592 /* startup the audio subsystem */
593 if (rtd->cpu_dai->ops.startup) {
594 ret = rtd->cpu_dai->ops.startup(substream);
595 if (ret < 0) {
596 printk(KERN_ERR "asoc: can't open interface %s\n",
597 rtd->cpu_dai->name);
598 goto out;
599 }
600 }
601
602 if (platform->pcm_ops->open) {
603 ret = platform->pcm_ops->open(substream);
604 if (ret < 0) {
605 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
606 goto platform_err;
607 }
608 }
609
610 if (machine->ops && machine->ops->startup) {
611 ret = machine->ops->startup(substream);
612 if (ret < 0) {
613 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
614 goto machine_err;
615 }
616 }
617
618 if (rtd->codec_dai->ops.startup) {
619 ret = rtd->codec_dai->ops.startup(substream);
620 if (ret < 0) {
621 printk(KERN_ERR "asoc: can't open codec %s\n",
622 rtd->codec_dai->name);
623 goto codec_dai_err;
624 }
625 }
626
627 /* create runtime params from DMA, codec and cpu DAI */
628 if (runtime->hw.rates)
629 runtime->hw.rates &=
630 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
631 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
632 else
633 runtime->hw.rates =
634 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
635 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
636 if (runtime->hw.formats)
637 runtime->hw.formats &=
638 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
639 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
640 else
641 runtime->hw.formats =
642 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
643 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
644
645 /* Check that the codec and cpu DAI's are compatible */
646 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
647 runtime->hw.rate_min =
648 max(rtd->codec_dai->playback.rate_min,
649 rtd->cpu_dai->playback.rate_min);
650 runtime->hw.rate_max =
651 min(rtd->codec_dai->playback.rate_max,
652 rtd->cpu_dai->playback.rate_max);
653 runtime->hw.channels_min =
654 max(rtd->codec_dai->playback.channels_min,
655 rtd->cpu_dai->playback.channels_min);
656 runtime->hw.channels_max =
657 min(rtd->codec_dai->playback.channels_max,
658 rtd->cpu_dai->playback.channels_max);
659 } else {
660 runtime->hw.rate_min =
661 max(rtd->codec_dai->capture.rate_min,
662 rtd->cpu_dai->capture.rate_min);
663 runtime->hw.rate_max =
664 min(rtd->codec_dai->capture.rate_max,
665 rtd->cpu_dai->capture.rate_max);
666 runtime->hw.channels_min =
667 max(rtd->codec_dai->capture.channels_min,
668 rtd->cpu_dai->capture.channels_min);
669 runtime->hw.channels_max =
670 min(rtd->codec_dai->capture.channels_max,
671 rtd->cpu_dai->capture.channels_max);
672 }
673
674 snd_pcm_limit_hw_rates(runtime);
675 if (!runtime->hw.rates) {
676 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
677 rtd->codec_dai->name, rtd->cpu_dai->name);
678 goto codec_dai_err;
679 }
680 if (!runtime->hw.formats) {
681 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
682 rtd->codec_dai->name, rtd->cpu_dai->name);
683 goto codec_dai_err;
684 }
685 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
686 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
687 rtd->codec_dai->name, rtd->cpu_dai->name);
688 goto codec_dai_err;
689 }
690
691 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
Liam Girdwoodb5c5fd22006-10-13 19:13:41 +0200692 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
693 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
694 runtime->hw.channels_max);
695 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
696 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200697
698
699 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
700 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
701 else
702 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
703 rtd->cpu_dai->active = rtd->codec_dai->active = 1;
704 rtd->cpu_dai->runtime = runtime;
705 socdev->codec->active++;
706 mutex_unlock(&pcm_mutex);
707 return 0;
708
709codec_dai_err:
710 if (machine->ops && machine->ops->shutdown)
711 machine->ops->shutdown(substream);
712
713machine_err:
714 if (platform->pcm_ops->close)
715 platform->pcm_ops->close(substream);
716
717platform_err:
718 if (rtd->cpu_dai->ops.shutdown)
719 rtd->cpu_dai->ops.shutdown(substream);
720out:
721 mutex_unlock(&pcm_mutex);
722 return ret;
723}
724
725/*
726 * Power down the audio subsytem pmdown_time msecs after close is called.
727 * This is to ensure there are no pops or clicks in between any music tracks
728 * due to DAPM power cycling.
729 */
Andrew Morton4484bb22006-12-15 09:30:07 +0100730static void close_delayed_work(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731{
Andrew Morton4484bb22006-12-15 09:30:07 +0100732 struct snd_soc_device *socdev =
733 container_of(work, struct snd_soc_device, delayed_work.work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200734 struct snd_soc_codec *codec = socdev->codec;
735 struct snd_soc_codec_dai *codec_dai;
736 int i;
737
738 mutex_lock(&pcm_mutex);
739 for(i = 0; i < codec->num_dai; i++) {
740 codec_dai = &codec->dai[i];
741
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");
746
747 /* are we waiting on this codec DAI stream */
748 if (codec_dai->pop_wait == 1) {
749
750 codec_dai->pop_wait = 0;
751 snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
752 SND_SOC_DAPM_STREAM_STOP);
753
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);
760 }
761 }
762 }
763 mutex_unlock(&pcm_mutex);
764}
765
766/*
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.
770 */
771static int soc_codec_close(struct snd_pcm_substream *substream)
772{
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;
778
779 mutex_lock(&pcm_mutex);
780
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;
785
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;
789 }
790 codec->active--;
791
792 if (rtd->cpu_dai->ops.shutdown)
793 rtd->cpu_dai->ops.shutdown(substream);
794
795 if (rtd->codec_dai->ops.shutdown)
796 rtd->codec_dai->ops.shutdown(substream);
797
798 if (machine->ops && machine->ops->shutdown)
799 machine->ops->shutdown(substream);
800
801 if (platform->pcm_ops->close)
802 platform->pcm_ops->close(substream);
803 rtd->cpu_dai->runtime = NULL;
804
805 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
806 /* start delayed pop wq here for playback streams */
807 rtd->codec_dai->pop_wait = 1;
Andrew Morton4484bb22006-12-15 09:30:07 +0100808 queue_delayed_work(soc_workq, &socdev->delayed_work,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200809 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);
814
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);
818 }
819 }
820
821 mutex_unlock(&pcm_mutex);
822 return 0;
823}
824
825/*
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.
829 */
830static int soc_pcm_prepare(struct snd_pcm_substream *substream)
831{
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;
837
838 mutex_lock(&pcm_mutex);
839 if (platform->pcm_ops->prepare) {
840 ret = platform->pcm_ops->prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200841 if (ret < 0) {
842 printk(KERN_ERR "asoc: platform prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200843 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200844 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845 }
846
847 if (rtd->codec_dai->ops.prepare) {
848 ret = rtd->codec_dai->ops.prepare(substream);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200849 if (ret < 0) {
850 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851 goto out;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200852 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200853 }
854
855 if (rtd->cpu_dai->ops.prepare)
856 ret = rtd->cpu_dai->ops.prepare(substream);
857
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;
Andrew Morton4484bb22006-12-15 09:30:07 +0100868 cancel_delayed_work(&socdev->delayed_work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200869 if (rtd->codec_dai->digital_mute)
870 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
871 }
872 } else {
873 /* no delayed work - do we need to power up codec */
874 if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
875
876 if (codec->dapm_event)
877 codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
878
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);
887
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);
892
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);
905 }
906 }
907
908out:
909 mutex_unlock(&pcm_mutex);
910 return ret;
911}
912
913/*
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.
917 */
918static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
919 struct snd_pcm_hw_params *params)
920{
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;
926
927 mutex_lock(&pcm_mutex);
928
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;
940 }
941
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;
948 }
949 }
950
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;
957 }
958 }
959
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;
966 }
967 }
968
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;
974 }
975 }
976
977out:
978 mutex_unlock(&pcm_mutex);
979 return ret;
980
981machine_err:
982 if (platform->pcm_ops->hw_free)
983 platform->pcm_ops->hw_free(substream);
984
985platform_err:
986 if (rtd->cpu_dai->ops.hw_free)
987 rtd->cpu_dai->ops.hw_free(substream);
988
989interface_err:
990 if (rtd->codec_dai->ops.hw_free)
991 rtd->codec_dai->ops.hw_free(substream);
992
993 mutex_unlock(&pcm_mutex);
994 return ret;
995}
996
997/*
998 * Free's resources allocated by hw_params, can be called multiple times
999 */
1000static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1001{
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;
1007
1008 mutex_lock(&pcm_mutex);
1009
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);
1013
1014 /* free any machine hw params */
1015 if (machine->ops && machine->ops->hw_free)
1016 machine->ops->hw_free(substream);
1017
1018 /* free any DMA resources */
1019 if (platform->pcm_ops->hw_free)
1020 platform->pcm_ops->hw_free(substream);
1021
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);
1025
1026 if (rtd->cpu_dai->ops.hw_free)
1027 rtd->cpu_dai->ops.hw_free(substream);
1028
1029 mutex_unlock(&pcm_mutex);
1030 return 0;
1031}
1032
1033static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1034{
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;
1039
1040 if (rtd->codec_dai->ops.trigger) {
1041 ret = rtd->codec_dai->ops.trigger(substream, cmd);
1042 if (ret < 0)
1043 return ret;
1044 }
1045
1046 if (platform->pcm_ops->trigger) {
1047 ret = platform->pcm_ops->trigger(substream, cmd);
1048 if (ret < 0)
1049 return ret;
1050 }
1051
1052 if (rtd->cpu_dai->ops.trigger) {
1053 ret = rtd->cpu_dai->ops.trigger(substream, cmd);
1054 if (ret < 0)
1055 return ret;
1056 }
1057 return 0;
1058}
1059
1060/* ASoC PCM operations */
1061static 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,
1068};
1069
1070#ifdef CONFIG_PM
1071/* powers down audio subsystem for suspend */
1072static int soc_suspend(struct platform_device *pdev, pm_message_t state)
1073{
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;
1080
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);
1086 }
1087
1088 if (machine->suspend_pre)
1089 machine->suspend_pre(pdev, state);
1090
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);
1097 }
1098
1099 /* close any waiting streams and save state */
1100 flush_workqueue(soc_workq);
1101 codec->suspend_dapm_state = codec->dapm_state;
1102
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);
1112 }
1113
1114 if (codec_dev->suspend)
1115 codec_dev->suspend(pdev, state);
1116
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);
1121 }
1122
1123 if (machine->suspend_post)
1124 machine->suspend_post(pdev, state);
1125
1126 return 0;
1127}
1128
1129/* powers up audio subsystem after a suspend */
1130static int soc_resume(struct platform_device *pdev)
1131{
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;
1138
1139 if (machine->resume_pre)
1140 machine->resume_pre(pdev);
1141
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);
1146 }
1147
1148 if (codec_dev->resume)
1149 codec_dev->resume(pdev);
1150
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);
1160 }
1161
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);
1167 }
1168
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);
1175 }
1176
1177 if (machine->resume_post)
1178 machine->resume_post(pdev);
1179
1180 return 0;
1181}
1182
1183#else
1184#define soc_suspend NULL
1185#define soc_resume NULL
1186#endif
1187
1188/* probes a new socdev */
1189static int soc_probe(struct platform_device *pdev)
1190{
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;
1196
1197 if (machine->probe) {
1198 ret = machine->probe(pdev);
1199 if(ret < 0)
1200 return ret;
1201 }
1202
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;
1209 }
1210 }
1211
1212 if (codec_dev->probe) {
1213 ret = codec_dev->probe(pdev);
1214 if(ret < 0)
1215 goto cpu_dai_err;
1216 }
1217
1218 if (platform->probe) {
1219 ret = platform->probe(pdev);
1220 if(ret < 0)
1221 goto platform_err;
1222 }
1223
1224 /* DAPM stream work */
1225 soc_workq = create_workqueue("kdapm");
1226 if (soc_workq == NULL)
1227 goto work_err;
Andrew Morton4484bb22006-12-15 09:30:07 +01001228 INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001229 return 0;
1230
1231work_err:
1232 if (platform->remove)
1233 platform->remove(pdev);
1234
1235platform_err:
1236 if (codec_dev->remove)
1237 codec_dev->remove(pdev);
1238
1239cpu_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);
1244 }
1245
1246 if (machine->remove)
1247 machine->remove(pdev);
1248
1249 return ret;
1250}
1251
1252/* removes a socdev */
1253static int soc_remove(struct platform_device *pdev)
1254{
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;
1260
1261 if (soc_workq)
1262 destroy_workqueue(soc_workq);
1263
1264 if (platform->remove)
1265 platform->remove(pdev);
1266
1267 if (codec_dev->remove)
1268 codec_dev->remove(pdev);
1269
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);
1274 }
1275
1276 if (machine->remove)
1277 machine->remove(pdev);
1278
1279 return 0;
1280}
1281
1282/* ASoC platform driver */
1283static struct platform_driver soc_driver = {
1284 .driver = {
1285 .name = "soc-audio",
1286 },
1287 .probe = soc_probe,
1288 .remove = soc_remove,
1289 .suspend = soc_suspend,
1290 .resume = soc_resume,
1291};
1292
1293/* create a new pcm */
1294static int soc_new_pcm(struct snd_soc_device *socdev,
1295 struct snd_soc_dai_link *dai_link, int num)
1296{
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;
1304
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;
1311
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);
1315
1316 if (codec_dai->playback.channels_min)
1317 playback = 1;
1318 if (codec_dai->capture.channels_min)
1319 capture = 1;
1320
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;
1327 }
1328
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;
1337
1338 if (playback)
1339 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1340
1341 if (capture)
1342 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1343
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;
1349 }
1350
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;
1355}
1356
1357/* codec register dump */
1358static ssize_t codec_reg_show(struct device *dev,
1359 struct device_attribute *attr, char *buf)
1360{
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;
1364
1365 if (!codec->reg_cache_size)
1366 return 0;
1367
1368 if (codec->reg_cache_step)
1369 step = codec->reg_cache_step;
1370
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));
1374
1375 return count;
1376}
1377static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
1378
1379/**
1380 * snd_soc_new_ac97_codec - initailise AC97 device
1381 * @codec: audio codec
1382 * @ops: AC97 bus operations
1383 * @num: AC97 codec number
1384 *
1385 * Initialises AC97 codec resources for use by ad-hoc devices only.
1386 */
1387int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1388 struct snd_ac97_bus_ops *ops, int num)
1389{
1390 mutex_lock(&codec->mutex);
1391
1392 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1393 if (codec->ac97 == NULL) {
1394 mutex_unlock(&codec->mutex);
1395 return -ENOMEM;
1396 }
1397
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;
1404 }
1405
1406 codec->ac97->bus->ops = ops;
1407 codec->ac97->num = num;
1408 mutex_unlock(&codec->mutex);
1409 return 0;
1410}
1411EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1412
1413/**
1414 * snd_soc_free_ac97_codec - free AC97 codec device
1415 * @codec: audio codec
1416 *
1417 * Frees AC97 codec device resources.
1418 */
1419void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1420{
1421 mutex_lock(&codec->mutex);
1422 kfree(codec->ac97->bus);
1423 kfree(codec->ac97);
1424 codec->ac97 = NULL;
1425 mutex_unlock(&codec->mutex);
1426}
1427EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1428
1429/**
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
1435 *
1436 * Writes new register value.
1437 *
1438 * Returns 1 for change else 0.
1439 */
1440int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1441 unsigned short mask, unsigned short value)
1442{
1443 int change;
1444 unsigned short old, new;
1445
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);
1452
1453 mutex_unlock(&io_mutex);
1454 return change;
1455}
1456EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1457
1458/**
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
1464 *
1465 * Tests a register with a new value and checks if the new value is
1466 * different from the old value.
1467 *
1468 * Returns 1 for change else 0.
1469 */
1470int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1471 unsigned short mask, unsigned short value)
1472{
1473 int change;
1474 unsigned short old, new;
1475
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);
1481
1482 return change;
1483}
1484EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1485
1486/**
1487 * snd_soc_get_rate - get int sample rate
1488 * @hwpcmrate: the hardware pcm rate
1489 *
1490 * Returns the audio rate integaer value, else 0.
1491 */
1492int snd_soc_get_rate(int hwpcmrate)
1493{
1494 int rate = ffs(hwpcmrate) - 1;
1495
1496 if (rate > ARRAY_SIZE(rates))
1497 return 0;
1498 return rates[rate];
1499}
1500EXPORT_SYMBOL_GPL(snd_soc_get_rate);
1501
1502/**
1503 * snd_soc_new_pcms - create new sound card and pcms
1504 * @socdev: the SoC audio device
1505 *
1506 * Create a new sound card based upon the codec and interface pcms.
1507 *
1508 * Returns 0 for success, else error.
1509 */
1510int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
1511{
1512 struct snd_soc_codec *codec = socdev->codec;
1513 struct snd_soc_machine *machine = socdev->machine;
1514 int ret = 0, i;
1515
1516 mutex_lock(&codec->mutex);
1517
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;
1525 }
1526
1527 codec->card->dev = socdev->dev;
1528 codec->card->private_data = codec;
1529 strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
1530
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;
1539 }
1540 }
1541
1542 mutex_unlock(&codec->mutex);
1543 return ret;
1544}
1545EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
1546
1547/**
1548 * snd_soc_register_card - register sound card
1549 * @socdev: the SoC audio device
1550 *
1551 * Register a SoC sound card. Also registers an AC97 device if the
1552 * codec is AC97 for ad hoc devices.
1553 *
1554 * Returns 0 for success, else error.
1555 */
1556int snd_soc_register_card(struct snd_soc_device *socdev)
1557{
1558 struct snd_soc_codec *codec = socdev->codec;
1559 struct snd_soc_machine *machine = socdev->machine;
Liam Girdwood12e74f72006-10-16 21:19:48 +02001560 int ret = 0, i, ac97 = 0, err = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001561
1562 mutex_lock(&codec->mutex);
1563 for(i = 0; i < machine->num_links; i++) {
Liam Girdwood12e74f72006-10-16 21:19:48 +02001564 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;
1570 }
1571 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001572 if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
1573 ac97 = 1;
1574 }
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);
1579
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);
Liam Girdwood12e74f72006-10-16 21:19:48 +02001584 goto out;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001585 }
1586
1587#ifdef CONFIG_SND_SOC_AC97_BUS
Liam Girdwood12e74f72006-10-16 21:19:48 +02001588 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;
1594 }
1595 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001596#endif
1597
Liam Girdwood12e74f72006-10-16 21:19:48 +02001598 err = snd_soc_dapm_sys_add(socdev->dev);
1599 if (err < 0)
1600 printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
1601
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");
1605out:
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001606 mutex_unlock(&codec->mutex);
1607 return ret;
1608}
1609EXPORT_SYMBOL_GPL(snd_soc_register_card);
1610
1611/**
1612 * snd_soc_free_pcms - free sound card and pcms
1613 * @socdev: the SoC audio device
1614 *
1615 * Frees sound card and pcms associated with the socdev.
1616 * Also unregister the codec if it is an AC97 device.
1617 */
1618void snd_soc_free_pcms(struct snd_soc_device *socdev)
1619{
1620 struct snd_soc_codec *codec = socdev->codec;
1621
1622 mutex_lock(&codec->mutex);
1623#ifdef CONFIG_SND_SOC_AC97_BUS
1624 if (codec->ac97)
1625 soc_ac97_dev_unregister(codec);
1626#endif
1627
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);
1632}
1633EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
1634
1635/**
1636 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1637 * @substream: the pcm substream
1638 * @hw: the hardware parameters
1639 *
1640 * Sets the substream runtime hardware parameters.
1641 */
1642int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1643 const struct snd_pcm_hardware *hw)
1644{
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;
1655}
1656EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1657
1658/**
1659 * snd_soc_cnew - create new control
1660 * @_template: control template
1661 * @data: control private data
1662 * @lnng_name: control long name
1663 *
1664 * Create a new mixer control from a template control.
1665 *
1666 * Returns 0 for success, else error.
1667 */
1668struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1669 void *data, char *long_name)
1670{
1671 struct snd_kcontrol_new template;
1672
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;
1678
1679 return snd_ctl_new1(&template, data);
1680}
1681EXPORT_SYMBOL_GPL(snd_soc_cnew);
1682
1683/**
1684 * snd_soc_info_enum_double - enumerated double mixer info callback
1685 * @kcontrol: mixer control
1686 * @uinfo: control element information
1687 *
1688 * Callback to provide information about a double enumerated
1689 * mixer control.
1690 *
1691 * Returns 0 for success.
1692 */
1693int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1694 struct snd_ctl_elem_info *uinfo)
1695{
1696 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1697
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;
1701
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;
1707}
1708EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1709
1710/**
1711 * snd_soc_get_enum_double - enumerated double mixer get callback
1712 * @kcontrol: mixer control
1713 * @uinfo: control element information
1714 *
1715 * Callback to get the value of a double enumerated mixer.
1716 *
1717 * Returns 0 for success.
1718 */
1719int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol)
1721{
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;
1725
1726 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1727 ;
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);
1733
1734 return 0;
1735}
1736EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1737
1738/**
1739 * snd_soc_put_enum_double - enumerated double mixer put callback
1740 * @kcontrol: mixer control
1741 * @uinfo: control element information
1742 *
1743 * Callback to set the value of a double enumerated mixer.
1744 *
1745 * Returns 0 for success.
1746 */
1747int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol)
1749{
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;
1754
1755 for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
1756 ;
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;
1766 }
1767
1768 return snd_soc_update_bits(codec, e->reg, mask, val);
1769}
1770EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
1771
1772/**
1773 * snd_soc_info_enum_ext - external enumerated single mixer info callback
1774 * @kcontrol: mixer control
1775 * @uinfo: control element information
1776 *
1777 * Callback to provide information about an external enumerated
1778 * single mixer.
1779 *
1780 * Returns 0 for success.
1781 */
1782int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_info *uinfo)
1784{
1785 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1786
1787 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1788 uinfo->count = 1;
1789 uinfo->value.enumerated.items = e->mask;
1790
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;
1796}
1797EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
1798
1799/**
1800 * snd_soc_info_volsw_ext - external single mixer info callback
1801 * @kcontrol: mixer control
1802 * @uinfo: control element information
1803 *
1804 * Callback to provide information about a single external mixer control.
1805 *
1806 * Returns 0 for success.
1807 */
1808int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_info *uinfo)
1810{
1811 int mask = kcontrol->private_value;
1812
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;
1819}
1820EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
1821
1822/**
1823 * snd_soc_info_bool_ext - external single boolean mixer info callback
1824 * @kcontrol: mixer control
1825 * @uinfo: control element information
1826 *
1827 * Callback to provide information about a single boolean external mixer control.
1828 *
1829 * Returns 0 for success.
1830 */
1831int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
1832 struct snd_ctl_elem_info *uinfo)
1833{
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;
1839}
1840EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
1841
1842/**
1843 * snd_soc_info_volsw - single mixer info callback
1844 * @kcontrol: mixer control
1845 * @uinfo: control element information
1846 *
1847 * Callback to provide information about a single mixer control.
1848 *
1849 * Returns 0 for success.
1850 */
1851int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
1852 struct snd_ctl_elem_info *uinfo)
1853{
1854 int mask = (kcontrol->private_value >> 16) & 0xff;
1855 int shift = (kcontrol->private_value >> 8) & 0x0f;
1856 int rshift = (kcontrol->private_value >> 12) & 0x0f;
1857
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;
1864}
1865EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
1866
1867/**
1868 * snd_soc_get_volsw - single mixer get callback
1869 * @kcontrol: mixer control
1870 * @uinfo: control element information
1871 *
1872 * Callback to get the value of a single mixer control.
1873 *
1874 * Returns 0 for success.
1875 */
1876int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
1878{
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;
1885
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];
1897 }
1898
1899 return 0;
1900}
1901EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
1902
1903/**
1904 * snd_soc_put_volsw - single mixer put callback
1905 * @kcontrol: mixer control
1906 * @uinfo: control element information
1907 *
1908 * Callback to set the value of a single mixer control.
1909 *
1910 * Returns 0 for success.
1911 */
1912int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
1913 struct snd_ctl_elem_value *ucontrol)
1914{
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;
1923
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;
1935 }
1936 err = snd_soc_update_bits(codec, reg, val_mask, val);
1937 return err;
1938}
1939EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
1940
1941/**
1942 * snd_soc_info_volsw_2r - double mixer info callback
1943 * @kcontrol: mixer control
1944 * @uinfo: control element information
1945 *
1946 * Callback to provide information about a double mixer control that
1947 * spans 2 codec registers.
1948 *
1949 * Returns 0 for success.
1950 */
1951int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_info *uinfo)
1953{
1954 int mask = (kcontrol->private_value >> 12) & 0xff;
1955
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;
1962}
1963EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
1964
1965/**
1966 * snd_soc_get_volsw_2r - double mixer get callback
1967 * @kcontrol: mixer control
1968 * @uinfo: control element information
1969 *
1970 * Callback to get the value of a double mixer control that spans 2 registers.
1971 *
1972 * Returns 0 for success.
1973 */
1974int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
1975 struct snd_ctl_elem_value *ucontrol)
1976{
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;
1983
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];
1993 }
1994
1995 return 0;
1996}
1997EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
1998
1999/**
2000 * snd_soc_put_volsw_2r - double mixer set callback
2001 * @kcontrol: mixer control
2002 * @uinfo: control element information
2003 *
2004 * Callback to set the value of a double mixer control that spans 2 registers.
2005 *
2006 * Returns 0 for success.
2007 */
2008int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2009 struct snd_ctl_elem_value *ucontrol)
2010{
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;
2019
2020 val_mask = mask << shift;
2021 val = (ucontrol->value.integer.value[0] & mask);
2022 val2 = (ucontrol->value.integer.value[1] & mask);
2023
2024 if (invert) {
2025 val = mask - val;
2026 val2 = mask - val2;
2027 }
2028
2029 val = val << shift;
2030 val2 = val2 << shift;
2031
2032 if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
2033 return err;
2034
2035 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
2036 return err;
2037}
2038EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2039
2040static int __devinit snd_soc_init(void)
2041{
2042 printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
2043 return platform_driver_register(&soc_driver);
2044}
2045
2046static void snd_soc_exit(void)
2047{
2048 platform_driver_unregister(&soc_driver);
2049}
2050
2051module_init(snd_soc_init);
2052module_exit(snd_soc_exit);
2053
2054/* Module information */
2055MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
2056MODULE_DESCRIPTION("ALSA SoC Core");
2057MODULE_LICENSE("GPL");