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 rate at all, it supports the stream. */
47 return codec_stream->rates;
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_close(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;
456 for_each_rtdcom(rtd, rtdcom) {
457 component = rtdcom->component;
459 if (component == last)
462 if (!component->driver->ops ||
463 !component->driver->ops->close)
466 component->driver->ops->close(substream);
468 if (component->driver->module_get_upon_open)
469 module_put(component->dev->driver->owner);
476 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
477 * then initialized and any private data can be allocated. This also calls
478 * startup for the cpu DAI, component, machine and codec DAI.
480 static int soc_pcm_open(struct snd_pcm_substream *substream)
482 struct snd_soc_pcm_runtime *rtd = substream->private_data;
483 struct snd_pcm_runtime *runtime = substream->runtime;
484 struct snd_soc_component *component;
485 struct snd_soc_rtdcom_list *rtdcom;
486 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
487 struct snd_soc_dai *codec_dai;
488 const char *codec_dai_name = "multicodec";
491 pinctrl_pm_select_default_state(cpu_dai->dev);
492 for_each_rtd_codec_dai(rtd, i, codec_dai)
493 pinctrl_pm_select_default_state(codec_dai->dev);
495 for_each_rtdcom(rtd, rtdcom) {
496 component = rtdcom->component;
498 pm_runtime_get_sync(component->dev);
501 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
503 /* startup the audio subsystem */
504 if (cpu_dai->driver->ops->startup) {
505 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
507 dev_err(cpu_dai->dev, "ASoC: can't open interface"
508 " %s: %d\n", cpu_dai->name, ret);
513 for_each_rtdcom(rtd, rtdcom) {
514 component = rtdcom->component;
516 if (!component->driver->ops ||
517 !component->driver->ops->open)
520 if (component->driver->module_get_upon_open &&
521 !try_module_get(component->dev->driver->owner))
524 ret = component->driver->ops->open(substream);
526 dev_err(component->dev,
527 "ASoC: can't open component %s: %d\n",
528 component->name, ret);
534 for_each_rtd_codec_dai(rtd, i, codec_dai) {
535 if (codec_dai->driver->ops->startup) {
536 ret = codec_dai->driver->ops->startup(substream,
539 dev_err(codec_dai->dev,
540 "ASoC: can't open codec %s: %d\n",
541 codec_dai->name, ret);
546 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
547 codec_dai->tx_mask = 0;
549 codec_dai->rx_mask = 0;
552 if (rtd->dai_link->ops->startup) {
553 ret = rtd->dai_link->ops->startup(substream);
555 pr_err("ASoC: %s startup failed: %d\n",
556 rtd->dai_link->name, ret);
561 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
562 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
565 /* Check that the codec and cpu DAIs are compatible */
566 soc_pcm_init_runtime_hw(substream);
568 if (rtd->num_codecs == 1)
569 codec_dai_name = rtd->codec_dai->name;
571 if (soc_pcm_has_symmetry(substream))
572 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
575 if (!runtime->hw.rates) {
576 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
577 codec_dai_name, cpu_dai->name);
580 if (!runtime->hw.formats) {
581 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
582 codec_dai_name, cpu_dai->name);
585 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
586 runtime->hw.channels_min > runtime->hw.channels_max) {
587 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
588 codec_dai_name, cpu_dai->name);
592 soc_pcm_apply_msb(substream);
594 /* Symmetry only applies if we've already got an active stream. */
595 if (cpu_dai->active) {
596 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
601 for_each_rtd_codec_dai(rtd, i, codec_dai) {
602 if (codec_dai->active) {
603 ret = soc_pcm_apply_symmetry(substream, codec_dai);
609 pr_debug("ASoC: %s <-> %s info:\n",
610 codec_dai_name, cpu_dai->name);
611 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
612 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
613 runtime->hw.channels_max);
614 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
615 runtime->hw.rate_max);
619 snd_soc_runtime_activate(rtd, substream->stream);
621 mutex_unlock(&rtd->pcm_mutex);
625 if (rtd->dai_link->ops->shutdown)
626 rtd->dai_link->ops->shutdown(substream);
632 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
633 if (codec_dai->driver->ops->shutdown)
634 codec_dai->driver->ops->shutdown(substream, codec_dai);
638 soc_pcm_components_close(substream, component);
640 if (cpu_dai->driver->ops->shutdown)
641 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
643 mutex_unlock(&rtd->pcm_mutex);
645 for_each_rtdcom(rtd, rtdcom) {
646 component = rtdcom->component;
648 pm_runtime_mark_last_busy(component->dev);
649 pm_runtime_put_autosuspend(component->dev);
652 for_each_rtd_codec_dai(rtd, i, codec_dai) {
653 if (!codec_dai->active)
654 pinctrl_pm_select_sleep_state(codec_dai->dev);
656 if (!cpu_dai->active)
657 pinctrl_pm_select_sleep_state(cpu_dai->dev);
663 * Power down the audio subsystem pmdown_time msecs after close is called.
664 * This is to ensure there are no pops or clicks in between any music tracks
665 * due to DAPM power cycling.
667 static void close_delayed_work(struct work_struct *work)
669 struct snd_soc_pcm_runtime *rtd =
670 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
671 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
673 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
675 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
676 codec_dai->driver->playback.stream_name,
677 codec_dai->playback_active ? "active" : "inactive",
678 rtd->pop_wait ? "yes" : "no");
680 /* are we waiting on this codec DAI stream */
681 if (rtd->pop_wait == 1) {
683 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
684 SND_SOC_DAPM_STREAM_STOP);
687 mutex_unlock(&rtd->pcm_mutex);
691 * Called by ALSA when a PCM substream is closed. Private data can be
692 * freed here. The cpu DAI, codec DAI, machine and components are also
695 static int soc_pcm_close(struct snd_pcm_substream *substream)
697 struct snd_soc_pcm_runtime *rtd = substream->private_data;
698 struct snd_soc_component *component;
699 struct snd_soc_rtdcom_list *rtdcom;
700 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
701 struct snd_soc_dai *codec_dai;
704 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
706 snd_soc_runtime_deactivate(rtd, substream->stream);
708 /* clear the corresponding DAIs rate when inactive */
709 if (!cpu_dai->active)
712 for_each_rtd_codec_dai(rtd, i, codec_dai) {
713 if (!codec_dai->active)
717 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
719 if (cpu_dai->driver->ops->shutdown)
720 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
722 for_each_rtd_codec_dai(rtd, i, codec_dai) {
723 if (codec_dai->driver->ops->shutdown)
724 codec_dai->driver->ops->shutdown(substream, codec_dai);
727 if (rtd->dai_link->ops->shutdown)
728 rtd->dai_link->ops->shutdown(substream);
730 soc_pcm_components_close(substream, NULL);
732 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
733 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
734 /* powered down playback stream now */
735 snd_soc_dapm_stream_event(rtd,
736 SNDRV_PCM_STREAM_PLAYBACK,
737 SND_SOC_DAPM_STREAM_STOP);
739 /* start delayed pop wq here for playback streams */
741 queue_delayed_work(system_power_efficient_wq,
743 msecs_to_jiffies(rtd->pmdown_time));
746 /* capture streams can be powered down now */
747 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
748 SND_SOC_DAPM_STREAM_STOP);
751 mutex_unlock(&rtd->pcm_mutex);
753 for_each_rtdcom(rtd, rtdcom) {
754 component = rtdcom->component;
756 pm_runtime_mark_last_busy(component->dev);
757 pm_runtime_put_autosuspend(component->dev);
760 for_each_rtd_codec_dai(rtd, i, codec_dai) {
761 if (!codec_dai->active)
762 pinctrl_pm_select_sleep_state(codec_dai->dev);
764 if (!cpu_dai->active)
765 pinctrl_pm_select_sleep_state(cpu_dai->dev);
771 * Called by ALSA when the PCM substream is prepared, can set format, sample
772 * rate, etc. This function is non atomic and can be called multiple times,
773 * it can refer to the runtime info.
775 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
778 struct snd_soc_component *component;
779 struct snd_soc_rtdcom_list *rtdcom;
780 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
781 struct snd_soc_dai *codec_dai;
784 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
786 if (rtd->dai_link->ops->prepare) {
787 ret = rtd->dai_link->ops->prepare(substream);
789 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
795 for_each_rtdcom(rtd, rtdcom) {
796 component = rtdcom->component;
798 if (!component->driver->ops ||
799 !component->driver->ops->prepare)
802 ret = component->driver->ops->prepare(substream);
804 dev_err(component->dev,
805 "ASoC: platform prepare error: %d\n", ret);
810 for_each_rtd_codec_dai(rtd, i, codec_dai) {
811 if (codec_dai->driver->ops->prepare) {
812 ret = codec_dai->driver->ops->prepare(substream,
815 dev_err(codec_dai->dev,
816 "ASoC: codec DAI prepare error: %d\n",
823 if (cpu_dai->driver->ops->prepare) {
824 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
826 dev_err(cpu_dai->dev,
827 "ASoC: cpu DAI prepare error: %d\n", ret);
832 /* cancel any delayed stream shutdown that is pending */
833 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
836 cancel_delayed_work(&rtd->delayed_work);
839 snd_soc_dapm_stream_event(rtd, substream->stream,
840 SND_SOC_DAPM_STREAM_START);
842 for_each_rtd_codec_dai(rtd, i, codec_dai)
843 snd_soc_dai_digital_mute(codec_dai, 0,
845 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
848 mutex_unlock(&rtd->pcm_mutex);
852 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
855 struct snd_interval *interval;
856 int channels = hweight_long(mask);
858 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
859 interval->min = channels;
860 interval->max = channels;
863 int soc_dai_hw_params(struct snd_pcm_substream *substream,
864 struct snd_pcm_hw_params *params,
865 struct snd_soc_dai *dai)
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
870 /* perform any topology hw_params fixups before DAI */
871 if (rtd->dai_link->be_hw_params_fixup) {
872 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
875 "ASoC: hw_params topology fixup failed %d\n",
881 if (dai->driver->ops->hw_params) {
882 ret = dai->driver->ops->hw_params(substream, params, dai);
884 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
893 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
894 struct snd_soc_component *last)
896 struct snd_soc_pcm_runtime *rtd = substream->private_data;
897 struct snd_soc_rtdcom_list *rtdcom;
898 struct snd_soc_component *component;
900 for_each_rtdcom(rtd, rtdcom) {
901 component = rtdcom->component;
903 if (component == last)
906 if (!component->driver->ops ||
907 !component->driver->ops->hw_free)
910 component->driver->ops->hw_free(substream);
917 * Called by ALSA when the hardware params are set by application. This
918 * function can also be called multiple times and can allocate buffers
919 * (using snd_pcm_lib_* ). It's non-atomic.
921 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
922 struct snd_pcm_hw_params *params)
924 struct snd_soc_pcm_runtime *rtd = substream->private_data;
925 struct snd_soc_component *component;
926 struct snd_soc_rtdcom_list *rtdcom;
927 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
928 struct snd_soc_dai *codec_dai;
931 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
932 if (rtd->dai_link->ops->hw_params) {
933 ret = rtd->dai_link->ops->hw_params(substream, params);
935 dev_err(rtd->card->dev, "ASoC: machine hw_params"
936 " failed: %d\n", ret);
941 for_each_rtd_codec_dai(rtd, i, codec_dai) {
942 struct snd_pcm_hw_params codec_params;
945 * Skip CODECs which don't support the current stream type,
946 * the idea being that if a CODEC is not used for the currently
947 * set up transfer direction, it should not need to be
948 * configured, especially since the configuration used might
949 * not even be supported by that CODEC. There may be cases
950 * however where a CODEC needs to be set up although it is
951 * actually not being used for the transfer, e.g. if a
952 * capture-only CODEC is acting as an LRCLK and/or BCLK master
953 * for the DAI link including a playback-only CODEC.
954 * If this becomes necessary, we will have to augment the
955 * machine driver setup with information on how to act, so
956 * we can do the right thing here.
958 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
961 /* copy params for each codec */
962 codec_params = *params;
964 /* fixup params based on TDM slot masks */
965 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
967 soc_pcm_codec_params_fixup(&codec_params,
970 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
972 soc_pcm_codec_params_fixup(&codec_params,
975 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
979 codec_dai->rate = params_rate(&codec_params);
980 codec_dai->channels = params_channels(&codec_params);
981 codec_dai->sample_bits = snd_pcm_format_physical_width(
982 params_format(&codec_params));
984 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
987 ret = soc_dai_hw_params(substream, params, cpu_dai);
991 for_each_rtdcom(rtd, rtdcom) {
992 component = rtdcom->component;
994 if (!component->driver->ops ||
995 !component->driver->ops->hw_params)
998 ret = component->driver->ops->hw_params(substream, params);
1000 dev_err(component->dev,
1001 "ASoC: %s hw params failed: %d\n",
1002 component->name, ret);
1008 /* store the parameters for each DAIs */
1009 cpu_dai->rate = params_rate(params);
1010 cpu_dai->channels = params_channels(params);
1011 cpu_dai->sample_bits =
1012 snd_pcm_format_physical_width(params_format(params));
1014 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1016 ret = soc_pcm_params_symmetry(substream, params);
1020 mutex_unlock(&rtd->pcm_mutex);
1024 soc_pcm_components_hw_free(substream, component);
1026 if (cpu_dai->driver->ops->hw_free)
1027 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1030 i = rtd->num_codecs;
1033 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1034 if (codec_dai->driver->ops->hw_free)
1035 codec_dai->driver->ops->hw_free(substream, codec_dai);
1036 codec_dai->rate = 0;
1039 if (rtd->dai_link->ops->hw_free)
1040 rtd->dai_link->ops->hw_free(substream);
1042 mutex_unlock(&rtd->pcm_mutex);
1047 * Frees resources allocated by hw_params, can be called multiple times
1049 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1051 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1052 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1053 struct snd_soc_dai *codec_dai;
1054 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1057 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1059 /* clear the corresponding DAIs parameters when going to be inactive */
1060 if (cpu_dai->active == 1) {
1062 cpu_dai->channels = 0;
1063 cpu_dai->sample_bits = 0;
1066 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1067 if (codec_dai->active == 1) {
1068 codec_dai->rate = 0;
1069 codec_dai->channels = 0;
1070 codec_dai->sample_bits = 0;
1074 /* apply codec digital mute */
1075 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1076 if ((playback && codec_dai->playback_active == 1) ||
1077 (!playback && codec_dai->capture_active == 1))
1078 snd_soc_dai_digital_mute(codec_dai, 1,
1082 /* free any machine hw params */
1083 if (rtd->dai_link->ops->hw_free)
1084 rtd->dai_link->ops->hw_free(substream);
1086 /* free any component resources */
1087 soc_pcm_components_hw_free(substream, NULL);
1089 /* now free hw params for the DAIs */
1090 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1091 if (codec_dai->driver->ops->hw_free)
1092 codec_dai->driver->ops->hw_free(substream, codec_dai);
1095 if (cpu_dai->driver->ops->hw_free)
1096 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1098 mutex_unlock(&rtd->pcm_mutex);
1102 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1104 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1105 struct snd_soc_component *component;
1106 struct snd_soc_rtdcom_list *rtdcom;
1107 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1108 struct snd_soc_dai *codec_dai;
1111 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1112 if (codec_dai->driver->ops->trigger) {
1113 ret = codec_dai->driver->ops->trigger(substream,
1120 for_each_rtdcom(rtd, rtdcom) {
1121 component = rtdcom->component;
1123 if (!component->driver->ops ||
1124 !component->driver->ops->trigger)
1127 ret = component->driver->ops->trigger(substream, cmd);
1132 if (cpu_dai->driver->ops->trigger) {
1133 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1138 if (rtd->dai_link->ops->trigger) {
1139 ret = rtd->dai_link->ops->trigger(substream, cmd);
1147 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1150 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1151 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1152 struct snd_soc_dai *codec_dai;
1155 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1156 if (codec_dai->driver->ops->bespoke_trigger) {
1157 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1164 if (cpu_dai->driver->ops->bespoke_trigger) {
1165 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1172 * soc level wrapper for pointer callback
1173 * If cpu_dai, codec_dai, component driver has the delay callback, then
1174 * the runtime->delay will be updated accordingly.
1176 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1179 struct snd_soc_component *component;
1180 struct snd_soc_rtdcom_list *rtdcom;
1181 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1182 struct snd_soc_dai *codec_dai;
1183 struct snd_pcm_runtime *runtime = substream->runtime;
1184 snd_pcm_uframes_t offset = 0;
1185 snd_pcm_sframes_t delay = 0;
1186 snd_pcm_sframes_t codec_delay = 0;
1189 /* clearing the previous total delay */
1192 for_each_rtdcom(rtd, rtdcom) {
1193 component = rtdcom->component;
1195 if (!component->driver->ops ||
1196 !component->driver->ops->pointer)
1199 /* FIXME: use 1st pointer */
1200 offset = component->driver->ops->pointer(substream);
1203 /* base delay if assigned in pointer callback */
1204 delay = runtime->delay;
1206 if (cpu_dai->driver->ops->delay)
1207 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1209 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1210 if (codec_dai->driver->ops->delay)
1211 codec_delay = max(codec_delay,
1212 codec_dai->driver->ops->delay(substream,
1215 delay += codec_delay;
1217 runtime->delay = delay;
1222 /* connect a FE and BE */
1223 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1224 struct snd_soc_pcm_runtime *be, int stream)
1226 struct snd_soc_dpcm *dpcm;
1227 unsigned long flags;
1229 /* only add new dpcms */
1230 for_each_dpcm_be(fe, stream, dpcm) {
1231 if (dpcm->be == be && dpcm->fe == fe)
1235 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1241 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1242 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1243 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1244 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1245 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1246 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1248 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1249 stream ? "capture" : "playback", fe->dai_link->name,
1250 stream ? "<-" : "->", be->dai_link->name);
1252 #ifdef CONFIG_DEBUG_FS
1253 if (fe->debugfs_dpcm_root)
1254 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1255 fe->debugfs_dpcm_root, &dpcm->state);
1260 /* reparent a BE onto another FE */
1261 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1262 struct snd_soc_pcm_runtime *be, int stream)
1264 struct snd_soc_dpcm *dpcm;
1265 struct snd_pcm_substream *fe_substream, *be_substream;
1267 /* reparent if BE is connected to other FEs */
1268 if (!be->dpcm[stream].users)
1271 be_substream = snd_soc_dpcm_get_substream(be, stream);
1273 for_each_dpcm_fe(be, stream, dpcm) {
1277 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1278 stream ? "capture" : "playback",
1279 dpcm->fe->dai_link->name,
1280 stream ? "<-" : "->", dpcm->be->dai_link->name);
1282 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1283 be_substream->runtime = fe_substream->runtime;
1288 /* disconnect a BE and FE */
1289 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1291 struct snd_soc_dpcm *dpcm, *d;
1292 unsigned long flags;
1294 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1295 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1296 stream ? "capture" : "playback",
1297 dpcm->be->dai_link->name);
1299 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1302 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1303 stream ? "capture" : "playback", fe->dai_link->name,
1304 stream ? "<-" : "->", dpcm->be->dai_link->name);
1306 /* BEs still alive need new FE */
1307 dpcm_be_reparent(fe, dpcm->be, stream);
1309 #ifdef CONFIG_DEBUG_FS
1310 debugfs_remove(dpcm->debugfs_state);
1312 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1313 list_del(&dpcm->list_be);
1314 list_del(&dpcm->list_fe);
1315 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1320 /* get BE for DAI widget and stream */
1321 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1322 struct snd_soc_dapm_widget *widget, int stream)
1324 struct snd_soc_pcm_runtime *be;
1325 struct snd_soc_dai *dai;
1328 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1330 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1331 for_each_card_rtds(card, be) {
1333 if (!be->dai_link->no_pcm)
1336 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1337 be->cpu_dai->playback_widget ?
1338 be->cpu_dai->playback_widget->name : "(not set)");
1340 if (be->cpu_dai->playback_widget == widget)
1343 for_each_rtd_codec_dai(be, i, dai) {
1344 if (dai->playback_widget == widget)
1350 for_each_card_rtds(card, be) {
1352 if (!be->dai_link->no_pcm)
1355 dev_dbg(card->dev, "ASoC: try BE %s\n",
1356 be->cpu_dai->capture_widget ?
1357 be->cpu_dai->capture_widget->name : "(not set)");
1359 if (be->cpu_dai->capture_widget == widget)
1362 for_each_rtd_codec_dai(be, i, dai) {
1363 if (dai->capture_widget == widget)
1369 /* dai link name and stream name set correctly ? */
1370 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1371 stream ? "capture" : "playback", widget->name);
1375 static inline struct snd_soc_dapm_widget *
1376 dai_get_widget(struct snd_soc_dai *dai, int stream)
1378 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1379 return dai->playback_widget;
1381 return dai->capture_widget;
1384 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1385 struct snd_soc_dapm_widget *widget)
1389 for (i = 0; i < list->num_widgets; i++) {
1390 if (widget == list->widgets[i])
1397 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1398 enum snd_soc_dapm_direction dir)
1400 struct snd_soc_card *card = widget->dapm->card;
1401 struct snd_soc_pcm_runtime *rtd;
1402 struct snd_soc_dai *dai;
1405 if (dir == SND_SOC_DAPM_DIR_OUT) {
1406 for_each_card_rtds(card, rtd) {
1407 if (!rtd->dai_link->no_pcm)
1410 if (rtd->cpu_dai->playback_widget == widget)
1413 for_each_rtd_codec_dai(rtd, i, dai) {
1414 if (dai->playback_widget == widget)
1418 } else { /* SND_SOC_DAPM_DIR_IN */
1419 for_each_card_rtds(card, rtd) {
1420 if (!rtd->dai_link->no_pcm)
1423 if (rtd->cpu_dai->capture_widget == widget)
1426 for_each_rtd_codec_dai(rtd, i, dai) {
1427 if (dai->capture_widget == widget)
1436 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1437 int stream, struct snd_soc_dapm_widget_list **list)
1439 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1442 /* get number of valid DAI paths and their widgets */
1443 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1444 dpcm_end_walk_at_be);
1446 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1447 stream ? "capture" : "playback");
1452 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1453 struct snd_soc_dapm_widget_list **list_)
1455 struct snd_soc_dpcm *dpcm;
1456 struct snd_soc_dapm_widget_list *list = *list_;
1457 struct snd_soc_dapm_widget *widget;
1458 struct snd_soc_dai *dai;
1461 /* Destroy any old FE <--> BE connections */
1462 for_each_dpcm_be(fe, stream, dpcm) {
1465 /* is there a valid CPU DAI widget for this BE */
1466 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1468 /* prune the BE if it's no longer in our active list */
1469 if (widget && widget_in_list(list, widget))
1472 /* is there a valid CODEC DAI widget for this BE */
1473 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1474 widget = dai_get_widget(dai, stream);
1476 /* prune the BE if it's no longer in our active list */
1477 if (widget && widget_in_list(list, widget))
1481 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1482 stream ? "capture" : "playback",
1483 dpcm->be->dai_link->name, fe->dai_link->name);
1484 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1485 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1489 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1493 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1494 struct snd_soc_dapm_widget_list **list_)
1496 struct snd_soc_card *card = fe->card;
1497 struct snd_soc_dapm_widget_list *list = *list_;
1498 struct snd_soc_pcm_runtime *be;
1499 int i, new = 0, err;
1501 /* Create any new FE <--> BE connections */
1502 for (i = 0; i < list->num_widgets; i++) {
1504 switch (list->widgets[i]->id) {
1505 case snd_soc_dapm_dai_in:
1506 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1509 case snd_soc_dapm_dai_out:
1510 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1517 /* is there a valid BE rtd for this widget */
1518 be = dpcm_get_be(card, list->widgets[i], stream);
1520 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1521 list->widgets[i]->name);
1525 /* make sure BE is a real BE */
1526 if (!be->dai_link->no_pcm)
1529 /* don't connect if FE is not running */
1530 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1533 /* newly connected FE and BE */
1534 err = dpcm_be_connect(fe, be, stream);
1536 dev_err(fe->dev, "ASoC: can't connect %s\n",
1537 list->widgets[i]->name);
1539 } else if (err == 0) /* already connected */
1543 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1547 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1552 * Find the corresponding BE DAIs that source or sink audio to this
1555 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1556 int stream, struct snd_soc_dapm_widget_list **list, int new)
1559 return dpcm_add_paths(fe, stream, list);
1561 return dpcm_prune_paths(fe, stream, list);
1564 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1566 struct snd_soc_dpcm *dpcm;
1567 unsigned long flags;
1569 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1570 for_each_dpcm_be(fe, stream, dpcm)
1571 dpcm->be->dpcm[stream].runtime_update =
1572 SND_SOC_DPCM_UPDATE_NO;
1573 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1576 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1579 struct snd_soc_dpcm *dpcm;
1581 /* disable any enabled and non active backends */
1582 for_each_dpcm_be(fe, stream, dpcm) {
1584 struct snd_soc_pcm_runtime *be = dpcm->be;
1585 struct snd_pcm_substream *be_substream =
1586 snd_soc_dpcm_get_substream(be, stream);
1588 if (be->dpcm[stream].users == 0)
1589 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1590 stream ? "capture" : "playback",
1591 be->dpcm[stream].state);
1593 if (--be->dpcm[stream].users != 0)
1596 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1599 soc_pcm_close(be_substream);
1600 be_substream->runtime = NULL;
1601 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1605 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1607 struct snd_soc_dpcm *dpcm;
1610 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1611 for_each_dpcm_be(fe, stream, dpcm) {
1613 struct snd_soc_pcm_runtime *be = dpcm->be;
1614 struct snd_pcm_substream *be_substream =
1615 snd_soc_dpcm_get_substream(be, stream);
1617 if (!be_substream) {
1618 dev_err(be->dev, "ASoC: no backend %s stream\n",
1619 stream ? "capture" : "playback");
1623 /* is this op for this BE ? */
1624 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1627 /* first time the dpcm is open ? */
1628 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1629 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1630 stream ? "capture" : "playback",
1631 be->dpcm[stream].state);
1633 if (be->dpcm[stream].users++ != 0)
1636 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1637 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1640 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1641 stream ? "capture" : "playback", be->dai_link->name);
1643 be_substream->runtime = be->dpcm[stream].runtime;
1644 err = soc_pcm_open(be_substream);
1646 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1647 be->dpcm[stream].users--;
1648 if (be->dpcm[stream].users < 0)
1649 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1650 stream ? "capture" : "playback",
1651 be->dpcm[stream].state);
1653 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1657 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1664 /* disable any enabled and non active backends */
1665 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1666 struct snd_soc_pcm_runtime *be = dpcm->be;
1667 struct snd_pcm_substream *be_substream =
1668 snd_soc_dpcm_get_substream(be, stream);
1670 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1673 if (be->dpcm[stream].users == 0)
1674 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1675 stream ? "capture" : "playback",
1676 be->dpcm[stream].state);
1678 if (--be->dpcm[stream].users != 0)
1681 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1684 soc_pcm_close(be_substream);
1685 be_substream->runtime = NULL;
1686 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1692 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1693 struct snd_soc_pcm_stream *stream)
1695 runtime->hw.rate_min = stream->rate_min;
1696 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1697 runtime->hw.channels_min = stream->channels_min;
1698 runtime->hw.channels_max = stream->channels_max;
1699 if (runtime->hw.formats)
1700 runtime->hw.formats &= stream->formats;
1702 runtime->hw.formats = stream->formats;
1703 runtime->hw.rates = stream->rates;
1706 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1709 struct snd_soc_pcm_runtime *fe = substream->private_data;
1710 struct snd_soc_dpcm *dpcm;
1711 struct snd_soc_dai *dai;
1712 int stream = substream->stream;
1714 if (!fe->dai_link->dpcm_merged_format)
1718 * It returns merged BE codec format
1719 * if FE want to use it (= dpcm_merged_format)
1722 for_each_dpcm_be(fe, stream, dpcm) {
1723 struct snd_soc_pcm_runtime *be = dpcm->be;
1724 struct snd_soc_dai_driver *codec_dai_drv;
1725 struct snd_soc_pcm_stream *codec_stream;
1728 for_each_rtd_codec_dai(be, i, dai) {
1730 * Skip CODECs which don't support the current stream
1731 * type. See soc_pcm_init_runtime_hw() for more details
1733 if (!snd_soc_dai_stream_valid(dai, stream))
1736 codec_dai_drv = dai->driver;
1737 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1738 codec_stream = &codec_dai_drv->playback;
1740 codec_stream = &codec_dai_drv->capture;
1742 *formats &= codec_stream->formats;
1747 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1748 unsigned int *channels_min,
1749 unsigned int *channels_max)
1751 struct snd_soc_pcm_runtime *fe = substream->private_data;
1752 struct snd_soc_dpcm *dpcm;
1753 int stream = substream->stream;
1755 if (!fe->dai_link->dpcm_merged_chan)
1759 * It returns merged BE codec channel;
1760 * if FE want to use it (= dpcm_merged_chan)
1763 for_each_dpcm_be(fe, stream, dpcm) {
1764 struct snd_soc_pcm_runtime *be = dpcm->be;
1765 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1766 struct snd_soc_dai_driver *codec_dai_drv;
1767 struct snd_soc_pcm_stream *codec_stream;
1768 struct snd_soc_pcm_stream *cpu_stream;
1770 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1771 cpu_stream = &cpu_dai_drv->playback;
1773 cpu_stream = &cpu_dai_drv->capture;
1775 *channels_min = max(*channels_min, cpu_stream->channels_min);
1776 *channels_max = min(*channels_max, cpu_stream->channels_max);
1779 * chan min/max cannot be enforced if there are multiple CODEC
1780 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1782 if (be->num_codecs == 1) {
1783 codec_dai_drv = be->codec_dais[0]->driver;
1785 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1786 codec_stream = &codec_dai_drv->playback;
1788 codec_stream = &codec_dai_drv->capture;
1790 *channels_min = max(*channels_min,
1791 codec_stream->channels_min);
1792 *channels_max = min(*channels_max,
1793 codec_stream->channels_max);
1798 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1799 unsigned int *rates,
1800 unsigned int *rate_min,
1801 unsigned int *rate_max)
1803 struct snd_soc_pcm_runtime *fe = substream->private_data;
1804 struct snd_soc_dpcm *dpcm;
1805 int stream = substream->stream;
1807 if (!fe->dai_link->dpcm_merged_rate)
1811 * It returns merged BE codec channel;
1812 * if FE want to use it (= dpcm_merged_chan)
1815 for_each_dpcm_be(fe, stream, dpcm) {
1816 struct snd_soc_pcm_runtime *be = dpcm->be;
1817 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1818 struct snd_soc_dai_driver *codec_dai_drv;
1819 struct snd_soc_pcm_stream *codec_stream;
1820 struct snd_soc_pcm_stream *cpu_stream;
1821 struct snd_soc_dai *dai;
1824 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1825 cpu_stream = &cpu_dai_drv->playback;
1827 cpu_stream = &cpu_dai_drv->capture;
1829 *rate_min = max(*rate_min, cpu_stream->rate_min);
1830 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1831 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1833 for_each_rtd_codec_dai(be, i, dai) {
1835 * Skip CODECs which don't support the current stream
1836 * type. See soc_pcm_init_runtime_hw() for more details
1838 if (!snd_soc_dai_stream_valid(dai, stream))
1841 codec_dai_drv = dai->driver;
1842 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1843 codec_stream = &codec_dai_drv->playback;
1845 codec_stream = &codec_dai_drv->capture;
1847 *rate_min = max(*rate_min, codec_stream->rate_min);
1848 *rate_max = min_not_zero(*rate_max,
1849 codec_stream->rate_max);
1850 *rates = snd_pcm_rate_mask_intersect(*rates,
1851 codec_stream->rates);
1856 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1858 struct snd_pcm_runtime *runtime = substream->runtime;
1859 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1860 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1861 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1864 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1866 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1868 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1869 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1870 &runtime->hw.channels_max);
1871 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1872 &runtime->hw.rate_min, &runtime->hw.rate_max);
1875 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1877 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1878 * for avoiding the race with trigger callback.
1879 * If the state is unset and a trigger is pending while the previous operation,
1880 * process the pending trigger action here.
1882 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1883 int stream, enum snd_soc_dpcm_update state)
1885 struct snd_pcm_substream *substream =
1886 snd_soc_dpcm_get_substream(fe, stream);
1888 snd_pcm_stream_lock_irq(substream);
1889 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1890 dpcm_fe_dai_do_trigger(substream,
1891 fe->dpcm[stream].trigger_pending - 1);
1892 fe->dpcm[stream].trigger_pending = 0;
1894 fe->dpcm[stream].runtime_update = state;
1895 snd_pcm_stream_unlock_irq(substream);
1898 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1901 struct snd_soc_dpcm *dpcm;
1902 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1903 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1906 /* apply symmetry for FE */
1907 if (soc_pcm_has_symmetry(fe_substream))
1908 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1910 /* Symmetry only applies if we've got an active stream. */
1911 if (fe_cpu_dai->active) {
1912 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1917 /* apply symmetry for BE */
1918 for_each_dpcm_be(fe, stream, dpcm) {
1919 struct snd_soc_pcm_runtime *be = dpcm->be;
1920 struct snd_pcm_substream *be_substream =
1921 snd_soc_dpcm_get_substream(be, stream);
1922 struct snd_soc_pcm_runtime *rtd;
1923 struct snd_soc_dai *codec_dai;
1926 /* A backend may not have the requested substream */
1930 rtd = be_substream->private_data;
1931 if (rtd->dai_link->be_hw_params_fixup)
1934 if (soc_pcm_has_symmetry(be_substream))
1935 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1937 /* Symmetry only applies if we've got an active stream. */
1938 if (rtd->cpu_dai->active) {
1939 err = soc_pcm_apply_symmetry(fe_substream,
1945 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1946 if (codec_dai->active) {
1947 err = soc_pcm_apply_symmetry(fe_substream,
1958 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1960 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1961 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1962 int stream = fe_substream->stream, ret = 0;
1964 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1966 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1968 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1972 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1974 /* start the DAI frontend */
1975 ret = soc_pcm_open(fe_substream);
1977 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1981 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1983 dpcm_set_fe_runtime(fe_substream);
1984 snd_pcm_limit_hw_rates(runtime);
1986 ret = dpcm_apply_symmetry(fe_substream, stream);
1988 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1993 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1997 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1999 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2003 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2005 struct snd_soc_dpcm *dpcm;
2007 /* only shutdown BEs that are either sinks or sources to this FE DAI */
2008 for_each_dpcm_be(fe, stream, dpcm) {
2010 struct snd_soc_pcm_runtime *be = dpcm->be;
2011 struct snd_pcm_substream *be_substream =
2012 snd_soc_dpcm_get_substream(be, stream);
2014 /* is this op for this BE ? */
2015 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2018 if (be->dpcm[stream].users == 0)
2019 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2020 stream ? "capture" : "playback",
2021 be->dpcm[stream].state);
2023 if (--be->dpcm[stream].users != 0)
2026 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2027 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2028 soc_pcm_hw_free(be_substream);
2029 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2032 dev_dbg(be->dev, "ASoC: close BE %s\n",
2033 be->dai_link->name);
2035 soc_pcm_close(be_substream);
2036 be_substream->runtime = NULL;
2038 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2043 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2045 struct snd_soc_pcm_runtime *fe = substream->private_data;
2046 int stream = substream->stream;
2048 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2050 /* shutdown the BEs */
2051 dpcm_be_dai_shutdown(fe, substream->stream);
2053 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2055 /* now shutdown the frontend */
2056 soc_pcm_close(substream);
2058 /* run the stream event for each BE */
2059 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2061 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2062 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2066 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2068 struct snd_soc_dpcm *dpcm;
2070 /* only hw_params backends that are either sinks or sources
2071 * to this frontend DAI */
2072 for_each_dpcm_be(fe, stream, dpcm) {
2074 struct snd_soc_pcm_runtime *be = dpcm->be;
2075 struct snd_pcm_substream *be_substream =
2076 snd_soc_dpcm_get_substream(be, stream);
2078 /* is this op for this BE ? */
2079 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2082 /* only free hw when no longer used - check all FEs */
2083 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2086 /* do not free hw if this BE is used by other FE */
2087 if (be->dpcm[stream].users > 1)
2090 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2091 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2092 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2093 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2094 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2095 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2098 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2099 be->dai_link->name);
2101 soc_pcm_hw_free(be_substream);
2103 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2109 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2111 struct snd_soc_pcm_runtime *fe = substream->private_data;
2112 int err, stream = substream->stream;
2114 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2115 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2117 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2119 /* call hw_free on the frontend */
2120 err = soc_pcm_hw_free(substream);
2122 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2123 fe->dai_link->name);
2125 /* only hw_params backends that are either sinks or sources
2126 * to this frontend DAI */
2127 err = dpcm_be_dai_hw_free(fe, stream);
2129 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2130 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2132 mutex_unlock(&fe->card->mutex);
2136 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2138 struct snd_soc_dpcm *dpcm;
2141 for_each_dpcm_be(fe, stream, dpcm) {
2143 struct snd_soc_pcm_runtime *be = dpcm->be;
2144 struct snd_pcm_substream *be_substream =
2145 snd_soc_dpcm_get_substream(be, stream);
2147 /* is this op for this BE ? */
2148 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2151 /* copy params for each dpcm */
2152 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2153 sizeof(struct snd_pcm_hw_params));
2155 /* perform any hw_params fixups */
2156 if (be->dai_link->be_hw_params_fixup) {
2157 ret = be->dai_link->be_hw_params_fixup(be,
2161 "ASoC: hw_params BE fixup failed %d\n",
2167 /* copy the fixed-up hw params for BE dai */
2168 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2169 sizeof(struct snd_pcm_hw_params));
2171 /* only allow hw_params() if no connected FEs are running */
2172 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2175 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2176 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2177 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2180 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2181 be->dai_link->name);
2183 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2185 dev_err(dpcm->be->dev,
2186 "ASoC: hw_params BE failed %d\n", ret);
2190 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2195 /* disable any enabled and non active backends */
2196 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2197 struct snd_soc_pcm_runtime *be = dpcm->be;
2198 struct snd_pcm_substream *be_substream =
2199 snd_soc_dpcm_get_substream(be, stream);
2201 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2204 /* only allow hw_free() if no connected FEs are running */
2205 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2208 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2209 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2210 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2211 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2214 soc_pcm_hw_free(be_substream);
2220 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2221 struct snd_pcm_hw_params *params)
2223 struct snd_soc_pcm_runtime *fe = substream->private_data;
2224 int ret, stream = substream->stream;
2226 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2227 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2229 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2230 sizeof(struct snd_pcm_hw_params));
2231 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2233 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2237 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2238 fe->dai_link->name, params_rate(params),
2239 params_channels(params), params_format(params));
2241 /* call hw_params on the frontend */
2242 ret = soc_pcm_hw_params(substream, params);
2244 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2245 dpcm_be_dai_hw_free(fe, stream);
2247 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2250 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2251 mutex_unlock(&fe->card->mutex);
2255 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2256 struct snd_pcm_substream *substream, int cmd)
2260 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2261 dpcm->be->dai_link->name, cmd);
2263 ret = soc_pcm_trigger(substream, cmd);
2265 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2270 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2273 struct snd_soc_dpcm *dpcm;
2276 for_each_dpcm_be(fe, stream, dpcm) {
2278 struct snd_soc_pcm_runtime *be = dpcm->be;
2279 struct snd_pcm_substream *be_substream =
2280 snd_soc_dpcm_get_substream(be, stream);
2282 /* is this op for this BE ? */
2283 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2287 case SNDRV_PCM_TRIGGER_START:
2288 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2289 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2292 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2296 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2298 case SNDRV_PCM_TRIGGER_RESUME:
2299 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2302 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2306 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2308 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2309 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2312 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2316 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2318 case SNDRV_PCM_TRIGGER_STOP:
2319 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2322 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2325 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2329 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2331 case SNDRV_PCM_TRIGGER_SUSPEND:
2332 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2335 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2338 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2342 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2344 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2345 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2348 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2351 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2355 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2362 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2364 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2366 struct snd_soc_pcm_runtime *fe = substream->private_data;
2367 int stream = substream->stream, ret;
2368 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2370 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2373 case SND_SOC_DPCM_TRIGGER_PRE:
2374 /* call trigger on the frontend before the backend. */
2376 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2377 fe->dai_link->name, cmd);
2379 ret = soc_pcm_trigger(substream, cmd);
2381 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2385 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2387 case SND_SOC_DPCM_TRIGGER_POST:
2388 /* call trigger on the frontend after the backend. */
2390 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2392 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2396 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2397 fe->dai_link->name, cmd);
2399 ret = soc_pcm_trigger(substream, cmd);
2401 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2402 /* bespoke trigger() - handles both FE and BEs */
2404 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2405 fe->dai_link->name, cmd);
2407 ret = soc_pcm_bespoke_trigger(substream, cmd);
2409 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2414 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2415 fe->dai_link->name);
2421 case SNDRV_PCM_TRIGGER_START:
2422 case SNDRV_PCM_TRIGGER_RESUME:
2423 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2424 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2426 case SNDRV_PCM_TRIGGER_STOP:
2427 case SNDRV_PCM_TRIGGER_SUSPEND:
2428 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2430 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2431 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2436 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2440 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2442 struct snd_soc_pcm_runtime *fe = substream->private_data;
2443 int stream = substream->stream;
2445 /* if FE's runtime_update is already set, we're in race;
2446 * process this trigger later at exit
2448 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2449 fe->dpcm[stream].trigger_pending = cmd + 1;
2450 return 0; /* delayed, assuming it's successful */
2453 /* we're alone, let's trigger */
2454 return dpcm_fe_dai_do_trigger(substream, cmd);
2457 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2459 struct snd_soc_dpcm *dpcm;
2462 for_each_dpcm_be(fe, stream, dpcm) {
2464 struct snd_soc_pcm_runtime *be = dpcm->be;
2465 struct snd_pcm_substream *be_substream =
2466 snd_soc_dpcm_get_substream(be, stream);
2468 /* is this op for this BE ? */
2469 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2472 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2473 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2474 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2477 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2478 be->dai_link->name);
2480 ret = soc_pcm_prepare(be_substream);
2482 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2487 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2492 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2494 struct snd_soc_pcm_runtime *fe = substream->private_data;
2495 int stream = substream->stream, ret = 0;
2497 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2499 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2501 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2503 /* there is no point preparing this FE if there are no BEs */
2504 if (list_empty(&fe->dpcm[stream].be_clients)) {
2505 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2506 fe->dai_link->name);
2511 ret = dpcm_be_dai_prepare(fe, substream->stream);
2515 /* call prepare on the frontend */
2516 ret = soc_pcm_prepare(substream);
2518 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2519 fe->dai_link->name);
2523 /* run the stream event for each BE */
2524 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2525 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2528 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2529 mutex_unlock(&fe->card->mutex);
2534 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2535 unsigned int cmd, void *arg)
2537 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2538 struct snd_soc_component *component;
2539 struct snd_soc_rtdcom_list *rtdcom;
2541 for_each_rtdcom(rtd, rtdcom) {
2542 component = rtdcom->component;
2544 if (!component->driver->ops ||
2545 !component->driver->ops->ioctl)
2548 /* FIXME: use 1st ioctl */
2549 return component->driver->ops->ioctl(substream, cmd, arg);
2552 return snd_pcm_lib_ioctl(substream, cmd, arg);
2555 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2557 struct snd_pcm_substream *substream =
2558 snd_soc_dpcm_get_substream(fe, stream);
2559 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2562 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2563 stream ? "capture" : "playback", fe->dai_link->name);
2565 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2566 /* call bespoke trigger - FE takes care of all BE triggers */
2567 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2568 fe->dai_link->name);
2570 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2572 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2574 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2575 fe->dai_link->name);
2577 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2579 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2582 err = dpcm_be_dai_hw_free(fe, stream);
2584 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2586 err = dpcm_be_dai_shutdown(fe, stream);
2588 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2590 /* run the stream event for each BE */
2591 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2596 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2598 struct snd_pcm_substream *substream =
2599 snd_soc_dpcm_get_substream(fe, stream);
2600 struct snd_soc_dpcm *dpcm;
2601 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2603 unsigned long flags;
2605 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2606 stream ? "capture" : "playback", fe->dai_link->name);
2608 /* Only start the BE if the FE is ready */
2609 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2610 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2613 /* startup must always be called for new BEs */
2614 ret = dpcm_be_dai_startup(fe, stream);
2618 /* keep going if FE state is > open */
2619 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2622 ret = dpcm_be_dai_hw_params(fe, stream);
2626 /* keep going if FE state is > hw_params */
2627 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2631 ret = dpcm_be_dai_prepare(fe, stream);
2635 /* run the stream event for each BE */
2636 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2638 /* keep going if FE state is > prepare */
2639 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2640 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2643 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2644 /* call trigger on the frontend - FE takes care of all BE triggers */
2645 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2646 fe->dai_link->name);
2648 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2650 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2654 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2655 fe->dai_link->name);
2657 ret = dpcm_be_dai_trigger(fe, stream,
2658 SNDRV_PCM_TRIGGER_START);
2660 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2668 dpcm_be_dai_hw_free(fe, stream);
2670 dpcm_be_dai_shutdown(fe, stream);
2672 /* disconnect any non started BEs */
2673 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2674 for_each_dpcm_be(fe, stream, dpcm) {
2675 struct snd_soc_pcm_runtime *be = dpcm->be;
2676 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2677 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2679 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2684 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2688 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2689 ret = dpcm_run_update_startup(fe, stream);
2691 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2692 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2697 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2701 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2702 ret = dpcm_run_update_shutdown(fe, stream);
2704 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2705 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2710 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2712 struct snd_soc_dapm_widget_list *list;
2715 if (!fe->dai_link->dynamic)
2718 /* only check active links */
2719 if (!fe->cpu_dai->active)
2722 /* DAPM sync will call this to update DSP paths */
2723 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2724 new ? "new" : "old", fe->dai_link->name);
2726 /* skip if FE doesn't have playback capability */
2727 if (!fe->cpu_dai->driver->playback.channels_min ||
2728 !fe->codec_dai->driver->playback.channels_min)
2731 /* skip if FE isn't currently playing */
2732 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2735 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2737 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2738 fe->dai_link->name, "playback");
2742 /* update any playback paths */
2743 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2746 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2748 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2750 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2751 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2754 dpcm_path_put(&list);
2757 /* skip if FE doesn't have capture capability */
2758 if (!fe->cpu_dai->driver->capture.channels_min ||
2759 !fe->codec_dai->driver->capture.channels_min)
2762 /* skip if FE isn't currently capturing */
2763 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2766 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2768 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2769 fe->dai_link->name, "capture");
2773 /* update any old capture paths */
2774 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2777 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2779 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2781 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2782 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2785 dpcm_path_put(&list);
2790 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2793 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2795 struct snd_soc_pcm_runtime *fe;
2798 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2799 /* shutdown all old paths first */
2800 for_each_card_rtds(card, fe) {
2801 ret = soc_dpcm_fe_runtime_update(fe, 0);
2806 /* bring new paths up */
2807 for_each_card_rtds(card, fe) {
2808 ret = soc_dpcm_fe_runtime_update(fe, 1);
2814 mutex_unlock(&card->mutex);
2817 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2819 struct snd_soc_dpcm *dpcm;
2820 struct snd_soc_dai *dai;
2822 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2824 struct snd_soc_pcm_runtime *be = dpcm->be;
2827 if (be->dai_link->ignore_suspend)
2830 for_each_rtd_codec_dai(be, i, dai) {
2831 struct snd_soc_dai_driver *drv = dai->driver;
2833 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2834 be->dai_link->name);
2836 if (drv->ops && drv->ops->digital_mute &&
2837 dai->playback_active)
2838 drv->ops->digital_mute(dai, mute);
2845 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2847 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2848 struct snd_soc_dpcm *dpcm;
2849 struct snd_soc_dapm_widget_list *list;
2851 int stream = fe_substream->stream;
2853 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2854 fe->dpcm[stream].runtime = fe_substream->runtime;
2856 ret = dpcm_path_get(fe, stream, &list);
2858 mutex_unlock(&fe->card->mutex);
2860 } else if (ret == 0) {
2861 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2862 fe->dai_link->name, stream ? "capture" : "playback");
2865 /* calculate valid and active FE <-> BE dpcms */
2866 dpcm_process_paths(fe, stream, &list, 1);
2868 ret = dpcm_fe_dai_startup(fe_substream);
2870 /* clean up all links */
2871 for_each_dpcm_be(fe, stream, dpcm)
2872 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2874 dpcm_be_disconnect(fe, stream);
2875 fe->dpcm[stream].runtime = NULL;
2878 dpcm_clear_pending_state(fe, stream);
2879 dpcm_path_put(&list);
2880 mutex_unlock(&fe->card->mutex);
2884 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2886 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2887 struct snd_soc_dpcm *dpcm;
2888 int stream = fe_substream->stream, ret;
2890 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2891 ret = dpcm_fe_dai_shutdown(fe_substream);
2893 /* mark FE's links ready to prune */
2894 for_each_dpcm_be(fe, stream, dpcm)
2895 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2897 dpcm_be_disconnect(fe, stream);
2899 fe->dpcm[stream].runtime = NULL;
2900 mutex_unlock(&fe->card->mutex);
2904 static void soc_pcm_private_free(struct snd_pcm *pcm)
2906 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2907 struct snd_soc_rtdcom_list *rtdcom;
2908 struct snd_soc_component *component;
2910 /* need to sync the delayed work before releasing resources */
2911 flush_delayed_work(&rtd->delayed_work);
2912 for_each_rtdcom(rtd, rtdcom) {
2913 component = rtdcom->component;
2915 if (component->driver->pcm_free)
2916 component->driver->pcm_free(pcm);
2920 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2922 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2923 struct snd_soc_rtdcom_list *rtdcom;
2924 struct snd_soc_component *component;
2926 for_each_rtdcom(rtd, rtdcom) {
2927 component = rtdcom->component;
2929 if (!component->driver->ops ||
2930 !component->driver->ops->ack)
2933 /* FIXME. it returns 1st ask now */
2934 return component->driver->ops->ack(substream);
2940 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2941 unsigned long pos, void __user *buf,
2942 unsigned long bytes)
2944 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2945 struct snd_soc_rtdcom_list *rtdcom;
2946 struct snd_soc_component *component;
2948 for_each_rtdcom(rtd, rtdcom) {
2949 component = rtdcom->component;
2951 if (!component->driver->ops ||
2952 !component->driver->ops->copy_user)
2955 /* FIXME. it returns 1st copy now */
2956 return component->driver->ops->copy_user(substream, channel,
2963 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2964 unsigned long pos, void *buf, unsigned long bytes)
2966 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2967 struct snd_soc_rtdcom_list *rtdcom;
2968 struct snd_soc_component *component;
2970 for_each_rtdcom(rtd, rtdcom) {
2971 component = rtdcom->component;
2973 if (!component->driver->ops ||
2974 !component->driver->ops->copy_kernel)
2977 /* FIXME. it returns 1st copy now */
2978 return component->driver->ops->copy_kernel(substream, channel,
2985 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2986 unsigned long pos, unsigned long bytes)
2988 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2989 struct snd_soc_rtdcom_list *rtdcom;
2990 struct snd_soc_component *component;
2992 for_each_rtdcom(rtd, rtdcom) {
2993 component = rtdcom->component;
2995 if (!component->driver->ops ||
2996 !component->driver->ops->fill_silence)
2999 /* FIXME. it returns 1st silence now */
3000 return component->driver->ops->fill_silence(substream, channel,
3007 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3008 unsigned long offset)
3010 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3011 struct snd_soc_rtdcom_list *rtdcom;
3012 struct snd_soc_component *component;
3015 for_each_rtdcom(rtd, rtdcom) {
3016 component = rtdcom->component;
3018 if (!component->driver->ops ||
3019 !component->driver->ops->page)
3022 /* FIXME. it returns 1st page now */
3023 page = component->driver->ops->page(substream, offset);
3031 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3032 struct vm_area_struct *vma)
3034 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3035 struct snd_soc_rtdcom_list *rtdcom;
3036 struct snd_soc_component *component;
3038 for_each_rtdcom(rtd, rtdcom) {
3039 component = rtdcom->component;
3041 if (!component->driver->ops ||
3042 !component->driver->ops->mmap)
3045 /* FIXME. it returns 1st mmap now */
3046 return component->driver->ops->mmap(substream, vma);
3052 /* create a new pcm */
3053 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3055 struct snd_soc_dai *codec_dai;
3056 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3057 struct snd_soc_component *component;
3058 struct snd_soc_rtdcom_list *rtdcom;
3059 struct snd_pcm *pcm;
3061 int ret = 0, playback = 0, capture = 0;
3064 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3065 playback = rtd->dai_link->dpcm_playback;
3066 capture = rtd->dai_link->dpcm_capture;
3068 for_each_rtd_codec_dai(rtd, i, codec_dai) {
3069 if (codec_dai->driver->playback.channels_min)
3071 if (codec_dai->driver->capture.channels_min)
3075 capture = capture && cpu_dai->driver->capture.channels_min;
3076 playback = playback && cpu_dai->driver->playback.channels_min;
3079 if (rtd->dai_link->playback_only) {
3084 if (rtd->dai_link->capture_only) {
3089 /* create the PCM */
3090 if (rtd->dai_link->no_pcm) {
3091 snprintf(new_name, sizeof(new_name), "(%s)",
3092 rtd->dai_link->stream_name);
3094 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3095 playback, capture, &pcm);
3097 if (rtd->dai_link->dynamic)
3098 snprintf(new_name, sizeof(new_name), "%s (*)",
3099 rtd->dai_link->stream_name);
3101 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3102 rtd->dai_link->stream_name,
3103 (rtd->num_codecs > 1) ?
3104 "multicodec" : rtd->codec_dai->name, num);
3106 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3110 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3111 rtd->dai_link->name);
3114 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3116 /* DAPM dai link stream work */
3117 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3119 pcm->nonatomic = rtd->dai_link->nonatomic;
3121 pcm->private_data = rtd;
3123 if (rtd->dai_link->no_pcm) {
3125 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3127 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3131 /* ASoC PCM operations */
3132 if (rtd->dai_link->dynamic) {
3133 rtd->ops.open = dpcm_fe_dai_open;
3134 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3135 rtd->ops.prepare = dpcm_fe_dai_prepare;
3136 rtd->ops.trigger = dpcm_fe_dai_trigger;
3137 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3138 rtd->ops.close = dpcm_fe_dai_close;
3139 rtd->ops.pointer = soc_pcm_pointer;
3140 rtd->ops.ioctl = soc_pcm_ioctl;
3142 rtd->ops.open = soc_pcm_open;
3143 rtd->ops.hw_params = soc_pcm_hw_params;
3144 rtd->ops.prepare = soc_pcm_prepare;
3145 rtd->ops.trigger = soc_pcm_trigger;
3146 rtd->ops.hw_free = soc_pcm_hw_free;
3147 rtd->ops.close = soc_pcm_close;
3148 rtd->ops.pointer = soc_pcm_pointer;
3149 rtd->ops.ioctl = soc_pcm_ioctl;
3152 for_each_rtdcom(rtd, rtdcom) {
3153 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3159 rtd->ops.ack = soc_rtdcom_ack;
3161 rtd->ops.copy_user = soc_rtdcom_copy_user;
3162 if (ops->copy_kernel)
3163 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3164 if (ops->fill_silence)
3165 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3167 rtd->ops.page = soc_rtdcom_page;
3169 rtd->ops.mmap = soc_rtdcom_mmap;
3173 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3176 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3178 for_each_rtdcom(rtd, rtdcom) {
3179 component = rtdcom->component;
3181 if (!component->driver->pcm_new)
3184 ret = component->driver->pcm_new(rtd);
3186 dev_err(component->dev,
3187 "ASoC: pcm constructor failed: %d\n",
3193 pcm->private_free = soc_pcm_private_free;
3194 pcm->no_device_suspend = true;
3196 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3197 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3202 /* is the current PCM operation for this FE ? */
3203 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3205 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3209 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3211 /* is the current PCM operation for this BE ? */
3212 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3213 struct snd_soc_pcm_runtime *be, int stream)
3215 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3216 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3217 be->dpcm[stream].runtime_update))
3221 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3223 /* get the substream for this BE */
3224 struct snd_pcm_substream *
3225 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3227 return be->pcm->streams[stream].substream;
3229 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3231 /* get the BE runtime state */
3232 enum snd_soc_dpcm_state
3233 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3235 return be->dpcm[stream].state;
3237 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3239 /* set the BE runtime state */
3240 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3241 int stream, enum snd_soc_dpcm_state state)
3243 be->dpcm[stream].state = state;
3245 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3248 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3249 * are not running, paused or suspended for the specified stream direction.
3251 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3252 struct snd_soc_pcm_runtime *be, int stream)
3254 struct snd_soc_dpcm *dpcm;
3257 unsigned long flags;
3259 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3260 for_each_dpcm_fe(be, stream, dpcm) {
3265 state = dpcm->fe->dpcm[stream].state;
3266 if (state == SND_SOC_DPCM_STATE_START ||
3267 state == SND_SOC_DPCM_STATE_PAUSED ||
3268 state == SND_SOC_DPCM_STATE_SUSPEND) {
3273 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3275 /* it's safe to free/stop this BE DAI */
3278 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3281 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3282 * running, paused or suspended for the specified stream direction.
3284 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3285 struct snd_soc_pcm_runtime *be, int stream)
3287 struct snd_soc_dpcm *dpcm;
3290 unsigned long flags;
3292 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3293 for_each_dpcm_fe(be, stream, dpcm) {
3298 state = dpcm->fe->dpcm[stream].state;
3299 if (state == SND_SOC_DPCM_STATE_START ||
3300 state == SND_SOC_DPCM_STATE_PAUSED ||
3301 state == SND_SOC_DPCM_STATE_SUSPEND ||
3302 state == SND_SOC_DPCM_STATE_PREPARE) {
3307 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3309 /* it's safe to change hw_params */
3312 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3314 #ifdef CONFIG_DEBUG_FS
3315 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3318 case SND_SOC_DPCM_STATE_NEW:
3320 case SND_SOC_DPCM_STATE_OPEN:
3322 case SND_SOC_DPCM_STATE_HW_PARAMS:
3324 case SND_SOC_DPCM_STATE_PREPARE:
3326 case SND_SOC_DPCM_STATE_START:
3328 case SND_SOC_DPCM_STATE_STOP:
3330 case SND_SOC_DPCM_STATE_SUSPEND:
3332 case SND_SOC_DPCM_STATE_PAUSED:
3334 case SND_SOC_DPCM_STATE_HW_FREE:
3336 case SND_SOC_DPCM_STATE_CLOSE:
3343 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3344 int stream, char *buf, size_t size)
3346 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3347 struct snd_soc_dpcm *dpcm;
3349 unsigned long flags;
3352 offset += snprintf(buf + offset, size - offset,
3353 "[%s - %s]\n", fe->dai_link->name,
3354 stream ? "Capture" : "Playback");
3356 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3357 dpcm_state_string(fe->dpcm[stream].state));
3359 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3360 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3361 offset += snprintf(buf + offset, size - offset,
3363 "Format = %s, Channels = %d, Rate = %d\n",
3364 snd_pcm_format_name(params_format(params)),
3365 params_channels(params),
3366 params_rate(params));
3369 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3371 if (list_empty(&fe->dpcm[stream].be_clients)) {
3372 offset += snprintf(buf + offset, size - offset,
3373 " No active DSP links\n");
3377 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3378 for_each_dpcm_be(fe, stream, dpcm) {
3379 struct snd_soc_pcm_runtime *be = dpcm->be;
3380 params = &dpcm->hw_params;
3382 offset += snprintf(buf + offset, size - offset,
3383 "- %s\n", be->dai_link->name);
3385 offset += snprintf(buf + offset, size - offset,
3387 dpcm_state_string(be->dpcm[stream].state));
3389 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3390 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3391 offset += snprintf(buf + offset, size - offset,
3392 " Hardware Params: "
3393 "Format = %s, Channels = %d, Rate = %d\n",
3394 snd_pcm_format_name(params_format(params)),
3395 params_channels(params),
3396 params_rate(params));
3398 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3403 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3404 size_t count, loff_t *ppos)
3406 struct snd_soc_pcm_runtime *fe = file->private_data;
3407 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3410 buf = kmalloc(out_count, GFP_KERNEL);
3414 if (fe->cpu_dai->driver->playback.channels_min)
3415 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3416 buf + offset, out_count - offset);
3418 if (fe->cpu_dai->driver->capture.channels_min)
3419 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3420 buf + offset, out_count - offset);
3422 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3428 static const struct file_operations dpcm_state_fops = {
3429 .open = simple_open,
3430 .read = dpcm_state_read_file,
3431 .llseek = default_llseek,
3434 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3439 if (!rtd->card->debugfs_card_root)
3442 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3443 rtd->card->debugfs_card_root);
3444 if (!rtd->debugfs_dpcm_root) {
3446 "ASoC: Failed to create dpcm debugfs directory %s\n",
3447 rtd->dai_link->name);
3451 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3452 rtd, &dpcm_state_fops);