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 #define DPCM_MAX_BE_USERS 8
32 static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
34 return (rtd)->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
36 static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
38 return (rtd)->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
41 #ifdef CONFIG_DEBUG_FS
42 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
45 case SND_SOC_DPCM_STATE_NEW:
47 case SND_SOC_DPCM_STATE_OPEN:
49 case SND_SOC_DPCM_STATE_HW_PARAMS:
51 case SND_SOC_DPCM_STATE_PREPARE:
53 case SND_SOC_DPCM_STATE_START:
55 case SND_SOC_DPCM_STATE_STOP:
57 case SND_SOC_DPCM_STATE_SUSPEND:
59 case SND_SOC_DPCM_STATE_PAUSED:
61 case SND_SOC_DPCM_STATE_HW_FREE:
63 case SND_SOC_DPCM_STATE_CLOSE:
70 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
71 int stream, char *buf, size_t size)
73 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
74 struct snd_soc_dpcm *dpcm;
79 offset += scnprintf(buf + offset, size - offset,
80 "[%s - %s]\n", fe->dai_link->name,
81 stream ? "Capture" : "Playback");
83 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
84 dpcm_state_string(fe->dpcm[stream].state));
86 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
87 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
88 offset += scnprintf(buf + offset, size - offset,
90 "Format = %s, Channels = %d, Rate = %d\n",
91 snd_pcm_format_name(params_format(params)),
92 params_channels(params),
96 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
98 if (list_empty(&fe->dpcm[stream].be_clients)) {
99 offset += scnprintf(buf + offset, size - offset,
100 " No active DSP links\n");
104 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
105 for_each_dpcm_be(fe, stream, dpcm) {
106 struct snd_soc_pcm_runtime *be = dpcm->be;
107 params = &dpcm->hw_params;
109 offset += scnprintf(buf + offset, size - offset,
110 "- %s\n", be->dai_link->name);
112 offset += scnprintf(buf + offset, size - offset,
114 dpcm_state_string(be->dpcm[stream].state));
116 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
117 (be->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));
125 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
130 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
131 size_t count, loff_t *ppos)
133 struct snd_soc_pcm_runtime *fe = file->private_data;
134 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
138 if (fe->num_cpus > 1) {
140 "%s doesn't support Multi CPU yet\n", __func__);
144 buf = kmalloc(out_count, GFP_KERNEL);
148 for_each_pcm_streams(stream)
149 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
150 offset += dpcm_show_state(fe, stream,
154 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
160 static const struct file_operations dpcm_state_fops = {
162 .read = dpcm_state_read_file,
163 .llseek = default_llseek,
166 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
171 if (!rtd->dai_link->dynamic)
174 if (!rtd->card->debugfs_card_root)
177 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
178 rtd->card->debugfs_card_root);
180 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
181 rtd, &dpcm_state_fops);
184 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
188 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
189 stream ? "capture" : "playback");
191 dpcm->debugfs_state = debugfs_create_dir(
192 name, dpcm->fe->debugfs_dpcm_root);
193 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
199 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
201 debugfs_remove_recursive(dpcm->debugfs_state);
205 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
210 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
215 /* Set FE's runtime_update state; the state is protected via PCM stream lock
216 * for avoiding the race with trigger callback.
217 * If the state is unset and a trigger is pending while the previous operation,
218 * process the pending trigger action here.
220 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
221 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
222 int stream, enum snd_soc_dpcm_update state)
224 struct snd_pcm_substream *substream =
225 snd_soc_dpcm_get_substream(fe, stream);
227 snd_pcm_stream_lock_irq(substream);
228 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
229 dpcm_fe_dai_do_trigger(substream,
230 fe->dpcm[stream].trigger_pending - 1);
231 fe->dpcm[stream].trigger_pending = 0;
233 fe->dpcm[stream].runtime_update = state;
234 snd_pcm_stream_unlock_irq(substream);
237 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
238 int stream, enum snd_soc_dpcm_update state)
240 be->dpcm[stream].runtime_update = state;
244 * snd_soc_runtime_action() - Increment/Decrement active count for
245 * PCM runtime components
246 * @rtd: ASoC PCM runtime that is activated
247 * @stream: Direction of the PCM stream
248 * @action: Activate stream if 1. Deactivate if -1.
250 * Increments/Decrements the active count for all the DAIs and components
251 * attached to a PCM runtime.
252 * Should typically be called when a stream is opened.
254 * Must be called with the rtd->card->pcm_mutex being held
256 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
257 int stream, int action)
259 struct snd_soc_dai *dai;
262 lockdep_assert_held(&rtd->card->pcm_mutex);
264 for_each_rtd_dais(rtd, i, dai)
265 snd_soc_dai_action(dai, stream, action);
267 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
270 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
271 * @rtd: The ASoC PCM runtime that should be checked.
273 * This function checks whether the power down delay should be ignored for a
274 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
275 * been configured to ignore the delay, or if none of the components benefits
276 * from having the delay.
278 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
280 struct snd_soc_component *component;
284 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
287 for_each_rtd_components(rtd, i, component)
288 ignore &= !component->driver->use_pmdown_time;
294 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
295 * @substream: the pcm substream
296 * @hw: the hardware parameters
298 * Sets the substream runtime hardware parameters.
300 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
301 const struct snd_pcm_hardware *hw)
303 substream->runtime->hw = *hw;
307 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
309 /* DPCM stream event, send event to FE and all active BEs. */
310 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
313 struct snd_soc_dpcm *dpcm;
315 for_each_dpcm_be(fe, dir, dpcm) {
317 struct snd_soc_pcm_runtime *be = dpcm->be;
319 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
320 be->dai_link->name, event, dir);
322 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
323 (be->dpcm[dir].users >= 1))
326 snd_soc_dapm_stream_event(be, dir, event);
329 snd_soc_dapm_stream_event(fe, dir, event);
334 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
335 struct snd_pcm_hw_params *params)
338 dai->rate = params_rate(params);
339 dai->channels = params_channels(params);
340 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
344 dai->sample_bits = 0;
348 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
349 struct snd_soc_dai *soc_dai)
351 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
354 if (!snd_soc_dai_active(soc_dai))
357 #define __soc_pcm_apply_symmetry(name, NAME) \
358 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
359 rtd->dai_link->symmetric_##name)) { \
360 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
361 #name, soc_dai->name); \
363 ret = snd_pcm_hw_constraint_single(substream->runtime, \
364 SNDRV_PCM_HW_PARAM_##NAME,\
367 dev_err(soc_dai->dev, \
368 "ASoC: Unable to apply %s constraint: %d\n",\
374 __soc_pcm_apply_symmetry(rate, RATE);
375 __soc_pcm_apply_symmetry(channels, CHANNELS);
376 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
381 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
382 struct snd_pcm_hw_params *params)
384 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
385 struct snd_soc_dai d;
386 struct snd_soc_dai *dai;
387 struct snd_soc_dai *cpu_dai;
388 unsigned int symmetry, i;
390 soc_pcm_set_dai_params(&d, params);
392 #define __soc_pcm_params_symmetry(name) \
393 symmetry = rtd->dai_link->symmetric_##name; \
394 for_each_rtd_dais(rtd, i, dai) \
395 symmetry |= dai->driver->symmetric_##name; \
398 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
399 if (cpu_dai->name && cpu_dai->name != d.name) { \
400 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %d - %d\n", \
401 #name, cpu_dai->name, d.name); \
405 /* reject unmatched parameters when applying symmetry */
406 __soc_pcm_params_symmetry(rate);
407 __soc_pcm_params_symmetry(channels);
408 __soc_pcm_params_symmetry(sample_bits);
413 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
415 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
416 struct snd_soc_dai_link *link = rtd->dai_link;
417 struct snd_soc_dai *dai;
418 unsigned int symmetry, i;
420 symmetry = link->symmetric_rate ||
421 link->symmetric_channels ||
422 link->symmetric_sample_bits;
424 for_each_rtd_dais(rtd, i, dai)
425 symmetry = symmetry ||
426 dai->driver->symmetric_rate ||
427 dai->driver->symmetric_channels ||
428 dai->driver->symmetric_sample_bits;
433 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
435 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
441 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
443 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
447 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
449 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
450 struct snd_soc_dai *cpu_dai;
451 struct snd_soc_dai *codec_dai;
452 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
453 int stream = substream->stream;
455 unsigned int bits = 0, cpu_bits = 0;
457 for_each_rtd_codec_dais(rtd, i, codec_dai) {
458 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
460 if (pcm_codec->sig_bits == 0) {
464 bits = max(pcm_codec->sig_bits, bits);
467 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
468 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
470 if (pcm_cpu->sig_bits == 0) {
474 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
477 soc_pcm_set_msb(substream, bits);
478 soc_pcm_set_msb(substream, cpu_bits);
481 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
483 hw->rates = UINT_MAX;
485 hw->rate_max = UINT_MAX;
486 hw->channels_min = 0;
487 hw->channels_max = UINT_MAX;
488 hw->formats = ULLONG_MAX;
491 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
492 struct snd_soc_pcm_stream *p)
494 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
496 /* setup hw->rate_min/max via hw->rates first */
497 snd_pcm_hw_limit_rates(hw);
499 /* update hw->rate_min/max by snd_soc_pcm_stream */
500 hw->rate_min = max(hw->rate_min, p->rate_min);
501 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
504 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
505 struct snd_soc_pcm_stream *p)
507 hw->channels_min = max(hw->channels_min, p->channels_min);
508 hw->channels_max = min(hw->channels_max, p->channels_max);
511 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
512 struct snd_soc_pcm_stream *p)
514 hw->formats &= p->formats;
518 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
519 * @rtd: ASoC PCM runtime
520 * @hw: PCM hardware parameters (output)
521 * @stream: Direction of the PCM stream
523 * Calculates the subset of stream parameters supported by all DAIs
524 * associated with the PCM stream.
526 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
527 struct snd_pcm_hardware *hw, int stream)
529 struct snd_soc_dai *codec_dai;
530 struct snd_soc_dai *cpu_dai;
531 struct snd_soc_pcm_stream *codec_stream;
532 struct snd_soc_pcm_stream *cpu_stream;
533 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
538 /* first calculate min/max only for CPUs in the DAI link */
539 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
542 * Skip CPUs 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(cpu_dai, stream))
550 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
552 soc_pcm_hw_update_chan(hw, cpu_stream);
553 soc_pcm_hw_update_rate(hw, cpu_stream);
554 soc_pcm_hw_update_format(hw, cpu_stream);
556 cpu_chan_min = hw->channels_min;
557 cpu_chan_max = hw->channels_max;
559 /* second calculate min/max only for CODECs in the DAI link */
560 for_each_rtd_codec_dais(rtd, i, codec_dai) {
563 * Skip CODECs which don't support the current stream type.
564 * Otherwise, since the rate, channel, and format values will
565 * zero in that case, we would have no usable settings left,
566 * causing the resulting setup to fail.
568 if (!snd_soc_dai_stream_valid(codec_dai, stream))
571 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
573 soc_pcm_hw_update_chan(hw, codec_stream);
574 soc_pcm_hw_update_rate(hw, codec_stream);
575 soc_pcm_hw_update_format(hw, codec_stream);
578 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
579 if (!hw->channels_min)
583 * chan min/max cannot be enforced if there are multiple CODEC DAIs
584 * connected to CPU DAI(s), use CPU DAI's directly and let
585 * channel allocation be fixed up later
587 if (rtd->num_codecs > 1) {
588 hw->channels_min = cpu_chan_min;
589 hw->channels_max = cpu_chan_max;
594 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
596 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
598 struct snd_pcm_hardware *hw = &substream->runtime->hw;
599 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
600 u64 formats = hw->formats;
603 * At least one CPU and one CODEC should match. Otherwise, we should
604 * have bailed out on a higher level, since there would be no CPU or
605 * CODEC to support the transfer direction in that case.
607 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
610 hw->formats &= formats;
613 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
615 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
616 struct snd_soc_component *component;
619 for_each_rtd_components(rtd, i, component) {
620 ret = snd_soc_component_module_get_when_open(component, substream);
624 ret = snd_soc_component_open(component, substream);
632 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
635 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
636 struct snd_soc_component *component;
639 for_each_rtd_components(rtd, i, component) {
640 r = snd_soc_component_close(component, substream, rollback);
642 ret = r; /* use last ret */
644 snd_soc_component_module_put_when_close(component, substream, rollback);
650 static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
652 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
653 struct snd_soc_component *component;
654 struct snd_soc_dai *dai;
657 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
660 snd_soc_runtime_deactivate(rtd, substream->stream);
662 for_each_rtd_dais(rtd, i, dai)
663 snd_soc_dai_shutdown(dai, substream, rollback);
665 snd_soc_link_shutdown(substream, rollback);
667 soc_pcm_components_close(substream, rollback);
670 mutex_unlock(&rtd->card->pcm_mutex);
672 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
674 for_each_rtd_components(rtd, i, component)
675 if (!snd_soc_component_active(component))
676 pinctrl_pm_select_sleep_state(component->dev);
682 * Called by ALSA when a PCM substream is closed. Private data can be
683 * freed here. The cpu DAI, codec DAI, machine and components are also
686 static int soc_pcm_close(struct snd_pcm_substream *substream)
688 return soc_pcm_clean(substream, 0);
692 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
693 * then initialized and any private data can be allocated. This also calls
694 * startup for the cpu DAI, component, machine and codec DAI.
696 static int soc_pcm_open(struct snd_pcm_substream *substream)
698 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
699 struct snd_pcm_runtime *runtime = substream->runtime;
700 struct snd_soc_component *component;
701 struct snd_soc_dai *dai;
702 const char *codec_dai_name = soc_codec_dai_name(rtd);
703 const char *cpu_dai_name = soc_cpu_dai_name(rtd);
706 for_each_rtd_components(rtd, i, component)
707 pinctrl_pm_select_default_state(component->dev);
709 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
713 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
715 ret = soc_pcm_components_open(substream);
719 ret = snd_soc_link_startup(substream);
723 /* startup the audio subsystem */
724 for_each_rtd_dais(rtd, i, dai) {
725 ret = snd_soc_dai_startup(dai, substream);
729 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
735 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
736 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
739 /* Check that the codec and cpu DAIs are compatible */
740 soc_pcm_init_runtime_hw(substream);
742 if (soc_pcm_has_symmetry(substream))
743 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
746 if (!runtime->hw.rates) {
747 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
748 codec_dai_name, cpu_dai_name);
751 if (!runtime->hw.formats) {
752 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
753 codec_dai_name, cpu_dai_name);
756 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
757 runtime->hw.channels_min > runtime->hw.channels_max) {
758 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
759 codec_dai_name, cpu_dai_name);
763 soc_pcm_apply_msb(substream);
765 /* Symmetry only applies if we've already got an active stream. */
766 for_each_rtd_dais(rtd, i, dai) {
767 ret = soc_pcm_apply_symmetry(substream, dai);
772 pr_debug("ASoC: %s <-> %s info:\n",
773 codec_dai_name, cpu_dai_name);
774 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
775 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
776 runtime->hw.channels_max);
777 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
778 runtime->hw.rate_max);
780 snd_soc_runtime_activate(rtd, substream->stream);
783 mutex_unlock(&rtd->card->pcm_mutex);
786 soc_pcm_clean(substream, 1);
791 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
794 * Currently nothing to do for c2c links
795 * Since c2c links are internal nodes in the DAPM graph and
796 * don't interface with the outside world or application layer
797 * we don't have to do any special handling on close.
802 * Called by ALSA when the PCM substream is prepared, can set format, sample
803 * rate, etc. This function is non atomic and can be called multiple times,
804 * it can refer to the runtime info.
806 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
808 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
809 struct snd_soc_dai *dai;
812 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
814 ret = snd_soc_link_prepare(substream);
818 ret = snd_soc_pcm_component_prepare(substream);
822 ret = snd_soc_pcm_dai_prepare(substream);
824 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
828 /* cancel any delayed stream shutdown that is pending */
829 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
832 cancel_delayed_work(&rtd->delayed_work);
835 snd_soc_dapm_stream_event(rtd, substream->stream,
836 SND_SOC_DAPM_STREAM_START);
838 for_each_rtd_dais(rtd, i, dai)
839 snd_soc_dai_digital_mute(dai, 0, substream->stream);
842 mutex_unlock(&rtd->card->pcm_mutex);
846 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
849 struct snd_interval *interval;
850 int channels = hweight_long(mask);
852 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
853 interval->min = channels;
854 interval->max = channels;
857 static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
859 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
860 struct snd_soc_dai *dai;
863 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
865 /* clear the corresponding DAIs parameters when going to be inactive */
866 for_each_rtd_dais(rtd, i, dai) {
867 int active = snd_soc_dai_stream_active(dai, substream->stream);
869 if (snd_soc_dai_active(dai) == 1)
870 soc_pcm_set_dai_params(dai, NULL);
873 snd_soc_dai_digital_mute(dai, 1, substream->stream);
876 /* run the stream event */
877 snd_soc_dapm_stream_stop(rtd, substream->stream);
879 /* free any machine hw params */
880 snd_soc_link_hw_free(substream, rollback);
882 /* free any component resources */
883 snd_soc_pcm_component_hw_free(substream, rollback);
885 /* now free hw params for the DAIs */
886 for_each_rtd_dais(rtd, i, dai) {
887 if (!snd_soc_dai_stream_valid(dai, substream->stream))
890 snd_soc_dai_hw_free(dai, substream, rollback);
893 mutex_unlock(&rtd->card->pcm_mutex);
898 * Frees resources allocated by hw_params, can be called multiple times
900 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
902 return soc_pcm_hw_clean(substream, 0);
906 * Called by ALSA when the hardware params are set by application. This
907 * function can also be called multiple times and can allocate buffers
908 * (using snd_pcm_lib_* ). It's non-atomic.
910 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
911 struct snd_pcm_hw_params *params)
913 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
914 struct snd_soc_dai *cpu_dai;
915 struct snd_soc_dai *codec_dai;
918 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
920 ret = soc_pcm_params_symmetry(substream, params);
924 ret = snd_soc_link_hw_params(substream, params);
928 for_each_rtd_codec_dais(rtd, i, codec_dai) {
929 struct snd_pcm_hw_params codec_params;
932 * Skip CODECs which don't support the current stream type,
933 * the idea being that if a CODEC is not used for the currently
934 * set up transfer direction, it should not need to be
935 * configured, especially since the configuration used might
936 * not even be supported by that CODEC. There may be cases
937 * however where a CODEC needs to be set up although it is
938 * actually not being used for the transfer, e.g. if a
939 * capture-only CODEC is acting as an LRCLK and/or BCLK master
940 * for the DAI link including a playback-only CODEC.
941 * If this becomes necessary, we will have to augment the
942 * machine driver setup with information on how to act, so
943 * we can do the right thing here.
945 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
948 /* copy params for each codec */
949 codec_params = *params;
951 /* fixup params based on TDM slot masks */
952 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
954 soc_pcm_codec_params_fixup(&codec_params,
957 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
959 soc_pcm_codec_params_fixup(&codec_params,
962 ret = snd_soc_dai_hw_params(codec_dai, substream,
967 soc_pcm_set_dai_params(codec_dai, &codec_params);
968 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
971 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
973 * Skip CPUs which don't support the current stream
974 * type. See soc_pcm_init_runtime_hw() for more details
976 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
979 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
983 /* store the parameters for each DAI */
984 soc_pcm_set_dai_params(cpu_dai, params);
985 snd_soc_dapm_update_dai(substream, params, cpu_dai);
988 ret = snd_soc_pcm_component_hw_params(substream, params);
990 mutex_unlock(&rtd->card->pcm_mutex);
993 soc_pcm_hw_clean(substream, 1);
998 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1000 int ret = -EINVAL, _ret = 0;
1004 case SNDRV_PCM_TRIGGER_START:
1005 case SNDRV_PCM_TRIGGER_RESUME:
1006 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1007 ret = snd_soc_link_trigger(substream, cmd, 0);
1011 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1015 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1024 case SNDRV_PCM_TRIGGER_START:
1025 cmd = SNDRV_PCM_TRIGGER_STOP;
1027 case SNDRV_PCM_TRIGGER_RESUME:
1028 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1030 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1031 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1037 case SNDRV_PCM_TRIGGER_STOP:
1038 case SNDRV_PCM_TRIGGER_SUSPEND:
1039 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1040 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1044 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1048 ret = snd_soc_link_trigger(substream, cmd, rollback);
1059 * soc level wrapper for pointer callback
1060 * If cpu_dai, codec_dai, component driver has the delay callback, then
1061 * the runtime->delay will be updated accordingly.
1063 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1065 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1066 struct snd_soc_dai *cpu_dai;
1067 struct snd_soc_dai *codec_dai;
1068 struct snd_pcm_runtime *runtime = substream->runtime;
1069 snd_pcm_uframes_t offset = 0;
1070 snd_pcm_sframes_t delay = 0;
1071 snd_pcm_sframes_t codec_delay = 0;
1072 snd_pcm_sframes_t cpu_delay = 0;
1075 /* clearing the previous total delay */
1078 offset = snd_soc_pcm_component_pointer(substream);
1080 /* base delay if assigned in pointer callback */
1081 delay = runtime->delay;
1083 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1084 cpu_delay = max(cpu_delay,
1085 snd_soc_dai_delay(cpu_dai, substream));
1089 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1090 codec_delay = max(codec_delay,
1091 snd_soc_dai_delay(codec_dai, substream));
1093 delay += codec_delay;
1095 runtime->delay = delay;
1100 /* connect a FE and BE */
1101 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1102 struct snd_soc_pcm_runtime *be, int stream)
1104 struct snd_soc_dpcm *dpcm;
1105 unsigned long flags;
1107 /* only add new dpcms */
1108 for_each_dpcm_be(fe, stream, dpcm) {
1109 if (dpcm->be == be && dpcm->fe == fe)
1113 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1119 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1120 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1121 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1122 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1123 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1124 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1126 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1127 stream ? "capture" : "playback", fe->dai_link->name,
1128 stream ? "<-" : "->", be->dai_link->name);
1130 dpcm_create_debugfs_state(dpcm, stream);
1135 /* reparent a BE onto another FE */
1136 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1137 struct snd_soc_pcm_runtime *be, int stream)
1139 struct snd_soc_dpcm *dpcm;
1140 struct snd_pcm_substream *fe_substream, *be_substream;
1142 /* reparent if BE is connected to other FEs */
1143 if (!be->dpcm[stream].users)
1146 be_substream = snd_soc_dpcm_get_substream(be, stream);
1148 for_each_dpcm_fe(be, stream, dpcm) {
1152 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1153 stream ? "capture" : "playback",
1154 dpcm->fe->dai_link->name,
1155 stream ? "<-" : "->", dpcm->be->dai_link->name);
1157 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1158 be_substream->runtime = fe_substream->runtime;
1163 /* disconnect a BE and FE */
1164 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1166 struct snd_soc_dpcm *dpcm, *d;
1167 unsigned long flags;
1169 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1170 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1171 stream ? "capture" : "playback",
1172 dpcm->be->dai_link->name);
1174 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1177 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1178 stream ? "capture" : "playback", fe->dai_link->name,
1179 stream ? "<-" : "->", dpcm->be->dai_link->name);
1181 /* BEs still alive need new FE */
1182 dpcm_be_reparent(fe, dpcm->be, stream);
1184 dpcm_remove_debugfs_state(dpcm);
1186 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1187 list_del(&dpcm->list_be);
1188 list_del(&dpcm->list_fe);
1189 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1194 /* get BE for DAI widget and stream */
1195 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1196 struct snd_soc_dapm_widget *widget, int stream)
1198 struct snd_soc_pcm_runtime *be;
1199 struct snd_soc_dapm_widget *w;
1200 struct snd_soc_dai *dai;
1203 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1205 for_each_card_rtds(card, be) {
1207 if (!be->dai_link->no_pcm)
1210 for_each_rtd_dais(be, i, dai) {
1211 w = snd_soc_dai_get_widget(dai, stream);
1213 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1214 w ? w->name : "(not set)");
1221 /* Widget provided is not a BE */
1225 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1226 struct snd_soc_dapm_widget *widget)
1228 struct snd_soc_dapm_widget *w;
1231 for_each_dapm_widgets(list, i, w)
1238 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1239 enum snd_soc_dapm_direction dir)
1241 struct snd_soc_card *card = widget->dapm->card;
1242 struct snd_soc_pcm_runtime *rtd;
1245 /* adjust dir to stream */
1246 if (dir == SND_SOC_DAPM_DIR_OUT)
1247 stream = SNDRV_PCM_STREAM_PLAYBACK;
1249 stream = SNDRV_PCM_STREAM_CAPTURE;
1251 rtd = dpcm_get_be(card, widget, stream);
1258 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1259 int stream, struct snd_soc_dapm_widget_list **list)
1261 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1264 if (fe->num_cpus > 1) {
1266 "%s doesn't support Multi CPU yet\n", __func__);
1270 /* get number of valid DAI paths and their widgets */
1271 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1272 fe->card->component_chaining ?
1273 NULL : dpcm_end_walk_at_be);
1275 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1276 stream ? "capture" : "playback");
1281 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1283 snd_soc_dapm_dai_free_widgets(list);
1286 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1287 struct snd_soc_dapm_widget_list *list)
1289 struct snd_soc_dapm_widget *widget;
1290 struct snd_soc_dai *dai;
1293 /* is there a valid DAI widget for this BE */
1294 for_each_rtd_dais(dpcm->be, i, dai) {
1295 widget = snd_soc_dai_get_widget(dai, stream);
1298 * The BE is pruned only if none of the dai
1299 * widgets are in the active list.
1301 if (widget && widget_in_list(list, widget))
1308 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1309 struct snd_soc_dapm_widget_list **list_)
1311 struct snd_soc_dpcm *dpcm;
1314 /* Destroy any old FE <--> BE connections */
1315 for_each_dpcm_be(fe, stream, dpcm) {
1316 if (dpcm_be_is_active(dpcm, stream, *list_))
1319 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1320 stream ? "capture" : "playback",
1321 dpcm->be->dai_link->name, fe->dai_link->name);
1322 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1323 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1327 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1331 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1332 struct snd_soc_dapm_widget_list **list_)
1334 struct snd_soc_card *card = fe->card;
1335 struct snd_soc_dapm_widget_list *list = *list_;
1336 struct snd_soc_pcm_runtime *be;
1337 struct snd_soc_dapm_widget *widget;
1338 int i, new = 0, err;
1340 /* Create any new FE <--> BE connections */
1341 for_each_dapm_widgets(list, i, widget) {
1343 switch (widget->id) {
1344 case snd_soc_dapm_dai_in:
1345 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1348 case snd_soc_dapm_dai_out:
1349 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1356 /* is there a valid BE rtd for this widget */
1357 be = dpcm_get_be(card, widget, stream);
1359 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1364 /* don't connect if FE is not running */
1365 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1368 /* newly connected FE and BE */
1369 err = dpcm_be_connect(fe, be, stream);
1371 dev_err(fe->dev, "ASoC: can't connect %s\n",
1374 } else if (err == 0) /* already connected */
1378 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1382 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1387 * Find the corresponding BE DAIs that source or sink audio to this
1390 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1391 int stream, struct snd_soc_dapm_widget_list **list, int new)
1394 return dpcm_add_paths(fe, stream, list);
1396 return dpcm_prune_paths(fe, stream, list);
1399 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1401 struct snd_soc_dpcm *dpcm;
1402 unsigned long flags;
1404 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1405 for_each_dpcm_be(fe, stream, dpcm)
1406 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1407 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1410 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1413 struct snd_soc_dpcm *dpcm;
1415 /* disable any enabled and non active backends */
1416 for_each_dpcm_be(fe, stream, dpcm) {
1418 struct snd_soc_pcm_runtime *be = dpcm->be;
1419 struct snd_pcm_substream *be_substream =
1420 snd_soc_dpcm_get_substream(be, stream);
1422 if (be->dpcm[stream].users == 0)
1423 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1424 stream ? "capture" : "playback",
1425 be->dpcm[stream].state);
1427 if (--be->dpcm[stream].users != 0)
1430 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1433 soc_pcm_close(be_substream);
1434 be_substream->runtime = NULL;
1435 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1439 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1441 struct snd_soc_dpcm *dpcm;
1444 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1445 for_each_dpcm_be(fe, stream, dpcm) {
1447 struct snd_soc_pcm_runtime *be = dpcm->be;
1448 struct snd_pcm_substream *be_substream =
1449 snd_soc_dpcm_get_substream(be, stream);
1451 if (!be_substream) {
1452 dev_err(be->dev, "ASoC: no backend %s stream\n",
1453 stream ? "capture" : "playback");
1457 /* is this op for this BE ? */
1458 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1461 /* first time the dpcm is open ? */
1462 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1463 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1464 stream ? "capture" : "playback",
1465 be->dpcm[stream].state);
1467 if (be->dpcm[stream].users++ != 0)
1470 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1471 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1474 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1475 stream ? "capture" : "playback", be->dai_link->name);
1477 be_substream->runtime = be->dpcm[stream].runtime;
1478 err = soc_pcm_open(be_substream);
1480 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1481 be->dpcm[stream].users--;
1482 if (be->dpcm[stream].users < 0)
1483 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1484 stream ? "capture" : "playback",
1485 be->dpcm[stream].state);
1487 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1491 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1498 /* disable any enabled and non active backends */
1499 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1500 struct snd_soc_pcm_runtime *be = dpcm->be;
1501 struct snd_pcm_substream *be_substream =
1502 snd_soc_dpcm_get_substream(be, stream);
1504 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1507 if (be->dpcm[stream].users == 0)
1508 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1509 stream ? "capture" : "playback",
1510 be->dpcm[stream].state);
1512 if (--be->dpcm[stream].users != 0)
1515 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1518 soc_pcm_close(be_substream);
1519 be_substream->runtime = NULL;
1520 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1526 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1528 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1529 struct snd_pcm_runtime *runtime = substream->runtime;
1530 struct snd_pcm_hardware *hw = &runtime->hw;
1531 struct snd_soc_dai *dai;
1532 int stream = substream->stream;
1535 soc_pcm_hw_init(hw);
1537 for_each_rtd_cpu_dais(fe, i, dai) {
1538 struct snd_soc_pcm_stream *cpu_stream;
1541 * Skip CPUs which don't support the current stream
1542 * type. See soc_pcm_init_runtime_hw() for more details
1544 if (!snd_soc_dai_stream_valid(dai, stream))
1547 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1549 soc_pcm_hw_update_rate(hw, cpu_stream);
1550 soc_pcm_hw_update_chan(hw, cpu_stream);
1551 soc_pcm_hw_update_format(hw, cpu_stream);
1556 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1558 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1559 struct snd_pcm_runtime *runtime = substream->runtime;
1560 struct snd_pcm_hardware *hw = &runtime->hw;
1561 struct snd_soc_dpcm *dpcm;
1562 struct snd_soc_dai *dai;
1563 int stream = substream->stream;
1565 if (!fe->dai_link->dpcm_merged_format)
1569 * It returns merged BE codec format
1570 * if FE want to use it (= dpcm_merged_format)
1573 for_each_dpcm_be(fe, stream, dpcm) {
1574 struct snd_soc_pcm_runtime *be = dpcm->be;
1575 struct snd_soc_pcm_stream *codec_stream;
1578 for_each_rtd_codec_dais(be, i, dai) {
1580 * Skip CODECs which don't support the current stream
1581 * type. See soc_pcm_init_runtime_hw() for more details
1583 if (!snd_soc_dai_stream_valid(dai, stream))
1586 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1588 soc_pcm_hw_update_format(hw, codec_stream);
1593 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1595 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1596 struct snd_pcm_runtime *runtime = substream->runtime;
1597 struct snd_pcm_hardware *hw = &runtime->hw;
1598 struct snd_soc_dpcm *dpcm;
1599 int stream = substream->stream;
1601 if (!fe->dai_link->dpcm_merged_chan)
1605 * It returns merged BE codec channel;
1606 * if FE want to use it (= dpcm_merged_chan)
1609 for_each_dpcm_be(fe, stream, dpcm) {
1610 struct snd_soc_pcm_runtime *be = dpcm->be;
1611 struct snd_soc_pcm_stream *codec_stream;
1612 struct snd_soc_pcm_stream *cpu_stream;
1613 struct snd_soc_dai *dai;
1616 for_each_rtd_cpu_dais(be, i, dai) {
1618 * Skip CPUs which don't support the current stream
1619 * type. See soc_pcm_init_runtime_hw() for more details
1621 if (!snd_soc_dai_stream_valid(dai, stream))
1624 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1626 soc_pcm_hw_update_chan(hw, cpu_stream);
1630 * chan min/max cannot be enforced if there are multiple CODEC
1631 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1633 if (be->num_codecs == 1) {
1634 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1636 soc_pcm_hw_update_chan(hw, codec_stream);
1641 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1643 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1644 struct snd_pcm_runtime *runtime = substream->runtime;
1645 struct snd_pcm_hardware *hw = &runtime->hw;
1646 struct snd_soc_dpcm *dpcm;
1647 int stream = substream->stream;
1649 if (!fe->dai_link->dpcm_merged_rate)
1653 * It returns merged BE codec channel;
1654 * if FE want to use it (= dpcm_merged_chan)
1657 for_each_dpcm_be(fe, stream, dpcm) {
1658 struct snd_soc_pcm_runtime *be = dpcm->be;
1659 struct snd_soc_pcm_stream *pcm;
1660 struct snd_soc_dai *dai;
1663 for_each_rtd_dais(be, i, dai) {
1665 * Skip DAIs which don't support the current stream
1666 * type. See soc_pcm_init_runtime_hw() for more details
1668 if (!snd_soc_dai_stream_valid(dai, stream))
1671 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1673 soc_pcm_hw_update_rate(hw, pcm);
1678 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1681 struct snd_soc_dpcm *dpcm;
1682 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1683 struct snd_soc_dai *fe_cpu_dai;
1687 /* apply symmetry for FE */
1688 if (soc_pcm_has_symmetry(fe_substream))
1689 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1691 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1692 /* Symmetry only applies if we've got an active stream. */
1693 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1698 /* apply symmetry for BE */
1699 for_each_dpcm_be(fe, stream, dpcm) {
1700 struct snd_soc_pcm_runtime *be = dpcm->be;
1701 struct snd_pcm_substream *be_substream =
1702 snd_soc_dpcm_get_substream(be, stream);
1703 struct snd_soc_pcm_runtime *rtd;
1704 struct snd_soc_dai *dai;
1706 /* A backend may not have the requested substream */
1710 rtd = asoc_substream_to_rtd(be_substream);
1711 if (rtd->dai_link->be_hw_params_fixup)
1714 if (soc_pcm_has_symmetry(be_substream))
1715 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1717 /* Symmetry only applies if we've got an active stream. */
1718 for_each_rtd_dais(rtd, i, dai) {
1719 err = soc_pcm_apply_symmetry(fe_substream, dai);
1728 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1730 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1731 int stream = fe_substream->stream, ret = 0;
1733 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1735 ret = dpcm_be_dai_startup(fe, stream);
1737 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1741 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1743 /* start the DAI frontend */
1744 ret = soc_pcm_open(fe_substream);
1746 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1750 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1752 dpcm_runtime_setup_fe(fe_substream);
1754 dpcm_runtime_setup_be_format(fe_substream);
1755 dpcm_runtime_setup_be_chan(fe_substream);
1756 dpcm_runtime_setup_be_rate(fe_substream);
1758 ret = dpcm_apply_symmetry(fe_substream, stream);
1760 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1765 dpcm_be_dai_startup_unwind(fe, stream);
1767 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1771 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1773 struct snd_soc_dpcm *dpcm;
1775 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1776 for_each_dpcm_be(fe, stream, dpcm) {
1778 struct snd_soc_pcm_runtime *be = dpcm->be;
1779 struct snd_pcm_substream *be_substream =
1780 snd_soc_dpcm_get_substream(be, stream);
1782 /* is this op for this BE ? */
1783 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1786 if (be->dpcm[stream].users == 0)
1787 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1788 stream ? "capture" : "playback",
1789 be->dpcm[stream].state);
1791 if (--be->dpcm[stream].users != 0)
1794 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1795 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1796 soc_pcm_hw_free(be_substream);
1797 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1800 dev_dbg(be->dev, "ASoC: close BE %s\n",
1801 be->dai_link->name);
1803 soc_pcm_close(be_substream);
1804 be_substream->runtime = NULL;
1806 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1811 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1813 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1814 int stream = substream->stream;
1816 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1818 /* shutdown the BEs */
1819 dpcm_be_dai_shutdown(fe, stream);
1821 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1823 /* now shutdown the frontend */
1824 soc_pcm_close(substream);
1826 /* run the stream stop event */
1827 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1829 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1830 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1834 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1836 struct snd_soc_dpcm *dpcm;
1838 /* only hw_params backends that are either sinks or sources
1839 * to this frontend DAI */
1840 for_each_dpcm_be(fe, stream, dpcm) {
1842 struct snd_soc_pcm_runtime *be = dpcm->be;
1843 struct snd_pcm_substream *be_substream =
1844 snd_soc_dpcm_get_substream(be, stream);
1846 /* is this op for this BE ? */
1847 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1850 /* only free hw when no longer used - check all FEs */
1851 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1854 /* do not free hw if this BE is used by other FE */
1855 if (be->dpcm[stream].users > 1)
1858 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1859 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1860 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1861 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1862 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1863 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1866 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1867 be->dai_link->name);
1869 soc_pcm_hw_free(be_substream);
1871 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1877 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1879 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1880 int err, stream = substream->stream;
1882 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1883 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1885 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1887 /* call hw_free on the frontend */
1888 err = soc_pcm_hw_free(substream);
1890 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1891 fe->dai_link->name);
1893 /* only hw_params backends that are either sinks or sources
1894 * to this frontend DAI */
1895 err = dpcm_be_dai_hw_free(fe, stream);
1897 dev_err(fe->dev, "ASoC: hw_free BE failed %d\n", err);
1899 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1900 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1902 mutex_unlock(&fe->card->mutex);
1906 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1908 struct snd_soc_dpcm *dpcm;
1911 for_each_dpcm_be(fe, stream, dpcm) {
1913 struct snd_soc_pcm_runtime *be = dpcm->be;
1914 struct snd_pcm_substream *be_substream =
1915 snd_soc_dpcm_get_substream(be, stream);
1917 /* is this op for this BE ? */
1918 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1921 /* copy params for each dpcm */
1922 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1923 sizeof(struct snd_pcm_hw_params));
1925 /* perform any hw_params fixups */
1926 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1930 /* copy the fixed-up hw params for BE dai */
1931 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1932 sizeof(struct snd_pcm_hw_params));
1934 /* only allow hw_params() if no connected FEs are running */
1935 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1938 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1940 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1943 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1944 be->dai_link->name);
1946 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1948 dev_err(dpcm->be->dev,
1949 "ASoC: hw_params BE failed %d\n", ret);
1953 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1958 /* disable any enabled and non active backends */
1959 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1960 struct snd_soc_pcm_runtime *be = dpcm->be;
1961 struct snd_pcm_substream *be_substream =
1962 snd_soc_dpcm_get_substream(be, stream);
1964 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1967 /* only allow hw_free() if no connected FEs are running */
1968 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1971 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1972 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1973 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1974 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1977 soc_pcm_hw_free(be_substream);
1983 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1984 struct snd_pcm_hw_params *params)
1986 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1987 int ret, stream = substream->stream;
1989 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1990 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1992 memcpy(&fe->dpcm[stream].hw_params, params,
1993 sizeof(struct snd_pcm_hw_params));
1994 ret = dpcm_be_dai_hw_params(fe, stream);
1996 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2000 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2001 fe->dai_link->name, params_rate(params),
2002 params_channels(params), params_format(params));
2004 /* call hw_params on the frontend */
2005 ret = soc_pcm_hw_params(substream, params);
2007 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2008 dpcm_be_dai_hw_free(fe, stream);
2010 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2013 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2014 mutex_unlock(&fe->card->mutex);
2018 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2021 struct snd_soc_dpcm *dpcm;
2024 for_each_dpcm_be(fe, stream, dpcm) {
2026 struct snd_soc_pcm_runtime *be = dpcm->be;
2027 struct snd_pcm_substream *be_substream =
2028 snd_soc_dpcm_get_substream(be, stream);
2030 /* is this op for this BE ? */
2031 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2034 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2035 be->dai_link->name, cmd);
2038 case SNDRV_PCM_TRIGGER_START:
2039 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2040 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2041 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2044 ret = soc_pcm_trigger(be_substream, cmd);
2048 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2050 case SNDRV_PCM_TRIGGER_RESUME:
2051 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2054 ret = soc_pcm_trigger(be_substream, cmd);
2058 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2060 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2061 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2064 ret = soc_pcm_trigger(be_substream, cmd);
2068 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2070 case SNDRV_PCM_TRIGGER_STOP:
2071 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2072 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2075 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2078 ret = soc_pcm_trigger(be_substream, cmd);
2082 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2084 case SNDRV_PCM_TRIGGER_SUSPEND:
2085 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2088 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2091 ret = soc_pcm_trigger(be_substream, cmd);
2095 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2097 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2098 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2101 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2104 ret = soc_pcm_trigger(be_substream, cmd);
2108 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2115 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2117 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2118 int cmd, bool fe_first)
2120 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2123 /* call trigger on the frontend before the backend. */
2125 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2126 fe->dai_link->name, cmd);
2128 ret = soc_pcm_trigger(substream, cmd);
2132 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2136 /* call trigger on the frontend after the backend. */
2137 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2141 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2142 fe->dai_link->name, cmd);
2144 ret = soc_pcm_trigger(substream, cmd);
2149 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2151 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2152 int stream = substream->stream;
2154 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2156 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2159 case SND_SOC_DPCM_TRIGGER_PRE:
2161 case SNDRV_PCM_TRIGGER_START:
2162 case SNDRV_PCM_TRIGGER_RESUME:
2163 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2164 case SNDRV_PCM_TRIGGER_DRAIN:
2165 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2167 case SNDRV_PCM_TRIGGER_STOP:
2168 case SNDRV_PCM_TRIGGER_SUSPEND:
2169 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2170 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2177 case SND_SOC_DPCM_TRIGGER_POST:
2179 case SNDRV_PCM_TRIGGER_START:
2180 case SNDRV_PCM_TRIGGER_RESUME:
2181 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2182 case SNDRV_PCM_TRIGGER_DRAIN:
2183 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2185 case SNDRV_PCM_TRIGGER_STOP:
2186 case SNDRV_PCM_TRIGGER_SUSPEND:
2187 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2188 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2195 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2196 /* bespoke trigger() - handles both FE and BEs */
2198 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2199 fe->dai_link->name, cmd);
2201 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2204 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2205 fe->dai_link->name);
2211 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2217 case SNDRV_PCM_TRIGGER_START:
2218 case SNDRV_PCM_TRIGGER_RESUME:
2219 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2220 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2222 case SNDRV_PCM_TRIGGER_STOP:
2223 case SNDRV_PCM_TRIGGER_SUSPEND:
2224 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2226 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2227 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2232 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2236 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2238 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2239 int stream = substream->stream;
2241 /* if FE's runtime_update is already set, we're in race;
2242 * process this trigger later at exit
2244 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2245 fe->dpcm[stream].trigger_pending = cmd + 1;
2246 return 0; /* delayed, assuming it's successful */
2249 /* we're alone, let's trigger */
2250 return dpcm_fe_dai_do_trigger(substream, cmd);
2253 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2255 struct snd_soc_dpcm *dpcm;
2258 for_each_dpcm_be(fe, stream, dpcm) {
2260 struct snd_soc_pcm_runtime *be = dpcm->be;
2261 struct snd_pcm_substream *be_substream =
2262 snd_soc_dpcm_get_substream(be, stream);
2264 /* is this op for this BE ? */
2265 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2268 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2269 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2270 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2271 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2274 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2275 be->dai_link->name);
2277 ret = soc_pcm_prepare(be_substream);
2279 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2284 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2289 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2291 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2292 int stream = substream->stream, ret = 0;
2294 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2296 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2298 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2300 /* there is no point preparing this FE if there are no BEs */
2301 if (list_empty(&fe->dpcm[stream].be_clients)) {
2302 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2303 fe->dai_link->name);
2308 ret = dpcm_be_dai_prepare(fe, stream);
2312 /* call prepare on the frontend */
2313 ret = soc_pcm_prepare(substream);
2315 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2316 fe->dai_link->name);
2320 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2323 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2324 mutex_unlock(&fe->card->mutex);
2329 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2331 struct snd_pcm_substream *substream =
2332 snd_soc_dpcm_get_substream(fe, stream);
2333 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2336 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2337 stream ? "capture" : "playback", fe->dai_link->name);
2339 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2340 /* call bespoke trigger - FE takes care of all BE triggers */
2341 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2342 fe->dai_link->name);
2344 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2346 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2348 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2349 fe->dai_link->name);
2351 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2353 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2356 err = dpcm_be_dai_hw_free(fe, stream);
2358 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2360 err = dpcm_be_dai_shutdown(fe, stream);
2362 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2364 /* run the stream event for each BE */
2365 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2370 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2372 struct snd_pcm_substream *substream =
2373 snd_soc_dpcm_get_substream(fe, stream);
2374 struct snd_soc_dpcm *dpcm;
2375 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2377 unsigned long flags;
2379 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2380 stream ? "capture" : "playback", fe->dai_link->name);
2382 /* Only start the BE if the FE is ready */
2383 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2384 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2385 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2386 fe->dai_link->name, fe->dpcm[stream].state);
2391 /* startup must always be called for new BEs */
2392 ret = dpcm_be_dai_startup(fe, stream);
2396 /* keep going if FE state is > open */
2397 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2400 ret = dpcm_be_dai_hw_params(fe, stream);
2404 /* keep going if FE state is > hw_params */
2405 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2409 ret = dpcm_be_dai_prepare(fe, stream);
2413 /* run the stream event for each BE */
2414 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2416 /* keep going if FE state is > prepare */
2417 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2418 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2421 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2422 /* call trigger on the frontend - FE takes care of all BE triggers */
2423 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2424 fe->dai_link->name);
2426 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2428 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2432 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2433 fe->dai_link->name);
2435 ret = dpcm_be_dai_trigger(fe, stream,
2436 SNDRV_PCM_TRIGGER_START);
2438 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2446 dpcm_be_dai_hw_free(fe, stream);
2448 dpcm_be_dai_shutdown(fe, stream);
2450 /* disconnect any pending BEs */
2451 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2452 for_each_dpcm_be(fe, stream, dpcm) {
2453 struct snd_soc_pcm_runtime *be = dpcm->be;
2455 /* is this op for this BE ? */
2456 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2459 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2460 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2461 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2463 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2468 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2470 struct snd_soc_dapm_widget_list *list;
2475 if (!fe->dai_link->dynamic)
2478 if (fe->num_cpus > 1) {
2480 "%s doesn't support Multi CPU yet\n", __func__);
2484 /* only check active links */
2485 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2488 /* DAPM sync will call this to update DSP paths */
2489 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2490 new ? "new" : "old", fe->dai_link->name);
2492 for_each_pcm_streams(stream) {
2494 /* skip if FE doesn't have playback/capture capability */
2495 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2496 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2499 /* skip if FE isn't currently playing/capturing */
2500 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2501 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2504 paths = dpcm_path_get(fe, stream, &list);
2506 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2508 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2509 "playback" : "capture");
2513 /* update any playback/capture paths */
2514 count = dpcm_process_paths(fe, stream, &list, new);
2516 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2518 ret = dpcm_run_update_startup(fe, stream);
2520 ret = dpcm_run_update_shutdown(fe, stream);
2522 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2523 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2525 dpcm_clear_pending_state(fe, stream);
2526 dpcm_be_disconnect(fe, stream);
2529 dpcm_path_put(&list);
2535 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2538 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2540 struct snd_soc_pcm_runtime *fe;
2543 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2544 /* shutdown all old paths first */
2545 for_each_card_rtds(card, fe) {
2546 ret = soc_dpcm_fe_runtime_update(fe, 0);
2551 /* bring new paths up */
2552 for_each_card_rtds(card, fe) {
2553 ret = soc_dpcm_fe_runtime_update(fe, 1);
2559 mutex_unlock(&card->mutex);
2562 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2564 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2566 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2567 struct snd_soc_dpcm *dpcm;
2568 int stream = fe_substream->stream;
2570 /* mark FE's links ready to prune */
2571 for_each_dpcm_be(fe, stream, dpcm)
2572 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2574 dpcm_be_disconnect(fe, stream);
2576 fe->dpcm[stream].runtime = NULL;
2579 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2581 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2584 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2585 ret = dpcm_fe_dai_shutdown(fe_substream);
2587 dpcm_fe_dai_cleanup(fe_substream);
2589 mutex_unlock(&fe->card->mutex);
2593 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2595 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2596 struct snd_soc_dapm_widget_list *list;
2598 int stream = fe_substream->stream;
2600 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2601 fe->dpcm[stream].runtime = fe_substream->runtime;
2603 ret = dpcm_path_get(fe, stream, &list);
2606 } else if (ret == 0) {
2607 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2608 fe->dai_link->name, stream ? "capture" : "playback");
2611 /* calculate valid and active FE <-> BE dpcms */
2612 dpcm_process_paths(fe, stream, &list, 1);
2614 ret = dpcm_fe_dai_startup(fe_substream);
2616 dpcm_fe_dai_cleanup(fe_substream);
2618 dpcm_clear_pending_state(fe, stream);
2619 dpcm_path_put(&list);
2621 mutex_unlock(&fe->card->mutex);
2625 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2626 int *playback, int *capture)
2628 struct snd_soc_dai *codec_dai;
2629 struct snd_soc_dai *cpu_dai;
2633 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2635 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2639 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2640 if (rtd->dai_link->dpcm_playback) {
2641 stream = SNDRV_PCM_STREAM_PLAYBACK;
2643 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2644 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2650 dev_err(rtd->card->dev,
2651 "No CPU DAIs support playback for stream %s\n",
2652 rtd->dai_link->stream_name);
2656 if (rtd->dai_link->dpcm_capture) {
2657 stream = SNDRV_PCM_STREAM_CAPTURE;
2659 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2660 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2667 dev_err(rtd->card->dev,
2668 "No CPU DAIs support capture for stream %s\n",
2669 rtd->dai_link->stream_name);
2674 /* Adapt stream for codec2codec links */
2675 int cpu_capture = rtd->dai_link->params ?
2676 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2677 int cpu_playback = rtd->dai_link->params ?
2678 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2680 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2681 if (rtd->num_cpus == 1) {
2682 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2683 } else if (rtd->num_cpus == rtd->num_codecs) {
2684 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2686 dev_err(rtd->card->dev,
2687 "N cpus to M codecs link is not supported yet\n");
2691 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2692 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2694 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2695 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2700 if (rtd->dai_link->playback_only) {
2705 if (rtd->dai_link->capture_only) {
2713 static int soc_create_pcm(struct snd_pcm **pcm,
2714 struct snd_soc_pcm_runtime *rtd,
2715 int playback, int capture, int num)
2720 /* create the PCM */
2721 if (rtd->dai_link->params) {
2722 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2723 rtd->dai_link->stream_name);
2725 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2726 playback, capture, pcm);
2727 } else if (rtd->dai_link->no_pcm) {
2728 snprintf(new_name, sizeof(new_name), "(%s)",
2729 rtd->dai_link->stream_name);
2731 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2732 playback, capture, pcm);
2734 if (rtd->dai_link->dynamic)
2735 snprintf(new_name, sizeof(new_name), "%s (*)",
2736 rtd->dai_link->stream_name);
2738 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2739 rtd->dai_link->stream_name,
2740 soc_codec_dai_name(rtd), num);
2742 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2746 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2747 new_name, rtd->dai_link->name, ret);
2750 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2755 /* create a new pcm */
2756 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2758 struct snd_soc_component *component;
2759 struct snd_pcm *pcm;
2760 int ret = 0, playback = 0, capture = 0;
2763 ret = soc_get_playback_capture(rtd, &playback, &capture);
2767 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2771 /* DAPM dai link stream work */
2772 if (rtd->dai_link->params)
2773 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2775 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2778 pcm->nonatomic = rtd->dai_link->nonatomic;
2779 pcm->private_data = rtd;
2781 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2783 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2785 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2789 /* ASoC PCM operations */
2790 if (rtd->dai_link->dynamic) {
2791 rtd->ops.open = dpcm_fe_dai_open;
2792 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2793 rtd->ops.prepare = dpcm_fe_dai_prepare;
2794 rtd->ops.trigger = dpcm_fe_dai_trigger;
2795 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2796 rtd->ops.close = dpcm_fe_dai_close;
2797 rtd->ops.pointer = soc_pcm_pointer;
2799 rtd->ops.open = soc_pcm_open;
2800 rtd->ops.hw_params = soc_pcm_hw_params;
2801 rtd->ops.prepare = soc_pcm_prepare;
2802 rtd->ops.trigger = soc_pcm_trigger;
2803 rtd->ops.hw_free = soc_pcm_hw_free;
2804 rtd->ops.close = soc_pcm_close;
2805 rtd->ops.pointer = soc_pcm_pointer;
2808 for_each_rtd_components(rtd, i, component) {
2809 const struct snd_soc_component_driver *drv = component->driver;
2812 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2814 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2816 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2818 rtd->ops.page = snd_soc_pcm_component_page;
2820 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2824 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2827 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2829 ret = snd_soc_pcm_component_new(rtd);
2831 dev_err(rtd->dev, "ASoC: pcm constructor failed for dailink %s: %d\n",
2832 rtd->dai_link->name, ret);
2836 pcm->no_device_suspend = true;
2838 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2839 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2843 /* is the current PCM operation for this FE ? */
2844 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2846 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2850 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2852 /* is the current PCM operation for this BE ? */
2853 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2854 struct snd_soc_pcm_runtime *be, int stream)
2856 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2857 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2858 be->dpcm[stream].runtime_update))
2862 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2864 /* get the substream for this BE */
2865 struct snd_pcm_substream *
2866 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2868 return be->pcm->streams[stream].substream;
2870 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2872 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2873 struct snd_soc_pcm_runtime *be,
2875 const enum snd_soc_dpcm_state *states,
2878 struct snd_soc_dpcm *dpcm;
2881 unsigned long flags;
2884 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2885 for_each_dpcm_fe(be, stream, dpcm) {
2890 state = dpcm->fe->dpcm[stream].state;
2891 for (i = 0; i < num_states; i++) {
2892 if (state == states[i]) {
2898 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2900 /* it's safe to do this BE DAI */
2905 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2906 * are not running, paused or suspended for the specified stream direction.
2908 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2909 struct snd_soc_pcm_runtime *be, int stream)
2911 const enum snd_soc_dpcm_state state[] = {
2912 SND_SOC_DPCM_STATE_START,
2913 SND_SOC_DPCM_STATE_PAUSED,
2914 SND_SOC_DPCM_STATE_SUSPEND,
2917 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2919 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2922 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2923 * running, paused or suspended for the specified stream direction.
2925 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2926 struct snd_soc_pcm_runtime *be, int stream)
2928 const enum snd_soc_dpcm_state state[] = {
2929 SND_SOC_DPCM_STATE_START,
2930 SND_SOC_DPCM_STATE_PAUSED,
2931 SND_SOC_DPCM_STATE_SUSPEND,
2932 SND_SOC_DPCM_STATE_PREPARE,
2935 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2937 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);