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 #ifdef CONFIG_DEBUG_FS
33 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
36 case SND_SOC_DPCM_STATE_NEW:
38 case SND_SOC_DPCM_STATE_OPEN:
40 case SND_SOC_DPCM_STATE_HW_PARAMS:
42 case SND_SOC_DPCM_STATE_PREPARE:
44 case SND_SOC_DPCM_STATE_START:
46 case SND_SOC_DPCM_STATE_STOP:
48 case SND_SOC_DPCM_STATE_SUSPEND:
50 case SND_SOC_DPCM_STATE_PAUSED:
52 case SND_SOC_DPCM_STATE_HW_FREE:
54 case SND_SOC_DPCM_STATE_CLOSE:
61 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
62 int stream, char *buf, size_t size)
64 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
65 struct snd_soc_dpcm *dpcm;
70 offset += scnprintf(buf + offset, size - offset,
71 "[%s - %s]\n", fe->dai_link->name,
72 stream ? "Capture" : "Playback");
74 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
75 dpcm_state_string(fe->dpcm[stream].state));
77 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
78 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
79 offset += scnprintf(buf + offset, size - offset,
81 "Format = %s, Channels = %d, Rate = %d\n",
82 snd_pcm_format_name(params_format(params)),
83 params_channels(params),
87 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
89 if (list_empty(&fe->dpcm[stream].be_clients)) {
90 offset += scnprintf(buf + offset, size - offset,
91 " No active DSP links\n");
95 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
96 for_each_dpcm_be(fe, stream, dpcm) {
97 struct snd_soc_pcm_runtime *be = dpcm->be;
98 params = &dpcm->hw_params;
100 offset += scnprintf(buf + offset, size - offset,
101 "- %s\n", be->dai_link->name);
103 offset += scnprintf(buf + offset, size - offset,
105 dpcm_state_string(be->dpcm[stream].state));
107 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
108 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
109 offset += scnprintf(buf + offset, size - offset,
111 "Format = %s, Channels = %d, Rate = %d\n",
112 snd_pcm_format_name(params_format(params)),
113 params_channels(params),
114 params_rate(params));
116 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
121 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
122 size_t count, loff_t *ppos)
124 struct snd_soc_pcm_runtime *fe = file->private_data;
125 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
129 if (fe->num_cpus > 1) {
131 "%s doesn't support Multi CPU yet\n", __func__);
135 buf = kmalloc(out_count, GFP_KERNEL);
139 for_each_pcm_streams(stream)
140 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
141 offset += dpcm_show_state(fe, stream,
145 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
151 static const struct file_operations dpcm_state_fops = {
153 .read = dpcm_state_read_file,
154 .llseek = default_llseek,
157 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
162 if (!rtd->dai_link->dynamic)
165 if (!rtd->card->debugfs_card_root)
168 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
169 rtd->card->debugfs_card_root);
171 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
172 rtd, &dpcm_state_fops);
175 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
179 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
180 stream ? "capture" : "playback");
182 dpcm->debugfs_state = debugfs_create_dir(
183 name, dpcm->fe->debugfs_dpcm_root);
184 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
190 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
192 debugfs_remove_recursive(dpcm->debugfs_state);
196 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
201 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
206 /* Set FE's runtime_update state; the state is protected via PCM stream lock
207 * for avoiding the race with trigger callback.
208 * If the state is unset and a trigger is pending while the previous operation,
209 * process the pending trigger action here.
211 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
212 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
213 int stream, enum snd_soc_dpcm_update state)
215 struct snd_pcm_substream *substream =
216 snd_soc_dpcm_get_substream(fe, stream);
218 snd_pcm_stream_lock_irq(substream);
219 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
220 dpcm_fe_dai_do_trigger(substream,
221 fe->dpcm[stream].trigger_pending - 1);
222 fe->dpcm[stream].trigger_pending = 0;
224 fe->dpcm[stream].runtime_update = state;
225 snd_pcm_stream_unlock_irq(substream);
228 static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
229 int stream, enum snd_soc_dpcm_update state)
231 be->dpcm[stream].runtime_update = state;
235 * snd_soc_runtime_action() - Increment/Decrement active count for
236 * PCM runtime components
237 * @rtd: ASoC PCM runtime that is activated
238 * @stream: Direction of the PCM stream
239 * @action: Activate stream if 1. Deactivate if -1.
241 * Increments/Decrements the active count for all the DAIs and components
242 * attached to a PCM runtime.
243 * Should typically be called when a stream is opened.
245 * Must be called with the rtd->card->pcm_mutex being held
247 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
248 int stream, int action)
250 struct snd_soc_dai *dai;
253 lockdep_assert_held(&rtd->card->pcm_mutex);
255 for_each_rtd_dais(rtd, i, dai)
256 snd_soc_dai_action(dai, stream, action);
258 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
261 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
262 * @rtd: The ASoC PCM runtime that should be checked.
264 * This function checks whether the power down delay should be ignored for a
265 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
266 * been configured to ignore the delay, or if none of the components benefits
267 * from having the delay.
269 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
271 struct snd_soc_component *component;
275 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
278 for_each_rtd_components(rtd, i, component)
279 ignore &= !component->driver->use_pmdown_time;
285 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
286 * @substream: the pcm substream
287 * @hw: the hardware parameters
289 * Sets the substream runtime hardware parameters.
291 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
292 const struct snd_pcm_hardware *hw)
294 struct snd_pcm_runtime *runtime = substream->runtime;
295 runtime->hw.info = hw->info;
296 runtime->hw.formats = hw->formats;
297 runtime->hw.period_bytes_min = hw->period_bytes_min;
298 runtime->hw.period_bytes_max = hw->period_bytes_max;
299 runtime->hw.periods_min = hw->periods_min;
300 runtime->hw.periods_max = hw->periods_max;
301 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
302 runtime->hw.fifo_size = hw->fifo_size;
305 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
307 /* DPCM stream event, send event to FE and all active BEs. */
308 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
311 struct snd_soc_dpcm *dpcm;
313 for_each_dpcm_be(fe, dir, dpcm) {
315 struct snd_soc_pcm_runtime *be = dpcm->be;
317 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
318 be->dai_link->name, event, dir);
320 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
321 (be->dpcm[dir].users >= 1))
324 snd_soc_dapm_stream_event(be, dir, event);
327 snd_soc_dapm_stream_event(fe, dir, event);
332 static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
333 struct snd_pcm_hw_params *params)
336 dai->rate = params_rate(params);
337 dai->channels = params_channels(params);
338 dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
342 dai->sample_bits = 0;
346 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
347 struct snd_soc_dai *soc_dai)
349 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
352 #define __soc_pcm_apply_symmetry(name, NAME) \
353 if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
354 rtd->dai_link->symmetric_##name)) { \
355 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
356 #name, soc_dai->name); \
358 ret = snd_pcm_hw_constraint_single(substream->runtime, \
359 SNDRV_PCM_HW_PARAM_##NAME,\
362 dev_err(soc_dai->dev, \
363 "ASoC: Unable to apply %s constraint: %d\n",\
369 __soc_pcm_apply_symmetry(rate, RATE);
370 __soc_pcm_apply_symmetry(channels, CHANNELS);
371 __soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
376 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
377 struct snd_pcm_hw_params *params)
379 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
380 struct snd_soc_dai d;
381 struct snd_soc_dai *dai;
382 struct snd_soc_dai *cpu_dai;
383 unsigned int symmetry, i;
385 soc_pcm_set_dai_params(&d, params);
387 #define __soc_pcm_params_symmetry(name) \
388 symmetry = rtd->dai_link->symmetric_##name; \
389 for_each_rtd_dais(rtd, i, dai) \
390 symmetry |= dai->driver->symmetric_##name; \
393 for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
394 if (cpu_dai->name && cpu_dai->name != d.name) { \
395 dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %d - %d\n", \
396 #name, cpu_dai->name, d.name); \
400 /* reject unmatched parameters when applying symmetry */
401 __soc_pcm_params_symmetry(rate);
402 __soc_pcm_params_symmetry(channels);
403 __soc_pcm_params_symmetry(sample_bits);
408 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
410 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
411 struct snd_soc_dai_link *link = rtd->dai_link;
412 struct snd_soc_dai *dai;
413 unsigned int symmetry, i;
415 symmetry = link->symmetric_rate ||
416 link->symmetric_channels ||
417 link->symmetric_sample_bits;
419 for_each_rtd_dais(rtd, i, dai)
420 symmetry = symmetry ||
421 dai->driver->symmetric_rate ||
422 dai->driver->symmetric_channels ||
423 dai->driver->symmetric_sample_bits;
428 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
430 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
436 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
438 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
442 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
444 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
445 struct snd_soc_dai *cpu_dai;
446 struct snd_soc_dai *codec_dai;
447 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
448 int stream = substream->stream;
450 unsigned int bits = 0, cpu_bits = 0;
452 for_each_rtd_codec_dais(rtd, i, codec_dai) {
453 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
455 if (pcm_codec->sig_bits == 0) {
459 bits = max(pcm_codec->sig_bits, bits);
462 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
463 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
465 if (pcm_cpu->sig_bits == 0) {
469 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
472 soc_pcm_set_msb(substream, bits);
473 soc_pcm_set_msb(substream, cpu_bits);
476 static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
478 hw->rates = UINT_MAX;
480 hw->rate_max = UINT_MAX;
481 hw->channels_min = 0;
482 hw->channels_max = UINT_MAX;
485 static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
486 struct snd_soc_pcm_stream *p)
488 hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
490 /* setup hw->rate_min/max via hw->rates first */
491 snd_pcm_hw_limit_rates(hw);
493 /* update hw->rate_min/max by snd_soc_pcm_stream */
494 hw->rate_min = max(hw->rate_min, p->rate_min);
495 hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
498 static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
499 struct snd_soc_pcm_stream *p)
501 hw->channels_min = max(hw->channels_min, p->channels_min);
502 hw->channels_max = min(hw->channels_max, p->channels_max);
506 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
507 * @rtd: ASoC PCM runtime
508 * @hw: PCM hardware parameters (output)
509 * @stream: Direction of the PCM stream
511 * Calculates the subset of stream parameters supported by all DAIs
512 * associated with the PCM stream.
514 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
515 struct snd_pcm_hardware *hw, int stream)
517 struct snd_soc_dai *codec_dai;
518 struct snd_soc_dai *cpu_dai;
519 struct snd_soc_pcm_stream *codec_stream;
520 struct snd_soc_pcm_stream *cpu_stream;
521 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
522 u64 formats = ULLONG_MAX;
527 /* first calculate min/max only for CPUs in the DAI link */
528 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
531 * Skip CPUs which don't support the current stream type.
532 * Otherwise, since the rate, channel, and format values will
533 * zero in that case, we would have no usable settings left,
534 * causing the resulting setup to fail.
536 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
539 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
541 soc_pcm_hw_update_chan(hw, cpu_stream);
542 soc_pcm_hw_update_rate(hw, cpu_stream);
543 formats &= cpu_stream->formats;
545 cpu_chan_min = hw->channels_min;
546 cpu_chan_max = hw->channels_max;
548 /* second calculate min/max only for CODECs in the DAI link */
549 for_each_rtd_codec_dais(rtd, i, codec_dai) {
552 * Skip CODECs which don't support the current stream type.
553 * Otherwise, since the rate, channel, and format values will
554 * zero in that case, we would have no usable settings left,
555 * causing the resulting setup to fail.
557 if (!snd_soc_dai_stream_valid(codec_dai, stream))
560 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
562 soc_pcm_hw_update_chan(hw, codec_stream);
563 soc_pcm_hw_update_rate(hw, codec_stream);
564 formats &= codec_stream->formats;
567 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
568 if (!hw->channels_min)
572 * chan min/max cannot be enforced if there are multiple CODEC DAIs
573 * connected to CPU DAI(s), use CPU DAI's directly and let
574 * channel allocation be fixed up later
576 if (rtd->num_codecs > 1) {
577 hw->channels_min = cpu_chan_min;
578 hw->channels_max = cpu_chan_max;
581 /* finally find a intersection between CODECs and CPUs */
582 hw->formats = formats;
586 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
588 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
590 struct snd_pcm_hardware *hw = &substream->runtime->hw;
591 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
592 u64 formats = hw->formats;
595 * At least one CPU and one CODEC should match. Otherwise, we should
596 * have bailed out on a higher level, since there would be no CPU or
597 * CODEC to support the transfer direction in that case.
599 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
602 hw->formats &= formats;
605 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
607 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
608 struct snd_soc_component *component;
611 for_each_rtd_components(rtd, i, component) {
612 ret = snd_soc_component_module_get_when_open(component, substream);
616 ret = snd_soc_component_open(component, substream);
624 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
627 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
628 struct snd_soc_component *component;
631 for_each_rtd_components(rtd, i, component) {
632 r = snd_soc_component_close(component, substream, rollback);
634 ret = r; /* use last ret */
636 snd_soc_component_module_put_when_close(component, substream, rollback);
642 static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
644 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
645 struct snd_soc_component *component;
646 struct snd_soc_dai *dai;
649 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
652 snd_soc_runtime_deactivate(rtd, substream->stream);
654 for_each_rtd_dais(rtd, i, dai)
655 snd_soc_dai_shutdown(dai, substream, rollback);
657 snd_soc_link_shutdown(substream, rollback);
659 soc_pcm_components_close(substream, rollback);
662 mutex_unlock(&rtd->card->pcm_mutex);
664 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
666 for_each_rtd_components(rtd, i, component)
667 if (!snd_soc_component_active(component))
668 pinctrl_pm_select_sleep_state(component->dev);
674 * Called by ALSA when a PCM substream is closed. Private data can be
675 * freed here. The cpu DAI, codec DAI, machine and components are also
678 static int soc_pcm_close(struct snd_pcm_substream *substream)
680 return soc_pcm_clean(substream, 0);
684 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
685 * then initialized and any private data can be allocated. This also calls
686 * startup for the cpu DAI, component, machine and codec DAI.
688 static int soc_pcm_open(struct snd_pcm_substream *substream)
690 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
691 struct snd_pcm_runtime *runtime = substream->runtime;
692 struct snd_soc_component *component;
693 struct snd_soc_dai *dai;
694 const char *codec_dai_name = "multicodec";
695 const char *cpu_dai_name = "multicpu";
698 for_each_rtd_components(rtd, i, component)
699 pinctrl_pm_select_default_state(component->dev);
701 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
705 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
707 ret = soc_pcm_components_open(substream);
711 ret = snd_soc_link_startup(substream);
715 /* startup the audio subsystem */
716 for_each_rtd_dais(rtd, i, dai) {
717 ret = snd_soc_dai_startup(dai, substream);
721 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
727 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
728 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
731 /* Check that the codec and cpu DAIs are compatible */
732 soc_pcm_init_runtime_hw(substream);
734 if (rtd->num_codecs == 1)
735 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
737 if (rtd->num_cpus == 1)
738 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
740 if (soc_pcm_has_symmetry(substream))
741 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
744 if (!runtime->hw.rates) {
745 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
746 codec_dai_name, cpu_dai_name);
749 if (!runtime->hw.formats) {
750 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
751 codec_dai_name, cpu_dai_name);
754 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
755 runtime->hw.channels_min > runtime->hw.channels_max) {
756 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
757 codec_dai_name, cpu_dai_name);
761 soc_pcm_apply_msb(substream);
763 /* Symmetry only applies if we've already got an active stream. */
764 for_each_rtd_dais(rtd, i, dai) {
765 if (snd_soc_dai_active(dai)) {
766 ret = soc_pcm_apply_symmetry(substream, dai);
772 pr_debug("ASoC: %s <-> %s info:\n",
773 codec_dai_name, cpu_dai_name);
774 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
775 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
776 runtime->hw.channels_max);
777 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
778 runtime->hw.rate_max);
780 snd_soc_runtime_activate(rtd, substream->stream);
783 mutex_unlock(&rtd->card->pcm_mutex);
786 soc_pcm_clean(substream, 1);
791 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
794 * Currently nothing to do for c2c links
795 * Since c2c links are internal nodes in the DAPM graph and
796 * don't interface with the outside world or application layer
797 * we don't have to do any special handling on close.
802 * Called by ALSA when the PCM substream is prepared, can set format, sample
803 * rate, etc. This function is non atomic and can be called multiple times,
804 * it can refer to the runtime info.
806 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
808 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
809 struct snd_soc_dai *dai;
812 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
814 ret = snd_soc_link_prepare(substream);
818 ret = snd_soc_pcm_component_prepare(substream);
822 ret = snd_soc_pcm_dai_prepare(substream);
824 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
828 /* cancel any delayed stream shutdown that is pending */
829 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
832 cancel_delayed_work(&rtd->delayed_work);
835 snd_soc_dapm_stream_event(rtd, substream->stream,
836 SND_SOC_DAPM_STREAM_START);
838 for_each_rtd_dais(rtd, i, dai)
839 snd_soc_dai_digital_mute(dai, 0, substream->stream);
842 mutex_unlock(&rtd->card->pcm_mutex);
846 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
849 struct snd_interval *interval;
850 int channels = hweight_long(mask);
852 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
853 interval->min = channels;
854 interval->max = channels;
857 static int soc_pcm_hw_clean(struct snd_pcm_substream *substream, int rollback)
859 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
860 struct snd_soc_dai *dai;
863 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
865 /* clear the corresponding DAIs parameters when going to be inactive */
866 for_each_rtd_dais(rtd, i, dai) {
867 int active = snd_soc_dai_stream_active(dai, substream->stream);
869 if (snd_soc_dai_active(dai) == 1)
870 soc_pcm_set_dai_params(dai, NULL);
873 snd_soc_dai_digital_mute(dai, 1, substream->stream);
876 /* run the stream event */
877 snd_soc_dapm_stream_stop(rtd, substream->stream);
879 /* free any machine hw params */
880 snd_soc_link_hw_free(substream, rollback);
882 /* free any component resources */
883 snd_soc_pcm_component_hw_free(substream, rollback);
885 /* now free hw params for the DAIs */
886 for_each_rtd_dais(rtd, i, dai) {
887 if (!snd_soc_dai_stream_valid(dai, substream->stream))
890 snd_soc_dai_hw_free(dai, substream, rollback);
893 mutex_unlock(&rtd->card->pcm_mutex);
898 * Frees resources allocated by hw_params, can be called multiple times
900 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
902 return soc_pcm_hw_clean(substream, 0);
906 * Called by ALSA when the hardware params are set by application. This
907 * function can also be called multiple times and can allocate buffers
908 * (using snd_pcm_lib_* ). It's non-atomic.
910 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
911 struct snd_pcm_hw_params *params)
913 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
914 struct snd_soc_dai *cpu_dai;
915 struct snd_soc_dai *codec_dai;
918 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
920 ret = soc_pcm_params_symmetry(substream, params);
924 ret = snd_soc_link_hw_params(substream, params);
928 for_each_rtd_codec_dais(rtd, i, codec_dai) {
929 struct snd_pcm_hw_params codec_params;
932 * Skip CODECs which don't support the current stream type,
933 * the idea being that if a CODEC is not used for the currently
934 * set up transfer direction, it should not need to be
935 * configured, especially since the configuration used might
936 * not even be supported by that CODEC. There may be cases
937 * however where a CODEC needs to be set up although it is
938 * actually not being used for the transfer, e.g. if a
939 * capture-only CODEC is acting as an LRCLK and/or BCLK master
940 * for the DAI link including a playback-only CODEC.
941 * If this becomes necessary, we will have to augment the
942 * machine driver setup with information on how to act, so
943 * we can do the right thing here.
945 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
948 /* copy params for each codec */
949 codec_params = *params;
951 /* fixup params based on TDM slot masks */
952 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
954 soc_pcm_codec_params_fixup(&codec_params,
957 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
959 soc_pcm_codec_params_fixup(&codec_params,
962 ret = snd_soc_dai_hw_params(codec_dai, substream,
967 soc_pcm_set_dai_params(codec_dai, &codec_params);
968 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
971 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
973 * Skip CPUs which don't support the current stream
974 * type. See soc_pcm_init_runtime_hw() for more details
976 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
979 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
983 /* store the parameters for each DAI */
984 soc_pcm_set_dai_params(cpu_dai, params);
985 snd_soc_dapm_update_dai(substream, params, cpu_dai);
988 ret = snd_soc_pcm_component_hw_params(substream, params);
990 mutex_unlock(&rtd->card->pcm_mutex);
993 soc_pcm_hw_clean(substream, 1);
998 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1000 int ret = -EINVAL, _ret = 0;
1004 case SNDRV_PCM_TRIGGER_START:
1005 case SNDRV_PCM_TRIGGER_RESUME:
1006 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1007 ret = snd_soc_link_trigger(substream, cmd, 0);
1011 ret = snd_soc_pcm_component_trigger(substream, cmd, 0);
1015 ret = snd_soc_pcm_dai_trigger(substream, cmd, 0);
1024 case SNDRV_PCM_TRIGGER_START:
1025 cmd = SNDRV_PCM_TRIGGER_STOP;
1027 case SNDRV_PCM_TRIGGER_RESUME:
1028 cmd = SNDRV_PCM_TRIGGER_SUSPEND;
1030 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1031 cmd = SNDRV_PCM_TRIGGER_PAUSE_PUSH;
1037 case SNDRV_PCM_TRIGGER_STOP:
1038 case SNDRV_PCM_TRIGGER_SUSPEND:
1039 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1040 ret = snd_soc_pcm_dai_trigger(substream, cmd, rollback);
1044 ret = snd_soc_pcm_component_trigger(substream, cmd, rollback);
1048 ret = snd_soc_link_trigger(substream, cmd, rollback);
1059 * soc level wrapper for pointer callback
1060 * If cpu_dai, codec_dai, component driver has the delay callback, then
1061 * the runtime->delay will be updated accordingly.
1063 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1065 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1066 struct snd_soc_dai *cpu_dai;
1067 struct snd_soc_dai *codec_dai;
1068 struct snd_pcm_runtime *runtime = substream->runtime;
1069 snd_pcm_uframes_t offset = 0;
1070 snd_pcm_sframes_t delay = 0;
1071 snd_pcm_sframes_t codec_delay = 0;
1072 snd_pcm_sframes_t cpu_delay = 0;
1075 /* clearing the previous total delay */
1078 offset = snd_soc_pcm_component_pointer(substream);
1080 /* base delay if assigned in pointer callback */
1081 delay = runtime->delay;
1083 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1084 cpu_delay = max(cpu_delay,
1085 snd_soc_dai_delay(cpu_dai, substream));
1089 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1090 codec_delay = max(codec_delay,
1091 snd_soc_dai_delay(codec_dai, substream));
1093 delay += codec_delay;
1095 runtime->delay = delay;
1100 /* connect a FE and BE */
1101 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1102 struct snd_soc_pcm_runtime *be, int stream)
1104 struct snd_soc_dpcm *dpcm;
1105 unsigned long flags;
1107 /* only add new dpcms */
1108 for_each_dpcm_be(fe, stream, dpcm) {
1109 if (dpcm->be == be && dpcm->fe == fe)
1113 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1119 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1120 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1121 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1122 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1123 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1124 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1126 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1127 stream ? "capture" : "playback", fe->dai_link->name,
1128 stream ? "<-" : "->", be->dai_link->name);
1130 dpcm_create_debugfs_state(dpcm, stream);
1135 /* reparent a BE onto another FE */
1136 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1137 struct snd_soc_pcm_runtime *be, int stream)
1139 struct snd_soc_dpcm *dpcm;
1140 struct snd_pcm_substream *fe_substream, *be_substream;
1142 /* reparent if BE is connected to other FEs */
1143 if (!be->dpcm[stream].users)
1146 be_substream = snd_soc_dpcm_get_substream(be, stream);
1148 for_each_dpcm_fe(be, stream, dpcm) {
1152 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1153 stream ? "capture" : "playback",
1154 dpcm->fe->dai_link->name,
1155 stream ? "<-" : "->", dpcm->be->dai_link->name);
1157 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1158 be_substream->runtime = fe_substream->runtime;
1163 /* disconnect a BE and FE */
1164 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1166 struct snd_soc_dpcm *dpcm, *d;
1167 unsigned long flags;
1169 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1170 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1171 stream ? "capture" : "playback",
1172 dpcm->be->dai_link->name);
1174 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1177 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1178 stream ? "capture" : "playback", fe->dai_link->name,
1179 stream ? "<-" : "->", dpcm->be->dai_link->name);
1181 /* BEs still alive need new FE */
1182 dpcm_be_reparent(fe, dpcm->be, stream);
1184 dpcm_remove_debugfs_state(dpcm);
1186 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1187 list_del(&dpcm->list_be);
1188 list_del(&dpcm->list_fe);
1189 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1194 /* get BE for DAI widget and stream */
1195 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1196 struct snd_soc_dapm_widget *widget, int stream)
1198 struct snd_soc_pcm_runtime *be;
1199 struct snd_soc_dapm_widget *w;
1200 struct snd_soc_dai *dai;
1203 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1205 for_each_card_rtds(card, be) {
1207 if (!be->dai_link->no_pcm)
1210 for_each_rtd_dais(be, i, dai) {
1211 w = snd_soc_dai_get_widget(dai, stream);
1213 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1214 w ? w->name : "(not set)");
1221 /* Widget provided is not a BE */
1225 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1226 struct snd_soc_dapm_widget *widget)
1228 struct snd_soc_dapm_widget *w;
1231 for_each_dapm_widgets(list, i, w)
1238 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1239 enum snd_soc_dapm_direction dir)
1241 struct snd_soc_card *card = widget->dapm->card;
1242 struct snd_soc_pcm_runtime *rtd;
1245 /* adjust dir to stream */
1246 if (dir == SND_SOC_DAPM_DIR_OUT)
1247 stream = SNDRV_PCM_STREAM_PLAYBACK;
1249 stream = SNDRV_PCM_STREAM_CAPTURE;
1251 rtd = dpcm_get_be(card, widget, stream);
1258 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1259 int stream, struct snd_soc_dapm_widget_list **list)
1261 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1264 if (fe->num_cpus > 1) {
1266 "%s doesn't support Multi CPU yet\n", __func__);
1270 /* get number of valid DAI paths and their widgets */
1271 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1272 fe->card->component_chaining ?
1273 NULL : dpcm_end_walk_at_be);
1275 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1276 stream ? "capture" : "playback");
1281 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1283 snd_soc_dapm_dai_free_widgets(list);
1286 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1287 struct snd_soc_dapm_widget_list *list)
1289 struct snd_soc_dapm_widget *widget;
1290 struct snd_soc_dai *dai;
1293 /* is there a valid DAI widget for this BE */
1294 for_each_rtd_dais(dpcm->be, i, dai) {
1295 widget = snd_soc_dai_get_widget(dai, stream);
1298 * The BE is pruned only if none of the dai
1299 * widgets are in the active list.
1301 if (widget && widget_in_list(list, widget))
1308 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1309 struct snd_soc_dapm_widget_list **list_)
1311 struct snd_soc_dpcm *dpcm;
1314 /* Destroy any old FE <--> BE connections */
1315 for_each_dpcm_be(fe, stream, dpcm) {
1316 if (dpcm_be_is_active(dpcm, stream, *list_))
1319 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1320 stream ? "capture" : "playback",
1321 dpcm->be->dai_link->name, fe->dai_link->name);
1322 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1323 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_BE);
1327 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1331 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1332 struct snd_soc_dapm_widget_list **list_)
1334 struct snd_soc_card *card = fe->card;
1335 struct snd_soc_dapm_widget_list *list = *list_;
1336 struct snd_soc_pcm_runtime *be;
1337 struct snd_soc_dapm_widget *widget;
1338 int i, new = 0, err;
1340 /* Create any new FE <--> BE connections */
1341 for_each_dapm_widgets(list, i, widget) {
1343 switch (widget->id) {
1344 case snd_soc_dapm_dai_in:
1345 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1348 case snd_soc_dapm_dai_out:
1349 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1356 /* is there a valid BE rtd for this widget */
1357 be = dpcm_get_be(card, widget, stream);
1359 dev_dbg(fe->dev, "ASoC: no BE found for %s\n",
1364 /* don't connect if FE is not running */
1365 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1368 /* newly connected FE and BE */
1369 err = dpcm_be_connect(fe, be, stream);
1371 dev_err(fe->dev, "ASoC: can't connect %s\n",
1374 } else if (err == 0) /* already connected */
1378 dpcm_set_be_update_state(be, stream, SND_SOC_DPCM_UPDATE_BE);
1382 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1387 * Find the corresponding BE DAIs that source or sink audio to this
1390 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1391 int stream, struct snd_soc_dapm_widget_list **list, int new)
1394 return dpcm_add_paths(fe, stream, list);
1396 return dpcm_prune_paths(fe, stream, list);
1399 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1401 struct snd_soc_dpcm *dpcm;
1402 unsigned long flags;
1404 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1405 for_each_dpcm_be(fe, stream, dpcm)
1406 dpcm_set_be_update_state(dpcm->be, stream, SND_SOC_DPCM_UPDATE_NO);
1407 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1410 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1413 struct snd_soc_dpcm *dpcm;
1415 /* disable any enabled and non active backends */
1416 for_each_dpcm_be(fe, stream, dpcm) {
1418 struct snd_soc_pcm_runtime *be = dpcm->be;
1419 struct snd_pcm_substream *be_substream =
1420 snd_soc_dpcm_get_substream(be, stream);
1422 if (be->dpcm[stream].users == 0)
1423 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1424 stream ? "capture" : "playback",
1425 be->dpcm[stream].state);
1427 if (--be->dpcm[stream].users != 0)
1430 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1433 soc_pcm_close(be_substream);
1434 be_substream->runtime = NULL;
1435 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1439 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1441 struct snd_soc_dpcm *dpcm;
1444 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1445 for_each_dpcm_be(fe, stream, dpcm) {
1447 struct snd_soc_pcm_runtime *be = dpcm->be;
1448 struct snd_pcm_substream *be_substream =
1449 snd_soc_dpcm_get_substream(be, stream);
1451 if (!be_substream) {
1452 dev_err(be->dev, "ASoC: no backend %s stream\n",
1453 stream ? "capture" : "playback");
1457 /* is this op for this BE ? */
1458 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1461 /* first time the dpcm is open ? */
1462 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1463 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1464 stream ? "capture" : "playback",
1465 be->dpcm[stream].state);
1467 if (be->dpcm[stream].users++ != 0)
1470 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1471 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1474 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1475 stream ? "capture" : "playback", be->dai_link->name);
1477 be_substream->runtime = be->dpcm[stream].runtime;
1478 err = soc_pcm_open(be_substream);
1480 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1481 be->dpcm[stream].users--;
1482 if (be->dpcm[stream].users < 0)
1483 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1484 stream ? "capture" : "playback",
1485 be->dpcm[stream].state);
1487 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1491 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1498 /* disable any enabled and non active backends */
1499 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1500 struct snd_soc_pcm_runtime *be = dpcm->be;
1501 struct snd_pcm_substream *be_substream =
1502 snd_soc_dpcm_get_substream(be, stream);
1504 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1507 if (be->dpcm[stream].users == 0)
1508 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1509 stream ? "capture" : "playback",
1510 be->dpcm[stream].state);
1512 if (--be->dpcm[stream].users != 0)
1515 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1518 soc_pcm_close(be_substream);
1519 be_substream->runtime = NULL;
1520 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1526 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1527 struct snd_soc_pcm_stream *stream)
1529 struct snd_pcm_hardware *hw = &runtime->hw;
1531 soc_pcm_hw_update_rate(hw, stream);
1532 soc_pcm_hw_update_chan(hw, stream);
1533 if (runtime->hw.formats)
1534 runtime->hw.formats &= stream->formats;
1536 runtime->hw.formats = stream->formats;
1539 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1540 struct snd_pcm_runtime *runtime)
1542 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1543 struct snd_pcm_hardware *hw = &runtime->hw;
1544 struct snd_soc_dpcm *dpcm;
1545 struct snd_soc_dai *dai;
1546 int stream = substream->stream;
1548 if (!fe->dai_link->dpcm_merged_format)
1552 * It returns merged BE codec format
1553 * if FE want to use it (= dpcm_merged_format)
1556 for_each_dpcm_be(fe, stream, dpcm) {
1557 struct snd_soc_pcm_runtime *be = dpcm->be;
1558 struct snd_soc_pcm_stream *codec_stream;
1561 for_each_rtd_codec_dais(be, i, dai) {
1563 * Skip CODECs which don't support the current stream
1564 * type. See soc_pcm_init_runtime_hw() for more details
1566 if (!snd_soc_dai_stream_valid(dai, stream))
1569 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1571 hw->formats &= codec_stream->formats;
1576 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1577 struct snd_pcm_runtime *runtime)
1579 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1580 struct snd_pcm_hardware *hw = &runtime->hw;
1581 struct snd_soc_dpcm *dpcm;
1582 int stream = substream->stream;
1584 if (!fe->dai_link->dpcm_merged_chan)
1588 * It returns merged BE codec channel;
1589 * if FE want to use it (= dpcm_merged_chan)
1592 for_each_dpcm_be(fe, stream, dpcm) {
1593 struct snd_soc_pcm_runtime *be = dpcm->be;
1594 struct snd_soc_pcm_stream *codec_stream;
1595 struct snd_soc_pcm_stream *cpu_stream;
1596 struct snd_soc_dai *dai;
1599 for_each_rtd_cpu_dais(be, i, dai) {
1601 * Skip CPUs which don't support the current stream
1602 * type. See soc_pcm_init_runtime_hw() for more details
1604 if (!snd_soc_dai_stream_valid(dai, stream))
1607 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1609 soc_pcm_hw_update_chan(hw, cpu_stream);
1613 * chan min/max cannot be enforced if there are multiple CODEC
1614 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1616 if (be->num_codecs == 1) {
1617 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1619 soc_pcm_hw_update_chan(hw, codec_stream);
1624 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1625 struct snd_pcm_runtime *runtime)
1627 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1628 struct snd_pcm_hardware *hw = &runtime->hw;
1629 struct snd_soc_dpcm *dpcm;
1630 int stream = substream->stream;
1632 if (!fe->dai_link->dpcm_merged_rate)
1636 * It returns merged BE codec channel;
1637 * if FE want to use it (= dpcm_merged_chan)
1640 for_each_dpcm_be(fe, stream, dpcm) {
1641 struct snd_soc_pcm_runtime *be = dpcm->be;
1642 struct snd_soc_pcm_stream *pcm;
1643 struct snd_soc_dai *dai;
1646 for_each_rtd_dais(be, i, dai) {
1648 * Skip DAIs which don't support the current stream
1649 * type. See soc_pcm_init_runtime_hw() for more details
1651 if (!snd_soc_dai_stream_valid(dai, stream))
1654 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1656 soc_pcm_hw_update_rate(hw, pcm);
1661 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1663 struct snd_pcm_runtime *runtime = substream->runtime;
1664 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1665 struct snd_soc_dai *cpu_dai;
1668 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1670 * Skip CPUs which don't support the current stream
1671 * type. See soc_pcm_init_runtime_hw() for more details
1673 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1676 dpcm_init_runtime_hw(runtime,
1677 snd_soc_dai_get_pcm_stream(cpu_dai,
1678 substream->stream));
1681 dpcm_runtime_merge_format(substream, runtime);
1682 dpcm_runtime_merge_chan(substream, runtime);
1683 dpcm_runtime_merge_rate(substream, runtime);
1686 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1689 struct snd_soc_dpcm *dpcm;
1690 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1691 struct snd_soc_dai *fe_cpu_dai;
1695 /* apply symmetry for FE */
1696 if (soc_pcm_has_symmetry(fe_substream))
1697 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1699 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1700 /* Symmetry only applies if we've got an active stream. */
1701 if (snd_soc_dai_active(fe_cpu_dai)) {
1702 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1708 /* apply symmetry for BE */
1709 for_each_dpcm_be(fe, stream, dpcm) {
1710 struct snd_soc_pcm_runtime *be = dpcm->be;
1711 struct snd_pcm_substream *be_substream =
1712 snd_soc_dpcm_get_substream(be, stream);
1713 struct snd_soc_pcm_runtime *rtd;
1714 struct snd_soc_dai *dai;
1717 /* A backend may not have the requested substream */
1721 rtd = asoc_substream_to_rtd(be_substream);
1722 if (rtd->dai_link->be_hw_params_fixup)
1725 if (soc_pcm_has_symmetry(be_substream))
1726 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1728 /* Symmetry only applies if we've got an active stream. */
1729 for_each_rtd_dais(rtd, i, dai) {
1730 if (snd_soc_dai_active(dai)) {
1731 err = soc_pcm_apply_symmetry(fe_substream, dai);
1741 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1743 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1744 int stream = fe_substream->stream, ret = 0;
1746 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1748 ret = dpcm_be_dai_startup(fe, stream);
1750 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1754 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1756 /* start the DAI frontend */
1757 ret = soc_pcm_open(fe_substream);
1759 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1763 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1765 dpcm_set_fe_runtime(fe_substream);
1767 ret = dpcm_apply_symmetry(fe_substream, stream);
1769 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1774 dpcm_be_dai_startup_unwind(fe, stream);
1776 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1780 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1782 struct snd_soc_dpcm *dpcm;
1784 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1785 for_each_dpcm_be(fe, stream, dpcm) {
1787 struct snd_soc_pcm_runtime *be = dpcm->be;
1788 struct snd_pcm_substream *be_substream =
1789 snd_soc_dpcm_get_substream(be, stream);
1791 /* is this op for this BE ? */
1792 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1795 if (be->dpcm[stream].users == 0)
1796 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1797 stream ? "capture" : "playback",
1798 be->dpcm[stream].state);
1800 if (--be->dpcm[stream].users != 0)
1803 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1804 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1805 soc_pcm_hw_free(be_substream);
1806 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1809 dev_dbg(be->dev, "ASoC: close BE %s\n",
1810 be->dai_link->name);
1812 soc_pcm_close(be_substream);
1813 be_substream->runtime = NULL;
1815 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1820 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1822 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1823 int stream = substream->stream;
1825 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1827 /* shutdown the BEs */
1828 dpcm_be_dai_shutdown(fe, stream);
1830 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1832 /* now shutdown the frontend */
1833 soc_pcm_close(substream);
1835 /* run the stream stop event */
1836 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1838 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1839 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1843 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1845 struct snd_soc_dpcm *dpcm;
1847 /* only hw_params backends that are either sinks or sources
1848 * to this frontend DAI */
1849 for_each_dpcm_be(fe, stream, dpcm) {
1851 struct snd_soc_pcm_runtime *be = dpcm->be;
1852 struct snd_pcm_substream *be_substream =
1853 snd_soc_dpcm_get_substream(be, stream);
1855 /* is this op for this BE ? */
1856 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1859 /* only free hw when no longer used - check all FEs */
1860 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1863 /* do not free hw if this BE is used by other FE */
1864 if (be->dpcm[stream].users > 1)
1867 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1868 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1869 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1870 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1871 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1872 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1875 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1876 be->dai_link->name);
1878 soc_pcm_hw_free(be_substream);
1880 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1886 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1888 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1889 int err, stream = substream->stream;
1891 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1892 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1894 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1896 /* call hw_free on the frontend */
1897 err = soc_pcm_hw_free(substream);
1899 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1900 fe->dai_link->name);
1902 /* only hw_params backends that are either sinks or sources
1903 * to this frontend DAI */
1904 err = dpcm_be_dai_hw_free(fe, stream);
1906 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1907 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1909 mutex_unlock(&fe->card->mutex);
1913 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1915 struct snd_soc_dpcm *dpcm;
1918 for_each_dpcm_be(fe, stream, dpcm) {
1920 struct snd_soc_pcm_runtime *be = dpcm->be;
1921 struct snd_pcm_substream *be_substream =
1922 snd_soc_dpcm_get_substream(be, stream);
1924 /* is this op for this BE ? */
1925 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1928 /* copy params for each dpcm */
1929 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1930 sizeof(struct snd_pcm_hw_params));
1932 /* perform any hw_params fixups */
1933 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1937 /* copy the fixed-up hw params for BE dai */
1938 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1939 sizeof(struct snd_pcm_hw_params));
1941 /* only allow hw_params() if no connected FEs are running */
1942 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1945 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1946 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1947 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1950 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1951 be->dai_link->name);
1953 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1955 dev_err(dpcm->be->dev,
1956 "ASoC: hw_params BE failed %d\n", ret);
1960 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1965 /* disable any enabled and non active backends */
1966 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1967 struct snd_soc_pcm_runtime *be = dpcm->be;
1968 struct snd_pcm_substream *be_substream =
1969 snd_soc_dpcm_get_substream(be, stream);
1971 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1974 /* only allow hw_free() if no connected FEs are running */
1975 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1978 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1979 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1980 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1981 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1984 soc_pcm_hw_free(be_substream);
1990 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1991 struct snd_pcm_hw_params *params)
1993 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1994 int ret, stream = substream->stream;
1996 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1997 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1999 memcpy(&fe->dpcm[stream].hw_params, params,
2000 sizeof(struct snd_pcm_hw_params));
2001 ret = dpcm_be_dai_hw_params(fe, stream);
2003 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2007 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2008 fe->dai_link->name, params_rate(params),
2009 params_channels(params), params_format(params));
2011 /* call hw_params on the frontend */
2012 ret = soc_pcm_hw_params(substream, params);
2014 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2015 dpcm_be_dai_hw_free(fe, stream);
2017 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2020 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2021 mutex_unlock(&fe->card->mutex);
2025 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2028 struct snd_soc_dpcm *dpcm;
2031 for_each_dpcm_be(fe, stream, dpcm) {
2033 struct snd_soc_pcm_runtime *be = dpcm->be;
2034 struct snd_pcm_substream *be_substream =
2035 snd_soc_dpcm_get_substream(be, stream);
2037 /* is this op for this BE ? */
2038 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2041 dev_dbg(be->dev, "ASoC: trigger BE %s cmd %d\n",
2042 be->dai_link->name, cmd);
2045 case SNDRV_PCM_TRIGGER_START:
2046 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2047 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2048 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2051 ret = soc_pcm_trigger(be_substream, cmd);
2055 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2057 case SNDRV_PCM_TRIGGER_RESUME:
2058 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2061 ret = soc_pcm_trigger(be_substream, cmd);
2065 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2067 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2068 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2071 ret = soc_pcm_trigger(be_substream, cmd);
2075 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2077 case SNDRV_PCM_TRIGGER_STOP:
2078 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2079 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2082 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2085 ret = soc_pcm_trigger(be_substream, cmd);
2089 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2091 case SNDRV_PCM_TRIGGER_SUSPEND:
2092 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2095 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2098 ret = soc_pcm_trigger(be_substream, cmd);
2102 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2104 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2105 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2108 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2111 ret = soc_pcm_trigger(be_substream, cmd);
2115 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2122 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2124 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2125 int cmd, bool fe_first)
2127 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2130 /* call trigger on the frontend before the backend. */
2132 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2133 fe->dai_link->name, cmd);
2135 ret = soc_pcm_trigger(substream, cmd);
2139 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2143 /* call trigger on the frontend after the backend. */
2144 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2148 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2149 fe->dai_link->name, cmd);
2151 ret = soc_pcm_trigger(substream, cmd);
2156 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2158 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2159 int stream = substream->stream;
2161 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2163 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2166 case SND_SOC_DPCM_TRIGGER_PRE:
2168 case SNDRV_PCM_TRIGGER_START:
2169 case SNDRV_PCM_TRIGGER_RESUME:
2170 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2171 case SNDRV_PCM_TRIGGER_DRAIN:
2172 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2174 case SNDRV_PCM_TRIGGER_STOP:
2175 case SNDRV_PCM_TRIGGER_SUSPEND:
2176 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2177 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2184 case SND_SOC_DPCM_TRIGGER_POST:
2186 case SNDRV_PCM_TRIGGER_START:
2187 case SNDRV_PCM_TRIGGER_RESUME:
2188 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2189 case SNDRV_PCM_TRIGGER_DRAIN:
2190 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2192 case SNDRV_PCM_TRIGGER_STOP:
2193 case SNDRV_PCM_TRIGGER_SUSPEND:
2194 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2195 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2202 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2203 /* bespoke trigger() - handles both FE and BEs */
2205 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2206 fe->dai_link->name, cmd);
2208 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2211 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2212 fe->dai_link->name);
2218 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2224 case SNDRV_PCM_TRIGGER_START:
2225 case SNDRV_PCM_TRIGGER_RESUME:
2226 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2227 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2229 case SNDRV_PCM_TRIGGER_STOP:
2230 case SNDRV_PCM_TRIGGER_SUSPEND:
2231 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2233 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2234 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2239 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2243 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2245 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2246 int stream = substream->stream;
2248 /* if FE's runtime_update is already set, we're in race;
2249 * process this trigger later at exit
2251 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2252 fe->dpcm[stream].trigger_pending = cmd + 1;
2253 return 0; /* delayed, assuming it's successful */
2256 /* we're alone, let's trigger */
2257 return dpcm_fe_dai_do_trigger(substream, cmd);
2260 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2262 struct snd_soc_dpcm *dpcm;
2265 for_each_dpcm_be(fe, stream, dpcm) {
2267 struct snd_soc_pcm_runtime *be = dpcm->be;
2268 struct snd_pcm_substream *be_substream =
2269 snd_soc_dpcm_get_substream(be, stream);
2271 /* is this op for this BE ? */
2272 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2275 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2276 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2277 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2278 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2281 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2282 be->dai_link->name);
2284 ret = soc_pcm_prepare(be_substream);
2286 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2291 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2296 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2298 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2299 int stream = substream->stream, ret = 0;
2301 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2303 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2305 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2307 /* there is no point preparing this FE if there are no BEs */
2308 if (list_empty(&fe->dpcm[stream].be_clients)) {
2309 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2310 fe->dai_link->name);
2315 ret = dpcm_be_dai_prepare(fe, stream);
2319 /* call prepare on the frontend */
2320 ret = soc_pcm_prepare(substream);
2322 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2323 fe->dai_link->name);
2327 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2330 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2331 mutex_unlock(&fe->card->mutex);
2336 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2338 struct snd_pcm_substream *substream =
2339 snd_soc_dpcm_get_substream(fe, stream);
2340 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2343 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2344 stream ? "capture" : "playback", fe->dai_link->name);
2346 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2347 /* call bespoke trigger - FE takes care of all BE triggers */
2348 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2349 fe->dai_link->name);
2351 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2353 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2355 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2356 fe->dai_link->name);
2358 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2360 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2363 err = dpcm_be_dai_hw_free(fe, stream);
2365 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2367 err = dpcm_be_dai_shutdown(fe, stream);
2369 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2371 /* run the stream event for each BE */
2372 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2377 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2379 struct snd_pcm_substream *substream =
2380 snd_soc_dpcm_get_substream(fe, stream);
2381 struct snd_soc_dpcm *dpcm;
2382 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2384 unsigned long flags;
2386 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2387 stream ? "capture" : "playback", fe->dai_link->name);
2389 /* Only start the BE if the FE is ready */
2390 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2391 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE) {
2393 dev_err(fe->dev, "ASoC: FE %s is not ready %d\n",
2394 fe->dai_link->name, fe->dpcm[stream].state);
2399 /* startup must always be called for new BEs */
2400 ret = dpcm_be_dai_startup(fe, stream);
2404 /* keep going if FE state is > open */
2405 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2408 ret = dpcm_be_dai_hw_params(fe, stream);
2412 /* keep going if FE state is > hw_params */
2413 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2417 ret = dpcm_be_dai_prepare(fe, stream);
2421 /* run the stream event for each BE */
2422 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2424 /* keep going if FE state is > prepare */
2425 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2426 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2429 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2430 /* call trigger on the frontend - FE takes care of all BE triggers */
2431 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2432 fe->dai_link->name);
2434 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2436 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2440 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2441 fe->dai_link->name);
2443 ret = dpcm_be_dai_trigger(fe, stream,
2444 SNDRV_PCM_TRIGGER_START);
2446 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2454 dpcm_be_dai_hw_free(fe, stream);
2456 dpcm_be_dai_shutdown(fe, stream);
2458 /* disconnect any pending BEs */
2459 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2460 for_each_dpcm_be(fe, stream, dpcm) {
2461 struct snd_soc_pcm_runtime *be = dpcm->be;
2463 /* is this op for this BE ? */
2464 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2467 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE ||
2468 be->dpcm[stream].state == SND_SOC_DPCM_STATE_NEW)
2469 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2471 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2476 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2478 struct snd_soc_dapm_widget_list *list;
2483 if (!fe->dai_link->dynamic)
2486 if (fe->num_cpus > 1) {
2488 "%s doesn't support Multi CPU yet\n", __func__);
2492 /* only check active links */
2493 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2496 /* DAPM sync will call this to update DSP paths */
2497 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2498 new ? "new" : "old", fe->dai_link->name);
2500 for_each_pcm_streams(stream) {
2502 /* skip if FE doesn't have playback/capture capability */
2503 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2504 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2507 /* skip if FE isn't currently playing/capturing */
2508 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2509 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2512 paths = dpcm_path_get(fe, stream, &list);
2514 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2516 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2517 "playback" : "capture");
2521 /* update any playback/capture paths */
2522 count = dpcm_process_paths(fe, stream, &list, new);
2524 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2526 ret = dpcm_run_update_startup(fe, stream);
2528 ret = dpcm_run_update_shutdown(fe, stream);
2530 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2531 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2533 dpcm_clear_pending_state(fe, stream);
2534 dpcm_be_disconnect(fe, stream);
2537 dpcm_path_put(&list);
2543 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2546 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2548 struct snd_soc_pcm_runtime *fe;
2551 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2552 /* shutdown all old paths first */
2553 for_each_card_rtds(card, fe) {
2554 ret = soc_dpcm_fe_runtime_update(fe, 0);
2559 /* bring new paths up */
2560 for_each_card_rtds(card, fe) {
2561 ret = soc_dpcm_fe_runtime_update(fe, 1);
2567 mutex_unlock(&card->mutex);
2570 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2572 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2574 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2575 struct snd_soc_dpcm *dpcm;
2576 int stream = fe_substream->stream;
2578 /* mark FE's links ready to prune */
2579 for_each_dpcm_be(fe, stream, dpcm)
2580 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2582 dpcm_be_disconnect(fe, stream);
2584 fe->dpcm[stream].runtime = NULL;
2587 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2589 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2592 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2593 ret = dpcm_fe_dai_shutdown(fe_substream);
2595 dpcm_fe_dai_cleanup(fe_substream);
2597 mutex_unlock(&fe->card->mutex);
2601 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2603 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2604 struct snd_soc_dapm_widget_list *list;
2606 int stream = fe_substream->stream;
2608 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2609 fe->dpcm[stream].runtime = fe_substream->runtime;
2611 ret = dpcm_path_get(fe, stream, &list);
2614 } else if (ret == 0) {
2615 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2616 fe->dai_link->name, stream ? "capture" : "playback");
2619 /* calculate valid and active FE <-> BE dpcms */
2620 dpcm_process_paths(fe, stream, &list, 1);
2622 ret = dpcm_fe_dai_startup(fe_substream);
2624 dpcm_fe_dai_cleanup(fe_substream);
2626 dpcm_clear_pending_state(fe, stream);
2627 dpcm_path_put(&list);
2629 mutex_unlock(&fe->card->mutex);
2633 static int soc_get_playback_capture(struct snd_soc_pcm_runtime *rtd,
2634 int *playback, int *capture)
2636 struct snd_soc_dai *codec_dai;
2637 struct snd_soc_dai *cpu_dai;
2641 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2643 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2647 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2648 if (rtd->dai_link->dpcm_playback) {
2649 stream = SNDRV_PCM_STREAM_PLAYBACK;
2651 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2652 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2658 dev_err(rtd->card->dev,
2659 "No CPU DAIs support playback for stream %s\n",
2660 rtd->dai_link->stream_name);
2664 if (rtd->dai_link->dpcm_capture) {
2665 stream = SNDRV_PCM_STREAM_CAPTURE;
2667 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2668 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2675 dev_err(rtd->card->dev,
2676 "No CPU DAIs support capture for stream %s\n",
2677 rtd->dai_link->stream_name);
2682 /* Adapt stream for codec2codec links */
2683 int cpu_capture = rtd->dai_link->params ?
2684 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2685 int cpu_playback = rtd->dai_link->params ?
2686 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2688 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2689 if (rtd->num_cpus == 1) {
2690 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2691 } else if (rtd->num_cpus == rtd->num_codecs) {
2692 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2694 dev_err(rtd->card->dev,
2695 "N cpus to M codecs link is not supported yet\n");
2699 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2700 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2702 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2703 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2708 if (rtd->dai_link->playback_only) {
2713 if (rtd->dai_link->capture_only) {
2721 static int soc_create_pcm(struct snd_pcm **pcm,
2722 struct snd_soc_pcm_runtime *rtd,
2723 int playback, int capture, int num)
2728 /* create the PCM */
2729 if (rtd->dai_link->params) {
2730 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2731 rtd->dai_link->stream_name);
2733 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2734 playback, capture, pcm);
2735 } else if (rtd->dai_link->no_pcm) {
2736 snprintf(new_name, sizeof(new_name), "(%s)",
2737 rtd->dai_link->stream_name);
2739 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2740 playback, capture, pcm);
2742 if (rtd->dai_link->dynamic)
2743 snprintf(new_name, sizeof(new_name), "%s (*)",
2744 rtd->dai_link->stream_name);
2746 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2747 rtd->dai_link->stream_name,
2748 (rtd->num_codecs > 1) ?
2749 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
2751 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2755 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2756 new_name, rtd->dai_link->name, ret);
2759 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2764 /* create a new pcm */
2765 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2767 struct snd_soc_component *component;
2768 struct snd_pcm *pcm;
2769 int ret = 0, playback = 0, capture = 0;
2772 ret = soc_get_playback_capture(rtd, &playback, &capture);
2776 ret = soc_create_pcm(&pcm, rtd, playback, capture, num);
2780 /* DAPM dai link stream work */
2781 if (rtd->dai_link->params)
2782 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2784 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2787 pcm->nonatomic = rtd->dai_link->nonatomic;
2788 pcm->private_data = rtd;
2790 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2792 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2794 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2798 /* ASoC PCM operations */
2799 if (rtd->dai_link->dynamic) {
2800 rtd->ops.open = dpcm_fe_dai_open;
2801 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2802 rtd->ops.prepare = dpcm_fe_dai_prepare;
2803 rtd->ops.trigger = dpcm_fe_dai_trigger;
2804 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2805 rtd->ops.close = dpcm_fe_dai_close;
2806 rtd->ops.pointer = soc_pcm_pointer;
2808 rtd->ops.open = soc_pcm_open;
2809 rtd->ops.hw_params = soc_pcm_hw_params;
2810 rtd->ops.prepare = soc_pcm_prepare;
2811 rtd->ops.trigger = soc_pcm_trigger;
2812 rtd->ops.hw_free = soc_pcm_hw_free;
2813 rtd->ops.close = soc_pcm_close;
2814 rtd->ops.pointer = soc_pcm_pointer;
2817 for_each_rtd_components(rtd, i, component) {
2818 const struct snd_soc_component_driver *drv = component->driver;
2821 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2823 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2825 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2827 rtd->ops.page = snd_soc_pcm_component_page;
2829 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2833 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2836 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2838 ret = snd_soc_pcm_component_new(rtd);
2840 dev_err(rtd->dev, "ASoC: pcm constructor failed for dailink %s: %d\n",
2841 rtd->dai_link->name, ret);
2845 pcm->no_device_suspend = true;
2847 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2848 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
2849 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
2853 /* is the current PCM operation for this FE ? */
2854 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2856 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2860 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2862 /* is the current PCM operation for this BE ? */
2863 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2864 struct snd_soc_pcm_runtime *be, int stream)
2866 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2867 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2868 be->dpcm[stream].runtime_update))
2872 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2874 /* get the substream for this BE */
2875 struct snd_pcm_substream *
2876 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2878 return be->pcm->streams[stream].substream;
2880 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2882 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2883 struct snd_soc_pcm_runtime *be,
2885 const enum snd_soc_dpcm_state *states,
2888 struct snd_soc_dpcm *dpcm;
2891 unsigned long flags;
2894 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2895 for_each_dpcm_fe(be, stream, dpcm) {
2900 state = dpcm->fe->dpcm[stream].state;
2901 for (i = 0; i < num_states; i++) {
2902 if (state == states[i]) {
2908 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2910 /* it's safe to do this BE DAI */
2915 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2916 * are not running, paused or suspended for the specified stream direction.
2918 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2919 struct snd_soc_pcm_runtime *be, int stream)
2921 const enum snd_soc_dpcm_state state[] = {
2922 SND_SOC_DPCM_STATE_START,
2923 SND_SOC_DPCM_STATE_PAUSED,
2924 SND_SOC_DPCM_STATE_SUSPEND,
2927 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2929 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2932 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2933 * running, paused or suspended for the specified stream direction.
2935 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2936 struct snd_soc_pcm_runtime *be, int stream)
2938 const enum snd_soc_dpcm_state state[] = {
2939 SND_SOC_DPCM_STATE_START,
2940 SND_SOC_DPCM_STATE_PAUSED,
2941 SND_SOC_DPCM_STATE_SUSPEND,
2942 SND_SOC_DPCM_STATE_PREPARE,
2945 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2947 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);