2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dpcm.h>
32 #include <sound/initval.h>
34 #define DPCM_MAX_BE_USERS 8
36 /* DPCM stream event, send event to FE and all active BEs. */
37 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
40 struct snd_soc_dpcm *dpcm;
42 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
44 struct snd_soc_pcm_runtime *be = dpcm->be;
46 dev_dbg(be->dev, "pm: BE %s event %d dir %d\n",
47 be->dai_link->name, event, dir);
49 snd_soc_dapm_stream_event(be, dir, event);
52 snd_soc_dapm_stream_event(fe, dir, event);
57 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
58 struct snd_soc_dai *soc_dai)
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
63 if (!soc_dai->driver->symmetric_rates &&
64 !rtd->dai_link->symmetric_rates)
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.
72 dev_warn(soc_dai->dev,
73 "Not enforcing symmetric_rates due to race\n");
77 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
79 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
80 SNDRV_PCM_HW_PARAM_RATE,
81 soc_dai->rate, soc_dai->rate);
84 "Unable to apply rate symmetry constraint: %d\n", ret);
92 * 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.
96 static int sample_sizes[] = {
100 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
101 struct snd_soc_dai *dai)
105 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
106 bits = dai->driver->playback.sig_bits;
108 bits = dai->driver->capture.sig_bits;
113 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
114 if (bits >= sample_sizes[i])
117 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
118 sample_sizes[i], bits);
121 "Failed to set MSB %d/%d: %d\n",
122 bits, sample_sizes[i], ret);
127 * 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.
131 static int soc_pcm_open(struct snd_pcm_substream *substream)
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;
142 pm_runtime_get_sync(cpu_dai->dev);
143 pm_runtime_get_sync(codec_dai->dev);
144 pm_runtime_get_sync(platform->dev);
146 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
148 /* startup the audio subsystem */
149 if (cpu_dai->driver->ops->startup) {
150 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
152 dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
158 if (platform->driver->ops && platform->driver->ops->open) {
159 ret = platform->driver->ops->open(substream);
161 dev_err(platform->dev, "can't open platform %s: %d\n",
162 platform->name, ret);
167 if (codec_dai->driver->ops->startup) {
168 ret = codec_dai->driver->ops->startup(substream, codec_dai);
170 dev_err(codec_dai->dev, "can't open codec %s: %d\n",
171 codec_dai->name, ret);
176 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
177 ret = rtd->dai_link->ops->startup(substream);
179 pr_err("asoc: %s startup failed: %d\n",
180 rtd->dai_link->name, ret);
185 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
186 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
189 /* 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;
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;
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;
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;
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);
245 if (!runtime->hw.formats) {
246 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
247 codec_dai->name, cpu_dai->name);
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);
257 soc_pcm_apply_msb(substream, codec_dai);
258 soc_pcm_apply_msb(substream, cpu_dai);
260 /* Symmetry only applies if we've already got an active stream. */
261 if (cpu_dai->active) {
262 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
267 if (codec_dai->active) {
268 ret = soc_pcm_apply_symmetry(substream, codec_dai);
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);
282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
283 cpu_dai->playback_active++;
284 codec_dai->playback_active++;
286 cpu_dai->capture_active++;
287 codec_dai->capture_active++;
291 rtd->codec->active++;
292 mutex_unlock(&rtd->pcm_mutex);
296 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
297 rtd->dai_link->ops->shutdown(substream);
300 if (codec_dai->driver->ops->shutdown)
301 codec_dai->driver->ops->shutdown(substream, codec_dai);
304 if (platform->driver->ops && platform->driver->ops->close)
305 platform->driver->ops->close(substream);
308 if (cpu_dai->driver->ops->shutdown)
309 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
311 mutex_unlock(&rtd->pcm_mutex);
313 pm_runtime_put(platform->dev);
314 pm_runtime_put(codec_dai->dev);
315 pm_runtime_put(cpu_dai->dev);
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.
325 static void close_delayed_work(struct work_struct *work)
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;
331 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
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");
338 /* are we waiting on this codec DAI stream */
339 if (codec_dai->pop_wait == 1) {
340 codec_dai->pop_wait = 0;
341 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
342 SND_SOC_DAPM_STREAM_STOP);
345 mutex_unlock(&rtd->pcm_mutex);
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
353 static int soc_pcm_close(struct snd_pcm_substream *substream)
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;
361 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
364 cpu_dai->playback_active--;
365 codec_dai->playback_active--;
367 cpu_dai->capture_active--;
368 codec_dai->capture_active--;
375 /* clear the corresponding DAIs rate when inactive */
376 if (!cpu_dai->active)
379 if (!codec_dai->active)
382 /* Muting the DAC suppresses artifacts caused during digital
383 * shutdown, for example from stopping clocks.
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 snd_soc_dai_digital_mute(codec_dai, 1);
388 if (cpu_dai->driver->ops->shutdown)
389 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
391 if (codec_dai->driver->ops->shutdown)
392 codec_dai->driver->ops->shutdown(substream, codec_dai);
394 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
395 rtd->dai_link->ops->shutdown(substream);
397 if (platform->driver->ops && platform->driver->ops->close)
398 platform->driver->ops->close(substream);
399 cpu_dai->runtime = NULL;
401 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
402 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
403 rtd->dai_link->ignore_pmdown_time) {
404 /* powered down playback stream now */
405 snd_soc_dapm_stream_event(rtd,
406 SNDRV_PCM_STREAM_PLAYBACK,
407 SND_SOC_DAPM_STREAM_STOP);
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));
415 /* capture streams can be powered down now */
416 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
417 SND_SOC_DAPM_STREAM_STOP);
420 mutex_unlock(&rtd->pcm_mutex);
422 pm_runtime_put(platform->dev);
423 pm_runtime_put(codec_dai->dev);
424 pm_runtime_put(cpu_dai->dev);
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.
434 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
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;
442 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
444 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
445 ret = rtd->dai_link->ops->prepare(substream);
447 pr_err("asoc: machine prepare error: %d\n", ret);
452 if (platform->driver->ops && platform->driver->ops->prepare) {
453 ret = platform->driver->ops->prepare(substream);
455 dev_err(platform->dev, "platform prepare error: %d\n",
461 if (codec_dai->driver->ops->prepare) {
462 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
464 dev_err(codec_dai->dev, "DAI prepare error: %d\n",
470 if (cpu_dai->driver->ops->prepare) {
471 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
473 dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
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);
486 snd_soc_dapm_stream_event(rtd, substream->stream,
487 SND_SOC_DAPM_STREAM_START);
489 snd_soc_dai_digital_mute(codec_dai, 0);
492 mutex_unlock(&rtd->pcm_mutex);
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.
501 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
502 struct snd_pcm_hw_params *params)
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;
510 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
512 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
513 ret = rtd->dai_link->ops->hw_params(substream, params);
515 pr_err("asoc: machine hw_params failed: %d\n", ret);
520 if (codec_dai->driver->ops->hw_params) {
521 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
523 dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
524 codec_dai->name, ret);
529 if (cpu_dai->driver->ops->hw_params) {
530 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
532 dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
538 if (platform->driver->ops && platform->driver->ops->hw_params) {
539 ret = platform->driver->ops->hw_params(substream, params);
541 dev_err(platform->dev, "%s hw params failed: %d\n",
542 platform->name, ret);
547 /* store the rate for each DAIs */
548 cpu_dai->rate = params_rate(params);
549 codec_dai->rate = params_rate(params);
552 mutex_unlock(&rtd->pcm_mutex);
556 if (cpu_dai->driver->ops->hw_free)
557 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
560 if (codec_dai->driver->ops->hw_free)
561 codec_dai->driver->ops->hw_free(substream, codec_dai);
564 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
565 rtd->dai_link->ops->hw_free(substream);
567 mutex_unlock(&rtd->pcm_mutex);
572 * Frees resources allocated by hw_params, can be called multiple times
574 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
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;
582 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
584 /* apply codec digital mute */
586 snd_soc_dai_digital_mute(codec_dai, 1);
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);
592 /* free any DMA resources */
593 if (platform->driver->ops && platform->driver->ops->hw_free)
594 platform->driver->ops->hw_free(substream);
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);
600 if (cpu_dai->driver->ops->hw_free)
601 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
603 mutex_unlock(&rtd->pcm_mutex);
607 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
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;
615 if (codec_dai->driver->ops->trigger) {
616 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
621 if (platform->driver->ops && platform->driver->ops->trigger) {
622 ret = platform->driver->ops->trigger(substream, cmd);
627 if (cpu_dai->driver->ops->trigger) {
628 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
635 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
638 struct snd_soc_pcm_runtime *rtd = substream->private_data;
639 struct snd_soc_platform *platform = rtd->platform;
640 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
644 if (codec_dai->driver->ops->bespoke_trigger) {
645 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
650 if (platform->driver->bespoke_trigger) {
651 ret = platform->driver->bespoke_trigger(substream, cmd);
656 if (cpu_dai->driver->ops->bespoke_trigger) {
657 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
664 * soc level wrapper for pointer callback
665 * If cpu_dai, codec_dai, platform driver has the delay callback, than
666 * the runtime->delay will be updated accordingly.
668 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
670 struct snd_soc_pcm_runtime *rtd = substream->private_data;
671 struct snd_soc_platform *platform = rtd->platform;
672 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
673 struct snd_soc_dai *codec_dai = rtd->codec_dai;
674 struct snd_pcm_runtime *runtime = substream->runtime;
675 snd_pcm_uframes_t offset = 0;
676 snd_pcm_sframes_t delay = 0;
678 if (platform->driver->ops && platform->driver->ops->pointer)
679 offset = platform->driver->ops->pointer(substream);
681 if (cpu_dai->driver->ops->delay)
682 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
684 if (codec_dai->driver->ops->delay)
685 delay += codec_dai->driver->ops->delay(substream, codec_dai);
687 if (platform->driver->delay)
688 delay += platform->driver->delay(substream, codec_dai);
690 runtime->delay = delay;
695 /* connect a FE and BE */
696 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
697 struct snd_soc_pcm_runtime *be, int stream)
699 struct snd_soc_dpcm *dpcm;
701 /* only add new dpcms */
702 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
703 if (dpcm->be == be && dpcm->fe == fe)
707 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
713 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
714 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
715 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
716 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
718 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
719 stream ? "capture" : "playback", fe->dai_link->name,
720 stream ? "<-" : "->", be->dai_link->name);
722 #ifdef CONFIG_DEBUG_FS
723 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
724 fe->debugfs_dpcm_root, &dpcm->state);
729 /* reparent a BE onto another FE */
730 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
731 struct snd_soc_pcm_runtime *be, int stream)
733 struct snd_soc_dpcm *dpcm;
734 struct snd_pcm_substream *fe_substream, *be_substream;
736 /* reparent if BE is connected to other FEs */
737 if (!be->dpcm[stream].users)
740 be_substream = snd_soc_dpcm_get_substream(be, stream);
742 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
746 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
747 stream ? "capture" : "playback",
748 dpcm->fe->dai_link->name,
749 stream ? "<-" : "->", dpcm->be->dai_link->name);
751 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
752 be_substream->runtime = fe_substream->runtime;
757 /* disconnect a BE and FE */
758 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
760 struct snd_soc_dpcm *dpcm, *d;
762 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
763 dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
764 stream ? "capture" : "playback",
765 dpcm->be->dai_link->name);
767 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
770 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
771 stream ? "capture" : "playback", fe->dai_link->name,
772 stream ? "<-" : "->", dpcm->be->dai_link->name);
774 /* BEs still alive need new FE */
775 dpcm_be_reparent(fe, dpcm->be, stream);
777 #ifdef CONFIG_DEBUG_FS
778 debugfs_remove(dpcm->debugfs_state);
780 list_del(&dpcm->list_be);
781 list_del(&dpcm->list_fe);
786 /* get BE for DAI widget and stream */
787 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
788 struct snd_soc_dapm_widget *widget, int stream)
790 struct snd_soc_pcm_runtime *be;
793 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
794 for (i = 0; i < card->num_links; i++) {
797 if (be->cpu_dai->playback_widget == widget ||
798 be->codec_dai->playback_widget == widget)
803 for (i = 0; i < card->num_links; i++) {
806 if (be->cpu_dai->capture_widget == widget ||
807 be->codec_dai->capture_widget == widget)
812 dev_err(card->dev, "can't get %s BE for %s\n",
813 stream ? "capture" : "playback", widget->name);
817 static inline struct snd_soc_dapm_widget *
818 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
820 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
821 return rtd->cpu_dai->playback_widget;
823 return rtd->cpu_dai->capture_widget;
826 static inline struct snd_soc_dapm_widget *
827 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
829 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
830 return rtd->codec_dai->playback_widget;
832 return rtd->codec_dai->capture_widget;
835 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
836 struct snd_soc_dapm_widget *widget)
840 for (i = 0; i < list->num_widgets; i++) {
841 if (widget == list->widgets[i])
848 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
849 int stream, struct snd_soc_dapm_widget_list **list_)
851 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
852 struct snd_soc_dapm_widget_list *list;
855 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
856 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
860 /* get number of valid DAI paths and their widgets */
861 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
863 dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
864 stream ? "capture" : "playback");
870 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
875 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
876 struct snd_soc_dapm_widget_list **list_)
878 struct snd_soc_dpcm *dpcm;
879 struct snd_soc_dapm_widget_list *list = *list_;
880 struct snd_soc_dapm_widget *widget;
883 /* Destroy any old FE <--> BE connections */
884 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
886 /* is there a valid CPU DAI widget for this BE */
887 widget = rtd_get_cpu_widget(dpcm->be, stream);
889 /* prune the BE if it's no longer in our active list */
890 if (widget && widget_in_list(list, widget))
893 /* is there a valid CODEC DAI widget for this BE */
894 widget = rtd_get_codec_widget(dpcm->be, stream);
896 /* prune the BE if it's no longer in our active list */
897 if (widget && widget_in_list(list, widget))
900 dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
901 stream ? "capture" : "playback",
902 dpcm->be->dai_link->name, fe->dai_link->name);
903 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
904 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
908 dev_dbg(fe->dev, "found %d old BE paths for pruning\n", prune);
912 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
913 struct snd_soc_dapm_widget_list **list_)
915 struct snd_soc_card *card = fe->card;
916 struct snd_soc_dapm_widget_list *list = *list_;
917 struct snd_soc_pcm_runtime *be;
920 /* Create any new FE <--> BE connections */
921 for (i = 0; i < list->num_widgets; i++) {
923 if (list->widgets[i]->id != snd_soc_dapm_dai)
926 /* is there a valid BE rtd for this widget */
927 be = dpcm_get_be(card, list->widgets[i], stream);
929 dev_err(fe->dev, "no BE found for %s\n",
930 list->widgets[i]->name);
934 /* make sure BE is a real BE */
935 if (!be->dai_link->no_pcm)
938 /* don't connect if FE is not running */
939 if (!fe->dpcm[stream].runtime)
942 /* newly connected FE and BE */
943 err = dpcm_be_connect(fe, be, stream);
945 dev_err(fe->dev, "can't connect %s\n",
946 list->widgets[i]->name);
948 } else if (err == 0) /* already connected */
952 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
956 dev_dbg(fe->dev, "found %d new BE paths\n", new);
961 * Find the corresponding BE DAIs that source or sink audio to this
964 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
965 int stream, struct snd_soc_dapm_widget_list **list, int new)
968 return dpcm_add_paths(fe, stream, list);
970 return dpcm_prune_paths(fe, stream, list);
973 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
975 struct snd_soc_dpcm *dpcm;
977 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
978 dpcm->be->dpcm[stream].runtime_update =
979 SND_SOC_DPCM_UPDATE_NO;
982 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
985 struct snd_soc_dpcm *dpcm;
987 /* disable any enabled and non active backends */
988 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
990 struct snd_soc_pcm_runtime *be = dpcm->be;
991 struct snd_pcm_substream *be_substream =
992 snd_soc_dpcm_get_substream(be, stream);
994 if (be->dpcm[stream].users == 0)
995 dev_err(be->dev, "no users %s at close - state %d\n",
996 stream ? "capture" : "playback",
997 be->dpcm[stream].state);
999 if (--be->dpcm[stream].users != 0)
1002 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1005 soc_pcm_close(be_substream);
1006 be_substream->runtime = NULL;
1007 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1011 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1013 struct snd_soc_dpcm *dpcm;
1016 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1017 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1019 struct snd_soc_pcm_runtime *be = dpcm->be;
1020 struct snd_pcm_substream *be_substream =
1021 snd_soc_dpcm_get_substream(be, stream);
1023 /* is this op for this BE ? */
1024 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1027 /* first time the dpcm is open ? */
1028 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1029 dev_err(be->dev, "too many users %s at open %d\n",
1030 stream ? "capture" : "playback",
1031 be->dpcm[stream].state);
1033 if (be->dpcm[stream].users++ != 0)
1036 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1037 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1040 dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
1042 be_substream->runtime = be->dpcm[stream].runtime;
1043 err = soc_pcm_open(be_substream);
1045 dev_err(be->dev, "BE open failed %d\n", err);
1046 be->dpcm[stream].users--;
1047 if (be->dpcm[stream].users < 0)
1048 dev_err(be->dev, "no users %s at unwind %d\n",
1049 stream ? "capture" : "playback",
1050 be->dpcm[stream].state);
1052 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1056 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1063 /* disable any enabled and non active backends */
1064 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1065 struct snd_soc_pcm_runtime *be = dpcm->be;
1066 struct snd_pcm_substream *be_substream =
1067 snd_soc_dpcm_get_substream(be, stream);
1069 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1072 if (be->dpcm[stream].users == 0)
1073 dev_err(be->dev, "no users %s at close %d\n",
1074 stream ? "capture" : "playback",
1075 be->dpcm[stream].state);
1077 if (--be->dpcm[stream].users != 0)
1080 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1083 soc_pcm_close(be_substream);
1084 be_substream->runtime = NULL;
1085 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1091 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1095 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1096 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1098 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1099 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1100 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1101 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1102 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1103 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1104 runtime->hw.rates = cpu_dai_drv->playback.rates;
1106 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1107 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1108 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1109 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1110 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1111 runtime->hw.rates = cpu_dai_drv->capture.rates;
1115 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1117 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1118 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1119 int stream = fe_substream->stream, ret = 0;
1121 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1123 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1125 dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
1129 dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
1131 /* start the DAI frontend */
1132 ret = soc_pcm_open(fe_substream);
1134 dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
1138 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1140 dpcm_set_fe_runtime(fe_substream);
1141 snd_pcm_limit_hw_rates(runtime);
1143 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1147 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1149 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1153 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1155 struct snd_soc_dpcm *dpcm;
1157 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1158 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1160 struct snd_soc_pcm_runtime *be = dpcm->be;
1161 struct snd_pcm_substream *be_substream =
1162 snd_soc_dpcm_get_substream(be, stream);
1164 /* is this op for this BE ? */
1165 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1168 if (be->dpcm[stream].users == 0)
1169 dev_err(be->dev, "no users %s at close - state %d\n",
1170 stream ? "capture" : "playback",
1171 be->dpcm[stream].state);
1173 if (--be->dpcm[stream].users != 0)
1176 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1177 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1180 dev_dbg(be->dev, "dpcm: close BE %s\n",
1181 dpcm->fe->dai_link->name);
1183 soc_pcm_close(be_substream);
1184 be_substream->runtime = NULL;
1186 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1191 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1193 struct snd_soc_pcm_runtime *fe = substream->private_data;
1194 int stream = substream->stream;
1196 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1198 /* shutdown the BEs */
1199 dpcm_be_dai_shutdown(fe, substream->stream);
1201 dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
1203 /* now shutdown the frontend */
1204 soc_pcm_close(substream);
1206 /* run the stream event for each BE */
1207 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1209 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1210 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1214 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1216 struct snd_soc_dpcm *dpcm;
1218 /* only hw_params backends that are either sinks or sources
1219 * to this frontend DAI */
1220 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1222 struct snd_soc_pcm_runtime *be = dpcm->be;
1223 struct snd_pcm_substream *be_substream =
1224 snd_soc_dpcm_get_substream(be, stream);
1226 /* is this op for this BE ? */
1227 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1230 /* only free hw when no longer used - check all FEs */
1231 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1234 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1235 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1236 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1237 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1240 dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
1241 dpcm->fe->dai_link->name);
1243 soc_pcm_hw_free(be_substream);
1245 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1251 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1253 struct snd_soc_pcm_runtime *fe = substream->private_data;
1254 int err, stream = substream->stream;
1256 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1257 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1259 dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
1261 /* call hw_free on the frontend */
1262 err = soc_pcm_hw_free(substream);
1264 dev_err(fe->dev,"dpcm: hw_free FE %s failed\n",
1265 fe->dai_link->name);
1267 /* only hw_params backends that are either sinks or sources
1268 * to this frontend DAI */
1269 err = dpcm_be_dai_hw_free(fe, stream);
1271 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1272 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1274 mutex_unlock(&fe->card->mutex);
1278 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1280 struct snd_soc_dpcm *dpcm;
1283 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1285 struct snd_soc_pcm_runtime *be = dpcm->be;
1286 struct snd_pcm_substream *be_substream =
1287 snd_soc_dpcm_get_substream(be, stream);
1289 /* is this op for this BE ? */
1290 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1293 /* only allow hw_params() if no connected FEs are running */
1294 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1297 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1298 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1299 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1302 dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
1303 dpcm->fe->dai_link->name);
1305 /* copy params for each dpcm */
1306 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1307 sizeof(struct snd_pcm_hw_params));
1309 /* perform any hw_params fixups */
1310 if (be->dai_link->be_hw_params_fixup) {
1311 ret = be->dai_link->be_hw_params_fixup(be,
1315 "dpcm: hw_params BE fixup failed %d\n",
1321 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1323 dev_err(dpcm->be->dev,
1324 "dpcm: hw_params BE failed %d\n", ret);
1328 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1333 /* disable any enabled and non active backends */
1334 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1335 struct snd_soc_pcm_runtime *be = dpcm->be;
1336 struct snd_pcm_substream *be_substream =
1337 snd_soc_dpcm_get_substream(be, stream);
1339 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1342 /* only allow hw_free() if no connected FEs are running */
1343 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1346 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1347 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1348 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1349 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1352 soc_pcm_hw_free(be_substream);
1358 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1359 struct snd_pcm_hw_params *params)
1361 struct snd_soc_pcm_runtime *fe = substream->private_data;
1362 int ret, stream = substream->stream;
1364 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1365 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1367 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1368 sizeof(struct snd_pcm_hw_params));
1369 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1371 dev_err(fe->dev,"dpcm: hw_params BE failed %d\n", ret);
1375 dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
1376 fe->dai_link->name, params_rate(params),
1377 params_channels(params), params_format(params));
1379 /* call hw_params on the frontend */
1380 ret = soc_pcm_hw_params(substream, params);
1382 dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
1383 dpcm_be_dai_hw_free(fe, stream);
1385 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1388 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1389 mutex_unlock(&fe->card->mutex);
1393 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1394 struct snd_pcm_substream *substream, int cmd)
1398 dev_dbg(dpcm->be->dev, "dpcm: trigger BE %s cmd %d\n",
1399 dpcm->fe->dai_link->name, cmd);
1401 ret = soc_pcm_trigger(substream, cmd);
1403 dev_err(dpcm->be->dev,"dpcm: trigger BE failed %d\n", ret);
1408 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1411 struct snd_soc_dpcm *dpcm;
1414 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1416 struct snd_soc_pcm_runtime *be = dpcm->be;
1417 struct snd_pcm_substream *be_substream =
1418 snd_soc_dpcm_get_substream(be, stream);
1420 /* is this op for this BE ? */
1421 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1425 case SNDRV_PCM_TRIGGER_START:
1426 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1427 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1430 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1434 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1436 case SNDRV_PCM_TRIGGER_RESUME:
1437 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1440 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1444 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1446 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1447 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1450 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1454 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1456 case SNDRV_PCM_TRIGGER_STOP:
1457 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1460 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1463 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1467 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1469 case SNDRV_PCM_TRIGGER_SUSPEND:
1470 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1473 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1476 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1482 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1483 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1486 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1489 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1493 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1500 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1502 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1504 struct snd_soc_pcm_runtime *fe = substream->private_data;
1505 int stream = substream->stream, ret;
1506 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1508 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1511 case SND_SOC_DPCM_TRIGGER_PRE:
1512 /* call trigger on the frontend before the backend. */
1514 dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
1515 fe->dai_link->name, cmd);
1517 ret = soc_pcm_trigger(substream, cmd);
1519 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1523 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1525 case SND_SOC_DPCM_TRIGGER_POST:
1526 /* call trigger on the frontend after the backend. */
1528 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1530 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1534 dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
1535 fe->dai_link->name, cmd);
1537 ret = soc_pcm_trigger(substream, cmd);
1539 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1540 /* bespoke trigger() - handles both FE and BEs */
1542 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd %d\n",
1543 fe->dai_link->name, cmd);
1545 ret = soc_pcm_bespoke_trigger(substream, cmd);
1547 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1552 dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
1553 fe->dai_link->name);
1559 case SNDRV_PCM_TRIGGER_START:
1560 case SNDRV_PCM_TRIGGER_RESUME:
1561 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1562 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1564 case SNDRV_PCM_TRIGGER_STOP:
1565 case SNDRV_PCM_TRIGGER_SUSPEND:
1566 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1567 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1572 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1576 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1578 struct snd_soc_dpcm *dpcm;
1581 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1583 struct snd_soc_pcm_runtime *be = dpcm->be;
1584 struct snd_pcm_substream *be_substream =
1585 snd_soc_dpcm_get_substream(be, stream);
1587 /* is this op for this BE ? */
1588 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1591 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1592 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1595 dev_dbg(be->dev, "dpcm: prepare BE %s\n",
1596 dpcm->fe->dai_link->name);
1598 ret = soc_pcm_prepare(be_substream);
1600 dev_err(be->dev, "dpcm: backend prepare failed %d\n",
1605 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1610 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1612 struct snd_soc_pcm_runtime *fe = substream->private_data;
1613 int stream = substream->stream, ret = 0;
1615 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1617 dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
1619 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1621 /* there is no point preparing this FE if there are no BEs */
1622 if (list_empty(&fe->dpcm[stream].be_clients)) {
1623 dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
1624 fe->dai_link->name);
1629 ret = dpcm_be_dai_prepare(fe, substream->stream);
1633 /* call prepare on the frontend */
1634 ret = soc_pcm_prepare(substream);
1636 dev_err(fe->dev,"dpcm: prepare FE %s failed\n",
1637 fe->dai_link->name);
1641 /* run the stream event for each BE */
1642 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1643 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1646 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1647 mutex_unlock(&fe->card->mutex);
1652 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1653 unsigned int cmd, void *arg)
1655 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1656 struct snd_soc_platform *platform = rtd->platform;
1658 if (platform->driver->ops->ioctl)
1659 return platform->driver->ops->ioctl(substream, cmd, arg);
1660 return snd_pcm_lib_ioctl(substream, cmd, arg);
1663 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1665 struct snd_pcm_substream *substream =
1666 snd_soc_dpcm_get_substream(fe, stream);
1667 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1670 dev_dbg(fe->dev, "runtime %s close on FE %s\n",
1671 stream ? "capture" : "playback", fe->dai_link->name);
1673 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1674 /* call bespoke trigger - FE takes care of all BE triggers */
1675 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd stop\n",
1676 fe->dai_link->name);
1678 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1680 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1682 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd stop\n",
1683 fe->dai_link->name);
1685 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1687 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1690 err = dpcm_be_dai_hw_free(fe, stream);
1692 dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
1694 err = dpcm_be_dai_shutdown(fe, stream);
1696 dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
1698 /* run the stream event for each BE */
1699 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1704 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1706 struct snd_pcm_substream *substream =
1707 snd_soc_dpcm_get_substream(fe, stream);
1708 struct snd_soc_dpcm *dpcm;
1709 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1712 dev_dbg(fe->dev, "runtime %s open on FE %s\n",
1713 stream ? "capture" : "playback", fe->dai_link->name);
1715 /* Only start the BE if the FE is ready */
1716 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1717 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1720 /* startup must always be called for new BEs */
1721 ret = dpcm_be_dai_startup(fe, stream);
1727 /* keep going if FE state is > open */
1728 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1731 ret = dpcm_be_dai_hw_params(fe, stream);
1737 /* keep going if FE state is > hw_params */
1738 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1742 ret = dpcm_be_dai_prepare(fe, stream);
1748 /* run the stream event for each BE */
1749 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1751 /* keep going if FE state is > prepare */
1752 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1753 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1756 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1757 /* call trigger on the frontend - FE takes care of all BE triggers */
1758 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd start\n",
1759 fe->dai_link->name);
1761 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1763 dev_err(fe->dev,"dpcm: bespoke trigger FE failed %d\n", ret);
1767 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
1768 fe->dai_link->name);
1770 ret = dpcm_be_dai_trigger(fe, stream,
1771 SNDRV_PCM_TRIGGER_START);
1773 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1781 dpcm_be_dai_hw_free(fe, stream);
1783 dpcm_be_dai_shutdown(fe, stream);
1785 /* disconnect any non started BEs */
1786 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1787 struct snd_soc_pcm_runtime *be = dpcm->be;
1788 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1789 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1795 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1799 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1800 ret = dpcm_run_update_startup(fe, stream);
1802 dev_err(fe->dev, "failed to startup some BEs\n");
1803 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1808 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1812 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1813 ret = dpcm_run_update_shutdown(fe, stream);
1815 dev_err(fe->dev, "failed to shutdown some BEs\n");
1816 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1821 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1824 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1826 struct snd_soc_card *card;
1827 int i, old, new, paths;
1830 card = widget->codec->card;
1831 else if (widget->platform)
1832 card = widget->platform->card;
1836 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1837 for (i = 0; i < card->num_rtd; i++) {
1838 struct snd_soc_dapm_widget_list *list;
1839 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1841 /* make sure link is FE */
1842 if (!fe->dai_link->dynamic)
1845 /* only check active links */
1846 if (!fe->cpu_dai->active)
1849 /* DAPM sync will call this to update DSP paths */
1850 dev_dbg(fe->dev, "DPCM runtime update for FE %s\n",
1851 fe->dai_link->name);
1853 /* skip if FE doesn't have playback capability */
1854 if (!fe->cpu_dai->driver->playback.channels_min)
1857 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1859 dev_warn(fe->dev, "%s no valid %s path\n",
1860 fe->dai_link->name, "playback");
1861 mutex_unlock(&card->mutex);
1865 /* update any new playback paths */
1866 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1868 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1869 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1870 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1873 /* update any old playback paths */
1874 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1876 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1877 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1878 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 /* skip if FE doesn't have capture capability */
1883 if (!fe->cpu_dai->driver->capture.channels_min)
1886 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1888 dev_warn(fe->dev, "%s no valid %s path\n",
1889 fe->dai_link->name, "capture");
1890 mutex_unlock(&card->mutex);
1894 /* update any new capture paths */
1895 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1897 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1898 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1899 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1902 /* update any old capture paths */
1903 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1905 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1906 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1907 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 dpcm_path_put(&list);
1913 mutex_unlock(&card->mutex);
1916 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1918 struct snd_soc_dpcm *dpcm;
1919 struct list_head *clients =
1920 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1922 list_for_each_entry(dpcm, clients, list_be) {
1924 struct snd_soc_pcm_runtime *be = dpcm->be;
1925 struct snd_soc_dai *dai = be->codec_dai;
1926 struct snd_soc_dai_driver *drv = dai->driver;
1928 if (be->dai_link->ignore_suspend)
1931 dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
1933 if (drv->ops->digital_mute && dai->playback_active)
1934 drv->ops->digital_mute(dai, mute);
1940 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1942 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1943 struct snd_soc_dpcm *dpcm;
1944 struct snd_soc_dapm_widget_list *list;
1946 int stream = fe_substream->stream;
1948 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1949 fe->dpcm[stream].runtime = fe_substream->runtime;
1951 if (dpcm_path_get(fe, stream, &list) <= 0) {
1952 dev_warn(fe->dev, "asoc: %s no valid %s route\n",
1953 fe->dai_link->name, stream ? "capture" : "playback");
1954 mutex_unlock(&fe->card->mutex);
1958 /* calculate valid and active FE <-> BE dpcms */
1959 dpcm_process_paths(fe, stream, &list, 1);
1961 ret = dpcm_fe_dai_startup(fe_substream);
1963 /* clean up all links */
1964 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1965 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1967 dpcm_be_disconnect(fe, stream);
1968 fe->dpcm[stream].runtime = NULL;
1971 dpcm_clear_pending_state(fe, stream);
1972 dpcm_path_put(&list);
1973 mutex_unlock(&fe->card->mutex);
1977 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1979 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1980 struct snd_soc_dpcm *dpcm;
1981 int stream = fe_substream->stream, ret;
1983 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1984 ret = dpcm_fe_dai_shutdown(fe_substream);
1986 /* mark FE's links ready to prune */
1987 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1988 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1990 dpcm_be_disconnect(fe, stream);
1992 fe->dpcm[stream].runtime = NULL;
1993 mutex_unlock(&fe->card->mutex);
1997 /* create a new pcm */
1998 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2000 struct snd_soc_codec *codec = rtd->codec;
2001 struct snd_soc_platform *platform = rtd->platform;
2002 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2003 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2004 struct snd_pcm *pcm;
2006 int ret = 0, playback = 0, capture = 0;
2008 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2009 if (cpu_dai->driver->playback.channels_min)
2011 if (cpu_dai->driver->capture.channels_min)
2014 if (codec_dai->driver->playback.channels_min)
2016 if (codec_dai->driver->capture.channels_min)
2020 /* create the PCM */
2021 if (rtd->dai_link->no_pcm) {
2022 snprintf(new_name, sizeof(new_name), "(%s)",
2023 rtd->dai_link->stream_name);
2025 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2026 playback, capture, &pcm);
2028 if (rtd->dai_link->dynamic)
2029 snprintf(new_name, sizeof(new_name), "%s (*)",
2030 rtd->dai_link->stream_name);
2032 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2033 rtd->dai_link->stream_name, codec_dai->name, num);
2035 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2039 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2042 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
2044 /* DAPM dai link stream work */
2045 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2048 pcm->private_data = rtd;
2050 if (rtd->dai_link->no_pcm) {
2052 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2054 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2058 /* ASoC PCM operations */
2059 if (rtd->dai_link->dynamic) {
2060 rtd->ops.open = dpcm_fe_dai_open;
2061 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2062 rtd->ops.prepare = dpcm_fe_dai_prepare;
2063 rtd->ops.trigger = dpcm_fe_dai_trigger;
2064 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2065 rtd->ops.close = dpcm_fe_dai_close;
2066 rtd->ops.pointer = soc_pcm_pointer;
2067 rtd->ops.ioctl = soc_pcm_ioctl;
2069 rtd->ops.open = soc_pcm_open;
2070 rtd->ops.hw_params = soc_pcm_hw_params;
2071 rtd->ops.prepare = soc_pcm_prepare;
2072 rtd->ops.trigger = soc_pcm_trigger;
2073 rtd->ops.hw_free = soc_pcm_hw_free;
2074 rtd->ops.close = soc_pcm_close;
2075 rtd->ops.pointer = soc_pcm_pointer;
2076 rtd->ops.ioctl = soc_pcm_ioctl;
2079 if (platform->driver->ops) {
2080 rtd->ops.ack = platform->driver->ops->ack;
2081 rtd->ops.copy = platform->driver->ops->copy;
2082 rtd->ops.silence = platform->driver->ops->silence;
2083 rtd->ops.page = platform->driver->ops->page;
2084 rtd->ops.mmap = platform->driver->ops->mmap;
2088 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2091 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2093 if (platform->driver->pcm_new) {
2094 ret = platform->driver->pcm_new(rtd);
2096 pr_err("asoc: platform pcm constructor failed\n");
2101 pcm->private_free = platform->driver->pcm_free;
2103 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2108 /* is the current PCM operation for this FE ? */
2109 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2111 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2115 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2117 /* is the current PCM operation for this BE ? */
2118 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2119 struct snd_soc_pcm_runtime *be, int stream)
2121 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2122 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2123 be->dpcm[stream].runtime_update))
2127 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2129 /* get the substream for this BE */
2130 struct snd_pcm_substream *
2131 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2133 return be->pcm->streams[stream].substream;
2135 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2137 /* get the BE runtime state */
2138 enum snd_soc_dpcm_state
2139 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2141 return be->dpcm[stream].state;
2143 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2145 /* set the BE runtime state */
2146 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2147 int stream, enum snd_soc_dpcm_state state)
2149 be->dpcm[stream].state = state;
2151 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2154 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2155 * are not running, paused or suspended for the specified stream direction.
2157 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2158 struct snd_soc_pcm_runtime *be, int stream)
2160 struct snd_soc_dpcm *dpcm;
2163 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2168 state = dpcm->fe->dpcm[stream].state;
2169 if (state == SND_SOC_DPCM_STATE_START ||
2170 state == SND_SOC_DPCM_STATE_PAUSED ||
2171 state == SND_SOC_DPCM_STATE_SUSPEND)
2175 /* it's safe to free/stop this BE DAI */
2178 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2181 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2182 * running, paused or suspended for the specified stream direction.
2184 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2185 struct snd_soc_pcm_runtime *be, int stream)
2187 struct snd_soc_dpcm *dpcm;
2190 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2195 state = dpcm->fe->dpcm[stream].state;
2196 if (state == SND_SOC_DPCM_STATE_START ||
2197 state == SND_SOC_DPCM_STATE_PAUSED ||
2198 state == SND_SOC_DPCM_STATE_SUSPEND ||
2199 state == SND_SOC_DPCM_STATE_PREPARE)
2203 /* it's safe to change hw_params */
2206 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2208 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2209 int cmd, struct snd_soc_platform *platform)
2211 if (platform->driver->ops->trigger)
2212 return platform->driver->ops->trigger(substream, cmd);
2215 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2217 #ifdef CONFIG_DEBUG_FS
2218 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2221 case SND_SOC_DPCM_STATE_NEW:
2223 case SND_SOC_DPCM_STATE_OPEN:
2225 case SND_SOC_DPCM_STATE_HW_PARAMS:
2227 case SND_SOC_DPCM_STATE_PREPARE:
2229 case SND_SOC_DPCM_STATE_START:
2231 case SND_SOC_DPCM_STATE_STOP:
2233 case SND_SOC_DPCM_STATE_SUSPEND:
2235 case SND_SOC_DPCM_STATE_PAUSED:
2237 case SND_SOC_DPCM_STATE_HW_FREE:
2239 case SND_SOC_DPCM_STATE_CLOSE:
2246 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2247 int stream, char *buf, size_t size)
2249 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2250 struct snd_soc_dpcm *dpcm;
2254 offset += snprintf(buf + offset, size - offset,
2255 "[%s - %s]\n", fe->dai_link->name,
2256 stream ? "Capture" : "Playback");
2258 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2259 dpcm_state_string(fe->dpcm[stream].state));
2261 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2262 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2263 offset += snprintf(buf + offset, size - offset,
2265 "Format = %s, Channels = %d, Rate = %d\n",
2266 snd_pcm_format_name(params_format(params)),
2267 params_channels(params),
2268 params_rate(params));
2271 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2273 if (list_empty(&fe->dpcm[stream].be_clients)) {
2274 offset += snprintf(buf + offset, size - offset,
2275 " No active DSP links\n");
2279 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2280 struct snd_soc_pcm_runtime *be = dpcm->be;
2281 params = &dpcm->hw_params;
2283 offset += snprintf(buf + offset, size - offset,
2284 "- %s\n", be->dai_link->name);
2286 offset += snprintf(buf + offset, size - offset,
2288 dpcm_state_string(be->dpcm[stream].state));
2290 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2291 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2292 offset += snprintf(buf + offset, size - offset,
2293 " Hardware Params: "
2294 "Format = %s, Channels = %d, Rate = %d\n",
2295 snd_pcm_format_name(params_format(params)),
2296 params_channels(params),
2297 params_rate(params));
2304 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2305 size_t count, loff_t *ppos)
2307 struct snd_soc_pcm_runtime *fe = file->private_data;
2308 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2311 buf = kmalloc(out_count, GFP_KERNEL);
2315 if (fe->cpu_dai->driver->playback.channels_min)
2316 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2317 buf + offset, out_count - offset);
2319 if (fe->cpu_dai->driver->capture.channels_min)
2320 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2321 buf + offset, out_count - offset);
2323 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2329 static const struct file_operations dpcm_state_fops = {
2330 .open = simple_open,
2331 .read = dpcm_state_read_file,
2332 .llseek = default_llseek,
2335 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2340 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2341 rtd->card->debugfs_card_root);
2342 if (!rtd->debugfs_dpcm_root) {
2344 "ASoC: Failed to create dpcm debugfs directory %s\n",
2345 rtd->dai_link->name);
2349 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2350 rtd->debugfs_dpcm_root,
2351 rtd, &dpcm_state_fops);