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_rtdcom_list *rtdcom;
115 struct snd_soc_component *component;
118 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
121 for_each_rtdcom(rtd, rtdcom) {
122 component = rtdcom->component;
124 ignore &= !component->driver->use_pmdown_time;
131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
135 * Sets the substream runtime hardware parameters.
137 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
151 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
153 /* DPCM stream event, send event to FE and all active BEs. */
154 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
157 struct snd_soc_dpcm *dpcm;
159 for_each_dpcm_be(fe, dir, dpcm) {
161 struct snd_soc_pcm_runtime *be = dpcm->be;
163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
164 be->dai_link->name, event, dir);
166 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
167 (be->dpcm[dir].users >= 1))
170 snd_soc_dapm_stream_event(be, dir, event);
173 snd_soc_dapm_stream_event(fe, dir, event);
178 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
179 struct snd_soc_dai *soc_dai)
181 struct snd_soc_pcm_runtime *rtd = substream->private_data;
184 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
185 rtd->dai_link->symmetric_rates)) {
186 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
189 ret = snd_pcm_hw_constraint_single(substream->runtime,
190 SNDRV_PCM_HW_PARAM_RATE,
193 dev_err(soc_dai->dev,
194 "ASoC: Unable to apply rate constraint: %d\n",
200 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
201 rtd->dai_link->symmetric_channels)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
205 ret = snd_pcm_hw_constraint_single(substream->runtime,
206 SNDRV_PCM_HW_PARAM_CHANNELS,
209 dev_err(soc_dai->dev,
210 "ASoC: Unable to apply channel symmetry constraint: %d\n",
216 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
217 rtd->dai_link->symmetric_samplebits)) {
218 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
219 soc_dai->sample_bits);
221 ret = snd_pcm_hw_constraint_single(substream->runtime,
222 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
223 soc_dai->sample_bits);
225 dev_err(soc_dai->dev,
226 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
235 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
236 struct snd_pcm_hw_params *params)
238 struct snd_soc_pcm_runtime *rtd = substream->private_data;
239 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
240 struct snd_soc_dai *codec_dai;
241 unsigned int rate, channels, sample_bits, symmetry, i;
243 rate = params_rate(params);
244 channels = params_channels(params);
245 sample_bits = snd_pcm_format_physical_width(params_format(params));
247 /* reject unmatched parameters when applying symmetry */
248 symmetry = cpu_dai->driver->symmetric_rates ||
249 rtd->dai_link->symmetric_rates;
251 for_each_rtd_codec_dai(rtd, i, codec_dai)
252 symmetry |= codec_dai->driver->symmetric_rates;
254 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
255 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
256 cpu_dai->rate, rate);
260 symmetry = cpu_dai->driver->symmetric_channels ||
261 rtd->dai_link->symmetric_channels;
263 for_each_rtd_codec_dai(rtd, i, codec_dai)
264 symmetry |= codec_dai->driver->symmetric_channels;
266 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
267 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
268 cpu_dai->channels, channels);
272 symmetry = cpu_dai->driver->symmetric_samplebits ||
273 rtd->dai_link->symmetric_samplebits;
275 for_each_rtd_codec_dai(rtd, i, codec_dai)
276 symmetry |= codec_dai->driver->symmetric_samplebits;
278 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
279 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
280 cpu_dai->sample_bits, sample_bits);
287 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
289 struct snd_soc_pcm_runtime *rtd = substream->private_data;
290 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
291 struct snd_soc_dai_link *link = rtd->dai_link;
292 struct snd_soc_dai *codec_dai;
293 unsigned int symmetry, i;
295 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
296 cpu_driver->symmetric_channels || link->symmetric_channels ||
297 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
299 for_each_rtd_codec_dai(rtd, i, codec_dai)
300 symmetry = symmetry ||
301 codec_dai->driver->symmetric_rates ||
302 codec_dai->driver->symmetric_channels ||
303 codec_dai->driver->symmetric_samplebits;
308 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
316 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
318 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
322 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
325 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
326 struct snd_soc_dai *codec_dai;
328 unsigned int bits = 0, cpu_bits;
330 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
331 for_each_rtd_codec_dai(rtd, i, codec_dai) {
332 if (codec_dai->driver->playback.sig_bits == 0) {
336 bits = max(codec_dai->driver->playback.sig_bits, bits);
338 cpu_bits = cpu_dai->driver->playback.sig_bits;
340 for_each_rtd_codec_dai(rtd, i, codec_dai) {
341 if (codec_dai->driver->capture.sig_bits == 0) {
345 bits = max(codec_dai->driver->capture.sig_bits, bits);
347 cpu_bits = cpu_dai->driver->capture.sig_bits;
350 soc_pcm_set_msb(substream, bits);
351 soc_pcm_set_msb(substream, cpu_bits);
354 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
356 struct snd_pcm_runtime *runtime = substream->runtime;
357 struct snd_pcm_hardware *hw = &runtime->hw;
358 struct snd_soc_pcm_runtime *rtd = substream->private_data;
359 struct snd_soc_dai *codec_dai;
360 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
361 struct snd_soc_dai_driver *codec_dai_drv;
362 struct snd_soc_pcm_stream *codec_stream;
363 struct snd_soc_pcm_stream *cpu_stream;
364 unsigned int chan_min = 0, chan_max = UINT_MAX;
365 unsigned int rate_min = 0, rate_max = UINT_MAX;
366 unsigned int rates = UINT_MAX;
367 u64 formats = ULLONG_MAX;
370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
371 cpu_stream = &cpu_dai_drv->playback;
373 cpu_stream = &cpu_dai_drv->capture;
375 /* first calculate min/max only for CODECs in the DAI link */
376 for_each_rtd_codec_dai(rtd, i, codec_dai) {
379 * Skip CODECs which don't support the current stream type.
380 * Otherwise, since the rate, channel, and format values will
381 * zero in that case, we would have no usable settings left,
382 * causing the resulting setup to fail.
383 * At least one CODEC should match, otherwise we should have
384 * bailed out on a higher level, since there would be no
385 * CODEC to support the transfer direction in that case.
387 if (!snd_soc_dai_stream_valid(codec_dai,
391 codec_dai_drv = codec_dai->driver;
392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
393 codec_stream = &codec_dai_drv->playback;
395 codec_stream = &codec_dai_drv->capture;
396 chan_min = max(chan_min, codec_stream->channels_min);
397 chan_max = min(chan_max, codec_stream->channels_max);
398 rate_min = max(rate_min, codec_stream->rate_min);
399 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
400 formats &= codec_stream->formats;
401 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
405 * chan min/max cannot be enforced if there are multiple CODEC DAIs
406 * connected to a single CPU DAI, use CPU DAI's directly and let
407 * channel allocation be fixed up later
409 if (rtd->num_codecs > 1) {
410 chan_min = cpu_stream->channels_min;
411 chan_max = cpu_stream->channels_max;
414 hw->channels_min = max(chan_min, cpu_stream->channels_min);
415 hw->channels_max = min(chan_max, cpu_stream->channels_max);
417 hw->formats &= formats & cpu_stream->formats;
419 hw->formats = formats & cpu_stream->formats;
420 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
422 snd_pcm_limit_hw_rates(runtime);
424 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
425 hw->rate_min = max(hw->rate_min, rate_min);
426 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
427 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
430 static int soc_pcm_components_open(struct snd_pcm_substream *substream,
431 struct snd_soc_component **last)
433 struct snd_soc_pcm_runtime *rtd = substream->private_data;
434 struct snd_soc_rtdcom_list *rtdcom;
435 struct snd_soc_component *component;
438 for_each_rtdcom(rtd, rtdcom) {
439 component = rtdcom->component;
442 ret = snd_soc_component_module_get_when_open(component);
444 dev_err(component->dev,
445 "ASoC: can't get module %s\n",
450 ret = snd_soc_component_open(component, substream);
452 dev_err(component->dev,
453 "ASoC: can't open component %s: %d\n",
454 component->name, ret);
462 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
463 struct snd_soc_component *last)
465 struct snd_soc_pcm_runtime *rtd = substream->private_data;
466 struct snd_soc_rtdcom_list *rtdcom;
467 struct snd_soc_component *component;
470 for_each_rtdcom(rtd, rtdcom) {
471 component = rtdcom->component;
473 if (component == last)
476 ret |= snd_soc_component_close(component, substream);
477 snd_soc_component_module_put_when_close(component);
484 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
485 * then initialized and any private data can be allocated. This also calls
486 * startup for the cpu DAI, component, machine and codec DAI.
488 static int soc_pcm_open(struct snd_pcm_substream *substream)
490 struct snd_soc_pcm_runtime *rtd = substream->private_data;
491 struct snd_pcm_runtime *runtime = substream->runtime;
492 struct snd_soc_component *component;
493 struct snd_soc_rtdcom_list *rtdcom;
494 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
495 struct snd_soc_dai *codec_dai;
496 const char *codec_dai_name = "multicodec";
499 pinctrl_pm_select_default_state(cpu_dai->dev);
500 for_each_rtd_codec_dai(rtd, i, codec_dai)
501 pinctrl_pm_select_default_state(codec_dai->dev);
503 for_each_rtdcom(rtd, rtdcom) {
504 component = rtdcom->component;
506 pm_runtime_get_sync(component->dev);
509 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
511 /* startup the audio subsystem */
512 ret = snd_soc_dai_startup(cpu_dai, substream);
514 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
519 ret = soc_pcm_components_open(substream, &component);
523 for_each_rtd_codec_dai(rtd, i, codec_dai) {
524 ret = snd_soc_dai_startup(codec_dai, substream);
526 dev_err(codec_dai->dev,
527 "ASoC: can't open codec %s: %d\n",
528 codec_dai->name, ret);
532 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
533 codec_dai->tx_mask = 0;
535 codec_dai->rx_mask = 0;
538 if (rtd->dai_link->ops->startup) {
539 ret = rtd->dai_link->ops->startup(substream);
541 pr_err("ASoC: %s startup failed: %d\n",
542 rtd->dai_link->name, ret);
547 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
548 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
551 /* Check that the codec and cpu DAIs are compatible */
552 soc_pcm_init_runtime_hw(substream);
554 if (rtd->num_codecs == 1)
555 codec_dai_name = rtd->codec_dai->name;
557 if (soc_pcm_has_symmetry(substream))
558 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
561 if (!runtime->hw.rates) {
562 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
563 codec_dai_name, cpu_dai->name);
566 if (!runtime->hw.formats) {
567 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
568 codec_dai_name, cpu_dai->name);
571 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
572 runtime->hw.channels_min > runtime->hw.channels_max) {
573 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
574 codec_dai_name, cpu_dai->name);
578 soc_pcm_apply_msb(substream);
580 /* Symmetry only applies if we've already got an active stream. */
581 if (cpu_dai->active) {
582 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
587 for_each_rtd_codec_dai(rtd, i, codec_dai) {
588 if (codec_dai->active) {
589 ret = soc_pcm_apply_symmetry(substream, codec_dai);
595 pr_debug("ASoC: %s <-> %s info:\n",
596 codec_dai_name, cpu_dai->name);
597 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
598 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
599 runtime->hw.channels_max);
600 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
601 runtime->hw.rate_max);
605 snd_soc_runtime_activate(rtd, substream->stream);
607 mutex_unlock(&rtd->card->pcm_mutex);
611 if (rtd->dai_link->ops->shutdown)
612 rtd->dai_link->ops->shutdown(substream);
618 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
619 snd_soc_dai_shutdown(codec_dai, substream);
622 soc_pcm_components_close(substream, component);
624 snd_soc_dai_shutdown(cpu_dai, substream);
626 mutex_unlock(&rtd->card->pcm_mutex);
628 for_each_rtdcom(rtd, rtdcom) {
629 component = rtdcom->component;
631 pm_runtime_mark_last_busy(component->dev);
632 pm_runtime_put_autosuspend(component->dev);
635 for_each_rtd_codec_dai(rtd, i, codec_dai) {
636 if (!codec_dai->active)
637 pinctrl_pm_select_sleep_state(codec_dai->dev);
639 if (!cpu_dai->active)
640 pinctrl_pm_select_sleep_state(cpu_dai->dev);
646 * Power down the audio subsystem pmdown_time msecs after close is called.
647 * This is to ensure there are no pops or clicks in between any music tracks
648 * due to DAPM power cycling.
650 static void close_delayed_work(struct work_struct *work)
652 struct snd_soc_pcm_runtime *rtd =
653 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
654 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
656 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
658 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
659 codec_dai->driver->playback.stream_name,
660 codec_dai->playback_active ? "active" : "inactive",
661 rtd->pop_wait ? "yes" : "no");
663 /* are we waiting on this codec DAI stream */
664 if (rtd->pop_wait == 1) {
666 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
667 SND_SOC_DAPM_STREAM_STOP);
670 mutex_unlock(&rtd->card->pcm_mutex);
673 static void codec2codec_close_delayed_work(struct work_struct *work)
676 * Currently nothing to do for c2c links
677 * Since c2c links are internal nodes in the DAPM graph and
678 * don't interface with the outside world or application layer
679 * we don't have to do any special handling on close.
684 * Called by ALSA when a PCM substream is closed. Private data can be
685 * freed here. The cpu DAI, codec DAI, machine and components are also
688 static int soc_pcm_close(struct snd_pcm_substream *substream)
690 struct snd_soc_pcm_runtime *rtd = substream->private_data;
691 struct snd_soc_component *component;
692 struct snd_soc_rtdcom_list *rtdcom;
693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
694 struct snd_soc_dai *codec_dai;
697 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
699 snd_soc_runtime_deactivate(rtd, substream->stream);
701 /* clear the corresponding DAIs rate when inactive */
702 if (!cpu_dai->active)
705 for_each_rtd_codec_dai(rtd, i, codec_dai) {
706 if (!codec_dai->active)
710 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
712 snd_soc_dai_shutdown(cpu_dai, substream);
714 for_each_rtd_codec_dai(rtd, i, codec_dai)
715 snd_soc_dai_shutdown(codec_dai, substream);
717 if (rtd->dai_link->ops->shutdown)
718 rtd->dai_link->ops->shutdown(substream);
720 soc_pcm_components_close(substream, NULL);
722 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
723 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
724 /* powered down playback stream now */
725 snd_soc_dapm_stream_event(rtd,
726 SNDRV_PCM_STREAM_PLAYBACK,
727 SND_SOC_DAPM_STREAM_STOP);
729 /* start delayed pop wq here for playback streams */
731 queue_delayed_work(system_power_efficient_wq,
733 msecs_to_jiffies(rtd->pmdown_time));
736 /* capture streams can be powered down now */
737 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
738 SND_SOC_DAPM_STREAM_STOP);
741 mutex_unlock(&rtd->card->pcm_mutex);
743 for_each_rtdcom(rtd, rtdcom) {
744 component = rtdcom->component;
746 pm_runtime_mark_last_busy(component->dev);
747 pm_runtime_put_autosuspend(component->dev);
750 for_each_rtd_codec_dai(rtd, i, codec_dai) {
751 if (!codec_dai->active)
752 pinctrl_pm_select_sleep_state(codec_dai->dev);
754 if (!cpu_dai->active)
755 pinctrl_pm_select_sleep_state(cpu_dai->dev);
761 * Called by ALSA when the PCM substream is prepared, can set format, sample
762 * rate, etc. This function is non atomic and can be called multiple times,
763 * it can refer to the runtime info.
765 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
767 struct snd_soc_pcm_runtime *rtd = substream->private_data;
768 struct snd_soc_component *component;
769 struct snd_soc_rtdcom_list *rtdcom;
770 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
771 struct snd_soc_dai *codec_dai;
774 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
776 if (rtd->dai_link->ops->prepare) {
777 ret = rtd->dai_link->ops->prepare(substream);
779 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
785 for_each_rtdcom(rtd, rtdcom) {
786 component = rtdcom->component;
788 ret = snd_soc_component_prepare(component, substream);
790 dev_err(component->dev,
791 "ASoC: platform prepare error: %d\n", ret);
796 for_each_rtd_codec_dai(rtd, i, codec_dai) {
797 ret = snd_soc_dai_prepare(codec_dai, substream);
799 dev_err(codec_dai->dev,
800 "ASoC: codec DAI prepare error: %d\n",
806 ret = snd_soc_dai_prepare(cpu_dai, substream);
808 dev_err(cpu_dai->dev,
809 "ASoC: cpu DAI prepare error: %d\n", ret);
813 /* cancel any delayed stream shutdown that is pending */
814 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
817 cancel_delayed_work(&rtd->delayed_work);
820 snd_soc_dapm_stream_event(rtd, substream->stream,
821 SND_SOC_DAPM_STREAM_START);
823 for_each_rtd_codec_dai(rtd, i, codec_dai)
824 snd_soc_dai_digital_mute(codec_dai, 0,
826 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
829 mutex_unlock(&rtd->card->pcm_mutex);
833 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
836 struct snd_interval *interval;
837 int channels = hweight_long(mask);
839 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
840 interval->min = channels;
841 interval->max = channels;
844 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
845 struct snd_soc_component *last)
847 struct snd_soc_pcm_runtime *rtd = substream->private_data;
848 struct snd_soc_rtdcom_list *rtdcom;
849 struct snd_soc_component *component;
852 for_each_rtdcom(rtd, rtdcom) {
853 component = rtdcom->component;
855 if (component == last)
858 ret |= snd_soc_component_hw_free(component, substream);
865 * Called by ALSA when the hardware params are set by application. This
866 * function can also be called multiple times and can allocate buffers
867 * (using snd_pcm_lib_* ). It's non-atomic.
869 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
870 struct snd_pcm_hw_params *params)
872 struct snd_soc_pcm_runtime *rtd = substream->private_data;
873 struct snd_soc_component *component;
874 struct snd_soc_rtdcom_list *rtdcom;
875 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
876 struct snd_soc_dai *codec_dai;
879 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
881 ret = soc_pcm_params_symmetry(substream, params);
885 if (rtd->dai_link->ops->hw_params) {
886 ret = rtd->dai_link->ops->hw_params(substream, params);
888 dev_err(rtd->card->dev, "ASoC: machine hw_params"
889 " failed: %d\n", ret);
894 for_each_rtd_codec_dai(rtd, i, codec_dai) {
895 struct snd_pcm_hw_params codec_params;
898 * Skip CODECs which don't support the current stream type,
899 * the idea being that if a CODEC is not used for the currently
900 * set up transfer direction, it should not need to be
901 * configured, especially since the configuration used might
902 * not even be supported by that CODEC. There may be cases
903 * however where a CODEC needs to be set up although it is
904 * actually not being used for the transfer, e.g. if a
905 * capture-only CODEC is acting as an LRCLK and/or BCLK master
906 * for the DAI link including a playback-only CODEC.
907 * If this becomes necessary, we will have to augment the
908 * machine driver setup with information on how to act, so
909 * we can do the right thing here.
911 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
914 /* copy params for each codec */
915 codec_params = *params;
917 /* fixup params based on TDM slot masks */
918 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
920 soc_pcm_codec_params_fixup(&codec_params,
923 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
925 soc_pcm_codec_params_fixup(&codec_params,
928 ret = snd_soc_dai_hw_params(codec_dai, substream,
933 codec_dai->rate = params_rate(&codec_params);
934 codec_dai->channels = params_channels(&codec_params);
935 codec_dai->sample_bits = snd_pcm_format_physical_width(
936 params_format(&codec_params));
938 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
941 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
945 /* store the parameters for each DAIs */
946 cpu_dai->rate = params_rate(params);
947 cpu_dai->channels = params_channels(params);
948 cpu_dai->sample_bits =
949 snd_pcm_format_physical_width(params_format(params));
951 snd_soc_dapm_update_dai(substream, params, cpu_dai);
953 for_each_rtdcom(rtd, rtdcom) {
954 component = rtdcom->component;
956 ret = snd_soc_component_hw_params(component, substream, params);
958 dev_err(component->dev,
959 "ASoC: %s hw params failed: %d\n",
960 component->name, ret);
967 mutex_unlock(&rtd->card->pcm_mutex);
971 soc_pcm_components_hw_free(substream, component);
973 snd_soc_dai_hw_free(cpu_dai, substream);
980 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
981 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
984 snd_soc_dai_hw_free(codec_dai, substream);
988 if (rtd->dai_link->ops->hw_free)
989 rtd->dai_link->ops->hw_free(substream);
991 mutex_unlock(&rtd->card->pcm_mutex);
996 * Frees resources allocated by hw_params, can be called multiple times
998 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1000 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1001 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1002 struct snd_soc_dai *codec_dai;
1003 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1006 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
1008 /* clear the corresponding DAIs parameters when going to be inactive */
1009 if (cpu_dai->active == 1) {
1011 cpu_dai->channels = 0;
1012 cpu_dai->sample_bits = 0;
1015 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1016 if (codec_dai->active == 1) {
1017 codec_dai->rate = 0;
1018 codec_dai->channels = 0;
1019 codec_dai->sample_bits = 0;
1023 /* apply codec digital mute */
1024 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1025 if ((playback && codec_dai->playback_active == 1) ||
1026 (!playback && codec_dai->capture_active == 1))
1027 snd_soc_dai_digital_mute(codec_dai, 1,
1031 /* free any machine hw params */
1032 if (rtd->dai_link->ops->hw_free)
1033 rtd->dai_link->ops->hw_free(substream);
1035 /* free any component resources */
1036 soc_pcm_components_hw_free(substream, NULL);
1038 /* now free hw params for the DAIs */
1039 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1040 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1043 snd_soc_dai_hw_free(codec_dai, substream);
1046 snd_soc_dai_hw_free(cpu_dai, substream);
1048 mutex_unlock(&rtd->card->pcm_mutex);
1052 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1054 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1055 struct snd_soc_component *component;
1056 struct snd_soc_rtdcom_list *rtdcom;
1057 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1058 struct snd_soc_dai *codec_dai;
1061 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1062 ret = snd_soc_dai_trigger(codec_dai, substream, cmd);
1067 for_each_rtdcom(rtd, rtdcom) {
1068 component = rtdcom->component;
1070 ret = snd_soc_component_trigger(component, substream, cmd);
1075 ret = snd_soc_dai_trigger(cpu_dai, substream, cmd);
1079 if (rtd->dai_link->ops->trigger) {
1080 ret = rtd->dai_link->ops->trigger(substream, cmd);
1088 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1091 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1092 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1093 struct snd_soc_dai *codec_dai;
1096 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1097 ret = snd_soc_dai_bespoke_trigger(codec_dai, substream, cmd);
1102 ret = snd_soc_dai_bespoke_trigger(cpu_dai, substream, cmd);
1109 * soc level wrapper for pointer callback
1110 * If cpu_dai, codec_dai, component driver has the delay callback, then
1111 * the runtime->delay will be updated accordingly.
1113 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1115 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1116 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1117 struct snd_soc_dai *codec_dai;
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 snd_pcm_uframes_t offset = 0;
1120 snd_pcm_sframes_t delay = 0;
1121 snd_pcm_sframes_t codec_delay = 0;
1124 /* clearing the previous total delay */
1127 offset = snd_soc_pcm_component_pointer(substream);
1129 /* base delay if assigned in pointer callback */
1130 delay = runtime->delay;
1132 delay += snd_soc_dai_delay(cpu_dai, substream);
1134 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1135 codec_delay = max(codec_delay,
1136 snd_soc_dai_delay(codec_dai, substream));
1138 delay += codec_delay;
1140 runtime->delay = delay;
1145 /* connect a FE and BE */
1146 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1147 struct snd_soc_pcm_runtime *be, int stream)
1149 struct snd_soc_dpcm *dpcm;
1150 unsigned long flags;
1151 #ifdef CONFIG_DEBUG_FS
1155 /* only add new dpcms */
1156 for_each_dpcm_be(fe, stream, dpcm) {
1157 if (dpcm->be == be && dpcm->fe == fe)
1161 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1167 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1168 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1169 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1170 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1171 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1172 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1174 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1175 stream ? "capture" : "playback", fe->dai_link->name,
1176 stream ? "<-" : "->", be->dai_link->name);
1178 #ifdef CONFIG_DEBUG_FS
1179 name = kasprintf(GFP_KERNEL, "%s:%s", be->dai_link->name,
1180 stream ? "capture" : "playback");
1182 dpcm->debugfs_state = debugfs_create_dir(name,
1183 fe->debugfs_dpcm_root);
1184 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
1192 /* reparent a BE onto another FE */
1193 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1194 struct snd_soc_pcm_runtime *be, int stream)
1196 struct snd_soc_dpcm *dpcm;
1197 struct snd_pcm_substream *fe_substream, *be_substream;
1199 /* reparent if BE is connected to other FEs */
1200 if (!be->dpcm[stream].users)
1203 be_substream = snd_soc_dpcm_get_substream(be, stream);
1207 for_each_dpcm_fe(be, stream, dpcm) {
1211 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1212 stream ? "capture" : "playback",
1213 dpcm->fe->dai_link->name,
1214 stream ? "<-" : "->", dpcm->be->dai_link->name);
1216 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1217 be_substream->runtime = fe_substream->runtime;
1222 /* disconnect a BE and FE */
1223 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1225 struct snd_soc_dpcm *dpcm, *d;
1226 unsigned long flags;
1228 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1229 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1230 stream ? "capture" : "playback",
1231 dpcm->be->dai_link->name);
1233 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1236 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1237 stream ? "capture" : "playback", fe->dai_link->name,
1238 stream ? "<-" : "->", dpcm->be->dai_link->name);
1240 /* BEs still alive need new FE */
1241 dpcm_be_reparent(fe, dpcm->be, stream);
1243 #ifdef CONFIG_DEBUG_FS
1244 debugfs_remove_recursive(dpcm->debugfs_state);
1246 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1247 list_del(&dpcm->list_be);
1248 list_del(&dpcm->list_fe);
1249 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1254 /* get BE for DAI widget and stream */
1255 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1256 struct snd_soc_dapm_widget *widget, int stream)
1258 struct snd_soc_pcm_runtime *be;
1259 struct snd_soc_dai *dai;
1262 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1264 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1265 for_each_card_rtds(card, be) {
1267 if (!be->dai_link->no_pcm)
1270 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1271 be->cpu_dai->playback_widget ?
1272 be->cpu_dai->playback_widget->name : "(not set)");
1274 if (be->cpu_dai->playback_widget == widget)
1277 for_each_rtd_codec_dai(be, i, dai) {
1278 if (dai->playback_widget == widget)
1284 for_each_card_rtds(card, be) {
1286 if (!be->dai_link->no_pcm)
1289 dev_dbg(card->dev, "ASoC: try BE %s\n",
1290 be->cpu_dai->capture_widget ?
1291 be->cpu_dai->capture_widget->name : "(not set)");
1293 if (be->cpu_dai->capture_widget == widget)
1296 for_each_rtd_codec_dai(be, i, dai) {
1297 if (dai->capture_widget == widget)
1303 /* dai link name and stream name set correctly ? */
1304 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1305 stream ? "capture" : "playback", widget->name);
1309 static inline struct snd_soc_dapm_widget *
1310 dai_get_widget(struct snd_soc_dai *dai, int stream)
1312 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1313 return dai->playback_widget;
1315 return dai->capture_widget;
1318 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1319 struct snd_soc_dapm_widget *widget)
1323 for (i = 0; i < list->num_widgets; i++) {
1324 if (widget == list->widgets[i])
1331 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1332 enum snd_soc_dapm_direction dir)
1334 struct snd_soc_card *card = widget->dapm->card;
1335 struct snd_soc_pcm_runtime *rtd;
1336 struct snd_soc_dai *dai;
1339 if (dir == SND_SOC_DAPM_DIR_OUT) {
1340 for_each_card_rtds(card, rtd) {
1341 if (!rtd->dai_link->no_pcm)
1344 if (rtd->cpu_dai->playback_widget == widget)
1347 for_each_rtd_codec_dai(rtd, i, dai) {
1348 if (dai->playback_widget == widget)
1352 } else { /* SND_SOC_DAPM_DIR_IN */
1353 for_each_card_rtds(card, rtd) {
1354 if (!rtd->dai_link->no_pcm)
1357 if (rtd->cpu_dai->capture_widget == widget)
1360 for_each_rtd_codec_dai(rtd, i, dai) {
1361 if (dai->capture_widget == widget)
1370 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1371 int stream, struct snd_soc_dapm_widget_list **list)
1373 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1376 /* get number of valid DAI paths and their widgets */
1377 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1378 dpcm_end_walk_at_be);
1380 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1381 stream ? "capture" : "playback");
1386 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1387 struct snd_soc_dapm_widget_list **list_)
1389 struct snd_soc_dpcm *dpcm;
1390 struct snd_soc_dapm_widget_list *list = *list_;
1391 struct snd_soc_dapm_widget *widget;
1392 struct snd_soc_dai *dai;
1396 /* Destroy any old FE <--> BE connections */
1397 for_each_dpcm_be(fe, stream, dpcm) {
1400 /* is there a valid CPU DAI widget for this BE */
1401 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1403 /* prune the BE if it's no longer in our active list */
1404 if (widget && widget_in_list(list, widget))
1407 /* is there a valid CODEC DAI widget for this BE */
1409 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1410 widget = dai_get_widget(dai, stream);
1412 /* prune the BE if it's no longer in our active list */
1413 if (widget && widget_in_list(list, widget))
1419 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1420 stream ? "capture" : "playback",
1421 dpcm->be->dai_link->name, fe->dai_link->name);
1422 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1423 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1427 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1431 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1432 struct snd_soc_dapm_widget_list **list_)
1434 struct snd_soc_card *card = fe->card;
1435 struct snd_soc_dapm_widget_list *list = *list_;
1436 struct snd_soc_pcm_runtime *be;
1437 int i, new = 0, err;
1439 /* Create any new FE <--> BE connections */
1440 for (i = 0; i < list->num_widgets; i++) {
1442 switch (list->widgets[i]->id) {
1443 case snd_soc_dapm_dai_in:
1444 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1447 case snd_soc_dapm_dai_out:
1448 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1455 /* is there a valid BE rtd for this widget */
1456 be = dpcm_get_be(card, list->widgets[i], stream);
1458 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1459 list->widgets[i]->name);
1463 /* make sure BE is a real BE */
1464 if (!be->dai_link->no_pcm)
1467 /* don't connect if FE is not running */
1468 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1471 /* newly connected FE and BE */
1472 err = dpcm_be_connect(fe, be, stream);
1474 dev_err(fe->dev, "ASoC: can't connect %s\n",
1475 list->widgets[i]->name);
1477 } else if (err == 0) /* already connected */
1481 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1485 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1490 * Find the corresponding BE DAIs that source or sink audio to this
1493 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1494 int stream, struct snd_soc_dapm_widget_list **list, int new)
1497 return dpcm_add_paths(fe, stream, list);
1499 return dpcm_prune_paths(fe, stream, list);
1502 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1504 struct snd_soc_dpcm *dpcm;
1505 unsigned long flags;
1507 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1508 for_each_dpcm_be(fe, stream, dpcm)
1509 dpcm->be->dpcm[stream].runtime_update =
1510 SND_SOC_DPCM_UPDATE_NO;
1511 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1514 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1517 struct snd_soc_dpcm *dpcm;
1519 /* disable any enabled and non active backends */
1520 for_each_dpcm_be(fe, stream, dpcm) {
1522 struct snd_soc_pcm_runtime *be = dpcm->be;
1523 struct snd_pcm_substream *be_substream =
1524 snd_soc_dpcm_get_substream(be, stream);
1526 if (be->dpcm[stream].users == 0)
1527 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1528 stream ? "capture" : "playback",
1529 be->dpcm[stream].state);
1531 if (--be->dpcm[stream].users != 0)
1534 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1537 soc_pcm_close(be_substream);
1538 be_substream->runtime = NULL;
1539 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1543 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1545 struct snd_soc_dpcm *dpcm;
1548 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1549 for_each_dpcm_be(fe, stream, dpcm) {
1551 struct snd_soc_pcm_runtime *be = dpcm->be;
1552 struct snd_pcm_substream *be_substream =
1553 snd_soc_dpcm_get_substream(be, stream);
1555 if (!be_substream) {
1556 dev_err(be->dev, "ASoC: no backend %s stream\n",
1557 stream ? "capture" : "playback");
1561 /* is this op for this BE ? */
1562 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1565 /* first time the dpcm is open ? */
1566 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1567 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1568 stream ? "capture" : "playback",
1569 be->dpcm[stream].state);
1571 if (be->dpcm[stream].users++ != 0)
1574 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1575 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1578 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1579 stream ? "capture" : "playback", be->dai_link->name);
1581 be_substream->runtime = be->dpcm[stream].runtime;
1582 err = soc_pcm_open(be_substream);
1584 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1585 be->dpcm[stream].users--;
1586 if (be->dpcm[stream].users < 0)
1587 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1588 stream ? "capture" : "playback",
1589 be->dpcm[stream].state);
1591 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1595 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1602 /* disable any enabled and non active backends */
1603 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1604 struct snd_soc_pcm_runtime *be = dpcm->be;
1605 struct snd_pcm_substream *be_substream =
1606 snd_soc_dpcm_get_substream(be, stream);
1608 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1611 if (be->dpcm[stream].users == 0)
1612 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1613 stream ? "capture" : "playback",
1614 be->dpcm[stream].state);
1616 if (--be->dpcm[stream].users != 0)
1619 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1622 soc_pcm_close(be_substream);
1623 be_substream->runtime = NULL;
1624 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1630 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1631 struct snd_soc_pcm_stream *stream)
1633 runtime->hw.rate_min = stream->rate_min;
1634 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1635 runtime->hw.channels_min = stream->channels_min;
1636 runtime->hw.channels_max = stream->channels_max;
1637 if (runtime->hw.formats)
1638 runtime->hw.formats &= stream->formats;
1640 runtime->hw.formats = stream->formats;
1641 runtime->hw.rates = stream->rates;
1644 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1647 struct snd_soc_pcm_runtime *fe = substream->private_data;
1648 struct snd_soc_dpcm *dpcm;
1649 struct snd_soc_dai *dai;
1650 int stream = substream->stream;
1652 if (!fe->dai_link->dpcm_merged_format)
1656 * It returns merged BE codec format
1657 * if FE want to use it (= dpcm_merged_format)
1660 for_each_dpcm_be(fe, stream, dpcm) {
1661 struct snd_soc_pcm_runtime *be = dpcm->be;
1662 struct snd_soc_dai_driver *codec_dai_drv;
1663 struct snd_soc_pcm_stream *codec_stream;
1666 for_each_rtd_codec_dai(be, i, dai) {
1668 * Skip CODECs which don't support the current stream
1669 * type. See soc_pcm_init_runtime_hw() for more details
1671 if (!snd_soc_dai_stream_valid(dai, stream))
1674 codec_dai_drv = dai->driver;
1675 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1676 codec_stream = &codec_dai_drv->playback;
1678 codec_stream = &codec_dai_drv->capture;
1680 *formats &= codec_stream->formats;
1685 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1686 unsigned int *channels_min,
1687 unsigned int *channels_max)
1689 struct snd_soc_pcm_runtime *fe = substream->private_data;
1690 struct snd_soc_dpcm *dpcm;
1691 int stream = substream->stream;
1693 if (!fe->dai_link->dpcm_merged_chan)
1697 * It returns merged BE codec channel;
1698 * if FE want to use it (= dpcm_merged_chan)
1701 for_each_dpcm_be(fe, stream, dpcm) {
1702 struct snd_soc_pcm_runtime *be = dpcm->be;
1703 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1704 struct snd_soc_dai_driver *codec_dai_drv;
1705 struct snd_soc_pcm_stream *codec_stream;
1706 struct snd_soc_pcm_stream *cpu_stream;
1708 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1709 cpu_stream = &cpu_dai_drv->playback;
1711 cpu_stream = &cpu_dai_drv->capture;
1713 *channels_min = max(*channels_min, cpu_stream->channels_min);
1714 *channels_max = min(*channels_max, cpu_stream->channels_max);
1717 * chan min/max cannot be enforced if there are multiple CODEC
1718 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1720 if (be->num_codecs == 1) {
1721 codec_dai_drv = be->codec_dais[0]->driver;
1723 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1724 codec_stream = &codec_dai_drv->playback;
1726 codec_stream = &codec_dai_drv->capture;
1728 *channels_min = max(*channels_min,
1729 codec_stream->channels_min);
1730 *channels_max = min(*channels_max,
1731 codec_stream->channels_max);
1736 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1737 unsigned int *rates,
1738 unsigned int *rate_min,
1739 unsigned int *rate_max)
1741 struct snd_soc_pcm_runtime *fe = substream->private_data;
1742 struct snd_soc_dpcm *dpcm;
1743 int stream = substream->stream;
1745 if (!fe->dai_link->dpcm_merged_rate)
1749 * It returns merged BE codec channel;
1750 * if FE want to use it (= dpcm_merged_chan)
1753 for_each_dpcm_be(fe, stream, dpcm) {
1754 struct snd_soc_pcm_runtime *be = dpcm->be;
1755 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1756 struct snd_soc_dai_driver *codec_dai_drv;
1757 struct snd_soc_pcm_stream *codec_stream;
1758 struct snd_soc_pcm_stream *cpu_stream;
1759 struct snd_soc_dai *dai;
1762 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1763 cpu_stream = &cpu_dai_drv->playback;
1765 cpu_stream = &cpu_dai_drv->capture;
1767 *rate_min = max(*rate_min, cpu_stream->rate_min);
1768 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1769 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1771 for_each_rtd_codec_dai(be, i, dai) {
1773 * Skip CODECs which don't support the current stream
1774 * type. See soc_pcm_init_runtime_hw() for more details
1776 if (!snd_soc_dai_stream_valid(dai, stream))
1779 codec_dai_drv = dai->driver;
1780 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1781 codec_stream = &codec_dai_drv->playback;
1783 codec_stream = &codec_dai_drv->capture;
1785 *rate_min = max(*rate_min, codec_stream->rate_min);
1786 *rate_max = min_not_zero(*rate_max,
1787 codec_stream->rate_max);
1788 *rates = snd_pcm_rate_mask_intersect(*rates,
1789 codec_stream->rates);
1794 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1796 struct snd_pcm_runtime *runtime = substream->runtime;
1797 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1798 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1799 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1801 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1802 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1804 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1806 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1807 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1808 &runtime->hw.channels_max);
1809 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1810 &runtime->hw.rate_min, &runtime->hw.rate_max);
1813 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1815 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1816 * for avoiding the race with trigger callback.
1817 * If the state is unset and a trigger is pending while the previous operation,
1818 * process the pending trigger action here.
1820 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1821 int stream, enum snd_soc_dpcm_update state)
1823 struct snd_pcm_substream *substream =
1824 snd_soc_dpcm_get_substream(fe, stream);
1826 snd_pcm_stream_lock_irq(substream);
1827 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1828 dpcm_fe_dai_do_trigger(substream,
1829 fe->dpcm[stream].trigger_pending - 1);
1830 fe->dpcm[stream].trigger_pending = 0;
1832 fe->dpcm[stream].runtime_update = state;
1833 snd_pcm_stream_unlock_irq(substream);
1836 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1839 struct snd_soc_dpcm *dpcm;
1840 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1841 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1844 /* apply symmetry for FE */
1845 if (soc_pcm_has_symmetry(fe_substream))
1846 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1848 /* Symmetry only applies if we've got an active stream. */
1849 if (fe_cpu_dai->active) {
1850 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1855 /* apply symmetry for BE */
1856 for_each_dpcm_be(fe, stream, dpcm) {
1857 struct snd_soc_pcm_runtime *be = dpcm->be;
1858 struct snd_pcm_substream *be_substream =
1859 snd_soc_dpcm_get_substream(be, stream);
1860 struct snd_soc_pcm_runtime *rtd;
1861 struct snd_soc_dai *codec_dai;
1864 /* A backend may not have the requested substream */
1868 rtd = be_substream->private_data;
1869 if (rtd->dai_link->be_hw_params_fixup)
1872 if (soc_pcm_has_symmetry(be_substream))
1873 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1875 /* Symmetry only applies if we've got an active stream. */
1876 if (rtd->cpu_dai->active) {
1877 err = soc_pcm_apply_symmetry(fe_substream,
1883 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1884 if (codec_dai->active) {
1885 err = soc_pcm_apply_symmetry(fe_substream,
1896 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1898 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1899 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1900 int stream = fe_substream->stream, ret = 0;
1902 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1904 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1906 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1910 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1912 /* start the DAI frontend */
1913 ret = soc_pcm_open(fe_substream);
1915 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1919 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1921 dpcm_set_fe_runtime(fe_substream);
1922 snd_pcm_limit_hw_rates(runtime);
1924 ret = dpcm_apply_symmetry(fe_substream, stream);
1926 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1931 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1935 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1937 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1941 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1943 struct snd_soc_dpcm *dpcm;
1945 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1946 for_each_dpcm_be(fe, stream, dpcm) {
1948 struct snd_soc_pcm_runtime *be = dpcm->be;
1949 struct snd_pcm_substream *be_substream =
1950 snd_soc_dpcm_get_substream(be, stream);
1952 /* is this op for this BE ? */
1953 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1956 if (be->dpcm[stream].users == 0)
1957 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1958 stream ? "capture" : "playback",
1959 be->dpcm[stream].state);
1961 if (--be->dpcm[stream].users != 0)
1964 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1965 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1966 soc_pcm_hw_free(be_substream);
1967 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1970 dev_dbg(be->dev, "ASoC: close BE %s\n",
1971 be->dai_link->name);
1973 soc_pcm_close(be_substream);
1974 be_substream->runtime = NULL;
1976 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1981 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1983 struct snd_soc_pcm_runtime *fe = substream->private_data;
1984 int stream = substream->stream;
1986 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1988 /* shutdown the BEs */
1989 dpcm_be_dai_shutdown(fe, substream->stream);
1991 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1993 /* now shutdown the frontend */
1994 soc_pcm_close(substream);
1996 /* run the stream event for each BE */
1997 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1999 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2000 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2004 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2006 struct snd_soc_dpcm *dpcm;
2008 /* only hw_params backends that are either sinks or sources
2009 * to this frontend DAI */
2010 for_each_dpcm_be(fe, stream, dpcm) {
2012 struct snd_soc_pcm_runtime *be = dpcm->be;
2013 struct snd_pcm_substream *be_substream =
2014 snd_soc_dpcm_get_substream(be, stream);
2016 /* is this op for this BE ? */
2017 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2020 /* only free hw when no longer used - check all FEs */
2021 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2024 /* do not free hw if this BE is used by other FE */
2025 if (be->dpcm[stream].users > 1)
2028 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2029 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2030 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2031 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2032 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2033 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2036 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2037 be->dai_link->name);
2039 soc_pcm_hw_free(be_substream);
2041 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2047 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2049 struct snd_soc_pcm_runtime *fe = substream->private_data;
2050 int err, stream = substream->stream;
2052 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2053 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2055 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2057 /* call hw_free on the frontend */
2058 err = soc_pcm_hw_free(substream);
2060 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2061 fe->dai_link->name);
2063 /* only hw_params backends that are either sinks or sources
2064 * to this frontend DAI */
2065 err = dpcm_be_dai_hw_free(fe, stream);
2067 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2068 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2070 mutex_unlock(&fe->card->mutex);
2074 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2076 struct snd_soc_dpcm *dpcm;
2079 for_each_dpcm_be(fe, stream, dpcm) {
2081 struct snd_soc_pcm_runtime *be = dpcm->be;
2082 struct snd_pcm_substream *be_substream =
2083 snd_soc_dpcm_get_substream(be, stream);
2085 /* is this op for this BE ? */
2086 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2089 /* copy params for each dpcm */
2090 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2091 sizeof(struct snd_pcm_hw_params));
2093 /* perform any hw_params fixups */
2094 if (be->dai_link->be_hw_params_fixup) {
2095 ret = be->dai_link->be_hw_params_fixup(be,
2099 "ASoC: hw_params BE fixup failed %d\n",
2105 /* copy the fixed-up hw params for BE dai */
2106 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2107 sizeof(struct snd_pcm_hw_params));
2109 /* only allow hw_params() if no connected FEs are running */
2110 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2113 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2114 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2115 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2118 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2119 be->dai_link->name);
2121 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2123 dev_err(dpcm->be->dev,
2124 "ASoC: hw_params BE failed %d\n", ret);
2128 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2133 /* disable any enabled and non active backends */
2134 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2135 struct snd_soc_pcm_runtime *be = dpcm->be;
2136 struct snd_pcm_substream *be_substream =
2137 snd_soc_dpcm_get_substream(be, stream);
2139 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2142 /* only allow hw_free() if no connected FEs are running */
2143 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2146 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2147 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2148 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2149 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2152 soc_pcm_hw_free(be_substream);
2158 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2159 struct snd_pcm_hw_params *params)
2161 struct snd_soc_pcm_runtime *fe = substream->private_data;
2162 int ret, stream = substream->stream;
2164 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2165 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2167 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2168 sizeof(struct snd_pcm_hw_params));
2169 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2171 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2175 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2176 fe->dai_link->name, params_rate(params),
2177 params_channels(params), params_format(params));
2179 /* call hw_params on the frontend */
2180 ret = soc_pcm_hw_params(substream, params);
2182 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2183 dpcm_be_dai_hw_free(fe, stream);
2185 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2188 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2189 mutex_unlock(&fe->card->mutex);
2193 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2194 struct snd_pcm_substream *substream, int cmd)
2198 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2199 dpcm->be->dai_link->name, cmd);
2201 ret = soc_pcm_trigger(substream, cmd);
2203 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2208 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2211 struct snd_soc_dpcm *dpcm;
2214 for_each_dpcm_be(fe, stream, dpcm) {
2216 struct snd_soc_pcm_runtime *be = dpcm->be;
2217 struct snd_pcm_substream *be_substream =
2218 snd_soc_dpcm_get_substream(be, stream);
2220 /* is this op for this BE ? */
2221 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2225 case SNDRV_PCM_TRIGGER_START:
2226 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2227 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2228 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2231 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2235 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2237 case SNDRV_PCM_TRIGGER_RESUME:
2238 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2241 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2245 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2247 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2248 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2251 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2255 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2257 case SNDRV_PCM_TRIGGER_STOP:
2258 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2259 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
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_STOP;
2271 case SNDRV_PCM_TRIGGER_SUSPEND:
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_SUSPEND;
2284 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2285 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2288 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2291 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2295 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2302 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2304 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2305 int cmd, bool fe_first)
2307 struct snd_soc_pcm_runtime *fe = substream->private_data;
2310 /* call trigger on the frontend before the backend. */
2312 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2313 fe->dai_link->name, cmd);
2315 ret = soc_pcm_trigger(substream, cmd);
2319 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2323 /* call trigger on the frontend after the backend. */
2324 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2328 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2329 fe->dai_link->name, cmd);
2331 ret = soc_pcm_trigger(substream, cmd);
2336 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2338 struct snd_soc_pcm_runtime *fe = substream->private_data;
2339 int stream = substream->stream;
2341 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2343 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2346 case SND_SOC_DPCM_TRIGGER_PRE:
2348 case SNDRV_PCM_TRIGGER_START:
2349 case SNDRV_PCM_TRIGGER_RESUME:
2350 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2351 case SNDRV_PCM_TRIGGER_DRAIN:
2352 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2354 case SNDRV_PCM_TRIGGER_STOP:
2355 case SNDRV_PCM_TRIGGER_SUSPEND:
2356 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2357 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2364 case SND_SOC_DPCM_TRIGGER_POST:
2366 case SNDRV_PCM_TRIGGER_START:
2367 case SNDRV_PCM_TRIGGER_RESUME:
2368 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2369 case SNDRV_PCM_TRIGGER_DRAIN:
2370 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2372 case SNDRV_PCM_TRIGGER_STOP:
2373 case SNDRV_PCM_TRIGGER_SUSPEND:
2374 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2375 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2382 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2383 /* bespoke trigger() - handles both FE and BEs */
2385 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2386 fe->dai_link->name, cmd);
2388 ret = soc_pcm_bespoke_trigger(substream, cmd);
2391 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2392 fe->dai_link->name);
2398 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2404 case SNDRV_PCM_TRIGGER_START:
2405 case SNDRV_PCM_TRIGGER_RESUME:
2406 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2407 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2409 case SNDRV_PCM_TRIGGER_STOP:
2410 case SNDRV_PCM_TRIGGER_SUSPEND:
2411 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2413 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2414 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2419 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2423 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2425 struct snd_soc_pcm_runtime *fe = substream->private_data;
2426 int stream = substream->stream;
2428 /* if FE's runtime_update is already set, we're in race;
2429 * process this trigger later at exit
2431 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2432 fe->dpcm[stream].trigger_pending = cmd + 1;
2433 return 0; /* delayed, assuming it's successful */
2436 /* we're alone, let's trigger */
2437 return dpcm_fe_dai_do_trigger(substream, cmd);
2440 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2442 struct snd_soc_dpcm *dpcm;
2445 for_each_dpcm_be(fe, stream, dpcm) {
2447 struct snd_soc_pcm_runtime *be = dpcm->be;
2448 struct snd_pcm_substream *be_substream =
2449 snd_soc_dpcm_get_substream(be, stream);
2451 /* is this op for this BE ? */
2452 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2455 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2456 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2457 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2458 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2461 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2462 be->dai_link->name);
2464 ret = soc_pcm_prepare(be_substream);
2466 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2471 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2476 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2478 struct snd_soc_pcm_runtime *fe = substream->private_data;
2479 int stream = substream->stream, ret = 0;
2481 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2483 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2485 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2487 /* there is no point preparing this FE if there are no BEs */
2488 if (list_empty(&fe->dpcm[stream].be_clients)) {
2489 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2490 fe->dai_link->name);
2495 ret = dpcm_be_dai_prepare(fe, substream->stream);
2499 /* call prepare on the frontend */
2500 ret = soc_pcm_prepare(substream);
2502 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2503 fe->dai_link->name);
2507 /* run the stream event for each BE */
2508 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2509 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2512 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2513 mutex_unlock(&fe->card->mutex);
2518 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2520 struct snd_pcm_substream *substream =
2521 snd_soc_dpcm_get_substream(fe, stream);
2522 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2525 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2526 stream ? "capture" : "playback", fe->dai_link->name);
2528 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2529 /* call bespoke trigger - FE takes care of all BE triggers */
2530 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2531 fe->dai_link->name);
2533 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2535 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2537 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2538 fe->dai_link->name);
2540 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2542 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2545 err = dpcm_be_dai_hw_free(fe, stream);
2547 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2549 err = dpcm_be_dai_shutdown(fe, stream);
2551 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2553 /* run the stream event for each BE */
2554 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2559 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2561 struct snd_pcm_substream *substream =
2562 snd_soc_dpcm_get_substream(fe, stream);
2563 struct snd_soc_dpcm *dpcm;
2564 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2566 unsigned long flags;
2568 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2569 stream ? "capture" : "playback", fe->dai_link->name);
2571 /* Only start the BE if the FE is ready */
2572 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2573 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2576 /* startup must always be called for new BEs */
2577 ret = dpcm_be_dai_startup(fe, stream);
2581 /* keep going if FE state is > open */
2582 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2585 ret = dpcm_be_dai_hw_params(fe, stream);
2589 /* keep going if FE state is > hw_params */
2590 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2594 ret = dpcm_be_dai_prepare(fe, stream);
2598 /* run the stream event for each BE */
2599 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2601 /* keep going if FE state is > prepare */
2602 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2603 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2606 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2607 /* call trigger on the frontend - FE takes care of all BE triggers */
2608 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2609 fe->dai_link->name);
2611 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2613 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2617 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2618 fe->dai_link->name);
2620 ret = dpcm_be_dai_trigger(fe, stream,
2621 SNDRV_PCM_TRIGGER_START);
2623 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2631 dpcm_be_dai_hw_free(fe, stream);
2633 dpcm_be_dai_shutdown(fe, stream);
2635 /* disconnect any non started BEs */
2636 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2637 for_each_dpcm_be(fe, stream, dpcm) {
2638 struct snd_soc_pcm_runtime *be = dpcm->be;
2639 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2640 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2642 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2647 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2651 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2652 ret = dpcm_run_update_startup(fe, stream);
2654 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2655 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2660 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2664 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2665 ret = dpcm_run_update_shutdown(fe, stream);
2667 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2668 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2673 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2675 struct snd_soc_dapm_widget_list *list;
2678 if (!fe->dai_link->dynamic)
2681 /* only check active links */
2682 if (!fe->cpu_dai->active)
2685 /* DAPM sync will call this to update DSP paths */
2686 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2687 new ? "new" : "old", fe->dai_link->name);
2689 /* skip if FE doesn't have playback capability */
2690 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK) ||
2691 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_PLAYBACK))
2694 /* skip if FE isn't currently playing */
2695 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2698 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2700 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2701 fe->dai_link->name, "playback");
2705 /* update any playback paths */
2706 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2709 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2711 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2713 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2714 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2717 dpcm_path_put(&list);
2720 /* skip if FE doesn't have capture capability */
2721 if (!snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE) ||
2722 !snd_soc_dai_stream_valid(fe->codec_dai, SNDRV_PCM_STREAM_CAPTURE))
2725 /* skip if FE isn't currently capturing */
2726 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2729 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2731 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2732 fe->dai_link->name, "capture");
2736 /* update any old capture paths */
2737 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2740 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2742 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2744 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2745 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2748 dpcm_path_put(&list);
2753 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2756 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2758 struct snd_soc_pcm_runtime *fe;
2761 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2762 /* shutdown all old paths first */
2763 for_each_card_rtds(card, fe) {
2764 ret = soc_dpcm_fe_runtime_update(fe, 0);
2769 /* bring new paths up */
2770 for_each_card_rtds(card, fe) {
2771 ret = soc_dpcm_fe_runtime_update(fe, 1);
2777 mutex_unlock(&card->mutex);
2780 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2782 struct snd_soc_dpcm *dpcm;
2783 struct snd_soc_dai *dai;
2785 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2787 struct snd_soc_pcm_runtime *be = dpcm->be;
2790 if (be->dai_link->ignore_suspend)
2793 for_each_rtd_codec_dai(be, i, dai) {
2794 struct snd_soc_dai_driver *drv = dai->driver;
2796 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2797 be->dai_link->name);
2799 if (drv->ops && drv->ops->digital_mute &&
2800 dai->playback_active)
2801 drv->ops->digital_mute(dai, mute);
2808 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2810 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2811 struct snd_soc_dpcm *dpcm;
2812 struct snd_soc_dapm_widget_list *list;
2814 int stream = fe_substream->stream;
2816 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2817 fe->dpcm[stream].runtime = fe_substream->runtime;
2819 ret = dpcm_path_get(fe, stream, &list);
2821 mutex_unlock(&fe->card->mutex);
2823 } else if (ret == 0) {
2824 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2825 fe->dai_link->name, stream ? "capture" : "playback");
2828 /* calculate valid and active FE <-> BE dpcms */
2829 dpcm_process_paths(fe, stream, &list, 1);
2831 ret = dpcm_fe_dai_startup(fe_substream);
2833 /* clean up all links */
2834 for_each_dpcm_be(fe, stream, dpcm)
2835 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2837 dpcm_be_disconnect(fe, stream);
2838 fe->dpcm[stream].runtime = NULL;
2841 dpcm_clear_pending_state(fe, stream);
2842 dpcm_path_put(&list);
2843 mutex_unlock(&fe->card->mutex);
2847 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2849 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2850 struct snd_soc_dpcm *dpcm;
2851 int stream = fe_substream->stream, ret;
2853 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2854 ret = dpcm_fe_dai_shutdown(fe_substream);
2856 /* mark FE's links ready to prune */
2857 for_each_dpcm_be(fe, stream, dpcm)
2858 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2860 dpcm_be_disconnect(fe, stream);
2862 fe->dpcm[stream].runtime = NULL;
2863 mutex_unlock(&fe->card->mutex);
2867 static void soc_pcm_private_free(struct snd_pcm *pcm)
2869 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2871 /* need to sync the delayed work before releasing resources */
2872 flush_delayed_work(&rtd->delayed_work);
2873 snd_soc_pcm_component_free(pcm);
2876 /* create a new pcm */
2877 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2879 struct snd_soc_dai *codec_dai;
2880 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2881 struct snd_soc_rtdcom_list *rtdcom;
2882 struct snd_pcm *pcm;
2884 int ret = 0, playback = 0, capture = 0;
2887 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2888 playback = rtd->dai_link->dpcm_playback;
2889 capture = rtd->dai_link->dpcm_capture;
2891 /* Adapt stream for codec2codec links */
2892 struct snd_soc_pcm_stream *cpu_capture = rtd->dai_link->params ?
2893 &cpu_dai->driver->playback : &cpu_dai->driver->capture;
2894 struct snd_soc_pcm_stream *cpu_playback = rtd->dai_link->params ?
2895 &cpu_dai->driver->capture : &cpu_dai->driver->playback;
2897 for_each_rtd_codec_dai(rtd, i, codec_dai) {
2898 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2899 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
2901 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2902 snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
2906 capture = capture && cpu_capture->channels_min;
2907 playback = playback && cpu_playback->channels_min;
2910 if (rtd->dai_link->playback_only) {
2915 if (rtd->dai_link->capture_only) {
2920 /* create the PCM */
2921 if (rtd->dai_link->params) {
2922 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2923 rtd->dai_link->stream_name);
2925 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2926 playback, capture, &pcm);
2927 } else if (rtd->dai_link->no_pcm) {
2928 snprintf(new_name, sizeof(new_name), "(%s)",
2929 rtd->dai_link->stream_name);
2931 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2932 playback, capture, &pcm);
2934 if (rtd->dai_link->dynamic)
2935 snprintf(new_name, sizeof(new_name), "%s (*)",
2936 rtd->dai_link->stream_name);
2938 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2939 rtd->dai_link->stream_name,
2940 (rtd->num_codecs > 1) ?
2941 "multicodec" : rtd->codec_dai->name, num);
2943 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2947 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2948 rtd->dai_link->name);
2951 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2953 /* DAPM dai link stream work */
2954 if (rtd->dai_link->params)
2955 INIT_DELAYED_WORK(&rtd->delayed_work,
2956 codec2codec_close_delayed_work);
2958 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2960 pcm->nonatomic = rtd->dai_link->nonatomic;
2962 pcm->private_data = rtd;
2964 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2966 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2968 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2972 /* ASoC PCM operations */
2973 if (rtd->dai_link->dynamic) {
2974 rtd->ops.open = dpcm_fe_dai_open;
2975 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2976 rtd->ops.prepare = dpcm_fe_dai_prepare;
2977 rtd->ops.trigger = dpcm_fe_dai_trigger;
2978 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2979 rtd->ops.close = dpcm_fe_dai_close;
2980 rtd->ops.pointer = soc_pcm_pointer;
2981 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2983 rtd->ops.open = soc_pcm_open;
2984 rtd->ops.hw_params = soc_pcm_hw_params;
2985 rtd->ops.prepare = soc_pcm_prepare;
2986 rtd->ops.trigger = soc_pcm_trigger;
2987 rtd->ops.hw_free = soc_pcm_hw_free;
2988 rtd->ops.close = soc_pcm_close;
2989 rtd->ops.pointer = soc_pcm_pointer;
2990 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2993 for_each_rtdcom(rtd, rtdcom) {
2994 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3000 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
3002 rtd->ops.page = snd_soc_pcm_component_page;
3004 rtd->ops.mmap = snd_soc_pcm_component_mmap;
3008 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3011 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3013 ret = snd_soc_pcm_component_new(pcm);
3015 dev_err(rtd->dev, "ASoC: pcm constructor failed: %d\n", ret);
3019 pcm->private_free = soc_pcm_private_free;
3020 pcm->no_device_suspend = true;
3022 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3023 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3028 /* is the current PCM operation for this FE ? */
3029 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3031 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3035 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3037 /* is the current PCM operation for this BE ? */
3038 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3039 struct snd_soc_pcm_runtime *be, int stream)
3041 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3042 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3043 be->dpcm[stream].runtime_update))
3047 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3049 /* get the substream for this BE */
3050 struct snd_pcm_substream *
3051 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3053 return be->pcm->streams[stream].substream;
3055 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3057 /* get the BE runtime state */
3058 enum snd_soc_dpcm_state
3059 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3061 return be->dpcm[stream].state;
3063 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3065 /* set the BE runtime state */
3066 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3067 int stream, enum snd_soc_dpcm_state state)
3069 be->dpcm[stream].state = state;
3071 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3074 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3075 * are not running, paused or suspended for the specified stream direction.
3077 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3078 struct snd_soc_pcm_runtime *be, int stream)
3080 struct snd_soc_dpcm *dpcm;
3083 unsigned long flags;
3085 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3086 for_each_dpcm_fe(be, stream, dpcm) {
3091 state = dpcm->fe->dpcm[stream].state;
3092 if (state == SND_SOC_DPCM_STATE_START ||
3093 state == SND_SOC_DPCM_STATE_PAUSED ||
3094 state == SND_SOC_DPCM_STATE_SUSPEND) {
3099 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3101 /* it's safe to free/stop this BE DAI */
3104 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3107 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3108 * running, paused or suspended for the specified stream direction.
3110 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3111 struct snd_soc_pcm_runtime *be, int stream)
3113 struct snd_soc_dpcm *dpcm;
3116 unsigned long flags;
3118 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3119 for_each_dpcm_fe(be, stream, dpcm) {
3124 state = dpcm->fe->dpcm[stream].state;
3125 if (state == SND_SOC_DPCM_STATE_START ||
3126 state == SND_SOC_DPCM_STATE_PAUSED ||
3127 state == SND_SOC_DPCM_STATE_SUSPEND ||
3128 state == SND_SOC_DPCM_STATE_PREPARE) {
3133 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3135 /* it's safe to change hw_params */
3138 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3140 #ifdef CONFIG_DEBUG_FS
3141 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3144 case SND_SOC_DPCM_STATE_NEW:
3146 case SND_SOC_DPCM_STATE_OPEN:
3148 case SND_SOC_DPCM_STATE_HW_PARAMS:
3150 case SND_SOC_DPCM_STATE_PREPARE:
3152 case SND_SOC_DPCM_STATE_START:
3154 case SND_SOC_DPCM_STATE_STOP:
3156 case SND_SOC_DPCM_STATE_SUSPEND:
3158 case SND_SOC_DPCM_STATE_PAUSED:
3160 case SND_SOC_DPCM_STATE_HW_FREE:
3162 case SND_SOC_DPCM_STATE_CLOSE:
3169 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3170 int stream, char *buf, size_t size)
3172 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3173 struct snd_soc_dpcm *dpcm;
3175 unsigned long flags;
3178 offset += scnprintf(buf + offset, size - offset,
3179 "[%s - %s]\n", fe->dai_link->name,
3180 stream ? "Capture" : "Playback");
3182 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
3183 dpcm_state_string(fe->dpcm[stream].state));
3185 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3186 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3187 offset += scnprintf(buf + offset, size - offset,
3189 "Format = %s, Channels = %d, Rate = %d\n",
3190 snd_pcm_format_name(params_format(params)),
3191 params_channels(params),
3192 params_rate(params));
3195 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
3197 if (list_empty(&fe->dpcm[stream].be_clients)) {
3198 offset += scnprintf(buf + offset, size - offset,
3199 " No active DSP links\n");
3203 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3204 for_each_dpcm_be(fe, stream, dpcm) {
3205 struct snd_soc_pcm_runtime *be = dpcm->be;
3206 params = &dpcm->hw_params;
3208 offset += scnprintf(buf + offset, size - offset,
3209 "- %s\n", be->dai_link->name);
3211 offset += scnprintf(buf + offset, size - offset,
3213 dpcm_state_string(be->dpcm[stream].state));
3215 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3216 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3217 offset += scnprintf(buf + offset, size - offset,
3218 " Hardware Params: "
3219 "Format = %s, Channels = %d, Rate = %d\n",
3220 snd_pcm_format_name(params_format(params)),
3221 params_channels(params),
3222 params_rate(params));
3224 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3229 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3230 size_t count, loff_t *ppos)
3232 struct snd_soc_pcm_runtime *fe = file->private_data;
3233 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3236 buf = kmalloc(out_count, GFP_KERNEL);
3240 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
3241 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3242 buf + offset, out_count - offset);
3244 if (snd_soc_dai_stream_valid(fe->cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
3245 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3246 buf + offset, out_count - offset);
3248 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3254 static const struct file_operations dpcm_state_fops = {
3255 .open = simple_open,
3256 .read = dpcm_state_read_file,
3257 .llseek = default_llseek,
3260 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3265 if (!rtd->dai_link->dynamic)
3268 if (!rtd->card->debugfs_card_root)
3271 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3272 rtd->card->debugfs_card_root);
3274 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3275 rtd, &dpcm_state_fops);