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 channels at all, it supports the stream */
52 return codec_stream->channels_min;
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_dai *cpu_dai = rtd->cpu_dai;
458 struct snd_soc_dai *codec_dai;
459 const char *codec_dai_name = "multicodec";
462 pinctrl_pm_select_default_state(cpu_dai->dev);
463 for (i = 0; i < rtd->num_codecs; i++)
464 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
465 pm_runtime_get_sync(cpu_dai->dev);
466 for (i = 0; i < rtd->num_codecs; i++)
467 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
468 pm_runtime_get_sync(platform->dev);
470 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
472 /* startup the audio subsystem */
473 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
474 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
476 dev_err(cpu_dai->dev, "ASoC: can't open interface"
477 " %s: %d\n", cpu_dai->name, ret);
482 if (platform->driver->ops && platform->driver->ops->open) {
483 ret = platform->driver->ops->open(substream);
485 dev_err(platform->dev, "ASoC: can't open platform"
486 " %s: %d\n", platform->component.name, ret);
491 for (i = 0; i < rtd->num_codecs; i++) {
492 codec_dai = rtd->codec_dais[i];
493 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
494 ret = codec_dai->driver->ops->startup(substream,
497 dev_err(codec_dai->dev,
498 "ASoC: can't open codec %s: %d\n",
499 codec_dai->name, ret);
504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
505 codec_dai->tx_mask = 0;
507 codec_dai->rx_mask = 0;
510 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511 ret = rtd->dai_link->ops->startup(substream);
513 pr_err("ASoC: %s startup failed: %d\n",
514 rtd->dai_link->name, ret);
519 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
520 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
523 /* Check that the codec and cpu DAIs are compatible */
524 soc_pcm_init_runtime_hw(substream);
526 if (rtd->num_codecs == 1)
527 codec_dai_name = rtd->codec_dai->name;
529 if (soc_pcm_has_symmetry(substream))
530 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
533 if (!runtime->hw.rates) {
534 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
535 codec_dai_name, cpu_dai->name);
538 if (!runtime->hw.formats) {
539 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
540 codec_dai_name, cpu_dai->name);
543 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
544 runtime->hw.channels_min > runtime->hw.channels_max) {
545 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
546 codec_dai_name, cpu_dai->name);
550 soc_pcm_apply_msb(substream);
552 /* Symmetry only applies if we've already got an active stream. */
553 if (cpu_dai->active) {
554 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
559 for (i = 0; i < rtd->num_codecs; i++) {
560 if (rtd->codec_dais[i]->active) {
561 ret = soc_pcm_apply_symmetry(substream,
568 pr_debug("ASoC: %s <-> %s info:\n",
569 codec_dai_name, cpu_dai->name);
570 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
571 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
572 runtime->hw.channels_max);
573 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
574 runtime->hw.rate_max);
578 snd_soc_runtime_activate(rtd, substream->stream);
580 mutex_unlock(&rtd->pcm_mutex);
584 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
585 rtd->dai_link->ops->shutdown(substream);
592 codec_dai = rtd->codec_dais[i];
593 if (codec_dai->driver->ops->shutdown)
594 codec_dai->driver->ops->shutdown(substream, codec_dai);
597 if (platform->driver->ops && platform->driver->ops->close)
598 platform->driver->ops->close(substream);
601 if (cpu_dai->driver->ops->shutdown)
602 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
604 mutex_unlock(&rtd->pcm_mutex);
606 pm_runtime_mark_last_busy(platform->dev);
607 pm_runtime_put_autosuspend(platform->dev);
608 for (i = 0; i < rtd->num_codecs; i++) {
609 pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
610 pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
613 pm_runtime_mark_last_busy(cpu_dai->dev);
614 pm_runtime_put_autosuspend(cpu_dai->dev);
615 for (i = 0; i < rtd->num_codecs; i++) {
616 if (!rtd->codec_dais[i]->active)
617 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
619 if (!cpu_dai->active)
620 pinctrl_pm_select_sleep_state(cpu_dai->dev);
626 * Power down the audio subsystem pmdown_time msecs after close is called.
627 * This is to ensure there are no pops or clicks in between any music tracks
628 * due to DAPM power cycling.
630 static void close_delayed_work(struct work_struct *work)
632 struct snd_soc_pcm_runtime *rtd =
633 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
634 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
636 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
638 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
639 codec_dai->driver->playback.stream_name,
640 codec_dai->playback_active ? "active" : "inactive",
641 rtd->pop_wait ? "yes" : "no");
643 /* are we waiting on this codec DAI stream */
644 if (rtd->pop_wait == 1) {
646 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
647 SND_SOC_DAPM_STREAM_STOP);
650 mutex_unlock(&rtd->pcm_mutex);
654 * Called by ALSA when a PCM substream is closed. Private data can be
655 * freed here. The cpu DAI, codec DAI, machine and platform are also
658 static int soc_pcm_close(struct snd_pcm_substream *substream)
660 struct snd_soc_pcm_runtime *rtd = substream->private_data;
661 struct snd_soc_platform *platform = rtd->platform;
662 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
663 struct snd_soc_dai *codec_dai;
666 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
668 snd_soc_runtime_deactivate(rtd, substream->stream);
670 /* clear the corresponding DAIs rate when inactive */
671 if (!cpu_dai->active)
674 for (i = 0; i < rtd->num_codecs; i++) {
675 codec_dai = rtd->codec_dais[i];
676 if (!codec_dai->active)
680 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
682 if (cpu_dai->driver->ops->shutdown)
683 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
685 for (i = 0; i < rtd->num_codecs; i++) {
686 codec_dai = rtd->codec_dais[i];
687 if (codec_dai->driver->ops->shutdown)
688 codec_dai->driver->ops->shutdown(substream, codec_dai);
691 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
692 rtd->dai_link->ops->shutdown(substream);
694 if (platform->driver->ops && platform->driver->ops->close)
695 platform->driver->ops->close(substream);
697 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
698 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
699 /* powered down playback stream now */
700 snd_soc_dapm_stream_event(rtd,
701 SNDRV_PCM_STREAM_PLAYBACK,
702 SND_SOC_DAPM_STREAM_STOP);
704 /* start delayed pop wq here for playback streams */
706 queue_delayed_work(system_power_efficient_wq,
708 msecs_to_jiffies(rtd->pmdown_time));
711 /* capture streams can be powered down now */
712 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
713 SND_SOC_DAPM_STREAM_STOP);
716 mutex_unlock(&rtd->pcm_mutex);
718 pm_runtime_mark_last_busy(platform->dev);
719 pm_runtime_put_autosuspend(platform->dev);
721 for (i = 0; i < rtd->num_codecs; i++) {
722 pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
723 pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
726 pm_runtime_mark_last_busy(cpu_dai->dev);
727 pm_runtime_put_autosuspend(cpu_dai->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 && 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 && 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 && 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 && 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);
859 ret = soc_pcm_params_symmetry(substream, params);
863 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
864 ret = rtd->dai_link->ops->hw_params(substream, params);
866 dev_err(rtd->card->dev, "ASoC: machine hw_params"
867 " failed: %d\n", ret);
872 for (i = 0; i < rtd->num_codecs; i++) {
873 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
874 struct snd_pcm_hw_params codec_params;
877 * Skip CODECs which don't support the current stream type,
878 * the idea being that if a CODEC is not used for the currently
879 * set up transfer direction, it should not need to be
880 * configured, especially since the configuration used might
881 * not even be supported by that CODEC. There may be cases
882 * however where a CODEC needs to be set up although it is
883 * actually not being used for the transfer, e.g. if a
884 * capture-only CODEC is acting as an LRCLK and/or BCLK master
885 * for the DAI link including a playback-only CODEC.
886 * If this becomes necessary, we will have to augment the
887 * machine driver setup with information on how to act, so
888 * we can do the right thing here.
890 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
893 /* copy params for each codec */
894 codec_params = *params;
896 /* fixup params based on TDM slot masks */
897 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
899 soc_pcm_codec_params_fixup(&codec_params,
902 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
904 soc_pcm_codec_params_fixup(&codec_params,
907 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
911 codec_dai->rate = params_rate(&codec_params);
912 codec_dai->channels = params_channels(&codec_params);
913 codec_dai->sample_bits = snd_pcm_format_physical_width(
914 params_format(&codec_params));
917 ret = soc_dai_hw_params(substream, params, cpu_dai);
921 if (platform->driver->ops && platform->driver->ops->hw_params) {
922 ret = platform->driver->ops->hw_params(substream, params);
924 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
925 platform->component.name, ret);
930 /* store the parameters for each DAIs */
931 cpu_dai->rate = params_rate(params);
932 cpu_dai->channels = params_channels(params);
933 cpu_dai->sample_bits =
934 snd_pcm_format_physical_width(params_format(params));
937 mutex_unlock(&rtd->pcm_mutex);
941 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
942 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
949 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
950 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
951 codec_dai->driver->ops->hw_free(substream, codec_dai);
955 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
956 rtd->dai_link->ops->hw_free(substream);
958 mutex_unlock(&rtd->pcm_mutex);
963 * Frees resources allocated by hw_params, can be called multiple times
965 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
967 struct snd_soc_pcm_runtime *rtd = substream->private_data;
968 struct snd_soc_platform *platform = rtd->platform;
969 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
970 struct snd_soc_dai *codec_dai;
971 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
974 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
976 /* clear the corresponding DAIs parameters when going to be inactive */
977 if (cpu_dai->active == 1) {
979 cpu_dai->channels = 0;
980 cpu_dai->sample_bits = 0;
983 for (i = 0; i < rtd->num_codecs; i++) {
984 codec_dai = rtd->codec_dais[i];
985 if (codec_dai->active == 1) {
987 codec_dai->channels = 0;
988 codec_dai->sample_bits = 0;
992 /* apply codec digital mute */
993 for (i = 0; i < rtd->num_codecs; i++) {
994 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
995 (!playback && rtd->codec_dais[i]->capture_active == 1))
996 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1000 /* free any machine hw params */
1001 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1002 rtd->dai_link->ops->hw_free(substream);
1004 /* free any DMA resources */
1005 if (platform->driver->ops && platform->driver->ops->hw_free)
1006 platform->driver->ops->hw_free(substream);
1008 /* now free hw params for the DAIs */
1009 for (i = 0; i < rtd->num_codecs; i++) {
1010 codec_dai = rtd->codec_dais[i];
1011 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1012 codec_dai->driver->ops->hw_free(substream, codec_dai);
1015 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1016 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1018 mutex_unlock(&rtd->pcm_mutex);
1022 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1024 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1025 struct snd_soc_platform *platform = rtd->platform;
1026 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1027 struct snd_soc_dai *codec_dai;
1030 for (i = 0; i < rtd->num_codecs; i++) {
1031 codec_dai = rtd->codec_dais[i];
1032 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1033 ret = codec_dai->driver->ops->trigger(substream,
1040 if (platform->driver->ops && platform->driver->ops->trigger) {
1041 ret = platform->driver->ops->trigger(substream, cmd);
1046 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1047 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1052 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1053 ret = rtd->dai_link->ops->trigger(substream, cmd);
1061 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1064 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1065 struct snd_soc_platform *platform = rtd->platform;
1066 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1067 struct snd_soc_dai *codec_dai;
1070 for (i = 0; i < rtd->num_codecs; i++) {
1071 codec_dai = rtd->codec_dais[i];
1072 if (codec_dai->driver->ops &&
1073 codec_dai->driver->ops->bespoke_trigger) {
1074 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1081 if (platform->driver->bespoke_trigger) {
1082 ret = platform->driver->bespoke_trigger(substream, cmd);
1087 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1088 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1095 * soc level wrapper for pointer callback
1096 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1097 * the runtime->delay will be updated accordingly.
1099 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1101 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1102 struct snd_soc_platform *platform = rtd->platform;
1103 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1104 struct snd_soc_dai *codec_dai;
1105 struct snd_pcm_runtime *runtime = substream->runtime;
1106 snd_pcm_uframes_t offset = 0;
1107 snd_pcm_sframes_t delay = 0;
1108 snd_pcm_sframes_t codec_delay = 0;
1111 if (platform->driver->ops && platform->driver->ops->pointer)
1112 offset = platform->driver->ops->pointer(substream);
1114 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1115 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1117 for (i = 0; i < rtd->num_codecs; i++) {
1118 codec_dai = rtd->codec_dais[i];
1119 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1120 codec_delay = max(codec_delay,
1121 codec_dai->driver->ops->delay(substream,
1124 delay += codec_delay;
1127 * None of the existing platform drivers implement delay(), so
1128 * for now the codec_dai of first multicodec entry is used
1130 if (platform->driver->delay)
1131 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1133 runtime->delay = delay;
1138 /* connect a FE and BE */
1139 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1140 struct snd_soc_pcm_runtime *be, int stream)
1142 struct snd_soc_dpcm *dpcm;
1144 /* only add new dpcms */
1145 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1146 if (dpcm->be == be && dpcm->fe == fe)
1150 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1156 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1157 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1158 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1159 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1161 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1162 stream ? "capture" : "playback", fe->dai_link->name,
1163 stream ? "<-" : "->", be->dai_link->name);
1165 #ifdef CONFIG_DEBUG_FS
1166 if (fe->debugfs_dpcm_root)
1167 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1168 fe->debugfs_dpcm_root, &dpcm->state);
1173 /* reparent a BE onto another FE */
1174 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1175 struct snd_soc_pcm_runtime *be, int stream)
1177 struct snd_soc_dpcm *dpcm;
1178 struct snd_pcm_substream *fe_substream, *be_substream;
1180 /* reparent if BE is connected to other FEs */
1181 if (!be->dpcm[stream].users)
1184 be_substream = snd_soc_dpcm_get_substream(be, stream);
1188 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1192 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1193 stream ? "capture" : "playback",
1194 dpcm->fe->dai_link->name,
1195 stream ? "<-" : "->", dpcm->be->dai_link->name);
1197 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1198 be_substream->runtime = fe_substream->runtime;
1203 /* disconnect a BE and FE */
1204 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1206 struct snd_soc_dpcm *dpcm, *d;
1208 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1209 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1210 stream ? "capture" : "playback",
1211 dpcm->be->dai_link->name);
1213 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1216 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1217 stream ? "capture" : "playback", fe->dai_link->name,
1218 stream ? "<-" : "->", dpcm->be->dai_link->name);
1220 /* BEs still alive need new FE */
1221 dpcm_be_reparent(fe, dpcm->be, stream);
1223 #ifdef CONFIG_DEBUG_FS
1224 debugfs_remove(dpcm->debugfs_state);
1226 list_del(&dpcm->list_be);
1227 list_del(&dpcm->list_fe);
1232 /* get BE for DAI widget and stream */
1233 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1234 struct snd_soc_dapm_widget *widget, int stream)
1236 struct snd_soc_pcm_runtime *be;
1239 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1240 list_for_each_entry(be, &card->rtd_list, list) {
1242 if (!be->dai_link->no_pcm)
1245 if (be->cpu_dai->playback_widget == widget)
1248 for (i = 0; i < be->num_codecs; i++) {
1249 struct snd_soc_dai *dai = be->codec_dais[i];
1250 if (dai->playback_widget == widget)
1256 list_for_each_entry(be, &card->rtd_list, list) {
1258 if (!be->dai_link->no_pcm)
1261 if (be->cpu_dai->capture_widget == widget)
1264 for (i = 0; i < be->num_codecs; i++) {
1265 struct snd_soc_dai *dai = be->codec_dais[i];
1266 if (dai->capture_widget == widget)
1272 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1273 stream ? "capture" : "playback", widget->name);
1277 static inline struct snd_soc_dapm_widget *
1278 dai_get_widget(struct snd_soc_dai *dai, int stream)
1280 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1281 return dai->playback_widget;
1283 return dai->capture_widget;
1286 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1287 struct snd_soc_dapm_widget *widget)
1291 for (i = 0; i < list->num_widgets; i++) {
1292 if (widget == list->widgets[i])
1299 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1300 enum snd_soc_dapm_direction dir)
1302 struct snd_soc_card *card = widget->dapm->card;
1303 struct snd_soc_pcm_runtime *rtd;
1306 if (dir == SND_SOC_DAPM_DIR_OUT) {
1307 list_for_each_entry(rtd, &card->rtd_list, list) {
1308 if (!rtd->dai_link->no_pcm)
1311 if (rtd->cpu_dai->playback_widget == widget)
1314 for (i = 0; i < rtd->num_codecs; ++i) {
1315 struct snd_soc_dai *dai = rtd->codec_dais[i];
1316 if (dai->playback_widget == widget)
1320 } else { /* SND_SOC_DAPM_DIR_IN */
1321 list_for_each_entry(rtd, &card->rtd_list, list) {
1322 if (!rtd->dai_link->no_pcm)
1325 if (rtd->cpu_dai->capture_widget == widget)
1328 for (i = 0; i < rtd->num_codecs; ++i) {
1329 struct snd_soc_dai *dai = rtd->codec_dais[i];
1330 if (dai->capture_widget == widget)
1339 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1340 int stream, struct snd_soc_dapm_widget_list **list)
1342 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1345 /* get number of valid DAI paths and their widgets */
1346 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1347 dpcm_end_walk_at_be);
1349 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1350 stream ? "capture" : "playback");
1355 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1356 struct snd_soc_dapm_widget_list **list_)
1358 struct snd_soc_dpcm *dpcm;
1359 struct snd_soc_dapm_widget_list *list = *list_;
1360 struct snd_soc_dapm_widget *widget;
1363 /* Destroy any old FE <--> BE connections */
1364 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1367 /* is there a valid CPU DAI widget for this BE */
1368 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1370 /* prune the BE if it's no longer in our active list */
1371 if (widget && widget_in_list(list, widget))
1374 /* is there a valid CODEC DAI widget for this BE */
1375 for (i = 0; i < dpcm->be->num_codecs; i++) {
1376 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1377 widget = dai_get_widget(dai, stream);
1379 /* prune the BE if it's no longer in our active list */
1380 if (widget && widget_in_list(list, widget))
1384 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1385 stream ? "capture" : "playback",
1386 dpcm->be->dai_link->name, fe->dai_link->name);
1387 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1388 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1392 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1396 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1397 struct snd_soc_dapm_widget_list **list_)
1399 struct snd_soc_card *card = fe->card;
1400 struct snd_soc_dapm_widget_list *list = *list_;
1401 struct snd_soc_pcm_runtime *be;
1402 int i, new = 0, err;
1404 /* Create any new FE <--> BE connections */
1405 for (i = 0; i < list->num_widgets; i++) {
1407 switch (list->widgets[i]->id) {
1408 case snd_soc_dapm_dai_in:
1409 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1412 case snd_soc_dapm_dai_out:
1413 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1420 /* is there a valid BE rtd for this widget */
1421 be = dpcm_get_be(card, list->widgets[i], stream);
1423 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1424 list->widgets[i]->name);
1428 /* make sure BE is a real BE */
1429 if (!be->dai_link->no_pcm)
1432 /* don't connect if FE is not running */
1433 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1436 /* newly connected FE and BE */
1437 err = dpcm_be_connect(fe, be, stream);
1439 dev_err(fe->dev, "ASoC: can't connect %s\n",
1440 list->widgets[i]->name);
1442 } else if (err == 0) /* already connected */
1446 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1450 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1455 * Find the corresponding BE DAIs that source or sink audio to this
1458 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1459 int stream, struct snd_soc_dapm_widget_list **list, int new)
1462 return dpcm_add_paths(fe, stream, list);
1464 return dpcm_prune_paths(fe, stream, list);
1467 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1469 struct snd_soc_dpcm *dpcm;
1471 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1472 dpcm->be->dpcm[stream].runtime_update =
1473 SND_SOC_DPCM_UPDATE_NO;
1476 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1479 struct snd_soc_dpcm *dpcm;
1481 /* disable any enabled and non active backends */
1482 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1484 struct snd_soc_pcm_runtime *be = dpcm->be;
1485 struct snd_pcm_substream *be_substream =
1486 snd_soc_dpcm_get_substream(be, stream);
1488 if (be->dpcm[stream].users == 0)
1489 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1490 stream ? "capture" : "playback",
1491 be->dpcm[stream].state);
1493 if (--be->dpcm[stream].users != 0)
1496 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1499 soc_pcm_close(be_substream);
1500 be_substream->runtime = NULL;
1501 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1505 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1507 struct snd_soc_dpcm *dpcm;
1510 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1511 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1513 struct snd_soc_pcm_runtime *be = dpcm->be;
1514 struct snd_pcm_substream *be_substream =
1515 snd_soc_dpcm_get_substream(be, stream);
1517 if (!be_substream) {
1518 dev_err(be->dev, "ASoC: no backend %s stream\n",
1519 stream ? "capture" : "playback");
1523 /* is this op for this BE ? */
1524 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1527 /* first time the dpcm is open ? */
1528 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1529 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1530 stream ? "capture" : "playback",
1531 be->dpcm[stream].state);
1533 if (be->dpcm[stream].users++ != 0)
1536 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1537 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1540 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1541 stream ? "capture" : "playback", be->dai_link->name);
1543 be_substream->runtime = be->dpcm[stream].runtime;
1544 err = soc_pcm_open(be_substream);
1546 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1547 be->dpcm[stream].users--;
1548 if (be->dpcm[stream].users < 0)
1549 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1550 stream ? "capture" : "playback",
1551 be->dpcm[stream].state);
1553 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1557 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1564 /* disable any enabled and non active backends */
1565 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1566 struct snd_soc_pcm_runtime *be = dpcm->be;
1567 struct snd_pcm_substream *be_substream =
1568 snd_soc_dpcm_get_substream(be, stream);
1570 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1573 if (be->dpcm[stream].users == 0)
1574 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1575 stream ? "capture" : "playback",
1576 be->dpcm[stream].state);
1578 if (--be->dpcm[stream].users != 0)
1581 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1584 soc_pcm_close(be_substream);
1585 be_substream->runtime = NULL;
1586 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1592 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1593 struct snd_soc_pcm_stream *stream,
1596 runtime->hw.rate_min = stream->rate_min;
1597 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1598 runtime->hw.channels_min = stream->channels_min;
1599 runtime->hw.channels_max = stream->channels_max;
1600 if (runtime->hw.formats)
1601 runtime->hw.formats &= formats & stream->formats;
1603 runtime->hw.formats = formats & stream->formats;
1604 runtime->hw.rates = stream->rates;
1607 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1609 struct snd_soc_pcm_runtime *fe = substream->private_data;
1610 struct snd_soc_dpcm *dpcm;
1611 u64 formats = ULLONG_MAX;
1612 int stream = substream->stream;
1614 if (!fe->dai_link->dpcm_merged_format)
1618 * It returns merged BE codec format
1619 * if FE want to use it (= dpcm_merged_format)
1622 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1623 struct snd_soc_pcm_runtime *be = dpcm->be;
1624 struct snd_soc_dai_driver *codec_dai_drv;
1625 struct snd_soc_pcm_stream *codec_stream;
1628 for (i = 0; i < be->num_codecs; i++) {
1630 * Skip CODECs which don't support the current stream
1631 * type. See soc_pcm_init_runtime_hw() for more details
1633 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1637 codec_dai_drv = be->codec_dais[i]->driver;
1638 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1639 codec_stream = &codec_dai_drv->playback;
1641 codec_stream = &codec_dai_drv->capture;
1643 formats &= codec_stream->formats;
1650 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1652 struct snd_pcm_runtime *runtime = substream->runtime;
1653 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1654 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1655 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1656 u64 format = dpcm_runtime_base_format(substream);
1658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1659 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1661 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1664 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1666 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1667 * for avoiding the race with trigger callback.
1668 * If the state is unset and a trigger is pending while the previous operation,
1669 * process the pending trigger action here.
1671 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1672 int stream, enum snd_soc_dpcm_update state)
1674 struct snd_pcm_substream *substream =
1675 snd_soc_dpcm_get_substream(fe, stream);
1677 snd_pcm_stream_lock_irq(substream);
1678 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1679 dpcm_fe_dai_do_trigger(substream,
1680 fe->dpcm[stream].trigger_pending - 1);
1681 fe->dpcm[stream].trigger_pending = 0;
1683 fe->dpcm[stream].runtime_update = state;
1684 snd_pcm_stream_unlock_irq(substream);
1687 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1690 struct snd_soc_dpcm *dpcm;
1691 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1692 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1695 /* apply symmetry for FE */
1696 if (soc_pcm_has_symmetry(fe_substream))
1697 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1699 /* Symmetry only applies if we've got an active stream. */
1700 if (fe_cpu_dai->active) {
1701 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1706 /* apply symmetry for BE */
1707 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1708 struct snd_soc_pcm_runtime *be = dpcm->be;
1709 struct snd_pcm_substream *be_substream =
1710 snd_soc_dpcm_get_substream(be, stream);
1711 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1714 if (rtd->dai_link->be_hw_params_fixup)
1717 if (soc_pcm_has_symmetry(be_substream))
1718 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1720 /* Symmetry only applies if we've got an active stream. */
1721 if (rtd->cpu_dai->active) {
1722 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1727 for (i = 0; i < rtd->num_codecs; i++) {
1728 if (rtd->codec_dais[i]->active) {
1729 err = soc_pcm_apply_symmetry(be_substream,
1730 rtd->codec_dais[i]);
1740 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1742 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1743 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1744 int stream = fe_substream->stream, ret = 0;
1746 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1748 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1750 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1754 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1756 /* start the DAI frontend */
1757 ret = soc_pcm_open(fe_substream);
1759 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1763 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1765 dpcm_set_fe_runtime(fe_substream);
1766 snd_pcm_limit_hw_rates(runtime);
1768 ret = dpcm_apply_symmetry(fe_substream, stream);
1770 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1775 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1779 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1781 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1785 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1787 struct snd_soc_dpcm *dpcm;
1789 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1790 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1792 struct snd_soc_pcm_runtime *be = dpcm->be;
1793 struct snd_pcm_substream *be_substream =
1794 snd_soc_dpcm_get_substream(be, stream);
1796 /* is this op for this BE ? */
1797 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1800 if (be->dpcm[stream].users == 0)
1801 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1802 stream ? "capture" : "playback",
1803 be->dpcm[stream].state);
1805 if (--be->dpcm[stream].users != 0)
1808 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1809 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1810 soc_pcm_hw_free(be_substream);
1811 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1814 dev_dbg(be->dev, "ASoC: close BE %s\n",
1815 be->dai_link->name);
1817 soc_pcm_close(be_substream);
1818 be_substream->runtime = NULL;
1820 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1825 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1827 struct snd_soc_pcm_runtime *fe = substream->private_data;
1828 int stream = substream->stream;
1830 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1832 /* shutdown the BEs */
1833 dpcm_be_dai_shutdown(fe, substream->stream);
1835 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1837 /* now shutdown the frontend */
1838 soc_pcm_close(substream);
1840 /* run the stream event for each BE */
1841 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1843 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1844 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1848 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1850 struct snd_soc_dpcm *dpcm;
1852 /* only hw_params backends that are either sinks or sources
1853 * to this frontend DAI */
1854 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1856 struct snd_soc_pcm_runtime *be = dpcm->be;
1857 struct snd_pcm_substream *be_substream =
1858 snd_soc_dpcm_get_substream(be, stream);
1860 /* is this op for this BE ? */
1861 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1864 /* only free hw when no longer used - check all FEs */
1865 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1868 /* do not free hw if this BE is used by other FE */
1869 if (be->dpcm[stream].users > 1)
1872 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1873 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1874 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1875 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1876 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1877 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1880 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1881 be->dai_link->name);
1883 soc_pcm_hw_free(be_substream);
1885 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1891 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1893 struct snd_soc_pcm_runtime *fe = substream->private_data;
1894 int err, stream = substream->stream;
1896 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1897 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1899 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1901 /* call hw_free on the frontend */
1902 err = soc_pcm_hw_free(substream);
1904 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1905 fe->dai_link->name);
1907 /* only hw_params backends that are either sinks or sources
1908 * to this frontend DAI */
1909 err = dpcm_be_dai_hw_free(fe, stream);
1911 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1912 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1914 mutex_unlock(&fe->card->mutex);
1918 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1920 struct snd_soc_dpcm *dpcm;
1923 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1925 struct snd_soc_pcm_runtime *be = dpcm->be;
1926 struct snd_pcm_substream *be_substream =
1927 snd_soc_dpcm_get_substream(be, stream);
1929 /* is this op for this BE ? */
1930 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1933 /* copy params for each dpcm */
1934 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1935 sizeof(struct snd_pcm_hw_params));
1937 /* perform any hw_params fixups */
1938 if (be->dai_link->be_hw_params_fixup) {
1939 ret = be->dai_link->be_hw_params_fixup(be,
1943 "ASoC: hw_params BE fixup failed %d\n",
1949 /* only allow hw_params() if no connected FEs are running */
1950 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1953 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1954 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1955 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1958 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1959 be->dai_link->name);
1961 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1963 dev_err(dpcm->be->dev,
1964 "ASoC: hw_params BE failed %d\n", ret);
1968 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1973 /* disable any enabled and non active backends */
1974 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1975 struct snd_soc_pcm_runtime *be = dpcm->be;
1976 struct snd_pcm_substream *be_substream =
1977 snd_soc_dpcm_get_substream(be, stream);
1979 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1982 /* only allow hw_free() if no connected FEs are running */
1983 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1986 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1987 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1988 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1989 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1992 soc_pcm_hw_free(be_substream);
1998 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1999 struct snd_pcm_hw_params *params)
2001 struct snd_soc_pcm_runtime *fe = substream->private_data;
2002 int ret, stream = substream->stream;
2004 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2005 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2007 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2008 sizeof(struct snd_pcm_hw_params));
2009 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2011 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2015 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2016 fe->dai_link->name, params_rate(params),
2017 params_channels(params), params_format(params));
2019 /* call hw_params on the frontend */
2020 ret = soc_pcm_hw_params(substream, params);
2022 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2023 dpcm_be_dai_hw_free(fe, stream);
2025 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2028 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2029 mutex_unlock(&fe->card->mutex);
2033 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2034 struct snd_pcm_substream *substream, int cmd)
2038 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2039 dpcm->be->dai_link->name, cmd);
2041 ret = soc_pcm_trigger(substream, cmd);
2043 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2048 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2051 struct snd_soc_dpcm *dpcm;
2054 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2056 struct snd_soc_pcm_runtime *be = dpcm->be;
2057 struct snd_pcm_substream *be_substream =
2058 snd_soc_dpcm_get_substream(be, stream);
2060 /* is this op for this BE ? */
2061 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2065 case SNDRV_PCM_TRIGGER_START:
2066 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2067 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2068 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2071 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2075 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2077 case SNDRV_PCM_TRIGGER_RESUME:
2078 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2081 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2085 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2087 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2088 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2091 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2095 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2097 case SNDRV_PCM_TRIGGER_STOP:
2098 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2099 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2102 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2105 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2109 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2111 case SNDRV_PCM_TRIGGER_SUSPEND:
2112 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2115 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2118 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2122 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2124 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2125 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2128 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2131 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2135 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2142 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2144 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2145 int cmd, bool fe_first)
2147 struct snd_soc_pcm_runtime *fe = substream->private_data;
2150 /* call trigger on the frontend before the backend. */
2152 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2153 fe->dai_link->name, cmd);
2155 ret = soc_pcm_trigger(substream, cmd);
2159 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2163 /* call trigger on the frontend after the backend. */
2164 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2168 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2169 fe->dai_link->name, cmd);
2171 ret = soc_pcm_trigger(substream, cmd);
2176 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2178 struct snd_soc_pcm_runtime *fe = substream->private_data;
2179 int stream = substream->stream;
2181 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2183 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2186 case SND_SOC_DPCM_TRIGGER_PRE:
2188 case SNDRV_PCM_TRIGGER_START:
2189 case SNDRV_PCM_TRIGGER_RESUME:
2190 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2191 case SNDRV_PCM_TRIGGER_DRAIN:
2192 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2194 case SNDRV_PCM_TRIGGER_STOP:
2195 case SNDRV_PCM_TRIGGER_SUSPEND:
2196 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2197 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2204 case SND_SOC_DPCM_TRIGGER_POST:
2206 case SNDRV_PCM_TRIGGER_START:
2207 case SNDRV_PCM_TRIGGER_RESUME:
2208 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2209 case SNDRV_PCM_TRIGGER_DRAIN:
2210 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2212 case SNDRV_PCM_TRIGGER_STOP:
2213 case SNDRV_PCM_TRIGGER_SUSPEND:
2214 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2215 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2222 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2223 /* bespoke trigger() - handles both FE and BEs */
2225 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2226 fe->dai_link->name, cmd);
2228 ret = soc_pcm_bespoke_trigger(substream, cmd);
2231 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2232 fe->dai_link->name);
2238 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2244 case SNDRV_PCM_TRIGGER_START:
2245 case SNDRV_PCM_TRIGGER_RESUME:
2246 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2247 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2249 case SNDRV_PCM_TRIGGER_STOP:
2250 case SNDRV_PCM_TRIGGER_SUSPEND:
2251 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2253 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2254 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2259 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2263 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2265 struct snd_soc_pcm_runtime *fe = substream->private_data;
2266 int stream = substream->stream;
2268 /* if FE's runtime_update is already set, we're in race;
2269 * process this trigger later at exit
2271 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2272 fe->dpcm[stream].trigger_pending = cmd + 1;
2273 return 0; /* delayed, assuming it's successful */
2276 /* we're alone, let's trigger */
2277 return dpcm_fe_dai_do_trigger(substream, cmd);
2280 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2282 struct snd_soc_dpcm *dpcm;
2285 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2287 struct snd_soc_pcm_runtime *be = dpcm->be;
2288 struct snd_pcm_substream *be_substream =
2289 snd_soc_dpcm_get_substream(be, stream);
2291 /* is this op for this BE ? */
2292 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2295 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2296 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2297 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2298 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2301 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2302 be->dai_link->name);
2304 ret = soc_pcm_prepare(be_substream);
2306 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2311 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2316 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2318 struct snd_soc_pcm_runtime *fe = substream->private_data;
2319 int stream = substream->stream, ret = 0;
2321 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2323 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2325 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2327 /* there is no point preparing this FE if there are no BEs */
2328 if (list_empty(&fe->dpcm[stream].be_clients)) {
2329 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2330 fe->dai_link->name);
2335 ret = dpcm_be_dai_prepare(fe, substream->stream);
2339 /* call prepare on the frontend */
2340 ret = soc_pcm_prepare(substream);
2342 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2343 fe->dai_link->name);
2347 /* run the stream event for each BE */
2348 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2349 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2352 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2353 mutex_unlock(&fe->card->mutex);
2358 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2359 unsigned int cmd, void *arg)
2361 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2362 struct snd_soc_platform *platform = rtd->platform;
2364 if (platform->driver->ops && platform->driver->ops->ioctl)
2365 return platform->driver->ops->ioctl(substream, cmd, arg);
2366 return snd_pcm_lib_ioctl(substream, cmd, arg);
2369 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2371 struct snd_pcm_substream *substream =
2372 snd_soc_dpcm_get_substream(fe, stream);
2373 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2376 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2377 stream ? "capture" : "playback", fe->dai_link->name);
2379 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2380 /* call bespoke trigger - FE takes care of all BE triggers */
2381 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2382 fe->dai_link->name);
2384 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2386 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2388 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2389 fe->dai_link->name);
2391 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2393 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2396 err = dpcm_be_dai_hw_free(fe, stream);
2398 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2400 err = dpcm_be_dai_shutdown(fe, stream);
2402 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2404 /* run the stream event for each BE */
2405 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2410 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2412 struct snd_pcm_substream *substream =
2413 snd_soc_dpcm_get_substream(fe, stream);
2414 struct snd_soc_dpcm *dpcm;
2415 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2418 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2419 stream ? "capture" : "playback", fe->dai_link->name);
2421 /* Only start the BE if the FE is ready */
2422 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2423 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2426 /* startup must always be called for new BEs */
2427 ret = dpcm_be_dai_startup(fe, stream);
2431 /* keep going if FE state is > open */
2432 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2435 ret = dpcm_be_dai_hw_params(fe, stream);
2439 /* keep going if FE state is > hw_params */
2440 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2444 ret = dpcm_be_dai_prepare(fe, stream);
2448 /* run the stream event for each BE */
2449 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2451 /* keep going if FE state is > prepare */
2452 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2453 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2456 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2457 /* call trigger on the frontend - FE takes care of all BE triggers */
2458 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2459 fe->dai_link->name);
2461 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2463 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2467 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2468 fe->dai_link->name);
2470 ret = dpcm_be_dai_trigger(fe, stream,
2471 SNDRV_PCM_TRIGGER_START);
2473 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2481 dpcm_be_dai_hw_free(fe, stream);
2483 dpcm_be_dai_shutdown(fe, stream);
2485 /* disconnect any non started BEs */
2486 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2487 struct snd_soc_pcm_runtime *be = dpcm->be;
2488 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2489 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2495 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2499 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2500 ret = dpcm_run_update_startup(fe, stream);
2502 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2503 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2508 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2512 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2513 ret = dpcm_run_update_shutdown(fe, stream);
2515 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2516 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2521 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2524 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2526 struct snd_soc_pcm_runtime *fe;
2527 int old, new, paths;
2529 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2530 list_for_each_entry(fe, &card->rtd_list, list) {
2531 struct snd_soc_dapm_widget_list *list;
2533 /* make sure link is FE */
2534 if (!fe->dai_link->dynamic)
2537 /* only check active links */
2538 if (!fe->cpu_dai->active)
2541 /* DAPM sync will call this to update DSP paths */
2542 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2543 fe->dai_link->name);
2545 /* skip if FE doesn't have playback capability */
2546 if (!fe->cpu_dai->driver->playback.channels_min
2547 || !fe->codec_dai->driver->playback.channels_min)
2550 /* skip if FE isn't currently playing */
2551 if (!fe->cpu_dai->playback_active
2552 || !fe->codec_dai->playback_active)
2555 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2557 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2558 fe->dai_link->name, "playback");
2559 mutex_unlock(&card->mutex);
2563 /* update any new playback paths */
2564 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2566 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2567 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2568 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2571 /* update any old playback paths */
2572 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2574 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2575 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2576 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2579 dpcm_path_put(&list);
2581 /* skip if FE doesn't have capture capability */
2582 if (!fe->cpu_dai->driver->capture.channels_min
2583 || !fe->codec_dai->driver->capture.channels_min)
2586 /* skip if FE isn't currently capturing */
2587 if (!fe->cpu_dai->capture_active
2588 || !fe->codec_dai->capture_active)
2591 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2593 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2594 fe->dai_link->name, "capture");
2595 mutex_unlock(&card->mutex);
2599 /* update any new capture paths */
2600 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2602 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2603 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2604 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2607 /* update any old capture paths */
2608 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2610 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2611 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2612 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2615 dpcm_path_put(&list);
2618 mutex_unlock(&card->mutex);
2621 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2623 struct snd_soc_dpcm *dpcm;
2624 struct list_head *clients =
2625 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2627 list_for_each_entry(dpcm, clients, list_be) {
2629 struct snd_soc_pcm_runtime *be = dpcm->be;
2632 if (be->dai_link->ignore_suspend)
2635 for (i = 0; i < be->num_codecs; i++) {
2636 struct snd_soc_dai *dai = be->codec_dais[i];
2637 struct snd_soc_dai_driver *drv = dai->driver;
2639 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2640 be->dai_link->name);
2642 if (drv->ops && drv->ops->digital_mute &&
2643 dai->playback_active)
2644 drv->ops->digital_mute(dai, mute);
2651 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2653 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2654 struct snd_soc_dpcm *dpcm;
2655 struct snd_soc_dapm_widget_list *list;
2657 int stream = fe_substream->stream;
2659 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2660 fe->dpcm[stream].runtime = fe_substream->runtime;
2662 ret = dpcm_path_get(fe, stream, &list);
2664 mutex_unlock(&fe->card->mutex);
2666 } else if (ret == 0) {
2667 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2668 fe->dai_link->name, stream ? "capture" : "playback");
2671 /* calculate valid and active FE <-> BE dpcms */
2672 dpcm_process_paths(fe, stream, &list, 1);
2674 ret = dpcm_fe_dai_startup(fe_substream);
2676 /* clean up all links */
2677 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2678 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2680 dpcm_be_disconnect(fe, stream);
2681 fe->dpcm[stream].runtime = NULL;
2684 dpcm_clear_pending_state(fe, stream);
2685 dpcm_path_put(&list);
2686 mutex_unlock(&fe->card->mutex);
2690 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2692 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2693 struct snd_soc_dpcm *dpcm;
2694 int stream = fe_substream->stream, ret;
2696 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2697 ret = dpcm_fe_dai_shutdown(fe_substream);
2699 /* mark FE's links ready to prune */
2700 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2701 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2703 dpcm_be_disconnect(fe, stream);
2705 fe->dpcm[stream].runtime = NULL;
2706 mutex_unlock(&fe->card->mutex);
2710 /* create a new pcm */
2711 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2713 struct snd_soc_platform *platform = rtd->platform;
2714 struct snd_soc_dai *codec_dai;
2715 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2716 struct snd_pcm *pcm;
2718 int ret = 0, playback = 0, capture = 0;
2721 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2722 playback = rtd->dai_link->dpcm_playback;
2723 capture = rtd->dai_link->dpcm_capture;
2725 for (i = 0; i < rtd->num_codecs; i++) {
2726 codec_dai = rtd->codec_dais[i];
2727 if (codec_dai->driver->playback.channels_min)
2729 if (codec_dai->driver->capture.channels_min)
2733 capture = capture && cpu_dai->driver->capture.channels_min;
2734 playback = playback && cpu_dai->driver->playback.channels_min;
2737 if (rtd->dai_link->playback_only) {
2742 if (rtd->dai_link->capture_only) {
2747 /* create the PCM */
2748 if (rtd->dai_link->no_pcm) {
2749 snprintf(new_name, sizeof(new_name), "(%s)",
2750 rtd->dai_link->stream_name);
2752 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2753 playback, capture, &pcm);
2755 if (rtd->dai_link->dynamic)
2756 snprintf(new_name, sizeof(new_name), "%s (*)",
2757 rtd->dai_link->stream_name);
2759 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2760 rtd->dai_link->stream_name,
2761 (rtd->num_codecs > 1) ?
2762 "multicodec" : rtd->codec_dai->name, num);
2764 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2768 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2769 rtd->dai_link->name);
2772 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2774 /* DAPM dai link stream work */
2775 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2777 pcm->nonatomic = rtd->dai_link->nonatomic;
2779 pcm->private_data = rtd;
2781 if (rtd->dai_link->no_pcm) {
2783 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2785 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2789 /* ASoC PCM operations */
2790 if (rtd->dai_link->dynamic) {
2791 rtd->ops.open = dpcm_fe_dai_open;
2792 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2793 rtd->ops.prepare = dpcm_fe_dai_prepare;
2794 rtd->ops.trigger = dpcm_fe_dai_trigger;
2795 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2796 rtd->ops.close = dpcm_fe_dai_close;
2797 rtd->ops.pointer = soc_pcm_pointer;
2798 rtd->ops.ioctl = soc_pcm_ioctl;
2800 rtd->ops.open = soc_pcm_open;
2801 rtd->ops.hw_params = soc_pcm_hw_params;
2802 rtd->ops.prepare = soc_pcm_prepare;
2803 rtd->ops.trigger = soc_pcm_trigger;
2804 rtd->ops.hw_free = soc_pcm_hw_free;
2805 rtd->ops.close = soc_pcm_close;
2806 rtd->ops.pointer = soc_pcm_pointer;
2807 rtd->ops.ioctl = soc_pcm_ioctl;
2810 if (platform->driver->ops) {
2811 rtd->ops.ack = platform->driver->ops->ack;
2812 rtd->ops.copy = platform->driver->ops->copy;
2813 rtd->ops.silence = platform->driver->ops->silence;
2814 rtd->ops.page = platform->driver->ops->page;
2815 rtd->ops.mmap = platform->driver->ops->mmap;
2819 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2822 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2824 if (platform->driver->pcm_new) {
2825 ret = platform->driver->pcm_new(rtd);
2827 dev_err(platform->dev,
2828 "ASoC: pcm constructor failed: %d\n",
2834 pcm->private_free = platform->driver->pcm_free;
2836 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2837 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2842 /* is the current PCM operation for this FE ? */
2843 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2845 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2849 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2851 /* is the current PCM operation for this BE ? */
2852 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2853 struct snd_soc_pcm_runtime *be, int stream)
2855 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2856 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2857 be->dpcm[stream].runtime_update))
2861 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2863 /* get the substream for this BE */
2864 struct snd_pcm_substream *
2865 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2867 return be->pcm->streams[stream].substream;
2869 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2871 /* get the BE runtime state */
2872 enum snd_soc_dpcm_state
2873 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2875 return be->dpcm[stream].state;
2877 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2879 /* set the BE runtime state */
2880 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2881 int stream, enum snd_soc_dpcm_state state)
2883 be->dpcm[stream].state = state;
2885 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2888 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2889 * are not running, paused or suspended for the specified stream direction.
2891 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2892 struct snd_soc_pcm_runtime *be, int stream)
2894 struct snd_soc_dpcm *dpcm;
2897 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2902 state = dpcm->fe->dpcm[stream].state;
2903 if (state == SND_SOC_DPCM_STATE_START ||
2904 state == SND_SOC_DPCM_STATE_PAUSED ||
2905 state == SND_SOC_DPCM_STATE_SUSPEND)
2909 /* it's safe to free/stop this BE DAI */
2912 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2915 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2916 * running, paused or suspended for the specified stream direction.
2918 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2919 struct snd_soc_pcm_runtime *be, int stream)
2921 struct snd_soc_dpcm *dpcm;
2924 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2929 state = dpcm->fe->dpcm[stream].state;
2930 if (state == SND_SOC_DPCM_STATE_START ||
2931 state == SND_SOC_DPCM_STATE_PAUSED ||
2932 state == SND_SOC_DPCM_STATE_SUSPEND ||
2933 state == SND_SOC_DPCM_STATE_PREPARE)
2937 /* it's safe to change hw_params */
2940 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2942 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2943 int cmd, struct snd_soc_platform *platform)
2945 if (platform->driver->ops && platform->driver->ops->trigger)
2946 return platform->driver->ops->trigger(substream, cmd);
2949 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2951 #ifdef CONFIG_DEBUG_FS
2952 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2955 case SND_SOC_DPCM_STATE_NEW:
2957 case SND_SOC_DPCM_STATE_OPEN:
2959 case SND_SOC_DPCM_STATE_HW_PARAMS:
2961 case SND_SOC_DPCM_STATE_PREPARE:
2963 case SND_SOC_DPCM_STATE_START:
2965 case SND_SOC_DPCM_STATE_STOP:
2967 case SND_SOC_DPCM_STATE_SUSPEND:
2969 case SND_SOC_DPCM_STATE_PAUSED:
2971 case SND_SOC_DPCM_STATE_HW_FREE:
2973 case SND_SOC_DPCM_STATE_CLOSE:
2980 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2981 int stream, char *buf, size_t size)
2983 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2984 struct snd_soc_dpcm *dpcm;
2988 offset += scnprintf(buf + offset, size - offset,
2989 "[%s - %s]\n", fe->dai_link->name,
2990 stream ? "Capture" : "Playback");
2992 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
2993 dpcm_state_string(fe->dpcm[stream].state));
2995 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2996 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2997 offset += scnprintf(buf + offset, size - offset,
2999 "Format = %s, Channels = %d, Rate = %d\n",
3000 snd_pcm_format_name(params_format(params)),
3001 params_channels(params),
3002 params_rate(params));
3005 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
3007 if (list_empty(&fe->dpcm[stream].be_clients)) {
3008 offset += scnprintf(buf + offset, size - offset,
3009 " No active DSP links\n");
3013 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3014 struct snd_soc_pcm_runtime *be = dpcm->be;
3015 params = &dpcm->hw_params;
3017 offset += scnprintf(buf + offset, size - offset,
3018 "- %s\n", be->dai_link->name);
3020 offset += scnprintf(buf + offset, size - offset,
3022 dpcm_state_string(be->dpcm[stream].state));
3024 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3025 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3026 offset += scnprintf(buf + offset, size - offset,
3027 " Hardware Params: "
3028 "Format = %s, Channels = %d, Rate = %d\n",
3029 snd_pcm_format_name(params_format(params)),
3030 params_channels(params),
3031 params_rate(params));
3038 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3039 size_t count, loff_t *ppos)
3041 struct snd_soc_pcm_runtime *fe = file->private_data;
3042 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3045 buf = kmalloc(out_count, GFP_KERNEL);
3049 if (fe->cpu_dai->driver->playback.channels_min)
3050 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3051 buf + offset, out_count - offset);
3053 if (fe->cpu_dai->driver->capture.channels_min)
3054 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3055 buf + offset, out_count - offset);
3057 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3063 static const struct file_operations dpcm_state_fops = {
3064 .open = simple_open,
3065 .read = dpcm_state_read_file,
3066 .llseek = default_llseek,
3069 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3074 if (!rtd->card->debugfs_card_root)
3077 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3078 rtd->card->debugfs_card_root);
3079 if (!rtd->debugfs_dpcm_root) {
3081 "ASoC: Failed to create dpcm debugfs directory %s\n",
3082 rtd->dai_link->name);
3086 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
3087 rtd->debugfs_dpcm_root,
3088 rtd, &dpcm_state_fops);