blob: 4e3be82e79cb433d7086e36df754d434156af674 [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>
Mark Brownd6652ef2011-12-03 20:14:31 +000022#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010023#include <linux/slab.h>
24#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010025#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010026#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010031#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010032#include <sound/initval.h>
33
Liam Girdwood01d75842012-04-25 12:12:49 +010034#define DPCM_MAX_BE_USERS 8
35
36/* DPCM stream event, send event to FE and all active BEs. */
37static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
38 int event)
39{
40 struct snd_soc_dpcm *dpcm;
41
42 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
43
44 struct snd_soc_pcm_runtime *be = dpcm->be;
45
46 dev_dbg(be->dev, "pm: BE %s event %d dir %d\n",
47 be->dai_link->name, event, dir);
48
49 snd_soc_dapm_stream_event(be, dir, event);
50 }
51
52 snd_soc_dapm_stream_event(fe, dir, event);
53
54 return 0;
55}
56
Dong Aisheng17841022011-08-29 17:15:14 +080057static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
58 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010059{
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010061 int ret;
62
Dong Aisheng17841022011-08-29 17:15:14 +080063 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010064 !rtd->dai_link->symmetric_rates)
65 return 0;
66
67 /* This can happen if multiple streams are starting simultaneously -
68 * the second can need to get its constraints before the first has
69 * picked a rate. Complain and allow the application to carry on.
70 */
Dong Aisheng17841022011-08-29 17:15:14 +080071 if (!soc_dai->rate) {
72 dev_warn(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +010073 "Not enforcing symmetric_rates due to race\n");
74 return 0;
75 }
76
Dong Aisheng17841022011-08-29 17:15:14 +080077 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010078
79 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
80 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +080081 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010082 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +080083 dev_err(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +010084 "Unable to apply rate symmetry constraint: %d\n", ret);
85 return ret;
86 }
87
88 return 0;
89}
90
91/*
Mark Brown58ba9b22012-01-16 18:38:51 +000092 * List of sample sizes that might go over the bus for parameter
93 * application. There ought to be a wildcard sample size for things
94 * like the DAC/ADC resolution to use but there isn't right now.
95 */
96static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +020097 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +000098};
99
100static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
101 struct snd_soc_dai *dai)
102{
103 int ret, i, bits;
104
105 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
106 bits = dai->driver->playback.sig_bits;
107 else
108 bits = dai->driver->capture.sig_bits;
109
110 if (!bits)
111 return;
112
113 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000114 if (bits >= sample_sizes[i])
115 continue;
116
117 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
118 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000119 if (ret != 0)
120 dev_warn(dai->dev,
121 "Failed to set MSB %d/%d: %d\n",
122 bits, sample_sizes[i], ret);
123 }
124}
125
126/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100127 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
128 * then initialized and any private data can be allocated. This also calls
129 * startup for the cpu DAI, platform, machine and codec DAI.
130 */
131static int soc_pcm_open(struct snd_pcm_substream *substream)
132{
133 struct snd_soc_pcm_runtime *rtd = substream->private_data;
134 struct snd_pcm_runtime *runtime = substream->runtime;
135 struct snd_soc_platform *platform = rtd->platform;
136 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
137 struct snd_soc_dai *codec_dai = rtd->codec_dai;
138 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
139 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
140 int ret = 0;
141
Mark Brownd6652ef2011-12-03 20:14:31 +0000142 pm_runtime_get_sync(cpu_dai->dev);
143 pm_runtime_get_sync(codec_dai->dev);
144 pm_runtime_get_sync(platform->dev);
145
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100146 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100147
148 /* startup the audio subsystem */
149 if (cpu_dai->driver->ops->startup) {
150 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
151 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000152 dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
153 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100154 goto out;
155 }
156 }
157
158 if (platform->driver->ops && platform->driver->ops->open) {
159 ret = platform->driver->ops->open(substream);
160 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000161 dev_err(platform->dev, "can't open platform %s: %d\n",
162 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100163 goto platform_err;
164 }
165 }
166
167 if (codec_dai->driver->ops->startup) {
168 ret = codec_dai->driver->ops->startup(substream, codec_dai);
169 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000170 dev_err(codec_dai->dev, "can't open codec %s: %d\n",
171 codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100172 goto codec_dai_err;
173 }
174 }
175
176 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
177 ret = rtd->dai_link->ops->startup(substream);
178 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000179 pr_err("asoc: %s startup failed: %d\n",
180 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100181 goto machine_err;
182 }
183 }
184
Liam Girdwood01d75842012-04-25 12:12:49 +0100185 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
186 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
187 goto dynamic;
188
Liam Girdwoodddee6272011-06-09 14:45:53 +0100189 /* Check that the codec and cpu DAIs are compatible */
190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
191 runtime->hw.rate_min =
192 max(codec_dai_drv->playback.rate_min,
193 cpu_dai_drv->playback.rate_min);
194 runtime->hw.rate_max =
195 min(codec_dai_drv->playback.rate_max,
196 cpu_dai_drv->playback.rate_max);
197 runtime->hw.channels_min =
198 max(codec_dai_drv->playback.channels_min,
199 cpu_dai_drv->playback.channels_min);
200 runtime->hw.channels_max =
201 min(codec_dai_drv->playback.channels_max,
202 cpu_dai_drv->playback.channels_max);
203 runtime->hw.formats =
204 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
205 runtime->hw.rates =
206 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
207 if (codec_dai_drv->playback.rates
208 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
209 runtime->hw.rates |= cpu_dai_drv->playback.rates;
210 if (cpu_dai_drv->playback.rates
211 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
212 runtime->hw.rates |= codec_dai_drv->playback.rates;
213 } else {
214 runtime->hw.rate_min =
215 max(codec_dai_drv->capture.rate_min,
216 cpu_dai_drv->capture.rate_min);
217 runtime->hw.rate_max =
218 min(codec_dai_drv->capture.rate_max,
219 cpu_dai_drv->capture.rate_max);
220 runtime->hw.channels_min =
221 max(codec_dai_drv->capture.channels_min,
222 cpu_dai_drv->capture.channels_min);
223 runtime->hw.channels_max =
224 min(codec_dai_drv->capture.channels_max,
225 cpu_dai_drv->capture.channels_max);
226 runtime->hw.formats =
227 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
228 runtime->hw.rates =
229 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
230 if (codec_dai_drv->capture.rates
231 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
232 runtime->hw.rates |= cpu_dai_drv->capture.rates;
233 if (cpu_dai_drv->capture.rates
234 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
235 runtime->hw.rates |= codec_dai_drv->capture.rates;
236 }
237
238 ret = -EINVAL;
239 snd_pcm_limit_hw_rates(runtime);
240 if (!runtime->hw.rates) {
241 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
242 codec_dai->name, cpu_dai->name);
243 goto config_err;
244 }
245 if (!runtime->hw.formats) {
246 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
247 codec_dai->name, cpu_dai->name);
248 goto config_err;
249 }
250 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
251 runtime->hw.channels_min > runtime->hw.channels_max) {
252 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
253 codec_dai->name, cpu_dai->name);
254 goto config_err;
255 }
256
Mark Brown58ba9b22012-01-16 18:38:51 +0000257 soc_pcm_apply_msb(substream, codec_dai);
258 soc_pcm_apply_msb(substream, cpu_dai);
259
Liam Girdwoodddee6272011-06-09 14:45:53 +0100260 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800261 if (cpu_dai->active) {
262 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
263 if (ret != 0)
264 goto config_err;
265 }
266
267 if (codec_dai->active) {
268 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100269 if (ret != 0)
270 goto config_err;
271 }
272
273 pr_debug("asoc: %s <-> %s info:\n",
274 codec_dai->name, cpu_dai->name);
275 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
276 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
277 runtime->hw.channels_max);
278 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
279 runtime->hw.rate_max);
280
Liam Girdwood01d75842012-04-25 12:12:49 +0100281dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
283 cpu_dai->playback_active++;
284 codec_dai->playback_active++;
285 } else {
286 cpu_dai->capture_active++;
287 codec_dai->capture_active++;
288 }
289 cpu_dai->active++;
290 codec_dai->active++;
291 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100292 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100293 return 0;
294
295config_err:
296 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
297 rtd->dai_link->ops->shutdown(substream);
298
299machine_err:
300 if (codec_dai->driver->ops->shutdown)
301 codec_dai->driver->ops->shutdown(substream, codec_dai);
302
303codec_dai_err:
304 if (platform->driver->ops && platform->driver->ops->close)
305 platform->driver->ops->close(substream);
306
307platform_err:
308 if (cpu_dai->driver->ops->shutdown)
309 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
310out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100311 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000312
313 pm_runtime_put(platform->dev);
314 pm_runtime_put(codec_dai->dev);
315 pm_runtime_put(cpu_dai->dev);
316
Liam Girdwoodddee6272011-06-09 14:45:53 +0100317 return ret;
318}
319
320/*
321 * Power down the audio subsystem pmdown_time msecs after close is called.
322 * This is to ensure there are no pops or clicks in between any music tracks
323 * due to DAPM power cycling.
324 */
325static void close_delayed_work(struct work_struct *work)
326{
327 struct snd_soc_pcm_runtime *rtd =
328 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
329 struct snd_soc_dai *codec_dai = rtd->codec_dai;
330
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100331 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100332
333 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
334 codec_dai->driver->playback.stream_name,
335 codec_dai->playback_active ? "active" : "inactive",
336 codec_dai->pop_wait ? "yes" : "no");
337
338 /* are we waiting on this codec DAI stream */
339 if (codec_dai->pop_wait == 1) {
340 codec_dai->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800341 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000342 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100343 }
344
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100345 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100346}
347
348/*
349 * Called by ALSA when a PCM substream is closed. Private data can be
350 * freed here. The cpu DAI, codec DAI, machine and platform are also
351 * shutdown.
352 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100353static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100354{
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 struct snd_soc_platform *platform = rtd->platform;
357 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
358 struct snd_soc_dai *codec_dai = rtd->codec_dai;
359 struct snd_soc_codec *codec = rtd->codec;
360
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100361 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100362
363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
364 cpu_dai->playback_active--;
365 codec_dai->playback_active--;
366 } else {
367 cpu_dai->capture_active--;
368 codec_dai->capture_active--;
369 }
370
371 cpu_dai->active--;
372 codec_dai->active--;
373 codec->active--;
374
Dong Aisheng17841022011-08-29 17:15:14 +0800375 /* clear the corresponding DAIs rate when inactive */
376 if (!cpu_dai->active)
377 cpu_dai->rate = 0;
378
379 if (!codec_dai->active)
380 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200381
Liam Girdwoodddee6272011-06-09 14:45:53 +0100382 /* Muting the DAC suppresses artifacts caused during digital
383 * shutdown, for example from stopping clocks.
384 */
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 snd_soc_dai_digital_mute(codec_dai, 1);
387
388 if (cpu_dai->driver->ops->shutdown)
389 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
390
391 if (codec_dai->driver->ops->shutdown)
392 codec_dai->driver->ops->shutdown(substream, codec_dai);
393
394 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
395 rtd->dai_link->ops->shutdown(substream);
396
397 if (platform->driver->ops && platform->driver->ops->close)
398 platform->driver->ops->close(substream);
399 cpu_dai->runtime = NULL;
400
401 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000402 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200403 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300404 /* powered down playback stream now */
405 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800406 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800407 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300408 } else {
409 /* start delayed pop wq here for playback streams */
410 codec_dai->pop_wait = 1;
411 schedule_delayed_work(&rtd->delayed_work,
412 msecs_to_jiffies(rtd->pmdown_time));
413 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100414 } else {
415 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800416 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000417 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100418 }
419
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100420 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000421
422 pm_runtime_put(platform->dev);
423 pm_runtime_put(codec_dai->dev);
424 pm_runtime_put(cpu_dai->dev);
425
Liam Girdwoodddee6272011-06-09 14:45:53 +0100426 return 0;
427}
428
429/*
430 * Called by ALSA when the PCM substream is prepared, can set format, sample
431 * rate, etc. This function is non atomic and can be called multiple times,
432 * it can refer to the runtime info.
433 */
434static int soc_pcm_prepare(struct snd_pcm_substream *substream)
435{
436 struct snd_soc_pcm_runtime *rtd = substream->private_data;
437 struct snd_soc_platform *platform = rtd->platform;
438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
439 struct snd_soc_dai *codec_dai = rtd->codec_dai;
440 int ret = 0;
441
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100442 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100443
444 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
445 ret = rtd->dai_link->ops->prepare(substream);
446 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000447 pr_err("asoc: machine prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448 goto out;
449 }
450 }
451
452 if (platform->driver->ops && platform->driver->ops->prepare) {
453 ret = platform->driver->ops->prepare(substream);
454 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000455 dev_err(platform->dev, "platform prepare error: %d\n",
456 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100457 goto out;
458 }
459 }
460
461 if (codec_dai->driver->ops->prepare) {
462 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
463 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000464 dev_err(codec_dai->dev, "DAI prepare error: %d\n",
465 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100466 goto out;
467 }
468 }
469
470 if (cpu_dai->driver->ops->prepare) {
471 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
472 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000473 dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
474 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100475 goto out;
476 }
477 }
478
479 /* cancel any delayed stream shutdown that is pending */
480 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
481 codec_dai->pop_wait) {
482 codec_dai->pop_wait = 0;
483 cancel_delayed_work(&rtd->delayed_work);
484 }
485
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000486 snd_soc_dapm_stream_event(rtd, substream->stream,
487 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100488
489 snd_soc_dai_digital_mute(codec_dai, 0);
490
491out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100492 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100493 return ret;
494}
495
496/*
497 * Called by ALSA when the hardware params are set by application. This
498 * function can also be called multiple times and can allocate buffers
499 * (using snd_pcm_lib_* ). It's non-atomic.
500 */
501static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
502 struct snd_pcm_hw_params *params)
503{
504 struct snd_soc_pcm_runtime *rtd = substream->private_data;
505 struct snd_soc_platform *platform = rtd->platform;
506 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
507 struct snd_soc_dai *codec_dai = rtd->codec_dai;
508 int ret = 0;
509
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100510 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100511
512 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
513 ret = rtd->dai_link->ops->hw_params(substream, params);
514 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000515 pr_err("asoc: machine hw_params failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100516 goto out;
517 }
518 }
519
520 if (codec_dai->driver->ops->hw_params) {
521 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
522 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000523 dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
524 codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100525 goto codec_err;
526 }
527 }
528
529 if (cpu_dai->driver->ops->hw_params) {
530 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
531 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000532 dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
533 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100534 goto interface_err;
535 }
536 }
537
538 if (platform->driver->ops && platform->driver->ops->hw_params) {
539 ret = platform->driver->ops->hw_params(substream, params);
540 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000541 dev_err(platform->dev, "%s hw params failed: %d\n",
542 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543 goto platform_err;
544 }
545 }
546
Dong Aisheng17841022011-08-29 17:15:14 +0800547 /* store the rate for each DAIs */
548 cpu_dai->rate = params_rate(params);
549 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550
551out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100552 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100553 return ret;
554
555platform_err:
556 if (cpu_dai->driver->ops->hw_free)
557 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
558
559interface_err:
560 if (codec_dai->driver->ops->hw_free)
561 codec_dai->driver->ops->hw_free(substream, codec_dai);
562
563codec_err:
564 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
565 rtd->dai_link->ops->hw_free(substream);
566
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100567 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100568 return ret;
569}
570
571/*
572 * Frees resources allocated by hw_params, can be called multiple times
573 */
574static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
575{
576 struct snd_soc_pcm_runtime *rtd = substream->private_data;
577 struct snd_soc_platform *platform = rtd->platform;
578 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
579 struct snd_soc_dai *codec_dai = rtd->codec_dai;
580 struct snd_soc_codec *codec = rtd->codec;
581
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100582 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100583
584 /* apply codec digital mute */
585 if (!codec->active)
586 snd_soc_dai_digital_mute(codec_dai, 1);
587
588 /* free any machine hw params */
589 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
590 rtd->dai_link->ops->hw_free(substream);
591
592 /* free any DMA resources */
593 if (platform->driver->ops && platform->driver->ops->hw_free)
594 platform->driver->ops->hw_free(substream);
595
596 /* now free hw params for the DAIs */
597 if (codec_dai->driver->ops->hw_free)
598 codec_dai->driver->ops->hw_free(substream, codec_dai);
599
600 if (cpu_dai->driver->ops->hw_free)
601 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
602
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100603 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100604 return 0;
605}
606
607static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
608{
609 struct snd_soc_pcm_runtime *rtd = substream->private_data;
610 struct snd_soc_platform *platform = rtd->platform;
611 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
612 struct snd_soc_dai *codec_dai = rtd->codec_dai;
613 int ret;
614
615 if (codec_dai->driver->ops->trigger) {
616 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
617 if (ret < 0)
618 return ret;
619 }
620
621 if (platform->driver->ops && platform->driver->ops->trigger) {
622 ret = platform->driver->ops->trigger(substream, cmd);
623 if (ret < 0)
624 return ret;
625 }
626
627 if (cpu_dai->driver->ops->trigger) {
628 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
629 if (ret < 0)
630 return ret;
631 }
632 return 0;
633}
634
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100635int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, int cmd)
636{
637 struct snd_soc_pcm_runtime *rtd = substream->private_data;
638 struct snd_soc_platform *platform = rtd->platform;
639 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
640 struct snd_soc_dai *codec_dai = rtd->codec_dai;
641 int ret;
642
643 if (codec_dai->driver->ops->bespoke_trigger) {
644 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
645 if (ret < 0)
646 return ret;
647 }
648
649 if (platform->driver->bespoke_trigger) {
650 ret = platform->driver->bespoke_trigger(substream, cmd);
651 if (ret < 0)
652 return ret;
653 }
654
655 if (cpu_dai->driver->ops->bespoke_trigger) {
656 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
657 if (ret < 0)
658 return ret;
659 }
660 return 0;
661}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100662/*
663 * soc level wrapper for pointer callback
664 * If cpu_dai, codec_dai, platform driver has the delay callback, than
665 * the runtime->delay will be updated accordingly.
666 */
667static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
668{
669 struct snd_soc_pcm_runtime *rtd = substream->private_data;
670 struct snd_soc_platform *platform = rtd->platform;
671 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
672 struct snd_soc_dai *codec_dai = rtd->codec_dai;
673 struct snd_pcm_runtime *runtime = substream->runtime;
674 snd_pcm_uframes_t offset = 0;
675 snd_pcm_sframes_t delay = 0;
676
677 if (platform->driver->ops && platform->driver->ops->pointer)
678 offset = platform->driver->ops->pointer(substream);
679
680 if (cpu_dai->driver->ops->delay)
681 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
682
683 if (codec_dai->driver->ops->delay)
684 delay += codec_dai->driver->ops->delay(substream, codec_dai);
685
686 if (platform->driver->delay)
687 delay += platform->driver->delay(substream, codec_dai);
688
689 runtime->delay = delay;
690
691 return offset;
692}
693
Liam Girdwood01d75842012-04-25 12:12:49 +0100694/* connect a FE and BE */
695static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
696 struct snd_soc_pcm_runtime *be, int stream)
697{
698 struct snd_soc_dpcm *dpcm;
699
700 /* only add new dpcms */
701 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
702 if (dpcm->be == be && dpcm->fe == fe)
703 return 0;
704 }
705
706 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
707 if (!dpcm)
708 return -ENOMEM;
709
710 dpcm->be = be;
711 dpcm->fe = fe;
712 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
713 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
714 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
715 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
716
717 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
718 stream ? "capture" : "playback", fe->dai_link->name,
719 stream ? "<-" : "->", be->dai_link->name);
720
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100721#ifdef CONFIG_DEBUG_FS
722 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
723 fe->debugfs_dpcm_root, &dpcm->state);
724#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100725 return 1;
726}
727
728/* reparent a BE onto another FE */
729static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
730 struct snd_soc_pcm_runtime *be, int stream)
731{
732 struct snd_soc_dpcm *dpcm;
733 struct snd_pcm_substream *fe_substream, *be_substream;
734
735 /* reparent if BE is connected to other FEs */
736 if (!be->dpcm[stream].users)
737 return;
738
739 be_substream = snd_soc_dpcm_get_substream(be, stream);
740
741 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
742 if (dpcm->fe == fe)
743 continue;
744
745 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
746 stream ? "capture" : "playback",
747 dpcm->fe->dai_link->name,
748 stream ? "<-" : "->", dpcm->be->dai_link->name);
749
750 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
751 be_substream->runtime = fe_substream->runtime;
752 break;
753 }
754}
755
756/* disconnect a BE and FE */
757static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
758{
759 struct snd_soc_dpcm *dpcm, *d;
760
761 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
762 dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
763 stream ? "capture" : "playback",
764 dpcm->be->dai_link->name);
765
766 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
767 continue;
768
769 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
770 stream ? "capture" : "playback", fe->dai_link->name,
771 stream ? "<-" : "->", dpcm->be->dai_link->name);
772
773 /* BEs still alive need new FE */
774 dpcm_be_reparent(fe, dpcm->be, stream);
775
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100776#ifdef CONFIG_DEBUG_FS
777 debugfs_remove(dpcm->debugfs_state);
778#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100779 list_del(&dpcm->list_be);
780 list_del(&dpcm->list_fe);
781 kfree(dpcm);
782 }
783}
784
785/* get BE for DAI widget and stream */
786static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
787 struct snd_soc_dapm_widget *widget, int stream)
788{
789 struct snd_soc_pcm_runtime *be;
790 int i;
791
792 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
793 for (i = 0; i < card->num_links; i++) {
794 be = &card->rtd[i];
795
796 if (be->cpu_dai->playback_widget == widget ||
797 be->codec_dai->playback_widget == widget)
798 return be;
799 }
800 } else {
801
802 for (i = 0; i < card->num_links; i++) {
803 be = &card->rtd[i];
804
805 if (be->cpu_dai->capture_widget == widget ||
806 be->codec_dai->capture_widget == widget)
807 return be;
808 }
809 }
810
811 dev_err(card->dev, "can't get %s BE for %s\n",
812 stream ? "capture" : "playback", widget->name);
813 return NULL;
814}
815
816static inline struct snd_soc_dapm_widget *
817 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
818{
819 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
820 return rtd->cpu_dai->playback_widget;
821 else
822 return rtd->cpu_dai->capture_widget;
823}
824
825static inline struct snd_soc_dapm_widget *
826 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
827{
828 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
829 return rtd->codec_dai->playback_widget;
830 else
831 return rtd->codec_dai->capture_widget;
832}
833
834static int widget_in_list(struct snd_soc_dapm_widget_list *list,
835 struct snd_soc_dapm_widget *widget)
836{
837 int i;
838
839 for (i = 0; i < list->num_widgets; i++) {
840 if (widget == list->widgets[i])
841 return 1;
842 }
843
844 return 0;
845}
846
847static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
848 int stream, struct snd_soc_dapm_widget_list **list_)
849{
850 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
851 struct snd_soc_dapm_widget_list *list;
852 int paths;
853
854 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
855 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
856 if (list == NULL)
857 return -ENOMEM;
858
859 /* get number of valid DAI paths and their widgets */
860 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
861
862 dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
863 stream ? "capture" : "playback");
864
865 *list_ = list;
866 return paths;
867}
868
869static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
870{
871 kfree(*list);
872}
873
874static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
875 struct snd_soc_dapm_widget_list **list_)
876{
877 struct snd_soc_dpcm *dpcm;
878 struct snd_soc_dapm_widget_list *list = *list_;
879 struct snd_soc_dapm_widget *widget;
880 int prune = 0;
881
882 /* Destroy any old FE <--> BE connections */
883 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
884
885 /* is there a valid CPU DAI widget for this BE */
886 widget = rtd_get_cpu_widget(dpcm->be, stream);
887
888 /* prune the BE if it's no longer in our active list */
889 if (widget && widget_in_list(list, widget))
890 continue;
891
892 /* is there a valid CODEC DAI widget for this BE */
893 widget = rtd_get_codec_widget(dpcm->be, stream);
894
895 /* prune the BE if it's no longer in our active list */
896 if (widget && widget_in_list(list, widget))
897 continue;
898
899 dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
900 stream ? "capture" : "playback",
901 dpcm->be->dai_link->name, fe->dai_link->name);
902 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
903 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
904 prune++;
905 }
906
907 dev_dbg(fe->dev, "found %d old BE paths for pruning\n", prune);
908 return prune;
909}
910
911static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
912 struct snd_soc_dapm_widget_list **list_)
913{
914 struct snd_soc_card *card = fe->card;
915 struct snd_soc_dapm_widget_list *list = *list_;
916 struct snd_soc_pcm_runtime *be;
917 int i, new = 0, err;
918
919 /* Create any new FE <--> BE connections */
920 for (i = 0; i < list->num_widgets; i++) {
921
922 if (list->widgets[i]->id != snd_soc_dapm_dai)
923 continue;
924
925 /* is there a valid BE rtd for this widget */
926 be = dpcm_get_be(card, list->widgets[i], stream);
927 if (!be) {
928 dev_err(fe->dev, "no BE found for %s\n",
929 list->widgets[i]->name);
930 continue;
931 }
932
933 /* make sure BE is a real BE */
934 if (!be->dai_link->no_pcm)
935 continue;
936
937 /* don't connect if FE is not running */
938 if (!fe->dpcm[stream].runtime)
939 continue;
940
941 /* newly connected FE and BE */
942 err = dpcm_be_connect(fe, be, stream);
943 if (err < 0) {
944 dev_err(fe->dev, "can't connect %s\n",
945 list->widgets[i]->name);
946 break;
947 } else if (err == 0) /* already connected */
948 continue;
949
950 /* new */
951 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
952 new++;
953 }
954
955 dev_dbg(fe->dev, "found %d new BE paths\n", new);
956 return new;
957}
958
959/*
960 * Find the corresponding BE DAIs that source or sink audio to this
961 * FE substream.
962 */
963static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
964 int stream, struct snd_soc_dapm_widget_list **list, int new)
965{
966 if (new)
967 return dpcm_add_paths(fe, stream, list);
968 else
969 return dpcm_prune_paths(fe, stream, list);
970}
971
972static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
973{
974 struct snd_soc_dpcm *dpcm;
975
976 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
977 dpcm->be->dpcm[stream].runtime_update =
978 SND_SOC_DPCM_UPDATE_NO;
979}
980
981static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
982 int stream)
983{
984 struct snd_soc_dpcm *dpcm;
985
986 /* disable any enabled and non active backends */
987 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
988
989 struct snd_soc_pcm_runtime *be = dpcm->be;
990 struct snd_pcm_substream *be_substream =
991 snd_soc_dpcm_get_substream(be, stream);
992
993 if (be->dpcm[stream].users == 0)
994 dev_err(be->dev, "no users %s at close - state %d\n",
995 stream ? "capture" : "playback",
996 be->dpcm[stream].state);
997
998 if (--be->dpcm[stream].users != 0)
999 continue;
1000
1001 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1002 continue;
1003
1004 soc_pcm_close(be_substream);
1005 be_substream->runtime = NULL;
1006 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1007 }
1008}
1009
1010static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1011{
1012 struct snd_soc_dpcm *dpcm;
1013 int err, count = 0;
1014
1015 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1016 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1017
1018 struct snd_soc_pcm_runtime *be = dpcm->be;
1019 struct snd_pcm_substream *be_substream =
1020 snd_soc_dpcm_get_substream(be, stream);
1021
1022 /* is this op for this BE ? */
1023 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1024 continue;
1025
1026 /* first time the dpcm is open ? */
1027 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1028 dev_err(be->dev, "too many users %s at open %d\n",
1029 stream ? "capture" : "playback",
1030 be->dpcm[stream].state);
1031
1032 if (be->dpcm[stream].users++ != 0)
1033 continue;
1034
1035 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1036 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1037 continue;
1038
1039 dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
1040
1041 be_substream->runtime = be->dpcm[stream].runtime;
1042 err = soc_pcm_open(be_substream);
1043 if (err < 0) {
1044 dev_err(be->dev, "BE open failed %d\n", err);
1045 be->dpcm[stream].users--;
1046 if (be->dpcm[stream].users < 0)
1047 dev_err(be->dev, "no users %s at unwind %d\n",
1048 stream ? "capture" : "playback",
1049 be->dpcm[stream].state);
1050
1051 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1052 goto unwind;
1053 }
1054
1055 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1056 count++;
1057 }
1058
1059 return count;
1060
1061unwind:
1062 /* disable any enabled and non active backends */
1063 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1064 struct snd_soc_pcm_runtime *be = dpcm->be;
1065 struct snd_pcm_substream *be_substream =
1066 snd_soc_dpcm_get_substream(be, stream);
1067
1068 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1069 continue;
1070
1071 if (be->dpcm[stream].users == 0)
1072 dev_err(be->dev, "no users %s at close %d\n",
1073 stream ? "capture" : "playback",
1074 be->dpcm[stream].state);
1075
1076 if (--be->dpcm[stream].users != 0)
1077 continue;
1078
1079 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1080 continue;
1081
1082 soc_pcm_close(be_substream);
1083 be_substream->runtime = NULL;
1084 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1085 }
1086
1087 return err;
1088}
1089
1090void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1091{
1092 struct snd_pcm_runtime *runtime = substream->runtime;
1093 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1094 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1095 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1096
1097 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1098 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1099 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1100 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1101 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1102 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1103 runtime->hw.rates = cpu_dai_drv->playback.rates;
1104 } else {
1105 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1106 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1107 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1108 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1109 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1110 runtime->hw.rates = cpu_dai_drv->capture.rates;
1111 }
1112}
1113
1114static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1115{
1116 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1117 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1118 int stream = fe_substream->stream, ret = 0;
1119
1120 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1121
1122 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1123 if (ret < 0) {
1124 dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
1125 goto be_err;
1126 }
1127
1128 dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
1129
1130 /* start the DAI frontend */
1131 ret = soc_pcm_open(fe_substream);
1132 if (ret < 0) {
1133 dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
1134 goto unwind;
1135 }
1136
1137 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1138
1139 dpcm_set_fe_runtime(fe_substream);
1140 snd_pcm_limit_hw_rates(runtime);
1141
1142 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1143 return 0;
1144
1145unwind:
1146 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1147be_err:
1148 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1149 return ret;
1150}
1151
1152static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1153{
1154 struct snd_soc_dpcm *dpcm;
1155
1156 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1157 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1158
1159 struct snd_soc_pcm_runtime *be = dpcm->be;
1160 struct snd_pcm_substream *be_substream =
1161 snd_soc_dpcm_get_substream(be, stream);
1162
1163 /* is this op for this BE ? */
1164 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1165 continue;
1166
1167 if (be->dpcm[stream].users == 0)
1168 dev_err(be->dev, "no users %s at close - state %d\n",
1169 stream ? "capture" : "playback",
1170 be->dpcm[stream].state);
1171
1172 if (--be->dpcm[stream].users != 0)
1173 continue;
1174
1175 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1176 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1177 continue;
1178
1179 dev_dbg(be->dev, "dpcm: close BE %s\n",
1180 dpcm->fe->dai_link->name);
1181
1182 soc_pcm_close(be_substream);
1183 be_substream->runtime = NULL;
1184
1185 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1186 }
1187 return 0;
1188}
1189
1190static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1191{
1192 struct snd_soc_pcm_runtime *fe = substream->private_data;
1193 int stream = substream->stream;
1194
1195 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1196
1197 /* shutdown the BEs */
1198 dpcm_be_dai_shutdown(fe, substream->stream);
1199
1200 dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
1201
1202 /* now shutdown the frontend */
1203 soc_pcm_close(substream);
1204
1205 /* run the stream event for each BE */
1206 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1207
1208 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1209 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1210 return 0;
1211}
1212
1213static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1214{
1215 struct snd_soc_dpcm *dpcm;
1216
1217 /* only hw_params backends that are either sinks or sources
1218 * to this frontend DAI */
1219 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1220
1221 struct snd_soc_pcm_runtime *be = dpcm->be;
1222 struct snd_pcm_substream *be_substream =
1223 snd_soc_dpcm_get_substream(be, stream);
1224
1225 /* is this op for this BE ? */
1226 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1227 continue;
1228
1229 /* only free hw when no longer used - check all FEs */
1230 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1231 continue;
1232
1233 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1234 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1235 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1236 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1237 continue;
1238
1239 dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
1240 dpcm->fe->dai_link->name);
1241
1242 soc_pcm_hw_free(be_substream);
1243
1244 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1245 }
1246
1247 return 0;
1248}
1249
1250int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1251{
1252 struct snd_soc_pcm_runtime *fe = substream->private_data;
1253 int err, stream = substream->stream;
1254
1255 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1256 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1257
1258 dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
1259
1260 /* call hw_free on the frontend */
1261 err = soc_pcm_hw_free(substream);
1262 if (err < 0)
1263 dev_err(fe->dev,"dpcm: hw_free FE %s failed\n",
1264 fe->dai_link->name);
1265
1266 /* only hw_params backends that are either sinks or sources
1267 * to this frontend DAI */
1268 err = dpcm_be_dai_hw_free(fe, stream);
1269
1270 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1271 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1272
1273 mutex_unlock(&fe->card->mutex);
1274 return 0;
1275}
1276
1277static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1278{
1279 struct snd_soc_dpcm *dpcm;
1280 int ret;
1281
1282 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1283
1284 struct snd_soc_pcm_runtime *be = dpcm->be;
1285 struct snd_pcm_substream *be_substream =
1286 snd_soc_dpcm_get_substream(be, stream);
1287
1288 /* is this op for this BE ? */
1289 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1290 continue;
1291
1292 /* only allow hw_params() if no connected FEs are running */
1293 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1294 continue;
1295
1296 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1297 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1298 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1299 continue;
1300
1301 dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
1302 dpcm->fe->dai_link->name);
1303
1304 /* copy params for each dpcm */
1305 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1306 sizeof(struct snd_pcm_hw_params));
1307
1308 /* perform any hw_params fixups */
1309 if (be->dai_link->be_hw_params_fixup) {
1310 ret = be->dai_link->be_hw_params_fixup(be,
1311 &dpcm->hw_params);
1312 if (ret < 0) {
1313 dev_err(be->dev,
1314 "dpcm: hw_params BE fixup failed %d\n",
1315 ret);
1316 goto unwind;
1317 }
1318 }
1319
1320 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1321 if (ret < 0) {
1322 dev_err(dpcm->be->dev,
1323 "dpcm: hw_params BE failed %d\n", ret);
1324 goto unwind;
1325 }
1326
1327 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1328 }
1329 return 0;
1330
1331unwind:
1332 /* disable any enabled and non active backends */
1333 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1334 struct snd_soc_pcm_runtime *be = dpcm->be;
1335 struct snd_pcm_substream *be_substream =
1336 snd_soc_dpcm_get_substream(be, stream);
1337
1338 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1339 continue;
1340
1341 /* only allow hw_free() if no connected FEs are running */
1342 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1343 continue;
1344
1345 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1346 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1347 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1348 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1349 continue;
1350
1351 soc_pcm_hw_free(be_substream);
1352 }
1353
1354 return ret;
1355}
1356
1357int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1358 struct snd_pcm_hw_params *params)
1359{
1360 struct snd_soc_pcm_runtime *fe = substream->private_data;
1361 int ret, stream = substream->stream;
1362
1363 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1364 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1365
1366 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1367 sizeof(struct snd_pcm_hw_params));
1368 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1369 if (ret < 0) {
1370 dev_err(fe->dev,"dpcm: hw_params BE failed %d\n", ret);
1371 goto out;
1372 }
1373
1374 dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
1375 fe->dai_link->name, params_rate(params),
1376 params_channels(params), params_format(params));
1377
1378 /* call hw_params on the frontend */
1379 ret = soc_pcm_hw_params(substream, params);
1380 if (ret < 0) {
1381 dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
1382 dpcm_be_dai_hw_free(fe, stream);
1383 } else
1384 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1385
1386out:
1387 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1388 mutex_unlock(&fe->card->mutex);
1389 return ret;
1390}
1391
1392static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1393 struct snd_pcm_substream *substream, int cmd)
1394{
1395 int ret;
1396
1397 dev_dbg(dpcm->be->dev, "dpcm: trigger BE %s cmd %d\n",
1398 dpcm->fe->dai_link->name, cmd);
1399
1400 ret = soc_pcm_trigger(substream, cmd);
1401 if (ret < 0)
1402 dev_err(dpcm->be->dev,"dpcm: trigger BE failed %d\n", ret);
1403
1404 return ret;
1405}
1406
1407int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd)
1408{
1409 struct snd_soc_dpcm *dpcm;
1410 int ret = 0;
1411
1412 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1413
1414 struct snd_soc_pcm_runtime *be = dpcm->be;
1415 struct snd_pcm_substream *be_substream =
1416 snd_soc_dpcm_get_substream(be, stream);
1417
1418 /* is this op for this BE ? */
1419 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1420 continue;
1421
1422 switch (cmd) {
1423 case SNDRV_PCM_TRIGGER_START:
1424 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1425 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1426 continue;
1427
1428 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1429 if (ret)
1430 return ret;
1431
1432 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1433 break;
1434 case SNDRV_PCM_TRIGGER_RESUME:
1435 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1436 continue;
1437
1438 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1439 if (ret)
1440 return ret;
1441
1442 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1443 break;
1444 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1445 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1446 continue;
1447
1448 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1449 if (ret)
1450 return ret;
1451
1452 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1453 break;
1454 case SNDRV_PCM_TRIGGER_STOP:
1455 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1456 continue;
1457
1458 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1459 continue;
1460
1461 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1462 if (ret)
1463 return ret;
1464
1465 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1466 break;
1467 case SNDRV_PCM_TRIGGER_SUSPEND:
1468 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1469 continue;
1470
1471 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1472 continue;
1473
1474 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1475 if (ret)
1476 return ret;
1477
1478 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1479 break;
1480 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1481 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1482 continue;
1483
1484 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1485 continue;
1486
1487 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1488 if (ret)
1489 return ret;
1490
1491 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1492 break;
1493 }
1494 }
1495
1496 return ret;
1497}
1498EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1499
1500int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1501{
1502 struct snd_soc_pcm_runtime *fe = substream->private_data;
1503 int stream = substream->stream, ret;
1504 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1505
1506 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1507
1508 switch (trigger) {
1509 case SND_SOC_DPCM_TRIGGER_PRE:
1510 /* call trigger on the frontend before the backend. */
1511
1512 dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
1513 fe->dai_link->name, cmd);
1514
1515 ret = soc_pcm_trigger(substream, cmd);
1516 if (ret < 0) {
1517 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1518 goto out;
1519 }
1520
1521 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1522 break;
1523 case SND_SOC_DPCM_TRIGGER_POST:
1524 /* call trigger on the frontend after the backend. */
1525
1526 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1527 if (ret < 0) {
1528 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1529 goto out;
1530 }
1531
1532 dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
1533 fe->dai_link->name, cmd);
1534
1535 ret = soc_pcm_trigger(substream, cmd);
1536 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001537 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1538 /* bespoke trigger() - handles both FE and BEs */
1539
1540 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd %d\n",
1541 fe->dai_link->name, cmd);
1542
1543 ret = soc_pcm_bespoke_trigger(substream, cmd);
1544 if (ret < 0) {
1545 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1546 goto out;
1547 }
1548 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001549 default:
1550 dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
1551 fe->dai_link->name);
1552 ret = -EINVAL;
1553 goto out;
1554 }
1555
1556 switch (cmd) {
1557 case SNDRV_PCM_TRIGGER_START:
1558 case SNDRV_PCM_TRIGGER_RESUME:
1559 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1560 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1561 break;
1562 case SNDRV_PCM_TRIGGER_STOP:
1563 case SNDRV_PCM_TRIGGER_SUSPEND:
1564 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1565 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1566 break;
1567 }
1568
1569out:
1570 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1571 return ret;
1572}
1573
1574static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1575{
1576 struct snd_soc_dpcm *dpcm;
1577 int ret = 0;
1578
1579 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1580
1581 struct snd_soc_pcm_runtime *be = dpcm->be;
1582 struct snd_pcm_substream *be_substream =
1583 snd_soc_dpcm_get_substream(be, stream);
1584
1585 /* is this op for this BE ? */
1586 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1587 continue;
1588
1589 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1590 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1591 continue;
1592
1593 dev_dbg(be->dev, "dpcm: prepare BE %s\n",
1594 dpcm->fe->dai_link->name);
1595
1596 ret = soc_pcm_prepare(be_substream);
1597 if (ret < 0) {
1598 dev_err(be->dev, "dpcm: backend prepare failed %d\n",
1599 ret);
1600 break;
1601 }
1602
1603 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1604 }
1605 return ret;
1606}
1607
1608int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1609{
1610 struct snd_soc_pcm_runtime *fe = substream->private_data;
1611 int stream = substream->stream, ret = 0;
1612
1613 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1614
1615 dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
1616
1617 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1618
1619 /* there is no point preparing this FE if there are no BEs */
1620 if (list_empty(&fe->dpcm[stream].be_clients)) {
1621 dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
1622 fe->dai_link->name);
1623 ret = -EINVAL;
1624 goto out;
1625 }
1626
1627 ret = dpcm_be_dai_prepare(fe, substream->stream);
1628 if (ret < 0)
1629 goto out;
1630
1631 /* call prepare on the frontend */
1632 ret = soc_pcm_prepare(substream);
1633 if (ret < 0) {
1634 dev_err(fe->dev,"dpcm: prepare FE %s failed\n",
1635 fe->dai_link->name);
1636 goto out;
1637 }
1638
1639 /* run the stream event for each BE */
1640 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1641 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1642
1643out:
1644 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1645 mutex_unlock(&fe->card->mutex);
1646
1647 return ret;
1648}
1649
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001650static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1651 unsigned int cmd, void *arg)
1652{
1653 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1654 struct snd_soc_platform *platform = rtd->platform;
1655
1656 if (platform->driver->ops->ioctl)
1657 return platform->driver->ops->ioctl(substream, cmd, arg);
1658 return snd_pcm_lib_ioctl(substream, cmd, arg);
1659}
1660
Liam Girdwood618dae12012-04-25 12:12:51 +01001661static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1662{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001663 struct snd_pcm_substream *substream =
1664 snd_soc_dpcm_get_substream(fe, stream);
1665 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001666 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001667
Liam Girdwood618dae12012-04-25 12:12:51 +01001668 dev_dbg(fe->dev, "runtime %s close on FE %s\n",
1669 stream ? "capture" : "playback", fe->dai_link->name);
1670
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001671 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1672 /* call bespoke trigger - FE takes care of all BE triggers */
1673 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd stop\n",
1674 fe->dai_link->name);
1675
1676 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1677 if (err < 0)
1678 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1679 } else {
1680 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd stop\n",
1681 fe->dai_link->name);
1682
1683 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1684 if (err < 0)
1685 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1686 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001687
1688 err = dpcm_be_dai_hw_free(fe, stream);
1689 if (err < 0)
1690 dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
1691
1692 err = dpcm_be_dai_shutdown(fe, stream);
1693 if (err < 0)
1694 dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
1695
1696 /* run the stream event for each BE */
1697 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1698
1699 return 0;
1700}
1701
1702static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1703{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001704 struct snd_pcm_substream *substream =
1705 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001706 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001707 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001708 int ret;
1709
1710 dev_dbg(fe->dev, "runtime %s open on FE %s\n",
1711 stream ? "capture" : "playback", fe->dai_link->name);
1712
1713 /* Only start the BE if the FE is ready */
1714 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1715 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1716 return -EINVAL;
1717
1718 /* startup must always be called for new BEs */
1719 ret = dpcm_be_dai_startup(fe, stream);
1720 if (ret < 0) {
1721 goto disconnect;
1722 return ret;
1723 }
1724
1725 /* keep going if FE state is > open */
1726 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1727 return 0;
1728
1729 ret = dpcm_be_dai_hw_params(fe, stream);
1730 if (ret < 0) {
1731 goto close;
1732 return ret;
1733 }
1734
1735 /* keep going if FE state is > hw_params */
1736 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1737 return 0;
1738
1739
1740 ret = dpcm_be_dai_prepare(fe, stream);
1741 if (ret < 0) {
1742 goto hw_free;
1743 return ret;
1744 }
1745
1746 /* run the stream event for each BE */
1747 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1748
1749 /* keep going if FE state is > prepare */
1750 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1751 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1752 return 0;
1753
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001754 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1755 /* call trigger on the frontend - FE takes care of all BE triggers */
1756 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd start\n",
1757 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001758
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001759 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1760 if (ret < 0) {
1761 dev_err(fe->dev,"dpcm: bespoke trigger FE failed %d\n", ret);
1762 goto hw_free;
1763 }
1764 } else {
1765 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
1766 fe->dai_link->name);
1767
1768 ret = dpcm_be_dai_trigger(fe, stream,
1769 SNDRV_PCM_TRIGGER_START);
1770 if (ret < 0) {
1771 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1772 goto hw_free;
1773 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001774 }
1775
1776 return 0;
1777
1778hw_free:
1779 dpcm_be_dai_hw_free(fe, stream);
1780close:
1781 dpcm_be_dai_shutdown(fe, stream);
1782disconnect:
1783 /* disconnect any non started BEs */
1784 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1785 struct snd_soc_pcm_runtime *be = dpcm->be;
1786 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1787 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1788 }
1789
1790 return ret;
1791}
1792
1793static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1794{
1795 int ret;
1796
1797 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1798 ret = dpcm_run_update_startup(fe, stream);
1799 if (ret < 0)
1800 dev_err(fe->dev, "failed to startup some BEs\n");
1801 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1802
1803 return ret;
1804}
1805
1806static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1807{
1808 int ret;
1809
1810 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1811 ret = dpcm_run_update_shutdown(fe, stream);
1812 if (ret < 0)
1813 dev_err(fe->dev, "failed to shutdown some BEs\n");
1814 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1815
1816 return ret;
1817}
1818
1819/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1820 * any DAI links.
1821 */
1822int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1823{
1824 struct snd_soc_card *card;
1825 int i, old, new, paths;
1826
1827 if (widget->codec)
1828 card = widget->codec->card;
1829 else if (widget->platform)
1830 card = widget->platform->card;
1831 else
1832 return -EINVAL;
1833
1834 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1835 for (i = 0; i < card->num_rtd; i++) {
1836 struct snd_soc_dapm_widget_list *list;
1837 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1838
1839 /* make sure link is FE */
1840 if (!fe->dai_link->dynamic)
1841 continue;
1842
1843 /* only check active links */
1844 if (!fe->cpu_dai->active)
1845 continue;
1846
1847 /* DAPM sync will call this to update DSP paths */
1848 dev_dbg(fe->dev, "DPCM runtime update for FE %s\n",
1849 fe->dai_link->name);
1850
1851 /* skip if FE doesn't have playback capability */
1852 if (!fe->cpu_dai->driver->playback.channels_min)
1853 goto capture;
1854
1855 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1856 if (paths < 0) {
1857 dev_warn(fe->dev, "%s no valid %s path\n",
1858 fe->dai_link->name, "playback");
1859 mutex_unlock(&card->mutex);
1860 return paths;
1861 }
1862
1863 /* update any new playback paths */
1864 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1865 if (new) {
1866 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1867 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1868 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1869 }
1870
1871 /* update any old playback paths */
1872 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1873 if (old) {
1874 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1875 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1876 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1877 }
1878
1879capture:
1880 /* skip if FE doesn't have capture capability */
1881 if (!fe->cpu_dai->driver->capture.channels_min)
1882 continue;
1883
1884 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1885 if (paths < 0) {
1886 dev_warn(fe->dev, "%s no valid %s path\n",
1887 fe->dai_link->name, "capture");
1888 mutex_unlock(&card->mutex);
1889 return paths;
1890 }
1891
1892 /* update any new capture paths */
1893 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1894 if (new) {
1895 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1896 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1897 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1898 }
1899
1900 /* update any old capture paths */
1901 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1902 if (old) {
1903 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1904 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1905 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1906 }
1907
1908 dpcm_path_put(&list);
1909 }
1910
1911 mutex_unlock(&card->mutex);
1912 return 0;
1913}
Liam Girdwood01d75842012-04-25 12:12:49 +01001914int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1915{
1916 struct snd_soc_dpcm *dpcm;
1917 struct list_head *clients =
1918 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1919
1920 list_for_each_entry(dpcm, clients, list_be) {
1921
1922 struct snd_soc_pcm_runtime *be = dpcm->be;
1923 struct snd_soc_dai *dai = be->codec_dai;
1924 struct snd_soc_dai_driver *drv = dai->driver;
1925
1926 if (be->dai_link->ignore_suspend)
1927 continue;
1928
1929 dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
1930
1931 if (drv->ops->digital_mute && dai->playback_active)
1932 drv->ops->digital_mute(dai, mute);
1933 }
1934
1935 return 0;
1936}
1937
1938int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1939{
1940 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1941 struct snd_soc_dpcm *dpcm;
1942 struct snd_soc_dapm_widget_list *list;
1943 int ret;
1944 int stream = fe_substream->stream;
1945
1946 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1947 fe->dpcm[stream].runtime = fe_substream->runtime;
1948
1949 if (dpcm_path_get(fe, stream, &list) <= 0) {
1950 dev_warn(fe->dev, "asoc: %s no valid %s route\n",
1951 fe->dai_link->name, stream ? "capture" : "playback");
1952 mutex_unlock(&fe->card->mutex);
1953 return -EINVAL;
1954 }
1955
1956 /* calculate valid and active FE <-> BE dpcms */
1957 dpcm_process_paths(fe, stream, &list, 1);
1958
1959 ret = dpcm_fe_dai_startup(fe_substream);
1960 if (ret < 0) {
1961 /* clean up all links */
1962 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1963 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1964
1965 dpcm_be_disconnect(fe, stream);
1966 fe->dpcm[stream].runtime = NULL;
1967 }
1968
1969 dpcm_clear_pending_state(fe, stream);
1970 dpcm_path_put(&list);
1971 mutex_unlock(&fe->card->mutex);
1972 return ret;
1973}
1974
1975int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1976{
1977 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1978 struct snd_soc_dpcm *dpcm;
1979 int stream = fe_substream->stream, ret;
1980
1981 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1982 ret = dpcm_fe_dai_shutdown(fe_substream);
1983
1984 /* mark FE's links ready to prune */
1985 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1986 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1987
1988 dpcm_be_disconnect(fe, stream);
1989
1990 fe->dpcm[stream].runtime = NULL;
1991 mutex_unlock(&fe->card->mutex);
1992 return ret;
1993}
1994
Liam Girdwoodddee6272011-06-09 14:45:53 +01001995/* create a new pcm */
1996int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1997{
1998 struct snd_soc_codec *codec = rtd->codec;
1999 struct snd_soc_platform *platform = rtd->platform;
2000 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2001 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2002 struct snd_pcm *pcm;
2003 char new_name[64];
2004 int ret = 0, playback = 0, capture = 0;
2005
Liam Girdwood01d75842012-04-25 12:12:49 +01002006 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2007 if (cpu_dai->driver->playback.channels_min)
2008 playback = 1;
2009 if (cpu_dai->driver->capture.channels_min)
2010 capture = 1;
2011 } else {
2012 if (codec_dai->driver->playback.channels_min)
2013 playback = 1;
2014 if (codec_dai->driver->capture.channels_min)
2015 capture = 1;
2016 }
Sangsu Parka5002312012-01-02 17:15:10 +09002017
Liam Girdwood01d75842012-04-25 12:12:49 +01002018 /* create the PCM */
2019 if (rtd->dai_link->no_pcm) {
2020 snprintf(new_name, sizeof(new_name), "(%s)",
2021 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002022
Liam Girdwood01d75842012-04-25 12:12:49 +01002023 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2024 playback, capture, &pcm);
2025 } else {
2026 if (rtd->dai_link->dynamic)
2027 snprintf(new_name, sizeof(new_name), "%s (*)",
2028 rtd->dai_link->stream_name);
2029 else
2030 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2031 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002032
Liam Girdwood01d75842012-04-25 12:12:49 +01002033 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2034 capture, &pcm);
2035 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002036 if (ret < 0) {
2037 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2038 return ret;
2039 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002040 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002041
2042 /* DAPM dai link stream work */
2043 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2044
2045 rtd->pcm = pcm;
2046 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002047
2048 if (rtd->dai_link->no_pcm) {
2049 if (playback)
2050 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2051 if (capture)
2052 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2053 goto out;
2054 }
2055
2056 /* ASoC PCM operations */
2057 if (rtd->dai_link->dynamic) {
2058 rtd->ops.open = dpcm_fe_dai_open;
2059 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2060 rtd->ops.prepare = dpcm_fe_dai_prepare;
2061 rtd->ops.trigger = dpcm_fe_dai_trigger;
2062 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2063 rtd->ops.close = dpcm_fe_dai_close;
2064 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002065 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002066 } else {
2067 rtd->ops.open = soc_pcm_open;
2068 rtd->ops.hw_params = soc_pcm_hw_params;
2069 rtd->ops.prepare = soc_pcm_prepare;
2070 rtd->ops.trigger = soc_pcm_trigger;
2071 rtd->ops.hw_free = soc_pcm_hw_free;
2072 rtd->ops.close = soc_pcm_close;
2073 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002074 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002075 }
2076
Liam Girdwoodddee6272011-06-09 14:45:53 +01002077 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002078 rtd->ops.ack = platform->driver->ops->ack;
2079 rtd->ops.copy = platform->driver->ops->copy;
2080 rtd->ops.silence = platform->driver->ops->silence;
2081 rtd->ops.page = platform->driver->ops->page;
2082 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002083 }
2084
2085 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002086 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002087
2088 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002089 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002090
2091 if (platform->driver->pcm_new) {
2092 ret = platform->driver->pcm_new(rtd);
2093 if (ret < 0) {
2094 pr_err("asoc: platform pcm constructor failed\n");
2095 return ret;
2096 }
2097 }
2098
2099 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002100out:
Liam Girdwoodddee6272011-06-09 14:45:53 +01002101 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2102 cpu_dai->name);
2103 return ret;
2104}
Liam Girdwood01d75842012-04-25 12:12:49 +01002105
2106/* is the current PCM operation for this FE ? */
2107int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2108{
2109 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2110 return 1;
2111 return 0;
2112}
2113EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2114
2115/* is the current PCM operation for this BE ? */
2116int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2117 struct snd_soc_pcm_runtime *be, int stream)
2118{
2119 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2120 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2121 be->dpcm[stream].runtime_update))
2122 return 1;
2123 return 0;
2124}
2125EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2126
2127/* get the substream for this BE */
2128struct snd_pcm_substream *
2129 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2130{
2131 return be->pcm->streams[stream].substream;
2132}
2133EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2134
2135/* get the BE runtime state */
2136enum snd_soc_dpcm_state
2137 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2138{
2139 return be->dpcm[stream].state;
2140}
2141EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2142
2143/* set the BE runtime state */
2144void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2145 int stream, enum snd_soc_dpcm_state state)
2146{
2147 be->dpcm[stream].state = state;
2148}
2149EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2150
2151/*
2152 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2153 * are not running, paused or suspended for the specified stream direction.
2154 */
2155int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2156 struct snd_soc_pcm_runtime *be, int stream)
2157{
2158 struct snd_soc_dpcm *dpcm;
2159 int state;
2160
2161 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2162
2163 if (dpcm->fe == fe)
2164 continue;
2165
2166 state = dpcm->fe->dpcm[stream].state;
2167 if (state == SND_SOC_DPCM_STATE_START ||
2168 state == SND_SOC_DPCM_STATE_PAUSED ||
2169 state == SND_SOC_DPCM_STATE_SUSPEND)
2170 return 0;
2171 }
2172
2173 /* it's safe to free/stop this BE DAI */
2174 return 1;
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2177
2178/*
2179 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2180 * running, paused or suspended for the specified stream direction.
2181 */
2182int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2183 struct snd_soc_pcm_runtime *be, int stream)
2184{
2185 struct snd_soc_dpcm *dpcm;
2186 int state;
2187
2188 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2189
2190 if (dpcm->fe == fe)
2191 continue;
2192
2193 state = dpcm->fe->dpcm[stream].state;
2194 if (state == SND_SOC_DPCM_STATE_START ||
2195 state == SND_SOC_DPCM_STATE_PAUSED ||
2196 state == SND_SOC_DPCM_STATE_SUSPEND ||
2197 state == SND_SOC_DPCM_STATE_PREPARE)
2198 return 0;
2199 }
2200
2201 /* it's safe to change hw_params */
2202 return 1;
2203}
2204EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002205
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002206int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2207 int cmd, struct snd_soc_platform *platform)
2208{
2209 if (platform->driver->ops->trigger)
2210 return platform->driver->ops->trigger(substream, cmd);
2211 return 0;
2212}
2213EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2214
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002215#ifdef CONFIG_DEBUG_FS
2216static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2217{
2218 switch (state) {
2219 case SND_SOC_DPCM_STATE_NEW:
2220 return "new";
2221 case SND_SOC_DPCM_STATE_OPEN:
2222 return "open";
2223 case SND_SOC_DPCM_STATE_HW_PARAMS:
2224 return "hw_params";
2225 case SND_SOC_DPCM_STATE_PREPARE:
2226 return "prepare";
2227 case SND_SOC_DPCM_STATE_START:
2228 return "start";
2229 case SND_SOC_DPCM_STATE_STOP:
2230 return "stop";
2231 case SND_SOC_DPCM_STATE_SUSPEND:
2232 return "suspend";
2233 case SND_SOC_DPCM_STATE_PAUSED:
2234 return "paused";
2235 case SND_SOC_DPCM_STATE_HW_FREE:
2236 return "hw_free";
2237 case SND_SOC_DPCM_STATE_CLOSE:
2238 return "close";
2239 }
2240
2241 return "unknown";
2242}
2243
2244static int dpcm_state_open_file(struct inode *inode, struct file *file)
2245{
2246 file->private_data = inode->i_private;
2247 return 0;
2248}
2249
2250static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2251 int stream, char *buf, size_t size)
2252{
2253 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2254 struct snd_soc_dpcm *dpcm;
2255 ssize_t offset = 0;
2256
2257 /* FE state */
2258 offset += snprintf(buf + offset, size - offset,
2259 "[%s - %s]\n", fe->dai_link->name,
2260 stream ? "Capture" : "Playback");
2261
2262 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2263 dpcm_state_string(fe->dpcm[stream].state));
2264
2265 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2266 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2267 offset += snprintf(buf + offset, size - offset,
2268 "Hardware Params: "
2269 "Format = %s, Channels = %d, Rate = %d\n",
2270 snd_pcm_format_name(params_format(params)),
2271 params_channels(params),
2272 params_rate(params));
2273
2274 /* BEs state */
2275 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2276
2277 if (list_empty(&fe->dpcm[stream].be_clients)) {
2278 offset += snprintf(buf + offset, size - offset,
2279 " No active DSP links\n");
2280 goto out;
2281 }
2282
2283 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2284 struct snd_soc_pcm_runtime *be = dpcm->be;
2285 params = &dpcm->hw_params;
2286
2287 offset += snprintf(buf + offset, size - offset,
2288 "- %s\n", be->dai_link->name);
2289
2290 offset += snprintf(buf + offset, size - offset,
2291 " State: %s\n",
2292 dpcm_state_string(be->dpcm[stream].state));
2293
2294 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2295 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2296 offset += snprintf(buf + offset, size - offset,
2297 " Hardware Params: "
2298 "Format = %s, Channels = %d, Rate = %d\n",
2299 snd_pcm_format_name(params_format(params)),
2300 params_channels(params),
2301 params_rate(params));
2302 }
2303
2304out:
2305 return offset;
2306}
2307
2308static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2309 size_t count, loff_t *ppos)
2310{
2311 struct snd_soc_pcm_runtime *fe = file->private_data;
2312 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2313 char *buf;
2314
2315 buf = kmalloc(out_count, GFP_KERNEL);
2316 if (!buf)
2317 return -ENOMEM;
2318
2319 if (fe->cpu_dai->driver->playback.channels_min)
2320 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2321 buf + offset, out_count - offset);
2322
2323 if (fe->cpu_dai->driver->capture.channels_min)
2324 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2325 buf + offset, out_count - offset);
2326
2327 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2328
2329 kfree(buf);
2330
2331 return ret;
2332}
2333
2334static const struct file_operations dpcm_state_fops = {
2335 .open = dpcm_state_open_file,
2336 .read = dpcm_state_read_file,
2337 .llseek = default_llseek,
2338};
2339
2340int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2341{
2342 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2343 rtd->card->debugfs_card_root);
2344 if (!rtd->debugfs_dpcm_root) {
2345 dev_dbg(rtd->dev,
2346 "ASoC: Failed to create dpcm debugfs directory %s\n",
2347 rtd->dai_link->name);
2348 return -EINVAL;
2349 }
2350
2351 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644,
2352 rtd->debugfs_dpcm_root,
2353 rtd, &dpcm_state_fops);
2354
2355 return 0;
2356}
2357#endif