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 static int soc_rtd_startup(struct snd_soc_pcm_runtime *rtd,
32 struct snd_pcm_substream *substream)
34 if (rtd->dai_link->ops &&
35 rtd->dai_link->ops->startup)
36 return rtd->dai_link->ops->startup(substream);
40 static void soc_rtd_shutdown(struct snd_soc_pcm_runtime *rtd,
41 struct snd_pcm_substream *substream)
43 if (rtd->dai_link->ops &&
44 rtd->dai_link->ops->shutdown)
45 rtd->dai_link->ops->shutdown(substream);
48 static int soc_rtd_prepare(struct snd_soc_pcm_runtime *rtd,
49 struct snd_pcm_substream *substream)
51 if (rtd->dai_link->ops &&
52 rtd->dai_link->ops->prepare)
53 return rtd->dai_link->ops->prepare(substream);
57 static int soc_rtd_hw_params(struct snd_soc_pcm_runtime *rtd,
58 struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
61 if (rtd->dai_link->ops &&
62 rtd->dai_link->ops->hw_params)
63 return rtd->dai_link->ops->hw_params(substream, params);
67 static void soc_rtd_hw_free(struct snd_soc_pcm_runtime *rtd,
68 struct snd_pcm_substream *substream)
70 if (rtd->dai_link->ops &&
71 rtd->dai_link->ops->hw_free)
72 rtd->dai_link->ops->hw_free(substream);
75 static int soc_rtd_trigger(struct snd_soc_pcm_runtime *rtd,
76 struct snd_pcm_substream *substream,
79 if (rtd->dai_link->ops &&
80 rtd->dai_link->ops->trigger)
81 return rtd->dai_link->ops->trigger(substream, cmd);
86 struct snd_soc_dapm_widget *dai_get_widget(struct snd_soc_dai *dai, int stream)
88 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
89 return dai->playback_widget;
91 return dai->capture_widget;
94 static void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
95 int stream, int action)
97 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 struct snd_soc_dai *codec_dai;
101 lockdep_assert_held(&rtd->card->pcm_mutex);
103 cpu_dai->stream_active[stream] += action;
104 for_each_rtd_codec_dai(rtd, i, codec_dai)
105 codec_dai->stream_active[stream] += action;
107 cpu_dai->active += action;
108 cpu_dai->component->active += action;
109 for_each_rtd_codec_dai(rtd, i, codec_dai) {
110 codec_dai->active += action;
111 codec_dai->component->active += action;
116 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
117 * @rtd: ASoC PCM runtime that is activated
118 * @stream: Direction of the PCM stream
120 * Increments the active count for all the DAIs and components attached to a PCM
121 * runtime. Should typically be called when a stream is opened.
123 * Must be called with the rtd->card->pcm_mutex being held
125 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
127 snd_soc_runtime_action(rtd, stream, 1);
131 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
132 * @rtd: ASoC PCM runtime that is deactivated
133 * @stream: Direction of the PCM stream
135 * Decrements the active count for all the DAIs and components attached to a PCM
136 * runtime. Should typically be called when a stream is closed.
138 * Must be called with the rtd->card->pcm_mutex being held
140 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
142 snd_soc_runtime_action(rtd, stream, -1);
146 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
147 * @rtd: The ASoC PCM runtime that should be checked.
149 * This function checks whether the power down delay should be ignored for a
150 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
151 * been configured to ignore the delay, or if none of the components benefits
152 * from having the delay.
154 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
156 struct snd_soc_component *component;
160 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
163 for_each_rtd_components(rtd, i, component)
164 ignore &= !component->driver->use_pmdown_time;
170 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
171 * @substream: the pcm substream
172 * @hw: the hardware parameters
174 * Sets the substream runtime hardware parameters.
176 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
177 const struct snd_pcm_hardware *hw)
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 runtime->hw.info = hw->info;
181 runtime->hw.formats = hw->formats;
182 runtime->hw.period_bytes_min = hw->period_bytes_min;
183 runtime->hw.period_bytes_max = hw->period_bytes_max;
184 runtime->hw.periods_min = hw->periods_min;
185 runtime->hw.periods_max = hw->periods_max;
186 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
187 runtime->hw.fifo_size = hw->fifo_size;
190 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
192 /* DPCM stream event, send event to FE and all active BEs. */
193 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
196 struct snd_soc_dpcm *dpcm;
198 for_each_dpcm_be(fe, dir, dpcm) {
200 struct snd_soc_pcm_runtime *be = dpcm->be;
202 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
203 be->dai_link->name, event, dir);
205 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
206 (be->dpcm[dir].users >= 1))
209 snd_soc_dapm_stream_event(be, dir, event);
212 snd_soc_dapm_stream_event(fe, dir, event);
217 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
218 struct snd_soc_dai *soc_dai)
220 struct snd_soc_pcm_runtime *rtd = substream->private_data;
223 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
224 rtd->dai_link->symmetric_rates)) {
225 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
228 ret = snd_pcm_hw_constraint_single(substream->runtime,
229 SNDRV_PCM_HW_PARAM_RATE,
232 dev_err(soc_dai->dev,
233 "ASoC: Unable to apply rate constraint: %d\n",
239 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
240 rtd->dai_link->symmetric_channels)) {
241 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
244 ret = snd_pcm_hw_constraint_single(substream->runtime,
245 SNDRV_PCM_HW_PARAM_CHANNELS,
248 dev_err(soc_dai->dev,
249 "ASoC: Unable to apply channel symmetry constraint: %d\n",
255 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
256 rtd->dai_link->symmetric_samplebits)) {
257 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
258 soc_dai->sample_bits);
260 ret = snd_pcm_hw_constraint_single(substream->runtime,
261 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
262 soc_dai->sample_bits);
264 dev_err(soc_dai->dev,
265 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
274 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
275 struct snd_pcm_hw_params *params)
277 struct snd_soc_pcm_runtime *rtd = substream->private_data;
278 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
279 struct snd_soc_dai *codec_dai;
280 unsigned int rate, channels, sample_bits, symmetry, i;
282 rate = params_rate(params);
283 channels = params_channels(params);
284 sample_bits = snd_pcm_format_physical_width(params_format(params));
286 /* reject unmatched parameters when applying symmetry */
287 symmetry = cpu_dai->driver->symmetric_rates ||
288 rtd->dai_link->symmetric_rates;
290 for_each_rtd_codec_dai(rtd, i, codec_dai)
291 symmetry |= codec_dai->driver->symmetric_rates;
293 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
294 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
295 cpu_dai->rate, rate);
299 symmetry = cpu_dai->driver->symmetric_channels ||
300 rtd->dai_link->symmetric_channels;
302 for_each_rtd_codec_dai(rtd, i, codec_dai)
303 symmetry |= codec_dai->driver->symmetric_channels;
305 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
306 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
307 cpu_dai->channels, channels);
311 symmetry = cpu_dai->driver->symmetric_samplebits ||
312 rtd->dai_link->symmetric_samplebits;
314 for_each_rtd_codec_dai(rtd, i, codec_dai)
315 symmetry |= codec_dai->driver->symmetric_samplebits;
317 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
318 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
319 cpu_dai->sample_bits, sample_bits);
326 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
328 struct snd_soc_pcm_runtime *rtd = substream->private_data;
329 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
330 struct snd_soc_dai_link *link = rtd->dai_link;
331 struct snd_soc_dai *codec_dai;
332 unsigned int symmetry, i;
334 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
335 cpu_driver->symmetric_channels || link->symmetric_channels ||
336 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
338 for_each_rtd_codec_dai(rtd, i, codec_dai)
339 symmetry = symmetry ||
340 codec_dai->driver->symmetric_rates ||
341 codec_dai->driver->symmetric_channels ||
342 codec_dai->driver->symmetric_samplebits;
347 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
349 struct snd_soc_pcm_runtime *rtd = substream->private_data;
355 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
357 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
361 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
363 struct snd_soc_pcm_runtime *rtd = substream->private_data;
364 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
365 struct snd_soc_dai *codec_dai;
367 unsigned int bits = 0, cpu_bits;
369 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
370 for_each_rtd_codec_dai(rtd, i, codec_dai) {
371 if (codec_dai->driver->playback.sig_bits == 0) {
375 bits = max(codec_dai->driver->playback.sig_bits, bits);
377 cpu_bits = cpu_dai->driver->playback.sig_bits;
379 for_each_rtd_codec_dai(rtd, i, codec_dai) {
380 if (codec_dai->driver->capture.sig_bits == 0) {
384 bits = max(codec_dai->driver->capture.sig_bits, bits);
386 cpu_bits = cpu_dai->driver->capture.sig_bits;
389 soc_pcm_set_msb(substream, bits);
390 soc_pcm_set_msb(substream, cpu_bits);
393 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
395 struct snd_pcm_runtime *runtime = substream->runtime;
396 struct snd_pcm_hardware *hw = &runtime->hw;
397 struct snd_soc_pcm_runtime *rtd = substream->private_data;
398 struct snd_soc_dai *codec_dai;
399 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
400 struct snd_soc_dai_driver *codec_dai_drv;
401 struct snd_soc_pcm_stream *codec_stream;
402 struct snd_soc_pcm_stream *cpu_stream;
403 unsigned int chan_min = 0, chan_max = UINT_MAX;
404 unsigned int rate_min = 0, rate_max = UINT_MAX;
405 unsigned int rates = UINT_MAX;
406 u64 formats = ULLONG_MAX;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 cpu_stream = &cpu_dai_drv->playback;
412 cpu_stream = &cpu_dai_drv->capture;
414 /* first calculate min/max only for CODECs in the DAI link */
415 for_each_rtd_codec_dai(rtd, i, codec_dai) {
418 * Skip CODECs which don't support the current stream type.
419 * Otherwise, since the rate, channel, and format values will
420 * zero in that case, we would have no usable settings left,
421 * causing the resulting setup to fail.
422 * At least one CODEC should match, otherwise we should have
423 * bailed out on a higher level, since there would be no
424 * CODEC to support the transfer direction in that case.
426 if (!snd_soc_dai_stream_valid(codec_dai,
430 codec_dai_drv = codec_dai->driver;
431 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
432 codec_stream = &codec_dai_drv->playback;
434 codec_stream = &codec_dai_drv->capture;
435 chan_min = max(chan_min, codec_stream->channels_min);
436 chan_max = min(chan_max, codec_stream->channels_max);
437 rate_min = max(rate_min, codec_stream->rate_min);
438 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
439 formats &= codec_stream->formats;
440 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
444 * chan min/max cannot be enforced if there are multiple CODEC DAIs
445 * connected to a single CPU DAI, use CPU DAI's directly and let
446 * channel allocation be fixed up later
448 if (rtd->num_codecs > 1) {
449 chan_min = cpu_stream->channels_min;
450 chan_max = cpu_stream->channels_max;
453 hw->channels_min = max(chan_min, cpu_stream->channels_min);
454 hw->channels_max = min(chan_max, cpu_stream->channels_max);
456 hw->formats &= formats & cpu_stream->formats;
458 hw->formats = formats & cpu_stream->formats;
459 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
461 snd_pcm_limit_hw_rates(runtime);
463 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
464 hw->rate_min = max(hw->rate_min, rate_min);
465 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
466 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
469 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
471 struct snd_soc_pcm_runtime *rtd = substream->private_data;
472 struct snd_soc_component *component;
475 for_each_rtd_components(rtd, i, component) {
476 ret = snd_soc_component_module_get_when_open(component);
478 dev_err(component->dev,
479 "ASoC: can't get module %s\n",
484 ret = snd_soc_component_open(component, substream);
486 dev_err(component->dev,
487 "ASoC: can't open component %s: %d\n",
488 component->name, ret);
496 static int soc_pcm_components_close(struct snd_pcm_substream *substream)
498 struct snd_soc_pcm_runtime *rtd = substream->private_data;
499 struct snd_soc_component *component;
502 for_each_rtd_components(rtd, i, component) {
503 r = snd_soc_component_close(component, substream);
505 ret = r; /* use last ret */
507 snd_soc_component_module_put_when_close(component);
514 * Called by ALSA when a PCM substream is closed. Private data can be
515 * freed here. The cpu DAI, codec DAI, machine and components are also
518 static int soc_pcm_close(struct snd_pcm_substream *substream)
520 struct snd_soc_pcm_runtime *rtd = substream->private_data;
521 struct snd_soc_component *component;
522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
523 struct snd_soc_dai *codec_dai;
526 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
528 snd_soc_runtime_deactivate(rtd, substream->stream);
530 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
532 snd_soc_dai_shutdown(cpu_dai, substream);
534 for_each_rtd_codec_dai(rtd, i, codec_dai)
535 snd_soc_dai_shutdown(codec_dai, substream);
537 soc_rtd_shutdown(rtd, substream);
539 soc_pcm_components_close(substream);
541 snd_soc_dapm_stream_stop(rtd, substream->stream);
543 mutex_unlock(&rtd->card->pcm_mutex);
545 for_each_rtd_components(rtd, i, component) {
546 pm_runtime_mark_last_busy(component->dev);
547 pm_runtime_put_autosuspend(component->dev);
550 for_each_rtd_components(rtd, i, component)
551 if (!component->active)
552 pinctrl_pm_select_sleep_state(component->dev);
558 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
559 * then initialized and any private data can be allocated. This also calls
560 * startup for the cpu DAI, component, machine and codec DAI.
562 static int soc_pcm_open(struct snd_pcm_substream *substream)
564 struct snd_soc_pcm_runtime *rtd = substream->private_data;
565 struct snd_pcm_runtime *runtime = substream->runtime;
566 struct snd_soc_component *component;
567 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
568 struct snd_soc_dai *codec_dai;
569 const char *codec_dai_name = "multicodec";
572 for_each_rtd_components(rtd, i, component)
573 pinctrl_pm_select_default_state(component->dev);
575 for_each_rtd_components(rtd, i, component)
576 pm_runtime_get_sync(component->dev);
578 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
580 ret = soc_pcm_components_open(substream);
584 ret = soc_rtd_startup(rtd, substream);
586 pr_err("ASoC: %s startup failed: %d\n",
587 rtd->dai_link->name, ret);
591 /* startup the audio subsystem */
592 ret = snd_soc_dai_startup(cpu_dai, substream);
594 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
599 for_each_rtd_codec_dai(rtd, i, codec_dai) {
600 ret = snd_soc_dai_startup(codec_dai, substream);
602 dev_err(codec_dai->dev,
603 "ASoC: can't open codec %s: %d\n",
604 codec_dai->name, ret);
608 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
609 codec_dai->tx_mask = 0;
611 codec_dai->rx_mask = 0;
614 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
615 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
618 /* Check that the codec and cpu DAIs are compatible */
619 soc_pcm_init_runtime_hw(substream);
621 if (rtd->num_codecs == 1)
622 codec_dai_name = rtd->codec_dai->name;
624 if (soc_pcm_has_symmetry(substream))
625 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
628 if (!runtime->hw.rates) {
629 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
630 codec_dai_name, cpu_dai->name);
633 if (!runtime->hw.formats) {
634 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
635 codec_dai_name, cpu_dai->name);
638 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
639 runtime->hw.channels_min > runtime->hw.channels_max) {
640 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
641 codec_dai_name, cpu_dai->name);
645 soc_pcm_apply_msb(substream);
647 /* Symmetry only applies if we've already got an active stream. */
648 if (cpu_dai->active) {
649 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
654 for_each_rtd_codec_dai(rtd, i, codec_dai) {
655 if (codec_dai->active) {
656 ret = soc_pcm_apply_symmetry(substream, codec_dai);
662 pr_debug("ASoC: %s <-> %s info:\n",
663 codec_dai_name, cpu_dai->name);
664 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
665 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
666 runtime->hw.channels_max);
667 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
668 runtime->hw.rate_max);
672 snd_soc_runtime_activate(rtd, substream->stream);
674 mutex_unlock(&rtd->card->pcm_mutex);
678 for_each_rtd_codec_dai(rtd, i, codec_dai)
679 snd_soc_dai_shutdown(codec_dai, substream);
681 snd_soc_dai_shutdown(cpu_dai, substream);
683 soc_rtd_shutdown(rtd, substream);
685 soc_pcm_components_close(substream);
687 mutex_unlock(&rtd->card->pcm_mutex);
689 for_each_rtd_components(rtd, i, component) {
690 pm_runtime_mark_last_busy(component->dev);
691 pm_runtime_put_autosuspend(component->dev);
694 for_each_rtd_components(rtd, i, component)
695 if (!component->active)
696 pinctrl_pm_select_sleep_state(component->dev);
701 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
704 * Currently nothing to do for c2c links
705 * Since c2c links are internal nodes in the DAPM graph and
706 * don't interface with the outside world or application layer
707 * we don't have to do any special handling on close.
712 * Called by ALSA when the PCM substream is prepared, can set format, sample
713 * rate, etc. This function is non atomic and can be called multiple times,
714 * it can refer to the runtime info.
716 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
718 struct snd_soc_pcm_runtime *rtd = substream->private_data;
719 struct snd_soc_component *component;
720 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
721 struct snd_soc_dai *codec_dai;
724 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
726 ret = soc_rtd_prepare(rtd, substream);
728 dev_err(rtd->card->dev,
729 "ASoC: machine prepare error: %d\n", ret);
733 for_each_rtd_components(rtd, i, component) {
734 ret = snd_soc_component_prepare(component, substream);
736 dev_err(component->dev,
737 "ASoC: platform prepare error: %d\n", ret);
742 for_each_rtd_codec_dai(rtd, i, codec_dai) {
743 ret = snd_soc_dai_prepare(codec_dai, substream);
745 dev_err(codec_dai->dev,
746 "ASoC: codec DAI prepare error: %d\n",
752 ret = snd_soc_dai_prepare(cpu_dai, substream);
754 dev_err(cpu_dai->dev,
755 "ASoC: cpu DAI prepare error: %d\n", ret);
759 /* cancel any delayed stream shutdown that is pending */
760 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
763 cancel_delayed_work(&rtd->delayed_work);
766 snd_soc_dapm_stream_event(rtd, substream->stream,
767 SND_SOC_DAPM_STREAM_START);
769 for_each_rtd_codec_dai(rtd, i, codec_dai)
770 snd_soc_dai_digital_mute(codec_dai, 0,
772 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
775 mutex_unlock(&rtd->card->pcm_mutex);
779 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
782 struct snd_interval *interval;
783 int channels = hweight_long(mask);
785 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
786 interval->min = channels;
787 interval->max = channels;
790 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
791 struct snd_soc_component *last)
793 struct snd_soc_pcm_runtime *rtd = substream->private_data;
794 struct snd_soc_component *component;
797 for_each_rtd_components(rtd, i, component) {
798 if (component == last)
801 r = snd_soc_component_hw_free(component, substream);
803 ret = r; /* use last ret */
810 * Called by ALSA when the hardware params are set by application. This
811 * function can also be called multiple times and can allocate buffers
812 * (using snd_pcm_lib_* ). It's non-atomic.
814 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
815 struct snd_pcm_hw_params *params)
817 struct snd_soc_pcm_runtime *rtd = substream->private_data;
818 struct snd_soc_component *component;
819 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
820 struct snd_soc_dai *codec_dai;
823 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
825 ret = soc_pcm_params_symmetry(substream, params);
829 ret = soc_rtd_hw_params(rtd, substream, params);
831 dev_err(rtd->card->dev,
832 "ASoC: machine hw_params failed: %d\n", ret);
836 for_each_rtd_codec_dai(rtd, i, codec_dai) {
837 struct snd_pcm_hw_params codec_params;
840 * Skip CODECs which don't support the current stream type,
841 * the idea being that if a CODEC is not used for the currently
842 * set up transfer direction, it should not need to be
843 * configured, especially since the configuration used might
844 * not even be supported by that CODEC. There may be cases
845 * however where a CODEC needs to be set up although it is
846 * actually not being used for the transfer, e.g. if a
847 * capture-only CODEC is acting as an LRCLK and/or BCLK master
848 * for the DAI link including a playback-only CODEC.
849 * If this becomes necessary, we will have to augment the
850 * machine driver setup with information on how to act, so
851 * we can do the right thing here.
853 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
856 /* copy params for each codec */
857 codec_params = *params;
859 /* fixup params based on TDM slot masks */
860 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
862 soc_pcm_codec_params_fixup(&codec_params,
865 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
867 soc_pcm_codec_params_fixup(&codec_params,
870 ret = snd_soc_dai_hw_params(codec_dai, substream,
875 codec_dai->rate = params_rate(&codec_params);
876 codec_dai->channels = params_channels(&codec_params);
877 codec_dai->sample_bits = snd_pcm_format_physical_width(
878 params_format(&codec_params));
880 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
883 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
887 /* store the parameters for each DAIs */
888 cpu_dai->rate = params_rate(params);
889 cpu_dai->channels = params_channels(params);
890 cpu_dai->sample_bits =
891 snd_pcm_format_physical_width(params_format(params));
893 snd_soc_dapm_update_dai(substream, params, cpu_dai);
895 for_each_rtd_components(rtd, i, component) {
896 ret = snd_soc_component_hw_params(component, substream, params);
898 dev_err(component->dev,
899 "ASoC: %s hw params failed: %d\n",
900 component->name, ret);
907 mutex_unlock(&rtd->card->pcm_mutex);
911 soc_pcm_components_hw_free(substream, component);
913 snd_soc_dai_hw_free(cpu_dai, substream);
920 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
921 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
924 snd_soc_dai_hw_free(codec_dai, substream);
928 soc_rtd_hw_free(rtd, 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 int playback_active = codec_dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK];
965 int capture_active = codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE];
967 if ((playback && playback_active == 1) ||
968 (!playback && capture_active == 1))
969 snd_soc_dai_digital_mute(codec_dai, 1,
973 /* free any machine hw params */
974 soc_rtd_hw_free(rtd, substream);
976 /* free any component resources */
977 soc_pcm_components_hw_free(substream, NULL);
979 /* now free hw params for the DAIs */
980 for_each_rtd_codec_dai(rtd, i, codec_dai) {
981 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
984 snd_soc_dai_hw_free(codec_dai, substream);
987 snd_soc_dai_hw_free(cpu_dai, substream);
989 mutex_unlock(&rtd->card->pcm_mutex);
993 static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
995 struct snd_soc_pcm_runtime *rtd = substream->private_data;
996 struct snd_soc_component *component;
997 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
998 struct snd_soc_dai *codec_dai;
1001 ret = soc_rtd_trigger(rtd, 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 ret = soc_rtd_trigger(rtd, substream, cmd);
1055 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1060 case SNDRV_PCM_TRIGGER_START:
1061 case SNDRV_PCM_TRIGGER_RESUME:
1062 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1063 ret = soc_pcm_trigger_start(substream, cmd);
1065 case SNDRV_PCM_TRIGGER_STOP:
1066 case SNDRV_PCM_TRIGGER_SUSPEND:
1067 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1068 ret = soc_pcm_trigger_stop(substream, cmd);
1077 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1080 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1081 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1082 struct snd_soc_dai *codec_dai;
1085 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1086 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
1091 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1098 * soc level wrapper for pointer callback
1099 * If cpu_dai, codec_dai, component driver has the delay callback, then
1100 * the runtime->delay will be updated accordingly.
1102 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1104 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1105 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1106 struct snd_soc_dai *codec_dai;
1107 struct snd_pcm_runtime *runtime = substream->runtime;
1108 snd_pcm_uframes_t offset = 0;
1109 snd_pcm_sframes_t delay = 0;
1110 snd_pcm_sframes_t codec_delay = 0;
1113 /* clearing the previous total delay */
1116 offset = snd_soc_pcm_component_pointer(substream);
1118 /* base delay if assigned in pointer callback */
1119 delay = runtime->delay;
1121 delay += snd_soc_dai_delay(cpu_dai, substream);
1123 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1124 codec_delay = max(codec_delay,
1125 snd_soc_dai_delay(codec_dai, substream));
1127 delay += codec_delay;
1129 runtime->delay = delay;
1134 /* connect a FE and BE */
1135 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1136 struct snd_soc_pcm_runtime *be, int stream)
1138 struct snd_soc_dpcm *dpcm;
1139 unsigned long flags;
1140 #ifdef CONFIG_DEBUG_FS
1144 /* only add new dpcms */
1145 for_each_dpcm_be(fe, stream, dpcm) {
1146 if (dpcm->be == be && dpcm->fe == fe)
1150 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1156 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1157 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1158 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1159 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1160 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1161 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1163 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1164 stream ? "capture" : "playback", fe->dai_link->name,
1165 stream ? "<-" : "->", be->dai_link->name);
1167 #ifdef CONFIG_DEBUG_FS
1168 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1169 stream ? "capture" : "playback");
1171 dpcm->debugfs_state = debugfs_create_dir(name,
1172 fe->debugfs_dpcm_root);
1173 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1181 /* reparent a BE onto another FE */
1182 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1183 struct snd_soc_pcm_runtime *be, int stream)
1185 struct snd_soc_dpcm *dpcm;
1186 struct snd_pcm_substream *fe_substream, *be_substream;
1188 /* reparent if BE is connected to other FEs */
1189 if (!be->dpcm[stream].users)
1192 be_substream = snd_soc_dpcm_get_substream(be, stream);
1194 for_each_dpcm_fe(be, stream, dpcm) {
1198 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1199 stream ? "capture" : "playback",
1200 dpcm->fe->dai_link->name,
1201 stream ? "<-" : "->", dpcm->be->dai_link->name);
1203 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1204 be_substream->runtime = fe_substream->runtime;
1209 /* disconnect a BE and FE */
1210 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1212 struct snd_soc_dpcm *dpcm, *d;
1213 unsigned long flags;
1215 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1216 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1217 stream ? "capture" : "playback",
1218 dpcm->be->dai_link->name);
1220 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1223 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1224 stream ? "capture" : "playback", fe->dai_link->name,
1225 stream ? "<-" : "->", dpcm->be->dai_link->name);
1227 /* BEs still alive need new FE */
1228 dpcm_be_reparent(fe, dpcm->be, stream);
1230 #ifdef CONFIG_DEBUG_FS
1231 debugfs_remove_recursive(dpcm->debugfs_state);
1233 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1234 list_del(&dpcm->list_be);
1235 list_del(&dpcm->list_fe);
1236 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1241 /* get BE for DAI widget and stream */
1242 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1243 struct snd_soc_dapm_widget *widget, int stream)
1245 struct snd_soc_pcm_runtime *be;
1246 struct snd_soc_dapm_widget *w;
1247 struct snd_soc_dai *dai;
1250 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1252 for_each_card_rtds(card, be) {
1254 if (!be->dai_link->no_pcm)
1257 w = dai_get_widget(be->cpu_dai, stream);
1259 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1260 w ? w->name : "(not set)");
1265 for_each_rtd_codec_dai(be, i, dai) {
1266 w = dai_get_widget(dai, stream);
1273 /* Widget provided is not a BE */
1277 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1278 struct snd_soc_dapm_widget *widget)
1280 struct snd_soc_dapm_widget *w;
1283 for_each_dapm_widgets(list, i, w)
1290 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1291 enum snd_soc_dapm_direction dir)
1293 struct snd_soc_card *card = widget->dapm->card;
1294 struct snd_soc_pcm_runtime *rtd;
1297 /* adjust dir to stream */
1298 if (dir == SND_SOC_DAPM_DIR_OUT)
1299 stream = SNDRV_PCM_STREAM_PLAYBACK;
1301 stream = SNDRV_PCM_STREAM_CAPTURE;
1303 rtd = dpcm_get_be(card, widget, stream);
1310 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1311 int stream, struct snd_soc_dapm_widget_list **list)
1313 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1316 /* get number of valid DAI paths and their widgets */
1317 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1318 dpcm_end_walk_at_be);
1320 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1321 stream ? "capture" : "playback");
1326 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1327 struct snd_soc_dapm_widget_list **list_)
1329 struct snd_soc_dpcm *dpcm;
1330 struct snd_soc_dapm_widget_list *list = *list_;
1331 struct snd_soc_dapm_widget *widget;
1332 struct snd_soc_dai *dai;
1336 /* Destroy any old FE <--> BE connections */
1337 for_each_dpcm_be(fe, stream, dpcm) {
1340 /* is there a valid CPU DAI widget for this BE */
1341 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1343 /* prune the BE if it's no longer in our active list */
1344 if (widget && widget_in_list(list, widget))
1347 /* is there a valid CODEC DAI widget for this BE */
1349 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1350 widget = dai_get_widget(dai, stream);
1352 /* prune the BE if it's no longer in our active list */
1353 if (widget && widget_in_list(list, widget))
1359 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1360 stream ? "capture" : "playback",
1361 dpcm->be->dai_link->name, fe->dai_link->name);
1362 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1363 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1367 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1371 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1372 struct snd_soc_dapm_widget_list **list_)
1374 struct snd_soc_card *card = fe->card;
1375 struct snd_soc_dapm_widget_list *list = *list_;
1376 struct snd_soc_pcm_runtime *be;
1377 struct snd_soc_dapm_widget *widget;
1378 int i, new = 0, err;
1380 /* Create any new FE <--> BE connections */
1381 for_each_dapm_widgets(list, i, widget) {
1383 switch (widget->id) {
1384 case snd_soc_dapm_dai_in:
1385 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1388 case snd_soc_dapm_dai_out:
1389 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1396 /* is there a valid BE rtd for this widget */
1397 be = dpcm_get_be(card, widget, stream);
1399 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1404 /* make sure BE is a real BE */
1405 if (!be->dai_link->no_pcm)
1408 /* don't connect if FE is not running */
1409 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1412 /* newly connected FE and BE */
1413 err = dpcm_be_connect(fe, be, stream);
1415 dev_err(fe->dev, "ASoC: can't connect %s\n",
1418 } else if (err == 0) /* already connected */
1422 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1426 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1431 * Find the corresponding BE DAIs that source or sink audio to this
1434 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1435 int stream, struct snd_soc_dapm_widget_list **list, int new)
1438 return dpcm_add_paths(fe, stream, list);
1440 return dpcm_prune_paths(fe, stream, list);
1443 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1445 struct snd_soc_dpcm *dpcm;
1446 unsigned long flags;
1448 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1449 for_each_dpcm_be(fe, stream, dpcm)
1450 dpcm->be->dpcm[stream].runtime_update =
1451 SND_SOC_DPCM_UPDATE_NO;
1452 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1455 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1458 struct snd_soc_dpcm *dpcm;
1460 /* disable any enabled and non active backends */
1461 for_each_dpcm_be(fe, stream, dpcm) {
1463 struct snd_soc_pcm_runtime *be = dpcm->be;
1464 struct snd_pcm_substream *be_substream =
1465 snd_soc_dpcm_get_substream(be, stream);
1467 if (be->dpcm[stream].users == 0)
1468 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1469 stream ? "capture" : "playback",
1470 be->dpcm[stream].state);
1472 if (--be->dpcm[stream].users != 0)
1475 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1478 soc_pcm_close(be_substream);
1479 be_substream->runtime = NULL;
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1484 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1486 struct snd_soc_dpcm *dpcm;
1489 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1490 for_each_dpcm_be(fe, stream, dpcm) {
1492 struct snd_soc_pcm_runtime *be = dpcm->be;
1493 struct snd_pcm_substream *be_substream =
1494 snd_soc_dpcm_get_substream(be, stream);
1496 if (!be_substream) {
1497 dev_err(be->dev, "ASoC: no backend %s stream\n",
1498 stream ? "capture" : "playback");
1502 /* is this op for this BE ? */
1503 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1506 /* first time the dpcm is open ? */
1507 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1508 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1509 stream ? "capture" : "playback",
1510 be->dpcm[stream].state);
1512 if (be->dpcm[stream].users++ != 0)
1515 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1516 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1519 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1520 stream ? "capture" : "playback", be->dai_link->name);
1522 be_substream->runtime = be->dpcm[stream].runtime;
1523 err = soc_pcm_open(be_substream);
1525 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1526 be->dpcm[stream].users--;
1527 if (be->dpcm[stream].users < 0)
1528 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1529 stream ? "capture" : "playback",
1530 be->dpcm[stream].state);
1532 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1536 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1543 /* disable any enabled and non active backends */
1544 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1545 struct snd_soc_pcm_runtime *be = dpcm->be;
1546 struct snd_pcm_substream *be_substream =
1547 snd_soc_dpcm_get_substream(be, stream);
1549 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1552 if (be->dpcm[stream].users == 0)
1553 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1554 stream ? "capture" : "playback",
1555 be->dpcm[stream].state);
1557 if (--be->dpcm[stream].users != 0)
1560 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1563 soc_pcm_close(be_substream);
1564 be_substream->runtime = NULL;
1565 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1571 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1572 struct snd_soc_pcm_stream *stream)
1574 runtime->hw.rate_min = stream->rate_min;
1575 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1576 runtime->hw.channels_min = stream->channels_min;
1577 runtime->hw.channels_max = stream->channels_max;
1578 if (runtime->hw.formats)
1579 runtime->hw.formats &= stream->formats;
1581 runtime->hw.formats = stream->formats;
1582 runtime->hw.rates = stream->rates;
1585 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1588 struct snd_soc_pcm_runtime *fe = substream->private_data;
1589 struct snd_soc_dpcm *dpcm;
1590 struct snd_soc_dai *dai;
1591 int stream = substream->stream;
1593 if (!fe->dai_link->dpcm_merged_format)
1597 * It returns merged BE codec format
1598 * if FE want to use it (= dpcm_merged_format)
1601 for_each_dpcm_be(fe, stream, dpcm) {
1602 struct snd_soc_pcm_runtime *be = dpcm->be;
1603 struct snd_soc_dai_driver *codec_dai_drv;
1604 struct snd_soc_pcm_stream *codec_stream;
1607 for_each_rtd_codec_dai(be, i, dai) {
1609 * Skip CODECs which don't support the current stream
1610 * type. See soc_pcm_init_runtime_hw() for more details
1612 if (!snd_soc_dai_stream_valid(dai, stream))
1615 codec_dai_drv = dai->driver;
1616 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1617 codec_stream = &codec_dai_drv->playback;
1619 codec_stream = &codec_dai_drv->capture;
1621 *formats &= codec_stream->formats;
1626 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1627 unsigned int *channels_min,
1628 unsigned int *channels_max)
1630 struct snd_soc_pcm_runtime *fe = substream->private_data;
1631 struct snd_soc_dpcm *dpcm;
1632 int stream = substream->stream;
1634 if (!fe->dai_link->dpcm_merged_chan)
1638 * It returns merged BE codec channel;
1639 * if FE want to use it (= dpcm_merged_chan)
1642 for_each_dpcm_be(fe, stream, dpcm) {
1643 struct snd_soc_pcm_runtime *be = dpcm->be;
1644 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1645 struct snd_soc_dai_driver *codec_dai_drv;
1646 struct snd_soc_pcm_stream *codec_stream;
1647 struct snd_soc_pcm_stream *cpu_stream;
1649 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1650 cpu_stream = &cpu_dai_drv->playback;
1652 cpu_stream = &cpu_dai_drv->capture;
1654 *channels_min = max(*channels_min, cpu_stream->channels_min);
1655 *channels_max = min(*channels_max, cpu_stream->channels_max);
1658 * chan min/max cannot be enforced if there are multiple CODEC
1659 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1661 if (be->num_codecs == 1) {
1662 codec_dai_drv = be->codec_dais[0]->driver;
1664 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1665 codec_stream = &codec_dai_drv->playback;
1667 codec_stream = &codec_dai_drv->capture;
1669 *channels_min = max(*channels_min,
1670 codec_stream->channels_min);
1671 *channels_max = min(*channels_max,
1672 codec_stream->channels_max);
1677 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1678 unsigned int *rates,
1679 unsigned int *rate_min,
1680 unsigned int *rate_max)
1682 struct snd_soc_pcm_runtime *fe = substream->private_data;
1683 struct snd_soc_dpcm *dpcm;
1684 int stream = substream->stream;
1686 if (!fe->dai_link->dpcm_merged_rate)
1690 * It returns merged BE codec channel;
1691 * if FE want to use it (= dpcm_merged_chan)
1694 for_each_dpcm_be(fe, stream, dpcm) {
1695 struct snd_soc_pcm_runtime *be = dpcm->be;
1696 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1697 struct snd_soc_dai_driver *codec_dai_drv;
1698 struct snd_soc_pcm_stream *codec_stream;
1699 struct snd_soc_pcm_stream *cpu_stream;
1700 struct snd_soc_dai *dai;
1703 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1704 cpu_stream = &cpu_dai_drv->playback;
1706 cpu_stream = &cpu_dai_drv->capture;
1708 *rate_min = max(*rate_min, cpu_stream->rate_min);
1709 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1710 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1712 for_each_rtd_codec_dai(be, i, dai) {
1714 * Skip CODECs which don't support the current stream
1715 * type. See soc_pcm_init_runtime_hw() for more details
1717 if (!snd_soc_dai_stream_valid(dai, stream))
1720 codec_dai_drv = dai->driver;
1721 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1722 codec_stream = &codec_dai_drv->playback;
1724 codec_stream = &codec_dai_drv->capture;
1726 *rate_min = max(*rate_min, codec_stream->rate_min);
1727 *rate_max = min_not_zero(*rate_max,
1728 codec_stream->rate_max);
1729 *rates = snd_pcm_rate_mask_intersect(*rates,
1730 codec_stream->rates);
1735 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1737 struct snd_pcm_runtime *runtime = substream->runtime;
1738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1739 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1740 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1742 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1743 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1745 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1747 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1748 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1749 &runtime->hw.channels_max);
1750 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1751 &runtime->hw.rate_min, &runtime->hw.rate_max);
1754 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1756 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1757 * for avoiding the race with trigger callback.
1758 * If the state is unset and a trigger is pending while the previous operation,
1759 * process the pending trigger action here.
1761 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1762 int stream, enum snd_soc_dpcm_update state)
1764 struct snd_pcm_substream *substream =
1765 snd_soc_dpcm_get_substream(fe, stream);
1767 snd_pcm_stream_lock_irq(substream);
1768 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1769 dpcm_fe_dai_do_trigger(substream,
1770 fe->dpcm[stream].trigger_pending - 1);
1771 fe->dpcm[stream].trigger_pending = 0;
1773 fe->dpcm[stream].runtime_update = state;
1774 snd_pcm_stream_unlock_irq(substream);
1777 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1780 struct snd_soc_dpcm *dpcm;
1781 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1782 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1785 /* apply symmetry for FE */
1786 if (soc_pcm_has_symmetry(fe_substream))
1787 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1789 /* Symmetry only applies if we've got an active stream. */
1790 if (fe_cpu_dai->active) {
1791 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1796 /* apply symmetry for BE */
1797 for_each_dpcm_be(fe, stream, dpcm) {
1798 struct snd_soc_pcm_runtime *be = dpcm->be;
1799 struct snd_pcm_substream *be_substream =
1800 snd_soc_dpcm_get_substream(be, stream);
1801 struct snd_soc_pcm_runtime *rtd;
1802 struct snd_soc_dai *codec_dai;
1805 /* A backend may not have the requested substream */
1809 rtd = be_substream->private_data;
1810 if (rtd->dai_link->be_hw_params_fixup)
1813 if (soc_pcm_has_symmetry(be_substream))
1814 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1816 /* Symmetry only applies if we've got an active stream. */
1817 if (rtd->cpu_dai->active) {
1818 err = soc_pcm_apply_symmetry(fe_substream,
1824 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1825 if (codec_dai->active) {
1826 err = soc_pcm_apply_symmetry(fe_substream,
1837 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1839 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1840 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1841 int stream = fe_substream->stream, ret = 0;
1843 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1845 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1847 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1851 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1853 /* start the DAI frontend */
1854 ret = soc_pcm_open(fe_substream);
1856 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1860 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1862 dpcm_set_fe_runtime(fe_substream);
1863 snd_pcm_limit_hw_rates(runtime);
1865 ret = dpcm_apply_symmetry(fe_substream, stream);
1867 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1872 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1876 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1878 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1882 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1884 struct snd_soc_dpcm *dpcm;
1886 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1887 for_each_dpcm_be(fe, stream, dpcm) {
1889 struct snd_soc_pcm_runtime *be = dpcm->be;
1890 struct snd_pcm_substream *be_substream =
1891 snd_soc_dpcm_get_substream(be, stream);
1893 /* is this op for this BE ? */
1894 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1897 if (be->dpcm[stream].users == 0)
1898 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1899 stream ? "capture" : "playback",
1900 be->dpcm[stream].state);
1902 if (--be->dpcm[stream].users != 0)
1905 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1906 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1907 soc_pcm_hw_free(be_substream);
1908 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1911 dev_dbg(be->dev, "ASoC: close BE %s\n",
1912 be->dai_link->name);
1914 soc_pcm_close(be_substream);
1915 be_substream->runtime = NULL;
1917 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1922 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1924 struct snd_soc_pcm_runtime *fe = substream->private_data;
1925 int stream = substream->stream;
1927 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1929 /* shutdown the BEs */
1930 dpcm_be_dai_shutdown(fe, substream->stream);
1932 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1934 /* now shutdown the frontend */
1935 soc_pcm_close(substream);
1937 /* run the stream event for each BE */
1938 snd_soc_dapm_stream_stop(fe, stream);
1940 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1941 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1945 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1947 struct snd_soc_dpcm *dpcm;
1949 /* only hw_params backends that are either sinks or sources
1950 * to this frontend DAI */
1951 for_each_dpcm_be(fe, stream, dpcm) {
1953 struct snd_soc_pcm_runtime *be = dpcm->be;
1954 struct snd_pcm_substream *be_substream =
1955 snd_soc_dpcm_get_substream(be, stream);
1957 /* is this op for this BE ? */
1958 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1961 /* only free hw when no longer used - check all FEs */
1962 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1965 /* do not free hw if this BE is used by other FE */
1966 if (be->dpcm[stream].users > 1)
1969 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1970 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1971 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1972 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1973 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1974 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1977 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1978 be->dai_link->name);
1980 soc_pcm_hw_free(be_substream);
1982 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1988 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1990 struct snd_soc_pcm_runtime *fe = substream->private_data;
1991 int err, stream = substream->stream;
1993 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1994 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1996 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1998 /* call hw_free on the frontend */
1999 err = soc_pcm_hw_free(substream);
2001 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2002 fe->dai_link->name);
2004 /* only hw_params backends that are either sinks or sources
2005 * to this frontend DAI */
2006 err = dpcm_be_dai_hw_free(fe, stream);
2008 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2009 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2011 mutex_unlock(&fe->card->mutex);
2015 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2017 struct snd_soc_dpcm *dpcm;
2020 for_each_dpcm_be(fe, stream, dpcm) {
2022 struct snd_soc_pcm_runtime *be = dpcm->be;
2023 struct snd_pcm_substream *be_substream =
2024 snd_soc_dpcm_get_substream(be, stream);
2026 /* is this op for this BE ? */
2027 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2030 /* copy params for each dpcm */
2031 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2032 sizeof(struct snd_pcm_hw_params));
2034 /* perform any hw_params fixups */
2035 if (be->dai_link->be_hw_params_fixup) {
2036 ret = be->dai_link->be_hw_params_fixup(be,
2040 "ASoC: hw_params BE fixup failed %d\n",
2046 /* copy the fixed-up hw params for BE dai */
2047 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2048 sizeof(struct snd_pcm_hw_params));
2050 /* only allow hw_params() if no connected FEs are running */
2051 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2054 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2055 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2056 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2059 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2060 be->dai_link->name);
2062 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2064 dev_err(dpcm->be->dev,
2065 "ASoC: hw_params BE failed %d\n", ret);
2069 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2074 /* disable any enabled and non active backends */
2075 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2076 struct snd_soc_pcm_runtime *be = dpcm->be;
2077 struct snd_pcm_substream *be_substream =
2078 snd_soc_dpcm_get_substream(be, stream);
2080 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2083 /* only allow hw_free() if no connected FEs are running */
2084 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2087 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2088 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2089 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2090 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2093 soc_pcm_hw_free(be_substream);
2099 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2100 struct snd_pcm_hw_params *params)
2102 struct snd_soc_pcm_runtime *fe = substream->private_data;
2103 int ret, stream = substream->stream;
2105 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2106 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2108 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2109 sizeof(struct snd_pcm_hw_params));
2110 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2112 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2116 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2117 fe->dai_link->name, params_rate(params),
2118 params_channels(params), params_format(params));
2120 /* call hw_params on the frontend */
2121 ret = soc_pcm_hw_params(substream, params);
2123 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2124 dpcm_be_dai_hw_free(fe, stream);
2126 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2129 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2130 mutex_unlock(&fe->card->mutex);
2134 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2135 struct snd_pcm_substream *substream, int cmd)
2139 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2140 dpcm->be->dai_link->name, cmd);
2142 ret = soc_pcm_trigger(substream, cmd);
2144 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2149 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2152 struct snd_soc_dpcm *dpcm;
2155 for_each_dpcm_be(fe, stream, dpcm) {
2157 struct snd_soc_pcm_runtime *be = dpcm->be;
2158 struct snd_pcm_substream *be_substream =
2159 snd_soc_dpcm_get_substream(be, stream);
2161 /* is this op for this BE ? */
2162 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2166 case SNDRV_PCM_TRIGGER_START:
2167 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2168 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2171 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2175 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2177 case SNDRV_PCM_TRIGGER_RESUME:
2178 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2181 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2185 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2187 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2188 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2191 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2195 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2197 case SNDRV_PCM_TRIGGER_STOP:
2198 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2201 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2204 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2208 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2210 case SNDRV_PCM_TRIGGER_SUSPEND:
2211 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2214 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2217 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2221 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2223 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2224 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2227 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2230 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2234 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2241 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2243 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2244 int cmd, bool fe_first)
2246 struct snd_soc_pcm_runtime *fe = substream->private_data;
2249 /* call trigger on the frontend before the backend. */
2251 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2252 fe->dai_link->name, cmd);
2254 ret = soc_pcm_trigger(substream, cmd);
2258 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2262 /* call trigger on the frontend after the backend. */
2263 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2267 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2268 fe->dai_link->name, cmd);
2270 ret = soc_pcm_trigger(substream, cmd);
2275 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2277 struct snd_soc_pcm_runtime *fe = substream->private_data;
2278 int stream = substream->stream;
2280 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2282 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2285 case SND_SOC_DPCM_TRIGGER_PRE:
2287 case SNDRV_PCM_TRIGGER_START:
2288 case SNDRV_PCM_TRIGGER_RESUME:
2289 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2290 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2292 case SNDRV_PCM_TRIGGER_STOP:
2293 case SNDRV_PCM_TRIGGER_SUSPEND:
2294 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2295 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2302 case SND_SOC_DPCM_TRIGGER_POST:
2304 case SNDRV_PCM_TRIGGER_START:
2305 case SNDRV_PCM_TRIGGER_RESUME:
2306 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2307 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2309 case SNDRV_PCM_TRIGGER_STOP:
2310 case SNDRV_PCM_TRIGGER_SUSPEND:
2311 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2312 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2319 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2320 /* bespoke trigger() - handles both FE and BEs */
2322 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2323 fe->dai_link->name, cmd);
2325 ret = soc_pcm_bespoke_trigger(substream, cmd);
2328 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2329 fe->dai_link->name);
2335 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2341 case SNDRV_PCM_TRIGGER_START:
2342 case SNDRV_PCM_TRIGGER_RESUME:
2343 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2344 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2346 case SNDRV_PCM_TRIGGER_STOP:
2347 case SNDRV_PCM_TRIGGER_SUSPEND:
2348 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2350 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2351 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2356 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2360 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2362 struct snd_soc_pcm_runtime *fe = substream->private_data;
2363 int stream = substream->stream;
2365 /* if FE's runtime_update is already set, we're in race;
2366 * process this trigger later at exit
2368 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2369 fe->dpcm[stream].trigger_pending = cmd + 1;
2370 return 0; /* delayed, assuming it's successful */
2373 /* we're alone, let's trigger */
2374 return dpcm_fe_dai_do_trigger(substream, cmd);
2377 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2379 struct snd_soc_dpcm *dpcm;
2382 for_each_dpcm_be(fe, stream, dpcm) {
2384 struct snd_soc_pcm_runtime *be = dpcm->be;
2385 struct snd_pcm_substream *be_substream =
2386 snd_soc_dpcm_get_substream(be, stream);
2388 /* is this op for this BE ? */
2389 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2392 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2393 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2394 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2395 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2398 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2399 be->dai_link->name);
2401 ret = soc_pcm_prepare(be_substream);
2403 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2408 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2413 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2415 struct snd_soc_pcm_runtime *fe = substream->private_data;
2416 int stream = substream->stream, ret = 0;
2418 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2420 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2422 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2424 /* there is no point preparing this FE if there are no BEs */
2425 if (list_empty(&fe->dpcm[stream].be_clients)) {
2426 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2427 fe->dai_link->name);
2432 ret = dpcm_be_dai_prepare(fe, substream->stream);
2436 /* call prepare on the frontend */
2437 ret = soc_pcm_prepare(substream);
2439 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2440 fe->dai_link->name);
2444 /* run the stream event for each BE */
2445 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2446 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2449 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2450 mutex_unlock(&fe->card->mutex);
2455 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2457 struct snd_pcm_substream *substream =
2458 snd_soc_dpcm_get_substream(fe, stream);
2459 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2462 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2463 stream ? "capture" : "playback", fe->dai_link->name);
2465 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2466 /* call bespoke trigger - FE takes care of all BE triggers */
2467 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2468 fe->dai_link->name);
2470 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2472 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2474 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2475 fe->dai_link->name);
2477 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2479 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2482 err = dpcm_be_dai_hw_free(fe, stream);
2484 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2486 err = dpcm_be_dai_shutdown(fe, stream);
2488 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2490 /* run the stream event for each BE */
2491 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2496 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2498 struct snd_pcm_substream *substream =
2499 snd_soc_dpcm_get_substream(fe, stream);
2500 struct snd_soc_dpcm *dpcm;
2501 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2503 unsigned long flags;
2505 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2506 stream ? "capture" : "playback", fe->dai_link->name);
2508 /* Only start the BE if the FE is ready */
2509 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2510 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2513 /* startup must always be called for new BEs */
2514 ret = dpcm_be_dai_startup(fe, stream);
2518 /* keep going if FE state is > open */
2519 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2522 ret = dpcm_be_dai_hw_params(fe, stream);
2526 /* keep going if FE state is > hw_params */
2527 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2531 ret = dpcm_be_dai_prepare(fe, stream);
2535 /* run the stream event for each BE */
2536 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2538 /* keep going if FE state is > prepare */
2539 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2540 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2543 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2544 /* call trigger on the frontend - FE takes care of all BE triggers */
2545 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2546 fe->dai_link->name);
2548 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2550 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2554 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2555 fe->dai_link->name);
2557 ret = dpcm_be_dai_trigger(fe, stream,
2558 SNDRV_PCM_TRIGGER_START);
2560 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2568 dpcm_be_dai_hw_free(fe, stream);
2570 dpcm_be_dai_shutdown(fe, stream);
2572 /* disconnect any non started BEs */
2573 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2574 for_each_dpcm_be(fe, stream, dpcm) {
2575 struct snd_soc_pcm_runtime *be = dpcm->be;
2576 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2577 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2579 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2584 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2588 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2589 ret = dpcm_run_update_startup(fe, stream);
2591 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2592 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2597 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2601 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2602 ret = dpcm_run_update_shutdown(fe, stream);
2604 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2605 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2610 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2612 struct snd_soc_dapm_widget_list *list;
2616 if (!fe->dai_link->dynamic)
2619 /* only check active links */
2620 if (!fe->cpu_dai->active)
2623 /* DAPM sync will call this to update DSP paths */
2624 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2625 new ? "new" : "old", fe->dai_link->name);
2627 for_each_pcm_streams(stream) {
2629 /* skip if FE doesn't have playback/capture capability */
2630 if (!snd_soc_dai_stream_valid(fe->cpu_dai, stream) ||
2631 !snd_soc_dai_stream_valid(fe->codec_dai, stream))
2634 /* skip if FE isn't currently playing/capturing */
2635 if (!fe->cpu_dai->stream_active[stream] ||
2636 !fe->codec_dai->stream_active[stream])
2639 paths = dpcm_path_get(fe, stream, &list);
2641 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2643 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2644 "playback" : "capture");
2648 /* update any playback/capture paths */
2649 count = dpcm_process_paths(fe, stream, &list, new);
2652 dpcm_run_new_update(fe, stream);
2654 dpcm_run_old_update(fe, stream);
2656 dpcm_clear_pending_state(fe, stream);
2657 dpcm_be_disconnect(fe, stream);
2660 dpcm_path_put(&list);
2666 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2669 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2671 struct snd_soc_pcm_runtime *fe;
2674 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2675 /* shutdown all old paths first */
2676 for_each_card_rtds(card, fe) {
2677 ret = soc_dpcm_fe_runtime_update(fe, 0);
2682 /* bring new paths up */
2683 for_each_card_rtds(card, fe) {
2684 ret = soc_dpcm_fe_runtime_update(fe, 1);
2690 mutex_unlock(&card->mutex);
2694 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2696 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2697 struct snd_soc_dpcm *dpcm;
2698 struct snd_soc_dapm_widget_list *list;
2700 int stream = fe_substream->stream;
2702 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2703 fe->dpcm[stream].runtime = fe_substream->runtime;
2705 ret = dpcm_path_get(fe, stream, &list);
2708 } else if (ret == 0) {
2709 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2710 fe->dai_link->name, stream ? "capture" : "playback");
2713 /* calculate valid and active FE <-> BE dpcms */
2714 dpcm_process_paths(fe, stream, &list, 1);
2716 ret = dpcm_fe_dai_startup(fe_substream);
2718 /* clean up all links */
2719 for_each_dpcm_be(fe, stream, dpcm)
2720 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2722 dpcm_be_disconnect(fe, stream);
2723 fe->dpcm[stream].runtime = NULL;
2726 dpcm_clear_pending_state(fe, stream);
2727 dpcm_path_put(&list);
2729 mutex_unlock(&fe->card->mutex);
2733 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2735 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2736 struct snd_soc_dpcm *dpcm;
2737 int stream = fe_substream->stream, ret;
2739 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2740 ret = dpcm_fe_dai_shutdown(fe_substream);
2742 /* mark FE's links ready to prune */
2743 for_each_dpcm_be(fe, stream, dpcm)
2744 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2746 dpcm_be_disconnect(fe, stream);
2748 fe->dpcm[stream].runtime = NULL;
2749 mutex_unlock(&fe->card->mutex);
2753 /* create a new pcm */
2754 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2756 struct snd_soc_dai *codec_dai;
2757 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2758 struct snd_soc_component *component;
2759 struct snd_pcm *pcm;
2761 int ret = 0, playback = 0, capture = 0;
2764 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2765 playback = rtd->dai_link->dpcm_playback;
2766 capture = rtd->dai_link->dpcm_capture;
2768 /* Adapt stream for codec2codec links */
2769 int cpu_capture = rtd->dai_link->params ?
2770 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2771 int cpu_playback = rtd->dai_link->params ?
2772 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2774 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2775 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2776 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2778 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2779 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2784 if (rtd->dai_link->playback_only) {
2789 if (rtd->dai_link->capture_only) {
2794 /* create the PCM */
2795 if (rtd->dai_link->params) {
2796 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2797 rtd->dai_link->stream_name);
2799 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2800 playback, capture, &pcm);
2801 } else if (rtd->dai_link->no_pcm) {
2802 snprintf(new_name, sizeof(new_name), "(%s)",
2803 rtd->dai_link->stream_name);
2805 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2806 playback, capture, &pcm);
2808 if (rtd->dai_link->dynamic)
2809 snprintf(new_name, sizeof(new_name), "%s (*)",
2810 rtd->dai_link->stream_name);
2812 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2813 rtd->dai_link->stream_name,
2814 (rtd->num_codecs > 1) ?
2815 "multicodec" : rtd->codec_dai->name, num);
2817 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2821 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2822 rtd->dai_link->name);
2825 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2827 /* DAPM dai link stream work */
2828 if (rtd->dai_link->params)
2829 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2831 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2833 pcm->nonatomic = rtd->dai_link->nonatomic;
2835 pcm->private_data = rtd;
2837 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2839 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2841 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2845 /* ASoC PCM operations */
2846 if (rtd->dai_link->dynamic) {
2847 rtd->ops.open = dpcm_fe_dai_open;
2848 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2849 rtd->ops.prepare = dpcm_fe_dai_prepare;
2850 rtd->ops.trigger = dpcm_fe_dai_trigger;
2851 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2852 rtd->ops.close = dpcm_fe_dai_close;
2853 rtd->ops.pointer = soc_pcm_pointer;
2855 rtd->ops.open = soc_pcm_open;
2856 rtd->ops.hw_params = soc_pcm_hw_params;
2857 rtd->ops.prepare = soc_pcm_prepare;
2858 rtd->ops.trigger = soc_pcm_trigger;
2859 rtd->ops.hw_free = soc_pcm_hw_free;
2860 rtd->ops.close = soc_pcm_close;
2861 rtd->ops.pointer = soc_pcm_pointer;
2864 for_each_rtd_components(rtd, i, component) {
2865 const struct snd_soc_component_driver *drv = component->driver;
2868 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2870 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2872 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2874 rtd->ops.page = snd_soc_pcm_component_page;
2876 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2880 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2883 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2885 ret = snd_soc_pcm_component_new(rtd);
2887 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
2891 pcm->no_device_suspend = true;
2893 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2894 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2899 /* is the current PCM operation for this FE ? */
2900 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2902 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2906 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2908 /* is the current PCM operation for this BE ? */
2909 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2910 struct snd_soc_pcm_runtime *be, int stream)
2912 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2913 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2914 be->dpcm[stream].runtime_update))
2918 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2920 /* get the substream for this BE */
2921 struct snd_pcm_substream *
2922 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2924 return be->pcm->streams[stream].substream;
2926 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2928 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2929 struct snd_soc_pcm_runtime *be,
2931 const enum snd_soc_dpcm_state *states,
2934 struct snd_soc_dpcm *dpcm;
2937 unsigned long flags;
2940 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2941 for_each_dpcm_fe(be, stream, dpcm) {
2946 state = dpcm->fe->dpcm[stream].state;
2947 for (i = 0; i < num_states; i++) {
2948 if (state == states[i]) {
2954 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2956 /* it's safe to do this BE DAI */
2961 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2962 * are not running, paused or suspended for the specified stream direction.
2964 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2965 struct snd_soc_pcm_runtime *be, int stream)
2967 const enum snd_soc_dpcm_state state[] = {
2968 SND_SOC_DPCM_STATE_START,
2969 SND_SOC_DPCM_STATE_PAUSED,
2970 SND_SOC_DPCM_STATE_SUSPEND,
2973 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2975 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2978 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2979 * running, paused or suspended for the specified stream direction.
2981 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2982 struct snd_soc_pcm_runtime *be, int stream)
2984 const enum snd_soc_dpcm_state state[] = {
2985 SND_SOC_DPCM_STATE_START,
2986 SND_SOC_DPCM_STATE_PAUSED,
2987 SND_SOC_DPCM_STATE_SUSPEND,
2988 SND_SOC_DPCM_STATE_PREPARE,
2991 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2993 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2995 #ifdef CONFIG_DEBUG_FS
2996 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
2999 case SND_SOC_DPCM_STATE_NEW:
3001 case SND_SOC_DPCM_STATE_OPEN:
3003 case SND_SOC_DPCM_STATE_HW_PARAMS:
3005 case SND_SOC_DPCM_STATE_PREPARE:
3007 case SND_SOC_DPCM_STATE_START:
3009 case SND_SOC_DPCM_STATE_STOP:
3011 case SND_SOC_DPCM_STATE_SUSPEND:
3013 case SND_SOC_DPCM_STATE_PAUSED:
3015 case SND_SOC_DPCM_STATE_HW_FREE:
3017 case SND_SOC_DPCM_STATE_CLOSE:
3024 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3025 int stream, char *buf, size_t size)
3027 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3028 struct snd_soc_dpcm *dpcm;
3030 unsigned long flags;
3033 offset += snprintf(buf + offset, size - offset,
3034 "[%s - %s]\n", fe->dai_link->name,
3035 stream ? "Capture" : "Playback");
3037 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3038 dpcm_state_string(fe->dpcm[stream].state));
3040 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3041 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3042 offset += snprintf(buf + offset, size - offset,
3044 "Format = %s, Channels = %d, Rate = %d\n",
3045 snd_pcm_format_name(params_format(params)),
3046 params_channels(params),
3047 params_rate(params));
3050 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3052 if (list_empty(&fe->dpcm[stream].be_clients)) {
3053 offset += snprintf(buf + offset, size - offset,
3054 " No active DSP links\n");
3058 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3059 for_each_dpcm_be(fe, stream, dpcm) {
3060 struct snd_soc_pcm_runtime *be = dpcm->be;
3061 params = &dpcm->hw_params;
3063 offset += snprintf(buf + offset, size - offset,
3064 "- %s\n", be->dai_link->name);
3066 offset += snprintf(buf + offset, size - offset,
3068 dpcm_state_string(be->dpcm[stream].state));
3070 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3071 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3072 offset += snprintf(buf + offset, size - offset,
3073 " Hardware Params: "
3074 "Format = %s, Channels = %d, Rate = %d\n",
3075 snd_pcm_format_name(params_format(params)),
3076 params_channels(params),
3077 params_rate(params));
3079 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3084 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3085 size_t count, loff_t *ppos)
3087 struct snd_soc_pcm_runtime *fe = file->private_data;
3088 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3092 buf = kmalloc(out_count, GFP_KERNEL);
3096 for_each_pcm_streams(stream)
3097 if (snd_soc_dai_stream_valid(fe->cpu_dai, stream))
3098 offset += dpcm_show_state(fe, stream,
3100 out_count - offset);
3102 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3108 static const struct file_operations dpcm_state_fops = {
3109 .open = simple_open,
3110 .read = dpcm_state_read_file,
3111 .llseek = default_llseek,
3114 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3119 if (!rtd->dai_link->dynamic)
3122 if (!rtd->card->debugfs_card_root)
3125 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3126 rtd->card->debugfs_card_root);
3128 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3129 rtd, &dpcm_state_fops);