2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
44 struct snd_soc_pcm_stream *codec_stream;
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70 lockdep_assert_held(&rtd->pcm_mutex);
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
83 cpu_dai->component->active++;
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 lockdep_assert_held(&rtd->pcm_mutex);
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
118 cpu_dai->component->active--;
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
175 struct snd_soc_dpcm *dpcm;
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
179 struct snd_soc_pcm_runtime *be = dpcm->be;
181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182 be->dai_link->name, event, dir);
184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
188 snd_soc_dapm_stream_event(be, dir, event);
191 snd_soc_dapm_stream_event(fe, dir, event);
196 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
207 ret = snd_pcm_hw_constraint_single(substream->runtime,
208 SNDRV_PCM_HW_PARAM_RATE,
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
223 ret = snd_pcm_hw_constraint_single(substream->runtime,
224 SNDRV_PCM_HW_PARAM_CHANNELS,
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
239 ret = snd_pcm_hw_constraint_single(substream->runtime,
240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
241 soc_dai->sample_bits);
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
253 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
258 unsigned int rate, channels, sample_bits, symmetry, i;
260 rate = params_rate(params);
261 channels = params_channels(params);
262 sample_bits = snd_pcm_format_physical_width(params_format(params));
264 /* reject unmatched parameters when applying symmetry */
265 symmetry = cpu_dai->driver->symmetric_rates ||
266 rtd->dai_link->symmetric_rates;
268 for (i = 0; i < rtd->num_codecs; i++)
269 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
271 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
272 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai->rate, rate);
277 symmetry = cpu_dai->driver->symmetric_channels ||
278 rtd->dai_link->symmetric_channels;
280 for (i = 0; i < rtd->num_codecs; i++)
281 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
283 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
284 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai->channels, channels);
289 symmetry = cpu_dai->driver->symmetric_samplebits ||
290 rtd->dai_link->symmetric_samplebits;
292 for (i = 0; i < rtd->num_codecs; i++)
293 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
295 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
296 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai->sample_bits, sample_bits);
304 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
308 struct snd_soc_dai_link *link = rtd->dai_link;
309 unsigned int symmetry, i;
311 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
312 cpu_driver->symmetric_channels || link->symmetric_channels ||
313 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
315 for (i = 0; i < rtd->num_codecs; i++)
316 symmetry = symmetry ||
317 rtd->codec_dais[i]->driver->symmetric_rates ||
318 rtd->codec_dais[i]->driver->symmetric_channels ||
319 rtd->codec_dais[i]->driver->symmetric_samplebits;
324 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
338 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
340 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
342 struct snd_soc_dai *codec_dai;
344 unsigned int bits = 0, cpu_bits;
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
347 for (i = 0; i < rtd->num_codecs; i++) {
348 codec_dai = rtd->codec_dais[i];
349 if (codec_dai->driver->playback.sig_bits == 0) {
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
355 cpu_bits = cpu_dai->driver->playback.sig_bits;
357 for (i = 0; i < rtd->num_codecs; i++) {
358 codec_dai = rtd->codec_dais[i];
359 if (codec_dai->driver->capture.sig_bits == 0) {
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
365 cpu_bits = cpu_dai->driver->capture.sig_bits;
368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
372 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_pcm_hardware *hw = &runtime->hw;
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
378 struct snd_soc_dai_driver *codec_dai_drv;
379 struct snd_soc_pcm_stream *codec_stream;
380 struct snd_soc_pcm_stream *cpu_stream;
381 unsigned int chan_min = 0, chan_max = UINT_MAX;
382 unsigned int rate_min = 0, rate_max = UINT_MAX;
383 unsigned int rates = UINT_MAX;
384 u64 formats = ULLONG_MAX;
387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
390 cpu_stream = &cpu_dai_drv->capture;
392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i = 0; i < rtd->num_codecs; i++) {
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
404 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
412 codec_stream = &codec_dai_drv->capture;
413 chan_min = max(chan_min, codec_stream->channels_min);
414 chan_max = min(chan_max, codec_stream->channels_max);
415 rate_min = max(rate_min, codec_stream->rate_min);
416 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
417 formats &= codec_stream->formats;
418 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
426 if (rtd->num_codecs > 1) {
427 chan_min = cpu_stream->channels_min;
428 chan_max = cpu_stream->channels_max;
431 hw->channels_min = max(chan_min, cpu_stream->channels_min);
432 hw->channels_max = min(chan_max, cpu_stream->channels_max);
434 hw->formats &= formats & cpu_stream->formats;
436 hw->formats = formats & cpu_stream->formats;
437 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
439 snd_pcm_limit_hw_rates(runtime);
441 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
442 hw->rate_min = max(hw->rate_min, rate_min);
443 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
444 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
452 static int soc_pcm_open(struct snd_pcm_substream *substream)
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct snd_soc_platform *platform = rtd->platform;
457 struct snd_soc_component *component;
458 struct snd_soc_rtdcom_list *rtdcom;
459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
460 struct snd_soc_dai *codec_dai;
461 const char *codec_dai_name = "multicodec";
464 pinctrl_pm_select_default_state(cpu_dai->dev);
465 for (i = 0; i < rtd->num_codecs; i++)
466 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
468 for_each_rtdcom(rtd, rtdcom) {
469 component = rtdcom->component;
471 pm_runtime_get_sync(component->dev);
474 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
476 /* startup the audio subsystem */
477 if (cpu_dai->driver->ops->startup) {
478 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
480 dev_err(cpu_dai->dev, "ASoC: can't open interface"
481 " %s: %d\n", cpu_dai->name, ret);
486 if (platform->driver->ops && platform->driver->ops->open) {
487 ret = platform->driver->ops->open(substream);
489 dev_err(platform->dev, "ASoC: can't open platform"
490 " %s: %d\n", platform->component.name, ret);
495 for (i = 0; i < rtd->num_codecs; i++) {
496 codec_dai = rtd->codec_dais[i];
497 if (codec_dai->driver->ops->startup) {
498 ret = codec_dai->driver->ops->startup(substream,
501 dev_err(codec_dai->dev,
502 "ASoC: can't open codec %s: %d\n",
503 codec_dai->name, ret);
508 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
509 codec_dai->tx_mask = 0;
511 codec_dai->rx_mask = 0;
514 if (rtd->dai_link->ops->startup) {
515 ret = rtd->dai_link->ops->startup(substream);
517 pr_err("ASoC: %s startup failed: %d\n",
518 rtd->dai_link->name, ret);
523 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
524 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
527 /* Check that the codec and cpu DAIs are compatible */
528 soc_pcm_init_runtime_hw(substream);
530 if (rtd->num_codecs == 1)
531 codec_dai_name = rtd->codec_dai->name;
533 if (soc_pcm_has_symmetry(substream))
534 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
537 if (!runtime->hw.rates) {
538 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
539 codec_dai_name, cpu_dai->name);
542 if (!runtime->hw.formats) {
543 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
544 codec_dai_name, cpu_dai->name);
547 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
548 runtime->hw.channels_min > runtime->hw.channels_max) {
549 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
550 codec_dai_name, cpu_dai->name);
554 soc_pcm_apply_msb(substream);
556 /* Symmetry only applies if we've already got an active stream. */
557 if (cpu_dai->active) {
558 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
563 for (i = 0; i < rtd->num_codecs; i++) {
564 if (rtd->codec_dais[i]->active) {
565 ret = soc_pcm_apply_symmetry(substream,
572 pr_debug("ASoC: %s <-> %s info:\n",
573 codec_dai_name, cpu_dai->name);
574 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
575 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
576 runtime->hw.channels_max);
577 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
578 runtime->hw.rate_max);
582 snd_soc_runtime_activate(rtd, substream->stream);
584 mutex_unlock(&rtd->pcm_mutex);
588 if (rtd->dai_link->ops->shutdown)
589 rtd->dai_link->ops->shutdown(substream);
596 codec_dai = rtd->codec_dais[i];
597 if (codec_dai->driver->ops->shutdown)
598 codec_dai->driver->ops->shutdown(substream, codec_dai);
601 if (platform->driver->ops && platform->driver->ops->close)
602 platform->driver->ops->close(substream);
605 if (cpu_dai->driver->ops->shutdown)
606 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
608 mutex_unlock(&rtd->pcm_mutex);
610 for_each_rtdcom(rtd, rtdcom) {
611 component = rtdcom->component;
613 pm_runtime_mark_last_busy(component->dev);
614 pm_runtime_put_autosuspend(component->dev);
617 for (i = 0; i < rtd->num_codecs; i++) {
618 if (!rtd->codec_dais[i]->active)
619 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
621 if (!cpu_dai->active)
622 pinctrl_pm_select_sleep_state(cpu_dai->dev);
628 * Power down the audio subsystem pmdown_time msecs after close is called.
629 * This is to ensure there are no pops or clicks in between any music tracks
630 * due to DAPM power cycling.
632 static void close_delayed_work(struct work_struct *work)
634 struct snd_soc_pcm_runtime *rtd =
635 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
636 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
638 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
640 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
641 codec_dai->driver->playback.stream_name,
642 codec_dai->playback_active ? "active" : "inactive",
643 rtd->pop_wait ? "yes" : "no");
645 /* are we waiting on this codec DAI stream */
646 if (rtd->pop_wait == 1) {
648 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
649 SND_SOC_DAPM_STREAM_STOP);
652 mutex_unlock(&rtd->pcm_mutex);
656 * Called by ALSA when a PCM substream is closed. Private data can be
657 * freed here. The cpu DAI, codec DAI, machine and platform are also
660 static int soc_pcm_close(struct snd_pcm_substream *substream)
662 struct snd_soc_pcm_runtime *rtd = substream->private_data;
663 struct snd_soc_platform *platform = rtd->platform;
664 struct snd_soc_component *component;
665 struct snd_soc_rtdcom_list *rtdcom;
666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
667 struct snd_soc_dai *codec_dai;
670 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
672 snd_soc_runtime_deactivate(rtd, substream->stream);
674 /* clear the corresponding DAIs rate when inactive */
675 if (!cpu_dai->active)
678 for (i = 0; i < rtd->num_codecs; i++) {
679 codec_dai = rtd->codec_dais[i];
680 if (!codec_dai->active)
684 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
686 if (cpu_dai->driver->ops->shutdown)
687 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
689 for (i = 0; i < rtd->num_codecs; i++) {
690 codec_dai = rtd->codec_dais[i];
691 if (codec_dai->driver->ops->shutdown)
692 codec_dai->driver->ops->shutdown(substream, codec_dai);
695 if (rtd->dai_link->ops->shutdown)
696 rtd->dai_link->ops->shutdown(substream);
698 if (platform->driver->ops && platform->driver->ops->close)
699 platform->driver->ops->close(substream);
701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
702 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
703 /* powered down playback stream now */
704 snd_soc_dapm_stream_event(rtd,
705 SNDRV_PCM_STREAM_PLAYBACK,
706 SND_SOC_DAPM_STREAM_STOP);
708 /* start delayed pop wq here for playback streams */
710 queue_delayed_work(system_power_efficient_wq,
712 msecs_to_jiffies(rtd->pmdown_time));
715 /* capture streams can be powered down now */
716 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
717 SND_SOC_DAPM_STREAM_STOP);
720 mutex_unlock(&rtd->pcm_mutex);
722 for_each_rtdcom(rtd, rtdcom) {
723 component = rtdcom->component;
725 pm_runtime_mark_last_busy(component->dev);
726 pm_runtime_put_autosuspend(component->dev);
729 for (i = 0; i < rtd->num_codecs; i++) {
730 if (!rtd->codec_dais[i]->active)
731 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
733 if (!cpu_dai->active)
734 pinctrl_pm_select_sleep_state(cpu_dai->dev);
740 * Called by ALSA when the PCM substream is prepared, can set format, sample
741 * rate, etc. This function is non atomic and can be called multiple times,
742 * it can refer to the runtime info.
744 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
746 struct snd_soc_pcm_runtime *rtd = substream->private_data;
747 struct snd_soc_platform *platform = rtd->platform;
748 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
749 struct snd_soc_dai *codec_dai;
752 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
754 if (rtd->dai_link->ops->prepare) {
755 ret = rtd->dai_link->ops->prepare(substream);
757 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
763 if (platform->driver->ops && platform->driver->ops->prepare) {
764 ret = platform->driver->ops->prepare(substream);
766 dev_err(platform->dev, "ASoC: platform prepare error:"
772 for (i = 0; i < rtd->num_codecs; i++) {
773 codec_dai = rtd->codec_dais[i];
774 if (codec_dai->driver->ops->prepare) {
775 ret = codec_dai->driver->ops->prepare(substream,
778 dev_err(codec_dai->dev,
779 "ASoC: codec DAI prepare error: %d\n",
786 if (cpu_dai->driver->ops->prepare) {
787 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
789 dev_err(cpu_dai->dev,
790 "ASoC: cpu DAI prepare error: %d\n", ret);
795 /* cancel any delayed stream shutdown that is pending */
796 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
799 cancel_delayed_work(&rtd->delayed_work);
802 snd_soc_dapm_stream_event(rtd, substream->stream,
803 SND_SOC_DAPM_STREAM_START);
805 for (i = 0; i < rtd->num_codecs; i++)
806 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
808 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
811 mutex_unlock(&rtd->pcm_mutex);
815 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
818 struct snd_interval *interval;
819 int channels = hweight_long(mask);
821 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
822 interval->min = channels;
823 interval->max = channels;
826 int soc_dai_hw_params(struct snd_pcm_substream *substream,
827 struct snd_pcm_hw_params *params,
828 struct snd_soc_dai *dai)
832 if (dai->driver->ops->hw_params) {
833 ret = dai->driver->ops->hw_params(substream, params, dai);
835 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
845 * Called by ALSA when the hardware params are set by application. This
846 * function can also be called multiple times and can allocate buffers
847 * (using snd_pcm_lib_* ). It's non-atomic.
849 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
850 struct snd_pcm_hw_params *params)
852 struct snd_soc_pcm_runtime *rtd = substream->private_data;
853 struct snd_soc_platform *platform = rtd->platform;
854 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
857 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
858 if (rtd->dai_link->ops->hw_params) {
859 ret = rtd->dai_link->ops->hw_params(substream, params);
861 dev_err(rtd->card->dev, "ASoC: machine hw_params"
862 " failed: %d\n", ret);
867 for (i = 0; i < rtd->num_codecs; i++) {
868 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
869 struct snd_pcm_hw_params codec_params;
872 * Skip CODECs which don't support the current stream type,
873 * the idea being that if a CODEC is not used for the currently
874 * set up transfer direction, it should not need to be
875 * configured, especially since the configuration used might
876 * not even be supported by that CODEC. There may be cases
877 * however where a CODEC needs to be set up although it is
878 * actually not being used for the transfer, e.g. if a
879 * capture-only CODEC is acting as an LRCLK and/or BCLK master
880 * for the DAI link including a playback-only CODEC.
881 * If this becomes necessary, we will have to augment the
882 * machine driver setup with information on how to act, so
883 * we can do the right thing here.
885 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
888 /* copy params for each codec */
889 codec_params = *params;
891 /* fixup params based on TDM slot masks */
892 if (codec_dai->tx_mask)
893 soc_pcm_codec_params_fixup(&codec_params,
895 if (codec_dai->rx_mask)
896 soc_pcm_codec_params_fixup(&codec_params,
899 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
903 codec_dai->rate = params_rate(&codec_params);
904 codec_dai->channels = params_channels(&codec_params);
905 codec_dai->sample_bits = snd_pcm_format_physical_width(
906 params_format(&codec_params));
909 ret = soc_dai_hw_params(substream, params, cpu_dai);
913 if (platform->driver->ops && platform->driver->ops->hw_params) {
914 ret = platform->driver->ops->hw_params(substream, params);
916 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
917 platform->component.name, ret);
922 /* store the parameters for each DAIs */
923 cpu_dai->rate = params_rate(params);
924 cpu_dai->channels = params_channels(params);
925 cpu_dai->sample_bits =
926 snd_pcm_format_physical_width(params_format(params));
929 ret = soc_pcm_params_symmetry(substream, params);
933 mutex_unlock(&rtd->pcm_mutex);
937 if (cpu_dai->driver->ops->hw_free)
938 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
945 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
946 if (codec_dai->driver->ops->hw_free)
947 codec_dai->driver->ops->hw_free(substream, codec_dai);
951 if (rtd->dai_link->ops->hw_free)
952 rtd->dai_link->ops->hw_free(substream);
954 mutex_unlock(&rtd->pcm_mutex);
959 * Frees resources allocated by hw_params, can be called multiple times
961 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
963 struct snd_soc_pcm_runtime *rtd = substream->private_data;
964 struct snd_soc_platform *platform = rtd->platform;
965 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
966 struct snd_soc_dai *codec_dai;
967 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
970 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
972 /* clear the corresponding DAIs parameters when going to be inactive */
973 if (cpu_dai->active == 1) {
975 cpu_dai->channels = 0;
976 cpu_dai->sample_bits = 0;
979 for (i = 0; i < rtd->num_codecs; i++) {
980 codec_dai = rtd->codec_dais[i];
981 if (codec_dai->active == 1) {
983 codec_dai->channels = 0;
984 codec_dai->sample_bits = 0;
988 /* apply codec digital mute */
989 for (i = 0; i < rtd->num_codecs; i++) {
990 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
991 (!playback && rtd->codec_dais[i]->capture_active == 1))
992 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
996 /* free any machine hw params */
997 if (rtd->dai_link->ops->hw_free)
998 rtd->dai_link->ops->hw_free(substream);
1000 /* free any DMA resources */
1001 if (platform->driver->ops && platform->driver->ops->hw_free)
1002 platform->driver->ops->hw_free(substream);
1004 /* now free hw params for the DAIs */
1005 for (i = 0; i < rtd->num_codecs; i++) {
1006 codec_dai = rtd->codec_dais[i];
1007 if (codec_dai->driver->ops->hw_free)
1008 codec_dai->driver->ops->hw_free(substream, codec_dai);
1011 if (cpu_dai->driver->ops->hw_free)
1012 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1014 mutex_unlock(&rtd->pcm_mutex);
1018 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1020 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1021 struct snd_soc_platform *platform = rtd->platform;
1022 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1023 struct snd_soc_dai *codec_dai;
1026 for (i = 0; i < rtd->num_codecs; i++) {
1027 codec_dai = rtd->codec_dais[i];
1028 if (codec_dai->driver->ops->trigger) {
1029 ret = codec_dai->driver->ops->trigger(substream,
1036 if (platform->driver->ops && platform->driver->ops->trigger) {
1037 ret = platform->driver->ops->trigger(substream, cmd);
1042 if (cpu_dai->driver->ops->trigger) {
1043 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1048 if (rtd->dai_link->ops->trigger) {
1049 ret = rtd->dai_link->ops->trigger(substream, cmd);
1057 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1060 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1061 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1062 struct snd_soc_dai *codec_dai;
1065 for (i = 0; i < rtd->num_codecs; i++) {
1066 codec_dai = rtd->codec_dais[i];
1067 if (codec_dai->driver->ops->bespoke_trigger) {
1068 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1075 if (cpu_dai->driver->ops->bespoke_trigger) {
1076 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1083 * soc level wrapper for pointer callback
1084 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1085 * the runtime->delay will be updated accordingly.
1087 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1089 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1090 struct snd_soc_platform *platform = rtd->platform;
1091 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1092 struct snd_soc_dai *codec_dai;
1093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 snd_pcm_uframes_t offset = 0;
1095 snd_pcm_sframes_t delay = 0;
1096 snd_pcm_sframes_t codec_delay = 0;
1099 if (platform->driver->ops && platform->driver->ops->pointer)
1100 offset = platform->driver->ops->pointer(substream);
1102 if (cpu_dai->driver->ops->delay)
1103 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1105 for (i = 0; i < rtd->num_codecs; i++) {
1106 codec_dai = rtd->codec_dais[i];
1107 if (codec_dai->driver->ops->delay)
1108 codec_delay = max(codec_delay,
1109 codec_dai->driver->ops->delay(substream,
1112 delay += codec_delay;
1114 runtime->delay = delay;
1119 /* connect a FE and BE */
1120 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1121 struct snd_soc_pcm_runtime *be, int stream)
1123 struct snd_soc_dpcm *dpcm;
1125 /* only add new dpcms */
1126 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1127 if (dpcm->be == be && dpcm->fe == fe)
1131 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1137 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1138 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1139 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1140 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1142 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1143 stream ? "capture" : "playback", fe->dai_link->name,
1144 stream ? "<-" : "->", be->dai_link->name);
1146 #ifdef CONFIG_DEBUG_FS
1147 if (fe->debugfs_dpcm_root)
1148 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1149 fe->debugfs_dpcm_root, &dpcm->state);
1154 /* reparent a BE onto another FE */
1155 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1156 struct snd_soc_pcm_runtime *be, int stream)
1158 struct snd_soc_dpcm *dpcm;
1159 struct snd_pcm_substream *fe_substream, *be_substream;
1161 /* reparent if BE is connected to other FEs */
1162 if (!be->dpcm[stream].users)
1165 be_substream = snd_soc_dpcm_get_substream(be, stream);
1167 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1171 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1172 stream ? "capture" : "playback",
1173 dpcm->fe->dai_link->name,
1174 stream ? "<-" : "->", dpcm->be->dai_link->name);
1176 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1177 be_substream->runtime = fe_substream->runtime;
1182 /* disconnect a BE and FE */
1183 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1185 struct snd_soc_dpcm *dpcm, *d;
1187 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1188 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1189 stream ? "capture" : "playback",
1190 dpcm->be->dai_link->name);
1192 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1195 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1196 stream ? "capture" : "playback", fe->dai_link->name,
1197 stream ? "<-" : "->", dpcm->be->dai_link->name);
1199 /* BEs still alive need new FE */
1200 dpcm_be_reparent(fe, dpcm->be, stream);
1202 #ifdef CONFIG_DEBUG_FS
1203 debugfs_remove(dpcm->debugfs_state);
1205 list_del(&dpcm->list_be);
1206 list_del(&dpcm->list_fe);
1211 /* get BE for DAI widget and stream */
1212 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1213 struct snd_soc_dapm_widget *widget, int stream)
1215 struct snd_soc_pcm_runtime *be;
1218 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1219 list_for_each_entry(be, &card->rtd_list, list) {
1221 if (!be->dai_link->no_pcm)
1224 if (be->cpu_dai->playback_widget == widget)
1227 for (i = 0; i < be->num_codecs; i++) {
1228 struct snd_soc_dai *dai = be->codec_dais[i];
1229 if (dai->playback_widget == widget)
1235 list_for_each_entry(be, &card->rtd_list, list) {
1237 if (!be->dai_link->no_pcm)
1240 if (be->cpu_dai->capture_widget == widget)
1243 for (i = 0; i < be->num_codecs; i++) {
1244 struct snd_soc_dai *dai = be->codec_dais[i];
1245 if (dai->capture_widget == widget)
1251 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1252 stream ? "capture" : "playback", widget->name);
1256 static inline struct snd_soc_dapm_widget *
1257 dai_get_widget(struct snd_soc_dai *dai, int stream)
1259 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1260 return dai->playback_widget;
1262 return dai->capture_widget;
1265 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1266 struct snd_soc_dapm_widget *widget)
1270 for (i = 0; i < list->num_widgets; i++) {
1271 if (widget == list->widgets[i])
1278 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1279 enum snd_soc_dapm_direction dir)
1281 struct snd_soc_card *card = widget->dapm->card;
1282 struct snd_soc_pcm_runtime *rtd;
1285 if (dir == SND_SOC_DAPM_DIR_OUT) {
1286 list_for_each_entry(rtd, &card->rtd_list, list) {
1287 if (!rtd->dai_link->no_pcm)
1290 if (rtd->cpu_dai->playback_widget == widget)
1293 for (i = 0; i < rtd->num_codecs; ++i) {
1294 struct snd_soc_dai *dai = rtd->codec_dais[i];
1295 if (dai->playback_widget == widget)
1299 } else { /* SND_SOC_DAPM_DIR_IN */
1300 list_for_each_entry(rtd, &card->rtd_list, list) {
1301 if (!rtd->dai_link->no_pcm)
1304 if (rtd->cpu_dai->capture_widget == widget)
1307 for (i = 0; i < rtd->num_codecs; ++i) {
1308 struct snd_soc_dai *dai = rtd->codec_dais[i];
1309 if (dai->capture_widget == widget)
1318 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1319 int stream, struct snd_soc_dapm_widget_list **list)
1321 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1324 /* get number of valid DAI paths and their widgets */
1325 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1326 dpcm_end_walk_at_be);
1328 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1329 stream ? "capture" : "playback");
1334 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1335 struct snd_soc_dapm_widget_list **list_)
1337 struct snd_soc_dpcm *dpcm;
1338 struct snd_soc_dapm_widget_list *list = *list_;
1339 struct snd_soc_dapm_widget *widget;
1342 /* Destroy any old FE <--> BE connections */
1343 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1346 /* is there a valid CPU DAI widget for this BE */
1347 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1349 /* prune the BE if it's no longer in our active list */
1350 if (widget && widget_in_list(list, widget))
1353 /* is there a valid CODEC DAI widget for this BE */
1354 for (i = 0; i < dpcm->be->num_codecs; i++) {
1355 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1356 widget = dai_get_widget(dai, stream);
1358 /* prune the BE if it's no longer in our active list */
1359 if (widget && widget_in_list(list, widget))
1363 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1364 stream ? "capture" : "playback",
1365 dpcm->be->dai_link->name, fe->dai_link->name);
1366 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1367 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1371 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1375 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1376 struct snd_soc_dapm_widget_list **list_)
1378 struct snd_soc_card *card = fe->card;
1379 struct snd_soc_dapm_widget_list *list = *list_;
1380 struct snd_soc_pcm_runtime *be;
1381 int i, new = 0, err;
1383 /* Create any new FE <--> BE connections */
1384 for (i = 0; i < list->num_widgets; i++) {
1386 switch (list->widgets[i]->id) {
1387 case snd_soc_dapm_dai_in:
1388 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1391 case snd_soc_dapm_dai_out:
1392 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1399 /* is there a valid BE rtd for this widget */
1400 be = dpcm_get_be(card, list->widgets[i], stream);
1402 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1403 list->widgets[i]->name);
1407 /* make sure BE is a real BE */
1408 if (!be->dai_link->no_pcm)
1411 /* don't connect if FE is not running */
1412 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1415 /* newly connected FE and BE */
1416 err = dpcm_be_connect(fe, be, stream);
1418 dev_err(fe->dev, "ASoC: can't connect %s\n",
1419 list->widgets[i]->name);
1421 } else if (err == 0) /* already connected */
1425 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1429 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1434 * Find the corresponding BE DAIs that source or sink audio to this
1437 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1438 int stream, struct snd_soc_dapm_widget_list **list, int new)
1441 return dpcm_add_paths(fe, stream, list);
1443 return dpcm_prune_paths(fe, stream, list);
1446 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1448 struct snd_soc_dpcm *dpcm;
1450 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1451 dpcm->be->dpcm[stream].runtime_update =
1452 SND_SOC_DPCM_UPDATE_NO;
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 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
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 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
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 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
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,
1575 runtime->hw.rate_min = stream->rate_min;
1576 runtime->hw.rate_max = stream->rate_max;
1577 runtime->hw.channels_min = stream->channels_min;
1578 runtime->hw.channels_max = stream->channels_max;
1579 if (runtime->hw.formats)
1580 runtime->hw.formats &= formats & stream->formats;
1582 runtime->hw.formats = formats & stream->formats;
1583 runtime->hw.rates = stream->rates;
1586 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1588 struct snd_soc_pcm_runtime *fe = substream->private_data;
1589 struct snd_soc_dpcm *dpcm;
1590 u64 formats = ULLONG_MAX;
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 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
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 (i = 0; i < be->num_codecs; i++) {
1608 codec_dai_drv = be->codec_dais[i]->driver;
1609 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1610 codec_stream = &codec_dai_drv->playback;
1612 codec_stream = &codec_dai_drv->capture;
1614 formats &= codec_stream->formats;
1621 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1623 struct snd_pcm_runtime *runtime = substream->runtime;
1624 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1625 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1626 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1627 u64 format = dpcm_runtime_base_format(substream);
1629 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1630 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1632 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1635 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1637 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1638 * for avoiding the race with trigger callback.
1639 * If the state is unset and a trigger is pending while the previous operation,
1640 * process the pending trigger action here.
1642 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1643 int stream, enum snd_soc_dpcm_update state)
1645 struct snd_pcm_substream *substream =
1646 snd_soc_dpcm_get_substream(fe, stream);
1648 snd_pcm_stream_lock_irq(substream);
1649 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1650 dpcm_fe_dai_do_trigger(substream,
1651 fe->dpcm[stream].trigger_pending - 1);
1652 fe->dpcm[stream].trigger_pending = 0;
1654 fe->dpcm[stream].runtime_update = state;
1655 snd_pcm_stream_unlock_irq(substream);
1658 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1661 struct snd_soc_dpcm *dpcm;
1662 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1663 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1666 /* apply symmetry for FE */
1667 if (soc_pcm_has_symmetry(fe_substream))
1668 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1670 /* Symmetry only applies if we've got an active stream. */
1671 if (fe_cpu_dai->active) {
1672 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1677 /* apply symmetry for BE */
1678 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1679 struct snd_soc_pcm_runtime *be = dpcm->be;
1680 struct snd_pcm_substream *be_substream =
1681 snd_soc_dpcm_get_substream(be, stream);
1682 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1685 if (rtd->dai_link->be_hw_params_fixup)
1688 if (soc_pcm_has_symmetry(be_substream))
1689 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1691 /* Symmetry only applies if we've got an active stream. */
1692 if (rtd->cpu_dai->active) {
1693 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1698 for (i = 0; i < rtd->num_codecs; i++) {
1699 if (rtd->codec_dais[i]->active) {
1700 err = soc_pcm_apply_symmetry(be_substream,
1701 rtd->codec_dais[i]);
1711 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1713 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1714 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1715 int stream = fe_substream->stream, ret = 0;
1717 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1719 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1721 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1725 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1727 /* start the DAI frontend */
1728 ret = soc_pcm_open(fe_substream);
1730 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1734 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1736 dpcm_set_fe_runtime(fe_substream);
1737 snd_pcm_limit_hw_rates(runtime);
1739 ret = dpcm_apply_symmetry(fe_substream, stream);
1741 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1746 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1750 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1752 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1756 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1758 struct snd_soc_dpcm *dpcm;
1760 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1761 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1763 struct snd_soc_pcm_runtime *be = dpcm->be;
1764 struct snd_pcm_substream *be_substream =
1765 snd_soc_dpcm_get_substream(be, stream);
1767 /* is this op for this BE ? */
1768 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1771 if (be->dpcm[stream].users == 0)
1772 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1773 stream ? "capture" : "playback",
1774 be->dpcm[stream].state);
1776 if (--be->dpcm[stream].users != 0)
1779 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1780 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1783 dev_dbg(be->dev, "ASoC: close BE %s\n",
1784 be->dai_link->name);
1786 soc_pcm_close(be_substream);
1787 be_substream->runtime = NULL;
1789 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1794 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1796 struct snd_soc_pcm_runtime *fe = substream->private_data;
1797 int stream = substream->stream;
1799 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1801 /* shutdown the BEs */
1802 dpcm_be_dai_shutdown(fe, substream->stream);
1804 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1806 /* now shutdown the frontend */
1807 soc_pcm_close(substream);
1809 /* run the stream event for each BE */
1810 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1812 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1813 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1817 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1819 struct snd_soc_dpcm *dpcm;
1821 /* only hw_params backends that are either sinks or sources
1822 * to this frontend DAI */
1823 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1825 struct snd_soc_pcm_runtime *be = dpcm->be;
1826 struct snd_pcm_substream *be_substream =
1827 snd_soc_dpcm_get_substream(be, stream);
1829 /* is this op for this BE ? */
1830 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1833 /* only free hw when no longer used - check all FEs */
1834 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1837 /* do not free hw if this BE is used by other FE */
1838 if (be->dpcm[stream].users > 1)
1841 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1842 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1843 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1844 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1845 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1846 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1849 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1850 be->dai_link->name);
1852 soc_pcm_hw_free(be_substream);
1854 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1860 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1862 struct snd_soc_pcm_runtime *fe = substream->private_data;
1863 int err, stream = substream->stream;
1865 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1866 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1868 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1870 /* call hw_free on the frontend */
1871 err = soc_pcm_hw_free(substream);
1873 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1874 fe->dai_link->name);
1876 /* only hw_params backends that are either sinks or sources
1877 * to this frontend DAI */
1878 err = dpcm_be_dai_hw_free(fe, stream);
1880 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1881 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1883 mutex_unlock(&fe->card->mutex);
1887 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1889 struct snd_soc_dpcm *dpcm;
1892 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1894 struct snd_soc_pcm_runtime *be = dpcm->be;
1895 struct snd_pcm_substream *be_substream =
1896 snd_soc_dpcm_get_substream(be, stream);
1898 /* is this op for this BE ? */
1899 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1902 /* copy params for each dpcm */
1903 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1904 sizeof(struct snd_pcm_hw_params));
1906 /* perform any hw_params fixups */
1907 if (be->dai_link->be_hw_params_fixup) {
1908 ret = be->dai_link->be_hw_params_fixup(be,
1912 "ASoC: hw_params BE fixup failed %d\n",
1918 /* only allow hw_params() if no connected FEs are running */
1919 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1922 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1923 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1924 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1927 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1928 be->dai_link->name);
1930 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1932 dev_err(dpcm->be->dev,
1933 "ASoC: hw_params BE failed %d\n", ret);
1937 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1942 /* disable any enabled and non active backends */
1943 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1944 struct snd_soc_pcm_runtime *be = dpcm->be;
1945 struct snd_pcm_substream *be_substream =
1946 snd_soc_dpcm_get_substream(be, stream);
1948 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1951 /* only allow hw_free() if no connected FEs are running */
1952 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1955 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1956 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1957 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1958 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1961 soc_pcm_hw_free(be_substream);
1967 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1968 struct snd_pcm_hw_params *params)
1970 struct snd_soc_pcm_runtime *fe = substream->private_data;
1971 int ret, stream = substream->stream;
1973 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1974 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1976 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1977 sizeof(struct snd_pcm_hw_params));
1978 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1980 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1984 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1985 fe->dai_link->name, params_rate(params),
1986 params_channels(params), params_format(params));
1988 /* call hw_params on the frontend */
1989 ret = soc_pcm_hw_params(substream, params);
1991 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1992 dpcm_be_dai_hw_free(fe, stream);
1994 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1997 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1998 mutex_unlock(&fe->card->mutex);
2002 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2003 struct snd_pcm_substream *substream, int cmd)
2007 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2008 dpcm->be->dai_link->name, cmd);
2010 ret = soc_pcm_trigger(substream, cmd);
2012 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2017 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2020 struct snd_soc_dpcm *dpcm;
2023 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2025 struct snd_soc_pcm_runtime *be = dpcm->be;
2026 struct snd_pcm_substream *be_substream =
2027 snd_soc_dpcm_get_substream(be, stream);
2029 /* is this op for this BE ? */
2030 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2034 case SNDRV_PCM_TRIGGER_START:
2035 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2036 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2039 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2043 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2045 case SNDRV_PCM_TRIGGER_RESUME:
2046 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2049 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2053 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2055 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2056 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2059 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2063 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2065 case SNDRV_PCM_TRIGGER_STOP:
2066 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2069 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2072 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2076 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2078 case SNDRV_PCM_TRIGGER_SUSPEND:
2079 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2082 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2085 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2089 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2091 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2092 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2095 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2098 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2102 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2109 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2111 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2113 struct snd_soc_pcm_runtime *fe = substream->private_data;
2114 int stream = substream->stream, ret;
2115 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2117 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2120 case SND_SOC_DPCM_TRIGGER_PRE:
2121 /* call trigger on the frontend before the backend. */
2123 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2124 fe->dai_link->name, cmd);
2126 ret = soc_pcm_trigger(substream, cmd);
2128 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2132 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2134 case SND_SOC_DPCM_TRIGGER_POST:
2135 /* call trigger on the frontend after the backend. */
2137 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2139 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2143 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2144 fe->dai_link->name, cmd);
2146 ret = soc_pcm_trigger(substream, cmd);
2148 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2149 /* bespoke trigger() - handles both FE and BEs */
2151 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2152 fe->dai_link->name, cmd);
2154 ret = soc_pcm_bespoke_trigger(substream, cmd);
2156 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2161 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2162 fe->dai_link->name);
2168 case SNDRV_PCM_TRIGGER_START:
2169 case SNDRV_PCM_TRIGGER_RESUME:
2170 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2171 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2173 case SNDRV_PCM_TRIGGER_STOP:
2174 case SNDRV_PCM_TRIGGER_SUSPEND:
2175 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2177 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2178 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2183 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2187 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2189 struct snd_soc_pcm_runtime *fe = substream->private_data;
2190 int stream = substream->stream;
2192 /* if FE's runtime_update is already set, we're in race;
2193 * process this trigger later at exit
2195 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2196 fe->dpcm[stream].trigger_pending = cmd + 1;
2197 return 0; /* delayed, assuming it's successful */
2200 /* we're alone, let's trigger */
2201 return dpcm_fe_dai_do_trigger(substream, cmd);
2204 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2206 struct snd_soc_dpcm *dpcm;
2209 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2211 struct snd_soc_pcm_runtime *be = dpcm->be;
2212 struct snd_pcm_substream *be_substream =
2213 snd_soc_dpcm_get_substream(be, stream);
2215 /* is this op for this BE ? */
2216 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2219 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2220 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2221 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2224 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2225 be->dai_link->name);
2227 ret = soc_pcm_prepare(be_substream);
2229 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2234 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2239 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2241 struct snd_soc_pcm_runtime *fe = substream->private_data;
2242 int stream = substream->stream, ret = 0;
2244 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2246 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2248 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2250 /* there is no point preparing this FE if there are no BEs */
2251 if (list_empty(&fe->dpcm[stream].be_clients)) {
2252 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2253 fe->dai_link->name);
2258 ret = dpcm_be_dai_prepare(fe, substream->stream);
2262 /* call prepare on the frontend */
2263 ret = soc_pcm_prepare(substream);
2265 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2266 fe->dai_link->name);
2270 /* run the stream event for each BE */
2271 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2272 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2275 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2276 mutex_unlock(&fe->card->mutex);
2281 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2282 unsigned int cmd, void *arg)
2284 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2285 struct snd_soc_platform *platform = rtd->platform;
2287 if (platform->driver->ops && platform->driver->ops->ioctl)
2288 return platform->driver->ops->ioctl(substream, cmd, arg);
2289 return snd_pcm_lib_ioctl(substream, cmd, arg);
2292 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2294 struct snd_pcm_substream *substream =
2295 snd_soc_dpcm_get_substream(fe, stream);
2296 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2299 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2300 stream ? "capture" : "playback", fe->dai_link->name);
2302 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2303 /* call bespoke trigger - FE takes care of all BE triggers */
2304 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2305 fe->dai_link->name);
2307 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2309 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2311 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2312 fe->dai_link->name);
2314 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2316 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2319 err = dpcm_be_dai_hw_free(fe, stream);
2321 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2323 err = dpcm_be_dai_shutdown(fe, stream);
2325 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2327 /* run the stream event for each BE */
2328 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2333 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2335 struct snd_pcm_substream *substream =
2336 snd_soc_dpcm_get_substream(fe, stream);
2337 struct snd_soc_dpcm *dpcm;
2338 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2341 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2342 stream ? "capture" : "playback", fe->dai_link->name);
2344 /* Only start the BE if the FE is ready */
2345 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2346 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2349 /* startup must always be called for new BEs */
2350 ret = dpcm_be_dai_startup(fe, stream);
2354 /* keep going if FE state is > open */
2355 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2358 ret = dpcm_be_dai_hw_params(fe, stream);
2362 /* keep going if FE state is > hw_params */
2363 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2367 ret = dpcm_be_dai_prepare(fe, stream);
2371 /* run the stream event for each BE */
2372 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2374 /* keep going if FE state is > prepare */
2375 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2376 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2379 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2380 /* call trigger on the frontend - FE takes care of all BE triggers */
2381 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2382 fe->dai_link->name);
2384 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2386 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2390 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2391 fe->dai_link->name);
2393 ret = dpcm_be_dai_trigger(fe, stream,
2394 SNDRV_PCM_TRIGGER_START);
2396 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2404 dpcm_be_dai_hw_free(fe, stream);
2406 dpcm_be_dai_shutdown(fe, stream);
2408 /* disconnect any non started BEs */
2409 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2410 struct snd_soc_pcm_runtime *be = dpcm->be;
2411 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2412 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2418 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2422 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2423 ret = dpcm_run_update_startup(fe, stream);
2425 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2426 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2431 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2435 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2436 ret = dpcm_run_update_shutdown(fe, stream);
2438 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2439 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2444 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2447 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2449 struct snd_soc_pcm_runtime *fe;
2450 int old, new, paths;
2452 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2453 list_for_each_entry(fe, &card->rtd_list, list) {
2454 struct snd_soc_dapm_widget_list *list;
2456 /* make sure link is FE */
2457 if (!fe->dai_link->dynamic)
2460 /* only check active links */
2461 if (!fe->cpu_dai->active)
2464 /* DAPM sync will call this to update DSP paths */
2465 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2466 fe->dai_link->name);
2468 /* skip if FE doesn't have playback capability */
2469 if (!fe->cpu_dai->driver->playback.channels_min
2470 || !fe->codec_dai->driver->playback.channels_min)
2473 /* skip if FE isn't currently playing */
2474 if (!fe->cpu_dai->playback_active
2475 || !fe->codec_dai->playback_active)
2478 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2480 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2481 fe->dai_link->name, "playback");
2482 mutex_unlock(&card->mutex);
2486 /* update any new playback paths */
2487 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2489 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2490 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2491 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2494 /* update any old playback paths */
2495 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2497 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2498 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2499 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2502 dpcm_path_put(&list);
2504 /* skip if FE doesn't have capture capability */
2505 if (!fe->cpu_dai->driver->capture.channels_min
2506 || !fe->codec_dai->driver->capture.channels_min)
2509 /* skip if FE isn't currently capturing */
2510 if (!fe->cpu_dai->capture_active
2511 || !fe->codec_dai->capture_active)
2514 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2516 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2517 fe->dai_link->name, "capture");
2518 mutex_unlock(&card->mutex);
2522 /* update any new capture paths */
2523 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2525 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2526 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2527 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2530 /* update any old capture paths */
2531 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2533 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2534 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2535 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2538 dpcm_path_put(&list);
2541 mutex_unlock(&card->mutex);
2544 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2546 struct snd_soc_dpcm *dpcm;
2547 struct list_head *clients =
2548 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2550 list_for_each_entry(dpcm, clients, list_be) {
2552 struct snd_soc_pcm_runtime *be = dpcm->be;
2555 if (be->dai_link->ignore_suspend)
2558 for (i = 0; i < be->num_codecs; i++) {
2559 struct snd_soc_dai *dai = be->codec_dais[i];
2560 struct snd_soc_dai_driver *drv = dai->driver;
2562 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2563 be->dai_link->name);
2565 if (drv->ops && drv->ops->digital_mute &&
2566 dai->playback_active)
2567 drv->ops->digital_mute(dai, mute);
2574 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2576 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2577 struct snd_soc_dpcm *dpcm;
2578 struct snd_soc_dapm_widget_list *list;
2580 int stream = fe_substream->stream;
2582 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2583 fe->dpcm[stream].runtime = fe_substream->runtime;
2585 ret = dpcm_path_get(fe, stream, &list);
2587 mutex_unlock(&fe->card->mutex);
2589 } else if (ret == 0) {
2590 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2591 fe->dai_link->name, stream ? "capture" : "playback");
2594 /* calculate valid and active FE <-> BE dpcms */
2595 dpcm_process_paths(fe, stream, &list, 1);
2597 ret = dpcm_fe_dai_startup(fe_substream);
2599 /* clean up all links */
2600 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2601 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2603 dpcm_be_disconnect(fe, stream);
2604 fe->dpcm[stream].runtime = NULL;
2607 dpcm_clear_pending_state(fe, stream);
2608 dpcm_path_put(&list);
2609 mutex_unlock(&fe->card->mutex);
2613 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2615 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2616 struct snd_soc_dpcm *dpcm;
2617 int stream = fe_substream->stream, ret;
2619 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2620 ret = dpcm_fe_dai_shutdown(fe_substream);
2622 /* mark FE's links ready to prune */
2623 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2624 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2626 dpcm_be_disconnect(fe, stream);
2628 fe->dpcm[stream].runtime = NULL;
2629 mutex_unlock(&fe->card->mutex);
2633 static void soc_pcm_private_free(struct snd_pcm *pcm)
2635 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2636 struct snd_soc_rtdcom_list *rtdcom;
2637 struct snd_soc_component *component;
2639 for_each_rtdcom(rtd, rtdcom) {
2640 /* need to sync the delayed work before releasing resources */
2642 flush_delayed_work(&rtd->delayed_work);
2643 component = rtdcom->component;
2645 if (component->pcm_free)
2646 component->pcm_free(component, pcm);
2650 /* create a new pcm */
2651 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2653 struct snd_soc_platform *platform = rtd->platform;
2654 struct snd_soc_dai *codec_dai;
2655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2656 struct snd_soc_component *component;
2657 struct snd_soc_rtdcom_list *rtdcom;
2658 struct snd_pcm *pcm;
2660 int ret = 0, playback = 0, capture = 0;
2663 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2664 playback = rtd->dai_link->dpcm_playback;
2665 capture = rtd->dai_link->dpcm_capture;
2667 for (i = 0; i < rtd->num_codecs; i++) {
2668 codec_dai = rtd->codec_dais[i];
2669 if (codec_dai->driver->playback.channels_min)
2671 if (codec_dai->driver->capture.channels_min)
2675 capture = capture && cpu_dai->driver->capture.channels_min;
2676 playback = playback && cpu_dai->driver->playback.channels_min;
2679 if (rtd->dai_link->playback_only) {
2684 if (rtd->dai_link->capture_only) {
2689 /* create the PCM */
2690 if (rtd->dai_link->no_pcm) {
2691 snprintf(new_name, sizeof(new_name), "(%s)",
2692 rtd->dai_link->stream_name);
2694 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2695 playback, capture, &pcm);
2697 if (rtd->dai_link->dynamic)
2698 snprintf(new_name, sizeof(new_name), "%s (*)",
2699 rtd->dai_link->stream_name);
2701 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2702 rtd->dai_link->stream_name,
2703 (rtd->num_codecs > 1) ?
2704 "multicodec" : rtd->codec_dai->name, num);
2706 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2710 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2711 rtd->dai_link->name);
2714 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2716 /* DAPM dai link stream work */
2717 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2719 pcm->nonatomic = rtd->dai_link->nonatomic;
2721 pcm->private_data = rtd;
2723 if (rtd->dai_link->no_pcm) {
2725 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2727 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2731 /* ASoC PCM operations */
2732 if (rtd->dai_link->dynamic) {
2733 rtd->ops.open = dpcm_fe_dai_open;
2734 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2735 rtd->ops.prepare = dpcm_fe_dai_prepare;
2736 rtd->ops.trigger = dpcm_fe_dai_trigger;
2737 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2738 rtd->ops.close = dpcm_fe_dai_close;
2739 rtd->ops.pointer = soc_pcm_pointer;
2740 rtd->ops.ioctl = soc_pcm_ioctl;
2742 rtd->ops.open = soc_pcm_open;
2743 rtd->ops.hw_params = soc_pcm_hw_params;
2744 rtd->ops.prepare = soc_pcm_prepare;
2745 rtd->ops.trigger = soc_pcm_trigger;
2746 rtd->ops.hw_free = soc_pcm_hw_free;
2747 rtd->ops.close = soc_pcm_close;
2748 rtd->ops.pointer = soc_pcm_pointer;
2749 rtd->ops.ioctl = soc_pcm_ioctl;
2752 if (platform->driver->ops) {
2753 rtd->ops.ack = platform->driver->ops->ack;
2754 rtd->ops.copy_user = platform->driver->ops->copy_user;
2755 rtd->ops.copy_kernel = platform->driver->ops->copy_kernel;
2756 rtd->ops.fill_silence = platform->driver->ops->fill_silence;
2757 rtd->ops.page = platform->driver->ops->page;
2758 rtd->ops.mmap = platform->driver->ops->mmap;
2762 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2765 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2767 for_each_rtdcom(rtd, rtdcom) {
2768 component = rtdcom->component;
2770 if (!component->pcm_new)
2773 ret = component->pcm_new(component, rtd);
2775 dev_err(component->dev,
2776 "ASoC: pcm constructor failed: %d\n",
2782 pcm->private_free = soc_pcm_private_free;
2784 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2785 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2790 /* is the current PCM operation for this FE ? */
2791 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2793 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2797 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2799 /* is the current PCM operation for this BE ? */
2800 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2801 struct snd_soc_pcm_runtime *be, int stream)
2803 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2804 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2805 be->dpcm[stream].runtime_update))
2809 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2811 /* get the substream for this BE */
2812 struct snd_pcm_substream *
2813 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2815 return be->pcm->streams[stream].substream;
2817 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2819 /* get the BE runtime state */
2820 enum snd_soc_dpcm_state
2821 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2823 return be->dpcm[stream].state;
2825 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2827 /* set the BE runtime state */
2828 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2829 int stream, enum snd_soc_dpcm_state state)
2831 be->dpcm[stream].state = state;
2833 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2836 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2837 * are not running, paused or suspended for the specified stream direction.
2839 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2840 struct snd_soc_pcm_runtime *be, int stream)
2842 struct snd_soc_dpcm *dpcm;
2845 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2850 state = dpcm->fe->dpcm[stream].state;
2851 if (state == SND_SOC_DPCM_STATE_START ||
2852 state == SND_SOC_DPCM_STATE_PAUSED ||
2853 state == SND_SOC_DPCM_STATE_SUSPEND)
2857 /* it's safe to free/stop this BE DAI */
2860 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2863 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2864 * running, paused or suspended for the specified stream direction.
2866 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2867 struct snd_soc_pcm_runtime *be, int stream)
2869 struct snd_soc_dpcm *dpcm;
2872 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2877 state = dpcm->fe->dpcm[stream].state;
2878 if (state == SND_SOC_DPCM_STATE_START ||
2879 state == SND_SOC_DPCM_STATE_PAUSED ||
2880 state == SND_SOC_DPCM_STATE_SUSPEND ||
2881 state == SND_SOC_DPCM_STATE_PREPARE)
2885 /* it's safe to change hw_params */
2888 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2890 #ifdef CONFIG_DEBUG_FS
2891 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
2894 case SND_SOC_DPCM_STATE_NEW:
2896 case SND_SOC_DPCM_STATE_OPEN:
2898 case SND_SOC_DPCM_STATE_HW_PARAMS:
2900 case SND_SOC_DPCM_STATE_PREPARE:
2902 case SND_SOC_DPCM_STATE_START:
2904 case SND_SOC_DPCM_STATE_STOP:
2906 case SND_SOC_DPCM_STATE_SUSPEND:
2908 case SND_SOC_DPCM_STATE_PAUSED:
2910 case SND_SOC_DPCM_STATE_HW_FREE:
2912 case SND_SOC_DPCM_STATE_CLOSE:
2919 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2920 int stream, char *buf, size_t size)
2922 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2923 struct snd_soc_dpcm *dpcm;
2927 offset += snprintf(buf + offset, size - offset,
2928 "[%s - %s]\n", fe->dai_link->name,
2929 stream ? "Capture" : "Playback");
2931 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2932 dpcm_state_string(fe->dpcm[stream].state));
2934 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2935 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2936 offset += snprintf(buf + offset, size - offset,
2938 "Format = %s, Channels = %d, Rate = %d\n",
2939 snd_pcm_format_name(params_format(params)),
2940 params_channels(params),
2941 params_rate(params));
2944 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2946 if (list_empty(&fe->dpcm[stream].be_clients)) {
2947 offset += snprintf(buf + offset, size - offset,
2948 " No active DSP links\n");
2952 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2953 struct snd_soc_pcm_runtime *be = dpcm->be;
2954 params = &dpcm->hw_params;
2956 offset += snprintf(buf + offset, size - offset,
2957 "- %s\n", be->dai_link->name);
2959 offset += snprintf(buf + offset, size - offset,
2961 dpcm_state_string(be->dpcm[stream].state));
2963 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2964 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2965 offset += snprintf(buf + offset, size - offset,
2966 " Hardware Params: "
2967 "Format = %s, Channels = %d, Rate = %d\n",
2968 snd_pcm_format_name(params_format(params)),
2969 params_channels(params),
2970 params_rate(params));
2977 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2978 size_t count, loff_t *ppos)
2980 struct snd_soc_pcm_runtime *fe = file->private_data;
2981 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2984 buf = kmalloc(out_count, GFP_KERNEL);
2988 if (fe->cpu_dai->driver->playback.channels_min)
2989 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2990 buf + offset, out_count - offset);
2992 if (fe->cpu_dai->driver->capture.channels_min)
2993 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2994 buf + offset, out_count - offset);
2996 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3002 static const struct file_operations dpcm_state_fops = {
3003 .open = simple_open,
3004 .read = dpcm_state_read_file,
3005 .llseek = default_llseek,
3008 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3013 if (!rtd->card->debugfs_card_root)
3016 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3017 rtd->card->debugfs_card_root);
3018 if (!rtd->debugfs_dpcm_root) {
3020 "ASoC: Failed to create dpcm debugfs directory %s\n",
3021 rtd->dai_link->name);
3025 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3026 rtd, &dpcm_state_fops);