1 // SPDX-License-Identifier: GPL-2.0+
3 // soc-pcm.c -- ALSA SoC PCM
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
10 // Authors: Liam Girdwood <lrg@ti.com>
11 // Mark Brown <broonie@opensource.wolfsonmicro.com>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dpcm.h>
27 #include <sound/initval.h>
29 #define DPCM_MAX_BE_USERS 8
32 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
34 * Returns true if the DAI supports the indicated stream type.
36 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
38 struct snd_soc_pcm_stream *codec_stream;
40 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
41 codec_stream = &dai->driver->playback;
43 codec_stream = &dai->driver->capture;
45 /* If the codec specifies any channels at all, it supports the stream */
46 return codec_stream->channels_min;
50 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
51 * @rtd: ASoC PCM runtime that is activated
52 * @stream: Direction of the PCM stream
54 * Increments the active count for all the DAIs and components attached to a PCM
55 * runtime. Should typically be called when a stream is opened.
57 * Must be called with the rtd->pcm_mutex being held
59 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
61 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
64 lockdep_assert_held(&rtd->pcm_mutex);
66 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
67 cpu_dai->playback_active++;
68 for (i = 0; i < rtd->num_codecs; i++)
69 rtd->codec_dais[i]->playback_active++;
71 cpu_dai->capture_active++;
72 for (i = 0; i < rtd->num_codecs; i++)
73 rtd->codec_dais[i]->capture_active++;
77 cpu_dai->component->active++;
78 for (i = 0; i < rtd->num_codecs; i++) {
79 rtd->codec_dais[i]->active++;
80 rtd->codec_dais[i]->component->active++;
85 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
86 * @rtd: ASoC PCM runtime that is deactivated
87 * @stream: Direction of the PCM stream
89 * Decrements the active count for all the DAIs and components attached to a PCM
90 * runtime. Should typically be called when a stream is closed.
92 * Must be called with the rtd->pcm_mutex being held
94 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
96 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99 lockdep_assert_held(&rtd->pcm_mutex);
101 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
102 cpu_dai->playback_active--;
103 for (i = 0; i < rtd->num_codecs; i++)
104 rtd->codec_dais[i]->playback_active--;
106 cpu_dai->capture_active--;
107 for (i = 0; i < rtd->num_codecs; i++)
108 rtd->codec_dais[i]->capture_active--;
112 cpu_dai->component->active--;
113 for (i = 0; i < rtd->num_codecs; i++) {
114 rtd->codec_dais[i]->component->active--;
115 rtd->codec_dais[i]->active--;
120 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
121 * @rtd: The ASoC PCM runtime that should be checked.
123 * This function checks whether the power down delay should be ignored for a
124 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
125 * been configured to ignore the delay, or if none of the components benefits
126 * from having the delay.
128 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
130 struct snd_soc_rtdcom_list *rtdcom;
131 struct snd_soc_component *component;
134 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
137 for_each_rtdcom(rtd, rtdcom) {
138 component = rtdcom->component;
140 ignore &= !component->driver->use_pmdown_time;
147 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
148 * @substream: the pcm substream
149 * @hw: the hardware parameters
151 * Sets the substream runtime hardware parameters.
153 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
154 const struct snd_pcm_hardware *hw)
156 struct snd_pcm_runtime *runtime = substream->runtime;
157 runtime->hw.info = hw->info;
158 runtime->hw.formats = hw->formats;
159 runtime->hw.period_bytes_min = hw->period_bytes_min;
160 runtime->hw.period_bytes_max = hw->period_bytes_max;
161 runtime->hw.periods_min = hw->periods_min;
162 runtime->hw.periods_max = hw->periods_max;
163 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
164 runtime->hw.fifo_size = hw->fifo_size;
167 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
169 /* DPCM stream event, send event to FE and all active BEs. */
170 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
173 struct snd_soc_dpcm *dpcm;
175 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
177 struct snd_soc_pcm_runtime *be = dpcm->be;
179 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
180 be->dai_link->name, event, dir);
182 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
183 (be->dpcm[dir].users >= 1))
186 snd_soc_dapm_stream_event(be, dir, event);
189 snd_soc_dapm_stream_event(fe, dir, event);
194 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
195 struct snd_soc_dai *soc_dai)
197 struct snd_soc_pcm_runtime *rtd = substream->private_data;
200 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
201 rtd->dai_link->symmetric_rates)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
205 ret = snd_pcm_hw_constraint_single(substream->runtime,
206 SNDRV_PCM_HW_PARAM_RATE,
209 dev_err(soc_dai->dev,
210 "ASoC: Unable to apply rate constraint: %d\n",
216 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
217 rtd->dai_link->symmetric_channels)) {
218 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
221 ret = snd_pcm_hw_constraint_single(substream->runtime,
222 SNDRV_PCM_HW_PARAM_CHANNELS,
225 dev_err(soc_dai->dev,
226 "ASoC: Unable to apply channel symmetry constraint: %d\n",
232 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
233 rtd->dai_link->symmetric_samplebits)) {
234 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
235 soc_dai->sample_bits);
237 ret = snd_pcm_hw_constraint_single(substream->runtime,
238 SNDRV_PCM_HW_PARAM_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);
445 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
446 struct snd_soc_component *last)
448 struct snd_soc_pcm_runtime *rtd = substream->private_data;
449 struct snd_soc_rtdcom_list *rtdcom;
450 struct snd_soc_component *component;
452 for_each_rtdcom(rtd, rtdcom) {
453 component = rtdcom->component;
455 if (component == last)
458 if (!component->driver->ops ||
459 !component->driver->ops->close)
462 component->driver->ops->close(substream);
469 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
470 * then initialized and any private data can be allocated. This also calls
471 * startup for the cpu DAI, component, machine and codec DAI.
473 static int soc_pcm_open(struct snd_pcm_substream *substream)
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_pcm_runtime *runtime = substream->runtime;
477 struct snd_soc_component *component;
478 struct snd_soc_rtdcom_list *rtdcom;
479 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
480 struct snd_soc_dai *codec_dai;
481 const char *codec_dai_name = "multicodec";
484 pinctrl_pm_select_default_state(cpu_dai->dev);
485 for (i = 0; i < rtd->num_codecs; i++)
486 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
488 for_each_rtdcom(rtd, rtdcom) {
489 component = rtdcom->component;
491 pm_runtime_get_sync(component->dev);
494 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
496 /* startup the audio subsystem */
497 if (cpu_dai->driver->ops->startup) {
498 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
500 dev_err(cpu_dai->dev, "ASoC: can't open interface"
501 " %s: %d\n", cpu_dai->name, ret);
506 for_each_rtdcom(rtd, rtdcom) {
507 component = rtdcom->component;
509 if (!component->driver->ops ||
510 !component->driver->ops->open)
513 ret = component->driver->ops->open(substream);
515 dev_err(component->dev,
516 "ASoC: can't open component %s: %d\n",
517 component->name, ret);
523 for (i = 0; i < rtd->num_codecs; i++) {
524 codec_dai = rtd->codec_dais[i];
525 if (codec_dai->driver->ops->startup) {
526 ret = codec_dai->driver->ops->startup(substream,
529 dev_err(codec_dai->dev,
530 "ASoC: can't open codec %s: %d\n",
531 codec_dai->name, ret);
536 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
537 codec_dai->tx_mask = 0;
539 codec_dai->rx_mask = 0;
542 if (rtd->dai_link->ops->startup) {
543 ret = rtd->dai_link->ops->startup(substream);
545 pr_err("ASoC: %s startup failed: %d\n",
546 rtd->dai_link->name, ret);
551 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
552 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
555 /* Check that the codec and cpu DAIs are compatible */
556 soc_pcm_init_runtime_hw(substream);
558 if (rtd->num_codecs == 1)
559 codec_dai_name = rtd->codec_dai->name;
561 if (soc_pcm_has_symmetry(substream))
562 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
565 if (!runtime->hw.rates) {
566 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
567 codec_dai_name, cpu_dai->name);
570 if (!runtime->hw.formats) {
571 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
572 codec_dai_name, cpu_dai->name);
575 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
576 runtime->hw.channels_min > runtime->hw.channels_max) {
577 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
578 codec_dai_name, cpu_dai->name);
582 soc_pcm_apply_msb(substream);
584 /* Symmetry only applies if we've already got an active stream. */
585 if (cpu_dai->active) {
586 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
591 for (i = 0; i < rtd->num_codecs; i++) {
592 if (rtd->codec_dais[i]->active) {
593 ret = soc_pcm_apply_symmetry(substream,
600 pr_debug("ASoC: %s <-> %s info:\n",
601 codec_dai_name, cpu_dai->name);
602 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
603 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
604 runtime->hw.channels_max);
605 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
606 runtime->hw.rate_max);
610 snd_soc_runtime_activate(rtd, substream->stream);
612 mutex_unlock(&rtd->pcm_mutex);
616 if (rtd->dai_link->ops->shutdown)
617 rtd->dai_link->ops->shutdown(substream);
624 codec_dai = rtd->codec_dais[i];
625 if (codec_dai->driver->ops->shutdown)
626 codec_dai->driver->ops->shutdown(substream, codec_dai);
630 soc_pcm_components_close(substream, component);
632 if (cpu_dai->driver->ops->shutdown)
633 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
635 mutex_unlock(&rtd->pcm_mutex);
637 for_each_rtdcom(rtd, rtdcom) {
638 component = rtdcom->component;
640 pm_runtime_mark_last_busy(component->dev);
641 pm_runtime_put_autosuspend(component->dev);
644 for (i = 0; i < rtd->num_codecs; i++) {
645 if (!rtd->codec_dais[i]->active)
646 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
648 if (!cpu_dai->active)
649 pinctrl_pm_select_sleep_state(cpu_dai->dev);
655 * Power down the audio subsystem pmdown_time msecs after close is called.
656 * This is to ensure there are no pops or clicks in between any music tracks
657 * due to DAPM power cycling.
659 static void close_delayed_work(struct work_struct *work)
661 struct snd_soc_pcm_runtime *rtd =
662 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
663 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
665 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
667 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
668 codec_dai->driver->playback.stream_name,
669 codec_dai->playback_active ? "active" : "inactive",
670 rtd->pop_wait ? "yes" : "no");
672 /* are we waiting on this codec DAI stream */
673 if (rtd->pop_wait == 1) {
675 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
676 SND_SOC_DAPM_STREAM_STOP);
679 mutex_unlock(&rtd->pcm_mutex);
683 * Called by ALSA when a PCM substream is closed. Private data can be
684 * freed here. The cpu DAI, codec DAI, machine and components are also
687 static int soc_pcm_close(struct snd_pcm_substream *substream)
689 struct snd_soc_pcm_runtime *rtd = substream->private_data;
690 struct snd_soc_component *component;
691 struct snd_soc_rtdcom_list *rtdcom;
692 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
693 struct snd_soc_dai *codec_dai;
696 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
698 snd_soc_runtime_deactivate(rtd, substream->stream);
700 /* clear the corresponding DAIs rate when inactive */
701 if (!cpu_dai->active)
704 for (i = 0; i < rtd->num_codecs; i++) {
705 codec_dai = rtd->codec_dais[i];
706 if (!codec_dai->active)
710 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
712 if (cpu_dai->driver->ops->shutdown)
713 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
715 for (i = 0; i < rtd->num_codecs; i++) {
716 codec_dai = rtd->codec_dais[i];
717 if (codec_dai->driver->ops->shutdown)
718 codec_dai->driver->ops->shutdown(substream, codec_dai);
721 if (rtd->dai_link->ops->shutdown)
722 rtd->dai_link->ops->shutdown(substream);
724 soc_pcm_components_close(substream, NULL);
726 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
727 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
728 /* powered down playback stream now */
729 snd_soc_dapm_stream_event(rtd,
730 SNDRV_PCM_STREAM_PLAYBACK,
731 SND_SOC_DAPM_STREAM_STOP);
733 /* start delayed pop wq here for playback streams */
735 queue_delayed_work(system_power_efficient_wq,
737 msecs_to_jiffies(rtd->pmdown_time));
740 /* capture streams can be powered down now */
741 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
742 SND_SOC_DAPM_STREAM_STOP);
745 mutex_unlock(&rtd->pcm_mutex);
747 for_each_rtdcom(rtd, rtdcom) {
748 component = rtdcom->component;
750 pm_runtime_mark_last_busy(component->dev);
751 pm_runtime_put_autosuspend(component->dev);
754 for (i = 0; i < rtd->num_codecs; i++) {
755 if (!rtd->codec_dais[i]->active)
756 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
758 if (!cpu_dai->active)
759 pinctrl_pm_select_sleep_state(cpu_dai->dev);
765 * Called by ALSA when the PCM substream is prepared, can set format, sample
766 * rate, etc. This function is non atomic and can be called multiple times,
767 * it can refer to the runtime info.
769 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
771 struct snd_soc_pcm_runtime *rtd = substream->private_data;
772 struct snd_soc_component *component;
773 struct snd_soc_rtdcom_list *rtdcom;
774 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
775 struct snd_soc_dai *codec_dai;
778 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
780 if (rtd->dai_link->ops->prepare) {
781 ret = rtd->dai_link->ops->prepare(substream);
783 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
789 for_each_rtdcom(rtd, rtdcom) {
790 component = rtdcom->component;
792 if (!component->driver->ops ||
793 !component->driver->ops->prepare)
796 ret = component->driver->ops->prepare(substream);
798 dev_err(component->dev,
799 "ASoC: platform prepare error: %d\n", ret);
804 for (i = 0; i < rtd->num_codecs; i++) {
805 codec_dai = rtd->codec_dais[i];
806 if (codec_dai->driver->ops->prepare) {
807 ret = codec_dai->driver->ops->prepare(substream,
810 dev_err(codec_dai->dev,
811 "ASoC: codec DAI prepare error: %d\n",
818 if (cpu_dai->driver->ops->prepare) {
819 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
821 dev_err(cpu_dai->dev,
822 "ASoC: cpu DAI prepare error: %d\n", ret);
827 /* cancel any delayed stream shutdown that is pending */
828 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
831 cancel_delayed_work(&rtd->delayed_work);
834 snd_soc_dapm_stream_event(rtd, substream->stream,
835 SND_SOC_DAPM_STREAM_START);
837 for (i = 0; i < rtd->num_codecs; i++)
838 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
840 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
843 mutex_unlock(&rtd->pcm_mutex);
847 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
850 struct snd_interval *interval;
851 int channels = hweight_long(mask);
853 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
854 interval->min = channels;
855 interval->max = channels;
858 int soc_dai_hw_params(struct snd_pcm_substream *substream,
859 struct snd_pcm_hw_params *params,
860 struct snd_soc_dai *dai)
862 struct snd_soc_pcm_runtime *rtd = substream->private_data;
865 /* perform any topology hw_params fixups before DAI */
866 if (rtd->dai_link->be_hw_params_fixup) {
867 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
870 "ASoC: hw_params topology fixup failed %d\n",
876 if (dai->driver->ops->hw_params) {
877 ret = dai->driver->ops->hw_params(substream, params, dai);
879 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
888 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
889 struct snd_soc_component *last)
891 struct snd_soc_pcm_runtime *rtd = substream->private_data;
892 struct snd_soc_rtdcom_list *rtdcom;
893 struct snd_soc_component *component;
895 for_each_rtdcom(rtd, rtdcom) {
896 component = rtdcom->component;
898 if (component == last)
901 if (!component->driver->ops ||
902 !component->driver->ops->hw_free)
905 component->driver->ops->hw_free(substream);
912 * Called by ALSA when the hardware params are set by application. This
913 * function can also be called multiple times and can allocate buffers
914 * (using snd_pcm_lib_* ). It's non-atomic.
916 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
917 struct snd_pcm_hw_params *params)
919 struct snd_soc_pcm_runtime *rtd = substream->private_data;
920 struct snd_soc_component *component;
921 struct snd_soc_rtdcom_list *rtdcom;
922 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
925 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
926 if (rtd->dai_link->ops->hw_params) {
927 ret = rtd->dai_link->ops->hw_params(substream, params);
929 dev_err(rtd->card->dev, "ASoC: machine hw_params"
930 " failed: %d\n", ret);
935 for (i = 0; i < rtd->num_codecs; i++) {
936 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
937 struct snd_pcm_hw_params codec_params;
940 * Skip CODECs which don't support the current stream type,
941 * the idea being that if a CODEC is not used for the currently
942 * set up transfer direction, it should not need to be
943 * configured, especially since the configuration used might
944 * not even be supported by that CODEC. There may be cases
945 * however where a CODEC needs to be set up although it is
946 * actually not being used for the transfer, e.g. if a
947 * capture-only CODEC is acting as an LRCLK and/or BCLK master
948 * for the DAI link including a playback-only CODEC.
949 * If this becomes necessary, we will have to augment the
950 * machine driver setup with information on how to act, so
951 * we can do the right thing here.
953 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
956 /* copy params for each codec */
957 codec_params = *params;
959 /* fixup params based on TDM slot masks */
960 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
962 soc_pcm_codec_params_fixup(&codec_params,
965 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
967 soc_pcm_codec_params_fixup(&codec_params,
970 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
974 codec_dai->rate = params_rate(&codec_params);
975 codec_dai->channels = params_channels(&codec_params);
976 codec_dai->sample_bits = snd_pcm_format_physical_width(
977 params_format(&codec_params));
980 ret = soc_dai_hw_params(substream, params, cpu_dai);
984 for_each_rtdcom(rtd, rtdcom) {
985 component = rtdcom->component;
987 if (!component->driver->ops ||
988 !component->driver->ops->hw_params)
991 ret = component->driver->ops->hw_params(substream, params);
993 dev_err(component->dev,
994 "ASoC: %s hw params failed: %d\n",
995 component->name, ret);
1001 /* store the parameters for each DAIs */
1002 cpu_dai->rate = params_rate(params);
1003 cpu_dai->channels = params_channels(params);
1004 cpu_dai->sample_bits =
1005 snd_pcm_format_physical_width(params_format(params));
1007 ret = soc_pcm_params_symmetry(substream, params);
1011 mutex_unlock(&rtd->pcm_mutex);
1015 soc_pcm_components_hw_free(substream, component);
1017 if (cpu_dai->driver->ops->hw_free)
1018 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1021 i = rtd->num_codecs;
1025 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1026 if (codec_dai->driver->ops->hw_free)
1027 codec_dai->driver->ops->hw_free(substream, codec_dai);
1028 codec_dai->rate = 0;
1031 if (rtd->dai_link->ops->hw_free)
1032 rtd->dai_link->ops->hw_free(substream);
1034 mutex_unlock(&rtd->pcm_mutex);
1039 * Frees resources allocated by hw_params, can be called multiple times
1041 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1043 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1044 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1045 struct snd_soc_dai *codec_dai;
1046 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1049 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1051 /* clear the corresponding DAIs parameters when going to be inactive */
1052 if (cpu_dai->active == 1) {
1054 cpu_dai->channels = 0;
1055 cpu_dai->sample_bits = 0;
1058 for (i = 0; i < rtd->num_codecs; i++) {
1059 codec_dai = rtd->codec_dais[i];
1060 if (codec_dai->active == 1) {
1061 codec_dai->rate = 0;
1062 codec_dai->channels = 0;
1063 codec_dai->sample_bits = 0;
1067 /* apply codec digital mute */
1068 for (i = 0; i < rtd->num_codecs; i++) {
1069 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1070 (!playback && rtd->codec_dais[i]->capture_active == 1))
1071 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1075 /* free any machine hw params */
1076 if (rtd->dai_link->ops->hw_free)
1077 rtd->dai_link->ops->hw_free(substream);
1079 /* free any component resources */
1080 soc_pcm_components_hw_free(substream, NULL);
1082 /* now free hw params for the DAIs */
1083 for (i = 0; i < rtd->num_codecs; i++) {
1084 codec_dai = rtd->codec_dais[i];
1085 if (codec_dai->driver->ops->hw_free)
1086 codec_dai->driver->ops->hw_free(substream, codec_dai);
1089 if (cpu_dai->driver->ops->hw_free)
1090 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1092 mutex_unlock(&rtd->pcm_mutex);
1096 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1098 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1099 struct snd_soc_component *component;
1100 struct snd_soc_rtdcom_list *rtdcom;
1101 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1102 struct snd_soc_dai *codec_dai;
1105 for (i = 0; i < rtd->num_codecs; i++) {
1106 codec_dai = rtd->codec_dais[i];
1107 if (codec_dai->driver->ops->trigger) {
1108 ret = codec_dai->driver->ops->trigger(substream,
1115 for_each_rtdcom(rtd, rtdcom) {
1116 component = rtdcom->component;
1118 if (!component->driver->ops ||
1119 !component->driver->ops->trigger)
1122 ret = component->driver->ops->trigger(substream, cmd);
1127 if (cpu_dai->driver->ops->trigger) {
1128 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1133 if (rtd->dai_link->ops->trigger) {
1134 ret = rtd->dai_link->ops->trigger(substream, cmd);
1142 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1146 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1147 struct snd_soc_dai *codec_dai;
1150 for (i = 0; i < rtd->num_codecs; i++) {
1151 codec_dai = rtd->codec_dais[i];
1152 if (codec_dai->driver->ops->bespoke_trigger) {
1153 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1160 if (cpu_dai->driver->ops->bespoke_trigger) {
1161 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1168 * soc level wrapper for pointer callback
1169 * If cpu_dai, codec_dai, component driver has the delay callback, then
1170 * the runtime->delay will be updated accordingly.
1172 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1174 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1175 struct snd_soc_component *component;
1176 struct snd_soc_rtdcom_list *rtdcom;
1177 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1178 struct snd_soc_dai *codec_dai;
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 snd_pcm_uframes_t offset = 0;
1181 snd_pcm_sframes_t delay = 0;
1182 snd_pcm_sframes_t codec_delay = 0;
1185 /* clearing the previous total delay */
1188 for_each_rtdcom(rtd, rtdcom) {
1189 component = rtdcom->component;
1191 if (!component->driver->ops ||
1192 !component->driver->ops->pointer)
1195 /* FIXME: use 1st pointer */
1196 offset = component->driver->ops->pointer(substream);
1199 /* base delay if assigned in pointer callback */
1200 delay = runtime->delay;
1202 if (cpu_dai->driver->ops->delay)
1203 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1205 for (i = 0; i < rtd->num_codecs; i++) {
1206 codec_dai = rtd->codec_dais[i];
1207 if (codec_dai->driver->ops->delay)
1208 codec_delay = max(codec_delay,
1209 codec_dai->driver->ops->delay(substream,
1212 delay += codec_delay;
1214 runtime->delay = delay;
1219 /* connect a FE and BE */
1220 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1221 struct snd_soc_pcm_runtime *be, int stream)
1223 struct snd_soc_dpcm *dpcm;
1224 unsigned long flags;
1226 /* only add new dpcms */
1227 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1228 if (dpcm->be == be && dpcm->fe == fe)
1232 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1238 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1239 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1240 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1241 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1242 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1243 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1245 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1246 stream ? "capture" : "playback", fe->dai_link->name,
1247 stream ? "<-" : "->", be->dai_link->name);
1249 #ifdef CONFIG_DEBUG_FS
1250 if (fe->debugfs_dpcm_root)
1251 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1252 fe->debugfs_dpcm_root, &dpcm->state);
1257 /* reparent a BE onto another FE */
1258 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1259 struct snd_soc_pcm_runtime *be, int stream)
1261 struct snd_soc_dpcm *dpcm;
1262 struct snd_pcm_substream *fe_substream, *be_substream;
1264 /* reparent if BE is connected to other FEs */
1265 if (!be->dpcm[stream].users)
1268 be_substream = snd_soc_dpcm_get_substream(be, stream);
1272 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1276 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1277 stream ? "capture" : "playback",
1278 dpcm->fe->dai_link->name,
1279 stream ? "<-" : "->", dpcm->be->dai_link->name);
1281 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1282 be_substream->runtime = fe_substream->runtime;
1287 /* disconnect a BE and FE */
1288 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1290 struct snd_soc_dpcm *dpcm, *d;
1291 unsigned long flags;
1293 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1294 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1295 stream ? "capture" : "playback",
1296 dpcm->be->dai_link->name);
1298 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1301 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1302 stream ? "capture" : "playback", fe->dai_link->name,
1303 stream ? "<-" : "->", dpcm->be->dai_link->name);
1305 /* BEs still alive need new FE */
1306 dpcm_be_reparent(fe, dpcm->be, stream);
1308 #ifdef CONFIG_DEBUG_FS
1309 debugfs_remove(dpcm->debugfs_state);
1311 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1312 list_del(&dpcm->list_be);
1313 list_del(&dpcm->list_fe);
1314 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1319 /* get BE for DAI widget and stream */
1320 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1321 struct snd_soc_dapm_widget *widget, int stream)
1323 struct snd_soc_pcm_runtime *be;
1326 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1328 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1329 list_for_each_entry(be, &card->rtd_list, list) {
1331 if (!be->dai_link->no_pcm)
1334 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1335 be->cpu_dai->playback_widget ?
1336 be->cpu_dai->playback_widget->name : "(not set)");
1338 if (be->cpu_dai->playback_widget == widget)
1341 for (i = 0; i < be->num_codecs; i++) {
1342 struct snd_soc_dai *dai = be->codec_dais[i];
1343 if (dai->playback_widget == widget)
1349 list_for_each_entry(be, &card->rtd_list, list) {
1351 if (!be->dai_link->no_pcm)
1354 dev_dbg(card->dev, "ASoC: try BE %s\n",
1355 be->cpu_dai->capture_widget ?
1356 be->cpu_dai->capture_widget->name : "(not set)");
1358 if (be->cpu_dai->capture_widget == widget)
1361 for (i = 0; i < be->num_codecs; i++) {
1362 struct snd_soc_dai *dai = be->codec_dais[i];
1363 if (dai->capture_widget == widget)
1369 /* dai link name and stream name set correctly ? */
1370 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1371 stream ? "capture" : "playback", widget->name);
1375 static inline struct snd_soc_dapm_widget *
1376 dai_get_widget(struct snd_soc_dai *dai, int stream)
1378 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1379 return dai->playback_widget;
1381 return dai->capture_widget;
1384 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1385 struct snd_soc_dapm_widget *widget)
1389 for (i = 0; i < list->num_widgets; i++) {
1390 if (widget == list->widgets[i])
1397 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1398 enum snd_soc_dapm_direction dir)
1400 struct snd_soc_card *card = widget->dapm->card;
1401 struct snd_soc_pcm_runtime *rtd;
1404 if (dir == SND_SOC_DAPM_DIR_OUT) {
1405 list_for_each_entry(rtd, &card->rtd_list, list) {
1406 if (!rtd->dai_link->no_pcm)
1409 if (rtd->cpu_dai->playback_widget == widget)
1412 for (i = 0; i < rtd->num_codecs; ++i) {
1413 struct snd_soc_dai *dai = rtd->codec_dais[i];
1414 if (dai->playback_widget == widget)
1418 } else { /* SND_SOC_DAPM_DIR_IN */
1419 list_for_each_entry(rtd, &card->rtd_list, list) {
1420 if (!rtd->dai_link->no_pcm)
1423 if (rtd->cpu_dai->capture_widget == widget)
1426 for (i = 0; i < rtd->num_codecs; ++i) {
1427 struct snd_soc_dai *dai = rtd->codec_dais[i];
1428 if (dai->capture_widget == widget)
1437 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1438 int stream, struct snd_soc_dapm_widget_list **list)
1440 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1443 /* get number of valid DAI paths and their widgets */
1444 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1445 dpcm_end_walk_at_be);
1447 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1448 stream ? "capture" : "playback");
1453 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1454 struct snd_soc_dapm_widget_list **list_)
1456 struct snd_soc_dpcm *dpcm;
1457 struct snd_soc_dapm_widget_list *list = *list_;
1458 struct snd_soc_dapm_widget *widget;
1461 /* Destroy any old FE <--> BE connections */
1462 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1465 /* is there a valid CPU DAI widget for this BE */
1466 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1468 /* prune the BE if it's no longer in our active list */
1469 if (widget && widget_in_list(list, widget))
1472 /* is there a valid CODEC DAI widget for this BE */
1473 for (i = 0; i < dpcm->be->num_codecs; i++) {
1474 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1475 widget = dai_get_widget(dai, stream);
1477 /* prune the BE if it's no longer in our active list */
1478 if (widget && widget_in_list(list, widget))
1482 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1483 stream ? "capture" : "playback",
1484 dpcm->be->dai_link->name, fe->dai_link->name);
1485 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1486 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1490 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1494 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1495 struct snd_soc_dapm_widget_list **list_)
1497 struct snd_soc_card *card = fe->card;
1498 struct snd_soc_dapm_widget_list *list = *list_;
1499 struct snd_soc_pcm_runtime *be;
1500 int i, new = 0, err;
1502 /* Create any new FE <--> BE connections */
1503 for (i = 0; i < list->num_widgets; i++) {
1505 switch (list->widgets[i]->id) {
1506 case snd_soc_dapm_dai_in:
1507 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1510 case snd_soc_dapm_dai_out:
1511 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1518 /* is there a valid BE rtd for this widget */
1519 be = dpcm_get_be(card, list->widgets[i], stream);
1521 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1522 list->widgets[i]->name);
1526 /* make sure BE is a real BE */
1527 if (!be->dai_link->no_pcm)
1530 /* don't connect if FE is not running */
1531 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1534 /* newly connected FE and BE */
1535 err = dpcm_be_connect(fe, be, stream);
1537 dev_err(fe->dev, "ASoC: can't connect %s\n",
1538 list->widgets[i]->name);
1540 } else if (err == 0) /* already connected */
1544 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1548 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1553 * Find the corresponding BE DAIs that source or sink audio to this
1556 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1557 int stream, struct snd_soc_dapm_widget_list **list, int new)
1560 return dpcm_add_paths(fe, stream, list);
1562 return dpcm_prune_paths(fe, stream, list);
1565 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1567 struct snd_soc_dpcm *dpcm;
1568 unsigned long flags;
1570 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1571 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1572 dpcm->be->dpcm[stream].runtime_update =
1573 SND_SOC_DPCM_UPDATE_NO;
1574 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1577 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1580 struct snd_soc_dpcm *dpcm;
1582 /* disable any enabled and non active backends */
1583 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1585 struct snd_soc_pcm_runtime *be = dpcm->be;
1586 struct snd_pcm_substream *be_substream =
1587 snd_soc_dpcm_get_substream(be, stream);
1589 if (be->dpcm[stream].users == 0)
1590 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1591 stream ? "capture" : "playback",
1592 be->dpcm[stream].state);
1594 if (--be->dpcm[stream].users != 0)
1597 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1600 soc_pcm_close(be_substream);
1601 be_substream->runtime = NULL;
1602 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1606 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1608 struct snd_soc_dpcm *dpcm;
1611 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1612 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1614 struct snd_soc_pcm_runtime *be = dpcm->be;
1615 struct snd_pcm_substream *be_substream =
1616 snd_soc_dpcm_get_substream(be, stream);
1618 if (!be_substream) {
1619 dev_err(be->dev, "ASoC: no backend %s stream\n",
1620 stream ? "capture" : "playback");
1624 /* is this op for this BE ? */
1625 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1628 /* first time the dpcm is open ? */
1629 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1630 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1631 stream ? "capture" : "playback",
1632 be->dpcm[stream].state);
1634 if (be->dpcm[stream].users++ != 0)
1637 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1638 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1641 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1642 stream ? "capture" : "playback", be->dai_link->name);
1644 be_substream->runtime = be->dpcm[stream].runtime;
1645 err = soc_pcm_open(be_substream);
1647 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1648 be->dpcm[stream].users--;
1649 if (be->dpcm[stream].users < 0)
1650 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1651 stream ? "capture" : "playback",
1652 be->dpcm[stream].state);
1654 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1658 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1665 /* disable any enabled and non active backends */
1666 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1667 struct snd_soc_pcm_runtime *be = dpcm->be;
1668 struct snd_pcm_substream *be_substream =
1669 snd_soc_dpcm_get_substream(be, stream);
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 %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_OPEN)
1685 soc_pcm_close(be_substream);
1686 be_substream->runtime = NULL;
1687 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1693 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1694 struct snd_soc_pcm_stream *stream)
1696 runtime->hw.rate_min = stream->rate_min;
1697 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1698 runtime->hw.channels_min = stream->channels_min;
1699 runtime->hw.channels_max = stream->channels_max;
1700 if (runtime->hw.formats)
1701 runtime->hw.formats &= stream->formats;
1703 runtime->hw.formats = stream->formats;
1704 runtime->hw.rates = stream->rates;
1707 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1710 struct snd_soc_pcm_runtime *fe = substream->private_data;
1711 struct snd_soc_dpcm *dpcm;
1712 int stream = substream->stream;
1714 if (!fe->dai_link->dpcm_merged_format)
1718 * It returns merged BE codec format
1719 * if FE want to use it (= dpcm_merged_format)
1722 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1723 struct snd_soc_pcm_runtime *be = dpcm->be;
1724 struct snd_soc_dai_driver *codec_dai_drv;
1725 struct snd_soc_pcm_stream *codec_stream;
1728 for (i = 0; i < be->num_codecs; i++) {
1730 * Skip CODECs which don't support the current stream
1731 * type. See soc_pcm_init_runtime_hw() for more details
1733 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1737 codec_dai_drv = be->codec_dais[i]->driver;
1738 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1739 codec_stream = &codec_dai_drv->playback;
1741 codec_stream = &codec_dai_drv->capture;
1743 *formats &= codec_stream->formats;
1748 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1749 unsigned int *channels_min,
1750 unsigned int *channels_max)
1752 struct snd_soc_pcm_runtime *fe = substream->private_data;
1753 struct snd_soc_dpcm *dpcm;
1754 int stream = substream->stream;
1756 if (!fe->dai_link->dpcm_merged_chan)
1760 * It returns merged BE codec channel;
1761 * if FE want to use it (= dpcm_merged_chan)
1764 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1765 struct snd_soc_pcm_runtime *be = dpcm->be;
1766 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1767 struct snd_soc_dai_driver *codec_dai_drv;
1768 struct snd_soc_pcm_stream *codec_stream;
1769 struct snd_soc_pcm_stream *cpu_stream;
1771 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1772 cpu_stream = &cpu_dai_drv->playback;
1774 cpu_stream = &cpu_dai_drv->capture;
1776 *channels_min = max(*channels_min, cpu_stream->channels_min);
1777 *channels_max = min(*channels_max, cpu_stream->channels_max);
1780 * chan min/max cannot be enforced if there are multiple CODEC
1781 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1783 if (be->num_codecs == 1) {
1784 codec_dai_drv = be->codec_dais[0]->driver;
1786 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1787 codec_stream = &codec_dai_drv->playback;
1789 codec_stream = &codec_dai_drv->capture;
1791 *channels_min = max(*channels_min,
1792 codec_stream->channels_min);
1793 *channels_max = min(*channels_max,
1794 codec_stream->channels_max);
1799 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1800 unsigned int *rates,
1801 unsigned int *rate_min,
1802 unsigned int *rate_max)
1804 struct snd_soc_pcm_runtime *fe = substream->private_data;
1805 struct snd_soc_dpcm *dpcm;
1806 int stream = substream->stream;
1808 if (!fe->dai_link->dpcm_merged_rate)
1812 * It returns merged BE codec channel;
1813 * if FE want to use it (= dpcm_merged_chan)
1816 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1817 struct snd_soc_pcm_runtime *be = dpcm->be;
1818 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1819 struct snd_soc_dai_driver *codec_dai_drv;
1820 struct snd_soc_pcm_stream *codec_stream;
1821 struct snd_soc_pcm_stream *cpu_stream;
1824 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1825 cpu_stream = &cpu_dai_drv->playback;
1827 cpu_stream = &cpu_dai_drv->capture;
1829 *rate_min = max(*rate_min, cpu_stream->rate_min);
1830 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1831 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1833 for (i = 0; i < be->num_codecs; i++) {
1835 * Skip CODECs which don't support the current stream
1836 * type. See soc_pcm_init_runtime_hw() for more details
1838 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1842 codec_dai_drv = be->codec_dais[i]->driver;
1843 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1844 codec_stream = &codec_dai_drv->playback;
1846 codec_stream = &codec_dai_drv->capture;
1848 *rate_min = max(*rate_min, codec_stream->rate_min);
1849 *rate_max = min_not_zero(*rate_max,
1850 codec_stream->rate_max);
1851 *rates = snd_pcm_rate_mask_intersect(*rates,
1852 codec_stream->rates);
1857 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1859 struct snd_pcm_runtime *runtime = substream->runtime;
1860 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1861 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1862 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1864 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1865 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1867 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1869 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1870 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1871 &runtime->hw.channels_max);
1872 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1873 &runtime->hw.rate_min, &runtime->hw.rate_max);
1876 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1878 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1879 * for avoiding the race with trigger callback.
1880 * If the state is unset and a trigger is pending while the previous operation,
1881 * process the pending trigger action here.
1883 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1884 int stream, enum snd_soc_dpcm_update state)
1886 struct snd_pcm_substream *substream =
1887 snd_soc_dpcm_get_substream(fe, stream);
1889 snd_pcm_stream_lock_irq(substream);
1890 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1891 dpcm_fe_dai_do_trigger(substream,
1892 fe->dpcm[stream].trigger_pending - 1);
1893 fe->dpcm[stream].trigger_pending = 0;
1895 fe->dpcm[stream].runtime_update = state;
1896 snd_pcm_stream_unlock_irq(substream);
1899 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1902 struct snd_soc_dpcm *dpcm;
1903 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1904 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1907 /* apply symmetry for FE */
1908 if (soc_pcm_has_symmetry(fe_substream))
1909 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1911 /* Symmetry only applies if we've got an active stream. */
1912 if (fe_cpu_dai->active) {
1913 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1918 /* apply symmetry for BE */
1919 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1920 struct snd_soc_pcm_runtime *be = dpcm->be;
1921 struct snd_pcm_substream *be_substream =
1922 snd_soc_dpcm_get_substream(be, stream);
1923 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1926 if (rtd->dai_link->be_hw_params_fixup)
1929 if (soc_pcm_has_symmetry(be_substream))
1930 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1932 /* Symmetry only applies if we've got an active stream. */
1933 if (rtd->cpu_dai->active) {
1934 err = soc_pcm_apply_symmetry(fe_substream,
1940 for (i = 0; i < rtd->num_codecs; i++) {
1941 if (rtd->codec_dais[i]->active) {
1942 err = soc_pcm_apply_symmetry(fe_substream,
1943 rtd->codec_dais[i]);
1953 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1955 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1956 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1957 int stream = fe_substream->stream, ret = 0;
1959 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1961 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1963 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1967 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1969 /* start the DAI frontend */
1970 ret = soc_pcm_open(fe_substream);
1972 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1976 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1978 dpcm_set_fe_runtime(fe_substream);
1979 snd_pcm_limit_hw_rates(runtime);
1981 ret = dpcm_apply_symmetry(fe_substream, stream);
1983 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1988 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1992 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1994 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1998 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2000 struct snd_soc_dpcm *dpcm;
2002 /* only shutdown BEs that are either sinks or sources to this FE DAI */
2003 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2005 struct snd_soc_pcm_runtime *be = dpcm->be;
2006 struct snd_pcm_substream *be_substream =
2007 snd_soc_dpcm_get_substream(be, stream);
2009 /* is this op for this BE ? */
2010 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2013 if (be->dpcm[stream].users == 0)
2014 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2015 stream ? "capture" : "playback",
2016 be->dpcm[stream].state);
2018 if (--be->dpcm[stream].users != 0)
2021 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2022 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2023 soc_pcm_hw_free(be_substream);
2024 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2027 dev_dbg(be->dev, "ASoC: close BE %s\n",
2028 be->dai_link->name);
2030 soc_pcm_close(be_substream);
2031 be_substream->runtime = NULL;
2033 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2038 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2040 struct snd_soc_pcm_runtime *fe = substream->private_data;
2041 int stream = substream->stream;
2043 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2045 /* shutdown the BEs */
2046 dpcm_be_dai_shutdown(fe, substream->stream);
2048 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2050 /* now shutdown the frontend */
2051 soc_pcm_close(substream);
2053 /* run the stream event for each BE */
2054 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2056 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2057 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2061 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2063 struct snd_soc_dpcm *dpcm;
2065 /* only hw_params backends that are either sinks or sources
2066 * to this frontend DAI */
2067 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2069 struct snd_soc_pcm_runtime *be = dpcm->be;
2070 struct snd_pcm_substream *be_substream =
2071 snd_soc_dpcm_get_substream(be, stream);
2073 /* is this op for this BE ? */
2074 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2077 /* only free hw when no longer used - check all FEs */
2078 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2081 /* do not free hw if this BE is used by other FE */
2082 if (be->dpcm[stream].users > 1)
2085 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2086 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2087 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2088 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2089 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2090 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2093 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2094 be->dai_link->name);
2096 soc_pcm_hw_free(be_substream);
2098 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2104 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2106 struct snd_soc_pcm_runtime *fe = substream->private_data;
2107 int err, stream = substream->stream;
2109 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2110 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2112 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2114 /* call hw_free on the frontend */
2115 err = soc_pcm_hw_free(substream);
2117 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2118 fe->dai_link->name);
2120 /* only hw_params backends that are either sinks or sources
2121 * to this frontend DAI */
2122 err = dpcm_be_dai_hw_free(fe, stream);
2124 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2125 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2127 mutex_unlock(&fe->card->mutex);
2131 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2133 struct snd_soc_dpcm *dpcm;
2136 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2138 struct snd_soc_pcm_runtime *be = dpcm->be;
2139 struct snd_pcm_substream *be_substream =
2140 snd_soc_dpcm_get_substream(be, stream);
2142 /* is this op for this BE ? */
2143 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2146 /* copy params for each dpcm */
2147 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2148 sizeof(struct snd_pcm_hw_params));
2150 /* perform any hw_params fixups */
2151 if (be->dai_link->be_hw_params_fixup) {
2152 ret = be->dai_link->be_hw_params_fixup(be,
2156 "ASoC: hw_params BE fixup failed %d\n",
2162 /* only allow hw_params() if no connected FEs are running */
2163 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2166 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2167 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2168 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2171 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2172 be->dai_link->name);
2174 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2176 dev_err(dpcm->be->dev,
2177 "ASoC: hw_params BE failed %d\n", ret);
2181 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2186 /* disable any enabled and non active backends */
2187 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2188 struct snd_soc_pcm_runtime *be = dpcm->be;
2189 struct snd_pcm_substream *be_substream =
2190 snd_soc_dpcm_get_substream(be, stream);
2192 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2195 /* only allow hw_free() if no connected FEs are running */
2196 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2199 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2200 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2201 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2202 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2205 soc_pcm_hw_free(be_substream);
2211 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2212 struct snd_pcm_hw_params *params)
2214 struct snd_soc_pcm_runtime *fe = substream->private_data;
2215 int ret, stream = substream->stream;
2217 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2218 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2220 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2221 sizeof(struct snd_pcm_hw_params));
2222 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2224 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2228 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2229 fe->dai_link->name, params_rate(params),
2230 params_channels(params), params_format(params));
2232 /* call hw_params on the frontend */
2233 ret = soc_pcm_hw_params(substream, params);
2235 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2236 dpcm_be_dai_hw_free(fe, stream);
2238 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2241 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2242 mutex_unlock(&fe->card->mutex);
2246 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2247 struct snd_pcm_substream *substream, int cmd)
2251 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2252 dpcm->be->dai_link->name, cmd);
2254 ret = soc_pcm_trigger(substream, cmd);
2256 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2261 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2264 struct snd_soc_dpcm *dpcm;
2267 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2269 struct snd_soc_pcm_runtime *be = dpcm->be;
2270 struct snd_pcm_substream *be_substream =
2271 snd_soc_dpcm_get_substream(be, stream);
2273 /* is this op for this BE ? */
2274 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2278 case SNDRV_PCM_TRIGGER_START:
2279 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2280 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2281 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2284 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2288 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2290 case SNDRV_PCM_TRIGGER_RESUME:
2291 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2294 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2298 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2301 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2304 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2308 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2310 case SNDRV_PCM_TRIGGER_STOP:
2311 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2312 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2315 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2318 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2322 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2324 case SNDRV_PCM_TRIGGER_SUSPEND:
2325 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2328 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2331 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2335 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2337 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2338 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2341 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2344 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2348 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2355 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2357 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2358 int cmd, bool fe_first)
2360 struct snd_soc_pcm_runtime *fe = substream->private_data;
2363 /* call trigger on the frontend before the backend. */
2365 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2366 fe->dai_link->name, cmd);
2368 ret = soc_pcm_trigger(substream, cmd);
2372 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2376 /* call trigger on the frontend after the backend. */
2377 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2381 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2382 fe->dai_link->name, cmd);
2384 ret = soc_pcm_trigger(substream, cmd);
2389 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2391 struct snd_soc_pcm_runtime *fe = substream->private_data;
2392 int stream = substream->stream;
2394 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2396 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2399 case SND_SOC_DPCM_TRIGGER_PRE:
2401 case SNDRV_PCM_TRIGGER_START:
2402 case SNDRV_PCM_TRIGGER_RESUME:
2403 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2404 case SNDRV_PCM_TRIGGER_DRAIN:
2405 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2407 case SNDRV_PCM_TRIGGER_STOP:
2408 case SNDRV_PCM_TRIGGER_SUSPEND:
2409 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2410 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2417 case SND_SOC_DPCM_TRIGGER_POST:
2419 case SNDRV_PCM_TRIGGER_START:
2420 case SNDRV_PCM_TRIGGER_RESUME:
2421 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2422 case SNDRV_PCM_TRIGGER_DRAIN:
2423 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2425 case SNDRV_PCM_TRIGGER_STOP:
2426 case SNDRV_PCM_TRIGGER_SUSPEND:
2427 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2428 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2435 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2436 /* bespoke trigger() - handles both FE and BEs */
2438 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2439 fe->dai_link->name, cmd);
2441 ret = soc_pcm_bespoke_trigger(substream, cmd);
2444 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2445 fe->dai_link->name);
2451 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2457 case SNDRV_PCM_TRIGGER_START:
2458 case SNDRV_PCM_TRIGGER_RESUME:
2459 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2460 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2462 case SNDRV_PCM_TRIGGER_STOP:
2463 case SNDRV_PCM_TRIGGER_SUSPEND:
2464 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2466 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2467 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2472 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2476 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2478 struct snd_soc_pcm_runtime *fe = substream->private_data;
2479 int stream = substream->stream;
2481 /* if FE's runtime_update is already set, we're in race;
2482 * process this trigger later at exit
2484 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2485 fe->dpcm[stream].trigger_pending = cmd + 1;
2486 return 0; /* delayed, assuming it's successful */
2489 /* we're alone, let's trigger */
2490 return dpcm_fe_dai_do_trigger(substream, cmd);
2493 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2495 struct snd_soc_dpcm *dpcm;
2498 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2500 struct snd_soc_pcm_runtime *be = dpcm->be;
2501 struct snd_pcm_substream *be_substream =
2502 snd_soc_dpcm_get_substream(be, stream);
2504 /* is this op for this BE ? */
2505 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2508 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2509 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2510 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2511 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2514 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2515 be->dai_link->name);
2517 ret = soc_pcm_prepare(be_substream);
2519 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2524 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2529 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2531 struct snd_soc_pcm_runtime *fe = substream->private_data;
2532 int stream = substream->stream, ret = 0;
2534 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2536 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2538 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2540 /* there is no point preparing this FE if there are no BEs */
2541 if (list_empty(&fe->dpcm[stream].be_clients)) {
2542 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2543 fe->dai_link->name);
2548 ret = dpcm_be_dai_prepare(fe, substream->stream);
2552 /* call prepare on the frontend */
2553 ret = soc_pcm_prepare(substream);
2555 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2556 fe->dai_link->name);
2560 /* run the stream event for each BE */
2561 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2562 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2565 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2566 mutex_unlock(&fe->card->mutex);
2571 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2572 unsigned int cmd, void *arg)
2574 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2575 struct snd_soc_component *component;
2576 struct snd_soc_rtdcom_list *rtdcom;
2578 for_each_rtdcom(rtd, rtdcom) {
2579 component = rtdcom->component;
2581 if (!component->driver->ops ||
2582 !component->driver->ops->ioctl)
2585 /* FIXME: use 1st ioctl */
2586 return component->driver->ops->ioctl(substream, cmd, arg);
2589 return snd_pcm_lib_ioctl(substream, cmd, arg);
2592 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2594 struct snd_pcm_substream *substream =
2595 snd_soc_dpcm_get_substream(fe, stream);
2596 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2599 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2600 stream ? "capture" : "playback", fe->dai_link->name);
2602 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2603 /* call bespoke trigger - FE takes care of all BE triggers */
2604 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2605 fe->dai_link->name);
2607 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2609 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2611 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2612 fe->dai_link->name);
2614 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2616 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2619 err = dpcm_be_dai_hw_free(fe, stream);
2621 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2623 err = dpcm_be_dai_shutdown(fe, stream);
2625 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2627 /* run the stream event for each BE */
2628 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2633 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2635 struct snd_pcm_substream *substream =
2636 snd_soc_dpcm_get_substream(fe, stream);
2637 struct snd_soc_dpcm *dpcm;
2638 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2640 unsigned long flags;
2642 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2643 stream ? "capture" : "playback", fe->dai_link->name);
2645 /* Only start the BE if the FE is ready */
2646 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2647 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2650 /* startup must always be called for new BEs */
2651 ret = dpcm_be_dai_startup(fe, stream);
2655 /* keep going if FE state is > open */
2656 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2659 ret = dpcm_be_dai_hw_params(fe, stream);
2663 /* keep going if FE state is > hw_params */
2664 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2668 ret = dpcm_be_dai_prepare(fe, stream);
2672 /* run the stream event for each BE */
2673 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2675 /* keep going if FE state is > prepare */
2676 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2677 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2680 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2681 /* call trigger on the frontend - FE takes care of all BE triggers */
2682 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2683 fe->dai_link->name);
2685 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2687 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2691 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2692 fe->dai_link->name);
2694 ret = dpcm_be_dai_trigger(fe, stream,
2695 SNDRV_PCM_TRIGGER_START);
2697 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2705 dpcm_be_dai_hw_free(fe, stream);
2707 dpcm_be_dai_shutdown(fe, stream);
2709 /* disconnect any non started BEs */
2710 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2711 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2712 struct snd_soc_pcm_runtime *be = dpcm->be;
2713 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2714 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2716 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2721 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2725 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2726 ret = dpcm_run_update_startup(fe, stream);
2728 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2729 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2734 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2738 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2739 ret = dpcm_run_update_shutdown(fe, stream);
2741 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2742 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2747 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2749 struct snd_soc_dapm_widget_list *list;
2752 if (!fe->dai_link->dynamic)
2755 /* only check active links */
2756 if (!fe->cpu_dai->active)
2759 /* DAPM sync will call this to update DSP paths */
2760 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2761 new ? "new" : "old", fe->dai_link->name);
2763 /* skip if FE doesn't have playback capability */
2764 if (!fe->cpu_dai->driver->playback.channels_min ||
2765 !fe->codec_dai->driver->playback.channels_min)
2768 /* skip if FE isn't currently playing */
2769 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2772 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2774 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2775 fe->dai_link->name, "playback");
2779 /* update any playback paths */
2780 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2783 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2785 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2787 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2788 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2791 dpcm_path_put(&list);
2794 /* skip if FE doesn't have capture capability */
2795 if (!fe->cpu_dai->driver->capture.channels_min ||
2796 !fe->codec_dai->driver->capture.channels_min)
2799 /* skip if FE isn't currently capturing */
2800 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2803 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2805 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2806 fe->dai_link->name, "capture");
2810 /* update any old capture paths */
2811 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2814 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2816 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2818 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2819 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2822 dpcm_path_put(&list);
2827 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2830 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2832 struct snd_soc_pcm_runtime *fe;
2835 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2836 /* shutdown all old paths first */
2837 list_for_each_entry(fe, &card->rtd_list, list) {
2838 ret = soc_dpcm_fe_runtime_update(fe, 0);
2843 /* bring new paths up */
2844 list_for_each_entry(fe, &card->rtd_list, list) {
2845 ret = soc_dpcm_fe_runtime_update(fe, 1);
2851 mutex_unlock(&card->mutex);
2854 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2856 struct snd_soc_dpcm *dpcm;
2857 struct list_head *clients =
2858 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2860 list_for_each_entry(dpcm, clients, list_be) {
2862 struct snd_soc_pcm_runtime *be = dpcm->be;
2865 if (be->dai_link->ignore_suspend)
2868 for (i = 0; i < be->num_codecs; i++) {
2869 struct snd_soc_dai *dai = be->codec_dais[i];
2870 struct snd_soc_dai_driver *drv = dai->driver;
2872 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2873 be->dai_link->name);
2875 if (drv->ops && drv->ops->digital_mute &&
2876 dai->playback_active)
2877 drv->ops->digital_mute(dai, mute);
2884 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2886 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2887 struct snd_soc_dpcm *dpcm;
2888 struct snd_soc_dapm_widget_list *list;
2890 int stream = fe_substream->stream;
2892 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2893 fe->dpcm[stream].runtime = fe_substream->runtime;
2895 ret = dpcm_path_get(fe, stream, &list);
2897 mutex_unlock(&fe->card->mutex);
2899 } else if (ret == 0) {
2900 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2901 fe->dai_link->name, stream ? "capture" : "playback");
2904 /* calculate valid and active FE <-> BE dpcms */
2905 dpcm_process_paths(fe, stream, &list, 1);
2907 ret = dpcm_fe_dai_startup(fe_substream);
2909 /* clean up all links */
2910 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2911 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2913 dpcm_be_disconnect(fe, stream);
2914 fe->dpcm[stream].runtime = NULL;
2917 dpcm_clear_pending_state(fe, stream);
2918 dpcm_path_put(&list);
2919 mutex_unlock(&fe->card->mutex);
2923 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2925 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2926 struct snd_soc_dpcm *dpcm;
2927 int stream = fe_substream->stream, ret;
2929 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2930 ret = dpcm_fe_dai_shutdown(fe_substream);
2932 /* mark FE's links ready to prune */
2933 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2934 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2936 dpcm_be_disconnect(fe, stream);
2938 fe->dpcm[stream].runtime = NULL;
2939 mutex_unlock(&fe->card->mutex);
2943 static void soc_pcm_private_free(struct snd_pcm *pcm)
2945 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2946 struct snd_soc_rtdcom_list *rtdcom;
2947 struct snd_soc_component *component;
2949 /* need to sync the delayed work before releasing resources */
2950 flush_delayed_work(&rtd->delayed_work);
2951 for_each_rtdcom(rtd, rtdcom) {
2952 component = rtdcom->component;
2954 if (component->driver->pcm_free)
2955 component->driver->pcm_free(pcm);
2959 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2961 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2962 struct snd_soc_rtdcom_list *rtdcom;
2963 struct snd_soc_component *component;
2965 for_each_rtdcom(rtd, rtdcom) {
2966 component = rtdcom->component;
2968 if (!component->driver->ops ||
2969 !component->driver->ops->ack)
2972 /* FIXME. it returns 1st ask now */
2973 return component->driver->ops->ack(substream);
2979 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2980 unsigned long pos, void __user *buf,
2981 unsigned long bytes)
2983 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2984 struct snd_soc_rtdcom_list *rtdcom;
2985 struct snd_soc_component *component;
2987 for_each_rtdcom(rtd, rtdcom) {
2988 component = rtdcom->component;
2990 if (!component->driver->ops ||
2991 !component->driver->ops->copy_user)
2994 /* FIXME. it returns 1st copy now */
2995 return component->driver->ops->copy_user(substream, channel,
3002 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
3003 unsigned long pos, void *buf, unsigned long bytes)
3005 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3006 struct snd_soc_rtdcom_list *rtdcom;
3007 struct snd_soc_component *component;
3009 for_each_rtdcom(rtd, rtdcom) {
3010 component = rtdcom->component;
3012 if (!component->driver->ops ||
3013 !component->driver->ops->copy_kernel)
3016 /* FIXME. it returns 1st copy now */
3017 return component->driver->ops->copy_kernel(substream, channel,
3024 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
3025 unsigned long pos, unsigned long bytes)
3027 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3028 struct snd_soc_rtdcom_list *rtdcom;
3029 struct snd_soc_component *component;
3031 for_each_rtdcom(rtd, rtdcom) {
3032 component = rtdcom->component;
3034 if (!component->driver->ops ||
3035 !component->driver->ops->fill_silence)
3038 /* FIXME. it returns 1st silence now */
3039 return component->driver->ops->fill_silence(substream, channel,
3046 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3047 unsigned long offset)
3049 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3050 struct snd_soc_rtdcom_list *rtdcom;
3051 struct snd_soc_component *component;
3054 for_each_rtdcom(rtd, rtdcom) {
3055 component = rtdcom->component;
3057 if (!component->driver->ops ||
3058 !component->driver->ops->page)
3061 /* FIXME. it returns 1st page now */
3062 page = component->driver->ops->page(substream, offset);
3070 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3071 struct vm_area_struct *vma)
3073 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3074 struct snd_soc_rtdcom_list *rtdcom;
3075 struct snd_soc_component *component;
3077 for_each_rtdcom(rtd, rtdcom) {
3078 component = rtdcom->component;
3080 if (!component->driver->ops ||
3081 !component->driver->ops->mmap)
3084 /* FIXME. it returns 1st mmap now */
3085 return component->driver->ops->mmap(substream, vma);
3091 /* create a new pcm */
3092 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3094 struct snd_soc_dai *codec_dai;
3095 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3096 struct snd_soc_component *component;
3097 struct snd_soc_rtdcom_list *rtdcom;
3098 struct snd_pcm *pcm;
3100 int ret = 0, playback = 0, capture = 0;
3103 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3104 playback = rtd->dai_link->dpcm_playback;
3105 capture = rtd->dai_link->dpcm_capture;
3107 for (i = 0; i < rtd->num_codecs; i++) {
3108 codec_dai = rtd->codec_dais[i];
3109 if (codec_dai->driver->playback.channels_min)
3111 if (codec_dai->driver->capture.channels_min)
3115 capture = capture && cpu_dai->driver->capture.channels_min;
3116 playback = playback && cpu_dai->driver->playback.channels_min;
3119 if (rtd->dai_link->playback_only) {
3124 if (rtd->dai_link->capture_only) {
3129 /* create the PCM */
3130 if (rtd->dai_link->no_pcm) {
3131 snprintf(new_name, sizeof(new_name), "(%s)",
3132 rtd->dai_link->stream_name);
3134 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3135 playback, capture, &pcm);
3137 if (rtd->dai_link->dynamic)
3138 snprintf(new_name, sizeof(new_name), "%s (*)",
3139 rtd->dai_link->stream_name);
3141 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3142 rtd->dai_link->stream_name,
3143 (rtd->num_codecs > 1) ?
3144 "multicodec" : rtd->codec_dai->name, num);
3146 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3150 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3151 rtd->dai_link->name);
3154 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3156 /* DAPM dai link stream work */
3157 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3159 pcm->nonatomic = rtd->dai_link->nonatomic;
3161 pcm->private_data = rtd;
3163 if (rtd->dai_link->no_pcm) {
3165 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3167 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3171 /* ASoC PCM operations */
3172 if (rtd->dai_link->dynamic) {
3173 rtd->ops.open = dpcm_fe_dai_open;
3174 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3175 rtd->ops.prepare = dpcm_fe_dai_prepare;
3176 rtd->ops.trigger = dpcm_fe_dai_trigger;
3177 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3178 rtd->ops.close = dpcm_fe_dai_close;
3179 rtd->ops.pointer = soc_pcm_pointer;
3180 rtd->ops.ioctl = soc_pcm_ioctl;
3182 rtd->ops.open = soc_pcm_open;
3183 rtd->ops.hw_params = soc_pcm_hw_params;
3184 rtd->ops.prepare = soc_pcm_prepare;
3185 rtd->ops.trigger = soc_pcm_trigger;
3186 rtd->ops.hw_free = soc_pcm_hw_free;
3187 rtd->ops.close = soc_pcm_close;
3188 rtd->ops.pointer = soc_pcm_pointer;
3189 rtd->ops.ioctl = soc_pcm_ioctl;
3192 for_each_rtdcom(rtd, rtdcom) {
3193 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3199 rtd->ops.ack = soc_rtdcom_ack;
3201 rtd->ops.copy_user = soc_rtdcom_copy_user;
3202 if (ops->copy_kernel)
3203 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3204 if (ops->fill_silence)
3205 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3207 rtd->ops.page = soc_rtdcom_page;
3209 rtd->ops.mmap = soc_rtdcom_mmap;
3213 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3216 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3218 for_each_rtdcom(rtd, rtdcom) {
3219 component = rtdcom->component;
3221 if (!component->driver->pcm_new)
3224 ret = component->driver->pcm_new(rtd);
3226 dev_err(component->dev,
3227 "ASoC: pcm constructor failed: %d\n",
3233 pcm->private_free = soc_pcm_private_free;
3235 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3236 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3241 /* is the current PCM operation for this FE ? */
3242 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3244 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3248 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3250 /* is the current PCM operation for this BE ? */
3251 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3252 struct snd_soc_pcm_runtime *be, int stream)
3254 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3255 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3256 be->dpcm[stream].runtime_update))
3260 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3262 /* get the substream for this BE */
3263 struct snd_pcm_substream *
3264 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3266 return be->pcm->streams[stream].substream;
3268 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3270 /* get the BE runtime state */
3271 enum snd_soc_dpcm_state
3272 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3274 return be->dpcm[stream].state;
3276 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3278 /* set the BE runtime state */
3279 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3280 int stream, enum snd_soc_dpcm_state state)
3282 be->dpcm[stream].state = state;
3284 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3287 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3288 * are not running, paused or suspended for the specified stream direction.
3290 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3291 struct snd_soc_pcm_runtime *be, int stream)
3293 struct snd_soc_dpcm *dpcm;
3296 unsigned long flags;
3298 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3299 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3304 state = dpcm->fe->dpcm[stream].state;
3305 if (state == SND_SOC_DPCM_STATE_START ||
3306 state == SND_SOC_DPCM_STATE_PAUSED ||
3307 state == SND_SOC_DPCM_STATE_SUSPEND) {
3312 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3314 /* it's safe to free/stop this BE DAI */
3317 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3320 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3321 * running, paused or suspended for the specified stream direction.
3323 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3324 struct snd_soc_pcm_runtime *be, int stream)
3326 struct snd_soc_dpcm *dpcm;
3329 unsigned long flags;
3331 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3332 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3337 state = dpcm->fe->dpcm[stream].state;
3338 if (state == SND_SOC_DPCM_STATE_START ||
3339 state == SND_SOC_DPCM_STATE_PAUSED ||
3340 state == SND_SOC_DPCM_STATE_SUSPEND ||
3341 state == SND_SOC_DPCM_STATE_PREPARE) {
3346 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3348 /* it's safe to change hw_params */
3351 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3353 #ifdef CONFIG_DEBUG_FS
3354 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3357 case SND_SOC_DPCM_STATE_NEW:
3359 case SND_SOC_DPCM_STATE_OPEN:
3361 case SND_SOC_DPCM_STATE_HW_PARAMS:
3363 case SND_SOC_DPCM_STATE_PREPARE:
3365 case SND_SOC_DPCM_STATE_START:
3367 case SND_SOC_DPCM_STATE_STOP:
3369 case SND_SOC_DPCM_STATE_SUSPEND:
3371 case SND_SOC_DPCM_STATE_PAUSED:
3373 case SND_SOC_DPCM_STATE_HW_FREE:
3375 case SND_SOC_DPCM_STATE_CLOSE:
3382 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3383 int stream, char *buf, size_t size)
3385 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3386 struct snd_soc_dpcm *dpcm;
3388 unsigned long flags;
3391 offset += scnprintf(buf + offset, size - offset,
3392 "[%s - %s]\n", fe->dai_link->name,
3393 stream ? "Capture" : "Playback");
3395 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
3396 dpcm_state_string(fe->dpcm[stream].state));
3398 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3399 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3400 offset += scnprintf(buf + offset, size - offset,
3402 "Format = %s, Channels = %d, Rate = %d\n",
3403 snd_pcm_format_name(params_format(params)),
3404 params_channels(params),
3405 params_rate(params));
3408 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
3410 if (list_empty(&fe->dpcm[stream].be_clients)) {
3411 offset += scnprintf(buf + offset, size - offset,
3412 " No active DSP links\n");
3416 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3417 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3418 struct snd_soc_pcm_runtime *be = dpcm->be;
3419 params = &dpcm->hw_params;
3421 offset += scnprintf(buf + offset, size - offset,
3422 "- %s\n", be->dai_link->name);
3424 offset += scnprintf(buf + offset, size - offset,
3426 dpcm_state_string(be->dpcm[stream].state));
3428 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3429 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3430 offset += scnprintf(buf + offset, size - offset,
3431 " Hardware Params: "
3432 "Format = %s, Channels = %d, Rate = %d\n",
3433 snd_pcm_format_name(params_format(params)),
3434 params_channels(params),
3435 params_rate(params));
3437 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3442 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3443 size_t count, loff_t *ppos)
3445 struct snd_soc_pcm_runtime *fe = file->private_data;
3446 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3449 buf = kmalloc(out_count, GFP_KERNEL);
3453 if (fe->cpu_dai->driver->playback.channels_min)
3454 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3455 buf + offset, out_count - offset);
3457 if (fe->cpu_dai->driver->capture.channels_min)
3458 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3459 buf + offset, out_count - offset);
3461 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3467 static const struct file_operations dpcm_state_fops = {
3468 .open = simple_open,
3469 .read = dpcm_state_read_file,
3470 .llseek = default_llseek,
3473 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3478 if (!rtd->card->debugfs_card_root)
3481 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3482 rtd->card->debugfs_card_root);
3483 if (!rtd->debugfs_dpcm_root) {
3485 "ASoC: Failed to create dpcm debugfs directory %s\n",
3486 rtd->dai_link->name);
3490 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3491 rtd, &dpcm_state_fops);