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 && 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 && 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 && 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 && 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 && 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 && 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 && cpu_dai->driver->ops->hw_free)
563 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
566 if (codec_dai->driver->ops && 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 && codec_dai->driver->ops->hw_free)
604 codec_dai->driver->ops->hw_free(substream, codec_dai);
606 if (cpu_dai->driver->ops && 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 && 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 && 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 &&
651 codec_dai->driver->ops->bespoke_trigger) {
652 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
657 if (platform->driver->ops && platform->driver->bespoke_trigger) {
658 ret = platform->driver->bespoke_trigger(substream, cmd);
663 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
664 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
671 * soc level wrapper for pointer callback
672 * If cpu_dai, codec_dai, platform driver has the delay callback, than
673 * the runtime->delay will be updated accordingly.
675 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
677 struct snd_soc_pcm_runtime *rtd = substream->private_data;
678 struct snd_soc_platform *platform = rtd->platform;
679 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
680 struct snd_soc_dai *codec_dai = rtd->codec_dai;
681 struct snd_pcm_runtime *runtime = substream->runtime;
682 snd_pcm_uframes_t offset = 0;
683 snd_pcm_sframes_t delay = 0;
685 if (platform->driver->ops && platform->driver->ops->pointer)
686 offset = platform->driver->ops->pointer(substream);
688 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
689 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
691 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
692 delay += codec_dai->driver->ops->delay(substream, codec_dai);
694 if (platform->driver->delay)
695 delay += platform->driver->delay(substream, codec_dai);
697 runtime->delay = delay;
702 /* connect a FE and BE */
703 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
704 struct snd_soc_pcm_runtime *be, int stream)
706 struct snd_soc_dpcm *dpcm;
708 /* only add new dpcms */
709 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
710 if (dpcm->be == be && dpcm->fe == fe)
714 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
720 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
721 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
722 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
723 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
725 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
726 stream ? "capture" : "playback", fe->dai_link->name,
727 stream ? "<-" : "->", be->dai_link->name);
729 #ifdef CONFIG_DEBUG_FS
730 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
731 fe->debugfs_dpcm_root, &dpcm->state);
736 /* reparent a BE onto another FE */
737 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
738 struct snd_soc_pcm_runtime *be, int stream)
740 struct snd_soc_dpcm *dpcm;
741 struct snd_pcm_substream *fe_substream, *be_substream;
743 /* reparent if BE is connected to other FEs */
744 if (!be->dpcm[stream].users)
747 be_substream = snd_soc_dpcm_get_substream(be, stream);
749 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
753 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
754 stream ? "capture" : "playback",
755 dpcm->fe->dai_link->name,
756 stream ? "<-" : "->", dpcm->be->dai_link->name);
758 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
759 be_substream->runtime = fe_substream->runtime;
764 /* disconnect a BE and FE */
765 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
767 struct snd_soc_dpcm *dpcm, *d;
769 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
770 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
771 stream ? "capture" : "playback",
772 dpcm->be->dai_link->name);
774 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
777 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
778 stream ? "capture" : "playback", fe->dai_link->name,
779 stream ? "<-" : "->", dpcm->be->dai_link->name);
781 /* BEs still alive need new FE */
782 dpcm_be_reparent(fe, dpcm->be, stream);
784 #ifdef CONFIG_DEBUG_FS
785 debugfs_remove(dpcm->debugfs_state);
787 list_del(&dpcm->list_be);
788 list_del(&dpcm->list_fe);
793 /* get BE for DAI widget and stream */
794 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
795 struct snd_soc_dapm_widget *widget, int stream)
797 struct snd_soc_pcm_runtime *be;
800 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
801 for (i = 0; i < card->num_links; i++) {
804 if (!be->dai_link->no_pcm)
807 if (be->cpu_dai->playback_widget == widget ||
808 be->codec_dai->playback_widget == widget)
813 for (i = 0; i < card->num_links; i++) {
816 if (!be->dai_link->no_pcm)
819 if (be->cpu_dai->capture_widget == widget ||
820 be->codec_dai->capture_widget == widget)
825 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
826 stream ? "capture" : "playback", widget->name);
830 static inline struct snd_soc_dapm_widget *
831 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
833 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
834 return rtd->cpu_dai->playback_widget;
836 return rtd->cpu_dai->capture_widget;
839 static inline struct snd_soc_dapm_widget *
840 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
842 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
843 return rtd->codec_dai->playback_widget;
845 return rtd->codec_dai->capture_widget;
848 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
849 struct snd_soc_dapm_widget *widget)
853 for (i = 0; i < list->num_widgets; i++) {
854 if (widget == list->widgets[i])
861 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
862 int stream, struct snd_soc_dapm_widget_list **list_)
864 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
865 struct snd_soc_dapm_widget_list *list;
868 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
869 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
873 /* get number of valid DAI paths and their widgets */
874 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
876 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
877 stream ? "capture" : "playback");
883 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
888 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
889 struct snd_soc_dapm_widget_list **list_)
891 struct snd_soc_dpcm *dpcm;
892 struct snd_soc_dapm_widget_list *list = *list_;
893 struct snd_soc_dapm_widget *widget;
896 /* Destroy any old FE <--> BE connections */
897 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
899 /* is there a valid CPU DAI widget for this BE */
900 widget = rtd_get_cpu_widget(dpcm->be, stream);
902 /* prune the BE if it's no longer in our active list */
903 if (widget && widget_in_list(list, widget))
906 /* is there a valid CODEC DAI widget for this BE */
907 widget = rtd_get_codec_widget(dpcm->be, stream);
909 /* prune the BE if it's no longer in our active list */
910 if (widget && widget_in_list(list, widget))
913 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
914 stream ? "capture" : "playback",
915 dpcm->be->dai_link->name, fe->dai_link->name);
916 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
917 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
921 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
925 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
926 struct snd_soc_dapm_widget_list **list_)
928 struct snd_soc_card *card = fe->card;
929 struct snd_soc_dapm_widget_list *list = *list_;
930 struct snd_soc_pcm_runtime *be;
933 /* Create any new FE <--> BE connections */
934 for (i = 0; i < list->num_widgets; i++) {
936 switch (list->widgets[i]->id) {
937 case snd_soc_dapm_dai_in:
938 case snd_soc_dapm_dai_out:
944 /* is there a valid BE rtd for this widget */
945 be = dpcm_get_be(card, list->widgets[i], stream);
947 dev_err(fe->dev, "ASoC: no BE found for %s\n",
948 list->widgets[i]->name);
952 /* make sure BE is a real BE */
953 if (!be->dai_link->no_pcm)
956 /* don't connect if FE is not running */
957 if (!fe->dpcm[stream].runtime)
960 /* newly connected FE and BE */
961 err = dpcm_be_connect(fe, be, stream);
963 dev_err(fe->dev, "ASoC: can't connect %s\n",
964 list->widgets[i]->name);
966 } else if (err == 0) /* already connected */
970 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
974 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
979 * Find the corresponding BE DAIs that source or sink audio to this
982 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
983 int stream, struct snd_soc_dapm_widget_list **list, int new)
986 return dpcm_add_paths(fe, stream, list);
988 return dpcm_prune_paths(fe, stream, list);
991 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
993 struct snd_soc_dpcm *dpcm;
995 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
996 dpcm->be->dpcm[stream].runtime_update =
997 SND_SOC_DPCM_UPDATE_NO;
1000 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1003 struct snd_soc_dpcm *dpcm;
1005 /* disable any enabled and non active backends */
1006 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1008 struct snd_soc_pcm_runtime *be = dpcm->be;
1009 struct snd_pcm_substream *be_substream =
1010 snd_soc_dpcm_get_substream(be, stream);
1012 if (be->dpcm[stream].users == 0)
1013 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1014 stream ? "capture" : "playback",
1015 be->dpcm[stream].state);
1017 if (--be->dpcm[stream].users != 0)
1020 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1023 soc_pcm_close(be_substream);
1024 be_substream->runtime = NULL;
1025 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1029 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1031 struct snd_soc_dpcm *dpcm;
1034 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1035 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1037 struct snd_soc_pcm_runtime *be = dpcm->be;
1038 struct snd_pcm_substream *be_substream =
1039 snd_soc_dpcm_get_substream(be, stream);
1041 /* is this op for this BE ? */
1042 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1045 /* first time the dpcm is open ? */
1046 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1047 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1048 stream ? "capture" : "playback",
1049 be->dpcm[stream].state);
1051 if (be->dpcm[stream].users++ != 0)
1054 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1055 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1058 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
1060 be_substream->runtime = be->dpcm[stream].runtime;
1061 err = soc_pcm_open(be_substream);
1063 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1064 be->dpcm[stream].users--;
1065 if (be->dpcm[stream].users < 0)
1066 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1067 stream ? "capture" : "playback",
1068 be->dpcm[stream].state);
1070 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1074 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1081 /* disable any enabled and non active backends */
1082 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1083 struct snd_soc_pcm_runtime *be = dpcm->be;
1084 struct snd_pcm_substream *be_substream =
1085 snd_soc_dpcm_get_substream(be, stream);
1087 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1090 if (be->dpcm[stream].users == 0)
1091 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1092 stream ? "capture" : "playback",
1093 be->dpcm[stream].state);
1095 if (--be->dpcm[stream].users != 0)
1098 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1101 soc_pcm_close(be_substream);
1102 be_substream->runtime = NULL;
1103 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1109 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1111 struct snd_pcm_runtime *runtime = substream->runtime;
1112 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1113 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1114 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1116 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1117 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1118 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1119 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1120 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1121 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1122 runtime->hw.rates = cpu_dai_drv->playback.rates;
1124 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1125 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1126 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1127 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1128 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1129 runtime->hw.rates = cpu_dai_drv->capture.rates;
1133 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1135 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1136 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1137 int stream = fe_substream->stream, ret = 0;
1139 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1141 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1143 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1147 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1149 /* start the DAI frontend */
1150 ret = soc_pcm_open(fe_substream);
1152 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1156 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1158 dpcm_set_fe_runtime(fe_substream);
1159 snd_pcm_limit_hw_rates(runtime);
1161 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1165 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1167 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1171 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1173 struct snd_soc_dpcm *dpcm;
1175 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1176 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1178 struct snd_soc_pcm_runtime *be = dpcm->be;
1179 struct snd_pcm_substream *be_substream =
1180 snd_soc_dpcm_get_substream(be, stream);
1182 /* is this op for this BE ? */
1183 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1186 if (be->dpcm[stream].users == 0)
1187 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1188 stream ? "capture" : "playback",
1189 be->dpcm[stream].state);
1191 if (--be->dpcm[stream].users != 0)
1194 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1195 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1198 dev_dbg(be->dev, "ASoC: close BE %s\n",
1199 dpcm->fe->dai_link->name);
1201 soc_pcm_close(be_substream);
1202 be_substream->runtime = NULL;
1204 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1209 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1211 struct snd_soc_pcm_runtime *fe = substream->private_data;
1212 int stream = substream->stream;
1214 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1216 /* shutdown the BEs */
1217 dpcm_be_dai_shutdown(fe, substream->stream);
1219 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1221 /* now shutdown the frontend */
1222 soc_pcm_close(substream);
1224 /* run the stream event for each BE */
1225 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1227 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1228 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1232 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1234 struct snd_soc_dpcm *dpcm;
1236 /* only hw_params backends that are either sinks or sources
1237 * to this frontend DAI */
1238 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1240 struct snd_soc_pcm_runtime *be = dpcm->be;
1241 struct snd_pcm_substream *be_substream =
1242 snd_soc_dpcm_get_substream(be, stream);
1244 /* is this op for this BE ? */
1245 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1248 /* only free hw when no longer used - check all FEs */
1249 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1252 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1254 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1255 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1256 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1259 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1260 dpcm->fe->dai_link->name);
1262 soc_pcm_hw_free(be_substream);
1264 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1270 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1272 struct snd_soc_pcm_runtime *fe = substream->private_data;
1273 int err, stream = substream->stream;
1275 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1276 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1278 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1280 /* call hw_free on the frontend */
1281 err = soc_pcm_hw_free(substream);
1283 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1284 fe->dai_link->name);
1286 /* only hw_params backends that are either sinks or sources
1287 * to this frontend DAI */
1288 err = dpcm_be_dai_hw_free(fe, stream);
1290 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1291 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1293 mutex_unlock(&fe->card->mutex);
1297 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1299 struct snd_soc_dpcm *dpcm;
1302 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1304 struct snd_soc_pcm_runtime *be = dpcm->be;
1305 struct snd_pcm_substream *be_substream =
1306 snd_soc_dpcm_get_substream(be, stream);
1308 /* is this op for this BE ? */
1309 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1312 /* only allow hw_params() if no connected FEs are running */
1313 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1316 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1317 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1318 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1321 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1322 dpcm->fe->dai_link->name);
1324 /* copy params for each dpcm */
1325 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1326 sizeof(struct snd_pcm_hw_params));
1328 /* perform any hw_params fixups */
1329 if (be->dai_link->be_hw_params_fixup) {
1330 ret = be->dai_link->be_hw_params_fixup(be,
1334 "ASoC: hw_params BE fixup failed %d\n",
1340 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1342 dev_err(dpcm->be->dev,
1343 "ASoC: hw_params BE failed %d\n", ret);
1347 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1352 /* disable any enabled and non active backends */
1353 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1354 struct snd_soc_pcm_runtime *be = dpcm->be;
1355 struct snd_pcm_substream *be_substream =
1356 snd_soc_dpcm_get_substream(be, stream);
1358 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1361 /* only allow hw_free() if no connected FEs are running */
1362 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1365 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1366 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1367 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1368 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1371 soc_pcm_hw_free(be_substream);
1377 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1378 struct snd_pcm_hw_params *params)
1380 struct snd_soc_pcm_runtime *fe = substream->private_data;
1381 int ret, stream = substream->stream;
1383 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1384 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1386 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1387 sizeof(struct snd_pcm_hw_params));
1388 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1390 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1394 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1395 fe->dai_link->name, params_rate(params),
1396 params_channels(params), params_format(params));
1398 /* call hw_params on the frontend */
1399 ret = soc_pcm_hw_params(substream, params);
1401 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1402 dpcm_be_dai_hw_free(fe, stream);
1404 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1407 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1408 mutex_unlock(&fe->card->mutex);
1412 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1413 struct snd_pcm_substream *substream, int cmd)
1417 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1418 dpcm->fe->dai_link->name, cmd);
1420 ret = soc_pcm_trigger(substream, cmd);
1422 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1427 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1430 struct snd_soc_dpcm *dpcm;
1433 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1435 struct snd_soc_pcm_runtime *be = dpcm->be;
1436 struct snd_pcm_substream *be_substream =
1437 snd_soc_dpcm_get_substream(be, stream);
1439 /* is this op for this BE ? */
1440 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1444 case SNDRV_PCM_TRIGGER_START:
1445 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1449 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1453 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1455 case SNDRV_PCM_TRIGGER_RESUME:
1456 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1459 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1463 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1465 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1466 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1469 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1473 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1475 case SNDRV_PCM_TRIGGER_STOP:
1476 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1479 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1482 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1486 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1488 case SNDRV_PCM_TRIGGER_SUSPEND:
1489 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1492 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1495 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1499 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1501 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1502 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1505 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1508 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1512 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1519 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1521 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1523 struct snd_soc_pcm_runtime *fe = substream->private_data;
1524 int stream = substream->stream, ret;
1525 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1527 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1530 case SND_SOC_DPCM_TRIGGER_PRE:
1531 /* call trigger on the frontend before the backend. */
1533 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1534 fe->dai_link->name, cmd);
1536 ret = soc_pcm_trigger(substream, cmd);
1538 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1542 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1544 case SND_SOC_DPCM_TRIGGER_POST:
1545 /* call trigger on the frontend after the backend. */
1547 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1549 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1553 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1554 fe->dai_link->name, cmd);
1556 ret = soc_pcm_trigger(substream, cmd);
1558 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1559 /* bespoke trigger() - handles both FE and BEs */
1561 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1562 fe->dai_link->name, cmd);
1564 ret = soc_pcm_bespoke_trigger(substream, cmd);
1566 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1571 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1572 fe->dai_link->name);
1578 case SNDRV_PCM_TRIGGER_START:
1579 case SNDRV_PCM_TRIGGER_RESUME:
1580 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1581 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1583 case SNDRV_PCM_TRIGGER_STOP:
1584 case SNDRV_PCM_TRIGGER_SUSPEND:
1585 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1586 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1591 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1595 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1597 struct snd_soc_dpcm *dpcm;
1600 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1602 struct snd_soc_pcm_runtime *be = dpcm->be;
1603 struct snd_pcm_substream *be_substream =
1604 snd_soc_dpcm_get_substream(be, stream);
1606 /* is this op for this BE ? */
1607 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1610 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1611 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1614 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1615 dpcm->fe->dai_link->name);
1617 ret = soc_pcm_prepare(be_substream);
1619 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1624 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1629 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1631 struct snd_soc_pcm_runtime *fe = substream->private_data;
1632 int stream = substream->stream, ret = 0;
1634 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1636 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1638 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1640 /* there is no point preparing this FE if there are no BEs */
1641 if (list_empty(&fe->dpcm[stream].be_clients)) {
1642 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1643 fe->dai_link->name);
1648 ret = dpcm_be_dai_prepare(fe, substream->stream);
1652 /* call prepare on the frontend */
1653 ret = soc_pcm_prepare(substream);
1655 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1656 fe->dai_link->name);
1660 /* run the stream event for each BE */
1661 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1662 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1665 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1666 mutex_unlock(&fe->card->mutex);
1671 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1672 unsigned int cmd, void *arg)
1674 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1675 struct snd_soc_platform *platform = rtd->platform;
1677 if (platform->driver->ops && platform->driver->ops->ioctl)
1678 return platform->driver->ops->ioctl(substream, cmd, arg);
1679 return snd_pcm_lib_ioctl(substream, cmd, arg);
1682 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1684 struct snd_pcm_substream *substream =
1685 snd_soc_dpcm_get_substream(fe, stream);
1686 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1689 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1690 stream ? "capture" : "playback", fe->dai_link->name);
1692 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1693 /* call bespoke trigger - FE takes care of all BE triggers */
1694 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1695 fe->dai_link->name);
1697 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1699 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1701 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1702 fe->dai_link->name);
1704 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1706 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1709 err = dpcm_be_dai_hw_free(fe, stream);
1711 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1713 err = dpcm_be_dai_shutdown(fe, stream);
1715 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1717 /* run the stream event for each BE */
1718 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1723 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1725 struct snd_pcm_substream *substream =
1726 snd_soc_dpcm_get_substream(fe, stream);
1727 struct snd_soc_dpcm *dpcm;
1728 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1731 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1732 stream ? "capture" : "playback", fe->dai_link->name);
1734 /* Only start the BE if the FE is ready */
1735 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1736 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1739 /* startup must always be called for new BEs */
1740 ret = dpcm_be_dai_startup(fe, stream);
1744 /* keep going if FE state is > open */
1745 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1748 ret = dpcm_be_dai_hw_params(fe, stream);
1752 /* keep going if FE state is > hw_params */
1753 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1757 ret = dpcm_be_dai_prepare(fe, stream);
1761 /* run the stream event for each BE */
1762 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1764 /* keep going if FE state is > prepare */
1765 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1766 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1769 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1770 /* call trigger on the frontend - FE takes care of all BE triggers */
1771 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1772 fe->dai_link->name);
1774 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1776 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1780 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1781 fe->dai_link->name);
1783 ret = dpcm_be_dai_trigger(fe, stream,
1784 SNDRV_PCM_TRIGGER_START);
1786 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1794 dpcm_be_dai_hw_free(fe, stream);
1796 dpcm_be_dai_shutdown(fe, stream);
1798 /* disconnect any non started BEs */
1799 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1800 struct snd_soc_pcm_runtime *be = dpcm->be;
1801 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1802 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1808 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1812 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1813 ret = dpcm_run_update_startup(fe, stream);
1815 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1816 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1821 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1825 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1826 ret = dpcm_run_update_shutdown(fe, stream);
1828 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1829 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1834 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1837 int soc_dpcm_runtime_update(struct snd_soc_card *card)
1839 int i, old, new, paths;
1841 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1842 for (i = 0; i < card->num_rtd; i++) {
1843 struct snd_soc_dapm_widget_list *list;
1844 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1846 /* make sure link is FE */
1847 if (!fe->dai_link->dynamic)
1850 /* only check active links */
1851 if (!fe->cpu_dai->active)
1854 /* DAPM sync will call this to update DSP paths */
1855 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1856 fe->dai_link->name);
1858 /* skip if FE doesn't have playback capability */
1859 if (!fe->cpu_dai->driver->playback.channels_min)
1862 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1864 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1865 fe->dai_link->name, "playback");
1866 mutex_unlock(&card->mutex);
1870 /* update any new playback paths */
1871 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1873 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1874 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1875 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1878 /* update any old playback paths */
1879 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1881 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1883 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1887 /* skip if FE doesn't have capture capability */
1888 if (!fe->cpu_dai->driver->capture.channels_min)
1891 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1893 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1894 fe->dai_link->name, "capture");
1895 mutex_unlock(&card->mutex);
1899 /* update any new capture paths */
1900 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1902 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1903 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1904 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1907 /* update any old capture paths */
1908 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1910 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1912 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1915 dpcm_path_put(&list);
1918 mutex_unlock(&card->mutex);
1921 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1923 struct snd_soc_dpcm *dpcm;
1924 struct list_head *clients =
1925 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1927 list_for_each_entry(dpcm, clients, list_be) {
1929 struct snd_soc_pcm_runtime *be = dpcm->be;
1930 struct snd_soc_dai *dai = be->codec_dai;
1931 struct snd_soc_dai_driver *drv = dai->driver;
1933 if (be->dai_link->ignore_suspend)
1936 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1938 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1939 drv->ops->digital_mute(dai, mute);
1945 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1947 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1948 struct snd_soc_dpcm *dpcm;
1949 struct snd_soc_dapm_widget_list *list;
1951 int stream = fe_substream->stream;
1953 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1954 fe->dpcm[stream].runtime = fe_substream->runtime;
1956 if (dpcm_path_get(fe, stream, &list) <= 0) {
1957 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1958 fe->dai_link->name, stream ? "capture" : "playback");
1961 /* calculate valid and active FE <-> BE dpcms */
1962 dpcm_process_paths(fe, stream, &list, 1);
1964 ret = dpcm_fe_dai_startup(fe_substream);
1966 /* clean up all links */
1967 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1968 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1970 dpcm_be_disconnect(fe, stream);
1971 fe->dpcm[stream].runtime = NULL;
1974 dpcm_clear_pending_state(fe, stream);
1975 dpcm_path_put(&list);
1976 mutex_unlock(&fe->card->mutex);
1980 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1982 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1983 struct snd_soc_dpcm *dpcm;
1984 int stream = fe_substream->stream, ret;
1986 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1987 ret = dpcm_fe_dai_shutdown(fe_substream);
1989 /* mark FE's links ready to prune */
1990 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1991 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1993 dpcm_be_disconnect(fe, stream);
1995 fe->dpcm[stream].runtime = NULL;
1996 mutex_unlock(&fe->card->mutex);
2000 /* create a new pcm */
2001 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2003 struct snd_soc_platform *platform = rtd->platform;
2004 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2005 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2006 struct snd_pcm *pcm;
2008 int ret = 0, playback = 0, capture = 0;
2010 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2011 if (cpu_dai->driver->playback.channels_min)
2013 if (cpu_dai->driver->capture.channels_min)
2016 if (codec_dai->driver->playback.channels_min &&
2017 cpu_dai->driver->playback.channels_min)
2019 if (codec_dai->driver->capture.channels_min &&
2020 cpu_dai->driver->capture.channels_min)
2024 if (rtd->dai_link->playback_only) {
2029 if (rtd->dai_link->capture_only) {
2034 /* create the PCM */
2035 if (rtd->dai_link->no_pcm) {
2036 snprintf(new_name, sizeof(new_name), "(%s)",
2037 rtd->dai_link->stream_name);
2039 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2040 playback, capture, &pcm);
2042 if (rtd->dai_link->dynamic)
2043 snprintf(new_name, sizeof(new_name), "%s (*)",
2044 rtd->dai_link->stream_name);
2046 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2047 rtd->dai_link->stream_name, codec_dai->name, num);
2049 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2053 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2054 rtd->dai_link->name);
2057 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2059 /* DAPM dai link stream work */
2060 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2063 pcm->private_data = rtd;
2065 if (rtd->dai_link->no_pcm) {
2067 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2069 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2073 /* ASoC PCM operations */
2074 if (rtd->dai_link->dynamic) {
2075 rtd->ops.open = dpcm_fe_dai_open;
2076 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2077 rtd->ops.prepare = dpcm_fe_dai_prepare;
2078 rtd->ops.trigger = dpcm_fe_dai_trigger;
2079 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2080 rtd->ops.close = dpcm_fe_dai_close;
2081 rtd->ops.pointer = soc_pcm_pointer;
2082 rtd->ops.ioctl = soc_pcm_ioctl;
2084 rtd->ops.open = soc_pcm_open;
2085 rtd->ops.hw_params = soc_pcm_hw_params;
2086 rtd->ops.prepare = soc_pcm_prepare;
2087 rtd->ops.trigger = soc_pcm_trigger;
2088 rtd->ops.hw_free = soc_pcm_hw_free;
2089 rtd->ops.close = soc_pcm_close;
2090 rtd->ops.pointer = soc_pcm_pointer;
2091 rtd->ops.ioctl = soc_pcm_ioctl;
2094 if (platform->driver->ops) {
2095 rtd->ops.ack = platform->driver->ops->ack;
2096 rtd->ops.copy = platform->driver->ops->copy;
2097 rtd->ops.silence = platform->driver->ops->silence;
2098 rtd->ops.page = platform->driver->ops->page;
2099 rtd->ops.mmap = platform->driver->ops->mmap;
2103 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2106 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2108 if (platform->driver->pcm_new) {
2109 ret = platform->driver->pcm_new(rtd);
2111 dev_err(platform->dev,
2112 "ASoC: pcm constructor failed: %d\n",
2118 pcm->private_free = platform->driver->pcm_free;
2120 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
2125 /* is the current PCM operation for this FE ? */
2126 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2128 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2132 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2134 /* is the current PCM operation for this BE ? */
2135 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2136 struct snd_soc_pcm_runtime *be, int stream)
2138 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2139 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2140 be->dpcm[stream].runtime_update))
2144 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2146 /* get the substream for this BE */
2147 struct snd_pcm_substream *
2148 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2150 return be->pcm->streams[stream].substream;
2152 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2154 /* get the BE runtime state */
2155 enum snd_soc_dpcm_state
2156 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2158 return be->dpcm[stream].state;
2160 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2162 /* set the BE runtime state */
2163 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2164 int stream, enum snd_soc_dpcm_state state)
2166 be->dpcm[stream].state = state;
2168 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2171 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2172 * are not running, paused or suspended for the specified stream direction.
2174 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2175 struct snd_soc_pcm_runtime *be, int stream)
2177 struct snd_soc_dpcm *dpcm;
2180 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2185 state = dpcm->fe->dpcm[stream].state;
2186 if (state == SND_SOC_DPCM_STATE_START ||
2187 state == SND_SOC_DPCM_STATE_PAUSED ||
2188 state == SND_SOC_DPCM_STATE_SUSPEND)
2192 /* it's safe to free/stop this BE DAI */
2195 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2198 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2199 * running, paused or suspended for the specified stream direction.
2201 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2202 struct snd_soc_pcm_runtime *be, int stream)
2204 struct snd_soc_dpcm *dpcm;
2207 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2212 state = dpcm->fe->dpcm[stream].state;
2213 if (state == SND_SOC_DPCM_STATE_START ||
2214 state == SND_SOC_DPCM_STATE_PAUSED ||
2215 state == SND_SOC_DPCM_STATE_SUSPEND ||
2216 state == SND_SOC_DPCM_STATE_PREPARE)
2220 /* it's safe to change hw_params */
2223 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2225 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2226 int cmd, struct snd_soc_platform *platform)
2228 if (platform->driver->ops && platform->driver->ops->trigger)
2229 return platform->driver->ops->trigger(substream, cmd);
2232 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2234 #ifdef CONFIG_DEBUG_FS
2235 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2238 case SND_SOC_DPCM_STATE_NEW:
2240 case SND_SOC_DPCM_STATE_OPEN:
2242 case SND_SOC_DPCM_STATE_HW_PARAMS:
2244 case SND_SOC_DPCM_STATE_PREPARE:
2246 case SND_SOC_DPCM_STATE_START:
2248 case SND_SOC_DPCM_STATE_STOP:
2250 case SND_SOC_DPCM_STATE_SUSPEND:
2252 case SND_SOC_DPCM_STATE_PAUSED:
2254 case SND_SOC_DPCM_STATE_HW_FREE:
2256 case SND_SOC_DPCM_STATE_CLOSE:
2263 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2264 int stream, char *buf, size_t size)
2266 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2267 struct snd_soc_dpcm *dpcm;
2271 offset += snprintf(buf + offset, size - offset,
2272 "[%s - %s]\n", fe->dai_link->name,
2273 stream ? "Capture" : "Playback");
2275 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2276 dpcm_state_string(fe->dpcm[stream].state));
2278 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2279 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2280 offset += snprintf(buf + offset, size - offset,
2282 "Format = %s, Channels = %d, Rate = %d\n",
2283 snd_pcm_format_name(params_format(params)),
2284 params_channels(params),
2285 params_rate(params));
2288 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2290 if (list_empty(&fe->dpcm[stream].be_clients)) {
2291 offset += snprintf(buf + offset, size - offset,
2292 " No active DSP links\n");
2296 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2297 struct snd_soc_pcm_runtime *be = dpcm->be;
2298 params = &dpcm->hw_params;
2300 offset += snprintf(buf + offset, size - offset,
2301 "- %s\n", be->dai_link->name);
2303 offset += snprintf(buf + offset, size - offset,
2305 dpcm_state_string(be->dpcm[stream].state));
2307 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2308 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2309 offset += snprintf(buf + offset, size - offset,
2310 " Hardware Params: "
2311 "Format = %s, Channels = %d, Rate = %d\n",
2312 snd_pcm_format_name(params_format(params)),
2313 params_channels(params),
2314 params_rate(params));
2321 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2322 size_t count, loff_t *ppos)
2324 struct snd_soc_pcm_runtime *fe = file->private_data;
2325 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2328 buf = kmalloc(out_count, GFP_KERNEL);
2332 if (fe->cpu_dai->driver->playback.channels_min)
2333 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2334 buf + offset, out_count - offset);
2336 if (fe->cpu_dai->driver->capture.channels_min)
2337 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2338 buf + offset, out_count - offset);
2340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2346 static const struct file_operations dpcm_state_fops = {
2347 .open = simple_open,
2348 .read = dpcm_state_read_file,
2349 .llseek = default_llseek,
2352 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2357 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2358 rtd->card->debugfs_card_root);
2359 if (!rtd->debugfs_dpcm_root) {
2361 "ASoC: Failed to create dpcm debugfs directory %s\n",
2362 rtd->dai_link->name);
2366 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2367 rtd->debugfs_dpcm_root,
2368 rtd, &dpcm_state_fops);