blob: 6da1616bf776d0abb21760ba69d142d426578fe5 [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;
59static struct work_struct soc_stream_work;
60static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
61
62/* supported sample rates */
63/* ATTENTION: these values depend on the definition in pcm.h! */
64static const unsigned int rates[] = {
65 5512, 8000, 11025, 16000, 22050, 32000, 44100,
66 48000, 64000, 88200, 96000, 176400, 192000
67};
68
69/*
70 * This is a timeout to do a DAPM powerdown after a stream is closed().
71 * It can be used to eliminate pops between different playback streams, e.g.
72 * between two audio tracks.
73 */
74static int pmdown_time = 5000;
75module_param(pmdown_time, int, 0);
76MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
77
78#ifdef CONFIG_SND_SOC_AC97_BUS
79/* unregister ac97 codec */
80static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
81{
82 if (codec->ac97->dev.bus)
83 device_unregister(&codec->ac97->dev);
84 return 0;
85}
86
87/* stop no dev release warning */
88static void soc_ac97_device_release(struct device *dev){}
89
90/* register ac97 codec to bus */
91static int soc_ac97_dev_register(struct snd_soc_codec *codec)
92{
93 int err;
94
95 codec->ac97->dev.bus = &ac97_bus_type;
96 codec->ac97->dev.parent = NULL;
97 codec->ac97->dev.release = soc_ac97_device_release;
98
99 snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
100 codec->card->number, 0, codec->name);
101 err = device_register(&codec->ac97->dev);
102 if (err < 0) {
103 snd_printk(KERN_ERR "Can't register ac97 bus\n");
104 codec->ac97->dev.bus = NULL;
105 return err;
106 }
107 return 0;
108}
109#endif
110
111static inline const char* get_dai_name(int type)
112{
113 switch(type) {
114 case SND_SOC_DAI_AC97:
115 return "AC97";
116 case SND_SOC_DAI_I2S:
117 return "I2S";
118 case SND_SOC_DAI_PCM:
119 return "PCM";
120 }
121 return NULL;
122}
123
124/* get rate format from rate */
125static inline int soc_get_rate_format(int rate)
126{
127 int i;
128
129 for (i = 0; i < ARRAY_SIZE(rates); i++) {
130 if (rates[i] == rate)
131 return 1 << i;
132 }
133 return 0;
134}
135
136/* gets the audio system mclk/sysclk for the given parameters */
137static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
138 struct snd_soc_clock_info *info)
139{
140 struct snd_soc_device *socdev = rtd->socdev;
141 struct snd_soc_machine *machine = socdev->machine;
142 int i;
143
144 /* find the matching machine config and get it's mclk for the given
145 * sample rate and hardware format */
146 for(i = 0; i < machine->num_links; i++) {
147 if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
148 machine->dai_link[i].config_sysclk)
149 return machine->dai_link[i].config_sysclk(rtd, info);
150 }
151 return 0;
152}
153
154/* changes a bitclk multiplier mask to a divider mask */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200155static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200156 unsigned int pcmfmt, unsigned int chn)
157{
158 int i, j;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200159 u64 bfs_ = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200160 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
161
162 if (size <= 0)
163 return 0;
164
165 /* the minimum bit clock that has enough bandwidth */
166 min = size * rate * chn;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200167 dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200168
Liam Girdwooda71a4682006-10-19 20:35:56 +0200169 for (i = 0; i < 64; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200170 if ((bfs >> i) & 0x1) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200171 j = min * (i + 1);
172 bfs_ |= SND_SOC_FSBD(mclk/j);
173 dbgc("rcw --> div support mult %d\n",
174 SND_SOC_FSBD_REAL(1<<i));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200175 }
176 }
177
178 return bfs_;
179}
180
181/* changes a bitclk divider mask to a multiplier mask */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200182static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200183 unsigned int pcmfmt, unsigned int chn)
184{
185 int i, j;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200186 u64 bfs_ = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200187
188 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
189
190 if (size <= 0)
191 return 0;
192
193 /* the minimum bit clock that has enough bandwidth */
194 min = size * rate * chn;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200195 dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200196
Liam Girdwooda71a4682006-10-19 20:35:56 +0200197 for (i = 0; i < 64; i++) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200198 if ((bfs >> i) & 0x1) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200199 j = mclk / (i + 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200200 if (j >= min) {
Liam Girdwooda71a4682006-10-19 20:35:56 +0200201 bfs_ |= SND_SOC_FSBW(j/min);
202 dbgc("div --> rcw support div %d\n",
203 SND_SOC_FSBW_REAL(1<<i));
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200204 }
205 }
206 }
207
208 return bfs_;
209}
210
Liam Girdwooda71a4682006-10-19 20:35:56 +0200211/* changes a constant bitclk to a multiplier mask */
212static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
213 unsigned int pcmfmt, unsigned int chn)
214{
215 unsigned int bfs_ = rate * bfs;
216 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
217
218 if (size <= 0)
219 return 0;
220
221 /* the minimum bit clock that has enough bandwidth */
222 min = size * rate * chn;
223 dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
224
225 if (bfs_ < min)
226 return 0;
227 else {
228 bfs_ = SND_SOC_FSBW(bfs_/min);
229 dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
230 return bfs_;
231 }
232}
233
234/* changes a bitclk multiplier mask to a divider mask */
235static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
236 unsigned int pcmfmt, unsigned int chn)
237{
238 unsigned int bfs_ = rate * bfs;
239 int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
240
241 if (size <= 0)
242 return 0;
243
244 /* the minimum bit clock that has enough bandwidth */
245 min = size * rate * chn;
246 dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
247
248 if (bfs_ < min)
249 return 0;
250 else {
251 bfs_ = SND_SOC_FSBW(mclk/bfs_);
252 dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
253 return bfs_;
254 }
255}
256
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200257/* Matches codec DAI and SoC CPU DAI hardware parameters */
258static int soc_hw_match_params(struct snd_pcm_substream *substream,
259 struct snd_pcm_hw_params *params)
260{
261 struct snd_soc_pcm_runtime *rtd = substream->private_data;
262 struct snd_soc_dai_mode *codec_dai_mode = NULL;
263 struct snd_soc_dai_mode *cpu_dai_mode = NULL;
264 struct snd_soc_clock_info clk_info;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200265 unsigned int fs, mclk, rate = params_rate(params),
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200266 chn, j, k, cpu_bclk, codec_bclk, pcmrate;
267 u16 fmt = 0;
Liam Girdwooda71a4682006-10-19 20:35:56 +0200268 u64 codec_bfs, cpu_bfs;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200269
270 dbg("asoc: match version %s\n", SND_SOC_VERSION);
271 clk_info.rate = rate;
272 pcmrate = soc_get_rate_format(rate);
273
274 /* try and find a match from the codec and cpu DAI capabilities */
275 for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
276 for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
277 codec_dai_mode = &rtd->codec_dai->caps.mode[j];
278 cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
279
280 if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
281 pcmrate)) {
282 dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
283 continue;
284 }
285
286 fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
287 if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
288 dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
289 continue;
290 }
291
292 if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
293 dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
294 j, k);
295 continue;
296 }
297
298 if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
299 dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
300 continue;
301 }
302
303 if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
304 dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
305 continue;
306 }
307
308 if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
309 dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
310 continue;
311 }
312
313 /* todo - still need to add tdm selection */
314 rtd->cpu_dai->dai_runtime.fmt =
315 rtd->codec_dai->dai_runtime.fmt =
316 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
317 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
318 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
319 clk_info.bclk_master =
320 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
321
322 /* make sure the ratio between rate and master
323 * clock is acceptable*/
324 fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
325 if (fs == 0) {
326 dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
327 continue;
328 }
329 clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
330 rtd->codec_dai->dai_runtime.fs = fs;
331
332 /* calculate audio system clocking using slowest clocks possible*/
333 mclk = soc_get_mclk(rtd, &clk_info);
334 if (mclk == 0) {
335 dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
336 dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
337 clk_info.bclk_master);
338 continue;
339 }
340
341 /* calculate word size (per channel) and frame size */
342 rtd->codec_dai->dai_runtime.pcmfmt =
343 rtd->cpu_dai->dai_runtime.pcmfmt =
344 1 << params_format(params);
345
346 chn = params_channels(params);
347 /* i2s always has left and right */
348 if (params_channels(params) == 1 &&
349 rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
350 SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
351 chn <<= 1;
352
353 /* Calculate bfs - the ratio between bitclock and the sample rate
354 * We must take into consideration the dividers and multipliers
355 * used in the codec and cpu DAI modes. We always choose the
356 * lowest possible clocks to reduce power.
357 */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200358 switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
359 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200360 /* cpu & codec bfs dividers */
361 rtd->cpu_dai->dai_runtime.bfs =
362 rtd->codec_dai->dai_runtime.bfs =
363 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200364 break;
365 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
366 /* normalise bfs codec divider & cpu rcw mult */
367 codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200368 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
369 rtd->cpu_dai->dai_runtime.bfs =
370 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200371 cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200372 rtd->codec_dai->dai_runtime.pcmfmt, chn);
373 rtd->codec_dai->dai_runtime.bfs =
374 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200375 break;
376 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
377 /* normalise bfs codec rcw mult & cpu divider */
378 codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200379 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
380 rtd->cpu_dai->dai_runtime.bfs =
381 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200382 cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200383 rtd->codec_dai->dai_runtime.pcmfmt, chn);
384 rtd->codec_dai->dai_runtime.bfs =
385 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200386 break;
387 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
388 /* codec & cpu bfs rate rcw multipliers */
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200389 rtd->cpu_dai->dai_runtime.bfs =
390 rtd->codec_dai->dai_runtime.bfs =
391 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200392 break;
393 case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
394 /* normalise cpu bfs rate const multiplier & codec div */
395 cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
396 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
397 if(codec_dai_mode->bfs & cpu_bfs) {
398 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
399 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
400 } else
401 rtd->cpu_dai->dai_runtime.bfs = 0;
402 break;
403 case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
404 /* normalise cpu bfs rate const multiplier & codec rcw mult */
405 cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
406 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
407 if(codec_dai_mode->bfs & cpu_bfs) {
408 rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
409 rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
410 } else
411 rtd->cpu_dai->dai_runtime.bfs = 0;
412 break;
413 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
414 /* normalise cpu bfs rate rcw multiplier & codec const mult */
415 codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
416 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
417 if(cpu_dai_mode->bfs & codec_bfs) {
418 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
419 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
420 } else
421 rtd->cpu_dai->dai_runtime.bfs = 0;
422 break;
423 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
424 /* normalise cpu bfs div & codec const mult */
425 codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
426 mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
427 if(codec_dai_mode->bfs & codec_bfs) {
428 rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
429 rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
430 } else
431 rtd->cpu_dai->dai_runtime.bfs = 0;
432 break;
433 case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
434 /* cpu & codec constant mult */
435 if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
436 rtd->cpu_dai->dai_runtime.bfs =
437 rtd->codec_dai->dai_runtime.bfs =
438 codec_dai_mode->bfs;
439 else
440 rtd->cpu_dai->dai_runtime.bfs =
441 rtd->codec_dai->dai_runtime.bfs = 0;
442 break;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200443 }
444
445 /* make sure the bit clock speed is acceptable */
446 if (!rtd->cpu_dai->dai_runtime.bfs ||
447 !rtd->codec_dai->dai_runtime.bfs) {
448 dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200449 dbgc("asoc: cpu_dai %llu codec %llu\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200450 rtd->cpu_dai->dai_runtime.bfs,
451 rtd->codec_dai->dai_runtime.bfs);
452 dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
453 continue;
454 }
455
456 goto found;
457 }
458 }
459 printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
460 return -EINVAL;
461
462found:
463 /* we have matching DAI's, so complete the runtime info */
464 rtd->codec_dai->dai_runtime.pcmrate =
465 rtd->cpu_dai->dai_runtime.pcmrate =
466 soc_get_rate_format(rate);
467
468 rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
469 rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
470 rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
471 rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
472
473 /* for debug atm */
474 dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
475 if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
476 codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
477 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
478 dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
479 rtd->codec_dai->dai_runtime.fs, mclk,
480 SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200481 } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
482 codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
483 dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200484 rtd->codec_dai->dai_runtime.fs, mclk,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200485 rtd->codec_dai->dai_runtime.bfs, codec_bclk);
486 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
487 codec_bclk = params_rate(params) * params_channels(params) *
488 snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
489 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
490 dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
491 rtd->codec_dai->dai_runtime.fs, mclk,
492 SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
493 } else
494 codec_bclk = 0;
495
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200496 if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
497 cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
498 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
499 dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
500 rtd->cpu_dai->dai_runtime.fs, mclk,
501 SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
Liam Girdwooda71a4682006-10-19 20:35:56 +0200502 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
503 cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
504 dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200505 rtd->cpu_dai->dai_runtime.fs, mclk,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200506 rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
507 } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
508 cpu_bclk = params_rate(params) * params_channels(params) *
509 snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
510 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
511 dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
512 rtd->cpu_dai->dai_runtime.fs, mclk,
513 SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
514 } else
515 cpu_bclk = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516
517 /*
518 * Check we have matching bitclocks. If we don't then it means the
519 * sysclock returned by either the codec or cpu DAI (selected by the
520 * machine sysclock function) is wrong compared with the supported DAI
521 * modes for the codec or cpu DAI.
522 */
Liam Girdwooda71a4682006-10-19 20:35:56 +0200523 if (cpu_bclk != codec_bclk && cpu_bclk){
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200524 printk(KERN_ERR
525 "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
526 );
527 printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
528 }
529
530 switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
531 case SND_SOC_DAIFMT_CBM_CFM:
532 dbg("asoc: DAI codec BCLK master, LRC master\n");
533 break;
534 case SND_SOC_DAIFMT_CBS_CFM:
535 dbg("asoc: DAI codec BCLK slave, LRC master\n");
536 break;
537 case SND_SOC_DAIFMT_CBM_CFS:
538 dbg("asoc: DAI codec BCLK master, LRC slave\n");
539 break;
540 case SND_SOC_DAIFMT_CBS_CFS:
541 dbg("asoc: DAI codec BCLK slave, LRC slave\n");
542 break;
543 }
544 dbg("asoc: mode %x, invert %x\n",
545 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
546 rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
547 dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
548 params_channels(params), params_format(params));
549
550 return 0;
551}
552
553static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
554{
555 int i;
556 u32 rates = 0;
557
558 for(i = 0; i < nmodes; i++)
559 rates |= modes[i].pcmrate;
560
561 return rates;
562}
563
564static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
565{
566 int i;
567 u64 formats = 0;
568
569 for(i = 0; i < nmodes; i++)
570 formats |= modes[i].pcmfmt;
571
572 return formats;
573}
574
575/*
576 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
577 * then initialized and any private data can be allocated. This also calls
578 * startup for the cpu DAI, platform, machine and codec DAI.
579 */
580static int soc_pcm_open(struct snd_pcm_substream *substream)
581{
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct snd_soc_device *socdev = rtd->socdev;
584 struct snd_pcm_runtime *runtime = substream->runtime;
585 struct snd_soc_machine *machine = socdev->machine;
586 struct snd_soc_platform *platform = socdev->platform;
587 struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
588 struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
589 int ret = 0;
590
591 mutex_lock(&pcm_mutex);
592
593 /* startup the audio subsystem */
594 if (rtd->cpu_dai->ops.startup) {
595 ret = rtd->cpu_dai->ops.startup(substream);
596 if (ret < 0) {
597 printk(KERN_ERR "asoc: can't open interface %s\n",
598 rtd->cpu_dai->name);
599 goto out;
600 }
601 }
602
603 if (platform->pcm_ops->open) {
604 ret = platform->pcm_ops->open(substream);
605 if (ret < 0) {
606 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
607 goto platform_err;
608 }
609 }
610
611 if (machine->ops && machine->ops->startup) {
612 ret = machine->ops->startup(substream);
613 if (ret < 0) {
614 printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
615 goto machine_err;
616 }
617 }
618
619 if (rtd->codec_dai->ops.startup) {
620 ret = rtd->codec_dai->ops.startup(substream);
621 if (ret < 0) {
622 printk(KERN_ERR "asoc: can't open codec %s\n",
623 rtd->codec_dai->name);
624 goto codec_dai_err;
625 }
626 }
627
628 /* create runtime params from DMA, codec and cpu DAI */
629 if (runtime->hw.rates)
630 runtime->hw.rates &=
631 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
632 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
633 else
634 runtime->hw.rates =
635 get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
636 get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
637 if (runtime->hw.formats)
638 runtime->hw.formats &=
639 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
640 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
641 else
642 runtime->hw.formats =
643 get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
644 get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
645
646 /* Check that the codec and cpu DAI's are compatible */
647 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
648 runtime->hw.rate_min =
649 max(rtd->codec_dai->playback.rate_min,
650 rtd->cpu_dai->playback.rate_min);
651 runtime->hw.rate_max =
652 min(rtd->codec_dai->playback.rate_max,
653 rtd->cpu_dai->playback.rate_max);
654 runtime->hw.channels_min =
655 max(rtd->codec_dai->playback.channels_min,
656 rtd->cpu_dai->playback.channels_min);
657 runtime->hw.channels_max =
658 min(rtd->codec_dai->playback.channels_max,
659 rtd->cpu_dai->playback.channels_max);
660 } else {
661 runtime->hw.rate_min =
662 max(rtd->codec_dai->capture.rate_min,
663 rtd->cpu_dai->capture.rate_min);
664 runtime->hw.rate_max =
665 min(rtd->codec_dai->capture.rate_max,
666 rtd->cpu_dai->capture.rate_max);
667 runtime->hw.channels_min =
668 max(rtd->codec_dai->capture.channels_min,
669 rtd->cpu_dai->capture.channels_min);
670 runtime->hw.channels_max =
671 min(rtd->codec_dai->capture.channels_max,
672 rtd->cpu_dai->capture.channels_max);
673 }
674
675 snd_pcm_limit_hw_rates(runtime);
676 if (!runtime->hw.rates) {
677 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
678 rtd->codec_dai->name, rtd->cpu_dai->name);
679 goto codec_dai_err;
680 }
681 if (!runtime->hw.formats) {
682 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
683 rtd->codec_dai->name, rtd->cpu_dai->name);
684 goto codec_dai_err;
685 }
686 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
687 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
688 rtd->codec_dai->name, rtd->cpu_dai->name);
689 goto codec_dai_err;
690 }
691
692 dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
Liam Girdwoodb5c5fd22006-10-13 19:13:41 +0200693 dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
694 dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
695 runtime->hw.channels_max);
696 dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
697 runtime->hw.rate_max);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698
699
700 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
701 rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
702 else
703 rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
704 rtd->cpu_dai->active = rtd->codec_dai->active = 1;
705 rtd->cpu_dai->runtime = runtime;
706 socdev->codec->active++;
707 mutex_unlock(&pcm_mutex);
708 return 0;
709
710codec_dai_err:
711 if (machine->ops && machine->ops->shutdown)
712 machine->ops->shutdown(substream);
713
714machine_err:
715 if (platform->pcm_ops->close)
716 platform->pcm_ops->close(substream);
717
718platform_err:
719 if (rtd->cpu_dai->ops.shutdown)
720 rtd->cpu_dai->ops.shutdown(substream);
721out:
722 mutex_unlock(&pcm_mutex);
723 return ret;
724}
725
726/*
727 * Power down the audio subsytem pmdown_time msecs after close is called.
728 * This is to ensure there are no pops or clicks in between any music tracks
729 * due to DAPM power cycling.
730 */
731static void close_delayed_work(void *data)
732{
733 struct snd_soc_device *socdev = data;
734 struct snd_soc_codec *codec = socdev->codec;
735 struct snd_soc_codec_dai *codec_dai;
736 int i;
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;
808 queue_delayed_work(soc_workq, &soc_stream_work,
809 msecs_to_jiffies(pmdown_time));
810 } else {
811 /* capture streams can be powered down now */
812 snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
813 SND_SOC_DAPM_STREAM_STOP);
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;
868 cancel_delayed_work(&soc_stream_work);
869 if (rtd->codec_dai->digital_mute)
870 rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
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;
1228 INIT_WORK(&soc_stream_work, close_delayed_work, socdev);
1229 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");