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)
168 if (!rtd->dai_link->dynamic)
171 if (!rtd->card->debugfs_card_root)
174 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
175 rtd->card->debugfs_card_root);
177 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
178 rtd, &dpcm_state_fops);
181 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
185 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
186 stream ? "capture" : "playback");
188 dpcm->debugfs_state = debugfs_create_dir(
189 name, dpcm->fe->debugfs_dpcm_root);
190 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
196 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
198 debugfs_remove_recursive(dpcm->debugfs_state);
202 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
207 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
212 /* Set FE's runtime_update state; the state is protected via PCM stream lock
213 * for avoiding the race with trigger callback.
214 * If the state is unset and a trigger is pending while the previous operation,
215 * process the pending trigger action here.
217 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
218 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
219 int stream, enum snd_soc_dpcm_update state)
221 struct snd_pcm_substream *substream =
222 snd_soc_dpcm_get_substream(fe, stream);
224 snd_pcm_stream_lock_irq(substream);
225 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
226 dpcm_fe_dai_do_trigger(substream,
227 fe->dpcm[stream].trigger_pending - 1);
228 fe->dpcm[stream].trigger_pending = 0;
230 fe->dpcm[stream].runtime_update = state;
231 snd_pcm_stream_unlock_irq(substream);
234 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
235 int stream, enum snd_soc_dpcm_update state)
237 be->dpcm[stream].runtime_update = state;
241 * snd_soc_runtime_action() - Increment/Decrement active count for
242 * PCM runtime components
243 * @rtd: ASoC PCM runtime that is activated
244 * @stream: Direction of the PCM stream
245 * @action: Activate stream if 1. Deactivate if -1.
247 * Increments/Decrements the active count for all the DAIs and components
248 * attached to a PCM runtime.
249 * Should typically be called when a stream is opened.
251 * Must be called with the rtd->card->pcm_mutex being held
253 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
254 int stream, int action)
256 struct snd_soc_dai *dai;
259 lockdep_assert_held(&rtd->card->pcm_mutex);
261 for_each_rtd_dais(rtd, i, dai)
262 snd_soc_dai_action(dai, stream, action);
264 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
267 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
268 * @rtd: The ASoC PCM runtime that should be checked.
270 * This function checks whether the power down delay should be ignored for a
271 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
272 * been configured to ignore the delay, or if none of the components benefits
273 * from having the delay.
275 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
277 struct snd_soc_component *component;
281 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
284 for_each_rtd_components(rtd, i, component)
285 ignore &= !component->driver->use_pmdown_time;
291 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
292 * @substream: the pcm substream
293 * @hw: the hardware parameters
295 * Sets the substream runtime hardware parameters.
297 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
298 const struct snd_pcm_hardware *hw)
300 substream->runtime->hw = *hw;
304 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
306 /* DPCM stream event, send event to FE and all active BEs. */
307 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
310 struct snd_soc_dpcm *dpcm;
312 for_each_dpcm_be(fe, dir, dpcm) {
314 struct snd_soc_pcm_runtime *be = dpcm->be;
316 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
317 be->dai_link->name, event, dir);
319 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
320 (be->dpcm[dir].users >= 1))
323 snd_soc_dapm_stream_event(be, dir, event);
326 snd_soc_dapm_stream_event(fe, dir, event);
331 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
332 struct snd_pcm_hw_params *params)
335 dai->rate = params_rate(params);
336 dai->channels = params_channels(params);
337 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
341 dai->sample_bits = 0;
345 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
346 struct snd_soc_dai *soc_dai)
348 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
351 if (!snd_soc_dai_active(soc_dai))
354 #define __soc_pcm_apply_symmetry(name, NAME) \
355 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
356 rtd->dai_link->symmetric_##name)) { \
357 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
358 #name, soc_dai->name); \
360 ret = snd_pcm_hw_constraint_single(substream->runtime, \
361 SNDRV_PCM_HW_PARAM_##NAME,\
364 dev_err(soc_dai->dev, \
365 "ASoC: Unable to apply %s constraint: %d\n",\
371 __soc_pcm_apply_symmetry(rate, RATE);
372 __soc_pcm_apply_symmetry(channels, CHANNELS);
373 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
378 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
379 struct snd_pcm_hw_params *params)
381 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
382 struct snd_soc_dai d;
383 struct snd_soc_dai *dai;
384 struct snd_soc_dai *cpu_dai;
385 unsigned int symmetry, i;
387 soc_pcm_set_dai_params(&d, params);
389 #define __soc_pcm_params_symmetry(name) \
390 symmetry = rtd->dai_link->symmetric_##name; \
391 for_each_rtd_dais(rtd, i, dai) \
392 symmetry |= dai->driver->symmetric_##name; \
395 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
396 if (cpu_dai->name && cpu_dai->name != d.name) { \
397 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %d - %d\n", \
398 #name, cpu_dai->name, d.name); \
402 /* reject unmatched parameters when applying symmetry */
403 __soc_pcm_params_symmetry(rate);
404 __soc_pcm_params_symmetry(channels);
405 __soc_pcm_params_symmetry(sample_bits);
410 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
412 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
413 struct snd_soc_dai_link *link = rtd->dai_link;
414 struct snd_soc_dai *dai;
415 unsigned int symmetry, i;
417 symmetry = link->symmetric_rate ||
418 link->symmetric_channels ||
419 link->symmetric_sample_bits;
421 for_each_rtd_dais(rtd, i, dai)
422 symmetry = symmetry ||
423 dai->driver->symmetric_rate ||
424 dai->driver->symmetric_channels ||
425 dai->driver->symmetric_sample_bits;
428 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
431 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
433 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
439 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
441 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
445 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
447 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
448 struct snd_soc_dai *cpu_dai;
449 struct snd_soc_dai *codec_dai;
450 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
451 int stream = substream->stream;
453 unsigned int bits = 0, cpu_bits = 0;
455 for_each_rtd_codec_dais(rtd, i, codec_dai) {
456 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
458 if (pcm_codec->sig_bits == 0) {
462 bits = max(pcm_codec->sig_bits, bits);
465 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
466 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
468 if (pcm_cpu->sig_bits == 0) {
472 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
475 soc_pcm_set_msb(substream, bits);
476 soc_pcm_set_msb(substream, cpu_bits);
479 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
481 hw->rates = UINT_MAX;
483 hw->rate_max = UINT_MAX;
484 hw->channels_min = 0;
485 hw->channels_max = UINT_MAX;
486 hw->formats = ULLONG_MAX;
489 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
490 struct snd_soc_pcm_stream *p)
492 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
494 /* setup hw->rate_min/max via hw->rates first */
495 snd_pcm_hw_limit_rates(hw);
497 /* update hw->rate_min/max by snd_soc_pcm_stream */
498 hw->rate_min = max(hw->rate_min, p->rate_min);
499 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
502 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
503 struct snd_soc_pcm_stream *p)
505 hw->channels_min = max(hw->channels_min, p->channels_min);
506 hw->channels_max = min(hw->channels_max, p->channels_max);
509 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
510 struct snd_soc_pcm_stream *p)
512 hw->formats &= p->formats;
516 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
517 * @rtd: ASoC PCM runtime
518 * @hw: PCM hardware parameters (output)
519 * @stream: Direction of the PCM stream
521 * Calculates the subset of stream parameters supported by all DAIs
522 * associated with the PCM stream.
524 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
525 struct snd_pcm_hardware *hw, int stream)
527 struct snd_soc_dai *codec_dai;
528 struct snd_soc_dai *cpu_dai;
529 struct snd_soc_pcm_stream *codec_stream;
530 struct snd_soc_pcm_stream *cpu_stream;
531 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
536 /* first calculate min/max only for CPUs in the DAI link */
537 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
540 * Skip CPUs which don't support the current stream type.
541 * Otherwise, since the rate, channel, and format values will
542 * zero in that case, we would have no usable settings left,
543 * causing the resulting setup to fail.
545 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
548 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
550 soc_pcm_hw_update_chan(hw, cpu_stream);
551 soc_pcm_hw_update_rate(hw, cpu_stream);
552 soc_pcm_hw_update_format(hw, cpu_stream);
554 cpu_chan_min = hw->channels_min;
555 cpu_chan_max = hw->channels_max;
557 /* second calculate min/max only for CODECs in the DAI link */
558 for_each_rtd_codec_dais(rtd, i, codec_dai) {
561 * Skip CODECs which don't support the current stream type.
562 * Otherwise, since the rate, channel, and format values will
563 * zero in that case, we would have no usable settings left,
564 * causing the resulting setup to fail.
566 if (!snd_soc_dai_stream_valid(codec_dai, stream))
569 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
571 soc_pcm_hw_update_chan(hw, codec_stream);
572 soc_pcm_hw_update_rate(hw, codec_stream);
573 soc_pcm_hw_update_format(hw, codec_stream);
576 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
577 if (!hw->channels_min)
581 * chan min/max cannot be enforced if there are multiple CODEC DAIs
582 * connected to CPU DAI(s), use CPU DAI's directly and let
583 * channel allocation be fixed up later
585 if (rtd->num_codecs > 1) {
586 hw->channels_min = cpu_chan_min;
587 hw->channels_max = cpu_chan_max;
592 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
594 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
596 struct snd_pcm_hardware *hw = &substream->runtime->hw;
597 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
598 u64 formats = hw->formats;
601 * At least one CPU and one CODEC should match. Otherwise, we should
602 * have bailed out on a higher level, since there would be no CPU or
603 * CODEC to support the transfer direction in that case.
605 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
608 hw->formats &= formats;
611 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
613 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
614 struct snd_soc_component *component;
617 for_each_rtd_components(rtd, i, component) {
618 ret = snd_soc_component_module_get_when_open(component, substream);
622 ret = snd_soc_component_open(component, substream);
630 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
633 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
634 struct snd_soc_component *component;
637 for_each_rtd_components(rtd, i, component) {
638 r = snd_soc_component_close(component, substream, rollback);
640 ret = r; /* use last ret */
642 snd_soc_component_module_put_when_close(component, substream, rollback);
648 static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
650 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
651 struct snd_soc_component *component;
652 struct snd_soc_dai *dai;
655 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
658 snd_soc_runtime_deactivate(rtd, substream->stream);
660 for_each_rtd_dais(rtd, i, dai)
661 snd_soc_dai_shutdown(dai, substream, rollback);
663 snd_soc_link_shutdown(substream, rollback);
665 soc_pcm_components_close(substream, rollback);
668 mutex_unlock(&rtd->card->pcm_mutex);
670 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
672 for_each_rtd_components(rtd, i, component)
673 if (!snd_soc_component_active(component))
674 pinctrl_pm_select_sleep_state(component->dev);
680 * Called by ALSA when a PCM substream is closed. Private data can be
681 * freed here. The cpu DAI, codec DAI, machine and components are also
684 static int soc_pcm_close(struct snd_pcm_substream *substream)
686 return soc_pcm_clean(substream, 0);
689 static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
691 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
692 struct snd_pcm_hardware *hw = &substream->runtime->hw;
693 const char *name_cpu = soc_cpu_dai_name(rtd);
694 const char *name_codec = soc_codec_dai_name(rtd);
696 struct device *dev = rtd->dev;
706 err_msg = "channels";
707 if (!hw->channels_min || !hw->channels_max ||
708 hw->channels_min > hw->channels_max)
711 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
713 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
714 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
716 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
722 dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
723 name_codec, name_cpu, err_msg);
728 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
729 * then initialized and any private data can be allocated. This also calls
730 * startup for the cpu DAI, component, machine and codec DAI.
732 static int soc_pcm_open(struct snd_pcm_substream *substream)
734 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
735 struct snd_soc_component *component;
736 struct snd_soc_dai *dai;
739 for_each_rtd_components(rtd, i, component)
740 pinctrl_pm_select_default_state(component->dev);
742 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
746 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
748 ret = soc_pcm_components_open(substream);
752 ret = snd_soc_link_startup(substream);
756 /* startup the audio subsystem */
757 for_each_rtd_dais(rtd, i, dai) {
758 ret = snd_soc_dai_startup(dai, substream);
762 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
768 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
769 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
772 /* Check that the codec and cpu DAIs are compatible */
773 soc_pcm_init_runtime_hw(substream);
775 soc_pcm_update_symmetry(substream);
777 ret = soc_hw_sanity_check(substream);
781 soc_pcm_apply_msb(substream);
783 /* Symmetry only applies if we've already got an active stream. */
784 for_each_rtd_dais(rtd, i, dai) {
785 ret = soc_pcm_apply_symmetry(substream, dai);
790 snd_soc_runtime_activate(rtd, substream->stream);
793 mutex_unlock(&rtd->card->pcm_mutex);
796 soc_pcm_clean(substream, 1);
797 dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
803 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
806 * Currently nothing to do for c2c links
807 * Since c2c links are internal nodes in the DAPM graph and
808 * don't interface with the outside world or application layer
809 * we don't have to do any special handling on close.
814 * Called by ALSA when the PCM substream is prepared, can set format, sample
815 * rate, etc. This function is non atomic and can be called multiple times,
816 * it can refer to the runtime info.
818 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
820 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
821 struct snd_soc_dai *dai;
824 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
826 ret = snd_soc_link_prepare(substream);
830 ret = snd_soc_pcm_component_prepare(substream);
834 ret = snd_soc_pcm_dai_prepare(substream);
838 /* cancel any delayed stream shutdown that is pending */
839 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
842 cancel_delayed_work(&rtd->delayed_work);
845 snd_soc_dapm_stream_event(rtd, substream->stream,
846 SND_SOC_DAPM_STREAM_START);
848 for_each_rtd_dais(rtd, i, dai)
849 snd_soc_dai_digital_mute(dai, 0, substream->stream);
852 mutex_unlock(&rtd->card->pcm_mutex);
855 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
860 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
863 struct snd_interval *interval;
864 int channels = hweight_long(mask);
866 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
867 interval->min = channels;
868 interval->max = channels;
871 static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
873 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
874 struct snd_soc_dai *dai;
877 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
879 /* clear the corresponding DAIs parameters when going to be inactive */
880 for_each_rtd_dais(rtd, i, dai) {
881 int active = snd_soc_dai_stream_active(dai, substream->stream);
883 if (snd_soc_dai_active(dai) == 1)
884 soc_pcm_set_dai_params(dai, NULL);
887 snd_soc_dai_digital_mute(dai, 1, substream->stream);
890 /* run the stream event */
891 snd_soc_dapm_stream_stop(rtd, substream->stream);
893 /* free any machine hw params */
894 snd_soc_link_hw_free(substream, rollback);
896 /* free any component resources */
897 snd_soc_pcm_component_hw_free(substream, rollback);
899 /* now free hw params for the DAIs */
900 for_each_rtd_dais(rtd, i, dai) {
901 if (!snd_soc_dai_stream_valid(dai, substream->stream))
904 snd_soc_dai_hw_free(dai, substream, rollback);
907 mutex_unlock(&rtd->card->pcm_mutex);
912 * Frees resources allocated by hw_params, can be called multiple times
914 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
916 return soc_pcm_hw_clean(substream, 0);
920 * Called by ALSA when the hardware params are set by application. This
921 * function can also be called multiple times and can allocate buffers
922 * (using snd_pcm_lib_* ). It's non-atomic.
924 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
925 struct snd_pcm_hw_params *params)
927 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
928 struct snd_soc_dai *cpu_dai;
929 struct snd_soc_dai *codec_dai;
932 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
934 ret = soc_pcm_params_symmetry(substream, params);
938 ret = snd_soc_link_hw_params(substream, params);
942 for_each_rtd_codec_dais(rtd, i, codec_dai) {
943 struct snd_pcm_hw_params codec_params;
946 * Skip CODECs which don't support the current stream type,
947 * the idea being that if a CODEC is not used for the currently
948 * set up transfer direction, it should not need to be
949 * configured, especially since the configuration used might
950 * not even be supported by that CODEC. There may be cases
951 * however where a CODEC needs to be set up although it is
952 * actually not being used for the transfer, e.g. if a
953 * capture-only CODEC is acting as an LRCLK and/or BCLK master
954 * for the DAI link including a playback-only CODEC.
955 * If this becomes necessary, we will have to augment the
956 * machine driver setup with information on how to act, so
957 * we can do the right thing here.
959 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
962 /* copy params for each codec */
963 codec_params = *params;
965 /* fixup params based on TDM slot masks */
966 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
968 soc_pcm_codec_params_fixup(&codec_params,
971 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
973 soc_pcm_codec_params_fixup(&codec_params,
976 ret = snd_soc_dai_hw_params(codec_dai, substream,
981 soc_pcm_set_dai_params(codec_dai, &codec_params);
982 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
985 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
987 * Skip CPUs which don't support the current stream
988 * type. See soc_pcm_init_runtime_hw() for more details
990 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
993 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
997 /* store the parameters for each DAI */
998 soc_pcm_set_dai_params(cpu_dai, params);
999 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1002 ret = snd_soc_pcm_component_hw_params(substream, params);
1004 mutex_unlock(&rtd->card->pcm_mutex);
1007 soc_pcm_hw_clean(substream, 1);
1008 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1014 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1016 int ret = -EINVAL, _ret = 0;
1020 case SNDRV_PCM_TRIGGER_START:
1021 case SNDRV_PCM_TRIGGER_RESUME:
1022 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1023 ret = snd_soc_link_trigger(substream, cmd, 0);
1027 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1031 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1040 case SNDRV_PCM_TRIGGER_START:
1041 cmd = SNDRV_PCM_TRIGGER_STOP;
1043 case SNDRV_PCM_TRIGGER_RESUME:
1044 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1046 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1047 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1053 case SNDRV_PCM_TRIGGER_STOP:
1054 case SNDRV_PCM_TRIGGER_SUSPEND:
1055 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1056 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1060 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1064 ret = snd_soc_link_trigger(substream, cmd, rollback);
1075 * soc level wrapper for pointer callback
1076 * If cpu_dai, codec_dai, component driver has the delay callback, then
1077 * the runtime->delay will be updated accordingly.
1079 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1081 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1082 struct snd_soc_dai *cpu_dai;
1083 struct snd_soc_dai *codec_dai;
1084 struct snd_pcm_runtime *runtime = substream->runtime;
1085 snd_pcm_uframes_t offset = 0;
1086 snd_pcm_sframes_t delay = 0;
1087 snd_pcm_sframes_t codec_delay = 0;
1088 snd_pcm_sframes_t cpu_delay = 0;
1091 /* clearing the previous total delay */
1094 offset = snd_soc_pcm_component_pointer(substream);
1096 /* base delay if assigned in pointer callback */
1097 delay = runtime->delay;
1099 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1100 cpu_delay = max(cpu_delay,
1101 snd_soc_dai_delay(cpu_dai, substream));
1105 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1106 codec_delay = max(codec_delay,
1107 snd_soc_dai_delay(codec_dai, substream));
1109 delay += codec_delay;
1111 runtime->delay = delay;
1116 /* connect a FE and BE */
1117 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1118 struct snd_soc_pcm_runtime *be, int stream)
1120 struct snd_soc_dpcm *dpcm;
1121 unsigned long flags;
1123 /* only add new dpcms */
1124 for_each_dpcm_be(fe, stream, dpcm) {
1125 if (dpcm->be == be && dpcm->fe == fe)
1129 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1135 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1136 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1137 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1138 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1139 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1140 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1142 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1143 stream ? "capture" : "playback", fe->dai_link->name,
1144 stream ? "<-" : "->", be->dai_link->name);
1146 dpcm_create_debugfs_state(dpcm, stream);
1151 /* reparent a BE onto another FE */
1152 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1153 struct snd_soc_pcm_runtime *be, int stream)
1155 struct snd_soc_dpcm *dpcm;
1156 struct snd_pcm_substream *fe_substream, *be_substream;
1158 /* reparent if BE is connected to other FEs */
1159 if (!be->dpcm[stream].users)
1162 be_substream = snd_soc_dpcm_get_substream(be, stream);
1164 for_each_dpcm_fe(be, stream, dpcm) {
1168 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1169 stream ? "capture" : "playback",
1170 dpcm->fe->dai_link->name,
1171 stream ? "<-" : "->", dpcm->be->dai_link->name);
1173 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1174 be_substream->runtime = fe_substream->runtime;
1179 /* disconnect a BE and FE */
1180 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1182 struct snd_soc_dpcm *dpcm, *d;
1183 unsigned long flags;
1185 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1186 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1187 stream ? "capture" : "playback",
1188 dpcm->be->dai_link->name);
1190 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1193 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1194 stream ? "capture" : "playback", fe->dai_link->name,
1195 stream ? "<-" : "->", dpcm->be->dai_link->name);
1197 /* BEs still alive need new FE */
1198 dpcm_be_reparent(fe, dpcm->be, stream);
1200 dpcm_remove_debugfs_state(dpcm);
1202 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1203 list_del(&dpcm->list_be);
1204 list_del(&dpcm->list_fe);
1205 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1210 /* get BE for DAI widget and stream */
1211 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1212 struct snd_soc_dapm_widget *widget, int stream)
1214 struct snd_soc_pcm_runtime *be;
1215 struct snd_soc_dapm_widget *w;
1216 struct snd_soc_dai *dai;
1219 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1221 for_each_card_rtds(card, be) {
1223 if (!be->dai_link->no_pcm)
1226 for_each_rtd_dais(be, i, dai) {
1227 w = snd_soc_dai_get_widget(dai, stream);
1229 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1230 w ? w->name : "(not set)");
1237 /* Widget provided is not a BE */
1241 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1242 struct snd_soc_dapm_widget *widget)
1244 struct snd_soc_dapm_widget *w;
1247 for_each_dapm_widgets(list, i, w)
1254 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1255 enum snd_soc_dapm_direction dir)
1257 struct snd_soc_card *card = widget->dapm->card;
1258 struct snd_soc_pcm_runtime *rtd;
1261 /* adjust dir to stream */
1262 if (dir == SND_SOC_DAPM_DIR_OUT)
1263 stream = SNDRV_PCM_STREAM_PLAYBACK;
1265 stream = SNDRV_PCM_STREAM_CAPTURE;
1267 rtd = dpcm_get_be(card, widget, stream);
1274 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1275 int stream, struct snd_soc_dapm_widget_list **list)
1277 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1280 if (fe->num_cpus > 1) {
1282 "%s doesn't support Multi CPU yet\n", __func__);
1286 /* get number of valid DAI paths and their widgets */
1287 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1288 fe->card->component_chaining ?
1289 NULL : dpcm_end_walk_at_be);
1292 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1293 stream ? "capture" : "playback");
1294 else if (paths == 0)
1295 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1296 stream ? "capture" : "playback");
1301 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1303 snd_soc_dapm_dai_free_widgets(list);
1306 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1307 struct snd_soc_dapm_widget_list *list)
1309 struct snd_soc_dapm_widget *widget;
1310 struct snd_soc_dai *dai;
1313 /* is there a valid DAI widget for this BE */
1314 for_each_rtd_dais(dpcm->be, i, dai) {
1315 widget = snd_soc_dai_get_widget(dai, stream);
1318 * The BE is pruned only if none of the dai
1319 * widgets are in the active list.
1321 if (widget && widget_in_list(list, widget))
1328 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1329 struct snd_soc_dapm_widget_list **list_)
1331 struct snd_soc_dpcm *dpcm;
1334 /* Destroy any old FE <--> BE connections */
1335 for_each_dpcm_be(fe, stream, dpcm) {
1336 if (dpcm_be_is_active(dpcm, stream, *list_))
1339 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1340 stream ? "capture" : "playback",
1341 dpcm->be->dai_link->name, fe->dai_link->name);
1342 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1343 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1347 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1351 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1352 struct snd_soc_dapm_widget_list **list_)
1354 struct snd_soc_card *card = fe->card;
1355 struct snd_soc_dapm_widget_list *list = *list_;
1356 struct snd_soc_pcm_runtime *be;
1357 struct snd_soc_dapm_widget *widget;
1358 int i, new = 0, err;
1360 /* Create any new FE <--> BE connections */
1361 for_each_dapm_widgets(list, i, widget) {
1363 switch (widget->id) {
1364 case snd_soc_dapm_dai_in:
1365 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1368 case snd_soc_dapm_dai_out:
1369 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1376 /* is there a valid BE rtd for this widget */
1377 be = dpcm_get_be(card, widget, stream);
1379 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1384 /* don't connect if FE is not running */
1385 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1388 /* newly connected FE and BE */
1389 err = dpcm_be_connect(fe, be, stream);
1391 dev_err(fe->dev, "ASoC: can't connect %s\n",
1394 } else if (err == 0) /* already connected */
1398 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1402 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1407 * Find the corresponding BE DAIs that source or sink audio to this
1410 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1411 int stream, struct snd_soc_dapm_widget_list **list, int new)
1414 return dpcm_add_paths(fe, stream, list);
1416 return dpcm_prune_paths(fe, stream, list);
1419 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1421 struct snd_soc_dpcm *dpcm;
1422 unsigned long flags;
1424 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1425 for_each_dpcm_be(fe, stream, dpcm)
1426 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1427 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1430 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1431 int do_hw_free, struct snd_soc_dpcm *last)
1433 struct snd_soc_dpcm *dpcm;
1435 /* disable any enabled and non active backends */
1436 for_each_dpcm_be(fe, stream, dpcm) {
1437 struct snd_soc_pcm_runtime *be = dpcm->be;
1438 struct snd_pcm_substream *be_substream =
1439 snd_soc_dpcm_get_substream(be, stream);
1444 /* is this op for this BE ? */
1445 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1448 if (be->dpcm[stream].users == 0) {
1449 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1450 stream ? "capture" : "playback",
1451 be->dpcm[stream].state);
1455 if (--be->dpcm[stream].users != 0)
1458 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1462 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1463 soc_pcm_hw_free(be_substream);
1464 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1468 soc_pcm_close(be_substream);
1469 be_substream->runtime = NULL;
1470 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1474 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1476 struct snd_soc_pcm_runtime *be;
1477 struct snd_soc_dpcm *dpcm;
1480 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1481 for_each_dpcm_be(fe, stream, dpcm) {
1482 struct snd_pcm_substream *be_substream;
1485 be_substream = snd_soc_dpcm_get_substream(be, stream);
1487 if (!be_substream) {
1488 dev_err(be->dev, "ASoC: no backend %s stream\n",
1489 stream ? "capture" : "playback");
1493 /* is this op for this BE ? */
1494 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1497 /* first time the dpcm is open ? */
1498 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1499 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1500 stream ? "capture" : "playback",
1501 be->dpcm[stream].state);
1505 if (be->dpcm[stream].users++ != 0)
1508 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1509 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1512 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1513 stream ? "capture" : "playback", be->dai_link->name);
1515 be_substream->runtime = be->dpcm[stream].runtime;
1516 err = soc_pcm_open(be_substream);
1518 be->dpcm[stream].users--;
1519 if (be->dpcm[stream].users < 0)
1520 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1521 stream ? "capture" : "playback",
1522 be->dpcm[stream].state);
1524 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1535 dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1537 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1538 __func__, be->dai_link->name, err);
1543 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1545 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1546 struct snd_pcm_runtime *runtime = substream->runtime;
1547 struct snd_pcm_hardware *hw = &runtime->hw;
1548 struct snd_soc_dai *dai;
1549 int stream = substream->stream;
1552 soc_pcm_hw_init(hw);
1554 for_each_rtd_cpu_dais(fe, i, dai) {
1555 struct snd_soc_pcm_stream *cpu_stream;
1558 * Skip CPUs which don't support the current stream
1559 * type. See soc_pcm_init_runtime_hw() for more details
1561 if (!snd_soc_dai_stream_valid(dai, stream))
1564 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1566 soc_pcm_hw_update_rate(hw, cpu_stream);
1567 soc_pcm_hw_update_chan(hw, cpu_stream);
1568 soc_pcm_hw_update_format(hw, cpu_stream);
1573 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1575 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1576 struct snd_pcm_runtime *runtime = substream->runtime;
1577 struct snd_pcm_hardware *hw = &runtime->hw;
1578 struct snd_soc_dpcm *dpcm;
1579 struct snd_soc_dai *dai;
1580 int stream = substream->stream;
1582 if (!fe->dai_link->dpcm_merged_format)
1586 * It returns merged BE codec format
1587 * if FE want to use it (= dpcm_merged_format)
1590 for_each_dpcm_be(fe, stream, dpcm) {
1591 struct snd_soc_pcm_runtime *be = dpcm->be;
1592 struct snd_soc_pcm_stream *codec_stream;
1595 for_each_rtd_codec_dais(be, i, dai) {
1597 * Skip CODECs which don't support the current stream
1598 * type. See soc_pcm_init_runtime_hw() for more details
1600 if (!snd_soc_dai_stream_valid(dai, stream))
1603 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1605 soc_pcm_hw_update_format(hw, codec_stream);
1610 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1612 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1613 struct snd_pcm_runtime *runtime = substream->runtime;
1614 struct snd_pcm_hardware *hw = &runtime->hw;
1615 struct snd_soc_dpcm *dpcm;
1616 int stream = substream->stream;
1618 if (!fe->dai_link->dpcm_merged_chan)
1622 * It returns merged BE codec channel;
1623 * if FE want to use it (= dpcm_merged_chan)
1626 for_each_dpcm_be(fe, stream, dpcm) {
1627 struct snd_soc_pcm_runtime *be = dpcm->be;
1628 struct snd_soc_pcm_stream *codec_stream;
1629 struct snd_soc_pcm_stream *cpu_stream;
1630 struct snd_soc_dai *dai;
1633 for_each_rtd_cpu_dais(be, i, dai) {
1635 * Skip CPUs which don't support the current stream
1636 * type. See soc_pcm_init_runtime_hw() for more details
1638 if (!snd_soc_dai_stream_valid(dai, stream))
1641 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1643 soc_pcm_hw_update_chan(hw, cpu_stream);
1647 * chan min/max cannot be enforced if there are multiple CODEC
1648 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1650 if (be->num_codecs == 1) {
1651 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1653 soc_pcm_hw_update_chan(hw, codec_stream);
1658 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1660 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1661 struct snd_pcm_runtime *runtime = substream->runtime;
1662 struct snd_pcm_hardware *hw = &runtime->hw;
1663 struct snd_soc_dpcm *dpcm;
1664 int stream = substream->stream;
1666 if (!fe->dai_link->dpcm_merged_rate)
1670 * It returns merged BE codec channel;
1671 * if FE want to use it (= dpcm_merged_chan)
1674 for_each_dpcm_be(fe, stream, dpcm) {
1675 struct snd_soc_pcm_runtime *be = dpcm->be;
1676 struct snd_soc_pcm_stream *pcm;
1677 struct snd_soc_dai *dai;
1680 for_each_rtd_dais(be, i, dai) {
1682 * Skip DAIs which don't support the current stream
1683 * type. See soc_pcm_init_runtime_hw() for more details
1685 if (!snd_soc_dai_stream_valid(dai, stream))
1688 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1690 soc_pcm_hw_update_rate(hw, pcm);
1695 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1698 struct snd_soc_dpcm *dpcm;
1699 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1700 struct snd_soc_dai *fe_cpu_dai;
1704 /* apply symmetry for FE */
1705 soc_pcm_update_symmetry(fe_substream);
1707 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1708 /* Symmetry only applies if we've got an active stream. */
1709 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1714 /* apply symmetry for BE */
1715 for_each_dpcm_be(fe, stream, dpcm) {
1716 struct snd_soc_pcm_runtime *be = dpcm->be;
1717 struct snd_pcm_substream *be_substream =
1718 snd_soc_dpcm_get_substream(be, stream);
1719 struct snd_soc_pcm_runtime *rtd;
1720 struct snd_soc_dai *dai;
1722 /* A backend may not have the requested substream */
1726 rtd = asoc_substream_to_rtd(be_substream);
1727 if (rtd->dai_link->be_hw_params_fixup)
1730 soc_pcm_update_symmetry(be_substream);
1732 /* Symmetry only applies if we've got an active stream. */
1733 for_each_rtd_dais(rtd, i, dai) {
1734 err = soc_pcm_apply_symmetry(fe_substream, dai);
1741 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
1746 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1748 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1749 int stream = fe_substream->stream, ret = 0;
1751 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1753 ret = dpcm_be_dai_startup(fe, stream);
1757 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1759 /* start the DAI frontend */
1760 ret = soc_pcm_open(fe_substream);
1764 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1766 dpcm_runtime_setup_fe(fe_substream);
1768 dpcm_runtime_setup_be_format(fe_substream);
1769 dpcm_runtime_setup_be_chan(fe_substream);
1770 dpcm_runtime_setup_be_rate(fe_substream);
1772 ret = dpcm_apply_symmetry(fe_substream, stream);
1776 dpcm_be_dai_startup_unwind(fe, stream);
1778 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1781 dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
1786 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1788 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1789 int stream = substream->stream;
1791 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1793 /* shutdown the BEs */
1794 dpcm_be_dai_shutdown(fe, stream);
1796 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1798 /* now shutdown the frontend */
1799 soc_pcm_close(substream);
1801 /* run the stream stop event */
1802 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1804 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1805 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1809 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1811 struct snd_soc_dpcm *dpcm;
1813 /* only hw_params backends that are either sinks or sources
1814 * to this frontend DAI */
1815 for_each_dpcm_be(fe, stream, dpcm) {
1817 struct snd_soc_pcm_runtime *be = dpcm->be;
1818 struct snd_pcm_substream *be_substream =
1819 snd_soc_dpcm_get_substream(be, stream);
1821 /* is this op for this BE ? */
1822 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1825 /* only free hw when no longer used - check all FEs */
1826 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1829 /* do not free hw if this BE is used by other FE */
1830 if (be->dpcm[stream].users > 1)
1833 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1834 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1835 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1836 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1837 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1838 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1841 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1842 be->dai_link->name);
1844 soc_pcm_hw_free(be_substream);
1846 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1850 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1852 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1853 int stream = substream->stream;
1855 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1856 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1858 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1860 /* call hw_free on the frontend */
1861 soc_pcm_hw_free(substream);
1863 /* only hw_params backends that are either sinks or sources
1864 * to this frontend DAI */
1865 dpcm_be_dai_hw_free(fe, stream);
1867 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1868 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1870 mutex_unlock(&fe->card->mutex);
1874 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1876 struct snd_soc_pcm_runtime *be;
1877 struct snd_pcm_substream *be_substream;
1878 struct snd_soc_dpcm *dpcm;
1881 for_each_dpcm_be(fe, stream, dpcm) {
1883 be_substream = snd_soc_dpcm_get_substream(be, stream);
1885 /* is this op for this BE ? */
1886 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1889 /* copy params for each dpcm */
1890 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1891 sizeof(struct snd_pcm_hw_params));
1893 /* perform any hw_params fixups */
1894 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1898 /* copy the fixed-up hw params for BE dai */
1899 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1900 sizeof(struct snd_pcm_hw_params));
1902 /* only allow hw_params() if no connected FEs are running */
1903 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1906 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1907 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1908 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1911 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1912 be->dai_link->name);
1914 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1918 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1923 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1924 __func__, be->dai_link->name, ret);
1926 /* disable any enabled and non active backends */
1927 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1929 be_substream = snd_soc_dpcm_get_substream(be, stream);
1931 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1934 /* only allow hw_free() if no connected FEs are running */
1935 if (!snd_soc_dpcm_can_be_free_stop(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) &&
1941 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1944 soc_pcm_hw_free(be_substream);
1950 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1951 struct snd_pcm_hw_params *params)
1953 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1954 int ret, stream = substream->stream;
1956 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1957 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1959 memcpy(&fe->dpcm[stream].hw_params, params,
1960 sizeof(struct snd_pcm_hw_params));
1961 ret = dpcm_be_dai_hw_params(fe, stream);
1965 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1966 fe->dai_link->name, params_rate(params),
1967 params_channels(params), params_format(params));
1969 /* call hw_params on the frontend */
1970 ret = soc_pcm_hw_params(substream, params);
1972 dpcm_be_dai_hw_free(fe, stream);
1974 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1977 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1978 mutex_unlock(&fe->card->mutex);
1981 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
1986 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1989 struct snd_soc_pcm_runtime *be;
1990 struct snd_soc_dpcm *dpcm;
1993 for_each_dpcm_be(fe, stream, dpcm) {
1994 struct snd_pcm_substream *be_substream;
1997 be_substream = snd_soc_dpcm_get_substream(be, stream);
1999 /* is this op for this BE ? */
2000 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2003 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2004 be->dai_link->name, cmd);
2007 case SNDRV_PCM_TRIGGER_START:
2008 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2009 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2010 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2013 ret = soc_pcm_trigger(be_substream, cmd);
2017 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2019 case SNDRV_PCM_TRIGGER_RESUME:
2020 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2023 ret = soc_pcm_trigger(be_substream, cmd);
2027 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2029 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2030 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2033 ret = soc_pcm_trigger(be_substream, cmd);
2037 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2039 case SNDRV_PCM_TRIGGER_STOP:
2040 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2041 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2044 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2047 ret = soc_pcm_trigger(be_substream, cmd);
2051 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2053 case SNDRV_PCM_TRIGGER_SUSPEND:
2054 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2057 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2060 ret = soc_pcm_trigger(be_substream, cmd);
2064 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2066 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2067 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2070 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2073 ret = soc_pcm_trigger(be_substream, cmd);
2077 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2083 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2084 __func__, be->dai_link->name, ret);
2087 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2089 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2090 int cmd, bool fe_first)
2092 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2095 /* call trigger on the frontend before the backend. */
2097 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2098 fe->dai_link->name, cmd);
2100 ret = soc_pcm_trigger(substream, cmd);
2104 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2108 /* call trigger on the frontend after the backend. */
2109 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2113 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2114 fe->dai_link->name, cmd);
2116 ret = soc_pcm_trigger(substream, cmd);
2121 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2123 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2124 int stream = substream->stream;
2126 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2128 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2131 case SND_SOC_DPCM_TRIGGER_PRE:
2133 case SNDRV_PCM_TRIGGER_START:
2134 case SNDRV_PCM_TRIGGER_RESUME:
2135 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2136 case SNDRV_PCM_TRIGGER_DRAIN:
2137 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2139 case SNDRV_PCM_TRIGGER_STOP:
2140 case SNDRV_PCM_TRIGGER_SUSPEND:
2141 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2142 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2149 case SND_SOC_DPCM_TRIGGER_POST:
2151 case SNDRV_PCM_TRIGGER_START:
2152 case SNDRV_PCM_TRIGGER_RESUME:
2153 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2154 case SNDRV_PCM_TRIGGER_DRAIN:
2155 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2157 case SNDRV_PCM_TRIGGER_STOP:
2158 case SNDRV_PCM_TRIGGER_SUSPEND:
2159 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2160 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2167 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2168 /* bespoke trigger() - handles both FE and BEs */
2170 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2171 fe->dai_link->name, cmd);
2173 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2176 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2177 fe->dai_link->name);
2183 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2189 case SNDRV_PCM_TRIGGER_START:
2190 case SNDRV_PCM_TRIGGER_RESUME:
2191 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2192 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2194 case SNDRV_PCM_TRIGGER_STOP:
2195 case SNDRV_PCM_TRIGGER_SUSPEND:
2196 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2198 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2199 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2204 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2208 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2210 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2211 int stream = substream->stream;
2213 /* if FE's runtime_update is already set, we're in race;
2214 * process this trigger later at exit
2216 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2217 fe->dpcm[stream].trigger_pending = cmd + 1;
2218 return 0; /* delayed, assuming it's successful */
2221 /* we're alone, let's trigger */
2222 return dpcm_fe_dai_do_trigger(substream, cmd);
2225 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2227 struct snd_soc_dpcm *dpcm;
2230 for_each_dpcm_be(fe, stream, dpcm) {
2232 struct snd_soc_pcm_runtime *be = dpcm->be;
2233 struct snd_pcm_substream *be_substream =
2234 snd_soc_dpcm_get_substream(be, stream);
2236 /* is this op for this BE ? */
2237 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2240 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2241 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2242 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2243 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2246 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2247 be->dai_link->name);
2249 ret = soc_pcm_prepare(be_substream);
2253 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2257 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2262 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2264 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2265 int stream = substream->stream, ret = 0;
2267 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2269 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2271 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2273 /* there is no point preparing this FE if there are no BEs */
2274 if (list_empty(&fe->dpcm[stream].be_clients)) {
2275 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2276 fe->dai_link->name);
2281 ret = dpcm_be_dai_prepare(fe, stream);
2285 /* call prepare on the frontend */
2286 ret = soc_pcm_prepare(substream);
2290 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2293 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2294 mutex_unlock(&fe->card->mutex);
2297 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2302 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2304 struct snd_pcm_substream *substream =
2305 snd_soc_dpcm_get_substream(fe, stream);
2306 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2309 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2310 stream ? "capture" : "playback", fe->dai_link->name);
2312 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2313 /* call bespoke trigger - FE takes care of all BE triggers */
2314 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2315 fe->dai_link->name);
2317 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2319 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2320 fe->dai_link->name);
2322 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2325 dpcm_be_dai_hw_free(fe, stream);
2327 dpcm_be_dai_shutdown(fe, stream);
2329 /* run the stream event for each BE */
2330 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2333 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
2338 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2340 struct snd_pcm_substream *substream =
2341 snd_soc_dpcm_get_substream(fe, stream);
2342 struct snd_soc_dpcm *dpcm;
2343 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2345 unsigned long flags;
2347 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2348 stream ? "capture" : "playback", fe->dai_link->name);
2350 /* Only start the BE if the FE is ready */
2351 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2352 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2353 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2354 fe->dai_link->name, fe->dpcm[stream].state);
2359 /* startup must always be called for new BEs */
2360 ret = dpcm_be_dai_startup(fe, stream);
2364 /* keep going if FE state is > open */
2365 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2368 ret = dpcm_be_dai_hw_params(fe, stream);
2372 /* keep going if FE state is > hw_params */
2373 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2376 ret = dpcm_be_dai_prepare(fe, stream);
2380 /* run the stream event for each BE */
2381 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2383 /* keep going if FE state is > prepare */
2384 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2385 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2388 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2389 /* call trigger on the frontend - FE takes care of all BE triggers */
2390 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2391 fe->dai_link->name);
2393 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2397 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2398 fe->dai_link->name);
2400 ret = dpcm_be_dai_trigger(fe, stream,
2401 SNDRV_PCM_TRIGGER_START);
2409 dpcm_be_dai_hw_free(fe, stream);
2411 dpcm_be_dai_shutdown(fe, stream);
2413 /* disconnect any pending BEs */
2414 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2415 for_each_dpcm_be(fe, stream, dpcm) {
2416 struct snd_soc_pcm_runtime *be = dpcm->be;
2418 /* is this op for this BE ? */
2419 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2422 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2423 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2424 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2426 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2429 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2434 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2436 struct snd_soc_dapm_widget_list *list;
2440 if (!fe->dai_link->dynamic)
2443 if (fe->num_cpus > 1) {
2445 "%s doesn't support Multi CPU yet\n", __func__);
2449 /* only check active links */
2450 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2453 /* DAPM sync will call this to update DSP paths */
2454 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2455 new ? "new" : "old", fe->dai_link->name);
2457 for_each_pcm_streams(stream) {
2459 /* skip if FE doesn't have playback/capture capability */
2460 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2461 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2464 /* skip if FE isn't currently playing/capturing */
2465 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2466 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2469 paths = dpcm_path_get(fe, stream, &list);
2473 /* update any playback/capture paths */
2474 count = dpcm_process_paths(fe, stream, &list, new);
2476 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2478 dpcm_run_update_startup(fe, stream);
2480 dpcm_run_update_shutdown(fe, stream);
2481 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2483 dpcm_clear_pending_state(fe, stream);
2484 dpcm_be_disconnect(fe, stream);
2487 dpcm_path_put(&list);
2493 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2496 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2498 struct snd_soc_pcm_runtime *fe;
2501 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2502 /* shutdown all old paths first */
2503 for_each_card_rtds(card, fe) {
2504 ret = soc_dpcm_fe_runtime_update(fe, 0);
2509 /* bring new paths up */
2510 for_each_card_rtds(card, fe) {
2511 ret = soc_dpcm_fe_runtime_update(fe, 1);
2517 mutex_unlock(&card->mutex);
2520 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2522 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2524 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2525 struct snd_soc_dpcm *dpcm;
2526 int stream = fe_substream->stream;
2528 /* mark FE's links ready to prune */
2529 for_each_dpcm_be(fe, stream, dpcm)
2530 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2532 dpcm_be_disconnect(fe, stream);
2534 fe->dpcm[stream].runtime = NULL;
2537 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2539 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2542 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2543 ret = dpcm_fe_dai_shutdown(fe_substream);
2545 dpcm_fe_dai_cleanup(fe_substream);
2547 mutex_unlock(&fe->card->mutex);
2551 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2553 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2554 struct snd_soc_dapm_widget_list *list;
2556 int stream = fe_substream->stream;
2558 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2559 fe->dpcm[stream].runtime = fe_substream->runtime;
2561 ret = dpcm_path_get(fe, stream, &list);
2565 /* calculate valid and active FE <-> BE dpcms */
2566 dpcm_process_paths(fe, stream, &list, 1);
2568 ret = dpcm_fe_dai_startup(fe_substream);
2570 dpcm_fe_dai_cleanup(fe_substream);
2572 dpcm_clear_pending_state(fe, stream);
2573 dpcm_path_put(&list);
2575 mutex_unlock(&fe->card->mutex);
2579 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2580 int *playback, int *capture)
2582 struct snd_soc_dai *codec_dai;
2583 struct snd_soc_dai *cpu_dai;
2587 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2589 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2593 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2594 if (rtd->dai_link->dpcm_playback) {
2595 stream = SNDRV_PCM_STREAM_PLAYBACK;
2597 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2598 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2604 dev_err(rtd->card->dev,
2605 "No CPU DAIs support playback for stream %s\n",
2606 rtd->dai_link->stream_name);
2610 if (rtd->dai_link->dpcm_capture) {
2611 stream = SNDRV_PCM_STREAM_CAPTURE;
2613 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2614 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2621 dev_err(rtd->card->dev,
2622 "No CPU DAIs support capture for stream %s\n",
2623 rtd->dai_link->stream_name);
2628 /* Adapt stream for codec2codec links */
2629 int cpu_capture = rtd->dai_link->params ?
2630 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2631 int cpu_playback = rtd->dai_link->params ?
2632 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2634 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2635 if (rtd->num_cpus == 1) {
2636 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2637 } else if (rtd->num_cpus == rtd->num_codecs) {
2638 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2640 dev_err(rtd->card->dev,
2641 "N cpus to M codecs link is not supported yet\n");
2645 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2646 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2648 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2649 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2654 if (rtd->dai_link->playback_only) {
2659 if (rtd->dai_link->capture_only) {
2667 static int soc_create_pcm(struct snd_pcm **pcm,
2668 struct snd_soc_pcm_runtime *rtd,
2669 int playback, int capture, int num)
2674 /* create the PCM */
2675 if (rtd->dai_link->params) {
2676 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2677 rtd->dai_link->stream_name);
2679 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2680 playback, capture, pcm);
2681 } else if (rtd->dai_link->no_pcm) {
2682 snprintf(new_name, sizeof(new_name), "(%s)",
2683 rtd->dai_link->stream_name);
2685 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2686 playback, capture, pcm);
2688 if (rtd->dai_link->dynamic)
2689 snprintf(new_name, sizeof(new_name), "%s (*)",
2690 rtd->dai_link->stream_name);
2692 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2693 rtd->dai_link->stream_name,
2694 soc_codec_dai_name(rtd), num);
2696 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2700 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2701 new_name, rtd->dai_link->name, ret);
2704 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2709 /* create a new pcm */
2710 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2712 struct snd_soc_component *component;
2713 struct snd_pcm *pcm;
2714 int ret = 0, playback = 0, capture = 0;
2717 ret = soc_get_playback_capture(rtd, &playback, &capture);
2721 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2725 /* DAPM dai link stream work */
2726 if (rtd->dai_link->params)
2727 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2729 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2732 pcm->nonatomic = rtd->dai_link->nonatomic;
2733 pcm->private_data = rtd;
2735 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2737 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2739 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2743 /* ASoC PCM operations */
2744 if (rtd->dai_link->dynamic) {
2745 rtd->ops.open = dpcm_fe_dai_open;
2746 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2747 rtd->ops.prepare = dpcm_fe_dai_prepare;
2748 rtd->ops.trigger = dpcm_fe_dai_trigger;
2749 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2750 rtd->ops.close = dpcm_fe_dai_close;
2751 rtd->ops.pointer = soc_pcm_pointer;
2753 rtd->ops.open = soc_pcm_open;
2754 rtd->ops.hw_params = soc_pcm_hw_params;
2755 rtd->ops.prepare = soc_pcm_prepare;
2756 rtd->ops.trigger = soc_pcm_trigger;
2757 rtd->ops.hw_free = soc_pcm_hw_free;
2758 rtd->ops.close = soc_pcm_close;
2759 rtd->ops.pointer = soc_pcm_pointer;
2762 for_each_rtd_components(rtd, i, component) {
2763 const struct snd_soc_component_driver *drv = component->driver;
2766 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2768 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2770 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2772 rtd->ops.page = snd_soc_pcm_component_page;
2774 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2776 rtd->ops.ack = snd_soc_pcm_component_ack;
2780 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2783 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2785 ret = snd_soc_pcm_component_new(rtd);
2789 pcm->no_device_suspend = true;
2791 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2792 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2796 /* is the current PCM operation for this FE ? */
2797 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2799 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2803 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2805 /* is the current PCM operation for this BE ? */
2806 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2807 struct snd_soc_pcm_runtime *be, int stream)
2809 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2810 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2811 be->dpcm[stream].runtime_update))
2815 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2817 /* get the substream for this BE */
2818 struct snd_pcm_substream *
2819 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2821 return be->pcm->streams[stream].substream;
2823 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2825 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2826 struct snd_soc_pcm_runtime *be,
2828 const enum snd_soc_dpcm_state *states,
2831 struct snd_soc_dpcm *dpcm;
2834 unsigned long flags;
2837 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2838 for_each_dpcm_fe(be, stream, dpcm) {
2843 state = dpcm->fe->dpcm[stream].state;
2844 for (i = 0; i < num_states; i++) {
2845 if (state == states[i]) {
2851 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2853 /* it's safe to do this BE DAI */
2858 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2859 * are not running, paused or suspended for the specified stream direction.
2861 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2862 struct snd_soc_pcm_runtime *be, int stream)
2864 const enum snd_soc_dpcm_state state[] = {
2865 SND_SOC_DPCM_STATE_START,
2866 SND_SOC_DPCM_STATE_PAUSED,
2867 SND_SOC_DPCM_STATE_SUSPEND,
2870 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2872 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2875 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2876 * running, paused or suspended for the specified stream direction.
2878 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2879 struct snd_soc_pcm_runtime *be, int stream)
2881 const enum snd_soc_dpcm_state state[] = {
2882 SND_SOC_DPCM_STATE_START,
2883 SND_SOC_DPCM_STATE_PAUSED,
2884 SND_SOC_DPCM_STATE_SUSPEND,
2885 SND_SOC_DPCM_STATE_PREPARE,
2888 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2890 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);