blob: 8eb0f0711f8c161d809983149b9f9739bbcdca08 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/slab.h>
23#include <linux/workqueue.h>
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/initval.h>
29
Dong Aisheng17841022011-08-29 17:15:14 +080030static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
31 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010032{
33 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010034 int ret;
35
Dong Aisheng17841022011-08-29 17:15:14 +080036 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010037 !rtd->dai_link->symmetric_rates)
38 return 0;
39
40 /* This can happen if multiple streams are starting simultaneously -
41 * the second can need to get its constraints before the first has
42 * picked a rate. Complain and allow the application to carry on.
43 */
Dong Aisheng17841022011-08-29 17:15:14 +080044 if (!soc_dai->rate) {
45 dev_warn(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +010046 "Not enforcing symmetric_rates due to race\n");
47 return 0;
48 }
49
Dong Aisheng17841022011-08-29 17:15:14 +080050 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010051
52 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
53 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +080054 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010055 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +080056 dev_err(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +010057 "Unable to apply rate symmetry constraint: %d\n", ret);
58 return ret;
59 }
60
61 return 0;
62}
63
64/*
65 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
66 * then initialized and any private data can be allocated. This also calls
67 * startup for the cpu DAI, platform, machine and codec DAI.
68 */
69static int soc_pcm_open(struct snd_pcm_substream *substream)
70{
71 struct snd_soc_pcm_runtime *rtd = substream->private_data;
72 struct snd_pcm_runtime *runtime = substream->runtime;
73 struct snd_soc_platform *platform = rtd->platform;
74 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
75 struct snd_soc_dai *codec_dai = rtd->codec_dai;
76 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
77 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
78 int ret = 0;
79
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +010080 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +010081
82 /* startup the audio subsystem */
83 if (cpu_dai->driver->ops->startup) {
84 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
85 if (ret < 0) {
86 printk(KERN_ERR "asoc: can't open interface %s\n",
87 cpu_dai->name);
88 goto out;
89 }
90 }
91
92 if (platform->driver->ops && platform->driver->ops->open) {
93 ret = platform->driver->ops->open(substream);
94 if (ret < 0) {
95 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
96 goto platform_err;
97 }
98 }
99
100 if (codec_dai->driver->ops->startup) {
101 ret = codec_dai->driver->ops->startup(substream, codec_dai);
102 if (ret < 0) {
103 printk(KERN_ERR "asoc: can't open codec %s\n",
104 codec_dai->name);
105 goto codec_dai_err;
106 }
107 }
108
109 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
110 ret = rtd->dai_link->ops->startup(substream);
111 if (ret < 0) {
112 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
113 goto machine_err;
114 }
115 }
116
117 /* Check that the codec and cpu DAIs are compatible */
118 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
119 runtime->hw.rate_min =
120 max(codec_dai_drv->playback.rate_min,
121 cpu_dai_drv->playback.rate_min);
122 runtime->hw.rate_max =
123 min(codec_dai_drv->playback.rate_max,
124 cpu_dai_drv->playback.rate_max);
125 runtime->hw.channels_min =
126 max(codec_dai_drv->playback.channels_min,
127 cpu_dai_drv->playback.channels_min);
128 runtime->hw.channels_max =
129 min(codec_dai_drv->playback.channels_max,
130 cpu_dai_drv->playback.channels_max);
131 runtime->hw.formats =
132 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
133 runtime->hw.rates =
134 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
135 if (codec_dai_drv->playback.rates
136 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
137 runtime->hw.rates |= cpu_dai_drv->playback.rates;
138 if (cpu_dai_drv->playback.rates
139 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
140 runtime->hw.rates |= codec_dai_drv->playback.rates;
141 } else {
142 runtime->hw.rate_min =
143 max(codec_dai_drv->capture.rate_min,
144 cpu_dai_drv->capture.rate_min);
145 runtime->hw.rate_max =
146 min(codec_dai_drv->capture.rate_max,
147 cpu_dai_drv->capture.rate_max);
148 runtime->hw.channels_min =
149 max(codec_dai_drv->capture.channels_min,
150 cpu_dai_drv->capture.channels_min);
151 runtime->hw.channels_max =
152 min(codec_dai_drv->capture.channels_max,
153 cpu_dai_drv->capture.channels_max);
154 runtime->hw.formats =
155 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
156 runtime->hw.rates =
157 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
158 if (codec_dai_drv->capture.rates
159 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
160 runtime->hw.rates |= cpu_dai_drv->capture.rates;
161 if (cpu_dai_drv->capture.rates
162 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
163 runtime->hw.rates |= codec_dai_drv->capture.rates;
164 }
165
166 ret = -EINVAL;
167 snd_pcm_limit_hw_rates(runtime);
168 if (!runtime->hw.rates) {
169 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
170 codec_dai->name, cpu_dai->name);
171 goto config_err;
172 }
173 if (!runtime->hw.formats) {
174 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
175 codec_dai->name, cpu_dai->name);
176 goto config_err;
177 }
178 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
179 runtime->hw.channels_min > runtime->hw.channels_max) {
180 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
181 codec_dai->name, cpu_dai->name);
182 goto config_err;
183 }
184
185 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800186 if (cpu_dai->active) {
187 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
188 if (ret != 0)
189 goto config_err;
190 }
191
192 if (codec_dai->active) {
193 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194 if (ret != 0)
195 goto config_err;
196 }
197
198 pr_debug("asoc: %s <-> %s info:\n",
199 codec_dai->name, cpu_dai->name);
200 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
201 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
202 runtime->hw.channels_max);
203 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
204 runtime->hw.rate_max);
205
206 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
207 cpu_dai->playback_active++;
208 codec_dai->playback_active++;
209 } else {
210 cpu_dai->capture_active++;
211 codec_dai->capture_active++;
212 }
213 cpu_dai->active++;
214 codec_dai->active++;
215 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100216 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100217 return 0;
218
219config_err:
220 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
221 rtd->dai_link->ops->shutdown(substream);
222
223machine_err:
224 if (codec_dai->driver->ops->shutdown)
225 codec_dai->driver->ops->shutdown(substream, codec_dai);
226
227codec_dai_err:
228 if (platform->driver->ops && platform->driver->ops->close)
229 platform->driver->ops->close(substream);
230
231platform_err:
232 if (cpu_dai->driver->ops->shutdown)
233 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
234out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100235 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100236 return ret;
237}
238
239/*
240 * Power down the audio subsystem pmdown_time msecs after close is called.
241 * This is to ensure there are no pops or clicks in between any music tracks
242 * due to DAPM power cycling.
243 */
244static void close_delayed_work(struct work_struct *work)
245{
246 struct snd_soc_pcm_runtime *rtd =
247 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
248 struct snd_soc_dai *codec_dai = rtd->codec_dai;
249
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100250 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100251
252 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
253 codec_dai->driver->playback.stream_name,
254 codec_dai->playback_active ? "active" : "inactive",
255 codec_dai->pop_wait ? "yes" : "no");
256
257 /* are we waiting on this codec DAI stream */
258 if (codec_dai->pop_wait == 1) {
259 codec_dai->pop_wait = 0;
260 snd_soc_dapm_stream_event(rtd,
261 codec_dai->driver->playback.stream_name,
262 SND_SOC_DAPM_STREAM_STOP);
263 }
264
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100265 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100266}
267
268/*
269 * Called by ALSA when a PCM substream is closed. Private data can be
270 * freed here. The cpu DAI, codec DAI, machine and platform are also
271 * shutdown.
272 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100273static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100274{
275 struct snd_soc_pcm_runtime *rtd = substream->private_data;
276 struct snd_soc_platform *platform = rtd->platform;
277 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
278 struct snd_soc_dai *codec_dai = rtd->codec_dai;
279 struct snd_soc_codec *codec = rtd->codec;
280
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100281 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100282
283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
284 cpu_dai->playback_active--;
285 codec_dai->playback_active--;
286 } else {
287 cpu_dai->capture_active--;
288 codec_dai->capture_active--;
289 }
290
291 cpu_dai->active--;
292 codec_dai->active--;
293 codec->active--;
294
Dong Aisheng17841022011-08-29 17:15:14 +0800295 /* clear the corresponding DAIs rate when inactive */
296 if (!cpu_dai->active)
297 cpu_dai->rate = 0;
298
299 if (!codec_dai->active)
300 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200301
Liam Girdwoodddee6272011-06-09 14:45:53 +0100302 /* Muting the DAC suppresses artifacts caused during digital
303 * shutdown, for example from stopping clocks.
304 */
305 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
306 snd_soc_dai_digital_mute(codec_dai, 1);
307
308 if (cpu_dai->driver->ops->shutdown)
309 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
310
311 if (codec_dai->driver->ops->shutdown)
312 codec_dai->driver->ops->shutdown(substream, codec_dai);
313
314 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
315 rtd->dai_link->ops->shutdown(substream);
316
317 if (platform->driver->ops && platform->driver->ops->close)
318 platform->driver->ops->close(substream);
319 cpu_dai->runtime = NULL;
320
321 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
322 /* start delayed pop wq here for playback streams */
323 codec_dai->pop_wait = 1;
324 schedule_delayed_work(&rtd->delayed_work,
325 msecs_to_jiffies(rtd->pmdown_time));
326 } else {
327 /* capture streams can be powered down now */
328 snd_soc_dapm_stream_event(rtd,
329 codec_dai->driver->capture.stream_name,
330 SND_SOC_DAPM_STREAM_STOP);
331 }
332
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100333 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100334 return 0;
335}
336
337/*
338 * Called by ALSA when the PCM substream is prepared, can set format, sample
339 * rate, etc. This function is non atomic and can be called multiple times,
340 * it can refer to the runtime info.
341 */
342static int soc_pcm_prepare(struct snd_pcm_substream *substream)
343{
344 struct snd_soc_pcm_runtime *rtd = substream->private_data;
345 struct snd_soc_platform *platform = rtd->platform;
346 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
347 struct snd_soc_dai *codec_dai = rtd->codec_dai;
348 int ret = 0;
349
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100350 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100351
352 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
353 ret = rtd->dai_link->ops->prepare(substream);
354 if (ret < 0) {
355 printk(KERN_ERR "asoc: machine prepare error\n");
356 goto out;
357 }
358 }
359
360 if (platform->driver->ops && platform->driver->ops->prepare) {
361 ret = platform->driver->ops->prepare(substream);
362 if (ret < 0) {
363 printk(KERN_ERR "asoc: platform prepare error\n");
364 goto out;
365 }
366 }
367
368 if (codec_dai->driver->ops->prepare) {
369 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
370 if (ret < 0) {
371 printk(KERN_ERR "asoc: codec DAI prepare error\n");
372 goto out;
373 }
374 }
375
376 if (cpu_dai->driver->ops->prepare) {
377 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
378 if (ret < 0) {
379 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
380 goto out;
381 }
382 }
383
384 /* cancel any delayed stream shutdown that is pending */
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
386 codec_dai->pop_wait) {
387 codec_dai->pop_wait = 0;
388 cancel_delayed_work(&rtd->delayed_work);
389 }
390
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 snd_soc_dapm_stream_event(rtd,
393 codec_dai->driver->playback.stream_name,
394 SND_SOC_DAPM_STREAM_START);
395 else
396 snd_soc_dapm_stream_event(rtd,
397 codec_dai->driver->capture.stream_name,
398 SND_SOC_DAPM_STREAM_START);
399
400 snd_soc_dai_digital_mute(codec_dai, 0);
401
402out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100403 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100404 return ret;
405}
406
407/*
408 * Called by ALSA when the hardware params are set by application. This
409 * function can also be called multiple times and can allocate buffers
410 * (using snd_pcm_lib_* ). It's non-atomic.
411 */
412static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
413 struct snd_pcm_hw_params *params)
414{
415 struct snd_soc_pcm_runtime *rtd = substream->private_data;
416 struct snd_soc_platform *platform = rtd->platform;
417 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
418 struct snd_soc_dai *codec_dai = rtd->codec_dai;
419 int ret = 0;
420
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100421 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100422
423 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
424 ret = rtd->dai_link->ops->hw_params(substream, params);
425 if (ret < 0) {
426 printk(KERN_ERR "asoc: machine hw_params failed\n");
427 goto out;
428 }
429 }
430
431 if (codec_dai->driver->ops->hw_params) {
432 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
433 if (ret < 0) {
434 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
435 codec_dai->name);
436 goto codec_err;
437 }
438 }
439
440 if (cpu_dai->driver->ops->hw_params) {
441 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
442 if (ret < 0) {
443 printk(KERN_ERR "asoc: interface %s hw params failed\n",
444 cpu_dai->name);
445 goto interface_err;
446 }
447 }
448
449 if (platform->driver->ops && platform->driver->ops->hw_params) {
450 ret = platform->driver->ops->hw_params(substream, params);
451 if (ret < 0) {
452 printk(KERN_ERR "asoc: platform %s hw params failed\n",
453 platform->name);
454 goto platform_err;
455 }
456 }
457
Dong Aisheng17841022011-08-29 17:15:14 +0800458 /* store the rate for each DAIs */
459 cpu_dai->rate = params_rate(params);
460 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100461
462out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100463 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100464 return ret;
465
466platform_err:
467 if (cpu_dai->driver->ops->hw_free)
468 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
469
470interface_err:
471 if (codec_dai->driver->ops->hw_free)
472 codec_dai->driver->ops->hw_free(substream, codec_dai);
473
474codec_err:
475 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
476 rtd->dai_link->ops->hw_free(substream);
477
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100478 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100479 return ret;
480}
481
482/*
483 * Frees resources allocated by hw_params, can be called multiple times
484 */
485static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
486{
487 struct snd_soc_pcm_runtime *rtd = substream->private_data;
488 struct snd_soc_platform *platform = rtd->platform;
489 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
490 struct snd_soc_dai *codec_dai = rtd->codec_dai;
491 struct snd_soc_codec *codec = rtd->codec;
492
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100493 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494
495 /* apply codec digital mute */
496 if (!codec->active)
497 snd_soc_dai_digital_mute(codec_dai, 1);
498
499 /* free any machine hw params */
500 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
501 rtd->dai_link->ops->hw_free(substream);
502
503 /* free any DMA resources */
504 if (platform->driver->ops && platform->driver->ops->hw_free)
505 platform->driver->ops->hw_free(substream);
506
507 /* now free hw params for the DAIs */
508 if (codec_dai->driver->ops->hw_free)
509 codec_dai->driver->ops->hw_free(substream, codec_dai);
510
511 if (cpu_dai->driver->ops->hw_free)
512 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
513
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100514 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100515 return 0;
516}
517
518static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
519{
520 struct snd_soc_pcm_runtime *rtd = substream->private_data;
521 struct snd_soc_platform *platform = rtd->platform;
522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
523 struct snd_soc_dai *codec_dai = rtd->codec_dai;
524 int ret;
525
526 if (codec_dai->driver->ops->trigger) {
527 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
528 if (ret < 0)
529 return ret;
530 }
531
532 if (platform->driver->ops && platform->driver->ops->trigger) {
533 ret = platform->driver->ops->trigger(substream, cmd);
534 if (ret < 0)
535 return ret;
536 }
537
538 if (cpu_dai->driver->ops->trigger) {
539 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
540 if (ret < 0)
541 return ret;
542 }
543 return 0;
544}
545
546/*
547 * soc level wrapper for pointer callback
548 * If cpu_dai, codec_dai, platform driver has the delay callback, than
549 * the runtime->delay will be updated accordingly.
550 */
551static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
552{
553 struct snd_soc_pcm_runtime *rtd = substream->private_data;
554 struct snd_soc_platform *platform = rtd->platform;
555 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
556 struct snd_soc_dai *codec_dai = rtd->codec_dai;
557 struct snd_pcm_runtime *runtime = substream->runtime;
558 snd_pcm_uframes_t offset = 0;
559 snd_pcm_sframes_t delay = 0;
560
561 if (platform->driver->ops && platform->driver->ops->pointer)
562 offset = platform->driver->ops->pointer(substream);
563
564 if (cpu_dai->driver->ops->delay)
565 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
566
567 if (codec_dai->driver->ops->delay)
568 delay += codec_dai->driver->ops->delay(substream, codec_dai);
569
570 if (platform->driver->delay)
571 delay += platform->driver->delay(substream, codec_dai);
572
573 runtime->delay = delay;
574
575 return offset;
576}
577
578/* ASoC PCM operations */
579static struct snd_pcm_ops soc_pcm_ops = {
580 .open = soc_pcm_open,
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100581 .close = soc_pcm_close,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100582 .hw_params = soc_pcm_hw_params,
583 .hw_free = soc_pcm_hw_free,
584 .prepare = soc_pcm_prepare,
585 .trigger = soc_pcm_trigger,
586 .pointer = soc_pcm_pointer,
587};
588
589/* create a new pcm */
590int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
591{
592 struct snd_soc_codec *codec = rtd->codec;
593 struct snd_soc_platform *platform = rtd->platform;
594 struct snd_soc_dai *codec_dai = rtd->codec_dai;
595 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
596 struct snd_pcm *pcm;
597 char new_name[64];
598 int ret = 0, playback = 0, capture = 0;
599
600 /* check client and interface hw capabilities */
601 snprintf(new_name, sizeof(new_name), "%s %s-%d",
602 rtd->dai_link->stream_name, codec_dai->name, num);
603
604 if (codec_dai->driver->playback.channels_min)
605 playback = 1;
606 if (codec_dai->driver->capture.channels_min)
607 capture = 1;
608
609 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
610 ret = snd_pcm_new(rtd->card->snd_card, new_name,
611 num, playback, capture, &pcm);
612 if (ret < 0) {
613 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
614 return ret;
615 }
616
617 /* DAPM dai link stream work */
618 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
619
620 rtd->pcm = pcm;
621 pcm->private_data = rtd;
622 if (platform->driver->ops) {
623 soc_pcm_ops.mmap = platform->driver->ops->mmap;
624 soc_pcm_ops.pointer = platform->driver->ops->pointer;
625 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
626 soc_pcm_ops.copy = platform->driver->ops->copy;
627 soc_pcm_ops.silence = platform->driver->ops->silence;
628 soc_pcm_ops.ack = platform->driver->ops->ack;
629 soc_pcm_ops.page = platform->driver->ops->page;
630 }
631
632 if (playback)
633 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
634
635 if (capture)
636 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
637
638 if (platform->driver->pcm_new) {
639 ret = platform->driver->pcm_new(rtd);
640 if (ret < 0) {
641 pr_err("asoc: platform pcm constructor failed\n");
642 return ret;
643 }
644 }
645
646 pcm->private_free = platform->driver->pcm_free;
647 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
648 cpu_dai->name);
649 return ret;
650}