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 schedule_delayed_work(&rtd->delayed_work,
415 msecs_to_jiffies(rtd->pmdown_time));
418 /* capture streams can be powered down now */
419 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
420 SND_SOC_DAPM_STREAM_STOP);
423 mutex_unlock(&rtd->pcm_mutex);
425 pm_runtime_put(platform->dev);
426 pm_runtime_put(codec_dai->dev);
427 pm_runtime_put(cpu_dai->dev);
433 * Called by ALSA when the PCM substream is prepared, can set format, sample
434 * rate, etc. This function is non atomic and can be called multiple times,
435 * it can refer to the runtime info.
437 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
439 struct snd_soc_pcm_runtime *rtd = substream->private_data;
440 struct snd_soc_platform *platform = rtd->platform;
441 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
442 struct snd_soc_dai *codec_dai = rtd->codec_dai;
445 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
447 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
448 ret = rtd->dai_link->ops->prepare(substream);
450 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
456 if (platform->driver->ops && platform->driver->ops->prepare) {
457 ret = platform->driver->ops->prepare(substream);
459 dev_err(platform->dev, "ASoC: platform prepare error:"
465 if (codec_dai->driver->ops->prepare) {
466 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
468 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
474 if (cpu_dai->driver->ops->prepare) {
475 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
477 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
483 /* cancel any delayed stream shutdown that is pending */
484 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
487 cancel_delayed_work(&rtd->delayed_work);
490 snd_soc_dapm_stream_event(rtd, substream->stream,
491 SND_SOC_DAPM_STREAM_START);
493 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
496 mutex_unlock(&rtd->pcm_mutex);
501 * Called by ALSA when the hardware params are set by application. This
502 * function can also be called multiple times and can allocate buffers
503 * (using snd_pcm_lib_* ). It's non-atomic.
505 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_hw_params *params)
508 struct snd_soc_pcm_runtime *rtd = substream->private_data;
509 struct snd_soc_platform *platform = rtd->platform;
510 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
511 struct snd_soc_dai *codec_dai = rtd->codec_dai;
514 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
516 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
517 ret = rtd->dai_link->ops->hw_params(substream, params);
519 dev_err(rtd->card->dev, "ASoC: machine hw_params"
520 " failed: %d\n", ret);
525 if (codec_dai->driver->ops->hw_params) {
526 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
528 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
529 " %d\n", codec_dai->name, ret);
534 if (cpu_dai->driver->ops->hw_params) {
535 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
537 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
543 if (platform->driver->ops && platform->driver->ops->hw_params) {
544 ret = platform->driver->ops->hw_params(substream, params);
546 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
547 platform->name, ret);
552 /* store the rate for each DAIs */
553 cpu_dai->rate = params_rate(params);
554 codec_dai->rate = params_rate(params);
557 mutex_unlock(&rtd->pcm_mutex);
561 if (cpu_dai->driver->ops->hw_free)
562 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
565 if (codec_dai->driver->ops->hw_free)
566 codec_dai->driver->ops->hw_free(substream, codec_dai);
569 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
570 rtd->dai_link->ops->hw_free(substream);
572 mutex_unlock(&rtd->pcm_mutex);
577 * Frees resources allocated by hw_params, can be called multiple times
579 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
581 struct snd_soc_pcm_runtime *rtd = substream->private_data;
582 struct snd_soc_platform *platform = rtd->platform;
583 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
584 struct snd_soc_dai *codec_dai = rtd->codec_dai;
585 struct snd_soc_codec *codec = rtd->codec;
587 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
589 /* apply codec digital mute */
591 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
593 /* free any machine hw params */
594 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
595 rtd->dai_link->ops->hw_free(substream);
597 /* free any DMA resources */
598 if (platform->driver->ops && platform->driver->ops->hw_free)
599 platform->driver->ops->hw_free(substream);
601 /* now free hw params for the DAIs */
602 if (codec_dai->driver->ops->hw_free)
603 codec_dai->driver->ops->hw_free(substream, codec_dai);
605 if (cpu_dai->driver->ops->hw_free)
606 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
608 mutex_unlock(&rtd->pcm_mutex);
612 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
614 struct snd_soc_pcm_runtime *rtd = substream->private_data;
615 struct snd_soc_platform *platform = rtd->platform;
616 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
617 struct snd_soc_dai *codec_dai = rtd->codec_dai;
620 if (codec_dai->driver->ops->trigger) {
621 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
626 if (platform->driver->ops && platform->driver->ops->trigger) {
627 ret = platform->driver->ops->trigger(substream, cmd);
632 if (cpu_dai->driver->ops->trigger) {
633 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
640 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
643 struct snd_soc_pcm_runtime *rtd = substream->private_data;
644 struct snd_soc_platform *platform = rtd->platform;
645 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
646 struct snd_soc_dai *codec_dai = rtd->codec_dai;
649 if (codec_dai->driver->ops->bespoke_trigger) {
650 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
655 if (platform->driver->bespoke_trigger) {
656 ret = platform->driver->bespoke_trigger(substream, cmd);
661 if (cpu_dai->driver->ops->bespoke_trigger) {
662 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
669 * soc level wrapper for pointer callback
670 * If cpu_dai, codec_dai, platform driver has the delay callback, than
671 * the runtime->delay will be updated accordingly.
673 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
675 struct snd_soc_pcm_runtime *rtd = substream->private_data;
676 struct snd_soc_platform *platform = rtd->platform;
677 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
678 struct snd_soc_dai *codec_dai = rtd->codec_dai;
679 struct snd_pcm_runtime *runtime = substream->runtime;
680 snd_pcm_uframes_t offset = 0;
681 snd_pcm_sframes_t delay = 0;
683 if (platform->driver->ops && platform->driver->ops->pointer)
684 offset = platform->driver->ops->pointer(substream);
686 if (cpu_dai->driver->ops->delay)
687 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
689 if (codec_dai->driver->ops->delay)
690 delay += codec_dai->driver->ops->delay(substream, codec_dai);
692 if (platform->driver->delay)
693 delay += platform->driver->delay(substream, codec_dai);
695 runtime->delay = delay;
700 /* connect a FE and BE */
701 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
702 struct snd_soc_pcm_runtime *be, int stream)
704 struct snd_soc_dpcm *dpcm;
706 /* only add new dpcms */
707 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
708 if (dpcm->be == be && dpcm->fe == fe)
712 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
718 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
719 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
720 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
721 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
723 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
724 stream ? "capture" : "playback", fe->dai_link->name,
725 stream ? "<-" : "->", be->dai_link->name);
727 #ifdef CONFIG_DEBUG_FS
728 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
729 fe->debugfs_dpcm_root, &dpcm->state);
734 /* reparent a BE onto another FE */
735 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
736 struct snd_soc_pcm_runtime *be, int stream)
738 struct snd_soc_dpcm *dpcm;
739 struct snd_pcm_substream *fe_substream, *be_substream;
741 /* reparent if BE is connected to other FEs */
742 if (!be->dpcm[stream].users)
745 be_substream = snd_soc_dpcm_get_substream(be, stream);
747 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
751 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
752 stream ? "capture" : "playback",
753 dpcm->fe->dai_link->name,
754 stream ? "<-" : "->", dpcm->be->dai_link->name);
756 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
757 be_substream->runtime = fe_substream->runtime;
762 /* disconnect a BE and FE */
763 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
765 struct snd_soc_dpcm *dpcm, *d;
767 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
768 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
769 stream ? "capture" : "playback",
770 dpcm->be->dai_link->name);
772 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
775 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
776 stream ? "capture" : "playback", fe->dai_link->name,
777 stream ? "<-" : "->", dpcm->be->dai_link->name);
779 /* BEs still alive need new FE */
780 dpcm_be_reparent(fe, dpcm->be, stream);
782 #ifdef CONFIG_DEBUG_FS
783 debugfs_remove(dpcm->debugfs_state);
785 list_del(&dpcm->list_be);
786 list_del(&dpcm->list_fe);
791 /* get BE for DAI widget and stream */
792 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
793 struct snd_soc_dapm_widget *widget, int stream)
795 struct snd_soc_pcm_runtime *be;
798 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
799 for (i = 0; i < card->num_links; i++) {
802 if (!be->dai_link->no_pcm)
805 if (be->cpu_dai->playback_widget == widget ||
806 be->codec_dai->playback_widget == widget)
811 for (i = 0; i < card->num_links; i++) {
814 if (!be->dai_link->no_pcm)
817 if (be->cpu_dai->capture_widget == widget ||
818 be->codec_dai->capture_widget == widget)
823 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
824 stream ? "capture" : "playback", widget->name);
828 static inline struct snd_soc_dapm_widget *
829 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
831 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
832 return rtd->cpu_dai->playback_widget;
834 return rtd->cpu_dai->capture_widget;
837 static inline struct snd_soc_dapm_widget *
838 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
840 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
841 return rtd->codec_dai->playback_widget;
843 return rtd->codec_dai->capture_widget;
846 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
847 struct snd_soc_dapm_widget *widget)
851 for (i = 0; i < list->num_widgets; i++) {
852 if (widget == list->widgets[i])
859 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
860 int stream, struct snd_soc_dapm_widget_list **list_)
862 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
863 struct snd_soc_dapm_widget_list *list;
866 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
867 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
871 /* get number of valid DAI paths and their widgets */
872 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
874 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
875 stream ? "capture" : "playback");
881 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
886 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
887 struct snd_soc_dapm_widget_list **list_)
889 struct snd_soc_dpcm *dpcm;
890 struct snd_soc_dapm_widget_list *list = *list_;
891 struct snd_soc_dapm_widget *widget;
894 /* Destroy any old FE <--> BE connections */
895 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
897 /* is there a valid CPU DAI widget for this BE */
898 widget = rtd_get_cpu_widget(dpcm->be, stream);
900 /* prune the BE if it's no longer in our active list */
901 if (widget && widget_in_list(list, widget))
904 /* is there a valid CODEC DAI widget for this BE */
905 widget = rtd_get_codec_widget(dpcm->be, stream);
907 /* prune the BE if it's no longer in our active list */
908 if (widget && widget_in_list(list, widget))
911 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
912 stream ? "capture" : "playback",
913 dpcm->be->dai_link->name, fe->dai_link->name);
914 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
915 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
919 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
923 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
924 struct snd_soc_dapm_widget_list **list_)
926 struct snd_soc_card *card = fe->card;
927 struct snd_soc_dapm_widget_list *list = *list_;
928 struct snd_soc_pcm_runtime *be;
931 /* Create any new FE <--> BE connections */
932 for (i = 0; i < list->num_widgets; i++) {
934 switch (list->widgets[i]->id) {
935 case snd_soc_dapm_dai_in:
936 case snd_soc_dapm_dai_out:
942 /* is there a valid BE rtd for this widget */
943 be = dpcm_get_be(card, list->widgets[i], stream);
945 dev_err(fe->dev, "ASoC: no BE found for %s\n",
946 list->widgets[i]->name);
950 /* make sure BE is a real BE */
951 if (!be->dai_link->no_pcm)
954 /* don't connect if FE is not running */
955 if (!fe->dpcm[stream].runtime)
958 /* newly connected FE and BE */
959 err = dpcm_be_connect(fe, be, stream);
961 dev_err(fe->dev, "ASoC: can't connect %s\n",
962 list->widgets[i]->name);
964 } else if (err == 0) /* already connected */
968 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
972 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
977 * Find the corresponding BE DAIs that source or sink audio to this
980 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
981 int stream, struct snd_soc_dapm_widget_list **list, int new)
984 return dpcm_add_paths(fe, stream, list);
986 return dpcm_prune_paths(fe, stream, list);
989 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
991 struct snd_soc_dpcm *dpcm;
993 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
994 dpcm->be->dpcm[stream].runtime_update =
995 SND_SOC_DPCM_UPDATE_NO;
998 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1001 struct snd_soc_dpcm *dpcm;
1003 /* disable any enabled and non active backends */
1004 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1006 struct snd_soc_pcm_runtime *be = dpcm->be;
1007 struct snd_pcm_substream *be_substream =
1008 snd_soc_dpcm_get_substream(be, stream);
1010 if (be->dpcm[stream].users == 0)
1011 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1012 stream ? "capture" : "playback",
1013 be->dpcm[stream].state);
1015 if (--be->dpcm[stream].users != 0)
1018 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1021 soc_pcm_close(be_substream);
1022 be_substream->runtime = NULL;
1023 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1027 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1029 struct snd_soc_dpcm *dpcm;
1032 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1033 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1035 struct snd_soc_pcm_runtime *be = dpcm->be;
1036 struct snd_pcm_substream *be_substream =
1037 snd_soc_dpcm_get_substream(be, stream);
1039 /* is this op for this BE ? */
1040 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1043 /* first time the dpcm is open ? */
1044 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1045 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1046 stream ? "capture" : "playback",
1047 be->dpcm[stream].state);
1049 if (be->dpcm[stream].users++ != 0)
1052 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1053 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1056 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1058 be_substream->runtime = be->dpcm[stream].runtime;
1059 err = soc_pcm_open(be_substream);
1061 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1062 be->dpcm[stream].users--;
1063 if (be->dpcm[stream].users < 0)
1064 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1065 stream ? "capture" : "playback",
1066 be->dpcm[stream].state);
1068 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1072 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1079 /* disable any enabled and non active backends */
1080 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1081 struct snd_soc_pcm_runtime *be = dpcm->be;
1082 struct snd_pcm_substream *be_substream =
1083 snd_soc_dpcm_get_substream(be, stream);
1085 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1088 if (be->dpcm[stream].users == 0)
1089 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1090 stream ? "capture" : "playback",
1091 be->dpcm[stream].state);
1093 if (--be->dpcm[stream].users != 0)
1096 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1099 soc_pcm_close(be_substream);
1100 be_substream->runtime = NULL;
1101 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1107 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1111 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1112 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1114 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1115 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1116 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1117 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1118 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1119 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1120 runtime->hw.rates = cpu_dai_drv->playback.rates;
1122 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1123 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1124 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1125 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1126 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1127 runtime->hw.rates = cpu_dai_drv->capture.rates;
1131 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1133 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1134 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1135 int stream = fe_substream->stream, ret = 0;
1137 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1139 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1141 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1145 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1147 /* start the DAI frontend */
1148 ret = soc_pcm_open(fe_substream);
1150 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1154 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1156 dpcm_set_fe_runtime(fe_substream);
1157 snd_pcm_limit_hw_rates(runtime);
1159 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1163 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1165 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1169 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1171 struct snd_soc_dpcm *dpcm;
1173 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1174 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1176 struct snd_soc_pcm_runtime *be = dpcm->be;
1177 struct snd_pcm_substream *be_substream =
1178 snd_soc_dpcm_get_substream(be, stream);
1180 /* is this op for this BE ? */
1181 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1184 if (be->dpcm[stream].users == 0)
1185 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1186 stream ? "capture" : "playback",
1187 be->dpcm[stream].state);
1189 if (--be->dpcm[stream].users != 0)
1192 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1193 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1196 dev_dbg(be->dev, "ASoC: close BE %s\n",
1197 dpcm->fe->dai_link->name);
1199 soc_pcm_close(be_substream);
1200 be_substream->runtime = NULL;
1202 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1207 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1209 struct snd_soc_pcm_runtime *fe = substream->private_data;
1210 int stream = substream->stream;
1212 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1214 /* shutdown the BEs */
1215 dpcm_be_dai_shutdown(fe, substream->stream);
1217 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1219 /* now shutdown the frontend */
1220 soc_pcm_close(substream);
1222 /* run the stream event for each BE */
1223 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1225 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1226 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1230 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1232 struct snd_soc_dpcm *dpcm;
1234 /* only hw_params backends that are either sinks or sources
1235 * to this frontend DAI */
1236 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1238 struct snd_soc_pcm_runtime *be = dpcm->be;
1239 struct snd_pcm_substream *be_substream =
1240 snd_soc_dpcm_get_substream(be, stream);
1242 /* is this op for this BE ? */
1243 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1246 /* only free hw when no longer used - check all FEs */
1247 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1250 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1251 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1252 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1254 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1257 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1258 dpcm->fe->dai_link->name);
1260 soc_pcm_hw_free(be_substream);
1262 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1268 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1270 struct snd_soc_pcm_runtime *fe = substream->private_data;
1271 int err, stream = substream->stream;
1273 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1274 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1276 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1278 /* call hw_free on the frontend */
1279 err = soc_pcm_hw_free(substream);
1281 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1282 fe->dai_link->name);
1284 /* only hw_params backends that are either sinks or sources
1285 * to this frontend DAI */
1286 err = dpcm_be_dai_hw_free(fe, stream);
1288 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1289 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1291 mutex_unlock(&fe->card->mutex);
1295 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1297 struct snd_soc_dpcm *dpcm;
1300 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1302 struct snd_soc_pcm_runtime *be = dpcm->be;
1303 struct snd_pcm_substream *be_substream =
1304 snd_soc_dpcm_get_substream(be, stream);
1306 /* is this op for this BE ? */
1307 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1310 /* only allow hw_params() if no connected FEs are running */
1311 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1314 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1315 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1316 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1319 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1320 dpcm->fe->dai_link->name);
1322 /* copy params for each dpcm */
1323 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1324 sizeof(struct snd_pcm_hw_params));
1326 /* perform any hw_params fixups */
1327 if (be->dai_link->be_hw_params_fixup) {
1328 ret = be->dai_link->be_hw_params_fixup(be,
1332 "ASoC: hw_params BE fixup failed %d\n",
1338 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1340 dev_err(dpcm->be->dev,
1341 "ASoC: hw_params BE failed %d\n", ret);
1345 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1350 /* disable any enabled and non active backends */
1351 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1352 struct snd_soc_pcm_runtime *be = dpcm->be;
1353 struct snd_pcm_substream *be_substream =
1354 snd_soc_dpcm_get_substream(be, stream);
1356 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1359 /* only allow hw_free() if no connected FEs are running */
1360 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1363 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1364 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1365 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1366 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1369 soc_pcm_hw_free(be_substream);
1375 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1376 struct snd_pcm_hw_params *params)
1378 struct snd_soc_pcm_runtime *fe = substream->private_data;
1379 int ret, stream = substream->stream;
1381 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1382 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1384 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1385 sizeof(struct snd_pcm_hw_params));
1386 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1388 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1392 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1393 fe->dai_link->name, params_rate(params),
1394 params_channels(params), params_format(params));
1396 /* call hw_params on the frontend */
1397 ret = soc_pcm_hw_params(substream, params);
1399 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1400 dpcm_be_dai_hw_free(fe, stream);
1402 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1405 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1406 mutex_unlock(&fe->card->mutex);
1410 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1411 struct snd_pcm_substream *substream, int cmd)
1415 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1416 dpcm->fe->dai_link->name, cmd);
1418 ret = soc_pcm_trigger(substream, cmd);
1420 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1425 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1428 struct snd_soc_dpcm *dpcm;
1431 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1433 struct snd_soc_pcm_runtime *be = dpcm->be;
1434 struct snd_pcm_substream *be_substream =
1435 snd_soc_dpcm_get_substream(be, stream);
1437 /* is this op for this BE ? */
1438 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1442 case SNDRV_PCM_TRIGGER_START:
1443 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1444 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1447 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1451 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1453 case SNDRV_PCM_TRIGGER_RESUME:
1454 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1457 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1461 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1463 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1464 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1467 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1471 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1473 case SNDRV_PCM_TRIGGER_STOP:
1474 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1477 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1480 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1484 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1486 case SNDRV_PCM_TRIGGER_SUSPEND:
1487 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1490 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1493 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1497 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1499 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1500 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1503 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1506 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1510 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1517 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1519 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1521 struct snd_soc_pcm_runtime *fe = substream->private_data;
1522 int stream = substream->stream, ret;
1523 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1525 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1528 case SND_SOC_DPCM_TRIGGER_PRE:
1529 /* call trigger on the frontend before the backend. */
1531 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1532 fe->dai_link->name, cmd);
1534 ret = soc_pcm_trigger(substream, cmd);
1536 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1540 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1542 case SND_SOC_DPCM_TRIGGER_POST:
1543 /* call trigger on the frontend after the backend. */
1545 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1547 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1551 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1552 fe->dai_link->name, cmd);
1554 ret = soc_pcm_trigger(substream, cmd);
1556 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1557 /* bespoke trigger() - handles both FE and BEs */
1559 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1560 fe->dai_link->name, cmd);
1562 ret = soc_pcm_bespoke_trigger(substream, cmd);
1564 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1569 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1570 fe->dai_link->name);
1576 case SNDRV_PCM_TRIGGER_START:
1577 case SNDRV_PCM_TRIGGER_RESUME:
1578 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1579 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1581 case SNDRV_PCM_TRIGGER_STOP:
1582 case SNDRV_PCM_TRIGGER_SUSPEND:
1583 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1584 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1589 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1593 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1595 struct snd_soc_dpcm *dpcm;
1598 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1600 struct snd_soc_pcm_runtime *be = dpcm->be;
1601 struct snd_pcm_substream *be_substream =
1602 snd_soc_dpcm_get_substream(be, stream);
1604 /* is this op for this BE ? */
1605 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1608 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1609 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1612 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1613 dpcm->fe->dai_link->name);
1615 ret = soc_pcm_prepare(be_substream);
1617 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1622 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1627 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1629 struct snd_soc_pcm_runtime *fe = substream->private_data;
1630 int stream = substream->stream, ret = 0;
1632 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1634 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1636 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1638 /* there is no point preparing this FE if there are no BEs */
1639 if (list_empty(&fe->dpcm[stream].be_clients)) {
1640 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1641 fe->dai_link->name);
1646 ret = dpcm_be_dai_prepare(fe, substream->stream);
1650 /* call prepare on the frontend */
1651 ret = soc_pcm_prepare(substream);
1653 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1654 fe->dai_link->name);
1658 /* run the stream event for each BE */
1659 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1660 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1663 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1664 mutex_unlock(&fe->card->mutex);
1669 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1670 unsigned int cmd, void *arg)
1672 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1673 struct snd_soc_platform *platform = rtd->platform;
1675 if (platform->driver->ops->ioctl)
1676 return platform->driver->ops->ioctl(substream, cmd, arg);
1677 return snd_pcm_lib_ioctl(substream, cmd, arg);
1680 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1682 struct snd_pcm_substream *substream =
1683 snd_soc_dpcm_get_substream(fe, stream);
1684 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1687 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1688 stream ? "capture" : "playback", fe->dai_link->name);
1690 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1691 /* call bespoke trigger - FE takes care of all BE triggers */
1692 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1693 fe->dai_link->name);
1695 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1697 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1699 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1700 fe->dai_link->name);
1702 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1704 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1707 err = dpcm_be_dai_hw_free(fe, stream);
1709 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1711 err = dpcm_be_dai_shutdown(fe, stream);
1713 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1715 /* run the stream event for each BE */
1716 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1721 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1723 struct snd_pcm_substream *substream =
1724 snd_soc_dpcm_get_substream(fe, stream);
1725 struct snd_soc_dpcm *dpcm;
1726 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1729 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1730 stream ? "capture" : "playback", fe->dai_link->name);
1732 /* Only start the BE if the FE is ready */
1733 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1734 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1737 /* startup must always be called for new BEs */
1738 ret = dpcm_be_dai_startup(fe, stream);
1742 /* keep going if FE state is > open */
1743 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1746 ret = dpcm_be_dai_hw_params(fe, stream);
1750 /* keep going if FE state is > hw_params */
1751 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1755 ret = dpcm_be_dai_prepare(fe, stream);
1759 /* run the stream event for each BE */
1760 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1762 /* keep going if FE state is > prepare */
1763 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1764 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1767 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1768 /* call trigger on the frontend - FE takes care of all BE triggers */
1769 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1770 fe->dai_link->name);
1772 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1774 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1778 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1779 fe->dai_link->name);
1781 ret = dpcm_be_dai_trigger(fe, stream,
1782 SNDRV_PCM_TRIGGER_START);
1784 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1792 dpcm_be_dai_hw_free(fe, stream);
1794 dpcm_be_dai_shutdown(fe, stream);
1796 /* disconnect any non started BEs */
1797 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1798 struct snd_soc_pcm_runtime *be = dpcm->be;
1799 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1800 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1806 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1810 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1811 ret = dpcm_run_update_startup(fe, stream);
1813 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1814 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1819 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1823 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1824 ret = dpcm_run_update_shutdown(fe, stream);
1826 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1827 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1832 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1835 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1837 struct snd_soc_card *card;
1838 int i, old, new, paths;
1841 card = widget->codec->card;
1842 else if (widget->platform)
1843 card = widget->platform->card;
1847 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1848 for (i = 0; i < card->num_rtd; i++) {
1849 struct snd_soc_dapm_widget_list *list;
1850 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1852 /* make sure link is FE */
1853 if (!fe->dai_link->dynamic)
1856 /* only check active links */
1857 if (!fe->cpu_dai->active)
1860 /* DAPM sync will call this to update DSP paths */
1861 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1862 fe->dai_link->name);
1864 /* skip if FE doesn't have playback capability */
1865 if (!fe->cpu_dai->driver->playback.channels_min)
1868 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1870 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1871 fe->dai_link->name, "playback");
1872 mutex_unlock(&card->mutex);
1876 /* update any new playback paths */
1877 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1879 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1880 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1884 /* update any old playback paths */
1885 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1887 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1888 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1889 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1893 /* skip if FE doesn't have capture capability */
1894 if (!fe->cpu_dai->driver->capture.channels_min)
1897 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1899 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1900 fe->dai_link->name, "capture");
1901 mutex_unlock(&card->mutex);
1905 /* update any new capture paths */
1906 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1908 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1909 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1913 /* update any old capture paths */
1914 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1916 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1917 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1918 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1921 dpcm_path_put(&list);
1924 mutex_unlock(&card->mutex);
1927 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1929 struct snd_soc_dpcm *dpcm;
1930 struct list_head *clients =
1931 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1933 list_for_each_entry(dpcm, clients, list_be) {
1935 struct snd_soc_pcm_runtime *be = dpcm->be;
1936 struct snd_soc_dai *dai = be->codec_dai;
1937 struct snd_soc_dai_driver *drv = dai->driver;
1939 if (be->dai_link->ignore_suspend)
1942 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1944 if (drv->ops->digital_mute && dai->playback_active)
1945 drv->ops->digital_mute(dai, mute);
1951 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1953 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1954 struct snd_soc_dpcm *dpcm;
1955 struct snd_soc_dapm_widget_list *list;
1957 int stream = fe_substream->stream;
1959 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1960 fe->dpcm[stream].runtime = fe_substream->runtime;
1962 if (dpcm_path_get(fe, stream, &list) <= 0) {
1963 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1964 fe->dai_link->name, stream ? "capture" : "playback");
1967 /* calculate valid and active FE <-> BE dpcms */
1968 dpcm_process_paths(fe, stream, &list, 1);
1970 ret = dpcm_fe_dai_startup(fe_substream);
1972 /* clean up all links */
1973 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1974 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1976 dpcm_be_disconnect(fe, stream);
1977 fe->dpcm[stream].runtime = NULL;
1980 dpcm_clear_pending_state(fe, stream);
1981 dpcm_path_put(&list);
1982 mutex_unlock(&fe->card->mutex);
1986 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1988 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1989 struct snd_soc_dpcm *dpcm;
1990 int stream = fe_substream->stream, ret;
1992 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1993 ret = dpcm_fe_dai_shutdown(fe_substream);
1995 /* mark FE's links ready to prune */
1996 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1997 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1999 dpcm_be_disconnect(fe, stream);
2001 fe->dpcm[stream].runtime = NULL;
2002 mutex_unlock(&fe->card->mutex);
2006 /* create a new pcm */
2007 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2009 struct snd_soc_platform *platform = rtd->platform;
2010 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2011 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2012 struct snd_pcm *pcm;
2014 int ret = 0, playback = 0, capture = 0;
2016 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2017 if (cpu_dai->driver->playback.channels_min)
2019 if (cpu_dai->driver->capture.channels_min)
2022 if (codec_dai->driver->playback.channels_min &&
2023 cpu_dai->driver->playback.channels_min)
2025 if (codec_dai->driver->capture.channels_min &&
2026 cpu_dai->driver->capture.channels_min)
2030 /* create the PCM */
2031 if (rtd->dai_link->no_pcm) {
2032 snprintf(new_name, sizeof(new_name), "(%s)",
2033 rtd->dai_link->stream_name);
2035 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2036 playback, capture, &pcm);
2038 if (rtd->dai_link->dynamic)
2039 snprintf(new_name, sizeof(new_name), "%s (*)",
2040 rtd->dai_link->stream_name);
2042 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2043 rtd->dai_link->stream_name, codec_dai->name, num);
2045 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2049 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2050 rtd->dai_link->name);
2053 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2055 /* DAPM dai link stream work */
2056 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2059 pcm->private_data = rtd;
2061 if (rtd->dai_link->no_pcm) {
2063 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2065 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2069 /* ASoC PCM operations */
2070 if (rtd->dai_link->dynamic) {
2071 rtd->ops.open = dpcm_fe_dai_open;
2072 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2073 rtd->ops.prepare = dpcm_fe_dai_prepare;
2074 rtd->ops.trigger = dpcm_fe_dai_trigger;
2075 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2076 rtd->ops.close = dpcm_fe_dai_close;
2077 rtd->ops.pointer = soc_pcm_pointer;
2078 rtd->ops.ioctl = soc_pcm_ioctl;
2080 rtd->ops.open = soc_pcm_open;
2081 rtd->ops.hw_params = soc_pcm_hw_params;
2082 rtd->ops.prepare = soc_pcm_prepare;
2083 rtd->ops.trigger = soc_pcm_trigger;
2084 rtd->ops.hw_free = soc_pcm_hw_free;
2085 rtd->ops.close = soc_pcm_close;
2086 rtd->ops.pointer = soc_pcm_pointer;
2087 rtd->ops.ioctl = soc_pcm_ioctl;
2090 if (platform->driver->ops) {
2091 rtd->ops.ack = platform->driver->ops->ack;
2092 rtd->ops.copy = platform->driver->ops->copy;
2093 rtd->ops.silence = platform->driver->ops->silence;
2094 rtd->ops.page = platform->driver->ops->page;
2095 rtd->ops.mmap = platform->driver->ops->mmap;
2099 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2102 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2104 if (platform->driver->pcm_new) {
2105 ret = platform->driver->pcm_new(rtd);
2107 dev_err(platform->dev,
2108 "ASoC: pcm constructor failed: %d\n",
2114 pcm->private_free = platform->driver->pcm_free;
2116 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2121 /* is the current PCM operation for this FE ? */
2122 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2124 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2128 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2130 /* is the current PCM operation for this BE ? */
2131 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2132 struct snd_soc_pcm_runtime *be, int stream)
2134 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2135 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2136 be->dpcm[stream].runtime_update))
2140 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2142 /* get the substream for this BE */
2143 struct snd_pcm_substream *
2144 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2146 return be->pcm->streams[stream].substream;
2148 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2150 /* get the BE runtime state */
2151 enum snd_soc_dpcm_state
2152 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2154 return be->dpcm[stream].state;
2156 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2158 /* set the BE runtime state */
2159 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2160 int stream, enum snd_soc_dpcm_state state)
2162 be->dpcm[stream].state = state;
2164 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2167 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2168 * are not running, paused or suspended for the specified stream direction.
2170 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2171 struct snd_soc_pcm_runtime *be, int stream)
2173 struct snd_soc_dpcm *dpcm;
2176 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2181 state = dpcm->fe->dpcm[stream].state;
2182 if (state == SND_SOC_DPCM_STATE_START ||
2183 state == SND_SOC_DPCM_STATE_PAUSED ||
2184 state == SND_SOC_DPCM_STATE_SUSPEND)
2188 /* it's safe to free/stop this BE DAI */
2191 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2194 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2195 * running, paused or suspended for the specified stream direction.
2197 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2198 struct snd_soc_pcm_runtime *be, int stream)
2200 struct snd_soc_dpcm *dpcm;
2203 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2208 state = dpcm->fe->dpcm[stream].state;
2209 if (state == SND_SOC_DPCM_STATE_START ||
2210 state == SND_SOC_DPCM_STATE_PAUSED ||
2211 state == SND_SOC_DPCM_STATE_SUSPEND ||
2212 state == SND_SOC_DPCM_STATE_PREPARE)
2216 /* it's safe to change hw_params */
2219 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2221 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2222 int cmd, struct snd_soc_platform *platform)
2224 if (platform->driver->ops->trigger)
2225 return platform->driver->ops->trigger(substream, cmd);
2228 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2230 #ifdef CONFIG_DEBUG_FS
2231 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2234 case SND_SOC_DPCM_STATE_NEW:
2236 case SND_SOC_DPCM_STATE_OPEN:
2238 case SND_SOC_DPCM_STATE_HW_PARAMS:
2240 case SND_SOC_DPCM_STATE_PREPARE:
2242 case SND_SOC_DPCM_STATE_START:
2244 case SND_SOC_DPCM_STATE_STOP:
2246 case SND_SOC_DPCM_STATE_SUSPEND:
2248 case SND_SOC_DPCM_STATE_PAUSED:
2250 case SND_SOC_DPCM_STATE_HW_FREE:
2252 case SND_SOC_DPCM_STATE_CLOSE:
2259 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2260 int stream, char *buf, size_t size)
2262 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2263 struct snd_soc_dpcm *dpcm;
2267 offset += snprintf(buf + offset, size - offset,
2268 "[%s - %s]\n", fe->dai_link->name,
2269 stream ? "Capture" : "Playback");
2271 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2272 dpcm_state_string(fe->dpcm[stream].state));
2274 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2275 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2276 offset += snprintf(buf + offset, size - offset,
2278 "Format = %s, Channels = %d, Rate = %d\n",
2279 snd_pcm_format_name(params_format(params)),
2280 params_channels(params),
2281 params_rate(params));
2284 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2286 if (list_empty(&fe->dpcm[stream].be_clients)) {
2287 offset += snprintf(buf + offset, size - offset,
2288 " No active DSP links\n");
2292 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2293 struct snd_soc_pcm_runtime *be = dpcm->be;
2294 params = &dpcm->hw_params;
2296 offset += snprintf(buf + offset, size - offset,
2297 "- %s\n", be->dai_link->name);
2299 offset += snprintf(buf + offset, size - offset,
2301 dpcm_state_string(be->dpcm[stream].state));
2303 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2304 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2305 offset += snprintf(buf + offset, size - offset,
2306 " Hardware Params: "
2307 "Format = %s, Channels = %d, Rate = %d\n",
2308 snd_pcm_format_name(params_format(params)),
2309 params_channels(params),
2310 params_rate(params));
2317 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2318 size_t count, loff_t *ppos)
2320 struct snd_soc_pcm_runtime *fe = file->private_data;
2321 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2324 buf = kmalloc(out_count, GFP_KERNEL);
2328 if (fe->cpu_dai->driver->playback.channels_min)
2329 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2330 buf + offset, out_count - offset);
2332 if (fe->cpu_dai->driver->capture.channels_min)
2333 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2334 buf + offset, out_count - offset);
2336 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2342 static const struct file_operations dpcm_state_fops = {
2343 .open = simple_open,
2344 .read = dpcm_state_read_file,
2345 .llseek = default_llseek,
2348 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2353 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2354 rtd->card->debugfs_card_root);
2355 if (!rtd->debugfs_dpcm_root) {
2357 "ASoC: Failed to create dpcm debugfs directory %s\n",
2358 rtd->dai_link->name);
2362 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2363 rtd->debugfs_dpcm_root,
2364 rtd, &dpcm_state_fops);