2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
44 struct snd_soc_pcm_stream *codec_stream;
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70 lockdep_assert_held(&rtd->pcm_mutex);
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
83 cpu_dai->component->active++;
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 lockdep_assert_held(&rtd->pcm_mutex);
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
118 cpu_dai->component->active--;
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
175 struct snd_soc_dpcm *dpcm;
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
179 struct snd_soc_pcm_runtime *be = dpcm->be;
181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182 be->dai_link->name, event, dir);
184 snd_soc_dapm_stream_event(be, dir, event);
187 snd_soc_dapm_stream_event(fe, dir, event);
192 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
193 struct snd_soc_dai *soc_dai)
195 struct snd_soc_pcm_runtime *rtd = substream->private_data;
198 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
199 rtd->dai_link->symmetric_rates)) {
200 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
203 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
204 SNDRV_PCM_HW_PARAM_RATE,
205 soc_dai->rate, soc_dai->rate);
207 dev_err(soc_dai->dev,
208 "ASoC: Unable to apply rate constraint: %d\n",
214 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
215 rtd->dai_link->symmetric_channels)) {
216 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
219 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
220 SNDRV_PCM_HW_PARAM_CHANNELS,
224 dev_err(soc_dai->dev,
225 "ASoC: Unable to apply channel symmetry constraint: %d\n",
231 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
232 rtd->dai_link->symmetric_samplebits)) {
233 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
234 soc_dai->sample_bits);
236 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
237 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
238 soc_dai->sample_bits,
239 soc_dai->sample_bits);
241 dev_err(soc_dai->dev,
242 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
251 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
256 unsigned int rate, channels, sample_bits, symmetry, i;
258 rate = params_rate(params);
259 channels = params_channels(params);
260 sample_bits = snd_pcm_format_physical_width(params_format(params));
262 /* reject unmatched parameters when applying symmetry */
263 symmetry = cpu_dai->driver->symmetric_rates ||
264 rtd->dai_link->symmetric_rates;
266 for (i = 0; i < rtd->num_codecs; i++)
267 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
269 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
270 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
271 cpu_dai->rate, rate);
275 symmetry = cpu_dai->driver->symmetric_channels ||
276 rtd->dai_link->symmetric_channels;
278 for (i = 0; i < rtd->num_codecs; i++)
279 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
281 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
282 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
283 cpu_dai->channels, channels);
287 symmetry = cpu_dai->driver->symmetric_samplebits ||
288 rtd->dai_link->symmetric_samplebits;
290 for (i = 0; i < rtd->num_codecs; i++)
291 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
293 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
294 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
295 cpu_dai->sample_bits, sample_bits);
302 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
306 struct snd_soc_dai_link *link = rtd->dai_link;
307 unsigned int symmetry, i;
309 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
310 cpu_driver->symmetric_channels || link->symmetric_channels ||
311 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
313 for (i = 0; i < rtd->num_codecs; i++)
314 symmetry = symmetry ||
315 rtd->codec_dais[i]->driver->symmetric_rates ||
316 rtd->codec_dais[i]->driver->symmetric_channels ||
317 rtd->codec_dais[i]->driver->symmetric_samplebits;
322 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
330 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
332 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
336 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
338 struct snd_soc_pcm_runtime *rtd = substream->private_data;
339 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
340 struct snd_soc_dai *codec_dai;
342 unsigned int bits = 0, cpu_bits;
344 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
345 for (i = 0; i < rtd->num_codecs; i++) {
346 codec_dai = rtd->codec_dais[i];
347 if (codec_dai->driver->playback.sig_bits == 0) {
351 bits = max(codec_dai->driver->playback.sig_bits, bits);
353 cpu_bits = cpu_dai->driver->playback.sig_bits;
355 for (i = 0; i < rtd->num_codecs; i++) {
356 codec_dai = rtd->codec_dais[i];
357 if (codec_dai->driver->capture.sig_bits == 0) {
361 bits = max(codec_dai->driver->capture.sig_bits, bits);
363 cpu_bits = cpu_dai->driver->capture.sig_bits;
366 soc_pcm_set_msb(substream, bits);
367 soc_pcm_set_msb(substream, cpu_bits);
370 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
372 struct snd_pcm_runtime *runtime = substream->runtime;
373 struct snd_pcm_hardware *hw = &runtime->hw;
374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
376 struct snd_soc_dai_driver *codec_dai_drv;
377 struct snd_soc_pcm_stream *codec_stream;
378 struct snd_soc_pcm_stream *cpu_stream;
379 unsigned int chan_min = 0, chan_max = UINT_MAX;
380 unsigned int rate_min = 0, rate_max = UINT_MAX;
381 unsigned int rates = UINT_MAX;
382 u64 formats = ULLONG_MAX;
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 cpu_stream = &cpu_dai_drv->playback;
388 cpu_stream = &cpu_dai_drv->capture;
390 /* first calculate min/max only for CODECs in the DAI link */
391 for (i = 0; i < rtd->num_codecs; i++) {
394 * Skip CODECs which don't support the current stream type.
395 * Otherwise, since the rate, channel, and format values will
396 * zero in that case, we would have no usable settings left,
397 * causing the resulting setup to fail.
398 * At least one CODEC should match, otherwise we should have
399 * bailed out on a higher level, since there would be no
400 * CODEC to support the transfer direction in that case.
402 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
406 codec_dai_drv = rtd->codec_dais[i]->driver;
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 codec_stream = &codec_dai_drv->playback;
410 codec_stream = &codec_dai_drv->capture;
411 chan_min = max(chan_min, codec_stream->channels_min);
412 chan_max = min(chan_max, codec_stream->channels_max);
413 rate_min = max(rate_min, codec_stream->rate_min);
414 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
415 formats &= codec_stream->formats;
416 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
420 * chan min/max cannot be enforced if there are multiple CODEC DAIs
421 * connected to a single CPU DAI, use CPU DAI's directly and let
422 * channel allocation be fixed up later
424 if (rtd->num_codecs > 1) {
425 chan_min = cpu_stream->channels_min;
426 chan_max = cpu_stream->channels_max;
429 hw->channels_min = max(chan_min, cpu_stream->channels_min);
430 hw->channels_max = min(chan_max, cpu_stream->channels_max);
432 hw->formats &= formats & cpu_stream->formats;
434 hw->formats = formats & cpu_stream->formats;
435 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
437 snd_pcm_limit_hw_rates(runtime);
439 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
440 hw->rate_min = max(hw->rate_min, rate_min);
441 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
442 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
446 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
447 * then initialized and any private data can be allocated. This also calls
448 * startup for the cpu DAI, platform, machine and codec DAI.
450 static int soc_pcm_open(struct snd_pcm_substream *substream)
452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
453 struct snd_pcm_runtime *runtime = substream->runtime;
454 struct snd_soc_platform *platform = rtd->platform;
455 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
456 struct snd_soc_dai *codec_dai;
457 const char *codec_dai_name = "multicodec";
460 pinctrl_pm_select_default_state(cpu_dai->dev);
461 for (i = 0; i < rtd->num_codecs; i++)
462 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
463 pm_runtime_get_sync(cpu_dai->dev);
464 for (i = 0; i < rtd->num_codecs; i++)
465 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
466 pm_runtime_get_sync(platform->dev);
468 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
470 /* startup the audio subsystem */
471 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
472 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
474 dev_err(cpu_dai->dev, "ASoC: can't open interface"
475 " %s: %d\n", cpu_dai->name, ret);
480 if (platform->driver->ops && platform->driver->ops->open) {
481 ret = platform->driver->ops->open(substream);
483 dev_err(platform->dev, "ASoC: can't open platform"
484 " %s: %d\n", platform->component.name, ret);
489 for (i = 0; i < rtd->num_codecs; i++) {
490 codec_dai = rtd->codec_dais[i];
491 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
492 ret = codec_dai->driver->ops->startup(substream,
495 dev_err(codec_dai->dev,
496 "ASoC: can't open codec %s: %d\n",
497 codec_dai->name, ret);
502 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
503 codec_dai->tx_mask = 0;
505 codec_dai->rx_mask = 0;
508 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
509 ret = rtd->dai_link->ops->startup(substream);
511 pr_err("ASoC: %s startup failed: %d\n",
512 rtd->dai_link->name, ret);
517 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
518 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
521 /* Check that the codec and cpu DAIs are compatible */
522 soc_pcm_init_runtime_hw(substream);
524 if (rtd->num_codecs == 1)
525 codec_dai_name = rtd->codec_dai->name;
527 if (soc_pcm_has_symmetry(substream))
528 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
531 if (!runtime->hw.rates) {
532 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
533 codec_dai_name, cpu_dai->name);
536 if (!runtime->hw.formats) {
537 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
538 codec_dai_name, cpu_dai->name);
541 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
542 runtime->hw.channels_min > runtime->hw.channels_max) {
543 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
544 codec_dai_name, cpu_dai->name);
548 soc_pcm_apply_msb(substream);
550 /* Symmetry only applies if we've already got an active stream. */
551 if (cpu_dai->active) {
552 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
557 for (i = 0; i < rtd->num_codecs; i++) {
558 if (rtd->codec_dais[i]->active) {
559 ret = soc_pcm_apply_symmetry(substream,
566 pr_debug("ASoC: %s <-> %s info:\n",
567 codec_dai_name, cpu_dai->name);
568 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
569 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
570 runtime->hw.channels_max);
571 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
572 runtime->hw.rate_max);
576 snd_soc_runtime_activate(rtd, substream->stream);
578 mutex_unlock(&rtd->pcm_mutex);
582 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
583 rtd->dai_link->ops->shutdown(substream);
590 codec_dai = rtd->codec_dais[i];
591 if (codec_dai->driver->ops->shutdown)
592 codec_dai->driver->ops->shutdown(substream, codec_dai);
595 if (platform->driver->ops && platform->driver->ops->close)
596 platform->driver->ops->close(substream);
599 if (cpu_dai->driver->ops->shutdown)
600 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
602 mutex_unlock(&rtd->pcm_mutex);
604 pm_runtime_put(platform->dev);
605 for (i = 0; i < rtd->num_codecs; i++)
606 pm_runtime_put(rtd->codec_dais[i]->dev);
607 pm_runtime_put(cpu_dai->dev);
608 for (i = 0; i < rtd->num_codecs; i++) {
609 if (!rtd->codec_dais[i]->active)
610 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
612 if (!cpu_dai->active)
613 pinctrl_pm_select_sleep_state(cpu_dai->dev);
619 * Power down the audio subsystem pmdown_time msecs after close is called.
620 * This is to ensure there are no pops or clicks in between any music tracks
621 * due to DAPM power cycling.
623 static void close_delayed_work(struct work_struct *work)
625 struct snd_soc_pcm_runtime *rtd =
626 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
627 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
629 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
631 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
632 codec_dai->driver->playback.stream_name,
633 codec_dai->playback_active ? "active" : "inactive",
634 rtd->pop_wait ? "yes" : "no");
636 /* are we waiting on this codec DAI stream */
637 if (rtd->pop_wait == 1) {
639 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
640 SND_SOC_DAPM_STREAM_STOP);
643 mutex_unlock(&rtd->pcm_mutex);
647 * Called by ALSA when a PCM substream is closed. Private data can be
648 * freed here. The cpu DAI, codec DAI, machine and platform are also
651 static int soc_pcm_close(struct snd_pcm_substream *substream)
653 struct snd_soc_pcm_runtime *rtd = substream->private_data;
654 struct snd_soc_platform *platform = rtd->platform;
655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
656 struct snd_soc_dai *codec_dai;
659 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
661 snd_soc_runtime_deactivate(rtd, substream->stream);
663 /* clear the corresponding DAIs rate when inactive */
664 if (!cpu_dai->active)
667 for (i = 0; i < rtd->num_codecs; i++) {
668 codec_dai = rtd->codec_dais[i];
669 if (!codec_dai->active)
673 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
675 if (cpu_dai->driver->ops->shutdown)
676 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
678 for (i = 0; i < rtd->num_codecs; i++) {
679 codec_dai = rtd->codec_dais[i];
680 if (codec_dai->driver->ops->shutdown)
681 codec_dai->driver->ops->shutdown(substream, codec_dai);
684 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
685 rtd->dai_link->ops->shutdown(substream);
687 if (platform->driver->ops && platform->driver->ops->close)
688 platform->driver->ops->close(substream);
690 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
691 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
692 /* powered down playback stream now */
693 snd_soc_dapm_stream_event(rtd,
694 SNDRV_PCM_STREAM_PLAYBACK,
695 SND_SOC_DAPM_STREAM_STOP);
697 /* start delayed pop wq here for playback streams */
699 queue_delayed_work(system_power_efficient_wq,
701 msecs_to_jiffies(rtd->pmdown_time));
704 /* capture streams can be powered down now */
705 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
706 SND_SOC_DAPM_STREAM_STOP);
709 mutex_unlock(&rtd->pcm_mutex);
711 pm_runtime_put(platform->dev);
712 for (i = 0; i < rtd->num_codecs; i++)
713 pm_runtime_put(rtd->codec_dais[i]->dev);
714 pm_runtime_put(cpu_dai->dev);
715 for (i = 0; i < rtd->num_codecs; i++) {
716 if (!rtd->codec_dais[i]->active)
717 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
719 if (!cpu_dai->active)
720 pinctrl_pm_select_sleep_state(cpu_dai->dev);
726 * Called by ALSA when the PCM substream is prepared, can set format, sample
727 * rate, etc. This function is non atomic and can be called multiple times,
728 * it can refer to the runtime info.
730 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
732 struct snd_soc_pcm_runtime *rtd = substream->private_data;
733 struct snd_soc_platform *platform = rtd->platform;
734 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
735 struct snd_soc_dai *codec_dai;
738 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
740 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
741 ret = rtd->dai_link->ops->prepare(substream);
743 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
749 if (platform->driver->ops && platform->driver->ops->prepare) {
750 ret = platform->driver->ops->prepare(substream);
752 dev_err(platform->dev, "ASoC: platform prepare error:"
758 for (i = 0; i < rtd->num_codecs; i++) {
759 codec_dai = rtd->codec_dais[i];
760 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
761 ret = codec_dai->driver->ops->prepare(substream,
764 dev_err(codec_dai->dev,
765 "ASoC: codec DAI prepare error: %d\n",
772 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
773 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
775 dev_err(cpu_dai->dev,
776 "ASoC: cpu DAI prepare error: %d\n", ret);
781 /* cancel any delayed stream shutdown that is pending */
782 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
785 cancel_delayed_work(&rtd->delayed_work);
788 snd_soc_dapm_stream_event(rtd, substream->stream,
789 SND_SOC_DAPM_STREAM_START);
791 for (i = 0; i < rtd->num_codecs; i++)
792 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
794 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
797 mutex_unlock(&rtd->pcm_mutex);
801 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
804 struct snd_interval *interval;
805 int channels = hweight_long(mask);
807 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
808 interval->min = channels;
809 interval->max = channels;
812 int soc_dai_hw_params(struct snd_pcm_substream *substream,
813 struct snd_pcm_hw_params *params,
814 struct snd_soc_dai *dai)
818 if (dai->driver->ops && dai->driver->ops->hw_params) {
819 ret = dai->driver->ops->hw_params(substream, params, dai);
821 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
831 * Called by ALSA when the hardware params are set by application. This
832 * function can also be called multiple times and can allocate buffers
833 * (using snd_pcm_lib_* ). It's non-atomic.
835 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
836 struct snd_pcm_hw_params *params)
838 struct snd_soc_pcm_runtime *rtd = substream->private_data;
839 struct snd_soc_platform *platform = rtd->platform;
840 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
843 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
845 ret = soc_pcm_params_symmetry(substream, params);
849 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
850 ret = rtd->dai_link->ops->hw_params(substream, params);
852 dev_err(rtd->card->dev, "ASoC: machine hw_params"
853 " failed: %d\n", ret);
858 for (i = 0; i < rtd->num_codecs; i++) {
859 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
860 struct snd_pcm_hw_params codec_params;
863 * Skip CODECs which don't support the current stream type,
864 * the idea being that if a CODEC is not used for the currently
865 * set up transfer direction, it should not need to be
866 * configured, especially since the configuration used might
867 * not even be supported by that CODEC. There may be cases
868 * however where a CODEC needs to be set up although it is
869 * actually not being used for the transfer, e.g. if a
870 * capture-only CODEC is acting as an LRCLK and/or BCLK master
871 * for the DAI link including a playback-only CODEC.
872 * If this becomes necessary, we will have to augment the
873 * machine driver setup with information on how to act, so
874 * we can do the right thing here.
876 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
879 /* copy params for each codec */
880 codec_params = *params;
882 /* fixup params based on TDM slot masks */
883 if (codec_dai->tx_mask)
884 soc_pcm_codec_params_fixup(&codec_params,
886 if (codec_dai->rx_mask)
887 soc_pcm_codec_params_fixup(&codec_params,
890 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
894 codec_dai->rate = params_rate(&codec_params);
895 codec_dai->channels = params_channels(&codec_params);
896 codec_dai->sample_bits = snd_pcm_format_physical_width(
897 params_format(&codec_params));
900 ret = soc_dai_hw_params(substream, params, cpu_dai);
904 if (platform->driver->ops && platform->driver->ops->hw_params) {
905 ret = platform->driver->ops->hw_params(substream, params);
907 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
908 platform->component.name, ret);
913 /* store the parameters for each DAIs */
914 cpu_dai->rate = params_rate(params);
915 cpu_dai->channels = params_channels(params);
916 cpu_dai->sample_bits =
917 snd_pcm_format_physical_width(params_format(params));
920 mutex_unlock(&rtd->pcm_mutex);
924 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
925 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
932 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
933 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
934 codec_dai->driver->ops->hw_free(substream, codec_dai);
938 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
939 rtd->dai_link->ops->hw_free(substream);
941 mutex_unlock(&rtd->pcm_mutex);
946 * Frees resources allocated by hw_params, can be called multiple times
948 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
950 struct snd_soc_pcm_runtime *rtd = substream->private_data;
951 struct snd_soc_platform *platform = rtd->platform;
952 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
953 struct snd_soc_dai *codec_dai;
954 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
957 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
959 /* clear the corresponding DAIs parameters when going to be inactive */
960 if (cpu_dai->active == 1) {
962 cpu_dai->channels = 0;
963 cpu_dai->sample_bits = 0;
966 for (i = 0; i < rtd->num_codecs; i++) {
967 codec_dai = rtd->codec_dais[i];
968 if (codec_dai->active == 1) {
970 codec_dai->channels = 0;
971 codec_dai->sample_bits = 0;
975 /* apply codec digital mute */
976 for (i = 0; i < rtd->num_codecs; i++) {
977 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
978 (!playback && rtd->codec_dais[i]->capture_active == 1))
979 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
983 /* free any machine hw params */
984 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
985 rtd->dai_link->ops->hw_free(substream);
987 /* free any DMA resources */
988 if (platform->driver->ops && platform->driver->ops->hw_free)
989 platform->driver->ops->hw_free(substream);
991 /* now free hw params for the DAIs */
992 for (i = 0; i < rtd->num_codecs; i++) {
993 codec_dai = rtd->codec_dais[i];
994 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
995 codec_dai->driver->ops->hw_free(substream, codec_dai);
998 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
999 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1001 mutex_unlock(&rtd->pcm_mutex);
1005 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1007 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1008 struct snd_soc_platform *platform = rtd->platform;
1009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1010 struct snd_soc_dai *codec_dai;
1013 for (i = 0; i < rtd->num_codecs; i++) {
1014 codec_dai = rtd->codec_dais[i];
1015 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1016 ret = codec_dai->driver->ops->trigger(substream,
1023 if (platform->driver->ops && platform->driver->ops->trigger) {
1024 ret = platform->driver->ops->trigger(substream, cmd);
1029 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1030 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1035 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1036 ret = rtd->dai_link->ops->trigger(substream, cmd);
1044 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1047 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1048 struct snd_soc_platform *platform = rtd->platform;
1049 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1050 struct snd_soc_dai *codec_dai;
1053 for (i = 0; i < rtd->num_codecs; i++) {
1054 codec_dai = rtd->codec_dais[i];
1055 if (codec_dai->driver->ops &&
1056 codec_dai->driver->ops->bespoke_trigger) {
1057 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1064 if (platform->driver->bespoke_trigger) {
1065 ret = platform->driver->bespoke_trigger(substream, cmd);
1070 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1071 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1078 * soc level wrapper for pointer callback
1079 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1080 * the runtime->delay will be updated accordingly.
1082 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1084 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1085 struct snd_soc_platform *platform = rtd->platform;
1086 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1087 struct snd_soc_dai *codec_dai;
1088 struct snd_pcm_runtime *runtime = substream->runtime;
1089 snd_pcm_uframes_t offset = 0;
1090 snd_pcm_sframes_t delay = 0;
1091 snd_pcm_sframes_t codec_delay = 0;
1094 if (platform->driver->ops && platform->driver->ops->pointer)
1095 offset = platform->driver->ops->pointer(substream);
1097 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1098 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1100 for (i = 0; i < rtd->num_codecs; i++) {
1101 codec_dai = rtd->codec_dais[i];
1102 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1103 codec_delay = max(codec_delay,
1104 codec_dai->driver->ops->delay(substream,
1107 delay += codec_delay;
1110 * None of the existing platform drivers implement delay(), so
1111 * for now the codec_dai of first multicodec entry is used
1113 if (platform->driver->delay)
1114 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1116 runtime->delay = delay;
1121 /* connect a FE and BE */
1122 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1123 struct snd_soc_pcm_runtime *be, int stream)
1125 struct snd_soc_dpcm *dpcm;
1127 /* only add new dpcms */
1128 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1129 if (dpcm->be == be && dpcm->fe == fe)
1133 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1139 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1140 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1141 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1142 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1144 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1145 stream ? "capture" : "playback", fe->dai_link->name,
1146 stream ? "<-" : "->", be->dai_link->name);
1148 #ifdef CONFIG_DEBUG_FS
1149 if (fe->debugfs_dpcm_root)
1150 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1151 fe->debugfs_dpcm_root, &dpcm->state);
1156 /* reparent a BE onto another FE */
1157 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1158 struct snd_soc_pcm_runtime *be, int stream)
1160 struct snd_soc_dpcm *dpcm;
1161 struct snd_pcm_substream *fe_substream, *be_substream;
1163 /* reparent if BE is connected to other FEs */
1164 if (!be->dpcm[stream].users)
1167 be_substream = snd_soc_dpcm_get_substream(be, stream);
1169 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1173 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1174 stream ? "capture" : "playback",
1175 dpcm->fe->dai_link->name,
1176 stream ? "<-" : "->", dpcm->be->dai_link->name);
1178 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1179 be_substream->runtime = fe_substream->runtime;
1184 /* disconnect a BE and FE */
1185 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1187 struct snd_soc_dpcm *dpcm, *d;
1189 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1190 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1191 stream ? "capture" : "playback",
1192 dpcm->be->dai_link->name);
1194 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1197 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1198 stream ? "capture" : "playback", fe->dai_link->name,
1199 stream ? "<-" : "->", dpcm->be->dai_link->name);
1201 /* BEs still alive need new FE */
1202 dpcm_be_reparent(fe, dpcm->be, stream);
1204 #ifdef CONFIG_DEBUG_FS
1205 debugfs_remove(dpcm->debugfs_state);
1207 list_del(&dpcm->list_be);
1208 list_del(&dpcm->list_fe);
1213 /* get BE for DAI widget and stream */
1214 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1215 struct snd_soc_dapm_widget *widget, int stream)
1217 struct snd_soc_pcm_runtime *be;
1220 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1221 for (i = 0; i < card->num_links; i++) {
1224 if (!be->dai_link->no_pcm)
1227 if (be->cpu_dai->playback_widget == widget)
1230 for (j = 0; j < be->num_codecs; j++) {
1231 struct snd_soc_dai *dai = be->codec_dais[j];
1232 if (dai->playback_widget == widget)
1238 for (i = 0; i < card->num_links; i++) {
1241 if (!be->dai_link->no_pcm)
1244 if (be->cpu_dai->capture_widget == widget)
1247 for (j = 0; j < be->num_codecs; j++) {
1248 struct snd_soc_dai *dai = be->codec_dais[j];
1249 if (dai->capture_widget == widget)
1255 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1256 stream ? "capture" : "playback", widget->name);
1260 static inline struct snd_soc_dapm_widget *
1261 dai_get_widget(struct snd_soc_dai *dai, int stream)
1263 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1264 return dai->playback_widget;
1266 return dai->capture_widget;
1269 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1270 struct snd_soc_dapm_widget *widget)
1274 for (i = 0; i < list->num_widgets; i++) {
1275 if (widget == list->widgets[i])
1282 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1283 int stream, struct snd_soc_dapm_widget_list **list)
1285 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1288 /* get number of valid DAI paths and their widgets */
1289 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
1291 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1292 stream ? "capture" : "playback");
1297 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1298 struct snd_soc_dapm_widget_list **list_)
1300 struct snd_soc_dpcm *dpcm;
1301 struct snd_soc_dapm_widget_list *list = *list_;
1302 struct snd_soc_dapm_widget *widget;
1305 /* Destroy any old FE <--> BE connections */
1306 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1309 /* is there a valid CPU DAI widget for this BE */
1310 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1312 /* prune the BE if it's no longer in our active list */
1313 if (widget && widget_in_list(list, widget))
1316 /* is there a valid CODEC DAI widget for this BE */
1317 for (i = 0; i < dpcm->be->num_codecs; i++) {
1318 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1319 widget = dai_get_widget(dai, stream);
1321 /* prune the BE if it's no longer in our active list */
1322 if (widget && widget_in_list(list, widget))
1326 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1327 stream ? "capture" : "playback",
1328 dpcm->be->dai_link->name, fe->dai_link->name);
1329 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1330 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1334 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1338 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1339 struct snd_soc_dapm_widget_list **list_)
1341 struct snd_soc_card *card = fe->card;
1342 struct snd_soc_dapm_widget_list *list = *list_;
1343 struct snd_soc_pcm_runtime *be;
1344 int i, new = 0, err;
1346 /* Create any new FE <--> BE connections */
1347 for (i = 0; i < list->num_widgets; i++) {
1349 switch (list->widgets[i]->id) {
1350 case snd_soc_dapm_dai_in:
1351 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1354 case snd_soc_dapm_dai_out:
1355 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1362 /* is there a valid BE rtd for this widget */
1363 be = dpcm_get_be(card, list->widgets[i], stream);
1365 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1366 list->widgets[i]->name);
1370 /* make sure BE is a real BE */
1371 if (!be->dai_link->no_pcm)
1374 /* don't connect if FE is not running */
1375 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1378 /* newly connected FE and BE */
1379 err = dpcm_be_connect(fe, be, stream);
1381 dev_err(fe->dev, "ASoC: can't connect %s\n",
1382 list->widgets[i]->name);
1384 } else if (err == 0) /* already connected */
1388 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1392 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1397 * Find the corresponding BE DAIs that source or sink audio to this
1400 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1401 int stream, struct snd_soc_dapm_widget_list **list, int new)
1404 return dpcm_add_paths(fe, stream, list);
1406 return dpcm_prune_paths(fe, stream, list);
1409 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1411 struct snd_soc_dpcm *dpcm;
1413 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1414 dpcm->be->dpcm[stream].runtime_update =
1415 SND_SOC_DPCM_UPDATE_NO;
1418 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1421 struct snd_soc_dpcm *dpcm;
1423 /* disable any enabled and non active backends */
1424 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1426 struct snd_soc_pcm_runtime *be = dpcm->be;
1427 struct snd_pcm_substream *be_substream =
1428 snd_soc_dpcm_get_substream(be, stream);
1430 if (be->dpcm[stream].users == 0)
1431 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1432 stream ? "capture" : "playback",
1433 be->dpcm[stream].state);
1435 if (--be->dpcm[stream].users != 0)
1438 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1441 soc_pcm_close(be_substream);
1442 be_substream->runtime = NULL;
1443 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1447 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1449 struct snd_soc_dpcm *dpcm;
1452 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1453 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1455 struct snd_soc_pcm_runtime *be = dpcm->be;
1456 struct snd_pcm_substream *be_substream =
1457 snd_soc_dpcm_get_substream(be, stream);
1459 if (!be_substream) {
1460 dev_err(be->dev, "ASoC: no backend %s stream\n",
1461 stream ? "capture" : "playback");
1465 /* is this op for this BE ? */
1466 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1469 /* first time the dpcm is open ? */
1470 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1471 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1472 stream ? "capture" : "playback",
1473 be->dpcm[stream].state);
1475 if (be->dpcm[stream].users++ != 0)
1478 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1479 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1482 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1483 stream ? "capture" : "playback", be->dai_link->name);
1485 be_substream->runtime = be->dpcm[stream].runtime;
1486 err = soc_pcm_open(be_substream);
1488 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1489 be->dpcm[stream].users--;
1490 if (be->dpcm[stream].users < 0)
1491 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1492 stream ? "capture" : "playback",
1493 be->dpcm[stream].state);
1495 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1499 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1506 /* disable any enabled and non active backends */
1507 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1508 struct snd_soc_pcm_runtime *be = dpcm->be;
1509 struct snd_pcm_substream *be_substream =
1510 snd_soc_dpcm_get_substream(be, stream);
1512 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1515 if (be->dpcm[stream].users == 0)
1516 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1517 stream ? "capture" : "playback",
1518 be->dpcm[stream].state);
1520 if (--be->dpcm[stream].users != 0)
1523 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1526 soc_pcm_close(be_substream);
1527 be_substream->runtime = NULL;
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1534 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1535 struct snd_soc_pcm_stream *stream,
1538 runtime->hw.rate_min = stream->rate_min;
1539 runtime->hw.rate_max = stream->rate_max;
1540 runtime->hw.channels_min = stream->channels_min;
1541 runtime->hw.channels_max = stream->channels_max;
1542 if (runtime->hw.formats)
1543 runtime->hw.formats &= formats & stream->formats;
1545 runtime->hw.formats = formats & stream->formats;
1546 runtime->hw.rates = stream->rates;
1549 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1551 struct snd_soc_pcm_runtime *fe = substream->private_data;
1552 struct snd_soc_dpcm *dpcm;
1553 u64 formats = ULLONG_MAX;
1554 int stream = substream->stream;
1556 if (!fe->dai_link->dpcm_merged_format)
1560 * It returns merged BE codec format
1561 * if FE want to use it (= dpcm_merged_format)
1564 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1565 struct snd_soc_pcm_runtime *be = dpcm->be;
1566 struct snd_soc_dai_driver *codec_dai_drv;
1567 struct snd_soc_pcm_stream *codec_stream;
1570 for (i = 0; i < be->num_codecs; i++) {
1571 codec_dai_drv = be->codec_dais[i]->driver;
1572 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1573 codec_stream = &codec_dai_drv->playback;
1575 codec_stream = &codec_dai_drv->capture;
1577 formats &= codec_stream->formats;
1584 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1586 struct snd_pcm_runtime *runtime = substream->runtime;
1587 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1588 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1589 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1590 u64 format = dpcm_runtime_base_format(substream);
1592 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1593 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1595 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1598 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1600 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1601 * for avoiding the race with trigger callback.
1602 * If the state is unset and a trigger is pending while the previous operation,
1603 * process the pending trigger action here.
1605 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1606 int stream, enum snd_soc_dpcm_update state)
1608 struct snd_pcm_substream *substream =
1609 snd_soc_dpcm_get_substream(fe, stream);
1611 snd_pcm_stream_lock_irq(substream);
1612 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1613 dpcm_fe_dai_do_trigger(substream,
1614 fe->dpcm[stream].trigger_pending - 1);
1615 fe->dpcm[stream].trigger_pending = 0;
1617 fe->dpcm[stream].runtime_update = state;
1618 snd_pcm_stream_unlock_irq(substream);
1621 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1623 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1624 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1625 int stream = fe_substream->stream, ret = 0;
1627 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1629 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1631 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1635 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1637 /* start the DAI frontend */
1638 ret = soc_pcm_open(fe_substream);
1640 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1644 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1646 dpcm_set_fe_runtime(fe_substream);
1647 snd_pcm_limit_hw_rates(runtime);
1649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1653 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1655 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1659 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1661 struct snd_soc_dpcm *dpcm;
1663 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1664 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1666 struct snd_soc_pcm_runtime *be = dpcm->be;
1667 struct snd_pcm_substream *be_substream =
1668 snd_soc_dpcm_get_substream(be, stream);
1670 /* is this op for this BE ? */
1671 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1674 if (be->dpcm[stream].users == 0)
1675 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1676 stream ? "capture" : "playback",
1677 be->dpcm[stream].state);
1679 if (--be->dpcm[stream].users != 0)
1682 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1683 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1686 dev_dbg(be->dev, "ASoC: close BE %s\n",
1687 dpcm->fe->dai_link->name);
1689 soc_pcm_close(be_substream);
1690 be_substream->runtime = NULL;
1692 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1697 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1699 struct snd_soc_pcm_runtime *fe = substream->private_data;
1700 int stream = substream->stream;
1702 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1704 /* shutdown the BEs */
1705 dpcm_be_dai_shutdown(fe, substream->stream);
1707 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1709 /* now shutdown the frontend */
1710 soc_pcm_close(substream);
1712 /* run the stream event for each BE */
1713 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1715 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1716 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1720 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1722 struct snd_soc_dpcm *dpcm;
1724 /* only hw_params backends that are either sinks or sources
1725 * to this frontend DAI */
1726 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1728 struct snd_soc_pcm_runtime *be = dpcm->be;
1729 struct snd_pcm_substream *be_substream =
1730 snd_soc_dpcm_get_substream(be, stream);
1732 /* is this op for this BE ? */
1733 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1736 /* only free hw when no longer used - check all FEs */
1737 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1740 /* do not free hw if this BE is used by other FE */
1741 if (be->dpcm[stream].users > 1)
1744 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1745 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1746 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1747 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1748 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1751 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1752 dpcm->fe->dai_link->name);
1754 soc_pcm_hw_free(be_substream);
1756 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1762 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1764 struct snd_soc_pcm_runtime *fe = substream->private_data;
1765 int err, stream = substream->stream;
1767 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1768 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1770 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1772 /* call hw_free on the frontend */
1773 err = soc_pcm_hw_free(substream);
1775 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1776 fe->dai_link->name);
1778 /* only hw_params backends that are either sinks or sources
1779 * to this frontend DAI */
1780 err = dpcm_be_dai_hw_free(fe, stream);
1782 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1783 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1785 mutex_unlock(&fe->card->mutex);
1789 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1791 struct snd_soc_dpcm *dpcm;
1794 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1796 struct snd_soc_pcm_runtime *be = dpcm->be;
1797 struct snd_pcm_substream *be_substream =
1798 snd_soc_dpcm_get_substream(be, stream);
1800 /* is this op for this BE ? */
1801 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1804 /* only allow hw_params() if no connected FEs are running */
1805 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1808 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1809 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1810 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1813 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1814 dpcm->fe->dai_link->name);
1816 /* copy params for each dpcm */
1817 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1818 sizeof(struct snd_pcm_hw_params));
1820 /* perform any hw_params fixups */
1821 if (be->dai_link->be_hw_params_fixup) {
1822 ret = be->dai_link->be_hw_params_fixup(be,
1826 "ASoC: hw_params BE fixup failed %d\n",
1832 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1834 dev_err(dpcm->be->dev,
1835 "ASoC: hw_params BE failed %d\n", ret);
1839 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1844 /* disable any enabled and non active backends */
1845 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1846 struct snd_soc_pcm_runtime *be = dpcm->be;
1847 struct snd_pcm_substream *be_substream =
1848 snd_soc_dpcm_get_substream(be, stream);
1850 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1853 /* only allow hw_free() if no connected FEs are running */
1854 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1857 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1858 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1859 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1860 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1863 soc_pcm_hw_free(be_substream);
1869 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1870 struct snd_pcm_hw_params *params)
1872 struct snd_soc_pcm_runtime *fe = substream->private_data;
1873 int ret, stream = substream->stream;
1875 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1876 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1878 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1879 sizeof(struct snd_pcm_hw_params));
1880 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1882 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1886 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1887 fe->dai_link->name, params_rate(params),
1888 params_channels(params), params_format(params));
1890 /* call hw_params on the frontend */
1891 ret = soc_pcm_hw_params(substream, params);
1893 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1894 dpcm_be_dai_hw_free(fe, stream);
1896 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1899 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1900 mutex_unlock(&fe->card->mutex);
1904 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1905 struct snd_pcm_substream *substream, int cmd)
1909 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1910 dpcm->fe->dai_link->name, cmd);
1912 ret = soc_pcm_trigger(substream, cmd);
1914 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1919 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1922 struct snd_soc_dpcm *dpcm;
1925 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1927 struct snd_soc_pcm_runtime *be = dpcm->be;
1928 struct snd_pcm_substream *be_substream =
1929 snd_soc_dpcm_get_substream(be, stream);
1931 /* is this op for this BE ? */
1932 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1936 case SNDRV_PCM_TRIGGER_START:
1937 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1938 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1941 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1945 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1947 case SNDRV_PCM_TRIGGER_RESUME:
1948 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1951 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1955 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1957 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1958 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1961 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1965 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1967 case SNDRV_PCM_TRIGGER_STOP:
1968 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1971 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1974 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1978 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1980 case SNDRV_PCM_TRIGGER_SUSPEND:
1981 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1984 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1987 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1991 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1993 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1994 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1997 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2000 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2004 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2011 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2013 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2015 struct snd_soc_pcm_runtime *fe = substream->private_data;
2016 int stream = substream->stream, ret;
2017 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2019 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2022 case SND_SOC_DPCM_TRIGGER_PRE:
2023 /* call trigger on the frontend before the backend. */
2025 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2026 fe->dai_link->name, cmd);
2028 ret = soc_pcm_trigger(substream, cmd);
2030 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2034 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2036 case SND_SOC_DPCM_TRIGGER_POST:
2037 /* call trigger on the frontend after the backend. */
2039 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2041 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2045 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2046 fe->dai_link->name, cmd);
2048 ret = soc_pcm_trigger(substream, cmd);
2050 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2051 /* bespoke trigger() - handles both FE and BEs */
2053 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2054 fe->dai_link->name, cmd);
2056 ret = soc_pcm_bespoke_trigger(substream, cmd);
2058 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2063 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2064 fe->dai_link->name);
2070 case SNDRV_PCM_TRIGGER_START:
2071 case SNDRV_PCM_TRIGGER_RESUME:
2072 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2073 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2075 case SNDRV_PCM_TRIGGER_STOP:
2076 case SNDRV_PCM_TRIGGER_SUSPEND:
2077 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2078 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2083 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2087 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2089 struct snd_soc_pcm_runtime *fe = substream->private_data;
2090 int stream = substream->stream;
2092 /* if FE's runtime_update is already set, we're in race;
2093 * process this trigger later at exit
2095 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2096 fe->dpcm[stream].trigger_pending = cmd + 1;
2097 return 0; /* delayed, assuming it's successful */
2100 /* we're alone, let's trigger */
2101 return dpcm_fe_dai_do_trigger(substream, cmd);
2104 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2106 struct snd_soc_dpcm *dpcm;
2109 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2111 struct snd_soc_pcm_runtime *be = dpcm->be;
2112 struct snd_pcm_substream *be_substream =
2113 snd_soc_dpcm_get_substream(be, stream);
2115 /* is this op for this BE ? */
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2119 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2120 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2123 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2124 dpcm->fe->dai_link->name);
2126 ret = soc_pcm_prepare(be_substream);
2128 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2133 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2138 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2140 struct snd_soc_pcm_runtime *fe = substream->private_data;
2141 int stream = substream->stream, ret = 0;
2143 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2145 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2147 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2149 /* there is no point preparing this FE if there are no BEs */
2150 if (list_empty(&fe->dpcm[stream].be_clients)) {
2151 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2152 fe->dai_link->name);
2157 ret = dpcm_be_dai_prepare(fe, substream->stream);
2161 /* call prepare on the frontend */
2162 ret = soc_pcm_prepare(substream);
2164 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2165 fe->dai_link->name);
2169 /* run the stream event for each BE */
2170 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2171 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2174 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2175 mutex_unlock(&fe->card->mutex);
2180 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2181 unsigned int cmd, void *arg)
2183 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2184 struct snd_soc_platform *platform = rtd->platform;
2186 if (platform->driver->ops && platform->driver->ops->ioctl)
2187 return platform->driver->ops->ioctl(substream, cmd, arg);
2188 return snd_pcm_lib_ioctl(substream, cmd, arg);
2191 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2193 struct snd_pcm_substream *substream =
2194 snd_soc_dpcm_get_substream(fe, stream);
2195 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2198 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2199 stream ? "capture" : "playback", fe->dai_link->name);
2201 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2202 /* call bespoke trigger - FE takes care of all BE triggers */
2203 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2204 fe->dai_link->name);
2206 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2208 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2210 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2211 fe->dai_link->name);
2213 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2215 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2218 err = dpcm_be_dai_hw_free(fe, stream);
2220 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2222 err = dpcm_be_dai_shutdown(fe, stream);
2224 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2226 /* run the stream event for each BE */
2227 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2232 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2234 struct snd_pcm_substream *substream =
2235 snd_soc_dpcm_get_substream(fe, stream);
2236 struct snd_soc_dpcm *dpcm;
2237 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2240 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2241 stream ? "capture" : "playback", fe->dai_link->name);
2243 /* Only start the BE if the FE is ready */
2244 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2245 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2248 /* startup must always be called for new BEs */
2249 ret = dpcm_be_dai_startup(fe, stream);
2253 /* keep going if FE state is > open */
2254 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2257 ret = dpcm_be_dai_hw_params(fe, stream);
2261 /* keep going if FE state is > hw_params */
2262 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2266 ret = dpcm_be_dai_prepare(fe, stream);
2270 /* run the stream event for each BE */
2271 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2273 /* keep going if FE state is > prepare */
2274 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2275 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2278 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2279 /* call trigger on the frontend - FE takes care of all BE triggers */
2280 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2281 fe->dai_link->name);
2283 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2285 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2289 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2290 fe->dai_link->name);
2292 ret = dpcm_be_dai_trigger(fe, stream,
2293 SNDRV_PCM_TRIGGER_START);
2295 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2303 dpcm_be_dai_hw_free(fe, stream);
2305 dpcm_be_dai_shutdown(fe, stream);
2307 /* disconnect any non started BEs */
2308 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2309 struct snd_soc_pcm_runtime *be = dpcm->be;
2310 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2311 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2317 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2321 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2322 ret = dpcm_run_update_startup(fe, stream);
2324 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2325 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2330 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2334 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2335 ret = dpcm_run_update_shutdown(fe, stream);
2337 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2338 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2343 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2346 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2348 int i, old, new, paths;
2350 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2351 for (i = 0; i < card->num_rtd; i++) {
2352 struct snd_soc_dapm_widget_list *list;
2353 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2355 /* make sure link is FE */
2356 if (!fe->dai_link->dynamic)
2359 /* only check active links */
2360 if (!fe->cpu_dai->active)
2363 /* DAPM sync will call this to update DSP paths */
2364 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2365 fe->dai_link->name);
2367 /* skip if FE doesn't have playback capability */
2368 if (!fe->cpu_dai->driver->playback.channels_min
2369 || !fe->codec_dai->driver->playback.channels_min)
2372 /* skip if FE isn't currently playing */
2373 if (!fe->cpu_dai->playback_active
2374 || !fe->codec_dai->playback_active)
2377 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2379 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2380 fe->dai_link->name, "playback");
2381 mutex_unlock(&card->mutex);
2385 /* update any new playback paths */
2386 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2388 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2389 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2390 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2393 /* update any old playback paths */
2394 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2396 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2397 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2398 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2401 dpcm_path_put(&list);
2403 /* skip if FE doesn't have capture capability */
2404 if (!fe->cpu_dai->driver->capture.channels_min
2405 || !fe->codec_dai->driver->capture.channels_min)
2408 /* skip if FE isn't currently capturing */
2409 if (!fe->cpu_dai->capture_active
2410 || !fe->codec_dai->capture_active)
2413 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2415 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2416 fe->dai_link->name, "capture");
2417 mutex_unlock(&card->mutex);
2421 /* update any new capture paths */
2422 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2424 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2425 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2426 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2429 /* update any old capture paths */
2430 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2432 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2433 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2434 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2437 dpcm_path_put(&list);
2440 mutex_unlock(&card->mutex);
2443 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2445 struct snd_soc_dpcm *dpcm;
2446 struct list_head *clients =
2447 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2449 list_for_each_entry(dpcm, clients, list_be) {
2451 struct snd_soc_pcm_runtime *be = dpcm->be;
2454 if (be->dai_link->ignore_suspend)
2457 for (i = 0; i < be->num_codecs; i++) {
2458 struct snd_soc_dai *dai = be->codec_dais[i];
2459 struct snd_soc_dai_driver *drv = dai->driver;
2461 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2462 be->dai_link->name);
2464 if (drv->ops && drv->ops->digital_mute &&
2465 dai->playback_active)
2466 drv->ops->digital_mute(dai, mute);
2473 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2475 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2476 struct snd_soc_dpcm *dpcm;
2477 struct snd_soc_dapm_widget_list *list;
2479 int stream = fe_substream->stream;
2481 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2482 fe->dpcm[stream].runtime = fe_substream->runtime;
2484 ret = dpcm_path_get(fe, stream, &list);
2486 mutex_unlock(&fe->card->mutex);
2488 } else if (ret == 0) {
2489 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2490 fe->dai_link->name, stream ? "capture" : "playback");
2493 /* calculate valid and active FE <-> BE dpcms */
2494 dpcm_process_paths(fe, stream, &list, 1);
2496 ret = dpcm_fe_dai_startup(fe_substream);
2498 /* clean up all links */
2499 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2500 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2502 dpcm_be_disconnect(fe, stream);
2503 fe->dpcm[stream].runtime = NULL;
2506 dpcm_clear_pending_state(fe, stream);
2507 dpcm_path_put(&list);
2508 mutex_unlock(&fe->card->mutex);
2512 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2514 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2515 struct snd_soc_dpcm *dpcm;
2516 int stream = fe_substream->stream, ret;
2518 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2519 ret = dpcm_fe_dai_shutdown(fe_substream);
2521 /* mark FE's links ready to prune */
2522 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2523 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2525 dpcm_be_disconnect(fe, stream);
2527 fe->dpcm[stream].runtime = NULL;
2528 mutex_unlock(&fe->card->mutex);
2532 /* create a new pcm */
2533 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2535 struct snd_soc_platform *platform = rtd->platform;
2536 struct snd_soc_dai *codec_dai;
2537 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2538 struct snd_pcm *pcm;
2540 int ret = 0, playback = 0, capture = 0;
2543 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2544 playback = rtd->dai_link->dpcm_playback;
2545 capture = rtd->dai_link->dpcm_capture;
2547 for (i = 0; i < rtd->num_codecs; i++) {
2548 codec_dai = rtd->codec_dais[i];
2549 if (codec_dai->driver->playback.channels_min)
2551 if (codec_dai->driver->capture.channels_min)
2555 capture = capture && cpu_dai->driver->capture.channels_min;
2556 playback = playback && cpu_dai->driver->playback.channels_min;
2559 if (rtd->dai_link->playback_only) {
2564 if (rtd->dai_link->capture_only) {
2569 /* create the PCM */
2570 if (rtd->dai_link->no_pcm) {
2571 snprintf(new_name, sizeof(new_name), "(%s)",
2572 rtd->dai_link->stream_name);
2574 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2575 playback, capture, &pcm);
2577 if (rtd->dai_link->dynamic)
2578 snprintf(new_name, sizeof(new_name), "%s (*)",
2579 rtd->dai_link->stream_name);
2581 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2582 rtd->dai_link->stream_name,
2583 (rtd->num_codecs > 1) ?
2584 "multicodec" : rtd->codec_dai->name, num);
2586 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2590 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2591 rtd->dai_link->name);
2594 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2596 /* DAPM dai link stream work */
2597 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2599 pcm->nonatomic = rtd->dai_link->nonatomic;
2601 pcm->private_data = rtd;
2603 if (rtd->dai_link->no_pcm) {
2605 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2607 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2611 /* ASoC PCM operations */
2612 if (rtd->dai_link->dynamic) {
2613 rtd->ops.open = dpcm_fe_dai_open;
2614 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2615 rtd->ops.prepare = dpcm_fe_dai_prepare;
2616 rtd->ops.trigger = dpcm_fe_dai_trigger;
2617 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2618 rtd->ops.close = dpcm_fe_dai_close;
2619 rtd->ops.pointer = soc_pcm_pointer;
2620 rtd->ops.ioctl = soc_pcm_ioctl;
2622 rtd->ops.open = soc_pcm_open;
2623 rtd->ops.hw_params = soc_pcm_hw_params;
2624 rtd->ops.prepare = soc_pcm_prepare;
2625 rtd->ops.trigger = soc_pcm_trigger;
2626 rtd->ops.hw_free = soc_pcm_hw_free;
2627 rtd->ops.close = soc_pcm_close;
2628 rtd->ops.pointer = soc_pcm_pointer;
2629 rtd->ops.ioctl = soc_pcm_ioctl;
2632 if (platform->driver->ops) {
2633 rtd->ops.ack = platform->driver->ops->ack;
2634 rtd->ops.copy = platform->driver->ops->copy;
2635 rtd->ops.silence = platform->driver->ops->silence;
2636 rtd->ops.page = platform->driver->ops->page;
2637 rtd->ops.mmap = platform->driver->ops->mmap;
2641 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2646 if (platform->driver->pcm_new) {
2647 ret = platform->driver->pcm_new(rtd);
2649 dev_err(platform->dev,
2650 "ASoC: pcm constructor failed: %d\n",
2656 pcm->private_free = platform->driver->pcm_free;
2658 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2659 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2664 /* is the current PCM operation for this FE ? */
2665 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2667 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2671 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2673 /* is the current PCM operation for this BE ? */
2674 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2675 struct snd_soc_pcm_runtime *be, int stream)
2677 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2678 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2679 be->dpcm[stream].runtime_update))
2683 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2685 /* get the substream for this BE */
2686 struct snd_pcm_substream *
2687 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2689 return be->pcm->streams[stream].substream;
2691 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2693 /* get the BE runtime state */
2694 enum snd_soc_dpcm_state
2695 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2697 return be->dpcm[stream].state;
2699 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2701 /* set the BE runtime state */
2702 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2703 int stream, enum snd_soc_dpcm_state state)
2705 be->dpcm[stream].state = state;
2707 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2710 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2711 * are not running, paused or suspended for the specified stream direction.
2713 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2714 struct snd_soc_pcm_runtime *be, int stream)
2716 struct snd_soc_dpcm *dpcm;
2719 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2724 state = dpcm->fe->dpcm[stream].state;
2725 if (state == SND_SOC_DPCM_STATE_START ||
2726 state == SND_SOC_DPCM_STATE_PAUSED ||
2727 state == SND_SOC_DPCM_STATE_SUSPEND)
2731 /* it's safe to free/stop this BE DAI */
2734 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2737 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2738 * running, paused or suspended for the specified stream direction.
2740 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2741 struct snd_soc_pcm_runtime *be, int stream)
2743 struct snd_soc_dpcm *dpcm;
2746 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2751 state = dpcm->fe->dpcm[stream].state;
2752 if (state == SND_SOC_DPCM_STATE_START ||
2753 state == SND_SOC_DPCM_STATE_PAUSED ||
2754 state == SND_SOC_DPCM_STATE_SUSPEND ||
2755 state == SND_SOC_DPCM_STATE_PREPARE)
2759 /* it's safe to change hw_params */
2762 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2764 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2765 int cmd, struct snd_soc_platform *platform)
2767 if (platform->driver->ops && platform->driver->ops->trigger)
2768 return platform->driver->ops->trigger(substream, cmd);
2771 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2773 #ifdef CONFIG_DEBUG_FS
2774 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2777 case SND_SOC_DPCM_STATE_NEW:
2779 case SND_SOC_DPCM_STATE_OPEN:
2781 case SND_SOC_DPCM_STATE_HW_PARAMS:
2783 case SND_SOC_DPCM_STATE_PREPARE:
2785 case SND_SOC_DPCM_STATE_START:
2787 case SND_SOC_DPCM_STATE_STOP:
2789 case SND_SOC_DPCM_STATE_SUSPEND:
2791 case SND_SOC_DPCM_STATE_PAUSED:
2793 case SND_SOC_DPCM_STATE_HW_FREE:
2795 case SND_SOC_DPCM_STATE_CLOSE:
2802 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2803 int stream, char *buf, size_t size)
2805 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2806 struct snd_soc_dpcm *dpcm;
2810 offset += snprintf(buf + offset, size - offset,
2811 "[%s - %s]\n", fe->dai_link->name,
2812 stream ? "Capture" : "Playback");
2814 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2815 dpcm_state_string(fe->dpcm[stream].state));
2817 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2818 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2819 offset += snprintf(buf + offset, size - offset,
2821 "Format = %s, Channels = %d, Rate = %d\n",
2822 snd_pcm_format_name(params_format(params)),
2823 params_channels(params),
2824 params_rate(params));
2827 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2829 if (list_empty(&fe->dpcm[stream].be_clients)) {
2830 offset += snprintf(buf + offset, size - offset,
2831 " No active DSP links\n");
2835 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2836 struct snd_soc_pcm_runtime *be = dpcm->be;
2837 params = &dpcm->hw_params;
2839 offset += snprintf(buf + offset, size - offset,
2840 "- %s\n", be->dai_link->name);
2842 offset += snprintf(buf + offset, size - offset,
2844 dpcm_state_string(be->dpcm[stream].state));
2846 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2847 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2848 offset += snprintf(buf + offset, size - offset,
2849 " Hardware Params: "
2850 "Format = %s, Channels = %d, Rate = %d\n",
2851 snd_pcm_format_name(params_format(params)),
2852 params_channels(params),
2853 params_rate(params));
2860 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2861 size_t count, loff_t *ppos)
2863 struct snd_soc_pcm_runtime *fe = file->private_data;
2864 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2867 buf = kmalloc(out_count, GFP_KERNEL);
2871 if (fe->cpu_dai->driver->playback.channels_min)
2872 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2873 buf + offset, out_count - offset);
2875 if (fe->cpu_dai->driver->capture.channels_min)
2876 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2877 buf + offset, out_count - offset);
2879 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2885 static const struct file_operations dpcm_state_fops = {
2886 .open = simple_open,
2887 .read = dpcm_state_read_file,
2888 .llseek = default_llseek,
2891 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2896 if (!rtd->card->debugfs_card_root)
2899 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2900 rtd->card->debugfs_card_root);
2901 if (!rtd->debugfs_dpcm_root) {
2903 "ASoC: Failed to create dpcm debugfs directory %s\n",
2904 rtd->dai_link->name);
2908 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2909 rtd->debugfs_dpcm_root,
2910 rtd, &dpcm_state_fops);