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 rate at all, it supports the stream. */
46 return codec_stream->rates;
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;
62 struct snd_soc_dai *codec_dai;
65 lockdep_assert_held(&rtd->pcm_mutex);
67 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
68 cpu_dai->playback_active++;
69 for_each_rtd_codec_dai(rtd, i, codec_dai)
70 codec_dai->playback_active++;
72 cpu_dai->capture_active++;
73 for_each_rtd_codec_dai(rtd, i, codec_dai)
74 codec_dai->capture_active++;
78 cpu_dai->component->active++;
79 for_each_rtd_codec_dai(rtd, i, codec_dai) {
81 codec_dai->component->active++;
86 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
87 * @rtd: ASoC PCM runtime that is deactivated
88 * @stream: Direction of the PCM stream
90 * Decrements the active count for all the DAIs and components attached to a PCM
91 * runtime. Should typically be called when a stream is closed.
93 * Must be called with the rtd->pcm_mutex being held
95 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
97 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
98 struct snd_soc_dai *codec_dai;
101 lockdep_assert_held(&rtd->pcm_mutex);
103 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
104 cpu_dai->playback_active--;
105 for_each_rtd_codec_dai(rtd, i, codec_dai)
106 codec_dai->playback_active--;
108 cpu_dai->capture_active--;
109 for_each_rtd_codec_dai(rtd, i, codec_dai)
110 codec_dai->capture_active--;
114 cpu_dai->component->active--;
115 for_each_rtd_codec_dai(rtd, i, codec_dai) {
116 codec_dai->component->active--;
122 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
123 * @rtd: The ASoC PCM runtime that should be checked.
125 * This function checks whether the power down delay should be ignored for a
126 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
127 * been configured to ignore the delay, or if none of the components benefits
128 * from having the delay.
130 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
132 struct snd_soc_rtdcom_list *rtdcom;
133 struct snd_soc_component *component;
136 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
139 for_each_rtdcom(rtd, rtdcom) {
140 component = rtdcom->component;
142 ignore &= !component->driver->use_pmdown_time;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
175 struct snd_soc_dpcm *dpcm;
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
179 struct snd_soc_pcm_runtime *be = dpcm->be;
181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182 be->dai_link->name, event, dir);
184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
188 snd_soc_dapm_stream_event(be, dir, event);
191 snd_soc_dapm_stream_event(fe, dir, event);
196 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
207 ret = snd_pcm_hw_constraint_single(substream->runtime,
208 SNDRV_PCM_HW_PARAM_RATE,
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
223 ret = snd_pcm_hw_constraint_single(substream->runtime,
224 SNDRV_PCM_HW_PARAM_CHANNELS,
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
239 ret = snd_pcm_hw_constraint_single(substream->runtime,
240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
241 soc_dai->sample_bits);
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
253 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
258 struct snd_soc_dai *codec_dai;
259 unsigned int rate, channels, sample_bits, symmetry, i;
261 rate = params_rate(params);
262 channels = params_channels(params);
263 sample_bits = snd_pcm_format_physical_width(params_format(params));
265 /* reject unmatched parameters when applying symmetry */
266 symmetry = cpu_dai->driver->symmetric_rates ||
267 rtd->dai_link->symmetric_rates;
269 for_each_rtd_codec_dai(rtd, i, codec_dai)
270 symmetry |= codec_dai->driver->symmetric_rates;
272 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
273 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
274 cpu_dai->rate, rate);
278 symmetry = cpu_dai->driver->symmetric_channels ||
279 rtd->dai_link->symmetric_channels;
281 for_each_rtd_codec_dai(rtd, i, codec_dai)
282 symmetry |= codec_dai->driver->symmetric_channels;
284 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
285 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
286 cpu_dai->channels, channels);
290 symmetry = cpu_dai->driver->symmetric_samplebits ||
291 rtd->dai_link->symmetric_samplebits;
293 for_each_rtd_codec_dai(rtd, i, codec_dai)
294 symmetry |= codec_dai->driver->symmetric_samplebits;
296 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
297 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
298 cpu_dai->sample_bits, sample_bits);
305 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
307 struct snd_soc_pcm_runtime *rtd = substream->private_data;
308 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
309 struct snd_soc_dai_link *link = rtd->dai_link;
310 struct snd_soc_dai *codec_dai;
311 unsigned int symmetry, i;
313 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
314 cpu_driver->symmetric_channels || link->symmetric_channels ||
315 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
317 for_each_rtd_codec_dai(rtd, i, codec_dai)
318 symmetry = symmetry ||
319 codec_dai->driver->symmetric_rates ||
320 codec_dai->driver->symmetric_channels ||
321 codec_dai->driver->symmetric_samplebits;
326 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
328 struct snd_soc_pcm_runtime *rtd = substream->private_data;
334 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
336 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
340 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
342 struct snd_soc_pcm_runtime *rtd = substream->private_data;
343 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
344 struct snd_soc_dai *codec_dai;
346 unsigned int bits = 0, cpu_bits;
348 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
349 for_each_rtd_codec_dai(rtd, i, codec_dai) {
350 if (codec_dai->driver->playback.sig_bits == 0) {
354 bits = max(codec_dai->driver->playback.sig_bits, bits);
356 cpu_bits = cpu_dai->driver->playback.sig_bits;
358 for_each_rtd_codec_dai(rtd, i, codec_dai) {
359 if (codec_dai->driver->capture.sig_bits == 0) {
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
365 cpu_bits = cpu_dai->driver->capture.sig_bits;
368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
372 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_pcm_hardware *hw = &runtime->hw;
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai *codec_dai;
378 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
379 struct snd_soc_dai_driver *codec_dai_drv;
380 struct snd_soc_pcm_stream *codec_stream;
381 struct snd_soc_pcm_stream *cpu_stream;
382 unsigned int chan_min = 0, chan_max = UINT_MAX;
383 unsigned int rate_min = 0, rate_max = UINT_MAX;
384 unsigned int rates = UINT_MAX;
385 u64 formats = ULLONG_MAX;
388 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
389 cpu_stream = &cpu_dai_drv->playback;
391 cpu_stream = &cpu_dai_drv->capture;
393 /* first calculate min/max only for CODECs in the DAI link */
394 for_each_rtd_codec_dai(rtd, i, codec_dai) {
397 * Skip CODECs which don't support the current stream type.
398 * Otherwise, since the rate, channel, and format values will
399 * zero in that case, we would have no usable settings left,
400 * causing the resulting setup to fail.
401 * At least one CODEC should match, otherwise we should have
402 * bailed out on a higher level, since there would be no
403 * CODEC to support the transfer direction in that case.
405 if (!snd_soc_dai_stream_valid(codec_dai,
409 codec_dai_drv = codec_dai->driver;
410 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
411 codec_stream = &codec_dai_drv->playback;
413 codec_stream = &codec_dai_drv->capture;
414 chan_min = max(chan_min, codec_stream->channels_min);
415 chan_max = min(chan_max, codec_stream->channels_max);
416 rate_min = max(rate_min, codec_stream->rate_min);
417 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
418 formats &= codec_stream->formats;
419 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
423 * chan min/max cannot be enforced if there are multiple CODEC DAIs
424 * connected to a single CPU DAI, use CPU DAI's directly and let
425 * channel allocation be fixed up later
427 if (rtd->num_codecs > 1) {
428 chan_min = cpu_stream->channels_min;
429 chan_max = cpu_stream->channels_max;
432 hw->channels_min = max(chan_min, cpu_stream->channels_min);
433 hw->channels_max = min(chan_max, cpu_stream->channels_max);
435 hw->formats &= formats & cpu_stream->formats;
437 hw->formats = formats & cpu_stream->formats;
438 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
440 snd_pcm_limit_hw_rates(runtime);
442 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
443 hw->rate_min = max(hw->rate_min, rate_min);
444 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
445 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
448 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
449 struct snd_soc_component *last)
451 struct snd_soc_pcm_runtime *rtd = substream->private_data;
452 struct snd_soc_rtdcom_list *rtdcom;
453 struct snd_soc_component *component;
455 for_each_rtdcom(rtd, rtdcom) {
456 component = rtdcom->component;
458 if (component == last)
461 if (!component->driver->ops ||
462 !component->driver->ops->close)
465 component->driver->ops->close(substream);
472 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
473 * then initialized and any private data can be allocated. This also calls
474 * startup for the cpu DAI, component, machine and codec DAI.
476 static int soc_pcm_open(struct snd_pcm_substream *substream)
478 struct snd_soc_pcm_runtime *rtd = substream->private_data;
479 struct snd_pcm_runtime *runtime = substream->runtime;
480 struct snd_soc_component *component;
481 struct snd_soc_rtdcom_list *rtdcom;
482 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
483 struct snd_soc_dai *codec_dai;
484 const char *codec_dai_name = "multicodec";
487 pinctrl_pm_select_default_state(cpu_dai->dev);
488 for_each_rtd_codec_dai(rtd, i, codec_dai)
489 pinctrl_pm_select_default_state(codec_dai->dev);
491 for_each_rtdcom(rtd, rtdcom) {
492 component = rtdcom->component;
494 pm_runtime_get_sync(component->dev);
497 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
499 /* startup the audio subsystem */
500 if (cpu_dai->driver->ops->startup) {
501 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
503 dev_err(cpu_dai->dev, "ASoC: can't open interface"
504 " %s: %d\n", cpu_dai->name, ret);
509 for_each_rtdcom(rtd, rtdcom) {
510 component = rtdcom->component;
512 if (!component->driver->ops ||
513 !component->driver->ops->open)
516 ret = component->driver->ops->open(substream);
518 dev_err(component->dev,
519 "ASoC: can't open component %s: %d\n",
520 component->name, ret);
526 for_each_rtd_codec_dai(rtd, i, codec_dai) {
527 if (codec_dai->driver->ops->startup) {
528 ret = codec_dai->driver->ops->startup(substream,
531 dev_err(codec_dai->dev,
532 "ASoC: can't open codec %s: %d\n",
533 codec_dai->name, ret);
538 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
539 codec_dai->tx_mask = 0;
541 codec_dai->rx_mask = 0;
544 if (rtd->dai_link->ops->startup) {
545 ret = rtd->dai_link->ops->startup(substream);
547 pr_err("ASoC: %s startup failed: %d\n",
548 rtd->dai_link->name, ret);
553 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
554 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
557 /* Check that the codec and cpu DAIs are compatible */
558 soc_pcm_init_runtime_hw(substream);
560 if (rtd->num_codecs == 1)
561 codec_dai_name = rtd->codec_dai->name;
563 if (soc_pcm_has_symmetry(substream))
564 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
567 if (!runtime->hw.rates) {
568 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
569 codec_dai_name, cpu_dai->name);
572 if (!runtime->hw.formats) {
573 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
574 codec_dai_name, cpu_dai->name);
577 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
578 runtime->hw.channels_min > runtime->hw.channels_max) {
579 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
580 codec_dai_name, cpu_dai->name);
584 soc_pcm_apply_msb(substream);
586 /* Symmetry only applies if we've already got an active stream. */
587 if (cpu_dai->active) {
588 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
593 for_each_rtd_codec_dai(rtd, i, codec_dai) {
594 if (codec_dai->active) {
595 ret = soc_pcm_apply_symmetry(substream, codec_dai);
601 pr_debug("ASoC: %s <-> %s info:\n",
602 codec_dai_name, cpu_dai->name);
603 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
604 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
605 runtime->hw.channels_max);
606 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
607 runtime->hw.rate_max);
611 snd_soc_runtime_activate(rtd, substream->stream);
613 mutex_unlock(&rtd->pcm_mutex);
617 if (rtd->dai_link->ops->shutdown)
618 rtd->dai_link->ops->shutdown(substream);
624 for_each_rtd_codec_dai_reverse(rtd, i, codec_dai) {
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_each_rtd_codec_dai(rtd, i, codec_dai) {
645 if (!codec_dai->active)
646 pinctrl_pm_select_sleep_state(codec_dai->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_each_rtd_codec_dai(rtd, i, codec_dai) {
705 if (!codec_dai->active)
709 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
711 if (cpu_dai->driver->ops->shutdown)
712 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
714 for_each_rtd_codec_dai(rtd, i, codec_dai) {
715 if (codec_dai->driver->ops->shutdown)
716 codec_dai->driver->ops->shutdown(substream, codec_dai);
719 if (rtd->dai_link->ops->shutdown)
720 rtd->dai_link->ops->shutdown(substream);
722 soc_pcm_components_close(substream, NULL);
724 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
725 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
726 /* powered down playback stream now */
727 snd_soc_dapm_stream_event(rtd,
728 SNDRV_PCM_STREAM_PLAYBACK,
729 SND_SOC_DAPM_STREAM_STOP);
731 /* start delayed pop wq here for playback streams */
733 queue_delayed_work(system_power_efficient_wq,
735 msecs_to_jiffies(rtd->pmdown_time));
738 /* capture streams can be powered down now */
739 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
740 SND_SOC_DAPM_STREAM_STOP);
743 mutex_unlock(&rtd->pcm_mutex);
745 for_each_rtdcom(rtd, rtdcom) {
746 component = rtdcom->component;
748 pm_runtime_mark_last_busy(component->dev);
749 pm_runtime_put_autosuspend(component->dev);
752 for_each_rtd_codec_dai(rtd, i, codec_dai) {
753 if (!codec_dai->active)
754 pinctrl_pm_select_sleep_state(codec_dai->dev);
756 if (!cpu_dai->active)
757 pinctrl_pm_select_sleep_state(cpu_dai->dev);
763 * Called by ALSA when the PCM substream is prepared, can set format, sample
764 * rate, etc. This function is non atomic and can be called multiple times,
765 * it can refer to the runtime info.
767 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
769 struct snd_soc_pcm_runtime *rtd = substream->private_data;
770 struct snd_soc_component *component;
771 struct snd_soc_rtdcom_list *rtdcom;
772 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
773 struct snd_soc_dai *codec_dai;
776 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
778 if (rtd->dai_link->ops->prepare) {
779 ret = rtd->dai_link->ops->prepare(substream);
781 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
787 for_each_rtdcom(rtd, rtdcom) {
788 component = rtdcom->component;
790 if (!component->driver->ops ||
791 !component->driver->ops->prepare)
794 ret = component->driver->ops->prepare(substream);
796 dev_err(component->dev,
797 "ASoC: platform prepare error: %d\n", ret);
802 for_each_rtd_codec_dai(rtd, i, codec_dai) {
803 if (codec_dai->driver->ops->prepare) {
804 ret = codec_dai->driver->ops->prepare(substream,
807 dev_err(codec_dai->dev,
808 "ASoC: codec DAI prepare error: %d\n",
815 if (cpu_dai->driver->ops->prepare) {
816 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
818 dev_err(cpu_dai->dev,
819 "ASoC: cpu DAI prepare error: %d\n", ret);
824 /* cancel any delayed stream shutdown that is pending */
825 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
828 cancel_delayed_work(&rtd->delayed_work);
831 snd_soc_dapm_stream_event(rtd, substream->stream,
832 SND_SOC_DAPM_STREAM_START);
834 for_each_rtd_codec_dai(rtd, i, codec_dai)
835 snd_soc_dai_digital_mute(codec_dai, 0,
837 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
840 mutex_unlock(&rtd->pcm_mutex);
844 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
847 struct snd_interval *interval;
848 int channels = hweight_long(mask);
850 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
851 interval->min = channels;
852 interval->max = channels;
855 int soc_dai_hw_params(struct snd_pcm_substream *substream,
856 struct snd_pcm_hw_params *params,
857 struct snd_soc_dai *dai)
859 struct snd_soc_pcm_runtime *rtd = substream->private_data;
862 /* perform any topology hw_params fixups before DAI */
863 if (rtd->dai_link->be_hw_params_fixup) {
864 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
867 "ASoC: hw_params topology fixup failed %d\n",
873 if (dai->driver->ops->hw_params) {
874 ret = dai->driver->ops->hw_params(substream, params, dai);
876 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
885 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
886 struct snd_soc_component *last)
888 struct snd_soc_pcm_runtime *rtd = substream->private_data;
889 struct snd_soc_rtdcom_list *rtdcom;
890 struct snd_soc_component *component;
892 for_each_rtdcom(rtd, rtdcom) {
893 component = rtdcom->component;
895 if (component == last)
898 if (!component->driver->ops ||
899 !component->driver->ops->hw_free)
902 component->driver->ops->hw_free(substream);
909 * Called by ALSA when the hardware params are set by application. This
910 * function can also be called multiple times and can allocate buffers
911 * (using snd_pcm_lib_* ). It's non-atomic.
913 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
914 struct snd_pcm_hw_params *params)
916 struct snd_soc_pcm_runtime *rtd = substream->private_data;
917 struct snd_soc_component *component;
918 struct snd_soc_rtdcom_list *rtdcom;
919 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
920 struct snd_soc_dai *codec_dai;
923 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
924 if (rtd->dai_link->ops->hw_params) {
925 ret = rtd->dai_link->ops->hw_params(substream, params);
927 dev_err(rtd->card->dev, "ASoC: machine hw_params"
928 " failed: %d\n", ret);
933 for_each_rtd_codec_dai(rtd, i, codec_dai) {
934 struct snd_pcm_hw_params codec_params;
937 * Skip CODECs which don't support the current stream type,
938 * the idea being that if a CODEC is not used for the currently
939 * set up transfer direction, it should not need to be
940 * configured, especially since the configuration used might
941 * not even be supported by that CODEC. There may be cases
942 * however where a CODEC needs to be set up although it is
943 * actually not being used for the transfer, e.g. if a
944 * capture-only CODEC is acting as an LRCLK and/or BCLK master
945 * for the DAI link including a playback-only CODEC.
946 * If this becomes necessary, we will have to augment the
947 * machine driver setup with information on how to act, so
948 * we can do the right thing here.
950 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
953 /* copy params for each codec */
954 codec_params = *params;
956 /* fixup params based on TDM slot masks */
957 if (codec_dai->tx_mask)
958 soc_pcm_codec_params_fixup(&codec_params,
960 if (codec_dai->rx_mask)
961 soc_pcm_codec_params_fixup(&codec_params,
964 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
968 codec_dai->rate = params_rate(&codec_params);
969 codec_dai->channels = params_channels(&codec_params);
970 codec_dai->sample_bits = snd_pcm_format_physical_width(
971 params_format(&codec_params));
974 ret = soc_dai_hw_params(substream, params, cpu_dai);
978 for_each_rtdcom(rtd, rtdcom) {
979 component = rtdcom->component;
981 if (!component->driver->ops ||
982 !component->driver->ops->hw_params)
985 ret = component->driver->ops->hw_params(substream, params);
987 dev_err(component->dev,
988 "ASoC: %s hw params failed: %d\n",
989 component->name, ret);
995 /* store the parameters for each DAIs */
996 cpu_dai->rate = params_rate(params);
997 cpu_dai->channels = params_channels(params);
998 cpu_dai->sample_bits =
999 snd_pcm_format_physical_width(params_format(params));
1001 ret = soc_pcm_params_symmetry(substream, params);
1005 mutex_unlock(&rtd->pcm_mutex);
1009 soc_pcm_components_hw_free(substream, component);
1011 if (cpu_dai->driver->ops->hw_free)
1012 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1015 i = rtd->num_codecs;
1018 for_each_rtd_codec_dai_reverse(rtd, i, codec_dai) {
1019 if (codec_dai->driver->ops->hw_free)
1020 codec_dai->driver->ops->hw_free(substream, codec_dai);
1021 codec_dai->rate = 0;
1024 if (rtd->dai_link->ops->hw_free)
1025 rtd->dai_link->ops->hw_free(substream);
1027 mutex_unlock(&rtd->pcm_mutex);
1032 * Frees resources allocated by hw_params, can be called multiple times
1034 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1036 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1037 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1038 struct snd_soc_dai *codec_dai;
1039 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1042 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1044 /* clear the corresponding DAIs parameters when going to be inactive */
1045 if (cpu_dai->active == 1) {
1047 cpu_dai->channels = 0;
1048 cpu_dai->sample_bits = 0;
1051 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1052 if (codec_dai->active == 1) {
1053 codec_dai->rate = 0;
1054 codec_dai->channels = 0;
1055 codec_dai->sample_bits = 0;
1059 /* apply codec digital mute */
1060 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1061 if ((playback && codec_dai->playback_active == 1) ||
1062 (!playback && codec_dai->capture_active == 1))
1063 snd_soc_dai_digital_mute(codec_dai, 1,
1067 /* free any machine hw params */
1068 if (rtd->dai_link->ops->hw_free)
1069 rtd->dai_link->ops->hw_free(substream);
1071 /* free any component resources */
1072 soc_pcm_components_hw_free(substream, NULL);
1074 /* now free hw params for the DAIs */
1075 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1076 if (codec_dai->driver->ops->hw_free)
1077 codec_dai->driver->ops->hw_free(substream, codec_dai);
1080 if (cpu_dai->driver->ops->hw_free)
1081 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1083 mutex_unlock(&rtd->pcm_mutex);
1087 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1089 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1090 struct snd_soc_component *component;
1091 struct snd_soc_rtdcom_list *rtdcom;
1092 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1093 struct snd_soc_dai *codec_dai;
1096 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1097 if (codec_dai->driver->ops->trigger) {
1098 ret = codec_dai->driver->ops->trigger(substream,
1105 for_each_rtdcom(rtd, rtdcom) {
1106 component = rtdcom->component;
1108 if (!component->driver->ops ||
1109 !component->driver->ops->trigger)
1112 ret = component->driver->ops->trigger(substream, cmd);
1117 if (cpu_dai->driver->ops->trigger) {
1118 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1123 if (rtd->dai_link->ops->trigger) {
1124 ret = rtd->dai_link->ops->trigger(substream, cmd);
1132 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1135 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1136 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1137 struct snd_soc_dai *codec_dai;
1140 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1141 if (codec_dai->driver->ops->bespoke_trigger) {
1142 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1149 if (cpu_dai->driver->ops->bespoke_trigger) {
1150 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1157 * soc level wrapper for pointer callback
1158 * If cpu_dai, codec_dai, component driver has the delay callback, then
1159 * the runtime->delay will be updated accordingly.
1161 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1163 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1164 struct snd_soc_component *component;
1165 struct snd_soc_rtdcom_list *rtdcom;
1166 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1167 struct snd_soc_dai *codec_dai;
1168 struct snd_pcm_runtime *runtime = substream->runtime;
1169 snd_pcm_uframes_t offset = 0;
1170 snd_pcm_sframes_t delay = 0;
1171 snd_pcm_sframes_t codec_delay = 0;
1174 /* clearing the previous total delay */
1177 for_each_rtdcom(rtd, rtdcom) {
1178 component = rtdcom->component;
1180 if (!component->driver->ops ||
1181 !component->driver->ops->pointer)
1184 /* FIXME: use 1st pointer */
1185 offset = component->driver->ops->pointer(substream);
1188 /* base delay if assigned in pointer callback */
1189 delay = runtime->delay;
1191 if (cpu_dai->driver->ops->delay)
1192 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1194 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1195 if (codec_dai->driver->ops->delay)
1196 codec_delay = max(codec_delay,
1197 codec_dai->driver->ops->delay(substream,
1200 delay += codec_delay;
1202 runtime->delay = delay;
1207 /* connect a FE and BE */
1208 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1209 struct snd_soc_pcm_runtime *be, int stream)
1211 struct snd_soc_dpcm *dpcm;
1213 /* only add new dpcms */
1214 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1215 if (dpcm->be == be && dpcm->fe == fe)
1219 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1225 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1226 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1227 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1228 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1230 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1231 stream ? "capture" : "playback", fe->dai_link->name,
1232 stream ? "<-" : "->", be->dai_link->name);
1234 #ifdef CONFIG_DEBUG_FS
1235 if (fe->debugfs_dpcm_root)
1236 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1237 fe->debugfs_dpcm_root, &dpcm->state);
1242 /* reparent a BE onto another FE */
1243 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1244 struct snd_soc_pcm_runtime *be, int stream)
1246 struct snd_soc_dpcm *dpcm;
1247 struct snd_pcm_substream *fe_substream, *be_substream;
1249 /* reparent if BE is connected to other FEs */
1250 if (!be->dpcm[stream].users)
1253 be_substream = snd_soc_dpcm_get_substream(be, stream);
1255 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1259 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1260 stream ? "capture" : "playback",
1261 dpcm->fe->dai_link->name,
1262 stream ? "<-" : "->", dpcm->be->dai_link->name);
1264 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1265 be_substream->runtime = fe_substream->runtime;
1270 /* disconnect a BE and FE */
1271 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1273 struct snd_soc_dpcm *dpcm, *d;
1275 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1276 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1277 stream ? "capture" : "playback",
1278 dpcm->be->dai_link->name);
1280 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1283 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1284 stream ? "capture" : "playback", fe->dai_link->name,
1285 stream ? "<-" : "->", dpcm->be->dai_link->name);
1287 /* BEs still alive need new FE */
1288 dpcm_be_reparent(fe, dpcm->be, stream);
1290 #ifdef CONFIG_DEBUG_FS
1291 debugfs_remove(dpcm->debugfs_state);
1293 list_del(&dpcm->list_be);
1294 list_del(&dpcm->list_fe);
1299 /* get BE for DAI widget and stream */
1300 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1301 struct snd_soc_dapm_widget *widget, int stream)
1303 struct snd_soc_pcm_runtime *be;
1306 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1308 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1309 list_for_each_entry(be, &card->rtd_list, list) {
1311 if (!be->dai_link->no_pcm)
1314 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1315 be->cpu_dai->playback_widget ?
1316 be->cpu_dai->playback_widget->name : "(not set)");
1318 if (be->cpu_dai->playback_widget == widget)
1321 for (i = 0; i < be->num_codecs; i++) {
1322 struct snd_soc_dai *dai = be->codec_dais[i];
1323 if (dai->playback_widget == widget)
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->capture_widget ?
1336 be->cpu_dai->capture_widget->name : "(not set)");
1338 if (be->cpu_dai->capture_widget == widget)
1341 for (i = 0; i < be->num_codecs; i++) {
1342 struct snd_soc_dai *dai = be->codec_dais[i];
1343 if (dai->capture_widget == widget)
1349 /* dai link name and stream name set correctly ? */
1350 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1351 stream ? "capture" : "playback", widget->name);
1355 static inline struct snd_soc_dapm_widget *
1356 dai_get_widget(struct snd_soc_dai *dai, int stream)
1358 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1359 return dai->playback_widget;
1361 return dai->capture_widget;
1364 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1365 struct snd_soc_dapm_widget *widget)
1369 for (i = 0; i < list->num_widgets; i++) {
1370 if (widget == list->widgets[i])
1377 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1378 enum snd_soc_dapm_direction dir)
1380 struct snd_soc_card *card = widget->dapm->card;
1381 struct snd_soc_pcm_runtime *rtd;
1382 struct snd_soc_dai *dai;
1385 if (dir == SND_SOC_DAPM_DIR_OUT) {
1386 list_for_each_entry(rtd, &card->rtd_list, list) {
1387 if (!rtd->dai_link->no_pcm)
1390 if (rtd->cpu_dai->playback_widget == widget)
1393 for_each_rtd_codec_dai(rtd, i, dai) {
1394 if (dai->playback_widget == widget)
1398 } else { /* SND_SOC_DAPM_DIR_IN */
1399 list_for_each_entry(rtd, &card->rtd_list, list) {
1400 if (!rtd->dai_link->no_pcm)
1403 if (rtd->cpu_dai->capture_widget == widget)
1406 for_each_rtd_codec_dai(rtd, i, dai) {
1407 if (dai->capture_widget == widget)
1416 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1417 int stream, struct snd_soc_dapm_widget_list **list)
1419 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1422 /* get number of valid DAI paths and their widgets */
1423 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1424 dpcm_end_walk_at_be);
1426 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1427 stream ? "capture" : "playback");
1432 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1433 struct snd_soc_dapm_widget_list **list_)
1435 struct snd_soc_dpcm *dpcm;
1436 struct snd_soc_dapm_widget_list *list = *list_;
1437 struct snd_soc_dapm_widget *widget;
1440 /* Destroy any old FE <--> BE connections */
1441 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1444 /* is there a valid CPU DAI widget for this BE */
1445 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1447 /* prune the BE if it's no longer in our active list */
1448 if (widget && widget_in_list(list, widget))
1451 /* is there a valid CODEC DAI widget for this BE */
1452 for (i = 0; i < dpcm->be->num_codecs; i++) {
1453 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1454 widget = dai_get_widget(dai, stream);
1456 /* prune the BE if it's no longer in our active list */
1457 if (widget && widget_in_list(list, widget))
1461 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1462 stream ? "capture" : "playback",
1463 dpcm->be->dai_link->name, fe->dai_link->name);
1464 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1465 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1469 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1473 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1474 struct snd_soc_dapm_widget_list **list_)
1476 struct snd_soc_card *card = fe->card;
1477 struct snd_soc_dapm_widget_list *list = *list_;
1478 struct snd_soc_pcm_runtime *be;
1479 int i, new = 0, err;
1481 /* Create any new FE <--> BE connections */
1482 for (i = 0; i < list->num_widgets; i++) {
1484 switch (list->widgets[i]->id) {
1485 case snd_soc_dapm_dai_in:
1486 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1489 case snd_soc_dapm_dai_out:
1490 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1497 /* is there a valid BE rtd for this widget */
1498 be = dpcm_get_be(card, list->widgets[i], stream);
1500 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1501 list->widgets[i]->name);
1505 /* make sure BE is a real BE */
1506 if (!be->dai_link->no_pcm)
1509 /* don't connect if FE is not running */
1510 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1513 /* newly connected FE and BE */
1514 err = dpcm_be_connect(fe, be, stream);
1516 dev_err(fe->dev, "ASoC: can't connect %s\n",
1517 list->widgets[i]->name);
1519 } else if (err == 0) /* already connected */
1523 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1527 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1532 * Find the corresponding BE DAIs that source or sink audio to this
1535 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1536 int stream, struct snd_soc_dapm_widget_list **list, int new)
1539 return dpcm_add_paths(fe, stream, list);
1541 return dpcm_prune_paths(fe, stream, list);
1544 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1546 struct snd_soc_dpcm *dpcm;
1548 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1549 dpcm->be->dpcm[stream].runtime_update =
1550 SND_SOC_DPCM_UPDATE_NO;
1553 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1556 struct snd_soc_dpcm *dpcm;
1558 /* disable any enabled and non active backends */
1559 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1561 struct snd_soc_pcm_runtime *be = dpcm->be;
1562 struct snd_pcm_substream *be_substream =
1563 snd_soc_dpcm_get_substream(be, stream);
1565 if (be->dpcm[stream].users == 0)
1566 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1567 stream ? "capture" : "playback",
1568 be->dpcm[stream].state);
1570 if (--be->dpcm[stream].users != 0)
1573 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1576 soc_pcm_close(be_substream);
1577 be_substream->runtime = NULL;
1578 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1582 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1584 struct snd_soc_dpcm *dpcm;
1587 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1588 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1590 struct snd_soc_pcm_runtime *be = dpcm->be;
1591 struct snd_pcm_substream *be_substream =
1592 snd_soc_dpcm_get_substream(be, stream);
1594 if (!be_substream) {
1595 dev_err(be->dev, "ASoC: no backend %s stream\n",
1596 stream ? "capture" : "playback");
1600 /* is this op for this BE ? */
1601 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1604 /* first time the dpcm is open ? */
1605 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1606 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1607 stream ? "capture" : "playback",
1608 be->dpcm[stream].state);
1610 if (be->dpcm[stream].users++ != 0)
1613 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1614 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1617 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1618 stream ? "capture" : "playback", be->dai_link->name);
1620 be_substream->runtime = be->dpcm[stream].runtime;
1621 err = soc_pcm_open(be_substream);
1623 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1624 be->dpcm[stream].users--;
1625 if (be->dpcm[stream].users < 0)
1626 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1627 stream ? "capture" : "playback",
1628 be->dpcm[stream].state);
1630 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1634 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1641 /* disable any enabled and non active backends */
1642 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1643 struct snd_soc_pcm_runtime *be = dpcm->be;
1644 struct snd_pcm_substream *be_substream =
1645 snd_soc_dpcm_get_substream(be, stream);
1647 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1650 if (be->dpcm[stream].users == 0)
1651 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1652 stream ? "capture" : "playback",
1653 be->dpcm[stream].state);
1655 if (--be->dpcm[stream].users != 0)
1658 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1661 soc_pcm_close(be_substream);
1662 be_substream->runtime = NULL;
1663 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1669 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1670 struct snd_soc_pcm_stream *stream)
1672 runtime->hw.rate_min = stream->rate_min;
1673 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1674 runtime->hw.channels_min = stream->channels_min;
1675 runtime->hw.channels_max = stream->channels_max;
1676 if (runtime->hw.formats)
1677 runtime->hw.formats &= stream->formats;
1679 runtime->hw.formats = stream->formats;
1680 runtime->hw.rates = stream->rates;
1683 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1686 struct snd_soc_pcm_runtime *fe = substream->private_data;
1687 struct snd_soc_dpcm *dpcm;
1688 int stream = substream->stream;
1690 if (!fe->dai_link->dpcm_merged_format)
1694 * It returns merged BE codec format
1695 * if FE want to use it (= dpcm_merged_format)
1698 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1699 struct snd_soc_pcm_runtime *be = dpcm->be;
1700 struct snd_soc_dai_driver *codec_dai_drv;
1701 struct snd_soc_pcm_stream *codec_stream;
1704 for (i = 0; i < be->num_codecs; i++) {
1706 * Skip CODECs which don't support the current stream
1707 * type. See soc_pcm_init_runtime_hw() for more details
1709 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1713 codec_dai_drv = be->codec_dais[i]->driver;
1714 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1715 codec_stream = &codec_dai_drv->playback;
1717 codec_stream = &codec_dai_drv->capture;
1719 *formats &= codec_stream->formats;
1724 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1725 unsigned int *channels_min,
1726 unsigned int *channels_max)
1728 struct snd_soc_pcm_runtime *fe = substream->private_data;
1729 struct snd_soc_dpcm *dpcm;
1730 int stream = substream->stream;
1732 if (!fe->dai_link->dpcm_merged_chan)
1736 * It returns merged BE codec channel;
1737 * if FE want to use it (= dpcm_merged_chan)
1740 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1741 struct snd_soc_pcm_runtime *be = dpcm->be;
1742 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1743 struct snd_soc_dai_driver *codec_dai_drv;
1744 struct snd_soc_pcm_stream *codec_stream;
1745 struct snd_soc_pcm_stream *cpu_stream;
1747 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1748 cpu_stream = &cpu_dai_drv->playback;
1750 cpu_stream = &cpu_dai_drv->capture;
1752 *channels_min = max(*channels_min, cpu_stream->channels_min);
1753 *channels_max = min(*channels_max, cpu_stream->channels_max);
1756 * chan min/max cannot be enforced if there are multiple CODEC
1757 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1759 if (be->num_codecs == 1) {
1760 codec_dai_drv = be->codec_dais[0]->driver;
1762 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1763 codec_stream = &codec_dai_drv->playback;
1765 codec_stream = &codec_dai_drv->capture;
1767 *channels_min = max(*channels_min,
1768 codec_stream->channels_min);
1769 *channels_max = min(*channels_max,
1770 codec_stream->channels_max);
1775 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1776 unsigned int *rates,
1777 unsigned int *rate_min,
1778 unsigned int *rate_max)
1780 struct snd_soc_pcm_runtime *fe = substream->private_data;
1781 struct snd_soc_dpcm *dpcm;
1782 int stream = substream->stream;
1784 if (!fe->dai_link->dpcm_merged_rate)
1788 * It returns merged BE codec channel;
1789 * if FE want to use it (= dpcm_merged_chan)
1792 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1793 struct snd_soc_pcm_runtime *be = dpcm->be;
1794 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1795 struct snd_soc_dai_driver *codec_dai_drv;
1796 struct snd_soc_pcm_stream *codec_stream;
1797 struct snd_soc_pcm_stream *cpu_stream;
1800 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1801 cpu_stream = &cpu_dai_drv->playback;
1803 cpu_stream = &cpu_dai_drv->capture;
1805 *rate_min = max(*rate_min, cpu_stream->rate_min);
1806 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1807 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1809 for (i = 0; i < be->num_codecs; i++) {
1811 * Skip CODECs which don't support the current stream
1812 * type. See soc_pcm_init_runtime_hw() for more details
1814 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1818 codec_dai_drv = be->codec_dais[i]->driver;
1819 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1820 codec_stream = &codec_dai_drv->playback;
1822 codec_stream = &codec_dai_drv->capture;
1824 *rate_min = max(*rate_min, codec_stream->rate_min);
1825 *rate_max = min_not_zero(*rate_max,
1826 codec_stream->rate_max);
1827 *rates = snd_pcm_rate_mask_intersect(*rates,
1828 codec_stream->rates);
1833 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1835 struct snd_pcm_runtime *runtime = substream->runtime;
1836 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1837 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1838 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1841 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1843 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1845 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1846 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1847 &runtime->hw.channels_max);
1848 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1849 &runtime->hw.rate_min, &runtime->hw.rate_max);
1852 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1854 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1855 * for avoiding the race with trigger callback.
1856 * If the state is unset and a trigger is pending while the previous operation,
1857 * process the pending trigger action here.
1859 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1860 int stream, enum snd_soc_dpcm_update state)
1862 struct snd_pcm_substream *substream =
1863 snd_soc_dpcm_get_substream(fe, stream);
1865 snd_pcm_stream_lock_irq(substream);
1866 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1867 dpcm_fe_dai_do_trigger(substream,
1868 fe->dpcm[stream].trigger_pending - 1);
1869 fe->dpcm[stream].trigger_pending = 0;
1871 fe->dpcm[stream].runtime_update = state;
1872 snd_pcm_stream_unlock_irq(substream);
1875 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1878 struct snd_soc_dpcm *dpcm;
1879 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1880 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1883 /* apply symmetry for FE */
1884 if (soc_pcm_has_symmetry(fe_substream))
1885 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1887 /* Symmetry only applies if we've got an active stream. */
1888 if (fe_cpu_dai->active) {
1889 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1894 /* apply symmetry for BE */
1895 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1896 struct snd_soc_pcm_runtime *be = dpcm->be;
1897 struct snd_pcm_substream *be_substream =
1898 snd_soc_dpcm_get_substream(be, stream);
1899 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1900 struct snd_soc_dai *codec_dai;
1903 if (rtd->dai_link->be_hw_params_fixup)
1906 if (soc_pcm_has_symmetry(be_substream))
1907 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1909 /* Symmetry only applies if we've got an active stream. */
1910 if (rtd->cpu_dai->active) {
1911 err = soc_pcm_apply_symmetry(fe_substream,
1917 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1918 if (codec_dai->active) {
1919 err = soc_pcm_apply_symmetry(fe_substream,
1930 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1932 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1933 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1934 int stream = fe_substream->stream, ret = 0;
1936 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1938 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1940 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1944 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1946 /* start the DAI frontend */
1947 ret = soc_pcm_open(fe_substream);
1949 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1953 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1955 dpcm_set_fe_runtime(fe_substream);
1956 snd_pcm_limit_hw_rates(runtime);
1958 ret = dpcm_apply_symmetry(fe_substream, stream);
1960 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1965 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1969 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1971 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1975 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1977 struct snd_soc_dpcm *dpcm;
1979 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1980 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1982 struct snd_soc_pcm_runtime *be = dpcm->be;
1983 struct snd_pcm_substream *be_substream =
1984 snd_soc_dpcm_get_substream(be, stream);
1986 /* is this op for this BE ? */
1987 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1990 if (be->dpcm[stream].users == 0)
1991 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1992 stream ? "capture" : "playback",
1993 be->dpcm[stream].state);
1995 if (--be->dpcm[stream].users != 0)
1998 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1999 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2000 soc_pcm_hw_free(be_substream);
2001 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2004 dev_dbg(be->dev, "ASoC: close BE %s\n",
2005 be->dai_link->name);
2007 soc_pcm_close(be_substream);
2008 be_substream->runtime = NULL;
2010 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2015 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2017 struct snd_soc_pcm_runtime *fe = substream->private_data;
2018 int stream = substream->stream;
2020 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2022 /* shutdown the BEs */
2023 dpcm_be_dai_shutdown(fe, substream->stream);
2025 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2027 /* now shutdown the frontend */
2028 soc_pcm_close(substream);
2030 /* run the stream event for each BE */
2031 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2033 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2034 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2038 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2040 struct snd_soc_dpcm *dpcm;
2042 /* only hw_params backends that are either sinks or sources
2043 * to this frontend DAI */
2044 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2046 struct snd_soc_pcm_runtime *be = dpcm->be;
2047 struct snd_pcm_substream *be_substream =
2048 snd_soc_dpcm_get_substream(be, stream);
2050 /* is this op for this BE ? */
2051 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2054 /* only free hw when no longer used - check all FEs */
2055 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2058 /* do not free hw if this BE is used by other FE */
2059 if (be->dpcm[stream].users > 1)
2062 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2063 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2064 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2065 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2066 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2067 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2070 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2071 be->dai_link->name);
2073 soc_pcm_hw_free(be_substream);
2075 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2081 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2083 struct snd_soc_pcm_runtime *fe = substream->private_data;
2084 int err, stream = substream->stream;
2086 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2087 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2089 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2091 /* call hw_free on the frontend */
2092 err = soc_pcm_hw_free(substream);
2094 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2095 fe->dai_link->name);
2097 /* only hw_params backends that are either sinks or sources
2098 * to this frontend DAI */
2099 err = dpcm_be_dai_hw_free(fe, stream);
2101 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2102 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2104 mutex_unlock(&fe->card->mutex);
2108 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2110 struct snd_soc_dpcm *dpcm;
2113 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2115 struct snd_soc_pcm_runtime *be = dpcm->be;
2116 struct snd_pcm_substream *be_substream =
2117 snd_soc_dpcm_get_substream(be, stream);
2119 /* is this op for this BE ? */
2120 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2123 /* copy params for each dpcm */
2124 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2125 sizeof(struct snd_pcm_hw_params));
2127 /* perform any hw_params fixups */
2128 if (be->dai_link->be_hw_params_fixup) {
2129 ret = be->dai_link->be_hw_params_fixup(be,
2133 "ASoC: hw_params BE fixup failed %d\n",
2139 /* only allow hw_params() if no connected FEs are running */
2140 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2143 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2144 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2145 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2148 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2149 be->dai_link->name);
2151 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2153 dev_err(dpcm->be->dev,
2154 "ASoC: hw_params BE failed %d\n", ret);
2158 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2163 /* disable any enabled and non active backends */
2164 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2165 struct snd_soc_pcm_runtime *be = dpcm->be;
2166 struct snd_pcm_substream *be_substream =
2167 snd_soc_dpcm_get_substream(be, stream);
2169 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2172 /* only allow hw_free() if no connected FEs are running */
2173 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2176 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2177 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2178 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2179 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2182 soc_pcm_hw_free(be_substream);
2188 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2189 struct snd_pcm_hw_params *params)
2191 struct snd_soc_pcm_runtime *fe = substream->private_data;
2192 int ret, stream = substream->stream;
2194 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2195 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2197 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2198 sizeof(struct snd_pcm_hw_params));
2199 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2201 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2205 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2206 fe->dai_link->name, params_rate(params),
2207 params_channels(params), params_format(params));
2209 /* call hw_params on the frontend */
2210 ret = soc_pcm_hw_params(substream, params);
2212 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2213 dpcm_be_dai_hw_free(fe, stream);
2215 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2218 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2219 mutex_unlock(&fe->card->mutex);
2223 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2224 struct snd_pcm_substream *substream, int cmd)
2228 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2229 dpcm->be->dai_link->name, cmd);
2231 ret = soc_pcm_trigger(substream, cmd);
2233 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2238 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2241 struct snd_soc_dpcm *dpcm;
2244 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2246 struct snd_soc_pcm_runtime *be = dpcm->be;
2247 struct snd_pcm_substream *be_substream =
2248 snd_soc_dpcm_get_substream(be, stream);
2250 /* is this op for this BE ? */
2251 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2255 case SNDRV_PCM_TRIGGER_START:
2256 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2257 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2260 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2264 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2266 case SNDRV_PCM_TRIGGER_RESUME:
2267 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2270 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2274 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2276 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2277 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2280 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2284 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2286 case SNDRV_PCM_TRIGGER_STOP:
2287 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2290 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2293 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2297 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2299 case SNDRV_PCM_TRIGGER_SUSPEND:
2300 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2303 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2306 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2310 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2312 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2313 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2316 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2319 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2323 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2330 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2332 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2334 struct snd_soc_pcm_runtime *fe = substream->private_data;
2335 int stream = substream->stream, ret;
2336 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2338 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2341 case SND_SOC_DPCM_TRIGGER_PRE:
2342 /* call trigger on the frontend before the backend. */
2344 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2345 fe->dai_link->name, cmd);
2347 ret = soc_pcm_trigger(substream, cmd);
2349 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2353 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2355 case SND_SOC_DPCM_TRIGGER_POST:
2356 /* call trigger on the frontend after the backend. */
2358 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2360 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2364 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2365 fe->dai_link->name, cmd);
2367 ret = soc_pcm_trigger(substream, cmd);
2369 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2370 /* bespoke trigger() - handles both FE and BEs */
2372 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2373 fe->dai_link->name, cmd);
2375 ret = soc_pcm_bespoke_trigger(substream, cmd);
2377 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2382 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2383 fe->dai_link->name);
2389 case SNDRV_PCM_TRIGGER_START:
2390 case SNDRV_PCM_TRIGGER_RESUME:
2391 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2392 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2394 case SNDRV_PCM_TRIGGER_STOP:
2395 case SNDRV_PCM_TRIGGER_SUSPEND:
2396 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2398 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2399 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2404 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2408 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2410 struct snd_soc_pcm_runtime *fe = substream->private_data;
2411 int stream = substream->stream;
2413 /* if FE's runtime_update is already set, we're in race;
2414 * process this trigger later at exit
2416 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2417 fe->dpcm[stream].trigger_pending = cmd + 1;
2418 return 0; /* delayed, assuming it's successful */
2421 /* we're alone, let's trigger */
2422 return dpcm_fe_dai_do_trigger(substream, cmd);
2425 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2427 struct snd_soc_dpcm *dpcm;
2430 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2432 struct snd_soc_pcm_runtime *be = dpcm->be;
2433 struct snd_pcm_substream *be_substream =
2434 snd_soc_dpcm_get_substream(be, stream);
2436 /* is this op for this BE ? */
2437 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2440 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2441 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2442 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2445 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2446 be->dai_link->name);
2448 ret = soc_pcm_prepare(be_substream);
2450 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2455 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2460 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2462 struct snd_soc_pcm_runtime *fe = substream->private_data;
2463 int stream = substream->stream, ret = 0;
2465 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2467 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2469 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2471 /* there is no point preparing this FE if there are no BEs */
2472 if (list_empty(&fe->dpcm[stream].be_clients)) {
2473 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2474 fe->dai_link->name);
2479 ret = dpcm_be_dai_prepare(fe, substream->stream);
2483 /* call prepare on the frontend */
2484 ret = soc_pcm_prepare(substream);
2486 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2487 fe->dai_link->name);
2491 /* run the stream event for each BE */
2492 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2493 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2496 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2497 mutex_unlock(&fe->card->mutex);
2502 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2503 unsigned int cmd, void *arg)
2505 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2506 struct snd_soc_component *component;
2507 struct snd_soc_rtdcom_list *rtdcom;
2509 for_each_rtdcom(rtd, rtdcom) {
2510 component = rtdcom->component;
2512 if (!component->driver->ops ||
2513 !component->driver->ops->ioctl)
2516 /* FIXME: use 1st ioctl */
2517 return component->driver->ops->ioctl(substream, cmd, arg);
2520 return snd_pcm_lib_ioctl(substream, cmd, arg);
2523 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2525 struct snd_pcm_substream *substream =
2526 snd_soc_dpcm_get_substream(fe, stream);
2527 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2530 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2531 stream ? "capture" : "playback", fe->dai_link->name);
2533 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2534 /* call bespoke trigger - FE takes care of all BE triggers */
2535 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2536 fe->dai_link->name);
2538 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2540 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2542 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2543 fe->dai_link->name);
2545 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2547 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2550 err = dpcm_be_dai_hw_free(fe, stream);
2552 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2554 err = dpcm_be_dai_shutdown(fe, stream);
2556 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2558 /* run the stream event for each BE */
2559 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2564 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2566 struct snd_pcm_substream *substream =
2567 snd_soc_dpcm_get_substream(fe, stream);
2568 struct snd_soc_dpcm *dpcm;
2569 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2572 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2573 stream ? "capture" : "playback", fe->dai_link->name);
2575 /* Only start the BE if the FE is ready */
2576 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2577 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2580 /* startup must always be called for new BEs */
2581 ret = dpcm_be_dai_startup(fe, stream);
2585 /* keep going if FE state is > open */
2586 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2589 ret = dpcm_be_dai_hw_params(fe, stream);
2593 /* keep going if FE state is > hw_params */
2594 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2598 ret = dpcm_be_dai_prepare(fe, stream);
2602 /* run the stream event for each BE */
2603 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2605 /* keep going if FE state is > prepare */
2606 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2607 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2610 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2611 /* call trigger on the frontend - FE takes care of all BE triggers */
2612 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2613 fe->dai_link->name);
2615 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2617 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2621 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2622 fe->dai_link->name);
2624 ret = dpcm_be_dai_trigger(fe, stream,
2625 SNDRV_PCM_TRIGGER_START);
2627 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2635 dpcm_be_dai_hw_free(fe, stream);
2637 dpcm_be_dai_shutdown(fe, stream);
2639 /* disconnect any non started BEs */
2640 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2641 struct snd_soc_pcm_runtime *be = dpcm->be;
2642 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2643 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2649 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2653 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2654 ret = dpcm_run_update_startup(fe, stream);
2656 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2657 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2662 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2666 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2667 ret = dpcm_run_update_shutdown(fe, stream);
2669 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2670 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2675 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2677 struct snd_soc_dapm_widget_list *list;
2680 if (!fe->dai_link->dynamic)
2683 /* only check active links */
2684 if (!fe->cpu_dai->active)
2687 /* DAPM sync will call this to update DSP paths */
2688 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2689 new ? "new" : "old", fe->dai_link->name);
2691 /* skip if FE doesn't have playback capability */
2692 if (!fe->cpu_dai->driver->playback.channels_min ||
2693 !fe->codec_dai->driver->playback.channels_min)
2696 /* skip if FE isn't currently playing */
2697 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2700 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2702 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2703 fe->dai_link->name, "playback");
2707 /* update any playback paths */
2708 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2711 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2713 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2715 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2716 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2719 dpcm_path_put(&list);
2722 /* skip if FE doesn't have capture capability */
2723 if (!fe->cpu_dai->driver->capture.channels_min ||
2724 !fe->codec_dai->driver->capture.channels_min)
2727 /* skip if FE isn't currently capturing */
2728 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2731 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2733 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2734 fe->dai_link->name, "capture");
2738 /* update any old capture paths */
2739 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2742 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2744 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2746 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2747 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2750 dpcm_path_put(&list);
2755 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2758 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2760 struct snd_soc_pcm_runtime *fe;
2763 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2764 /* shutdown all old paths first */
2765 list_for_each_entry(fe, &card->rtd_list, list) {
2766 ret = soc_dpcm_fe_runtime_update(fe, 0);
2771 /* bring new paths up */
2772 list_for_each_entry(fe, &card->rtd_list, list) {
2773 ret = soc_dpcm_fe_runtime_update(fe, 1);
2779 mutex_unlock(&card->mutex);
2782 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2784 struct snd_soc_dpcm *dpcm;
2785 struct list_head *clients =
2786 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2788 list_for_each_entry(dpcm, clients, list_be) {
2790 struct snd_soc_pcm_runtime *be = dpcm->be;
2793 if (be->dai_link->ignore_suspend)
2796 for (i = 0; i < be->num_codecs; i++) {
2797 struct snd_soc_dai *dai = be->codec_dais[i];
2798 struct snd_soc_dai_driver *drv = dai->driver;
2800 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2801 be->dai_link->name);
2803 if (drv->ops && drv->ops->digital_mute &&
2804 dai->playback_active)
2805 drv->ops->digital_mute(dai, mute);
2812 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2814 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2815 struct snd_soc_dpcm *dpcm;
2816 struct snd_soc_dapm_widget_list *list;
2818 int stream = fe_substream->stream;
2820 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2821 fe->dpcm[stream].runtime = fe_substream->runtime;
2823 ret = dpcm_path_get(fe, stream, &list);
2825 mutex_unlock(&fe->card->mutex);
2827 } else if (ret == 0) {
2828 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2829 fe->dai_link->name, stream ? "capture" : "playback");
2832 /* calculate valid and active FE <-> BE dpcms */
2833 dpcm_process_paths(fe, stream, &list, 1);
2835 ret = dpcm_fe_dai_startup(fe_substream);
2837 /* clean up all links */
2838 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2839 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2841 dpcm_be_disconnect(fe, stream);
2842 fe->dpcm[stream].runtime = NULL;
2845 dpcm_clear_pending_state(fe, stream);
2846 dpcm_path_put(&list);
2847 mutex_unlock(&fe->card->mutex);
2851 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2853 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2854 struct snd_soc_dpcm *dpcm;
2855 int stream = fe_substream->stream, ret;
2857 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2858 ret = dpcm_fe_dai_shutdown(fe_substream);
2860 /* mark FE's links ready to prune */
2861 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2862 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2864 dpcm_be_disconnect(fe, stream);
2866 fe->dpcm[stream].runtime = NULL;
2867 mutex_unlock(&fe->card->mutex);
2871 static void soc_pcm_private_free(struct snd_pcm *pcm)
2873 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2874 struct snd_soc_rtdcom_list *rtdcom;
2875 struct snd_soc_component *component;
2877 /* need to sync the delayed work before releasing resources */
2878 flush_delayed_work(&rtd->delayed_work);
2879 for_each_rtdcom(rtd, rtdcom) {
2880 component = rtdcom->component;
2882 if (component->driver->pcm_free)
2883 component->driver->pcm_free(pcm);
2887 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2889 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2890 struct snd_soc_rtdcom_list *rtdcom;
2891 struct snd_soc_component *component;
2893 for_each_rtdcom(rtd, rtdcom) {
2894 component = rtdcom->component;
2896 if (!component->driver->ops ||
2897 !component->driver->ops->ack)
2900 /* FIXME. it returns 1st ask now */
2901 return component->driver->ops->ack(substream);
2907 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2908 unsigned long pos, void __user *buf,
2909 unsigned long bytes)
2911 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2912 struct snd_soc_rtdcom_list *rtdcom;
2913 struct snd_soc_component *component;
2915 for_each_rtdcom(rtd, rtdcom) {
2916 component = rtdcom->component;
2918 if (!component->driver->ops ||
2919 !component->driver->ops->copy_user)
2922 /* FIXME. it returns 1st copy now */
2923 return component->driver->ops->copy_user(substream, channel,
2930 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2931 unsigned long pos, void *buf, unsigned long bytes)
2933 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2934 struct snd_soc_rtdcom_list *rtdcom;
2935 struct snd_soc_component *component;
2937 for_each_rtdcom(rtd, rtdcom) {
2938 component = rtdcom->component;
2940 if (!component->driver->ops ||
2941 !component->driver->ops->copy_kernel)
2944 /* FIXME. it returns 1st copy now */
2945 return component->driver->ops->copy_kernel(substream, channel,
2952 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2953 unsigned long pos, unsigned long bytes)
2955 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2956 struct snd_soc_rtdcom_list *rtdcom;
2957 struct snd_soc_component *component;
2959 for_each_rtdcom(rtd, rtdcom) {
2960 component = rtdcom->component;
2962 if (!component->driver->ops ||
2963 !component->driver->ops->fill_silence)
2966 /* FIXME. it returns 1st silence now */
2967 return component->driver->ops->fill_silence(substream, channel,
2974 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2975 unsigned long offset)
2977 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2978 struct snd_soc_rtdcom_list *rtdcom;
2979 struct snd_soc_component *component;
2982 for_each_rtdcom(rtd, rtdcom) {
2983 component = rtdcom->component;
2985 if (!component->driver->ops ||
2986 !component->driver->ops->page)
2989 /* FIXME. it returns 1st page now */
2990 page = component->driver->ops->page(substream, offset);
2998 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2999 struct vm_area_struct *vma)
3001 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3002 struct snd_soc_rtdcom_list *rtdcom;
3003 struct snd_soc_component *component;
3005 for_each_rtdcom(rtd, rtdcom) {
3006 component = rtdcom->component;
3008 if (!component->driver->ops ||
3009 !component->driver->ops->mmap)
3012 /* FIXME. it returns 1st mmap now */
3013 return component->driver->ops->mmap(substream, vma);
3019 /* create a new pcm */
3020 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3022 struct snd_soc_dai *codec_dai;
3023 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3024 struct snd_soc_component *component;
3025 struct snd_soc_rtdcom_list *rtdcom;
3026 struct snd_pcm *pcm;
3028 int ret = 0, playback = 0, capture = 0;
3031 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3032 playback = rtd->dai_link->dpcm_playback;
3033 capture = rtd->dai_link->dpcm_capture;
3035 for_each_rtd_codec_dai(rtd, i, codec_dai) {
3036 if (codec_dai->driver->playback.channels_min)
3038 if (codec_dai->driver->capture.channels_min)
3042 capture = capture && cpu_dai->driver->capture.channels_min;
3043 playback = playback && cpu_dai->driver->playback.channels_min;
3046 if (rtd->dai_link->playback_only) {
3051 if (rtd->dai_link->capture_only) {
3056 /* create the PCM */
3057 if (rtd->dai_link->no_pcm) {
3058 snprintf(new_name, sizeof(new_name), "(%s)",
3059 rtd->dai_link->stream_name);
3061 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3062 playback, capture, &pcm);
3064 if (rtd->dai_link->dynamic)
3065 snprintf(new_name, sizeof(new_name), "%s (*)",
3066 rtd->dai_link->stream_name);
3068 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3069 rtd->dai_link->stream_name,
3070 (rtd->num_codecs > 1) ?
3071 "multicodec" : rtd->codec_dai->name, num);
3073 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3077 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3078 rtd->dai_link->name);
3081 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3083 /* DAPM dai link stream work */
3084 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3086 pcm->nonatomic = rtd->dai_link->nonatomic;
3088 pcm->private_data = rtd;
3090 if (rtd->dai_link->no_pcm) {
3092 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3094 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3098 /* ASoC PCM operations */
3099 if (rtd->dai_link->dynamic) {
3100 rtd->ops.open = dpcm_fe_dai_open;
3101 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3102 rtd->ops.prepare = dpcm_fe_dai_prepare;
3103 rtd->ops.trigger = dpcm_fe_dai_trigger;
3104 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3105 rtd->ops.close = dpcm_fe_dai_close;
3106 rtd->ops.pointer = soc_pcm_pointer;
3107 rtd->ops.ioctl = soc_pcm_ioctl;
3109 rtd->ops.open = soc_pcm_open;
3110 rtd->ops.hw_params = soc_pcm_hw_params;
3111 rtd->ops.prepare = soc_pcm_prepare;
3112 rtd->ops.trigger = soc_pcm_trigger;
3113 rtd->ops.hw_free = soc_pcm_hw_free;
3114 rtd->ops.close = soc_pcm_close;
3115 rtd->ops.pointer = soc_pcm_pointer;
3116 rtd->ops.ioctl = soc_pcm_ioctl;
3119 for_each_rtdcom(rtd, rtdcom) {
3120 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3126 rtd->ops.ack = soc_rtdcom_ack;
3128 rtd->ops.copy_user = soc_rtdcom_copy_user;
3129 if (ops->copy_kernel)
3130 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3131 if (ops->fill_silence)
3132 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3134 rtd->ops.page = soc_rtdcom_page;
3136 rtd->ops.mmap = soc_rtdcom_mmap;
3140 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3143 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3145 for_each_rtdcom(rtd, rtdcom) {
3146 component = rtdcom->component;
3148 if (!component->driver->pcm_new)
3151 ret = component->driver->pcm_new(rtd);
3153 dev_err(component->dev,
3154 "ASoC: pcm constructor failed: %d\n",
3160 pcm->private_free = soc_pcm_private_free;
3162 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3163 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3168 /* is the current PCM operation for this FE ? */
3169 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3171 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3175 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3177 /* is the current PCM operation for this BE ? */
3178 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3179 struct snd_soc_pcm_runtime *be, int stream)
3181 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3182 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3183 be->dpcm[stream].runtime_update))
3187 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3189 /* get the substream for this BE */
3190 struct snd_pcm_substream *
3191 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3193 return be->pcm->streams[stream].substream;
3195 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3197 /* get the BE runtime state */
3198 enum snd_soc_dpcm_state
3199 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3201 return be->dpcm[stream].state;
3203 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3205 /* set the BE runtime state */
3206 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3207 int stream, enum snd_soc_dpcm_state state)
3209 be->dpcm[stream].state = state;
3211 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3214 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3215 * are not running, paused or suspended for the specified stream direction.
3217 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3218 struct snd_soc_pcm_runtime *be, int stream)
3220 struct snd_soc_dpcm *dpcm;
3223 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3228 state = dpcm->fe->dpcm[stream].state;
3229 if (state == SND_SOC_DPCM_STATE_START ||
3230 state == SND_SOC_DPCM_STATE_PAUSED ||
3231 state == SND_SOC_DPCM_STATE_SUSPEND)
3235 /* it's safe to free/stop this BE DAI */
3238 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3241 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3242 * running, paused or suspended for the specified stream direction.
3244 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3245 struct snd_soc_pcm_runtime *be, int stream)
3247 struct snd_soc_dpcm *dpcm;
3250 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3255 state = dpcm->fe->dpcm[stream].state;
3256 if (state == SND_SOC_DPCM_STATE_START ||
3257 state == SND_SOC_DPCM_STATE_PAUSED ||
3258 state == SND_SOC_DPCM_STATE_SUSPEND ||
3259 state == SND_SOC_DPCM_STATE_PREPARE)
3263 /* it's safe to change hw_params */
3266 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3268 #ifdef CONFIG_DEBUG_FS
3269 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3272 case SND_SOC_DPCM_STATE_NEW:
3274 case SND_SOC_DPCM_STATE_OPEN:
3276 case SND_SOC_DPCM_STATE_HW_PARAMS:
3278 case SND_SOC_DPCM_STATE_PREPARE:
3280 case SND_SOC_DPCM_STATE_START:
3282 case SND_SOC_DPCM_STATE_STOP:
3284 case SND_SOC_DPCM_STATE_SUSPEND:
3286 case SND_SOC_DPCM_STATE_PAUSED:
3288 case SND_SOC_DPCM_STATE_HW_FREE:
3290 case SND_SOC_DPCM_STATE_CLOSE:
3297 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3298 int stream, char *buf, size_t size)
3300 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3301 struct snd_soc_dpcm *dpcm;
3305 offset += snprintf(buf + offset, size - offset,
3306 "[%s - %s]\n", fe->dai_link->name,
3307 stream ? "Capture" : "Playback");
3309 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3310 dpcm_state_string(fe->dpcm[stream].state));
3312 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3313 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3314 offset += snprintf(buf + offset, size - offset,
3316 "Format = %s, Channels = %d, Rate = %d\n",
3317 snd_pcm_format_name(params_format(params)),
3318 params_channels(params),
3319 params_rate(params));
3322 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3324 if (list_empty(&fe->dpcm[stream].be_clients)) {
3325 offset += snprintf(buf + offset, size - offset,
3326 " No active DSP links\n");
3330 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3331 struct snd_soc_pcm_runtime *be = dpcm->be;
3332 params = &dpcm->hw_params;
3334 offset += snprintf(buf + offset, size - offset,
3335 "- %s\n", be->dai_link->name);
3337 offset += snprintf(buf + offset, size - offset,
3339 dpcm_state_string(be->dpcm[stream].state));
3341 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3342 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3343 offset += snprintf(buf + offset, size - offset,
3344 " Hardware Params: "
3345 "Format = %s, Channels = %d, Rate = %d\n",
3346 snd_pcm_format_name(params_format(params)),
3347 params_channels(params),
3348 params_rate(params));
3355 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3356 size_t count, loff_t *ppos)
3358 struct snd_soc_pcm_runtime *fe = file->private_data;
3359 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3362 buf = kmalloc(out_count, GFP_KERNEL);
3366 if (fe->cpu_dai->driver->playback.channels_min)
3367 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3368 buf + offset, out_count - offset);
3370 if (fe->cpu_dai->driver->capture.channels_min)
3371 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3372 buf + offset, out_count - offset);
3374 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3380 static const struct file_operations dpcm_state_fops = {
3381 .open = simple_open,
3382 .read = dpcm_state_read_file,
3383 .llseek = default_llseek,
3386 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3391 if (!rtd->card->debugfs_card_root)
3394 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3395 rtd->card->debugfs_card_root);
3396 if (!rtd->debugfs_dpcm_root) {
3398 "ASoC: Failed to create dpcm debugfs directory %s\n",
3399 rtd->dai_link->name);
3403 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3404 rtd, &dpcm_state_fops);