1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-pcm.c -- ALSA SoC PCM
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Authors: Liam Girdwood <lrg@ti.com>
11 // Mark Brown <broonie@opensource.wolfsonmicro.com>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dpcm.h>
27 #include <sound/soc-link.h>
28 #include <sound/initval.h>
30 static inline void snd_soc_dpcm_mutex_lock(struct snd_soc_pcm_runtime *rtd)
32 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
35 static inline void snd_soc_dpcm_mutex_unlock(struct snd_soc_pcm_runtime *rtd)
37 mutex_unlock(&rtd->card->pcm_mutex);
40 #define snd_soc_dpcm_mutex_assert_held(rtd) \
41 lockdep_assert_held(&(rtd)->card->pcm_mutex)
43 static inline void snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime *rtd,
46 snd_pcm_stream_lock_irq(snd_soc_dpcm_get_substream(rtd, stream));
49 #define snd_soc_dpcm_stream_lock_irqsave_nested(rtd, stream, flags) \
50 snd_pcm_stream_lock_irqsave_nested(snd_soc_dpcm_get_substream(rtd, stream), flags)
52 static inline void snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime *rtd,
55 snd_pcm_stream_unlock_irq(snd_soc_dpcm_get_substream(rtd, stream));
58 #define snd_soc_dpcm_stream_unlock_irqrestore(rtd, stream, flags) \
59 snd_pcm_stream_unlock_irqrestore(snd_soc_dpcm_get_substream(rtd, stream), flags)
61 #define DPCM_MAX_BE_USERS 8
63 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
65 return (rtd)->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
67 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
69 return (rtd)->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
72 #ifdef CONFIG_DEBUG_FS
73 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
76 case SND_SOC_DPCM_STATE_NEW:
78 case SND_SOC_DPCM_STATE_OPEN:
80 case SND_SOC_DPCM_STATE_HW_PARAMS:
82 case SND_SOC_DPCM_STATE_PREPARE:
84 case SND_SOC_DPCM_STATE_START:
86 case SND_SOC_DPCM_STATE_STOP:
88 case SND_SOC_DPCM_STATE_SUSPEND:
90 case SND_SOC_DPCM_STATE_PAUSED:
92 case SND_SOC_DPCM_STATE_HW_FREE:
94 case SND_SOC_DPCM_STATE_CLOSE:
101 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
102 int stream, char *buf, size_t size)
104 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
105 struct snd_soc_dpcm *dpcm;
109 offset += scnprintf(buf + offset, size - offset,
110 "[%s - %s]\n", fe->dai_link->name,
111 stream ? "Capture" : "Playback");
113 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
114 dpcm_state_string(fe->dpcm[stream].state));
116 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
117 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
118 offset += scnprintf(buf + offset, size - offset,
120 "Format = %s, Channels = %d, Rate = %d\n",
121 snd_pcm_format_name(params_format(params)),
122 params_channels(params),
123 params_rate(params));
126 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
128 if (list_empty(&fe->dpcm[stream].be_clients)) {
129 offset += scnprintf(buf + offset, size - offset,
130 " No active DSP links\n");
134 for_each_dpcm_be(fe, stream, dpcm) {
135 struct snd_soc_pcm_runtime *be = dpcm->be;
136 params = &dpcm->hw_params;
138 offset += scnprintf(buf + offset, size - offset,
139 "- %s\n", be->dai_link->name);
141 offset += scnprintf(buf + offset, size - offset,
143 dpcm_state_string(be->dpcm[stream].state));
145 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
146 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
147 offset += scnprintf(buf + offset, size - offset,
149 "Format = %s, Channels = %d, Rate = %d\n",
150 snd_pcm_format_name(params_format(params)),
151 params_channels(params),
152 params_rate(params));
158 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
159 size_t count, loff_t *ppos)
161 struct snd_soc_pcm_runtime *fe = file->private_data;
162 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
166 if (fe->num_cpus > 1) {
168 "%s doesn't support Multi CPU yet\n", __func__);
172 buf = kmalloc(out_count, GFP_KERNEL);
176 snd_soc_dpcm_mutex_lock(fe);
177 for_each_pcm_streams(stream)
178 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
179 offset += dpcm_show_state(fe, stream,
182 snd_soc_dpcm_mutex_unlock(fe);
184 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
190 static const struct file_operations dpcm_state_fops = {
192 .read = dpcm_state_read_file,
193 .llseek = default_llseek,
196 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
198 if (!rtd->dai_link->dynamic)
201 if (!rtd->card->debugfs_card_root)
204 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
205 rtd->card->debugfs_card_root);
207 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
208 rtd, &dpcm_state_fops);
211 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
215 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
216 stream ? "capture" : "playback");
218 dpcm->debugfs_state = debugfs_create_dir(
219 name, dpcm->fe->debugfs_dpcm_root);
220 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
226 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
228 debugfs_remove_recursive(dpcm->debugfs_state);
232 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
237 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
242 /* Set FE's runtime_update state; the state is protected via PCM stream lock
243 * for avoiding the race with trigger callback.
244 * If the state is unset and a trigger is pending while the previous operation,
245 * process the pending trigger action here.
247 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
248 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
249 int stream, enum snd_soc_dpcm_update state)
251 struct snd_pcm_substream *substream =
252 snd_soc_dpcm_get_substream(fe, stream);
254 snd_soc_dpcm_stream_lock_irq(fe, stream);
255 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
256 dpcm_fe_dai_do_trigger(substream,
257 fe->dpcm[stream].trigger_pending - 1);
258 fe->dpcm[stream].trigger_pending = 0;
260 fe->dpcm[stream].runtime_update = state;
261 snd_soc_dpcm_stream_unlock_irq(fe, stream);
264 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
265 int stream, enum snd_soc_dpcm_update state)
267 be->dpcm[stream].runtime_update = state;
271 * snd_soc_runtime_action() - Increment/Decrement active count for
272 * PCM runtime components
273 * @rtd: ASoC PCM runtime that is activated
274 * @stream: Direction of the PCM stream
275 * @action: Activate stream if 1. Deactivate if -1.
277 * Increments/Decrements the active count for all the DAIs and components
278 * attached to a PCM runtime.
279 * Should typically be called when a stream is opened.
281 * Must be called with the rtd->card->pcm_mutex being held
283 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
284 int stream, int action)
286 struct snd_soc_dai *dai;
289 snd_soc_dpcm_mutex_assert_held(rtd);
291 for_each_rtd_dais(rtd, i, dai)
292 snd_soc_dai_action(dai, stream, action);
294 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
297 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
298 * @rtd: The ASoC PCM runtime that should be checked.
300 * This function checks whether the power down delay should be ignored for a
301 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
302 * been configured to ignore the delay, or if none of the components benefits
303 * from having the delay.
305 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
307 struct snd_soc_component *component;
311 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
314 for_each_rtd_components(rtd, i, component)
315 ignore &= !component->driver->use_pmdown_time;
321 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
322 * @substream: the pcm substream
323 * @hw: the hardware parameters
325 * Sets the substream runtime hardware parameters.
327 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
328 const struct snd_pcm_hardware *hw)
330 substream->runtime->hw = *hw;
334 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
336 /* DPCM stream event, send event to FE and all active BEs. */
337 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
340 struct snd_soc_dpcm *dpcm;
342 snd_soc_dpcm_mutex_assert_held(fe);
344 for_each_dpcm_be(fe, dir, dpcm) {
346 struct snd_soc_pcm_runtime *be = dpcm->be;
348 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
349 be->dai_link->name, event, dir);
351 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
352 (be->dpcm[dir].users >= 1))
355 snd_soc_dapm_stream_event(be, dir, event);
358 snd_soc_dapm_stream_event(fe, dir, event);
363 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
364 struct snd_pcm_hw_params *params)
367 dai->rate = params_rate(params);
368 dai->channels = params_channels(params);
369 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
373 dai->sample_bits = 0;
377 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
378 struct snd_soc_dai *soc_dai)
380 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
383 if (!snd_soc_dai_active(soc_dai))
386 #define __soc_pcm_apply_symmetry(name, NAME) \
387 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
388 rtd->dai_link->symmetric_##name)) { \
389 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
390 #name, soc_dai->name); \
392 ret = snd_pcm_hw_constraint_single(substream->runtime, \
393 SNDRV_PCM_HW_PARAM_##NAME,\
396 dev_err(soc_dai->dev, \
397 "ASoC: Unable to apply %s constraint: %d\n",\
403 __soc_pcm_apply_symmetry(rate, RATE);
404 __soc_pcm_apply_symmetry(channels, CHANNELS);
405 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
410 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
411 struct snd_pcm_hw_params *params)
413 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
414 struct snd_soc_dai d;
415 struct snd_soc_dai *dai;
416 struct snd_soc_dai *cpu_dai;
417 unsigned int symmetry, i;
420 soc_pcm_set_dai_params(&d, params);
422 #define __soc_pcm_params_symmetry(xxx) \
423 symmetry = rtd->dai_link->symmetric_##xxx; \
424 for_each_rtd_dais(rtd, i, dai) \
425 symmetry |= dai->driver->symmetric_##xxx; \
428 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
429 if (!snd_soc_dai_is_dummy(cpu_dai) && \
430 cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
431 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
432 #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
436 /* reject unmatched parameters when applying symmetry */
437 __soc_pcm_params_symmetry(rate);
438 __soc_pcm_params_symmetry(channels);
439 __soc_pcm_params_symmetry(sample_bits);
444 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
446 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
447 struct snd_soc_dai_link *link = rtd->dai_link;
448 struct snd_soc_dai *dai;
449 unsigned int symmetry, i;
451 symmetry = link->symmetric_rate ||
452 link->symmetric_channels ||
453 link->symmetric_sample_bits;
455 for_each_rtd_dais(rtd, i, dai)
456 symmetry = symmetry ||
457 dai->driver->symmetric_rate ||
458 dai->driver->symmetric_channels ||
459 dai->driver->symmetric_sample_bits;
462 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
465 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
467 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
473 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
475 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
479 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
481 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
482 struct snd_soc_dai *cpu_dai;
483 struct snd_soc_dai *codec_dai;
484 int stream = substream->stream;
486 unsigned int bits = 0, cpu_bits = 0;
488 for_each_rtd_codec_dais(rtd, i, codec_dai) {
489 struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
491 if (pcm_codec->sig_bits == 0) {
495 bits = max(pcm_codec->sig_bits, bits);
498 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
499 struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
501 if (pcm_cpu->sig_bits == 0) {
505 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
508 soc_pcm_set_msb(substream, bits);
509 soc_pcm_set_msb(substream, cpu_bits);
512 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
514 hw->rates = UINT_MAX;
516 hw->rate_max = UINT_MAX;
517 hw->channels_min = 0;
518 hw->channels_max = UINT_MAX;
519 hw->formats = ULLONG_MAX;
522 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
523 struct snd_soc_pcm_stream *p)
525 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
527 /* setup hw->rate_min/max via hw->rates first */
528 snd_pcm_hw_limit_rates(hw);
530 /* update hw->rate_min/max by snd_soc_pcm_stream */
531 hw->rate_min = max(hw->rate_min, p->rate_min);
532 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
535 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
536 struct snd_soc_pcm_stream *p)
538 hw->channels_min = max(hw->channels_min, p->channels_min);
539 hw->channels_max = min(hw->channels_max, p->channels_max);
542 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
543 struct snd_soc_pcm_stream *p)
545 hw->formats &= p->formats;
549 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
550 * @rtd: ASoC PCM runtime
551 * @hw: PCM hardware parameters (output)
552 * @stream: Direction of the PCM stream
554 * Calculates the subset of stream parameters supported by all DAIs
555 * associated with the PCM stream.
557 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
558 struct snd_pcm_hardware *hw, int stream)
560 struct snd_soc_dai *codec_dai;
561 struct snd_soc_dai *cpu_dai;
562 struct snd_soc_pcm_stream *codec_stream;
563 struct snd_soc_pcm_stream *cpu_stream;
564 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
569 /* first calculate min/max only for CPUs in the DAI link */
570 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
573 * Skip CPUs which don't support the current stream type.
574 * Otherwise, since the rate, channel, and format values will
575 * zero in that case, we would have no usable settings left,
576 * causing the resulting setup to fail.
578 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
581 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
583 soc_pcm_hw_update_chan(hw, cpu_stream);
584 soc_pcm_hw_update_rate(hw, cpu_stream);
585 soc_pcm_hw_update_format(hw, cpu_stream);
587 cpu_chan_min = hw->channels_min;
588 cpu_chan_max = hw->channels_max;
590 /* second calculate min/max only for CODECs in the DAI link */
591 for_each_rtd_codec_dais(rtd, i, codec_dai) {
594 * Skip CODECs which don't support the current stream type.
595 * Otherwise, since the rate, channel, and format values will
596 * zero in that case, we would have no usable settings left,
597 * causing the resulting setup to fail.
599 if (!snd_soc_dai_stream_valid(codec_dai, stream))
602 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
604 soc_pcm_hw_update_chan(hw, codec_stream);
605 soc_pcm_hw_update_rate(hw, codec_stream);
606 soc_pcm_hw_update_format(hw, codec_stream);
609 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
610 if (!hw->channels_min)
614 * chan min/max cannot be enforced if there are multiple CODEC DAIs
615 * connected to CPU DAI(s), use CPU DAI's directly and let
616 * channel allocation be fixed up later
618 if (rtd->num_codecs > 1) {
619 hw->channels_min = cpu_chan_min;
620 hw->channels_max = cpu_chan_max;
625 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
627 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
629 struct snd_pcm_hardware *hw = &substream->runtime->hw;
630 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
631 u64 formats = hw->formats;
634 * At least one CPU and one CODEC should match. Otherwise, we should
635 * have bailed out on a higher level, since there would be no CPU or
636 * CODEC to support the transfer direction in that case.
638 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
641 hw->formats &= formats;
644 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
646 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
647 struct snd_soc_component *component;
650 for_each_rtd_components(rtd, i, component) {
651 ret = snd_soc_component_module_get_when_open(component, substream);
655 ret = snd_soc_component_open(component, substream);
663 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
666 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
667 struct snd_soc_component *component;
670 for_each_rtd_components(rtd, i, component) {
671 int r = snd_soc_component_close(component, substream, rollback);
673 ret = r; /* use last ret */
675 snd_soc_component_module_put_when_close(component, substream, rollback);
681 static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
682 struct snd_pcm_substream *substream, int rollback)
684 struct snd_soc_component *component;
685 struct snd_soc_dai *dai;
688 snd_soc_dpcm_mutex_assert_held(rtd);
691 snd_soc_runtime_deactivate(rtd, substream->stream);
693 for_each_rtd_dais(rtd, i, dai)
694 snd_soc_dai_shutdown(dai, substream, rollback);
696 snd_soc_link_shutdown(substream, rollback);
698 soc_pcm_components_close(substream, rollback);
700 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
702 for_each_rtd_components(rtd, i, component)
703 if (!snd_soc_component_active(component))
704 pinctrl_pm_select_sleep_state(component->dev);
710 * Called by ALSA when a PCM substream is closed. Private data can be
711 * freed here. The cpu DAI, codec DAI, machine and components are also
714 static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
715 struct snd_pcm_substream *substream)
717 return soc_pcm_clean(rtd, substream, 0);
720 /* PCM close ops for non-DPCM streams */
721 static int soc_pcm_close(struct snd_pcm_substream *substream)
723 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
725 snd_soc_dpcm_mutex_lock(rtd);
726 soc_pcm_clean(rtd, substream, 0);
727 snd_soc_dpcm_mutex_unlock(rtd);
731 static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
733 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
734 struct snd_pcm_hardware *hw = &substream->runtime->hw;
735 const char *name_cpu = soc_cpu_dai_name(rtd);
736 const char *name_codec = soc_codec_dai_name(rtd);
738 struct device *dev = rtd->dev;
748 err_msg = "channels";
749 if (!hw->channels_min || !hw->channels_max ||
750 hw->channels_min > hw->channels_max)
753 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
755 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
756 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
758 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
764 dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
765 name_codec, name_cpu, err_msg);
770 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
771 * then initialized and any private data can be allocated. This also calls
772 * startup for the cpu DAI, component, machine and codec DAI.
774 static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
775 struct snd_pcm_substream *substream)
777 struct snd_soc_component *component;
778 struct snd_soc_dai *dai;
781 snd_soc_dpcm_mutex_assert_held(rtd);
783 for_each_rtd_components(rtd, i, component)
784 pinctrl_pm_select_default_state(component->dev);
786 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
790 ret = soc_pcm_components_open(substream);
794 ret = snd_soc_link_startup(substream);
798 /* startup the audio subsystem */
799 for_each_rtd_dais(rtd, i, dai) {
800 ret = snd_soc_dai_startup(dai, substream);
804 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
810 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
811 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
814 /* Check that the codec and cpu DAIs are compatible */
815 soc_pcm_init_runtime_hw(substream);
817 soc_pcm_update_symmetry(substream);
819 ret = soc_hw_sanity_check(substream);
823 soc_pcm_apply_msb(substream);
825 /* Symmetry only applies if we've already got an active stream. */
826 for_each_rtd_dais(rtd, i, dai) {
827 ret = soc_pcm_apply_symmetry(substream, dai);
832 snd_soc_runtime_activate(rtd, substream->stream);
836 soc_pcm_clean(rtd, substream, 1);
837 dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
843 /* PCM open ops for non-DPCM streams */
844 static int soc_pcm_open(struct snd_pcm_substream *substream)
846 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
849 snd_soc_dpcm_mutex_lock(rtd);
850 ret = __soc_pcm_open(rtd, substream);
851 snd_soc_dpcm_mutex_unlock(rtd);
855 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
858 * Currently nothing to do for c2c links
859 * Since c2c links are internal nodes in the DAPM graph and
860 * don't interface with the outside world or application layer
861 * we don't have to do any special handling on close.
866 * Called by ALSA when the PCM substream is prepared, can set format, sample
867 * rate, etc. This function is non atomic and can be called multiple times,
868 * it can refer to the runtime info.
870 static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
871 struct snd_pcm_substream *substream)
873 struct snd_soc_dai *dai;
876 snd_soc_dpcm_mutex_assert_held(rtd);
878 ret = snd_soc_link_prepare(substream);
882 ret = snd_soc_pcm_component_prepare(substream);
886 ret = snd_soc_pcm_dai_prepare(substream);
890 /* cancel any delayed stream shutdown that is pending */
891 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
894 cancel_delayed_work(&rtd->delayed_work);
897 snd_soc_dapm_stream_event(rtd, substream->stream,
898 SND_SOC_DAPM_STREAM_START);
900 for_each_rtd_dais(rtd, i, dai)
901 snd_soc_dai_digital_mute(dai, 0, substream->stream);
905 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
910 /* PCM prepare ops for non-DPCM streams */
911 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
913 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
916 snd_soc_dpcm_mutex_lock(rtd);
917 ret = __soc_pcm_prepare(rtd, substream);
918 snd_soc_dpcm_mutex_unlock(rtd);
922 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
925 struct snd_interval *interval;
926 int channels = hweight_long(mask);
928 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
929 interval->min = channels;
930 interval->max = channels;
933 static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
934 struct snd_pcm_substream *substream, int rollback)
936 struct snd_soc_dai *dai;
939 snd_soc_dpcm_mutex_assert_held(rtd);
941 /* clear the corresponding DAIs parameters when going to be inactive */
942 for_each_rtd_dais(rtd, i, dai) {
943 if (snd_soc_dai_active(dai) == 1)
944 soc_pcm_set_dai_params(dai, NULL);
946 if (snd_soc_dai_stream_active(dai, substream->stream) == 1)
947 snd_soc_dai_digital_mute(dai, 1, substream->stream);
950 /* run the stream event */
951 snd_soc_dapm_stream_stop(rtd, substream->stream);
953 /* free any machine hw params */
954 snd_soc_link_hw_free(substream, rollback);
956 /* free any component resources */
957 snd_soc_pcm_component_hw_free(substream, rollback);
959 /* now free hw params for the DAIs */
960 for_each_rtd_dais(rtd, i, dai)
961 if (snd_soc_dai_stream_valid(dai, substream->stream))
962 snd_soc_dai_hw_free(dai, substream, rollback);
968 * Frees resources allocated by hw_params, can be called multiple times
970 static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
971 struct snd_pcm_substream *substream)
973 return soc_pcm_hw_clean(rtd, substream, 0);
976 /* hw_free PCM ops for non-DPCM streams */
977 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
979 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
982 snd_soc_dpcm_mutex_lock(rtd);
983 ret = __soc_pcm_hw_free(rtd, substream);
984 snd_soc_dpcm_mutex_unlock(rtd);
989 * Called by ALSA when the hardware params are set by application. This
990 * function can also be called multiple times and can allocate buffers
991 * (using snd_pcm_lib_* ). It's non-atomic.
993 static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
994 struct snd_pcm_substream *substream,
995 struct snd_pcm_hw_params *params)
997 struct snd_soc_dai *cpu_dai;
998 struct snd_soc_dai *codec_dai;
1001 snd_soc_dpcm_mutex_assert_held(rtd);
1003 ret = soc_pcm_params_symmetry(substream, params);
1007 ret = snd_soc_link_hw_params(substream, params);
1011 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1012 struct snd_pcm_hw_params codec_params;
1015 * Skip CODECs which don't support the current stream type,
1016 * the idea being that if a CODEC is not used for the currently
1017 * set up transfer direction, it should not need to be
1018 * configured, especially since the configuration used might
1019 * not even be supported by that CODEC. There may be cases
1020 * however where a CODEC needs to be set up although it is
1021 * actually not being used for the transfer, e.g. if a
1022 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1023 * for the DAI link including a playback-only CODEC.
1024 * If this becomes necessary, we will have to augment the
1025 * machine driver setup with information on how to act, so
1026 * we can do the right thing here.
1028 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1031 /* copy params for each codec */
1032 codec_params = *params;
1034 /* fixup params based on TDM slot masks */
1035 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1037 soc_pcm_codec_params_fixup(&codec_params,
1038 codec_dai->tx_mask);
1040 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1042 soc_pcm_codec_params_fixup(&codec_params,
1043 codec_dai->rx_mask);
1045 ret = snd_soc_dai_hw_params(codec_dai, substream,
1050 soc_pcm_set_dai_params(codec_dai, &codec_params);
1051 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1054 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1056 * Skip CPUs which don't support the current stream
1057 * type. See soc_pcm_init_runtime_hw() for more details
1059 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1062 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1066 /* store the parameters for each DAI */
1067 soc_pcm_set_dai_params(cpu_dai, params);
1068 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1071 ret = snd_soc_pcm_component_hw_params(substream, params);
1074 soc_pcm_hw_clean(rtd, substream, 1);
1075 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1081 /* hw_params PCM ops for non-DPCM streams */
1082 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
1083 struct snd_pcm_hw_params *params)
1085 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1088 snd_soc_dpcm_mutex_lock(rtd);
1089 ret = __soc_pcm_hw_params(rtd, substream, params);
1090 snd_soc_dpcm_mutex_unlock(rtd);
1094 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1096 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1097 int ret = -EINVAL, _ret = 0;
1101 case SNDRV_PCM_TRIGGER_START:
1102 case SNDRV_PCM_TRIGGER_RESUME:
1103 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1104 ret = snd_soc_link_trigger(substream, cmd, 0);
1108 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1112 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1121 case SNDRV_PCM_TRIGGER_START:
1122 cmd = SNDRV_PCM_TRIGGER_STOP;
1124 case SNDRV_PCM_TRIGGER_RESUME:
1125 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1127 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1128 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1134 case SNDRV_PCM_TRIGGER_STOP:
1135 case SNDRV_PCM_TRIGGER_SUSPEND:
1136 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1137 if (rtd->dai_link->stop_dma_first) {
1138 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1142 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1146 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1150 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1154 ret = snd_soc_link_trigger(substream, cmd, rollback);
1165 * soc level wrapper for pointer callback
1166 * If cpu_dai, codec_dai, component driver has the delay callback, then
1167 * the runtime->delay will be updated via snd_soc_pcm_component/dai_delay().
1169 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1171 struct snd_pcm_runtime *runtime = substream->runtime;
1172 snd_pcm_uframes_t offset = 0;
1173 snd_pcm_sframes_t codec_delay = 0;
1174 snd_pcm_sframes_t cpu_delay = 0;
1176 offset = snd_soc_pcm_component_pointer(substream);
1178 /* should be called *after* snd_soc_pcm_component_pointer() */
1179 snd_soc_pcm_dai_delay(substream, &cpu_delay, &codec_delay);
1180 snd_soc_pcm_component_delay(substream, &cpu_delay, &codec_delay);
1182 runtime->delay = cpu_delay + codec_delay;
1187 /* connect a FE and BE */
1188 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1189 struct snd_soc_pcm_runtime *be, int stream)
1191 struct snd_pcm_substream *fe_substream;
1192 struct snd_pcm_substream *be_substream;
1193 struct snd_soc_dpcm *dpcm;
1195 snd_soc_dpcm_mutex_assert_held(fe);
1197 /* only add new dpcms */
1198 for_each_dpcm_be(fe, stream, dpcm) {
1199 if (dpcm->be == be && dpcm->fe == fe)
1203 fe_substream = snd_soc_dpcm_get_substream(fe, stream);
1204 be_substream = snd_soc_dpcm_get_substream(be, stream);
1206 if (!fe_substream->pcm->nonatomic && be_substream->pcm->nonatomic) {
1207 dev_err(be->dev, "%s: FE is atomic but BE is nonatomic, invalid configuration\n",
1211 if (fe_substream->pcm->nonatomic && !be_substream->pcm->nonatomic) {
1212 dev_dbg(be->dev, "FE is nonatomic but BE is not, forcing BE as nonatomic\n");
1213 be_substream->pcm->nonatomic = 1;
1216 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1222 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1223 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1224 snd_soc_dpcm_stream_lock_irq(fe, stream);
1225 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1226 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1227 snd_soc_dpcm_stream_unlock_irq(fe, stream);
1229 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1230 stream ? "capture" : "playback", fe->dai_link->name,
1231 stream ? "<-" : "->", be->dai_link->name);
1233 dpcm_create_debugfs_state(dpcm, stream);
1238 /* reparent a BE onto another FE */
1239 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1240 struct snd_soc_pcm_runtime *be, int stream)
1242 struct snd_soc_dpcm *dpcm;
1243 struct snd_pcm_substream *fe_substream, *be_substream;
1245 /* reparent if BE is connected to other FEs */
1246 if (!be->dpcm[stream].users)
1249 be_substream = snd_soc_dpcm_get_substream(be, stream);
1251 for_each_dpcm_fe(be, stream, dpcm) {
1255 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1256 stream ? "capture" : "playback",
1257 dpcm->fe->dai_link->name,
1258 stream ? "<-" : "->", dpcm->be->dai_link->name);
1260 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1261 be_substream->runtime = fe_substream->runtime;
1266 /* disconnect a BE and FE */
1267 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1269 struct snd_soc_dpcm *dpcm, *d;
1270 LIST_HEAD(deleted_dpcms);
1272 snd_soc_dpcm_mutex_assert_held(fe);
1274 snd_soc_dpcm_stream_lock_irq(fe, stream);
1275 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1276 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1277 stream ? "capture" : "playback",
1278 dpcm->be->dai_link->name);
1280 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1283 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1284 stream ? "capture" : "playback", fe->dai_link->name,
1285 stream ? "<-" : "->", dpcm->be->dai_link->name);
1287 /* BEs still alive need new FE */
1288 dpcm_be_reparent(fe, dpcm->be, stream);
1290 list_del(&dpcm->list_be);
1291 list_move(&dpcm->list_fe, &deleted_dpcms);
1293 snd_soc_dpcm_stream_unlock_irq(fe, stream);
1295 while (!list_empty(&deleted_dpcms)) {
1296 dpcm = list_first_entry(&deleted_dpcms, struct snd_soc_dpcm,
1298 list_del(&dpcm->list_fe);
1299 dpcm_remove_debugfs_state(dpcm);
1304 /* get BE for DAI widget and stream */
1305 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1306 struct snd_soc_dapm_widget *widget, int stream)
1308 struct snd_soc_pcm_runtime *be;
1309 struct snd_soc_dapm_widget *w;
1310 struct snd_soc_dai *dai;
1313 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1315 for_each_card_rtds(card, be) {
1317 if (!be->dai_link->no_pcm)
1320 for_each_rtd_dais(be, i, dai) {
1321 w = snd_soc_dai_get_widget(dai, stream);
1323 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1324 w ? w->name : "(not set)");
1331 /* Widget provided is not a BE */
1335 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1336 struct snd_soc_dapm_widget *widget)
1338 struct snd_soc_dapm_widget *w;
1341 for_each_dapm_widgets(list, i, w)
1348 bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget, enum snd_soc_dapm_direction dir)
1350 struct snd_soc_card *card = widget->dapm->card;
1351 struct snd_soc_pcm_runtime *rtd;
1354 /* adjust dir to stream */
1355 if (dir == SND_SOC_DAPM_DIR_OUT)
1356 stream = SNDRV_PCM_STREAM_PLAYBACK;
1358 stream = SNDRV_PCM_STREAM_CAPTURE;
1360 rtd = dpcm_get_be(card, widget, stream);
1366 EXPORT_SYMBOL_GPL(dpcm_end_walk_at_be);
1368 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1369 int stream, struct snd_soc_dapm_widget_list **list)
1371 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1374 if (fe->num_cpus > 1) {
1376 "%s doesn't support Multi CPU yet\n", __func__);
1380 /* get number of valid DAI paths and their widgets */
1381 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1382 fe->card->component_chaining ?
1383 NULL : dpcm_end_walk_at_be);
1386 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1387 stream ? "capture" : "playback");
1388 else if (paths == 0)
1389 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1390 stream ? "capture" : "playback");
1395 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1397 snd_soc_dapm_dai_free_widgets(list);
1400 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1401 struct snd_soc_dapm_widget_list *list)
1403 struct snd_soc_dai *dai;
1406 /* is there a valid DAI widget for this BE */
1407 for_each_rtd_dais(dpcm->be, i, dai) {
1408 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
1411 * The BE is pruned only if none of the dai
1412 * widgets are in the active list.
1414 if (widget && widget_in_list(list, widget))
1421 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1422 struct snd_soc_dapm_widget_list **list_)
1424 struct snd_soc_dpcm *dpcm;
1427 /* Destroy any old FE <--> BE connections */
1428 for_each_dpcm_be(fe, stream, dpcm) {
1429 if (dpcm_be_is_active(dpcm, stream, *list_))
1432 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1433 stream ? "capture" : "playback",
1434 dpcm->be->dai_link->name, fe->dai_link->name);
1435 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1436 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1440 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1444 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1445 struct snd_soc_dapm_widget_list **list_)
1447 struct snd_soc_card *card = fe->card;
1448 struct snd_soc_dapm_widget_list *list = *list_;
1449 struct snd_soc_pcm_runtime *be;
1450 struct snd_soc_dapm_widget *widget;
1451 int i, new = 0, err;
1453 /* Create any new FE <--> BE connections */
1454 for_each_dapm_widgets(list, i, widget) {
1456 switch (widget->id) {
1457 case snd_soc_dapm_dai_in:
1458 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1461 case snd_soc_dapm_dai_out:
1462 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1469 /* is there a valid BE rtd for this widget */
1470 be = dpcm_get_be(card, widget, stream);
1472 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1477 /* don't connect if FE is not running */
1478 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1482 * Filter for systems with 'component_chaining' enabled.
1483 * This helps to avoid unnecessary re-configuration of an
1484 * already active BE on such systems.
1486 if (fe->card->component_chaining &&
1487 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1488 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1491 /* newly connected FE and BE */
1492 err = dpcm_be_connect(fe, be, stream);
1494 dev_err(fe->dev, "ASoC: can't connect %s\n",
1497 } else if (err == 0) /* already connected */
1501 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1505 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1510 * Find the corresponding BE DAIs that source or sink audio to this
1513 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1514 int stream, struct snd_soc_dapm_widget_list **list, int new)
1517 return dpcm_add_paths(fe, stream, list);
1519 return dpcm_prune_paths(fe, stream, list);
1522 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1524 struct snd_soc_dpcm *dpcm;
1526 for_each_dpcm_be(fe, stream, dpcm)
1527 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1530 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1531 int do_hw_free, struct snd_soc_dpcm *last)
1533 struct snd_soc_dpcm *dpcm;
1535 /* disable any enabled and non active backends */
1536 for_each_dpcm_be(fe, stream, dpcm) {
1537 struct snd_soc_pcm_runtime *be = dpcm->be;
1538 struct snd_pcm_substream *be_substream =
1539 snd_soc_dpcm_get_substream(be, stream);
1544 /* is this op for this BE ? */
1545 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1548 if (be->dpcm[stream].users == 0) {
1549 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1550 stream ? "capture" : "playback",
1551 be->dpcm[stream].state);
1555 if (--be->dpcm[stream].users != 0)
1558 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1562 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1563 __soc_pcm_hw_free(be, be_substream);
1564 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1568 __soc_pcm_close(be, be_substream);
1569 be_substream->runtime = NULL;
1570 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1574 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1576 struct snd_soc_pcm_runtime *be;
1577 struct snd_soc_dpcm *dpcm;
1580 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1581 for_each_dpcm_be(fe, stream, dpcm) {
1582 struct snd_pcm_substream *be_substream;
1585 be_substream = snd_soc_dpcm_get_substream(be, stream);
1587 if (!be_substream) {
1588 dev_err(be->dev, "ASoC: no backend %s stream\n",
1589 stream ? "capture" : "playback");
1593 /* is this op for this BE ? */
1594 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1597 /* first time the dpcm is open ? */
1598 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1599 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1600 stream ? "capture" : "playback",
1601 be->dpcm[stream].state);
1605 if (be->dpcm[stream].users++ != 0)
1608 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1609 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1612 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1613 stream ? "capture" : "playback", be->dai_link->name);
1615 be_substream->runtime = be->dpcm[stream].runtime;
1616 err = __soc_pcm_open(be, be_substream);
1618 be->dpcm[stream].users--;
1619 if (be->dpcm[stream].users < 0)
1620 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1621 stream ? "capture" : "playback",
1622 be->dpcm[stream].state);
1624 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1627 be->dpcm[stream].be_start = 0;
1628 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1635 dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1637 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1638 __func__, be->dai_link->name, err);
1643 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1645 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1646 struct snd_pcm_runtime *runtime = substream->runtime;
1647 struct snd_pcm_hardware *hw = &runtime->hw;
1648 struct snd_soc_dai *dai;
1649 int stream = substream->stream;
1652 soc_pcm_hw_init(hw);
1654 for_each_rtd_cpu_dais(fe, i, dai) {
1655 struct snd_soc_pcm_stream *cpu_stream;
1658 * Skip CPUs which don't support the current stream
1659 * type. See soc_pcm_init_runtime_hw() for more details
1661 if (!snd_soc_dai_stream_valid(dai, stream))
1664 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1666 soc_pcm_hw_update_rate(hw, cpu_stream);
1667 soc_pcm_hw_update_chan(hw, cpu_stream);
1668 soc_pcm_hw_update_format(hw, cpu_stream);
1673 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1675 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1676 struct snd_pcm_runtime *runtime = substream->runtime;
1677 struct snd_pcm_hardware *hw = &runtime->hw;
1678 struct snd_soc_dpcm *dpcm;
1679 struct snd_soc_dai *dai;
1680 int stream = substream->stream;
1682 if (!fe->dai_link->dpcm_merged_format)
1686 * It returns merged BE codec format
1687 * if FE want to use it (= dpcm_merged_format)
1690 for_each_dpcm_be(fe, stream, dpcm) {
1691 struct snd_soc_pcm_runtime *be = dpcm->be;
1692 struct snd_soc_pcm_stream *codec_stream;
1695 for_each_rtd_codec_dais(be, i, dai) {
1697 * Skip CODECs which don't support the current stream
1698 * type. See soc_pcm_init_runtime_hw() for more details
1700 if (!snd_soc_dai_stream_valid(dai, stream))
1703 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1705 soc_pcm_hw_update_format(hw, codec_stream);
1710 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1712 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1713 struct snd_pcm_runtime *runtime = substream->runtime;
1714 struct snd_pcm_hardware *hw = &runtime->hw;
1715 struct snd_soc_dpcm *dpcm;
1716 int stream = substream->stream;
1718 if (!fe->dai_link->dpcm_merged_chan)
1722 * It returns merged BE codec channel;
1723 * if FE want to use it (= dpcm_merged_chan)
1726 for_each_dpcm_be(fe, stream, dpcm) {
1727 struct snd_soc_pcm_runtime *be = dpcm->be;
1728 struct snd_soc_pcm_stream *cpu_stream;
1729 struct snd_soc_dai *dai;
1732 for_each_rtd_cpu_dais(be, i, dai) {
1734 * Skip CPUs which don't support the current stream
1735 * type. See soc_pcm_init_runtime_hw() for more details
1737 if (!snd_soc_dai_stream_valid(dai, stream))
1740 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1742 soc_pcm_hw_update_chan(hw, cpu_stream);
1746 * chan min/max cannot be enforced if there are multiple CODEC
1747 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1749 if (be->num_codecs == 1) {
1750 struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
1751 asoc_rtd_to_codec(be, 0), stream);
1753 soc_pcm_hw_update_chan(hw, codec_stream);
1758 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1760 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1761 struct snd_pcm_runtime *runtime = substream->runtime;
1762 struct snd_pcm_hardware *hw = &runtime->hw;
1763 struct snd_soc_dpcm *dpcm;
1764 int stream = substream->stream;
1766 if (!fe->dai_link->dpcm_merged_rate)
1770 * It returns merged BE codec channel;
1771 * if FE want to use it (= dpcm_merged_chan)
1774 for_each_dpcm_be(fe, stream, dpcm) {
1775 struct snd_soc_pcm_runtime *be = dpcm->be;
1776 struct snd_soc_pcm_stream *pcm;
1777 struct snd_soc_dai *dai;
1780 for_each_rtd_dais(be, i, dai) {
1782 * Skip DAIs which don't support the current stream
1783 * type. See soc_pcm_init_runtime_hw() for more details
1785 if (!snd_soc_dai_stream_valid(dai, stream))
1788 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1790 soc_pcm_hw_update_rate(hw, pcm);
1795 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1798 struct snd_soc_dpcm *dpcm;
1799 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1800 struct snd_soc_dai *fe_cpu_dai;
1804 /* apply symmetry for FE */
1805 soc_pcm_update_symmetry(fe_substream);
1807 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1808 /* Symmetry only applies if we've got an active stream. */
1809 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1814 /* apply symmetry for BE */
1815 for_each_dpcm_be(fe, stream, dpcm) {
1816 struct snd_soc_pcm_runtime *be = dpcm->be;
1817 struct snd_pcm_substream *be_substream =
1818 snd_soc_dpcm_get_substream(be, stream);
1819 struct snd_soc_pcm_runtime *rtd;
1820 struct snd_soc_dai *dai;
1822 /* A backend may not have the requested substream */
1826 rtd = asoc_substream_to_rtd(be_substream);
1827 if (rtd->dai_link->be_hw_params_fixup)
1830 soc_pcm_update_symmetry(be_substream);
1832 /* Symmetry only applies if we've got an active stream. */
1833 for_each_rtd_dais(rtd, i, dai) {
1834 err = soc_pcm_apply_symmetry(fe_substream, dai);
1841 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
1846 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1848 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1849 int stream = fe_substream->stream, ret = 0;
1851 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1853 ret = dpcm_be_dai_startup(fe, stream);
1857 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1859 /* start the DAI frontend */
1860 ret = __soc_pcm_open(fe, fe_substream);
1864 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1866 dpcm_runtime_setup_fe(fe_substream);
1868 dpcm_runtime_setup_be_format(fe_substream);
1869 dpcm_runtime_setup_be_chan(fe_substream);
1870 dpcm_runtime_setup_be_rate(fe_substream);
1872 ret = dpcm_apply_symmetry(fe_substream, stream);
1876 dpcm_be_dai_startup_unwind(fe, stream);
1878 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1881 dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
1886 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1888 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1889 int stream = substream->stream;
1891 snd_soc_dpcm_mutex_assert_held(fe);
1893 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1895 /* shutdown the BEs */
1896 dpcm_be_dai_shutdown(fe, stream);
1898 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1900 /* now shutdown the frontend */
1901 __soc_pcm_close(fe, substream);
1903 /* run the stream stop event */
1904 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1906 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1907 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1911 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1913 struct snd_soc_dpcm *dpcm;
1915 /* only hw_params backends that are either sinks or sources
1916 * to this frontend DAI */
1917 for_each_dpcm_be(fe, stream, dpcm) {
1919 struct snd_soc_pcm_runtime *be = dpcm->be;
1920 struct snd_pcm_substream *be_substream =
1921 snd_soc_dpcm_get_substream(be, stream);
1923 /* is this op for this BE ? */
1924 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1927 /* only free hw when no longer used - check all FEs */
1928 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1931 /* do not free hw if this BE is used by other FE */
1932 if (be->dpcm[stream].users > 1)
1935 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1936 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1937 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1938 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1940 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1943 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1944 be->dai_link->name);
1946 __soc_pcm_hw_free(be, be_substream);
1948 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1952 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1954 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1955 int stream = substream->stream;
1957 snd_soc_dpcm_mutex_lock(fe);
1958 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1960 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1962 /* call hw_free on the frontend */
1963 soc_pcm_hw_clean(fe, substream, 0);
1965 /* only hw_params backends that are either sinks or sources
1966 * to this frontend DAI */
1967 dpcm_be_dai_hw_free(fe, stream);
1969 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1970 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1972 snd_soc_dpcm_mutex_unlock(fe);
1976 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1978 struct snd_soc_pcm_runtime *be;
1979 struct snd_pcm_substream *be_substream;
1980 struct snd_soc_dpcm *dpcm;
1983 for_each_dpcm_be(fe, stream, dpcm) {
1985 be_substream = snd_soc_dpcm_get_substream(be, stream);
1987 /* is this op for this BE ? */
1988 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1991 /* copy params for each dpcm */
1992 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1993 sizeof(struct snd_pcm_hw_params));
1995 /* perform any hw_params fixups */
1996 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
2000 /* copy the fixed-up hw params for BE dai */
2001 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2002 sizeof(struct snd_pcm_hw_params));
2004 /* only allow hw_params() if no connected FEs are running */
2005 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2008 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2009 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2010 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2013 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2014 be->dai_link->name);
2016 ret = __soc_pcm_hw_params(be, be_substream, &dpcm->hw_params);
2020 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2025 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2026 __func__, be->dai_link->name, ret);
2028 /* disable any enabled and non active backends */
2029 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2031 be_substream = snd_soc_dpcm_get_substream(be, stream);
2033 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2036 /* only allow hw_free() if no connected FEs are running */
2037 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2040 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2041 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2042 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2043 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2046 __soc_pcm_hw_free(be, be_substream);
2052 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2053 struct snd_pcm_hw_params *params)
2055 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2056 int ret, stream = substream->stream;
2058 snd_soc_dpcm_mutex_lock(fe);
2059 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2061 memcpy(&fe->dpcm[stream].hw_params, params,
2062 sizeof(struct snd_pcm_hw_params));
2063 ret = dpcm_be_dai_hw_params(fe, stream);
2067 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2068 fe->dai_link->name, params_rate(params),
2069 params_channels(params), params_format(params));
2071 /* call hw_params on the frontend */
2072 ret = __soc_pcm_hw_params(fe, substream, params);
2074 dpcm_be_dai_hw_free(fe, stream);
2076 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2079 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2080 snd_soc_dpcm_mutex_unlock(fe);
2083 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
2088 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2091 struct snd_soc_pcm_runtime *be;
2092 bool pause_stop_transition;
2093 struct snd_soc_dpcm *dpcm;
2094 unsigned long flags;
2097 for_each_dpcm_be(fe, stream, dpcm) {
2098 struct snd_pcm_substream *be_substream;
2101 be_substream = snd_soc_dpcm_get_substream(be, stream);
2103 snd_soc_dpcm_stream_lock_irqsave_nested(be, stream, flags);
2105 /* is this op for this BE ? */
2106 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2109 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2110 be->dai_link->name, cmd);
2113 case SNDRV_PCM_TRIGGER_START:
2114 if (!be->dpcm[stream].be_start &&
2115 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2116 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2117 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2120 be->dpcm[stream].be_start++;
2121 if (be->dpcm[stream].be_start != 1)
2124 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED)
2125 ret = soc_pcm_trigger(be_substream,
2126 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
2128 ret = soc_pcm_trigger(be_substream,
2129 SNDRV_PCM_TRIGGER_START);
2131 be->dpcm[stream].be_start--;
2135 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2137 case SNDRV_PCM_TRIGGER_RESUME:
2138 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2141 be->dpcm[stream].be_start++;
2142 if (be->dpcm[stream].be_start != 1)
2145 ret = soc_pcm_trigger(be_substream, cmd);
2147 be->dpcm[stream].be_start--;
2151 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2153 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2154 if (!be->dpcm[stream].be_start &&
2155 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2156 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2159 fe->dpcm[stream].fe_pause = false;
2160 be->dpcm[stream].be_pause--;
2162 be->dpcm[stream].be_start++;
2163 if (be->dpcm[stream].be_start != 1)
2166 ret = soc_pcm_trigger(be_substream, cmd);
2168 be->dpcm[stream].be_start--;
2172 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2174 case SNDRV_PCM_TRIGGER_STOP:
2175 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2176 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2179 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2180 be->dpcm[stream].be_start--;
2182 if (be->dpcm[stream].be_start != 0)
2185 pause_stop_transition = false;
2186 if (fe->dpcm[stream].fe_pause) {
2187 pause_stop_transition = true;
2188 fe->dpcm[stream].fe_pause = false;
2189 be->dpcm[stream].be_pause--;
2192 if (be->dpcm[stream].be_pause != 0)
2193 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
2195 ret = soc_pcm_trigger(be_substream, SNDRV_PCM_TRIGGER_STOP);
2198 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_START)
2199 be->dpcm[stream].be_start++;
2200 if (pause_stop_transition) {
2201 fe->dpcm[stream].fe_pause = true;
2202 be->dpcm[stream].be_pause++;
2207 if (be->dpcm[stream].be_pause != 0)
2208 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2210 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2213 case SNDRV_PCM_TRIGGER_SUSPEND:
2214 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2217 be->dpcm[stream].be_start--;
2218 if (be->dpcm[stream].be_start != 0)
2221 ret = soc_pcm_trigger(be_substream, cmd);
2223 be->dpcm[stream].be_start++;
2227 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2229 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2230 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2233 fe->dpcm[stream].fe_pause = true;
2234 be->dpcm[stream].be_pause++;
2236 be->dpcm[stream].be_start--;
2237 if (be->dpcm[stream].be_start != 0)
2240 ret = soc_pcm_trigger(be_substream, cmd);
2242 be->dpcm[stream].be_start++;
2246 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2250 snd_soc_dpcm_stream_unlock_irqrestore(be, stream, flags);
2255 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2256 __func__, be->dai_link->name, ret);
2259 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2261 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2262 int cmd, bool fe_first)
2264 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2267 /* call trigger on the frontend before the backend. */
2269 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2270 fe->dai_link->name, cmd);
2272 ret = soc_pcm_trigger(substream, cmd);
2276 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2280 /* call trigger on the frontend after the backend. */
2281 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2285 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2286 fe->dai_link->name, cmd);
2288 ret = soc_pcm_trigger(substream, cmd);
2293 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2295 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2296 int stream = substream->stream;
2298 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2300 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2303 case SND_SOC_DPCM_TRIGGER_PRE:
2305 case SNDRV_PCM_TRIGGER_START:
2306 case SNDRV_PCM_TRIGGER_RESUME:
2307 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2308 case SNDRV_PCM_TRIGGER_DRAIN:
2309 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2311 case SNDRV_PCM_TRIGGER_STOP:
2312 case SNDRV_PCM_TRIGGER_SUSPEND:
2313 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2314 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2321 case SND_SOC_DPCM_TRIGGER_POST:
2323 case SNDRV_PCM_TRIGGER_START:
2324 case SNDRV_PCM_TRIGGER_RESUME:
2325 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2326 case SNDRV_PCM_TRIGGER_DRAIN:
2327 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2329 case SNDRV_PCM_TRIGGER_STOP:
2330 case SNDRV_PCM_TRIGGER_SUSPEND:
2331 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2332 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2339 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2340 /* bespoke trigger() - handles both FE and BEs */
2342 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2343 fe->dai_link->name, cmd);
2345 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2348 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2349 fe->dai_link->name);
2355 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2361 case SNDRV_PCM_TRIGGER_START:
2362 case SNDRV_PCM_TRIGGER_RESUME:
2363 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2364 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2366 case SNDRV_PCM_TRIGGER_STOP:
2367 case SNDRV_PCM_TRIGGER_SUSPEND:
2368 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2370 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2371 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2376 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2380 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2382 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2383 int stream = substream->stream;
2385 /* if FE's runtime_update is already set, we're in race;
2386 * process this trigger later at exit
2388 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2389 fe->dpcm[stream].trigger_pending = cmd + 1;
2390 return 0; /* delayed, assuming it's successful */
2393 /* we're alone, let's trigger */
2394 return dpcm_fe_dai_do_trigger(substream, cmd);
2397 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2399 struct snd_soc_dpcm *dpcm;
2402 for_each_dpcm_be(fe, stream, dpcm) {
2404 struct snd_soc_pcm_runtime *be = dpcm->be;
2405 struct snd_pcm_substream *be_substream =
2406 snd_soc_dpcm_get_substream(be, stream);
2408 /* is this op for this BE ? */
2409 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2412 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2413 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2414 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2415 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2418 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2419 be->dai_link->name);
2421 ret = __soc_pcm_prepare(be, be_substream);
2425 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2429 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2434 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2436 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2437 int stream = substream->stream, ret = 0;
2439 snd_soc_dpcm_mutex_lock(fe);
2441 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2443 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2445 /* there is no point preparing this FE if there are no BEs */
2446 if (list_empty(&fe->dpcm[stream].be_clients)) {
2447 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2448 fe->dai_link->name);
2453 ret = dpcm_be_dai_prepare(fe, stream);
2457 /* call prepare on the frontend */
2458 ret = __soc_pcm_prepare(fe, substream);
2462 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2465 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2466 snd_soc_dpcm_mutex_unlock(fe);
2469 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2474 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2476 struct snd_pcm_substream *substream =
2477 snd_soc_dpcm_get_substream(fe, stream);
2478 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2481 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2482 stream ? "capture" : "playback", fe->dai_link->name);
2484 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2485 /* call bespoke trigger - FE takes care of all BE triggers */
2486 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2487 fe->dai_link->name);
2489 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2491 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2492 fe->dai_link->name);
2494 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2497 dpcm_be_dai_hw_free(fe, stream);
2499 dpcm_be_dai_shutdown(fe, stream);
2501 /* run the stream event for each BE */
2502 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2505 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
2510 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2512 struct snd_pcm_substream *substream =
2513 snd_soc_dpcm_get_substream(fe, stream);
2514 struct snd_soc_dpcm *dpcm;
2515 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2518 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2519 stream ? "capture" : "playback", fe->dai_link->name);
2521 /* Only start the BE if the FE is ready */
2522 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2523 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2524 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2525 fe->dai_link->name, fe->dpcm[stream].state);
2530 /* startup must always be called for new BEs */
2531 ret = dpcm_be_dai_startup(fe, stream);
2535 /* keep going if FE state is > open */
2536 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2539 ret = dpcm_be_dai_hw_params(fe, stream);
2543 /* keep going if FE state is > hw_params */
2544 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2547 ret = dpcm_be_dai_prepare(fe, stream);
2551 /* run the stream event for each BE */
2552 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2554 /* keep going if FE state is > prepare */
2555 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2556 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2559 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2560 /* call trigger on the frontend - FE takes care of all BE triggers */
2561 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2562 fe->dai_link->name);
2564 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2568 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2569 fe->dai_link->name);
2571 ret = dpcm_be_dai_trigger(fe, stream,
2572 SNDRV_PCM_TRIGGER_START);
2580 dpcm_be_dai_hw_free(fe, stream);
2582 dpcm_be_dai_shutdown(fe, stream);
2584 /* disconnect any pending BEs */
2585 for_each_dpcm_be(fe, stream, dpcm) {
2586 struct snd_soc_pcm_runtime *be = dpcm->be;
2588 /* is this op for this BE ? */
2589 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2592 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2593 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2594 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2598 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2603 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2605 struct snd_soc_dapm_widget_list *list;
2609 if (!fe->dai_link->dynamic)
2612 if (fe->num_cpus > 1) {
2614 "%s doesn't support Multi CPU yet\n", __func__);
2618 /* only check active links */
2619 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2622 /* DAPM sync will call this to update DSP paths */
2623 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2624 new ? "new" : "old", fe->dai_link->name);
2626 for_each_pcm_streams(stream) {
2628 /* skip if FE doesn't have playback/capture capability */
2629 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2630 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2633 /* skip if FE isn't currently playing/capturing */
2634 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2635 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2638 paths = dpcm_path_get(fe, stream, &list);
2642 /* update any playback/capture paths */
2643 count = dpcm_process_paths(fe, stream, &list, new);
2645 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2647 dpcm_run_update_startup(fe, stream);
2649 dpcm_run_update_shutdown(fe, stream);
2650 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2652 dpcm_clear_pending_state(fe, stream);
2653 dpcm_be_disconnect(fe, stream);
2656 dpcm_path_put(&list);
2662 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2665 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2667 struct snd_soc_pcm_runtime *fe;
2670 mutex_lock_nested(&card->pcm_mutex, card->pcm_subclass);
2671 /* shutdown all old paths first */
2672 for_each_card_rtds(card, fe) {
2673 ret = soc_dpcm_fe_runtime_update(fe, 0);
2678 /* bring new paths up */
2679 for_each_card_rtds(card, fe) {
2680 ret = soc_dpcm_fe_runtime_update(fe, 1);
2686 mutex_unlock(&card->pcm_mutex);
2689 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2691 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2693 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2694 struct snd_soc_dpcm *dpcm;
2695 int stream = fe_substream->stream;
2697 snd_soc_dpcm_mutex_assert_held(fe);
2699 /* mark FE's links ready to prune */
2700 for_each_dpcm_be(fe, stream, dpcm)
2701 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2703 dpcm_be_disconnect(fe, stream);
2705 fe->dpcm[stream].runtime = NULL;
2708 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2710 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2713 snd_soc_dpcm_mutex_lock(fe);
2714 ret = dpcm_fe_dai_shutdown(fe_substream);
2716 dpcm_fe_dai_cleanup(fe_substream);
2718 snd_soc_dpcm_mutex_unlock(fe);
2722 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2724 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2725 struct snd_soc_dapm_widget_list *list;
2727 int stream = fe_substream->stream;
2729 snd_soc_dpcm_mutex_lock(fe);
2730 fe->dpcm[stream].runtime = fe_substream->runtime;
2732 ret = dpcm_path_get(fe, stream, &list);
2736 /* calculate valid and active FE <-> BE dpcms */
2737 dpcm_process_paths(fe, stream, &list, 1);
2739 ret = dpcm_fe_dai_startup(fe_substream);
2741 dpcm_fe_dai_cleanup(fe_substream);
2743 dpcm_clear_pending_state(fe, stream);
2744 dpcm_path_put(&list);
2746 snd_soc_dpcm_mutex_unlock(fe);
2750 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2751 int *playback, int *capture)
2753 struct snd_soc_dai *cpu_dai;
2756 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2758 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2762 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2765 if (rtd->dai_link->dpcm_playback) {
2766 stream = SNDRV_PCM_STREAM_PLAYBACK;
2768 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2769 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2775 dev_err(rtd->card->dev,
2776 "No CPU DAIs support playback for stream %s\n",
2777 rtd->dai_link->stream_name);
2781 if (rtd->dai_link->dpcm_capture) {
2782 stream = SNDRV_PCM_STREAM_CAPTURE;
2784 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2785 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2792 dev_err(rtd->card->dev,
2793 "No CPU DAIs support capture for stream %s\n",
2794 rtd->dai_link->stream_name);
2799 struct snd_soc_dai *codec_dai;
2801 /* Adapt stream for codec2codec links */
2802 int cpu_capture = rtd->dai_link->params ?
2803 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2804 int cpu_playback = rtd->dai_link->params ?
2805 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2807 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2808 if (rtd->num_cpus == 1) {
2809 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2810 } else if (rtd->num_cpus == rtd->num_codecs) {
2811 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2813 dev_err(rtd->card->dev,
2814 "N cpus to M codecs link is not supported yet\n");
2818 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2819 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2821 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2822 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2827 if (rtd->dai_link->playback_only) {
2832 if (rtd->dai_link->capture_only) {
2840 static int soc_create_pcm(struct snd_pcm **pcm,
2841 struct snd_soc_pcm_runtime *rtd,
2842 int playback, int capture, int num)
2847 /* create the PCM */
2848 if (rtd->dai_link->params) {
2849 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2850 rtd->dai_link->stream_name);
2852 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2853 playback, capture, pcm);
2854 } else if (rtd->dai_link->no_pcm) {
2855 snprintf(new_name, sizeof(new_name), "(%s)",
2856 rtd->dai_link->stream_name);
2858 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2859 playback, capture, pcm);
2861 if (rtd->dai_link->dynamic)
2862 snprintf(new_name, sizeof(new_name), "%s (*)",
2863 rtd->dai_link->stream_name);
2865 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2866 rtd->dai_link->stream_name,
2867 soc_codec_dai_name(rtd), num);
2869 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2873 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2874 new_name, rtd->dai_link->name, ret);
2877 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2882 /* create a new pcm */
2883 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2885 struct snd_soc_component *component;
2886 struct snd_pcm *pcm;
2887 int ret = 0, playback = 0, capture = 0;
2890 ret = soc_get_playback_capture(rtd, &playback, &capture);
2894 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2898 /* DAPM dai link stream work */
2899 if (rtd->dai_link->params)
2900 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2902 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2905 pcm->nonatomic = rtd->dai_link->nonatomic;
2906 pcm->private_data = rtd;
2908 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2910 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2912 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2916 /* ASoC PCM operations */
2917 if (rtd->dai_link->dynamic) {
2918 rtd->ops.open = dpcm_fe_dai_open;
2919 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2920 rtd->ops.prepare = dpcm_fe_dai_prepare;
2921 rtd->ops.trigger = dpcm_fe_dai_trigger;
2922 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2923 rtd->ops.close = dpcm_fe_dai_close;
2924 rtd->ops.pointer = soc_pcm_pointer;
2926 rtd->ops.open = soc_pcm_open;
2927 rtd->ops.hw_params = soc_pcm_hw_params;
2928 rtd->ops.prepare = soc_pcm_prepare;
2929 rtd->ops.trigger = soc_pcm_trigger;
2930 rtd->ops.hw_free = soc_pcm_hw_free;
2931 rtd->ops.close = soc_pcm_close;
2932 rtd->ops.pointer = soc_pcm_pointer;
2935 for_each_rtd_components(rtd, i, component) {
2936 const struct snd_soc_component_driver *drv = component->driver;
2939 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2941 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2943 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2945 rtd->ops.page = snd_soc_pcm_component_page;
2947 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2949 rtd->ops.ack = snd_soc_pcm_component_ack;
2953 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2956 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2958 ret = snd_soc_pcm_component_new(rtd);
2962 pcm->no_device_suspend = true;
2964 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2965 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2969 /* is the current PCM operation for this FE ? */
2970 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2972 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2976 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2978 /* is the current PCM operation for this BE ? */
2979 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2980 struct snd_soc_pcm_runtime *be, int stream)
2982 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2983 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2984 be->dpcm[stream].runtime_update))
2988 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2990 /* get the substream for this BE */
2991 struct snd_pcm_substream *
2992 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2994 return be->pcm->streams[stream].substream;
2996 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2998 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2999 struct snd_soc_pcm_runtime *be,
3001 const enum snd_soc_dpcm_state *states,
3004 struct snd_soc_dpcm *dpcm;
3009 for_each_dpcm_fe(be, stream, dpcm) {
3014 state = dpcm->fe->dpcm[stream].state;
3015 for (i = 0; i < num_states; i++) {
3016 if (state == states[i]) {
3023 /* it's safe to do this BE DAI */
3028 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3029 * are not running, paused or suspended for the specified stream direction.
3031 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3032 struct snd_soc_pcm_runtime *be, int stream)
3034 const enum snd_soc_dpcm_state state[] = {
3035 SND_SOC_DPCM_STATE_START,
3036 SND_SOC_DPCM_STATE_PAUSED,
3037 SND_SOC_DPCM_STATE_SUSPEND,
3040 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3042 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3045 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3046 * running, paused or suspended for the specified stream direction.
3048 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3049 struct snd_soc_pcm_runtime *be, int stream)
3051 const enum snd_soc_dpcm_state state[] = {
3052 SND_SOC_DPCM_STATE_START,
3053 SND_SOC_DPCM_STATE_PAUSED,
3054 SND_SOC_DPCM_STATE_SUSPEND,
3055 SND_SOC_DPCM_STATE_PREPARE,
3058 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3060 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);