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/module.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
21 #include <linux/export.h>
22 #include <linux/debugfs.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dpcm.h>
28 #include <sound/initval.h>
30 #define DPCM_MAX_BE_USERS 8
33 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
35 * Returns true if the DAI supports the indicated stream type.
37 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
39 struct snd_soc_pcm_stream *codec_stream;
41 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
42 codec_stream = &dai->driver->playback;
44 codec_stream = &dai->driver->capture;
46 /* If the codec specifies any channels at all, it supports the stream */
47 return codec_stream->channels_min;
51 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
52 * @rtd: ASoC PCM runtime that is activated
53 * @stream: Direction of the PCM stream
55 * Increments the active count for all the DAIs and components attached to a PCM
56 * runtime. Should typically be called when a stream is opened.
58 * Must be called with the rtd->pcm_mutex being held
60 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
62 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
63 struct snd_soc_dai *codec_dai;
66 lockdep_assert_held(&rtd->pcm_mutex);
68 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
69 cpu_dai->playback_active++;
70 for_each_rtd_codec_dai(rtd, i, codec_dai)
71 codec_dai->playback_active++;
73 cpu_dai->capture_active++;
74 for_each_rtd_codec_dai(rtd, i, codec_dai)
75 codec_dai->capture_active++;
79 cpu_dai->component->active++;
80 for_each_rtd_codec_dai(rtd, i, codec_dai) {
82 codec_dai->component->active++;
87 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
88 * @rtd: ASoC PCM runtime that is deactivated
89 * @stream: Direction of the PCM stream
91 * Decrements the active count for all the DAIs and components attached to a PCM
92 * runtime. Should typically be called when a stream is closed.
94 * Must be called with the rtd->pcm_mutex being held
96 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
98 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99 struct snd_soc_dai *codec_dai;
102 lockdep_assert_held(&rtd->pcm_mutex);
104 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
105 cpu_dai->playback_active--;
106 for_each_rtd_codec_dai(rtd, i, codec_dai)
107 codec_dai->playback_active--;
109 cpu_dai->capture_active--;
110 for_each_rtd_codec_dai(rtd, i, codec_dai)
111 codec_dai->capture_active--;
115 cpu_dai->component->active--;
116 for_each_rtd_codec_dai(rtd, i, codec_dai) {
117 codec_dai->component->active--;
123 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
124 * @rtd: The ASoC PCM runtime that should be checked.
126 * This function checks whether the power down delay should be ignored for a
127 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
128 * been configured to ignore the delay, or if none of the components benefits
129 * from having the delay.
131 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
133 struct snd_soc_rtdcom_list *rtdcom;
134 struct snd_soc_component *component;
137 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140 for_each_rtdcom(rtd, rtdcom) {
141 component = rtdcom->component;
143 ignore &= !component->driver->use_pmdown_time;
150 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
151 * @substream: the pcm substream
152 * @hw: the hardware parameters
154 * Sets the substream runtime hardware parameters.
156 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
157 const struct snd_pcm_hardware *hw)
159 struct snd_pcm_runtime *runtime = substream->runtime;
160 runtime->hw.info = hw->info;
161 runtime->hw.formats = hw->formats;
162 runtime->hw.period_bytes_min = hw->period_bytes_min;
163 runtime->hw.period_bytes_max = hw->period_bytes_max;
164 runtime->hw.periods_min = hw->periods_min;
165 runtime->hw.periods_max = hw->periods_max;
166 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
167 runtime->hw.fifo_size = hw->fifo_size;
170 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
172 /* DPCM stream event, send event to FE and all active BEs. */
173 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
176 struct snd_soc_dpcm *dpcm;
178 for_each_dpcm_be(fe, dir, dpcm) {
180 struct snd_soc_pcm_runtime *be = dpcm->be;
182 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
183 be->dai_link->name, event, dir);
185 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
186 (be->dpcm[dir].users >= 1))
189 snd_soc_dapm_stream_event(be, dir, event);
192 snd_soc_dapm_stream_event(fe, dir, event);
197 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
198 struct snd_soc_dai *soc_dai)
200 struct snd_soc_pcm_runtime *rtd = substream->private_data;
203 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
204 rtd->dai_link->symmetric_rates)) {
205 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
208 ret = snd_pcm_hw_constraint_single(substream->runtime,
209 SNDRV_PCM_HW_PARAM_RATE,
212 dev_err(soc_dai->dev,
213 "ASoC: Unable to apply rate constraint: %d\n",
219 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
220 rtd->dai_link->symmetric_channels)) {
221 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
224 ret = snd_pcm_hw_constraint_single(substream->runtime,
225 SNDRV_PCM_HW_PARAM_CHANNELS,
228 dev_err(soc_dai->dev,
229 "ASoC: Unable to apply channel symmetry constraint: %d\n",
235 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
236 rtd->dai_link->symmetric_samplebits)) {
237 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
238 soc_dai->sample_bits);
240 ret = snd_pcm_hw_constraint_single(substream->runtime,
241 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
242 soc_dai->sample_bits);
244 dev_err(soc_dai->dev,
245 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
254 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
255 struct snd_pcm_hw_params *params)
257 struct snd_soc_pcm_runtime *rtd = substream->private_data;
258 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
259 struct snd_soc_dai *codec_dai;
260 unsigned int rate, channels, sample_bits, symmetry, i;
262 rate = params_rate(params);
263 channels = params_channels(params);
264 sample_bits = snd_pcm_format_physical_width(params_format(params));
266 /* reject unmatched parameters when applying symmetry */
267 symmetry = cpu_dai->driver->symmetric_rates ||
268 rtd->dai_link->symmetric_rates;
270 for_each_rtd_codec_dai(rtd, i, codec_dai)
271 symmetry |= codec_dai->driver->symmetric_rates;
273 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
274 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
275 cpu_dai->rate, rate);
279 symmetry = cpu_dai->driver->symmetric_channels ||
280 rtd->dai_link->symmetric_channels;
282 for_each_rtd_codec_dai(rtd, i, codec_dai)
283 symmetry |= codec_dai->driver->symmetric_channels;
285 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
286 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
287 cpu_dai->channels, channels);
291 symmetry = cpu_dai->driver->symmetric_samplebits ||
292 rtd->dai_link->symmetric_samplebits;
294 for_each_rtd_codec_dai(rtd, i, codec_dai)
295 symmetry |= codec_dai->driver->symmetric_samplebits;
297 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
298 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
299 cpu_dai->sample_bits, sample_bits);
306 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
310 struct snd_soc_dai_link *link = rtd->dai_link;
311 struct snd_soc_dai *codec_dai;
312 unsigned int symmetry, i;
314 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
315 cpu_driver->symmetric_channels || link->symmetric_channels ||
316 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
318 for_each_rtd_codec_dai(rtd, i, codec_dai)
319 symmetry = symmetry ||
320 codec_dai->driver->symmetric_rates ||
321 codec_dai->driver->symmetric_channels ||
322 codec_dai->driver->symmetric_samplebits;
327 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
329 struct snd_soc_pcm_runtime *rtd = substream->private_data;
335 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
337 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
341 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
343 struct snd_soc_pcm_runtime *rtd = substream->private_data;
344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
345 struct snd_soc_dai *codec_dai;
347 unsigned int bits = 0, cpu_bits;
349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
350 for_each_rtd_codec_dai(rtd, i, codec_dai) {
351 if (codec_dai->driver->playback.sig_bits == 0) {
355 bits = max(codec_dai->driver->playback.sig_bits, bits);
357 cpu_bits = cpu_dai->driver->playback.sig_bits;
359 for_each_rtd_codec_dai(rtd, i, codec_dai) {
360 if (codec_dai->driver->capture.sig_bits == 0) {
364 bits = max(codec_dai->driver->capture.sig_bits, bits);
366 cpu_bits = cpu_dai->driver->capture.sig_bits;
369 soc_pcm_set_msb(substream, bits);
370 soc_pcm_set_msb(substream, cpu_bits);
373 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
375 struct snd_pcm_runtime *runtime = substream->runtime;
376 struct snd_pcm_hardware *hw = &runtime->hw;
377 struct snd_soc_pcm_runtime *rtd = substream->private_data;
378 struct snd_soc_dai *codec_dai;
379 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
380 struct snd_soc_dai_driver *codec_dai_drv;
381 struct snd_soc_pcm_stream *codec_stream;
382 struct snd_soc_pcm_stream *cpu_stream;
383 unsigned int chan_min = 0, chan_max = UINT_MAX;
384 unsigned int rate_min = 0, rate_max = UINT_MAX;
385 unsigned int rates = UINT_MAX;
386 u64 formats = ULLONG_MAX;
389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
390 cpu_stream = &cpu_dai_drv->playback;
392 cpu_stream = &cpu_dai_drv->capture;
394 /* first calculate min/max only for CODECs in the DAI link */
395 for_each_rtd_codec_dai(rtd, i, codec_dai) {
398 * Skip CODECs which don't support the current stream type.
399 * Otherwise, since the rate, channel, and format values will
400 * zero in that case, we would have no usable settings left,
401 * causing the resulting setup to fail.
402 * At least one CODEC should match, otherwise we should have
403 * bailed out on a higher level, since there would be no
404 * CODEC to support the transfer direction in that case.
406 if (!snd_soc_dai_stream_valid(codec_dai,
410 codec_dai_drv = codec_dai->driver;
411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
412 codec_stream = &codec_dai_drv->playback;
414 codec_stream = &codec_dai_drv->capture;
415 chan_min = max(chan_min, codec_stream->channels_min);
416 chan_max = min(chan_max, codec_stream->channels_max);
417 rate_min = max(rate_min, codec_stream->rate_min);
418 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
419 formats &= codec_stream->formats;
420 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
424 * chan min/max cannot be enforced if there are multiple CODEC DAIs
425 * connected to a single CPU DAI, use CPU DAI's directly and let
426 * channel allocation be fixed up later
428 if (rtd->num_codecs > 1) {
429 chan_min = cpu_stream->channels_min;
430 chan_max = cpu_stream->channels_max;
433 hw->channels_min = max(chan_min, cpu_stream->channels_min);
434 hw->channels_max = min(chan_max, cpu_stream->channels_max);
436 hw->formats &= formats & cpu_stream->formats;
438 hw->formats = formats & cpu_stream->formats;
439 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
441 snd_pcm_limit_hw_rates(runtime);
443 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
444 hw->rate_min = max(hw->rate_min, rate_min);
445 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
446 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
449 static int soc_pcm_components_open(struct snd_pcm_substream *substream,
450 struct snd_soc_component **last)
452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
453 struct snd_soc_rtdcom_list *rtdcom;
454 struct snd_soc_component *component;
457 for_each_rtdcom(rtd, rtdcom) {
458 component = rtdcom->component;
461 if (component->driver->module_get_upon_open &&
462 !try_module_get(component->dev->driver->owner)) {
463 dev_err(component->dev,
464 "ASoC: can't get module %s\n",
469 if (!component->driver->ops ||
470 !component->driver->ops->open)
473 ret = component->driver->ops->open(substream);
475 dev_err(component->dev,
476 "ASoC: can't open component %s: %d\n",
477 component->name, ret);
485 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
486 struct snd_soc_component *last)
488 struct snd_soc_pcm_runtime *rtd = substream->private_data;
489 struct snd_soc_rtdcom_list *rtdcom;
490 struct snd_soc_component *component;
492 for_each_rtdcom(rtd, rtdcom) {
493 component = rtdcom->component;
495 if (component == last)
498 if (component->driver->ops &&
499 component->driver->ops->close)
500 component->driver->ops->close(substream);
502 if (component->driver->module_get_upon_open)
503 module_put(component->dev->driver->owner);
510 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
511 * then initialized and any private data can be allocated. This also calls
512 * startup for the cpu DAI, component, machine and codec DAI.
514 static int soc_pcm_open(struct snd_pcm_substream *substream)
516 struct snd_soc_pcm_runtime *rtd = substream->private_data;
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 struct snd_soc_component *component;
519 struct snd_soc_rtdcom_list *rtdcom;
520 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
521 struct snd_soc_dai *codec_dai;
522 const char *codec_dai_name = "multicodec";
525 pinctrl_pm_select_default_state(cpu_dai->dev);
526 for_each_rtd_codec_dai(rtd, i, codec_dai)
527 pinctrl_pm_select_default_state(codec_dai->dev);
529 for_each_rtdcom(rtd, rtdcom) {
530 component = rtdcom->component;
532 pm_runtime_get_sync(component->dev);
535 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
537 /* startup the audio subsystem */
538 ret = snd_soc_dai_startup(cpu_dai, substream);
540 dev_err(cpu_dai->dev, "ASoC: can't open interface %s: %d\n",
545 ret = soc_pcm_components_open(substream, &component);
549 for_each_rtd_codec_dai(rtd, i, codec_dai) {
550 ret = snd_soc_dai_startup(codec_dai, substream);
552 dev_err(codec_dai->dev,
553 "ASoC: can't open codec %s: %d\n",
554 codec_dai->name, ret);
558 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
559 codec_dai->tx_mask = 0;
561 codec_dai->rx_mask = 0;
564 if (rtd->dai_link->ops->startup) {
565 ret = rtd->dai_link->ops->startup(substream);
567 pr_err("ASoC: %s startup failed: %d\n",
568 rtd->dai_link->name, ret);
573 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
574 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
577 /* Check that the codec and cpu DAIs are compatible */
578 soc_pcm_init_runtime_hw(substream);
580 if (rtd->num_codecs == 1)
581 codec_dai_name = rtd->codec_dai->name;
583 if (soc_pcm_has_symmetry(substream))
584 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
587 if (!runtime->hw.rates) {
588 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
589 codec_dai_name, cpu_dai->name);
592 if (!runtime->hw.formats) {
593 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
594 codec_dai_name, cpu_dai->name);
597 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
598 runtime->hw.channels_min > runtime->hw.channels_max) {
599 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
600 codec_dai_name, cpu_dai->name);
604 soc_pcm_apply_msb(substream);
606 /* Symmetry only applies if we've already got an active stream. */
607 if (cpu_dai->active) {
608 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
613 for_each_rtd_codec_dai(rtd, i, codec_dai) {
614 if (codec_dai->active) {
615 ret = soc_pcm_apply_symmetry(substream, codec_dai);
621 pr_debug("ASoC: %s <-> %s info:\n",
622 codec_dai_name, cpu_dai->name);
623 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
624 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
625 runtime->hw.channels_max);
626 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
627 runtime->hw.rate_max);
631 snd_soc_runtime_activate(rtd, substream->stream);
633 mutex_unlock(&rtd->pcm_mutex);
637 if (rtd->dai_link->ops->shutdown)
638 rtd->dai_link->ops->shutdown(substream);
644 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai)
645 snd_soc_dai_shutdown(codec_dai, substream);
648 soc_pcm_components_close(substream, component);
650 snd_soc_dai_shutdown(cpu_dai, substream);
652 mutex_unlock(&rtd->pcm_mutex);
654 for_each_rtdcom(rtd, rtdcom) {
655 component = rtdcom->component;
657 pm_runtime_mark_last_busy(component->dev);
658 pm_runtime_put_autosuspend(component->dev);
661 for_each_rtd_codec_dai(rtd, i, codec_dai) {
662 if (!codec_dai->active)
663 pinctrl_pm_select_sleep_state(codec_dai->dev);
665 if (!cpu_dai->active)
666 pinctrl_pm_select_sleep_state(cpu_dai->dev);
672 * Power down the audio subsystem pmdown_time msecs after close is called.
673 * This is to ensure there are no pops or clicks in between any music tracks
674 * due to DAPM power cycling.
676 static void close_delayed_work(struct work_struct *work)
678 struct snd_soc_pcm_runtime *rtd =
679 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
680 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
682 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
684 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
685 codec_dai->driver->playback.stream_name,
686 codec_dai->playback_active ? "active" : "inactive",
687 rtd->pop_wait ? "yes" : "no");
689 /* are we waiting on this codec DAI stream */
690 if (rtd->pop_wait == 1) {
692 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
693 SND_SOC_DAPM_STREAM_STOP);
696 mutex_unlock(&rtd->pcm_mutex);
700 * Called by ALSA when a PCM substream is closed. Private data can be
701 * freed here. The cpu DAI, codec DAI, machine and components are also
704 static int soc_pcm_close(struct snd_pcm_substream *substream)
706 struct snd_soc_pcm_runtime *rtd = substream->private_data;
707 struct snd_soc_component *component;
708 struct snd_soc_rtdcom_list *rtdcom;
709 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
710 struct snd_soc_dai *codec_dai;
713 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
715 snd_soc_runtime_deactivate(rtd, substream->stream);
717 /* clear the corresponding DAIs rate when inactive */
718 if (!cpu_dai->active)
721 for_each_rtd_codec_dai(rtd, i, codec_dai) {
722 if (!codec_dai->active)
726 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
728 snd_soc_dai_shutdown(cpu_dai, substream);
730 for_each_rtd_codec_dai(rtd, i, codec_dai)
731 snd_soc_dai_shutdown(codec_dai, substream);
733 if (rtd->dai_link->ops->shutdown)
734 rtd->dai_link->ops->shutdown(substream);
736 soc_pcm_components_close(substream, NULL);
738 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
739 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
740 /* powered down playback stream now */
741 snd_soc_dapm_stream_event(rtd,
742 SNDRV_PCM_STREAM_PLAYBACK,
743 SND_SOC_DAPM_STREAM_STOP);
745 /* start delayed pop wq here for playback streams */
747 queue_delayed_work(system_power_efficient_wq,
749 msecs_to_jiffies(rtd->pmdown_time));
752 /* capture streams can be powered down now */
753 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
754 SND_SOC_DAPM_STREAM_STOP);
757 mutex_unlock(&rtd->pcm_mutex);
759 for_each_rtdcom(rtd, rtdcom) {
760 component = rtdcom->component;
762 pm_runtime_mark_last_busy(component->dev);
763 pm_runtime_put_autosuspend(component->dev);
766 for_each_rtd_codec_dai(rtd, i, codec_dai) {
767 if (!codec_dai->active)
768 pinctrl_pm_select_sleep_state(codec_dai->dev);
770 if (!cpu_dai->active)
771 pinctrl_pm_select_sleep_state(cpu_dai->dev);
777 * Called by ALSA when the PCM substream is prepared, can set format, sample
778 * rate, etc. This function is non atomic and can be called multiple times,
779 * it can refer to the runtime info.
781 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
783 struct snd_soc_pcm_runtime *rtd = substream->private_data;
784 struct snd_soc_component *component;
785 struct snd_soc_rtdcom_list *rtdcom;
786 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
787 struct snd_soc_dai *codec_dai;
790 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
792 if (rtd->dai_link->ops->prepare) {
793 ret = rtd->dai_link->ops->prepare(substream);
795 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
801 for_each_rtdcom(rtd, rtdcom) {
802 component = rtdcom->component;
804 if (!component->driver->ops ||
805 !component->driver->ops->prepare)
808 ret = component->driver->ops->prepare(substream);
810 dev_err(component->dev,
811 "ASoC: platform prepare error: %d\n", ret);
816 for_each_rtd_codec_dai(rtd, i, codec_dai) {
817 ret = snd_soc_dai_prepare(codec_dai, substream);
819 dev_err(codec_dai->dev,
820 "ASoC: codec DAI prepare error: %d\n",
826 ret = snd_soc_dai_prepare(cpu_dai, substream);
828 dev_err(cpu_dai->dev,
829 "ASoC: cpu DAI prepare error: %d\n", ret);
833 /* cancel any delayed stream shutdown that is pending */
834 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
837 cancel_delayed_work(&rtd->delayed_work);
840 snd_soc_dapm_stream_event(rtd, substream->stream,
841 SND_SOC_DAPM_STREAM_START);
843 for_each_rtd_codec_dai(rtd, i, codec_dai)
844 snd_soc_dai_digital_mute(codec_dai, 0,
846 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
849 mutex_unlock(&rtd->pcm_mutex);
853 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
856 struct snd_interval *interval;
857 int channels = hweight_long(mask);
859 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
860 interval->min = channels;
861 interval->max = channels;
864 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
865 struct snd_soc_component *last)
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
868 struct snd_soc_rtdcom_list *rtdcom;
869 struct snd_soc_component *component;
871 for_each_rtdcom(rtd, rtdcom) {
872 component = rtdcom->component;
874 if (component == last)
877 if (!component->driver->ops ||
878 !component->driver->ops->hw_free)
881 component->driver->ops->hw_free(substream);
888 * Called by ALSA when the hardware params are set by application. This
889 * function can also be called multiple times and can allocate buffers
890 * (using snd_pcm_lib_* ). It's non-atomic.
892 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
893 struct snd_pcm_hw_params *params)
895 struct snd_soc_pcm_runtime *rtd = substream->private_data;
896 struct snd_soc_component *component;
897 struct snd_soc_rtdcom_list *rtdcom;
898 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
899 struct snd_soc_dai *codec_dai;
902 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
903 if (rtd->dai_link->ops->hw_params) {
904 ret = rtd->dai_link->ops->hw_params(substream, params);
906 dev_err(rtd->card->dev, "ASoC: machine hw_params"
907 " failed: %d\n", ret);
912 for_each_rtd_codec_dai(rtd, i, codec_dai) {
913 struct snd_pcm_hw_params codec_params;
916 * Skip CODECs which don't support the current stream type,
917 * the idea being that if a CODEC is not used for the currently
918 * set up transfer direction, it should not need to be
919 * configured, especially since the configuration used might
920 * not even be supported by that CODEC. There may be cases
921 * however where a CODEC needs to be set up although it is
922 * actually not being used for the transfer, e.g. if a
923 * capture-only CODEC is acting as an LRCLK and/or BCLK master
924 * for the DAI link including a playback-only CODEC.
925 * If this becomes necessary, we will have to augment the
926 * machine driver setup with information on how to act, so
927 * we can do the right thing here.
929 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
932 /* copy params for each codec */
933 codec_params = *params;
935 /* fixup params based on TDM slot masks */
936 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
938 soc_pcm_codec_params_fixup(&codec_params,
941 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
943 soc_pcm_codec_params_fixup(&codec_params,
946 ret = snd_soc_dai_hw_params(codec_dai, substream,
951 codec_dai->rate = params_rate(&codec_params);
952 codec_dai->channels = params_channels(&codec_params);
953 codec_dai->sample_bits = snd_pcm_format_physical_width(
954 params_format(&codec_params));
956 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
959 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
963 /* store the parameters for each DAIs */
964 cpu_dai->rate = params_rate(params);
965 cpu_dai->channels = params_channels(params);
966 cpu_dai->sample_bits =
967 snd_pcm_format_physical_width(params_format(params));
969 snd_soc_dapm_update_dai(substream, params, cpu_dai);
971 for_each_rtdcom(rtd, rtdcom) {
972 component = rtdcom->component;
974 if (!component->driver->ops ||
975 !component->driver->ops->hw_params)
978 ret = component->driver->ops->hw_params(substream, params);
980 dev_err(component->dev,
981 "ASoC: %s hw params failed: %d\n",
982 component->name, ret);
988 ret = soc_pcm_params_symmetry(substream, params);
992 mutex_unlock(&rtd->pcm_mutex);
996 soc_pcm_components_hw_free(substream, component);
998 snd_soc_dai_hw_free(cpu_dai, substream);
1002 i = rtd->num_codecs;
1005 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1006 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1009 snd_soc_dai_hw_free(codec_dai, substream);
1010 codec_dai->rate = 0;
1013 if (rtd->dai_link->ops->hw_free)
1014 rtd->dai_link->ops->hw_free(substream);
1016 mutex_unlock(&rtd->pcm_mutex);
1021 * Frees resources allocated by hw_params, can be called multiple times
1023 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1025 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1026 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1027 struct snd_soc_dai *codec_dai;
1028 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1031 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1033 /* clear the corresponding DAIs parameters when going to be inactive */
1034 if (cpu_dai->active == 1) {
1036 cpu_dai->channels = 0;
1037 cpu_dai->sample_bits = 0;
1040 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1041 if (codec_dai->active == 1) {
1042 codec_dai->rate = 0;
1043 codec_dai->channels = 0;
1044 codec_dai->sample_bits = 0;
1048 /* apply codec digital mute */
1049 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1050 if ((playback && codec_dai->playback_active == 1) ||
1051 (!playback && codec_dai->capture_active == 1))
1052 snd_soc_dai_digital_mute(codec_dai, 1,
1056 /* free any machine hw params */
1057 if (rtd->dai_link->ops->hw_free)
1058 rtd->dai_link->ops->hw_free(substream);
1060 /* free any component resources */
1061 soc_pcm_components_hw_free(substream, NULL);
1063 /* now free hw params for the DAIs */
1064 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1065 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1068 snd_soc_dai_hw_free(codec_dai, substream);
1071 snd_soc_dai_hw_free(cpu_dai, substream);
1073 mutex_unlock(&rtd->pcm_mutex);
1077 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1079 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1080 struct snd_soc_component *component;
1081 struct snd_soc_rtdcom_list *rtdcom;
1082 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1083 struct snd_soc_dai *codec_dai;
1086 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1087 if (codec_dai->driver->ops->trigger) {
1088 ret = codec_dai->driver->ops->trigger(substream,
1095 for_each_rtdcom(rtd, rtdcom) {
1096 component = rtdcom->component;
1098 if (!component->driver->ops ||
1099 !component->driver->ops->trigger)
1102 ret = component->driver->ops->trigger(substream, cmd);
1107 if (cpu_dai->driver->ops->trigger) {
1108 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1113 if (rtd->dai_link->ops->trigger) {
1114 ret = rtd->dai_link->ops->trigger(substream, cmd);
1122 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1125 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1126 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1127 struct snd_soc_dai *codec_dai;
1130 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1131 if (codec_dai->driver->ops->bespoke_trigger) {
1132 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1139 if (cpu_dai->driver->ops->bespoke_trigger) {
1140 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1147 * soc level wrapper for pointer callback
1148 * If cpu_dai, codec_dai, component driver has the delay callback, then
1149 * the runtime->delay will be updated accordingly.
1151 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1153 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1154 struct snd_soc_component *component;
1155 struct snd_soc_rtdcom_list *rtdcom;
1156 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1157 struct snd_soc_dai *codec_dai;
1158 struct snd_pcm_runtime *runtime = substream->runtime;
1159 snd_pcm_uframes_t offset = 0;
1160 snd_pcm_sframes_t delay = 0;
1161 snd_pcm_sframes_t codec_delay = 0;
1164 /* clearing the previous total delay */
1167 for_each_rtdcom(rtd, rtdcom) {
1168 component = rtdcom->component;
1170 if (!component->driver->ops ||
1171 !component->driver->ops->pointer)
1174 /* FIXME: use 1st pointer */
1175 offset = component->driver->ops->pointer(substream);
1178 /* base delay if assigned in pointer callback */
1179 delay = runtime->delay;
1181 if (cpu_dai->driver->ops->delay)
1182 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1184 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1185 if (codec_dai->driver->ops->delay)
1186 codec_delay = max(codec_delay,
1187 codec_dai->driver->ops->delay(substream,
1190 delay += codec_delay;
1192 runtime->delay = delay;
1197 /* connect a FE and BE */
1198 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1199 struct snd_soc_pcm_runtime *be, int stream)
1201 struct snd_soc_dpcm *dpcm;
1202 unsigned long flags;
1204 /* only add new dpcms */
1205 for_each_dpcm_be(fe, stream, dpcm) {
1206 if (dpcm->be == be && dpcm->fe == fe)
1210 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1216 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1217 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1218 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1219 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1220 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1221 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1223 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1224 stream ? "capture" : "playback", fe->dai_link->name,
1225 stream ? "<-" : "->", be->dai_link->name);
1227 #ifdef CONFIG_DEBUG_FS
1228 if (fe->debugfs_dpcm_root)
1229 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1230 fe->debugfs_dpcm_root, &dpcm->state);
1235 /* reparent a BE onto another FE */
1236 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1237 struct snd_soc_pcm_runtime *be, int stream)
1239 struct snd_soc_dpcm *dpcm;
1240 struct snd_pcm_substream *fe_substream, *be_substream;
1242 /* reparent if BE is connected to other FEs */
1243 if (!be->dpcm[stream].users)
1246 be_substream = snd_soc_dpcm_get_substream(be, stream);
1248 for_each_dpcm_fe(be, stream, dpcm) {
1252 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1253 stream ? "capture" : "playback",
1254 dpcm->fe->dai_link->name,
1255 stream ? "<-" : "->", dpcm->be->dai_link->name);
1257 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1258 be_substream->runtime = fe_substream->runtime;
1263 /* disconnect a BE and FE */
1264 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1266 struct snd_soc_dpcm *dpcm, *d;
1267 unsigned long flags;
1269 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1270 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1271 stream ? "capture" : "playback",
1272 dpcm->be->dai_link->name);
1274 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1277 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1278 stream ? "capture" : "playback", fe->dai_link->name,
1279 stream ? "<-" : "->", dpcm->be->dai_link->name);
1281 /* BEs still alive need new FE */
1282 dpcm_be_reparent(fe, dpcm->be, stream);
1284 #ifdef CONFIG_DEBUG_FS
1285 debugfs_remove(dpcm->debugfs_state);
1287 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1288 list_del(&dpcm->list_be);
1289 list_del(&dpcm->list_fe);
1290 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1295 /* get BE for DAI widget and stream */
1296 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1297 struct snd_soc_dapm_widget *widget, int stream)
1299 struct snd_soc_pcm_runtime *be;
1300 struct snd_soc_dai *dai;
1303 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1305 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1306 for_each_card_rtds(card, be) {
1308 if (!be->dai_link->no_pcm)
1311 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1312 be->cpu_dai->playback_widget ?
1313 be->cpu_dai->playback_widget->name : "(not set)");
1315 if (be->cpu_dai->playback_widget == widget)
1318 for_each_rtd_codec_dai(be, i, dai) {
1319 if (dai->playback_widget == widget)
1325 for_each_card_rtds(card, be) {
1327 if (!be->dai_link->no_pcm)
1330 dev_dbg(card->dev, "ASoC: try BE %s\n",
1331 be->cpu_dai->capture_widget ?
1332 be->cpu_dai->capture_widget->name : "(not set)");
1334 if (be->cpu_dai->capture_widget == widget)
1337 for_each_rtd_codec_dai(be, i, dai) {
1338 if (dai->capture_widget == widget)
1344 /* dai link name and stream name set correctly ? */
1345 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1346 stream ? "capture" : "playback", widget->name);
1350 static inline struct snd_soc_dapm_widget *
1351 dai_get_widget(struct snd_soc_dai *dai, int stream)
1353 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1354 return dai->playback_widget;
1356 return dai->capture_widget;
1359 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1360 struct snd_soc_dapm_widget *widget)
1364 for (i = 0; i < list->num_widgets; i++) {
1365 if (widget == list->widgets[i])
1372 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1373 enum snd_soc_dapm_direction dir)
1375 struct snd_soc_card *card = widget->dapm->card;
1376 struct snd_soc_pcm_runtime *rtd;
1377 struct snd_soc_dai *dai;
1380 if (dir == SND_SOC_DAPM_DIR_OUT) {
1381 for_each_card_rtds(card, rtd) {
1382 if (!rtd->dai_link->no_pcm)
1385 if (rtd->cpu_dai->playback_widget == widget)
1388 for_each_rtd_codec_dai(rtd, i, dai) {
1389 if (dai->playback_widget == widget)
1393 } else { /* SND_SOC_DAPM_DIR_IN */
1394 for_each_card_rtds(card, rtd) {
1395 if (!rtd->dai_link->no_pcm)
1398 if (rtd->cpu_dai->capture_widget == widget)
1401 for_each_rtd_codec_dai(rtd, i, dai) {
1402 if (dai->capture_widget == widget)
1411 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1412 int stream, struct snd_soc_dapm_widget_list **list)
1414 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1417 /* get number of valid DAI paths and their widgets */
1418 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1419 dpcm_end_walk_at_be);
1421 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1422 stream ? "capture" : "playback");
1427 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1428 struct snd_soc_dapm_widget_list **list_)
1430 struct snd_soc_dpcm *dpcm;
1431 struct snd_soc_dapm_widget_list *list = *list_;
1432 struct snd_soc_dapm_widget *widget;
1433 struct snd_soc_dai *dai;
1436 /* Destroy any old FE <--> BE connections */
1437 for_each_dpcm_be(fe, stream, dpcm) {
1440 /* is there a valid CPU DAI widget for this BE */
1441 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1443 /* prune the BE if it's no longer in our active list */
1444 if (widget && widget_in_list(list, widget))
1447 /* is there a valid CODEC DAI widget for this BE */
1448 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1449 widget = dai_get_widget(dai, stream);
1451 /* prune the BE if it's no longer in our active list */
1452 if (widget && widget_in_list(list, widget))
1456 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1457 stream ? "capture" : "playback",
1458 dpcm->be->dai_link->name, fe->dai_link->name);
1459 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1460 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1464 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1468 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1469 struct snd_soc_dapm_widget_list **list_)
1471 struct snd_soc_card *card = fe->card;
1472 struct snd_soc_dapm_widget_list *list = *list_;
1473 struct snd_soc_pcm_runtime *be;
1474 int i, new = 0, err;
1476 /* Create any new FE <--> BE connections */
1477 for (i = 0; i < list->num_widgets; i++) {
1479 switch (list->widgets[i]->id) {
1480 case snd_soc_dapm_dai_in:
1481 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1484 case snd_soc_dapm_dai_out:
1485 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1492 /* is there a valid BE rtd for this widget */
1493 be = dpcm_get_be(card, list->widgets[i], stream);
1495 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1496 list->widgets[i]->name);
1500 /* make sure BE is a real BE */
1501 if (!be->dai_link->no_pcm)
1504 /* don't connect if FE is not running */
1505 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1508 /* newly connected FE and BE */
1509 err = dpcm_be_connect(fe, be, stream);
1511 dev_err(fe->dev, "ASoC: can't connect %s\n",
1512 list->widgets[i]->name);
1514 } else if (err == 0) /* already connected */
1518 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1522 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1527 * Find the corresponding BE DAIs that source or sink audio to this
1530 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1531 int stream, struct snd_soc_dapm_widget_list **list, int new)
1534 return dpcm_add_paths(fe, stream, list);
1536 return dpcm_prune_paths(fe, stream, list);
1539 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1541 struct snd_soc_dpcm *dpcm;
1542 unsigned long flags;
1544 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1545 for_each_dpcm_be(fe, stream, dpcm)
1546 dpcm->be->dpcm[stream].runtime_update =
1547 SND_SOC_DPCM_UPDATE_NO;
1548 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1551 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1554 struct snd_soc_dpcm *dpcm;
1556 /* disable any enabled and non active backends */
1557 for_each_dpcm_be(fe, stream, dpcm) {
1559 struct snd_soc_pcm_runtime *be = dpcm->be;
1560 struct snd_pcm_substream *be_substream =
1561 snd_soc_dpcm_get_substream(be, stream);
1563 if (be->dpcm[stream].users == 0)
1564 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1565 stream ? "capture" : "playback",
1566 be->dpcm[stream].state);
1568 if (--be->dpcm[stream].users != 0)
1571 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1574 soc_pcm_close(be_substream);
1575 be_substream->runtime = NULL;
1576 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1580 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1582 struct snd_soc_dpcm *dpcm;
1585 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1586 for_each_dpcm_be(fe, stream, dpcm) {
1588 struct snd_soc_pcm_runtime *be = dpcm->be;
1589 struct snd_pcm_substream *be_substream =
1590 snd_soc_dpcm_get_substream(be, stream);
1592 if (!be_substream) {
1593 dev_err(be->dev, "ASoC: no backend %s stream\n",
1594 stream ? "capture" : "playback");
1598 /* is this op for this BE ? */
1599 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1602 /* first time the dpcm is open ? */
1603 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1604 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1605 stream ? "capture" : "playback",
1606 be->dpcm[stream].state);
1608 if (be->dpcm[stream].users++ != 0)
1611 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1612 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1615 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1616 stream ? "capture" : "playback", be->dai_link->name);
1618 be_substream->runtime = be->dpcm[stream].runtime;
1619 err = soc_pcm_open(be_substream);
1621 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1622 be->dpcm[stream].users--;
1623 if (be->dpcm[stream].users < 0)
1624 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1625 stream ? "capture" : "playback",
1626 be->dpcm[stream].state);
1628 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1632 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1639 /* disable any enabled and non active backends */
1640 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1641 struct snd_soc_pcm_runtime *be = dpcm->be;
1642 struct snd_pcm_substream *be_substream =
1643 snd_soc_dpcm_get_substream(be, stream);
1645 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1648 if (be->dpcm[stream].users == 0)
1649 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1650 stream ? "capture" : "playback",
1651 be->dpcm[stream].state);
1653 if (--be->dpcm[stream].users != 0)
1656 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1659 soc_pcm_close(be_substream);
1660 be_substream->runtime = NULL;
1661 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1667 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1668 struct snd_soc_pcm_stream *stream)
1670 runtime->hw.rate_min = stream->rate_min;
1671 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1672 runtime->hw.channels_min = stream->channels_min;
1673 runtime->hw.channels_max = stream->channels_max;
1674 if (runtime->hw.formats)
1675 runtime->hw.formats &= stream->formats;
1677 runtime->hw.formats = stream->formats;
1678 runtime->hw.rates = stream->rates;
1681 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1684 struct snd_soc_pcm_runtime *fe = substream->private_data;
1685 struct snd_soc_dpcm *dpcm;
1686 struct snd_soc_dai *dai;
1687 int stream = substream->stream;
1689 if (!fe->dai_link->dpcm_merged_format)
1693 * It returns merged BE codec format
1694 * if FE want to use it (= dpcm_merged_format)
1697 for_each_dpcm_be(fe, stream, dpcm) {
1698 struct snd_soc_pcm_runtime *be = dpcm->be;
1699 struct snd_soc_dai_driver *codec_dai_drv;
1700 struct snd_soc_pcm_stream *codec_stream;
1703 for_each_rtd_codec_dai(be, i, dai) {
1705 * Skip CODECs which don't support the current stream
1706 * type. See soc_pcm_init_runtime_hw() for more details
1708 if (!snd_soc_dai_stream_valid(dai, stream))
1711 codec_dai_drv = dai->driver;
1712 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1713 codec_stream = &codec_dai_drv->playback;
1715 codec_stream = &codec_dai_drv->capture;
1717 *formats &= codec_stream->formats;
1722 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1723 unsigned int *channels_min,
1724 unsigned int *channels_max)
1726 struct snd_soc_pcm_runtime *fe = substream->private_data;
1727 struct snd_soc_dpcm *dpcm;
1728 int stream = substream->stream;
1730 if (!fe->dai_link->dpcm_merged_chan)
1734 * It returns merged BE codec channel;
1735 * if FE want to use it (= dpcm_merged_chan)
1738 for_each_dpcm_be(fe, stream, dpcm) {
1739 struct snd_soc_pcm_runtime *be = dpcm->be;
1740 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1741 struct snd_soc_dai_driver *codec_dai_drv;
1742 struct snd_soc_pcm_stream *codec_stream;
1743 struct snd_soc_pcm_stream *cpu_stream;
1745 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1746 cpu_stream = &cpu_dai_drv->playback;
1748 cpu_stream = &cpu_dai_drv->capture;
1750 *channels_min = max(*channels_min, cpu_stream->channels_min);
1751 *channels_max = min(*channels_max, cpu_stream->channels_max);
1754 * chan min/max cannot be enforced if there are multiple CODEC
1755 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1757 if (be->num_codecs == 1) {
1758 codec_dai_drv = be->codec_dais[0]->driver;
1760 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1761 codec_stream = &codec_dai_drv->playback;
1763 codec_stream = &codec_dai_drv->capture;
1765 *channels_min = max(*channels_min,
1766 codec_stream->channels_min);
1767 *channels_max = min(*channels_max,
1768 codec_stream->channels_max);
1773 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1774 unsigned int *rates,
1775 unsigned int *rate_min,
1776 unsigned int *rate_max)
1778 struct snd_soc_pcm_runtime *fe = substream->private_data;
1779 struct snd_soc_dpcm *dpcm;
1780 int stream = substream->stream;
1782 if (!fe->dai_link->dpcm_merged_rate)
1786 * It returns merged BE codec channel;
1787 * if FE want to use it (= dpcm_merged_chan)
1790 for_each_dpcm_be(fe, stream, dpcm) {
1791 struct snd_soc_pcm_runtime *be = dpcm->be;
1792 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1793 struct snd_soc_dai_driver *codec_dai_drv;
1794 struct snd_soc_pcm_stream *codec_stream;
1795 struct snd_soc_pcm_stream *cpu_stream;
1796 struct snd_soc_dai *dai;
1799 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1800 cpu_stream = &cpu_dai_drv->playback;
1802 cpu_stream = &cpu_dai_drv->capture;
1804 *rate_min = max(*rate_min, cpu_stream->rate_min);
1805 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1806 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1808 for_each_rtd_codec_dai(be, i, dai) {
1810 * Skip CODECs which don't support the current stream
1811 * type. See soc_pcm_init_runtime_hw() for more details
1813 if (!snd_soc_dai_stream_valid(dai, stream))
1816 codec_dai_drv = dai->driver;
1817 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1818 codec_stream = &codec_dai_drv->playback;
1820 codec_stream = &codec_dai_drv->capture;
1822 *rate_min = max(*rate_min, codec_stream->rate_min);
1823 *rate_max = min_not_zero(*rate_max,
1824 codec_stream->rate_max);
1825 *rates = snd_pcm_rate_mask_intersect(*rates,
1826 codec_stream->rates);
1831 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1833 struct snd_pcm_runtime *runtime = substream->runtime;
1834 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1835 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1836 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1838 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1839 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1841 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1843 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1844 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1845 &runtime->hw.channels_max);
1846 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1847 &runtime->hw.rate_min, &runtime->hw.rate_max);
1850 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1852 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1853 * for avoiding the race with trigger callback.
1854 * If the state is unset and a trigger is pending while the previous operation,
1855 * process the pending trigger action here.
1857 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1858 int stream, enum snd_soc_dpcm_update state)
1860 struct snd_pcm_substream *substream =
1861 snd_soc_dpcm_get_substream(fe, stream);
1863 snd_pcm_stream_lock_irq(substream);
1864 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1865 dpcm_fe_dai_do_trigger(substream,
1866 fe->dpcm[stream].trigger_pending - 1);
1867 fe->dpcm[stream].trigger_pending = 0;
1869 fe->dpcm[stream].runtime_update = state;
1870 snd_pcm_stream_unlock_irq(substream);
1873 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1876 struct snd_soc_dpcm *dpcm;
1877 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1878 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1881 /* apply symmetry for FE */
1882 if (soc_pcm_has_symmetry(fe_substream))
1883 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1885 /* Symmetry only applies if we've got an active stream. */
1886 if (fe_cpu_dai->active) {
1887 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1892 /* apply symmetry for BE */
1893 for_each_dpcm_be(fe, stream, dpcm) {
1894 struct snd_soc_pcm_runtime *be = dpcm->be;
1895 struct snd_pcm_substream *be_substream =
1896 snd_soc_dpcm_get_substream(be, stream);
1897 struct snd_soc_pcm_runtime *rtd;
1898 struct snd_soc_dai *codec_dai;
1901 /* A backend may not have the requested substream */
1905 rtd = be_substream->private_data;
1906 if (rtd->dai_link->be_hw_params_fixup)
1909 if (soc_pcm_has_symmetry(be_substream))
1910 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1912 /* Symmetry only applies if we've got an active stream. */
1913 if (rtd->cpu_dai->active) {
1914 err = soc_pcm_apply_symmetry(fe_substream,
1920 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1921 if (codec_dai->active) {
1922 err = soc_pcm_apply_symmetry(fe_substream,
1933 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1935 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1936 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1937 int stream = fe_substream->stream, ret = 0;
1939 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1941 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1943 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1947 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1949 /* start the DAI frontend */
1950 ret = soc_pcm_open(fe_substream);
1952 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1956 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1958 dpcm_set_fe_runtime(fe_substream);
1959 snd_pcm_limit_hw_rates(runtime);
1961 ret = dpcm_apply_symmetry(fe_substream, stream);
1963 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1968 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1972 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1974 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1978 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1980 struct snd_soc_dpcm *dpcm;
1982 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1983 for_each_dpcm_be(fe, stream, dpcm) {
1985 struct snd_soc_pcm_runtime *be = dpcm->be;
1986 struct snd_pcm_substream *be_substream =
1987 snd_soc_dpcm_get_substream(be, stream);
1989 /* is this op for this BE ? */
1990 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1993 if (be->dpcm[stream].users == 0)
1994 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1995 stream ? "capture" : "playback",
1996 be->dpcm[stream].state);
1998 if (--be->dpcm[stream].users != 0)
2001 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2002 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2003 soc_pcm_hw_free(be_substream);
2004 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2007 dev_dbg(be->dev, "ASoC: close BE %s\n",
2008 be->dai_link->name);
2010 soc_pcm_close(be_substream);
2011 be_substream->runtime = NULL;
2013 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2018 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2020 struct snd_soc_pcm_runtime *fe = substream->private_data;
2021 int stream = substream->stream;
2023 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2025 /* shutdown the BEs */
2026 dpcm_be_dai_shutdown(fe, substream->stream);
2028 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2030 /* now shutdown the frontend */
2031 soc_pcm_close(substream);
2033 /* run the stream event for each BE */
2034 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2036 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2037 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2041 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2043 struct snd_soc_dpcm *dpcm;
2045 /* only hw_params backends that are either sinks or sources
2046 * to this frontend DAI */
2047 for_each_dpcm_be(fe, stream, dpcm) {
2049 struct snd_soc_pcm_runtime *be = dpcm->be;
2050 struct snd_pcm_substream *be_substream =
2051 snd_soc_dpcm_get_substream(be, stream);
2053 /* is this op for this BE ? */
2054 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2057 /* only free hw when no longer used - check all FEs */
2058 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2061 /* do not free hw if this BE is used by other FE */
2062 if (be->dpcm[stream].users > 1)
2065 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2066 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2067 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2068 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2069 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2070 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2073 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2074 be->dai_link->name);
2076 soc_pcm_hw_free(be_substream);
2078 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2084 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2086 struct snd_soc_pcm_runtime *fe = substream->private_data;
2087 int err, stream = substream->stream;
2089 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2090 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2092 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2094 /* call hw_free on the frontend */
2095 err = soc_pcm_hw_free(substream);
2097 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2098 fe->dai_link->name);
2100 /* only hw_params backends that are either sinks or sources
2101 * to this frontend DAI */
2102 err = dpcm_be_dai_hw_free(fe, stream);
2104 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2105 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2107 mutex_unlock(&fe->card->mutex);
2111 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2113 struct snd_soc_dpcm *dpcm;
2116 for_each_dpcm_be(fe, stream, dpcm) {
2118 struct snd_soc_pcm_runtime *be = dpcm->be;
2119 struct snd_pcm_substream *be_substream =
2120 snd_soc_dpcm_get_substream(be, stream);
2122 /* is this op for this BE ? */
2123 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2126 /* copy params for each dpcm */
2127 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2128 sizeof(struct snd_pcm_hw_params));
2130 /* perform any hw_params fixups */
2131 if (be->dai_link->be_hw_params_fixup) {
2132 ret = be->dai_link->be_hw_params_fixup(be,
2136 "ASoC: hw_params BE fixup failed %d\n",
2142 /* copy the fixed-up hw params for BE dai */
2143 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2144 sizeof(struct snd_pcm_hw_params));
2146 /* only allow hw_params() if no connected FEs are running */
2147 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2150 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2151 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2152 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2155 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2156 be->dai_link->name);
2158 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2160 dev_err(dpcm->be->dev,
2161 "ASoC: hw_params BE failed %d\n", ret);
2165 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2170 /* disable any enabled and non active backends */
2171 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2172 struct snd_soc_pcm_runtime *be = dpcm->be;
2173 struct snd_pcm_substream *be_substream =
2174 snd_soc_dpcm_get_substream(be, stream);
2176 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2179 /* only allow hw_free() if no connected FEs are running */
2180 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2183 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2184 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2185 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2186 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2189 soc_pcm_hw_free(be_substream);
2195 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2196 struct snd_pcm_hw_params *params)
2198 struct snd_soc_pcm_runtime *fe = substream->private_data;
2199 int ret, stream = substream->stream;
2201 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2202 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2204 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2205 sizeof(struct snd_pcm_hw_params));
2206 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2208 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2212 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2213 fe->dai_link->name, params_rate(params),
2214 params_channels(params), params_format(params));
2216 /* call hw_params on the frontend */
2217 ret = soc_pcm_hw_params(substream, params);
2219 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2220 dpcm_be_dai_hw_free(fe, stream);
2222 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2225 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2226 mutex_unlock(&fe->card->mutex);
2230 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2231 struct snd_pcm_substream *substream, int cmd)
2235 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2236 dpcm->be->dai_link->name, cmd);
2238 ret = soc_pcm_trigger(substream, cmd);
2240 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2245 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2248 struct snd_soc_dpcm *dpcm;
2251 for_each_dpcm_be(fe, stream, dpcm) {
2253 struct snd_soc_pcm_runtime *be = dpcm->be;
2254 struct snd_pcm_substream *be_substream =
2255 snd_soc_dpcm_get_substream(be, stream);
2257 /* is this op for this BE ? */
2258 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2262 case SNDRV_PCM_TRIGGER_START:
2263 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2264 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2267 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2271 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2273 case SNDRV_PCM_TRIGGER_RESUME:
2274 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2277 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2281 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2283 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2284 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2287 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2291 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2293 case SNDRV_PCM_TRIGGER_STOP:
2294 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2297 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2300 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2304 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2306 case SNDRV_PCM_TRIGGER_SUSPEND:
2307 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2310 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2313 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2317 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2319 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2320 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2323 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2326 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2330 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2337 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2339 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2341 struct snd_soc_pcm_runtime *fe = substream->private_data;
2342 int stream = substream->stream, ret;
2343 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2345 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2348 case SND_SOC_DPCM_TRIGGER_PRE:
2349 /* call trigger on the frontend before the backend. */
2351 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2352 fe->dai_link->name, cmd);
2354 ret = soc_pcm_trigger(substream, cmd);
2356 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2360 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2362 case SND_SOC_DPCM_TRIGGER_POST:
2363 /* call trigger on the frontend after the backend. */
2365 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2367 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2371 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2372 fe->dai_link->name, cmd);
2374 ret = soc_pcm_trigger(substream, cmd);
2376 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2377 /* bespoke trigger() - handles both FE and BEs */
2379 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2380 fe->dai_link->name, cmd);
2382 ret = soc_pcm_bespoke_trigger(substream, cmd);
2384 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2389 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2390 fe->dai_link->name);
2396 case SNDRV_PCM_TRIGGER_START:
2397 case SNDRV_PCM_TRIGGER_RESUME:
2398 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2399 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2401 case SNDRV_PCM_TRIGGER_STOP:
2402 case SNDRV_PCM_TRIGGER_SUSPEND:
2403 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2405 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2406 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2411 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2415 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2417 struct snd_soc_pcm_runtime *fe = substream->private_data;
2418 int stream = substream->stream;
2420 /* if FE's runtime_update is already set, we're in race;
2421 * process this trigger later at exit
2423 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2424 fe->dpcm[stream].trigger_pending = cmd + 1;
2425 return 0; /* delayed, assuming it's successful */
2428 /* we're alone, let's trigger */
2429 return dpcm_fe_dai_do_trigger(substream, cmd);
2432 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2434 struct snd_soc_dpcm *dpcm;
2437 for_each_dpcm_be(fe, stream, dpcm) {
2439 struct snd_soc_pcm_runtime *be = dpcm->be;
2440 struct snd_pcm_substream *be_substream =
2441 snd_soc_dpcm_get_substream(be, stream);
2443 /* is this op for this BE ? */
2444 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2447 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2448 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2449 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2450 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2453 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2454 be->dai_link->name);
2456 ret = soc_pcm_prepare(be_substream);
2458 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2463 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2468 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2470 struct snd_soc_pcm_runtime *fe = substream->private_data;
2471 int stream = substream->stream, ret = 0;
2473 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2475 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2477 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2479 /* there is no point preparing this FE if there are no BEs */
2480 if (list_empty(&fe->dpcm[stream].be_clients)) {
2481 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2482 fe->dai_link->name);
2487 ret = dpcm_be_dai_prepare(fe, substream->stream);
2491 /* call prepare on the frontend */
2492 ret = soc_pcm_prepare(substream);
2494 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2495 fe->dai_link->name);
2499 /* run the stream event for each BE */
2500 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2501 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2504 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2505 mutex_unlock(&fe->card->mutex);
2510 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2511 unsigned int cmd, void *arg)
2513 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2514 struct snd_soc_component *component;
2515 struct snd_soc_rtdcom_list *rtdcom;
2517 for_each_rtdcom(rtd, rtdcom) {
2518 component = rtdcom->component;
2520 if (!component->driver->ops ||
2521 !component->driver->ops->ioctl)
2524 /* FIXME: use 1st ioctl */
2525 return component->driver->ops->ioctl(substream, cmd, arg);
2528 return snd_pcm_lib_ioctl(substream, cmd, arg);
2531 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2533 struct snd_pcm_substream *substream =
2534 snd_soc_dpcm_get_substream(fe, stream);
2535 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2538 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2539 stream ? "capture" : "playback", fe->dai_link->name);
2541 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2542 /* call bespoke trigger - FE takes care of all BE triggers */
2543 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2544 fe->dai_link->name);
2546 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2548 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2550 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2551 fe->dai_link->name);
2553 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2555 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2558 err = dpcm_be_dai_hw_free(fe, stream);
2560 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2562 err = dpcm_be_dai_shutdown(fe, stream);
2564 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2566 /* run the stream event for each BE */
2567 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2572 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2574 struct snd_pcm_substream *substream =
2575 snd_soc_dpcm_get_substream(fe, stream);
2576 struct snd_soc_dpcm *dpcm;
2577 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2579 unsigned long flags;
2581 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2582 stream ? "capture" : "playback", fe->dai_link->name);
2584 /* Only start the BE if the FE is ready */
2585 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2586 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2589 /* startup must always be called for new BEs */
2590 ret = dpcm_be_dai_startup(fe, stream);
2594 /* keep going if FE state is > open */
2595 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2598 ret = dpcm_be_dai_hw_params(fe, stream);
2602 /* keep going if FE state is > hw_params */
2603 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2607 ret = dpcm_be_dai_prepare(fe, stream);
2611 /* run the stream event for each BE */
2612 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2614 /* keep going if FE state is > prepare */
2615 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2616 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2619 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2620 /* call trigger on the frontend - FE takes care of all BE triggers */
2621 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2622 fe->dai_link->name);
2624 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2626 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2630 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2631 fe->dai_link->name);
2633 ret = dpcm_be_dai_trigger(fe, stream,
2634 SNDRV_PCM_TRIGGER_START);
2636 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2644 dpcm_be_dai_hw_free(fe, stream);
2646 dpcm_be_dai_shutdown(fe, stream);
2648 /* disconnect any non started BEs */
2649 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2650 for_each_dpcm_be(fe, stream, dpcm) {
2651 struct snd_soc_pcm_runtime *be = dpcm->be;
2652 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2653 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2655 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2660 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2664 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2665 ret = dpcm_run_update_startup(fe, stream);
2667 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2668 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2673 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2677 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2678 ret = dpcm_run_update_shutdown(fe, stream);
2680 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2681 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2686 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2688 struct snd_soc_dapm_widget_list *list;
2691 if (!fe->dai_link->dynamic)
2694 /* only check active links */
2695 if (!fe->cpu_dai->active)
2698 /* DAPM sync will call this to update DSP paths */
2699 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2700 new ? "new" : "old", fe->dai_link->name);
2702 /* skip if FE doesn't have playback capability */
2703 if (!fe->cpu_dai->driver->playback.channels_min ||
2704 !fe->codec_dai->driver->playback.channels_min)
2707 /* skip if FE isn't currently playing */
2708 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2711 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2713 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2714 fe->dai_link->name, "playback");
2718 /* update any playback paths */
2719 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2722 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2724 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2726 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2727 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2730 dpcm_path_put(&list);
2733 /* skip if FE doesn't have capture capability */
2734 if (!fe->cpu_dai->driver->capture.channels_min ||
2735 !fe->codec_dai->driver->capture.channels_min)
2738 /* skip if FE isn't currently capturing */
2739 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2742 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2744 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2745 fe->dai_link->name, "capture");
2749 /* update any old capture paths */
2750 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2753 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2755 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2757 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2758 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2761 dpcm_path_put(&list);
2766 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2769 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2771 struct snd_soc_pcm_runtime *fe;
2774 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2775 /* shutdown all old paths first */
2776 for_each_card_rtds(card, fe) {
2777 ret = soc_dpcm_fe_runtime_update(fe, 0);
2782 /* bring new paths up */
2783 for_each_card_rtds(card, fe) {
2784 ret = soc_dpcm_fe_runtime_update(fe, 1);
2790 mutex_unlock(&card->mutex);
2793 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2795 struct snd_soc_dpcm *dpcm;
2796 struct snd_soc_dai *dai;
2798 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2800 struct snd_soc_pcm_runtime *be = dpcm->be;
2803 if (be->dai_link->ignore_suspend)
2806 for_each_rtd_codec_dai(be, i, dai) {
2807 struct snd_soc_dai_driver *drv = dai->driver;
2809 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2810 be->dai_link->name);
2812 if (drv->ops && drv->ops->digital_mute &&
2813 dai->playback_active)
2814 drv->ops->digital_mute(dai, mute);
2821 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2823 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2824 struct snd_soc_dpcm *dpcm;
2825 struct snd_soc_dapm_widget_list *list;
2827 int stream = fe_substream->stream;
2829 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2830 fe->dpcm[stream].runtime = fe_substream->runtime;
2832 ret = dpcm_path_get(fe, stream, &list);
2834 mutex_unlock(&fe->card->mutex);
2836 } else if (ret == 0) {
2837 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2838 fe->dai_link->name, stream ? "capture" : "playback");
2841 /* calculate valid and active FE <-> BE dpcms */
2842 dpcm_process_paths(fe, stream, &list, 1);
2844 ret = dpcm_fe_dai_startup(fe_substream);
2846 /* clean up all links */
2847 for_each_dpcm_be(fe, stream, dpcm)
2848 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2850 dpcm_be_disconnect(fe, stream);
2851 fe->dpcm[stream].runtime = NULL;
2854 dpcm_clear_pending_state(fe, stream);
2855 dpcm_path_put(&list);
2856 mutex_unlock(&fe->card->mutex);
2860 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2862 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2863 struct snd_soc_dpcm *dpcm;
2864 int stream = fe_substream->stream, ret;
2866 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2867 ret = dpcm_fe_dai_shutdown(fe_substream);
2869 /* mark FE's links ready to prune */
2870 for_each_dpcm_be(fe, stream, dpcm)
2871 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2873 dpcm_be_disconnect(fe, stream);
2875 fe->dpcm[stream].runtime = NULL;
2876 mutex_unlock(&fe->card->mutex);
2880 static void soc_pcm_private_free(struct snd_pcm *pcm)
2882 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2883 struct snd_soc_rtdcom_list *rtdcom;
2884 struct snd_soc_component *component;
2886 /* need to sync the delayed work before releasing resources */
2887 flush_delayed_work(&rtd->delayed_work);
2888 for_each_rtdcom(rtd, rtdcom) {
2889 component = rtdcom->component;
2891 if (component->driver->pcm_free)
2892 component->driver->pcm_free(pcm);
2896 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2898 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2899 struct snd_soc_rtdcom_list *rtdcom;
2900 struct snd_soc_component *component;
2902 for_each_rtdcom(rtd, rtdcom) {
2903 component = rtdcom->component;
2905 if (!component->driver->ops ||
2906 !component->driver->ops->ack)
2909 /* FIXME. it returns 1st ask now */
2910 return component->driver->ops->ack(substream);
2916 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2917 unsigned long pos, void __user *buf,
2918 unsigned long bytes)
2920 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2921 struct snd_soc_rtdcom_list *rtdcom;
2922 struct snd_soc_component *component;
2924 for_each_rtdcom(rtd, rtdcom) {
2925 component = rtdcom->component;
2927 if (!component->driver->ops ||
2928 !component->driver->ops->copy_user)
2931 /* FIXME. it returns 1st copy now */
2932 return component->driver->ops->copy_user(substream, channel,
2939 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2940 unsigned long pos, void *buf, unsigned long bytes)
2942 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2943 struct snd_soc_rtdcom_list *rtdcom;
2944 struct snd_soc_component *component;
2946 for_each_rtdcom(rtd, rtdcom) {
2947 component = rtdcom->component;
2949 if (!component->driver->ops ||
2950 !component->driver->ops->copy_kernel)
2953 /* FIXME. it returns 1st copy now */
2954 return component->driver->ops->copy_kernel(substream, channel,
2961 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2962 unsigned long pos, unsigned long bytes)
2964 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2965 struct snd_soc_rtdcom_list *rtdcom;
2966 struct snd_soc_component *component;
2968 for_each_rtdcom(rtd, rtdcom) {
2969 component = rtdcom->component;
2971 if (!component->driver->ops ||
2972 !component->driver->ops->fill_silence)
2975 /* FIXME. it returns 1st silence now */
2976 return component->driver->ops->fill_silence(substream, channel,
2983 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2984 unsigned long offset)
2986 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2987 struct snd_soc_rtdcom_list *rtdcom;
2988 struct snd_soc_component *component;
2991 for_each_rtdcom(rtd, rtdcom) {
2992 component = rtdcom->component;
2994 if (!component->driver->ops ||
2995 !component->driver->ops->page)
2998 /* FIXME. it returns 1st page now */
2999 page = component->driver->ops->page(substream, offset);
3007 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3008 struct vm_area_struct *vma)
3010 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3011 struct snd_soc_rtdcom_list *rtdcom;
3012 struct snd_soc_component *component;
3014 for_each_rtdcom(rtd, rtdcom) {
3015 component = rtdcom->component;
3017 if (!component->driver->ops ||
3018 !component->driver->ops->mmap)
3021 /* FIXME. it returns 1st mmap now */
3022 return component->driver->ops->mmap(substream, vma);
3028 /* create a new pcm */
3029 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3031 struct snd_soc_dai *codec_dai;
3032 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3033 struct snd_soc_component *component;
3034 struct snd_soc_rtdcom_list *rtdcom;
3035 struct snd_pcm *pcm;
3037 int ret = 0, playback = 0, capture = 0;
3040 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3041 playback = rtd->dai_link->dpcm_playback;
3042 capture = rtd->dai_link->dpcm_capture;
3044 for_each_rtd_codec_dai(rtd, i, codec_dai) {
3045 if (codec_dai->driver->playback.channels_min)
3047 if (codec_dai->driver->capture.channels_min)
3051 capture = capture && cpu_dai->driver->capture.channels_min;
3052 playback = playback && cpu_dai->driver->playback.channels_min;
3055 if (rtd->dai_link->playback_only) {
3060 if (rtd->dai_link->capture_only) {
3065 /* create the PCM */
3066 if (rtd->dai_link->no_pcm) {
3067 snprintf(new_name, sizeof(new_name), "(%s)",
3068 rtd->dai_link->stream_name);
3070 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3071 playback, capture, &pcm);
3073 if (rtd->dai_link->dynamic)
3074 snprintf(new_name, sizeof(new_name), "%s (*)",
3075 rtd->dai_link->stream_name);
3077 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3078 rtd->dai_link->stream_name,
3079 (rtd->num_codecs > 1) ?
3080 "multicodec" : rtd->codec_dai->name, num);
3082 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3086 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3087 rtd->dai_link->name);
3090 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3092 /* DAPM dai link stream work */
3093 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3095 pcm->nonatomic = rtd->dai_link->nonatomic;
3097 pcm->private_data = rtd;
3099 if (rtd->dai_link->no_pcm) {
3101 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3103 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3107 /* ASoC PCM operations */
3108 if (rtd->dai_link->dynamic) {
3109 rtd->ops.open = dpcm_fe_dai_open;
3110 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3111 rtd->ops.prepare = dpcm_fe_dai_prepare;
3112 rtd->ops.trigger = dpcm_fe_dai_trigger;
3113 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3114 rtd->ops.close = dpcm_fe_dai_close;
3115 rtd->ops.pointer = soc_pcm_pointer;
3116 rtd->ops.ioctl = soc_pcm_ioctl;
3118 rtd->ops.open = soc_pcm_open;
3119 rtd->ops.hw_params = soc_pcm_hw_params;
3120 rtd->ops.prepare = soc_pcm_prepare;
3121 rtd->ops.trigger = soc_pcm_trigger;
3122 rtd->ops.hw_free = soc_pcm_hw_free;
3123 rtd->ops.close = soc_pcm_close;
3124 rtd->ops.pointer = soc_pcm_pointer;
3125 rtd->ops.ioctl = soc_pcm_ioctl;
3128 for_each_rtdcom(rtd, rtdcom) {
3129 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3135 rtd->ops.ack = soc_rtdcom_ack;
3137 rtd->ops.copy_user = soc_rtdcom_copy_user;
3138 if (ops->copy_kernel)
3139 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3140 if (ops->fill_silence)
3141 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3143 rtd->ops.page = soc_rtdcom_page;
3145 rtd->ops.mmap = soc_rtdcom_mmap;
3149 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3152 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3154 for_each_rtdcom(rtd, rtdcom) {
3155 component = rtdcom->component;
3157 if (!component->driver->pcm_new)
3160 ret = component->driver->pcm_new(rtd);
3162 dev_err(component->dev,
3163 "ASoC: pcm constructor failed: %d\n",
3169 pcm->private_free = soc_pcm_private_free;
3170 pcm->no_device_suspend = true;
3172 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3173 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3178 /* is the current PCM operation for this FE ? */
3179 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3181 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3185 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3187 /* is the current PCM operation for this BE ? */
3188 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3189 struct snd_soc_pcm_runtime *be, int stream)
3191 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3192 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3193 be->dpcm[stream].runtime_update))
3197 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3199 /* get the substream for this BE */
3200 struct snd_pcm_substream *
3201 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3203 return be->pcm->streams[stream].substream;
3205 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3207 /* get the BE runtime state */
3208 enum snd_soc_dpcm_state
3209 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3211 return be->dpcm[stream].state;
3213 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3215 /* set the BE runtime state */
3216 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3217 int stream, enum snd_soc_dpcm_state state)
3219 be->dpcm[stream].state = state;
3221 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3224 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3225 * are not running, paused or suspended for the specified stream direction.
3227 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3228 struct snd_soc_pcm_runtime *be, int stream)
3230 struct snd_soc_dpcm *dpcm;
3233 unsigned long flags;
3235 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3236 for_each_dpcm_fe(be, stream, dpcm) {
3241 state = dpcm->fe->dpcm[stream].state;
3242 if (state == SND_SOC_DPCM_STATE_START ||
3243 state == SND_SOC_DPCM_STATE_PAUSED ||
3244 state == SND_SOC_DPCM_STATE_SUSPEND) {
3249 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3251 /* it's safe to free/stop this BE DAI */
3254 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3257 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3258 * running, paused or suspended for the specified stream direction.
3260 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3261 struct snd_soc_pcm_runtime *be, int stream)
3263 struct snd_soc_dpcm *dpcm;
3266 unsigned long flags;
3268 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3269 for_each_dpcm_fe(be, stream, dpcm) {
3274 state = dpcm->fe->dpcm[stream].state;
3275 if (state == SND_SOC_DPCM_STATE_START ||
3276 state == SND_SOC_DPCM_STATE_PAUSED ||
3277 state == SND_SOC_DPCM_STATE_SUSPEND ||
3278 state == SND_SOC_DPCM_STATE_PREPARE) {
3283 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3285 /* it's safe to change hw_params */
3288 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3290 #ifdef CONFIG_DEBUG_FS
3291 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3294 case SND_SOC_DPCM_STATE_NEW:
3296 case SND_SOC_DPCM_STATE_OPEN:
3298 case SND_SOC_DPCM_STATE_HW_PARAMS:
3300 case SND_SOC_DPCM_STATE_PREPARE:
3302 case SND_SOC_DPCM_STATE_START:
3304 case SND_SOC_DPCM_STATE_STOP:
3306 case SND_SOC_DPCM_STATE_SUSPEND:
3308 case SND_SOC_DPCM_STATE_PAUSED:
3310 case SND_SOC_DPCM_STATE_HW_FREE:
3312 case SND_SOC_DPCM_STATE_CLOSE:
3319 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3320 int stream, char *buf, size_t size)
3322 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3323 struct snd_soc_dpcm *dpcm;
3325 unsigned long flags;
3328 offset += snprintf(buf + offset, size - offset,
3329 "[%s - %s]\n", fe->dai_link->name,
3330 stream ? "Capture" : "Playback");
3332 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3333 dpcm_state_string(fe->dpcm[stream].state));
3335 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3336 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3337 offset += snprintf(buf + offset, size - offset,
3339 "Format = %s, Channels = %d, Rate = %d\n",
3340 snd_pcm_format_name(params_format(params)),
3341 params_channels(params),
3342 params_rate(params));
3345 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3347 if (list_empty(&fe->dpcm[stream].be_clients)) {
3348 offset += snprintf(buf + offset, size - offset,
3349 " No active DSP links\n");
3353 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3354 for_each_dpcm_be(fe, stream, dpcm) {
3355 struct snd_soc_pcm_runtime *be = dpcm->be;
3356 params = &dpcm->hw_params;
3358 offset += snprintf(buf + offset, size - offset,
3359 "- %s\n", be->dai_link->name);
3361 offset += snprintf(buf + offset, size - offset,
3363 dpcm_state_string(be->dpcm[stream].state));
3365 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3366 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3367 offset += snprintf(buf + offset, size - offset,
3368 " Hardware Params: "
3369 "Format = %s, Channels = %d, Rate = %d\n",
3370 snd_pcm_format_name(params_format(params)),
3371 params_channels(params),
3372 params_rate(params));
3374 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3379 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3380 size_t count, loff_t *ppos)
3382 struct snd_soc_pcm_runtime *fe = file->private_data;
3383 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3386 buf = kmalloc(out_count, GFP_KERNEL);
3390 if (fe->cpu_dai->driver->playback.channels_min)
3391 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3392 buf + offset, out_count - offset);
3394 if (fe->cpu_dai->driver->capture.channels_min)
3395 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3396 buf + offset, out_count - offset);
3398 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3404 static const struct file_operations dpcm_state_fops = {
3405 .open = simple_open,
3406 .read = dpcm_state_read_file,
3407 .llseek = default_llseek,
3410 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3415 if (!rtd->card->debugfs_card_root)
3418 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3419 rtd->card->debugfs_card_root);
3420 if (!rtd->debugfs_dpcm_root) {
3422 "ASoC: Failed to create dpcm debugfs directory %s\n",
3423 rtd->dai_link->name);
3427 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3428 rtd, &dpcm_state_fops);