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/initval.h>
29 #define DPCM_MAX_BE_USERS 8
31 #ifdef CONFIG_DEBUG_FS
32 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
35 case SND_SOC_DPCM_STATE_NEW:
37 case SND_SOC_DPCM_STATE_OPEN:
39 case SND_SOC_DPCM_STATE_HW_PARAMS:
41 case SND_SOC_DPCM_STATE_PREPARE:
43 case SND_SOC_DPCM_STATE_START:
45 case SND_SOC_DPCM_STATE_STOP:
47 case SND_SOC_DPCM_STATE_SUSPEND:
49 case SND_SOC_DPCM_STATE_PAUSED:
51 case SND_SOC_DPCM_STATE_HW_FREE:
53 case SND_SOC_DPCM_STATE_CLOSE:
60 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
61 int stream, char *buf, size_t size)
63 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
64 struct snd_soc_dpcm *dpcm;
69 offset += snprintf(buf + offset, size - offset,
70 "[%s - %s]\n", fe->dai_link->name,
71 stream ? "Capture" : "Playback");
73 offset += snprintf(buf + offset, size - offset, "State: %s\n",
74 dpcm_state_string(fe->dpcm[stream].state));
76 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
77 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
78 offset += snprintf(buf + offset, size - offset,
80 "Format = %s, Channels = %d, Rate = %d\n",
81 snd_pcm_format_name(params_format(params)),
82 params_channels(params),
86 offset += snprintf(buf + offset, size - offset, "Backends:\n");
88 if (list_empty(&fe->dpcm[stream].be_clients)) {
89 offset += snprintf(buf + offset, size - offset,
90 " No active DSP links\n");
94 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
95 for_each_dpcm_be(fe, stream, dpcm) {
96 struct snd_soc_pcm_runtime *be = dpcm->be;
97 params = &dpcm->hw_params;
99 offset += snprintf(buf + offset, size - offset,
100 "- %s\n", be->dai_link->name);
102 offset += snprintf(buf + offset, size - offset,
104 dpcm_state_string(be->dpcm[stream].state));
106 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
107 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
108 offset += snprintf(buf + offset, size - offset,
110 "Format = %s, Channels = %d, Rate = %d\n",
111 snd_pcm_format_name(params_format(params)),
112 params_channels(params),
113 params_rate(params));
115 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
120 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
121 size_t count, loff_t *ppos)
123 struct snd_soc_pcm_runtime *fe = file->private_data;
124 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
128 buf = kmalloc(out_count, GFP_KERNEL);
132 for_each_pcm_streams(stream)
133 if (snd_soc_dai_stream_valid(fe->cpu_dai, stream))
134 offset += dpcm_show_state(fe, stream,
138 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
144 static const struct file_operations dpcm_state_fops = {
146 .read = dpcm_state_read_file,
147 .llseek = default_llseek,
150 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
155 if (!rtd->dai_link->dynamic)
158 if (!rtd->card->debugfs_card_root)
161 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
162 rtd->card->debugfs_card_root);
164 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
165 rtd, &dpcm_state_fops);
168 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
172 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
173 stream ? "capture" : "playback");
175 dpcm->debugfs_state = debugfs_create_dir(
176 name, dpcm->fe->debugfs_dpcm_root);
177 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
183 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
185 debugfs_remove_recursive(dpcm->debugfs_state);
189 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
194 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
199 static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
200 struct snd_pcm_substream *substream)
202 if (rtd->dai_link->ops &&
203 rtd->dai_link->ops->startup)
204 return rtd->dai_link->ops->startup(substream);
208 static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
209 struct snd_pcm_substream *substream)
211 if (rtd->dai_link->ops &&
212 rtd->dai_link->ops->shutdown)
213 rtd->dai_link->ops->shutdown(substream);
216 static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
217 struct snd_pcm_substream *substream)
219 if (rtd->dai_link->ops &&
220 rtd->dai_link->ops->prepare)
221 return rtd->dai_link->ops->prepare(substream);
225 static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
226 struct snd_pcm_substream *substream,
227 struct snd_pcm_hw_params *params)
229 if (rtd->dai_link->ops &&
230 rtd->dai_link->ops->hw_params)
231 return rtd->dai_link->ops->hw_params(substream, params);
235 static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
236 struct snd_pcm_substream *substream)
238 if (rtd->dai_link->ops &&
239 rtd->dai_link->ops->hw_free)
240 rtd->dai_link->ops->hw_free(substream);
243 static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
244 struct snd_pcm_substream *substream,
247 if (rtd->dai_link->ops &&
248 rtd->dai_link->ops->trigger)
249 return rtd->dai_link->ops->trigger(substream, cmd);
253 static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
254 int stream, int action)
256 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
257 struct snd_soc_dai *codec_dai;
260 lockdep_assert_held(&rtd->card->pcm_mutex);
262 cpu_dai->stream_active[stream] += action;
263 for_each_rtd_codec_dai(rtd, i, codec_dai)
264 codec_dai->stream_active[stream] += action;
266 cpu_dai->active += action;
267 cpu_dai->component->active += action;
268 for_each_rtd_codec_dai(rtd, i, codec_dai) {
269 codec_dai->active += action;
270 codec_dai->component->active += action;
275 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
276 * @rtd: ASoC PCM runtime that is activated
277 * @stream: Direction of the PCM stream
279 * Increments the active count for all the DAIs and components attached to a PCM
280 * runtime. Should typically be called when a stream is opened.
282 * Must be called with the rtd->card->pcm_mutex being held
284 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
286 snd_soc_runtime_action(rtd, stream, 1);
290 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
291 * @rtd: ASoC PCM runtime that is deactivated
292 * @stream: Direction of the PCM stream
294 * Decrements the active count for all the DAIs and components attached to a PCM
295 * runtime. Should typically be called when a stream is closed.
297 * Must be called with the rtd->card->pcm_mutex being held
299 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
301 snd_soc_runtime_action(rtd, stream, -1);
305 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
306 * @rtd: The ASoC PCM runtime that should be checked.
308 * This function checks whether the power down delay should be ignored for a
309 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
310 * been configured to ignore the delay, or if none of the components benefits
311 * from having the delay.
313 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
315 struct snd_soc_component *component;
319 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
322 for_each_rtd_components(rtd, i, component)
323 ignore &= !component->driver->use_pmdown_time;
329 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
330 * @substream: the pcm substream
331 * @hw: the hardware parameters
333 * Sets the substream runtime hardware parameters.
335 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
336 const struct snd_pcm_hardware *hw)
338 struct snd_pcm_runtime *runtime = substream->runtime;
339 runtime->hw.info = hw->info;
340 runtime->hw.formats = hw->formats;
341 runtime->hw.period_bytes_min = hw->period_bytes_min;
342 runtime->hw.period_bytes_max = hw->period_bytes_max;
343 runtime->hw.periods_min = hw->periods_min;
344 runtime->hw.periods_max = hw->periods_max;
345 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
346 runtime->hw.fifo_size = hw->fifo_size;
349 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
351 /* DPCM stream event, send event to FE and all active BEs. */
352 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
355 struct snd_soc_dpcm *dpcm;
357 for_each_dpcm_be(fe, dir, dpcm) {
359 struct snd_soc_pcm_runtime *be = dpcm->be;
361 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
362 be->dai_link->name, event, dir);
364 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
365 (be->dpcm[dir].users >= 1))
368 snd_soc_dapm_stream_event(be, dir, event);
371 snd_soc_dapm_stream_event(fe, dir, event);
376 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
377 struct snd_soc_dai *soc_dai)
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
382 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
383 rtd->dai_link->symmetric_rates)) {
384 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
387 ret = snd_pcm_hw_constraint_single(substream->runtime,
388 SNDRV_PCM_HW_PARAM_RATE,
391 dev_err(soc_dai->dev,
392 "ASoC: Unable to apply rate constraint: %d\n",
398 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
399 rtd->dai_link->symmetric_channels)) {
400 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
403 ret = snd_pcm_hw_constraint_single(substream->runtime,
404 SNDRV_PCM_HW_PARAM_CHANNELS,
407 dev_err(soc_dai->dev,
408 "ASoC: Unable to apply channel symmetry constraint: %d\n",
414 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
415 rtd->dai_link->symmetric_samplebits)) {
416 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
417 soc_dai->sample_bits);
419 ret = snd_pcm_hw_constraint_single(substream->runtime,
420 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
421 soc_dai->sample_bits);
423 dev_err(soc_dai->dev,
424 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
433 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
434 struct snd_pcm_hw_params *params)
436 struct snd_soc_pcm_runtime *rtd = substream->private_data;
437 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
438 struct snd_soc_dai *codec_dai;
439 unsigned int rate, channels, sample_bits, symmetry, i;
441 rate = params_rate(params);
442 channels = params_channels(params);
443 sample_bits = snd_pcm_format_physical_width(params_format(params));
445 /* reject unmatched parameters when applying symmetry */
446 symmetry = cpu_dai->driver->symmetric_rates ||
447 rtd->dai_link->symmetric_rates;
449 for_each_rtd_codec_dai(rtd, i, codec_dai)
450 symmetry |= codec_dai->driver->symmetric_rates;
452 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
453 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
454 cpu_dai->rate, rate);
458 symmetry = cpu_dai->driver->symmetric_channels ||
459 rtd->dai_link->symmetric_channels;
461 for_each_rtd_codec_dai(rtd, i, codec_dai)
462 symmetry |= codec_dai->driver->symmetric_channels;
464 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
465 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
466 cpu_dai->channels, channels);
470 symmetry = cpu_dai->driver->symmetric_samplebits ||
471 rtd->dai_link->symmetric_samplebits;
473 for_each_rtd_codec_dai(rtd, i, codec_dai)
474 symmetry |= codec_dai->driver->symmetric_samplebits;
476 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
477 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
478 cpu_dai->sample_bits, sample_bits);
485 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
487 struct snd_soc_pcm_runtime *rtd = substream->private_data;
488 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
489 struct snd_soc_dai_link *link = rtd->dai_link;
490 struct snd_soc_dai *codec_dai;
491 unsigned int symmetry, i;
493 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
494 cpu_driver->symmetric_channels || link->symmetric_channels ||
495 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
497 for_each_rtd_codec_dai(rtd, i, codec_dai)
498 symmetry = symmetry ||
499 codec_dai->driver->symmetric_rates ||
500 codec_dai->driver->symmetric_channels ||
501 codec_dai->driver->symmetric_samplebits;
506 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
508 struct snd_soc_pcm_runtime *rtd = substream->private_data;
514 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
516 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
520 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
522 struct snd_soc_pcm_runtime *rtd = substream->private_data;
523 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
524 struct snd_soc_dai *codec_dai;
525 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
526 int stream = substream->stream;
528 unsigned int bits = 0, cpu_bits;
530 for_each_rtd_codec_dai(rtd, i, codec_dai) {
531 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
533 if (pcm_codec->sig_bits == 0) {
537 bits = max(pcm_codec->sig_bits, bits);
540 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
541 cpu_bits = pcm_cpu->sig_bits;
543 soc_pcm_set_msb(substream, bits);
544 soc_pcm_set_msb(substream, cpu_bits);
547 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
549 struct snd_pcm_runtime *runtime = substream->runtime;
550 struct snd_pcm_hardware *hw = &runtime->hw;
551 struct snd_soc_pcm_runtime *rtd = substream->private_data;
552 struct snd_soc_dai *codec_dai;
553 struct snd_soc_pcm_stream *codec_stream;
554 struct snd_soc_pcm_stream *cpu_stream;
555 unsigned int chan_min = 0, chan_max = UINT_MAX;
556 unsigned int rate_min = 0, rate_max = UINT_MAX;
557 unsigned int rates = UINT_MAX;
558 u64 formats = ULLONG_MAX;
559 int stream = substream->stream;
562 cpu_stream = snd_soc_dai_get_pcm_stream(rtd->cpu_dai, stream);
564 /* first calculate min/max only for CODECs in the DAI link */
565 for_each_rtd_codec_dai(rtd, i, codec_dai) {
568 * Skip CODECs which don't support the current stream type.
569 * Otherwise, since the rate, channel, and format values will
570 * zero in that case, we would have no usable settings left,
571 * causing the resulting setup to fail.
572 * At least one CODEC should match, otherwise we should have
573 * bailed out on a higher level, since there would be no
574 * CODEC to support the transfer direction in that case.
576 if (!snd_soc_dai_stream_valid(codec_dai,
580 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
582 chan_min = max(chan_min, codec_stream->channels_min);
583 chan_max = min(chan_max, codec_stream->channels_max);
584 rate_min = max(rate_min, codec_stream->rate_min);
585 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
586 formats &= codec_stream->formats;
587 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
591 * chan min/max cannot be enforced if there are multiple CODEC DAIs
592 * connected to a single CPU DAI, use CPU DAI's directly and let
593 * channel allocation be fixed up later
595 if (rtd->num_codecs > 1) {
596 chan_min = cpu_stream->channels_min;
597 chan_max = cpu_stream->channels_max;
600 hw->channels_min = max(chan_min, cpu_stream->channels_min);
601 hw->channels_max = min(chan_max, cpu_stream->channels_max);
603 hw->formats &= formats & cpu_stream->formats;
605 hw->formats = formats & cpu_stream->formats;
606 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
608 snd_pcm_limit_hw_rates(runtime);
610 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
611 hw->rate_min = max(hw->rate_min, rate_min);
612 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
613 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
616 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
618 struct snd_soc_pcm_runtime *rtd = substream->private_data;
619 struct snd_soc_component *last = NULL;
620 struct snd_soc_component *component;
623 for_each_rtd_components(rtd, i, component) {
626 ret = snd_soc_component_module_get_when_open(component);
628 dev_err(component->dev,
629 "ASoC: can't get module %s\n",
634 ret = snd_soc_component_open(component, substream);
636 snd_soc_component_module_put_when_close(component);
637 dev_err(component->dev,
638 "ASoC: can't open component %s: %d\n",
639 component->name, ret);
645 /* rollback on error */
646 for_each_rtd_components(rtd, i, component) {
647 if (component == last)
650 snd_soc_component_close(component, substream);
651 snd_soc_component_module_put_when_close(component);
658 static int soc_pcm_components_close(struct snd_pcm_substream *substream)
660 struct snd_soc_pcm_runtime *rtd = substream->private_data;
661 struct snd_soc_component *component;
664 for_each_rtd_components(rtd, i, component) {
665 r = snd_soc_component_close(component, substream);
667 ret = r; /* use last ret */
669 snd_soc_component_module_put_when_close(component);
676 * Called by ALSA when a PCM substream is closed. Private data can be
677 * freed here. The cpu DAI, codec DAI, machine and components are also
680 static int soc_pcm_close(struct snd_pcm_substream *substream)
682 struct snd_soc_pcm_runtime *rtd = substream->private_data;
683 struct snd_soc_component *component;
684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
685 struct snd_soc_dai *codec_dai;
688 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
690 snd_soc_runtime_deactivate(rtd, substream->stream);
692 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
694 snd_soc_dai_shutdown(cpu_dai, substream);
696 for_each_rtd_codec_dai(rtd, i, codec_dai)
697 snd_soc_dai_shutdown(codec_dai, substream);
699 soc_rtd_shutdown(rtd, substream);
701 soc_pcm_components_close(substream);
703 snd_soc_dapm_stream_stop(rtd, substream->stream);
705 mutex_unlock(&rtd->card->pcm_mutex);
707 for_each_rtd_components(rtd, i, component) {
708 pm_runtime_mark_last_busy(component->dev);
709 pm_runtime_put_autosuspend(component->dev);
712 for_each_rtd_components(rtd, i, component)
713 if (!component->active)
714 pinctrl_pm_select_sleep_state(component->dev);
720 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
721 * then initialized and any private data can be allocated. This also calls
722 * startup for the cpu DAI, component, machine and codec DAI.
724 static int soc_pcm_open(struct snd_pcm_substream *substream)
726 struct snd_soc_pcm_runtime *rtd = substream->private_data;
727 struct snd_pcm_runtime *runtime = substream->runtime;
728 struct snd_soc_component *component;
729 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
730 struct snd_soc_dai *codec_dai;
731 const char *codec_dai_name = "multicodec";
734 for_each_rtd_components(rtd, i, component)
735 pinctrl_pm_select_default_state(component->dev);
737 for_each_rtd_components(rtd, i, component)
738 pm_runtime_get_sync(component->dev);
740 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
742 ret = soc_pcm_components_open(substream);
746 ret = soc_rtd_startup(rtd, substream);
748 pr_err("ASoC: %s startup failed: %d\n",
749 rtd->dai_link->name, ret);
750 goto rtd_startup_err;
753 /* startup the audio subsystem */
754 ret = snd_soc_dai_startup(cpu_dai, substream);
756 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
761 for_each_rtd_codec_dai(rtd, i, codec_dai) {
762 ret = snd_soc_dai_startup(codec_dai, substream);
764 dev_err(codec_dai->dev,
765 "ASoC: can't open codec %s: %d\n",
766 codec_dai->name, ret);
770 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
771 codec_dai->tx_mask = 0;
773 codec_dai->rx_mask = 0;
776 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
777 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
780 /* Check that the codec and cpu DAIs are compatible */
781 soc_pcm_init_runtime_hw(substream);
783 if (rtd->num_codecs == 1)
784 codec_dai_name = rtd->codec_dai->name;
786 if (soc_pcm_has_symmetry(substream))
787 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
790 if (!runtime->hw.rates) {
791 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
792 codec_dai_name, cpu_dai->name);
795 if (!runtime->hw.formats) {
796 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
797 codec_dai_name, cpu_dai->name);
800 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
801 runtime->hw.channels_min > runtime->hw.channels_max) {
802 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
803 codec_dai_name, cpu_dai->name);
807 soc_pcm_apply_msb(substream);
809 /* Symmetry only applies if we've already got an active stream. */
810 if (cpu_dai->active) {
811 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
816 for_each_rtd_codec_dai(rtd, i, codec_dai) {
817 if (codec_dai->active) {
818 ret = soc_pcm_apply_symmetry(substream, codec_dai);
824 pr_debug("ASoC: %s <-> %s info:\n",
825 codec_dai_name, cpu_dai->name);
826 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
827 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
828 runtime->hw.channels_max);
829 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
830 runtime->hw.rate_max);
834 snd_soc_runtime_activate(rtd, substream->stream);
836 mutex_unlock(&rtd->card->pcm_mutex);
840 for_each_rtd_codec_dai(rtd, i, codec_dai)
841 snd_soc_dai_shutdown(codec_dai, substream);
843 snd_soc_dai_shutdown(cpu_dai, substream);
845 soc_rtd_shutdown(rtd, substream);
847 soc_pcm_components_close(substream);
849 mutex_unlock(&rtd->card->pcm_mutex);
851 for_each_rtd_components(rtd, i, component) {
852 pm_runtime_mark_last_busy(component->dev);
853 pm_runtime_put_autosuspend(component->dev);
856 for_each_rtd_components(rtd, i, component)
857 if (!component->active)
858 pinctrl_pm_select_sleep_state(component->dev);
863 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
866 * Currently nothing to do for c2c links
867 * Since c2c links are internal nodes in the DAPM graph and
868 * don't interface with the outside world or application layer
869 * we don't have to do any special handling on close.
874 * Called by ALSA when the PCM substream is prepared, can set format, sample
875 * rate, etc. This function is non atomic and can be called multiple times,
876 * it can refer to the runtime info.
878 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
880 struct snd_soc_pcm_runtime *rtd = substream->private_data;
881 struct snd_soc_component *component;
882 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
883 struct snd_soc_dai *codec_dai;
886 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
888 ret = soc_rtd_prepare(rtd, substream);
890 dev_err(rtd->card->dev,
891 "ASoC: machine prepare error: %d\n", ret);
895 for_each_rtd_components(rtd, i, component) {
896 ret = snd_soc_component_prepare(component, substream);
898 dev_err(component->dev,
899 "ASoC: platform prepare error: %d\n", ret);
904 for_each_rtd_codec_dai(rtd, i, codec_dai) {
905 ret = snd_soc_dai_prepare(codec_dai, substream);
907 dev_err(codec_dai->dev,
908 "ASoC: codec DAI prepare error: %d\n",
914 ret = snd_soc_dai_prepare(cpu_dai, substream);
916 dev_err(cpu_dai->dev,
917 "ASoC: cpu DAI prepare error: %d\n", ret);
921 /* cancel any delayed stream shutdown that is pending */
922 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
925 cancel_delayed_work(&rtd->delayed_work);
928 snd_soc_dapm_stream_event(rtd, substream->stream,
929 SND_SOC_DAPM_STREAM_START);
931 for_each_rtd_codec_dai(rtd, i, codec_dai)
932 snd_soc_dai_digital_mute(codec_dai, 0,
934 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
937 mutex_unlock(&rtd->card->pcm_mutex);
941 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
944 struct snd_interval *interval;
945 int channels = hweight_long(mask);
947 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
948 interval->min = channels;
949 interval->max = channels;
952 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
953 struct snd_soc_component *last)
955 struct snd_soc_pcm_runtime *rtd = substream->private_data;
956 struct snd_soc_component *component;
959 for_each_rtd_components(rtd, i, component) {
960 if (component == last)
963 r = snd_soc_component_hw_free(component, substream);
965 ret = r; /* use last ret */
972 * Called by ALSA when the hardware params are set by application. This
973 * function can also be called multiple times and can allocate buffers
974 * (using snd_pcm_lib_* ). It's non-atomic.
976 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
977 struct snd_pcm_hw_params *params)
979 struct snd_soc_pcm_runtime *rtd = substream->private_data;
980 struct snd_soc_component *component;
981 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
982 struct snd_soc_dai *codec_dai;
985 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
987 ret = soc_pcm_params_symmetry(substream, params);
991 ret = soc_rtd_hw_params(rtd, substream, params);
993 dev_err(rtd->card->dev,
994 "ASoC: machine hw_params failed: %d\n", ret);
998 for_each_rtd_codec_dai(rtd, i, codec_dai) {
999 struct snd_pcm_hw_params codec_params;
1002 * Skip CODECs which don't support the current stream type,
1003 * the idea being that if a CODEC is not used for the currently
1004 * set up transfer direction, it should not need to be
1005 * configured, especially since the configuration used might
1006 * not even be supported by that CODEC. There may be cases
1007 * however where a CODEC needs to be set up although it is
1008 * actually not being used for the transfer, e.g. if a
1009 * capture-only CODEC is acting as an LRCLK and/or BCLK master
1010 * for the DAI link including a playback-only CODEC.
1011 * If this becomes necessary, we will have to augment the
1012 * machine driver setup with information on how to act, so
1013 * we can do the right thing here.
1015 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1018 /* copy params for each codec */
1019 codec_params = *params;
1021 /* fixup params based on TDM slot masks */
1022 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1024 soc_pcm_codec_params_fixup(&codec_params,
1025 codec_dai->tx_mask);
1027 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
1029 soc_pcm_codec_params_fixup(&codec_params,
1030 codec_dai->rx_mask);
1032 ret = snd_soc_dai_hw_params(codec_dai, substream,
1037 codec_dai->rate = params_rate(&codec_params);
1038 codec_dai->channels = params_channels(&codec_params);
1039 codec_dai->sample_bits = snd_pcm_format_physical_width(
1040 params_format(&codec_params));
1042 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1045 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
1049 /* store the parameters for each DAIs */
1050 cpu_dai->rate = params_rate(params);
1051 cpu_dai->channels = params_channels(params);
1052 cpu_dai->sample_bits =
1053 snd_pcm_format_physical_width(params_format(params));
1055 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1057 for_each_rtd_components(rtd, i, component) {
1058 ret = snd_soc_component_hw_params(component, substream, params);
1060 dev_err(component->dev,
1061 "ASoC: %s hw params failed: %d\n",
1062 component->name, ret);
1069 mutex_unlock(&rtd->card->pcm_mutex);
1073 soc_pcm_components_hw_free(substream, component);
1075 snd_soc_dai_hw_free(cpu_dai, substream);
1079 i = rtd->num_codecs;
1082 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1083 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1086 snd_soc_dai_hw_free(codec_dai, substream);
1087 codec_dai->rate = 0;
1090 soc_rtd_hw_free(rtd, substream);
1092 mutex_unlock(&rtd->card->pcm_mutex);
1097 * Frees resources allocated by hw_params, can be called multiple times
1099 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1101 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1103 struct snd_soc_dai *codec_dai;
1104 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1107 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1109 /* clear the corresponding DAIs parameters when going to be inactive */
1110 if (cpu_dai->active == 1) {
1112 cpu_dai->channels = 0;
1113 cpu_dai->sample_bits = 0;
1116 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1117 if (codec_dai->active == 1) {
1118 codec_dai->rate = 0;
1119 codec_dai->channels = 0;
1120 codec_dai->sample_bits = 0;
1124 /* apply codec digital mute */
1125 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1126 int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK];
1127 int capture_active = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE];
1129 if ((playback && playback_active == 1) ||
1130 (!playback && capture_active == 1))
1131 snd_soc_dai_digital_mute(codec_dai, 1,
1135 /* free any machine hw params */
1136 soc_rtd_hw_free(rtd, substream);
1138 /* free any component resources */
1139 soc_pcm_components_hw_free(substream, NULL);
1141 /* now free hw params for the DAIs */
1142 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1143 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1146 snd_soc_dai_hw_free(codec_dai, substream);
1149 snd_soc_dai_hw_free(cpu_dai, substream);
1151 mutex_unlock(&rtd->card->pcm_mutex);
1155 static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
1157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1158 struct snd_soc_component *component;
1159 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1160 struct snd_soc_dai *codec_dai;
1163 ret = soc_rtd_trigger(rtd, substream, cmd);
1167 for_each_rtd_components(rtd, i, component) {
1168 ret = snd_soc_component_trigger(component, substream, cmd);
1173 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1177 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1178 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1186 static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1188 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1189 struct snd_soc_component *component;
1190 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1191 struct snd_soc_dai *codec_dai;
1194 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1195 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1200 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1204 for_each_rtd_components(rtd, i, component) {
1205 ret = snd_soc_component_trigger(component, substream, cmd);
1210 ret = soc_rtd_trigger(rtd, substream, cmd);
1217 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1222 case SNDRV_PCM_TRIGGER_START:
1223 case SNDRV_PCM_TRIGGER_RESUME:
1224 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1225 ret = soc_pcm_trigger_start(substream, cmd);
1227 case SNDRV_PCM_TRIGGER_STOP:
1228 case SNDRV_PCM_TRIGGER_SUSPEND:
1229 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1230 ret = soc_pcm_trigger_stop(substream, cmd);
1239 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1242 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1243 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1244 struct snd_soc_dai *codec_dai;
1247 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1248 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
1253 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1260 * soc level wrapper for pointer callback
1261 * If cpu_dai, codec_dai, component driver has the delay callback, then
1262 * the runtime->delay will be updated accordingly.
1264 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1266 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1267 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1268 struct snd_soc_dai *codec_dai;
1269 struct snd_pcm_runtime *runtime = substream->runtime;
1270 snd_pcm_uframes_t offset = 0;
1271 snd_pcm_sframes_t delay = 0;
1272 snd_pcm_sframes_t codec_delay = 0;
1275 /* clearing the previous total delay */
1278 offset = snd_soc_pcm_component_pointer(substream);
1280 /* base delay if assigned in pointer callback */
1281 delay = runtime->delay;
1283 delay += snd_soc_dai_delay(cpu_dai, substream);
1285 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1286 codec_delay = max(codec_delay,
1287 snd_soc_dai_delay(codec_dai, substream));
1289 delay += codec_delay;
1291 runtime->delay = delay;
1296 /* connect a FE and BE */
1297 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1298 struct snd_soc_pcm_runtime *be, int stream)
1300 struct snd_soc_dpcm *dpcm;
1301 unsigned long flags;
1303 /* only add new dpcms */
1304 for_each_dpcm_be(fe, stream, dpcm) {
1305 if (dpcm->be == be && dpcm->fe == fe)
1309 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1315 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1316 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1317 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1318 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1319 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1320 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1322 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1323 stream ? "capture" : "playback", fe->dai_link->name,
1324 stream ? "<-" : "->", be->dai_link->name);
1326 dpcm_create_debugfs_state(dpcm, stream);
1331 /* reparent a BE onto another FE */
1332 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1333 struct snd_soc_pcm_runtime *be, int stream)
1335 struct snd_soc_dpcm *dpcm;
1336 struct snd_pcm_substream *fe_substream, *be_substream;
1338 /* reparent if BE is connected to other FEs */
1339 if (!be->dpcm[stream].users)
1342 be_substream = snd_soc_dpcm_get_substream(be, stream);
1344 for_each_dpcm_fe(be, stream, dpcm) {
1348 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1349 stream ? "capture" : "playback",
1350 dpcm->fe->dai_link->name,
1351 stream ? "<-" : "->", dpcm->be->dai_link->name);
1353 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1354 be_substream->runtime = fe_substream->runtime;
1359 /* disconnect a BE and FE */
1360 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1362 struct snd_soc_dpcm *dpcm, *d;
1363 unsigned long flags;
1365 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1366 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1367 stream ? "capture" : "playback",
1368 dpcm->be->dai_link->name);
1370 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1373 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1374 stream ? "capture" : "playback", fe->dai_link->name,
1375 stream ? "<-" : "->", dpcm->be->dai_link->name);
1377 /* BEs still alive need new FE */
1378 dpcm_be_reparent(fe, dpcm->be, stream);
1380 dpcm_remove_debugfs_state(dpcm);
1382 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1383 list_del(&dpcm->list_be);
1384 list_del(&dpcm->list_fe);
1385 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1390 /* get BE for DAI widget and stream */
1391 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1392 struct snd_soc_dapm_widget *widget, int stream)
1394 struct snd_soc_pcm_runtime *be;
1395 struct snd_soc_dapm_widget *w;
1396 struct snd_soc_dai *dai;
1399 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1401 for_each_card_rtds(card, be) {
1403 if (!be->dai_link->no_pcm)
1406 w = snd_soc_dai_get_widget(be->cpu_dai, stream);
1408 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1409 w ? w->name : "(not set)");
1414 for_each_rtd_codec_dai(be, i, dai) {
1415 w = snd_soc_dai_get_widget(dai, stream);
1422 /* Widget provided is not a BE */
1426 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1427 struct snd_soc_dapm_widget *widget)
1429 struct snd_soc_dapm_widget *w;
1432 for_each_dapm_widgets(list, i, w)
1439 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1440 enum snd_soc_dapm_direction dir)
1442 struct snd_soc_card *card = widget->dapm->card;
1443 struct snd_soc_pcm_runtime *rtd;
1446 /* adjust dir to stream */
1447 if (dir == SND_SOC_DAPM_DIR_OUT)
1448 stream = SNDRV_PCM_STREAM_PLAYBACK;
1450 stream = SNDRV_PCM_STREAM_CAPTURE;
1452 rtd = dpcm_get_be(card, widget, stream);
1459 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1460 int stream, struct snd_soc_dapm_widget_list **list)
1462 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1465 /* get number of valid DAI paths and their widgets */
1466 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1467 dpcm_end_walk_at_be);
1469 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1470 stream ? "capture" : "playback");
1475 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1477 snd_soc_dapm_dai_free_widgets(list);
1480 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1481 struct snd_soc_dapm_widget_list **list_)
1483 struct snd_soc_dpcm *dpcm;
1484 struct snd_soc_dapm_widget_list *list = *list_;
1485 struct snd_soc_dapm_widget *widget;
1486 struct snd_soc_dai *dai;
1490 /* Destroy any old FE <--> BE connections */
1491 for_each_dpcm_be(fe, stream, dpcm) {
1494 /* is there a valid CPU DAI widget for this BE */
1495 widget = snd_soc_dai_get_widget(dpcm->be->cpu_dai, stream);
1497 /* prune the BE if it's no longer in our active list */
1498 if (widget && widget_in_list(list, widget))
1501 /* is there a valid CODEC DAI widget for this BE */
1503 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1504 widget = snd_soc_dai_get_widget(dai, stream);
1506 /* prune the BE if it's no longer in our active list */
1507 if (widget && widget_in_list(list, widget))
1513 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1514 stream ? "capture" : "playback",
1515 dpcm->be->dai_link->name, fe->dai_link->name);
1516 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1517 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1521 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1525 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1526 struct snd_soc_dapm_widget_list **list_)
1528 struct snd_soc_card *card = fe->card;
1529 struct snd_soc_dapm_widget_list *list = *list_;
1530 struct snd_soc_pcm_runtime *be;
1531 struct snd_soc_dapm_widget *widget;
1532 int i, new = 0, err;
1534 /* Create any new FE <--> BE connections */
1535 for_each_dapm_widgets(list, i, widget) {
1537 switch (widget->id) {
1538 case snd_soc_dapm_dai_in:
1539 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1542 case snd_soc_dapm_dai_out:
1543 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1550 /* is there a valid BE rtd for this widget */
1551 be = dpcm_get_be(card, widget, stream);
1553 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1558 /* make sure BE is a real BE */
1559 if (!be->dai_link->no_pcm)
1562 /* don't connect if FE is not running */
1563 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1566 /* newly connected FE and BE */
1567 err = dpcm_be_connect(fe, be, stream);
1569 dev_err(fe->dev, "ASoC: can't connect %s\n",
1572 } else if (err == 0) /* already connected */
1576 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1580 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1585 * Find the corresponding BE DAIs that source or sink audio to this
1588 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1589 int stream, struct snd_soc_dapm_widget_list **list, int new)
1592 return dpcm_add_paths(fe, stream, list);
1594 return dpcm_prune_paths(fe, stream, list);
1597 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1599 struct snd_soc_dpcm *dpcm;
1600 unsigned long flags;
1602 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1603 for_each_dpcm_be(fe, stream, dpcm)
1604 dpcm->be->dpcm[stream].runtime_update =
1605 SND_SOC_DPCM_UPDATE_NO;
1606 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1609 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1612 struct snd_soc_dpcm *dpcm;
1614 /* disable any enabled and non active backends */
1615 for_each_dpcm_be(fe, stream, dpcm) {
1617 struct snd_soc_pcm_runtime *be = dpcm->be;
1618 struct snd_pcm_substream *be_substream =
1619 snd_soc_dpcm_get_substream(be, stream);
1621 if (be->dpcm[stream].users == 0)
1622 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1623 stream ? "capture" : "playback",
1624 be->dpcm[stream].state);
1626 if (--be->dpcm[stream].users != 0)
1629 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1632 soc_pcm_close(be_substream);
1633 be_substream->runtime = NULL;
1634 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1638 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1640 struct snd_soc_dpcm *dpcm;
1643 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1644 for_each_dpcm_be(fe, stream, dpcm) {
1646 struct snd_soc_pcm_runtime *be = dpcm->be;
1647 struct snd_pcm_substream *be_substream =
1648 snd_soc_dpcm_get_substream(be, stream);
1650 if (!be_substream) {
1651 dev_err(be->dev, "ASoC: no backend %s stream\n",
1652 stream ? "capture" : "playback");
1656 /* is this op for this BE ? */
1657 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1660 /* first time the dpcm is open ? */
1661 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1662 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1663 stream ? "capture" : "playback",
1664 be->dpcm[stream].state);
1666 if (be->dpcm[stream].users++ != 0)
1669 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1670 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1673 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1674 stream ? "capture" : "playback", be->dai_link->name);
1676 be_substream->runtime = be->dpcm[stream].runtime;
1677 err = soc_pcm_open(be_substream);
1679 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1680 be->dpcm[stream].users--;
1681 if (be->dpcm[stream].users < 0)
1682 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1683 stream ? "capture" : "playback",
1684 be->dpcm[stream].state);
1686 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1690 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1697 /* disable any enabled and non active backends */
1698 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1699 struct snd_soc_pcm_runtime *be = dpcm->be;
1700 struct snd_pcm_substream *be_substream =
1701 snd_soc_dpcm_get_substream(be, stream);
1703 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1706 if (be->dpcm[stream].users == 0)
1707 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1708 stream ? "capture" : "playback",
1709 be->dpcm[stream].state);
1711 if (--be->dpcm[stream].users != 0)
1714 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1717 soc_pcm_close(be_substream);
1718 be_substream->runtime = NULL;
1719 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1725 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1726 struct snd_soc_pcm_stream *stream)
1728 runtime->hw.rate_min = stream->rate_min;
1729 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1730 runtime->hw.channels_min = stream->channels_min;
1731 runtime->hw.channels_max = stream->channels_max;
1732 if (runtime->hw.formats)
1733 runtime->hw.formats &= stream->formats;
1735 runtime->hw.formats = stream->formats;
1736 runtime->hw.rates = stream->rates;
1739 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1742 struct snd_soc_pcm_runtime *fe = substream->private_data;
1743 struct snd_soc_dpcm *dpcm;
1744 struct snd_soc_dai *dai;
1745 int stream = substream->stream;
1747 if (!fe->dai_link->dpcm_merged_format)
1751 * It returns merged BE codec format
1752 * if FE want to use it (= dpcm_merged_format)
1755 for_each_dpcm_be(fe, stream, dpcm) {
1756 struct snd_soc_pcm_runtime *be = dpcm->be;
1757 struct snd_soc_pcm_stream *codec_stream;
1760 for_each_rtd_codec_dai(be, i, dai) {
1762 * Skip CODECs which don't support the current stream
1763 * type. See soc_pcm_init_runtime_hw() for more details
1765 if (!snd_soc_dai_stream_valid(dai, stream))
1768 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1770 *formats &= codec_stream->formats;
1775 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1776 unsigned int *channels_min,
1777 unsigned int *channels_max)
1779 struct snd_soc_pcm_runtime *fe = substream->private_data;
1780 struct snd_soc_dpcm *dpcm;
1781 int stream = substream->stream;
1783 if (!fe->dai_link->dpcm_merged_chan)
1787 * It returns merged BE codec channel;
1788 * if FE want to use it (= dpcm_merged_chan)
1791 for_each_dpcm_be(fe, stream, dpcm) {
1792 struct snd_soc_pcm_runtime *be = dpcm->be;
1793 struct snd_soc_pcm_stream *codec_stream;
1794 struct snd_soc_pcm_stream *cpu_stream;
1796 cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream);
1798 *channels_min = max(*channels_min, cpu_stream->channels_min);
1799 *channels_max = min(*channels_max, cpu_stream->channels_max);
1802 * chan min/max cannot be enforced if there are multiple CODEC
1803 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1805 if (be->num_codecs == 1) {
1806 codec_stream = snd_soc_dai_get_pcm_stream(be->codec_dais[0], stream);
1808 *channels_min = max(*channels_min,
1809 codec_stream->channels_min);
1810 *channels_max = min(*channels_max,
1811 codec_stream->channels_max);
1816 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1817 unsigned int *rates,
1818 unsigned int *rate_min,
1819 unsigned int *rate_max)
1821 struct snd_soc_pcm_runtime *fe = substream->private_data;
1822 struct snd_soc_dpcm *dpcm;
1823 int stream = substream->stream;
1825 if (!fe->dai_link->dpcm_merged_rate)
1829 * It returns merged BE codec channel;
1830 * if FE want to use it (= dpcm_merged_chan)
1833 for_each_dpcm_be(fe, stream, dpcm) {
1834 struct snd_soc_pcm_runtime *be = dpcm->be;
1835 struct snd_soc_pcm_stream *codec_stream;
1836 struct snd_soc_pcm_stream *cpu_stream;
1837 struct snd_soc_dai *dai;
1840 cpu_stream = snd_soc_dai_get_pcm_stream(be->cpu_dai, stream);
1842 *rate_min = max(*rate_min, cpu_stream->rate_min);
1843 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1844 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1846 for_each_rtd_codec_dai(be, i, dai) {
1848 * Skip CODECs which don't support the current stream
1849 * type. See soc_pcm_init_runtime_hw() for more details
1851 if (!snd_soc_dai_stream_valid(dai, stream))
1854 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1856 *rate_min = max(*rate_min, codec_stream->rate_min);
1857 *rate_max = min_not_zero(*rate_max,
1858 codec_stream->rate_max);
1859 *rates = snd_pcm_rate_mask_intersect(*rates,
1860 codec_stream->rates);
1865 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1867 struct snd_pcm_runtime *runtime = substream->runtime;
1868 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1870 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1872 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1873 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1875 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1877 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1878 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1879 &runtime->hw.channels_max);
1880 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1881 &runtime->hw.rate_min, &runtime->hw.rate_max);
1884 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1886 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1887 * for avoiding the race with trigger callback.
1888 * If the state is unset and a trigger is pending while the previous operation,
1889 * process the pending trigger action here.
1891 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1892 int stream, enum snd_soc_dpcm_update state)
1894 struct snd_pcm_substream *substream =
1895 snd_soc_dpcm_get_substream(fe, stream);
1897 snd_pcm_stream_lock_irq(substream);
1898 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1899 dpcm_fe_dai_do_trigger(substream,
1900 fe->dpcm[stream].trigger_pending - 1);
1901 fe->dpcm[stream].trigger_pending = 0;
1903 fe->dpcm[stream].runtime_update = state;
1904 snd_pcm_stream_unlock_irq(substream);
1907 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1910 struct snd_soc_dpcm *dpcm;
1911 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1912 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1915 /* apply symmetry for FE */
1916 if (soc_pcm_has_symmetry(fe_substream))
1917 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1919 /* Symmetry only applies if we've got an active stream. */
1920 if (fe_cpu_dai->active) {
1921 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1926 /* apply symmetry for BE */
1927 for_each_dpcm_be(fe, stream, dpcm) {
1928 struct snd_soc_pcm_runtime *be = dpcm->be;
1929 struct snd_pcm_substream *be_substream =
1930 snd_soc_dpcm_get_substream(be, stream);
1931 struct snd_soc_pcm_runtime *rtd;
1932 struct snd_soc_dai *codec_dai;
1935 /* A backend may not have the requested substream */
1939 rtd = be_substream->private_data;
1940 if (rtd->dai_link->be_hw_params_fixup)
1943 if (soc_pcm_has_symmetry(be_substream))
1944 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1946 /* Symmetry only applies if we've got an active stream. */
1947 if (rtd->cpu_dai->active) {
1948 err = soc_pcm_apply_symmetry(fe_substream,
1954 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1955 if (codec_dai->active) {
1956 err = soc_pcm_apply_symmetry(fe_substream,
1967 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1969 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1970 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1971 int stream = fe_substream->stream, ret = 0;
1973 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1975 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1977 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1981 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1983 /* start the DAI frontend */
1984 ret = soc_pcm_open(fe_substream);
1986 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1990 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1992 dpcm_set_fe_runtime(fe_substream);
1993 snd_pcm_limit_hw_rates(runtime);
1995 ret = dpcm_apply_symmetry(fe_substream, stream);
1997 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2002 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2006 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
2008 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2012 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2014 struct snd_soc_dpcm *dpcm;
2016 /* only shutdown BEs that are either sinks or sources to this FE DAI */
2017 for_each_dpcm_be(fe, stream, dpcm) {
2019 struct snd_soc_pcm_runtime *be = dpcm->be;
2020 struct snd_pcm_substream *be_substream =
2021 snd_soc_dpcm_get_substream(be, stream);
2023 /* is this op for this BE ? */
2024 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2027 if (be->dpcm[stream].users == 0)
2028 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2029 stream ? "capture" : "playback",
2030 be->dpcm[stream].state);
2032 if (--be->dpcm[stream].users != 0)
2035 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2036 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2037 soc_pcm_hw_free(be_substream);
2038 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2041 dev_dbg(be->dev, "ASoC: close BE %s\n",
2042 be->dai_link->name);
2044 soc_pcm_close(be_substream);
2045 be_substream->runtime = NULL;
2047 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2052 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2054 struct snd_soc_pcm_runtime *fe = substream->private_data;
2055 int stream = substream->stream;
2057 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2059 /* shutdown the BEs */
2060 dpcm_be_dai_shutdown(fe, substream->stream);
2062 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2064 /* now shutdown the frontend */
2065 soc_pcm_close(substream);
2067 /* run the stream event for each BE */
2068 snd_soc_dapm_stream_stop(fe, stream);
2070 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2071 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2075 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2077 struct snd_soc_dpcm *dpcm;
2079 /* only hw_params backends that are either sinks or sources
2080 * to this frontend DAI */
2081 for_each_dpcm_be(fe, stream, dpcm) {
2083 struct snd_soc_pcm_runtime *be = dpcm->be;
2084 struct snd_pcm_substream *be_substream =
2085 snd_soc_dpcm_get_substream(be, stream);
2087 /* is this op for this BE ? */
2088 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2091 /* only free hw when no longer used - check all FEs */
2092 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2095 /* do not free hw if this BE is used by other FE */
2096 if (be->dpcm[stream].users > 1)
2099 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2100 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2101 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2102 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2103 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2104 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2107 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2108 be->dai_link->name);
2110 soc_pcm_hw_free(be_substream);
2112 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2118 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2120 struct snd_soc_pcm_runtime *fe = substream->private_data;
2121 int err, stream = substream->stream;
2123 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2124 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2126 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2128 /* call hw_free on the frontend */
2129 err = soc_pcm_hw_free(substream);
2131 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2132 fe->dai_link->name);
2134 /* only hw_params backends that are either sinks or sources
2135 * to this frontend DAI */
2136 err = dpcm_be_dai_hw_free(fe, stream);
2138 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2139 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2141 mutex_unlock(&fe->card->mutex);
2145 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2147 struct snd_soc_dpcm *dpcm;
2150 for_each_dpcm_be(fe, stream, dpcm) {
2152 struct snd_soc_pcm_runtime *be = dpcm->be;
2153 struct snd_pcm_substream *be_substream =
2154 snd_soc_dpcm_get_substream(be, stream);
2156 /* is this op for this BE ? */
2157 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2160 /* copy params for each dpcm */
2161 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2162 sizeof(struct snd_pcm_hw_params));
2164 /* perform any hw_params fixups */
2165 if (be->dai_link->be_hw_params_fixup) {
2166 ret = be->dai_link->be_hw_params_fixup(be,
2170 "ASoC: hw_params BE fixup failed %d\n",
2176 /* copy the fixed-up hw params for BE dai */
2177 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2178 sizeof(struct snd_pcm_hw_params));
2180 /* only allow hw_params() if no connected FEs are running */
2181 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2184 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2185 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2186 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2189 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2190 be->dai_link->name);
2192 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2194 dev_err(dpcm->be->dev,
2195 "ASoC: hw_params BE failed %d\n", ret);
2199 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2204 /* disable any enabled and non active backends */
2205 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2206 struct snd_soc_pcm_runtime *be = dpcm->be;
2207 struct snd_pcm_substream *be_substream =
2208 snd_soc_dpcm_get_substream(be, stream);
2210 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2213 /* only allow hw_free() if no connected FEs are running */
2214 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2217 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2218 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2219 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2220 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2223 soc_pcm_hw_free(be_substream);
2229 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2230 struct snd_pcm_hw_params *params)
2232 struct snd_soc_pcm_runtime *fe = substream->private_data;
2233 int ret, stream = substream->stream;
2235 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2236 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2238 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2239 sizeof(struct snd_pcm_hw_params));
2240 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2242 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2246 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2247 fe->dai_link->name, params_rate(params),
2248 params_channels(params), params_format(params));
2250 /* call hw_params on the frontend */
2251 ret = soc_pcm_hw_params(substream, params);
2253 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2254 dpcm_be_dai_hw_free(fe, stream);
2256 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2259 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2260 mutex_unlock(&fe->card->mutex);
2264 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2265 struct snd_pcm_substream *substream, int cmd)
2269 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2270 dpcm->be->dai_link->name, cmd);
2272 ret = soc_pcm_trigger(substream, cmd);
2274 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2279 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2282 struct snd_soc_dpcm *dpcm;
2285 for_each_dpcm_be(fe, stream, dpcm) {
2287 struct snd_soc_pcm_runtime *be = dpcm->be;
2288 struct snd_pcm_substream *be_substream =
2289 snd_soc_dpcm_get_substream(be, stream);
2291 /* is this op for this BE ? */
2292 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2296 case SNDRV_PCM_TRIGGER_START:
2297 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2298 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2301 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2305 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2307 case SNDRV_PCM_TRIGGER_RESUME:
2308 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2311 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2315 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2317 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2318 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2321 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2325 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2327 case SNDRV_PCM_TRIGGER_STOP:
2328 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2331 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2334 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2338 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2340 case SNDRV_PCM_TRIGGER_SUSPEND:
2341 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2344 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2347 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2351 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2353 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2354 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2357 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2360 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2364 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2371 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2373 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2374 int cmd, bool fe_first)
2376 struct snd_soc_pcm_runtime *fe = substream->private_data;
2379 /* call trigger on the frontend before the backend. */
2381 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2382 fe->dai_link->name, cmd);
2384 ret = soc_pcm_trigger(substream, cmd);
2388 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2392 /* call trigger on the frontend after the backend. */
2393 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2397 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2398 fe->dai_link->name, cmd);
2400 ret = soc_pcm_trigger(substream, cmd);
2405 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2407 struct snd_soc_pcm_runtime *fe = substream->private_data;
2408 int stream = substream->stream;
2410 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2412 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2415 case SND_SOC_DPCM_TRIGGER_PRE:
2417 case SNDRV_PCM_TRIGGER_START:
2418 case SNDRV_PCM_TRIGGER_RESUME:
2419 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2420 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2422 case SNDRV_PCM_TRIGGER_STOP:
2423 case SNDRV_PCM_TRIGGER_SUSPEND:
2424 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2425 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2432 case SND_SOC_DPCM_TRIGGER_POST:
2434 case SNDRV_PCM_TRIGGER_START:
2435 case SNDRV_PCM_TRIGGER_RESUME:
2436 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2437 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2439 case SNDRV_PCM_TRIGGER_STOP:
2440 case SNDRV_PCM_TRIGGER_SUSPEND:
2441 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2442 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2449 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2450 /* bespoke trigger() - handles both FE and BEs */
2452 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2453 fe->dai_link->name, cmd);
2455 ret = soc_pcm_bespoke_trigger(substream, cmd);
2458 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2459 fe->dai_link->name);
2465 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2471 case SNDRV_PCM_TRIGGER_START:
2472 case SNDRV_PCM_TRIGGER_RESUME:
2473 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2474 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2476 case SNDRV_PCM_TRIGGER_STOP:
2477 case SNDRV_PCM_TRIGGER_SUSPEND:
2478 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2480 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2481 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2486 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2490 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2492 struct snd_soc_pcm_runtime *fe = substream->private_data;
2493 int stream = substream->stream;
2495 /* if FE's runtime_update is already set, we're in race;
2496 * process this trigger later at exit
2498 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2499 fe->dpcm[stream].trigger_pending = cmd + 1;
2500 return 0; /* delayed, assuming it's successful */
2503 /* we're alone, let's trigger */
2504 return dpcm_fe_dai_do_trigger(substream, cmd);
2507 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2509 struct snd_soc_dpcm *dpcm;
2512 for_each_dpcm_be(fe, stream, dpcm) {
2514 struct snd_soc_pcm_runtime *be = dpcm->be;
2515 struct snd_pcm_substream *be_substream =
2516 snd_soc_dpcm_get_substream(be, stream);
2518 /* is this op for this BE ? */
2519 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2522 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2523 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2524 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2525 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2528 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2529 be->dai_link->name);
2531 ret = soc_pcm_prepare(be_substream);
2533 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2538 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2543 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2545 struct snd_soc_pcm_runtime *fe = substream->private_data;
2546 int stream = substream->stream, ret = 0;
2548 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2550 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2552 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2554 /* there is no point preparing this FE if there are no BEs */
2555 if (list_empty(&fe->dpcm[stream].be_clients)) {
2556 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2557 fe->dai_link->name);
2562 ret = dpcm_be_dai_prepare(fe, substream->stream);
2566 /* call prepare on the frontend */
2567 ret = soc_pcm_prepare(substream);
2569 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2570 fe->dai_link->name);
2574 /* run the stream event for each BE */
2575 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2576 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2579 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2580 mutex_unlock(&fe->card->mutex);
2585 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2587 struct snd_pcm_substream *substream =
2588 snd_soc_dpcm_get_substream(fe, stream);
2589 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2592 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2593 stream ? "capture" : "playback", fe->dai_link->name);
2595 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2596 /* call bespoke trigger - FE takes care of all BE triggers */
2597 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2598 fe->dai_link->name);
2600 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2602 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2604 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2605 fe->dai_link->name);
2607 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2609 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2612 err = dpcm_be_dai_hw_free(fe, stream);
2614 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2616 err = dpcm_be_dai_shutdown(fe, stream);
2618 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2620 /* run the stream event for each BE */
2621 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2626 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2628 struct snd_pcm_substream *substream =
2629 snd_soc_dpcm_get_substream(fe, stream);
2630 struct snd_soc_dpcm *dpcm;
2631 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2633 unsigned long flags;
2635 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2636 stream ? "capture" : "playback", fe->dai_link->name);
2638 /* Only start the BE if the FE is ready */
2639 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2640 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2643 /* startup must always be called for new BEs */
2644 ret = dpcm_be_dai_startup(fe, stream);
2648 /* keep going if FE state is > open */
2649 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2652 ret = dpcm_be_dai_hw_params(fe, stream);
2656 /* keep going if FE state is > hw_params */
2657 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2661 ret = dpcm_be_dai_prepare(fe, stream);
2665 /* run the stream event for each BE */
2666 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2668 /* keep going if FE state is > prepare */
2669 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2670 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2673 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2674 /* call trigger on the frontend - FE takes care of all BE triggers */
2675 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2676 fe->dai_link->name);
2678 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2680 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2684 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2685 fe->dai_link->name);
2687 ret = dpcm_be_dai_trigger(fe, stream,
2688 SNDRV_PCM_TRIGGER_START);
2690 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2698 dpcm_be_dai_hw_free(fe, stream);
2700 dpcm_be_dai_shutdown(fe, stream);
2702 /* disconnect any non started BEs */
2703 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2704 for_each_dpcm_be(fe, stream, dpcm) {
2705 struct snd_soc_pcm_runtime *be = dpcm->be;
2706 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2707 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2709 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2714 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2716 struct snd_soc_dapm_widget_list *list;
2721 if (!fe->dai_link->dynamic)
2724 /* only check active links */
2725 if (!fe->cpu_dai->active)
2728 /* DAPM sync will call this to update DSP paths */
2729 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2730 new ? "new" : "old", fe->dai_link->name);
2732 for_each_pcm_streams(stream) {
2734 /* skip if FE doesn't have playback/capture capability */
2735 if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) ||
2736 !snd_soc_dai_stream_valid(fe->codec_dai, stream))
2739 /* skip if FE isn't currently playing/capturing */
2740 if (!fe->cpu_dai->stream_active[stream] ||
2741 !fe->codec_dai->stream_active[stream])
2744 paths = dpcm_path_get(fe, stream, &list);
2746 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2748 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2749 "playback" : "capture");
2753 /* update any playback/capture paths */
2754 count = dpcm_process_paths(fe, stream, &list, new);
2756 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2758 ret = dpcm_run_update_startup(fe, stream);
2760 ret = dpcm_run_update_shutdown(fe, stream);
2762 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2763 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2765 dpcm_clear_pending_state(fe, stream);
2766 dpcm_be_disconnect(fe, stream);
2769 dpcm_path_put(&list);
2775 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2778 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2780 struct snd_soc_pcm_runtime *fe;
2783 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2784 /* shutdown all old paths first */
2785 for_each_card_rtds(card, fe) {
2786 ret = soc_dpcm_fe_runtime_update(fe, 0);
2791 /* bring new paths up */
2792 for_each_card_rtds(card, fe) {
2793 ret = soc_dpcm_fe_runtime_update(fe, 1);
2799 mutex_unlock(&card->mutex);
2803 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2805 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2806 struct snd_soc_dpcm *dpcm;
2807 struct snd_soc_dapm_widget_list *list;
2809 int stream = fe_substream->stream;
2811 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2812 fe->dpcm[stream].runtime = fe_substream->runtime;
2814 ret = dpcm_path_get(fe, stream, &list);
2817 } else if (ret == 0) {
2818 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2819 fe->dai_link->name, stream ? "capture" : "playback");
2822 /* calculate valid and active FE <-> BE dpcms */
2823 dpcm_process_paths(fe, stream, &list, 1);
2825 ret = dpcm_fe_dai_startup(fe_substream);
2827 /* clean up all links */
2828 for_each_dpcm_be(fe, stream, dpcm)
2829 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2831 dpcm_be_disconnect(fe, stream);
2832 fe->dpcm[stream].runtime = NULL;
2835 dpcm_clear_pending_state(fe, stream);
2836 dpcm_path_put(&list);
2838 mutex_unlock(&fe->card->mutex);
2842 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2844 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2845 struct snd_soc_dpcm *dpcm;
2846 int stream = fe_substream->stream, ret;
2848 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2849 ret = dpcm_fe_dai_shutdown(fe_substream);
2851 /* mark FE's links ready to prune */
2852 for_each_dpcm_be(fe, stream, dpcm)
2853 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2855 dpcm_be_disconnect(fe, stream);
2857 fe->dpcm[stream].runtime = NULL;
2858 mutex_unlock(&fe->card->mutex);
2862 /* create a new pcm */
2863 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2865 struct snd_soc_dai *codec_dai;
2866 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2867 struct snd_soc_component *component;
2868 struct snd_pcm *pcm;
2870 int ret = 0, playback = 0, capture = 0;
2873 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2874 playback = rtd->dai_link->dpcm_playback;
2875 capture = rtd->dai_link->dpcm_capture;
2877 /* Adapt stream for codec2codec links */
2878 int cpu_capture = rtd->dai_link->params ?
2879 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2880 int cpu_playback = rtd->dai_link->params ?
2881 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2883 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2884 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2885 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2887 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2888 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2893 if (rtd->dai_link->playback_only) {
2898 if (rtd->dai_link->capture_only) {
2903 /* create the PCM */
2904 if (rtd->dai_link->params) {
2905 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2906 rtd->dai_link->stream_name);
2908 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2909 playback, capture, &pcm);
2910 } else if (rtd->dai_link->no_pcm) {
2911 snprintf(new_name, sizeof(new_name), "(%s)",
2912 rtd->dai_link->stream_name);
2914 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2915 playback, capture, &pcm);
2917 if (rtd->dai_link->dynamic)
2918 snprintf(new_name, sizeof(new_name), "%s (*)",
2919 rtd->dai_link->stream_name);
2921 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2922 rtd->dai_link->stream_name,
2923 (rtd->num_codecs > 1) ?
2924 "multicodec" : rtd->codec_dai->name, num);
2926 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2930 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2931 rtd->dai_link->name);
2934 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2936 /* DAPM dai link stream work */
2937 if (rtd->dai_link->params)
2938 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2940 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2942 pcm->nonatomic = rtd->dai_link->nonatomic;
2944 pcm->private_data = rtd;
2946 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2948 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2950 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2954 /* ASoC PCM operations */
2955 if (rtd->dai_link->dynamic) {
2956 rtd->ops.open = dpcm_fe_dai_open;
2957 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2958 rtd->ops.prepare = dpcm_fe_dai_prepare;
2959 rtd->ops.trigger = dpcm_fe_dai_trigger;
2960 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2961 rtd->ops.close = dpcm_fe_dai_close;
2962 rtd->ops.pointer = soc_pcm_pointer;
2964 rtd->ops.open = soc_pcm_open;
2965 rtd->ops.hw_params = soc_pcm_hw_params;
2966 rtd->ops.prepare = soc_pcm_prepare;
2967 rtd->ops.trigger = soc_pcm_trigger;
2968 rtd->ops.hw_free = soc_pcm_hw_free;
2969 rtd->ops.close = soc_pcm_close;
2970 rtd->ops.pointer = soc_pcm_pointer;
2973 for_each_rtd_components(rtd, i, component) {
2974 const struct snd_soc_component_driver *drv = component->driver;
2977 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2979 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2981 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2983 rtd->ops.page = snd_soc_pcm_component_page;
2985 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2989 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2992 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2994 ret = snd_soc_pcm_component_new(rtd);
2996 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3000 pcm->no_device_suspend = true;
3002 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3003 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3008 /* is the current PCM operation for this FE ? */
3009 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3011 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3015 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3017 /* is the current PCM operation for this BE ? */
3018 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3019 struct snd_soc_pcm_runtime *be, int stream)
3021 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3022 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3023 be->dpcm[stream].runtime_update))
3027 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3029 /* get the substream for this BE */
3030 struct snd_pcm_substream *
3031 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3033 return be->pcm->streams[stream].substream;
3035 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3037 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
3038 struct snd_soc_pcm_runtime *be,
3040 const enum snd_soc_dpcm_state *states,
3043 struct snd_soc_dpcm *dpcm;
3046 unsigned long flags;
3049 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3050 for_each_dpcm_fe(be, stream, dpcm) {
3055 state = dpcm->fe->dpcm[stream].state;
3056 for (i = 0; i < num_states; i++) {
3057 if (state == states[i]) {
3063 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3065 /* it's safe to do this BE DAI */
3070 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3071 * are not running, paused or suspended for the specified stream direction.
3073 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3074 struct snd_soc_pcm_runtime *be, int stream)
3076 const enum snd_soc_dpcm_state state[] = {
3077 SND_SOC_DPCM_STATE_START,
3078 SND_SOC_DPCM_STATE_PAUSED,
3079 SND_SOC_DPCM_STATE_SUSPEND,
3082 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3084 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3087 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3088 * running, paused or suspended for the specified stream direction.
3090 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3091 struct snd_soc_pcm_runtime *be, int stream)
3093 const enum snd_soc_dpcm_state state[] = {
3094 SND_SOC_DPCM_STATE_START,
3095 SND_SOC_DPCM_STATE_PAUSED,
3096 SND_SOC_DPCM_STATE_SUSPEND,
3097 SND_SOC_DPCM_STATE_PREPARE,
3100 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
3102 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);