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;
388 soc_pcm_set_dai_params(&d, params);
390 #define __soc_pcm_params_symmetry(xxx) \
391 symmetry = rtd->dai_link->symmetric_##xxx; \
392 for_each_rtd_dais(rtd, i, dai) \
393 symmetry |= dai->driver->symmetric_##xxx; \
396 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
397 if (!snd_soc_dai_is_dummy(cpu_dai) && \
398 cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
399 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
400 #xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
404 /* reject unmatched parameters when applying symmetry */
405 __soc_pcm_params_symmetry(rate);
406 __soc_pcm_params_symmetry(channels);
407 __soc_pcm_params_symmetry(sample_bits);
412 static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
414 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
415 struct snd_soc_dai_link *link = rtd->dai_link;
416 struct snd_soc_dai *dai;
417 unsigned int symmetry, i;
419 symmetry = link->symmetric_rate ||
420 link->symmetric_channels ||
421 link->symmetric_sample_bits;
423 for_each_rtd_dais(rtd, i, dai)
424 symmetry = symmetry ||
425 dai->driver->symmetric_rate ||
426 dai->driver->symmetric_channels ||
427 dai->driver->symmetric_sample_bits;
430 substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
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 int stream = substream->stream;
454 unsigned int bits = 0, cpu_bits = 0;
456 for_each_rtd_codec_dais(rtd, i, codec_dai) {
457 struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
459 if (pcm_codec->sig_bits == 0) {
463 bits = max(pcm_codec->sig_bits, bits);
466 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
467 struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
469 if (pcm_cpu->sig_bits == 0) {
473 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
476 soc_pcm_set_msb(substream, bits);
477 soc_pcm_set_msb(substream, cpu_bits);
480 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
482 hw->rates = UINT_MAX;
484 hw->rate_max = UINT_MAX;
485 hw->channels_min = 0;
486 hw->channels_max = UINT_MAX;
487 hw->formats = ULLONG_MAX;
490 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
491 struct snd_soc_pcm_stream *p)
493 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
495 /* setup hw->rate_min/max via hw->rates first */
496 snd_pcm_hw_limit_rates(hw);
498 /* update hw->rate_min/max by snd_soc_pcm_stream */
499 hw->rate_min = max(hw->rate_min, p->rate_min);
500 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
503 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
504 struct snd_soc_pcm_stream *p)
506 hw->channels_min = max(hw->channels_min, p->channels_min);
507 hw->channels_max = min(hw->channels_max, p->channels_max);
510 static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
511 struct snd_soc_pcm_stream *p)
513 hw->formats &= p->formats;
517 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
518 * @rtd: ASoC PCM runtime
519 * @hw: PCM hardware parameters (output)
520 * @stream: Direction of the PCM stream
522 * Calculates the subset of stream parameters supported by all DAIs
523 * associated with the PCM stream.
525 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
526 struct snd_pcm_hardware *hw, int stream)
528 struct snd_soc_dai *codec_dai;
529 struct snd_soc_dai *cpu_dai;
530 struct snd_soc_pcm_stream *codec_stream;
531 struct snd_soc_pcm_stream *cpu_stream;
532 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
537 /* first calculate min/max only for CPUs in the DAI link */
538 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
541 * Skip CPUs which don't support the current stream type.
542 * Otherwise, since the rate, channel, and format values will
543 * zero in that case, we would have no usable settings left,
544 * causing the resulting setup to fail.
546 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
549 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
551 soc_pcm_hw_update_chan(hw, cpu_stream);
552 soc_pcm_hw_update_rate(hw, cpu_stream);
553 soc_pcm_hw_update_format(hw, cpu_stream);
555 cpu_chan_min = hw->channels_min;
556 cpu_chan_max = hw->channels_max;
558 /* second calculate min/max only for CODECs in the DAI link */
559 for_each_rtd_codec_dais(rtd, i, codec_dai) {
562 * Skip CODECs which don't support the current stream type.
563 * Otherwise, since the rate, channel, and format values will
564 * zero in that case, we would have no usable settings left,
565 * causing the resulting setup to fail.
567 if (!snd_soc_dai_stream_valid(codec_dai, stream))
570 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
572 soc_pcm_hw_update_chan(hw, codec_stream);
573 soc_pcm_hw_update_rate(hw, codec_stream);
574 soc_pcm_hw_update_format(hw, codec_stream);
577 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
578 if (!hw->channels_min)
582 * chan min/max cannot be enforced if there are multiple CODEC DAIs
583 * connected to CPU DAI(s), use CPU DAI's directly and let
584 * channel allocation be fixed up later
586 if (rtd->num_codecs > 1) {
587 hw->channels_min = cpu_chan_min;
588 hw->channels_max = cpu_chan_max;
593 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
595 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
597 struct snd_pcm_hardware *hw = &substream->runtime->hw;
598 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
599 u64 formats = hw->formats;
602 * At least one CPU and one CODEC should match. Otherwise, we should
603 * have bailed out on a higher level, since there would be no CPU or
604 * CODEC to support the transfer direction in that case.
606 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
609 hw->formats &= formats;
612 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
614 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
615 struct snd_soc_component *component;
618 for_each_rtd_components(rtd, i, component) {
619 ret = snd_soc_component_module_get_when_open(component, substream);
623 ret = snd_soc_component_open(component, substream);
631 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
634 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
635 struct snd_soc_component *component;
638 for_each_rtd_components(rtd, i, component) {
639 int r = snd_soc_component_close(component, substream, rollback);
641 ret = r; /* use last ret */
643 snd_soc_component_module_put_when_close(component, substream, rollback);
649 static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
651 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
652 struct snd_soc_component *component;
653 struct snd_soc_dai *dai;
656 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
659 snd_soc_runtime_deactivate(rtd, substream->stream);
661 for_each_rtd_dais(rtd, i, dai)
662 snd_soc_dai_shutdown(dai, substream, rollback);
664 snd_soc_link_shutdown(substream, rollback);
666 soc_pcm_components_close(substream, rollback);
669 mutex_unlock(&rtd->card->pcm_mutex);
671 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
673 for_each_rtd_components(rtd, i, component)
674 if (!snd_soc_component_active(component))
675 pinctrl_pm_select_sleep_state(component->dev);
681 * Called by ALSA when a PCM substream is closed. Private data can be
682 * freed here. The cpu DAI, codec DAI, machine and components are also
685 static int soc_pcm_close(struct snd_pcm_substream *substream)
687 return soc_pcm_clean(substream, 0);
690 static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
692 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
693 struct snd_pcm_hardware *hw = &substream->runtime->hw;
694 const char *name_cpu = soc_cpu_dai_name(rtd);
695 const char *name_codec = soc_codec_dai_name(rtd);
697 struct device *dev = rtd->dev;
707 err_msg = "channels";
708 if (!hw->channels_min || !hw->channels_max ||
709 hw->channels_min > hw->channels_max)
712 dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
714 dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
715 dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
717 dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
723 dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
724 name_codec, name_cpu, err_msg);
729 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
730 * then initialized and any private data can be allocated. This also calls
731 * startup for the cpu DAI, component, machine and codec DAI.
733 static int soc_pcm_open(struct snd_pcm_substream *substream)
735 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
736 struct snd_soc_component *component;
737 struct snd_soc_dai *dai;
740 for_each_rtd_components(rtd, i, component)
741 pinctrl_pm_select_default_state(component->dev);
743 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
747 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
749 ret = soc_pcm_components_open(substream);
753 ret = snd_soc_link_startup(substream);
757 /* startup the audio subsystem */
758 for_each_rtd_dais(rtd, i, dai) {
759 ret = snd_soc_dai_startup(dai, substream);
763 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
769 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
770 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
773 /* Check that the codec and cpu DAIs are compatible */
774 soc_pcm_init_runtime_hw(substream);
776 soc_pcm_update_symmetry(substream);
778 ret = soc_hw_sanity_check(substream);
782 soc_pcm_apply_msb(substream);
784 /* Symmetry only applies if we've already got an active stream. */
785 for_each_rtd_dais(rtd, i, dai) {
786 ret = soc_pcm_apply_symmetry(substream, dai);
791 snd_soc_runtime_activate(rtd, substream->stream);
794 mutex_unlock(&rtd->card->pcm_mutex);
797 soc_pcm_clean(substream, 1);
798 dev_err(rtd->dev, "%s() failed (%d)", __func__, ret);
804 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
807 * Currently nothing to do for c2c links
808 * Since c2c links are internal nodes in the DAPM graph and
809 * don't interface with the outside world or application layer
810 * we don't have to do any special handling on close.
815 * Called by ALSA when the PCM substream is prepared, can set format, sample
816 * rate, etc. This function is non atomic and can be called multiple times,
817 * it can refer to the runtime info.
819 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
821 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
822 struct snd_soc_dai *dai;
825 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
827 ret = snd_soc_link_prepare(substream);
831 ret = snd_soc_pcm_component_prepare(substream);
835 ret = snd_soc_pcm_dai_prepare(substream);
839 /* cancel any delayed stream shutdown that is pending */
840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
843 cancel_delayed_work(&rtd->delayed_work);
846 snd_soc_dapm_stream_event(rtd, substream->stream,
847 SND_SOC_DAPM_STREAM_START);
849 for_each_rtd_dais(rtd, i, dai)
850 snd_soc_dai_digital_mute(dai, 0, substream->stream);
853 mutex_unlock(&rtd->card->pcm_mutex);
856 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
861 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
864 struct snd_interval *interval;
865 int channels = hweight_long(mask);
867 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
868 interval->min = channels;
869 interval->max = channels;
872 static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
874 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
875 struct snd_soc_dai *dai;
878 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
880 /* clear the corresponding DAIs parameters when going to be inactive */
881 for_each_rtd_dais(rtd, i, dai) {
882 int active = snd_soc_dai_stream_active(dai, substream->stream);
884 if (snd_soc_dai_active(dai) == 1)
885 soc_pcm_set_dai_params(dai, NULL);
888 snd_soc_dai_digital_mute(dai, 1, substream->stream);
891 /* run the stream event */
892 snd_soc_dapm_stream_stop(rtd, substream->stream);
894 /* free any machine hw params */
895 snd_soc_link_hw_free(substream, rollback);
897 /* free any component resources */
898 snd_soc_pcm_component_hw_free(substream, rollback);
900 /* now free hw params for the DAIs */
901 for_each_rtd_dais(rtd, i, dai) {
902 if (!snd_soc_dai_stream_valid(dai, substream->stream))
905 snd_soc_dai_hw_free(dai, substream, rollback);
908 mutex_unlock(&rtd->card->pcm_mutex);
913 * Frees resources allocated by hw_params, can be called multiple times
915 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
917 return soc_pcm_hw_clean(substream, 0);
921 * Called by ALSA when the hardware params are set by application. This
922 * function can also be called multiple times and can allocate buffers
923 * (using snd_pcm_lib_* ). It's non-atomic.
925 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
926 struct snd_pcm_hw_params *params)
928 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
929 struct snd_soc_dai *cpu_dai;
930 struct snd_soc_dai *codec_dai;
933 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
935 ret = soc_pcm_params_symmetry(substream, params);
939 ret = snd_soc_link_hw_params(substream, params);
943 for_each_rtd_codec_dais(rtd, i, codec_dai) {
944 struct snd_pcm_hw_params codec_params;
947 * Skip CODECs which don't support the current stream type,
948 * the idea being that if a CODEC is not used for the currently
949 * set up transfer direction, it should not need to be
950 * configured, especially since the configuration used might
951 * not even be supported by that CODEC. There may be cases
952 * however where a CODEC needs to be set up although it is
953 * actually not being used for the transfer, e.g. if a
954 * capture-only CODEC is acting as an LRCLK and/or BCLK master
955 * for the DAI link including a playback-only CODEC.
956 * If this becomes necessary, we will have to augment the
957 * machine driver setup with information on how to act, so
958 * we can do the right thing here.
960 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
963 /* copy params for each codec */
964 codec_params = *params;
966 /* fixup params based on TDM slot masks */
967 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
969 soc_pcm_codec_params_fixup(&codec_params,
972 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
974 soc_pcm_codec_params_fixup(&codec_params,
977 ret = snd_soc_dai_hw_params(codec_dai, substream,
982 soc_pcm_set_dai_params(codec_dai, &codec_params);
983 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
986 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
988 * Skip CPUs which don't support the current stream
989 * type. See soc_pcm_init_runtime_hw() for more details
991 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
994 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
998 /* store the parameters for each DAI */
999 soc_pcm_set_dai_params(cpu_dai, params);
1000 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1003 ret = snd_soc_pcm_component_hw_params(substream, params);
1005 mutex_unlock(&rtd->card->pcm_mutex);
1008 soc_pcm_hw_clean(substream, 1);
1009 dev_err(rtd->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
1015 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1017 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1018 int ret = -EINVAL, _ret = 0;
1022 case SNDRV_PCM_TRIGGER_START:
1023 case SNDRV_PCM_TRIGGER_RESUME:
1024 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1025 ret = snd_soc_link_trigger(substream, cmd, 0);
1029 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1033 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1042 case SNDRV_PCM_TRIGGER_START:
1043 cmd = SNDRV_PCM_TRIGGER_STOP;
1045 case SNDRV_PCM_TRIGGER_RESUME:
1046 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1048 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1049 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1055 case SNDRV_PCM_TRIGGER_STOP:
1056 case SNDRV_PCM_TRIGGER_SUSPEND:
1057 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1058 if (rtd->dai_link->stop_dma_first) {
1059 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1063 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1067 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1071 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1075 ret = snd_soc_link_trigger(substream, cmd, rollback);
1086 * soc level wrapper for pointer callback
1087 * If cpu_dai, codec_dai, component driver has the delay callback, then
1088 * the runtime->delay will be updated accordingly.
1090 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1092 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1093 struct snd_soc_dai *cpu_dai;
1094 struct snd_soc_dai *codec_dai;
1095 struct snd_pcm_runtime *runtime = substream->runtime;
1096 snd_pcm_uframes_t offset = 0;
1097 snd_pcm_sframes_t delay = 0;
1098 snd_pcm_sframes_t codec_delay = 0;
1099 snd_pcm_sframes_t cpu_delay = 0;
1102 /* clearing the previous total delay */
1105 offset = snd_soc_pcm_component_pointer(substream);
1107 /* base delay if assigned in pointer callback */
1108 delay = runtime->delay;
1110 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1111 cpu_delay = max(cpu_delay,
1112 snd_soc_dai_delay(cpu_dai, substream));
1116 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1117 codec_delay = max(codec_delay,
1118 snd_soc_dai_delay(codec_dai, substream));
1120 delay += codec_delay;
1122 runtime->delay = delay;
1127 /* connect a FE and BE */
1128 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1129 struct snd_soc_pcm_runtime *be, int stream)
1131 struct snd_soc_dpcm *dpcm;
1132 unsigned long flags;
1134 /* only add new dpcms */
1135 for_each_dpcm_be(fe, stream, dpcm) {
1136 if (dpcm->be == be && dpcm->fe == fe)
1140 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1146 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1147 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1148 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1149 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1150 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1151 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1153 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1154 stream ? "capture" : "playback", fe->dai_link->name,
1155 stream ? "<-" : "->", be->dai_link->name);
1157 dpcm_create_debugfs_state(dpcm, stream);
1162 /* reparent a BE onto another FE */
1163 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1164 struct snd_soc_pcm_runtime *be, int stream)
1166 struct snd_soc_dpcm *dpcm;
1167 struct snd_pcm_substream *fe_substream, *be_substream;
1169 /* reparent if BE is connected to other FEs */
1170 if (!be->dpcm[stream].users)
1173 be_substream = snd_soc_dpcm_get_substream(be, stream);
1175 for_each_dpcm_fe(be, stream, dpcm) {
1179 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1180 stream ? "capture" : "playback",
1181 dpcm->fe->dai_link->name,
1182 stream ? "<-" : "->", dpcm->be->dai_link->name);
1184 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1185 be_substream->runtime = fe_substream->runtime;
1190 /* disconnect a BE and FE */
1191 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1193 struct snd_soc_dpcm *dpcm, *d;
1194 unsigned long flags;
1196 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1197 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1198 stream ? "capture" : "playback",
1199 dpcm->be->dai_link->name);
1201 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1204 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1205 stream ? "capture" : "playback", fe->dai_link->name,
1206 stream ? "<-" : "->", dpcm->be->dai_link->name);
1208 /* BEs still alive need new FE */
1209 dpcm_be_reparent(fe, dpcm->be, stream);
1211 dpcm_remove_debugfs_state(dpcm);
1213 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1214 list_del(&dpcm->list_be);
1215 list_del(&dpcm->list_fe);
1216 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1221 /* get BE for DAI widget and stream */
1222 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1223 struct snd_soc_dapm_widget *widget, int stream)
1225 struct snd_soc_pcm_runtime *be;
1226 struct snd_soc_dapm_widget *w;
1227 struct snd_soc_dai *dai;
1230 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1232 for_each_card_rtds(card, be) {
1234 if (!be->dai_link->no_pcm)
1237 for_each_rtd_dais(be, i, dai) {
1238 w = snd_soc_dai_get_widget(dai, stream);
1240 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1241 w ? w->name : "(not set)");
1248 /* Widget provided is not a BE */
1252 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1253 struct snd_soc_dapm_widget *widget)
1255 struct snd_soc_dapm_widget *w;
1258 for_each_dapm_widgets(list, i, w)
1265 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1266 enum snd_soc_dapm_direction dir)
1268 struct snd_soc_card *card = widget->dapm->card;
1269 struct snd_soc_pcm_runtime *rtd;
1272 /* adjust dir to stream */
1273 if (dir == SND_SOC_DAPM_DIR_OUT)
1274 stream = SNDRV_PCM_STREAM_PLAYBACK;
1276 stream = SNDRV_PCM_STREAM_CAPTURE;
1278 rtd = dpcm_get_be(card, widget, stream);
1285 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1286 int stream, struct snd_soc_dapm_widget_list **list)
1288 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1291 if (fe->num_cpus > 1) {
1293 "%s doesn't support Multi CPU yet\n", __func__);
1297 /* get number of valid DAI paths and their widgets */
1298 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1299 fe->card->component_chaining ?
1300 NULL : dpcm_end_walk_at_be);
1303 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1304 stream ? "capture" : "playback");
1305 else if (paths == 0)
1306 dev_dbg(fe->dev, "ASoC: %s no valid %s path\n", fe->dai_link->name,
1307 stream ? "capture" : "playback");
1312 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1314 snd_soc_dapm_dai_free_widgets(list);
1317 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1318 struct snd_soc_dapm_widget_list *list)
1320 struct snd_soc_dai *dai;
1323 /* is there a valid DAI widget for this BE */
1324 for_each_rtd_dais(dpcm->be, i, dai) {
1325 struct snd_soc_dapm_widget *widget = snd_soc_dai_get_widget(dai, stream);
1328 * The BE is pruned only if none of the dai
1329 * widgets are in the active list.
1331 if (widget && widget_in_list(list, widget))
1338 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1339 struct snd_soc_dapm_widget_list **list_)
1341 struct snd_soc_dpcm *dpcm;
1344 /* Destroy any old FE <--> BE connections */
1345 for_each_dpcm_be(fe, stream, dpcm) {
1346 if (dpcm_be_is_active(dpcm, stream, *list_))
1349 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1350 stream ? "capture" : "playback",
1351 dpcm->be->dai_link->name, fe->dai_link->name);
1352 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1353 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1357 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1361 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1362 struct snd_soc_dapm_widget_list **list_)
1364 struct snd_soc_card *card = fe->card;
1365 struct snd_soc_dapm_widget_list *list = *list_;
1366 struct snd_soc_pcm_runtime *be;
1367 struct snd_soc_dapm_widget *widget;
1368 int i, new = 0, err;
1370 /* Create any new FE <--> BE connections */
1371 for_each_dapm_widgets(list, i, widget) {
1373 switch (widget->id) {
1374 case snd_soc_dapm_dai_in:
1375 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1378 case snd_soc_dapm_dai_out:
1379 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1386 /* is there a valid BE rtd for this widget */
1387 be = dpcm_get_be(card, widget, stream);
1389 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1394 /* don't connect if FE is not running */
1395 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1398 /* newly connected FE and BE */
1399 err = dpcm_be_connect(fe, be, stream);
1401 dev_err(fe->dev, "ASoC: can't connect %s\n",
1404 } else if (err == 0) /* already connected */
1408 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1412 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1417 * Find the corresponding BE DAIs that source or sink audio to this
1420 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1421 int stream, struct snd_soc_dapm_widget_list **list, int new)
1424 return dpcm_add_paths(fe, stream, list);
1426 return dpcm_prune_paths(fe, stream, list);
1429 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1431 struct snd_soc_dpcm *dpcm;
1432 unsigned long flags;
1434 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1435 for_each_dpcm_be(fe, stream, dpcm)
1436 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1437 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1440 void dpcm_be_dai_stop(struct snd_soc_pcm_runtime *fe, int stream,
1441 int do_hw_free, struct snd_soc_dpcm *last)
1443 struct snd_soc_dpcm *dpcm;
1445 /* disable any enabled and non active backends */
1446 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);
1454 /* is this op for this BE ? */
1455 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1458 if (be->dpcm[stream].users == 0) {
1459 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1460 stream ? "capture" : "playback",
1461 be->dpcm[stream].state);
1465 if (--be->dpcm[stream].users != 0)
1468 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) {
1472 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) {
1473 soc_pcm_hw_free(be_substream);
1474 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1478 soc_pcm_close(be_substream);
1479 be_substream->runtime = NULL;
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1484 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1486 struct snd_soc_pcm_runtime *be;
1487 struct snd_soc_dpcm *dpcm;
1490 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1491 for_each_dpcm_be(fe, stream, dpcm) {
1492 struct snd_pcm_substream *be_substream;
1495 be_substream = snd_soc_dpcm_get_substream(be, stream);
1497 if (!be_substream) {
1498 dev_err(be->dev, "ASoC: no backend %s stream\n",
1499 stream ? "capture" : "playback");
1503 /* is this op for this BE ? */
1504 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1507 /* first time the dpcm is open ? */
1508 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS) {
1509 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1510 stream ? "capture" : "playback",
1511 be->dpcm[stream].state);
1515 if (be->dpcm[stream].users++ != 0)
1518 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1519 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1522 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1523 stream ? "capture" : "playback", be->dai_link->name);
1525 be_substream->runtime = be->dpcm[stream].runtime;
1526 err = soc_pcm_open(be_substream);
1528 be->dpcm[stream].users--;
1529 if (be->dpcm[stream].users < 0)
1530 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1531 stream ? "capture" : "playback",
1532 be->dpcm[stream].state);
1534 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1538 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1545 dpcm_be_dai_startup_rollback(fe, stream, dpcm);
1547 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1548 __func__, be->dai_link->name, err);
1553 static void dpcm_runtime_setup_fe(struct snd_pcm_substream *substream)
1555 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1556 struct snd_pcm_runtime *runtime = substream->runtime;
1557 struct snd_pcm_hardware *hw = &runtime->hw;
1558 struct snd_soc_dai *dai;
1559 int stream = substream->stream;
1562 soc_pcm_hw_init(hw);
1564 for_each_rtd_cpu_dais(fe, i, dai) {
1565 struct snd_soc_pcm_stream *cpu_stream;
1568 * Skip CPUs which don't support the current stream
1569 * type. See soc_pcm_init_runtime_hw() for more details
1571 if (!snd_soc_dai_stream_valid(dai, stream))
1574 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1576 soc_pcm_hw_update_rate(hw, cpu_stream);
1577 soc_pcm_hw_update_chan(hw, cpu_stream);
1578 soc_pcm_hw_update_format(hw, cpu_stream);
1583 static void dpcm_runtime_setup_be_format(struct snd_pcm_substream *substream)
1585 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1586 struct snd_pcm_runtime *runtime = substream->runtime;
1587 struct snd_pcm_hardware *hw = &runtime->hw;
1588 struct snd_soc_dpcm *dpcm;
1589 struct snd_soc_dai *dai;
1590 int stream = substream->stream;
1592 if (!fe->dai_link->dpcm_merged_format)
1596 * It returns merged BE codec format
1597 * if FE want to use it (= dpcm_merged_format)
1600 for_each_dpcm_be(fe, stream, dpcm) {
1601 struct snd_soc_pcm_runtime *be = dpcm->be;
1602 struct snd_soc_pcm_stream *codec_stream;
1605 for_each_rtd_codec_dais(be, i, dai) {
1607 * Skip CODECs which don't support the current stream
1608 * type. See soc_pcm_init_runtime_hw() for more details
1610 if (!snd_soc_dai_stream_valid(dai, stream))
1613 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1615 soc_pcm_hw_update_format(hw, codec_stream);
1620 static void dpcm_runtime_setup_be_chan(struct snd_pcm_substream *substream)
1622 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1623 struct snd_pcm_runtime *runtime = substream->runtime;
1624 struct snd_pcm_hardware *hw = &runtime->hw;
1625 struct snd_soc_dpcm *dpcm;
1626 int stream = substream->stream;
1628 if (!fe->dai_link->dpcm_merged_chan)
1632 * It returns merged BE codec channel;
1633 * if FE want to use it (= dpcm_merged_chan)
1636 for_each_dpcm_be(fe, stream, dpcm) {
1637 struct snd_soc_pcm_runtime *be = dpcm->be;
1638 struct snd_soc_pcm_stream *cpu_stream;
1639 struct snd_soc_dai *dai;
1642 for_each_rtd_cpu_dais(be, i, dai) {
1644 * Skip CPUs which don't support the current stream
1645 * type. See soc_pcm_init_runtime_hw() for more details
1647 if (!snd_soc_dai_stream_valid(dai, stream))
1650 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1652 soc_pcm_hw_update_chan(hw, cpu_stream);
1656 * chan min/max cannot be enforced if there are multiple CODEC
1657 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1659 if (be->num_codecs == 1) {
1660 struct snd_soc_pcm_stream *codec_stream = snd_soc_dai_get_pcm_stream(
1661 asoc_rtd_to_codec(be, 0), stream);
1663 soc_pcm_hw_update_chan(hw, codec_stream);
1668 static void dpcm_runtime_setup_be_rate(struct snd_pcm_substream *substream)
1670 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1671 struct snd_pcm_runtime *runtime = substream->runtime;
1672 struct snd_pcm_hardware *hw = &runtime->hw;
1673 struct snd_soc_dpcm *dpcm;
1674 int stream = substream->stream;
1676 if (!fe->dai_link->dpcm_merged_rate)
1680 * It returns merged BE codec channel;
1681 * if FE want to use it (= dpcm_merged_chan)
1684 for_each_dpcm_be(fe, stream, dpcm) {
1685 struct snd_soc_pcm_runtime *be = dpcm->be;
1686 struct snd_soc_pcm_stream *pcm;
1687 struct snd_soc_dai *dai;
1690 for_each_rtd_dais(be, i, dai) {
1692 * Skip DAIs which don't support the current stream
1693 * type. See soc_pcm_init_runtime_hw() for more details
1695 if (!snd_soc_dai_stream_valid(dai, stream))
1698 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1700 soc_pcm_hw_update_rate(hw, pcm);
1705 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1708 struct snd_soc_dpcm *dpcm;
1709 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1710 struct snd_soc_dai *fe_cpu_dai;
1714 /* apply symmetry for FE */
1715 soc_pcm_update_symmetry(fe_substream);
1717 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1718 /* Symmetry only applies if we've got an active stream. */
1719 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1724 /* apply symmetry for BE */
1725 for_each_dpcm_be(fe, stream, dpcm) {
1726 struct snd_soc_pcm_runtime *be = dpcm->be;
1727 struct snd_pcm_substream *be_substream =
1728 snd_soc_dpcm_get_substream(be, stream);
1729 struct snd_soc_pcm_runtime *rtd;
1730 struct snd_soc_dai *dai;
1732 /* A backend may not have the requested substream */
1736 rtd = asoc_substream_to_rtd(be_substream);
1737 if (rtd->dai_link->be_hw_params_fixup)
1740 soc_pcm_update_symmetry(be_substream);
1742 /* Symmetry only applies if we've got an active stream. */
1743 for_each_rtd_dais(rtd, i, dai) {
1744 err = soc_pcm_apply_symmetry(fe_substream, dai);
1751 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, err);
1756 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1758 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1759 int stream = fe_substream->stream, ret = 0;
1761 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1763 ret = dpcm_be_dai_startup(fe, stream);
1767 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1769 /* start the DAI frontend */
1770 ret = soc_pcm_open(fe_substream);
1774 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1776 dpcm_runtime_setup_fe(fe_substream);
1778 dpcm_runtime_setup_be_format(fe_substream);
1779 dpcm_runtime_setup_be_chan(fe_substream);
1780 dpcm_runtime_setup_be_rate(fe_substream);
1782 ret = dpcm_apply_symmetry(fe_substream, stream);
1786 dpcm_be_dai_startup_unwind(fe, stream);
1788 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1791 dev_err(fe->dev, "%s() failed (%d)\n", __func__, ret);
1796 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1798 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1799 int stream = substream->stream;
1801 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1803 /* shutdown the BEs */
1804 dpcm_be_dai_shutdown(fe, stream);
1806 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1808 /* now shutdown the frontend */
1809 soc_pcm_close(substream);
1811 /* run the stream stop event */
1812 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1814 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1815 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1819 void dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1821 struct snd_soc_dpcm *dpcm;
1823 /* only hw_params backends that are either sinks or sources
1824 * to this frontend DAI */
1825 for_each_dpcm_be(fe, stream, dpcm) {
1827 struct snd_soc_pcm_runtime *be = dpcm->be;
1828 struct snd_pcm_substream *be_substream =
1829 snd_soc_dpcm_get_substream(be, stream);
1831 /* is this op for this BE ? */
1832 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1835 /* only free hw when no longer used - check all FEs */
1836 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1839 /* do not free hw if this BE is used by other FE */
1840 if (be->dpcm[stream].users > 1)
1843 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1844 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1845 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1846 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1847 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1848 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1851 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1852 be->dai_link->name);
1854 soc_pcm_hw_free(be_substream);
1856 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1860 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1862 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1863 int stream = substream->stream;
1865 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1866 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1868 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1870 /* call hw_free on the frontend */
1871 soc_pcm_hw_free(substream);
1873 /* only hw_params backends that are either sinks or sources
1874 * to this frontend DAI */
1875 dpcm_be_dai_hw_free(fe, stream);
1877 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1878 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1880 mutex_unlock(&fe->card->mutex);
1884 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1886 struct snd_soc_pcm_runtime *be;
1887 struct snd_pcm_substream *be_substream;
1888 struct snd_soc_dpcm *dpcm;
1891 for_each_dpcm_be(fe, stream, dpcm) {
1893 be_substream = snd_soc_dpcm_get_substream(be, stream);
1895 /* is this op for this BE ? */
1896 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1899 /* copy params for each dpcm */
1900 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1901 sizeof(struct snd_pcm_hw_params));
1903 /* perform any hw_params fixups */
1904 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1908 /* copy the fixed-up hw params for BE dai */
1909 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1910 sizeof(struct snd_pcm_hw_params));
1912 /* only allow hw_params() if no connected FEs are running */
1913 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1916 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1917 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1918 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1921 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1922 be->dai_link->name);
1924 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1928 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1933 dev_dbg(fe->dev, "ASoC: %s() failed at %s (%d)\n",
1934 __func__, be->dai_link->name, ret);
1936 /* disable any enabled and non active backends */
1937 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1939 be_substream = snd_soc_dpcm_get_substream(be, stream);
1941 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1944 /* only allow hw_free() if no connected FEs are running */
1945 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1948 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1949 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1950 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1951 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1954 soc_pcm_hw_free(be_substream);
1960 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1961 struct snd_pcm_hw_params *params)
1963 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1964 int ret, stream = substream->stream;
1966 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1967 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1969 memcpy(&fe->dpcm[stream].hw_params, params,
1970 sizeof(struct snd_pcm_hw_params));
1971 ret = dpcm_be_dai_hw_params(fe, stream);
1975 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1976 fe->dai_link->name, params_rate(params),
1977 params_channels(params), params_format(params));
1979 /* call hw_params on the frontend */
1980 ret = soc_pcm_hw_params(substream, params);
1982 dpcm_be_dai_hw_free(fe, stream);
1984 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1987 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1988 mutex_unlock(&fe->card->mutex);
1991 dev_err(fe->dev, "ASoC: %s failed (%d)\n", __func__, ret);
1996 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1999 struct snd_soc_pcm_runtime *be;
2000 struct snd_soc_dpcm *dpcm;
2003 for_each_dpcm_be(fe, stream, dpcm) {
2004 struct snd_pcm_substream *be_substream;
2007 be_substream = snd_soc_dpcm_get_substream(be, stream);
2009 /* is this op for this BE ? */
2010 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2013 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2014 be->dai_link->name, cmd);
2017 case SNDRV_PCM_TRIGGER_START:
2018 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2019 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2020 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2023 ret = soc_pcm_trigger(be_substream, cmd);
2027 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2029 case SNDRV_PCM_TRIGGER_RESUME:
2030 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2033 ret = soc_pcm_trigger(be_substream, cmd);
2037 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2039 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2040 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2043 ret = soc_pcm_trigger(be_substream, cmd);
2047 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2049 case SNDRV_PCM_TRIGGER_STOP:
2050 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2054 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2057 ret = soc_pcm_trigger(be_substream, cmd);
2061 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2063 case SNDRV_PCM_TRIGGER_SUSPEND:
2064 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2067 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2070 ret = soc_pcm_trigger(be_substream, cmd);
2074 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2076 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2077 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2080 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2083 ret = soc_pcm_trigger(be_substream, cmd);
2087 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2093 dev_err(fe->dev, "ASoC: %s() failed at %s (%d)\n",
2094 __func__, be->dai_link->name, ret);
2097 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2099 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2100 int cmd, bool fe_first)
2102 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2105 /* call trigger on the frontend before the backend. */
2107 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2108 fe->dai_link->name, cmd);
2110 ret = soc_pcm_trigger(substream, cmd);
2114 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2118 /* call trigger on the frontend after the backend. */
2119 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2123 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2124 fe->dai_link->name, cmd);
2126 ret = soc_pcm_trigger(substream, cmd);
2131 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2133 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2134 int stream = substream->stream;
2136 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2138 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2141 case SND_SOC_DPCM_TRIGGER_PRE:
2143 case SNDRV_PCM_TRIGGER_START:
2144 case SNDRV_PCM_TRIGGER_RESUME:
2145 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2146 case SNDRV_PCM_TRIGGER_DRAIN:
2147 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2149 case SNDRV_PCM_TRIGGER_STOP:
2150 case SNDRV_PCM_TRIGGER_SUSPEND:
2151 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2152 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2159 case SND_SOC_DPCM_TRIGGER_POST:
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, false);
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, true);
2177 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2178 /* bespoke trigger() - handles both FE and BEs */
2180 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2181 fe->dai_link->name, cmd);
2183 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2186 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2187 fe->dai_link->name);
2193 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2199 case SNDRV_PCM_TRIGGER_START:
2200 case SNDRV_PCM_TRIGGER_RESUME:
2201 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2202 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2204 case SNDRV_PCM_TRIGGER_STOP:
2205 case SNDRV_PCM_TRIGGER_SUSPEND:
2206 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2208 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2209 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2214 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2218 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2220 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2221 int stream = substream->stream;
2223 /* if FE's runtime_update is already set, we're in race;
2224 * process this trigger later at exit
2226 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2227 fe->dpcm[stream].trigger_pending = cmd + 1;
2228 return 0; /* delayed, assuming it's successful */
2231 /* we're alone, let's trigger */
2232 return dpcm_fe_dai_do_trigger(substream, cmd);
2235 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2237 struct snd_soc_dpcm *dpcm;
2240 for_each_dpcm_be(fe, stream, dpcm) {
2242 struct snd_soc_pcm_runtime *be = dpcm->be;
2243 struct snd_pcm_substream *be_substream =
2244 snd_soc_dpcm_get_substream(be, stream);
2246 /* is this op for this BE ? */
2247 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2250 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2251 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2252 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2256 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2257 be->dai_link->name);
2259 ret = soc_pcm_prepare(be_substream);
2263 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2267 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2272 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2274 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2275 int stream = substream->stream, ret = 0;
2277 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2279 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2281 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2283 /* there is no point preparing this FE if there are no BEs */
2284 if (list_empty(&fe->dpcm[stream].be_clients)) {
2285 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2286 fe->dai_link->name);
2291 ret = dpcm_be_dai_prepare(fe, stream);
2295 /* call prepare on the frontend */
2296 ret = soc_pcm_prepare(substream);
2300 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2303 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2304 mutex_unlock(&fe->card->mutex);
2307 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2312 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2314 struct snd_pcm_substream *substream =
2315 snd_soc_dpcm_get_substream(fe, stream);
2316 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2319 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2320 stream ? "capture" : "playback", fe->dai_link->name);
2322 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2323 /* call bespoke trigger - FE takes care of all BE triggers */
2324 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2325 fe->dai_link->name);
2327 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2329 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2330 fe->dai_link->name);
2332 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2335 dpcm_be_dai_hw_free(fe, stream);
2337 dpcm_be_dai_shutdown(fe, stream);
2339 /* run the stream event for each BE */
2340 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2343 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, err);
2348 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2350 struct snd_pcm_substream *substream =
2351 snd_soc_dpcm_get_substream(fe, stream);
2352 struct snd_soc_dpcm *dpcm;
2353 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2355 unsigned long flags;
2357 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2358 stream ? "capture" : "playback", fe->dai_link->name);
2360 /* Only start the BE if the FE is ready */
2361 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2362 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2363 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2364 fe->dai_link->name, fe->dpcm[stream].state);
2369 /* startup must always be called for new BEs */
2370 ret = dpcm_be_dai_startup(fe, stream);
2374 /* keep going if FE state is > open */
2375 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2378 ret = dpcm_be_dai_hw_params(fe, stream);
2382 /* keep going if FE state is > hw_params */
2383 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2386 ret = dpcm_be_dai_prepare(fe, stream);
2390 /* run the stream event for each BE */
2391 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2393 /* keep going if FE state is > prepare */
2394 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2395 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2398 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2399 /* call trigger on the frontend - FE takes care of all BE triggers */
2400 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2401 fe->dai_link->name);
2403 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2407 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2408 fe->dai_link->name);
2410 ret = dpcm_be_dai_trigger(fe, stream,
2411 SNDRV_PCM_TRIGGER_START);
2419 dpcm_be_dai_hw_free(fe, stream);
2421 dpcm_be_dai_shutdown(fe, stream);
2423 /* disconnect any pending BEs */
2424 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2425 for_each_dpcm_be(fe, stream, dpcm) {
2426 struct snd_soc_pcm_runtime *be = dpcm->be;
2428 /* is this op for this BE ? */
2429 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2432 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2433 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2434 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2436 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2439 dev_err(fe->dev, "ASoC: %s() failed (%d)\n", __func__, ret);
2444 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2446 struct snd_soc_dapm_widget_list *list;
2450 if (!fe->dai_link->dynamic)
2453 if (fe->num_cpus > 1) {
2455 "%s doesn't support Multi CPU yet\n", __func__);
2459 /* only check active links */
2460 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2463 /* DAPM sync will call this to update DSP paths */
2464 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2465 new ? "new" : "old", fe->dai_link->name);
2467 for_each_pcm_streams(stream) {
2469 /* skip if FE doesn't have playback/capture capability */
2470 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2471 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2474 /* skip if FE isn't currently playing/capturing */
2475 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2476 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2479 paths = dpcm_path_get(fe, stream, &list);
2483 /* update any playback/capture paths */
2484 count = dpcm_process_paths(fe, stream, &list, new);
2486 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2488 dpcm_run_update_startup(fe, stream);
2490 dpcm_run_update_shutdown(fe, stream);
2491 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2493 dpcm_clear_pending_state(fe, stream);
2494 dpcm_be_disconnect(fe, stream);
2497 dpcm_path_put(&list);
2503 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2506 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2508 struct snd_soc_pcm_runtime *fe;
2511 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2512 /* shutdown all old paths first */
2513 for_each_card_rtds(card, fe) {
2514 ret = soc_dpcm_fe_runtime_update(fe, 0);
2519 /* bring new paths up */
2520 for_each_card_rtds(card, fe) {
2521 ret = soc_dpcm_fe_runtime_update(fe, 1);
2527 mutex_unlock(&card->mutex);
2530 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2532 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2534 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2535 struct snd_soc_dpcm *dpcm;
2536 int stream = fe_substream->stream;
2538 /* mark FE's links ready to prune */
2539 for_each_dpcm_be(fe, stream, dpcm)
2540 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2542 dpcm_be_disconnect(fe, stream);
2544 fe->dpcm[stream].runtime = NULL;
2547 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2549 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2552 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2553 ret = dpcm_fe_dai_shutdown(fe_substream);
2555 dpcm_fe_dai_cleanup(fe_substream);
2557 mutex_unlock(&fe->card->mutex);
2561 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2563 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2564 struct snd_soc_dapm_widget_list *list;
2566 int stream = fe_substream->stream;
2568 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2569 fe->dpcm[stream].runtime = fe_substream->runtime;
2571 ret = dpcm_path_get(fe, stream, &list);
2575 /* calculate valid and active FE <-> BE dpcms */
2576 dpcm_process_paths(fe, stream, &list, 1);
2578 ret = dpcm_fe_dai_startup(fe_substream);
2580 dpcm_fe_dai_cleanup(fe_substream);
2582 dpcm_clear_pending_state(fe, stream);
2583 dpcm_path_put(&list);
2585 mutex_unlock(&fe->card->mutex);
2589 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2590 int *playback, int *capture)
2592 struct snd_soc_dai *cpu_dai;
2595 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2597 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2601 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2604 if (rtd->dai_link->dpcm_playback) {
2605 stream = SNDRV_PCM_STREAM_PLAYBACK;
2607 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2608 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2614 dev_err(rtd->card->dev,
2615 "No CPU DAIs support playback for stream %s\n",
2616 rtd->dai_link->stream_name);
2620 if (rtd->dai_link->dpcm_capture) {
2621 stream = SNDRV_PCM_STREAM_CAPTURE;
2623 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2624 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2631 dev_err(rtd->card->dev,
2632 "No CPU DAIs support capture for stream %s\n",
2633 rtd->dai_link->stream_name);
2638 struct snd_soc_dai *codec_dai;
2640 /* Adapt stream for codec2codec links */
2641 int cpu_capture = rtd->dai_link->params ?
2642 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2643 int cpu_playback = rtd->dai_link->params ?
2644 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2646 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2647 if (rtd->num_cpus == 1) {
2648 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2649 } else if (rtd->num_cpus == rtd->num_codecs) {
2650 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2652 dev_err(rtd->card->dev,
2653 "N cpus to M codecs link is not supported yet\n");
2657 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2658 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2660 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2661 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2666 if (rtd->dai_link->playback_only) {
2671 if (rtd->dai_link->capture_only) {
2679 static int soc_create_pcm(struct snd_pcm **pcm,
2680 struct snd_soc_pcm_runtime *rtd,
2681 int playback, int capture, int num)
2686 /* create the PCM */
2687 if (rtd->dai_link->params) {
2688 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2689 rtd->dai_link->stream_name);
2691 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2692 playback, capture, pcm);
2693 } else if (rtd->dai_link->no_pcm) {
2694 snprintf(new_name, sizeof(new_name), "(%s)",
2695 rtd->dai_link->stream_name);
2697 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2698 playback, capture, pcm);
2700 if (rtd->dai_link->dynamic)
2701 snprintf(new_name, sizeof(new_name), "%s (*)",
2702 rtd->dai_link->stream_name);
2704 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2705 rtd->dai_link->stream_name,
2706 soc_codec_dai_name(rtd), num);
2708 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2712 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2713 new_name, rtd->dai_link->name, ret);
2716 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2721 /* create a new pcm */
2722 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2724 struct snd_soc_component *component;
2725 struct snd_pcm *pcm;
2726 int ret = 0, playback = 0, capture = 0;
2729 ret = soc_get_playback_capture(rtd, &playback, &capture);
2733 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2737 /* DAPM dai link stream work */
2738 if (rtd->dai_link->params)
2739 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2741 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2744 pcm->nonatomic = rtd->dai_link->nonatomic;
2745 pcm->private_data = rtd;
2747 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2749 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2751 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2755 /* ASoC PCM operations */
2756 if (rtd->dai_link->dynamic) {
2757 rtd->ops.open = dpcm_fe_dai_open;
2758 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2759 rtd->ops.prepare = dpcm_fe_dai_prepare;
2760 rtd->ops.trigger = dpcm_fe_dai_trigger;
2761 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2762 rtd->ops.close = dpcm_fe_dai_close;
2763 rtd->ops.pointer = soc_pcm_pointer;
2765 rtd->ops.open = soc_pcm_open;
2766 rtd->ops.hw_params = soc_pcm_hw_params;
2767 rtd->ops.prepare = soc_pcm_prepare;
2768 rtd->ops.trigger = soc_pcm_trigger;
2769 rtd->ops.hw_free = soc_pcm_hw_free;
2770 rtd->ops.close = soc_pcm_close;
2771 rtd->ops.pointer = soc_pcm_pointer;
2774 for_each_rtd_components(rtd, i, component) {
2775 const struct snd_soc_component_driver *drv = component->driver;
2778 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2780 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2782 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2784 rtd->ops.page = snd_soc_pcm_component_page;
2786 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2788 rtd->ops.ack = snd_soc_pcm_component_ack;
2792 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2795 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2797 ret = snd_soc_pcm_component_new(rtd);
2801 pcm->no_device_suspend = true;
2803 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2804 soc_codec_dai_name(rtd), soc_cpu_dai_name(rtd));
2808 /* is the current PCM operation for this FE ? */
2809 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2811 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2815 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2817 /* is the current PCM operation for this BE ? */
2818 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2819 struct snd_soc_pcm_runtime *be, int stream)
2821 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2822 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2823 be->dpcm[stream].runtime_update))
2827 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2829 /* get the substream for this BE */
2830 struct snd_pcm_substream *
2831 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2833 return be->pcm->streams[stream].substream;
2835 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2837 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2838 struct snd_soc_pcm_runtime *be,
2840 const enum snd_soc_dpcm_state *states,
2843 struct snd_soc_dpcm *dpcm;
2846 unsigned long flags;
2849 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2850 for_each_dpcm_fe(be, stream, dpcm) {
2855 state = dpcm->fe->dpcm[stream].state;
2856 for (i = 0; i < num_states; i++) {
2857 if (state == states[i]) {
2863 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2865 /* it's safe to do this BE DAI */
2870 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2871 * are not running, paused or suspended for the specified stream direction.
2873 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2874 struct snd_soc_pcm_runtime *be, int stream)
2876 const enum snd_soc_dpcm_state state[] = {
2877 SND_SOC_DPCM_STATE_START,
2878 SND_SOC_DPCM_STATE_PAUSED,
2879 SND_SOC_DPCM_STATE_SUSPEND,
2882 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2884 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2887 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2888 * running, paused or suspended for the specified stream direction.
2890 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2891 struct snd_soc_pcm_runtime *be, int stream)
2893 const enum snd_soc_dpcm_state state[] = {
2894 SND_SOC_DPCM_STATE_START,
2895 SND_SOC_DPCM_STATE_PAUSED,
2896 SND_SOC_DPCM_STATE_SUSPEND,
2897 SND_SOC_DPCM_STATE_PREPARE,
2900 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2902 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);