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/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
44 struct snd_soc_pcm_stream *codec_stream;
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any channels at all, it supports the stream */
52 return codec_stream->channels_min;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70 lockdep_assert_held(&rtd->pcm_mutex);
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
83 cpu_dai->component->active++;
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 lockdep_assert_held(&rtd->pcm_mutex);
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
118 cpu_dai->component->active--;
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
175 struct snd_soc_dpcm *dpcm;
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
179 struct snd_soc_pcm_runtime *be = dpcm->be;
181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182 be->dai_link->name, event, dir);
184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
188 snd_soc_dapm_stream_event(be, dir, event);
191 snd_soc_dapm_stream_event(fe, dir, event);
196 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
207 ret = snd_pcm_hw_constraint_single(substream->runtime,
208 SNDRV_PCM_HW_PARAM_RATE,
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
223 ret = snd_pcm_hw_constraint_single(substream->runtime,
224 SNDRV_PCM_HW_PARAM_CHANNELS,
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
239 ret = snd_pcm_hw_constraint_single(substream->runtime,
240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
241 soc_dai->sample_bits);
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
253 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
258 unsigned int rate, channels, sample_bits, symmetry, i;
260 rate = params_rate(params);
261 channels = params_channels(params);
262 sample_bits = snd_pcm_format_physical_width(params_format(params));
264 /* reject unmatched parameters when applying symmetry */
265 symmetry = cpu_dai->driver->symmetric_rates ||
266 rtd->dai_link->symmetric_rates;
268 for (i = 0; i < rtd->num_codecs; i++)
269 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
271 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
272 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai->rate, rate);
277 symmetry = cpu_dai->driver->symmetric_channels ||
278 rtd->dai_link->symmetric_channels;
280 for (i = 0; i < rtd->num_codecs; i++)
281 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
283 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
284 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai->channels, channels);
289 symmetry = cpu_dai->driver->symmetric_samplebits ||
290 rtd->dai_link->symmetric_samplebits;
292 for (i = 0; i < rtd->num_codecs; i++)
293 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
295 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
296 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai->sample_bits, sample_bits);
304 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
308 struct snd_soc_dai_link *link = rtd->dai_link;
309 unsigned int symmetry, i;
311 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
312 cpu_driver->symmetric_channels || link->symmetric_channels ||
313 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
315 for (i = 0; i < rtd->num_codecs; i++)
316 symmetry = symmetry ||
317 rtd->codec_dais[i]->driver->symmetric_rates ||
318 rtd->codec_dais[i]->driver->symmetric_channels ||
319 rtd->codec_dais[i]->driver->symmetric_samplebits;
324 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
338 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
340 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
342 struct snd_soc_dai *codec_dai;
344 unsigned int bits = 0, cpu_bits;
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
347 for (i = 0; i < rtd->num_codecs; i++) {
348 codec_dai = rtd->codec_dais[i];
349 if (codec_dai->driver->playback.sig_bits == 0) {
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
355 cpu_bits = cpu_dai->driver->playback.sig_bits;
357 for (i = 0; i < rtd->num_codecs; i++) {
358 codec_dai = rtd->codec_dais[i];
359 if (codec_dai->driver->capture.sig_bits == 0) {
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
365 cpu_bits = cpu_dai->driver->capture.sig_bits;
368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
372 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_pcm_hardware *hw = &runtime->hw;
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
378 struct snd_soc_dai_driver *codec_dai_drv;
379 struct snd_soc_pcm_stream *codec_stream;
380 struct snd_soc_pcm_stream *cpu_stream;
381 unsigned int chan_min = 0, chan_max = UINT_MAX;
382 unsigned int rate_min = 0, rate_max = UINT_MAX;
383 unsigned int rates = UINT_MAX;
384 u64 formats = ULLONG_MAX;
387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
390 cpu_stream = &cpu_dai_drv->capture;
392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i = 0; i < rtd->num_codecs; i++) {
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
404 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
412 codec_stream = &codec_dai_drv->capture;
413 chan_min = max(chan_min, codec_stream->channels_min);
414 chan_max = min(chan_max, codec_stream->channels_max);
415 rate_min = max(rate_min, codec_stream->rate_min);
416 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
417 formats &= codec_stream->formats;
418 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
426 if (rtd->num_codecs > 1) {
427 chan_min = cpu_stream->channels_min;
428 chan_max = cpu_stream->channels_max;
431 hw->channels_min = max(chan_min, cpu_stream->channels_min);
432 hw->channels_max = min(chan_max, cpu_stream->channels_max);
434 hw->formats &= formats & cpu_stream->formats;
436 hw->formats = formats & cpu_stream->formats;
437 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
439 snd_pcm_limit_hw_rates(runtime);
441 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
442 hw->rate_min = max(hw->rate_min, rate_min);
443 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
444 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
452 static int soc_pcm_open(struct snd_pcm_substream *substream)
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct snd_soc_platform *platform = rtd->platform;
457 struct snd_soc_component *component;
458 struct snd_soc_rtdcom_list *rtdcom;
459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
460 struct snd_soc_dai *codec_dai;
461 const char *codec_dai_name = "multicodec";
464 pinctrl_pm_select_default_state(cpu_dai->dev);
465 for (i = 0; i < rtd->num_codecs; i++)
466 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
468 for_each_rtdcom(rtd, rtdcom) {
469 component = rtdcom->component;
471 pm_runtime_get_sync(component->dev);
474 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
476 /* startup the audio subsystem */
477 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
478 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
480 dev_err(cpu_dai->dev, "ASoC: can't open interface"
481 " %s: %d\n", cpu_dai->name, ret);
486 if (platform->driver->ops && platform->driver->ops->open) {
487 ret = platform->driver->ops->open(substream);
489 dev_err(platform->dev, "ASoC: can't open platform"
490 " %s: %d\n", platform->component.name, ret);
495 for (i = 0; i < rtd->num_codecs; i++) {
496 codec_dai = rtd->codec_dais[i];
497 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
498 ret = codec_dai->driver->ops->startup(substream,
501 dev_err(codec_dai->dev,
502 "ASoC: can't open codec %s: %d\n",
503 codec_dai->name, ret);
508 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
509 codec_dai->tx_mask = 0;
511 codec_dai->rx_mask = 0;
514 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
515 ret = rtd->dai_link->ops->startup(substream);
517 pr_err("ASoC: %s startup failed: %d\n",
518 rtd->dai_link->name, ret);
523 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
524 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
527 /* Check that the codec and cpu DAIs are compatible */
528 soc_pcm_init_runtime_hw(substream);
530 if (rtd->num_codecs == 1)
531 codec_dai_name = rtd->codec_dai->name;
533 if (soc_pcm_has_symmetry(substream))
534 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
537 if (!runtime->hw.rates) {
538 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
539 codec_dai_name, cpu_dai->name);
542 if (!runtime->hw.formats) {
543 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
544 codec_dai_name, cpu_dai->name);
547 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
548 runtime->hw.channels_min > runtime->hw.channels_max) {
549 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
550 codec_dai_name, cpu_dai->name);
554 soc_pcm_apply_msb(substream);
556 /* Symmetry only applies if we've already got an active stream. */
557 if (cpu_dai->active) {
558 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
563 for (i = 0; i < rtd->num_codecs; i++) {
564 if (rtd->codec_dais[i]->active) {
565 ret = soc_pcm_apply_symmetry(substream,
572 pr_debug("ASoC: %s <-> %s info:\n",
573 codec_dai_name, cpu_dai->name);
574 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
575 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
576 runtime->hw.channels_max);
577 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
578 runtime->hw.rate_max);
582 snd_soc_runtime_activate(rtd, substream->stream);
584 mutex_unlock(&rtd->pcm_mutex);
588 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
589 rtd->dai_link->ops->shutdown(substream);
596 codec_dai = rtd->codec_dais[i];
597 if (codec_dai->driver->ops->shutdown)
598 codec_dai->driver->ops->shutdown(substream, codec_dai);
601 if (platform->driver->ops && platform->driver->ops->close)
602 platform->driver->ops->close(substream);
605 if (cpu_dai->driver->ops->shutdown)
606 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
608 mutex_unlock(&rtd->pcm_mutex);
610 for_each_rtdcom(rtd, rtdcom) {
611 component = rtdcom->component;
613 pm_runtime_mark_last_busy(component->dev);
614 pm_runtime_put_autosuspend(component->dev);
617 for (i = 0; i < rtd->num_codecs; i++) {
618 if (!rtd->codec_dais[i]->active)
619 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
621 if (!cpu_dai->active)
622 pinctrl_pm_select_sleep_state(cpu_dai->dev);
628 * Power down the audio subsystem pmdown_time msecs after close is called.
629 * This is to ensure there are no pops or clicks in between any music tracks
630 * due to DAPM power cycling.
632 static void close_delayed_work(struct work_struct *work)
634 struct snd_soc_pcm_runtime *rtd =
635 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
636 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
638 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
640 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
641 codec_dai->driver->playback.stream_name,
642 codec_dai->playback_active ? "active" : "inactive",
643 rtd->pop_wait ? "yes" : "no");
645 /* are we waiting on this codec DAI stream */
646 if (rtd->pop_wait == 1) {
648 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
649 SND_SOC_DAPM_STREAM_STOP);
652 mutex_unlock(&rtd->pcm_mutex);
656 * Called by ALSA when a PCM substream is closed. Private data can be
657 * freed here. The cpu DAI, codec DAI, machine and platform are also
660 static int soc_pcm_close(struct snd_pcm_substream *substream)
662 struct snd_soc_pcm_runtime *rtd = substream->private_data;
663 struct snd_soc_platform *platform = rtd->platform;
664 struct snd_soc_component *component;
665 struct snd_soc_rtdcom_list *rtdcom;
666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
667 struct snd_soc_dai *codec_dai;
670 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
672 snd_soc_runtime_deactivate(rtd, substream->stream);
674 /* clear the corresponding DAIs rate when inactive */
675 if (!cpu_dai->active)
678 for (i = 0; i < rtd->num_codecs; i++) {
679 codec_dai = rtd->codec_dais[i];
680 if (!codec_dai->active)
684 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
686 if (cpu_dai->driver->ops->shutdown)
687 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
689 for (i = 0; i < rtd->num_codecs; i++) {
690 codec_dai = rtd->codec_dais[i];
691 if (codec_dai->driver->ops->shutdown)
692 codec_dai->driver->ops->shutdown(substream, codec_dai);
695 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
696 rtd->dai_link->ops->shutdown(substream);
698 if (platform->driver->ops && platform->driver->ops->close)
699 platform->driver->ops->close(substream);
701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
702 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
703 /* powered down playback stream now */
704 snd_soc_dapm_stream_event(rtd,
705 SNDRV_PCM_STREAM_PLAYBACK,
706 SND_SOC_DAPM_STREAM_STOP);
708 /* start delayed pop wq here for playback streams */
710 queue_delayed_work(system_power_efficient_wq,
712 msecs_to_jiffies(rtd->pmdown_time));
715 /* capture streams can be powered down now */
716 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
717 SND_SOC_DAPM_STREAM_STOP);
720 mutex_unlock(&rtd->pcm_mutex);
722 for_each_rtdcom(rtd, rtdcom) {
723 component = rtdcom->component;
725 pm_runtime_mark_last_busy(component->dev);
726 pm_runtime_put_autosuspend(component->dev);
729 for (i = 0; i < rtd->num_codecs; i++) {
730 if (!rtd->codec_dais[i]->active)
731 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
733 if (!cpu_dai->active)
734 pinctrl_pm_select_sleep_state(cpu_dai->dev);
740 * Called by ALSA when the PCM substream is prepared, can set format, sample
741 * rate, etc. This function is non atomic and can be called multiple times,
742 * it can refer to the runtime info.
744 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
746 struct snd_soc_pcm_runtime *rtd = substream->private_data;
747 struct snd_soc_platform *platform = rtd->platform;
748 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
749 struct snd_soc_dai *codec_dai;
752 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
754 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
755 ret = rtd->dai_link->ops->prepare(substream);
757 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
763 if (platform->driver->ops && platform->driver->ops->prepare) {
764 ret = platform->driver->ops->prepare(substream);
766 dev_err(platform->dev, "ASoC: platform prepare error:"
772 for (i = 0; i < rtd->num_codecs; i++) {
773 codec_dai = rtd->codec_dais[i];
774 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
775 ret = codec_dai->driver->ops->prepare(substream,
778 dev_err(codec_dai->dev,
779 "ASoC: codec DAI prepare error: %d\n",
786 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
787 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
789 dev_err(cpu_dai->dev,
790 "ASoC: cpu DAI prepare error: %d\n", ret);
795 /* cancel any delayed stream shutdown that is pending */
796 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
799 cancel_delayed_work(&rtd->delayed_work);
802 snd_soc_dapm_stream_event(rtd, substream->stream,
803 SND_SOC_DAPM_STREAM_START);
805 for (i = 0; i < rtd->num_codecs; i++)
806 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
808 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
811 mutex_unlock(&rtd->pcm_mutex);
815 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
818 struct snd_interval *interval;
819 int channels = hweight_long(mask);
821 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
822 interval->min = channels;
823 interval->max = channels;
826 int soc_dai_hw_params(struct snd_pcm_substream *substream,
827 struct snd_pcm_hw_params *params,
828 struct snd_soc_dai *dai)
832 if (dai->driver->ops && dai->driver->ops->hw_params) {
833 ret = dai->driver->ops->hw_params(substream, params, dai);
835 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
845 * Called by ALSA when the hardware params are set by application. This
846 * function can also be called multiple times and can allocate buffers
847 * (using snd_pcm_lib_* ). It's non-atomic.
849 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
850 struct snd_pcm_hw_params *params)
852 struct snd_soc_pcm_runtime *rtd = substream->private_data;
853 struct snd_soc_platform *platform = rtd->platform;
854 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
857 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
859 ret = soc_pcm_params_symmetry(substream, params);
863 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
864 ret = rtd->dai_link->ops->hw_params(substream, params);
866 dev_err(rtd->card->dev, "ASoC: machine hw_params"
867 " failed: %d\n", ret);
872 for (i = 0; i < rtd->num_codecs; i++) {
873 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
874 struct snd_pcm_hw_params codec_params;
877 * Skip CODECs which don't support the current stream type,
878 * the idea being that if a CODEC is not used for the currently
879 * set up transfer direction, it should not need to be
880 * configured, especially since the configuration used might
881 * not even be supported by that CODEC. There may be cases
882 * however where a CODEC needs to be set up although it is
883 * actually not being used for the transfer, e.g. if a
884 * capture-only CODEC is acting as an LRCLK and/or BCLK master
885 * for the DAI link including a playback-only CODEC.
886 * If this becomes necessary, we will have to augment the
887 * machine driver setup with information on how to act, so
888 * we can do the right thing here.
890 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
893 /* copy params for each codec */
894 codec_params = *params;
896 /* fixup params based on TDM slot masks */
897 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
899 soc_pcm_codec_params_fixup(&codec_params,
902 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
904 soc_pcm_codec_params_fixup(&codec_params,
907 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
911 codec_dai->rate = params_rate(&codec_params);
912 codec_dai->channels = params_channels(&codec_params);
913 codec_dai->sample_bits = snd_pcm_format_physical_width(
914 params_format(&codec_params));
917 ret = soc_dai_hw_params(substream, params, cpu_dai);
921 if (platform->driver->ops && platform->driver->ops->hw_params) {
922 ret = platform->driver->ops->hw_params(substream, params);
924 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
925 platform->component.name, ret);
930 /* store the parameters for each DAIs */
931 cpu_dai->rate = params_rate(params);
932 cpu_dai->channels = params_channels(params);
933 cpu_dai->sample_bits =
934 snd_pcm_format_physical_width(params_format(params));
937 mutex_unlock(&rtd->pcm_mutex);
941 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
942 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
949 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
950 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
951 codec_dai->driver->ops->hw_free(substream, codec_dai);
955 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
956 rtd->dai_link->ops->hw_free(substream);
958 mutex_unlock(&rtd->pcm_mutex);
963 * Frees resources allocated by hw_params, can be called multiple times
965 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
967 struct snd_soc_pcm_runtime *rtd = substream->private_data;
968 struct snd_soc_platform *platform = rtd->platform;
969 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
970 struct snd_soc_dai *codec_dai;
971 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
974 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
976 /* clear the corresponding DAIs parameters when going to be inactive */
977 if (cpu_dai->active == 1) {
979 cpu_dai->channels = 0;
980 cpu_dai->sample_bits = 0;
983 for (i = 0; i < rtd->num_codecs; i++) {
984 codec_dai = rtd->codec_dais[i];
985 if (codec_dai->active == 1) {
987 codec_dai->channels = 0;
988 codec_dai->sample_bits = 0;
992 /* apply codec digital mute */
993 for (i = 0; i < rtd->num_codecs; i++) {
994 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
995 (!playback && rtd->codec_dais[i]->capture_active == 1))
996 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1000 /* free any machine hw params */
1001 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1002 rtd->dai_link->ops->hw_free(substream);
1004 /* free any DMA resources */
1005 if (platform->driver->ops && platform->driver->ops->hw_free)
1006 platform->driver->ops->hw_free(substream);
1008 /* now free hw params for the DAIs */
1009 for (i = 0; i < rtd->num_codecs; i++) {
1010 codec_dai = rtd->codec_dais[i];
1011 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1012 codec_dai->driver->ops->hw_free(substream, codec_dai);
1015 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1016 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1018 mutex_unlock(&rtd->pcm_mutex);
1022 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1024 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1025 struct snd_soc_platform *platform = rtd->platform;
1026 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1027 struct snd_soc_dai *codec_dai;
1030 for (i = 0; i < rtd->num_codecs; i++) {
1031 codec_dai = rtd->codec_dais[i];
1032 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1033 ret = codec_dai->driver->ops->trigger(substream,
1040 if (platform->driver->ops && platform->driver->ops->trigger) {
1041 ret = platform->driver->ops->trigger(substream, cmd);
1046 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1047 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1052 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1053 ret = rtd->dai_link->ops->trigger(substream, cmd);
1061 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1064 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1065 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1066 struct snd_soc_dai *codec_dai;
1069 for (i = 0; i < rtd->num_codecs; i++) {
1070 codec_dai = rtd->codec_dais[i];
1071 if (codec_dai->driver->ops &&
1072 codec_dai->driver->ops->bespoke_trigger) {
1073 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1080 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1081 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1088 * soc level wrapper for pointer callback
1089 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1090 * the runtime->delay will be updated accordingly.
1092 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1094 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1095 struct snd_soc_platform *platform = rtd->platform;
1096 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1097 struct snd_soc_dai *codec_dai;
1098 struct snd_pcm_runtime *runtime = substream->runtime;
1099 snd_pcm_uframes_t offset = 0;
1100 snd_pcm_sframes_t delay = 0;
1101 snd_pcm_sframes_t codec_delay = 0;
1104 if (platform->driver->ops && platform->driver->ops->pointer)
1105 offset = platform->driver->ops->pointer(substream);
1107 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1108 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1110 for (i = 0; i < rtd->num_codecs; i++) {
1111 codec_dai = rtd->codec_dais[i];
1112 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1113 codec_delay = max(codec_delay,
1114 codec_dai->driver->ops->delay(substream,
1117 delay += codec_delay;
1119 runtime->delay = delay;
1124 /* connect a FE and BE */
1125 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1126 struct snd_soc_pcm_runtime *be, int stream)
1128 struct snd_soc_dpcm *dpcm;
1130 /* only add new dpcms */
1131 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1132 if (dpcm->be == be && dpcm->fe == fe)
1136 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1142 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1143 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1144 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1145 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1147 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1148 stream ? "capture" : "playback", fe->dai_link->name,
1149 stream ? "<-" : "->", be->dai_link->name);
1151 #ifdef CONFIG_DEBUG_FS
1152 if (fe->debugfs_dpcm_root)
1153 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1154 fe->debugfs_dpcm_root, &dpcm->state);
1159 /* reparent a BE onto another FE */
1160 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1161 struct snd_soc_pcm_runtime *be, int stream)
1163 struct snd_soc_dpcm *dpcm;
1164 struct snd_pcm_substream *fe_substream, *be_substream;
1166 /* reparent if BE is connected to other FEs */
1167 if (!be->dpcm[stream].users)
1170 be_substream = snd_soc_dpcm_get_substream(be, stream);
1172 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1176 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1177 stream ? "capture" : "playback",
1178 dpcm->fe->dai_link->name,
1179 stream ? "<-" : "->", dpcm->be->dai_link->name);
1181 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1182 be_substream->runtime = fe_substream->runtime;
1187 /* disconnect a BE and FE */
1188 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1190 struct snd_soc_dpcm *dpcm, *d;
1192 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1193 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1194 stream ? "capture" : "playback",
1195 dpcm->be->dai_link->name);
1197 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1200 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1201 stream ? "capture" : "playback", fe->dai_link->name,
1202 stream ? "<-" : "->", dpcm->be->dai_link->name);
1204 /* BEs still alive need new FE */
1205 dpcm_be_reparent(fe, dpcm->be, stream);
1207 #ifdef CONFIG_DEBUG_FS
1208 debugfs_remove(dpcm->debugfs_state);
1210 list_del(&dpcm->list_be);
1211 list_del(&dpcm->list_fe);
1216 /* get BE for DAI widget and stream */
1217 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1218 struct snd_soc_dapm_widget *widget, int stream)
1220 struct snd_soc_pcm_runtime *be;
1223 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1224 list_for_each_entry(be, &card->rtd_list, list) {
1226 if (!be->dai_link->no_pcm)
1229 if (be->cpu_dai->playback_widget == widget)
1232 for (i = 0; i < be->num_codecs; i++) {
1233 struct snd_soc_dai *dai = be->codec_dais[i];
1234 if (dai->playback_widget == widget)
1240 list_for_each_entry(be, &card->rtd_list, list) {
1242 if (!be->dai_link->no_pcm)
1245 if (be->cpu_dai->capture_widget == widget)
1248 for (i = 0; i < be->num_codecs; i++) {
1249 struct snd_soc_dai *dai = be->codec_dais[i];
1250 if (dai->capture_widget == widget)
1256 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1257 stream ? "capture" : "playback", widget->name);
1261 static inline struct snd_soc_dapm_widget *
1262 dai_get_widget(struct snd_soc_dai *dai, int stream)
1264 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1265 return dai->playback_widget;
1267 return dai->capture_widget;
1270 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1271 struct snd_soc_dapm_widget *widget)
1275 for (i = 0; i < list->num_widgets; i++) {
1276 if (widget == list->widgets[i])
1283 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1284 enum snd_soc_dapm_direction dir)
1286 struct snd_soc_card *card = widget->dapm->card;
1287 struct snd_soc_pcm_runtime *rtd;
1290 if (dir == SND_SOC_DAPM_DIR_OUT) {
1291 list_for_each_entry(rtd, &card->rtd_list, list) {
1292 if (!rtd->dai_link->no_pcm)
1295 if (rtd->cpu_dai->playback_widget == widget)
1298 for (i = 0; i < rtd->num_codecs; ++i) {
1299 struct snd_soc_dai *dai = rtd->codec_dais[i];
1300 if (dai->playback_widget == widget)
1304 } else { /* SND_SOC_DAPM_DIR_IN */
1305 list_for_each_entry(rtd, &card->rtd_list, list) {
1306 if (!rtd->dai_link->no_pcm)
1309 if (rtd->cpu_dai->capture_widget == widget)
1312 for (i = 0; i < rtd->num_codecs; ++i) {
1313 struct snd_soc_dai *dai = rtd->codec_dais[i];
1314 if (dai->capture_widget == widget)
1323 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1324 int stream, struct snd_soc_dapm_widget_list **list)
1326 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1329 /* get number of valid DAI paths and their widgets */
1330 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1331 dpcm_end_walk_at_be);
1333 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1334 stream ? "capture" : "playback");
1339 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1340 struct snd_soc_dapm_widget_list **list_)
1342 struct snd_soc_dpcm *dpcm;
1343 struct snd_soc_dapm_widget_list *list = *list_;
1344 struct snd_soc_dapm_widget *widget;
1347 /* Destroy any old FE <--> BE connections */
1348 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1351 /* is there a valid CPU DAI widget for this BE */
1352 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1354 /* prune the BE if it's no longer in our active list */
1355 if (widget && widget_in_list(list, widget))
1358 /* is there a valid CODEC DAI widget for this BE */
1359 for (i = 0; i < dpcm->be->num_codecs; i++) {
1360 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1361 widget = dai_get_widget(dai, stream);
1363 /* prune the BE if it's no longer in our active list */
1364 if (widget && widget_in_list(list, widget))
1368 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1369 stream ? "capture" : "playback",
1370 dpcm->be->dai_link->name, fe->dai_link->name);
1371 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1372 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1376 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1380 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1381 struct snd_soc_dapm_widget_list **list_)
1383 struct snd_soc_card *card = fe->card;
1384 struct snd_soc_dapm_widget_list *list = *list_;
1385 struct snd_soc_pcm_runtime *be;
1386 int i, new = 0, err;
1388 /* Create any new FE <--> BE connections */
1389 for (i = 0; i < list->num_widgets; i++) {
1391 switch (list->widgets[i]->id) {
1392 case snd_soc_dapm_dai_in:
1393 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1396 case snd_soc_dapm_dai_out:
1397 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1404 /* is there a valid BE rtd for this widget */
1405 be = dpcm_get_be(card, list->widgets[i], stream);
1407 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1408 list->widgets[i]->name);
1412 /* make sure BE is a real BE */
1413 if (!be->dai_link->no_pcm)
1416 /* don't connect if FE is not running */
1417 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1420 /* newly connected FE and BE */
1421 err = dpcm_be_connect(fe, be, stream);
1423 dev_err(fe->dev, "ASoC: can't connect %s\n",
1424 list->widgets[i]->name);
1426 } else if (err == 0) /* already connected */
1430 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1434 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1439 * Find the corresponding BE DAIs that source or sink audio to this
1442 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1443 int stream, struct snd_soc_dapm_widget_list **list, int new)
1446 return dpcm_add_paths(fe, stream, list);
1448 return dpcm_prune_paths(fe, stream, list);
1451 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1453 struct snd_soc_dpcm *dpcm;
1455 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1456 dpcm->be->dpcm[stream].runtime_update =
1457 SND_SOC_DPCM_UPDATE_NO;
1460 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1463 struct snd_soc_dpcm *dpcm;
1465 /* disable any enabled and non active backends */
1466 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1468 struct snd_soc_pcm_runtime *be = dpcm->be;
1469 struct snd_pcm_substream *be_substream =
1470 snd_soc_dpcm_get_substream(be, stream);
1472 if (be->dpcm[stream].users == 0)
1473 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1474 stream ? "capture" : "playback",
1475 be->dpcm[stream].state);
1477 if (--be->dpcm[stream].users != 0)
1480 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1483 soc_pcm_close(be_substream);
1484 be_substream->runtime = NULL;
1485 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1489 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1491 struct snd_soc_dpcm *dpcm;
1494 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1495 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1497 struct snd_soc_pcm_runtime *be = dpcm->be;
1498 struct snd_pcm_substream *be_substream =
1499 snd_soc_dpcm_get_substream(be, stream);
1501 if (!be_substream) {
1502 dev_err(be->dev, "ASoC: no backend %s stream\n",
1503 stream ? "capture" : "playback");
1507 /* is this op for this BE ? */
1508 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1511 /* first time the dpcm is open ? */
1512 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1513 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1514 stream ? "capture" : "playback",
1515 be->dpcm[stream].state);
1517 if (be->dpcm[stream].users++ != 0)
1520 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1521 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1524 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1525 stream ? "capture" : "playback", be->dai_link->name);
1527 be_substream->runtime = be->dpcm[stream].runtime;
1528 err = soc_pcm_open(be_substream);
1530 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1531 be->dpcm[stream].users--;
1532 if (be->dpcm[stream].users < 0)
1533 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1534 stream ? "capture" : "playback",
1535 be->dpcm[stream].state);
1537 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1541 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1548 /* disable any enabled and non active backends */
1549 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1550 struct snd_soc_pcm_runtime *be = dpcm->be;
1551 struct snd_pcm_substream *be_substream =
1552 snd_soc_dpcm_get_substream(be, stream);
1554 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1557 if (be->dpcm[stream].users == 0)
1558 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1559 stream ? "capture" : "playback",
1560 be->dpcm[stream].state);
1562 if (--be->dpcm[stream].users != 0)
1565 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1568 soc_pcm_close(be_substream);
1569 be_substream->runtime = NULL;
1570 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1576 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1577 struct snd_soc_pcm_stream *stream,
1580 runtime->hw.rate_min = stream->rate_min;
1581 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1582 runtime->hw.channels_min = stream->channels_min;
1583 runtime->hw.channels_max = stream->channels_max;
1584 if (runtime->hw.formats)
1585 runtime->hw.formats &= formats & stream->formats;
1587 runtime->hw.formats = formats & stream->formats;
1588 runtime->hw.rates = stream->rates;
1591 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1593 struct snd_soc_pcm_runtime *fe = substream->private_data;
1594 struct snd_soc_dpcm *dpcm;
1595 u64 formats = ULLONG_MAX;
1596 int stream = substream->stream;
1598 if (!fe->dai_link->dpcm_merged_format)
1602 * It returns merged BE codec format
1603 * if FE want to use it (= dpcm_merged_format)
1606 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1607 struct snd_soc_pcm_runtime *be = dpcm->be;
1608 struct snd_soc_dai_driver *codec_dai_drv;
1609 struct snd_soc_pcm_stream *codec_stream;
1612 for (i = 0; i < be->num_codecs; i++) {
1614 * Skip CODECs which don't support the current stream
1615 * type. See soc_pcm_init_runtime_hw() for more details
1617 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1621 codec_dai_drv = be->codec_dais[i]->driver;
1622 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1623 codec_stream = &codec_dai_drv->playback;
1625 codec_stream = &codec_dai_drv->capture;
1627 formats &= codec_stream->formats;
1634 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1636 struct snd_pcm_runtime *runtime = substream->runtime;
1637 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1638 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1639 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1640 u64 format = dpcm_runtime_base_format(substream);
1642 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1643 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1645 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1648 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1650 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1651 * for avoiding the race with trigger callback.
1652 * If the state is unset and a trigger is pending while the previous operation,
1653 * process the pending trigger action here.
1655 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1656 int stream, enum snd_soc_dpcm_update state)
1658 struct snd_pcm_substream *substream =
1659 snd_soc_dpcm_get_substream(fe, stream);
1661 snd_pcm_stream_lock_irq(substream);
1662 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1663 dpcm_fe_dai_do_trigger(substream,
1664 fe->dpcm[stream].trigger_pending - 1);
1665 fe->dpcm[stream].trigger_pending = 0;
1667 fe->dpcm[stream].runtime_update = state;
1668 snd_pcm_stream_unlock_irq(substream);
1671 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1674 struct snd_soc_dpcm *dpcm;
1675 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1676 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1679 /* apply symmetry for FE */
1680 if (soc_pcm_has_symmetry(fe_substream))
1681 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1683 /* Symmetry only applies if we've got an active stream. */
1684 if (fe_cpu_dai->active) {
1685 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1690 /* apply symmetry for BE */
1691 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1692 struct snd_soc_pcm_runtime *be = dpcm->be;
1693 struct snd_pcm_substream *be_substream =
1694 snd_soc_dpcm_get_substream(be, stream);
1695 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1698 if (rtd->dai_link->be_hw_params_fixup)
1701 if (soc_pcm_has_symmetry(be_substream))
1702 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1704 /* Symmetry only applies if we've got an active stream. */
1705 if (rtd->cpu_dai->active) {
1706 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1711 for (i = 0; i < rtd->num_codecs; i++) {
1712 if (rtd->codec_dais[i]->active) {
1713 err = soc_pcm_apply_symmetry(be_substream,
1714 rtd->codec_dais[i]);
1724 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1726 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1727 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1728 int stream = fe_substream->stream, ret = 0;
1730 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1732 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1734 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1738 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1740 /* start the DAI frontend */
1741 ret = soc_pcm_open(fe_substream);
1743 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1747 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1749 dpcm_set_fe_runtime(fe_substream);
1750 snd_pcm_limit_hw_rates(runtime);
1752 ret = dpcm_apply_symmetry(fe_substream, stream);
1754 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1759 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1763 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1765 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1769 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1771 struct snd_soc_dpcm *dpcm;
1773 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1774 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1776 struct snd_soc_pcm_runtime *be = dpcm->be;
1777 struct snd_pcm_substream *be_substream =
1778 snd_soc_dpcm_get_substream(be, stream);
1780 /* is this op for this BE ? */
1781 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1784 if (be->dpcm[stream].users == 0)
1785 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1786 stream ? "capture" : "playback",
1787 be->dpcm[stream].state);
1789 if (--be->dpcm[stream].users != 0)
1792 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1793 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1794 soc_pcm_hw_free(be_substream);
1795 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1798 dev_dbg(be->dev, "ASoC: close BE %s\n",
1799 be->dai_link->name);
1801 soc_pcm_close(be_substream);
1802 be_substream->runtime = NULL;
1804 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1809 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1811 struct snd_soc_pcm_runtime *fe = substream->private_data;
1812 int stream = substream->stream;
1814 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1816 /* shutdown the BEs */
1817 dpcm_be_dai_shutdown(fe, substream->stream);
1819 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1821 /* now shutdown the frontend */
1822 soc_pcm_close(substream);
1824 /* run the stream event for each BE */
1825 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1827 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1828 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1832 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1834 struct snd_soc_dpcm *dpcm;
1836 /* only hw_params backends that are either sinks or sources
1837 * to this frontend DAI */
1838 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1840 struct snd_soc_pcm_runtime *be = dpcm->be;
1841 struct snd_pcm_substream *be_substream =
1842 snd_soc_dpcm_get_substream(be, stream);
1844 /* is this op for this BE ? */
1845 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1848 /* only free hw when no longer used - check all FEs */
1849 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1852 /* do not free hw if this BE is used by other FE */
1853 if (be->dpcm[stream].users > 1)
1856 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1857 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1858 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1859 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1860 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1861 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1864 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1865 be->dai_link->name);
1867 soc_pcm_hw_free(be_substream);
1869 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1875 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1877 struct snd_soc_pcm_runtime *fe = substream->private_data;
1878 int err, stream = substream->stream;
1880 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1881 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1883 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1885 /* call hw_free on the frontend */
1886 err = soc_pcm_hw_free(substream);
1888 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1889 fe->dai_link->name);
1891 /* only hw_params backends that are either sinks or sources
1892 * to this frontend DAI */
1893 err = dpcm_be_dai_hw_free(fe, stream);
1895 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1896 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1898 mutex_unlock(&fe->card->mutex);
1902 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1904 struct snd_soc_dpcm *dpcm;
1907 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1909 struct snd_soc_pcm_runtime *be = dpcm->be;
1910 struct snd_pcm_substream *be_substream =
1911 snd_soc_dpcm_get_substream(be, stream);
1913 /* is this op for this BE ? */
1914 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1917 /* copy params for each dpcm */
1918 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1919 sizeof(struct snd_pcm_hw_params));
1921 /* perform any hw_params fixups */
1922 if (be->dai_link->be_hw_params_fixup) {
1923 ret = be->dai_link->be_hw_params_fixup(be,
1927 "ASoC: hw_params BE fixup failed %d\n",
1933 /* only allow hw_params() if no connected FEs are running */
1934 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1937 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1938 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1942 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1943 be->dai_link->name);
1945 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1947 dev_err(dpcm->be->dev,
1948 "ASoC: hw_params BE failed %d\n", ret);
1952 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1957 /* disable any enabled and non active backends */
1958 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1959 struct snd_soc_pcm_runtime *be = dpcm->be;
1960 struct snd_pcm_substream *be_substream =
1961 snd_soc_dpcm_get_substream(be, stream);
1963 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1966 /* only allow hw_free() if no connected FEs are running */
1967 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1970 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1971 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1972 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1973 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1976 soc_pcm_hw_free(be_substream);
1982 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1983 struct snd_pcm_hw_params *params)
1985 struct snd_soc_pcm_runtime *fe = substream->private_data;
1986 int ret, stream = substream->stream;
1988 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1989 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1991 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1992 sizeof(struct snd_pcm_hw_params));
1993 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1995 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1999 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2000 fe->dai_link->name, params_rate(params),
2001 params_channels(params), params_format(params));
2003 /* call hw_params on the frontend */
2004 ret = soc_pcm_hw_params(substream, params);
2006 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2007 dpcm_be_dai_hw_free(fe, stream);
2009 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2012 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2013 mutex_unlock(&fe->card->mutex);
2017 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2018 struct snd_pcm_substream *substream, int cmd)
2022 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2023 dpcm->be->dai_link->name, cmd);
2025 ret = soc_pcm_trigger(substream, cmd);
2027 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2032 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2035 struct snd_soc_dpcm *dpcm;
2038 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2040 struct snd_soc_pcm_runtime *be = dpcm->be;
2041 struct snd_pcm_substream *be_substream =
2042 snd_soc_dpcm_get_substream(be, stream);
2044 /* is this op for this BE ? */
2045 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2049 case SNDRV_PCM_TRIGGER_START:
2050 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2052 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2055 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2059 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2061 case SNDRV_PCM_TRIGGER_RESUME:
2062 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2065 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2069 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2071 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2072 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2075 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2079 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2081 case SNDRV_PCM_TRIGGER_STOP:
2082 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2083 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2086 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2089 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2093 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2095 case SNDRV_PCM_TRIGGER_SUSPEND:
2096 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2099 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2102 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2106 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2108 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2109 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2112 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2115 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2119 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2126 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2128 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2129 int cmd, bool fe_first)
2131 struct snd_soc_pcm_runtime *fe = substream->private_data;
2134 /* call trigger on the frontend before the backend. */
2136 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2137 fe->dai_link->name, cmd);
2139 ret = soc_pcm_trigger(substream, cmd);
2143 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2147 /* call trigger on the frontend after the backend. */
2148 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2152 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2153 fe->dai_link->name, cmd);
2155 ret = soc_pcm_trigger(substream, cmd);
2160 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2162 struct snd_soc_pcm_runtime *fe = substream->private_data;
2163 int stream = substream->stream;
2165 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2167 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2170 case SND_SOC_DPCM_TRIGGER_PRE:
2172 case SNDRV_PCM_TRIGGER_START:
2173 case SNDRV_PCM_TRIGGER_RESUME:
2174 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2175 case SNDRV_PCM_TRIGGER_DRAIN:
2176 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2178 case SNDRV_PCM_TRIGGER_STOP:
2179 case SNDRV_PCM_TRIGGER_SUSPEND:
2180 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2181 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2188 case SND_SOC_DPCM_TRIGGER_POST:
2190 case SNDRV_PCM_TRIGGER_START:
2191 case SNDRV_PCM_TRIGGER_RESUME:
2192 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2193 case SNDRV_PCM_TRIGGER_DRAIN:
2194 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2196 case SNDRV_PCM_TRIGGER_STOP:
2197 case SNDRV_PCM_TRIGGER_SUSPEND:
2198 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2199 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2206 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2207 /* bespoke trigger() - handles both FE and BEs */
2209 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2210 fe->dai_link->name, cmd);
2212 ret = soc_pcm_bespoke_trigger(substream, cmd);
2215 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2216 fe->dai_link->name);
2222 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2228 case SNDRV_PCM_TRIGGER_START:
2229 case SNDRV_PCM_TRIGGER_RESUME:
2230 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2231 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2233 case SNDRV_PCM_TRIGGER_STOP:
2234 case SNDRV_PCM_TRIGGER_SUSPEND:
2235 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2237 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2238 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2243 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2247 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2249 struct snd_soc_pcm_runtime *fe = substream->private_data;
2250 int stream = substream->stream;
2252 /* if FE's runtime_update is already set, we're in race;
2253 * process this trigger later at exit
2255 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2256 fe->dpcm[stream].trigger_pending = cmd + 1;
2257 return 0; /* delayed, assuming it's successful */
2260 /* we're alone, let's trigger */
2261 return dpcm_fe_dai_do_trigger(substream, cmd);
2264 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2266 struct snd_soc_dpcm *dpcm;
2269 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2271 struct snd_soc_pcm_runtime *be = dpcm->be;
2272 struct snd_pcm_substream *be_substream =
2273 snd_soc_dpcm_get_substream(be, stream);
2275 /* is this op for this BE ? */
2276 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2279 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2280 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2281 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2282 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2285 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2286 be->dai_link->name);
2288 ret = soc_pcm_prepare(be_substream);
2290 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2295 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2300 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2302 struct snd_soc_pcm_runtime *fe = substream->private_data;
2303 int stream = substream->stream, ret = 0;
2305 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2307 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2309 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2311 /* there is no point preparing this FE if there are no BEs */
2312 if (list_empty(&fe->dpcm[stream].be_clients)) {
2313 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2314 fe->dai_link->name);
2319 ret = dpcm_be_dai_prepare(fe, substream->stream);
2323 /* call prepare on the frontend */
2324 ret = soc_pcm_prepare(substream);
2326 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2327 fe->dai_link->name);
2331 /* run the stream event for each BE */
2332 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2333 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2336 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2337 mutex_unlock(&fe->card->mutex);
2342 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2343 unsigned int cmd, void *arg)
2345 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2346 struct snd_soc_platform *platform = rtd->platform;
2348 if (platform->driver->ops && platform->driver->ops->ioctl)
2349 return platform->driver->ops->ioctl(substream, cmd, arg);
2350 return snd_pcm_lib_ioctl(substream, cmd, arg);
2353 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2355 struct snd_pcm_substream *substream =
2356 snd_soc_dpcm_get_substream(fe, stream);
2357 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2360 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2361 stream ? "capture" : "playback", fe->dai_link->name);
2363 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2364 /* call bespoke trigger - FE takes care of all BE triggers */
2365 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2366 fe->dai_link->name);
2368 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2370 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2372 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2373 fe->dai_link->name);
2375 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2377 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2380 err = dpcm_be_dai_hw_free(fe, stream);
2382 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2384 err = dpcm_be_dai_shutdown(fe, stream);
2386 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2388 /* run the stream event for each BE */
2389 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2394 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2396 struct snd_pcm_substream *substream =
2397 snd_soc_dpcm_get_substream(fe, stream);
2398 struct snd_soc_dpcm *dpcm;
2399 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2402 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2403 stream ? "capture" : "playback", fe->dai_link->name);
2405 /* Only start the BE if the FE is ready */
2406 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2407 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2410 /* startup must always be called for new BEs */
2411 ret = dpcm_be_dai_startup(fe, stream);
2415 /* keep going if FE state is > open */
2416 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2419 ret = dpcm_be_dai_hw_params(fe, stream);
2423 /* keep going if FE state is > hw_params */
2424 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2428 ret = dpcm_be_dai_prepare(fe, stream);
2432 /* run the stream event for each BE */
2433 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2435 /* keep going if FE state is > prepare */
2436 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2437 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2440 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2441 /* call trigger on the frontend - FE takes care of all BE triggers */
2442 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2443 fe->dai_link->name);
2445 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2447 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2451 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2452 fe->dai_link->name);
2454 ret = dpcm_be_dai_trigger(fe, stream,
2455 SNDRV_PCM_TRIGGER_START);
2457 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2465 dpcm_be_dai_hw_free(fe, stream);
2467 dpcm_be_dai_shutdown(fe, stream);
2469 /* disconnect any non started BEs */
2470 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2471 struct snd_soc_pcm_runtime *be = dpcm->be;
2472 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2473 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2479 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2483 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2484 ret = dpcm_run_update_startup(fe, stream);
2486 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2487 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2492 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2496 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2497 ret = dpcm_run_update_shutdown(fe, stream);
2499 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2500 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2505 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2508 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2510 struct snd_soc_pcm_runtime *fe;
2511 int old, new, paths;
2513 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2514 list_for_each_entry(fe, &card->rtd_list, list) {
2515 struct snd_soc_dapm_widget_list *list;
2517 /* make sure link is FE */
2518 if (!fe->dai_link->dynamic)
2521 /* only check active links */
2522 if (!fe->cpu_dai->active)
2525 /* DAPM sync will call this to update DSP paths */
2526 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2527 fe->dai_link->name);
2529 /* skip if FE doesn't have playback capability */
2530 if (!fe->cpu_dai->driver->playback.channels_min
2531 || !fe->codec_dai->driver->playback.channels_min)
2534 /* skip if FE isn't currently playing */
2535 if (!fe->cpu_dai->playback_active
2536 || !fe->codec_dai->playback_active)
2539 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2541 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2542 fe->dai_link->name, "playback");
2543 mutex_unlock(&card->mutex);
2547 /* update any new playback paths */
2548 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2550 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2551 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2552 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2555 /* update any old playback paths */
2556 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2558 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2559 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2560 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2563 dpcm_path_put(&list);
2565 /* skip if FE doesn't have capture capability */
2566 if (!fe->cpu_dai->driver->capture.channels_min
2567 || !fe->codec_dai->driver->capture.channels_min)
2570 /* skip if FE isn't currently capturing */
2571 if (!fe->cpu_dai->capture_active
2572 || !fe->codec_dai->capture_active)
2575 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2577 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2578 fe->dai_link->name, "capture");
2579 mutex_unlock(&card->mutex);
2583 /* update any new capture paths */
2584 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2586 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2587 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2588 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2591 /* update any old capture paths */
2592 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2594 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2595 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2596 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2599 dpcm_path_put(&list);
2602 mutex_unlock(&card->mutex);
2605 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2607 struct snd_soc_dpcm *dpcm;
2608 struct list_head *clients =
2609 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2611 list_for_each_entry(dpcm, clients, list_be) {
2613 struct snd_soc_pcm_runtime *be = dpcm->be;
2616 if (be->dai_link->ignore_suspend)
2619 for (i = 0; i < be->num_codecs; i++) {
2620 struct snd_soc_dai *dai = be->codec_dais[i];
2621 struct snd_soc_dai_driver *drv = dai->driver;
2623 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2624 be->dai_link->name);
2626 if (drv->ops && drv->ops->digital_mute &&
2627 dai->playback_active)
2628 drv->ops->digital_mute(dai, mute);
2635 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2637 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2638 struct snd_soc_dpcm *dpcm;
2639 struct snd_soc_dapm_widget_list *list;
2641 int stream = fe_substream->stream;
2643 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2644 fe->dpcm[stream].runtime = fe_substream->runtime;
2646 ret = dpcm_path_get(fe, stream, &list);
2648 mutex_unlock(&fe->card->mutex);
2650 } else if (ret == 0) {
2651 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2652 fe->dai_link->name, stream ? "capture" : "playback");
2655 /* calculate valid and active FE <-> BE dpcms */
2656 dpcm_process_paths(fe, stream, &list, 1);
2658 ret = dpcm_fe_dai_startup(fe_substream);
2660 /* clean up all links */
2661 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2662 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2664 dpcm_be_disconnect(fe, stream);
2665 fe->dpcm[stream].runtime = NULL;
2668 dpcm_clear_pending_state(fe, stream);
2669 dpcm_path_put(&list);
2670 mutex_unlock(&fe->card->mutex);
2674 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2676 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2677 struct snd_soc_dpcm *dpcm;
2678 int stream = fe_substream->stream, ret;
2680 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2681 ret = dpcm_fe_dai_shutdown(fe_substream);
2683 /* mark FE's links ready to prune */
2684 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2685 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2687 dpcm_be_disconnect(fe, stream);
2689 fe->dpcm[stream].runtime = NULL;
2690 mutex_unlock(&fe->card->mutex);
2694 /* create a new pcm */
2695 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2697 struct snd_soc_platform *platform = rtd->platform;
2698 struct snd_soc_dai *codec_dai;
2699 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2700 struct snd_pcm *pcm;
2702 int ret = 0, playback = 0, capture = 0;
2705 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2706 playback = rtd->dai_link->dpcm_playback;
2707 capture = rtd->dai_link->dpcm_capture;
2709 for (i = 0; i < rtd->num_codecs; i++) {
2710 codec_dai = rtd->codec_dais[i];
2711 if (codec_dai->driver->playback.channels_min)
2713 if (codec_dai->driver->capture.channels_min)
2717 capture = capture && cpu_dai->driver->capture.channels_min;
2718 playback = playback && cpu_dai->driver->playback.channels_min;
2721 if (rtd->dai_link->playback_only) {
2726 if (rtd->dai_link->capture_only) {
2731 /* create the PCM */
2732 if (rtd->dai_link->no_pcm) {
2733 snprintf(new_name, sizeof(new_name), "(%s)",
2734 rtd->dai_link->stream_name);
2736 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2737 playback, capture, &pcm);
2739 if (rtd->dai_link->dynamic)
2740 snprintf(new_name, sizeof(new_name), "%s (*)",
2741 rtd->dai_link->stream_name);
2743 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2744 rtd->dai_link->stream_name,
2745 (rtd->num_codecs > 1) ?
2746 "multicodec" : rtd->codec_dai->name, num);
2748 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2752 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2753 rtd->dai_link->name);
2756 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2758 /* DAPM dai link stream work */
2759 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2761 pcm->nonatomic = rtd->dai_link->nonatomic;
2763 pcm->private_data = rtd;
2765 if (rtd->dai_link->no_pcm) {
2767 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2769 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2773 /* ASoC PCM operations */
2774 if (rtd->dai_link->dynamic) {
2775 rtd->ops.open = dpcm_fe_dai_open;
2776 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2777 rtd->ops.prepare = dpcm_fe_dai_prepare;
2778 rtd->ops.trigger = dpcm_fe_dai_trigger;
2779 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2780 rtd->ops.close = dpcm_fe_dai_close;
2781 rtd->ops.pointer = soc_pcm_pointer;
2782 rtd->ops.ioctl = soc_pcm_ioctl;
2784 rtd->ops.open = soc_pcm_open;
2785 rtd->ops.hw_params = soc_pcm_hw_params;
2786 rtd->ops.prepare = soc_pcm_prepare;
2787 rtd->ops.trigger = soc_pcm_trigger;
2788 rtd->ops.hw_free = soc_pcm_hw_free;
2789 rtd->ops.close = soc_pcm_close;
2790 rtd->ops.pointer = soc_pcm_pointer;
2791 rtd->ops.ioctl = soc_pcm_ioctl;
2794 if (platform->driver->ops) {
2795 rtd->ops.ack = platform->driver->ops->ack;
2796 rtd->ops.copy_user = platform->driver->ops->copy_user;
2797 rtd->ops.copy_kernel = platform->driver->ops->copy_kernel;
2798 rtd->ops.fill_silence = platform->driver->ops->fill_silence;
2799 rtd->ops.page = platform->driver->ops->page;
2800 rtd->ops.mmap = platform->driver->ops->mmap;
2804 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2807 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2809 if (platform->driver->pcm_new) {
2810 ret = platform->driver->pcm_new(rtd);
2812 dev_err(platform->dev,
2813 "ASoC: pcm constructor failed: %d\n",
2819 pcm->private_free = platform->driver->pcm_free;
2821 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2822 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2827 /* is the current PCM operation for this FE ? */
2828 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2830 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2834 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2836 /* is the current PCM operation for this BE ? */
2837 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2838 struct snd_soc_pcm_runtime *be, int stream)
2840 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2841 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2842 be->dpcm[stream].runtime_update))
2846 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2848 /* get the substream for this BE */
2849 struct snd_pcm_substream *
2850 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2852 return be->pcm->streams[stream].substream;
2854 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2856 /* get the BE runtime state */
2857 enum snd_soc_dpcm_state
2858 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2860 return be->dpcm[stream].state;
2862 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2864 /* set the BE runtime state */
2865 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2866 int stream, enum snd_soc_dpcm_state state)
2868 be->dpcm[stream].state = state;
2870 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2873 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2874 * are not running, paused or suspended for the specified stream direction.
2876 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2877 struct snd_soc_pcm_runtime *be, int stream)
2879 struct snd_soc_dpcm *dpcm;
2882 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2887 state = dpcm->fe->dpcm[stream].state;
2888 if (state == SND_SOC_DPCM_STATE_START ||
2889 state == SND_SOC_DPCM_STATE_PAUSED ||
2890 state == SND_SOC_DPCM_STATE_SUSPEND)
2894 /* it's safe to free/stop this BE DAI */
2897 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2900 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2901 * running, paused or suspended for the specified stream direction.
2903 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2904 struct snd_soc_pcm_runtime *be, int stream)
2906 struct snd_soc_dpcm *dpcm;
2909 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2914 state = dpcm->fe->dpcm[stream].state;
2915 if (state == SND_SOC_DPCM_STATE_START ||
2916 state == SND_SOC_DPCM_STATE_PAUSED ||
2917 state == SND_SOC_DPCM_STATE_SUSPEND ||
2918 state == SND_SOC_DPCM_STATE_PREPARE)
2922 /* it's safe to change hw_params */
2925 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2927 #ifdef CONFIG_DEBUG_FS
2928 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
2931 case SND_SOC_DPCM_STATE_NEW:
2933 case SND_SOC_DPCM_STATE_OPEN:
2935 case SND_SOC_DPCM_STATE_HW_PARAMS:
2937 case SND_SOC_DPCM_STATE_PREPARE:
2939 case SND_SOC_DPCM_STATE_START:
2941 case SND_SOC_DPCM_STATE_STOP:
2943 case SND_SOC_DPCM_STATE_SUSPEND:
2945 case SND_SOC_DPCM_STATE_PAUSED:
2947 case SND_SOC_DPCM_STATE_HW_FREE:
2949 case SND_SOC_DPCM_STATE_CLOSE:
2956 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2957 int stream, char *buf, size_t size)
2959 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2960 struct snd_soc_dpcm *dpcm;
2964 offset += scnprintf(buf + offset, size - offset,
2965 "[%s - %s]\n", fe->dai_link->name,
2966 stream ? "Capture" : "Playback");
2968 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
2969 dpcm_state_string(fe->dpcm[stream].state));
2971 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2972 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2973 offset += scnprintf(buf + offset, size - offset,
2975 "Format = %s, Channels = %d, Rate = %d\n",
2976 snd_pcm_format_name(params_format(params)),
2977 params_channels(params),
2978 params_rate(params));
2981 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
2983 if (list_empty(&fe->dpcm[stream].be_clients)) {
2984 offset += scnprintf(buf + offset, size - offset,
2985 " No active DSP links\n");
2989 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2990 struct snd_soc_pcm_runtime *be = dpcm->be;
2991 params = &dpcm->hw_params;
2993 offset += scnprintf(buf + offset, size - offset,
2994 "- %s\n", be->dai_link->name);
2996 offset += scnprintf(buf + offset, size - offset,
2998 dpcm_state_string(be->dpcm[stream].state));
3000 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3001 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3002 offset += scnprintf(buf + offset, size - offset,
3003 " Hardware Params: "
3004 "Format = %s, Channels = %d, Rate = %d\n",
3005 snd_pcm_format_name(params_format(params)),
3006 params_channels(params),
3007 params_rate(params));
3014 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3015 size_t count, loff_t *ppos)
3017 struct snd_soc_pcm_runtime *fe = file->private_data;
3018 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3021 buf = kmalloc(out_count, GFP_KERNEL);
3025 if (fe->cpu_dai->driver->playback.channels_min)
3026 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3027 buf + offset, out_count - offset);
3029 if (fe->cpu_dai->driver->capture.channels_min)
3030 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3031 buf + offset, out_count - offset);
3033 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3039 static const struct file_operations dpcm_state_fops = {
3040 .open = simple_open,
3041 .read = dpcm_state_read_file,
3042 .llseek = default_llseek,
3045 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3050 if (!rtd->card->debugfs_card_root)
3053 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3054 rtd->card->debugfs_card_root);
3055 if (!rtd->debugfs_dpcm_root) {
3057 "ASoC: Failed to create dpcm debugfs directory %s\n",
3058 rtd->dai_link->name);
3062 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3063 rtd, &dpcm_state_fops);