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
37 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
38 * @substream: the pcm substream
39 * @hw: the hardware parameters
41 * Sets the substream runtime hardware parameters.
43 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
44 const struct snd_pcm_hardware *hw)
46 struct snd_pcm_runtime *runtime = substream->runtime;
47 runtime->hw.info = hw->info;
48 runtime->hw.formats = hw->formats;
49 runtime->hw.period_bytes_min = hw->period_bytes_min;
50 runtime->hw.period_bytes_max = hw->period_bytes_max;
51 runtime->hw.periods_min = hw->periods_min;
52 runtime->hw.periods_max = hw->periods_max;
53 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
54 runtime->hw.fifo_size = hw->fifo_size;
57 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
59 /* DPCM stream event, send event to FE and all active BEs. */
60 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
63 struct snd_soc_dpcm *dpcm;
65 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
67 struct snd_soc_pcm_runtime *be = dpcm->be;
69 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
70 be->dai_link->name, event, dir);
72 snd_soc_dapm_stream_event(be, dir, event);
75 snd_soc_dapm_stream_event(fe, dir, event);
80 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
81 struct snd_soc_dai *soc_dai)
83 struct snd_soc_pcm_runtime *rtd = substream->private_data;
86 if (!soc_dai->driver->symmetric_rates &&
87 !rtd->dai_link->symmetric_rates)
90 /* This can happen if multiple streams are starting simultaneously -
91 * the second can need to get its constraints before the first has
92 * picked a rate. Complain and allow the application to carry on.
95 dev_warn(soc_dai->dev,
96 "ASoC: Not enforcing symmetric_rates due to race\n");
100 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
102 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
103 SNDRV_PCM_HW_PARAM_RATE,
104 soc_dai->rate, soc_dai->rate);
106 dev_err(soc_dai->dev,
107 "ASoC: Unable to apply rate symmetry constraint: %d\n",
116 * List of sample sizes that might go over the bus for parameter
117 * application. There ought to be a wildcard sample size for things
118 * like the DAC/ADC resolution to use but there isn't right now.
120 static int sample_sizes[] = {
124 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
125 struct snd_soc_dai *dai)
129 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
130 bits = dai->driver->playback.sig_bits;
132 bits = dai->driver->capture.sig_bits;
137 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
138 if (bits >= sample_sizes[i])
141 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
142 sample_sizes[i], bits);
145 "ASoC: Failed to set MSB %d/%d: %d\n",
146 bits, sample_sizes[i], ret);
150 static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
151 struct snd_soc_pcm_stream *codec_stream,
152 struct snd_soc_pcm_stream *cpu_stream)
154 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
155 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
156 hw->channels_min = max(codec_stream->channels_min,
157 cpu_stream->channels_min);
158 hw->channels_max = min(codec_stream->channels_max,
159 cpu_stream->channels_max);
160 hw->formats = codec_stream->formats & cpu_stream->formats;
161 hw->rates = codec_stream->rates & cpu_stream->rates;
162 if (codec_stream->rates
163 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
164 hw->rates |= cpu_stream->rates;
165 if (cpu_stream->rates
166 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
167 hw->rates |= codec_stream->rates;
171 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
172 * then initialized and any private data can be allocated. This also calls
173 * startup for the cpu DAI, platform, machine and codec DAI.
175 static int soc_pcm_open(struct snd_pcm_substream *substream)
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct snd_pcm_runtime *runtime = substream->runtime;
179 struct snd_soc_platform *platform = rtd->platform;
180 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
181 struct snd_soc_dai *codec_dai = rtd->codec_dai;
182 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
183 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
186 pm_runtime_get_sync(cpu_dai->dev);
187 pm_runtime_get_sync(codec_dai->dev);
188 pm_runtime_get_sync(platform->dev);
190 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
192 /* startup the audio subsystem */
193 if (cpu_dai->driver->ops->startup) {
194 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
196 dev_err(cpu_dai->dev, "ASoC: can't open interface"
197 " %s: %d\n", cpu_dai->name, ret);
202 if (platform->driver->ops && platform->driver->ops->open) {
203 ret = platform->driver->ops->open(substream);
205 dev_err(platform->dev, "ASoC: can't open platform"
206 " %s: %d\n", platform->name, ret);
211 if (codec_dai->driver->ops->startup) {
212 ret = codec_dai->driver->ops->startup(substream, codec_dai);
214 dev_err(codec_dai->dev, "ASoC: can't open codec"
215 " %s: %d\n", codec_dai->name, ret);
220 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
221 ret = rtd->dai_link->ops->startup(substream);
223 pr_err("ASoC: %s startup failed: %d\n",
224 rtd->dai_link->name, ret);
229 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
230 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
233 /* Check that the codec and cpu DAIs are compatible */
234 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
235 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
236 &cpu_dai_drv->playback);
238 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
239 &cpu_dai_drv->capture);
243 snd_pcm_limit_hw_rates(runtime);
244 if (!runtime->hw.rates) {
245 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
246 codec_dai->name, cpu_dai->name);
249 if (!runtime->hw.formats) {
250 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
251 codec_dai->name, cpu_dai->name);
254 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
255 runtime->hw.channels_min > runtime->hw.channels_max) {
256 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
257 codec_dai->name, cpu_dai->name);
261 soc_pcm_apply_msb(substream, codec_dai);
262 soc_pcm_apply_msb(substream, cpu_dai);
264 /* Symmetry only applies if we've already got an active stream. */
265 if (cpu_dai->active) {
266 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
271 if (codec_dai->active) {
272 ret = soc_pcm_apply_symmetry(substream, codec_dai);
277 pr_debug("ASoC: %s <-> %s info:\n",
278 codec_dai->name, cpu_dai->name);
279 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
280 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
281 runtime->hw.channels_max);
282 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
283 runtime->hw.rate_max);
286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
287 cpu_dai->playback_active++;
288 codec_dai->playback_active++;
290 cpu_dai->capture_active++;
291 codec_dai->capture_active++;
295 rtd->codec->active++;
296 mutex_unlock(&rtd->pcm_mutex);
300 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
301 rtd->dai_link->ops->shutdown(substream);
304 if (codec_dai->driver->ops->shutdown)
305 codec_dai->driver->ops->shutdown(substream, codec_dai);
308 if (platform->driver->ops && platform->driver->ops->close)
309 platform->driver->ops->close(substream);
312 if (cpu_dai->driver->ops->shutdown)
313 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
315 mutex_unlock(&rtd->pcm_mutex);
317 pm_runtime_put(platform->dev);
318 pm_runtime_put(codec_dai->dev);
319 pm_runtime_put(cpu_dai->dev);
325 * Power down the audio subsystem pmdown_time msecs after close is called.
326 * This is to ensure there are no pops or clicks in between any music tracks
327 * due to DAPM power cycling.
329 static void close_delayed_work(struct work_struct *work)
331 struct snd_soc_pcm_runtime *rtd =
332 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
333 struct snd_soc_dai *codec_dai = rtd->codec_dai;
335 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
337 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
338 codec_dai->driver->playback.stream_name,
339 codec_dai->playback_active ? "active" : "inactive",
340 rtd->pop_wait ? "yes" : "no");
342 /* are we waiting on this codec DAI stream */
343 if (rtd->pop_wait == 1) {
345 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
346 SND_SOC_DAPM_STREAM_STOP);
349 mutex_unlock(&rtd->pcm_mutex);
353 * Called by ALSA when a PCM substream is closed. Private data can be
354 * freed here. The cpu DAI, codec DAI, machine and platform are also
357 static int soc_pcm_close(struct snd_pcm_substream *substream)
359 struct snd_soc_pcm_runtime *rtd = substream->private_data;
360 struct snd_soc_platform *platform = rtd->platform;
361 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
362 struct snd_soc_dai *codec_dai = rtd->codec_dai;
363 struct snd_soc_codec *codec = rtd->codec;
365 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368 cpu_dai->playback_active--;
369 codec_dai->playback_active--;
371 cpu_dai->capture_active--;
372 codec_dai->capture_active--;
379 /* clear the corresponding DAIs rate when inactive */
380 if (!cpu_dai->active)
383 if (!codec_dai->active)
386 /* Muting the DAC suppresses artifacts caused during digital
387 * shutdown, for example from stopping clocks.
389 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
391 if (cpu_dai->driver->ops->shutdown)
392 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
394 if (codec_dai->driver->ops->shutdown)
395 codec_dai->driver->ops->shutdown(substream, codec_dai);
397 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
398 rtd->dai_link->ops->shutdown(substream);
400 if (platform->driver->ops && platform->driver->ops->close)
401 platform->driver->ops->close(substream);
402 cpu_dai->runtime = NULL;
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
405 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
406 rtd->dai_link->ignore_pmdown_time) {
407 /* powered down playback stream now */
408 snd_soc_dapm_stream_event(rtd,
409 SNDRV_PCM_STREAM_PLAYBACK,
410 SND_SOC_DAPM_STREAM_STOP);
412 /* start delayed pop wq here for playback streams */
414 queue_delayed_work(system_power_efficient_wq,
416 msecs_to_jiffies(rtd->pmdown_time));
419 /* capture streams can be powered down now */
420 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
421 SND_SOC_DAPM_STREAM_STOP);
424 mutex_unlock(&rtd->pcm_mutex);
426 pm_runtime_put(platform->dev);
427 pm_runtime_put(codec_dai->dev);
428 pm_runtime_put(cpu_dai->dev);
434 * Called by ALSA when the PCM substream is prepared, can set format, sample
435 * rate, etc. This function is non atomic and can be called multiple times,
436 * it can refer to the runtime info.
438 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
440 struct snd_soc_pcm_runtime *rtd = substream->private_data;
441 struct snd_soc_platform *platform = rtd->platform;
442 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
443 struct snd_soc_dai *codec_dai = rtd->codec_dai;
446 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
448 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
449 ret = rtd->dai_link->ops->prepare(substream);
451 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
457 if (platform->driver->ops && platform->driver->ops->prepare) {
458 ret = platform->driver->ops->prepare(substream);
460 dev_err(platform->dev, "ASoC: platform prepare error:"
466 if (codec_dai->driver->ops->prepare) {
467 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
469 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
475 if (cpu_dai->driver->ops->prepare) {
476 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
478 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
484 /* cancel any delayed stream shutdown that is pending */
485 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
488 cancel_delayed_work(&rtd->delayed_work);
491 snd_soc_dapm_stream_event(rtd, substream->stream,
492 SND_SOC_DAPM_STREAM_START);
494 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
497 mutex_unlock(&rtd->pcm_mutex);
502 * Called by ALSA when the hardware params are set by application. This
503 * function can also be called multiple times and can allocate buffers
504 * (using snd_pcm_lib_* ). It's non-atomic.
506 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *params)
509 struct snd_soc_pcm_runtime *rtd = substream->private_data;
510 struct snd_soc_platform *platform = rtd->platform;
511 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
512 struct snd_soc_dai *codec_dai = rtd->codec_dai;
515 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
517 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
518 ret = rtd->dai_link->ops->hw_params(substream, params);
520 dev_err(rtd->card->dev, "ASoC: machine hw_params"
521 " failed: %d\n", ret);
526 if (codec_dai->driver->ops->hw_params) {
527 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
529 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
530 " %d\n", codec_dai->name, ret);
535 if (cpu_dai->driver->ops->hw_params) {
536 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
538 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
544 if (platform->driver->ops && platform->driver->ops->hw_params) {
545 ret = platform->driver->ops->hw_params(substream, params);
547 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
548 platform->name, ret);
553 /* store the rate for each DAIs */
554 cpu_dai->rate = params_rate(params);
555 codec_dai->rate = params_rate(params);
558 mutex_unlock(&rtd->pcm_mutex);
562 if (cpu_dai->driver->ops->hw_free)
563 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
566 if (codec_dai->driver->ops->hw_free)
567 codec_dai->driver->ops->hw_free(substream, codec_dai);
570 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
571 rtd->dai_link->ops->hw_free(substream);
573 mutex_unlock(&rtd->pcm_mutex);
578 * Frees resources allocated by hw_params, can be called multiple times
580 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct snd_soc_platform *platform = rtd->platform;
584 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
585 struct snd_soc_dai *codec_dai = rtd->codec_dai;
586 struct snd_soc_codec *codec = rtd->codec;
588 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
590 /* apply codec digital mute */
592 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
594 /* free any machine hw params */
595 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
596 rtd->dai_link->ops->hw_free(substream);
598 /* free any DMA resources */
599 if (platform->driver->ops && platform->driver->ops->hw_free)
600 platform->driver->ops->hw_free(substream);
602 /* now free hw params for the DAIs */
603 if (codec_dai->driver->ops->hw_free)
604 codec_dai->driver->ops->hw_free(substream, codec_dai);
606 if (cpu_dai->driver->ops->hw_free)
607 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
609 mutex_unlock(&rtd->pcm_mutex);
613 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
615 struct snd_soc_pcm_runtime *rtd = substream->private_data;
616 struct snd_soc_platform *platform = rtd->platform;
617 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
618 struct snd_soc_dai *codec_dai = rtd->codec_dai;
621 if (codec_dai->driver->ops->trigger) {
622 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
627 if (platform->driver->ops && platform->driver->ops->trigger) {
628 ret = platform->driver->ops->trigger(substream, cmd);
633 if (cpu_dai->driver->ops->trigger) {
634 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
641 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
644 struct snd_soc_pcm_runtime *rtd = substream->private_data;
645 struct snd_soc_platform *platform = rtd->platform;
646 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
647 struct snd_soc_dai *codec_dai = rtd->codec_dai;
650 if (codec_dai->driver->ops->bespoke_trigger) {
651 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
656 if (platform->driver->bespoke_trigger) {
657 ret = platform->driver->bespoke_trigger(substream, cmd);
662 if (cpu_dai->driver->ops->bespoke_trigger) {
663 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
670 * soc level wrapper for pointer callback
671 * If cpu_dai, codec_dai, platform driver has the delay callback, than
672 * the runtime->delay will be updated accordingly.
674 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
676 struct snd_soc_pcm_runtime *rtd = substream->private_data;
677 struct snd_soc_platform *platform = rtd->platform;
678 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
679 struct snd_soc_dai *codec_dai = rtd->codec_dai;
680 struct snd_pcm_runtime *runtime = substream->runtime;
681 snd_pcm_uframes_t offset = 0;
682 snd_pcm_sframes_t delay = 0;
684 if (platform->driver->ops && platform->driver->ops->pointer)
685 offset = platform->driver->ops->pointer(substream);
687 if (cpu_dai->driver->ops->delay)
688 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
690 if (codec_dai->driver->ops->delay)
691 delay += codec_dai->driver->ops->delay(substream, codec_dai);
693 if (platform->driver->delay)
694 delay += platform->driver->delay(substream, codec_dai);
696 runtime->delay = delay;
701 /* connect a FE and BE */
702 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
703 struct snd_soc_pcm_runtime *be, int stream)
705 struct snd_soc_dpcm *dpcm;
707 /* only add new dpcms */
708 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
709 if (dpcm->be == be && dpcm->fe == fe)
713 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
719 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
720 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
721 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
722 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
724 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
725 stream ? "capture" : "playback", fe->dai_link->name,
726 stream ? "<-" : "->", be->dai_link->name);
728 #ifdef CONFIG_DEBUG_FS
729 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
730 fe->debugfs_dpcm_root, &dpcm->state);
735 /* reparent a BE onto another FE */
736 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
737 struct snd_soc_pcm_runtime *be, int stream)
739 struct snd_soc_dpcm *dpcm;
740 struct snd_pcm_substream *fe_substream, *be_substream;
742 /* reparent if BE is connected to other FEs */
743 if (!be->dpcm[stream].users)
746 be_substream = snd_soc_dpcm_get_substream(be, stream);
748 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
752 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
753 stream ? "capture" : "playback",
754 dpcm->fe->dai_link->name,
755 stream ? "<-" : "->", dpcm->be->dai_link->name);
757 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
758 be_substream->runtime = fe_substream->runtime;
763 /* disconnect a BE and FE */
764 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
766 struct snd_soc_dpcm *dpcm, *d;
768 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
769 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
770 stream ? "capture" : "playback",
771 dpcm->be->dai_link->name);
773 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
776 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
777 stream ? "capture" : "playback", fe->dai_link->name,
778 stream ? "<-" : "->", dpcm->be->dai_link->name);
780 /* BEs still alive need new FE */
781 dpcm_be_reparent(fe, dpcm->be, stream);
783 #ifdef CONFIG_DEBUG_FS
784 debugfs_remove(dpcm->debugfs_state);
786 list_del(&dpcm->list_be);
787 list_del(&dpcm->list_fe);
792 /* get BE for DAI widget and stream */
793 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
794 struct snd_soc_dapm_widget *widget, int stream)
796 struct snd_soc_pcm_runtime *be;
799 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
800 for (i = 0; i < card->num_links; i++) {
803 if (!be->dai_link->no_pcm)
806 if (be->cpu_dai->playback_widget == widget ||
807 be->codec_dai->playback_widget == widget)
812 for (i = 0; i < card->num_links; i++) {
815 if (!be->dai_link->no_pcm)
818 if (be->cpu_dai->capture_widget == widget ||
819 be->codec_dai->capture_widget == widget)
824 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
825 stream ? "capture" : "playback", widget->name);
829 static inline struct snd_soc_dapm_widget *
830 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
832 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
833 return rtd->cpu_dai->playback_widget;
835 return rtd->cpu_dai->capture_widget;
838 static inline struct snd_soc_dapm_widget *
839 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
841 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
842 return rtd->codec_dai->playback_widget;
844 return rtd->codec_dai->capture_widget;
847 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
848 struct snd_soc_dapm_widget *widget)
852 for (i = 0; i < list->num_widgets; i++) {
853 if (widget == list->widgets[i])
860 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
861 int stream, struct snd_soc_dapm_widget_list **list_)
863 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
864 struct snd_soc_dapm_widget_list *list;
867 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
868 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
872 /* get number of valid DAI paths and their widgets */
873 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
875 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
876 stream ? "capture" : "playback");
882 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
887 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
888 struct snd_soc_dapm_widget_list **list_)
890 struct snd_soc_dpcm *dpcm;
891 struct snd_soc_dapm_widget_list *list = *list_;
892 struct snd_soc_dapm_widget *widget;
895 /* Destroy any old FE <--> BE connections */
896 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
898 /* is there a valid CPU DAI widget for this BE */
899 widget = rtd_get_cpu_widget(dpcm->be, stream);
901 /* prune the BE if it's no longer in our active list */
902 if (widget && widget_in_list(list, widget))
905 /* is there a valid CODEC DAI widget for this BE */
906 widget = rtd_get_codec_widget(dpcm->be, stream);
908 /* prune the BE if it's no longer in our active list */
909 if (widget && widget_in_list(list, widget))
912 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
913 stream ? "capture" : "playback",
914 dpcm->be->dai_link->name, fe->dai_link->name);
915 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
916 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
920 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
924 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
925 struct snd_soc_dapm_widget_list **list_)
927 struct snd_soc_card *card = fe->card;
928 struct snd_soc_dapm_widget_list *list = *list_;
929 struct snd_soc_pcm_runtime *be;
932 /* Create any new FE <--> BE connections */
933 for (i = 0; i < list->num_widgets; i++) {
935 switch (list->widgets[i]->id) {
936 case snd_soc_dapm_dai_in:
937 case snd_soc_dapm_dai_out:
943 /* is there a valid BE rtd for this widget */
944 be = dpcm_get_be(card, list->widgets[i], stream);
946 dev_err(fe->dev, "ASoC: no BE found for %s\n",
947 list->widgets[i]->name);
951 /* make sure BE is a real BE */
952 if (!be->dai_link->no_pcm)
955 /* don't connect if FE is not running */
956 if (!fe->dpcm[stream].runtime)
959 /* newly connected FE and BE */
960 err = dpcm_be_connect(fe, be, stream);
962 dev_err(fe->dev, "ASoC: can't connect %s\n",
963 list->widgets[i]->name);
965 } else if (err == 0) /* already connected */
969 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
973 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
978 * Find the corresponding BE DAIs that source or sink audio to this
981 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
982 int stream, struct snd_soc_dapm_widget_list **list, int new)
985 return dpcm_add_paths(fe, stream, list);
987 return dpcm_prune_paths(fe, stream, list);
990 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
992 struct snd_soc_dpcm *dpcm;
994 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
995 dpcm->be->dpcm[stream].runtime_update =
996 SND_SOC_DPCM_UPDATE_NO;
999 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1002 struct snd_soc_dpcm *dpcm;
1004 /* disable any enabled and non active backends */
1005 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1007 struct snd_soc_pcm_runtime *be = dpcm->be;
1008 struct snd_pcm_substream *be_substream =
1009 snd_soc_dpcm_get_substream(be, stream);
1011 if (be->dpcm[stream].users == 0)
1012 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1013 stream ? "capture" : "playback",
1014 be->dpcm[stream].state);
1016 if (--be->dpcm[stream].users != 0)
1019 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1022 soc_pcm_close(be_substream);
1023 be_substream->runtime = NULL;
1024 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1028 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1030 struct snd_soc_dpcm *dpcm;
1033 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1034 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1036 struct snd_soc_pcm_runtime *be = dpcm->be;
1037 struct snd_pcm_substream *be_substream =
1038 snd_soc_dpcm_get_substream(be, stream);
1040 /* is this op for this BE ? */
1041 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1044 /* first time the dpcm is open ? */
1045 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1046 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1047 stream ? "capture" : "playback",
1048 be->dpcm[stream].state);
1050 if (be->dpcm[stream].users++ != 0)
1053 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1054 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1057 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1059 be_substream->runtime = be->dpcm[stream].runtime;
1060 err = soc_pcm_open(be_substream);
1062 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1063 be->dpcm[stream].users--;
1064 if (be->dpcm[stream].users < 0)
1065 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1066 stream ? "capture" : "playback",
1067 be->dpcm[stream].state);
1069 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1073 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1080 /* disable any enabled and non active backends */
1081 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1082 struct snd_soc_pcm_runtime *be = dpcm->be;
1083 struct snd_pcm_substream *be_substream =
1084 snd_soc_dpcm_get_substream(be, stream);
1086 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1089 if (be->dpcm[stream].users == 0)
1090 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1091 stream ? "capture" : "playback",
1092 be->dpcm[stream].state);
1094 if (--be->dpcm[stream].users != 0)
1097 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1100 soc_pcm_close(be_substream);
1101 be_substream->runtime = NULL;
1102 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1108 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1112 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1113 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1115 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1116 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1117 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1118 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1119 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1120 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1121 runtime->hw.rates = cpu_dai_drv->playback.rates;
1123 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1124 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1125 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1126 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1127 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1128 runtime->hw.rates = cpu_dai_drv->capture.rates;
1132 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1134 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1135 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1136 int stream = fe_substream->stream, ret = 0;
1138 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1140 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1142 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1146 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1148 /* start the DAI frontend */
1149 ret = soc_pcm_open(fe_substream);
1151 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1155 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1157 dpcm_set_fe_runtime(fe_substream);
1158 snd_pcm_limit_hw_rates(runtime);
1160 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1164 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1166 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1170 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1172 struct snd_soc_dpcm *dpcm;
1174 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1175 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1177 struct snd_soc_pcm_runtime *be = dpcm->be;
1178 struct snd_pcm_substream *be_substream =
1179 snd_soc_dpcm_get_substream(be, stream);
1181 /* is this op for this BE ? */
1182 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1185 if (be->dpcm[stream].users == 0)
1186 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1187 stream ? "capture" : "playback",
1188 be->dpcm[stream].state);
1190 if (--be->dpcm[stream].users != 0)
1193 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1194 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1197 dev_dbg(be->dev, "ASoC: close BE %s\n",
1198 dpcm->fe->dai_link->name);
1200 soc_pcm_close(be_substream);
1201 be_substream->runtime = NULL;
1203 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1208 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1210 struct snd_soc_pcm_runtime *fe = substream->private_data;
1211 int stream = substream->stream;
1213 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1215 /* shutdown the BEs */
1216 dpcm_be_dai_shutdown(fe, substream->stream);
1218 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1220 /* now shutdown the frontend */
1221 soc_pcm_close(substream);
1223 /* run the stream event for each BE */
1224 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1226 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1227 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1231 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1233 struct snd_soc_dpcm *dpcm;
1235 /* only hw_params backends that are either sinks or sources
1236 * to this frontend DAI */
1237 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1239 struct snd_soc_pcm_runtime *be = dpcm->be;
1240 struct snd_pcm_substream *be_substream =
1241 snd_soc_dpcm_get_substream(be, stream);
1243 /* is this op for this BE ? */
1244 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1247 /* only free hw when no longer used - check all FEs */
1248 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1251 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1252 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1254 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1255 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1258 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1259 dpcm->fe->dai_link->name);
1261 soc_pcm_hw_free(be_substream);
1263 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1269 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1271 struct snd_soc_pcm_runtime *fe = substream->private_data;
1272 int err, stream = substream->stream;
1274 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1275 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1277 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1279 /* call hw_free on the frontend */
1280 err = soc_pcm_hw_free(substream);
1282 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1283 fe->dai_link->name);
1285 /* only hw_params backends that are either sinks or sources
1286 * to this frontend DAI */
1287 err = dpcm_be_dai_hw_free(fe, stream);
1289 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1290 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1292 mutex_unlock(&fe->card->mutex);
1296 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1298 struct snd_soc_dpcm *dpcm;
1301 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1303 struct snd_soc_pcm_runtime *be = dpcm->be;
1304 struct snd_pcm_substream *be_substream =
1305 snd_soc_dpcm_get_substream(be, stream);
1307 /* is this op for this BE ? */
1308 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1311 /* only allow hw_params() if no connected FEs are running */
1312 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1315 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1316 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1317 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1320 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1321 dpcm->fe->dai_link->name);
1323 /* copy params for each dpcm */
1324 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1325 sizeof(struct snd_pcm_hw_params));
1327 /* perform any hw_params fixups */
1328 if (be->dai_link->be_hw_params_fixup) {
1329 ret = be->dai_link->be_hw_params_fixup(be,
1333 "ASoC: hw_params BE fixup failed %d\n",
1339 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1341 dev_err(dpcm->be->dev,
1342 "ASoC: hw_params BE failed %d\n", ret);
1346 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1351 /* disable any enabled and non active backends */
1352 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1353 struct snd_soc_pcm_runtime *be = dpcm->be;
1354 struct snd_pcm_substream *be_substream =
1355 snd_soc_dpcm_get_substream(be, stream);
1357 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1360 /* only allow hw_free() if no connected FEs are running */
1361 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1364 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1365 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1366 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1367 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1370 soc_pcm_hw_free(be_substream);
1376 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1377 struct snd_pcm_hw_params *params)
1379 struct snd_soc_pcm_runtime *fe = substream->private_data;
1380 int ret, stream = substream->stream;
1382 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1383 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1385 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1386 sizeof(struct snd_pcm_hw_params));
1387 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1389 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1393 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1394 fe->dai_link->name, params_rate(params),
1395 params_channels(params), params_format(params));
1397 /* call hw_params on the frontend */
1398 ret = soc_pcm_hw_params(substream, params);
1400 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1401 dpcm_be_dai_hw_free(fe, stream);
1403 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1406 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1407 mutex_unlock(&fe->card->mutex);
1411 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1412 struct snd_pcm_substream *substream, int cmd)
1416 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1417 dpcm->fe->dai_link->name, cmd);
1419 ret = soc_pcm_trigger(substream, cmd);
1421 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1426 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1429 struct snd_soc_dpcm *dpcm;
1432 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1434 struct snd_soc_pcm_runtime *be = dpcm->be;
1435 struct snd_pcm_substream *be_substream =
1436 snd_soc_dpcm_get_substream(be, stream);
1438 /* is this op for this BE ? */
1439 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1443 case SNDRV_PCM_TRIGGER_START:
1444 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1445 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1448 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1452 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1454 case SNDRV_PCM_TRIGGER_RESUME:
1455 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1458 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1462 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1464 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1465 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1468 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1472 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1474 case SNDRV_PCM_TRIGGER_STOP:
1475 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1478 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1481 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1485 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1487 case SNDRV_PCM_TRIGGER_SUSPEND:
1488 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1491 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1494 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1498 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1500 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1501 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1504 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1507 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1511 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1518 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1520 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1522 struct snd_soc_pcm_runtime *fe = substream->private_data;
1523 int stream = substream->stream, ret;
1524 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1526 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1529 case SND_SOC_DPCM_TRIGGER_PRE:
1530 /* call trigger on the frontend before the backend. */
1532 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1533 fe->dai_link->name, cmd);
1535 ret = soc_pcm_trigger(substream, cmd);
1537 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1541 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1543 case SND_SOC_DPCM_TRIGGER_POST:
1544 /* call trigger on the frontend after the backend. */
1546 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1548 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1552 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1553 fe->dai_link->name, cmd);
1555 ret = soc_pcm_trigger(substream, cmd);
1557 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1558 /* bespoke trigger() - handles both FE and BEs */
1560 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1561 fe->dai_link->name, cmd);
1563 ret = soc_pcm_bespoke_trigger(substream, cmd);
1565 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1570 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1571 fe->dai_link->name);
1577 case SNDRV_PCM_TRIGGER_START:
1578 case SNDRV_PCM_TRIGGER_RESUME:
1579 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1580 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1582 case SNDRV_PCM_TRIGGER_STOP:
1583 case SNDRV_PCM_TRIGGER_SUSPEND:
1584 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1585 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1590 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1594 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1596 struct snd_soc_dpcm *dpcm;
1599 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1601 struct snd_soc_pcm_runtime *be = dpcm->be;
1602 struct snd_pcm_substream *be_substream =
1603 snd_soc_dpcm_get_substream(be, stream);
1605 /* is this op for this BE ? */
1606 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1609 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1610 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1613 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1614 dpcm->fe->dai_link->name);
1616 ret = soc_pcm_prepare(be_substream);
1618 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1623 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1628 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1630 struct snd_soc_pcm_runtime *fe = substream->private_data;
1631 int stream = substream->stream, ret = 0;
1633 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1635 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1637 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1639 /* there is no point preparing this FE if there are no BEs */
1640 if (list_empty(&fe->dpcm[stream].be_clients)) {
1641 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1642 fe->dai_link->name);
1647 ret = dpcm_be_dai_prepare(fe, substream->stream);
1651 /* call prepare on the frontend */
1652 ret = soc_pcm_prepare(substream);
1654 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1655 fe->dai_link->name);
1659 /* run the stream event for each BE */
1660 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1661 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1664 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1665 mutex_unlock(&fe->card->mutex);
1670 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1671 unsigned int cmd, void *arg)
1673 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1674 struct snd_soc_platform *platform = rtd->platform;
1676 if (platform->driver->ops->ioctl)
1677 return platform->driver->ops->ioctl(substream, cmd, arg);
1678 return snd_pcm_lib_ioctl(substream, cmd, arg);
1681 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1683 struct snd_pcm_substream *substream =
1684 snd_soc_dpcm_get_substream(fe, stream);
1685 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1688 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1689 stream ? "capture" : "playback", fe->dai_link->name);
1691 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1692 /* call bespoke trigger - FE takes care of all BE triggers */
1693 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1694 fe->dai_link->name);
1696 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1698 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1700 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1701 fe->dai_link->name);
1703 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1705 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1708 err = dpcm_be_dai_hw_free(fe, stream);
1710 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1712 err = dpcm_be_dai_shutdown(fe, stream);
1714 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1716 /* run the stream event for each BE */
1717 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1722 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1724 struct snd_pcm_substream *substream =
1725 snd_soc_dpcm_get_substream(fe, stream);
1726 struct snd_soc_dpcm *dpcm;
1727 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1730 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1731 stream ? "capture" : "playback", fe->dai_link->name);
1733 /* Only start the BE if the FE is ready */
1734 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1735 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1738 /* startup must always be called for new BEs */
1739 ret = dpcm_be_dai_startup(fe, stream);
1743 /* keep going if FE state is > open */
1744 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1747 ret = dpcm_be_dai_hw_params(fe, stream);
1751 /* keep going if FE state is > hw_params */
1752 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1756 ret = dpcm_be_dai_prepare(fe, stream);
1760 /* run the stream event for each BE */
1761 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1763 /* keep going if FE state is > prepare */
1764 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1765 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1768 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1769 /* call trigger on the frontend - FE takes care of all BE triggers */
1770 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1771 fe->dai_link->name);
1773 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1775 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1779 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1780 fe->dai_link->name);
1782 ret = dpcm_be_dai_trigger(fe, stream,
1783 SNDRV_PCM_TRIGGER_START);
1785 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1793 dpcm_be_dai_hw_free(fe, stream);
1795 dpcm_be_dai_shutdown(fe, stream);
1797 /* disconnect any non started BEs */
1798 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1799 struct snd_soc_pcm_runtime *be = dpcm->be;
1800 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1801 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1807 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1811 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1812 ret = dpcm_run_update_startup(fe, stream);
1814 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1815 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1820 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1824 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1825 ret = dpcm_run_update_shutdown(fe, stream);
1827 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1828 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1833 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1836 int soc_dpcm_runtime_update(struct snd_soc_card *card)
1838 int i, old, new, paths;
1840 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1841 for (i = 0; i < card->num_rtd; i++) {
1842 struct snd_soc_dapm_widget_list *list;
1843 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1845 /* make sure link is FE */
1846 if (!fe->dai_link->dynamic)
1849 /* only check active links */
1850 if (!fe->cpu_dai->active)
1853 /* DAPM sync will call this to update DSP paths */
1854 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1855 fe->dai_link->name);
1857 /* skip if FE doesn't have playback capability */
1858 if (!fe->cpu_dai->driver->playback.channels_min)
1861 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1863 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1864 fe->dai_link->name, "playback");
1865 mutex_unlock(&card->mutex);
1869 /* update any new playback paths */
1870 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1872 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1873 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1874 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1877 /* update any old playback paths */
1878 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1880 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1886 /* skip if FE doesn't have capture capability */
1887 if (!fe->cpu_dai->driver->capture.channels_min)
1890 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1892 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1893 fe->dai_link->name, "capture");
1894 mutex_unlock(&card->mutex);
1898 /* update any new capture paths */
1899 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1901 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1902 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1903 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1906 /* update any old capture paths */
1907 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1909 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1914 dpcm_path_put(&list);
1917 mutex_unlock(&card->mutex);
1920 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1922 struct snd_soc_dpcm *dpcm;
1923 struct list_head *clients =
1924 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1926 list_for_each_entry(dpcm, clients, list_be) {
1928 struct snd_soc_pcm_runtime *be = dpcm->be;
1929 struct snd_soc_dai *dai = be->codec_dai;
1930 struct snd_soc_dai_driver *drv = dai->driver;
1932 if (be->dai_link->ignore_suspend)
1935 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1937 if (drv->ops->digital_mute && dai->playback_active)
1938 drv->ops->digital_mute(dai, mute);
1944 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1946 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1947 struct snd_soc_dpcm *dpcm;
1948 struct snd_soc_dapm_widget_list *list;
1950 int stream = fe_substream->stream;
1952 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1953 fe->dpcm[stream].runtime = fe_substream->runtime;
1955 if (dpcm_path_get(fe, stream, &list) <= 0) {
1956 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1957 fe->dai_link->name, stream ? "capture" : "playback");
1960 /* calculate valid and active FE <-> BE dpcms */
1961 dpcm_process_paths(fe, stream, &list, 1);
1963 ret = dpcm_fe_dai_startup(fe_substream);
1965 /* clean up all links */
1966 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1967 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1969 dpcm_be_disconnect(fe, stream);
1970 fe->dpcm[stream].runtime = NULL;
1973 dpcm_clear_pending_state(fe, stream);
1974 dpcm_path_put(&list);
1975 mutex_unlock(&fe->card->mutex);
1979 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1981 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1982 struct snd_soc_dpcm *dpcm;
1983 int stream = fe_substream->stream, ret;
1985 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1986 ret = dpcm_fe_dai_shutdown(fe_substream);
1988 /* mark FE's links ready to prune */
1989 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1990 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1992 dpcm_be_disconnect(fe, stream);
1994 fe->dpcm[stream].runtime = NULL;
1995 mutex_unlock(&fe->card->mutex);
1999 /* create a new pcm */
2000 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2002 struct snd_soc_platform *platform = rtd->platform;
2003 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2004 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2005 struct snd_pcm *pcm;
2007 int ret = 0, playback = 0, capture = 0;
2009 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2010 if (cpu_dai->driver->playback.channels_min)
2012 if (cpu_dai->driver->capture.channels_min)
2015 if (codec_dai->driver->playback.channels_min &&
2016 cpu_dai->driver->playback.channels_min)
2018 if (codec_dai->driver->capture.channels_min &&
2019 cpu_dai->driver->capture.channels_min)
2023 if (rtd->dai_link->playback_only) {
2028 if (rtd->dai_link->capture_only) {
2033 /* create the PCM */
2034 if (rtd->dai_link->no_pcm) {
2035 snprintf(new_name, sizeof(new_name), "(%s)",
2036 rtd->dai_link->stream_name);
2038 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2039 playback, capture, &pcm);
2041 if (rtd->dai_link->dynamic)
2042 snprintf(new_name, sizeof(new_name), "%s (*)",
2043 rtd->dai_link->stream_name);
2045 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2046 rtd->dai_link->stream_name, codec_dai->name, num);
2048 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2052 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2053 rtd->dai_link->name);
2056 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2058 /* DAPM dai link stream work */
2059 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2062 pcm->private_data = rtd;
2064 if (rtd->dai_link->no_pcm) {
2066 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2068 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2072 /* ASoC PCM operations */
2073 if (rtd->dai_link->dynamic) {
2074 rtd->ops.open = dpcm_fe_dai_open;
2075 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2076 rtd->ops.prepare = dpcm_fe_dai_prepare;
2077 rtd->ops.trigger = dpcm_fe_dai_trigger;
2078 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2079 rtd->ops.close = dpcm_fe_dai_close;
2080 rtd->ops.pointer = soc_pcm_pointer;
2081 rtd->ops.ioctl = soc_pcm_ioctl;
2083 rtd->ops.open = soc_pcm_open;
2084 rtd->ops.hw_params = soc_pcm_hw_params;
2085 rtd->ops.prepare = soc_pcm_prepare;
2086 rtd->ops.trigger = soc_pcm_trigger;
2087 rtd->ops.hw_free = soc_pcm_hw_free;
2088 rtd->ops.close = soc_pcm_close;
2089 rtd->ops.pointer = soc_pcm_pointer;
2090 rtd->ops.ioctl = soc_pcm_ioctl;
2093 if (platform->driver->ops) {
2094 rtd->ops.ack = platform->driver->ops->ack;
2095 rtd->ops.copy = platform->driver->ops->copy;
2096 rtd->ops.silence = platform->driver->ops->silence;
2097 rtd->ops.page = platform->driver->ops->page;
2098 rtd->ops.mmap = platform->driver->ops->mmap;
2102 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2105 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2107 if (platform->driver->pcm_new) {
2108 ret = platform->driver->pcm_new(rtd);
2110 dev_err(platform->dev,
2111 "ASoC: pcm constructor failed: %d\n",
2117 pcm->private_free = platform->driver->pcm_free;
2119 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2124 /* is the current PCM operation for this FE ? */
2125 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2127 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2131 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2133 /* is the current PCM operation for this BE ? */
2134 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2135 struct snd_soc_pcm_runtime *be, int stream)
2137 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2138 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2139 be->dpcm[stream].runtime_update))
2143 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2145 /* get the substream for this BE */
2146 struct snd_pcm_substream *
2147 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2149 return be->pcm->streams[stream].substream;
2151 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2153 /* get the BE runtime state */
2154 enum snd_soc_dpcm_state
2155 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2157 return be->dpcm[stream].state;
2159 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2161 /* set the BE runtime state */
2162 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2163 int stream, enum snd_soc_dpcm_state state)
2165 be->dpcm[stream].state = state;
2167 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2170 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2171 * are not running, paused or suspended for the specified stream direction.
2173 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2174 struct snd_soc_pcm_runtime *be, int stream)
2176 struct snd_soc_dpcm *dpcm;
2179 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2184 state = dpcm->fe->dpcm[stream].state;
2185 if (state == SND_SOC_DPCM_STATE_START ||
2186 state == SND_SOC_DPCM_STATE_PAUSED ||
2187 state == SND_SOC_DPCM_STATE_SUSPEND)
2191 /* it's safe to free/stop this BE DAI */
2194 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2197 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2198 * running, paused or suspended for the specified stream direction.
2200 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2201 struct snd_soc_pcm_runtime *be, int stream)
2203 struct snd_soc_dpcm *dpcm;
2206 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2211 state = dpcm->fe->dpcm[stream].state;
2212 if (state == SND_SOC_DPCM_STATE_START ||
2213 state == SND_SOC_DPCM_STATE_PAUSED ||
2214 state == SND_SOC_DPCM_STATE_SUSPEND ||
2215 state == SND_SOC_DPCM_STATE_PREPARE)
2219 /* it's safe to change hw_params */
2222 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2224 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2225 int cmd, struct snd_soc_platform *platform)
2227 if (platform->driver->ops->trigger)
2228 return platform->driver->ops->trigger(substream, cmd);
2231 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2233 #ifdef CONFIG_DEBUG_FS
2234 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2237 case SND_SOC_DPCM_STATE_NEW:
2239 case SND_SOC_DPCM_STATE_OPEN:
2241 case SND_SOC_DPCM_STATE_HW_PARAMS:
2243 case SND_SOC_DPCM_STATE_PREPARE:
2245 case SND_SOC_DPCM_STATE_START:
2247 case SND_SOC_DPCM_STATE_STOP:
2249 case SND_SOC_DPCM_STATE_SUSPEND:
2251 case SND_SOC_DPCM_STATE_PAUSED:
2253 case SND_SOC_DPCM_STATE_HW_FREE:
2255 case SND_SOC_DPCM_STATE_CLOSE:
2262 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2263 int stream, char *buf, size_t size)
2265 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2266 struct snd_soc_dpcm *dpcm;
2270 offset += snprintf(buf + offset, size - offset,
2271 "[%s - %s]\n", fe->dai_link->name,
2272 stream ? "Capture" : "Playback");
2274 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2275 dpcm_state_string(fe->dpcm[stream].state));
2277 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2278 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2279 offset += snprintf(buf + offset, size - offset,
2281 "Format = %s, Channels = %d, Rate = %d\n",
2282 snd_pcm_format_name(params_format(params)),
2283 params_channels(params),
2284 params_rate(params));
2287 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2289 if (list_empty(&fe->dpcm[stream].be_clients)) {
2290 offset += snprintf(buf + offset, size - offset,
2291 " No active DSP links\n");
2295 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2296 struct snd_soc_pcm_runtime *be = dpcm->be;
2297 params = &dpcm->hw_params;
2299 offset += snprintf(buf + offset, size - offset,
2300 "- %s\n", be->dai_link->name);
2302 offset += snprintf(buf + offset, size - offset,
2304 dpcm_state_string(be->dpcm[stream].state));
2306 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2307 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2308 offset += snprintf(buf + offset, size - offset,
2309 " Hardware Params: "
2310 "Format = %s, Channels = %d, Rate = %d\n",
2311 snd_pcm_format_name(params_format(params)),
2312 params_channels(params),
2313 params_rate(params));
2320 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2321 size_t count, loff_t *ppos)
2323 struct snd_soc_pcm_runtime *fe = file->private_data;
2324 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2327 buf = kmalloc(out_count, GFP_KERNEL);
2331 if (fe->cpu_dai->driver->playback.channels_min)
2332 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2333 buf + offset, out_count - offset);
2335 if (fe->cpu_dai->driver->capture.channels_min)
2336 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2337 buf + offset, out_count - offset);
2339 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2345 static const struct file_operations dpcm_state_fops = {
2346 .open = simple_open,
2347 .read = dpcm_state_read_file,
2348 .llseek = default_llseek,
2351 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2356 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2357 rtd->card->debugfs_card_root);
2358 if (!rtd->debugfs_dpcm_root) {
2360 "ASoC: Failed to create dpcm debugfs directory %s\n",
2361 rtd->dai_link->name);
2365 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2366 rtd->debugfs_dpcm_root,
2367 rtd, &dpcm_state_fops);