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 rate at all, it supports the stream. */
53 return codec_stream->rates;
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 = stream->rate_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_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2220 struct snd_soc_pcm_runtime *fe = substream->private_data;
2221 int stream = substream->stream, ret;
2222 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2224 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2227 case SND_SOC_DPCM_TRIGGER_PRE:
2228 /* call trigger on the frontend before the backend. */
2230 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2231 fe->dai_link->name, cmd);
2233 ret = soc_pcm_trigger(substream, cmd);
2235 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2239 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2241 case SND_SOC_DPCM_TRIGGER_POST:
2242 /* call trigger on the frontend after the backend. */
2244 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2246 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2250 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2251 fe->dai_link->name, cmd);
2253 ret = soc_pcm_trigger(substream, cmd);
2255 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2256 /* bespoke trigger() - handles both FE and BEs */
2258 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2259 fe->dai_link->name, cmd);
2261 ret = soc_pcm_bespoke_trigger(substream, cmd);
2263 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2268 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2269 fe->dai_link->name);
2275 case SNDRV_PCM_TRIGGER_START:
2276 case SNDRV_PCM_TRIGGER_RESUME:
2277 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2278 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2280 case SNDRV_PCM_TRIGGER_STOP:
2281 case SNDRV_PCM_TRIGGER_SUSPEND:
2282 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2284 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2285 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2290 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2294 int dpcm_fe_dai_prepare_be(struct snd_soc_pcm_runtime *fe,
2295 struct snd_soc_pcm_runtime *be, int stream)
2297 struct snd_pcm_substream *be_substream =
2298 snd_soc_dpcm_get_substream(be, stream);
2301 /* is this op for this BE ? */
2302 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2305 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2306 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2309 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2310 fe->dai_link->name);
2312 ret = soc_pcm_prepare(be_substream);
2314 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2319 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2323 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2325 struct snd_soc_pcm_runtime *fe = substream->private_data;
2326 int stream = substream->stream;
2328 /* if FE's runtime_update is already set, we're in race;
2329 * process this trigger later at exit
2331 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2332 fe->dpcm[stream].trigger_pending = cmd + 1;
2333 return 0; /* delayed, assuming it's successful */
2336 /* we're alone, let's trigger */
2337 return dpcm_fe_dai_do_trigger(substream, cmd);
2340 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2342 struct snd_soc_dpcm *dpcm;
2345 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2347 struct snd_soc_pcm_runtime *be = dpcm->be;
2348 struct snd_pcm_substream *be_substream =
2349 snd_soc_dpcm_get_substream(be, stream);
2351 /* is this op for this BE ? */
2352 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2355 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2356 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2359 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2360 dpcm->fe->dai_link->name);
2362 ret = soc_pcm_prepare(be_substream);
2364 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2369 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2374 static void dpcm_be_async_prepare(void *data, async_cookie_t cookie)
2376 struct snd_soc_dpcm *dpcm = data;
2377 struct snd_soc_pcm_runtime *be = dpcm->be;
2378 int stream = dpcm->stream;
2379 struct snd_pcm_substream *be_substream =
2380 snd_soc_dpcm_get_substream(be, stream);
2383 dev_dbg(be->dev, "%s ASoC: prepare BE %s\n", __func__,
2384 dpcm->fe->dai_link->name);
2385 ret = soc_pcm_prepare(be_substream);
2388 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2392 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2395 void dpcm_be_dai_prepare_async(struct snd_soc_pcm_runtime *fe, int stream,
2396 struct async_domain *domain)
2398 struct snd_soc_dpcm *dpcm;
2399 struct snd_soc_dpcm *dpcm_async[DPCM_MAX_BE_USERS];
2402 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2403 struct snd_soc_pcm_runtime *be = dpcm->be;
2406 /* is this op for this BE ? */
2407 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2410 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2411 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2414 /* does this BE support async op ?*/
2415 if ((fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE) &&
2416 (be->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE)) {
2417 dpcm->stream = stream;
2418 async_schedule_domain(dpcm_be_async_prepare,
2421 dpcm_async[i++] = dpcm;
2422 if (i == DPCM_MAX_BE_USERS) {
2423 dev_dbg(fe->dev, "ASoC: MAX backend users!\n");
2429 for (j = 0; j < i; j++) {
2430 struct snd_soc_dpcm *dpcm = dpcm_async[j];
2431 struct snd_soc_pcm_runtime *be = dpcm->be;
2432 struct snd_pcm_substream *be_substream =
2433 snd_soc_dpcm_get_substream(be, stream);
2436 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2437 dpcm->fe->dai_link->name);
2439 ret = soc_pcm_prepare(be_substream);
2441 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2447 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2451 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2453 struct snd_soc_pcm_runtime *fe = substream->private_data;
2454 struct snd_soc_dpcm *dpcm;
2455 int stream = substream->stream, ret = 0;
2456 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2458 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2462 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2464 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2466 /* there is no point preparing this FE if there are no BEs */
2467 if (list_empty(&fe->dpcm[stream].be_clients)) {
2468 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2469 fe->dai_link->name);
2474 if (!(fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE)) {
2475 ret = dpcm_be_dai_prepare(fe, substream->stream);
2478 /* call prepare on the frontend */
2479 ret = soc_pcm_prepare(substream);
2481 dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
2482 fe->dai_link->name);
2486 dpcm_be_dai_prepare_async(fe, substream->stream,
2489 /* call prepare on the frontend */
2490 ret = soc_pcm_prepare(substream);
2493 dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
2494 fe->dai_link->name);
2497 async_synchronize_full_domain(&async_domain);
2499 /* check if any BE failed */
2500 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients,
2502 struct snd_soc_pcm_runtime *be = dpcm->be;
2504 if (be->err_ops < 0) {
2510 /* check if FE failed */
2511 if (fe->err_ops < 0) {
2517 /* run the stream event for each BE */
2518 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2519 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2522 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2523 mutex_unlock(&fe->card->mutex);
2528 static int soc_pcm_compat_ioctl(struct snd_pcm_substream *substream,
2529 unsigned int cmd, void *arg)
2531 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2532 struct snd_soc_platform *platform = rtd->platform;
2534 if (platform->driver->ops->compat_ioctl)
2535 return platform->driver->ops->compat_ioctl(substream,
2537 return snd_pcm_lib_ioctl(substream, cmd, arg);
2540 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2541 unsigned int cmd, void *arg)
2543 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2544 struct snd_soc_platform *platform = rtd->platform;
2546 if (platform->driver->ops && platform->driver->ops->ioctl)
2547 return platform->driver->ops->ioctl(substream, cmd, arg);
2548 return snd_pcm_lib_ioctl(substream, cmd, arg);
2551 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2553 struct snd_pcm_substream *substream =
2554 snd_soc_dpcm_get_substream(fe, stream);
2555 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2558 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2559 stream ? "capture" : "playback", fe->dai_link->name);
2561 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2562 /* call bespoke trigger - FE takes care of all BE triggers */
2563 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2564 fe->dai_link->name);
2566 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2568 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2570 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2571 fe->dai_link->name);
2573 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2575 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2578 err = dpcm_be_dai_hw_free(fe, stream);
2580 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2582 err = dpcm_be_dai_shutdown(fe, stream);
2584 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2586 /* run the stream event for each BE */
2587 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2592 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2594 struct snd_pcm_substream *substream =
2595 snd_soc_dpcm_get_substream(fe, stream);
2596 struct snd_soc_dpcm *dpcm;
2597 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2600 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2601 stream ? "capture" : "playback", fe->dai_link->name);
2603 /* Only start the BE if the FE is ready */
2604 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2605 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2608 /* startup must always be called for new BEs */
2609 ret = dpcm_be_dai_startup(fe, stream);
2613 /* keep going if FE state is > open */
2614 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2617 ret = dpcm_be_dai_hw_params(fe, stream);
2621 /* keep going if FE state is > hw_params */
2622 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2626 ret = dpcm_be_dai_prepare(fe, stream);
2630 /* run the stream event for each BE */
2631 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2633 /* keep going if FE state is > prepare */
2634 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2635 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2638 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2639 /* call trigger on the frontend - FE takes care of all BE triggers */
2640 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2641 fe->dai_link->name);
2643 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2645 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2649 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2650 fe->dai_link->name);
2652 ret = dpcm_be_dai_trigger(fe, stream,
2653 SNDRV_PCM_TRIGGER_START);
2655 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2663 dpcm_be_dai_hw_free(fe, stream);
2665 dpcm_be_dai_shutdown(fe, stream);
2667 /* disconnect any non started BEs */
2668 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2669 struct snd_soc_pcm_runtime *be = dpcm->be;
2670 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2671 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2677 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2681 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2682 ret = dpcm_run_update_startup(fe, stream);
2684 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2685 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2690 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2694 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2695 ret = dpcm_run_update_shutdown(fe, stream);
2697 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2698 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2703 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2706 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2708 int i, old, new, paths;
2710 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2711 for (i = 0; i < card->num_rtd; i++) {
2712 struct snd_soc_dapm_widget_list *list;
2713 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2715 /* make sure link is FE */
2716 if (!fe->dai_link->dynamic)
2719 /* only check active links */
2720 if (!fe->cpu_dai->active)
2723 /* DAPM sync will call this to update DSP paths */
2724 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2725 fe->dai_link->name);
2727 /* skip if FE doesn't have playback capability */
2728 if (!fe->cpu_dai->driver->playback.channels_min
2729 || !fe->codec_dai->driver->playback.channels_min)
2732 /* skip if FE isn't currently playing */
2733 if (!fe->cpu_dai->playback_active
2734 || !fe->codec_dai->playback_active)
2737 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2739 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2740 fe->dai_link->name, "playback");
2741 mutex_unlock(&card->mutex);
2745 /* update any new playback paths */
2746 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2748 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2749 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2750 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2753 /* update any old playback paths */
2754 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2756 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2757 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2758 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2761 dpcm_path_put(&list);
2763 /* skip if FE doesn't have capture capability */
2764 if (!fe->cpu_dai->driver->capture.channels_min
2765 || !fe->codec_dai->driver->capture.channels_min)
2768 /* skip if FE isn't currently capturing */
2769 if (!fe->cpu_dai->capture_active
2770 || !fe->codec_dai->capture_active)
2773 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2775 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2776 fe->dai_link->name, "capture");
2777 mutex_unlock(&card->mutex);
2781 /* update any new capture paths */
2782 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2784 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2785 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2786 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2789 /* update any old capture paths */
2790 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2792 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2793 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2794 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2797 dpcm_path_put(&list);
2800 mutex_unlock(&card->mutex);
2803 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2805 struct snd_soc_dpcm *dpcm;
2806 struct list_head *clients =
2807 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2809 list_for_each_entry(dpcm, clients, list_be) {
2811 struct snd_soc_pcm_runtime *be = dpcm->be;
2814 if (be->dai_link->ignore_suspend)
2817 for (i = 0; i < be->num_codecs; i++) {
2818 struct snd_soc_dai *dai = be->codec_dais[i];
2819 struct snd_soc_dai_driver *drv = dai->driver;
2821 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2822 be->dai_link->name);
2824 if (drv->ops && drv->ops->digital_mute &&
2825 dai->playback_active)
2826 drv->ops->digital_mute(dai, mute);
2833 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2835 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2836 struct snd_soc_dpcm *dpcm;
2837 struct snd_soc_dapm_widget_list *list;
2839 int stream = fe_substream->stream;
2841 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2842 fe->dpcm[stream].runtime = fe_substream->runtime;
2844 ret = dpcm_path_get(fe, stream, &list);
2846 mutex_unlock(&fe->card->mutex);
2848 } else if (ret == 0) {
2849 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2850 fe->dai_link->name, stream ? "capture" : "playback");
2853 /* calculate valid and active FE <-> BE dpcms */
2854 dpcm_process_paths(fe, stream, &list, 1);
2856 ret = dpcm_fe_dai_startup(fe_substream);
2858 /* clean up all links */
2859 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2860 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2862 dpcm_be_disconnect(fe, stream);
2863 fe->dpcm[stream].runtime = NULL;
2866 dpcm_clear_pending_state(fe, stream);
2867 dpcm_path_put(&list);
2868 mutex_unlock(&fe->card->mutex);
2872 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2874 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2875 struct snd_soc_dpcm *dpcm;
2876 int stream = fe_substream->stream, ret;
2878 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2879 ret = dpcm_fe_dai_shutdown(fe_substream);
2881 /* mark FE's links ready to prune */
2882 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2883 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2885 dpcm_be_disconnect(fe, stream);
2887 fe->dpcm[stream].runtime = NULL;
2888 mutex_unlock(&fe->card->mutex);
2892 /* create a new pcm */
2893 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2895 struct snd_soc_platform *platform = rtd->platform;
2896 struct snd_soc_dai *codec_dai;
2897 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2898 struct snd_pcm *pcm;
2900 int ret = 0, playback = 0, capture = 0;
2903 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2904 playback = rtd->dai_link->dpcm_playback;
2905 capture = rtd->dai_link->dpcm_capture;
2907 for (i = 0; i < rtd->num_codecs; i++) {
2908 codec_dai = rtd->codec_dais[i];
2909 if (codec_dai->driver->playback.channels_min)
2911 if (codec_dai->driver->capture.channels_min)
2915 capture = capture && cpu_dai->driver->capture.channels_min;
2916 playback = playback && cpu_dai->driver->playback.channels_min;
2919 if (rtd->dai_link->playback_only) {
2924 if (rtd->dai_link->capture_only) {
2929 /* create the PCM */
2930 if (rtd->dai_link->no_pcm) {
2931 snprintf(new_name, sizeof(new_name), "(%s)",
2932 rtd->dai_link->stream_name);
2934 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2935 playback, capture, &pcm);
2937 if (rtd->dai_link->dynamic)
2938 snprintf(new_name, sizeof(new_name), "%s (*)",
2939 rtd->dai_link->stream_name);
2941 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2942 rtd->dai_link->stream_name,
2943 (rtd->num_codecs > 1) ?
2944 "multicodec" : rtd->codec_dai->name, num);
2946 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2950 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2951 rtd->dai_link->name);
2954 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2956 /* DAPM dai link stream work */
2957 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2959 pcm->nonatomic = rtd->dai_link->nonatomic;
2961 pcm->private_data = rtd;
2963 if (rtd->dai_link->no_pcm) {
2965 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2967 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2968 if (platform->driver->pcm_new)
2969 rtd->platform->driver->pcm_new(rtd);
2973 /* setup any hostless PCMs - i.e. no host IO is performed */
2974 if (rtd->dai_link->no_host_mode) {
2975 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
2976 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->hw_no_buffer = 1;
2977 snd_soc_set_runtime_hwparams(
2978 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
2981 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
2982 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->hw_no_buffer = 1;
2983 snd_soc_set_runtime_hwparams(
2984 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
2989 /* ASoC PCM operations */
2990 if (rtd->dai_link->dynamic) {
2991 rtd->ops.open = dpcm_fe_dai_open;
2992 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2993 rtd->ops.prepare = dpcm_fe_dai_prepare;
2994 rtd->ops.trigger = dpcm_fe_dai_trigger;
2995 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2996 rtd->ops.close = dpcm_fe_dai_close;
2997 rtd->ops.pointer = soc_pcm_pointer;
2998 rtd->ops.delay_blk = soc_pcm_delay_blk;
2999 rtd->ops.ioctl = soc_pcm_ioctl;
3000 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
3002 rtd->ops.open = soc_pcm_open;
3003 rtd->ops.hw_params = soc_pcm_hw_params;
3004 rtd->ops.prepare = soc_pcm_prepare;
3005 rtd->ops.trigger = soc_pcm_trigger;
3006 rtd->ops.hw_free = soc_pcm_hw_free;
3007 rtd->ops.close = soc_pcm_close;
3008 rtd->ops.pointer = soc_pcm_pointer;
3009 rtd->ops.delay_blk = soc_pcm_delay_blk;
3010 rtd->ops.ioctl = soc_pcm_ioctl;
3011 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
3014 if (platform->driver->ops) {
3015 rtd->ops.ack = platform->driver->ops->ack;
3016 rtd->ops.copy = platform->driver->ops->copy;
3017 rtd->ops.silence = platform->driver->ops->silence;
3018 rtd->ops.page = platform->driver->ops->page;
3019 rtd->ops.mmap = platform->driver->ops->mmap;
3023 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3026 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3028 if (platform->driver->pcm_new) {
3029 ret = platform->driver->pcm_new(rtd);
3031 dev_err(platform->dev,
3032 "ASoC: pcm constructor failed: %d\n",
3038 pcm->private_free = platform->driver->pcm_free;
3040 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
3041 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3046 /* is the current PCM operation for this FE ? */
3047 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3049 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3053 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3055 /* is the current PCM operation for this BE ? */
3056 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3057 struct snd_soc_pcm_runtime *be, int stream)
3059 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3060 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3061 be->dpcm[stream].runtime_update))
3065 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3067 /* get the substream for this BE */
3068 struct snd_pcm_substream *
3069 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3071 return be->pcm->streams[stream].substream;
3073 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3075 /* get the BE runtime state */
3076 enum snd_soc_dpcm_state
3077 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3079 return be->dpcm[stream].state;
3081 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3083 /* set the BE runtime state */
3084 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3085 int stream, enum snd_soc_dpcm_state state)
3087 be->dpcm[stream].state = state;
3089 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3092 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3093 * are not running, paused or suspended for the specified stream direction.
3095 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3096 struct snd_soc_pcm_runtime *be, int stream)
3098 struct snd_soc_dpcm *dpcm;
3101 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3106 state = dpcm->fe->dpcm[stream].state;
3107 if (state == SND_SOC_DPCM_STATE_START ||
3108 state == SND_SOC_DPCM_STATE_PAUSED ||
3109 state == SND_SOC_DPCM_STATE_SUSPEND)
3113 /* it's safe to free/stop this BE DAI */
3116 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3119 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3120 * running, paused or suspended for the specified stream direction.
3122 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3123 struct snd_soc_pcm_runtime *be, int stream)
3125 struct snd_soc_dpcm *dpcm;
3128 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3133 state = dpcm->fe->dpcm[stream].state;
3134 if (state == SND_SOC_DPCM_STATE_START ||
3135 state == SND_SOC_DPCM_STATE_PAUSED ||
3136 state == SND_SOC_DPCM_STATE_SUSPEND ||
3137 state == SND_SOC_DPCM_STATE_PREPARE)
3141 /* it's safe to change hw_params */
3144 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3146 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
3147 int cmd, struct snd_soc_platform *platform)
3149 if (platform->driver->ops && platform->driver->ops->trigger)
3150 return platform->driver->ops->trigger(substream, cmd);
3153 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
3155 #ifdef CONFIG_DEBUG_FS
3156 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
3159 case SND_SOC_DPCM_STATE_NEW:
3161 case SND_SOC_DPCM_STATE_OPEN:
3163 case SND_SOC_DPCM_STATE_HW_PARAMS:
3165 case SND_SOC_DPCM_STATE_PREPARE:
3167 case SND_SOC_DPCM_STATE_START:
3169 case SND_SOC_DPCM_STATE_STOP:
3171 case SND_SOC_DPCM_STATE_SUSPEND:
3173 case SND_SOC_DPCM_STATE_PAUSED:
3175 case SND_SOC_DPCM_STATE_HW_FREE:
3177 case SND_SOC_DPCM_STATE_CLOSE:
3184 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3185 int stream, char *buf, size_t size)
3187 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3188 struct snd_soc_dpcm *dpcm;
3192 offset += snprintf(buf + offset, size - offset,
3193 "[%s - %s]\n", fe->dai_link->name,
3194 stream ? "Capture" : "Playback");
3196 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3197 dpcm_state_string(fe->dpcm[stream].state));
3199 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3200 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3201 offset += snprintf(buf + offset, size - offset,
3203 "Format = %s, Channels = %d, Rate = %d\n",
3204 snd_pcm_format_name(params_format(params)),
3205 params_channels(params),
3206 params_rate(params));
3209 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3211 if (list_empty(&fe->dpcm[stream].be_clients)) {
3212 offset += snprintf(buf + offset, size - offset,
3213 " No active DSP links\n");
3217 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3218 struct snd_soc_pcm_runtime *be = dpcm->be;
3219 params = &dpcm->hw_params;
3221 offset += snprintf(buf + offset, size - offset,
3222 "- %s\n", be->dai_link->name);
3224 offset += snprintf(buf + offset, size - offset,
3226 dpcm_state_string(be->dpcm[stream].state));
3228 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3229 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3230 offset += snprintf(buf + offset, size - offset,
3231 " Hardware Params: "
3232 "Format = %s, Channels = %d, Rate = %d\n",
3233 snd_pcm_format_name(params_format(params)),
3234 params_channels(params),
3235 params_rate(params));
3242 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3243 size_t count, loff_t *ppos)
3245 struct snd_soc_pcm_runtime *fe = file->private_data;
3246 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3249 buf = kmalloc(out_count, GFP_KERNEL);
3253 if (fe->cpu_dai->driver->playback.channels_min)
3254 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3255 buf + offset, out_count - offset);
3257 if (fe->cpu_dai->driver->capture.channels_min)
3258 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3259 buf + offset, out_count - offset);
3261 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3267 static const struct file_operations dpcm_state_fops = {
3268 .open = simple_open,
3269 .read = dpcm_state_read_file,
3270 .llseek = default_llseek,
3273 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3278 if (!rtd->card->debugfs_card_root)
3281 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3282 rtd->card->debugfs_card_root);
3283 if (!rtd->debugfs_dpcm_root) {
3285 "ASoC: Failed to create dpcm debugfs directory %s\n",
3286 rtd->dai_link->name);
3290 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
3291 rtd->debugfs_dpcm_root,
3292 rtd, &dpcm_state_fops);