2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
44 struct snd_soc_pcm_stream *codec_stream;
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any channels at all, it supports the stream */
52 return codec_stream->channels_min;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70 lockdep_assert_held(&rtd->pcm_mutex);
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
83 cpu_dai->component->active++;
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 lockdep_assert_held(&rtd->pcm_mutex);
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
118 cpu_dai->component->active--;
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
175 struct snd_soc_dpcm *dpcm;
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
179 struct snd_soc_pcm_runtime *be = dpcm->be;
181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182 be->dai_link->name, event, dir);
184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
188 snd_soc_dapm_stream_event(be, dir, event);
191 snd_soc_dapm_stream_event(fe, dir, event);
196 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
207 ret = snd_pcm_hw_constraint_single(substream->runtime,
208 SNDRV_PCM_HW_PARAM_RATE,
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
223 ret = snd_pcm_hw_constraint_single(substream->runtime,
224 SNDRV_PCM_HW_PARAM_CHANNELS,
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
239 ret = snd_pcm_hw_constraint_single(substream->runtime,
240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
241 soc_dai->sample_bits);
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
253 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
258 unsigned int rate, channels, sample_bits, symmetry, i;
260 rate = params_rate(params);
261 channels = params_channels(params);
262 sample_bits = snd_pcm_format_physical_width(params_format(params));
264 /* reject unmatched parameters when applying symmetry */
265 symmetry = cpu_dai->driver->symmetric_rates ||
266 rtd->dai_link->symmetric_rates;
268 for (i = 0; i < rtd->num_codecs; i++)
269 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
271 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
272 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai->rate, rate);
277 symmetry = cpu_dai->driver->symmetric_channels ||
278 rtd->dai_link->symmetric_channels;
280 for (i = 0; i < rtd->num_codecs; i++)
281 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
283 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
284 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai->channels, channels);
289 symmetry = cpu_dai->driver->symmetric_samplebits ||
290 rtd->dai_link->symmetric_samplebits;
292 for (i = 0; i < rtd->num_codecs; i++)
293 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
295 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
296 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai->sample_bits, sample_bits);
304 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
308 struct snd_soc_dai_link *link = rtd->dai_link;
309 unsigned int symmetry, i;
311 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
312 cpu_driver->symmetric_channels || link->symmetric_channels ||
313 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
315 for (i = 0; i < rtd->num_codecs; i++)
316 symmetry = symmetry ||
317 rtd->codec_dais[i]->driver->symmetric_rates ||
318 rtd->codec_dais[i]->driver->symmetric_channels ||
319 rtd->codec_dais[i]->driver->symmetric_samplebits;
324 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
338 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
340 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
342 struct snd_soc_dai *codec_dai;
344 unsigned int bits = 0, cpu_bits;
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
347 for (i = 0; i < rtd->num_codecs; i++) {
348 codec_dai = rtd->codec_dais[i];
349 if (codec_dai->driver->playback.sig_bits == 0) {
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
355 cpu_bits = cpu_dai->driver->playback.sig_bits;
357 for (i = 0; i < rtd->num_codecs; i++) {
358 codec_dai = rtd->codec_dais[i];
359 if (codec_dai->driver->capture.sig_bits == 0) {
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
365 cpu_bits = cpu_dai->driver->capture.sig_bits;
368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
372 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_pcm_hardware *hw = &runtime->hw;
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
378 struct snd_soc_dai_driver *codec_dai_drv;
379 struct snd_soc_pcm_stream *codec_stream;
380 struct snd_soc_pcm_stream *cpu_stream;
381 unsigned int chan_min = 0, chan_max = UINT_MAX;
382 unsigned int rate_min = 0, rate_max = UINT_MAX;
383 unsigned int rates = UINT_MAX;
384 u64 formats = ULLONG_MAX;
387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
390 cpu_stream = &cpu_dai_drv->capture;
392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i = 0; i < rtd->num_codecs; i++) {
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
404 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
412 codec_stream = &codec_dai_drv->capture;
413 chan_min = max(chan_min, codec_stream->channels_min);
414 chan_max = min(chan_max, codec_stream->channels_max);
415 rate_min = max(rate_min, codec_stream->rate_min);
416 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
417 formats &= codec_stream->formats;
418 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
426 if (rtd->num_codecs > 1) {
427 chan_min = cpu_stream->channels_min;
428 chan_max = cpu_stream->channels_max;
431 hw->channels_min = max(chan_min, cpu_stream->channels_min);
432 hw->channels_max = min(chan_max, cpu_stream->channels_max);
434 hw->formats &= formats & cpu_stream->formats;
436 hw->formats = formats & cpu_stream->formats;
437 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
439 snd_pcm_limit_hw_rates(runtime);
441 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
442 hw->rate_min = max(hw->rate_min, rate_min);
443 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
444 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
452 static int soc_pcm_open(struct snd_pcm_substream *substream)
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct snd_soc_platform *platform = rtd->platform;
457 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
458 struct snd_soc_dai *codec_dai;
459 const char *codec_dai_name = "multicodec";
462 pinctrl_pm_select_default_state(cpu_dai->dev);
463 for (i = 0; i < rtd->num_codecs; i++)
464 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
465 pm_runtime_get_sync(cpu_dai->dev);
466 for (i = 0; i < rtd->num_codecs; i++)
467 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
468 pm_runtime_get_sync(platform->dev);
470 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
472 /* startup the audio subsystem */
473 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
474 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
476 dev_err(cpu_dai->dev, "ASoC: can't open interface"
477 " %s: %d\n", cpu_dai->name, ret);
482 if (platform->driver->ops && platform->driver->ops->open) {
483 ret = platform->driver->ops->open(substream);
485 dev_err(platform->dev, "ASoC: can't open platform"
486 " %s: %d\n", platform->component.name, ret);
491 for (i = 0; i < rtd->num_codecs; i++) {
492 codec_dai = rtd->codec_dais[i];
493 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
494 ret = codec_dai->driver->ops->startup(substream,
497 dev_err(codec_dai->dev,
498 "ASoC: can't open codec %s: %d\n",
499 codec_dai->name, ret);
504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
505 codec_dai->tx_mask = 0;
507 codec_dai->rx_mask = 0;
510 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511 ret = rtd->dai_link->ops->startup(substream);
513 pr_err("ASoC: %s startup failed: %d\n",
514 rtd->dai_link->name, ret);
519 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
520 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
523 /* Check that the codec and cpu DAIs are compatible */
524 soc_pcm_init_runtime_hw(substream);
526 if (rtd->num_codecs == 1)
527 codec_dai_name = rtd->codec_dai->name;
529 if (soc_pcm_has_symmetry(substream))
530 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
533 if (!runtime->hw.rates) {
534 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
535 codec_dai_name, cpu_dai->name);
538 if (!runtime->hw.formats) {
539 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
540 codec_dai_name, cpu_dai->name);
543 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
544 runtime->hw.channels_min > runtime->hw.channels_max) {
545 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
546 codec_dai_name, cpu_dai->name);
550 soc_pcm_apply_msb(substream);
552 /* Symmetry only applies if we've already got an active stream. */
553 if (cpu_dai->active) {
554 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
559 for (i = 0; i < rtd->num_codecs; i++) {
560 if (rtd->codec_dais[i]->active) {
561 ret = soc_pcm_apply_symmetry(substream,
568 pr_debug("ASoC: %s <-> %s info:\n",
569 codec_dai_name, cpu_dai->name);
570 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
571 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
572 runtime->hw.channels_max);
573 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
574 runtime->hw.rate_max);
578 snd_soc_runtime_activate(rtd, substream->stream);
580 mutex_unlock(&rtd->pcm_mutex);
584 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
585 rtd->dai_link->ops->shutdown(substream);
592 codec_dai = rtd->codec_dais[i];
593 if (codec_dai->driver->ops->shutdown)
594 codec_dai->driver->ops->shutdown(substream, codec_dai);
597 if (platform->driver->ops && platform->driver->ops->close)
598 platform->driver->ops->close(substream);
601 if (cpu_dai->driver->ops->shutdown)
602 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
604 mutex_unlock(&rtd->pcm_mutex);
606 pm_runtime_put(platform->dev);
607 for (i = 0; i < rtd->num_codecs; i++)
608 pm_runtime_put(rtd->codec_dais[i]->dev);
609 pm_runtime_put(cpu_dai->dev);
610 for (i = 0; i < rtd->num_codecs; i++) {
611 if (!rtd->codec_dais[i]->active)
612 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
614 if (!cpu_dai->active)
615 pinctrl_pm_select_sleep_state(cpu_dai->dev);
621 * Power down the audio subsystem pmdown_time msecs after close is called.
622 * This is to ensure there are no pops or clicks in between any music tracks
623 * due to DAPM power cycling.
625 static void close_delayed_work(struct work_struct *work)
627 struct snd_soc_pcm_runtime *rtd =
628 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
629 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
631 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
633 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
634 codec_dai->driver->playback.stream_name,
635 codec_dai->playback_active ? "active" : "inactive",
636 rtd->pop_wait ? "yes" : "no");
638 /* are we waiting on this codec DAI stream */
639 if (rtd->pop_wait == 1) {
641 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
642 SND_SOC_DAPM_STREAM_STOP);
645 mutex_unlock(&rtd->pcm_mutex);
649 * Called by ALSA when a PCM substream is closed. Private data can be
650 * freed here. The cpu DAI, codec DAI, machine and platform are also
653 static int soc_pcm_close(struct snd_pcm_substream *substream)
655 struct snd_soc_pcm_runtime *rtd = substream->private_data;
656 struct snd_soc_platform *platform = rtd->platform;
657 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
658 struct snd_soc_dai *codec_dai;
661 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
663 snd_soc_runtime_deactivate(rtd, substream->stream);
665 /* clear the corresponding DAIs rate when inactive */
666 if (!cpu_dai->active)
669 for (i = 0; i < rtd->num_codecs; i++) {
670 codec_dai = rtd->codec_dais[i];
671 if (!codec_dai->active)
675 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
677 if (cpu_dai->driver->ops->shutdown)
678 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
680 for (i = 0; i < rtd->num_codecs; i++) {
681 codec_dai = rtd->codec_dais[i];
682 if (codec_dai->driver->ops->shutdown)
683 codec_dai->driver->ops->shutdown(substream, codec_dai);
686 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
687 rtd->dai_link->ops->shutdown(substream);
689 if (platform->driver->ops && platform->driver->ops->close)
690 platform->driver->ops->close(substream);
692 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
693 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
694 /* powered down playback stream now */
695 snd_soc_dapm_stream_event(rtd,
696 SNDRV_PCM_STREAM_PLAYBACK,
697 SND_SOC_DAPM_STREAM_STOP);
699 /* start delayed pop wq here for playback streams */
701 queue_delayed_work(system_power_efficient_wq,
703 msecs_to_jiffies(rtd->pmdown_time));
706 /* capture streams can be powered down now */
707 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
708 SND_SOC_DAPM_STREAM_STOP);
711 mutex_unlock(&rtd->pcm_mutex);
713 pm_runtime_put(platform->dev);
714 for (i = 0; i < rtd->num_codecs; i++)
715 pm_runtime_put(rtd->codec_dais[i]->dev);
716 pm_runtime_put(cpu_dai->dev);
717 for (i = 0; i < rtd->num_codecs; i++) {
718 if (!rtd->codec_dais[i]->active)
719 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
721 if (!cpu_dai->active)
722 pinctrl_pm_select_sleep_state(cpu_dai->dev);
728 * Called by ALSA when the PCM substream is prepared, can set format, sample
729 * rate, etc. This function is non atomic and can be called multiple times,
730 * it can refer to the runtime info.
732 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
734 struct snd_soc_pcm_runtime *rtd = substream->private_data;
735 struct snd_soc_platform *platform = rtd->platform;
736 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
737 struct snd_soc_dai *codec_dai;
740 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
742 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
743 ret = rtd->dai_link->ops->prepare(substream);
745 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
751 if (platform->driver->ops && platform->driver->ops->prepare) {
752 ret = platform->driver->ops->prepare(substream);
754 dev_err(platform->dev, "ASoC: platform prepare error:"
760 for (i = 0; i < rtd->num_codecs; i++) {
761 codec_dai = rtd->codec_dais[i];
762 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
763 ret = codec_dai->driver->ops->prepare(substream,
766 dev_err(codec_dai->dev,
767 "ASoC: codec DAI prepare error: %d\n",
774 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
775 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
777 dev_err(cpu_dai->dev,
778 "ASoC: cpu DAI prepare error: %d\n", ret);
783 /* cancel any delayed stream shutdown that is pending */
784 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
787 cancel_delayed_work(&rtd->delayed_work);
790 snd_soc_dapm_stream_event(rtd, substream->stream,
791 SND_SOC_DAPM_STREAM_START);
793 for (i = 0; i < rtd->num_codecs; i++)
794 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
796 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
799 mutex_unlock(&rtd->pcm_mutex);
803 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
806 struct snd_interval *interval;
807 int channels = hweight_long(mask);
809 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
810 interval->min = channels;
811 interval->max = channels;
814 int soc_dai_hw_params(struct snd_pcm_substream *substream,
815 struct snd_pcm_hw_params *params,
816 struct snd_soc_dai *dai)
820 if (dai->driver->ops && dai->driver->ops->hw_params) {
821 ret = dai->driver->ops->hw_params(substream, params, dai);
823 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
833 * Called by ALSA when the hardware params are set by application. This
834 * function can also be called multiple times and can allocate buffers
835 * (using snd_pcm_lib_* ). It's non-atomic.
837 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
838 struct snd_pcm_hw_params *params)
840 struct snd_soc_pcm_runtime *rtd = substream->private_data;
841 struct snd_soc_platform *platform = rtd->platform;
842 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
845 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
847 ret = soc_pcm_params_symmetry(substream, params);
851 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
852 ret = rtd->dai_link->ops->hw_params(substream, params);
854 dev_err(rtd->card->dev, "ASoC: machine hw_params"
855 " failed: %d\n", ret);
860 for (i = 0; i < rtd->num_codecs; i++) {
861 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
862 struct snd_pcm_hw_params codec_params;
865 * Skip CODECs which don't support the current stream type,
866 * the idea being that if a CODEC is not used for the currently
867 * set up transfer direction, it should not need to be
868 * configured, especially since the configuration used might
869 * not even be supported by that CODEC. There may be cases
870 * however where a CODEC needs to be set up although it is
871 * actually not being used for the transfer, e.g. if a
872 * capture-only CODEC is acting as an LRCLK and/or BCLK master
873 * for the DAI link including a playback-only CODEC.
874 * If this becomes necessary, we will have to augment the
875 * machine driver setup with information on how to act, so
876 * we can do the right thing here.
878 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
881 /* copy params for each codec */
882 codec_params = *params;
884 /* fixup params based on TDM slot masks */
885 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
887 soc_pcm_codec_params_fixup(&codec_params,
890 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
892 soc_pcm_codec_params_fixup(&codec_params,
895 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
899 codec_dai->rate = params_rate(&codec_params);
900 codec_dai->channels = params_channels(&codec_params);
901 codec_dai->sample_bits = snd_pcm_format_physical_width(
902 params_format(&codec_params));
905 ret = soc_dai_hw_params(substream, params, cpu_dai);
909 if (platform->driver->ops && platform->driver->ops->hw_params) {
910 ret = platform->driver->ops->hw_params(substream, params);
912 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
913 platform->component.name, ret);
918 /* store the parameters for each DAIs */
919 cpu_dai->rate = params_rate(params);
920 cpu_dai->channels = params_channels(params);
921 cpu_dai->sample_bits =
922 snd_pcm_format_physical_width(params_format(params));
925 mutex_unlock(&rtd->pcm_mutex);
929 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
930 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
937 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
938 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
939 codec_dai->driver->ops->hw_free(substream, codec_dai);
943 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
944 rtd->dai_link->ops->hw_free(substream);
946 mutex_unlock(&rtd->pcm_mutex);
951 * Frees resources allocated by hw_params, can be called multiple times
953 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
955 struct snd_soc_pcm_runtime *rtd = substream->private_data;
956 struct snd_soc_platform *platform = rtd->platform;
957 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
958 struct snd_soc_dai *codec_dai;
959 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
962 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
964 /* clear the corresponding DAIs parameters when going to be inactive */
965 if (cpu_dai->active == 1) {
967 cpu_dai->channels = 0;
968 cpu_dai->sample_bits = 0;
971 for (i = 0; i < rtd->num_codecs; i++) {
972 codec_dai = rtd->codec_dais[i];
973 if (codec_dai->active == 1) {
975 codec_dai->channels = 0;
976 codec_dai->sample_bits = 0;
980 /* apply codec digital mute */
981 for (i = 0; i < rtd->num_codecs; i++) {
982 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
983 (!playback && rtd->codec_dais[i]->capture_active == 1))
984 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
988 /* free any machine hw params */
989 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
990 rtd->dai_link->ops->hw_free(substream);
992 /* free any DMA resources */
993 if (platform->driver->ops && platform->driver->ops->hw_free)
994 platform->driver->ops->hw_free(substream);
996 /* now free hw params for the DAIs */
997 for (i = 0; i < rtd->num_codecs; i++) {
998 codec_dai = rtd->codec_dais[i];
999 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1000 codec_dai->driver->ops->hw_free(substream, codec_dai);
1003 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1004 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1006 mutex_unlock(&rtd->pcm_mutex);
1010 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1012 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1013 struct snd_soc_platform *platform = rtd->platform;
1014 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1015 struct snd_soc_dai *codec_dai;
1018 for (i = 0; i < rtd->num_codecs; i++) {
1019 codec_dai = rtd->codec_dais[i];
1020 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1021 ret = codec_dai->driver->ops->trigger(substream,
1028 if (platform->driver->ops && platform->driver->ops->trigger) {
1029 ret = platform->driver->ops->trigger(substream, cmd);
1034 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1035 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1040 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1041 ret = rtd->dai_link->ops->trigger(substream, cmd);
1049 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1052 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1053 struct snd_soc_platform *platform = rtd->platform;
1054 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1055 struct snd_soc_dai *codec_dai;
1058 for (i = 0; i < rtd->num_codecs; i++) {
1059 codec_dai = rtd->codec_dais[i];
1060 if (codec_dai->driver->ops &&
1061 codec_dai->driver->ops->bespoke_trigger) {
1062 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1069 if (platform->driver->bespoke_trigger) {
1070 ret = platform->driver->bespoke_trigger(substream, cmd);
1075 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1076 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1083 * soc level wrapper for pointer callback
1084 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1085 * the runtime->delay will be updated accordingly.
1087 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1089 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1090 struct snd_soc_platform *platform = rtd->platform;
1091 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1092 struct snd_soc_dai *codec_dai;
1093 struct snd_pcm_runtime *runtime = substream->runtime;
1094 snd_pcm_uframes_t offset = 0;
1095 snd_pcm_sframes_t delay = 0;
1096 snd_pcm_sframes_t codec_delay = 0;
1099 if (platform->driver->ops && platform->driver->ops->pointer)
1100 offset = platform->driver->ops->pointer(substream);
1102 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1103 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1105 for (i = 0; i < rtd->num_codecs; i++) {
1106 codec_dai = rtd->codec_dais[i];
1107 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1108 codec_delay = max(codec_delay,
1109 codec_dai->driver->ops->delay(substream,
1112 delay += codec_delay;
1115 * None of the existing platform drivers implement delay(), so
1116 * for now the codec_dai of first multicodec entry is used
1118 if (platform->driver->delay)
1119 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1121 runtime->delay = delay;
1126 /* connect a FE and BE */
1127 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1128 struct snd_soc_pcm_runtime *be, int stream)
1130 struct snd_soc_dpcm *dpcm;
1132 /* only add new dpcms */
1133 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1134 if (dpcm->be == be && dpcm->fe == fe)
1138 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1144 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1145 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1146 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1147 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1149 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1150 stream ? "capture" : "playback", fe->dai_link->name,
1151 stream ? "<-" : "->", be->dai_link->name);
1153 #ifdef CONFIG_DEBUG_FS
1154 if (fe->debugfs_dpcm_root)
1155 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1156 fe->debugfs_dpcm_root, &dpcm->state);
1161 /* reparent a BE onto another FE */
1162 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1163 struct snd_soc_pcm_runtime *be, int stream)
1165 struct snd_soc_dpcm *dpcm;
1166 struct snd_pcm_substream *fe_substream, *be_substream;
1168 /* reparent if BE is connected to other FEs */
1169 if (!be->dpcm[stream].users)
1172 be_substream = snd_soc_dpcm_get_substream(be, stream);
1174 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1178 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1179 stream ? "capture" : "playback",
1180 dpcm->fe->dai_link->name,
1181 stream ? "<-" : "->", dpcm->be->dai_link->name);
1183 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1184 be_substream->runtime = fe_substream->runtime;
1189 /* disconnect a BE and FE */
1190 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1192 struct snd_soc_dpcm *dpcm, *d;
1194 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1195 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1196 stream ? "capture" : "playback",
1197 dpcm->be->dai_link->name);
1199 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1202 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1203 stream ? "capture" : "playback", fe->dai_link->name,
1204 stream ? "<-" : "->", dpcm->be->dai_link->name);
1206 /* BEs still alive need new FE */
1207 dpcm_be_reparent(fe, dpcm->be, stream);
1209 #ifdef CONFIG_DEBUG_FS
1210 debugfs_remove(dpcm->debugfs_state);
1212 list_del(&dpcm->list_be);
1213 list_del(&dpcm->list_fe);
1218 /* get BE for DAI widget and stream */
1219 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1220 struct snd_soc_dapm_widget *widget, int stream)
1222 struct snd_soc_pcm_runtime *be;
1225 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1226 for (i = 0; i < card->num_links; i++) {
1229 if (!be->dai_link->no_pcm)
1232 if (be->cpu_dai->playback_widget == widget)
1235 for (j = 0; j < be->num_codecs; j++) {
1236 struct snd_soc_dai *dai = be->codec_dais[j];
1237 if (dai->playback_widget == widget)
1243 for (i = 0; i < card->num_links; i++) {
1246 if (!be->dai_link->no_pcm)
1249 if (be->cpu_dai->capture_widget == widget)
1252 for (j = 0; j < be->num_codecs; j++) {
1253 struct snd_soc_dai *dai = be->codec_dais[j];
1254 if (dai->capture_widget == widget)
1260 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1261 stream ? "capture" : "playback", widget->name);
1265 static inline struct snd_soc_dapm_widget *
1266 dai_get_widget(struct snd_soc_dai *dai, int stream)
1268 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1269 return dai->playback_widget;
1271 return dai->capture_widget;
1274 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1275 struct snd_soc_dapm_widget *widget)
1279 for (i = 0; i < list->num_widgets; i++) {
1280 if (widget == list->widgets[i])
1287 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1288 int stream, struct snd_soc_dapm_widget_list **list)
1290 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1293 /* get number of valid DAI paths and their widgets */
1294 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
1296 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1297 stream ? "capture" : "playback");
1302 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1303 struct snd_soc_dapm_widget_list **list_)
1305 struct snd_soc_dpcm *dpcm;
1306 struct snd_soc_dapm_widget_list *list = *list_;
1307 struct snd_soc_dapm_widget *widget;
1310 /* Destroy any old FE <--> BE connections */
1311 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1314 /* is there a valid CPU DAI widget for this BE */
1315 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1317 /* prune the BE if it's no longer in our active list */
1318 if (widget && widget_in_list(list, widget))
1321 /* is there a valid CODEC DAI widget for this BE */
1322 for (i = 0; i < dpcm->be->num_codecs; i++) {
1323 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1324 widget = dai_get_widget(dai, stream);
1326 /* prune the BE if it's no longer in our active list */
1327 if (widget && widget_in_list(list, widget))
1331 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1332 stream ? "capture" : "playback",
1333 dpcm->be->dai_link->name, fe->dai_link->name);
1334 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1335 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1339 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1343 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1344 struct snd_soc_dapm_widget_list **list_)
1346 struct snd_soc_card *card = fe->card;
1347 struct snd_soc_dapm_widget_list *list = *list_;
1348 struct snd_soc_pcm_runtime *be;
1349 int i, new = 0, err;
1351 /* Create any new FE <--> BE connections */
1352 for (i = 0; i < list->num_widgets; i++) {
1354 switch (list->widgets[i]->id) {
1355 case snd_soc_dapm_dai_in:
1356 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1359 case snd_soc_dapm_dai_out:
1360 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1367 /* is there a valid BE rtd for this widget */
1368 be = dpcm_get_be(card, list->widgets[i], stream);
1370 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1371 list->widgets[i]->name);
1375 /* make sure BE is a real BE */
1376 if (!be->dai_link->no_pcm)
1379 /* don't connect if FE is not running */
1380 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1383 /* newly connected FE and BE */
1384 err = dpcm_be_connect(fe, be, stream);
1386 dev_err(fe->dev, "ASoC: can't connect %s\n",
1387 list->widgets[i]->name);
1389 } else if (err == 0) /* already connected */
1393 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1397 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1402 * Find the corresponding BE DAIs that source or sink audio to this
1405 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1406 int stream, struct snd_soc_dapm_widget_list **list, int new)
1409 return dpcm_add_paths(fe, stream, list);
1411 return dpcm_prune_paths(fe, stream, list);
1414 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1416 struct snd_soc_dpcm *dpcm;
1418 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1419 dpcm->be->dpcm[stream].runtime_update =
1420 SND_SOC_DPCM_UPDATE_NO;
1423 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1426 struct snd_soc_dpcm *dpcm;
1428 /* disable any enabled and non active backends */
1429 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1431 struct snd_soc_pcm_runtime *be = dpcm->be;
1432 struct snd_pcm_substream *be_substream =
1433 snd_soc_dpcm_get_substream(be, stream);
1435 if (be->dpcm[stream].users == 0)
1436 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1437 stream ? "capture" : "playback",
1438 be->dpcm[stream].state);
1440 if (--be->dpcm[stream].users != 0)
1443 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1446 soc_pcm_close(be_substream);
1447 be_substream->runtime = NULL;
1448 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1452 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1454 struct snd_soc_dpcm *dpcm;
1457 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1458 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1460 struct snd_soc_pcm_runtime *be = dpcm->be;
1461 struct snd_pcm_substream *be_substream =
1462 snd_soc_dpcm_get_substream(be, stream);
1464 if (!be_substream) {
1465 dev_err(be->dev, "ASoC: no backend %s stream\n",
1466 stream ? "capture" : "playback");
1470 /* is this op for this BE ? */
1471 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1474 /* first time the dpcm is open ? */
1475 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1476 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1477 stream ? "capture" : "playback",
1478 be->dpcm[stream].state);
1480 if (be->dpcm[stream].users++ != 0)
1483 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1484 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1487 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1488 stream ? "capture" : "playback", be->dai_link->name);
1490 be_substream->runtime = be->dpcm[stream].runtime;
1491 err = soc_pcm_open(be_substream);
1493 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1494 be->dpcm[stream].users--;
1495 if (be->dpcm[stream].users < 0)
1496 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1497 stream ? "capture" : "playback",
1498 be->dpcm[stream].state);
1500 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1504 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1511 /* disable any enabled and non active backends */
1512 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1513 struct snd_soc_pcm_runtime *be = dpcm->be;
1514 struct snd_pcm_substream *be_substream =
1515 snd_soc_dpcm_get_substream(be, stream);
1517 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1520 if (be->dpcm[stream].users == 0)
1521 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1522 stream ? "capture" : "playback",
1523 be->dpcm[stream].state);
1525 if (--be->dpcm[stream].users != 0)
1528 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1531 soc_pcm_close(be_substream);
1532 be_substream->runtime = NULL;
1533 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1539 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1540 struct snd_soc_pcm_stream *stream,
1543 runtime->hw.rate_min = stream->rate_min;
1544 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1545 runtime->hw.channels_min = stream->channels_min;
1546 runtime->hw.channels_max = stream->channels_max;
1547 if (runtime->hw.formats)
1548 runtime->hw.formats &= formats & stream->formats;
1550 runtime->hw.formats = formats & stream->formats;
1551 runtime->hw.rates = stream->rates;
1554 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1556 struct snd_soc_pcm_runtime *fe = substream->private_data;
1557 struct snd_soc_dpcm *dpcm;
1558 u64 formats = ULLONG_MAX;
1559 int stream = substream->stream;
1561 if (!fe->dai_link->dpcm_merged_format)
1565 * It returns merged BE codec format
1566 * if FE want to use it (= dpcm_merged_format)
1569 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1570 struct snd_soc_pcm_runtime *be = dpcm->be;
1571 struct snd_soc_dai_driver *codec_dai_drv;
1572 struct snd_soc_pcm_stream *codec_stream;
1575 for (i = 0; i < be->num_codecs; i++) {
1577 * Skip CODECs which don't support the current stream
1578 * type. See soc_pcm_init_runtime_hw() for more details
1580 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1584 codec_dai_drv = be->codec_dais[i]->driver;
1585 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1586 codec_stream = &codec_dai_drv->playback;
1588 codec_stream = &codec_dai_drv->capture;
1590 formats &= codec_stream->formats;
1597 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1599 struct snd_pcm_runtime *runtime = substream->runtime;
1600 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1601 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1602 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1603 u64 format = dpcm_runtime_base_format(substream);
1605 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1606 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1608 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1611 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1613 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1614 * for avoiding the race with trigger callback.
1615 * If the state is unset and a trigger is pending while the previous operation,
1616 * process the pending trigger action here.
1618 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1619 int stream, enum snd_soc_dpcm_update state)
1621 struct snd_pcm_substream *substream =
1622 snd_soc_dpcm_get_substream(fe, stream);
1624 snd_pcm_stream_lock_irq(substream);
1625 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1626 dpcm_fe_dai_do_trigger(substream,
1627 fe->dpcm[stream].trigger_pending - 1);
1628 fe->dpcm[stream].trigger_pending = 0;
1630 fe->dpcm[stream].runtime_update = state;
1631 snd_pcm_stream_unlock_irq(substream);
1634 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1636 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1637 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1638 int stream = fe_substream->stream, ret = 0;
1640 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1642 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1644 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1648 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1650 /* start the DAI frontend */
1651 ret = soc_pcm_open(fe_substream);
1653 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1657 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1659 dpcm_set_fe_runtime(fe_substream);
1660 snd_pcm_limit_hw_rates(runtime);
1662 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1666 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1668 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1672 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1674 struct snd_soc_dpcm *dpcm;
1676 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1677 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1679 struct snd_soc_pcm_runtime *be = dpcm->be;
1680 struct snd_pcm_substream *be_substream =
1681 snd_soc_dpcm_get_substream(be, stream);
1683 /* is this op for this BE ? */
1684 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1687 if (be->dpcm[stream].users == 0)
1688 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1689 stream ? "capture" : "playback",
1690 be->dpcm[stream].state);
1692 if (--be->dpcm[stream].users != 0)
1695 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1696 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1697 soc_pcm_hw_free(be_substream);
1698 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1701 dev_dbg(be->dev, "ASoC: close BE %s\n",
1702 dpcm->fe->dai_link->name);
1704 soc_pcm_close(be_substream);
1705 be_substream->runtime = NULL;
1707 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1712 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1714 struct snd_soc_pcm_runtime *fe = substream->private_data;
1715 int stream = substream->stream;
1717 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1719 /* shutdown the BEs */
1720 dpcm_be_dai_shutdown(fe, substream->stream);
1722 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1724 /* now shutdown the frontend */
1725 soc_pcm_close(substream);
1727 /* run the stream event for each BE */
1728 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1730 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1731 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1735 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1737 struct snd_soc_dpcm *dpcm;
1739 /* only hw_params backends that are either sinks or sources
1740 * to this frontend DAI */
1741 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1743 struct snd_soc_pcm_runtime *be = dpcm->be;
1744 struct snd_pcm_substream *be_substream =
1745 snd_soc_dpcm_get_substream(be, stream);
1747 /* is this op for this BE ? */
1748 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1751 /* only free hw when no longer used - check all FEs */
1752 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1755 /* do not free hw if this BE is used by other FE */
1756 if (be->dpcm[stream].users > 1)
1759 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1760 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1761 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1762 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1763 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1764 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1767 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1768 dpcm->fe->dai_link->name);
1770 soc_pcm_hw_free(be_substream);
1772 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1778 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1780 struct snd_soc_pcm_runtime *fe = substream->private_data;
1781 int err, stream = substream->stream;
1783 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1784 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1786 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1788 /* call hw_free on the frontend */
1789 err = soc_pcm_hw_free(substream);
1791 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1792 fe->dai_link->name);
1794 /* only hw_params backends that are either sinks or sources
1795 * to this frontend DAI */
1796 err = dpcm_be_dai_hw_free(fe, stream);
1798 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1799 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1801 mutex_unlock(&fe->card->mutex);
1805 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1807 struct snd_soc_dpcm *dpcm;
1810 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1812 struct snd_soc_pcm_runtime *be = dpcm->be;
1813 struct snd_pcm_substream *be_substream =
1814 snd_soc_dpcm_get_substream(be, stream);
1816 /* is this op for this BE ? */
1817 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1820 /* only allow hw_params() if no connected FEs are running */
1821 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1824 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1825 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1826 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1829 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1830 dpcm->fe->dai_link->name);
1832 /* copy params for each dpcm */
1833 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1834 sizeof(struct snd_pcm_hw_params));
1836 /* perform any hw_params fixups */
1837 if (be->dai_link->be_hw_params_fixup) {
1838 ret = be->dai_link->be_hw_params_fixup(be,
1842 "ASoC: hw_params BE fixup failed %d\n",
1848 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1850 dev_err(dpcm->be->dev,
1851 "ASoC: hw_params BE failed %d\n", ret);
1855 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1860 /* disable any enabled and non active backends */
1861 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1862 struct snd_soc_pcm_runtime *be = dpcm->be;
1863 struct snd_pcm_substream *be_substream =
1864 snd_soc_dpcm_get_substream(be, stream);
1866 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1869 /* only allow hw_free() if no connected FEs are running */
1870 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1873 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1874 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1875 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1876 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1879 soc_pcm_hw_free(be_substream);
1885 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1886 struct snd_pcm_hw_params *params)
1888 struct snd_soc_pcm_runtime *fe = substream->private_data;
1889 int ret, stream = substream->stream;
1891 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1892 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1894 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1895 sizeof(struct snd_pcm_hw_params));
1896 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1898 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1902 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1903 fe->dai_link->name, params_rate(params),
1904 params_channels(params), params_format(params));
1906 /* call hw_params on the frontend */
1907 ret = soc_pcm_hw_params(substream, params);
1909 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1910 dpcm_be_dai_hw_free(fe, stream);
1912 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1915 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1916 mutex_unlock(&fe->card->mutex);
1920 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1921 struct snd_pcm_substream *substream, int cmd)
1925 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1926 dpcm->fe->dai_link->name, cmd);
1928 ret = soc_pcm_trigger(substream, cmd);
1930 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1935 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1938 struct snd_soc_dpcm *dpcm;
1941 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1943 struct snd_soc_pcm_runtime *be = dpcm->be;
1944 struct snd_pcm_substream *be_substream =
1945 snd_soc_dpcm_get_substream(be, stream);
1947 /* is this op for this BE ? */
1948 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1952 case SNDRV_PCM_TRIGGER_START:
1953 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1954 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1955 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1958 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1962 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1964 case SNDRV_PCM_TRIGGER_RESUME:
1965 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1968 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1972 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1974 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1975 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1978 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1982 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1984 case SNDRV_PCM_TRIGGER_STOP:
1985 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
1986 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1989 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1992 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1996 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1998 case SNDRV_PCM_TRIGGER_SUSPEND:
1999 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2002 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2005 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2009 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2011 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2012 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2015 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2018 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2022 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2029 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2031 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2032 int cmd, bool fe_first)
2034 struct snd_soc_pcm_runtime *fe = substream->private_data;
2037 /* call trigger on the frontend before the backend. */
2039 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2040 fe->dai_link->name, cmd);
2042 ret = soc_pcm_trigger(substream, cmd);
2046 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2050 /* call trigger on the frontend after the backend. */
2051 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2055 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2056 fe->dai_link->name, cmd);
2058 ret = soc_pcm_trigger(substream, cmd);
2063 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2065 struct snd_soc_pcm_runtime *fe = substream->private_data;
2066 int stream = substream->stream;
2068 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2070 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2073 case SND_SOC_DPCM_TRIGGER_PRE:
2075 case SNDRV_PCM_TRIGGER_START:
2076 case SNDRV_PCM_TRIGGER_RESUME:
2077 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2078 case SNDRV_PCM_TRIGGER_DRAIN:
2079 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2081 case SNDRV_PCM_TRIGGER_STOP:
2082 case SNDRV_PCM_TRIGGER_SUSPEND:
2083 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2084 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2091 case SND_SOC_DPCM_TRIGGER_POST:
2093 case SNDRV_PCM_TRIGGER_START:
2094 case SNDRV_PCM_TRIGGER_RESUME:
2095 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2096 case SNDRV_PCM_TRIGGER_DRAIN:
2097 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2099 case SNDRV_PCM_TRIGGER_STOP:
2100 case SNDRV_PCM_TRIGGER_SUSPEND:
2101 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2102 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2109 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2110 /* bespoke trigger() - handles both FE and BEs */
2112 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2113 fe->dai_link->name, cmd);
2115 ret = soc_pcm_bespoke_trigger(substream, cmd);
2118 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2119 fe->dai_link->name);
2125 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2131 case SNDRV_PCM_TRIGGER_START:
2132 case SNDRV_PCM_TRIGGER_RESUME:
2133 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2134 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2136 case SNDRV_PCM_TRIGGER_STOP:
2137 case SNDRV_PCM_TRIGGER_SUSPEND:
2138 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2140 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2141 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2146 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2150 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2152 struct snd_soc_pcm_runtime *fe = substream->private_data;
2153 int stream = substream->stream;
2155 /* if FE's runtime_update is already set, we're in race;
2156 * process this trigger later at exit
2158 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2159 fe->dpcm[stream].trigger_pending = cmd + 1;
2160 return 0; /* delayed, assuming it's successful */
2163 /* we're alone, let's trigger */
2164 return dpcm_fe_dai_do_trigger(substream, cmd);
2167 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2169 struct snd_soc_dpcm *dpcm;
2172 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2174 struct snd_soc_pcm_runtime *be = dpcm->be;
2175 struct snd_pcm_substream *be_substream =
2176 snd_soc_dpcm_get_substream(be, stream);
2178 /* is this op for this BE ? */
2179 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2182 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2183 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2186 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2187 dpcm->fe->dai_link->name);
2189 ret = soc_pcm_prepare(be_substream);
2191 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2196 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2201 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2203 struct snd_soc_pcm_runtime *fe = substream->private_data;
2204 int stream = substream->stream, ret = 0;
2206 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2208 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2210 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2212 /* there is no point preparing this FE if there are no BEs */
2213 if (list_empty(&fe->dpcm[stream].be_clients)) {
2214 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2215 fe->dai_link->name);
2220 ret = dpcm_be_dai_prepare(fe, substream->stream);
2224 /* call prepare on the frontend */
2225 ret = soc_pcm_prepare(substream);
2227 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2228 fe->dai_link->name);
2232 /* run the stream event for each BE */
2233 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2234 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2237 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2238 mutex_unlock(&fe->card->mutex);
2243 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2244 unsigned int cmd, void *arg)
2246 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2247 struct snd_soc_platform *platform = rtd->platform;
2249 if (platform->driver->ops && platform->driver->ops->ioctl)
2250 return platform->driver->ops->ioctl(substream, cmd, arg);
2251 return snd_pcm_lib_ioctl(substream, cmd, arg);
2254 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2256 struct snd_pcm_substream *substream =
2257 snd_soc_dpcm_get_substream(fe, stream);
2258 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2261 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2262 stream ? "capture" : "playback", fe->dai_link->name);
2264 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2265 /* call bespoke trigger - FE takes care of all BE triggers */
2266 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2267 fe->dai_link->name);
2269 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2271 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2273 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2274 fe->dai_link->name);
2276 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2278 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2281 err = dpcm_be_dai_hw_free(fe, stream);
2283 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2285 err = dpcm_be_dai_shutdown(fe, stream);
2287 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2289 /* run the stream event for each BE */
2290 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2295 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2297 struct snd_pcm_substream *substream =
2298 snd_soc_dpcm_get_substream(fe, stream);
2299 struct snd_soc_dpcm *dpcm;
2300 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2303 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2304 stream ? "capture" : "playback", fe->dai_link->name);
2306 /* Only start the BE if the FE is ready */
2307 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2308 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2311 /* startup must always be called for new BEs */
2312 ret = dpcm_be_dai_startup(fe, stream);
2316 /* keep going if FE state is > open */
2317 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2320 ret = dpcm_be_dai_hw_params(fe, stream);
2324 /* keep going if FE state is > hw_params */
2325 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2329 ret = dpcm_be_dai_prepare(fe, stream);
2333 /* run the stream event for each BE */
2334 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2336 /* keep going if FE state is > prepare */
2337 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2338 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2341 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2342 /* call trigger on the frontend - FE takes care of all BE triggers */
2343 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2344 fe->dai_link->name);
2346 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2348 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2352 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2353 fe->dai_link->name);
2355 ret = dpcm_be_dai_trigger(fe, stream,
2356 SNDRV_PCM_TRIGGER_START);
2358 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2366 dpcm_be_dai_hw_free(fe, stream);
2368 dpcm_be_dai_shutdown(fe, stream);
2370 /* disconnect any non started BEs */
2371 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2372 struct snd_soc_pcm_runtime *be = dpcm->be;
2373 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2374 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2380 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2384 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2385 ret = dpcm_run_update_startup(fe, stream);
2387 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2388 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2393 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2397 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2398 ret = dpcm_run_update_shutdown(fe, stream);
2400 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2401 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2406 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2409 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2411 int i, old, new, paths;
2413 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2414 for (i = 0; i < card->num_rtd; i++) {
2415 struct snd_soc_dapm_widget_list *list;
2416 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2418 /* make sure link is FE */
2419 if (!fe->dai_link->dynamic)
2422 /* only check active links */
2423 if (!fe->cpu_dai->active)
2426 /* DAPM sync will call this to update DSP paths */
2427 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2428 fe->dai_link->name);
2430 /* skip if FE doesn't have playback capability */
2431 if (!fe->cpu_dai->driver->playback.channels_min
2432 || !fe->codec_dai->driver->playback.channels_min)
2435 /* skip if FE isn't currently playing */
2436 if (!fe->cpu_dai->playback_active
2437 || !fe->codec_dai->playback_active)
2440 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2442 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2443 fe->dai_link->name, "playback");
2444 mutex_unlock(&card->mutex);
2448 /* update any new playback paths */
2449 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2451 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2452 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2453 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2456 /* update any old playback paths */
2457 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2459 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2460 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2461 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2464 dpcm_path_put(&list);
2466 /* skip if FE doesn't have capture capability */
2467 if (!fe->cpu_dai->driver->capture.channels_min
2468 || !fe->codec_dai->driver->capture.channels_min)
2471 /* skip if FE isn't currently capturing */
2472 if (!fe->cpu_dai->capture_active
2473 || !fe->codec_dai->capture_active)
2476 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2478 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2479 fe->dai_link->name, "capture");
2480 mutex_unlock(&card->mutex);
2484 /* update any new capture paths */
2485 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2487 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2488 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2489 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2492 /* update any old capture paths */
2493 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2495 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2496 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2497 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2500 dpcm_path_put(&list);
2503 mutex_unlock(&card->mutex);
2506 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2508 struct snd_soc_dpcm *dpcm;
2509 struct list_head *clients =
2510 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2512 list_for_each_entry(dpcm, clients, list_be) {
2514 struct snd_soc_pcm_runtime *be = dpcm->be;
2517 if (be->dai_link->ignore_suspend)
2520 for (i = 0; i < be->num_codecs; i++) {
2521 struct snd_soc_dai *dai = be->codec_dais[i];
2522 struct snd_soc_dai_driver *drv = dai->driver;
2524 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2525 be->dai_link->name);
2527 if (drv->ops && drv->ops->digital_mute &&
2528 dai->playback_active)
2529 drv->ops->digital_mute(dai, mute);
2536 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2538 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2539 struct snd_soc_dpcm *dpcm;
2540 struct snd_soc_dapm_widget_list *list;
2542 int stream = fe_substream->stream;
2544 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2545 fe->dpcm[stream].runtime = fe_substream->runtime;
2547 ret = dpcm_path_get(fe, stream, &list);
2549 mutex_unlock(&fe->card->mutex);
2551 } else if (ret == 0) {
2552 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2553 fe->dai_link->name, stream ? "capture" : "playback");
2556 /* calculate valid and active FE <-> BE dpcms */
2557 dpcm_process_paths(fe, stream, &list, 1);
2559 ret = dpcm_fe_dai_startup(fe_substream);
2561 /* clean up all links */
2562 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2563 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2565 dpcm_be_disconnect(fe, stream);
2566 fe->dpcm[stream].runtime = NULL;
2569 dpcm_clear_pending_state(fe, stream);
2570 dpcm_path_put(&list);
2571 mutex_unlock(&fe->card->mutex);
2575 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2577 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2578 struct snd_soc_dpcm *dpcm;
2579 int stream = fe_substream->stream, ret;
2581 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2582 ret = dpcm_fe_dai_shutdown(fe_substream);
2584 /* mark FE's links ready to prune */
2585 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2586 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2588 dpcm_be_disconnect(fe, stream);
2590 fe->dpcm[stream].runtime = NULL;
2591 mutex_unlock(&fe->card->mutex);
2595 /* create a new pcm */
2596 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2598 struct snd_soc_platform *platform = rtd->platform;
2599 struct snd_soc_dai *codec_dai;
2600 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2601 struct snd_pcm *pcm;
2603 int ret = 0, playback = 0, capture = 0;
2606 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2607 playback = rtd->dai_link->dpcm_playback;
2608 capture = rtd->dai_link->dpcm_capture;
2610 for (i = 0; i < rtd->num_codecs; i++) {
2611 codec_dai = rtd->codec_dais[i];
2612 if (codec_dai->driver->playback.channels_min)
2614 if (codec_dai->driver->capture.channels_min)
2618 capture = capture && cpu_dai->driver->capture.channels_min;
2619 playback = playback && cpu_dai->driver->playback.channels_min;
2622 if (rtd->dai_link->playback_only) {
2627 if (rtd->dai_link->capture_only) {
2632 /* create the PCM */
2633 if (rtd->dai_link->no_pcm) {
2634 snprintf(new_name, sizeof(new_name), "(%s)",
2635 rtd->dai_link->stream_name);
2637 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2638 playback, capture, &pcm);
2640 if (rtd->dai_link->dynamic)
2641 snprintf(new_name, sizeof(new_name), "%s (*)",
2642 rtd->dai_link->stream_name);
2644 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2645 rtd->dai_link->stream_name,
2646 (rtd->num_codecs > 1) ?
2647 "multicodec" : rtd->codec_dai->name, num);
2649 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2653 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2654 rtd->dai_link->name);
2657 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2659 /* DAPM dai link stream work */
2660 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2662 pcm->nonatomic = rtd->dai_link->nonatomic;
2664 pcm->private_data = rtd;
2666 if (rtd->dai_link->no_pcm) {
2668 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2670 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2674 /* ASoC PCM operations */
2675 if (rtd->dai_link->dynamic) {
2676 rtd->ops.open = dpcm_fe_dai_open;
2677 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2678 rtd->ops.prepare = dpcm_fe_dai_prepare;
2679 rtd->ops.trigger = dpcm_fe_dai_trigger;
2680 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2681 rtd->ops.close = dpcm_fe_dai_close;
2682 rtd->ops.pointer = soc_pcm_pointer;
2683 rtd->ops.ioctl = soc_pcm_ioctl;
2685 rtd->ops.open = soc_pcm_open;
2686 rtd->ops.hw_params = soc_pcm_hw_params;
2687 rtd->ops.prepare = soc_pcm_prepare;
2688 rtd->ops.trigger = soc_pcm_trigger;
2689 rtd->ops.hw_free = soc_pcm_hw_free;
2690 rtd->ops.close = soc_pcm_close;
2691 rtd->ops.pointer = soc_pcm_pointer;
2692 rtd->ops.ioctl = soc_pcm_ioctl;
2695 if (platform->driver->ops) {
2696 rtd->ops.ack = platform->driver->ops->ack;
2697 rtd->ops.copy = platform->driver->ops->copy;
2698 rtd->ops.silence = platform->driver->ops->silence;
2699 rtd->ops.page = platform->driver->ops->page;
2700 rtd->ops.mmap = platform->driver->ops->mmap;
2704 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2707 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2709 if (platform->driver->pcm_new) {
2710 ret = platform->driver->pcm_new(rtd);
2712 dev_err(platform->dev,
2713 "ASoC: pcm constructor failed: %d\n",
2719 pcm->private_free = platform->driver->pcm_free;
2721 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2722 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2727 /* is the current PCM operation for this FE ? */
2728 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2730 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2734 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2736 /* is the current PCM operation for this BE ? */
2737 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2738 struct snd_soc_pcm_runtime *be, int stream)
2740 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2741 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2742 be->dpcm[stream].runtime_update))
2746 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2748 /* get the substream for this BE */
2749 struct snd_pcm_substream *
2750 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2752 return be->pcm->streams[stream].substream;
2754 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2756 /* get the BE runtime state */
2757 enum snd_soc_dpcm_state
2758 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2760 return be->dpcm[stream].state;
2762 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2764 /* set the BE runtime state */
2765 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2766 int stream, enum snd_soc_dpcm_state state)
2768 be->dpcm[stream].state = state;
2770 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2773 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2774 * are not running, paused or suspended for the specified stream direction.
2776 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2777 struct snd_soc_pcm_runtime *be, int stream)
2779 struct snd_soc_dpcm *dpcm;
2782 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2787 state = dpcm->fe->dpcm[stream].state;
2788 if (state == SND_SOC_DPCM_STATE_START ||
2789 state == SND_SOC_DPCM_STATE_PAUSED ||
2790 state == SND_SOC_DPCM_STATE_SUSPEND)
2794 /* it's safe to free/stop this BE DAI */
2797 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2800 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2801 * running, paused or suspended for the specified stream direction.
2803 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2804 struct snd_soc_pcm_runtime *be, int stream)
2806 struct snd_soc_dpcm *dpcm;
2809 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2814 state = dpcm->fe->dpcm[stream].state;
2815 if (state == SND_SOC_DPCM_STATE_START ||
2816 state == SND_SOC_DPCM_STATE_PAUSED ||
2817 state == SND_SOC_DPCM_STATE_SUSPEND ||
2818 state == SND_SOC_DPCM_STATE_PREPARE)
2822 /* it's safe to change hw_params */
2825 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2827 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2828 int cmd, struct snd_soc_platform *platform)
2830 if (platform->driver->ops && platform->driver->ops->trigger)
2831 return platform->driver->ops->trigger(substream, cmd);
2834 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2836 #ifdef CONFIG_DEBUG_FS
2837 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2840 case SND_SOC_DPCM_STATE_NEW:
2842 case SND_SOC_DPCM_STATE_OPEN:
2844 case SND_SOC_DPCM_STATE_HW_PARAMS:
2846 case SND_SOC_DPCM_STATE_PREPARE:
2848 case SND_SOC_DPCM_STATE_START:
2850 case SND_SOC_DPCM_STATE_STOP:
2852 case SND_SOC_DPCM_STATE_SUSPEND:
2854 case SND_SOC_DPCM_STATE_PAUSED:
2856 case SND_SOC_DPCM_STATE_HW_FREE:
2858 case SND_SOC_DPCM_STATE_CLOSE:
2865 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2866 int stream, char *buf, size_t size)
2868 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2869 struct snd_soc_dpcm *dpcm;
2873 offset += scnprintf(buf + offset, size - offset,
2874 "[%s - %s]\n", fe->dai_link->name,
2875 stream ? "Capture" : "Playback");
2877 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
2878 dpcm_state_string(fe->dpcm[stream].state));
2880 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2881 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2882 offset += scnprintf(buf + offset, size - offset,
2884 "Format = %s, Channels = %d, Rate = %d\n",
2885 snd_pcm_format_name(params_format(params)),
2886 params_channels(params),
2887 params_rate(params));
2890 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
2892 if (list_empty(&fe->dpcm[stream].be_clients)) {
2893 offset += scnprintf(buf + offset, size - offset,
2894 " No active DSP links\n");
2898 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2899 struct snd_soc_pcm_runtime *be = dpcm->be;
2900 params = &dpcm->hw_params;
2902 offset += scnprintf(buf + offset, size - offset,
2903 "- %s\n", be->dai_link->name);
2905 offset += scnprintf(buf + offset, size - offset,
2907 dpcm_state_string(be->dpcm[stream].state));
2909 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2910 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2911 offset += scnprintf(buf + offset, size - offset,
2912 " Hardware Params: "
2913 "Format = %s, Channels = %d, Rate = %d\n",
2914 snd_pcm_format_name(params_format(params)),
2915 params_channels(params),
2916 params_rate(params));
2923 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2924 size_t count, loff_t *ppos)
2926 struct snd_soc_pcm_runtime *fe = file->private_data;
2927 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2930 buf = kmalloc(out_count, GFP_KERNEL);
2934 if (fe->cpu_dai->driver->playback.channels_min)
2935 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2936 buf + offset, out_count - offset);
2938 if (fe->cpu_dai->driver->capture.channels_min)
2939 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2940 buf + offset, out_count - offset);
2942 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2948 static const struct file_operations dpcm_state_fops = {
2949 .open = simple_open,
2950 .read = dpcm_state_read_file,
2951 .llseek = default_llseek,
2954 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2959 if (!rtd->card->debugfs_card_root)
2962 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2963 rtd->card->debugfs_card_root);
2964 if (!rtd->debugfs_dpcm_root) {
2966 "ASoC: Failed to create dpcm debugfs directory %s\n",
2967 rtd->dai_link->name);
2971 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2972 rtd->debugfs_dpcm_root,
2973 rtd, &dpcm_state_fops);