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
32 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
33 * @rtd: ASoC PCM runtime that is activated
34 * @stream: Direction of the PCM stream
36 * Increments the active count for all the DAIs and components attached to a PCM
37 * runtime. Should typically be called when a stream is opened.
39 * Must be called with the rtd->card->pcm_mutex being held
41 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
43 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
44 struct snd_soc_dai *codec_dai;
47 lockdep_assert_held(&rtd->card->pcm_mutex);
49 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
50 cpu_dai->playback_active++;
51 for_each_rtd_codec_dai(rtd, i, codec_dai)
52 codec_dai->playback_active++;
54 cpu_dai->capture_active++;
55 for_each_rtd_codec_dai(rtd, i, codec_dai)
56 codec_dai->capture_active++;
60 cpu_dai->component->active++;
61 for_each_rtd_codec_dai(rtd, i, codec_dai) {
63 codec_dai->component->active++;
68 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
69 * @rtd: ASoC PCM runtime that is deactivated
70 * @stream: Direction of the PCM stream
72 * Decrements the active count for all the DAIs and components attached to a PCM
73 * runtime. Should typically be called when a stream is closed.
75 * Must be called with the rtd->card->pcm_mutex being held
77 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
79 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
80 struct snd_soc_dai *codec_dai;
83 lockdep_assert_held(&rtd->card->pcm_mutex);
85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 cpu_dai->playback_active--;
87 for_each_rtd_codec_dai(rtd, i, codec_dai)
88 codec_dai->playback_active--;
90 cpu_dai->capture_active--;
91 for_each_rtd_codec_dai(rtd, i, codec_dai)
92 codec_dai->capture_active--;
96 cpu_dai->component->active--;
97 for_each_rtd_codec_dai(rtd, i, codec_dai) {
98 codec_dai->component->active--;
104 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
105 * @rtd: The ASoC PCM runtime that should be checked.
107 * This function checks whether the power down delay should be ignored for a
108 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
109 * been configured to ignore the delay, or if none of the components benefits
110 * from having the delay.
112 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
114 struct snd_soc_component *component;
118 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
121 for_each_rtd_components(rtd, i, component)
122 ignore &= !component->driver->use_pmdown_time;
128 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
129 * @substream: the pcm substream
130 * @hw: the hardware parameters
132 * Sets the substream runtime hardware parameters.
134 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
135 const struct snd_pcm_hardware *hw)
137 struct snd_pcm_runtime *runtime = substream->runtime;
138 runtime->hw.info = hw->info;
139 runtime->hw.formats = hw->formats;
140 runtime->hw.period_bytes_min = hw->period_bytes_min;
141 runtime->hw.period_bytes_max = hw->period_bytes_max;
142 runtime->hw.periods_min = hw->periods_min;
143 runtime->hw.periods_max = hw->periods_max;
144 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
145 runtime->hw.fifo_size = hw->fifo_size;
148 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
150 /* DPCM stream event, send event to FE and all active BEs. */
151 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
154 struct snd_soc_dpcm *dpcm;
156 for_each_dpcm_be(fe, dir, dpcm) {
158 struct snd_soc_pcm_runtime *be = dpcm->be;
160 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
161 be->dai_link->name, event, dir);
163 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
164 (be->dpcm[dir].users >= 1))
167 snd_soc_dapm_stream_event(be, dir, event);
170 snd_soc_dapm_stream_event(fe, dir, event);
175 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
176 struct snd_soc_dai *soc_dai)
178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
181 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
182 rtd->dai_link->symmetric_rates)) {
183 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
186 ret = snd_pcm_hw_constraint_single(substream->runtime,
187 SNDRV_PCM_HW_PARAM_RATE,
190 dev_err(soc_dai->dev,
191 "ASoC: Unable to apply rate constraint: %d\n",
197 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
198 rtd->dai_link->symmetric_channels)) {
199 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
202 ret = snd_pcm_hw_constraint_single(substream->runtime,
203 SNDRV_PCM_HW_PARAM_CHANNELS,
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
218 ret = snd_pcm_hw_constraint_single(substream->runtime,
219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 soc_dai->sample_bits);
222 dev_err(soc_dai->dev,
223 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
232 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
233 struct snd_pcm_hw_params *params)
235 struct snd_soc_pcm_runtime *rtd = substream->private_data;
236 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
237 struct snd_soc_dai *codec_dai;
238 unsigned int rate, channels, sample_bits, symmetry, i;
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
246 rtd->dai_link->symmetric_rates;
248 for_each_rtd_codec_dai(rtd, i, codec_dai)
249 symmetry |= codec_dai->driver->symmetric_rates;
251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
257 symmetry = cpu_dai->driver->symmetric_channels ||
258 rtd->dai_link->symmetric_channels;
260 for_each_rtd_codec_dai(rtd, i, codec_dai)
261 symmetry |= codec_dai->driver->symmetric_channels;
263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
270 rtd->dai_link->symmetric_samplebits;
272 for_each_rtd_codec_dai(rtd, i, codec_dai)
273 symmetry |= codec_dai->driver->symmetric_samplebits;
275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
284 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
288 struct snd_soc_dai_link *link = rtd->dai_link;
289 struct snd_soc_dai *codec_dai;
290 unsigned int symmetry, i;
292 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
293 cpu_driver->symmetric_channels || link->symmetric_channels ||
294 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
296 for_each_rtd_codec_dai(rtd, i, codec_dai)
297 symmetry = symmetry ||
298 codec_dai->driver->symmetric_rates ||
299 codec_dai->driver->symmetric_channels ||
300 codec_dai->driver->symmetric_samplebits;
305 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
307 struct snd_soc_pcm_runtime *rtd = substream->private_data;
313 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
315 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
319 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
321 struct snd_soc_pcm_runtime *rtd = substream->private_data;
322 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
323 struct snd_soc_dai *codec_dai;
325 unsigned int bits = 0, cpu_bits;
327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
328 for_each_rtd_codec_dai(rtd, i, codec_dai) {
329 if (codec_dai->driver->playback.sig_bits == 0) {
333 bits = max(codec_dai->driver->playback.sig_bits, bits);
335 cpu_bits = cpu_dai->driver->playback.sig_bits;
337 for_each_rtd_codec_dai(rtd, i, codec_dai) {
338 if (codec_dai->driver->capture.sig_bits == 0) {
342 bits = max(codec_dai->driver->capture.sig_bits, bits);
344 cpu_bits = cpu_dai->driver->capture.sig_bits;
347 soc_pcm_set_msb(substream, bits);
348 soc_pcm_set_msb(substream, cpu_bits);
351 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct snd_pcm_hardware *hw = &runtime->hw;
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 struct snd_soc_dai *codec_dai;
357 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
358 struct snd_soc_dai_driver *codec_dai_drv;
359 struct snd_soc_pcm_stream *codec_stream;
360 struct snd_soc_pcm_stream *cpu_stream;
361 unsigned int chan_min = 0, chan_max = UINT_MAX;
362 unsigned int rate_min = 0, rate_max = UINT_MAX;
363 unsigned int rates = UINT_MAX;
364 u64 formats = ULLONG_MAX;
367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 cpu_stream = &cpu_dai_drv->playback;
370 cpu_stream = &cpu_dai_drv->capture;
372 /* first calculate min/max only for CODECs in the DAI link */
373 for_each_rtd_codec_dai(rtd, i, codec_dai) {
376 * Skip CODECs which don't support the current stream type.
377 * Otherwise, since the rate, channel, and format values will
378 * zero in that case, we would have no usable settings left,
379 * causing the resulting setup to fail.
380 * At least one CODEC should match, otherwise we should have
381 * bailed out on a higher level, since there would be no
382 * CODEC to support the transfer direction in that case.
384 if (!snd_soc_dai_stream_valid(codec_dai,
388 codec_dai_drv = codec_dai->driver;
389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
390 codec_stream = &codec_dai_drv->playback;
392 codec_stream = &codec_dai_drv->capture;
393 chan_min = max(chan_min, codec_stream->channels_min);
394 chan_max = min(chan_max, codec_stream->channels_max);
395 rate_min = max(rate_min, codec_stream->rate_min);
396 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
397 formats &= codec_stream->formats;
398 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
402 * chan min/max cannot be enforced if there are multiple CODEC DAIs
403 * connected to a single CPU DAI, use CPU DAI's directly and let
404 * channel allocation be fixed up later
406 if (rtd->num_codecs > 1) {
407 chan_min = cpu_stream->channels_min;
408 chan_max = cpu_stream->channels_max;
411 hw->channels_min = max(chan_min, cpu_stream->channels_min);
412 hw->channels_max = min(chan_max, cpu_stream->channels_max);
414 hw->formats &= formats & cpu_stream->formats;
416 hw->formats = formats & cpu_stream->formats;
417 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
419 snd_pcm_limit_hw_rates(runtime);
421 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
422 hw->rate_min = max(hw->rate_min, rate_min);
423 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
424 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
427 static int soc_pcm_components_open(struct snd_pcm_substream *substream,
428 struct snd_soc_component **last)
430 struct snd_soc_pcm_runtime *rtd = substream->private_data;
431 struct snd_soc_component *component;
434 for_each_rtd_components(rtd, i, component) {
437 ret = snd_soc_component_module_get_when_open(component);
439 dev_err(component->dev,
440 "ASoC: can't get module %s\n",
445 ret = snd_soc_component_open(component, substream);
447 dev_err(component->dev,
448 "ASoC: can't open component %s: %d\n",
449 component->name, ret);
457 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
458 struct snd_soc_component *last)
460 struct snd_soc_pcm_runtime *rtd = substream->private_data;
461 struct snd_soc_component *component;
464 for_each_rtd_components(rtd, i, component) {
465 if (component == last)
468 ret |= snd_soc_component_close(component, substream);
469 snd_soc_component_module_put_when_close(component);
476 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
477 * then initialized and any private data can be allocated. This also calls
478 * startup for the cpu DAI, component, machine and codec DAI.
480 static int soc_pcm_open(struct snd_pcm_substream *substream)
482 struct snd_soc_pcm_runtime *rtd = substream->private_data;
483 struct snd_pcm_runtime *runtime = substream->runtime;
484 struct snd_soc_component *component;
485 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
486 struct snd_soc_dai *codec_dai;
487 const char *codec_dai_name = "multicodec";
490 for_each_rtd_components(rtd, i, component)
491 pinctrl_pm_select_default_state(component->dev);
493 for_each_rtd_components(rtd, i, component)
494 pm_runtime_get_sync(component->dev);
496 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
498 /* startup the audio subsystem */
499 ret = snd_soc_dai_startup(cpu_dai, substream);
501 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
506 ret = soc_pcm_components_open(substream, &component);
510 for_each_rtd_codec_dai(rtd, i, codec_dai) {
511 ret = snd_soc_dai_startup(codec_dai, substream);
513 dev_err(codec_dai->dev,
514 "ASoC: can't open codec %s: %d\n",
515 codec_dai->name, ret);
519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
520 codec_dai->tx_mask = 0;
522 codec_dai->rx_mask = 0;
525 if (rtd->dai_link->ops->startup) {
526 ret = rtd->dai_link->ops->startup(substream);
528 pr_err("ASoC: %s startup failed: %d\n",
529 rtd->dai_link->name, ret);
534 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
535 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
538 /* Check that the codec and cpu DAIs are compatible */
539 soc_pcm_init_runtime_hw(substream);
541 if (rtd->num_codecs == 1)
542 codec_dai_name = rtd->codec_dai->name;
544 if (soc_pcm_has_symmetry(substream))
545 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
548 if (!runtime->hw.rates) {
549 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
550 codec_dai_name, cpu_dai->name);
553 if (!runtime->hw.formats) {
554 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
555 codec_dai_name, cpu_dai->name);
558 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
559 runtime->hw.channels_min > runtime->hw.channels_max) {
560 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
561 codec_dai_name, cpu_dai->name);
565 soc_pcm_apply_msb(substream);
567 /* Symmetry only applies if we've already got an active stream. */
568 if (cpu_dai->active) {
569 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
574 for_each_rtd_codec_dai(rtd, i, codec_dai) {
575 if (codec_dai->active) {
576 ret = soc_pcm_apply_symmetry(substream, codec_dai);
582 pr_debug("ASoC: %s <-> %s info:\n",
583 codec_dai_name, cpu_dai->name);
584 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
585 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
586 runtime->hw.channels_max);
587 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
588 runtime->hw.rate_max);
592 snd_soc_runtime_activate(rtd, substream->stream);
594 mutex_unlock(&rtd->card->pcm_mutex);
598 if (rtd->dai_link->ops->shutdown)
599 rtd->dai_link->ops->shutdown(substream);
605 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
606 snd_soc_dai_shutdown(codec_dai, substream);
609 soc_pcm_components_close(substream, component);
611 snd_soc_dai_shutdown(cpu_dai, substream);
613 mutex_unlock(&rtd->card->pcm_mutex);
615 for_each_rtd_components(rtd, i, component) {
616 pm_runtime_mark_last_busy(component->dev);
617 pm_runtime_put_autosuspend(component->dev);
620 for_each_rtd_components(rtd, i, component)
621 if (!component->active)
622 pinctrl_pm_select_sleep_state(component->dev);
627 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
630 * Currently nothing to do for c2c links
631 * Since c2c links are internal nodes in the DAPM graph and
632 * don't interface with the outside world or application layer
633 * we don't have to do any special handling on close.
638 * Called by ALSA when a PCM substream is closed. Private data can be
639 * freed here. The cpu DAI, codec DAI, machine and components are also
642 static int soc_pcm_close(struct snd_pcm_substream *substream)
644 struct snd_soc_pcm_runtime *rtd = substream->private_data;
645 struct snd_soc_component *component;
646 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
647 struct snd_soc_dai *codec_dai;
650 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
652 snd_soc_runtime_deactivate(rtd, substream->stream);
654 /* clear the corresponding DAIs rate when inactive */
655 if (!cpu_dai->active)
658 for_each_rtd_codec_dai(rtd, i, codec_dai) {
659 if (!codec_dai->active)
663 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
665 snd_soc_dai_shutdown(cpu_dai, substream);
667 for_each_rtd_codec_dai(rtd, i, codec_dai)
668 snd_soc_dai_shutdown(codec_dai, substream);
670 if (rtd->dai_link->ops->shutdown)
671 rtd->dai_link->ops->shutdown(substream);
673 soc_pcm_components_close(substream, NULL);
675 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
676 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
677 /* powered down playback stream now */
678 snd_soc_dapm_stream_event(rtd,
679 SNDRV_PCM_STREAM_PLAYBACK,
680 SND_SOC_DAPM_STREAM_STOP);
682 /* start delayed pop wq here for playback streams */
684 queue_delayed_work(system_power_efficient_wq,
686 msecs_to_jiffies(rtd->pmdown_time));
689 /* capture streams can be powered down now */
690 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
691 SND_SOC_DAPM_STREAM_STOP);
694 mutex_unlock(&rtd->card->pcm_mutex);
696 for_each_rtd_components(rtd, i, component) {
697 pm_runtime_mark_last_busy(component->dev);
698 pm_runtime_put_autosuspend(component->dev);
701 for_each_rtd_components(rtd, i, component)
702 if (!component->active)
703 pinctrl_pm_select_sleep_state(component->dev);
709 * Called by ALSA when the PCM substream is prepared, can set format, sample
710 * rate, etc. This function is non atomic and can be called multiple times,
711 * it can refer to the runtime info.
713 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
715 struct snd_soc_pcm_runtime *rtd = substream->private_data;
716 struct snd_soc_component *component;
717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
718 struct snd_soc_dai *codec_dai;
721 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
723 if (rtd->dai_link->ops->prepare) {
724 ret = rtd->dai_link->ops->prepare(substream);
726 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
732 for_each_rtd_components(rtd, i, component) {
733 ret = snd_soc_component_prepare(component, substream);
735 dev_err(component->dev,
736 "ASoC: platform prepare error: %d\n", ret);
741 for_each_rtd_codec_dai(rtd, i, codec_dai) {
742 ret = snd_soc_dai_prepare(codec_dai, substream);
744 dev_err(codec_dai->dev,
745 "ASoC: codec DAI prepare error: %d\n",
751 ret = snd_soc_dai_prepare(cpu_dai, substream);
753 dev_err(cpu_dai->dev,
754 "ASoC: cpu DAI prepare error: %d\n", ret);
758 /* cancel any delayed stream shutdown that is pending */
759 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
762 cancel_delayed_work(&rtd->delayed_work);
765 snd_soc_dapm_stream_event(rtd, substream->stream,
766 SND_SOC_DAPM_STREAM_START);
768 for_each_rtd_codec_dai(rtd, i, codec_dai)
769 snd_soc_dai_digital_mute(codec_dai, 0,
771 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
774 mutex_unlock(&rtd->card->pcm_mutex);
778 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
781 struct snd_interval *interval;
782 int channels = hweight_long(mask);
784 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
785 interval->min = channels;
786 interval->max = channels;
789 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
790 struct snd_soc_component *last)
792 struct snd_soc_pcm_runtime *rtd = substream->private_data;
793 struct snd_soc_component *component;
796 for_each_rtd_components(rtd, i, component) {
797 if (component == last)
800 ret |= snd_soc_component_hw_free(component, substream);
807 * Called by ALSA when the hardware params are set by application. This
808 * function can also be called multiple times and can allocate buffers
809 * (using snd_pcm_lib_* ). It's non-atomic.
811 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
812 struct snd_pcm_hw_params *params)
814 struct snd_soc_pcm_runtime *rtd = substream->private_data;
815 struct snd_soc_component *component;
816 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
817 struct snd_soc_dai *codec_dai;
820 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
822 ret = soc_pcm_params_symmetry(substream, params);
826 if (rtd->dai_link->ops->hw_params) {
827 ret = rtd->dai_link->ops->hw_params(substream, params);
829 dev_err(rtd->card->dev, "ASoC: machine hw_params"
830 " failed: %d\n", ret);
835 for_each_rtd_codec_dai(rtd, i, codec_dai) {
836 struct snd_pcm_hw_params codec_params;
839 * Skip CODECs which don't support the current stream type,
840 * the idea being that if a CODEC is not used for the currently
841 * set up transfer direction, it should not need to be
842 * configured, especially since the configuration used might
843 * not even be supported by that CODEC. There may be cases
844 * however where a CODEC needs to be set up although it is
845 * actually not being used for the transfer, e.g. if a
846 * capture-only CODEC is acting as an LRCLK and/or BCLK master
847 * for the DAI link including a playback-only CODEC.
848 * If this becomes necessary, we will have to augment the
849 * machine driver setup with information on how to act, so
850 * we can do the right thing here.
852 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
855 /* copy params for each codec */
856 codec_params = *params;
858 /* fixup params based on TDM slot masks */
859 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
861 soc_pcm_codec_params_fixup(&codec_params,
864 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
866 soc_pcm_codec_params_fixup(&codec_params,
869 ret = snd_soc_dai_hw_params(codec_dai, substream,
874 codec_dai->rate = params_rate(&codec_params);
875 codec_dai->channels = params_channels(&codec_params);
876 codec_dai->sample_bits = snd_pcm_format_physical_width(
877 params_format(&codec_params));
879 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
882 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
886 /* store the parameters for each DAIs */
887 cpu_dai->rate = params_rate(params);
888 cpu_dai->channels = params_channels(params);
889 cpu_dai->sample_bits =
890 snd_pcm_format_physical_width(params_format(params));
892 snd_soc_dapm_update_dai(substream, params, cpu_dai);
894 for_each_rtd_components(rtd, i, component) {
895 ret = snd_soc_component_hw_params(component, substream, params);
897 dev_err(component->dev,
898 "ASoC: %s hw params failed: %d\n",
899 component->name, ret);
906 mutex_unlock(&rtd->card->pcm_mutex);
910 soc_pcm_components_hw_free(substream, component);
912 snd_soc_dai_hw_free(cpu_dai, substream);
919 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
920 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
923 snd_soc_dai_hw_free(codec_dai, substream);
927 if (rtd->dai_link->ops->hw_free)
928 rtd->dai_link->ops->hw_free(substream);
930 mutex_unlock(&rtd->card->pcm_mutex);
935 * Frees resources allocated by hw_params, can be called multiple times
937 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
939 struct snd_soc_pcm_runtime *rtd = substream->private_data;
940 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
941 struct snd_soc_dai *codec_dai;
942 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
945 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
947 /* clear the corresponding DAIs parameters when going to be inactive */
948 if (cpu_dai->active == 1) {
950 cpu_dai->channels = 0;
951 cpu_dai->sample_bits = 0;
954 for_each_rtd_codec_dai(rtd, i, codec_dai) {
955 if (codec_dai->active == 1) {
957 codec_dai->channels = 0;
958 codec_dai->sample_bits = 0;
962 /* apply codec digital mute */
963 for_each_rtd_codec_dai(rtd, i, codec_dai) {
964 if ((playback && codec_dai->playback_active == 1) ||
965 (!playback && codec_dai->capture_active == 1))
966 snd_soc_dai_digital_mute(codec_dai, 1,
970 /* free any machine hw params */
971 if (rtd->dai_link->ops->hw_free)
972 rtd->dai_link->ops->hw_free(substream);
974 /* free any component resources */
975 soc_pcm_components_hw_free(substream, NULL);
977 /* now free hw params for the DAIs */
978 for_each_rtd_codec_dai(rtd, i, codec_dai) {
979 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
982 snd_soc_dai_hw_free(codec_dai, substream);
985 snd_soc_dai_hw_free(cpu_dai, substream);
987 mutex_unlock(&rtd->card->pcm_mutex);
991 static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
993 struct snd_soc_pcm_runtime *rtd = substream->private_data;
994 struct snd_soc_component *component;
995 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
996 struct snd_soc_dai *codec_dai;
999 if (rtd->dai_link->ops->trigger) {
1000 ret = rtd->dai_link->ops->trigger(substream, cmd);
1005 for_each_rtd_components(rtd, i, component) {
1006 ret = snd_soc_component_trigger(component, substream, cmd);
1011 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1015 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1016 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1024 static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
1026 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1027 struct snd_soc_component *component;
1028 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1029 struct snd_soc_dai *codec_dai;
1032 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1033 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1038 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1042 for_each_rtd_components(rtd, i, component) {
1043 ret = snd_soc_component_trigger(component, substream, cmd);
1048 if (rtd->dai_link->ops->trigger) {
1049 ret = rtd->dai_link->ops->trigger(substream, cmd);
1057 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1062 case SNDRV_PCM_TRIGGER_START:
1063 case SNDRV_PCM_TRIGGER_RESUME:
1064 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1065 ret = soc_pcm_trigger_start(substream, cmd);
1067 case SNDRV_PCM_TRIGGER_STOP:
1068 case SNDRV_PCM_TRIGGER_SUSPEND:
1069 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1070 ret = soc_pcm_trigger_stop(substream, cmd);
1079 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1082 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1083 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1084 struct snd_soc_dai *codec_dai;
1087 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1088 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
1093 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1100 * soc level wrapper for pointer callback
1101 * If cpu_dai, codec_dai, component driver has the delay callback, then
1102 * the runtime->delay will be updated accordingly.
1104 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1106 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1107 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1108 struct snd_soc_dai *codec_dai;
1109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 snd_pcm_uframes_t offset = 0;
1111 snd_pcm_sframes_t delay = 0;
1112 snd_pcm_sframes_t codec_delay = 0;
1115 /* clearing the previous total delay */
1118 offset = snd_soc_pcm_component_pointer(substream);
1120 /* base delay if assigned in pointer callback */
1121 delay = runtime->delay;
1123 delay += snd_soc_dai_delay(cpu_dai, substream);
1125 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1126 codec_delay = max(codec_delay,
1127 snd_soc_dai_delay(codec_dai, substream));
1129 delay += codec_delay;
1131 runtime->delay = delay;
1136 /* connect a FE and BE */
1137 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1138 struct snd_soc_pcm_runtime *be, int stream)
1140 struct snd_soc_dpcm *dpcm;
1141 unsigned long flags;
1142 #ifdef CONFIG_DEBUG_FS
1146 /* only add new dpcms */
1147 for_each_dpcm_be(fe, stream, dpcm) {
1148 if (dpcm->be == be && dpcm->fe == fe)
1152 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1158 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1159 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1160 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1161 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1162 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1163 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1165 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1166 stream ? "capture" : "playback", fe->dai_link->name,
1167 stream ? "<-" : "->", be->dai_link->name);
1169 #ifdef CONFIG_DEBUG_FS
1170 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1171 stream ? "capture" : "playback");
1173 dpcm->debugfs_state = debugfs_create_dir(name,
1174 fe->debugfs_dpcm_root);
1175 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1183 /* reparent a BE onto another FE */
1184 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1185 struct snd_soc_pcm_runtime *be, int stream)
1187 struct snd_soc_dpcm *dpcm;
1188 struct snd_pcm_substream *fe_substream, *be_substream;
1190 /* reparent if BE is connected to other FEs */
1191 if (!be->dpcm[stream].users)
1194 be_substream = snd_soc_dpcm_get_substream(be, stream);
1196 for_each_dpcm_fe(be, stream, dpcm) {
1200 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1201 stream ? "capture" : "playback",
1202 dpcm->fe->dai_link->name,
1203 stream ? "<-" : "->", dpcm->be->dai_link->name);
1205 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1206 be_substream->runtime = fe_substream->runtime;
1211 /* disconnect a BE and FE */
1212 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1214 struct snd_soc_dpcm *dpcm, *d;
1215 unsigned long flags;
1217 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1218 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1219 stream ? "capture" : "playback",
1220 dpcm->be->dai_link->name);
1222 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1225 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1226 stream ? "capture" : "playback", fe->dai_link->name,
1227 stream ? "<-" : "->", dpcm->be->dai_link->name);
1229 /* BEs still alive need new FE */
1230 dpcm_be_reparent(fe, dpcm->be, stream);
1232 #ifdef CONFIG_DEBUG_FS
1233 debugfs_remove_recursive(dpcm->debugfs_state);
1235 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1236 list_del(&dpcm->list_be);
1237 list_del(&dpcm->list_fe);
1238 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1243 /* get BE for DAI widget and stream */
1244 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1245 struct snd_soc_dapm_widget *widget, int stream)
1247 struct snd_soc_pcm_runtime *be;
1248 struct snd_soc_dai *dai;
1251 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1253 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1254 for_each_card_rtds(card, be) {
1256 if (!be->dai_link->no_pcm)
1259 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1260 be->cpu_dai->playback_widget ?
1261 be->cpu_dai->playback_widget->name : "(not set)");
1263 if (be->cpu_dai->playback_widget == widget)
1266 for_each_rtd_codec_dai(be, i, dai) {
1267 if (dai->playback_widget == widget)
1273 for_each_card_rtds(card, be) {
1275 if (!be->dai_link->no_pcm)
1278 dev_dbg(card->dev, "ASoC: try BE %s\n",
1279 be->cpu_dai->capture_widget ?
1280 be->cpu_dai->capture_widget->name : "(not set)");
1282 if (be->cpu_dai->capture_widget == widget)
1285 for_each_rtd_codec_dai(be, i, dai) {
1286 if (dai->capture_widget == widget)
1292 /* dai link name and stream name set correctly ? */
1293 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1294 stream ? "capture" : "playback", widget->name);
1298 static inline struct snd_soc_dapm_widget *
1299 dai_get_widget(struct snd_soc_dai *dai, int stream)
1301 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1302 return dai->playback_widget;
1304 return dai->capture_widget;
1307 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1308 struct snd_soc_dapm_widget *widget)
1312 for (i = 0; i < list->num_widgets; i++) {
1313 if (widget == list->widgets[i])
1320 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1321 enum snd_soc_dapm_direction dir)
1323 struct snd_soc_card *card = widget->dapm->card;
1324 struct snd_soc_pcm_runtime *rtd;
1325 struct snd_soc_dai *dai;
1328 if (dir == SND_SOC_DAPM_DIR_OUT) {
1329 for_each_card_rtds(card, rtd) {
1330 if (!rtd->dai_link->no_pcm)
1333 if (rtd->cpu_dai->playback_widget == widget)
1336 for_each_rtd_codec_dai(rtd, i, dai) {
1337 if (dai->playback_widget == widget)
1341 } else { /* SND_SOC_DAPM_DIR_IN */
1342 for_each_card_rtds(card, rtd) {
1343 if (!rtd->dai_link->no_pcm)
1346 if (rtd->cpu_dai->capture_widget == widget)
1349 for_each_rtd_codec_dai(rtd, i, dai) {
1350 if (dai->capture_widget == widget)
1359 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1360 int stream, struct snd_soc_dapm_widget_list **list)
1362 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1365 /* get number of valid DAI paths and their widgets */
1366 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1367 dpcm_end_walk_at_be);
1369 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1370 stream ? "capture" : "playback");
1375 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1376 struct snd_soc_dapm_widget_list **list_)
1378 struct snd_soc_dpcm *dpcm;
1379 struct snd_soc_dapm_widget_list *list = *list_;
1380 struct snd_soc_dapm_widget *widget;
1381 struct snd_soc_dai *dai;
1385 /* Destroy any old FE <--> BE connections */
1386 for_each_dpcm_be(fe, stream, dpcm) {
1389 /* is there a valid CPU DAI widget for this BE */
1390 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1392 /* prune the BE if it's no longer in our active list */
1393 if (widget && widget_in_list(list, widget))
1396 /* is there a valid CODEC DAI widget for this BE */
1398 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1399 widget = dai_get_widget(dai, stream);
1401 /* prune the BE if it's no longer in our active list */
1402 if (widget && widget_in_list(list, widget))
1408 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1409 stream ? "capture" : "playback",
1410 dpcm->be->dai_link->name, fe->dai_link->name);
1411 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1412 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1416 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1420 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1421 struct snd_soc_dapm_widget_list **list_)
1423 struct snd_soc_card *card = fe->card;
1424 struct snd_soc_dapm_widget_list *list = *list_;
1425 struct snd_soc_pcm_runtime *be;
1426 int i, new = 0, err;
1428 /* Create any new FE <--> BE connections */
1429 for (i = 0; i < list->num_widgets; i++) {
1431 switch (list->widgets[i]->id) {
1432 case snd_soc_dapm_dai_in:
1433 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1436 case snd_soc_dapm_dai_out:
1437 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1444 /* is there a valid BE rtd for this widget */
1445 be = dpcm_get_be(card, list->widgets[i], stream);
1447 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1448 list->widgets[i]->name);
1452 /* make sure BE is a real BE */
1453 if (!be->dai_link->no_pcm)
1456 /* don't connect if FE is not running */
1457 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1460 /* newly connected FE and BE */
1461 err = dpcm_be_connect(fe, be, stream);
1463 dev_err(fe->dev, "ASoC: can't connect %s\n",
1464 list->widgets[i]->name);
1466 } else if (err == 0) /* already connected */
1470 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1474 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1479 * Find the corresponding BE DAIs that source or sink audio to this
1482 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1483 int stream, struct snd_soc_dapm_widget_list **list, int new)
1486 return dpcm_add_paths(fe, stream, list);
1488 return dpcm_prune_paths(fe, stream, list);
1491 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1493 struct snd_soc_dpcm *dpcm;
1494 unsigned long flags;
1496 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1497 for_each_dpcm_be(fe, stream, dpcm)
1498 dpcm->be->dpcm[stream].runtime_update =
1499 SND_SOC_DPCM_UPDATE_NO;
1500 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1503 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1506 struct snd_soc_dpcm *dpcm;
1508 /* disable any enabled and non active backends */
1509 for_each_dpcm_be(fe, stream, dpcm) {
1511 struct snd_soc_pcm_runtime *be = dpcm->be;
1512 struct snd_pcm_substream *be_substream =
1513 snd_soc_dpcm_get_substream(be, stream);
1515 if (be->dpcm[stream].users == 0)
1516 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1517 stream ? "capture" : "playback",
1518 be->dpcm[stream].state);
1520 if (--be->dpcm[stream].users != 0)
1523 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1526 soc_pcm_close(be_substream);
1527 be_substream->runtime = NULL;
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1532 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1534 struct snd_soc_dpcm *dpcm;
1537 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1538 for_each_dpcm_be(fe, stream, dpcm) {
1540 struct snd_soc_pcm_runtime *be = dpcm->be;
1541 struct snd_pcm_substream *be_substream =
1542 snd_soc_dpcm_get_substream(be, stream);
1544 if (!be_substream) {
1545 dev_err(be->dev, "ASoC: no backend %s stream\n",
1546 stream ? "capture" : "playback");
1550 /* is this op for this BE ? */
1551 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1554 /* first time the dpcm is open ? */
1555 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1556 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1557 stream ? "capture" : "playback",
1558 be->dpcm[stream].state);
1560 if (be->dpcm[stream].users++ != 0)
1563 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1564 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1567 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1568 stream ? "capture" : "playback", be->dai_link->name);
1570 be_substream->runtime = be->dpcm[stream].runtime;
1571 err = soc_pcm_open(be_substream);
1573 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1574 be->dpcm[stream].users--;
1575 if (be->dpcm[stream].users < 0)
1576 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1577 stream ? "capture" : "playback",
1578 be->dpcm[stream].state);
1580 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1584 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1591 /* disable any enabled and non active backends */
1592 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1593 struct snd_soc_pcm_runtime *be = dpcm->be;
1594 struct snd_pcm_substream *be_substream =
1595 snd_soc_dpcm_get_substream(be, stream);
1597 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1600 if (be->dpcm[stream].users == 0)
1601 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1602 stream ? "capture" : "playback",
1603 be->dpcm[stream].state);
1605 if (--be->dpcm[stream].users != 0)
1608 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1611 soc_pcm_close(be_substream);
1612 be_substream->runtime = NULL;
1613 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1619 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1620 struct snd_soc_pcm_stream *stream)
1622 runtime->hw.rate_min = stream->rate_min;
1623 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1624 runtime->hw.channels_min = stream->channels_min;
1625 runtime->hw.channels_max = stream->channels_max;
1626 if (runtime->hw.formats)
1627 runtime->hw.formats &= stream->formats;
1629 runtime->hw.formats = stream->formats;
1630 runtime->hw.rates = stream->rates;
1633 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1636 struct snd_soc_pcm_runtime *fe = substream->private_data;
1637 struct snd_soc_dpcm *dpcm;
1638 struct snd_soc_dai *dai;
1639 int stream = substream->stream;
1641 if (!fe->dai_link->dpcm_merged_format)
1645 * It returns merged BE codec format
1646 * if FE want to use it (= dpcm_merged_format)
1649 for_each_dpcm_be(fe, stream, dpcm) {
1650 struct snd_soc_pcm_runtime *be = dpcm->be;
1651 struct snd_soc_dai_driver *codec_dai_drv;
1652 struct snd_soc_pcm_stream *codec_stream;
1655 for_each_rtd_codec_dai(be, i, dai) {
1657 * Skip CODECs which don't support the current stream
1658 * type. See soc_pcm_init_runtime_hw() for more details
1660 if (!snd_soc_dai_stream_valid(dai, stream))
1663 codec_dai_drv = dai->driver;
1664 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1665 codec_stream = &codec_dai_drv->playback;
1667 codec_stream = &codec_dai_drv->capture;
1669 *formats &= codec_stream->formats;
1674 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1675 unsigned int *channels_min,
1676 unsigned int *channels_max)
1678 struct snd_soc_pcm_runtime *fe = substream->private_data;
1679 struct snd_soc_dpcm *dpcm;
1680 int stream = substream->stream;
1682 if (!fe->dai_link->dpcm_merged_chan)
1686 * It returns merged BE codec channel;
1687 * if FE want to use it (= dpcm_merged_chan)
1690 for_each_dpcm_be(fe, stream, dpcm) {
1691 struct snd_soc_pcm_runtime *be = dpcm->be;
1692 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1693 struct snd_soc_dai_driver *codec_dai_drv;
1694 struct snd_soc_pcm_stream *codec_stream;
1695 struct snd_soc_pcm_stream *cpu_stream;
1697 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1698 cpu_stream = &cpu_dai_drv->playback;
1700 cpu_stream = &cpu_dai_drv->capture;
1702 *channels_min = max(*channels_min, cpu_stream->channels_min);
1703 *channels_max = min(*channels_max, cpu_stream->channels_max);
1706 * chan min/max cannot be enforced if there are multiple CODEC
1707 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1709 if (be->num_codecs == 1) {
1710 codec_dai_drv = be->codec_dais[0]->driver;
1712 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1713 codec_stream = &codec_dai_drv->playback;
1715 codec_stream = &codec_dai_drv->capture;
1717 *channels_min = max(*channels_min,
1718 codec_stream->channels_min);
1719 *channels_max = min(*channels_max,
1720 codec_stream->channels_max);
1725 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1726 unsigned int *rates,
1727 unsigned int *rate_min,
1728 unsigned int *rate_max)
1730 struct snd_soc_pcm_runtime *fe = substream->private_data;
1731 struct snd_soc_dpcm *dpcm;
1732 int stream = substream->stream;
1734 if (!fe->dai_link->dpcm_merged_rate)
1738 * It returns merged BE codec channel;
1739 * if FE want to use it (= dpcm_merged_chan)
1742 for_each_dpcm_be(fe, stream, dpcm) {
1743 struct snd_soc_pcm_runtime *be = dpcm->be;
1744 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1745 struct snd_soc_dai_driver *codec_dai_drv;
1746 struct snd_soc_pcm_stream *codec_stream;
1747 struct snd_soc_pcm_stream *cpu_stream;
1748 struct snd_soc_dai *dai;
1751 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1752 cpu_stream = &cpu_dai_drv->playback;
1754 cpu_stream = &cpu_dai_drv->capture;
1756 *rate_min = max(*rate_min, cpu_stream->rate_min);
1757 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1758 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
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_dai_drv = dai->driver;
1769 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1770 codec_stream = &codec_dai_drv->playback;
1772 codec_stream = &codec_dai_drv->capture;
1774 *rate_min = max(*rate_min, codec_stream->rate_min);
1775 *rate_max = min_not_zero(*rate_max,
1776 codec_stream->rate_max);
1777 *rates = snd_pcm_rate_mask_intersect(*rates,
1778 codec_stream->rates);
1783 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1785 struct snd_pcm_runtime *runtime = substream->runtime;
1786 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1787 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1788 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1790 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1791 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1793 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1795 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1796 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1797 &runtime->hw.channels_max);
1798 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1799 &runtime->hw.rate_min, &runtime->hw.rate_max);
1802 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1804 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1805 * for avoiding the race with trigger callback.
1806 * If the state is unset and a trigger is pending while the previous operation,
1807 * process the pending trigger action here.
1809 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1810 int stream, enum snd_soc_dpcm_update state)
1812 struct snd_pcm_substream *substream =
1813 snd_soc_dpcm_get_substream(fe, stream);
1815 snd_pcm_stream_lock_irq(substream);
1816 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1817 dpcm_fe_dai_do_trigger(substream,
1818 fe->dpcm[stream].trigger_pending - 1);
1819 fe->dpcm[stream].trigger_pending = 0;
1821 fe->dpcm[stream].runtime_update = state;
1822 snd_pcm_stream_unlock_irq(substream);
1825 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1828 struct snd_soc_dpcm *dpcm;
1829 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1830 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1833 /* apply symmetry for FE */
1834 if (soc_pcm_has_symmetry(fe_substream))
1835 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1837 /* Symmetry only applies if we've got an active stream. */
1838 if (fe_cpu_dai->active) {
1839 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1844 /* apply symmetry for BE */
1845 for_each_dpcm_be(fe, stream, dpcm) {
1846 struct snd_soc_pcm_runtime *be = dpcm->be;
1847 struct snd_pcm_substream *be_substream =
1848 snd_soc_dpcm_get_substream(be, stream);
1849 struct snd_soc_pcm_runtime *rtd;
1850 struct snd_soc_dai *codec_dai;
1853 /* A backend may not have the requested substream */
1857 rtd = be_substream->private_data;
1858 if (rtd->dai_link->be_hw_params_fixup)
1861 if (soc_pcm_has_symmetry(be_substream))
1862 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1864 /* Symmetry only applies if we've got an active stream. */
1865 if (rtd->cpu_dai->active) {
1866 err = soc_pcm_apply_symmetry(fe_substream,
1872 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1873 if (codec_dai->active) {
1874 err = soc_pcm_apply_symmetry(fe_substream,
1885 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1887 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1888 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1889 int stream = fe_substream->stream, ret = 0;
1891 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1893 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1895 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1899 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1901 /* start the DAI frontend */
1902 ret = soc_pcm_open(fe_substream);
1904 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1908 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1910 dpcm_set_fe_runtime(fe_substream);
1911 snd_pcm_limit_hw_rates(runtime);
1913 ret = dpcm_apply_symmetry(fe_substream, stream);
1915 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1920 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1924 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1926 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1930 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1932 struct snd_soc_dpcm *dpcm;
1934 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1935 for_each_dpcm_be(fe, stream, dpcm) {
1937 struct snd_soc_pcm_runtime *be = dpcm->be;
1938 struct snd_pcm_substream *be_substream =
1939 snd_soc_dpcm_get_substream(be, stream);
1941 /* is this op for this BE ? */
1942 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1945 if (be->dpcm[stream].users == 0)
1946 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1947 stream ? "capture" : "playback",
1948 be->dpcm[stream].state);
1950 if (--be->dpcm[stream].users != 0)
1953 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1954 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1955 soc_pcm_hw_free(be_substream);
1956 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1959 dev_dbg(be->dev, "ASoC: close BE %s\n",
1960 be->dai_link->name);
1962 soc_pcm_close(be_substream);
1963 be_substream->runtime = NULL;
1965 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1970 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1972 struct snd_soc_pcm_runtime *fe = substream->private_data;
1973 int stream = substream->stream;
1975 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1977 /* shutdown the BEs */
1978 dpcm_be_dai_shutdown(fe, substream->stream);
1980 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1982 /* now shutdown the frontend */
1983 soc_pcm_close(substream);
1985 /* run the stream event for each BE */
1986 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1988 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1989 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1993 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1995 struct snd_soc_dpcm *dpcm;
1997 /* only hw_params backends that are either sinks or sources
1998 * to this frontend DAI */
1999 for_each_dpcm_be(fe, stream, dpcm) {
2001 struct snd_soc_pcm_runtime *be = dpcm->be;
2002 struct snd_pcm_substream *be_substream =
2003 snd_soc_dpcm_get_substream(be, stream);
2005 /* is this op for this BE ? */
2006 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2009 /* only free hw when no longer used - check all FEs */
2010 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2013 /* do not free hw if this BE is used by other FE */
2014 if (be->dpcm[stream].users > 1)
2017 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2018 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2019 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2020 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2021 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2022 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2025 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2026 be->dai_link->name);
2028 soc_pcm_hw_free(be_substream);
2030 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2036 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2038 struct snd_soc_pcm_runtime *fe = substream->private_data;
2039 int err, stream = substream->stream;
2041 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2042 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2044 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2046 /* call hw_free on the frontend */
2047 err = soc_pcm_hw_free(substream);
2049 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2050 fe->dai_link->name);
2052 /* only hw_params backends that are either sinks or sources
2053 * to this frontend DAI */
2054 err = dpcm_be_dai_hw_free(fe, stream);
2056 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2057 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2059 mutex_unlock(&fe->card->mutex);
2063 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2065 struct snd_soc_dpcm *dpcm;
2068 for_each_dpcm_be(fe, stream, dpcm) {
2070 struct snd_soc_pcm_runtime *be = dpcm->be;
2071 struct snd_pcm_substream *be_substream =
2072 snd_soc_dpcm_get_substream(be, stream);
2074 /* is this op for this BE ? */
2075 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2078 /* copy params for each dpcm */
2079 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2080 sizeof(struct snd_pcm_hw_params));
2082 /* perform any hw_params fixups */
2083 if (be->dai_link->be_hw_params_fixup) {
2084 ret = be->dai_link->be_hw_params_fixup(be,
2088 "ASoC: hw_params BE fixup failed %d\n",
2094 /* copy the fixed-up hw params for BE dai */
2095 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2096 sizeof(struct snd_pcm_hw_params));
2098 /* only allow hw_params() if no connected FEs are running */
2099 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2102 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2103 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2104 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2107 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2108 be->dai_link->name);
2110 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2112 dev_err(dpcm->be->dev,
2113 "ASoC: hw_params BE failed %d\n", ret);
2117 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2122 /* disable any enabled and non active backends */
2123 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2124 struct snd_soc_pcm_runtime *be = dpcm->be;
2125 struct snd_pcm_substream *be_substream =
2126 snd_soc_dpcm_get_substream(be, stream);
2128 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2131 /* only allow hw_free() if no connected FEs are running */
2132 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2135 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2136 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2137 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2138 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2141 soc_pcm_hw_free(be_substream);
2147 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2148 struct snd_pcm_hw_params *params)
2150 struct snd_soc_pcm_runtime *fe = substream->private_data;
2151 int ret, stream = substream->stream;
2153 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2154 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2156 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2157 sizeof(struct snd_pcm_hw_params));
2158 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2160 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2164 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2165 fe->dai_link->name, params_rate(params),
2166 params_channels(params), params_format(params));
2168 /* call hw_params on the frontend */
2169 ret = soc_pcm_hw_params(substream, params);
2171 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2172 dpcm_be_dai_hw_free(fe, stream);
2174 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2177 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2178 mutex_unlock(&fe->card->mutex);
2182 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2183 struct snd_pcm_substream *substream, int cmd)
2187 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2188 dpcm->be->dai_link->name, cmd);
2190 ret = soc_pcm_trigger(substream, cmd);
2192 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2197 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2200 struct snd_soc_dpcm *dpcm;
2203 for_each_dpcm_be(fe, stream, dpcm) {
2205 struct snd_soc_pcm_runtime *be = dpcm->be;
2206 struct snd_pcm_substream *be_substream =
2207 snd_soc_dpcm_get_substream(be, stream);
2209 /* is this op for this BE ? */
2210 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2214 case SNDRV_PCM_TRIGGER_START:
2215 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2216 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2219 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2223 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2225 case SNDRV_PCM_TRIGGER_RESUME:
2226 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2229 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2233 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2235 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2236 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2239 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2243 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2245 case SNDRV_PCM_TRIGGER_STOP:
2246 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2249 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2252 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2256 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2258 case SNDRV_PCM_TRIGGER_SUSPEND:
2259 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2262 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2265 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2269 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2271 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2272 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2275 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2278 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2282 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2289 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2291 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2292 int cmd, bool fe_first)
2294 struct snd_soc_pcm_runtime *fe = substream->private_data;
2297 /* call trigger on the frontend before the backend. */
2299 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2300 fe->dai_link->name, cmd);
2302 ret = soc_pcm_trigger(substream, cmd);
2306 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2310 /* call trigger on the frontend after the backend. */
2311 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2315 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2316 fe->dai_link->name, cmd);
2318 ret = soc_pcm_trigger(substream, cmd);
2323 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2325 struct snd_soc_pcm_runtime *fe = substream->private_data;
2326 int stream = substream->stream;
2328 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2330 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2333 case SND_SOC_DPCM_TRIGGER_PRE:
2335 case SNDRV_PCM_TRIGGER_START:
2336 case SNDRV_PCM_TRIGGER_RESUME:
2337 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2338 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2340 case SNDRV_PCM_TRIGGER_STOP:
2341 case SNDRV_PCM_TRIGGER_SUSPEND:
2342 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2343 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2350 case SND_SOC_DPCM_TRIGGER_POST:
2352 case SNDRV_PCM_TRIGGER_START:
2353 case SNDRV_PCM_TRIGGER_RESUME:
2354 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2355 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2357 case SNDRV_PCM_TRIGGER_STOP:
2358 case SNDRV_PCM_TRIGGER_SUSPEND:
2359 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2360 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2367 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2368 /* bespoke trigger() - handles both FE and BEs */
2370 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2371 fe->dai_link->name, cmd);
2373 ret = soc_pcm_bespoke_trigger(substream, cmd);
2376 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2377 fe->dai_link->name);
2383 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2389 case SNDRV_PCM_TRIGGER_START:
2390 case SNDRV_PCM_TRIGGER_RESUME:
2391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2392 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2394 case SNDRV_PCM_TRIGGER_STOP:
2395 case SNDRV_PCM_TRIGGER_SUSPEND:
2396 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2398 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2399 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2404 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2408 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2410 struct snd_soc_pcm_runtime *fe = substream->private_data;
2411 int stream = substream->stream;
2413 /* if FE's runtime_update is already set, we're in race;
2414 * process this trigger later at exit
2416 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2417 fe->dpcm[stream].trigger_pending = cmd + 1;
2418 return 0; /* delayed, assuming it's successful */
2421 /* we're alone, let's trigger */
2422 return dpcm_fe_dai_do_trigger(substream, cmd);
2425 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2427 struct snd_soc_dpcm *dpcm;
2430 for_each_dpcm_be(fe, stream, dpcm) {
2432 struct snd_soc_pcm_runtime *be = dpcm->be;
2433 struct snd_pcm_substream *be_substream =
2434 snd_soc_dpcm_get_substream(be, stream);
2436 /* is this op for this BE ? */
2437 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2440 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2441 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2442 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2443 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2446 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2447 be->dai_link->name);
2449 ret = soc_pcm_prepare(be_substream);
2451 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2456 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2461 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2463 struct snd_soc_pcm_runtime *fe = substream->private_data;
2464 int stream = substream->stream, ret = 0;
2466 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2468 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2470 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2472 /* there is no point preparing this FE if there are no BEs */
2473 if (list_empty(&fe->dpcm[stream].be_clients)) {
2474 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2475 fe->dai_link->name);
2480 ret = dpcm_be_dai_prepare(fe, substream->stream);
2484 /* call prepare on the frontend */
2485 ret = soc_pcm_prepare(substream);
2487 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2488 fe->dai_link->name);
2492 /* run the stream event for each BE */
2493 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2494 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2497 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2498 mutex_unlock(&fe->card->mutex);
2503 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2505 struct snd_pcm_substream *substream =
2506 snd_soc_dpcm_get_substream(fe, stream);
2507 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2510 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2511 stream ? "capture" : "playback", fe->dai_link->name);
2513 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2514 /* call bespoke trigger - FE takes care of all BE triggers */
2515 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2516 fe->dai_link->name);
2518 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2520 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2522 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2523 fe->dai_link->name);
2525 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2527 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2530 err = dpcm_be_dai_hw_free(fe, stream);
2532 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2534 err = dpcm_be_dai_shutdown(fe, stream);
2536 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2538 /* run the stream event for each BE */
2539 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2544 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2546 struct snd_pcm_substream *substream =
2547 snd_soc_dpcm_get_substream(fe, stream);
2548 struct snd_soc_dpcm *dpcm;
2549 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2551 unsigned long flags;
2553 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2554 stream ? "capture" : "playback", fe->dai_link->name);
2556 /* Only start the BE if the FE is ready */
2557 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2558 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2561 /* startup must always be called for new BEs */
2562 ret = dpcm_be_dai_startup(fe, stream);
2566 /* keep going if FE state is > open */
2567 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2570 ret = dpcm_be_dai_hw_params(fe, stream);
2574 /* keep going if FE state is > hw_params */
2575 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2579 ret = dpcm_be_dai_prepare(fe, stream);
2583 /* run the stream event for each BE */
2584 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2586 /* keep going if FE state is > prepare */
2587 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2588 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2591 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2592 /* call trigger on the frontend - FE takes care of all BE triggers */
2593 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2594 fe->dai_link->name);
2596 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2598 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2602 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2603 fe->dai_link->name);
2605 ret = dpcm_be_dai_trigger(fe, stream,
2606 SNDRV_PCM_TRIGGER_START);
2608 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2616 dpcm_be_dai_hw_free(fe, stream);
2618 dpcm_be_dai_shutdown(fe, stream);
2620 /* disconnect any non started BEs */
2621 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2622 for_each_dpcm_be(fe, stream, dpcm) {
2623 struct snd_soc_pcm_runtime *be = dpcm->be;
2624 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2625 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2627 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2632 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2636 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2637 ret = dpcm_run_update_startup(fe, stream);
2639 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2640 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2645 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2650 ret = dpcm_run_update_shutdown(fe, stream);
2652 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2653 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2658 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2660 struct snd_soc_dapm_widget_list *list;
2663 if (!fe->dai_link->dynamic)
2666 /* only check active links */
2667 if (!fe->cpu_dai->active)
2670 /* DAPM sync will call this to update DSP paths */
2671 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2672 new ? "new" : "old", fe->dai_link->name);
2674 /* skip if FE doesn't have playback capability */
2675 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2676 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
2679 /* skip if FE isn't currently playing */
2680 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2683 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2685 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2686 fe->dai_link->name, "playback");
2690 /* update any playback paths */
2691 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2694 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2696 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2698 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2699 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2702 dpcm_path_put(&list);
2705 /* skip if FE doesn't have capture capability */
2706 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2707 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
2710 /* skip if FE isn't currently capturing */
2711 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2714 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2716 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2717 fe->dai_link->name, "capture");
2721 /* update any old capture paths */
2722 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2725 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2727 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2729 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2730 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2733 dpcm_path_put(&list);
2738 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2741 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2743 struct snd_soc_pcm_runtime *fe;
2746 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2747 /* shutdown all old paths first */
2748 for_each_card_rtds(card, fe) {
2749 ret = soc_dpcm_fe_runtime_update(fe, 0);
2754 /* bring new paths up */
2755 for_each_card_rtds(card, fe) {
2756 ret = soc_dpcm_fe_runtime_update(fe, 1);
2762 mutex_unlock(&card->mutex);
2765 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2767 struct snd_soc_dpcm *dpcm;
2768 struct snd_soc_dai *dai;
2770 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2772 struct snd_soc_pcm_runtime *be = dpcm->be;
2775 if (be->dai_link->ignore_suspend)
2778 for_each_rtd_codec_dai(be, i, dai) {
2779 struct snd_soc_dai_driver *drv = dai->driver;
2781 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2782 be->dai_link->name);
2784 if (drv->ops && drv->ops->digital_mute &&
2785 dai->playback_active)
2786 drv->ops->digital_mute(dai, mute);
2793 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2795 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2796 struct snd_soc_dpcm *dpcm;
2797 struct snd_soc_dapm_widget_list *list;
2799 int stream = fe_substream->stream;
2801 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2802 fe->dpcm[stream].runtime = fe_substream->runtime;
2804 ret = dpcm_path_get(fe, stream, &list);
2806 mutex_unlock(&fe->card->mutex);
2808 } else if (ret == 0) {
2809 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2810 fe->dai_link->name, stream ? "capture" : "playback");
2813 /* calculate valid and active FE <-> BE dpcms */
2814 dpcm_process_paths(fe, stream, &list, 1);
2816 ret = dpcm_fe_dai_startup(fe_substream);
2818 /* clean up all links */
2819 for_each_dpcm_be(fe, stream, dpcm)
2820 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2822 dpcm_be_disconnect(fe, stream);
2823 fe->dpcm[stream].runtime = NULL;
2826 dpcm_clear_pending_state(fe, stream);
2827 dpcm_path_put(&list);
2828 mutex_unlock(&fe->card->mutex);
2832 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2834 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2835 struct snd_soc_dpcm *dpcm;
2836 int stream = fe_substream->stream, ret;
2838 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2839 ret = dpcm_fe_dai_shutdown(fe_substream);
2841 /* mark FE's links ready to prune */
2842 for_each_dpcm_be(fe, stream, dpcm)
2843 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2845 dpcm_be_disconnect(fe, stream);
2847 fe->dpcm[stream].runtime = NULL;
2848 mutex_unlock(&fe->card->mutex);
2852 /* create a new pcm */
2853 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2855 struct snd_soc_dai *codec_dai;
2856 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2857 struct snd_soc_component *component;
2858 struct snd_pcm *pcm;
2860 int ret = 0, playback = 0, capture = 0;
2863 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2864 playback = rtd->dai_link->dpcm_playback;
2865 capture = rtd->dai_link->dpcm_capture;
2867 /* Adapt stream for codec2codec links */
2868 struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2869 &cpu_dai->driver->playback : &cpu_dai->driver->capture;
2870 struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2871 &cpu_dai->driver->capture : &cpu_dai->driver->playback;
2873 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2874 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2875 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
2877 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2878 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
2882 capture = capture && cpu_capture->channels_min;
2883 playback = playback && cpu_playback->channels_min;
2886 if (rtd->dai_link->playback_only) {
2891 if (rtd->dai_link->capture_only) {
2896 /* create the PCM */
2897 if (rtd->dai_link->params) {
2898 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2899 rtd->dai_link->stream_name);
2901 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2902 playback, capture, &pcm);
2903 } else if (rtd->dai_link->no_pcm) {
2904 snprintf(new_name, sizeof(new_name), "(%s)",
2905 rtd->dai_link->stream_name);
2907 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2908 playback, capture, &pcm);
2910 if (rtd->dai_link->dynamic)
2911 snprintf(new_name, sizeof(new_name), "%s (*)",
2912 rtd->dai_link->stream_name);
2914 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2915 rtd->dai_link->stream_name,
2916 (rtd->num_codecs > 1) ?
2917 "multicodec" : rtd->codec_dai->name, num);
2919 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2923 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2924 rtd->dai_link->name);
2927 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2929 /* DAPM dai link stream work */
2930 if (rtd->dai_link->params)
2931 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2933 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2935 pcm->nonatomic = rtd->dai_link->nonatomic;
2937 pcm->private_data = rtd;
2939 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2941 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2943 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2947 /* ASoC PCM operations */
2948 if (rtd->dai_link->dynamic) {
2949 rtd->ops.open = dpcm_fe_dai_open;
2950 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2951 rtd->ops.prepare = dpcm_fe_dai_prepare;
2952 rtd->ops.trigger = dpcm_fe_dai_trigger;
2953 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2954 rtd->ops.close = dpcm_fe_dai_close;
2955 rtd->ops.pointer = soc_pcm_pointer;
2957 rtd->ops.open = soc_pcm_open;
2958 rtd->ops.hw_params = soc_pcm_hw_params;
2959 rtd->ops.prepare = soc_pcm_prepare;
2960 rtd->ops.trigger = soc_pcm_trigger;
2961 rtd->ops.hw_free = soc_pcm_hw_free;
2962 rtd->ops.close = soc_pcm_close;
2963 rtd->ops.pointer = soc_pcm_pointer;
2966 for_each_rtd_components(rtd, i, component) {
2967 const struct snd_soc_component_driver *drv = component->driver;
2970 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2972 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2974 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2976 rtd->ops.page = snd_soc_pcm_component_page;
2978 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2982 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2985 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2987 ret = snd_soc_pcm_component_new(rtd);
2989 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2993 pcm->no_device_suspend = true;
2995 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2996 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3001 /* is the current PCM operation for this FE ? */
3002 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3004 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3008 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3010 /* is the current PCM operation for this BE ? */
3011 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3012 struct snd_soc_pcm_runtime *be, int stream)
3014 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3015 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3016 be->dpcm[stream].runtime_update))
3020 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3022 /* get the substream for this BE */
3023 struct snd_pcm_substream *
3024 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3026 return be->pcm->streams[stream].substream;
3028 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3030 /* get the BE runtime state */
3031 enum snd_soc_dpcm_state
3032 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3034 return be->dpcm[stream].state;
3036 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3038 /* set the BE runtime state */
3039 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3040 int stream, enum snd_soc_dpcm_state state)
3042 be->dpcm[stream].state = state;
3044 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3047 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3048 * are not running, paused or suspended for the specified stream direction.
3050 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3051 struct snd_soc_pcm_runtime *be, int stream)
3053 struct snd_soc_dpcm *dpcm;
3056 unsigned long flags;
3058 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3059 for_each_dpcm_fe(be, stream, dpcm) {
3064 state = dpcm->fe->dpcm[stream].state;
3065 if (state == SND_SOC_DPCM_STATE_START ||
3066 state == SND_SOC_DPCM_STATE_PAUSED ||
3067 state == SND_SOC_DPCM_STATE_SUSPEND) {
3072 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3074 /* it's safe to free/stop this BE DAI */
3077 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3080 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3081 * running, paused or suspended for the specified stream direction.
3083 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3084 struct snd_soc_pcm_runtime *be, int stream)
3086 struct snd_soc_dpcm *dpcm;
3089 unsigned long flags;
3091 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3092 for_each_dpcm_fe(be, stream, dpcm) {
3097 state = dpcm->fe->dpcm[stream].state;
3098 if (state == SND_SOC_DPCM_STATE_START ||
3099 state == SND_SOC_DPCM_STATE_PAUSED ||
3100 state == SND_SOC_DPCM_STATE_SUSPEND ||
3101 state == SND_SOC_DPCM_STATE_PREPARE) {
3106 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3108 /* it's safe to change hw_params */
3111 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3113 #ifdef CONFIG_DEBUG_FS
3114 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3117 case SND_SOC_DPCM_STATE_NEW:
3119 case SND_SOC_DPCM_STATE_OPEN:
3121 case SND_SOC_DPCM_STATE_HW_PARAMS:
3123 case SND_SOC_DPCM_STATE_PREPARE:
3125 case SND_SOC_DPCM_STATE_START:
3127 case SND_SOC_DPCM_STATE_STOP:
3129 case SND_SOC_DPCM_STATE_SUSPEND:
3131 case SND_SOC_DPCM_STATE_PAUSED:
3133 case SND_SOC_DPCM_STATE_HW_FREE:
3135 case SND_SOC_DPCM_STATE_CLOSE:
3142 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3143 int stream, char *buf, size_t size)
3145 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3146 struct snd_soc_dpcm *dpcm;
3148 unsigned long flags;
3151 offset += snprintf(buf + offset, size - offset,
3152 "[%s - %s]\n", fe->dai_link->name,
3153 stream ? "Capture" : "Playback");
3155 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3156 dpcm_state_string(fe->dpcm[stream].state));
3158 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3159 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3160 offset += snprintf(buf + offset, size - offset,
3162 "Format = %s, Channels = %d, Rate = %d\n",
3163 snd_pcm_format_name(params_format(params)),
3164 params_channels(params),
3165 params_rate(params));
3168 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3170 if (list_empty(&fe->dpcm[stream].be_clients)) {
3171 offset += snprintf(buf + offset, size - offset,
3172 " No active DSP links\n");
3176 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3177 for_each_dpcm_be(fe, stream, dpcm) {
3178 struct snd_soc_pcm_runtime *be = dpcm->be;
3179 params = &dpcm->hw_params;
3181 offset += snprintf(buf + offset, size - offset,
3182 "- %s\n", be->dai_link->name);
3184 offset += snprintf(buf + offset, size - offset,
3186 dpcm_state_string(be->dpcm[stream].state));
3188 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3189 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3190 offset += snprintf(buf + offset, size - offset,
3191 " Hardware Params: "
3192 "Format = %s, Channels = %d, Rate = %d\n",
3193 snd_pcm_format_name(params_format(params)),
3194 params_channels(params),
3195 params_rate(params));
3197 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3202 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3203 size_t count, loff_t *ppos)
3205 struct snd_soc_pcm_runtime *fe = file->private_data;
3206 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3209 buf = kmalloc(out_count, GFP_KERNEL);
3213 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
3214 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3215 buf + offset, out_count - offset);
3217 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
3218 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3219 buf + offset, out_count - offset);
3221 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3227 static const struct file_operations dpcm_state_fops = {
3228 .open = simple_open,
3229 .read = dpcm_state_read_file,
3230 .llseek = default_llseek,
3233 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3238 if (!rtd->dai_link->dynamic)
3241 if (!rtd->card->debugfs_card_root)
3244 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3245 rtd->card->debugfs_card_root);
3247 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3248 rtd, &dpcm_state_fops);