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;
1225 /* only add new dpcms */
1226 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1227 if (dpcm->be == be && dpcm->fe == fe)
1231 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1237 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1238 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1239 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1240 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1242 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1243 stream ? "capture" : "playback", fe->dai_link->name,
1244 stream ? "<-" : "->", be->dai_link->name);
1246 #ifdef CONFIG_DEBUG_FS
1247 if (fe->debugfs_dpcm_root)
1248 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1249 fe->debugfs_dpcm_root, &dpcm->state);
1254 /* reparent a BE onto another FE */
1255 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1256 struct snd_soc_pcm_runtime *be, int stream)
1258 struct snd_soc_dpcm *dpcm;
1259 struct snd_pcm_substream *fe_substream, *be_substream;
1261 /* reparent if BE is connected to other FEs */
1262 if (!be->dpcm[stream].users)
1265 be_substream = snd_soc_dpcm_get_substream(be, stream);
1267 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1271 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1272 stream ? "capture" : "playback",
1273 dpcm->fe->dai_link->name,
1274 stream ? "<-" : "->", dpcm->be->dai_link->name);
1276 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1277 be_substream->runtime = fe_substream->runtime;
1282 /* disconnect a BE and FE */
1283 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1285 struct snd_soc_dpcm *dpcm, *d;
1287 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1288 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1289 stream ? "capture" : "playback",
1290 dpcm->be->dai_link->name);
1292 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1295 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1296 stream ? "capture" : "playback", fe->dai_link->name,
1297 stream ? "<-" : "->", dpcm->be->dai_link->name);
1299 /* BEs still alive need new FE */
1300 dpcm_be_reparent(fe, dpcm->be, stream);
1302 #ifdef CONFIG_DEBUG_FS
1303 debugfs_remove(dpcm->debugfs_state);
1305 list_del(&dpcm->list_be);
1306 list_del(&dpcm->list_fe);
1311 /* get BE for DAI widget and stream */
1312 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1313 struct snd_soc_dapm_widget *widget, int stream)
1315 struct snd_soc_pcm_runtime *be;
1318 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1320 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1321 list_for_each_entry(be, &card->rtd_list, list) {
1323 if (!be->dai_link->no_pcm)
1326 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1327 be->cpu_dai->playback_widget ?
1328 be->cpu_dai->playback_widget->name : "(not set)");
1330 if (be->cpu_dai->playback_widget == widget)
1333 for (i = 0; i < be->num_codecs; i++) {
1334 struct snd_soc_dai *dai = be->codec_dais[i];
1335 if (dai->playback_widget == widget)
1341 list_for_each_entry(be, &card->rtd_list, list) {
1343 if (!be->dai_link->no_pcm)
1346 dev_dbg(card->dev, "ASoC: try BE %s\n",
1347 be->cpu_dai->capture_widget ?
1348 be->cpu_dai->capture_widget->name : "(not set)");
1350 if (be->cpu_dai->capture_widget == widget)
1353 for (i = 0; i < be->num_codecs; i++) {
1354 struct snd_soc_dai *dai = be->codec_dais[i];
1355 if (dai->capture_widget == widget)
1361 /* dai link name and stream name set correctly ? */
1362 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1363 stream ? "capture" : "playback", widget->name);
1367 static inline struct snd_soc_dapm_widget *
1368 dai_get_widget(struct snd_soc_dai *dai, int stream)
1370 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1371 return dai->playback_widget;
1373 return dai->capture_widget;
1376 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1377 struct snd_soc_dapm_widget *widget)
1381 for (i = 0; i < list->num_widgets; i++) {
1382 if (widget == list->widgets[i])
1389 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1390 enum snd_soc_dapm_direction dir)
1392 struct snd_soc_card *card = widget->dapm->card;
1393 struct snd_soc_pcm_runtime *rtd;
1396 if (dir == SND_SOC_DAPM_DIR_OUT) {
1397 list_for_each_entry(rtd, &card->rtd_list, list) {
1398 if (!rtd->dai_link->no_pcm)
1401 if (rtd->cpu_dai->playback_widget == widget)
1404 for (i = 0; i < rtd->num_codecs; ++i) {
1405 struct snd_soc_dai *dai = rtd->codec_dais[i];
1406 if (dai->playback_widget == widget)
1410 } else { /* SND_SOC_DAPM_DIR_IN */
1411 list_for_each_entry(rtd, &card->rtd_list, list) {
1412 if (!rtd->dai_link->no_pcm)
1415 if (rtd->cpu_dai->capture_widget == widget)
1418 for (i = 0; i < rtd->num_codecs; ++i) {
1419 struct snd_soc_dai *dai = rtd->codec_dais[i];
1420 if (dai->capture_widget == widget)
1429 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1430 int stream, struct snd_soc_dapm_widget_list **list)
1432 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1435 /* get number of valid DAI paths and their widgets */
1436 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1437 dpcm_end_walk_at_be);
1439 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1440 stream ? "capture" : "playback");
1445 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1446 struct snd_soc_dapm_widget_list **list_)
1448 struct snd_soc_dpcm *dpcm;
1449 struct snd_soc_dapm_widget_list *list = *list_;
1450 struct snd_soc_dapm_widget *widget;
1453 /* Destroy any old FE <--> BE connections */
1454 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1457 /* is there a valid CPU DAI widget for this BE */
1458 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1460 /* prune the BE if it's no longer in our active list */
1461 if (widget && widget_in_list(list, widget))
1464 /* is there a valid CODEC DAI widget for this BE */
1465 for (i = 0; i < dpcm->be->num_codecs; i++) {
1466 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1467 widget = dai_get_widget(dai, stream);
1469 /* prune the BE if it's no longer in our active list */
1470 if (widget && widget_in_list(list, widget))
1474 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1475 stream ? "capture" : "playback",
1476 dpcm->be->dai_link->name, fe->dai_link->name);
1477 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1478 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1482 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1486 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1487 struct snd_soc_dapm_widget_list **list_)
1489 struct snd_soc_card *card = fe->card;
1490 struct snd_soc_dapm_widget_list *list = *list_;
1491 struct snd_soc_pcm_runtime *be;
1492 int i, new = 0, err;
1494 /* Create any new FE <--> BE connections */
1495 for (i = 0; i < list->num_widgets; i++) {
1497 switch (list->widgets[i]->id) {
1498 case snd_soc_dapm_dai_in:
1499 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1502 case snd_soc_dapm_dai_out:
1503 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1510 /* is there a valid BE rtd for this widget */
1511 be = dpcm_get_be(card, list->widgets[i], stream);
1513 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1514 list->widgets[i]->name);
1518 /* make sure BE is a real BE */
1519 if (!be->dai_link->no_pcm)
1522 /* don't connect if FE is not running */
1523 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1526 /* newly connected FE and BE */
1527 err = dpcm_be_connect(fe, be, stream);
1529 dev_err(fe->dev, "ASoC: can't connect %s\n",
1530 list->widgets[i]->name);
1532 } else if (err == 0) /* already connected */
1536 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1540 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1545 * Find the corresponding BE DAIs that source or sink audio to this
1548 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1549 int stream, struct snd_soc_dapm_widget_list **list, int new)
1552 return dpcm_add_paths(fe, stream, list);
1554 return dpcm_prune_paths(fe, stream, list);
1557 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1559 struct snd_soc_dpcm *dpcm;
1561 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1562 dpcm->be->dpcm[stream].runtime_update =
1563 SND_SOC_DPCM_UPDATE_NO;
1566 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1569 struct snd_soc_dpcm *dpcm;
1571 /* disable any enabled and non active backends */
1572 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1574 struct snd_soc_pcm_runtime *be = dpcm->be;
1575 struct snd_pcm_substream *be_substream =
1576 snd_soc_dpcm_get_substream(be, stream);
1578 if (be->dpcm[stream].users == 0)
1579 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1580 stream ? "capture" : "playback",
1581 be->dpcm[stream].state);
1583 if (--be->dpcm[stream].users != 0)
1586 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1589 soc_pcm_close(be_substream);
1590 be_substream->runtime = NULL;
1591 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1595 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1597 struct snd_soc_dpcm *dpcm;
1600 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1601 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1603 struct snd_soc_pcm_runtime *be = dpcm->be;
1604 struct snd_pcm_substream *be_substream =
1605 snd_soc_dpcm_get_substream(be, stream);
1607 if (!be_substream) {
1608 dev_err(be->dev, "ASoC: no backend %s stream\n",
1609 stream ? "capture" : "playback");
1613 /* is this op for this BE ? */
1614 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1617 /* first time the dpcm is open ? */
1618 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1619 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1620 stream ? "capture" : "playback",
1621 be->dpcm[stream].state);
1623 if (be->dpcm[stream].users++ != 0)
1626 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1627 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1630 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1631 stream ? "capture" : "playback", be->dai_link->name);
1633 be_substream->runtime = be->dpcm[stream].runtime;
1634 err = soc_pcm_open(be_substream);
1636 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1637 be->dpcm[stream].users--;
1638 if (be->dpcm[stream].users < 0)
1639 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1640 stream ? "capture" : "playback",
1641 be->dpcm[stream].state);
1643 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1647 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1654 /* disable any enabled and non active backends */
1655 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1656 struct snd_soc_pcm_runtime *be = dpcm->be;
1657 struct snd_pcm_substream *be_substream =
1658 snd_soc_dpcm_get_substream(be, stream);
1660 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1663 if (be->dpcm[stream].users == 0)
1664 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1665 stream ? "capture" : "playback",
1666 be->dpcm[stream].state);
1668 if (--be->dpcm[stream].users != 0)
1671 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1674 soc_pcm_close(be_substream);
1675 be_substream->runtime = NULL;
1676 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1682 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1683 struct snd_soc_pcm_stream *stream)
1685 runtime->hw.rate_min = stream->rate_min;
1686 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1687 runtime->hw.channels_min = stream->channels_min;
1688 runtime->hw.channels_max = stream->channels_max;
1689 if (runtime->hw.formats)
1690 runtime->hw.formats &= stream->formats;
1692 runtime->hw.formats = stream->formats;
1693 runtime->hw.rates = stream->rates;
1696 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1699 struct snd_soc_pcm_runtime *fe = substream->private_data;
1700 struct snd_soc_dpcm *dpcm;
1701 int stream = substream->stream;
1703 if (!fe->dai_link->dpcm_merged_format)
1707 * It returns merged BE codec format
1708 * if FE want to use it (= dpcm_merged_format)
1711 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1712 struct snd_soc_pcm_runtime *be = dpcm->be;
1713 struct snd_soc_dai_driver *codec_dai_drv;
1714 struct snd_soc_pcm_stream *codec_stream;
1717 for (i = 0; i < be->num_codecs; i++) {
1719 * Skip CODECs which don't support the current stream
1720 * type. See soc_pcm_init_runtime_hw() for more details
1722 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1726 codec_dai_drv = be->codec_dais[i]->driver;
1727 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1728 codec_stream = &codec_dai_drv->playback;
1730 codec_stream = &codec_dai_drv->capture;
1732 *formats &= codec_stream->formats;
1737 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1738 unsigned int *channels_min,
1739 unsigned int *channels_max)
1741 struct snd_soc_pcm_runtime *fe = substream->private_data;
1742 struct snd_soc_dpcm *dpcm;
1743 int stream = substream->stream;
1745 if (!fe->dai_link->dpcm_merged_chan)
1749 * It returns merged BE codec channel;
1750 * if FE want to use it (= dpcm_merged_chan)
1753 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1754 struct snd_soc_pcm_runtime *be = dpcm->be;
1755 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1756 struct snd_soc_dai_driver *codec_dai_drv;
1757 struct snd_soc_pcm_stream *codec_stream;
1758 struct snd_soc_pcm_stream *cpu_stream;
1760 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1761 cpu_stream = &cpu_dai_drv->playback;
1763 cpu_stream = &cpu_dai_drv->capture;
1765 *channels_min = max(*channels_min, cpu_stream->channels_min);
1766 *channels_max = min(*channels_max, cpu_stream->channels_max);
1769 * chan min/max cannot be enforced if there are multiple CODEC
1770 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1772 if (be->num_codecs == 1) {
1773 codec_dai_drv = be->codec_dais[0]->driver;
1775 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1776 codec_stream = &codec_dai_drv->playback;
1778 codec_stream = &codec_dai_drv->capture;
1780 *channels_min = max(*channels_min,
1781 codec_stream->channels_min);
1782 *channels_max = min(*channels_max,
1783 codec_stream->channels_max);
1788 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1789 unsigned int *rates,
1790 unsigned int *rate_min,
1791 unsigned int *rate_max)
1793 struct snd_soc_pcm_runtime *fe = substream->private_data;
1794 struct snd_soc_dpcm *dpcm;
1795 int stream = substream->stream;
1797 if (!fe->dai_link->dpcm_merged_rate)
1801 * It returns merged BE codec channel;
1802 * if FE want to use it (= dpcm_merged_chan)
1805 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1806 struct snd_soc_pcm_runtime *be = dpcm->be;
1807 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1808 struct snd_soc_dai_driver *codec_dai_drv;
1809 struct snd_soc_pcm_stream *codec_stream;
1810 struct snd_soc_pcm_stream *cpu_stream;
1813 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1814 cpu_stream = &cpu_dai_drv->playback;
1816 cpu_stream = &cpu_dai_drv->capture;
1818 *rate_min = max(*rate_min, cpu_stream->rate_min);
1819 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1820 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1822 for (i = 0; i < be->num_codecs; i++) {
1824 * Skip CODECs which don't support the current stream
1825 * type. See soc_pcm_init_runtime_hw() for more details
1827 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1831 codec_dai_drv = be->codec_dais[i]->driver;
1832 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1833 codec_stream = &codec_dai_drv->playback;
1835 codec_stream = &codec_dai_drv->capture;
1837 *rate_min = max(*rate_min, codec_stream->rate_min);
1838 *rate_max = min_not_zero(*rate_max,
1839 codec_stream->rate_max);
1840 *rates = snd_pcm_rate_mask_intersect(*rates,
1841 codec_stream->rates);
1846 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1848 struct snd_pcm_runtime *runtime = substream->runtime;
1849 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1850 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1851 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1853 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1854 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1856 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1858 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1859 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1860 &runtime->hw.channels_max);
1861 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1862 &runtime->hw.rate_min, &runtime->hw.rate_max);
1865 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1867 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1868 * for avoiding the race with trigger callback.
1869 * If the state is unset and a trigger is pending while the previous operation,
1870 * process the pending trigger action here.
1872 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1873 int stream, enum snd_soc_dpcm_update state)
1875 struct snd_pcm_substream *substream =
1876 snd_soc_dpcm_get_substream(fe, stream);
1878 snd_pcm_stream_lock_irq(substream);
1879 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1880 dpcm_fe_dai_do_trigger(substream,
1881 fe->dpcm[stream].trigger_pending - 1);
1882 fe->dpcm[stream].trigger_pending = 0;
1884 fe->dpcm[stream].runtime_update = state;
1885 snd_pcm_stream_unlock_irq(substream);
1888 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1891 struct snd_soc_dpcm *dpcm;
1892 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1893 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1896 /* apply symmetry for FE */
1897 if (soc_pcm_has_symmetry(fe_substream))
1898 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1900 /* Symmetry only applies if we've got an active stream. */
1901 if (fe_cpu_dai->active) {
1902 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1907 /* apply symmetry for BE */
1908 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1909 struct snd_soc_pcm_runtime *be = dpcm->be;
1910 struct snd_pcm_substream *be_substream =
1911 snd_soc_dpcm_get_substream(be, stream);
1912 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1915 if (rtd->dai_link->be_hw_params_fixup)
1918 if (soc_pcm_has_symmetry(be_substream))
1919 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1921 /* Symmetry only applies if we've got an active stream. */
1922 if (rtd->cpu_dai->active) {
1923 err = soc_pcm_apply_symmetry(fe_substream,
1929 for (i = 0; i < rtd->num_codecs; i++) {
1930 if (rtd->codec_dais[i]->active) {
1931 err = soc_pcm_apply_symmetry(fe_substream,
1932 rtd->codec_dais[i]);
1942 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1944 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1945 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1946 int stream = fe_substream->stream, ret = 0;
1948 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1950 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1952 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1956 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1958 /* start the DAI frontend */
1959 ret = soc_pcm_open(fe_substream);
1961 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1965 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1967 dpcm_set_fe_runtime(fe_substream);
1968 snd_pcm_limit_hw_rates(runtime);
1970 ret = dpcm_apply_symmetry(fe_substream, stream);
1972 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1977 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1981 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1983 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1987 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1989 struct snd_soc_dpcm *dpcm;
1991 /* only shutdown BEs that are either sinks or sources to this FE DAI */
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 if (be->dpcm[stream].users == 0)
2003 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2004 stream ? "capture" : "playback",
2005 be->dpcm[stream].state);
2007 if (--be->dpcm[stream].users != 0)
2010 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2011 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2012 soc_pcm_hw_free(be_substream);
2013 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2016 dev_dbg(be->dev, "ASoC: close BE %s\n",
2017 be->dai_link->name);
2019 soc_pcm_close(be_substream);
2020 be_substream->runtime = NULL;
2022 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2027 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2029 struct snd_soc_pcm_runtime *fe = substream->private_data;
2030 int stream = substream->stream;
2032 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2034 /* shutdown the BEs */
2035 dpcm_be_dai_shutdown(fe, substream->stream);
2037 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2039 /* now shutdown the frontend */
2040 soc_pcm_close(substream);
2042 /* run the stream event for each BE */
2043 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2045 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2046 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2050 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2052 struct snd_soc_dpcm *dpcm;
2054 /* only hw_params backends that are either sinks or sources
2055 * to this frontend DAI */
2056 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2058 struct snd_soc_pcm_runtime *be = dpcm->be;
2059 struct snd_pcm_substream *be_substream =
2060 snd_soc_dpcm_get_substream(be, stream);
2062 /* is this op for this BE ? */
2063 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2066 /* only free hw when no longer used - check all FEs */
2067 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2070 /* do not free hw if this BE is used by other FE */
2071 if (be->dpcm[stream].users > 1)
2074 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2075 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2076 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2077 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2078 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2079 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2082 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2083 be->dai_link->name);
2085 soc_pcm_hw_free(be_substream);
2087 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2093 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2095 struct snd_soc_pcm_runtime *fe = substream->private_data;
2096 int err, stream = substream->stream;
2098 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2099 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2101 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2103 /* call hw_free on the frontend */
2104 err = soc_pcm_hw_free(substream);
2106 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2107 fe->dai_link->name);
2109 /* only hw_params backends that are either sinks or sources
2110 * to this frontend DAI */
2111 err = dpcm_be_dai_hw_free(fe, stream);
2113 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2114 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2116 mutex_unlock(&fe->card->mutex);
2120 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2122 struct snd_soc_dpcm *dpcm;
2125 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2127 struct snd_soc_pcm_runtime *be = dpcm->be;
2128 struct snd_pcm_substream *be_substream =
2129 snd_soc_dpcm_get_substream(be, stream);
2131 /* is this op for this BE ? */
2132 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2135 /* copy params for each dpcm */
2136 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2137 sizeof(struct snd_pcm_hw_params));
2139 /* perform any hw_params fixups */
2140 if (be->dai_link->be_hw_params_fixup) {
2141 ret = be->dai_link->be_hw_params_fixup(be,
2145 "ASoC: hw_params BE fixup failed %d\n",
2151 /* only allow hw_params() if no connected FEs are running */
2152 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2155 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2156 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2157 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2160 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2161 be->dai_link->name);
2163 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2165 dev_err(dpcm->be->dev,
2166 "ASoC: hw_params BE failed %d\n", ret);
2170 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2175 /* disable any enabled and non active backends */
2176 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2177 struct snd_soc_pcm_runtime *be = dpcm->be;
2178 struct snd_pcm_substream *be_substream =
2179 snd_soc_dpcm_get_substream(be, stream);
2181 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2184 /* only allow hw_free() if no connected FEs are running */
2185 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2188 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2189 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2190 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2191 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2194 soc_pcm_hw_free(be_substream);
2200 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2201 struct snd_pcm_hw_params *params)
2203 struct snd_soc_pcm_runtime *fe = substream->private_data;
2204 int ret, stream = substream->stream;
2206 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2207 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2209 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2210 sizeof(struct snd_pcm_hw_params));
2211 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2213 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2217 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2218 fe->dai_link->name, params_rate(params),
2219 params_channels(params), params_format(params));
2221 /* call hw_params on the frontend */
2222 ret = soc_pcm_hw_params(substream, params);
2224 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2225 dpcm_be_dai_hw_free(fe, stream);
2227 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2230 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2231 mutex_unlock(&fe->card->mutex);
2235 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2236 struct snd_pcm_substream *substream, int cmd)
2240 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2241 dpcm->be->dai_link->name, cmd);
2243 ret = soc_pcm_trigger(substream, cmd);
2245 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2250 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2253 struct snd_soc_dpcm *dpcm;
2256 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2258 struct snd_soc_pcm_runtime *be = dpcm->be;
2259 struct snd_pcm_substream *be_substream =
2260 snd_soc_dpcm_get_substream(be, stream);
2262 /* is this op for this BE ? */
2263 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2267 case SNDRV_PCM_TRIGGER_START:
2268 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2269 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2270 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2273 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2277 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2279 case SNDRV_PCM_TRIGGER_RESUME:
2280 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2283 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2287 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2289 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2290 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2293 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2297 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2299 case SNDRV_PCM_TRIGGER_STOP:
2300 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2301 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2304 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2307 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2311 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2313 case SNDRV_PCM_TRIGGER_SUSPEND:
2314 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2317 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2320 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2324 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2326 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2327 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2330 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2333 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2337 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2344 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2346 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2347 int cmd, bool fe_first)
2349 struct snd_soc_pcm_runtime *fe = substream->private_data;
2352 /* call trigger on the frontend before the backend. */
2354 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2355 fe->dai_link->name, cmd);
2357 ret = soc_pcm_trigger(substream, cmd);
2361 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2365 /* call trigger on the frontend after the backend. */
2366 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2370 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2371 fe->dai_link->name, cmd);
2373 ret = soc_pcm_trigger(substream, cmd);
2378 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2380 struct snd_soc_pcm_runtime *fe = substream->private_data;
2381 int stream = substream->stream;
2383 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2385 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2388 case SND_SOC_DPCM_TRIGGER_PRE:
2390 case SNDRV_PCM_TRIGGER_START:
2391 case SNDRV_PCM_TRIGGER_RESUME:
2392 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2393 case SNDRV_PCM_TRIGGER_DRAIN:
2394 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2396 case SNDRV_PCM_TRIGGER_STOP:
2397 case SNDRV_PCM_TRIGGER_SUSPEND:
2398 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2399 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2406 case SND_SOC_DPCM_TRIGGER_POST:
2408 case SNDRV_PCM_TRIGGER_START:
2409 case SNDRV_PCM_TRIGGER_RESUME:
2410 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2411 case SNDRV_PCM_TRIGGER_DRAIN:
2412 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2414 case SNDRV_PCM_TRIGGER_STOP:
2415 case SNDRV_PCM_TRIGGER_SUSPEND:
2416 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2417 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2424 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2425 /* bespoke trigger() - handles both FE and BEs */
2427 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2428 fe->dai_link->name, cmd);
2430 ret = soc_pcm_bespoke_trigger(substream, cmd);
2433 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2434 fe->dai_link->name);
2440 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2446 case SNDRV_PCM_TRIGGER_START:
2447 case SNDRV_PCM_TRIGGER_RESUME:
2448 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2449 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2451 case SNDRV_PCM_TRIGGER_STOP:
2452 case SNDRV_PCM_TRIGGER_SUSPEND:
2453 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2455 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2456 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2461 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2465 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2467 struct snd_soc_pcm_runtime *fe = substream->private_data;
2468 int stream = substream->stream;
2470 /* if FE's runtime_update is already set, we're in race;
2471 * process this trigger later at exit
2473 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2474 fe->dpcm[stream].trigger_pending = cmd + 1;
2475 return 0; /* delayed, assuming it's successful */
2478 /* we're alone, let's trigger */
2479 return dpcm_fe_dai_do_trigger(substream, cmd);
2482 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2484 struct snd_soc_dpcm *dpcm;
2487 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2489 struct snd_soc_pcm_runtime *be = dpcm->be;
2490 struct snd_pcm_substream *be_substream =
2491 snd_soc_dpcm_get_substream(be, stream);
2493 /* is this op for this BE ? */
2494 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2497 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2498 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2499 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2500 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2503 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2504 be->dai_link->name);
2506 ret = soc_pcm_prepare(be_substream);
2508 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2513 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2518 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2520 struct snd_soc_pcm_runtime *fe = substream->private_data;
2521 int stream = substream->stream, ret = 0;
2523 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2525 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2527 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2529 /* there is no point preparing this FE if there are no BEs */
2530 if (list_empty(&fe->dpcm[stream].be_clients)) {
2531 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2532 fe->dai_link->name);
2537 ret = dpcm_be_dai_prepare(fe, substream->stream);
2541 /* call prepare on the frontend */
2542 ret = soc_pcm_prepare(substream);
2544 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2545 fe->dai_link->name);
2549 /* run the stream event for each BE */
2550 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2551 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2554 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2555 mutex_unlock(&fe->card->mutex);
2560 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2561 unsigned int cmd, void *arg)
2563 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2564 struct snd_soc_component *component;
2565 struct snd_soc_rtdcom_list *rtdcom;
2567 for_each_rtdcom(rtd, rtdcom) {
2568 component = rtdcom->component;
2570 if (!component->driver->ops ||
2571 !component->driver->ops->ioctl)
2574 /* FIXME: use 1st ioctl */
2575 return component->driver->ops->ioctl(substream, cmd, arg);
2578 return snd_pcm_lib_ioctl(substream, cmd, arg);
2581 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2583 struct snd_pcm_substream *substream =
2584 snd_soc_dpcm_get_substream(fe, stream);
2585 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2588 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2589 stream ? "capture" : "playback", fe->dai_link->name);
2591 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2592 /* call bespoke trigger - FE takes care of all BE triggers */
2593 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2594 fe->dai_link->name);
2596 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2598 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2600 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2601 fe->dai_link->name);
2603 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2605 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2608 err = dpcm_be_dai_hw_free(fe, stream);
2610 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2612 err = dpcm_be_dai_shutdown(fe, stream);
2614 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2616 /* run the stream event for each BE */
2617 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2622 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2624 struct snd_pcm_substream *substream =
2625 snd_soc_dpcm_get_substream(fe, stream);
2626 struct snd_soc_dpcm *dpcm;
2627 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2630 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2631 stream ? "capture" : "playback", fe->dai_link->name);
2633 /* Only start the BE if the FE is ready */
2634 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2635 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2638 /* startup must always be called for new BEs */
2639 ret = dpcm_be_dai_startup(fe, stream);
2643 /* keep going if FE state is > open */
2644 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2647 ret = dpcm_be_dai_hw_params(fe, stream);
2651 /* keep going if FE state is > hw_params */
2652 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2656 ret = dpcm_be_dai_prepare(fe, stream);
2660 /* run the stream event for each BE */
2661 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2663 /* keep going if FE state is > prepare */
2664 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2665 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2668 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2669 /* call trigger on the frontend - FE takes care of all BE triggers */
2670 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2671 fe->dai_link->name);
2673 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2675 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2679 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2680 fe->dai_link->name);
2682 ret = dpcm_be_dai_trigger(fe, stream,
2683 SNDRV_PCM_TRIGGER_START);
2685 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2693 dpcm_be_dai_hw_free(fe, stream);
2695 dpcm_be_dai_shutdown(fe, stream);
2697 /* disconnect any non started BEs */
2698 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2699 struct snd_soc_pcm_runtime *be = dpcm->be;
2700 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2701 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2707 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2711 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2712 ret = dpcm_run_update_startup(fe, stream);
2714 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2715 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2720 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2724 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2725 ret = dpcm_run_update_shutdown(fe, stream);
2727 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2728 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2733 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2735 struct snd_soc_dapm_widget_list *list;
2738 if (!fe->dai_link->dynamic)
2741 /* only check active links */
2742 if (!fe->cpu_dai->active)
2745 /* DAPM sync will call this to update DSP paths */
2746 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2747 new ? "new" : "old", fe->dai_link->name);
2749 /* skip if FE doesn't have playback capability */
2750 if (!fe->cpu_dai->driver->playback.channels_min ||
2751 !fe->codec_dai->driver->playback.channels_min)
2754 /* skip if FE isn't currently playing */
2755 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2758 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2760 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2761 fe->dai_link->name, "playback");
2765 /* update any playback paths */
2766 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2769 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2771 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2773 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2774 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2777 dpcm_path_put(&list);
2780 /* skip if FE doesn't have capture capability */
2781 if (!fe->cpu_dai->driver->capture.channels_min ||
2782 !fe->codec_dai->driver->capture.channels_min)
2785 /* skip if FE isn't currently capturing */
2786 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2789 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2791 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2792 fe->dai_link->name, "capture");
2796 /* update any old capture paths */
2797 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2800 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2802 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2804 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2805 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2808 dpcm_path_put(&list);
2813 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2816 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2818 struct snd_soc_pcm_runtime *fe;
2821 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2822 /* shutdown all old paths first */
2823 list_for_each_entry(fe, &card->rtd_list, list) {
2824 ret = soc_dpcm_fe_runtime_update(fe, 0);
2829 /* bring new paths up */
2830 list_for_each_entry(fe, &card->rtd_list, list) {
2831 ret = soc_dpcm_fe_runtime_update(fe, 1);
2837 mutex_unlock(&card->mutex);
2840 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2842 struct snd_soc_dpcm *dpcm;
2843 struct list_head *clients =
2844 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2846 list_for_each_entry(dpcm, clients, list_be) {
2848 struct snd_soc_pcm_runtime *be = dpcm->be;
2851 if (be->dai_link->ignore_suspend)
2854 for (i = 0; i < be->num_codecs; i++) {
2855 struct snd_soc_dai *dai = be->codec_dais[i];
2856 struct snd_soc_dai_driver *drv = dai->driver;
2858 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2859 be->dai_link->name);
2861 if (drv->ops && drv->ops->digital_mute &&
2862 dai->playback_active)
2863 drv->ops->digital_mute(dai, mute);
2870 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2872 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2873 struct snd_soc_dpcm *dpcm;
2874 struct snd_soc_dapm_widget_list *list;
2876 int stream = fe_substream->stream;
2878 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2879 fe->dpcm[stream].runtime = fe_substream->runtime;
2881 ret = dpcm_path_get(fe, stream, &list);
2883 mutex_unlock(&fe->card->mutex);
2885 } else if (ret == 0) {
2886 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2887 fe->dai_link->name, stream ? "capture" : "playback");
2890 /* calculate valid and active FE <-> BE dpcms */
2891 dpcm_process_paths(fe, stream, &list, 1);
2893 ret = dpcm_fe_dai_startup(fe_substream);
2895 /* clean up all links */
2896 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2897 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2899 dpcm_be_disconnect(fe, stream);
2900 fe->dpcm[stream].runtime = NULL;
2903 dpcm_clear_pending_state(fe, stream);
2904 dpcm_path_put(&list);
2905 mutex_unlock(&fe->card->mutex);
2909 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2911 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2912 struct snd_soc_dpcm *dpcm;
2913 int stream = fe_substream->stream, ret;
2915 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2916 ret = dpcm_fe_dai_shutdown(fe_substream);
2918 /* mark FE's links ready to prune */
2919 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2920 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2922 dpcm_be_disconnect(fe, stream);
2924 fe->dpcm[stream].runtime = NULL;
2925 mutex_unlock(&fe->card->mutex);
2929 static void soc_pcm_private_free(struct snd_pcm *pcm)
2931 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2932 struct snd_soc_rtdcom_list *rtdcom;
2933 struct snd_soc_component *component;
2935 /* need to sync the delayed work before releasing resources */
2936 flush_delayed_work(&rtd->delayed_work);
2937 for_each_rtdcom(rtd, rtdcom) {
2938 component = rtdcom->component;
2940 if (component->driver->pcm_free)
2941 component->driver->pcm_free(pcm);
2945 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2947 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2948 struct snd_soc_rtdcom_list *rtdcom;
2949 struct snd_soc_component *component;
2951 for_each_rtdcom(rtd, rtdcom) {
2952 component = rtdcom->component;
2954 if (!component->driver->ops ||
2955 !component->driver->ops->ack)
2958 /* FIXME. it returns 1st ask now */
2959 return component->driver->ops->ack(substream);
2965 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2966 unsigned long pos, void __user *buf,
2967 unsigned long bytes)
2969 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2970 struct snd_soc_rtdcom_list *rtdcom;
2971 struct snd_soc_component *component;
2973 for_each_rtdcom(rtd, rtdcom) {
2974 component = rtdcom->component;
2976 if (!component->driver->ops ||
2977 !component->driver->ops->copy_user)
2980 /* FIXME. it returns 1st copy now */
2981 return component->driver->ops->copy_user(substream, channel,
2988 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2989 unsigned long pos, void *buf, unsigned long bytes)
2991 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2992 struct snd_soc_rtdcom_list *rtdcom;
2993 struct snd_soc_component *component;
2995 for_each_rtdcom(rtd, rtdcom) {
2996 component = rtdcom->component;
2998 if (!component->driver->ops ||
2999 !component->driver->ops->copy_kernel)
3002 /* FIXME. it returns 1st copy now */
3003 return component->driver->ops->copy_kernel(substream, channel,
3010 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
3011 unsigned long pos, unsigned long bytes)
3013 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3014 struct snd_soc_rtdcom_list *rtdcom;
3015 struct snd_soc_component *component;
3017 for_each_rtdcom(rtd, rtdcom) {
3018 component = rtdcom->component;
3020 if (!component->driver->ops ||
3021 !component->driver->ops->fill_silence)
3024 /* FIXME. it returns 1st silence now */
3025 return component->driver->ops->fill_silence(substream, channel,
3032 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3033 unsigned long offset)
3035 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3036 struct snd_soc_rtdcom_list *rtdcom;
3037 struct snd_soc_component *component;
3040 for_each_rtdcom(rtd, rtdcom) {
3041 component = rtdcom->component;
3043 if (!component->driver->ops ||
3044 !component->driver->ops->page)
3047 /* FIXME. it returns 1st page now */
3048 page = component->driver->ops->page(substream, offset);
3056 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3057 struct vm_area_struct *vma)
3059 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3060 struct snd_soc_rtdcom_list *rtdcom;
3061 struct snd_soc_component *component;
3063 for_each_rtdcom(rtd, rtdcom) {
3064 component = rtdcom->component;
3066 if (!component->driver->ops ||
3067 !component->driver->ops->mmap)
3070 /* FIXME. it returns 1st mmap now */
3071 return component->driver->ops->mmap(substream, vma);
3077 /* create a new pcm */
3078 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3080 struct snd_soc_dai *codec_dai;
3081 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3082 struct snd_soc_component *component;
3083 struct snd_soc_rtdcom_list *rtdcom;
3084 struct snd_pcm *pcm;
3086 int ret = 0, playback = 0, capture = 0;
3089 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3090 playback = rtd->dai_link->dpcm_playback;
3091 capture = rtd->dai_link->dpcm_capture;
3093 for (i = 0; i < rtd->num_codecs; i++) {
3094 codec_dai = rtd->codec_dais[i];
3095 if (codec_dai->driver->playback.channels_min)
3097 if (codec_dai->driver->capture.channels_min)
3101 capture = capture && cpu_dai->driver->capture.channels_min;
3102 playback = playback && cpu_dai->driver->playback.channels_min;
3105 if (rtd->dai_link->playback_only) {
3110 if (rtd->dai_link->capture_only) {
3115 /* create the PCM */
3116 if (rtd->dai_link->no_pcm) {
3117 snprintf(new_name, sizeof(new_name), "(%s)",
3118 rtd->dai_link->stream_name);
3120 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3121 playback, capture, &pcm);
3123 if (rtd->dai_link->dynamic)
3124 snprintf(new_name, sizeof(new_name), "%s (*)",
3125 rtd->dai_link->stream_name);
3127 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3128 rtd->dai_link->stream_name,
3129 (rtd->num_codecs > 1) ?
3130 "multicodec" : rtd->codec_dai->name, num);
3132 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3136 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3137 rtd->dai_link->name);
3140 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3142 /* DAPM dai link stream work */
3143 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3145 pcm->nonatomic = rtd->dai_link->nonatomic;
3147 pcm->private_data = rtd;
3149 if (rtd->dai_link->no_pcm) {
3151 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3153 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3157 /* ASoC PCM operations */
3158 if (rtd->dai_link->dynamic) {
3159 rtd->ops.open = dpcm_fe_dai_open;
3160 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3161 rtd->ops.prepare = dpcm_fe_dai_prepare;
3162 rtd->ops.trigger = dpcm_fe_dai_trigger;
3163 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3164 rtd->ops.close = dpcm_fe_dai_close;
3165 rtd->ops.pointer = soc_pcm_pointer;
3166 rtd->ops.ioctl = soc_pcm_ioctl;
3168 rtd->ops.open = soc_pcm_open;
3169 rtd->ops.hw_params = soc_pcm_hw_params;
3170 rtd->ops.prepare = soc_pcm_prepare;
3171 rtd->ops.trigger = soc_pcm_trigger;
3172 rtd->ops.hw_free = soc_pcm_hw_free;
3173 rtd->ops.close = soc_pcm_close;
3174 rtd->ops.pointer = soc_pcm_pointer;
3175 rtd->ops.ioctl = soc_pcm_ioctl;
3178 for_each_rtdcom(rtd, rtdcom) {
3179 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3185 rtd->ops.ack = soc_rtdcom_ack;
3187 rtd->ops.copy_user = soc_rtdcom_copy_user;
3188 if (ops->copy_kernel)
3189 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3190 if (ops->fill_silence)
3191 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3193 rtd->ops.page = soc_rtdcom_page;
3195 rtd->ops.mmap = soc_rtdcom_mmap;
3199 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3202 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3204 for_each_rtdcom(rtd, rtdcom) {
3205 component = rtdcom->component;
3207 if (!component->driver->pcm_new)
3210 ret = component->driver->pcm_new(rtd);
3212 dev_err(component->dev,
3213 "ASoC: pcm constructor failed: %d\n",
3219 pcm->private_free = soc_pcm_private_free;
3221 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3222 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3227 /* is the current PCM operation for this FE ? */
3228 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3230 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3234 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3236 /* is the current PCM operation for this BE ? */
3237 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3238 struct snd_soc_pcm_runtime *be, int stream)
3240 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3241 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3242 be->dpcm[stream].runtime_update))
3246 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3248 /* get the substream for this BE */
3249 struct snd_pcm_substream *
3250 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3252 return be->pcm->streams[stream].substream;
3254 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3256 /* get the BE runtime state */
3257 enum snd_soc_dpcm_state
3258 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3260 return be->dpcm[stream].state;
3262 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3264 /* set the BE runtime state */
3265 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3266 int stream, enum snd_soc_dpcm_state state)
3268 be->dpcm[stream].state = state;
3270 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3273 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3274 * are not running, paused or suspended for the specified stream direction.
3276 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3277 struct snd_soc_pcm_runtime *be, int stream)
3279 struct snd_soc_dpcm *dpcm;
3282 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3287 state = dpcm->fe->dpcm[stream].state;
3288 if (state == SND_SOC_DPCM_STATE_START ||
3289 state == SND_SOC_DPCM_STATE_PAUSED ||
3290 state == SND_SOC_DPCM_STATE_SUSPEND)
3294 /* it's safe to free/stop this BE DAI */
3297 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3300 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3301 * running, paused or suspended for the specified stream direction.
3303 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3304 struct snd_soc_pcm_runtime *be, int stream)
3306 struct snd_soc_dpcm *dpcm;
3309 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3314 state = dpcm->fe->dpcm[stream].state;
3315 if (state == SND_SOC_DPCM_STATE_START ||
3316 state == SND_SOC_DPCM_STATE_PAUSED ||
3317 state == SND_SOC_DPCM_STATE_SUSPEND ||
3318 state == SND_SOC_DPCM_STATE_PREPARE)
3322 /* it's safe to change hw_params */
3325 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3327 #ifdef CONFIG_DEBUG_FS
3328 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3331 case SND_SOC_DPCM_STATE_NEW:
3333 case SND_SOC_DPCM_STATE_OPEN:
3335 case SND_SOC_DPCM_STATE_HW_PARAMS:
3337 case SND_SOC_DPCM_STATE_PREPARE:
3339 case SND_SOC_DPCM_STATE_START:
3341 case SND_SOC_DPCM_STATE_STOP:
3343 case SND_SOC_DPCM_STATE_SUSPEND:
3345 case SND_SOC_DPCM_STATE_PAUSED:
3347 case SND_SOC_DPCM_STATE_HW_FREE:
3349 case SND_SOC_DPCM_STATE_CLOSE:
3356 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3357 int stream, char *buf, size_t size)
3359 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3360 struct snd_soc_dpcm *dpcm;
3364 offset += scnprintf(buf + offset, size - offset,
3365 "[%s - %s]\n", fe->dai_link->name,
3366 stream ? "Capture" : "Playback");
3368 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
3369 dpcm_state_string(fe->dpcm[stream].state));
3371 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3372 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3373 offset += scnprintf(buf + offset, size - offset,
3375 "Format = %s, Channels = %d, Rate = %d\n",
3376 snd_pcm_format_name(params_format(params)),
3377 params_channels(params),
3378 params_rate(params));
3381 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
3383 if (list_empty(&fe->dpcm[stream].be_clients)) {
3384 offset += scnprintf(buf + offset, size - offset,
3385 " No active DSP links\n");
3389 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3390 struct snd_soc_pcm_runtime *be = dpcm->be;
3391 params = &dpcm->hw_params;
3393 offset += scnprintf(buf + offset, size - offset,
3394 "- %s\n", be->dai_link->name);
3396 offset += scnprintf(buf + offset, size - offset,
3398 dpcm_state_string(be->dpcm[stream].state));
3400 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3401 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3402 offset += scnprintf(buf + offset, size - offset,
3403 " Hardware Params: "
3404 "Format = %s, Channels = %d, Rate = %d\n",
3405 snd_pcm_format_name(params_format(params)),
3406 params_channels(params),
3407 params_rate(params));
3414 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3415 size_t count, loff_t *ppos)
3417 struct snd_soc_pcm_runtime *fe = file->private_data;
3418 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3421 buf = kmalloc(out_count, GFP_KERNEL);
3425 if (fe->cpu_dai->driver->playback.channels_min)
3426 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3427 buf + offset, out_count - offset);
3429 if (fe->cpu_dai->driver->capture.channels_min)
3430 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3431 buf + offset, out_count - offset);
3433 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3439 static const struct file_operations dpcm_state_fops = {
3440 .open = simple_open,
3441 .read = dpcm_state_read_file,
3442 .llseek = default_llseek,
3445 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3450 if (!rtd->card->debugfs_card_root)
3453 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3454 rtd->card->debugfs_card_root);
3455 if (!rtd->debugfs_dpcm_root) {
3457 "ASoC: Failed to create dpcm debugfs directory %s\n",
3458 rtd->dai_link->name);
3462 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3463 rtd, &dpcm_state_fops);