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, "ASoC: 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 "ASoC: Not enforcing symmetric_rates due to race\n");
77 dev_dbg(soc_dai->dev, "ASoC: 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 "ASoC: Unable to apply rate symmetry constraint: %d\n",
93 * List of sample sizes that might go over the bus for parameter
94 * application. There ought to be a wildcard sample size for things
95 * like the DAC/ADC resolution to use but there isn't right now.
97 static int sample_sizes[] = {
101 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
102 struct snd_soc_dai *dai)
106 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
107 bits = dai->driver->playback.sig_bits;
109 bits = dai->driver->capture.sig_bits;
114 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
115 if (bits >= sample_sizes[i])
118 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
119 sample_sizes[i], bits);
122 "ASoC: Failed to set MSB %d/%d: %d\n",
123 bits, sample_sizes[i], ret);
128 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
129 * then initialized and any private data can be allocated. This also calls
130 * startup for the cpu DAI, platform, machine and codec DAI.
132 static int soc_pcm_open(struct snd_pcm_substream *substream)
134 struct snd_soc_pcm_runtime *rtd = substream->private_data;
135 struct snd_pcm_runtime *runtime = substream->runtime;
136 struct snd_soc_platform *platform = rtd->platform;
137 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
138 struct snd_soc_dai *codec_dai = rtd->codec_dai;
139 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
140 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
143 pm_runtime_get_sync(cpu_dai->dev);
144 pm_runtime_get_sync(codec_dai->dev);
145 pm_runtime_get_sync(platform->dev);
147 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
149 /* startup the audio subsystem */
150 if (cpu_dai->driver->ops->startup) {
151 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
153 dev_err(cpu_dai->dev, "ASoC: can't open interface"
154 " %s: %d\n", cpu_dai->name, ret);
159 if (platform->driver->ops && platform->driver->ops->open) {
160 ret = platform->driver->ops->open(substream);
162 dev_err(platform->dev, "ASoC: can't open platform"
163 " %s: %d\n", platform->name, ret);
168 if (codec_dai->driver->ops->startup) {
169 ret = codec_dai->driver->ops->startup(substream, codec_dai);
171 dev_err(codec_dai->dev, "ASoC: can't open codec"
172 " %s: %d\n", codec_dai->name, ret);
177 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
178 ret = rtd->dai_link->ops->startup(substream);
180 pr_err("ASoC: %s startup failed: %d\n",
181 rtd->dai_link->name, ret);
186 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
187 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
190 /* Check that the codec and cpu DAIs are compatible */
191 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
192 runtime->hw.rate_min =
193 max(codec_dai_drv->playback.rate_min,
194 cpu_dai_drv->playback.rate_min);
195 runtime->hw.rate_max =
196 min(codec_dai_drv->playback.rate_max,
197 cpu_dai_drv->playback.rate_max);
198 runtime->hw.channels_min =
199 max(codec_dai_drv->playback.channels_min,
200 cpu_dai_drv->playback.channels_min);
201 runtime->hw.channels_max =
202 min(codec_dai_drv->playback.channels_max,
203 cpu_dai_drv->playback.channels_max);
204 runtime->hw.formats =
205 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
207 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
208 if (codec_dai_drv->playback.rates
209 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
210 runtime->hw.rates |= cpu_dai_drv->playback.rates;
211 if (cpu_dai_drv->playback.rates
212 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
213 runtime->hw.rates |= codec_dai_drv->playback.rates;
215 runtime->hw.rate_min =
216 max(codec_dai_drv->capture.rate_min,
217 cpu_dai_drv->capture.rate_min);
218 runtime->hw.rate_max =
219 min(codec_dai_drv->capture.rate_max,
220 cpu_dai_drv->capture.rate_max);
221 runtime->hw.channels_min =
222 max(codec_dai_drv->capture.channels_min,
223 cpu_dai_drv->capture.channels_min);
224 runtime->hw.channels_max =
225 min(codec_dai_drv->capture.channels_max,
226 cpu_dai_drv->capture.channels_max);
227 runtime->hw.formats =
228 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
230 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
231 if (codec_dai_drv->capture.rates
232 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
233 runtime->hw.rates |= cpu_dai_drv->capture.rates;
234 if (cpu_dai_drv->capture.rates
235 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
236 runtime->hw.rates |= codec_dai_drv->capture.rates;
240 snd_pcm_limit_hw_rates(runtime);
241 if (!runtime->hw.rates) {
242 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
243 codec_dai->name, cpu_dai->name);
246 if (!runtime->hw.formats) {
247 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
248 codec_dai->name, cpu_dai->name);
251 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
252 runtime->hw.channels_min > runtime->hw.channels_max) {
253 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
254 codec_dai->name, cpu_dai->name);
258 soc_pcm_apply_msb(substream, codec_dai);
259 soc_pcm_apply_msb(substream, cpu_dai);
261 /* Symmetry only applies if we've already got an active stream. */
262 if (cpu_dai->active) {
263 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
268 if (codec_dai->active) {
269 ret = soc_pcm_apply_symmetry(substream, codec_dai);
274 pr_debug("ASoC: %s <-> %s info:\n",
275 codec_dai->name, cpu_dai->name);
276 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
277 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
278 runtime->hw.channels_max);
279 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
280 runtime->hw.rate_max);
283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
284 cpu_dai->playback_active++;
285 codec_dai->playback_active++;
287 cpu_dai->capture_active++;
288 codec_dai->capture_active++;
292 rtd->codec->active++;
293 mutex_unlock(&rtd->pcm_mutex);
297 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
298 rtd->dai_link->ops->shutdown(substream);
301 if (codec_dai->driver->ops->shutdown)
302 codec_dai->driver->ops->shutdown(substream, codec_dai);
305 if (platform->driver->ops && platform->driver->ops->close)
306 platform->driver->ops->close(substream);
309 if (cpu_dai->driver->ops->shutdown)
310 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
312 mutex_unlock(&rtd->pcm_mutex);
314 pm_runtime_put(platform->dev);
315 pm_runtime_put(codec_dai->dev);
316 pm_runtime_put(cpu_dai->dev);
322 * Power down the audio subsystem pmdown_time msecs after close is called.
323 * This is to ensure there are no pops or clicks in between any music tracks
324 * due to DAPM power cycling.
326 static void close_delayed_work(struct work_struct *work)
328 struct snd_soc_pcm_runtime *rtd =
329 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
330 struct snd_soc_dai *codec_dai = rtd->codec_dai;
332 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
334 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
335 codec_dai->driver->playback.stream_name,
336 codec_dai->playback_active ? "active" : "inactive",
337 rtd->pop_wait ? "yes" : "no");
339 /* are we waiting on this codec DAI stream */
340 if (rtd->pop_wait == 1) {
342 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
343 SND_SOC_DAPM_STREAM_STOP);
346 mutex_unlock(&rtd->pcm_mutex);
350 * Called by ALSA when a PCM substream is closed. Private data can be
351 * freed here. The cpu DAI, codec DAI, machine and platform are also
354 static int soc_pcm_close(struct snd_pcm_substream *substream)
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_platform *platform = rtd->platform;
358 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
359 struct snd_soc_dai *codec_dai = rtd->codec_dai;
360 struct snd_soc_codec *codec = rtd->codec;
362 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
364 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
365 cpu_dai->playback_active--;
366 codec_dai->playback_active--;
368 cpu_dai->capture_active--;
369 codec_dai->capture_active--;
376 /* clear the corresponding DAIs rate when inactive */
377 if (!cpu_dai->active)
380 if (!codec_dai->active)
383 /* Muting the DAC suppresses artifacts caused during digital
384 * shutdown, for example from stopping clocks.
386 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
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 */
411 queue_delayed_work(system_power_efficient_wq,
413 msecs_to_jiffies(rtd->pmdown_time));
416 /* capture streams can be powered down now */
417 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
418 SND_SOC_DAPM_STREAM_STOP);
421 mutex_unlock(&rtd->pcm_mutex);
423 pm_runtime_put(platform->dev);
424 pm_runtime_put(codec_dai->dev);
425 pm_runtime_put(cpu_dai->dev);
431 * Called by ALSA when the PCM substream is prepared, can set format, sample
432 * rate, etc. This function is non atomic and can be called multiple times,
433 * it can refer to the runtime info.
435 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
437 struct snd_soc_pcm_runtime *rtd = substream->private_data;
438 struct snd_soc_platform *platform = rtd->platform;
439 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
440 struct snd_soc_dai *codec_dai = rtd->codec_dai;
443 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
445 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
446 ret = rtd->dai_link->ops->prepare(substream);
448 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
454 if (platform->driver->ops && platform->driver->ops->prepare) {
455 ret = platform->driver->ops->prepare(substream);
457 dev_err(platform->dev, "ASoC: platform prepare error:"
463 if (codec_dai->driver->ops->prepare) {
464 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
466 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
472 if (cpu_dai->driver->ops->prepare) {
473 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
475 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
481 /* cancel any delayed stream shutdown that is pending */
482 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
485 cancel_delayed_work(&rtd->delayed_work);
488 snd_soc_dapm_stream_event(rtd, substream->stream,
489 SND_SOC_DAPM_STREAM_START);
491 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
494 mutex_unlock(&rtd->pcm_mutex);
499 * Called by ALSA when the hardware params are set by application. This
500 * function can also be called multiple times and can allocate buffers
501 * (using snd_pcm_lib_* ). It's non-atomic.
503 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
504 struct snd_pcm_hw_params *params)
506 struct snd_soc_pcm_runtime *rtd = substream->private_data;
507 struct snd_soc_platform *platform = rtd->platform;
508 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
509 struct snd_soc_dai *codec_dai = rtd->codec_dai;
512 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
514 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
515 ret = rtd->dai_link->ops->hw_params(substream, params);
517 dev_err(rtd->card->dev, "ASoC: machine hw_params"
518 " failed: %d\n", ret);
523 if (codec_dai->driver->ops->hw_params) {
524 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
526 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
527 " %d\n", codec_dai->name, ret);
532 if (cpu_dai->driver->ops->hw_params) {
533 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
535 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
541 if (platform->driver->ops && platform->driver->ops->hw_params) {
542 ret = platform->driver->ops->hw_params(substream, params);
544 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
545 platform->name, ret);
550 /* store the rate for each DAIs */
551 cpu_dai->rate = params_rate(params);
552 codec_dai->rate = params_rate(params);
555 mutex_unlock(&rtd->pcm_mutex);
559 if (cpu_dai->driver->ops->hw_free)
560 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
563 if (codec_dai->driver->ops->hw_free)
564 codec_dai->driver->ops->hw_free(substream, codec_dai);
567 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
568 rtd->dai_link->ops->hw_free(substream);
570 mutex_unlock(&rtd->pcm_mutex);
575 * Frees resources allocated by hw_params, can be called multiple times
577 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
579 struct snd_soc_pcm_runtime *rtd = substream->private_data;
580 struct snd_soc_platform *platform = rtd->platform;
581 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
582 struct snd_soc_dai *codec_dai = rtd->codec_dai;
583 struct snd_soc_codec *codec = rtd->codec;
585 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
587 /* apply codec digital mute */
589 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
591 /* free any machine hw params */
592 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
593 rtd->dai_link->ops->hw_free(substream);
595 /* free any DMA resources */
596 if (platform->driver->ops && platform->driver->ops->hw_free)
597 platform->driver->ops->hw_free(substream);
599 /* now free hw params for the DAIs */
600 if (codec_dai->driver->ops->hw_free)
601 codec_dai->driver->ops->hw_free(substream, codec_dai);
603 if (cpu_dai->driver->ops->hw_free)
604 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
606 mutex_unlock(&rtd->pcm_mutex);
610 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
612 struct snd_soc_pcm_runtime *rtd = substream->private_data;
613 struct snd_soc_platform *platform = rtd->platform;
614 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
615 struct snd_soc_dai *codec_dai = rtd->codec_dai;
618 if (codec_dai->driver->ops->trigger) {
619 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
624 if (platform->driver->ops && platform->driver->ops->trigger) {
625 ret = platform->driver->ops->trigger(substream, cmd);
630 if (cpu_dai->driver->ops->trigger) {
631 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
638 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
641 struct snd_soc_pcm_runtime *rtd = substream->private_data;
642 struct snd_soc_platform *platform = rtd->platform;
643 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
644 struct snd_soc_dai *codec_dai = rtd->codec_dai;
647 if (codec_dai->driver->ops->bespoke_trigger) {
648 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
653 if (platform->driver->bespoke_trigger) {
654 ret = platform->driver->bespoke_trigger(substream, cmd);
659 if (cpu_dai->driver->ops->bespoke_trigger) {
660 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
667 * soc level wrapper for pointer callback
668 * If cpu_dai, codec_dai, platform driver has the delay callback, than
669 * the runtime->delay will be updated accordingly.
671 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
673 struct snd_soc_pcm_runtime *rtd = substream->private_data;
674 struct snd_soc_platform *platform = rtd->platform;
675 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
676 struct snd_soc_dai *codec_dai = rtd->codec_dai;
677 struct snd_pcm_runtime *runtime = substream->runtime;
678 snd_pcm_uframes_t offset = 0;
679 snd_pcm_sframes_t delay = 0;
681 if (platform->driver->ops && platform->driver->ops->pointer)
682 offset = platform->driver->ops->pointer(substream);
684 if (cpu_dai->driver->ops->delay)
685 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
687 if (codec_dai->driver->ops->delay)
688 delay += codec_dai->driver->ops->delay(substream, codec_dai);
690 if (platform->driver->delay)
691 delay += platform->driver->delay(substream, codec_dai);
693 runtime->delay = delay;
698 /* connect a FE and BE */
699 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
700 struct snd_soc_pcm_runtime *be, int stream)
702 struct snd_soc_dpcm *dpcm;
704 /* only add new dpcms */
705 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
706 if (dpcm->be == be && dpcm->fe == fe)
710 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
716 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
717 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
718 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
719 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
721 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
722 stream ? "capture" : "playback", fe->dai_link->name,
723 stream ? "<-" : "->", be->dai_link->name);
725 #ifdef CONFIG_DEBUG_FS
726 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
727 fe->debugfs_dpcm_root, &dpcm->state);
732 /* reparent a BE onto another FE */
733 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
734 struct snd_soc_pcm_runtime *be, int stream)
736 struct snd_soc_dpcm *dpcm;
737 struct snd_pcm_substream *fe_substream, *be_substream;
739 /* reparent if BE is connected to other FEs */
740 if (!be->dpcm[stream].users)
743 be_substream = snd_soc_dpcm_get_substream(be, stream);
745 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
749 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
750 stream ? "capture" : "playback",
751 dpcm->fe->dai_link->name,
752 stream ? "<-" : "->", dpcm->be->dai_link->name);
754 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
755 be_substream->runtime = fe_substream->runtime;
760 /* disconnect a BE and FE */
761 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
763 struct snd_soc_dpcm *dpcm, *d;
765 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
766 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
767 stream ? "capture" : "playback",
768 dpcm->be->dai_link->name);
770 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
773 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
774 stream ? "capture" : "playback", fe->dai_link->name,
775 stream ? "<-" : "->", dpcm->be->dai_link->name);
777 /* BEs still alive need new FE */
778 dpcm_be_reparent(fe, dpcm->be, stream);
780 #ifdef CONFIG_DEBUG_FS
781 debugfs_remove(dpcm->debugfs_state);
783 list_del(&dpcm->list_be);
784 list_del(&dpcm->list_fe);
789 /* get BE for DAI widget and stream */
790 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
791 struct snd_soc_dapm_widget *widget, int stream)
793 struct snd_soc_pcm_runtime *be;
796 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
797 for (i = 0; i < card->num_links; i++) {
800 if (!be->dai_link->no_pcm)
803 if (be->cpu_dai->playback_widget == widget ||
804 be->codec_dai->playback_widget == widget)
809 for (i = 0; i < card->num_links; i++) {
812 if (!be->dai_link->no_pcm)
815 if (be->cpu_dai->capture_widget == widget ||
816 be->codec_dai->capture_widget == widget)
821 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
822 stream ? "capture" : "playback", widget->name);
826 static inline struct snd_soc_dapm_widget *
827 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
829 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
830 return rtd->cpu_dai->playback_widget;
832 return rtd->cpu_dai->capture_widget;
835 static inline struct snd_soc_dapm_widget *
836 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
838 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
839 return rtd->codec_dai->playback_widget;
841 return rtd->codec_dai->capture_widget;
844 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
845 struct snd_soc_dapm_widget *widget)
849 for (i = 0; i < list->num_widgets; i++) {
850 if (widget == list->widgets[i])
857 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
858 int stream, struct snd_soc_dapm_widget_list **list_)
860 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
861 struct snd_soc_dapm_widget_list *list;
864 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
865 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
869 /* get number of valid DAI paths and their widgets */
870 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
872 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
873 stream ? "capture" : "playback");
879 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
884 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
885 struct snd_soc_dapm_widget_list **list_)
887 struct snd_soc_dpcm *dpcm;
888 struct snd_soc_dapm_widget_list *list = *list_;
889 struct snd_soc_dapm_widget *widget;
892 /* Destroy any old FE <--> BE connections */
893 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
895 /* is there a valid CPU DAI widget for this BE */
896 widget = rtd_get_cpu_widget(dpcm->be, stream);
898 /* prune the BE if it's no longer in our active list */
899 if (widget && widget_in_list(list, widget))
902 /* is there a valid CODEC DAI widget for this BE */
903 widget = rtd_get_codec_widget(dpcm->be, stream);
905 /* prune the BE if it's no longer in our active list */
906 if (widget && widget_in_list(list, widget))
909 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
910 stream ? "capture" : "playback",
911 dpcm->be->dai_link->name, fe->dai_link->name);
912 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
913 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
917 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
921 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
922 struct snd_soc_dapm_widget_list **list_)
924 struct snd_soc_card *card = fe->card;
925 struct snd_soc_dapm_widget_list *list = *list_;
926 struct snd_soc_pcm_runtime *be;
929 /* Create any new FE <--> BE connections */
930 for (i = 0; i < list->num_widgets; i++) {
932 switch (list->widgets[i]->id) {
933 case snd_soc_dapm_dai_in:
934 case snd_soc_dapm_dai_out:
940 /* is there a valid BE rtd for this widget */
941 be = dpcm_get_be(card, list->widgets[i], stream);
943 dev_err(fe->dev, "ASoC: no BE found for %s\n",
944 list->widgets[i]->name);
948 /* make sure BE is a real BE */
949 if (!be->dai_link->no_pcm)
952 /* don't connect if FE is not running */
953 if (!fe->dpcm[stream].runtime)
956 /* newly connected FE and BE */
957 err = dpcm_be_connect(fe, be, stream);
959 dev_err(fe->dev, "ASoC: can't connect %s\n",
960 list->widgets[i]->name);
962 } else if (err == 0) /* already connected */
966 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
970 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
975 * Find the corresponding BE DAIs that source or sink audio to this
978 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
979 int stream, struct snd_soc_dapm_widget_list **list, int new)
982 return dpcm_add_paths(fe, stream, list);
984 return dpcm_prune_paths(fe, stream, list);
987 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
989 struct snd_soc_dpcm *dpcm;
991 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
992 dpcm->be->dpcm[stream].runtime_update =
993 SND_SOC_DPCM_UPDATE_NO;
996 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
999 struct snd_soc_dpcm *dpcm;
1001 /* disable any enabled and non active backends */
1002 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1004 struct snd_soc_pcm_runtime *be = dpcm->be;
1005 struct snd_pcm_substream *be_substream =
1006 snd_soc_dpcm_get_substream(be, stream);
1008 if (be->dpcm[stream].users == 0)
1009 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1010 stream ? "capture" : "playback",
1011 be->dpcm[stream].state);
1013 if (--be->dpcm[stream].users != 0)
1016 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1019 soc_pcm_close(be_substream);
1020 be_substream->runtime = NULL;
1021 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1025 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1027 struct snd_soc_dpcm *dpcm;
1030 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1031 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1033 struct snd_soc_pcm_runtime *be = dpcm->be;
1034 struct snd_pcm_substream *be_substream =
1035 snd_soc_dpcm_get_substream(be, stream);
1037 /* is this op for this BE ? */
1038 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1041 /* first time the dpcm is open ? */
1042 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1043 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1044 stream ? "capture" : "playback",
1045 be->dpcm[stream].state);
1047 if (be->dpcm[stream].users++ != 0)
1050 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1054 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1056 be_substream->runtime = be->dpcm[stream].runtime;
1057 err = soc_pcm_open(be_substream);
1059 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1060 be->dpcm[stream].users--;
1061 if (be->dpcm[stream].users < 0)
1062 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1063 stream ? "capture" : "playback",
1064 be->dpcm[stream].state);
1066 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1070 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1077 /* disable any enabled and non active backends */
1078 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1079 struct snd_soc_pcm_runtime *be = dpcm->be;
1080 struct snd_pcm_substream *be_substream =
1081 snd_soc_dpcm_get_substream(be, stream);
1083 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1086 if (be->dpcm[stream].users == 0)
1087 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1088 stream ? "capture" : "playback",
1089 be->dpcm[stream].state);
1091 if (--be->dpcm[stream].users != 0)
1094 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1097 soc_pcm_close(be_substream);
1098 be_substream->runtime = NULL;
1099 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1105 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1107 struct snd_pcm_runtime *runtime = substream->runtime;
1108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1109 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1110 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1112 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1113 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1114 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1115 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1116 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1117 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1118 runtime->hw.rates = cpu_dai_drv->playback.rates;
1120 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1121 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1122 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1123 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1124 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1125 runtime->hw.rates = cpu_dai_drv->capture.rates;
1129 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1131 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1132 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1133 int stream = fe_substream->stream, ret = 0;
1135 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1137 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1139 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1143 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1145 /* start the DAI frontend */
1146 ret = soc_pcm_open(fe_substream);
1148 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1152 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1154 dpcm_set_fe_runtime(fe_substream);
1155 snd_pcm_limit_hw_rates(runtime);
1157 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1161 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1163 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1167 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1169 struct snd_soc_dpcm *dpcm;
1171 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1172 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1174 struct snd_soc_pcm_runtime *be = dpcm->be;
1175 struct snd_pcm_substream *be_substream =
1176 snd_soc_dpcm_get_substream(be, stream);
1178 /* is this op for this BE ? */
1179 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1182 if (be->dpcm[stream].users == 0)
1183 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1184 stream ? "capture" : "playback",
1185 be->dpcm[stream].state);
1187 if (--be->dpcm[stream].users != 0)
1190 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1191 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1194 dev_dbg(be->dev, "ASoC: close BE %s\n",
1195 dpcm->fe->dai_link->name);
1197 soc_pcm_close(be_substream);
1198 be_substream->runtime = NULL;
1200 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1205 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1207 struct snd_soc_pcm_runtime *fe = substream->private_data;
1208 int stream = substream->stream;
1210 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1212 /* shutdown the BEs */
1213 dpcm_be_dai_shutdown(fe, substream->stream);
1215 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1217 /* now shutdown the frontend */
1218 soc_pcm_close(substream);
1220 /* run the stream event for each BE */
1221 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1223 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1224 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1228 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1230 struct snd_soc_dpcm *dpcm;
1232 /* only hw_params backends that are either sinks or sources
1233 * to this frontend DAI */
1234 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1236 struct snd_soc_pcm_runtime *be = dpcm->be;
1237 struct snd_pcm_substream *be_substream =
1238 snd_soc_dpcm_get_substream(be, stream);
1240 /* is this op for this BE ? */
1241 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1244 /* only free hw when no longer used - check all FEs */
1245 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1248 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1249 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1250 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1251 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1252 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1255 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1256 dpcm->fe->dai_link->name);
1258 soc_pcm_hw_free(be_substream);
1260 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1266 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1268 struct snd_soc_pcm_runtime *fe = substream->private_data;
1269 int err, stream = substream->stream;
1271 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1272 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1274 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1276 /* call hw_free on the frontend */
1277 err = soc_pcm_hw_free(substream);
1279 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1280 fe->dai_link->name);
1282 /* only hw_params backends that are either sinks or sources
1283 * to this frontend DAI */
1284 err = dpcm_be_dai_hw_free(fe, stream);
1286 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1287 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1289 mutex_unlock(&fe->card->mutex);
1293 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1295 struct snd_soc_dpcm *dpcm;
1298 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1300 struct snd_soc_pcm_runtime *be = dpcm->be;
1301 struct snd_pcm_substream *be_substream =
1302 snd_soc_dpcm_get_substream(be, stream);
1304 /* is this op for this BE ? */
1305 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1308 /* only allow hw_params() if no connected FEs are running */
1309 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1312 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1313 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1314 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1317 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1318 dpcm->fe->dai_link->name);
1320 /* copy params for each dpcm */
1321 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1322 sizeof(struct snd_pcm_hw_params));
1324 /* perform any hw_params fixups */
1325 if (be->dai_link->be_hw_params_fixup) {
1326 ret = be->dai_link->be_hw_params_fixup(be,
1330 "ASoC: hw_params BE fixup failed %d\n",
1336 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1338 dev_err(dpcm->be->dev,
1339 "ASoC: hw_params BE failed %d\n", ret);
1343 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1348 /* disable any enabled and non active backends */
1349 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1350 struct snd_soc_pcm_runtime *be = dpcm->be;
1351 struct snd_pcm_substream *be_substream =
1352 snd_soc_dpcm_get_substream(be, stream);
1354 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1357 /* only allow hw_free() if no connected FEs are running */
1358 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1361 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1362 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1363 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1364 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1367 soc_pcm_hw_free(be_substream);
1373 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1374 struct snd_pcm_hw_params *params)
1376 struct snd_soc_pcm_runtime *fe = substream->private_data;
1377 int ret, stream = substream->stream;
1379 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1380 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1382 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1383 sizeof(struct snd_pcm_hw_params));
1384 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1386 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1390 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1391 fe->dai_link->name, params_rate(params),
1392 params_channels(params), params_format(params));
1394 /* call hw_params on the frontend */
1395 ret = soc_pcm_hw_params(substream, params);
1397 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1398 dpcm_be_dai_hw_free(fe, stream);
1400 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1403 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1404 mutex_unlock(&fe->card->mutex);
1408 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1409 struct snd_pcm_substream *substream, int cmd)
1413 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1414 dpcm->fe->dai_link->name, cmd);
1416 ret = soc_pcm_trigger(substream, cmd);
1418 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1423 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1426 struct snd_soc_dpcm *dpcm;
1429 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1431 struct snd_soc_pcm_runtime *be = dpcm->be;
1432 struct snd_pcm_substream *be_substream =
1433 snd_soc_dpcm_get_substream(be, stream);
1435 /* is this op for this BE ? */
1436 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1440 case SNDRV_PCM_TRIGGER_START:
1441 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1442 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1445 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1449 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1451 case SNDRV_PCM_TRIGGER_RESUME:
1452 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1455 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1459 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1461 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1462 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1465 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1469 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1471 case SNDRV_PCM_TRIGGER_STOP:
1472 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1475 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1478 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1482 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1484 case SNDRV_PCM_TRIGGER_SUSPEND:
1485 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1488 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1491 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1495 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1497 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1498 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1501 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1504 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1508 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1515 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1517 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1519 struct snd_soc_pcm_runtime *fe = substream->private_data;
1520 int stream = substream->stream, ret;
1521 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1523 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1526 case SND_SOC_DPCM_TRIGGER_PRE:
1527 /* call trigger on the frontend before the backend. */
1529 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1530 fe->dai_link->name, cmd);
1532 ret = soc_pcm_trigger(substream, cmd);
1534 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1538 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1540 case SND_SOC_DPCM_TRIGGER_POST:
1541 /* call trigger on the frontend after the backend. */
1543 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1545 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1549 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1550 fe->dai_link->name, cmd);
1552 ret = soc_pcm_trigger(substream, cmd);
1554 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1555 /* bespoke trigger() - handles both FE and BEs */
1557 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1558 fe->dai_link->name, cmd);
1560 ret = soc_pcm_bespoke_trigger(substream, cmd);
1562 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1567 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1568 fe->dai_link->name);
1574 case SNDRV_PCM_TRIGGER_START:
1575 case SNDRV_PCM_TRIGGER_RESUME:
1576 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1577 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1579 case SNDRV_PCM_TRIGGER_STOP:
1580 case SNDRV_PCM_TRIGGER_SUSPEND:
1581 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1582 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1587 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1591 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1593 struct snd_soc_dpcm *dpcm;
1596 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1598 struct snd_soc_pcm_runtime *be = dpcm->be;
1599 struct snd_pcm_substream *be_substream =
1600 snd_soc_dpcm_get_substream(be, stream);
1602 /* is this op for this BE ? */
1603 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1606 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1607 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1610 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1611 dpcm->fe->dai_link->name);
1613 ret = soc_pcm_prepare(be_substream);
1615 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1620 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1625 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1627 struct snd_soc_pcm_runtime *fe = substream->private_data;
1628 int stream = substream->stream, ret = 0;
1630 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1632 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1634 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1636 /* there is no point preparing this FE if there are no BEs */
1637 if (list_empty(&fe->dpcm[stream].be_clients)) {
1638 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1639 fe->dai_link->name);
1644 ret = dpcm_be_dai_prepare(fe, substream->stream);
1648 /* call prepare on the frontend */
1649 ret = soc_pcm_prepare(substream);
1651 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1652 fe->dai_link->name);
1656 /* run the stream event for each BE */
1657 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1658 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1661 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1662 mutex_unlock(&fe->card->mutex);
1667 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1668 unsigned int cmd, void *arg)
1670 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1671 struct snd_soc_platform *platform = rtd->platform;
1673 if (platform->driver->ops->ioctl)
1674 return platform->driver->ops->ioctl(substream, cmd, arg);
1675 return snd_pcm_lib_ioctl(substream, cmd, arg);
1678 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1680 struct snd_pcm_substream *substream =
1681 snd_soc_dpcm_get_substream(fe, stream);
1682 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1685 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1686 stream ? "capture" : "playback", fe->dai_link->name);
1688 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1689 /* call bespoke trigger - FE takes care of all BE triggers */
1690 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1691 fe->dai_link->name);
1693 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1695 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1697 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1698 fe->dai_link->name);
1700 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1702 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1705 err = dpcm_be_dai_hw_free(fe, stream);
1707 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1709 err = dpcm_be_dai_shutdown(fe, stream);
1711 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1713 /* run the stream event for each BE */
1714 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1719 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1721 struct snd_pcm_substream *substream =
1722 snd_soc_dpcm_get_substream(fe, stream);
1723 struct snd_soc_dpcm *dpcm;
1724 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1727 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1728 stream ? "capture" : "playback", fe->dai_link->name);
1730 /* Only start the BE if the FE is ready */
1731 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1732 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1735 /* startup must always be called for new BEs */
1736 ret = dpcm_be_dai_startup(fe, stream);
1740 /* keep going if FE state is > open */
1741 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1744 ret = dpcm_be_dai_hw_params(fe, stream);
1748 /* keep going if FE state is > hw_params */
1749 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1753 ret = dpcm_be_dai_prepare(fe, stream);
1757 /* run the stream event for each BE */
1758 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1760 /* keep going if FE state is > prepare */
1761 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1762 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1765 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1766 /* call trigger on the frontend - FE takes care of all BE triggers */
1767 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1768 fe->dai_link->name);
1770 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1772 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1776 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1777 fe->dai_link->name);
1779 ret = dpcm_be_dai_trigger(fe, stream,
1780 SNDRV_PCM_TRIGGER_START);
1782 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1790 dpcm_be_dai_hw_free(fe, stream);
1792 dpcm_be_dai_shutdown(fe, stream);
1794 /* disconnect any non started BEs */
1795 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1796 struct snd_soc_pcm_runtime *be = dpcm->be;
1797 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1798 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1804 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1808 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1809 ret = dpcm_run_update_startup(fe, stream);
1811 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1812 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1817 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1821 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1822 ret = dpcm_run_update_shutdown(fe, stream);
1824 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1825 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1830 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1833 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1835 struct snd_soc_card *card;
1836 int i, old, new, paths;
1839 card = widget->codec->card;
1840 else if (widget->platform)
1841 card = widget->platform->card;
1845 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1846 for (i = 0; i < card->num_rtd; i++) {
1847 struct snd_soc_dapm_widget_list *list;
1848 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1850 /* make sure link is FE */
1851 if (!fe->dai_link->dynamic)
1854 /* only check active links */
1855 if (!fe->cpu_dai->active)
1858 /* DAPM sync will call this to update DSP paths */
1859 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1860 fe->dai_link->name);
1862 /* skip if FE doesn't have playback capability */
1863 if (!fe->cpu_dai->driver->playback.channels_min)
1866 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1868 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1869 fe->dai_link->name, "playback");
1870 mutex_unlock(&card->mutex);
1874 /* update any new playback paths */
1875 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1877 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1878 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1879 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 /* update any old playback paths */
1883 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1885 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1886 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1887 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1891 /* skip if FE doesn't have capture capability */
1892 if (!fe->cpu_dai->driver->capture.channels_min)
1895 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1897 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1898 fe->dai_link->name, "capture");
1899 mutex_unlock(&card->mutex);
1903 /* update any new capture paths */
1904 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1906 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1907 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1908 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 /* update any old capture paths */
1912 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1914 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1915 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1916 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1919 dpcm_path_put(&list);
1922 mutex_unlock(&card->mutex);
1925 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1927 struct snd_soc_dpcm *dpcm;
1928 struct list_head *clients =
1929 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1931 list_for_each_entry(dpcm, clients, list_be) {
1933 struct snd_soc_pcm_runtime *be = dpcm->be;
1934 struct snd_soc_dai *dai = be->codec_dai;
1935 struct snd_soc_dai_driver *drv = dai->driver;
1937 if (be->dai_link->ignore_suspend)
1940 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1942 if (drv->ops->digital_mute && dai->playback_active)
1943 drv->ops->digital_mute(dai, mute);
1949 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1951 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1952 struct snd_soc_dpcm *dpcm;
1953 struct snd_soc_dapm_widget_list *list;
1955 int stream = fe_substream->stream;
1957 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1958 fe->dpcm[stream].runtime = fe_substream->runtime;
1960 if (dpcm_path_get(fe, stream, &list) <= 0) {
1961 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1962 fe->dai_link->name, stream ? "capture" : "playback");
1965 /* calculate valid and active FE <-> BE dpcms */
1966 dpcm_process_paths(fe, stream, &list, 1);
1968 ret = dpcm_fe_dai_startup(fe_substream);
1970 /* clean up all links */
1971 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1972 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1974 dpcm_be_disconnect(fe, stream);
1975 fe->dpcm[stream].runtime = NULL;
1978 dpcm_clear_pending_state(fe, stream);
1979 dpcm_path_put(&list);
1980 mutex_unlock(&fe->card->mutex);
1984 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1986 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1987 struct snd_soc_dpcm *dpcm;
1988 int stream = fe_substream->stream, ret;
1990 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1991 ret = dpcm_fe_dai_shutdown(fe_substream);
1993 /* mark FE's links ready to prune */
1994 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1995 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1997 dpcm_be_disconnect(fe, stream);
1999 fe->dpcm[stream].runtime = NULL;
2000 mutex_unlock(&fe->card->mutex);
2004 /* create a new pcm */
2005 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2007 struct snd_soc_platform *platform = rtd->platform;
2008 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2010 struct snd_pcm *pcm;
2012 int ret = 0, playback = 0, capture = 0;
2014 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2015 if (cpu_dai->driver->playback.channels_min)
2017 if (cpu_dai->driver->capture.channels_min)
2020 if (codec_dai->driver->playback.channels_min &&
2021 cpu_dai->driver->playback.channels_min)
2023 if (codec_dai->driver->capture.channels_min &&
2024 cpu_dai->driver->capture.channels_min)
2028 /* create the PCM */
2029 if (rtd->dai_link->no_pcm) {
2030 snprintf(new_name, sizeof(new_name), "(%s)",
2031 rtd->dai_link->stream_name);
2033 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2034 playback, capture, &pcm);
2036 if (rtd->dai_link->dynamic)
2037 snprintf(new_name, sizeof(new_name), "%s (*)",
2038 rtd->dai_link->stream_name);
2040 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2041 rtd->dai_link->stream_name, codec_dai->name, num);
2043 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2047 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2048 rtd->dai_link->name);
2051 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2053 /* DAPM dai link stream work */
2054 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2057 pcm->private_data = rtd;
2059 if (rtd->dai_link->no_pcm) {
2061 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2063 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2067 /* ASoC PCM operations */
2068 if (rtd->dai_link->dynamic) {
2069 rtd->ops.open = dpcm_fe_dai_open;
2070 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2071 rtd->ops.prepare = dpcm_fe_dai_prepare;
2072 rtd->ops.trigger = dpcm_fe_dai_trigger;
2073 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2074 rtd->ops.close = dpcm_fe_dai_close;
2075 rtd->ops.pointer = soc_pcm_pointer;
2076 rtd->ops.ioctl = soc_pcm_ioctl;
2078 rtd->ops.open = soc_pcm_open;
2079 rtd->ops.hw_params = soc_pcm_hw_params;
2080 rtd->ops.prepare = soc_pcm_prepare;
2081 rtd->ops.trigger = soc_pcm_trigger;
2082 rtd->ops.hw_free = soc_pcm_hw_free;
2083 rtd->ops.close = soc_pcm_close;
2084 rtd->ops.pointer = soc_pcm_pointer;
2085 rtd->ops.ioctl = soc_pcm_ioctl;
2088 if (platform->driver->ops) {
2089 rtd->ops.ack = platform->driver->ops->ack;
2090 rtd->ops.copy = platform->driver->ops->copy;
2091 rtd->ops.silence = platform->driver->ops->silence;
2092 rtd->ops.page = platform->driver->ops->page;
2093 rtd->ops.mmap = platform->driver->ops->mmap;
2097 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2100 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2102 if (platform->driver->pcm_new) {
2103 ret = platform->driver->pcm_new(rtd);
2105 dev_err(platform->dev,
2106 "ASoC: pcm constructor failed: %d\n",
2112 pcm->private_free = platform->driver->pcm_free;
2114 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2119 /* is the current PCM operation for this FE ? */
2120 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2122 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2126 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2128 /* is the current PCM operation for this BE ? */
2129 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2130 struct snd_soc_pcm_runtime *be, int stream)
2132 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2133 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2134 be->dpcm[stream].runtime_update))
2138 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2140 /* get the substream for this BE */
2141 struct snd_pcm_substream *
2142 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2144 return be->pcm->streams[stream].substream;
2146 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2148 /* get the BE runtime state */
2149 enum snd_soc_dpcm_state
2150 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2152 return be->dpcm[stream].state;
2154 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2156 /* set the BE runtime state */
2157 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2158 int stream, enum snd_soc_dpcm_state state)
2160 be->dpcm[stream].state = state;
2162 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2165 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2166 * are not running, paused or suspended for the specified stream direction.
2168 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2169 struct snd_soc_pcm_runtime *be, int stream)
2171 struct snd_soc_dpcm *dpcm;
2174 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2179 state = dpcm->fe->dpcm[stream].state;
2180 if (state == SND_SOC_DPCM_STATE_START ||
2181 state == SND_SOC_DPCM_STATE_PAUSED ||
2182 state == SND_SOC_DPCM_STATE_SUSPEND)
2186 /* it's safe to free/stop this BE DAI */
2189 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2192 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2193 * running, paused or suspended for the specified stream direction.
2195 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2196 struct snd_soc_pcm_runtime *be, int stream)
2198 struct snd_soc_dpcm *dpcm;
2201 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2206 state = dpcm->fe->dpcm[stream].state;
2207 if (state == SND_SOC_DPCM_STATE_START ||
2208 state == SND_SOC_DPCM_STATE_PAUSED ||
2209 state == SND_SOC_DPCM_STATE_SUSPEND ||
2210 state == SND_SOC_DPCM_STATE_PREPARE)
2214 /* it's safe to change hw_params */
2217 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2219 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2220 int cmd, struct snd_soc_platform *platform)
2222 if (platform->driver->ops->trigger)
2223 return platform->driver->ops->trigger(substream, cmd);
2226 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2228 #ifdef CONFIG_DEBUG_FS
2229 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2232 case SND_SOC_DPCM_STATE_NEW:
2234 case SND_SOC_DPCM_STATE_OPEN:
2236 case SND_SOC_DPCM_STATE_HW_PARAMS:
2238 case SND_SOC_DPCM_STATE_PREPARE:
2240 case SND_SOC_DPCM_STATE_START:
2242 case SND_SOC_DPCM_STATE_STOP:
2244 case SND_SOC_DPCM_STATE_SUSPEND:
2246 case SND_SOC_DPCM_STATE_PAUSED:
2248 case SND_SOC_DPCM_STATE_HW_FREE:
2250 case SND_SOC_DPCM_STATE_CLOSE:
2257 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2258 int stream, char *buf, size_t size)
2260 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2261 struct snd_soc_dpcm *dpcm;
2265 offset += snprintf(buf + offset, size - offset,
2266 "[%s - %s]\n", fe->dai_link->name,
2267 stream ? "Capture" : "Playback");
2269 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2270 dpcm_state_string(fe->dpcm[stream].state));
2272 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2273 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2274 offset += snprintf(buf + offset, size - offset,
2276 "Format = %s, Channels = %d, Rate = %d\n",
2277 snd_pcm_format_name(params_format(params)),
2278 params_channels(params),
2279 params_rate(params));
2282 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2284 if (list_empty(&fe->dpcm[stream].be_clients)) {
2285 offset += snprintf(buf + offset, size - offset,
2286 " No active DSP links\n");
2290 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2291 struct snd_soc_pcm_runtime *be = dpcm->be;
2292 params = &dpcm->hw_params;
2294 offset += snprintf(buf + offset, size - offset,
2295 "- %s\n", be->dai_link->name);
2297 offset += snprintf(buf + offset, size - offset,
2299 dpcm_state_string(be->dpcm[stream].state));
2301 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2302 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2303 offset += snprintf(buf + offset, size - offset,
2304 " Hardware Params: "
2305 "Format = %s, Channels = %d, Rate = %d\n",
2306 snd_pcm_format_name(params_format(params)),
2307 params_channels(params),
2308 params_rate(params));
2315 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2316 size_t count, loff_t *ppos)
2318 struct snd_soc_pcm_runtime *fe = file->private_data;
2319 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2322 buf = kmalloc(out_count, GFP_KERNEL);
2326 if (fe->cpu_dai->driver->playback.channels_min)
2327 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2328 buf + offset, out_count - offset);
2330 if (fe->cpu_dai->driver->capture.channels_min)
2331 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2332 buf + offset, out_count - offset);
2334 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2340 static const struct file_operations dpcm_state_fops = {
2341 .open = simple_open,
2342 .read = dpcm_state_read_file,
2343 .llseek = default_llseek,
2346 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2351 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2352 rtd->card->debugfs_card_root);
2353 if (!rtd->debugfs_dpcm_root) {
2355 "ASoC: Failed to create dpcm debugfs directory %s\n",
2356 rtd->dai_link->name);
2360 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2361 rtd->debugfs_dpcm_root,
2362 rtd, &dpcm_state_fops);