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)
136 struct snd_soc_rtdcom_list *rtdcom;
137 struct snd_soc_component *component;
140 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
143 for_each_rtdcom(rtd, rtdcom) {
144 component = rtdcom->component;
146 ignore &= !component->driver->use_pmdown_time;
153 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
154 * @substream: the pcm substream
155 * @hw: the hardware parameters
157 * Sets the substream runtime hardware parameters.
159 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
160 const struct snd_pcm_hardware *hw)
162 struct snd_pcm_runtime *runtime = substream->runtime;
163 runtime->hw.info = hw->info;
164 runtime->hw.formats = hw->formats;
165 runtime->hw.period_bytes_min = hw->period_bytes_min;
166 runtime->hw.period_bytes_max = hw->period_bytes_max;
167 runtime->hw.periods_min = hw->periods_min;
168 runtime->hw.periods_max = hw->periods_max;
169 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
170 runtime->hw.fifo_size = hw->fifo_size;
173 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
175 /* DPCM stream event, send event to FE and all active BEs. */
176 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
179 struct snd_soc_dpcm *dpcm;
181 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
183 struct snd_soc_pcm_runtime *be = dpcm->be;
185 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
186 be->dai_link->name, event, dir);
188 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
189 (be->dpcm[dir].users >= 1))
192 snd_soc_dapm_stream_event(be, dir, event);
195 snd_soc_dapm_stream_event(fe, dir, event);
200 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
201 struct snd_soc_dai *soc_dai)
203 struct snd_soc_pcm_runtime *rtd = substream->private_data;
206 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
207 rtd->dai_link->symmetric_rates)) {
208 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
211 ret = snd_pcm_hw_constraint_single(substream->runtime,
212 SNDRV_PCM_HW_PARAM_RATE,
215 dev_err(soc_dai->dev,
216 "ASoC: Unable to apply rate constraint: %d\n",
222 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
223 rtd->dai_link->symmetric_channels)) {
224 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
227 ret = snd_pcm_hw_constraint_single(substream->runtime,
228 SNDRV_PCM_HW_PARAM_CHANNELS,
231 dev_err(soc_dai->dev,
232 "ASoC: Unable to apply channel symmetry constraint: %d\n",
238 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
239 rtd->dai_link->symmetric_samplebits)) {
240 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
241 soc_dai->sample_bits);
243 ret = snd_pcm_hw_constraint_single(substream->runtime,
244 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
245 soc_dai->sample_bits);
247 dev_err(soc_dai->dev,
248 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
257 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
258 struct snd_pcm_hw_params *params)
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
262 unsigned int rate, channels, sample_bits, symmetry, i;
264 rate = params_rate(params);
265 channels = params_channels(params);
266 sample_bits = snd_pcm_format_physical_width(params_format(params));
268 /* reject unmatched parameters when applying symmetry */
269 symmetry = cpu_dai->driver->symmetric_rates ||
270 rtd->dai_link->symmetric_rates;
272 for (i = 0; i < rtd->num_codecs; i++)
273 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
275 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
276 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
277 cpu_dai->rate, rate);
281 symmetry = cpu_dai->driver->symmetric_channels ||
282 rtd->dai_link->symmetric_channels;
284 for (i = 0; i < rtd->num_codecs; i++)
285 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
287 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
288 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
289 cpu_dai->channels, channels);
293 symmetry = cpu_dai->driver->symmetric_samplebits ||
294 rtd->dai_link->symmetric_samplebits;
296 for (i = 0; i < rtd->num_codecs; i++)
297 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
299 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
300 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
301 cpu_dai->sample_bits, sample_bits);
308 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
311 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
312 struct snd_soc_dai_link *link = rtd->dai_link;
313 unsigned int symmetry, i;
315 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
316 cpu_driver->symmetric_channels || link->symmetric_channels ||
317 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
319 for (i = 0; i < rtd->num_codecs; i++)
320 symmetry = symmetry ||
321 rtd->codec_dais[i]->driver->symmetric_rates ||
322 rtd->codec_dais[i]->driver->symmetric_channels ||
323 rtd->codec_dais[i]->driver->symmetric_samplebits;
328 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
330 struct snd_soc_pcm_runtime *rtd = substream->private_data;
336 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
338 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
342 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
344 struct snd_soc_pcm_runtime *rtd = substream->private_data;
345 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
346 struct snd_soc_dai *codec_dai;
348 unsigned int bits = 0, cpu_bits;
350 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
351 for (i = 0; i < rtd->num_codecs; i++) {
352 codec_dai = rtd->codec_dais[i];
353 if (codec_dai->driver->playback.sig_bits == 0) {
357 bits = max(codec_dai->driver->playback.sig_bits, bits);
359 cpu_bits = cpu_dai->driver->playback.sig_bits;
361 for (i = 0; i < rtd->num_codecs; i++) {
362 codec_dai = rtd->codec_dais[i];
363 if (codec_dai->driver->capture.sig_bits == 0) {
367 bits = max(codec_dai->driver->capture.sig_bits, bits);
369 cpu_bits = cpu_dai->driver->capture.sig_bits;
372 soc_pcm_set_msb(substream, bits);
373 soc_pcm_set_msb(substream, cpu_bits);
376 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
378 struct snd_pcm_runtime *runtime = substream->runtime;
379 struct snd_pcm_hardware *hw = &runtime->hw;
380 struct snd_soc_pcm_runtime *rtd = substream->private_data;
381 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
382 struct snd_soc_dai_driver *codec_dai_drv;
383 struct snd_soc_pcm_stream *codec_stream;
384 struct snd_soc_pcm_stream *cpu_stream;
385 unsigned int chan_min = 0, chan_max = UINT_MAX;
386 unsigned int rate_min = 0, rate_max = UINT_MAX;
387 unsigned int rates = UINT_MAX;
388 u64 formats = ULLONG_MAX;
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 cpu_stream = &cpu_dai_drv->playback;
394 cpu_stream = &cpu_dai_drv->capture;
396 /* first calculate min/max only for CODECs in the DAI link */
397 for (i = 0; i < rtd->num_codecs; i++) {
400 * Skip CODECs which don't support the current stream type.
401 * Otherwise, since the rate, channel, and format values will
402 * zero in that case, we would have no usable settings left,
403 * causing the resulting setup to fail.
404 * At least one CODEC should match, otherwise we should have
405 * bailed out on a higher level, since there would be no
406 * CODEC to support the transfer direction in that case.
408 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
412 codec_dai_drv = rtd->codec_dais[i]->driver;
413 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
414 codec_stream = &codec_dai_drv->playback;
416 codec_stream = &codec_dai_drv->capture;
417 chan_min = max(chan_min, codec_stream->channels_min);
418 chan_max = min(chan_max, codec_stream->channels_max);
419 rate_min = max(rate_min, codec_stream->rate_min);
420 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
421 formats &= codec_stream->formats;
422 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
426 * chan min/max cannot be enforced if there are multiple CODEC DAIs
427 * connected to a single CPU DAI, use CPU DAI's directly and let
428 * channel allocation be fixed up later
430 if (rtd->num_codecs > 1) {
431 chan_min = cpu_stream->channels_min;
432 chan_max = cpu_stream->channels_max;
435 hw->channels_min = max(chan_min, cpu_stream->channels_min);
436 hw->channels_max = min(chan_max, cpu_stream->channels_max);
438 hw->formats &= formats & cpu_stream->formats;
440 hw->formats = formats & cpu_stream->formats;
441 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
443 snd_pcm_limit_hw_rates(runtime);
445 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
446 hw->rate_min = max(hw->rate_min, rate_min);
447 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
448 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
452 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
453 * then initialized and any private data can be allocated. This also calls
454 * startup for the cpu DAI, component, machine and codec DAI.
456 static int soc_pcm_open(struct snd_pcm_substream *substream)
458 struct snd_soc_pcm_runtime *rtd = substream->private_data;
459 struct snd_pcm_runtime *runtime = substream->runtime;
460 struct snd_soc_component *component;
461 struct snd_soc_rtdcom_list *rtdcom;
462 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
463 struct snd_soc_dai *codec_dai;
464 const char *codec_dai_name = "multicodec";
465 int i, ret = 0, __ret;
467 pinctrl_pm_select_default_state(cpu_dai->dev);
468 for (i = 0; i < rtd->num_codecs; i++)
469 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
471 for_each_rtdcom(rtd, rtdcom) {
472 component = rtdcom->component;
474 pm_runtime_get_sync(component->dev);
477 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
479 /* startup the audio subsystem */
480 if (cpu_dai->driver->ops->startup) {
481 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
483 dev_err(cpu_dai->dev, "ASoC: can't open interface"
484 " %s: %d\n", cpu_dai->name, ret);
490 for_each_rtdcom(rtd, rtdcom) {
491 component = rtdcom->component;
493 if (!component->driver->ops ||
494 !component->driver->ops->open)
497 __ret = component->driver->ops->open(substream);
499 dev_err(component->dev,
500 "ASoC: can't open component %s: %d\n",
501 component->name, __ret);
508 for (i = 0; i < rtd->num_codecs; i++) {
509 codec_dai = rtd->codec_dais[i];
510 if (codec_dai->driver->ops->startup) {
511 ret = codec_dai->driver->ops->startup(substream,
514 dev_err(codec_dai->dev,
515 "ASoC: can't open codec %s: %d\n",
516 codec_dai->name, ret);
521 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
522 codec_dai->tx_mask = 0;
524 codec_dai->rx_mask = 0;
527 if (rtd->dai_link->ops->startup) {
528 ret = rtd->dai_link->ops->startup(substream);
530 pr_err("ASoC: %s startup failed: %d\n",
531 rtd->dai_link->name, ret);
536 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
537 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
540 /* Check that the codec and cpu DAIs are compatible */
541 soc_pcm_init_runtime_hw(substream);
543 if (rtd->num_codecs == 1)
544 codec_dai_name = rtd->codec_dai->name;
546 if (soc_pcm_has_symmetry(substream))
547 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
550 if (!runtime->hw.rates) {
551 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
552 codec_dai_name, cpu_dai->name);
555 if (!runtime->hw.formats) {
556 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
557 codec_dai_name, cpu_dai->name);
560 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
561 runtime->hw.channels_min > runtime->hw.channels_max) {
562 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
563 codec_dai_name, cpu_dai->name);
567 soc_pcm_apply_msb(substream);
569 /* Symmetry only applies if we've already got an active stream. */
570 if (cpu_dai->active) {
571 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
576 for (i = 0; i < rtd->num_codecs; i++) {
577 if (rtd->codec_dais[i]->active) {
578 ret = soc_pcm_apply_symmetry(substream,
585 pr_debug("ASoC: %s <-> %s info:\n",
586 codec_dai_name, cpu_dai->name);
587 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
588 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
589 runtime->hw.channels_max);
590 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
591 runtime->hw.rate_max);
595 snd_soc_runtime_activate(rtd, substream->stream);
597 mutex_unlock(&rtd->pcm_mutex);
601 if (rtd->dai_link->ops->shutdown)
602 rtd->dai_link->ops->shutdown(substream);
609 codec_dai = rtd->codec_dais[i];
610 if (codec_dai->driver->ops->shutdown)
611 codec_dai->driver->ops->shutdown(substream, codec_dai);
615 for_each_rtdcom(rtd, rtdcom) {
616 component = rtdcom->component;
618 if (!component->driver->ops ||
619 !component->driver->ops->close)
622 component->driver->ops->close(substream);
625 if (cpu_dai->driver->ops->shutdown)
626 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
628 mutex_unlock(&rtd->pcm_mutex);
630 for_each_rtdcom(rtd, rtdcom) {
631 component = rtdcom->component;
633 pm_runtime_mark_last_busy(component->dev);
634 pm_runtime_put_autosuspend(component->dev);
637 for (i = 0; i < rtd->num_codecs; i++) {
638 if (!rtd->codec_dais[i]->active)
639 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
641 if (!cpu_dai->active)
642 pinctrl_pm_select_sleep_state(cpu_dai->dev);
648 * Power down the audio subsystem pmdown_time msecs after close is called.
649 * This is to ensure there are no pops or clicks in between any music tracks
650 * due to DAPM power cycling.
652 static void close_delayed_work(struct work_struct *work)
654 struct snd_soc_pcm_runtime *rtd =
655 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
656 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
658 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
660 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
661 codec_dai->driver->playback.stream_name,
662 codec_dai->playback_active ? "active" : "inactive",
663 rtd->pop_wait ? "yes" : "no");
665 /* are we waiting on this codec DAI stream */
666 if (rtd->pop_wait == 1) {
668 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
669 SND_SOC_DAPM_STREAM_STOP);
672 mutex_unlock(&rtd->pcm_mutex);
676 * Called by ALSA when a PCM substream is closed. Private data can be
677 * freed here. The cpu DAI, codec DAI, machine and components are also
680 static int soc_pcm_close(struct snd_pcm_substream *substream)
682 struct snd_soc_pcm_runtime *rtd = substream->private_data;
683 struct snd_soc_component *component;
684 struct snd_soc_rtdcom_list *rtdcom;
685 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
686 struct snd_soc_dai *codec_dai;
689 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
691 snd_soc_runtime_deactivate(rtd, substream->stream);
693 /* clear the corresponding DAIs rate when inactive */
694 if (!cpu_dai->active)
697 for (i = 0; i < rtd->num_codecs; i++) {
698 codec_dai = rtd->codec_dais[i];
699 if (!codec_dai->active)
703 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
705 if (cpu_dai->driver->ops->shutdown)
706 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
708 for (i = 0; i < rtd->num_codecs; i++) {
709 codec_dai = rtd->codec_dais[i];
710 if (codec_dai->driver->ops->shutdown)
711 codec_dai->driver->ops->shutdown(substream, codec_dai);
714 if (rtd->dai_link->ops->shutdown)
715 rtd->dai_link->ops->shutdown(substream);
717 for_each_rtdcom(rtd, rtdcom) {
718 component = rtdcom->component;
720 if (!component->driver->ops ||
721 !component->driver->ops->close)
724 component->driver->ops->close(substream);
727 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
728 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
729 /* powered down playback stream now */
730 snd_soc_dapm_stream_event(rtd,
731 SNDRV_PCM_STREAM_PLAYBACK,
732 SND_SOC_DAPM_STREAM_STOP);
734 /* start delayed pop wq here for playback streams */
736 queue_delayed_work(system_power_efficient_wq,
738 msecs_to_jiffies(rtd->pmdown_time));
741 /* capture streams can be powered down now */
742 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
743 SND_SOC_DAPM_STREAM_STOP);
746 mutex_unlock(&rtd->pcm_mutex);
748 for_each_rtdcom(rtd, rtdcom) {
749 component = rtdcom->component;
751 pm_runtime_mark_last_busy(component->dev);
752 pm_runtime_put_autosuspend(component->dev);
755 for (i = 0; i < rtd->num_codecs; i++) {
756 if (!rtd->codec_dais[i]->active)
757 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
759 if (!cpu_dai->active)
760 pinctrl_pm_select_sleep_state(cpu_dai->dev);
766 * Called by ALSA when the PCM substream is prepared, can set format, sample
767 * rate, etc. This function is non atomic and can be called multiple times,
768 * it can refer to the runtime info.
770 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
772 struct snd_soc_pcm_runtime *rtd = substream->private_data;
773 struct snd_soc_component *component;
774 struct snd_soc_rtdcom_list *rtdcom;
775 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
776 struct snd_soc_dai *codec_dai;
779 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
781 if (rtd->dai_link->ops->prepare) {
782 ret = rtd->dai_link->ops->prepare(substream);
784 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
790 for_each_rtdcom(rtd, rtdcom) {
791 component = rtdcom->component;
793 if (!component->driver->ops ||
794 !component->driver->ops->prepare)
797 ret = component->driver->ops->prepare(substream);
799 dev_err(component->dev,
800 "ASoC: platform prepare error: %d\n", ret);
805 for (i = 0; i < rtd->num_codecs; i++) {
806 codec_dai = rtd->codec_dais[i];
807 if (codec_dai->driver->ops->prepare) {
808 ret = codec_dai->driver->ops->prepare(substream,
811 dev_err(codec_dai->dev,
812 "ASoC: codec DAI prepare error: %d\n",
819 if (cpu_dai->driver->ops->prepare) {
820 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
822 dev_err(cpu_dai->dev,
823 "ASoC: cpu DAI prepare error: %d\n", ret);
828 /* cancel any delayed stream shutdown that is pending */
829 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
832 cancel_delayed_work(&rtd->delayed_work);
835 snd_soc_dapm_stream_event(rtd, substream->stream,
836 SND_SOC_DAPM_STREAM_START);
838 for (i = 0; i < rtd->num_codecs; i++)
839 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
841 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
844 mutex_unlock(&rtd->pcm_mutex);
848 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
851 struct snd_interval *interval;
852 int channels = hweight_long(mask);
854 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
855 interval->min = channels;
856 interval->max = channels;
859 int soc_dai_hw_params(struct snd_pcm_substream *substream,
860 struct snd_pcm_hw_params *params,
861 struct snd_soc_dai *dai)
865 if (dai->driver->ops->hw_params) {
866 ret = dai->driver->ops->hw_params(substream, params, dai);
868 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
878 * Called by ALSA when the hardware params are set by application. This
879 * function can also be called multiple times and can allocate buffers
880 * (using snd_pcm_lib_* ). It's non-atomic.
882 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
883 struct snd_pcm_hw_params *params)
885 struct snd_soc_pcm_runtime *rtd = substream->private_data;
886 struct snd_soc_component *component;
887 struct snd_soc_rtdcom_list *rtdcom;
888 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
889 int i, ret = 0, __ret;
891 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
892 if (rtd->dai_link->ops->hw_params) {
893 ret = rtd->dai_link->ops->hw_params(substream, params);
895 dev_err(rtd->card->dev, "ASoC: machine hw_params"
896 " failed: %d\n", ret);
901 for (i = 0; i < rtd->num_codecs; i++) {
902 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
903 struct snd_pcm_hw_params codec_params;
906 * Skip CODECs which don't support the current stream type,
907 * the idea being that if a CODEC is not used for the currently
908 * set up transfer direction, it should not need to be
909 * configured, especially since the configuration used might
910 * not even be supported by that CODEC. There may be cases
911 * however where a CODEC needs to be set up although it is
912 * actually not being used for the transfer, e.g. if a
913 * capture-only CODEC is acting as an LRCLK and/or BCLK master
914 * for the DAI link including a playback-only CODEC.
915 * If this becomes necessary, we will have to augment the
916 * machine driver setup with information on how to act, so
917 * we can do the right thing here.
919 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
922 /* copy params for each codec */
923 codec_params = *params;
925 /* fixup params based on TDM slot masks */
926 if (codec_dai->tx_mask)
927 soc_pcm_codec_params_fixup(&codec_params,
929 if (codec_dai->rx_mask)
930 soc_pcm_codec_params_fixup(&codec_params,
933 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
937 codec_dai->rate = params_rate(&codec_params);
938 codec_dai->channels = params_channels(&codec_params);
939 codec_dai->sample_bits = snd_pcm_format_physical_width(
940 params_format(&codec_params));
943 ret = soc_dai_hw_params(substream, params, cpu_dai);
948 for_each_rtdcom(rtd, rtdcom) {
949 component = rtdcom->component;
951 if (!component->driver->ops ||
952 !component->driver->ops->hw_params)
955 __ret = component->driver->ops->hw_params(substream, params);
957 dev_err(component->dev,
958 "ASoC: %s hw params failed: %d\n",
959 component->name, __ret);
966 /* store the parameters for each DAIs */
967 cpu_dai->rate = params_rate(params);
968 cpu_dai->channels = params_channels(params);
969 cpu_dai->sample_bits =
970 snd_pcm_format_physical_width(params_format(params));
972 ret = soc_pcm_params_symmetry(substream, params);
976 mutex_unlock(&rtd->pcm_mutex);
980 for_each_rtdcom(rtd, rtdcom) {
981 component = rtdcom->component;
983 if (!component->driver->ops ||
984 !component->driver->ops->hw_free)
987 component->driver->ops->hw_free(substream);
990 if (cpu_dai->driver->ops->hw_free)
991 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
998 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
999 if (codec_dai->driver->ops->hw_free)
1000 codec_dai->driver->ops->hw_free(substream, codec_dai);
1001 codec_dai->rate = 0;
1004 if (rtd->dai_link->ops->hw_free)
1005 rtd->dai_link->ops->hw_free(substream);
1007 mutex_unlock(&rtd->pcm_mutex);
1012 * Frees resources allocated by hw_params, can be called multiple times
1014 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1016 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1017 struct snd_soc_component *component;
1018 struct snd_soc_rtdcom_list *rtdcom;
1019 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1020 struct snd_soc_dai *codec_dai;
1021 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1024 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1026 /* clear the corresponding DAIs parameters when going to be inactive */
1027 if (cpu_dai->active == 1) {
1029 cpu_dai->channels = 0;
1030 cpu_dai->sample_bits = 0;
1033 for (i = 0; i < rtd->num_codecs; i++) {
1034 codec_dai = rtd->codec_dais[i];
1035 if (codec_dai->active == 1) {
1036 codec_dai->rate = 0;
1037 codec_dai->channels = 0;
1038 codec_dai->sample_bits = 0;
1042 /* apply codec digital mute */
1043 for (i = 0; i < rtd->num_codecs; i++) {
1044 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1045 (!playback && rtd->codec_dais[i]->capture_active == 1))
1046 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1050 /* free any machine hw params */
1051 if (rtd->dai_link->ops->hw_free)
1052 rtd->dai_link->ops->hw_free(substream);
1054 /* free any component resources */
1055 for_each_rtdcom(rtd, rtdcom) {
1056 component = rtdcom->component;
1058 if (!component->driver->ops ||
1059 !component->driver->ops->hw_free)
1062 component->driver->ops->hw_free(substream);
1065 /* now free hw params for the DAIs */
1066 for (i = 0; i < rtd->num_codecs; i++) {
1067 codec_dai = rtd->codec_dais[i];
1068 if (codec_dai->driver->ops->hw_free)
1069 codec_dai->driver->ops->hw_free(substream, codec_dai);
1072 if (cpu_dai->driver->ops->hw_free)
1073 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1075 mutex_unlock(&rtd->pcm_mutex);
1079 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1081 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1082 struct snd_soc_component *component;
1083 struct snd_soc_rtdcom_list *rtdcom;
1084 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1085 struct snd_soc_dai *codec_dai;
1088 for (i = 0; i < rtd->num_codecs; i++) {
1089 codec_dai = rtd->codec_dais[i];
1090 if (codec_dai->driver->ops->trigger) {
1091 ret = codec_dai->driver->ops->trigger(substream,
1098 for_each_rtdcom(rtd, rtdcom) {
1099 component = rtdcom->component;
1101 if (!component->driver->ops ||
1102 !component->driver->ops->trigger)
1105 ret = component->driver->ops->trigger(substream, cmd);
1110 if (cpu_dai->driver->ops->trigger) {
1111 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1116 if (rtd->dai_link->ops->trigger) {
1117 ret = rtd->dai_link->ops->trigger(substream, cmd);
1125 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1129 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1130 struct snd_soc_dai *codec_dai;
1133 for (i = 0; i < rtd->num_codecs; i++) {
1134 codec_dai = rtd->codec_dais[i];
1135 if (codec_dai->driver->ops->bespoke_trigger) {
1136 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1143 if (cpu_dai->driver->ops->bespoke_trigger) {
1144 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1151 * soc level wrapper for pointer callback
1152 * If cpu_dai, codec_dai, component driver has the delay callback, then
1153 * the runtime->delay will be updated accordingly.
1155 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1157 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1158 struct snd_soc_component *component;
1159 struct snd_soc_rtdcom_list *rtdcom;
1160 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1161 struct snd_soc_dai *codec_dai;
1162 struct snd_pcm_runtime *runtime = substream->runtime;
1163 snd_pcm_uframes_t offset = 0;
1164 snd_pcm_sframes_t delay = 0;
1165 snd_pcm_sframes_t codec_delay = 0;
1168 for_each_rtdcom(rtd, rtdcom) {
1169 component = rtdcom->component;
1171 if (!component->driver->ops ||
1172 !component->driver->ops->pointer)
1175 /* FIXME: use 1st pointer */
1176 offset = component->driver->ops->pointer(substream);
1180 if (cpu_dai->driver->ops->delay)
1181 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1183 for (i = 0; i < rtd->num_codecs; i++) {
1184 codec_dai = rtd->codec_dais[i];
1185 if (codec_dai->driver->ops->delay)
1186 codec_delay = max(codec_delay,
1187 codec_dai->driver->ops->delay(substream,
1190 delay += codec_delay;
1192 runtime->delay = delay;
1197 /* connect a FE and BE */
1198 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1199 struct snd_soc_pcm_runtime *be, int stream)
1201 struct snd_soc_dpcm *dpcm;
1203 /* only add new dpcms */
1204 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1205 if (dpcm->be == be && dpcm->fe == fe)
1209 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1215 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1216 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1217 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1218 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1220 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1221 stream ? "capture" : "playback", fe->dai_link->name,
1222 stream ? "<-" : "->", be->dai_link->name);
1224 #ifdef CONFIG_DEBUG_FS
1225 if (fe->debugfs_dpcm_root)
1226 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1227 fe->debugfs_dpcm_root, &dpcm->state);
1232 /* reparent a BE onto another FE */
1233 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1234 struct snd_soc_pcm_runtime *be, int stream)
1236 struct snd_soc_dpcm *dpcm;
1237 struct snd_pcm_substream *fe_substream, *be_substream;
1239 /* reparent if BE is connected to other FEs */
1240 if (!be->dpcm[stream].users)
1243 be_substream = snd_soc_dpcm_get_substream(be, stream);
1245 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1249 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1250 stream ? "capture" : "playback",
1251 dpcm->fe->dai_link->name,
1252 stream ? "<-" : "->", dpcm->be->dai_link->name);
1254 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1255 be_substream->runtime = fe_substream->runtime;
1260 /* disconnect a BE and FE */
1261 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1263 struct snd_soc_dpcm *dpcm, *d;
1265 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1266 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1267 stream ? "capture" : "playback",
1268 dpcm->be->dai_link->name);
1270 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1273 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1274 stream ? "capture" : "playback", fe->dai_link->name,
1275 stream ? "<-" : "->", dpcm->be->dai_link->name);
1277 /* BEs still alive need new FE */
1278 dpcm_be_reparent(fe, dpcm->be, stream);
1280 #ifdef CONFIG_DEBUG_FS
1281 debugfs_remove(dpcm->debugfs_state);
1283 list_del(&dpcm->list_be);
1284 list_del(&dpcm->list_fe);
1289 /* get BE for DAI widget and stream */
1290 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1291 struct snd_soc_dapm_widget *widget, int stream)
1293 struct snd_soc_pcm_runtime *be;
1296 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1298 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1299 list_for_each_entry(be, &card->rtd_list, list) {
1301 if (!be->dai_link->no_pcm)
1304 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1305 be->cpu_dai->playback_widget ?
1306 be->cpu_dai->playback_widget->name : "(not set)");
1308 if (be->cpu_dai->playback_widget == widget)
1311 for (i = 0; i < be->num_codecs; i++) {
1312 struct snd_soc_dai *dai = be->codec_dais[i];
1313 if (dai->playback_widget == widget)
1319 list_for_each_entry(be, &card->rtd_list, list) {
1321 if (!be->dai_link->no_pcm)
1324 dev_dbg(card->dev, "ASoC: try BE %s\n",
1325 be->cpu_dai->capture_widget ?
1326 be->cpu_dai->capture_widget->name : "(not set)");
1328 if (be->cpu_dai->capture_widget == widget)
1331 for (i = 0; i < be->num_codecs; i++) {
1332 struct snd_soc_dai *dai = be->codec_dais[i];
1333 if (dai->capture_widget == widget)
1339 /* dai link name and stream name set correctly ? */
1340 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1341 stream ? "capture" : "playback", widget->name);
1345 static inline struct snd_soc_dapm_widget *
1346 dai_get_widget(struct snd_soc_dai *dai, int stream)
1348 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1349 return dai->playback_widget;
1351 return dai->capture_widget;
1354 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1355 struct snd_soc_dapm_widget *widget)
1359 for (i = 0; i < list->num_widgets; i++) {
1360 if (widget == list->widgets[i])
1367 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1368 enum snd_soc_dapm_direction dir)
1370 struct snd_soc_card *card = widget->dapm->card;
1371 struct snd_soc_pcm_runtime *rtd;
1374 if (dir == SND_SOC_DAPM_DIR_OUT) {
1375 list_for_each_entry(rtd, &card->rtd_list, list) {
1376 if (!rtd->dai_link->no_pcm)
1379 if (rtd->cpu_dai->playback_widget == widget)
1382 for (i = 0; i < rtd->num_codecs; ++i) {
1383 struct snd_soc_dai *dai = rtd->codec_dais[i];
1384 if (dai->playback_widget == widget)
1388 } else { /* SND_SOC_DAPM_DIR_IN */
1389 list_for_each_entry(rtd, &card->rtd_list, list) {
1390 if (!rtd->dai_link->no_pcm)
1393 if (rtd->cpu_dai->capture_widget == widget)
1396 for (i = 0; i < rtd->num_codecs; ++i) {
1397 struct snd_soc_dai *dai = rtd->codec_dais[i];
1398 if (dai->capture_widget == widget)
1407 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1408 int stream, struct snd_soc_dapm_widget_list **list)
1410 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1413 /* get number of valid DAI paths and their widgets */
1414 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1415 dpcm_end_walk_at_be);
1417 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1418 stream ? "capture" : "playback");
1423 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1424 struct snd_soc_dapm_widget_list **list_)
1426 struct snd_soc_dpcm *dpcm;
1427 struct snd_soc_dapm_widget_list *list = *list_;
1428 struct snd_soc_dapm_widget *widget;
1431 /* Destroy any old FE <--> BE connections */
1432 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1435 /* is there a valid CPU DAI widget for this BE */
1436 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1438 /* prune the BE if it's no longer in our active list */
1439 if (widget && widget_in_list(list, widget))
1442 /* is there a valid CODEC DAI widget for this BE */
1443 for (i = 0; i < dpcm->be->num_codecs; i++) {
1444 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1445 widget = dai_get_widget(dai, stream);
1447 /* prune the BE if it's no longer in our active list */
1448 if (widget && widget_in_list(list, widget))
1452 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1453 stream ? "capture" : "playback",
1454 dpcm->be->dai_link->name, fe->dai_link->name);
1455 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1456 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1460 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1464 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1465 struct snd_soc_dapm_widget_list **list_)
1467 struct snd_soc_card *card = fe->card;
1468 struct snd_soc_dapm_widget_list *list = *list_;
1469 struct snd_soc_pcm_runtime *be;
1470 int i, new = 0, err;
1472 /* Create any new FE <--> BE connections */
1473 for (i = 0; i < list->num_widgets; i++) {
1475 switch (list->widgets[i]->id) {
1476 case snd_soc_dapm_dai_in:
1477 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1480 case snd_soc_dapm_dai_out:
1481 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1488 /* is there a valid BE rtd for this widget */
1489 be = dpcm_get_be(card, list->widgets[i], stream);
1491 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1492 list->widgets[i]->name);
1496 /* make sure BE is a real BE */
1497 if (!be->dai_link->no_pcm)
1500 /* don't connect if FE is not running */
1501 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1504 /* newly connected FE and BE */
1505 err = dpcm_be_connect(fe, be, stream);
1507 dev_err(fe->dev, "ASoC: can't connect %s\n",
1508 list->widgets[i]->name);
1510 } else if (err == 0) /* already connected */
1514 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1518 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1523 * Find the corresponding BE DAIs that source or sink audio to this
1526 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1527 int stream, struct snd_soc_dapm_widget_list **list, int new)
1530 return dpcm_add_paths(fe, stream, list);
1532 return dpcm_prune_paths(fe, stream, list);
1535 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1537 struct snd_soc_dpcm *dpcm;
1539 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1540 dpcm->be->dpcm[stream].runtime_update =
1541 SND_SOC_DPCM_UPDATE_NO;
1544 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1547 struct snd_soc_dpcm *dpcm;
1549 /* disable any enabled and non active backends */
1550 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1552 struct snd_soc_pcm_runtime *be = dpcm->be;
1553 struct snd_pcm_substream *be_substream =
1554 snd_soc_dpcm_get_substream(be, stream);
1556 if (be->dpcm[stream].users == 0)
1557 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1558 stream ? "capture" : "playback",
1559 be->dpcm[stream].state);
1561 if (--be->dpcm[stream].users != 0)
1564 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1567 soc_pcm_close(be_substream);
1568 be_substream->runtime = NULL;
1569 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1573 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1575 struct snd_soc_dpcm *dpcm;
1578 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1579 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1581 struct snd_soc_pcm_runtime *be = dpcm->be;
1582 struct snd_pcm_substream *be_substream =
1583 snd_soc_dpcm_get_substream(be, stream);
1585 if (!be_substream) {
1586 dev_err(be->dev, "ASoC: no backend %s stream\n",
1587 stream ? "capture" : "playback");
1591 /* is this op for this BE ? */
1592 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1595 /* first time the dpcm is open ? */
1596 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1597 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1598 stream ? "capture" : "playback",
1599 be->dpcm[stream].state);
1601 if (be->dpcm[stream].users++ != 0)
1604 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1605 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1608 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1609 stream ? "capture" : "playback", be->dai_link->name);
1611 be_substream->runtime = be->dpcm[stream].runtime;
1612 err = soc_pcm_open(be_substream);
1614 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1615 be->dpcm[stream].users--;
1616 if (be->dpcm[stream].users < 0)
1617 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1618 stream ? "capture" : "playback",
1619 be->dpcm[stream].state);
1621 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1625 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1632 /* disable any enabled and non active backends */
1633 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1634 struct snd_soc_pcm_runtime *be = dpcm->be;
1635 struct snd_pcm_substream *be_substream =
1636 snd_soc_dpcm_get_substream(be, stream);
1638 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1641 if (be->dpcm[stream].users == 0)
1642 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1643 stream ? "capture" : "playback",
1644 be->dpcm[stream].state);
1646 if (--be->dpcm[stream].users != 0)
1649 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1652 soc_pcm_close(be_substream);
1653 be_substream->runtime = NULL;
1654 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1660 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1661 struct snd_soc_pcm_stream *stream,
1664 runtime->hw.rate_min = stream->rate_min;
1665 runtime->hw.rate_max = stream->rate_max;
1666 runtime->hw.channels_min = stream->channels_min;
1667 runtime->hw.channels_max = stream->channels_max;
1668 if (runtime->hw.formats)
1669 runtime->hw.formats &= formats & stream->formats;
1671 runtime->hw.formats = formats & stream->formats;
1672 runtime->hw.rates = stream->rates;
1675 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1677 struct snd_soc_pcm_runtime *fe = substream->private_data;
1678 struct snd_soc_dpcm *dpcm;
1679 u64 formats = ULLONG_MAX;
1680 int stream = substream->stream;
1682 if (!fe->dai_link->dpcm_merged_format)
1686 * It returns merged BE codec format
1687 * if FE want to use it (= dpcm_merged_format)
1690 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1691 struct snd_soc_pcm_runtime *be = dpcm->be;
1692 struct snd_soc_dai_driver *codec_dai_drv;
1693 struct snd_soc_pcm_stream *codec_stream;
1696 for (i = 0; i < be->num_codecs; i++) {
1698 * Skip CODECs which don't support the current stream
1699 * type. See soc_pcm_init_runtime_hw() for more details
1701 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1705 codec_dai_drv = be->codec_dais[i]->driver;
1706 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1707 codec_stream = &codec_dai_drv->playback;
1709 codec_stream = &codec_dai_drv->capture;
1711 formats &= codec_stream->formats;
1718 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1720 struct snd_pcm_runtime *runtime = substream->runtime;
1721 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1722 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1723 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1724 u64 format = dpcm_runtime_base_format(substream);
1726 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1727 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1729 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1732 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1734 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1735 * for avoiding the race with trigger callback.
1736 * If the state is unset and a trigger is pending while the previous operation,
1737 * process the pending trigger action here.
1739 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1740 int stream, enum snd_soc_dpcm_update state)
1742 struct snd_pcm_substream *substream =
1743 snd_soc_dpcm_get_substream(fe, stream);
1745 snd_pcm_stream_lock_irq(substream);
1746 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1747 dpcm_fe_dai_do_trigger(substream,
1748 fe->dpcm[stream].trigger_pending - 1);
1749 fe->dpcm[stream].trigger_pending = 0;
1751 fe->dpcm[stream].runtime_update = state;
1752 snd_pcm_stream_unlock_irq(substream);
1755 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1758 struct snd_soc_dpcm *dpcm;
1759 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1760 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1763 /* apply symmetry for FE */
1764 if (soc_pcm_has_symmetry(fe_substream))
1765 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1767 /* Symmetry only applies if we've got an active stream. */
1768 if (fe_cpu_dai->active) {
1769 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1774 /* apply symmetry for BE */
1775 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1776 struct snd_soc_pcm_runtime *be = dpcm->be;
1777 struct snd_pcm_substream *be_substream =
1778 snd_soc_dpcm_get_substream(be, stream);
1779 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1782 if (rtd->dai_link->be_hw_params_fixup)
1785 if (soc_pcm_has_symmetry(be_substream))
1786 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1788 /* Symmetry only applies if we've got an active stream. */
1789 if (rtd->cpu_dai->active) {
1790 err = soc_pcm_apply_symmetry(fe_substream,
1796 for (i = 0; i < rtd->num_codecs; i++) {
1797 if (rtd->codec_dais[i]->active) {
1798 err = soc_pcm_apply_symmetry(fe_substream,
1799 rtd->codec_dais[i]);
1809 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1811 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1812 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1813 int stream = fe_substream->stream, ret = 0;
1815 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1817 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1819 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1823 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1825 /* start the DAI frontend */
1826 ret = soc_pcm_open(fe_substream);
1828 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1832 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1834 dpcm_set_fe_runtime(fe_substream);
1835 snd_pcm_limit_hw_rates(runtime);
1837 ret = dpcm_apply_symmetry(fe_substream, stream);
1839 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1844 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1848 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1850 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1854 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1856 struct snd_soc_dpcm *dpcm;
1858 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1859 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1861 struct snd_soc_pcm_runtime *be = dpcm->be;
1862 struct snd_pcm_substream *be_substream =
1863 snd_soc_dpcm_get_substream(be, stream);
1865 /* is this op for this BE ? */
1866 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1869 if (be->dpcm[stream].users == 0)
1870 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1871 stream ? "capture" : "playback",
1872 be->dpcm[stream].state);
1874 if (--be->dpcm[stream].users != 0)
1877 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1878 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1879 soc_pcm_hw_free(be_substream);
1880 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1883 dev_dbg(be->dev, "ASoC: close BE %s\n",
1884 be->dai_link->name);
1886 soc_pcm_close(be_substream);
1887 be_substream->runtime = NULL;
1889 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1894 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1896 struct snd_soc_pcm_runtime *fe = substream->private_data;
1897 int stream = substream->stream;
1899 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1901 /* shutdown the BEs */
1902 dpcm_be_dai_shutdown(fe, substream->stream);
1904 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1906 /* now shutdown the frontend */
1907 soc_pcm_close(substream);
1909 /* run the stream event for each BE */
1910 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1912 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1913 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1917 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1919 struct snd_soc_dpcm *dpcm;
1921 /* only hw_params backends that are either sinks or sources
1922 * to this frontend DAI */
1923 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1925 struct snd_soc_pcm_runtime *be = dpcm->be;
1926 struct snd_pcm_substream *be_substream =
1927 snd_soc_dpcm_get_substream(be, stream);
1929 /* is this op for this BE ? */
1930 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1933 /* only free hw when no longer used - check all FEs */
1934 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1937 /* do not free hw if this BE is used by other FE */
1938 if (be->dpcm[stream].users > 1)
1941 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1942 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1943 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1944 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1945 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1946 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1949 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1950 be->dai_link->name);
1952 soc_pcm_hw_free(be_substream);
1954 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1960 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1962 struct snd_soc_pcm_runtime *fe = substream->private_data;
1963 int err, stream = substream->stream;
1965 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1966 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1968 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1970 /* call hw_free on the frontend */
1971 err = soc_pcm_hw_free(substream);
1973 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1974 fe->dai_link->name);
1976 /* only hw_params backends that are either sinks or sources
1977 * to this frontend DAI */
1978 err = dpcm_be_dai_hw_free(fe, stream);
1980 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1981 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1983 mutex_unlock(&fe->card->mutex);
1987 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1989 struct snd_soc_dpcm *dpcm;
1992 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1994 struct snd_soc_pcm_runtime *be = dpcm->be;
1995 struct snd_pcm_substream *be_substream =
1996 snd_soc_dpcm_get_substream(be, stream);
1998 /* is this op for this BE ? */
1999 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2002 /* copy params for each dpcm */
2003 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2004 sizeof(struct snd_pcm_hw_params));
2006 /* perform any hw_params fixups */
2007 if (be->dai_link->be_hw_params_fixup) {
2008 ret = be->dai_link->be_hw_params_fixup(be,
2012 "ASoC: hw_params BE fixup failed %d\n",
2018 /* only allow hw_params() if no connected FEs are running */
2019 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2022 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2023 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2024 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2027 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2028 be->dai_link->name);
2030 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2032 dev_err(dpcm->be->dev,
2033 "ASoC: hw_params BE failed %d\n", ret);
2037 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2042 /* disable any enabled and non active backends */
2043 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2044 struct snd_soc_pcm_runtime *be = dpcm->be;
2045 struct snd_pcm_substream *be_substream =
2046 snd_soc_dpcm_get_substream(be, stream);
2048 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2051 /* only allow hw_free() if no connected FEs are running */
2052 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2055 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2056 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2057 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2058 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2061 soc_pcm_hw_free(be_substream);
2067 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2068 struct snd_pcm_hw_params *params)
2070 struct snd_soc_pcm_runtime *fe = substream->private_data;
2071 int ret, stream = substream->stream;
2073 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2074 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2076 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2077 sizeof(struct snd_pcm_hw_params));
2078 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2080 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2084 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2085 fe->dai_link->name, params_rate(params),
2086 params_channels(params), params_format(params));
2088 /* call hw_params on the frontend */
2089 ret = soc_pcm_hw_params(substream, params);
2091 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2092 dpcm_be_dai_hw_free(fe, stream);
2094 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2097 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2098 mutex_unlock(&fe->card->mutex);
2102 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2103 struct snd_pcm_substream *substream, int cmd)
2107 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2108 dpcm->be->dai_link->name, cmd);
2110 ret = soc_pcm_trigger(substream, cmd);
2112 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2117 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2120 struct snd_soc_dpcm *dpcm;
2123 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2125 struct snd_soc_pcm_runtime *be = dpcm->be;
2126 struct snd_pcm_substream *be_substream =
2127 snd_soc_dpcm_get_substream(be, stream);
2129 /* is this op for this BE ? */
2130 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2134 case SNDRV_PCM_TRIGGER_START:
2135 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2136 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2139 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2143 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2145 case SNDRV_PCM_TRIGGER_RESUME:
2146 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2149 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2153 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2155 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2156 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2159 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2163 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2165 case SNDRV_PCM_TRIGGER_STOP:
2166 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2169 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2172 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2176 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2178 case SNDRV_PCM_TRIGGER_SUSPEND:
2179 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2182 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2185 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2189 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2191 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2192 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2195 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2198 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2202 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2209 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2211 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2213 struct snd_soc_pcm_runtime *fe = substream->private_data;
2214 int stream = substream->stream, ret;
2215 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2217 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2220 case SND_SOC_DPCM_TRIGGER_PRE:
2221 /* call trigger on the frontend before the backend. */
2223 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2224 fe->dai_link->name, cmd);
2226 ret = soc_pcm_trigger(substream, cmd);
2228 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2232 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2234 case SND_SOC_DPCM_TRIGGER_POST:
2235 /* call trigger on the frontend after the backend. */
2237 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2239 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2243 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2244 fe->dai_link->name, cmd);
2246 ret = soc_pcm_trigger(substream, cmd);
2248 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2249 /* bespoke trigger() - handles both FE and BEs */
2251 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2252 fe->dai_link->name, cmd);
2254 ret = soc_pcm_bespoke_trigger(substream, cmd);
2256 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2261 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2262 fe->dai_link->name);
2268 case SNDRV_PCM_TRIGGER_START:
2269 case SNDRV_PCM_TRIGGER_RESUME:
2270 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2271 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2273 case SNDRV_PCM_TRIGGER_STOP:
2274 case SNDRV_PCM_TRIGGER_SUSPEND:
2275 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2277 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2278 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2283 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2287 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2289 struct snd_soc_pcm_runtime *fe = substream->private_data;
2290 int stream = substream->stream;
2292 /* if FE's runtime_update is already set, we're in race;
2293 * process this trigger later at exit
2295 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2296 fe->dpcm[stream].trigger_pending = cmd + 1;
2297 return 0; /* delayed, assuming it's successful */
2300 /* we're alone, let's trigger */
2301 return dpcm_fe_dai_do_trigger(substream, cmd);
2304 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2306 struct snd_soc_dpcm *dpcm;
2309 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2311 struct snd_soc_pcm_runtime *be = dpcm->be;
2312 struct snd_pcm_substream *be_substream =
2313 snd_soc_dpcm_get_substream(be, stream);
2315 /* is this op for this BE ? */
2316 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2319 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2320 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2321 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2324 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2325 be->dai_link->name);
2327 ret = soc_pcm_prepare(be_substream);
2329 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2334 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2339 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2341 struct snd_soc_pcm_runtime *fe = substream->private_data;
2342 int stream = substream->stream, ret = 0;
2344 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2346 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2348 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2350 /* there is no point preparing this FE if there are no BEs */
2351 if (list_empty(&fe->dpcm[stream].be_clients)) {
2352 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2353 fe->dai_link->name);
2358 ret = dpcm_be_dai_prepare(fe, substream->stream);
2362 /* call prepare on the frontend */
2363 ret = soc_pcm_prepare(substream);
2365 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2366 fe->dai_link->name);
2370 /* run the stream event for each BE */
2371 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2372 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2375 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2376 mutex_unlock(&fe->card->mutex);
2381 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2382 unsigned int cmd, void *arg)
2384 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2385 struct snd_soc_component *component;
2386 struct snd_soc_rtdcom_list *rtdcom;
2388 for_each_rtdcom(rtd, rtdcom) {
2389 component = rtdcom->component;
2391 if (!component->driver->ops ||
2392 !component->driver->ops->ioctl)
2395 /* FIXME: use 1st ioctl */
2396 return component->driver->ops->ioctl(substream, cmd, arg);
2399 return snd_pcm_lib_ioctl(substream, cmd, arg);
2402 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2404 struct snd_pcm_substream *substream =
2405 snd_soc_dpcm_get_substream(fe, stream);
2406 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2409 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2410 stream ? "capture" : "playback", fe->dai_link->name);
2412 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2413 /* call bespoke trigger - FE takes care of all BE triggers */
2414 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2415 fe->dai_link->name);
2417 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2419 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2421 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2422 fe->dai_link->name);
2424 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2426 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2429 err = dpcm_be_dai_hw_free(fe, stream);
2431 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2433 err = dpcm_be_dai_shutdown(fe, stream);
2435 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2437 /* run the stream event for each BE */
2438 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2443 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2445 struct snd_pcm_substream *substream =
2446 snd_soc_dpcm_get_substream(fe, stream);
2447 struct snd_soc_dpcm *dpcm;
2448 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2451 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2452 stream ? "capture" : "playback", fe->dai_link->name);
2454 /* Only start the BE if the FE is ready */
2455 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2456 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2459 /* startup must always be called for new BEs */
2460 ret = dpcm_be_dai_startup(fe, stream);
2464 /* keep going if FE state is > open */
2465 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2468 ret = dpcm_be_dai_hw_params(fe, stream);
2472 /* keep going if FE state is > hw_params */
2473 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2477 ret = dpcm_be_dai_prepare(fe, stream);
2481 /* run the stream event for each BE */
2482 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2484 /* keep going if FE state is > prepare */
2485 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2486 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2489 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2490 /* call trigger on the frontend - FE takes care of all BE triggers */
2491 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2492 fe->dai_link->name);
2494 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2496 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2500 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2501 fe->dai_link->name);
2503 ret = dpcm_be_dai_trigger(fe, stream,
2504 SNDRV_PCM_TRIGGER_START);
2506 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2514 dpcm_be_dai_hw_free(fe, stream);
2516 dpcm_be_dai_shutdown(fe, stream);
2518 /* disconnect any non started BEs */
2519 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2520 struct snd_soc_pcm_runtime *be = dpcm->be;
2521 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2522 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2528 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2532 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2533 ret = dpcm_run_update_startup(fe, stream);
2535 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2536 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2541 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2545 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2546 ret = dpcm_run_update_shutdown(fe, stream);
2548 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2549 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2554 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2557 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2559 struct snd_soc_pcm_runtime *fe;
2560 int old, new, paths;
2562 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2563 list_for_each_entry(fe, &card->rtd_list, list) {
2564 struct snd_soc_dapm_widget_list *list;
2566 /* make sure link is FE */
2567 if (!fe->dai_link->dynamic)
2570 /* only check active links */
2571 if (!fe->cpu_dai->active)
2574 /* DAPM sync will call this to update DSP paths */
2575 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2576 fe->dai_link->name);
2578 /* skip if FE doesn't have playback capability */
2579 if (!fe->cpu_dai->driver->playback.channels_min
2580 || !fe->codec_dai->driver->playback.channels_min)
2583 /* skip if FE isn't currently playing */
2584 if (!fe->cpu_dai->playback_active
2585 || !fe->codec_dai->playback_active)
2588 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2590 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2591 fe->dai_link->name, "playback");
2592 mutex_unlock(&card->mutex);
2596 /* update any new playback paths */
2597 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2599 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2600 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2601 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2604 /* update any old playback paths */
2605 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2607 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2608 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2609 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2612 dpcm_path_put(&list);
2614 /* skip if FE doesn't have capture capability */
2615 if (!fe->cpu_dai->driver->capture.channels_min
2616 || !fe->codec_dai->driver->capture.channels_min)
2619 /* skip if FE isn't currently capturing */
2620 if (!fe->cpu_dai->capture_active
2621 || !fe->codec_dai->capture_active)
2624 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2626 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2627 fe->dai_link->name, "capture");
2628 mutex_unlock(&card->mutex);
2632 /* update any new capture paths */
2633 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2635 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2636 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2637 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2640 /* update any old capture paths */
2641 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2643 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2644 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2645 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2648 dpcm_path_put(&list);
2651 mutex_unlock(&card->mutex);
2654 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2656 struct snd_soc_dpcm *dpcm;
2657 struct list_head *clients =
2658 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2660 list_for_each_entry(dpcm, clients, list_be) {
2662 struct snd_soc_pcm_runtime *be = dpcm->be;
2665 if (be->dai_link->ignore_suspend)
2668 for (i = 0; i < be->num_codecs; i++) {
2669 struct snd_soc_dai *dai = be->codec_dais[i];
2670 struct snd_soc_dai_driver *drv = dai->driver;
2672 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2673 be->dai_link->name);
2675 if (drv->ops && drv->ops->digital_mute &&
2676 dai->playback_active)
2677 drv->ops->digital_mute(dai, mute);
2684 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2686 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2687 struct snd_soc_dpcm *dpcm;
2688 struct snd_soc_dapm_widget_list *list;
2690 int stream = fe_substream->stream;
2692 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2693 fe->dpcm[stream].runtime = fe_substream->runtime;
2695 ret = dpcm_path_get(fe, stream, &list);
2697 mutex_unlock(&fe->card->mutex);
2699 } else if (ret == 0) {
2700 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2701 fe->dai_link->name, stream ? "capture" : "playback");
2704 /* calculate valid and active FE <-> BE dpcms */
2705 dpcm_process_paths(fe, stream, &list, 1);
2707 ret = dpcm_fe_dai_startup(fe_substream);
2709 /* clean up all links */
2710 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2711 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2713 dpcm_be_disconnect(fe, stream);
2714 fe->dpcm[stream].runtime = NULL;
2717 dpcm_clear_pending_state(fe, stream);
2718 dpcm_path_put(&list);
2719 mutex_unlock(&fe->card->mutex);
2723 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2725 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2726 struct snd_soc_dpcm *dpcm;
2727 int stream = fe_substream->stream, ret;
2729 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2730 ret = dpcm_fe_dai_shutdown(fe_substream);
2732 /* mark FE's links ready to prune */
2733 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2734 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2736 dpcm_be_disconnect(fe, stream);
2738 fe->dpcm[stream].runtime = NULL;
2739 mutex_unlock(&fe->card->mutex);
2743 static void soc_pcm_private_free(struct snd_pcm *pcm)
2745 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2746 struct snd_soc_rtdcom_list *rtdcom;
2747 struct snd_soc_component *component;
2749 /* need to sync the delayed work before releasing resources */
2750 flush_delayed_work(&rtd->delayed_work);
2751 for_each_rtdcom(rtd, rtdcom) {
2752 component = rtdcom->component;
2754 if (component->driver->pcm_free)
2755 component->driver->pcm_free(pcm);
2759 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2761 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2762 struct snd_soc_rtdcom_list *rtdcom;
2763 struct snd_soc_component *component;
2765 for_each_rtdcom(rtd, rtdcom) {
2766 component = rtdcom->component;
2768 if (!component->driver->ops ||
2769 !component->driver->ops->ack)
2772 /* FIXME. it returns 1st ask now */
2773 return component->driver->ops->ack(substream);
2779 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2780 unsigned long pos, void __user *buf,
2781 unsigned long bytes)
2783 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2784 struct snd_soc_rtdcom_list *rtdcom;
2785 struct snd_soc_component *component;
2787 for_each_rtdcom(rtd, rtdcom) {
2788 component = rtdcom->component;
2790 if (!component->driver->ops ||
2791 !component->driver->ops->copy_user)
2794 /* FIXME. it returns 1st copy now */
2795 return component->driver->ops->copy_user(substream, channel,
2802 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2803 unsigned long pos, void *buf, unsigned long bytes)
2805 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2806 struct snd_soc_rtdcom_list *rtdcom;
2807 struct snd_soc_component *component;
2809 for_each_rtdcom(rtd, rtdcom) {
2810 component = rtdcom->component;
2812 if (!component->driver->ops ||
2813 !component->driver->ops->copy_kernel)
2816 /* FIXME. it returns 1st copy now */
2817 return component->driver->ops->copy_kernel(substream, channel,
2824 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2825 unsigned long pos, unsigned long bytes)
2827 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2828 struct snd_soc_rtdcom_list *rtdcom;
2829 struct snd_soc_component *component;
2831 for_each_rtdcom(rtd, rtdcom) {
2832 component = rtdcom->component;
2834 if (!component->driver->ops ||
2835 !component->driver->ops->fill_silence)
2838 /* FIXME. it returns 1st silence now */
2839 return component->driver->ops->fill_silence(substream, channel,
2846 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2847 unsigned long offset)
2849 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2850 struct snd_soc_rtdcom_list *rtdcom;
2851 struct snd_soc_component *component;
2854 for_each_rtdcom(rtd, rtdcom) {
2855 component = rtdcom->component;
2857 if (!component->driver->ops ||
2858 !component->driver->ops->page)
2861 /* FIXME. it returns 1st page now */
2862 page = component->driver->ops->page(substream, offset);
2870 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2871 struct vm_area_struct *vma)
2873 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2874 struct snd_soc_rtdcom_list *rtdcom;
2875 struct snd_soc_component *component;
2877 for_each_rtdcom(rtd, rtdcom) {
2878 component = rtdcom->component;
2880 if (!component->driver->ops ||
2881 !component->driver->ops->mmap)
2884 /* FIXME. it returns 1st mmap now */
2885 return component->driver->ops->mmap(substream, vma);
2891 /* create a new pcm */
2892 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2894 struct snd_soc_dai *codec_dai;
2895 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2896 struct snd_soc_component *component;
2897 struct snd_soc_rtdcom_list *rtdcom;
2898 struct snd_pcm *pcm;
2900 int ret = 0, playback = 0, capture = 0;
2903 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2904 playback = rtd->dai_link->dpcm_playback;
2905 capture = rtd->dai_link->dpcm_capture;
2907 for (i = 0; i < rtd->num_codecs; i++) {
2908 codec_dai = rtd->codec_dais[i];
2909 if (codec_dai->driver->playback.channels_min)
2911 if (codec_dai->driver->capture.channels_min)
2915 capture = capture && cpu_dai->driver->capture.channels_min;
2916 playback = playback && cpu_dai->driver->playback.channels_min;
2919 if (rtd->dai_link->playback_only) {
2924 if (rtd->dai_link->capture_only) {
2929 /* create the PCM */
2930 if (rtd->dai_link->no_pcm) {
2931 snprintf(new_name, sizeof(new_name), "(%s)",
2932 rtd->dai_link->stream_name);
2934 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2935 playback, capture, &pcm);
2937 if (rtd->dai_link->dynamic)
2938 snprintf(new_name, sizeof(new_name), "%s (*)",
2939 rtd->dai_link->stream_name);
2941 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2942 rtd->dai_link->stream_name,
2943 (rtd->num_codecs > 1) ?
2944 "multicodec" : rtd->codec_dai->name, num);
2946 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2950 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2951 rtd->dai_link->name);
2954 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2956 /* DAPM dai link stream work */
2957 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2959 pcm->nonatomic = rtd->dai_link->nonatomic;
2961 pcm->private_data = rtd;
2963 if (rtd->dai_link->no_pcm) {
2965 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2967 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2971 /* ASoC PCM operations */
2972 if (rtd->dai_link->dynamic) {
2973 rtd->ops.open = dpcm_fe_dai_open;
2974 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2975 rtd->ops.prepare = dpcm_fe_dai_prepare;
2976 rtd->ops.trigger = dpcm_fe_dai_trigger;
2977 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2978 rtd->ops.close = dpcm_fe_dai_close;
2979 rtd->ops.pointer = soc_pcm_pointer;
2980 rtd->ops.ioctl = soc_pcm_ioctl;
2982 rtd->ops.open = soc_pcm_open;
2983 rtd->ops.hw_params = soc_pcm_hw_params;
2984 rtd->ops.prepare = soc_pcm_prepare;
2985 rtd->ops.trigger = soc_pcm_trigger;
2986 rtd->ops.hw_free = soc_pcm_hw_free;
2987 rtd->ops.close = soc_pcm_close;
2988 rtd->ops.pointer = soc_pcm_pointer;
2989 rtd->ops.ioctl = soc_pcm_ioctl;
2992 for_each_rtdcom(rtd, rtdcom) {
2993 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
2999 rtd->ops.ack = soc_rtdcom_ack;
3001 rtd->ops.copy_user = soc_rtdcom_copy_user;
3002 if (ops->copy_kernel)
3003 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3004 if (ops->fill_silence)
3005 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3007 rtd->ops.page = soc_rtdcom_page;
3009 rtd->ops.mmap = soc_rtdcom_mmap;
3013 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3016 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3018 for_each_rtdcom(rtd, rtdcom) {
3019 component = rtdcom->component;
3021 if (!component->driver->pcm_new)
3024 ret = component->driver->pcm_new(rtd);
3026 dev_err(component->dev,
3027 "ASoC: pcm constructor failed: %d\n",
3033 pcm->private_free = soc_pcm_private_free;
3035 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3036 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3041 /* is the current PCM operation for this FE ? */
3042 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3044 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3048 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3050 /* is the current PCM operation for this BE ? */
3051 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3052 struct snd_soc_pcm_runtime *be, int stream)
3054 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3055 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3056 be->dpcm[stream].runtime_update))
3060 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3062 /* get the substream for this BE */
3063 struct snd_pcm_substream *
3064 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3066 return be->pcm->streams[stream].substream;
3068 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3070 /* get the BE runtime state */
3071 enum snd_soc_dpcm_state
3072 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3074 return be->dpcm[stream].state;
3076 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3078 /* set the BE runtime state */
3079 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3080 int stream, enum snd_soc_dpcm_state state)
3082 be->dpcm[stream].state = state;
3084 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3087 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3088 * are not running, paused or suspended for the specified stream direction.
3090 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3091 struct snd_soc_pcm_runtime *be, int stream)
3093 struct snd_soc_dpcm *dpcm;
3096 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3101 state = dpcm->fe->dpcm[stream].state;
3102 if (state == SND_SOC_DPCM_STATE_START ||
3103 state == SND_SOC_DPCM_STATE_PAUSED ||
3104 state == SND_SOC_DPCM_STATE_SUSPEND)
3108 /* it's safe to free/stop this BE DAI */
3111 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3114 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3115 * running, paused or suspended for the specified stream direction.
3117 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3118 struct snd_soc_pcm_runtime *be, int stream)
3120 struct snd_soc_dpcm *dpcm;
3123 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3128 state = dpcm->fe->dpcm[stream].state;
3129 if (state == SND_SOC_DPCM_STATE_START ||
3130 state == SND_SOC_DPCM_STATE_PAUSED ||
3131 state == SND_SOC_DPCM_STATE_SUSPEND ||
3132 state == SND_SOC_DPCM_STATE_PREPARE)
3136 /* it's safe to change hw_params */
3139 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3141 #ifdef CONFIG_DEBUG_FS
3142 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3145 case SND_SOC_DPCM_STATE_NEW:
3147 case SND_SOC_DPCM_STATE_OPEN:
3149 case SND_SOC_DPCM_STATE_HW_PARAMS:
3151 case SND_SOC_DPCM_STATE_PREPARE:
3153 case SND_SOC_DPCM_STATE_START:
3155 case SND_SOC_DPCM_STATE_STOP:
3157 case SND_SOC_DPCM_STATE_SUSPEND:
3159 case SND_SOC_DPCM_STATE_PAUSED:
3161 case SND_SOC_DPCM_STATE_HW_FREE:
3163 case SND_SOC_DPCM_STATE_CLOSE:
3170 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3171 int stream, char *buf, size_t size)
3173 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3174 struct snd_soc_dpcm *dpcm;
3178 offset += snprintf(buf + offset, size - offset,
3179 "[%s - %s]\n", fe->dai_link->name,
3180 stream ? "Capture" : "Playback");
3182 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3183 dpcm_state_string(fe->dpcm[stream].state));
3185 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3186 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3187 offset += snprintf(buf + offset, size - offset,
3189 "Format = %s, Channels = %d, Rate = %d\n",
3190 snd_pcm_format_name(params_format(params)),
3191 params_channels(params),
3192 params_rate(params));
3195 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3197 if (list_empty(&fe->dpcm[stream].be_clients)) {
3198 offset += snprintf(buf + offset, size - offset,
3199 " No active DSP links\n");
3203 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3204 struct snd_soc_pcm_runtime *be = dpcm->be;
3205 params = &dpcm->hw_params;
3207 offset += snprintf(buf + offset, size - offset,
3208 "- %s\n", be->dai_link->name);
3210 offset += snprintf(buf + offset, size - offset,
3212 dpcm_state_string(be->dpcm[stream].state));
3214 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3215 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3216 offset += snprintf(buf + offset, size - offset,
3217 " Hardware Params: "
3218 "Format = %s, Channels = %d, Rate = %d\n",
3219 snd_pcm_format_name(params_format(params)),
3220 params_channels(params),
3221 params_rate(params));
3228 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3229 size_t count, loff_t *ppos)
3231 struct snd_soc_pcm_runtime *fe = file->private_data;
3232 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3235 buf = kmalloc(out_count, GFP_KERNEL);
3239 if (fe->cpu_dai->driver->playback.channels_min)
3240 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3241 buf + offset, out_count - offset);
3243 if (fe->cpu_dai->driver->capture.channels_min)
3244 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3245 buf + offset, out_count - offset);
3247 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3253 static const struct file_operations dpcm_state_fops = {
3254 .open = simple_open,
3255 .read = dpcm_state_read_file,
3256 .llseek = default_llseek,
3259 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3264 if (!rtd->card->debugfs_card_root)
3267 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3268 rtd->card->debugfs_card_root);
3269 if (!rtd->debugfs_dpcm_root) {
3271 "ASoC: Failed to create dpcm debugfs directory %s\n",
3272 rtd->dai_link->name);
3276 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3277 rtd, &dpcm_state_fops);