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);
1174 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1178 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1179 stream ? "capture" : "playback",
1180 dpcm->fe->dai_link->name,
1181 stream ? "<-" : "->", dpcm->be->dai_link->name);
1183 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1184 be_substream->runtime = fe_substream->runtime;
1189 /* disconnect a BE and FE */
1190 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1192 struct snd_soc_dpcm *dpcm, *d;
1194 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1195 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1196 stream ? "capture" : "playback",
1197 dpcm->be->dai_link->name);
1199 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1202 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1203 stream ? "capture" : "playback", fe->dai_link->name,
1204 stream ? "<-" : "->", dpcm->be->dai_link->name);
1206 /* BEs still alive need new FE */
1207 dpcm_be_reparent(fe, dpcm->be, stream);
1209 #ifdef CONFIG_DEBUG_FS
1210 debugfs_remove(dpcm->debugfs_state);
1212 list_del(&dpcm->list_be);
1213 list_del(&dpcm->list_fe);
1218 /* get BE for DAI widget and stream */
1219 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1220 struct snd_soc_dapm_widget *widget, int stream)
1222 struct snd_soc_pcm_runtime *be;
1225 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1226 list_for_each_entry(be, &card->rtd_list, list) {
1228 if (!be->dai_link->no_pcm)
1231 if (be->cpu_dai->playback_widget == widget)
1234 for (i = 0; i < be->num_codecs; i++) {
1235 struct snd_soc_dai *dai = be->codec_dais[i];
1236 if (dai->playback_widget == widget)
1242 list_for_each_entry(be, &card->rtd_list, list) {
1244 if (!be->dai_link->no_pcm)
1247 if (be->cpu_dai->capture_widget == widget)
1250 for (i = 0; i < be->num_codecs; i++) {
1251 struct snd_soc_dai *dai = be->codec_dais[i];
1252 if (dai->capture_widget == widget)
1258 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1259 stream ? "capture" : "playback", widget->name);
1263 static inline struct snd_soc_dapm_widget *
1264 dai_get_widget(struct snd_soc_dai *dai, int stream)
1266 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1267 return dai->playback_widget;
1269 return dai->capture_widget;
1272 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1273 struct snd_soc_dapm_widget *widget)
1277 for (i = 0; i < list->num_widgets; i++) {
1278 if (widget == list->widgets[i])
1285 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1286 enum snd_soc_dapm_direction dir)
1288 struct snd_soc_card *card = widget->dapm->card;
1289 struct snd_soc_pcm_runtime *rtd;
1292 if (dir == SND_SOC_DAPM_DIR_OUT) {
1293 list_for_each_entry(rtd, &card->rtd_list, list) {
1294 if (!rtd->dai_link->no_pcm)
1297 if (rtd->cpu_dai->playback_widget == widget)
1300 for (i = 0; i < rtd->num_codecs; ++i) {
1301 struct snd_soc_dai *dai = rtd->codec_dais[i];
1302 if (dai->playback_widget == widget)
1306 } else { /* SND_SOC_DAPM_DIR_IN */
1307 list_for_each_entry(rtd, &card->rtd_list, list) {
1308 if (!rtd->dai_link->no_pcm)
1311 if (rtd->cpu_dai->capture_widget == widget)
1314 for (i = 0; i < rtd->num_codecs; ++i) {
1315 struct snd_soc_dai *dai = rtd->codec_dais[i];
1316 if (dai->capture_widget == widget)
1325 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1326 int stream, struct snd_soc_dapm_widget_list **list)
1328 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1331 /* get number of valid DAI paths and their widgets */
1332 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1333 dpcm_end_walk_at_be);
1335 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1336 stream ? "capture" : "playback");
1341 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1342 struct snd_soc_dapm_widget_list **list_)
1344 struct snd_soc_dpcm *dpcm;
1345 struct snd_soc_dapm_widget_list *list = *list_;
1346 struct snd_soc_dapm_widget *widget;
1349 /* Destroy any old FE <--> BE connections */
1350 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1353 /* is there a valid CPU DAI widget for this BE */
1354 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1356 /* prune the BE if it's no longer in our active list */
1357 if (widget && widget_in_list(list, widget))
1360 /* is there a valid CODEC DAI widget for this BE */
1361 for (i = 0; i < dpcm->be->num_codecs; i++) {
1362 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1363 widget = dai_get_widget(dai, stream);
1365 /* prune the BE if it's no longer in our active list */
1366 if (widget && widget_in_list(list, widget))
1370 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1371 stream ? "capture" : "playback",
1372 dpcm->be->dai_link->name, fe->dai_link->name);
1373 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1374 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1378 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1382 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1383 struct snd_soc_dapm_widget_list **list_)
1385 struct snd_soc_card *card = fe->card;
1386 struct snd_soc_dapm_widget_list *list = *list_;
1387 struct snd_soc_pcm_runtime *be;
1388 int i, new = 0, err;
1390 /* Create any new FE <--> BE connections */
1391 for (i = 0; i < list->num_widgets; i++) {
1393 switch (list->widgets[i]->id) {
1394 case snd_soc_dapm_dai_in:
1395 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1398 case snd_soc_dapm_dai_out:
1399 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1406 /* is there a valid BE rtd for this widget */
1407 be = dpcm_get_be(card, list->widgets[i], stream);
1409 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1410 list->widgets[i]->name);
1414 /* make sure BE is a real BE */
1415 if (!be->dai_link->no_pcm)
1418 /* don't connect if FE is not running */
1419 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1422 /* newly connected FE and BE */
1423 err = dpcm_be_connect(fe, be, stream);
1425 dev_err(fe->dev, "ASoC: can't connect %s\n",
1426 list->widgets[i]->name);
1428 } else if (err == 0) /* already connected */
1432 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1436 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1441 * Find the corresponding BE DAIs that source or sink audio to this
1444 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1445 int stream, struct snd_soc_dapm_widget_list **list, int new)
1448 return dpcm_add_paths(fe, stream, list);
1450 return dpcm_prune_paths(fe, stream, list);
1453 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1455 struct snd_soc_dpcm *dpcm;
1457 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1458 dpcm->be->dpcm[stream].runtime_update =
1459 SND_SOC_DPCM_UPDATE_NO;
1462 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1465 struct snd_soc_dpcm *dpcm;
1467 /* disable any enabled and non active backends */
1468 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1470 struct snd_soc_pcm_runtime *be = dpcm->be;
1471 struct snd_pcm_substream *be_substream =
1472 snd_soc_dpcm_get_substream(be, stream);
1474 if (be->dpcm[stream].users == 0)
1475 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1476 stream ? "capture" : "playback",
1477 be->dpcm[stream].state);
1479 if (--be->dpcm[stream].users != 0)
1482 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1485 soc_pcm_close(be_substream);
1486 be_substream->runtime = NULL;
1487 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1491 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1493 struct snd_soc_dpcm *dpcm;
1496 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1497 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1499 struct snd_soc_pcm_runtime *be = dpcm->be;
1500 struct snd_pcm_substream *be_substream =
1501 snd_soc_dpcm_get_substream(be, stream);
1503 if (!be_substream) {
1504 dev_err(be->dev, "ASoC: no backend %s stream\n",
1505 stream ? "capture" : "playback");
1509 /* is this op for this BE ? */
1510 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1513 /* first time the dpcm is open ? */
1514 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1515 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1516 stream ? "capture" : "playback",
1517 be->dpcm[stream].state);
1519 if (be->dpcm[stream].users++ != 0)
1522 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1523 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1526 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1527 stream ? "capture" : "playback", be->dai_link->name);
1529 be_substream->runtime = be->dpcm[stream].runtime;
1530 err = soc_pcm_open(be_substream);
1532 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1533 be->dpcm[stream].users--;
1534 if (be->dpcm[stream].users < 0)
1535 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1536 stream ? "capture" : "playback",
1537 be->dpcm[stream].state);
1539 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1543 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1550 /* disable any enabled and non active backends */
1551 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1552 struct snd_soc_pcm_runtime *be = dpcm->be;
1553 struct snd_pcm_substream *be_substream =
1554 snd_soc_dpcm_get_substream(be, stream);
1556 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1559 if (be->dpcm[stream].users == 0)
1560 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1561 stream ? "capture" : "playback",
1562 be->dpcm[stream].state);
1564 if (--be->dpcm[stream].users != 0)
1567 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1570 soc_pcm_close(be_substream);
1571 be_substream->runtime = NULL;
1572 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1578 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1579 struct snd_soc_pcm_stream *stream,
1582 runtime->hw.rate_min = stream->rate_min;
1583 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1584 runtime->hw.channels_min = stream->channels_min;
1585 runtime->hw.channels_max = stream->channels_max;
1586 if (runtime->hw.formats)
1587 runtime->hw.formats &= formats & stream->formats;
1589 runtime->hw.formats = formats & stream->formats;
1590 runtime->hw.rates = stream->rates;
1593 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1595 struct snd_soc_pcm_runtime *fe = substream->private_data;
1596 struct snd_soc_dpcm *dpcm;
1597 u64 formats = ULLONG_MAX;
1598 int stream = substream->stream;
1600 if (!fe->dai_link->dpcm_merged_format)
1604 * It returns merged BE codec format
1605 * if FE want to use it (= dpcm_merged_format)
1608 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1609 struct snd_soc_pcm_runtime *be = dpcm->be;
1610 struct snd_soc_dai_driver *codec_dai_drv;
1611 struct snd_soc_pcm_stream *codec_stream;
1614 for (i = 0; i < be->num_codecs; i++) {
1616 * Skip CODECs which don't support the current stream
1617 * type. See soc_pcm_init_runtime_hw() for more details
1619 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1623 codec_dai_drv = be->codec_dais[i]->driver;
1624 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1625 codec_stream = &codec_dai_drv->playback;
1627 codec_stream = &codec_dai_drv->capture;
1629 formats &= codec_stream->formats;
1636 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1638 struct snd_pcm_runtime *runtime = substream->runtime;
1639 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1640 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1641 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1642 u64 format = dpcm_runtime_base_format(substream);
1644 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1645 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1647 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1650 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1652 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1653 * for avoiding the race with trigger callback.
1654 * If the state is unset and a trigger is pending while the previous operation,
1655 * process the pending trigger action here.
1657 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1658 int stream, enum snd_soc_dpcm_update state)
1660 struct snd_pcm_substream *substream =
1661 snd_soc_dpcm_get_substream(fe, stream);
1663 snd_pcm_stream_lock_irq(substream);
1664 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1665 dpcm_fe_dai_do_trigger(substream,
1666 fe->dpcm[stream].trigger_pending - 1);
1667 fe->dpcm[stream].trigger_pending = 0;
1669 fe->dpcm[stream].runtime_update = state;
1670 snd_pcm_stream_unlock_irq(substream);
1673 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1676 struct snd_soc_dpcm *dpcm;
1677 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1678 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1681 /* apply symmetry for FE */
1682 if (soc_pcm_has_symmetry(fe_substream))
1683 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1685 /* Symmetry only applies if we've got an active stream. */
1686 if (fe_cpu_dai->active) {
1687 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1692 /* apply symmetry for BE */
1693 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1694 struct snd_soc_pcm_runtime *be = dpcm->be;
1695 struct snd_pcm_substream *be_substream =
1696 snd_soc_dpcm_get_substream(be, stream);
1697 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1700 if (rtd->dai_link->be_hw_params_fixup)
1703 if (soc_pcm_has_symmetry(be_substream))
1704 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1706 /* Symmetry only applies if we've got an active stream. */
1707 if (rtd->cpu_dai->active) {
1708 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1713 for (i = 0; i < rtd->num_codecs; i++) {
1714 if (rtd->codec_dais[i]->active) {
1715 err = soc_pcm_apply_symmetry(be_substream,
1716 rtd->codec_dais[i]);
1726 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1728 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1729 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1730 int stream = fe_substream->stream, ret = 0;
1732 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1734 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1736 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1740 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1742 /* start the DAI frontend */
1743 ret = soc_pcm_open(fe_substream);
1745 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1749 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1751 dpcm_set_fe_runtime(fe_substream);
1752 snd_pcm_limit_hw_rates(runtime);
1754 ret = dpcm_apply_symmetry(fe_substream, stream);
1756 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1761 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1765 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1767 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1771 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1773 struct snd_soc_dpcm *dpcm;
1775 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1776 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1778 struct snd_soc_pcm_runtime *be = dpcm->be;
1779 struct snd_pcm_substream *be_substream =
1780 snd_soc_dpcm_get_substream(be, stream);
1782 /* is this op for this BE ? */
1783 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1786 if (be->dpcm[stream].users == 0)
1787 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1788 stream ? "capture" : "playback",
1789 be->dpcm[stream].state);
1791 if (--be->dpcm[stream].users != 0)
1794 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1795 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1796 soc_pcm_hw_free(be_substream);
1797 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1800 dev_dbg(be->dev, "ASoC: close BE %s\n",
1801 be->dai_link->name);
1803 soc_pcm_close(be_substream);
1804 be_substream->runtime = NULL;
1806 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1811 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1813 struct snd_soc_pcm_runtime *fe = substream->private_data;
1814 int stream = substream->stream;
1816 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1818 /* shutdown the BEs */
1819 dpcm_be_dai_shutdown(fe, substream->stream);
1821 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1823 /* now shutdown the frontend */
1824 soc_pcm_close(substream);
1826 /* run the stream event for each BE */
1827 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1829 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1830 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1834 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1836 struct snd_soc_dpcm *dpcm;
1838 /* only hw_params backends that are either sinks or sources
1839 * to this frontend DAI */
1840 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1842 struct snd_soc_pcm_runtime *be = dpcm->be;
1843 struct snd_pcm_substream *be_substream =
1844 snd_soc_dpcm_get_substream(be, stream);
1846 /* is this op for this BE ? */
1847 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1850 /* only free hw when no longer used - check all FEs */
1851 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1854 /* do not free hw if this BE is used by other FE */
1855 if (be->dpcm[stream].users > 1)
1858 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1859 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1860 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1861 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1862 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1863 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1866 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1867 be->dai_link->name);
1869 soc_pcm_hw_free(be_substream);
1871 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1877 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1879 struct snd_soc_pcm_runtime *fe = substream->private_data;
1880 int err, stream = substream->stream;
1882 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1883 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1885 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1887 /* call hw_free on the frontend */
1888 err = soc_pcm_hw_free(substream);
1890 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1891 fe->dai_link->name);
1893 /* only hw_params backends that are either sinks or sources
1894 * to this frontend DAI */
1895 err = dpcm_be_dai_hw_free(fe, stream);
1897 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1898 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1900 mutex_unlock(&fe->card->mutex);
1904 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1906 struct snd_soc_dpcm *dpcm;
1909 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1911 struct snd_soc_pcm_runtime *be = dpcm->be;
1912 struct snd_pcm_substream *be_substream =
1913 snd_soc_dpcm_get_substream(be, stream);
1915 /* is this op for this BE ? */
1916 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1919 /* copy params for each dpcm */
1920 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1921 sizeof(struct snd_pcm_hw_params));
1923 /* perform any hw_params fixups */
1924 if (be->dai_link->be_hw_params_fixup) {
1925 ret = be->dai_link->be_hw_params_fixup(be,
1929 "ASoC: hw_params BE fixup failed %d\n",
1935 /* only allow hw_params() if no connected FEs are running */
1936 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1939 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1940 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1941 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1944 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1945 be->dai_link->name);
1947 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1949 dev_err(dpcm->be->dev,
1950 "ASoC: hw_params BE failed %d\n", ret);
1954 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1959 /* disable any enabled and non active backends */
1960 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1961 struct snd_soc_pcm_runtime *be = dpcm->be;
1962 struct snd_pcm_substream *be_substream =
1963 snd_soc_dpcm_get_substream(be, stream);
1965 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1968 /* only allow hw_free() if no connected FEs are running */
1969 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1972 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1973 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1974 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1975 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1978 soc_pcm_hw_free(be_substream);
1984 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1985 struct snd_pcm_hw_params *params)
1987 struct snd_soc_pcm_runtime *fe = substream->private_data;
1988 int ret, stream = substream->stream;
1990 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1991 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1993 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1994 sizeof(struct snd_pcm_hw_params));
1995 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1997 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2001 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2002 fe->dai_link->name, params_rate(params),
2003 params_channels(params), params_format(params));
2005 /* call hw_params on the frontend */
2006 ret = soc_pcm_hw_params(substream, params);
2008 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2009 dpcm_be_dai_hw_free(fe, stream);
2011 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2014 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2015 mutex_unlock(&fe->card->mutex);
2019 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2020 struct snd_pcm_substream *substream, int cmd)
2024 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2025 dpcm->be->dai_link->name, cmd);
2027 ret = soc_pcm_trigger(substream, cmd);
2029 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2034 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2037 struct snd_soc_dpcm *dpcm;
2040 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2042 struct snd_soc_pcm_runtime *be = dpcm->be;
2043 struct snd_pcm_substream *be_substream =
2044 snd_soc_dpcm_get_substream(be, stream);
2046 /* is this op for this BE ? */
2047 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2051 case SNDRV_PCM_TRIGGER_START:
2052 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2053 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2054 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2057 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2061 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2063 case SNDRV_PCM_TRIGGER_RESUME:
2064 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2067 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2071 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2073 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2074 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2077 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2081 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2083 case SNDRV_PCM_TRIGGER_STOP:
2084 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2085 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2088 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2091 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2095 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2097 case SNDRV_PCM_TRIGGER_SUSPEND:
2098 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2101 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2104 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2108 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2110 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2111 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2114 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2117 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2121 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2128 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2130 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2131 int cmd, bool fe_first)
2133 struct snd_soc_pcm_runtime *fe = substream->private_data;
2136 /* call trigger on the frontend before the backend. */
2138 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2139 fe->dai_link->name, cmd);
2141 ret = soc_pcm_trigger(substream, cmd);
2145 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2149 /* call trigger on the frontend after the backend. */
2150 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2154 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2155 fe->dai_link->name, cmd);
2157 ret = soc_pcm_trigger(substream, cmd);
2162 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2164 struct snd_soc_pcm_runtime *fe = substream->private_data;
2165 int stream = substream->stream;
2167 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2169 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2172 case SND_SOC_DPCM_TRIGGER_PRE:
2174 case SNDRV_PCM_TRIGGER_START:
2175 case SNDRV_PCM_TRIGGER_RESUME:
2176 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2177 case SNDRV_PCM_TRIGGER_DRAIN:
2178 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2180 case SNDRV_PCM_TRIGGER_STOP:
2181 case SNDRV_PCM_TRIGGER_SUSPEND:
2182 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2183 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2190 case SND_SOC_DPCM_TRIGGER_POST:
2192 case SNDRV_PCM_TRIGGER_START:
2193 case SNDRV_PCM_TRIGGER_RESUME:
2194 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2195 case SNDRV_PCM_TRIGGER_DRAIN:
2196 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2198 case SNDRV_PCM_TRIGGER_STOP:
2199 case SNDRV_PCM_TRIGGER_SUSPEND:
2200 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2201 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2208 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2209 /* bespoke trigger() - handles both FE and BEs */
2211 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2212 fe->dai_link->name, cmd);
2214 ret = soc_pcm_bespoke_trigger(substream, cmd);
2217 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2218 fe->dai_link->name);
2224 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2230 case SNDRV_PCM_TRIGGER_START:
2231 case SNDRV_PCM_TRIGGER_RESUME:
2232 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2233 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2235 case SNDRV_PCM_TRIGGER_STOP:
2236 case SNDRV_PCM_TRIGGER_SUSPEND:
2237 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2239 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2240 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2245 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2249 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2251 struct snd_soc_pcm_runtime *fe = substream->private_data;
2252 int stream = substream->stream;
2254 /* if FE's runtime_update is already set, we're in race;
2255 * process this trigger later at exit
2257 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2258 fe->dpcm[stream].trigger_pending = cmd + 1;
2259 return 0; /* delayed, assuming it's successful */
2262 /* we're alone, let's trigger */
2263 return dpcm_fe_dai_do_trigger(substream, cmd);
2266 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2268 struct snd_soc_dpcm *dpcm;
2271 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2273 struct snd_soc_pcm_runtime *be = dpcm->be;
2274 struct snd_pcm_substream *be_substream =
2275 snd_soc_dpcm_get_substream(be, stream);
2277 /* is this op for this BE ? */
2278 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2281 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2282 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2283 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2284 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2287 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2288 be->dai_link->name);
2290 ret = soc_pcm_prepare(be_substream);
2292 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2297 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2302 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2304 struct snd_soc_pcm_runtime *fe = substream->private_data;
2305 int stream = substream->stream, ret = 0;
2307 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2309 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2311 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2313 /* there is no point preparing this FE if there are no BEs */
2314 if (list_empty(&fe->dpcm[stream].be_clients)) {
2315 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2316 fe->dai_link->name);
2321 ret = dpcm_be_dai_prepare(fe, substream->stream);
2325 /* call prepare on the frontend */
2326 ret = soc_pcm_prepare(substream);
2328 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2329 fe->dai_link->name);
2333 /* run the stream event for each BE */
2334 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2335 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2338 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2339 mutex_unlock(&fe->card->mutex);
2344 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2345 unsigned int cmd, void *arg)
2347 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2348 struct snd_soc_platform *platform = rtd->platform;
2350 if (platform->driver->ops && platform->driver->ops->ioctl)
2351 return platform->driver->ops->ioctl(substream, cmd, arg);
2352 return snd_pcm_lib_ioctl(substream, cmd, arg);
2355 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2357 struct snd_pcm_substream *substream =
2358 snd_soc_dpcm_get_substream(fe, stream);
2359 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2362 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2363 stream ? "capture" : "playback", fe->dai_link->name);
2365 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2366 /* call bespoke trigger - FE takes care of all BE triggers */
2367 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2368 fe->dai_link->name);
2370 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2372 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2374 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2375 fe->dai_link->name);
2377 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2379 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2382 err = dpcm_be_dai_hw_free(fe, stream);
2384 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2386 err = dpcm_be_dai_shutdown(fe, stream);
2388 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2390 /* run the stream event for each BE */
2391 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2396 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2398 struct snd_pcm_substream *substream =
2399 snd_soc_dpcm_get_substream(fe, stream);
2400 struct snd_soc_dpcm *dpcm;
2401 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2404 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2405 stream ? "capture" : "playback", fe->dai_link->name);
2407 /* Only start the BE if the FE is ready */
2408 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2409 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2412 /* startup must always be called for new BEs */
2413 ret = dpcm_be_dai_startup(fe, stream);
2417 /* keep going if FE state is > open */
2418 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2421 ret = dpcm_be_dai_hw_params(fe, stream);
2425 /* keep going if FE state is > hw_params */
2426 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2430 ret = dpcm_be_dai_prepare(fe, stream);
2434 /* run the stream event for each BE */
2435 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2437 /* keep going if FE state is > prepare */
2438 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2439 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2442 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2443 /* call trigger on the frontend - FE takes care of all BE triggers */
2444 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2445 fe->dai_link->name);
2447 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2449 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2453 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2454 fe->dai_link->name);
2456 ret = dpcm_be_dai_trigger(fe, stream,
2457 SNDRV_PCM_TRIGGER_START);
2459 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2467 dpcm_be_dai_hw_free(fe, stream);
2469 dpcm_be_dai_shutdown(fe, stream);
2471 /* disconnect any non started BEs */
2472 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2473 struct snd_soc_pcm_runtime *be = dpcm->be;
2474 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2475 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2481 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2485 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2486 ret = dpcm_run_update_startup(fe, stream);
2488 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2489 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2494 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2498 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2499 ret = dpcm_run_update_shutdown(fe, stream);
2501 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2502 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2507 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2510 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2512 struct snd_soc_pcm_runtime *fe;
2513 int old, new, paths;
2515 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2516 list_for_each_entry(fe, &card->rtd_list, list) {
2517 struct snd_soc_dapm_widget_list *list;
2519 /* make sure link is FE */
2520 if (!fe->dai_link->dynamic)
2523 /* only check active links */
2524 if (!fe->cpu_dai->active)
2527 /* DAPM sync will call this to update DSP paths */
2528 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2529 fe->dai_link->name);
2531 /* skip if FE doesn't have playback capability */
2532 if (!fe->cpu_dai->driver->playback.channels_min
2533 || !fe->codec_dai->driver->playback.channels_min)
2536 /* skip if FE isn't currently playing */
2537 if (!fe->cpu_dai->playback_active
2538 || !fe->codec_dai->playback_active)
2541 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2543 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2544 fe->dai_link->name, "playback");
2545 mutex_unlock(&card->mutex);
2549 /* update any new playback paths */
2550 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2552 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2553 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2554 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2557 /* update any old playback paths */
2558 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2560 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2561 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2562 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2565 dpcm_path_put(&list);
2567 /* skip if FE doesn't have capture capability */
2568 if (!fe->cpu_dai->driver->capture.channels_min
2569 || !fe->codec_dai->driver->capture.channels_min)
2572 /* skip if FE isn't currently capturing */
2573 if (!fe->cpu_dai->capture_active
2574 || !fe->codec_dai->capture_active)
2577 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2579 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2580 fe->dai_link->name, "capture");
2581 mutex_unlock(&card->mutex);
2585 /* update any new capture paths */
2586 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2588 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2589 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2590 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2593 /* update any old capture paths */
2594 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2596 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2597 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2598 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2601 dpcm_path_put(&list);
2604 mutex_unlock(&card->mutex);
2607 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2609 struct snd_soc_dpcm *dpcm;
2610 struct list_head *clients =
2611 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2613 list_for_each_entry(dpcm, clients, list_be) {
2615 struct snd_soc_pcm_runtime *be = dpcm->be;
2618 if (be->dai_link->ignore_suspend)
2621 for (i = 0; i < be->num_codecs; i++) {
2622 struct snd_soc_dai *dai = be->codec_dais[i];
2623 struct snd_soc_dai_driver *drv = dai->driver;
2625 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2626 be->dai_link->name);
2628 if (drv->ops && drv->ops->digital_mute &&
2629 dai->playback_active)
2630 drv->ops->digital_mute(dai, mute);
2637 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2639 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2640 struct snd_soc_dpcm *dpcm;
2641 struct snd_soc_dapm_widget_list *list;
2643 int stream = fe_substream->stream;
2645 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2646 fe->dpcm[stream].runtime = fe_substream->runtime;
2648 ret = dpcm_path_get(fe, stream, &list);
2650 mutex_unlock(&fe->card->mutex);
2652 } else if (ret == 0) {
2653 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2654 fe->dai_link->name, stream ? "capture" : "playback");
2657 /* calculate valid and active FE <-> BE dpcms */
2658 dpcm_process_paths(fe, stream, &list, 1);
2660 ret = dpcm_fe_dai_startup(fe_substream);
2662 /* clean up all links */
2663 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2664 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2666 dpcm_be_disconnect(fe, stream);
2667 fe->dpcm[stream].runtime = NULL;
2670 dpcm_clear_pending_state(fe, stream);
2671 dpcm_path_put(&list);
2672 mutex_unlock(&fe->card->mutex);
2676 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2678 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2679 struct snd_soc_dpcm *dpcm;
2680 int stream = fe_substream->stream, ret;
2682 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2683 ret = dpcm_fe_dai_shutdown(fe_substream);
2685 /* mark FE's links ready to prune */
2686 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2687 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2689 dpcm_be_disconnect(fe, stream);
2691 fe->dpcm[stream].runtime = NULL;
2692 mutex_unlock(&fe->card->mutex);
2696 /* create a new pcm */
2697 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2699 struct snd_soc_platform *platform = rtd->platform;
2700 struct snd_soc_dai *codec_dai;
2701 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2702 struct snd_pcm *pcm;
2704 int ret = 0, playback = 0, capture = 0;
2707 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2708 playback = rtd->dai_link->dpcm_playback;
2709 capture = rtd->dai_link->dpcm_capture;
2711 for (i = 0; i < rtd->num_codecs; i++) {
2712 codec_dai = rtd->codec_dais[i];
2713 if (codec_dai->driver->playback.channels_min)
2715 if (codec_dai->driver->capture.channels_min)
2719 capture = capture && cpu_dai->driver->capture.channels_min;
2720 playback = playback && cpu_dai->driver->playback.channels_min;
2723 if (rtd->dai_link->playback_only) {
2728 if (rtd->dai_link->capture_only) {
2733 /* create the PCM */
2734 if (rtd->dai_link->no_pcm) {
2735 snprintf(new_name, sizeof(new_name), "(%s)",
2736 rtd->dai_link->stream_name);
2738 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2739 playback, capture, &pcm);
2741 if (rtd->dai_link->dynamic)
2742 snprintf(new_name, sizeof(new_name), "%s (*)",
2743 rtd->dai_link->stream_name);
2745 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2746 rtd->dai_link->stream_name,
2747 (rtd->num_codecs > 1) ?
2748 "multicodec" : rtd->codec_dai->name, num);
2750 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2754 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2755 rtd->dai_link->name);
2758 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2760 /* DAPM dai link stream work */
2761 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2763 pcm->nonatomic = rtd->dai_link->nonatomic;
2765 pcm->private_data = rtd;
2767 if (rtd->dai_link->no_pcm) {
2769 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2771 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2775 /* ASoC PCM operations */
2776 if (rtd->dai_link->dynamic) {
2777 rtd->ops.open = dpcm_fe_dai_open;
2778 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2779 rtd->ops.prepare = dpcm_fe_dai_prepare;
2780 rtd->ops.trigger = dpcm_fe_dai_trigger;
2781 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2782 rtd->ops.close = dpcm_fe_dai_close;
2783 rtd->ops.pointer = soc_pcm_pointer;
2784 rtd->ops.ioctl = soc_pcm_ioctl;
2786 rtd->ops.open = soc_pcm_open;
2787 rtd->ops.hw_params = soc_pcm_hw_params;
2788 rtd->ops.prepare = soc_pcm_prepare;
2789 rtd->ops.trigger = soc_pcm_trigger;
2790 rtd->ops.hw_free = soc_pcm_hw_free;
2791 rtd->ops.close = soc_pcm_close;
2792 rtd->ops.pointer = soc_pcm_pointer;
2793 rtd->ops.ioctl = soc_pcm_ioctl;
2796 if (platform->driver->ops) {
2797 rtd->ops.ack = platform->driver->ops->ack;
2798 rtd->ops.copy_user = platform->driver->ops->copy_user;
2799 rtd->ops.copy_kernel = platform->driver->ops->copy_kernel;
2800 rtd->ops.fill_silence = platform->driver->ops->fill_silence;
2801 rtd->ops.page = platform->driver->ops->page;
2802 rtd->ops.mmap = platform->driver->ops->mmap;
2806 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2809 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2811 if (platform->driver->pcm_new) {
2812 ret = platform->driver->pcm_new(rtd);
2814 dev_err(platform->dev,
2815 "ASoC: pcm constructor failed: %d\n",
2821 pcm->private_free = platform->driver->pcm_free;
2823 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2824 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2829 /* is the current PCM operation for this FE ? */
2830 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2832 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2836 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2838 /* is the current PCM operation for this BE ? */
2839 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2840 struct snd_soc_pcm_runtime *be, int stream)
2842 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2843 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2844 be->dpcm[stream].runtime_update))
2848 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2850 /* get the substream for this BE */
2851 struct snd_pcm_substream *
2852 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2854 return be->pcm->streams[stream].substream;
2856 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2858 /* get the BE runtime state */
2859 enum snd_soc_dpcm_state
2860 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2862 return be->dpcm[stream].state;
2864 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2866 /* set the BE runtime state */
2867 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2868 int stream, enum snd_soc_dpcm_state state)
2870 be->dpcm[stream].state = state;
2872 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2875 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2876 * are not running, paused or suspended for the specified stream direction.
2878 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2879 struct snd_soc_pcm_runtime *be, int stream)
2881 struct snd_soc_dpcm *dpcm;
2884 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2889 state = dpcm->fe->dpcm[stream].state;
2890 if (state == SND_SOC_DPCM_STATE_START ||
2891 state == SND_SOC_DPCM_STATE_PAUSED ||
2892 state == SND_SOC_DPCM_STATE_SUSPEND)
2896 /* it's safe to free/stop this BE DAI */
2899 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2902 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2903 * running, paused or suspended for the specified stream direction.
2905 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2906 struct snd_soc_pcm_runtime *be, int stream)
2908 struct snd_soc_dpcm *dpcm;
2911 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2916 state = dpcm->fe->dpcm[stream].state;
2917 if (state == SND_SOC_DPCM_STATE_START ||
2918 state == SND_SOC_DPCM_STATE_PAUSED ||
2919 state == SND_SOC_DPCM_STATE_SUSPEND ||
2920 state == SND_SOC_DPCM_STATE_PREPARE)
2924 /* it's safe to change hw_params */
2927 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2929 #ifdef CONFIG_DEBUG_FS
2930 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
2933 case SND_SOC_DPCM_STATE_NEW:
2935 case SND_SOC_DPCM_STATE_OPEN:
2937 case SND_SOC_DPCM_STATE_HW_PARAMS:
2939 case SND_SOC_DPCM_STATE_PREPARE:
2941 case SND_SOC_DPCM_STATE_START:
2943 case SND_SOC_DPCM_STATE_STOP:
2945 case SND_SOC_DPCM_STATE_SUSPEND:
2947 case SND_SOC_DPCM_STATE_PAUSED:
2949 case SND_SOC_DPCM_STATE_HW_FREE:
2951 case SND_SOC_DPCM_STATE_CLOSE:
2958 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2959 int stream, char *buf, size_t size)
2961 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2962 struct snd_soc_dpcm *dpcm;
2966 offset += scnprintf(buf + offset, size - offset,
2967 "[%s - %s]\n", fe->dai_link->name,
2968 stream ? "Capture" : "Playback");
2970 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
2971 dpcm_state_string(fe->dpcm[stream].state));
2973 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2974 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2975 offset += scnprintf(buf + offset, size - offset,
2977 "Format = %s, Channels = %d, Rate = %d\n",
2978 snd_pcm_format_name(params_format(params)),
2979 params_channels(params),
2980 params_rate(params));
2983 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
2985 if (list_empty(&fe->dpcm[stream].be_clients)) {
2986 offset += scnprintf(buf + offset, size - offset,
2987 " No active DSP links\n");
2991 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2992 struct snd_soc_pcm_runtime *be = dpcm->be;
2993 params = &dpcm->hw_params;
2995 offset += scnprintf(buf + offset, size - offset,
2996 "- %s\n", be->dai_link->name);
2998 offset += scnprintf(buf + offset, size - offset,
3000 dpcm_state_string(be->dpcm[stream].state));
3002 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3003 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3004 offset += scnprintf(buf + offset, size - offset,
3005 " Hardware Params: "
3006 "Format = %s, Channels = %d, Rate = %d\n",
3007 snd_pcm_format_name(params_format(params)),
3008 params_channels(params),
3009 params_rate(params));
3016 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3017 size_t count, loff_t *ppos)
3019 struct snd_soc_pcm_runtime *fe = file->private_data;
3020 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3023 buf = kmalloc(out_count, GFP_KERNEL);
3027 if (fe->cpu_dai->driver->playback.channels_min)
3028 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3029 buf + offset, out_count - offset);
3031 if (fe->cpu_dai->driver->capture.channels_min)
3032 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3033 buf + offset, out_count - offset);
3035 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3041 static const struct file_operations dpcm_state_fops = {
3042 .open = simple_open,
3043 .read = dpcm_state_read_file,
3044 .llseek = default_llseek,
3047 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3052 if (!rtd->card->debugfs_card_root)
3055 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3056 rtd->card->debugfs_card_root);
3057 if (!rtd->debugfs_dpcm_root) {
3059 "ASoC: Failed to create dpcm debugfs directory %s\n",
3060 rtd->dai_link->name);
3064 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3065 rtd, &dpcm_state_fops);