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.
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 #define DPCM_MAX_BE_USERS 8
32 #ifdef CONFIG_DEBUG_FS
33 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
36 case SND_SOC_DPCM_STATE_NEW:
38 case SND_SOC_DPCM_STATE_OPEN:
40 case SND_SOC_DPCM_STATE_HW_PARAMS:
42 case SND_SOC_DPCM_STATE_PREPARE:
44 case SND_SOC_DPCM_STATE_START:
46 case SND_SOC_DPCM_STATE_STOP:
48 case SND_SOC_DPCM_STATE_SUSPEND:
50 case SND_SOC_DPCM_STATE_PAUSED:
52 case SND_SOC_DPCM_STATE_HW_FREE:
54 case SND_SOC_DPCM_STATE_CLOSE:
61 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
62 int stream, char *buf, size_t size)
64 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
65 struct snd_soc_dpcm *dpcm;
70 offset += scnprintf(buf + offset, size - offset,
71 "[%s - %s]\n", fe->dai_link->name,
72 stream ? "Capture" : "Playback");
74 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
75 dpcm_state_string(fe->dpcm[stream].state));
77 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
78 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
79 offset += scnprintf(buf + offset, size - offset,
81 "Format = %s, Channels = %d, Rate = %d\n",
82 snd_pcm_format_name(params_format(params)),
83 params_channels(params),
87 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
89 if (list_empty(&fe->dpcm[stream].be_clients)) {
90 offset += scnprintf(buf + offset, size - offset,
91 " No active DSP links\n");
95 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
96 for_each_dpcm_be(fe, stream, dpcm) {
97 struct snd_soc_pcm_runtime *be = dpcm->be;
98 params = &dpcm->hw_params;
100 offset += scnprintf(buf + offset, size - offset,
101 "- %s\n", be->dai_link->name);
103 offset += scnprintf(buf + offset, size - offset,
105 dpcm_state_string(be->dpcm[stream].state));
107 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
108 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
109 offset += scnprintf(buf + offset, size - offset,
111 "Format = %s, Channels = %d, Rate = %d\n",
112 snd_pcm_format_name(params_format(params)),
113 params_channels(params),
114 params_rate(params));
116 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
121 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
122 size_t count, loff_t *ppos)
124 struct snd_soc_pcm_runtime *fe = file->private_data;
125 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
129 if (fe->num_cpus > 1) {
131 "%s doesn't support Multi CPU yet\n", __func__);
135 buf = kmalloc(out_count, GFP_KERNEL);
139 for_each_pcm_streams(stream)
140 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
141 offset += dpcm_show_state(fe, stream,
145 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
151 static const struct file_operations dpcm_state_fops = {
153 .read = dpcm_state_read_file,
154 .llseek = default_llseek,
157 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
162 if (!rtd->dai_link->dynamic)
165 if (!rtd->card->debugfs_card_root)
168 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
169 rtd->card->debugfs_card_root);
171 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
172 rtd, &dpcm_state_fops);
175 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
179 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
180 stream ? "capture" : "playback");
182 dpcm->debugfs_state = debugfs_create_dir(
183 name, dpcm->fe->debugfs_dpcm_root);
184 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
190 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
192 debugfs_remove_recursive(dpcm->debugfs_state);
196 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
201 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
207 * snd_soc_runtime_action() - Increment/Decrement active count for
208 * PCM runtime components
209 * @rtd: ASoC PCM runtime that is activated
210 * @stream: Direction of the PCM stream
212 * Increments/Decrements the active count for all the DAIs and components
213 * attached to a PCM runtime.
214 * Should typically be called when a stream is opened.
216 * Must be called with the rtd->card->pcm_mutex being held
218 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
219 int stream, int action)
221 struct snd_soc_dai *dai;
224 lockdep_assert_held(&rtd->card->pcm_mutex);
226 for_each_rtd_dais(rtd, i, dai)
227 snd_soc_dai_action(dai, stream, action);
229 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
232 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
233 * @rtd: The ASoC PCM runtime that should be checked.
235 * This function checks whether the power down delay should be ignored for a
236 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
237 * been configured to ignore the delay, or if none of the components benefits
238 * from having the delay.
240 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
242 struct snd_soc_component *component;
246 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
249 for_each_rtd_components(rtd, i, component)
250 ignore &= !component->driver->use_pmdown_time;
256 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
257 * @substream: the pcm substream
258 * @hw: the hardware parameters
260 * Sets the substream runtime hardware parameters.
262 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
263 const struct snd_pcm_hardware *hw)
265 struct snd_pcm_runtime *runtime = substream->runtime;
266 runtime->hw.info = hw->info;
267 runtime->hw.formats = hw->formats;
268 runtime->hw.period_bytes_min = hw->period_bytes_min;
269 runtime->hw.period_bytes_max = hw->period_bytes_max;
270 runtime->hw.periods_min = hw->periods_min;
271 runtime->hw.periods_max = hw->periods_max;
272 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
273 runtime->hw.fifo_size = hw->fifo_size;
276 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
278 /* DPCM stream event, send event to FE and all active BEs. */
279 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
282 struct snd_soc_dpcm *dpcm;
284 for_each_dpcm_be(fe, dir, dpcm) {
286 struct snd_soc_pcm_runtime *be = dpcm->be;
288 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
289 be->dai_link->name, event, dir);
291 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
292 (be->dpcm[dir].users >= 1))
295 snd_soc_dapm_stream_event(be, dir, event);
298 snd_soc_dapm_stream_event(fe, dir, event);
303 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
304 struct snd_soc_dai *soc_dai)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
310 rtd->dai_link->symmetric_rates)) {
311 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
314 ret = snd_pcm_hw_constraint_single(substream->runtime,
315 SNDRV_PCM_HW_PARAM_RATE,
318 dev_err(soc_dai->dev,
319 "ASoC: Unable to apply rate constraint: %d\n",
325 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
326 rtd->dai_link->symmetric_channels)) {
327 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
330 ret = snd_pcm_hw_constraint_single(substream->runtime,
331 SNDRV_PCM_HW_PARAM_CHANNELS,
334 dev_err(soc_dai->dev,
335 "ASoC: Unable to apply channel symmetry constraint: %d\n",
341 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
342 rtd->dai_link->symmetric_samplebits)) {
343 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
344 soc_dai->sample_bits);
346 ret = snd_pcm_hw_constraint_single(substream->runtime,
347 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
348 soc_dai->sample_bits);
350 dev_err(soc_dai->dev,
351 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
360 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
361 struct snd_pcm_hw_params *params)
363 struct snd_soc_pcm_runtime *rtd = substream->private_data;
364 struct snd_soc_dai *dai;
365 struct snd_soc_dai *cpu_dai;
366 unsigned int rate, channels, sample_bits, symmetry, i;
368 rate = params_rate(params);
369 channels = params_channels(params);
370 sample_bits = snd_pcm_format_physical_width(params_format(params));
372 /* reject unmatched parameters when applying symmetry */
373 symmetry = rtd->dai_link->symmetric_rates;
375 for_each_rtd_cpu_dais(rtd, i, dai)
376 symmetry |= dai->driver->symmetric_rates;
379 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
380 if (cpu_dai->rate && cpu_dai->rate != rate) {
381 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
382 cpu_dai->rate, rate);
388 symmetry = rtd->dai_link->symmetric_channels;
390 for_each_rtd_dais(rtd, i, dai)
391 symmetry |= dai->driver->symmetric_channels;
394 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
395 if (cpu_dai->channels &&
396 cpu_dai->channels != channels) {
397 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
398 cpu_dai->channels, channels);
404 symmetry = rtd->dai_link->symmetric_samplebits;
406 for_each_rtd_dais(rtd, i, dai)
407 symmetry |= dai->driver->symmetric_samplebits;
410 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
411 if (cpu_dai->sample_bits &&
412 cpu_dai->sample_bits != sample_bits) {
413 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
414 cpu_dai->sample_bits, sample_bits);
423 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
425 struct snd_soc_pcm_runtime *rtd = substream->private_data;
426 struct snd_soc_dai_link *link = rtd->dai_link;
427 struct snd_soc_dai *dai;
428 unsigned int symmetry, i;
430 symmetry = link->symmetric_rates ||
431 link->symmetric_channels ||
432 link->symmetric_samplebits;
434 for_each_rtd_dais(rtd, i, dai)
435 symmetry = symmetry ||
436 dai->driver->symmetric_rates ||
437 dai->driver->symmetric_channels ||
438 dai->driver->symmetric_samplebits;
443 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
445 struct snd_soc_pcm_runtime *rtd = substream->private_data;
451 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
453 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
457 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
459 struct snd_soc_pcm_runtime *rtd = substream->private_data;
460 struct snd_soc_dai *cpu_dai;
461 struct snd_soc_dai *codec_dai;
462 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
463 int stream = substream->stream;
465 unsigned int bits = 0, cpu_bits = 0;
467 for_each_rtd_codec_dais(rtd, i, codec_dai) {
468 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
470 if (pcm_codec->sig_bits == 0) {
474 bits = max(pcm_codec->sig_bits, bits);
477 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
478 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
480 if (pcm_cpu->sig_bits == 0) {
484 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
487 soc_pcm_set_msb(substream, bits);
488 soc_pcm_set_msb(substream, cpu_bits);
492 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
493 * @rtd: ASoC PCM runtime
494 * @hw: PCM hardware parameters (output)
495 * @stream: Direction of the PCM stream
497 * Calculates the subset of stream parameters supported by all DAIs
498 * associated with the PCM stream.
500 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
501 struct snd_pcm_hardware *hw, int stream)
503 struct snd_soc_dai *codec_dai;
504 struct snd_soc_dai *cpu_dai;
505 struct snd_soc_pcm_stream *codec_stream;
506 struct snd_soc_pcm_stream *cpu_stream;
507 unsigned int chan_min = 0, chan_max = UINT_MAX;
508 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
509 unsigned int rate_min = 0, rate_max = UINT_MAX;
510 unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
511 unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
512 u64 formats = ULLONG_MAX;
515 /* first calculate min/max only for CPUs in the DAI link */
516 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
519 * Skip CPUs which don't support the current stream type.
520 * Otherwise, since the rate, channel, and format values will
521 * zero in that case, we would have no usable settings left,
522 * causing the resulting setup to fail.
524 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
527 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
529 cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
530 cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
531 cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
532 cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
533 formats &= cpu_stream->formats;
534 cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
538 /* second calculate min/max only for CODECs in the DAI link */
539 for_each_rtd_codec_dais(rtd, i, codec_dai) {
542 * Skip CODECs which don't support the current stream type.
543 * Otherwise, since the rate, channel, and format values will
544 * zero in that case, we would have no usable settings left,
545 * causing the resulting setup to fail.
547 if (!snd_soc_dai_stream_valid(codec_dai, stream))
550 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
552 chan_min = max(chan_min, codec_stream->channels_min);
553 chan_max = min(chan_max, codec_stream->channels_max);
554 rate_min = max(rate_min, codec_stream->rate_min);
555 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
556 formats &= codec_stream->formats;
557 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
560 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
561 if (!chan_min || !cpu_chan_min)
565 * chan min/max cannot be enforced if there are multiple CODEC DAIs
566 * connected to CPU DAI(s), use CPU DAI's directly and let
567 * channel allocation be fixed up later
569 if (rtd->num_codecs > 1) {
570 chan_min = cpu_chan_min;
571 chan_max = cpu_chan_max;
574 /* finally find a intersection between CODECs and CPUs */
575 hw->channels_min = max(chan_min, cpu_chan_min);
576 hw->channels_max = min(chan_max, cpu_chan_max);
577 hw->formats = formats;
578 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
580 snd_pcm_hw_limit_rates(hw);
582 hw->rate_min = max(hw->rate_min, cpu_rate_min);
583 hw->rate_min = max(hw->rate_min, rate_min);
584 hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
585 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
589 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
591 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
593 struct snd_pcm_hardware *hw = &substream->runtime->hw;
594 struct snd_soc_pcm_runtime *rtd = substream->private_data;
595 u64 formats = hw->formats;
598 * At least one CPU and one CODEC should match. Otherwise, we should
599 * have bailed out on a higher level, since there would be no CPU or
600 * CODEC to support the transfer direction in that case.
602 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
605 hw->formats &= formats;
608 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
610 struct snd_soc_pcm_runtime *rtd = substream->private_data;
611 struct snd_soc_component *last = NULL;
612 struct snd_soc_component *component;
615 for_each_rtd_components(rtd, i, component) {
618 ret = snd_soc_component_module_get_when_open(component);
620 dev_err(component->dev,
621 "ASoC: can't get module %s\n",
626 ret = snd_soc_component_open(component, substream);
628 snd_soc_component_module_put_when_close(component);
629 dev_err(component->dev,
630 "ASoC: can't open component %s: %d\n",
631 component->name, ret);
637 /* rollback on error */
638 for_each_rtd_components(rtd, i, component) {
639 if (component == last)
642 snd_soc_component_close(component, substream);
643 snd_soc_component_module_put_when_close(component);
650 static int soc_pcm_components_close(struct snd_pcm_substream *substream)
652 struct snd_soc_pcm_runtime *rtd = substream->private_data;
653 struct snd_soc_component *component;
656 for_each_rtd_components(rtd, i, component) {
657 r = snd_soc_component_close(component, substream);
659 ret = r; /* use last ret */
661 snd_soc_component_module_put_when_close(component);
668 * Called by ALSA when a PCM substream is closed. Private data can be
669 * freed here. The cpu DAI, codec DAI, machine and components are also
672 static int soc_pcm_close(struct snd_pcm_substream *substream)
674 struct snd_soc_pcm_runtime *rtd = substream->private_data;
675 struct snd_soc_component *component;
676 struct snd_soc_dai *dai;
679 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
681 snd_soc_runtime_deactivate(rtd, substream->stream);
683 for_each_rtd_dais(rtd, i, dai)
684 snd_soc_dai_shutdown(dai, substream);
686 snd_soc_link_shutdown(substream);
688 soc_pcm_components_close(substream);
690 snd_soc_dapm_stream_stop(rtd, substream->stream);
692 mutex_unlock(&rtd->card->pcm_mutex);
694 for_each_rtd_components(rtd, i, component) {
695 pm_runtime_mark_last_busy(component->dev);
696 pm_runtime_put_autosuspend(component->dev);
699 for_each_rtd_components(rtd, i, component)
700 if (!snd_soc_component_active(component))
701 pinctrl_pm_select_sleep_state(component->dev);
707 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
708 * then initialized and any private data can be allocated. This also calls
709 * startup for the cpu DAI, component, machine and codec DAI.
711 static int soc_pcm_open(struct snd_pcm_substream *substream)
713 struct snd_soc_pcm_runtime *rtd = substream->private_data;
714 struct snd_pcm_runtime *runtime = substream->runtime;
715 struct snd_soc_component *component;
716 struct snd_soc_dai *dai;
717 const char *codec_dai_name = "multicodec";
718 const char *cpu_dai_name = "multicpu";
721 for_each_rtd_components(rtd, i, component)
722 pinctrl_pm_select_default_state(component->dev);
724 for_each_rtd_components(rtd, i, component)
725 pm_runtime_get_sync(component->dev);
727 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
729 ret = soc_pcm_components_open(substream);
733 ret = snd_soc_link_startup(substream);
735 goto rtd_startup_err;
737 /* startup the audio subsystem */
738 for_each_rtd_dais(rtd, i, dai) {
739 ret = snd_soc_dai_startup(dai, substream);
742 "ASoC: can't open DAI %s: %d\n",
747 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
753 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
754 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
757 /* Check that the codec and cpu DAIs are compatible */
758 soc_pcm_init_runtime_hw(substream);
760 if (rtd->num_codecs == 1)
761 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
763 if (rtd->num_cpus == 1)
764 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
766 if (soc_pcm_has_symmetry(substream))
767 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
770 if (!runtime->hw.rates) {
771 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
772 codec_dai_name, cpu_dai_name);
775 if (!runtime->hw.formats) {
776 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
777 codec_dai_name, cpu_dai_name);
780 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
781 runtime->hw.channels_min > runtime->hw.channels_max) {
782 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
783 codec_dai_name, cpu_dai_name);
787 soc_pcm_apply_msb(substream);
789 /* Symmetry only applies if we've already got an active stream. */
790 for_each_rtd_dais(rtd, i, dai) {
791 if (snd_soc_dai_active(dai)) {
792 ret = soc_pcm_apply_symmetry(substream, dai);
798 pr_debug("ASoC: %s <-> %s info:\n",
799 codec_dai_name, cpu_dai_name);
800 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
801 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
802 runtime->hw.channels_max);
803 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
804 runtime->hw.rate_max);
808 snd_soc_runtime_activate(rtd, substream->stream);
810 mutex_unlock(&rtd->card->pcm_mutex);
814 for_each_rtd_dais(rtd, i, dai)
815 snd_soc_dai_shutdown(dai, substream);
817 snd_soc_link_shutdown(substream);
819 soc_pcm_components_close(substream);
821 mutex_unlock(&rtd->card->pcm_mutex);
823 for_each_rtd_components(rtd, i, component) {
824 pm_runtime_mark_last_busy(component->dev);
825 pm_runtime_put_autosuspend(component->dev);
828 for_each_rtd_components(rtd, i, component)
829 if (!snd_soc_component_active(component))
830 pinctrl_pm_select_sleep_state(component->dev);
835 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
838 * Currently nothing to do for c2c links
839 * Since c2c links are internal nodes in the DAPM graph and
840 * don't interface with the outside world or application layer
841 * we don't have to do any special handling on close.
846 * Called by ALSA when the PCM substream is prepared, can set format, sample
847 * rate, etc. This function is non atomic and can be called multiple times,
848 * it can refer to the runtime info.
850 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
852 struct snd_soc_pcm_runtime *rtd = substream->private_data;
853 struct snd_soc_component *component;
854 struct snd_soc_dai *dai;
857 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
859 ret = snd_soc_link_prepare(substream);
863 for_each_rtd_components(rtd, i, component) {
864 ret = snd_soc_component_prepare(component, substream);
866 dev_err(component->dev,
867 "ASoC: platform prepare error: %d\n", ret);
872 ret = snd_soc_pcm_dai_prepare(substream);
874 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
878 /* cancel any delayed stream shutdown that is pending */
879 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
882 cancel_delayed_work(&rtd->delayed_work);
885 snd_soc_dapm_stream_event(rtd, substream->stream,
886 SND_SOC_DAPM_STREAM_START);
888 for_each_rtd_dais(rtd, i, dai)
889 snd_soc_dai_digital_mute(dai, 0, substream->stream);
892 mutex_unlock(&rtd->card->pcm_mutex);
896 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
899 struct snd_interval *interval;
900 int channels = hweight_long(mask);
902 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
903 interval->min = channels;
904 interval->max = channels;
907 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
908 struct snd_soc_component *last)
910 struct snd_soc_pcm_runtime *rtd = substream->private_data;
911 struct snd_soc_component *component;
914 for_each_rtd_components(rtd, i, component) {
915 if (component == last)
918 r = snd_soc_component_hw_free(component, substream);
920 ret = r; /* use last ret */
927 * Called by ALSA when the hardware params are set by application. This
928 * function can also be called multiple times and can allocate buffers
929 * (using snd_pcm_lib_* ). It's non-atomic.
931 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
932 struct snd_pcm_hw_params *params)
934 struct snd_soc_pcm_runtime *rtd = substream->private_data;
935 struct snd_soc_component *component;
936 struct snd_soc_dai *cpu_dai;
937 struct snd_soc_dai *codec_dai;
940 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
942 ret = soc_pcm_params_symmetry(substream, params);
946 ret = snd_soc_link_hw_params(substream, params);
950 for_each_rtd_codec_dais(rtd, i, codec_dai) {
951 struct snd_pcm_hw_params codec_params;
954 * Skip CODECs which don't support the current stream type,
955 * the idea being that if a CODEC is not used for the currently
956 * set up transfer direction, it should not need to be
957 * configured, especially since the configuration used might
958 * not even be supported by that CODEC. There may be cases
959 * however where a CODEC needs to be set up although it is
960 * actually not being used for the transfer, e.g. if a
961 * capture-only CODEC is acting as an LRCLK and/or BCLK master
962 * for the DAI link including a playback-only CODEC.
963 * If this becomes necessary, we will have to augment the
964 * machine driver setup with information on how to act, so
965 * we can do the right thing here.
967 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
970 /* copy params for each codec */
971 codec_params = *params;
973 /* fixup params based on TDM slot masks */
974 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
976 soc_pcm_codec_params_fixup(&codec_params,
979 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
981 soc_pcm_codec_params_fixup(&codec_params,
984 ret = snd_soc_dai_hw_params(codec_dai, substream,
989 codec_dai->rate = params_rate(&codec_params);
990 codec_dai->channels = params_channels(&codec_params);
991 codec_dai->sample_bits = snd_pcm_format_physical_width(
992 params_format(&codec_params));
994 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
997 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
999 * Skip CPUs which don't support the current stream
1000 * type. See soc_pcm_init_runtime_hw() for more details
1002 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1005 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1009 /* store the parameters for each DAI */
1010 cpu_dai->rate = params_rate(params);
1011 cpu_dai->channels = params_channels(params);
1012 cpu_dai->sample_bits =
1013 snd_pcm_format_physical_width(params_format(params));
1015 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1018 for_each_rtd_components(rtd, i, component) {
1019 ret = snd_soc_component_hw_params(component, substream, params);
1021 dev_err(component->dev,
1022 "ASoC: %s hw params failed: %d\n",
1023 component->name, ret);
1030 mutex_unlock(&rtd->card->pcm_mutex);
1034 soc_pcm_components_hw_free(substream, component);
1039 for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
1040 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1043 snd_soc_dai_hw_free(cpu_dai, substream);
1047 i = rtd->num_codecs;
1050 for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
1051 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1054 snd_soc_dai_hw_free(codec_dai, substream);
1055 codec_dai->rate = 0;
1058 snd_soc_link_hw_free(substream);
1060 mutex_unlock(&rtd->card->pcm_mutex);
1065 * Frees resources allocated by hw_params, can be called multiple times
1067 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1069 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1070 struct snd_soc_dai *dai;
1073 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1075 /* clear the corresponding DAIs parameters when going to be inactive */
1076 for_each_rtd_dais(rtd, i, dai) {
1077 int active = snd_soc_dai_stream_active(dai, substream->stream);
1079 if (snd_soc_dai_active(dai) == 1) {
1082 dai->sample_bits = 0;
1086 snd_soc_dai_digital_mute(dai, 1, substream->stream);
1089 /* free any machine hw params */
1090 snd_soc_link_hw_free(substream);
1092 /* free any component resources */
1093 soc_pcm_components_hw_free(substream, NULL);
1095 /* now free hw params for the DAIs */
1096 for_each_rtd_dais(rtd, i, dai) {
1097 if (!snd_soc_dai_stream_valid(dai, substream->stream))
1100 snd_soc_dai_hw_free(dai, substream);
1103 mutex_unlock(&rtd->card->pcm_mutex);
1107 static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1109 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1110 struct snd_soc_component *component;
1113 ret = snd_soc_link_trigger(substream, cmd);
1117 for_each_rtd_components(rtd, i, component) {
1118 ret = snd_soc_component_trigger(component, substream, cmd);
1123 return snd_soc_pcm_dai_trigger(substream, cmd);
1126 static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1129 struct snd_soc_component *component;
1132 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1136 for_each_rtd_components(rtd, i, component) {
1137 ret = snd_soc_component_trigger(component, substream, cmd);
1142 ret = snd_soc_link_trigger(substream, cmd);
1149 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1154 case SNDRV_PCM_TRIGGER_START:
1155 case SNDRV_PCM_TRIGGER_RESUME:
1156 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1157 ret = soc_pcm_trigger_start(substream, cmd);
1159 case SNDRV_PCM_TRIGGER_STOP:
1160 case SNDRV_PCM_TRIGGER_SUSPEND:
1161 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1162 ret = soc_pcm_trigger_stop(substream, cmd);
1172 * soc level wrapper for pointer callback
1173 * If cpu_dai, codec_dai, component driver has the delay callback, then
1174 * the runtime->delay will be updated accordingly.
1176 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1179 struct snd_soc_dai *cpu_dai;
1180 struct snd_soc_dai *codec_dai;
1181 struct snd_pcm_runtime *runtime = substream->runtime;
1182 snd_pcm_uframes_t offset = 0;
1183 snd_pcm_sframes_t delay = 0;
1184 snd_pcm_sframes_t codec_delay = 0;
1185 snd_pcm_sframes_t cpu_delay = 0;
1188 /* clearing the previous total delay */
1191 offset = snd_soc_pcm_component_pointer(substream);
1193 /* base delay if assigned in pointer callback */
1194 delay = runtime->delay;
1196 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1197 cpu_delay = max(cpu_delay,
1198 snd_soc_dai_delay(cpu_dai, substream));
1202 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1203 codec_delay = max(codec_delay,
1204 snd_soc_dai_delay(codec_dai, substream));
1206 delay += codec_delay;
1208 runtime->delay = delay;
1213 /* connect a FE and BE */
1214 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1215 struct snd_soc_pcm_runtime *be, int stream)
1217 struct snd_soc_dpcm *dpcm;
1218 unsigned long flags;
1220 /* only add new dpcms */
1221 for_each_dpcm_be(fe, stream, dpcm) {
1222 if (dpcm->be == be && dpcm->fe == fe)
1226 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1232 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1233 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1234 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1235 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1236 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1237 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1239 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1240 stream ? "capture" : "playback", fe->dai_link->name,
1241 stream ? "<-" : "->", be->dai_link->name);
1243 dpcm_create_debugfs_state(dpcm, stream);
1248 /* reparent a BE onto another FE */
1249 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1250 struct snd_soc_pcm_runtime *be, int stream)
1252 struct snd_soc_dpcm *dpcm;
1253 struct snd_pcm_substream *fe_substream, *be_substream;
1255 /* reparent if BE is connected to other FEs */
1256 if (!be->dpcm[stream].users)
1259 be_substream = snd_soc_dpcm_get_substream(be, stream);
1261 for_each_dpcm_fe(be, stream, dpcm) {
1265 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1266 stream ? "capture" : "playback",
1267 dpcm->fe->dai_link->name,
1268 stream ? "<-" : "->", dpcm->be->dai_link->name);
1270 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1271 be_substream->runtime = fe_substream->runtime;
1276 /* disconnect a BE and FE */
1277 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1279 struct snd_soc_dpcm *dpcm, *d;
1280 unsigned long flags;
1282 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1283 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1284 stream ? "capture" : "playback",
1285 dpcm->be->dai_link->name);
1287 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1290 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1291 stream ? "capture" : "playback", fe->dai_link->name,
1292 stream ? "<-" : "->", dpcm->be->dai_link->name);
1294 /* BEs still alive need new FE */
1295 dpcm_be_reparent(fe, dpcm->be, stream);
1297 dpcm_remove_debugfs_state(dpcm);
1299 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1300 list_del(&dpcm->list_be);
1301 list_del(&dpcm->list_fe);
1302 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1307 /* get BE for DAI widget and stream */
1308 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1309 struct snd_soc_dapm_widget *widget, int stream)
1311 struct snd_soc_pcm_runtime *be;
1312 struct snd_soc_dapm_widget *w;
1313 struct snd_soc_dai *dai;
1316 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1318 for_each_card_rtds(card, be) {
1320 if (!be->dai_link->no_pcm)
1323 for_each_rtd_dais(be, i, dai) {
1324 w = snd_soc_dai_get_widget(dai, stream);
1326 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1327 w ? w->name : "(not set)");
1334 /* Widget provided is not a BE */
1338 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1339 struct snd_soc_dapm_widget *widget)
1341 struct snd_soc_dapm_widget *w;
1344 for_each_dapm_widgets(list, i, w)
1351 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1352 enum snd_soc_dapm_direction dir)
1354 struct snd_soc_card *card = widget->dapm->card;
1355 struct snd_soc_pcm_runtime *rtd;
1358 /* adjust dir to stream */
1359 if (dir == SND_SOC_DAPM_DIR_OUT)
1360 stream = SNDRV_PCM_STREAM_PLAYBACK;
1362 stream = SNDRV_PCM_STREAM_CAPTURE;
1364 rtd = dpcm_get_be(card, widget, stream);
1371 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1372 int stream, struct snd_soc_dapm_widget_list **list)
1374 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1377 if (fe->num_cpus > 1) {
1379 "%s doesn't support Multi CPU yet\n", __func__);
1383 /* get number of valid DAI paths and their widgets */
1384 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1385 dpcm_end_walk_at_be);
1387 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1388 stream ? "capture" : "playback");
1393 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1395 snd_soc_dapm_dai_free_widgets(list);
1398 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1399 struct snd_soc_dapm_widget_list *list)
1401 struct snd_soc_dapm_widget *widget;
1402 struct snd_soc_dai *dai;
1405 /* is there a valid DAI widget for this BE */
1406 for_each_rtd_dais(dpcm->be, i, dai) {
1407 widget = snd_soc_dai_get_widget(dai, stream);
1410 * The BE is pruned only if none of the dai
1411 * widgets are in the active list.
1413 if (widget && widget_in_list(list, widget))
1420 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1421 struct snd_soc_dapm_widget_list **list_)
1423 struct snd_soc_dpcm *dpcm;
1426 /* Destroy any old FE <--> BE connections */
1427 for_each_dpcm_be(fe, stream, dpcm) {
1428 if (dpcm_be_is_active(dpcm, stream, *list_))
1431 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1432 stream ? "capture" : "playback",
1433 dpcm->be->dai_link->name, fe->dai_link->name);
1434 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1435 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1439 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1443 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1444 struct snd_soc_dapm_widget_list **list_)
1446 struct snd_soc_card *card = fe->card;
1447 struct snd_soc_dapm_widget_list *list = *list_;
1448 struct snd_soc_pcm_runtime *be;
1449 struct snd_soc_dapm_widget *widget;
1450 int i, new = 0, err;
1452 /* Create any new FE <--> BE connections */
1453 for_each_dapm_widgets(list, i, widget) {
1455 switch (widget->id) {
1456 case snd_soc_dapm_dai_in:
1457 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1460 case snd_soc_dapm_dai_out:
1461 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1468 /* is there a valid BE rtd for this widget */
1469 be = dpcm_get_be(card, widget, stream);
1471 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1476 /* don't connect if FE is not running */
1477 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1480 /* newly connected FE and BE */
1481 err = dpcm_be_connect(fe, be, stream);
1483 dev_err(fe->dev, "ASoC: can't connect %s\n",
1486 } else if (err == 0) /* already connected */
1490 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1494 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1499 * Find the corresponding BE DAIs that source or sink audio to this
1502 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1503 int stream, struct snd_soc_dapm_widget_list **list, int new)
1506 return dpcm_add_paths(fe, stream, list);
1508 return dpcm_prune_paths(fe, stream, list);
1511 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1513 struct snd_soc_dpcm *dpcm;
1514 unsigned long flags;
1516 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1517 for_each_dpcm_be(fe, stream, dpcm)
1518 dpcm->be->dpcm[stream].runtime_update =
1519 SND_SOC_DPCM_UPDATE_NO;
1520 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1523 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1526 struct snd_soc_dpcm *dpcm;
1528 /* disable any enabled and non active backends */
1529 for_each_dpcm_be(fe, stream, dpcm) {
1531 struct snd_soc_pcm_runtime *be = dpcm->be;
1532 struct snd_pcm_substream *be_substream =
1533 snd_soc_dpcm_get_substream(be, stream);
1535 if (be->dpcm[stream].users == 0)
1536 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1537 stream ? "capture" : "playback",
1538 be->dpcm[stream].state);
1540 if (--be->dpcm[stream].users != 0)
1543 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1546 soc_pcm_close(be_substream);
1547 be_substream->runtime = NULL;
1548 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1552 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1554 struct snd_soc_dpcm *dpcm;
1557 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1558 for_each_dpcm_be(fe, stream, dpcm) {
1560 struct snd_soc_pcm_runtime *be = dpcm->be;
1561 struct snd_pcm_substream *be_substream =
1562 snd_soc_dpcm_get_substream(be, stream);
1564 if (!be_substream) {
1565 dev_err(be->dev, "ASoC: no backend %s stream\n",
1566 stream ? "capture" : "playback");
1570 /* is this op for this BE ? */
1571 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1574 /* first time the dpcm is open ? */
1575 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1576 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1577 stream ? "capture" : "playback",
1578 be->dpcm[stream].state);
1580 if (be->dpcm[stream].users++ != 0)
1583 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1584 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1587 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1588 stream ? "capture" : "playback", be->dai_link->name);
1590 be_substream->runtime = be->dpcm[stream].runtime;
1591 err = soc_pcm_open(be_substream);
1593 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1594 be->dpcm[stream].users--;
1595 if (be->dpcm[stream].users < 0)
1596 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1597 stream ? "capture" : "playback",
1598 be->dpcm[stream].state);
1600 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1604 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1611 /* disable any enabled and non active backends */
1612 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1613 struct snd_soc_pcm_runtime *be = dpcm->be;
1614 struct snd_pcm_substream *be_substream =
1615 snd_soc_dpcm_get_substream(be, stream);
1617 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1620 if (be->dpcm[stream].users == 0)
1621 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1622 stream ? "capture" : "playback",
1623 be->dpcm[stream].state);
1625 if (--be->dpcm[stream].users != 0)
1628 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1631 soc_pcm_close(be_substream);
1632 be_substream->runtime = NULL;
1633 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1639 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1640 struct snd_soc_pcm_stream *stream)
1642 runtime->hw.rate_min = stream->rate_min;
1643 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1644 runtime->hw.channels_min = stream->channels_min;
1645 runtime->hw.channels_max = stream->channels_max;
1646 if (runtime->hw.formats)
1647 runtime->hw.formats &= stream->formats;
1649 runtime->hw.formats = stream->formats;
1650 runtime->hw.rates = stream->rates;
1653 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1656 struct snd_soc_pcm_runtime *fe = substream->private_data;
1657 struct snd_soc_dpcm *dpcm;
1658 struct snd_soc_dai *dai;
1659 int stream = substream->stream;
1661 if (!fe->dai_link->dpcm_merged_format)
1665 * It returns merged BE codec format
1666 * if FE want to use it (= dpcm_merged_format)
1669 for_each_dpcm_be(fe, stream, dpcm) {
1670 struct snd_soc_pcm_runtime *be = dpcm->be;
1671 struct snd_soc_pcm_stream *codec_stream;
1674 for_each_rtd_codec_dais(be, i, dai) {
1676 * Skip CODECs which don't support the current stream
1677 * type. See soc_pcm_init_runtime_hw() for more details
1679 if (!snd_soc_dai_stream_valid(dai, stream))
1682 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1684 *formats &= codec_stream->formats;
1689 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1690 unsigned int *channels_min,
1691 unsigned int *channels_max)
1693 struct snd_soc_pcm_runtime *fe = substream->private_data;
1694 struct snd_soc_dpcm *dpcm;
1695 int stream = substream->stream;
1697 if (!fe->dai_link->dpcm_merged_chan)
1701 * It returns merged BE codec channel;
1702 * if FE want to use it (= dpcm_merged_chan)
1705 for_each_dpcm_be(fe, stream, dpcm) {
1706 struct snd_soc_pcm_runtime *be = dpcm->be;
1707 struct snd_soc_pcm_stream *codec_stream;
1708 struct snd_soc_pcm_stream *cpu_stream;
1709 struct snd_soc_dai *dai;
1712 for_each_rtd_cpu_dais(be, i, dai) {
1714 * Skip CPUs which don't support the current stream
1715 * type. See soc_pcm_init_runtime_hw() for more details
1717 if (!snd_soc_dai_stream_valid(dai, stream))
1720 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1722 *channels_min = max(*channels_min,
1723 cpu_stream->channels_min);
1724 *channels_max = min(*channels_max,
1725 cpu_stream->channels_max);
1729 * chan min/max cannot be enforced if there are multiple CODEC
1730 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1732 if (be->num_codecs == 1) {
1733 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1735 *channels_min = max(*channels_min,
1736 codec_stream->channels_min);
1737 *channels_max = min(*channels_max,
1738 codec_stream->channels_max);
1743 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1744 unsigned int *rates,
1745 unsigned int *rate_min,
1746 unsigned int *rate_max)
1748 struct snd_soc_pcm_runtime *fe = substream->private_data;
1749 struct snd_soc_dpcm *dpcm;
1750 int stream = substream->stream;
1752 if (!fe->dai_link->dpcm_merged_rate)
1756 * It returns merged BE codec channel;
1757 * if FE want to use it (= dpcm_merged_chan)
1760 for_each_dpcm_be(fe, stream, dpcm) {
1761 struct snd_soc_pcm_runtime *be = dpcm->be;
1762 struct snd_soc_pcm_stream *pcm;
1763 struct snd_soc_dai *dai;
1766 for_each_rtd_dais(be, i, dai) {
1768 * Skip DAIs which don't support the current stream
1769 * type. See soc_pcm_init_runtime_hw() for more details
1771 if (!snd_soc_dai_stream_valid(dai, stream))
1774 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1776 *rate_min = max(*rate_min, pcm->rate_min);
1777 *rate_max = min_not_zero(*rate_max, pcm->rate_max);
1778 *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
1783 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1785 struct snd_pcm_runtime *runtime = substream->runtime;
1786 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1787 struct snd_soc_dai *cpu_dai;
1790 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1792 * Skip CPUs which don't support the current stream
1793 * type. See soc_pcm_init_runtime_hw() for more details
1795 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1798 dpcm_init_runtime_hw(runtime,
1799 snd_soc_dai_get_pcm_stream(cpu_dai,
1800 substream->stream));
1803 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1804 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1805 &runtime->hw.channels_max);
1806 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1807 &runtime->hw.rate_min, &runtime->hw.rate_max);
1810 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1812 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1813 * for avoiding the race with trigger callback.
1814 * If the state is unset and a trigger is pending while the previous operation,
1815 * process the pending trigger action here.
1817 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1818 int stream, enum snd_soc_dpcm_update state)
1820 struct snd_pcm_substream *substream =
1821 snd_soc_dpcm_get_substream(fe, stream);
1823 snd_pcm_stream_lock_irq(substream);
1824 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1825 dpcm_fe_dai_do_trigger(substream,
1826 fe->dpcm[stream].trigger_pending - 1);
1827 fe->dpcm[stream].trigger_pending = 0;
1829 fe->dpcm[stream].runtime_update = state;
1830 snd_pcm_stream_unlock_irq(substream);
1833 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1836 struct snd_soc_dpcm *dpcm;
1837 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1838 struct snd_soc_dai *fe_cpu_dai;
1842 /* apply symmetry for FE */
1843 if (soc_pcm_has_symmetry(fe_substream))
1844 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1846 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1847 /* Symmetry only applies if we've got an active stream. */
1848 if (snd_soc_dai_active(fe_cpu_dai)) {
1849 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1855 /* apply symmetry for BE */
1856 for_each_dpcm_be(fe, stream, dpcm) {
1857 struct snd_soc_pcm_runtime *be = dpcm->be;
1858 struct snd_pcm_substream *be_substream =
1859 snd_soc_dpcm_get_substream(be, stream);
1860 struct snd_soc_pcm_runtime *rtd;
1861 struct snd_soc_dai *dai;
1864 /* A backend may not have the requested substream */
1868 rtd = be_substream->private_data;
1869 if (rtd->dai_link->be_hw_params_fixup)
1872 if (soc_pcm_has_symmetry(be_substream))
1873 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1875 /* Symmetry only applies if we've got an active stream. */
1876 for_each_rtd_dais(rtd, i, dai) {
1877 if (snd_soc_dai_active(dai)) {
1878 err = soc_pcm_apply_symmetry(fe_substream, dai);
1888 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1890 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1891 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1892 int stream = fe_substream->stream, ret = 0;
1894 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1896 ret = dpcm_be_dai_startup(fe, stream);
1898 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1902 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1904 /* start the DAI frontend */
1905 ret = soc_pcm_open(fe_substream);
1907 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1911 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1913 dpcm_set_fe_runtime(fe_substream);
1914 snd_pcm_limit_hw_rates(runtime);
1916 ret = dpcm_apply_symmetry(fe_substream, stream);
1918 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1923 dpcm_be_dai_startup_unwind(fe, stream);
1925 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1929 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1931 struct snd_soc_dpcm *dpcm;
1933 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1934 for_each_dpcm_be(fe, stream, dpcm) {
1936 struct snd_soc_pcm_runtime *be = dpcm->be;
1937 struct snd_pcm_substream *be_substream =
1938 snd_soc_dpcm_get_substream(be, stream);
1940 /* is this op for this BE ? */
1941 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1944 if (be->dpcm[stream].users == 0)
1945 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1946 stream ? "capture" : "playback",
1947 be->dpcm[stream].state);
1949 if (--be->dpcm[stream].users != 0)
1952 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1953 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1954 soc_pcm_hw_free(be_substream);
1955 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1958 dev_dbg(be->dev, "ASoC: close BE %s\n",
1959 be->dai_link->name);
1961 soc_pcm_close(be_substream);
1962 be_substream->runtime = NULL;
1964 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1969 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1971 struct snd_soc_pcm_runtime *fe = substream->private_data;
1972 int stream = substream->stream;
1974 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1976 /* shutdown the BEs */
1977 dpcm_be_dai_shutdown(fe, stream);
1979 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1981 /* now shutdown the frontend */
1982 soc_pcm_close(substream);
1984 /* run the stream event for each BE */
1985 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1987 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1988 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1992 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1994 struct snd_soc_dpcm *dpcm;
1996 /* only hw_params backends that are either sinks or sources
1997 * to this frontend DAI */
1998 for_each_dpcm_be(fe, stream, dpcm) {
2000 struct snd_soc_pcm_runtime *be = dpcm->be;
2001 struct snd_pcm_substream *be_substream =
2002 snd_soc_dpcm_get_substream(be, stream);
2004 /* is this op for this BE ? */
2005 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2008 /* only free hw when no longer used - check all FEs */
2009 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2012 /* do not free hw if this BE is used by other FE */
2013 if (be->dpcm[stream].users > 1)
2016 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2017 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2018 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2019 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2020 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2021 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2024 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2025 be->dai_link->name);
2027 soc_pcm_hw_free(be_substream);
2029 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2035 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2037 struct snd_soc_pcm_runtime *fe = substream->private_data;
2038 int err, stream = substream->stream;
2040 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2041 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2043 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2045 /* call hw_free on the frontend */
2046 err = soc_pcm_hw_free(substream);
2048 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2049 fe->dai_link->name);
2051 /* only hw_params backends that are either sinks or sources
2052 * to this frontend DAI */
2053 err = dpcm_be_dai_hw_free(fe, stream);
2055 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2056 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2058 mutex_unlock(&fe->card->mutex);
2062 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2064 struct snd_soc_dpcm *dpcm;
2067 for_each_dpcm_be(fe, stream, dpcm) {
2069 struct snd_soc_pcm_runtime *be = dpcm->be;
2070 struct snd_pcm_substream *be_substream =
2071 snd_soc_dpcm_get_substream(be, stream);
2073 /* is this op for this BE ? */
2074 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2077 /* copy params for each dpcm */
2078 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2079 sizeof(struct snd_pcm_hw_params));
2081 /* perform any hw_params fixups */
2082 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
2086 /* copy the fixed-up hw params for BE dai */
2087 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2088 sizeof(struct snd_pcm_hw_params));
2090 /* only allow hw_params() if no connected FEs are running */
2091 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2094 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2095 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2096 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2099 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2100 be->dai_link->name);
2102 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2104 dev_err(dpcm->be->dev,
2105 "ASoC: hw_params BE failed %d\n", ret);
2109 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2114 /* disable any enabled and non active backends */
2115 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2116 struct snd_soc_pcm_runtime *be = dpcm->be;
2117 struct snd_pcm_substream *be_substream =
2118 snd_soc_dpcm_get_substream(be, stream);
2120 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2123 /* only allow hw_free() if no connected FEs are running */
2124 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2127 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2128 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2129 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2130 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2133 soc_pcm_hw_free(be_substream);
2139 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2140 struct snd_pcm_hw_params *params)
2142 struct snd_soc_pcm_runtime *fe = substream->private_data;
2143 int ret, stream = substream->stream;
2145 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2146 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2148 memcpy(&fe->dpcm[stream].hw_params, params,
2149 sizeof(struct snd_pcm_hw_params));
2150 ret = dpcm_be_dai_hw_params(fe, stream);
2152 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2156 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2157 fe->dai_link->name, params_rate(params),
2158 params_channels(params), params_format(params));
2160 /* call hw_params on the frontend */
2161 ret = soc_pcm_hw_params(substream, params);
2163 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2164 dpcm_be_dai_hw_free(fe, stream);
2166 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2169 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2170 mutex_unlock(&fe->card->mutex);
2174 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2175 struct snd_pcm_substream *substream, int cmd)
2179 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2180 dpcm->be->dai_link->name, cmd);
2182 ret = soc_pcm_trigger(substream, cmd);
2184 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2189 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2192 struct snd_soc_dpcm *dpcm;
2195 for_each_dpcm_be(fe, stream, dpcm) {
2197 struct snd_soc_pcm_runtime *be = dpcm->be;
2198 struct snd_pcm_substream *be_substream =
2199 snd_soc_dpcm_get_substream(be, stream);
2201 /* is this op for this BE ? */
2202 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2206 case SNDRV_PCM_TRIGGER_START:
2207 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2208 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2209 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2212 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2216 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2218 case SNDRV_PCM_TRIGGER_RESUME:
2219 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2222 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2226 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2228 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2229 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2232 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2236 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2238 case SNDRV_PCM_TRIGGER_STOP:
2239 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2240 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2243 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2246 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2250 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2252 case SNDRV_PCM_TRIGGER_SUSPEND:
2253 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2256 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2259 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2263 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2265 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2266 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2269 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2272 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2276 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2283 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2285 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2286 int cmd, bool fe_first)
2288 struct snd_soc_pcm_runtime *fe = substream->private_data;
2291 /* call trigger on the frontend before the backend. */
2293 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2294 fe->dai_link->name, cmd);
2296 ret = soc_pcm_trigger(substream, cmd);
2300 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2304 /* call trigger on the frontend after the backend. */
2305 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2309 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2310 fe->dai_link->name, cmd);
2312 ret = soc_pcm_trigger(substream, cmd);
2317 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2319 struct snd_soc_pcm_runtime *fe = substream->private_data;
2320 int stream = substream->stream;
2322 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2324 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2327 case SND_SOC_DPCM_TRIGGER_PRE:
2329 case SNDRV_PCM_TRIGGER_START:
2330 case SNDRV_PCM_TRIGGER_RESUME:
2331 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2332 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2334 case SNDRV_PCM_TRIGGER_STOP:
2335 case SNDRV_PCM_TRIGGER_SUSPEND:
2336 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2337 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2344 case SND_SOC_DPCM_TRIGGER_POST:
2346 case SNDRV_PCM_TRIGGER_START:
2347 case SNDRV_PCM_TRIGGER_RESUME:
2348 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2349 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2351 case SNDRV_PCM_TRIGGER_STOP:
2352 case SNDRV_PCM_TRIGGER_SUSPEND:
2353 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2354 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2361 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2362 /* bespoke trigger() - handles both FE and BEs */
2364 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2365 fe->dai_link->name, cmd);
2367 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2370 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2371 fe->dai_link->name);
2377 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2383 case SNDRV_PCM_TRIGGER_START:
2384 case SNDRV_PCM_TRIGGER_RESUME:
2385 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2386 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2388 case SNDRV_PCM_TRIGGER_STOP:
2389 case SNDRV_PCM_TRIGGER_SUSPEND:
2390 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2392 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2393 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2398 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2402 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2404 struct snd_soc_pcm_runtime *fe = substream->private_data;
2405 int stream = substream->stream;
2407 /* if FE's runtime_update is already set, we're in race;
2408 * process this trigger later at exit
2410 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2411 fe->dpcm[stream].trigger_pending = cmd + 1;
2412 return 0; /* delayed, assuming it's successful */
2415 /* we're alone, let's trigger */
2416 return dpcm_fe_dai_do_trigger(substream, cmd);
2419 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2421 struct snd_soc_dpcm *dpcm;
2424 for_each_dpcm_be(fe, stream, dpcm) {
2426 struct snd_soc_pcm_runtime *be = dpcm->be;
2427 struct snd_pcm_substream *be_substream =
2428 snd_soc_dpcm_get_substream(be, stream);
2430 /* is this op for this BE ? */
2431 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2434 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2435 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2436 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2440 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2441 be->dai_link->name);
2443 ret = soc_pcm_prepare(be_substream);
2445 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2450 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2455 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2457 struct snd_soc_pcm_runtime *fe = substream->private_data;
2458 int stream = substream->stream, ret = 0;
2460 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2462 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2464 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2466 /* there is no point preparing this FE if there are no BEs */
2467 if (list_empty(&fe->dpcm[stream].be_clients)) {
2468 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2469 fe->dai_link->name);
2474 ret = dpcm_be_dai_prepare(fe, stream);
2478 /* call prepare on the frontend */
2479 ret = soc_pcm_prepare(substream);
2481 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2482 fe->dai_link->name);
2486 /* run the stream event for each BE */
2487 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2488 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2491 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2492 mutex_unlock(&fe->card->mutex);
2497 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2499 struct snd_pcm_substream *substream =
2500 snd_soc_dpcm_get_substream(fe, stream);
2501 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2504 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2505 stream ? "capture" : "playback", fe->dai_link->name);
2507 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2508 /* call bespoke trigger - FE takes care of all BE triggers */
2509 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2510 fe->dai_link->name);
2512 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2514 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2516 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2517 fe->dai_link->name);
2519 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2521 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2524 err = dpcm_be_dai_hw_free(fe, stream);
2526 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2528 err = dpcm_be_dai_shutdown(fe, stream);
2530 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2532 /* run the stream event for each BE */
2533 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2538 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2540 struct snd_pcm_substream *substream =
2541 snd_soc_dpcm_get_substream(fe, stream);
2542 struct snd_soc_dpcm *dpcm;
2543 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2545 unsigned long flags;
2547 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2548 stream ? "capture" : "playback", fe->dai_link->name);
2550 /* Only start the BE if the FE is ready */
2551 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2552 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2555 /* startup must always be called for new BEs */
2556 ret = dpcm_be_dai_startup(fe, stream);
2560 /* keep going if FE state is > open */
2561 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2564 ret = dpcm_be_dai_hw_params(fe, stream);
2568 /* keep going if FE state is > hw_params */
2569 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2573 ret = dpcm_be_dai_prepare(fe, stream);
2577 /* run the stream event for each BE */
2578 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2580 /* keep going if FE state is > prepare */
2581 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2582 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2585 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2586 /* call trigger on the frontend - FE takes care of all BE triggers */
2587 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2588 fe->dai_link->name);
2590 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2592 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2596 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2597 fe->dai_link->name);
2599 ret = dpcm_be_dai_trigger(fe, stream,
2600 SNDRV_PCM_TRIGGER_START);
2602 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2610 dpcm_be_dai_hw_free(fe, stream);
2612 dpcm_be_dai_shutdown(fe, stream);
2614 /* disconnect any closed BEs */
2615 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2616 for_each_dpcm_be(fe, stream, dpcm) {
2617 struct snd_soc_pcm_runtime *be = dpcm->be;
2618 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2619 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2621 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2626 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2628 struct snd_soc_dapm_widget_list *list;
2633 if (fe->num_cpus > 1) {
2635 "%s doesn't support Multi CPU yet\n", __func__);
2639 if (!fe->dai_link->dynamic)
2642 /* only check active links */
2643 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2646 /* DAPM sync will call this to update DSP paths */
2647 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2648 new ? "new" : "old", fe->dai_link->name);
2650 for_each_pcm_streams(stream) {
2652 /* skip if FE doesn't have playback/capture capability */
2653 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2654 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2657 /* skip if FE isn't currently playing/capturing */
2658 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2659 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2662 paths = dpcm_path_get(fe, stream, &list);
2664 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2666 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2667 "playback" : "capture");
2671 /* update any playback/capture paths */
2672 count = dpcm_process_paths(fe, stream, &list, new);
2674 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2676 ret = dpcm_run_update_startup(fe, stream);
2678 ret = dpcm_run_update_shutdown(fe, stream);
2680 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2681 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2683 dpcm_clear_pending_state(fe, stream);
2684 dpcm_be_disconnect(fe, stream);
2687 dpcm_path_put(&list);
2693 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2696 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2698 struct snd_soc_pcm_runtime *fe;
2701 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2702 /* shutdown all old paths first */
2703 for_each_card_rtds(card, fe) {
2704 ret = soc_dpcm_fe_runtime_update(fe, 0);
2709 /* bring new paths up */
2710 for_each_card_rtds(card, fe) {
2711 ret = soc_dpcm_fe_runtime_update(fe, 1);
2717 mutex_unlock(&card->mutex);
2720 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2722 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2724 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2725 struct snd_soc_dpcm *dpcm;
2726 int stream = fe_substream->stream;
2728 /* mark FE's links ready to prune */
2729 for_each_dpcm_be(fe, stream, dpcm)
2730 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2732 dpcm_be_disconnect(fe, stream);
2734 fe->dpcm[stream].runtime = NULL;
2737 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2739 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2742 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2743 ret = dpcm_fe_dai_shutdown(fe_substream);
2745 dpcm_fe_dai_cleanup(fe_substream);
2747 mutex_unlock(&fe->card->mutex);
2751 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2753 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2754 struct snd_soc_dapm_widget_list *list;
2756 int stream = fe_substream->stream;
2758 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2759 fe->dpcm[stream].runtime = fe_substream->runtime;
2761 ret = dpcm_path_get(fe, stream, &list);
2764 } else if (ret == 0) {
2765 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2766 fe->dai_link->name, stream ? "capture" : "playback");
2769 /* calculate valid and active FE <-> BE dpcms */
2770 dpcm_process_paths(fe, stream, &list, 1);
2772 ret = dpcm_fe_dai_startup(fe_substream);
2774 dpcm_fe_dai_cleanup(fe_substream);
2776 dpcm_clear_pending_state(fe, stream);
2777 dpcm_path_put(&list);
2779 mutex_unlock(&fe->card->mutex);
2783 /* create a new pcm */
2784 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2786 struct snd_soc_dai *codec_dai;
2787 struct snd_soc_dai *cpu_dai;
2788 struct snd_soc_component *component;
2789 struct snd_pcm *pcm;
2791 int ret = 0, playback = 0, capture = 0;
2794 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2795 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2796 if (rtd->num_cpus > 1) {
2798 "DPCM doesn't support Multi CPU yet\n");
2802 playback = rtd->dai_link->dpcm_playback &&
2803 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK);
2804 capture = rtd->dai_link->dpcm_capture &&
2805 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE);
2807 /* Adapt stream for codec2codec links */
2808 int cpu_capture = rtd->dai_link->params ?
2809 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2810 int cpu_playback = rtd->dai_link->params ?
2811 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2813 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2814 if (rtd->num_cpus == 1) {
2815 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2816 } else if (rtd->num_cpus == rtd->num_codecs) {
2817 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2819 dev_err(rtd->card->dev,
2820 "N cpus to M codecs link is not supported yet\n");
2824 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2825 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2827 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2828 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2833 if (rtd->dai_link->playback_only) {
2838 if (rtd->dai_link->capture_only) {
2843 /* create the PCM */
2844 if (rtd->dai_link->params) {
2845 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2846 rtd->dai_link->stream_name);
2848 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2849 playback, capture, &pcm);
2850 } else if (rtd->dai_link->no_pcm) {
2851 snprintf(new_name, sizeof(new_name), "(%s)",
2852 rtd->dai_link->stream_name);
2854 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2855 playback, capture, &pcm);
2857 if (rtd->dai_link->dynamic)
2858 snprintf(new_name, sizeof(new_name), "%s (*)",
2859 rtd->dai_link->stream_name);
2861 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2862 rtd->dai_link->stream_name,
2863 (rtd->num_codecs > 1) ?
2864 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
2866 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2870 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2871 rtd->dai_link->name);
2874 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2876 /* DAPM dai link stream work */
2877 if (rtd->dai_link->params)
2878 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2880 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2882 pcm->nonatomic = rtd->dai_link->nonatomic;
2884 pcm->private_data = rtd;
2886 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2888 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2890 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2894 /* ASoC PCM operations */
2895 if (rtd->dai_link->dynamic) {
2896 rtd->ops.open = dpcm_fe_dai_open;
2897 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2898 rtd->ops.prepare = dpcm_fe_dai_prepare;
2899 rtd->ops.trigger = dpcm_fe_dai_trigger;
2900 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2901 rtd->ops.close = dpcm_fe_dai_close;
2902 rtd->ops.pointer = soc_pcm_pointer;
2904 rtd->ops.open = soc_pcm_open;
2905 rtd->ops.hw_params = soc_pcm_hw_params;
2906 rtd->ops.prepare = soc_pcm_prepare;
2907 rtd->ops.trigger = soc_pcm_trigger;
2908 rtd->ops.hw_free = soc_pcm_hw_free;
2909 rtd->ops.close = soc_pcm_close;
2910 rtd->ops.pointer = soc_pcm_pointer;
2913 for_each_rtd_components(rtd, i, component) {
2914 const struct snd_soc_component_driver *drv = component->driver;
2917 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2919 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2921 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2923 rtd->ops.page = snd_soc_pcm_component_page;
2925 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2929 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2932 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2934 ret = snd_soc_pcm_component_new(rtd);
2936 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2940 pcm->no_device_suspend = true;
2942 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2943 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
2944 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
2948 /* is the current PCM operation for this FE ? */
2949 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2951 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2955 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2957 /* is the current PCM operation for this BE ? */
2958 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2959 struct snd_soc_pcm_runtime *be, int stream)
2961 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2962 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2963 be->dpcm[stream].runtime_update))
2967 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2969 /* get the substream for this BE */
2970 struct snd_pcm_substream *
2971 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2973 return be->pcm->streams[stream].substream;
2975 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2977 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2978 struct snd_soc_pcm_runtime *be,
2980 const enum snd_soc_dpcm_state *states,
2983 struct snd_soc_dpcm *dpcm;
2986 unsigned long flags;
2989 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2990 for_each_dpcm_fe(be, stream, dpcm) {
2995 state = dpcm->fe->dpcm[stream].state;
2996 for (i = 0; i < num_states; i++) {
2997 if (state == states[i]) {
3003 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3005 /* it's safe to do this BE DAI */
3010 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3011 * are not running, paused or suspended for the specified stream direction.
3013 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3014 struct snd_soc_pcm_runtime *be, int stream)
3016 const enum snd_soc_dpcm_state state[] = {
3017 SND_SOC_DPCM_STATE_START,
3018 SND_SOC_DPCM_STATE_PAUSED,
3019 SND_SOC_DPCM_STATE_SUSPEND,
3022 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3024 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3027 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3028 * running, paused or suspended for the specified stream direction.
3030 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3031 struct snd_soc_pcm_runtime *be, int stream)
3033 const enum snd_soc_dpcm_state state[] = {
3034 SND_SOC_DPCM_STATE_START,
3035 SND_SOC_DPCM_STATE_PAUSED,
3036 SND_SOC_DPCM_STATE_SUSPEND,
3037 SND_SOC_DPCM_STATE_PREPARE,
3040 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3042 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);