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 <linux/dma-mapping.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include <sound/soc-dpcm.h>
34 #include <sound/initval.h>
36 #define DPCM_MAX_BE_USERS 8
39 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
41 * Returns true if the DAI supports the indicated stream type.
43 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
45 struct snd_soc_pcm_stream *codec_stream;
47 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
48 codec_stream = &dai->driver->playback;
50 codec_stream = &dai->driver->capture;
52 /* If the codec specifies any channels at all, it supports the stream */
53 return codec_stream->channels_min;
56 static const struct snd_pcm_hardware no_host_hardware = {
57 .info = SNDRV_PCM_INFO_MMAP |
58 SNDRV_PCM_INFO_MMAP_VALID |
59 SNDRV_PCM_INFO_INTERLEAVED |
60 SNDRV_PCM_INFO_PAUSE |
61 SNDRV_PCM_INFO_RESUME,
62 .formats = SNDRV_PCM_FMTBIT_S16_LE |
63 SNDRV_PCM_FMTBIT_S32_LE,
64 .period_bytes_min = PAGE_SIZE >> 2,
65 .period_bytes_max = PAGE_SIZE >> 1,
69 * Increase the max buffer bytes as PAGE_SIZE bytes is
70 * not enough to encompass all the scenarios sent by
73 .buffer_bytes_max = PAGE_SIZE * 4,
77 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
78 * @rtd: ASoC PCM runtime that is activated
79 * @stream: Direction of the PCM stream
81 * Increments the active count for all the DAIs and components attached to a PCM
82 * runtime. Should typically be called when a stream is opened.
84 * Must be called with the rtd->pcm_mutex being held
86 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
88 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
91 lockdep_assert_held(&rtd->pcm_mutex);
93 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
94 cpu_dai->playback_active++;
95 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->playback_active++;
98 cpu_dai->capture_active++;
99 for (i = 0; i < rtd->num_codecs; i++)
100 rtd->codec_dais[i]->capture_active++;
104 cpu_dai->component->active++;
105 for (i = 0; i < rtd->num_codecs; i++) {
106 rtd->codec_dais[i]->active++;
107 rtd->codec_dais[i]->component->active++;
112 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
113 * @rtd: ASoC PCM runtime that is deactivated
114 * @stream: Direction of the PCM stream
116 * Decrements the active count for all the DAIs and components attached to a PCM
117 * runtime. Should typically be called when a stream is closed.
119 * Must be called with the rtd->pcm_mutex being held
121 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
123 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
126 lockdep_assert_held(&rtd->pcm_mutex);
128 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
129 cpu_dai->playback_active--;
130 for (i = 0; i < rtd->num_codecs; i++)
131 rtd->codec_dais[i]->playback_active--;
133 cpu_dai->capture_active--;
134 for (i = 0; i < rtd->num_codecs; i++)
135 rtd->codec_dais[i]->capture_active--;
139 cpu_dai->component->active--;
140 for (i = 0; i < rtd->num_codecs; i++) {
141 rtd->codec_dais[i]->component->active--;
142 rtd->codec_dais[i]->active--;
147 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
148 * @rtd: The ASoC PCM runtime that should be checked.
150 * This function checks whether the power down delay should be ignored for a
151 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
152 * been configured to ignore the delay, or if none of the components benefits
153 * from having the delay.
155 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
160 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
163 for (i = 0; i < rtd->num_codecs; i++)
164 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
166 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
170 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
171 * @substream: the pcm substream
172 * @hw: the hardware parameters
174 * Sets the substream runtime hardware parameters.
176 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
177 const struct snd_pcm_hardware *hw)
179 struct snd_pcm_runtime *runtime = substream->runtime;
182 runtime->hw.info = hw->info;
183 runtime->hw.formats = hw->formats;
184 runtime->hw.period_bytes_min = hw->period_bytes_min;
185 runtime->hw.period_bytes_max = hw->period_bytes_max;
186 runtime->hw.periods_min = hw->periods_min;
187 runtime->hw.periods_max = hw->periods_max;
188 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
189 runtime->hw.fifo_size = hw->fifo_size;
192 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
194 /* DPCM stream event, send event to FE and all active BEs. */
195 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
198 struct snd_soc_dpcm *dpcm;
200 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
202 struct snd_soc_pcm_runtime *be = dpcm->be;
204 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
205 be->dai_link->name, event, dir);
207 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
208 (be->dpcm[dir].users >= 1)) {
209 pr_debug("%s Don't close BE \n", __func__);
213 snd_soc_dapm_stream_event(be, dir, event);
216 snd_soc_dapm_stream_event(fe, dir, event);
221 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
222 struct snd_soc_dai *soc_dai)
224 struct snd_soc_pcm_runtime *rtd = substream->private_data;
227 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
228 rtd->dai_link->symmetric_rates)) {
229 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
232 ret = snd_pcm_hw_constraint_single(substream->runtime,
233 SNDRV_PCM_HW_PARAM_RATE,
236 dev_err(soc_dai->dev,
237 "ASoC: Unable to apply rate constraint: %d\n",
243 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
244 rtd->dai_link->symmetric_channels)) {
245 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
248 ret = snd_pcm_hw_constraint_single(substream->runtime,
249 SNDRV_PCM_HW_PARAM_CHANNELS,
252 dev_err(soc_dai->dev,
253 "ASoC: Unable to apply channel symmetry constraint: %d\n",
259 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
260 rtd->dai_link->symmetric_samplebits)) {
261 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
262 soc_dai->sample_bits);
264 ret = snd_pcm_hw_constraint_single(substream->runtime,
265 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
266 soc_dai->sample_bits);
268 dev_err(soc_dai->dev,
269 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
278 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
279 struct snd_pcm_hw_params *params)
281 struct snd_soc_pcm_runtime *rtd = substream->private_data;
282 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
283 unsigned int rate, channels, sample_bits, symmetry, i;
285 rate = params_rate(params);
286 channels = params_channels(params);
287 sample_bits = snd_pcm_format_physical_width(params_format(params));
289 /* reject unmatched parameters when applying symmetry */
290 symmetry = cpu_dai->driver->symmetric_rates ||
291 rtd->dai_link->symmetric_rates;
293 for (i = 0; i < rtd->num_codecs; i++)
294 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
296 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
297 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
298 cpu_dai->rate, rate);
302 symmetry = cpu_dai->driver->symmetric_channels ||
303 rtd->dai_link->symmetric_channels;
305 for (i = 0; i < rtd->num_codecs; i++)
306 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
308 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
309 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
310 cpu_dai->channels, channels);
314 symmetry = cpu_dai->driver->symmetric_samplebits ||
315 rtd->dai_link->symmetric_samplebits;
317 for (i = 0; i < rtd->num_codecs; i++)
318 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
320 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
321 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
322 cpu_dai->sample_bits, sample_bits);
329 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
331 struct snd_soc_pcm_runtime *rtd = substream->private_data;
332 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
333 struct snd_soc_dai_link *link = rtd->dai_link;
334 unsigned int symmetry, i;
336 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
337 cpu_driver->symmetric_channels || link->symmetric_channels ||
338 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
340 for (i = 0; i < rtd->num_codecs; i++)
341 symmetry = symmetry ||
342 rtd->codec_dais[i]->driver->symmetric_rates ||
343 rtd->codec_dais[i]->driver->symmetric_channels ||
344 rtd->codec_dais[i]->driver->symmetric_samplebits;
349 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
351 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
359 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
363 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
365 struct snd_soc_pcm_runtime *rtd = substream->private_data;
366 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
367 struct snd_soc_dai *codec_dai;
369 unsigned int bits = 0, cpu_bits;
371 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
372 for (i = 0; i < rtd->num_codecs; i++) {
373 codec_dai = rtd->codec_dais[i];
374 if (codec_dai->driver->playback.sig_bits == 0) {
378 bits = max(codec_dai->driver->playback.sig_bits, bits);
380 cpu_bits = cpu_dai->driver->playback.sig_bits;
382 for (i = 0; i < rtd->num_codecs; i++) {
383 codec_dai = rtd->codec_dais[i];
384 if (codec_dai->driver->capture.sig_bits == 0) {
388 bits = max(codec_dai->driver->capture.sig_bits, bits);
390 cpu_bits = cpu_dai->driver->capture.sig_bits;
393 soc_pcm_set_msb(substream, bits);
394 soc_pcm_set_msb(substream, cpu_bits);
397 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
399 struct snd_pcm_runtime *runtime = substream->runtime;
400 struct snd_pcm_hardware *hw = &runtime->hw;
401 struct snd_soc_pcm_runtime *rtd = substream->private_data;
402 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
403 struct snd_soc_dai_driver *codec_dai_drv;
404 struct snd_soc_pcm_stream *codec_stream;
405 struct snd_soc_pcm_stream *cpu_stream;
406 unsigned int chan_min = 0, chan_max = UINT_MAX;
407 unsigned int rate_min = 0, rate_max = UINT_MAX;
408 unsigned int rates = UINT_MAX;
409 u64 formats = ULLONG_MAX;
412 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
413 cpu_stream = &cpu_dai_drv->playback;
415 cpu_stream = &cpu_dai_drv->capture;
417 /* first calculate min/max only for CODECs in the DAI link */
418 for (i = 0; i < rtd->num_codecs; i++) {
421 * Skip CODECs which don't support the current stream type.
422 * Otherwise, since the rate, channel, and format values will
423 * zero in that case, we would have no usable settings left,
424 * causing the resulting setup to fail.
425 * At least one CODEC should match, otherwise we should have
426 * bailed out on a higher level, since there would be no
427 * CODEC to support the transfer direction in that case.
429 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
433 codec_dai_drv = rtd->codec_dais[i]->driver;
434 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
435 codec_stream = &codec_dai_drv->playback;
437 codec_stream = &codec_dai_drv->capture;
438 chan_min = max(chan_min, codec_stream->channels_min);
439 chan_max = min(chan_max, codec_stream->channels_max);
440 rate_min = max(rate_min, codec_stream->rate_min);
441 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
442 formats &= codec_stream->formats;
443 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
447 * chan min/max cannot be enforced if there are multiple CODEC DAIs
448 * connected to a single CPU DAI, use CPU DAI's directly and let
449 * channel allocation be fixed up later
451 if (rtd->num_codecs > 1) {
452 chan_min = cpu_stream->channels_min;
453 chan_max = cpu_stream->channels_max;
456 hw->channels_min = max(chan_min, cpu_stream->channels_min);
457 hw->channels_max = min(chan_max, cpu_stream->channels_max);
459 hw->formats &= formats & cpu_stream->formats;
461 hw->formats = formats & cpu_stream->formats;
462 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
464 snd_pcm_limit_hw_rates(runtime);
466 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
467 hw->rate_min = max(hw->rate_min, rate_min);
468 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
469 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
473 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
474 * then initialized and any private data can be allocated. This also calls
475 * startup for the cpu DAI, platform, machine and codec DAI.
477 static int soc_pcm_open(struct snd_pcm_substream *substream)
479 struct snd_soc_pcm_runtime *rtd = substream->private_data;
480 struct snd_pcm_runtime *runtime = substream->runtime;
481 struct snd_soc_platform *platform = rtd->platform;
482 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
483 struct snd_soc_dai *codec_dai;
484 const char *codec_dai_name = "multicodec";
487 pinctrl_pm_select_default_state(cpu_dai->dev);
488 for (i = 0; i < rtd->num_codecs; i++)
489 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
490 pm_runtime_get_sync(cpu_dai->dev);
491 for (i = 0; i < rtd->num_codecs; i++)
492 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
493 pm_runtime_get_sync(platform->dev);
495 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
496 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
497 snd_soc_set_runtime_hwparams(substream, &no_host_hardware);
499 /* startup the audio subsystem */
500 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
501 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
503 dev_err(cpu_dai->dev, "ASoC: can't open interface"
504 " %s: %d\n", cpu_dai->name, ret);
509 if (platform->driver->ops && platform->driver->ops->open) {
510 ret = platform->driver->ops->open(substream);
512 dev_err(platform->dev, "ASoC: can't open platform"
513 " %s: %d\n", platform->component.name, ret);
518 for (i = 0; i < rtd->num_codecs; i++) {
519 codec_dai = rtd->codec_dais[i];
520 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
521 ret = codec_dai->driver->ops->startup(substream,
524 dev_err(codec_dai->dev,
525 "ASoC: can't open codec %s: %d\n",
526 codec_dai->name, ret);
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
532 codec_dai->tx_mask = 0;
534 codec_dai->rx_mask = 0;
537 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
538 ret = rtd->dai_link->ops->startup(substream);
540 pr_err("ASoC: %s startup failed: %d\n",
541 rtd->dai_link->name, ret);
546 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
547 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
550 /* Check that the codec and cpu DAIs are compatible */
551 soc_pcm_init_runtime_hw(substream);
553 if (rtd->num_codecs == 1)
554 codec_dai_name = rtd->codec_dai->name;
556 if (soc_pcm_has_symmetry(substream))
557 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
560 if (!runtime->hw.rates) {
561 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
562 codec_dai_name, cpu_dai->name);
565 if (!runtime->hw.formats) {
566 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
567 codec_dai_name, cpu_dai->name);
570 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
571 runtime->hw.channels_min > runtime->hw.channels_max) {
572 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
573 codec_dai_name, cpu_dai->name);
577 soc_pcm_apply_msb(substream);
579 /* Symmetry only applies if we've already got an active stream. */
580 if (cpu_dai->active) {
581 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
586 for (i = 0; i < rtd->num_codecs; i++) {
587 if (rtd->codec_dais[i]->active) {
588 ret = soc_pcm_apply_symmetry(substream,
595 pr_debug("ASoC: %s <-> %s info:\n",
596 codec_dai_name, cpu_dai->name);
597 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
598 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
599 runtime->hw.channels_max);
600 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
601 runtime->hw.rate_max);
605 snd_soc_runtime_activate(rtd, substream->stream);
607 mutex_unlock(&rtd->pcm_mutex);
611 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
612 rtd->dai_link->ops->shutdown(substream);
619 codec_dai = rtd->codec_dais[i];
620 if (codec_dai->driver->ops->shutdown)
621 codec_dai->driver->ops->shutdown(substream, codec_dai);
624 if (platform->driver->ops && platform->driver->ops->close)
625 platform->driver->ops->close(substream);
628 if (cpu_dai->driver->ops && cpu_dai->driver->ops->shutdown)
629 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
631 mutex_unlock(&rtd->pcm_mutex);
633 pm_runtime_put(platform->dev);
634 for (i = 0; i < rtd->num_codecs; i++)
635 pm_runtime_put(rtd->codec_dais[i]->dev);
636 pm_runtime_put(cpu_dai->dev);
637 for (i = 0; i < rtd->num_codecs; i++) {
638 if (!rtd->codec_dais[i]->active)
639 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
641 if (!cpu_dai->active)
642 pinctrl_pm_select_sleep_state(cpu_dai->dev);
648 * Power down the audio subsystem pmdown_time msecs after close is called.
649 * This is to ensure there are no pops or clicks in between any music tracks
650 * due to DAPM power cycling.
652 static void close_delayed_work(struct work_struct *work)
654 struct snd_soc_pcm_runtime *rtd =
655 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
656 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
658 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
660 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
661 codec_dai->driver->playback.stream_name,
662 codec_dai->playback_active ? "active" : "inactive",
663 rtd->pop_wait ? "yes" : "no");
665 /* are we waiting on this codec DAI stream */
666 if (rtd->pop_wait == 1) {
668 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
669 SND_SOC_DAPM_STREAM_STOP);
672 mutex_unlock(&rtd->pcm_mutex);
676 * Called by ALSA when a PCM substream is closed. Private data can be
677 * freed here. The cpu DAI, codec DAI, machine and platform are also
680 static int soc_pcm_close(struct snd_pcm_substream *substream)
682 struct snd_soc_pcm_runtime *rtd = substream->private_data;
683 struct snd_soc_platform *platform = rtd->platform;
684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
685 struct snd_soc_dai *codec_dai;
688 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
690 snd_soc_runtime_deactivate(rtd, substream->stream);
692 /* clear the corresponding DAIs rate when inactive */
693 if (!cpu_dai->active)
696 for (i = 0; i < rtd->num_codecs; i++) {
697 codec_dai = rtd->codec_dais[i];
698 if (!codec_dai->active)
702 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
704 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
705 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
706 /* powered down playback stream now */
707 snd_soc_dapm_stream_event(rtd,
708 SNDRV_PCM_STREAM_PLAYBACK,
709 SND_SOC_DAPM_STREAM_STOP);
711 /* start delayed pop wq here for playback streams */
713 queue_delayed_work(system_power_efficient_wq,
715 msecs_to_jiffies(rtd->pmdown_time));
718 if (cpu_dai->driver->ops->shutdown)
719 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
721 for (i = 0; i < rtd->num_codecs; i++) {
722 codec_dai = rtd->codec_dais[i];
723 if (codec_dai->driver->ops->shutdown)
724 codec_dai->driver->ops->shutdown(substream, codec_dai);
727 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
728 rtd->dai_link->ops->shutdown(substream);
730 if (platform->driver->ops && platform->driver->ops->close)
731 platform->driver->ops->close(substream);
733 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
734 /* capture streams can be powered down now */
735 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
736 SND_SOC_DAPM_STREAM_STOP);
739 mutex_unlock(&rtd->pcm_mutex);
741 pm_runtime_put(platform->dev);
742 for (i = 0; i < rtd->num_codecs; i++)
743 pm_runtime_put(rtd->codec_dais[i]->dev);
744 pm_runtime_put(cpu_dai->dev);
745 for (i = 0; i < rtd->num_codecs; i++) {
746 if (!rtd->codec_dais[i]->active)
747 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
749 if (!cpu_dai->active)
750 pinctrl_pm_select_sleep_state(cpu_dai->dev);
756 * Called by ALSA when the PCM substream is prepared, can set format, sample
757 * rate, etc. This function is non atomic and can be called multiple times,
758 * it can refer to the runtime info.
760 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
762 struct snd_soc_pcm_runtime *rtd = substream->private_data;
763 struct snd_soc_platform *platform = rtd->platform;
764 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
765 struct snd_soc_dai *codec_dai;
768 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
770 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
771 snd_soc_dapm_stream_event(rtd,
772 SNDRV_PCM_STREAM_PLAYBACK,
773 SND_SOC_DAPM_STREAM_START);
775 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
776 ret = rtd->dai_link->ops->prepare(substream);
778 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
784 if (platform->driver->ops && platform->driver->ops->prepare) {
785 ret = platform->driver->ops->prepare(substream);
787 dev_err(platform->dev, "ASoC: platform prepare error:"
793 for (i = 0; i < rtd->num_codecs; i++) {
794 codec_dai = rtd->codec_dais[i];
795 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
796 ret = codec_dai->driver->ops->prepare(substream,
799 dev_err(codec_dai->dev,
800 "ASoC: codec DAI prepare error: %d\n",
807 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
808 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
810 dev_err(cpu_dai->dev,
811 "ASoC: cpu DAI prepare error: %d\n", ret);
816 /* cancel any delayed stream shutdown that is pending */
817 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
820 cancel_delayed_work(&rtd->delayed_work);
823 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
824 for (i = 0; i < rtd->num_codecs; i++) {
825 codec_dai = rtd->codec_dais[i];
826 if (codec_dai->capture_active == 1)
827 snd_soc_dapm_stream_event(rtd,
828 SNDRV_PCM_STREAM_CAPTURE,
829 SND_SOC_DAPM_STREAM_START);
833 for (i = 0; i < rtd->num_codecs; i++)
834 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
836 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
839 if (ret < 0 && substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
840 pr_err("%s: Issue stop stream for codec_dai due to op failure %d = ret\n",
842 snd_soc_dapm_stream_event(rtd,
843 SNDRV_PCM_STREAM_PLAYBACK,
844 SND_SOC_DAPM_STREAM_STOP);
846 mutex_unlock(&rtd->pcm_mutex);
850 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
853 struct snd_interval *interval;
854 int channels = hweight_long(mask);
856 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
857 interval->min = channels;
858 interval->max = channels;
861 int soc_dai_hw_params(struct snd_pcm_substream *substream,
862 struct snd_pcm_hw_params *params,
863 struct snd_soc_dai *dai)
867 if (dai->driver->ops && dai->driver->ops->hw_params) {
868 ret = dai->driver->ops->hw_params(substream, params, dai);
870 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
880 * Called by ALSA when the hardware params are set by application. This
881 * function can also be called multiple times and can allocate buffers
882 * (using snd_pcm_lib_* ). It's non-atomic.
884 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
885 struct snd_pcm_hw_params *params)
887 struct snd_soc_pcm_runtime *rtd = substream->private_data;
888 struct snd_soc_platform *platform = rtd->platform;
889 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
892 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
894 /* perform any hw_params fixups */
895 if ((rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) &&
896 rtd->dai_link->be_hw_params_fixup) {
897 ret = rtd->dai_link->be_hw_params_fixup(rtd,
900 dev_err(rtd->card->dev, "ASoC: fixup failed for %s\n",
901 rtd->dai_link->name);
904 ret = soc_pcm_params_symmetry(substream, params);
908 /* perform any hw_params fixups */
909 if ((rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) &&
910 rtd->dai_link->be_hw_params_fixup) {
911 ret = rtd->dai_link->be_hw_params_fixup(rtd,
914 dev_err(rtd->card->dev, "ASoC: fixup failed for %s\n",
915 rtd->dai_link->name);
919 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
920 ret = rtd->dai_link->ops->hw_params(substream, params);
922 dev_err(rtd->card->dev, "ASoC: machine hw_params"
923 " failed: %d\n", ret);
928 for (i = 0; i < rtd->num_codecs; i++) {
929 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
930 struct snd_pcm_hw_params codec_params;
933 * Skip CODECs which don't support the current stream type,
934 * the idea being that if a CODEC is not used for the currently
935 * set up transfer direction, it should not need to be
936 * configured, especially since the configuration used might
937 * not even be supported by that CODEC. There may be cases
938 * however where a CODEC needs to be set up although it is
939 * actually not being used for the transfer, e.g. if a
940 * capture-only CODEC is acting as an LRCLK and/or BCLK master
941 * for the DAI link including a playback-only CODEC.
942 * If this becomes necessary, we will have to augment the
943 * machine driver setup with information on how to act, so
944 * we can do the right thing here.
946 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
949 /* copy params for each codec */
950 codec_params = *params;
952 /* fixup params based on TDM slot masks */
953 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
955 soc_pcm_codec_params_fixup(&codec_params,
958 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
960 soc_pcm_codec_params_fixup(&codec_params,
963 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
967 codec_dai->rate = params_rate(&codec_params);
968 codec_dai->channels = params_channels(&codec_params);
969 codec_dai->sample_bits = snd_pcm_format_physical_width(
970 params_format(&codec_params));
973 ret = soc_dai_hw_params(substream, params, cpu_dai);
977 if (platform->driver->ops && platform->driver->ops->hw_params) {
978 ret = platform->driver->ops->hw_params(substream, params);
980 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
981 platform->component.name, ret);
986 /* store the parameters for each DAIs */
987 cpu_dai->rate = params_rate(params);
988 cpu_dai->channels = params_channels(params);
989 cpu_dai->sample_bits =
990 snd_pcm_format_physical_width(params_format(params));
991 /* malloc a page for hostless IO.
992 * FIXME: rework with alsa-lib changes so that this malloc is not required.
994 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
995 substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
996 substream->dma_buffer.dev.dev = rtd->dev;
997 substream->dma_buffer.dev.dev->coherent_dma_mask =
998 DMA_BIT_MASK(sizeof(dma_addr_t) * 8);
999 substream->dma_buffer.private_data = NULL;
1001 arch_setup_dma_ops(substream->dma_buffer.dev.dev,
1003 ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
1009 mutex_unlock(&rtd->pcm_mutex);
1013 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1014 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1017 i = rtd->num_codecs;
1021 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1022 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1023 codec_dai->driver->ops->hw_free(substream, codec_dai);
1024 codec_dai->rate = 0;
1027 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1028 rtd->dai_link->ops->hw_free(substream);
1030 mutex_unlock(&rtd->pcm_mutex);
1035 * Frees resources allocated by hw_params, can be called multiple times
1037 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1039 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1040 struct snd_soc_platform *platform = rtd->platform;
1041 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1042 struct snd_soc_dai *codec_dai;
1043 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1046 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1048 /* clear the corresponding DAIs parameters when going to be inactive */
1049 if (cpu_dai->active == 1) {
1051 cpu_dai->channels = 0;
1052 cpu_dai->sample_bits = 0;
1055 for (i = 0; i < rtd->num_codecs; i++) {
1056 codec_dai = rtd->codec_dais[i];
1057 if (codec_dai->active == 1) {
1058 codec_dai->rate = 0;
1059 codec_dai->channels = 0;
1060 codec_dai->sample_bits = 0;
1064 /* apply codec digital mute */
1065 for (i = 0; i < rtd->num_codecs; i++) {
1066 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1067 (!playback && rtd->codec_dais[i]->capture_active == 1))
1068 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1072 /* free any machine hw params */
1073 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1074 rtd->dai_link->ops->hw_free(substream);
1076 /* free any DMA resources */
1077 if (platform->driver->ops && platform->driver->ops->hw_free)
1078 platform->driver->ops->hw_free(substream);
1080 /* now free hw params for the DAIs */
1081 for (i = 0; i < rtd->num_codecs; i++) {
1082 codec_dai = rtd->codec_dais[i];
1083 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1084 codec_dai->driver->ops->hw_free(substream, codec_dai);
1087 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1088 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1090 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
1091 snd_pcm_lib_free_pages(substream);
1093 mutex_unlock(&rtd->pcm_mutex);
1097 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1099 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1100 struct snd_soc_platform *platform = rtd->platform;
1101 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1102 struct snd_soc_dai *codec_dai;
1105 for (i = 0; i < rtd->num_codecs; i++) {
1106 codec_dai = rtd->codec_dais[i];
1107 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1108 ret = codec_dai->driver->ops->trigger(substream,
1115 if (platform->driver->ops && platform->driver->ops->trigger) {
1116 ret = platform->driver->ops->trigger(substream, cmd);
1121 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1122 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1127 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1128 ret = rtd->dai_link->ops->trigger(substream, cmd);
1136 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1139 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1140 struct snd_soc_platform *platform = rtd->platform;
1141 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1142 struct snd_soc_dai *codec_dai;
1145 for (i = 0; i < rtd->num_codecs; i++) {
1146 codec_dai = rtd->codec_dais[i];
1147 if (codec_dai->driver->ops &&
1148 codec_dai->driver->ops->bespoke_trigger) {
1149 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1156 if (platform->driver->bespoke_trigger) {
1157 ret = platform->driver->bespoke_trigger(substream, cmd);
1162 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1163 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1170 * soc level wrapper for pointer callback
1171 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1172 * the runtime->delay will be updated accordingly.
1174 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1176 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1177 struct snd_soc_platform *platform = rtd->platform;
1178 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1179 struct snd_soc_dai *codec_dai;
1180 struct snd_pcm_runtime *runtime = substream->runtime;
1181 snd_pcm_uframes_t offset = 0;
1182 snd_pcm_sframes_t delay = 0;
1183 snd_pcm_sframes_t codec_delay = 0;
1186 if (platform->driver->ops && platform->driver->ops->pointer)
1187 offset = platform->driver->ops->pointer(substream);
1189 if (platform->driver->delay_blk)
1192 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1193 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1195 for (i = 0; i < rtd->num_codecs; i++) {
1196 codec_dai = rtd->codec_dais[i];
1197 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1198 codec_delay = max(codec_delay,
1199 codec_dai->driver->ops->delay(substream,
1202 delay += codec_delay;
1205 * None of the existing platform drivers implement delay(), so
1206 * for now the codec_dai of first multicodec entry is used
1208 if (platform->driver->delay)
1209 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1211 runtime->delay = delay;
1216 static int soc_pcm_delay_blk(struct snd_pcm_substream *substream)
1218 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1219 struct snd_soc_platform *platform = rtd->platform;
1220 struct snd_pcm_runtime *runtime = substream->runtime;
1221 snd_pcm_sframes_t delay = 0;
1223 if (platform->driver->delay_blk)
1224 delay = platform->driver->delay_blk(substream,
1225 rtd->codec_dais[0]);
1227 runtime->delay = delay;
1232 /* connect a FE and BE */
1233 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1234 struct snd_soc_pcm_runtime *be, int stream)
1236 struct snd_soc_dpcm *dpcm;
1238 /* only add new dpcms */
1239 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1240 if (dpcm->be == be && dpcm->fe == fe)
1244 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1250 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1251 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1252 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1253 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1255 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1256 stream ? "capture" : "playback", fe->dai_link->name,
1257 stream ? "<-" : "->", be->dai_link->name);
1259 #ifdef CONFIG_DEBUG_FS
1260 if (fe->debugfs_dpcm_root)
1261 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1262 fe->debugfs_dpcm_root, &dpcm->state);
1267 /* reparent a BE onto another FE */
1268 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1269 struct snd_soc_pcm_runtime *be, int stream)
1271 struct snd_soc_dpcm *dpcm;
1272 struct snd_pcm_substream *fe_substream, *be_substream;
1274 /* reparent if BE is connected to other FEs */
1275 if (!be->dpcm[stream].users)
1278 be_substream = snd_soc_dpcm_get_substream(be, stream);
1280 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1284 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1285 stream ? "capture" : "playback",
1286 dpcm->fe->dai_link->name,
1287 stream ? "<-" : "->", dpcm->be->dai_link->name);
1289 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1290 be_substream->runtime = fe_substream->runtime;
1295 /* disconnect a BE and FE */
1296 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1298 struct snd_soc_dpcm *dpcm, *d;
1300 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1301 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1302 stream ? "capture" : "playback",
1303 dpcm->be->dai_link->name);
1305 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1308 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1309 stream ? "capture" : "playback", fe->dai_link->name,
1310 stream ? "<-" : "->", dpcm->be->dai_link->name);
1312 /* BEs still alive need new FE */
1313 dpcm_be_reparent(fe, dpcm->be, stream);
1315 #ifdef CONFIG_DEBUG_FS
1316 debugfs_remove(dpcm->debugfs_state);
1318 list_del(&dpcm->list_be);
1319 list_del(&dpcm->list_fe);
1324 /* get BE for DAI widget and stream */
1325 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1326 struct snd_soc_dapm_widget *widget, int stream)
1328 struct snd_soc_pcm_runtime *be;
1331 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1332 for (i = 0; i < card->num_links; i++) {
1335 if (!be->dai_link->no_pcm)
1338 if ((be->cpu_dai->playback_widget == widget &&
1339 (be->dai_link->stream_name &&
1340 !strcmp(be->dai_link->stream_name,
1341 be->cpu_dai->playback_widget->sname))) ||
1342 be->codec_dai->playback_widget == widget)
1345 for (j = 0; j < be->num_codecs; j++) {
1346 struct snd_soc_dai *dai = be->codec_dais[j];
1347 if (dai->playback_widget == widget)
1353 for (i = 0; i < card->num_links; i++) {
1356 if (!be->dai_link->no_pcm)
1359 if ((be->cpu_dai->capture_widget == widget &&
1360 (be->dai_link->stream_name &&
1361 !strcmp(be->dai_link->stream_name,
1362 be->cpu_dai->capture_widget->sname))) ||
1363 be->codec_dai->capture_widget == widget)
1366 for (j = 0; j < be->num_codecs; j++) {
1367 struct snd_soc_dai *dai = be->codec_dais[j];
1368 if (dai->capture_widget == widget)
1374 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1375 stream ? "capture" : "playback", widget->name);
1379 static inline struct snd_soc_dapm_widget *
1380 dai_get_widget(struct snd_soc_dai *dai, int stream)
1382 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1383 return dai->playback_widget;
1385 return dai->capture_widget;
1388 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1389 struct snd_soc_dapm_widget *widget)
1393 for (i = 0; i < list->num_widgets; i++) {
1394 if (widget == list->widgets[i])
1401 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1402 int stream, struct snd_soc_dapm_widget_list **list)
1404 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1407 /* get number of valid DAI paths and their widgets */
1408 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
1410 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1411 stream ? "capture" : "playback");
1416 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1417 struct snd_soc_dapm_widget_list **list_)
1419 struct snd_soc_dpcm *dpcm;
1420 struct snd_soc_dapm_widget_list *list = *list_;
1421 struct snd_soc_dapm_widget *widget;
1424 /* Destroy any old FE <--> BE connections */
1425 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1428 /* is there a valid CPU DAI widget for this BE */
1429 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1431 /* prune the BE if it's no longer in our active list */
1432 if (widget && widget_in_list(list, widget))
1435 /* is there a valid CODEC DAI widget for this BE */
1436 for (i = 0; i < dpcm->be->num_codecs; i++) {
1437 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1438 widget = dai_get_widget(dai, stream);
1440 /* prune the BE if it's no longer in our active list */
1441 if (widget && widget_in_list(list, widget))
1445 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1446 stream ? "capture" : "playback",
1447 dpcm->be->dai_link->name, fe->dai_link->name);
1448 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1449 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1453 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1457 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1458 struct snd_soc_dapm_widget_list **list_)
1460 struct snd_soc_card *card = fe->card;
1461 struct snd_soc_dapm_widget_list *list = *list_;
1462 struct snd_soc_pcm_runtime *be;
1463 int i, new = 0, err;
1465 /* Create any new FE <--> BE connections */
1466 for (i = 0; i < list->num_widgets; i++) {
1468 switch (list->widgets[i]->id) {
1469 case snd_soc_dapm_dai_in:
1470 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1473 case snd_soc_dapm_dai_out:
1474 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1481 /* is there a valid BE rtd for this widget */
1482 be = dpcm_get_be(card, list->widgets[i], stream);
1484 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1485 list->widgets[i]->name);
1489 /* make sure BE is a real BE */
1490 if (!be->dai_link->no_pcm)
1493 /* don't connect if FE is not running */
1494 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1497 /* newly connected FE and BE */
1498 err = dpcm_be_connect(fe, be, stream);
1500 dev_err(fe->dev, "ASoC: can't connect %s\n",
1501 list->widgets[i]->name);
1503 } else if (err == 0) /* already connected */
1507 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1511 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1516 * Find the corresponding BE DAIs that source or sink audio to this
1519 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1520 int stream, struct snd_soc_dapm_widget_list **list, int new)
1523 return dpcm_add_paths(fe, stream, list);
1525 return dpcm_prune_paths(fe, stream, list);
1528 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1530 struct snd_soc_dpcm *dpcm;
1532 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1533 dpcm->be->dpcm[stream].runtime_update =
1534 SND_SOC_DPCM_UPDATE_NO;
1537 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1540 struct snd_soc_dpcm *dpcm;
1542 /* disable any enabled and non active backends */
1543 list_for_each_entry(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 (be->dpcm[stream].users == 0)
1550 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1551 stream ? "capture" : "playback",
1552 be->dpcm[stream].state);
1554 if (--be->dpcm[stream].users != 0)
1557 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1560 soc_pcm_close(be_substream);
1561 be_substream->runtime = NULL;
1562 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1566 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1568 struct snd_soc_dpcm *dpcm;
1571 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1572 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1574 struct snd_soc_pcm_runtime *be = dpcm->be;
1575 struct snd_pcm_substream *be_substream =
1576 snd_soc_dpcm_get_substream(be, stream);
1578 if (!be_substream) {
1579 dev_err(be->dev, "ASoC: no backend %s stream\n",
1580 stream ? "capture" : "playback");
1584 /* is this op for this BE ? */
1585 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1588 /* first time the dpcm is open ? */
1589 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1590 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1591 stream ? "capture" : "playback",
1592 be->dpcm[stream].state);
1594 if (be->dpcm[stream].users++ != 0)
1597 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1598 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1601 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1602 stream ? "capture" : "playback", be->dai_link->name);
1604 be_substream->runtime = be->dpcm[stream].runtime;
1605 err = soc_pcm_open(be_substream);
1607 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1608 be->dpcm[stream].users--;
1609 if (be->dpcm[stream].users < 0)
1610 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1611 stream ? "capture" : "playback",
1612 be->dpcm[stream].state);
1614 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1618 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1625 /* disable any enabled and non active backends */
1626 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1627 struct snd_soc_pcm_runtime *be = dpcm->be;
1628 struct snd_pcm_substream *be_substream =
1629 snd_soc_dpcm_get_substream(be, stream);
1631 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1634 if (be->dpcm[stream].users == 0)
1635 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1636 stream ? "capture" : "playback",
1637 be->dpcm[stream].state);
1639 if (--be->dpcm[stream].users != 0)
1642 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1645 soc_pcm_close(be_substream);
1646 be_substream->runtime = NULL;
1647 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1653 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1654 struct snd_soc_pcm_stream *stream,
1657 runtime->hw.rate_min = stream->rate_min;
1658 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1659 runtime->hw.channels_min = stream->channels_min;
1660 runtime->hw.channels_max = stream->channels_max;
1661 if (runtime->hw.formats)
1662 runtime->hw.formats &= formats & stream->formats;
1664 runtime->hw.formats = formats & stream->formats;
1665 runtime->hw.rates = stream->rates;
1668 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1670 struct snd_soc_pcm_runtime *fe = substream->private_data;
1671 struct snd_soc_dpcm *dpcm;
1672 u64 formats = ULLONG_MAX;
1673 int stream = substream->stream;
1675 if (!fe->dai_link->dpcm_merged_format)
1679 * It returns merged BE codec format
1680 * if FE want to use it (= dpcm_merged_format)
1683 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1684 struct snd_soc_pcm_runtime *be = dpcm->be;
1685 struct snd_soc_dai_driver *codec_dai_drv;
1686 struct snd_soc_pcm_stream *codec_stream;
1689 for (i = 0; i < be->num_codecs; i++) {
1691 * Skip CODECs which don't support the current stream
1692 * type. See soc_pcm_init_runtime_hw() for more details
1694 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1698 codec_dai_drv = be->codec_dais[i]->driver;
1699 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1700 codec_stream = &codec_dai_drv->playback;
1702 codec_stream = &codec_dai_drv->capture;
1704 formats &= codec_stream->formats;
1711 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1713 struct snd_pcm_runtime *runtime = substream->runtime;
1714 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1715 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1716 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1717 u64 format = dpcm_runtime_base_format(substream);
1719 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1720 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1722 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1725 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1727 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1728 * for avoiding the race with trigger callback.
1729 * If the state is unset and a trigger is pending while the previous operation,
1730 * process the pending trigger action here.
1732 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1733 int stream, enum snd_soc_dpcm_update state)
1735 struct snd_pcm_substream *substream =
1736 snd_soc_dpcm_get_substream(fe, stream);
1738 snd_pcm_stream_lock_irq(substream);
1739 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1740 dpcm_fe_dai_do_trigger(substream,
1741 fe->dpcm[stream].trigger_pending - 1);
1742 fe->dpcm[stream].trigger_pending = 0;
1744 fe->dpcm[stream].runtime_update = state;
1745 snd_pcm_stream_unlock_irq(substream);
1748 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1750 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1751 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1752 int stream = fe_substream->stream, ret = 0;
1754 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1756 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1758 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1762 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1764 /* start the DAI frontend */
1765 ret = soc_pcm_open(fe_substream);
1767 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1771 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1773 dpcm_set_fe_runtime(fe_substream);
1774 snd_pcm_limit_hw_rates(runtime);
1776 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1780 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1782 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1786 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1788 struct snd_soc_dpcm *dpcm;
1790 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1791 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1793 struct snd_soc_pcm_runtime *be = dpcm->be;
1794 struct snd_pcm_substream *be_substream =
1795 snd_soc_dpcm_get_substream(be, stream);
1797 /* is this op for this BE ? */
1798 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1801 if (be->dpcm[stream].users == 0)
1802 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1803 stream ? "capture" : "playback",
1804 be->dpcm[stream].state);
1806 if (--be->dpcm[stream].users != 0)
1809 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1810 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1811 soc_pcm_hw_free(be_substream);
1812 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1815 dev_dbg(be->dev, "ASoC: close BE %s\n",
1816 dpcm->fe->dai_link->name);
1818 soc_pcm_close(be_substream);
1819 be_substream->runtime = NULL;
1821 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1826 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1828 struct snd_soc_pcm_runtime *fe = substream->private_data;
1829 int stream = substream->stream;
1831 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1833 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1835 /* now shutdown the frontend */
1836 soc_pcm_close(substream);
1838 /* shutdown the BEs */
1839 dpcm_be_dai_shutdown(fe, substream->stream);
1841 /* run the stream event for each BE */
1842 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1844 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1845 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1849 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1851 struct snd_soc_dpcm *dpcm;
1853 /* only hw_params backends that are either sinks or sources
1854 * to this frontend DAI */
1855 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1857 struct snd_soc_pcm_runtime *be = dpcm->be;
1858 struct snd_pcm_substream *be_substream =
1859 snd_soc_dpcm_get_substream(be, stream);
1861 /* is this op for this BE ? */
1862 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1865 /* only free hw when no longer used - check all FEs */
1866 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1869 /* do not free hw if this BE is used by other FE */
1870 if (be->dpcm[stream].users > 1)
1873 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1874 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1875 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1876 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1877 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1878 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1881 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1882 dpcm->fe->dai_link->name);
1884 soc_pcm_hw_free(be_substream);
1886 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1892 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1894 struct snd_soc_pcm_runtime *fe = substream->private_data;
1895 int err, stream = substream->stream;
1897 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1898 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1900 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1902 /* call hw_free on the frontend */
1903 err = soc_pcm_hw_free(substream);
1905 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1906 fe->dai_link->name);
1908 /* only hw_params backends that are either sinks or sources
1909 * to this frontend DAI */
1910 err = dpcm_be_dai_hw_free(fe, stream);
1912 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1913 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1915 mutex_unlock(&fe->card->mutex);
1919 int dpcm_fe_dai_hw_params_be(struct snd_soc_pcm_runtime *fe,
1920 struct snd_soc_pcm_runtime *be,
1921 struct snd_pcm_hw_params *params, int stream)
1924 struct snd_soc_dpcm *dpcm;
1925 struct snd_pcm_substream *be_substream =
1926 snd_soc_dpcm_get_substream(be, stream);
1928 /* is this op for this BE ? */
1929 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1932 /* only allow hw_params() if no connected FEs are running */
1933 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1936 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1937 (be->dpcm[stream].state !=
1938 SND_SOC_DPCM_STATE_HW_PARAMS) &&
1939 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1942 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1943 fe->dai_link->name);
1945 /* perform any hw_params fixups */
1946 if (be->dai_link->be_hw_params_fixup) {
1947 ret = be->dai_link->be_hw_params_fixup(be,
1951 "ASoC: hw_params BE fixup failed %d\n",
1957 ret = soc_pcm_hw_params(be_substream, params);
1959 dev_err(be->dev, "ASoC: hw_params BE failed %d\n", ret);
1963 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1967 /* disable any enabled and non active backends */
1968 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1969 struct snd_soc_pcm_runtime *be = dpcm->be;
1970 struct snd_pcm_substream *be_substream =
1971 snd_soc_dpcm_get_substream(be, stream);
1973 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1976 /* only allow hw_free() if no connected FEs are running */
1977 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1980 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1981 (be->dpcm[stream].state
1982 != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1983 (be->dpcm[stream].state
1984 != SND_SOC_DPCM_STATE_HW_FREE) &&
1985 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1988 soc_pcm_hw_free(be_substream);
1994 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1996 struct snd_soc_dpcm *dpcm;
1999 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2001 struct snd_soc_pcm_runtime *be = dpcm->be;
2002 struct snd_pcm_substream *be_substream =
2003 snd_soc_dpcm_get_substream(be, stream);
2005 /* is this op for this BE ? */
2006 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2009 /* only allow hw_params() if no connected FEs are running */
2010 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2013 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2014 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2015 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2018 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2019 dpcm->fe->dai_link->name);
2021 /* copy params for each dpcm */
2022 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2023 sizeof(struct snd_pcm_hw_params));
2025 /* perform any hw_params fixups */
2026 if (be->dai_link->be_hw_params_fixup) {
2027 ret = be->dai_link->be_hw_params_fixup(be,
2031 "ASoC: hw_params BE fixup failed %d\n",
2037 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2039 dev_err(dpcm->be->dev,
2040 "ASoC: hw_params BE failed %d\n", ret);
2044 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2049 /* disable any enabled and non active backends */
2050 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2051 struct snd_soc_pcm_runtime *be = dpcm->be;
2052 struct snd_pcm_substream *be_substream =
2053 snd_soc_dpcm_get_substream(be, stream);
2055 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2058 /* only allow hw_free() if no connected FEs are running */
2059 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2062 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2063 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2064 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2065 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2068 soc_pcm_hw_free(be_substream);
2074 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2075 struct snd_pcm_hw_params *params)
2077 struct snd_soc_pcm_runtime *fe = substream->private_data;
2078 int ret, stream = substream->stream;
2080 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2081 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2083 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2084 sizeof(struct snd_pcm_hw_params));
2085 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2087 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2091 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2092 fe->dai_link->name, params_rate(params),
2093 params_channels(params), params_format(params));
2095 /* call hw_params on the frontend */
2096 ret = soc_pcm_hw_params(substream, params);
2098 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2099 dpcm_be_dai_hw_free(fe, stream);
2101 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2104 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2105 mutex_unlock(&fe->card->mutex);
2109 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2110 struct snd_pcm_substream *substream, int cmd)
2114 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2115 dpcm->fe->dai_link->name, cmd);
2117 ret = soc_pcm_trigger(substream, cmd);
2119 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2124 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2127 struct snd_soc_dpcm *dpcm;
2130 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2132 struct snd_soc_pcm_runtime *be = dpcm->be;
2133 struct snd_pcm_substream *be_substream =
2134 snd_soc_dpcm_get_substream(be, stream);
2136 /* is this op for this BE ? */
2137 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2141 case SNDRV_PCM_TRIGGER_START:
2142 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2143 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2146 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2150 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2152 case SNDRV_PCM_TRIGGER_RESUME:
2153 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2156 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2160 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2162 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2163 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2166 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2170 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2172 case SNDRV_PCM_TRIGGER_STOP:
2173 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2176 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2179 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2183 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2185 case SNDRV_PCM_TRIGGER_SUSPEND:
2186 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2189 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2192 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2196 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2198 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2199 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2202 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2205 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2209 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2216 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2218 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2219 int cmd, bool fe_first)
2221 struct snd_soc_pcm_runtime *fe = substream->private_data;
2224 /* call trigger on the frontend before the backend. */
2226 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2227 fe->dai_link->name, cmd);
2229 ret = soc_pcm_trigger(substream, cmd);
2233 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2237 /* call trigger on the frontend after the backend. */
2238 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2242 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2243 fe->dai_link->name, cmd);
2245 ret = soc_pcm_trigger(substream, cmd);
2250 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2252 struct snd_soc_pcm_runtime *fe = substream->private_data;
2253 int stream = substream->stream;
2255 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2257 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2260 case SND_SOC_DPCM_TRIGGER_PRE:
2262 case SNDRV_PCM_TRIGGER_START:
2263 case SNDRV_PCM_TRIGGER_RESUME:
2264 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2265 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2267 case SNDRV_PCM_TRIGGER_STOP:
2268 case SNDRV_PCM_TRIGGER_SUSPEND:
2269 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2270 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2277 case SND_SOC_DPCM_TRIGGER_POST:
2279 case SNDRV_PCM_TRIGGER_START:
2280 case SNDRV_PCM_TRIGGER_RESUME:
2281 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2282 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2284 case SNDRV_PCM_TRIGGER_STOP:
2285 case SNDRV_PCM_TRIGGER_SUSPEND:
2286 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2287 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2294 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2295 /* bespoke trigger() - handles both FE and BEs */
2297 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2298 fe->dai_link->name, cmd);
2300 ret = soc_pcm_bespoke_trigger(substream, cmd);
2303 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2304 fe->dai_link->name);
2310 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2316 case SNDRV_PCM_TRIGGER_START:
2317 case SNDRV_PCM_TRIGGER_RESUME:
2318 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2319 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2321 case SNDRV_PCM_TRIGGER_STOP:
2322 case SNDRV_PCM_TRIGGER_SUSPEND:
2323 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2325 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2326 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2331 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2335 int dpcm_fe_dai_prepare_be(struct snd_soc_pcm_runtime *fe,
2336 struct snd_soc_pcm_runtime *be, int stream)
2338 struct snd_pcm_substream *be_substream =
2339 snd_soc_dpcm_get_substream(be, stream);
2342 /* is this op for this BE ? */
2343 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2346 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2347 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2350 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2351 fe->dai_link->name);
2353 ret = soc_pcm_prepare(be_substream);
2355 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2360 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2364 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2366 struct snd_soc_pcm_runtime *fe = substream->private_data;
2367 int stream = substream->stream;
2369 /* if FE's runtime_update is already set, we're in race;
2370 * process this trigger later at exit
2372 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2373 fe->dpcm[stream].trigger_pending = cmd + 1;
2374 return 0; /* delayed, assuming it's successful */
2377 /* we're alone, let's trigger */
2378 return dpcm_fe_dai_do_trigger(substream, cmd);
2381 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2383 struct snd_soc_dpcm *dpcm;
2386 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2388 struct snd_soc_pcm_runtime *be = dpcm->be;
2389 struct snd_pcm_substream *be_substream =
2390 snd_soc_dpcm_get_substream(be, stream);
2392 /* is this op for this BE ? */
2393 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2396 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2397 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2400 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2401 dpcm->fe->dai_link->name);
2403 ret = soc_pcm_prepare(be_substream);
2405 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2410 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2415 static void dpcm_be_async_prepare(void *data, async_cookie_t cookie)
2417 struct snd_soc_dpcm *dpcm = data;
2418 struct snd_soc_pcm_runtime *be = dpcm->be;
2419 int stream = dpcm->stream;
2420 struct snd_pcm_substream *be_substream =
2421 snd_soc_dpcm_get_substream(be, stream);
2424 dev_dbg(be->dev, "%s ASoC: prepare BE %s\n", __func__,
2425 dpcm->fe->dai_link->name);
2426 ret = soc_pcm_prepare(be_substream);
2429 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2433 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2436 void dpcm_be_dai_prepare_async(struct snd_soc_pcm_runtime *fe, int stream,
2437 struct async_domain *domain)
2439 struct snd_soc_dpcm *dpcm;
2440 struct snd_soc_dpcm *dpcm_async[DPCM_MAX_BE_USERS];
2443 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2444 struct snd_soc_pcm_runtime *be = dpcm->be;
2447 /* is this op for this BE ? */
2448 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2451 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2452 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2455 /* does this BE support async op ?*/
2456 if ((fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE) &&
2457 (be->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE)) {
2458 dpcm->stream = stream;
2459 async_schedule_domain(dpcm_be_async_prepare,
2462 dpcm_async[i++] = dpcm;
2463 if (i == DPCM_MAX_BE_USERS) {
2464 dev_dbg(fe->dev, "ASoC: MAX backend users!\n");
2470 for (j = 0; j < i; j++) {
2471 struct snd_soc_dpcm *dpcm = dpcm_async[j];
2472 struct snd_soc_pcm_runtime *be = dpcm->be;
2473 struct snd_pcm_substream *be_substream =
2474 snd_soc_dpcm_get_substream(be, stream);
2477 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2478 dpcm->fe->dai_link->name);
2480 ret = soc_pcm_prepare(be_substream);
2482 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2488 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2492 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2494 struct snd_soc_pcm_runtime *fe = substream->private_data;
2495 struct snd_soc_dpcm *dpcm;
2496 int stream = substream->stream, ret = 0;
2497 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2499 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2503 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2505 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2507 /* there is no point preparing this FE if there are no BEs */
2508 if (list_empty(&fe->dpcm[stream].be_clients)) {
2509 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2510 fe->dai_link->name);
2515 if (!(fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE)) {
2516 ret = dpcm_be_dai_prepare(fe, substream->stream);
2519 /* call prepare on the frontend */
2520 ret = soc_pcm_prepare(substream);
2522 dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
2523 fe->dai_link->name);
2527 dpcm_be_dai_prepare_async(fe, substream->stream,
2530 /* call prepare on the frontend */
2531 ret = soc_pcm_prepare(substream);
2534 dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
2535 fe->dai_link->name);
2538 async_synchronize_full_domain(&async_domain);
2540 /* check if any BE failed */
2541 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients,
2543 struct snd_soc_pcm_runtime *be = dpcm->be;
2545 if (be->err_ops < 0) {
2551 /* check if FE failed */
2552 if (fe->err_ops < 0) {
2558 /* run the stream event for each BE */
2559 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2560 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2563 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2564 mutex_unlock(&fe->card->mutex);
2569 static int soc_pcm_compat_ioctl(struct snd_pcm_substream *substream,
2570 unsigned int cmd, void *arg)
2572 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2573 struct snd_soc_platform *platform = rtd->platform;
2575 if (platform->driver->ops->compat_ioctl)
2576 return platform->driver->ops->compat_ioctl(substream,
2578 return snd_pcm_lib_ioctl(substream, cmd, arg);
2581 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2582 unsigned int cmd, void *arg)
2584 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2585 struct snd_soc_platform *platform = rtd->platform;
2587 if (platform->driver->ops && platform->driver->ops->ioctl)
2588 return platform->driver->ops->ioctl(substream, cmd, arg);
2589 return snd_pcm_lib_ioctl(substream, cmd, arg);
2592 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2594 struct snd_pcm_substream *substream =
2595 snd_soc_dpcm_get_substream(fe, stream);
2596 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2599 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2600 stream ? "capture" : "playback", fe->dai_link->name);
2602 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2603 /* call bespoke trigger - FE takes care of all BE triggers */
2604 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2605 fe->dai_link->name);
2607 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2609 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2611 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2612 fe->dai_link->name);
2614 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2616 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2619 err = dpcm_be_dai_hw_free(fe, stream);
2621 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2623 err = dpcm_be_dai_shutdown(fe, stream);
2625 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2627 /* run the stream event for each BE */
2628 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2633 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2635 struct snd_pcm_substream *substream =
2636 snd_soc_dpcm_get_substream(fe, stream);
2637 struct snd_soc_dpcm *dpcm;
2638 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2641 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2642 stream ? "capture" : "playback", fe->dai_link->name);
2644 /* Only start the BE if the FE is ready */
2645 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2646 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2649 /* startup must always be called for new BEs */
2650 ret = dpcm_be_dai_startup(fe, stream);
2654 /* keep going if FE state is > open */
2655 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2658 ret = dpcm_be_dai_hw_params(fe, stream);
2662 /* keep going if FE state is > hw_params */
2663 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2667 ret = dpcm_be_dai_prepare(fe, stream);
2671 /* run the stream event for each BE */
2672 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2674 /* keep going if FE state is > prepare */
2675 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2676 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2679 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2680 /* call trigger on the frontend - FE takes care of all BE triggers */
2681 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2682 fe->dai_link->name);
2684 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2686 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2690 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2691 fe->dai_link->name);
2693 ret = dpcm_be_dai_trigger(fe, stream,
2694 SNDRV_PCM_TRIGGER_START);
2696 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2704 dpcm_be_dai_hw_free(fe, stream);
2706 dpcm_be_dai_shutdown(fe, stream);
2708 /* disconnect any non started BEs */
2709 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2710 struct snd_soc_pcm_runtime *be = dpcm->be;
2711 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2712 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2718 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2722 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2723 ret = dpcm_run_update_startup(fe, stream);
2725 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2726 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2731 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2735 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2736 ret = dpcm_run_update_shutdown(fe, stream);
2738 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2739 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2744 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2747 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2749 int i, old, new, paths;
2751 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2752 for (i = 0; i < card->num_rtd; i++) {
2753 struct snd_soc_dapm_widget_list *list;
2754 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2756 /* make sure link is FE */
2757 if (!fe->dai_link->dynamic)
2760 /* only check active links */
2761 if (!fe->cpu_dai->active)
2764 /* DAPM sync will call this to update DSP paths */
2765 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2766 fe->dai_link->name);
2768 /* skip if FE doesn't have playback capability */
2769 if (!fe->cpu_dai->driver->playback.channels_min
2770 || !fe->codec_dai->driver->playback.channels_min)
2773 /* skip if FE isn't currently playing */
2774 if (!fe->cpu_dai->playback_active
2775 || !fe->codec_dai->playback_active)
2778 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2780 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2781 fe->dai_link->name, "playback");
2782 mutex_unlock(&card->mutex);
2786 /* update any new playback paths */
2787 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2789 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2790 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2791 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2794 /* update any old playback paths */
2795 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2797 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2798 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2799 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2802 dpcm_path_put(&list);
2804 /* skip if FE doesn't have capture capability */
2805 if (!fe->cpu_dai->driver->capture.channels_min
2806 || !fe->codec_dai->driver->capture.channels_min)
2809 /* skip if FE isn't currently capturing */
2810 if (!fe->cpu_dai->capture_active
2811 || !fe->codec_dai->capture_active)
2814 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2816 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2817 fe->dai_link->name, "capture");
2818 mutex_unlock(&card->mutex);
2822 /* update any new capture paths */
2823 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2825 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2826 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2827 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2830 /* update any old capture paths */
2831 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2833 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2834 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2835 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2838 dpcm_path_put(&list);
2841 mutex_unlock(&card->mutex);
2844 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2846 struct snd_soc_dpcm *dpcm;
2847 struct list_head *clients =
2848 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2850 list_for_each_entry(dpcm, clients, list_be) {
2852 struct snd_soc_pcm_runtime *be = dpcm->be;
2855 if (be->dai_link->ignore_suspend)
2858 for (i = 0; i < be->num_codecs; i++) {
2859 struct snd_soc_dai *dai = be->codec_dais[i];
2860 struct snd_soc_dai_driver *drv = dai->driver;
2862 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2863 be->dai_link->name);
2865 if (drv->ops && drv->ops->digital_mute &&
2866 dai->playback_active)
2867 drv->ops->digital_mute(dai, mute);
2874 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2876 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2877 struct snd_soc_dpcm *dpcm;
2878 struct snd_soc_dapm_widget_list *list;
2880 int stream = fe_substream->stream;
2882 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2883 fe->dpcm[stream].runtime = fe_substream->runtime;
2885 ret = dpcm_path_get(fe, stream, &list);
2887 mutex_unlock(&fe->card->mutex);
2889 } else if (ret == 0) {
2890 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2891 fe->dai_link->name, stream ? "capture" : "playback");
2894 /* calculate valid and active FE <-> BE dpcms */
2895 dpcm_process_paths(fe, stream, &list, 1);
2897 ret = dpcm_fe_dai_startup(fe_substream);
2899 /* clean up all links */
2900 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2901 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2903 dpcm_be_disconnect(fe, stream);
2904 fe->dpcm[stream].runtime = NULL;
2907 dpcm_clear_pending_state(fe, stream);
2908 dpcm_path_put(&list);
2909 mutex_unlock(&fe->card->mutex);
2913 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2915 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2916 struct snd_soc_dpcm *dpcm;
2917 int stream = fe_substream->stream, ret;
2919 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2920 ret = dpcm_fe_dai_shutdown(fe_substream);
2922 /* mark FE's links ready to prune */
2923 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2924 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2926 dpcm_be_disconnect(fe, stream);
2928 fe->dpcm[stream].runtime = NULL;
2929 mutex_unlock(&fe->card->mutex);
2933 /* create a new pcm */
2934 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2936 struct snd_soc_platform *platform = rtd->platform;
2937 struct snd_soc_dai *codec_dai;
2938 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2939 struct snd_pcm *pcm;
2941 int ret = 0, playback = 0, capture = 0;
2944 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2945 playback = rtd->dai_link->dpcm_playback;
2946 capture = rtd->dai_link->dpcm_capture;
2948 for (i = 0; i < rtd->num_codecs; i++) {
2949 codec_dai = rtd->codec_dais[i];
2950 if (codec_dai->driver->playback.channels_min)
2952 if (codec_dai->driver->capture.channels_min)
2956 capture = capture && cpu_dai->driver->capture.channels_min;
2957 playback = playback && cpu_dai->driver->playback.channels_min;
2960 if (rtd->dai_link->playback_only) {
2965 if (rtd->dai_link->capture_only) {
2970 /* create the PCM */
2971 if (rtd->dai_link->no_pcm) {
2972 snprintf(new_name, sizeof(new_name), "(%s)",
2973 rtd->dai_link->stream_name);
2975 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2976 playback, capture, &pcm);
2978 if (rtd->dai_link->dynamic)
2979 snprintf(new_name, sizeof(new_name), "%s (*)",
2980 rtd->dai_link->stream_name);
2982 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2983 rtd->dai_link->stream_name,
2984 (rtd->num_codecs > 1) ?
2985 "multicodec" : rtd->codec_dai->name, num);
2987 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2991 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2992 rtd->dai_link->name);
2995 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2997 /* DAPM dai link stream work */
2998 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3000 pcm->nonatomic = rtd->dai_link->nonatomic;
3002 pcm->private_data = rtd;
3004 if (rtd->dai_link->no_pcm) {
3006 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3008 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3009 if (platform->driver->pcm_new)
3010 rtd->platform->driver->pcm_new(rtd);
3014 /* setup any hostless PCMs - i.e. no host IO is performed */
3015 if (rtd->dai_link->no_host_mode) {
3016 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
3017 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->hw_no_buffer = 1;
3018 snd_soc_set_runtime_hwparams(
3019 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
3022 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
3023 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->hw_no_buffer = 1;
3024 snd_soc_set_runtime_hwparams(
3025 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
3030 /* ASoC PCM operations */
3031 if (rtd->dai_link->dynamic) {
3032 rtd->ops.open = dpcm_fe_dai_open;
3033 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3034 rtd->ops.prepare = dpcm_fe_dai_prepare;
3035 rtd->ops.trigger = dpcm_fe_dai_trigger;
3036 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3037 rtd->ops.close = dpcm_fe_dai_close;
3038 rtd->ops.pointer = soc_pcm_pointer;
3039 rtd->ops.delay_blk = soc_pcm_delay_blk;
3040 rtd->ops.ioctl = soc_pcm_ioctl;
3041 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
3043 rtd->ops.open = soc_pcm_open;
3044 rtd->ops.hw_params = soc_pcm_hw_params;
3045 rtd->ops.prepare = soc_pcm_prepare;
3046 rtd->ops.trigger = soc_pcm_trigger;
3047 rtd->ops.hw_free = soc_pcm_hw_free;
3048 rtd->ops.close = soc_pcm_close;
3049 rtd->ops.pointer = soc_pcm_pointer;
3050 rtd->ops.delay_blk = soc_pcm_delay_blk;
3051 rtd->ops.ioctl = soc_pcm_ioctl;
3052 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
3055 if (platform->driver->ops) {
3056 rtd->ops.ack = platform->driver->ops->ack;
3057 rtd->ops.copy = platform->driver->ops->copy;
3058 rtd->ops.silence = platform->driver->ops->silence;
3059 rtd->ops.page = platform->driver->ops->page;
3060 rtd->ops.mmap = platform->driver->ops->mmap;
3064 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3067 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3069 if (platform->driver->pcm_new) {
3070 ret = platform->driver->pcm_new(rtd);
3072 dev_err(platform->dev,
3073 "ASoC: pcm constructor failed: %d\n",
3079 pcm->private_free = platform->driver->pcm_free;
3081 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
3082 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3087 /* is the current PCM operation for this FE ? */
3088 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3090 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3094 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3096 /* is the current PCM operation for this BE ? */
3097 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3098 struct snd_soc_pcm_runtime *be, int stream)
3100 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3101 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3102 be->dpcm[stream].runtime_update))
3106 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3108 /* get the substream for this BE */
3109 struct snd_pcm_substream *
3110 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3112 return be->pcm->streams[stream].substream;
3114 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3116 /* get the BE runtime state */
3117 enum snd_soc_dpcm_state
3118 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3120 return be->dpcm[stream].state;
3122 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3124 /* set the BE runtime state */
3125 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3126 int stream, enum snd_soc_dpcm_state state)
3128 be->dpcm[stream].state = state;
3130 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3133 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3134 * are not running, paused or suspended for the specified stream direction.
3136 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3137 struct snd_soc_pcm_runtime *be, int stream)
3139 struct snd_soc_dpcm *dpcm;
3142 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3147 state = dpcm->fe->dpcm[stream].state;
3148 if (state == SND_SOC_DPCM_STATE_START ||
3149 state == SND_SOC_DPCM_STATE_PAUSED ||
3150 state == SND_SOC_DPCM_STATE_SUSPEND)
3154 /* it's safe to free/stop this BE DAI */
3157 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3160 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3161 * running, paused or suspended for the specified stream direction.
3163 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3164 struct snd_soc_pcm_runtime *be, int stream)
3166 struct snd_soc_dpcm *dpcm;
3169 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3174 state = dpcm->fe->dpcm[stream].state;
3175 if (state == SND_SOC_DPCM_STATE_START ||
3176 state == SND_SOC_DPCM_STATE_PAUSED ||
3177 state == SND_SOC_DPCM_STATE_SUSPEND ||
3178 state == SND_SOC_DPCM_STATE_PREPARE)
3182 /* it's safe to change hw_params */
3185 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3187 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
3188 int cmd, struct snd_soc_platform *platform)
3190 if (platform->driver->ops && platform->driver->ops->trigger)
3191 return platform->driver->ops->trigger(substream, cmd);
3194 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
3196 #ifdef CONFIG_DEBUG_FS
3197 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
3200 case SND_SOC_DPCM_STATE_NEW:
3202 case SND_SOC_DPCM_STATE_OPEN:
3204 case SND_SOC_DPCM_STATE_HW_PARAMS:
3206 case SND_SOC_DPCM_STATE_PREPARE:
3208 case SND_SOC_DPCM_STATE_START:
3210 case SND_SOC_DPCM_STATE_STOP:
3212 case SND_SOC_DPCM_STATE_SUSPEND:
3214 case SND_SOC_DPCM_STATE_PAUSED:
3216 case SND_SOC_DPCM_STATE_HW_FREE:
3218 case SND_SOC_DPCM_STATE_CLOSE:
3225 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3226 int stream, char *buf, size_t size)
3228 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3229 struct snd_soc_dpcm *dpcm;
3233 offset += snprintf(buf + offset, size - offset,
3234 "[%s - %s]\n", fe->dai_link->name,
3235 stream ? "Capture" : "Playback");
3237 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3238 dpcm_state_string(fe->dpcm[stream].state));
3240 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3241 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3242 offset += snprintf(buf + offset, size - offset,
3244 "Format = %s, Channels = %d, Rate = %d\n",
3245 snd_pcm_format_name(params_format(params)),
3246 params_channels(params),
3247 params_rate(params));
3250 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3252 if (list_empty(&fe->dpcm[stream].be_clients)) {
3253 offset += snprintf(buf + offset, size - offset,
3254 " No active DSP links\n");
3258 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3259 struct snd_soc_pcm_runtime *be = dpcm->be;
3260 params = &dpcm->hw_params;
3262 offset += snprintf(buf + offset, size - offset,
3263 "- %s\n", be->dai_link->name);
3265 offset += snprintf(buf + offset, size - offset,
3267 dpcm_state_string(be->dpcm[stream].state));
3269 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3270 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3271 offset += snprintf(buf + offset, size - offset,
3272 " Hardware Params: "
3273 "Format = %s, Channels = %d, Rate = %d\n",
3274 snd_pcm_format_name(params_format(params)),
3275 params_channels(params),
3276 params_rate(params));
3283 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3284 size_t count, loff_t *ppos)
3286 struct snd_soc_pcm_runtime *fe = file->private_data;
3287 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3290 buf = kmalloc(out_count, GFP_KERNEL);
3294 if (fe->cpu_dai->driver->playback.channels_min)
3295 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3296 buf + offset, out_count - offset);
3298 if (fe->cpu_dai->driver->capture.channels_min)
3299 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3300 buf + offset, out_count - offset);
3302 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3308 static const struct file_operations dpcm_state_fops = {
3309 .open = simple_open,
3310 .read = dpcm_state_read_file,
3311 .llseek = default_llseek,
3314 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3319 if (!rtd->card->debugfs_card_root)
3322 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3323 rtd->card->debugfs_card_root);
3324 if (!rtd->debugfs_dpcm_root) {
3326 "ASoC: Failed to create dpcm debugfs directory %s\n",
3327 rtd->dai_link->name);
3331 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
3332 rtd->debugfs_dpcm_root,
3333 rtd, &dpcm_state_fops);