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 if (cpu_dai->driver->ops->startup) {
539 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
541 dev_err(cpu_dai->dev, "ASoC: can't open interface"
542 " %s: %d\n", cpu_dai->name, ret);
547 ret = soc_pcm_components_open(substream, &component);
551 for_each_rtd_codec_dai(rtd, i, codec_dai) {
552 if (codec_dai->driver->ops->startup) {
553 ret = codec_dai->driver->ops->startup(substream,
556 dev_err(codec_dai->dev,
557 "ASoC: can't open codec %s: %d\n",
558 codec_dai->name, ret);
563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
564 codec_dai->tx_mask = 0;
566 codec_dai->rx_mask = 0;
569 if (rtd->dai_link->ops->startup) {
570 ret = rtd->dai_link->ops->startup(substream);
572 pr_err("ASoC: %s startup failed: %d\n",
573 rtd->dai_link->name, ret);
578 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
579 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
582 /* Check that the codec and cpu DAIs are compatible */
583 soc_pcm_init_runtime_hw(substream);
585 if (rtd->num_codecs == 1)
586 codec_dai_name = rtd->codec_dai->name;
588 if (soc_pcm_has_symmetry(substream))
589 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
592 if (!runtime->hw.rates) {
593 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
594 codec_dai_name, cpu_dai->name);
597 if (!runtime->hw.formats) {
598 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
599 codec_dai_name, cpu_dai->name);
602 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
603 runtime->hw.channels_min > runtime->hw.channels_max) {
604 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
605 codec_dai_name, cpu_dai->name);
609 soc_pcm_apply_msb(substream);
611 /* Symmetry only applies if we've already got an active stream. */
612 if (cpu_dai->active) {
613 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
618 for_each_rtd_codec_dai(rtd, i, codec_dai) {
619 if (codec_dai->active) {
620 ret = soc_pcm_apply_symmetry(substream, codec_dai);
626 pr_debug("ASoC: %s <-> %s info:\n",
627 codec_dai_name, cpu_dai->name);
628 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
629 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
630 runtime->hw.channels_max);
631 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
632 runtime->hw.rate_max);
636 snd_soc_runtime_activate(rtd, substream->stream);
638 mutex_unlock(&rtd->pcm_mutex);
642 if (rtd->dai_link->ops->shutdown)
643 rtd->dai_link->ops->shutdown(substream);
649 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
650 if (codec_dai->driver->ops->shutdown)
651 codec_dai->driver->ops->shutdown(substream, codec_dai);
655 soc_pcm_components_close(substream, component);
657 if (cpu_dai->driver->ops->shutdown)
658 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
660 mutex_unlock(&rtd->pcm_mutex);
662 for_each_rtdcom(rtd, rtdcom) {
663 component = rtdcom->component;
665 pm_runtime_mark_last_busy(component->dev);
666 pm_runtime_put_autosuspend(component->dev);
669 for_each_rtd_codec_dai(rtd, i, codec_dai) {
670 if (!codec_dai->active)
671 pinctrl_pm_select_sleep_state(codec_dai->dev);
673 if (!cpu_dai->active)
674 pinctrl_pm_select_sleep_state(cpu_dai->dev);
680 * Power down the audio subsystem pmdown_time msecs after close is called.
681 * This is to ensure there are no pops or clicks in between any music tracks
682 * due to DAPM power cycling.
684 static void close_delayed_work(struct work_struct *work)
686 struct snd_soc_pcm_runtime *rtd =
687 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
688 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
690 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
692 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
693 codec_dai->driver->playback.stream_name,
694 codec_dai->playback_active ? "active" : "inactive",
695 rtd->pop_wait ? "yes" : "no");
697 /* are we waiting on this codec DAI stream */
698 if (rtd->pop_wait == 1) {
700 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
701 SND_SOC_DAPM_STREAM_STOP);
704 mutex_unlock(&rtd->pcm_mutex);
708 * Called by ALSA when a PCM substream is closed. Private data can be
709 * freed here. The cpu DAI, codec DAI, machine and components are also
712 static int soc_pcm_close(struct snd_pcm_substream *substream)
714 struct snd_soc_pcm_runtime *rtd = substream->private_data;
715 struct snd_soc_component *component;
716 struct snd_soc_rtdcom_list *rtdcom;
717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
718 struct snd_soc_dai *codec_dai;
721 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
723 snd_soc_runtime_deactivate(rtd, substream->stream);
725 /* clear the corresponding DAIs rate when inactive */
726 if (!cpu_dai->active)
729 for_each_rtd_codec_dai(rtd, i, codec_dai) {
730 if (!codec_dai->active)
734 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
736 if (cpu_dai->driver->ops->shutdown)
737 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
739 for_each_rtd_codec_dai(rtd, i, codec_dai) {
740 if (codec_dai->driver->ops->shutdown)
741 codec_dai->driver->ops->shutdown(substream, codec_dai);
744 if (rtd->dai_link->ops->shutdown)
745 rtd->dai_link->ops->shutdown(substream);
747 soc_pcm_components_close(substream, NULL);
749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
750 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
751 /* powered down playback stream now */
752 snd_soc_dapm_stream_event(rtd,
753 SNDRV_PCM_STREAM_PLAYBACK,
754 SND_SOC_DAPM_STREAM_STOP);
756 /* start delayed pop wq here for playback streams */
758 queue_delayed_work(system_power_efficient_wq,
760 msecs_to_jiffies(rtd->pmdown_time));
763 /* capture streams can be powered down now */
764 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
765 SND_SOC_DAPM_STREAM_STOP);
768 mutex_unlock(&rtd->pcm_mutex);
770 for_each_rtdcom(rtd, rtdcom) {
771 component = rtdcom->component;
773 pm_runtime_mark_last_busy(component->dev);
774 pm_runtime_put_autosuspend(component->dev);
777 for_each_rtd_codec_dai(rtd, i, codec_dai) {
778 if (!codec_dai->active)
779 pinctrl_pm_select_sleep_state(codec_dai->dev);
781 if (!cpu_dai->active)
782 pinctrl_pm_select_sleep_state(cpu_dai->dev);
788 * Called by ALSA when the PCM substream is prepared, can set format, sample
789 * rate, etc. This function is non atomic and can be called multiple times,
790 * it can refer to the runtime info.
792 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
794 struct snd_soc_pcm_runtime *rtd = substream->private_data;
795 struct snd_soc_component *component;
796 struct snd_soc_rtdcom_list *rtdcom;
797 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
798 struct snd_soc_dai *codec_dai;
801 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
803 if (rtd->dai_link->ops->prepare) {
804 ret = rtd->dai_link->ops->prepare(substream);
806 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
812 for_each_rtdcom(rtd, rtdcom) {
813 component = rtdcom->component;
815 if (!component->driver->ops ||
816 !component->driver->ops->prepare)
819 ret = component->driver->ops->prepare(substream);
821 dev_err(component->dev,
822 "ASoC: platform prepare error: %d\n", ret);
827 for_each_rtd_codec_dai(rtd, i, codec_dai) {
828 if (codec_dai->driver->ops->prepare) {
829 ret = codec_dai->driver->ops->prepare(substream,
832 dev_err(codec_dai->dev,
833 "ASoC: codec DAI prepare error: %d\n",
840 if (cpu_dai->driver->ops->prepare) {
841 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
843 dev_err(cpu_dai->dev,
844 "ASoC: cpu DAI prepare error: %d\n", ret);
849 /* cancel any delayed stream shutdown that is pending */
850 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
853 cancel_delayed_work(&rtd->delayed_work);
856 snd_soc_dapm_stream_event(rtd, substream->stream,
857 SND_SOC_DAPM_STREAM_START);
859 for_each_rtd_codec_dai(rtd, i, codec_dai)
860 snd_soc_dai_digital_mute(codec_dai, 0,
862 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
865 mutex_unlock(&rtd->pcm_mutex);
869 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
872 struct snd_interval *interval;
873 int channels = hweight_long(mask);
875 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
876 interval->min = channels;
877 interval->max = channels;
880 int soc_dai_hw_params(struct snd_pcm_substream *substream,
881 struct snd_pcm_hw_params *params,
882 struct snd_soc_dai *dai)
884 struct snd_soc_pcm_runtime *rtd = substream->private_data;
887 /* perform any topology hw_params fixups before DAI */
888 if (rtd->dai_link->be_hw_params_fixup) {
889 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
892 "ASoC: hw_params topology fixup failed %d\n",
898 if (dai->driver->ops->hw_params) {
899 ret = dai->driver->ops->hw_params(substream, params, dai);
901 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
910 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
911 struct snd_soc_component *last)
913 struct snd_soc_pcm_runtime *rtd = substream->private_data;
914 struct snd_soc_rtdcom_list *rtdcom;
915 struct snd_soc_component *component;
917 for_each_rtdcom(rtd, rtdcom) {
918 component = rtdcom->component;
920 if (component == last)
923 if (!component->driver->ops ||
924 !component->driver->ops->hw_free)
927 component->driver->ops->hw_free(substream);
934 * Called by ALSA when the hardware params are set by application. This
935 * function can also be called multiple times and can allocate buffers
936 * (using snd_pcm_lib_* ). It's non-atomic.
938 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
939 struct snd_pcm_hw_params *params)
941 struct snd_soc_pcm_runtime *rtd = substream->private_data;
942 struct snd_soc_component *component;
943 struct snd_soc_rtdcom_list *rtdcom;
944 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
945 struct snd_soc_dai *codec_dai;
948 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
949 if (rtd->dai_link->ops->hw_params) {
950 ret = rtd->dai_link->ops->hw_params(substream, params);
952 dev_err(rtd->card->dev, "ASoC: machine hw_params"
953 " failed: %d\n", ret);
958 for_each_rtd_codec_dai(rtd, i, codec_dai) {
959 struct snd_pcm_hw_params codec_params;
962 * Skip CODECs which don't support the current stream type,
963 * the idea being that if a CODEC is not used for the currently
964 * set up transfer direction, it should not need to be
965 * configured, especially since the configuration used might
966 * not even be supported by that CODEC. There may be cases
967 * however where a CODEC needs to be set up although it is
968 * actually not being used for the transfer, e.g. if a
969 * capture-only CODEC is acting as an LRCLK and/or BCLK master
970 * for the DAI link including a playback-only CODEC.
971 * If this becomes necessary, we will have to augment the
972 * machine driver setup with information on how to act, so
973 * we can do the right thing here.
975 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
978 /* copy params for each codec */
979 codec_params = *params;
981 /* fixup params based on TDM slot masks */
982 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
984 soc_pcm_codec_params_fixup(&codec_params,
987 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
989 soc_pcm_codec_params_fixup(&codec_params,
992 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
996 codec_dai->rate = params_rate(&codec_params);
997 codec_dai->channels = params_channels(&codec_params);
998 codec_dai->sample_bits = snd_pcm_format_physical_width(
999 params_format(&codec_params));
1001 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1004 ret = soc_dai_hw_params(substream, params, cpu_dai);
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 for_each_rtdcom(rtd, rtdcom) {
1017 component = rtdcom->component;
1019 if (!component->driver->ops ||
1020 !component->driver->ops->hw_params)
1023 ret = component->driver->ops->hw_params(substream, params);
1025 dev_err(component->dev,
1026 "ASoC: %s hw params failed: %d\n",
1027 component->name, ret);
1033 ret = soc_pcm_params_symmetry(substream, params);
1037 mutex_unlock(&rtd->pcm_mutex);
1041 soc_pcm_components_hw_free(substream, component);
1043 if (cpu_dai->driver->ops->hw_free)
1044 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1048 i = rtd->num_codecs;
1051 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1052 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1055 if (codec_dai->driver->ops->hw_free)
1056 codec_dai->driver->ops->hw_free(substream, codec_dai);
1057 codec_dai->rate = 0;
1060 if (rtd->dai_link->ops->hw_free)
1061 rtd->dai_link->ops->hw_free(substream);
1063 mutex_unlock(&rtd->pcm_mutex);
1068 * Frees resources allocated by hw_params, can be called multiple times
1070 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1072 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1073 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1074 struct snd_soc_dai *codec_dai;
1075 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1078 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1080 /* clear the corresponding DAIs parameters when going to be inactive */
1081 if (cpu_dai->active == 1) {
1083 cpu_dai->channels = 0;
1084 cpu_dai->sample_bits = 0;
1087 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1088 if (codec_dai->active == 1) {
1089 codec_dai->rate = 0;
1090 codec_dai->channels = 0;
1091 codec_dai->sample_bits = 0;
1095 /* apply codec digital mute */
1096 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1097 if ((playback && codec_dai->playback_active == 1) ||
1098 (!playback && codec_dai->capture_active == 1))
1099 snd_soc_dai_digital_mute(codec_dai, 1,
1103 /* free any machine hw params */
1104 if (rtd->dai_link->ops->hw_free)
1105 rtd->dai_link->ops->hw_free(substream);
1107 /* free any component resources */
1108 soc_pcm_components_hw_free(substream, NULL);
1110 /* now free hw params for the DAIs */
1111 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1112 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1115 if (codec_dai->driver->ops->hw_free)
1116 codec_dai->driver->ops->hw_free(substream, codec_dai);
1119 if (cpu_dai->driver->ops->hw_free)
1120 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1122 mutex_unlock(&rtd->pcm_mutex);
1126 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1129 struct snd_soc_component *component;
1130 struct snd_soc_rtdcom_list *rtdcom;
1131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1132 struct snd_soc_dai *codec_dai;
1135 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1136 if (codec_dai->driver->ops->trigger) {
1137 ret = codec_dai->driver->ops->trigger(substream,
1144 for_each_rtdcom(rtd, rtdcom) {
1145 component = rtdcom->component;
1147 if (!component->driver->ops ||
1148 !component->driver->ops->trigger)
1151 ret = component->driver->ops->trigger(substream, cmd);
1156 if (cpu_dai->driver->ops->trigger) {
1157 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1162 if (rtd->dai_link->ops->trigger) {
1163 ret = rtd->dai_link->ops->trigger(substream, cmd);
1171 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1174 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1175 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1176 struct snd_soc_dai *codec_dai;
1179 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1180 if (codec_dai->driver->ops->bespoke_trigger) {
1181 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1188 if (cpu_dai->driver->ops->bespoke_trigger) {
1189 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1196 * soc level wrapper for pointer callback
1197 * If cpu_dai, codec_dai, component driver has the delay callback, then
1198 * the runtime->delay will be updated accordingly.
1200 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1202 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1203 struct snd_soc_component *component;
1204 struct snd_soc_rtdcom_list *rtdcom;
1205 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1206 struct snd_soc_dai *codec_dai;
1207 struct snd_pcm_runtime *runtime = substream->runtime;
1208 snd_pcm_uframes_t offset = 0;
1209 snd_pcm_sframes_t delay = 0;
1210 snd_pcm_sframes_t codec_delay = 0;
1213 /* clearing the previous total delay */
1216 for_each_rtdcom(rtd, rtdcom) {
1217 component = rtdcom->component;
1219 if (!component->driver->ops ||
1220 !component->driver->ops->pointer)
1223 /* FIXME: use 1st pointer */
1224 offset = component->driver->ops->pointer(substream);
1227 /* base delay if assigned in pointer callback */
1228 delay = runtime->delay;
1230 if (cpu_dai->driver->ops->delay)
1231 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1233 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1234 if (codec_dai->driver->ops->delay)
1235 codec_delay = max(codec_delay,
1236 codec_dai->driver->ops->delay(substream,
1239 delay += codec_delay;
1241 runtime->delay = delay;
1246 /* connect a FE and BE */
1247 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1248 struct snd_soc_pcm_runtime *be, int stream)
1250 struct snd_soc_dpcm *dpcm;
1251 unsigned long flags;
1253 /* only add new dpcms */
1254 for_each_dpcm_be(fe, stream, dpcm) {
1255 if (dpcm->be == be && dpcm->fe == fe)
1259 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1265 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1266 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1267 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1268 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1269 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1270 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1272 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1273 stream ? "capture" : "playback", fe->dai_link->name,
1274 stream ? "<-" : "->", be->dai_link->name);
1276 #ifdef CONFIG_DEBUG_FS
1277 if (fe->debugfs_dpcm_root)
1278 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1279 fe->debugfs_dpcm_root, &dpcm->state);
1284 /* reparent a BE onto another FE */
1285 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1286 struct snd_soc_pcm_runtime *be, int stream)
1288 struct snd_soc_dpcm *dpcm;
1289 struct snd_pcm_substream *fe_substream, *be_substream;
1291 /* reparent if BE is connected to other FEs */
1292 if (!be->dpcm[stream].users)
1295 be_substream = snd_soc_dpcm_get_substream(be, stream);
1297 for_each_dpcm_fe(be, stream, dpcm) {
1301 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1302 stream ? "capture" : "playback",
1303 dpcm->fe->dai_link->name,
1304 stream ? "<-" : "->", dpcm->be->dai_link->name);
1306 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1307 be_substream->runtime = fe_substream->runtime;
1312 /* disconnect a BE and FE */
1313 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1315 struct snd_soc_dpcm *dpcm, *d;
1316 unsigned long flags;
1318 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1319 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1320 stream ? "capture" : "playback",
1321 dpcm->be->dai_link->name);
1323 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1326 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1327 stream ? "capture" : "playback", fe->dai_link->name,
1328 stream ? "<-" : "->", dpcm->be->dai_link->name);
1330 /* BEs still alive need new FE */
1331 dpcm_be_reparent(fe, dpcm->be, stream);
1333 #ifdef CONFIG_DEBUG_FS
1334 debugfs_remove(dpcm->debugfs_state);
1336 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1337 list_del(&dpcm->list_be);
1338 list_del(&dpcm->list_fe);
1339 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1344 /* get BE for DAI widget and stream */
1345 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1346 struct snd_soc_dapm_widget *widget, int stream)
1348 struct snd_soc_pcm_runtime *be;
1349 struct snd_soc_dai *dai;
1352 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1354 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1355 for_each_card_rtds(card, be) {
1357 if (!be->dai_link->no_pcm)
1360 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1361 be->cpu_dai->playback_widget ?
1362 be->cpu_dai->playback_widget->name : "(not set)");
1364 if (be->cpu_dai->playback_widget == widget)
1367 for_each_rtd_codec_dai(be, i, dai) {
1368 if (dai->playback_widget == widget)
1374 for_each_card_rtds(card, be) {
1376 if (!be->dai_link->no_pcm)
1379 dev_dbg(card->dev, "ASoC: try BE %s\n",
1380 be->cpu_dai->capture_widget ?
1381 be->cpu_dai->capture_widget->name : "(not set)");
1383 if (be->cpu_dai->capture_widget == widget)
1386 for_each_rtd_codec_dai(be, i, dai) {
1387 if (dai->capture_widget == widget)
1393 /* dai link name and stream name set correctly ? */
1394 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1395 stream ? "capture" : "playback", widget->name);
1399 static inline struct snd_soc_dapm_widget *
1400 dai_get_widget(struct snd_soc_dai *dai, int stream)
1402 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1403 return dai->playback_widget;
1405 return dai->capture_widget;
1408 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1409 struct snd_soc_dapm_widget *widget)
1413 for (i = 0; i < list->num_widgets; i++) {
1414 if (widget == list->widgets[i])
1421 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1422 enum snd_soc_dapm_direction dir)
1424 struct snd_soc_card *card = widget->dapm->card;
1425 struct snd_soc_pcm_runtime *rtd;
1426 struct snd_soc_dai *dai;
1429 if (dir == SND_SOC_DAPM_DIR_OUT) {
1430 for_each_card_rtds(card, rtd) {
1431 if (!rtd->dai_link->no_pcm)
1434 if (rtd->cpu_dai->playback_widget == widget)
1437 for_each_rtd_codec_dai(rtd, i, dai) {
1438 if (dai->playback_widget == widget)
1442 } else { /* SND_SOC_DAPM_DIR_IN */
1443 for_each_card_rtds(card, rtd) {
1444 if (!rtd->dai_link->no_pcm)
1447 if (rtd->cpu_dai->capture_widget == widget)
1450 for_each_rtd_codec_dai(rtd, i, dai) {
1451 if (dai->capture_widget == widget)
1460 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1461 int stream, struct snd_soc_dapm_widget_list **list)
1463 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1466 /* get number of valid DAI paths and their widgets */
1467 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1468 dpcm_end_walk_at_be);
1470 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1471 stream ? "capture" : "playback");
1476 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1477 struct snd_soc_dapm_widget_list **list_)
1479 struct snd_soc_dpcm *dpcm;
1480 struct snd_soc_dapm_widget_list *list = *list_;
1481 struct snd_soc_dapm_widget *widget;
1482 struct snd_soc_dai *dai;
1485 /* Destroy any old FE <--> BE connections */
1486 for_each_dpcm_be(fe, stream, dpcm) {
1489 /* is there a valid CPU DAI widget for this BE */
1490 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1492 /* prune the BE if it's no longer in our active list */
1493 if (widget && widget_in_list(list, widget))
1496 /* is there a valid CODEC DAI widget for this BE */
1497 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1498 widget = dai_get_widget(dai, stream);
1500 /* prune the BE if it's no longer in our active list */
1501 if (widget && widget_in_list(list, widget))
1505 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1506 stream ? "capture" : "playback",
1507 dpcm->be->dai_link->name, fe->dai_link->name);
1508 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1509 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1513 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1517 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1518 struct snd_soc_dapm_widget_list **list_)
1520 struct snd_soc_card *card = fe->card;
1521 struct snd_soc_dapm_widget_list *list = *list_;
1522 struct snd_soc_pcm_runtime *be;
1523 int i, new = 0, err;
1525 /* Create any new FE <--> BE connections */
1526 for (i = 0; i < list->num_widgets; i++) {
1528 switch (list->widgets[i]->id) {
1529 case snd_soc_dapm_dai_in:
1530 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1533 case snd_soc_dapm_dai_out:
1534 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1541 /* is there a valid BE rtd for this widget */
1542 be = dpcm_get_be(card, list->widgets[i], stream);
1544 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1545 list->widgets[i]->name);
1549 /* make sure BE is a real BE */
1550 if (!be->dai_link->no_pcm)
1553 /* don't connect if FE is not running */
1554 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1557 /* newly connected FE and BE */
1558 err = dpcm_be_connect(fe, be, stream);
1560 dev_err(fe->dev, "ASoC: can't connect %s\n",
1561 list->widgets[i]->name);
1563 } else if (err == 0) /* already connected */
1567 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1571 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1576 * Find the corresponding BE DAIs that source or sink audio to this
1579 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1580 int stream, struct snd_soc_dapm_widget_list **list, int new)
1583 return dpcm_add_paths(fe, stream, list);
1585 return dpcm_prune_paths(fe, stream, list);
1588 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1590 struct snd_soc_dpcm *dpcm;
1591 unsigned long flags;
1593 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1594 for_each_dpcm_be(fe, stream, dpcm)
1595 dpcm->be->dpcm[stream].runtime_update =
1596 SND_SOC_DPCM_UPDATE_NO;
1597 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1600 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1603 struct snd_soc_dpcm *dpcm;
1605 /* disable any enabled and non active backends */
1606 for_each_dpcm_be(fe, stream, dpcm) {
1608 struct snd_soc_pcm_runtime *be = dpcm->be;
1609 struct snd_pcm_substream *be_substream =
1610 snd_soc_dpcm_get_substream(be, stream);
1612 if (be->dpcm[stream].users == 0)
1613 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1614 stream ? "capture" : "playback",
1615 be->dpcm[stream].state);
1617 if (--be->dpcm[stream].users != 0)
1620 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1623 soc_pcm_close(be_substream);
1624 be_substream->runtime = NULL;
1625 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1629 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1631 struct snd_soc_dpcm *dpcm;
1634 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1635 for_each_dpcm_be(fe, stream, dpcm) {
1637 struct snd_soc_pcm_runtime *be = dpcm->be;
1638 struct snd_pcm_substream *be_substream =
1639 snd_soc_dpcm_get_substream(be, stream);
1641 if (!be_substream) {
1642 dev_err(be->dev, "ASoC: no backend %s stream\n",
1643 stream ? "capture" : "playback");
1647 /* is this op for this BE ? */
1648 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1651 /* first time the dpcm is open ? */
1652 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1653 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1654 stream ? "capture" : "playback",
1655 be->dpcm[stream].state);
1657 if (be->dpcm[stream].users++ != 0)
1660 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1661 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1664 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1665 stream ? "capture" : "playback", be->dai_link->name);
1667 be_substream->runtime = be->dpcm[stream].runtime;
1668 err = soc_pcm_open(be_substream);
1670 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1671 be->dpcm[stream].users--;
1672 if (be->dpcm[stream].users < 0)
1673 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1674 stream ? "capture" : "playback",
1675 be->dpcm[stream].state);
1677 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1681 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1688 /* disable any enabled and non active backends */
1689 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1690 struct snd_soc_pcm_runtime *be = dpcm->be;
1691 struct snd_pcm_substream *be_substream =
1692 snd_soc_dpcm_get_substream(be, stream);
1694 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1697 if (be->dpcm[stream].users == 0)
1698 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1699 stream ? "capture" : "playback",
1700 be->dpcm[stream].state);
1702 if (--be->dpcm[stream].users != 0)
1705 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1708 soc_pcm_close(be_substream);
1709 be_substream->runtime = NULL;
1710 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1716 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1717 struct snd_soc_pcm_stream *stream)
1719 runtime->hw.rate_min = stream->rate_min;
1720 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1721 runtime->hw.channels_min = stream->channels_min;
1722 runtime->hw.channels_max = stream->channels_max;
1723 if (runtime->hw.formats)
1724 runtime->hw.formats &= stream->formats;
1726 runtime->hw.formats = stream->formats;
1727 runtime->hw.rates = stream->rates;
1730 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1733 struct snd_soc_pcm_runtime *fe = substream->private_data;
1734 struct snd_soc_dpcm *dpcm;
1735 struct snd_soc_dai *dai;
1736 int stream = substream->stream;
1738 if (!fe->dai_link->dpcm_merged_format)
1742 * It returns merged BE codec format
1743 * if FE want to use it (= dpcm_merged_format)
1746 for_each_dpcm_be(fe, stream, dpcm) {
1747 struct snd_soc_pcm_runtime *be = dpcm->be;
1748 struct snd_soc_dai_driver *codec_dai_drv;
1749 struct snd_soc_pcm_stream *codec_stream;
1752 for_each_rtd_codec_dai(be, i, dai) {
1754 * Skip CODECs which don't support the current stream
1755 * type. See soc_pcm_init_runtime_hw() for more details
1757 if (!snd_soc_dai_stream_valid(dai, stream))
1760 codec_dai_drv = dai->driver;
1761 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1762 codec_stream = &codec_dai_drv->playback;
1764 codec_stream = &codec_dai_drv->capture;
1766 *formats &= codec_stream->formats;
1771 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1772 unsigned int *channels_min,
1773 unsigned int *channels_max)
1775 struct snd_soc_pcm_runtime *fe = substream->private_data;
1776 struct snd_soc_dpcm *dpcm;
1777 int stream = substream->stream;
1779 if (!fe->dai_link->dpcm_merged_chan)
1783 * It returns merged BE codec channel;
1784 * if FE want to use it (= dpcm_merged_chan)
1787 for_each_dpcm_be(fe, stream, dpcm) {
1788 struct snd_soc_pcm_runtime *be = dpcm->be;
1789 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1790 struct snd_soc_dai_driver *codec_dai_drv;
1791 struct snd_soc_pcm_stream *codec_stream;
1792 struct snd_soc_pcm_stream *cpu_stream;
1794 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1795 cpu_stream = &cpu_dai_drv->playback;
1797 cpu_stream = &cpu_dai_drv->capture;
1799 *channels_min = max(*channels_min, cpu_stream->channels_min);
1800 *channels_max = min(*channels_max, cpu_stream->channels_max);
1803 * chan min/max cannot be enforced if there are multiple CODEC
1804 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1806 if (be->num_codecs == 1) {
1807 codec_dai_drv = be->codec_dais[0]->driver;
1809 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1810 codec_stream = &codec_dai_drv->playback;
1812 codec_stream = &codec_dai_drv->capture;
1814 *channels_min = max(*channels_min,
1815 codec_stream->channels_min);
1816 *channels_max = min(*channels_max,
1817 codec_stream->channels_max);
1822 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1823 unsigned int *rates,
1824 unsigned int *rate_min,
1825 unsigned int *rate_max)
1827 struct snd_soc_pcm_runtime *fe = substream->private_data;
1828 struct snd_soc_dpcm *dpcm;
1829 int stream = substream->stream;
1831 if (!fe->dai_link->dpcm_merged_rate)
1835 * It returns merged BE codec channel;
1836 * if FE want to use it (= dpcm_merged_chan)
1839 for_each_dpcm_be(fe, stream, dpcm) {
1840 struct snd_soc_pcm_runtime *be = dpcm->be;
1841 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1842 struct snd_soc_dai_driver *codec_dai_drv;
1843 struct snd_soc_pcm_stream *codec_stream;
1844 struct snd_soc_pcm_stream *cpu_stream;
1845 struct snd_soc_dai *dai;
1848 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1849 cpu_stream = &cpu_dai_drv->playback;
1851 cpu_stream = &cpu_dai_drv->capture;
1853 *rate_min = max(*rate_min, cpu_stream->rate_min);
1854 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1855 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1857 for_each_rtd_codec_dai(be, i, dai) {
1859 * Skip CODECs which don't support the current stream
1860 * type. See soc_pcm_init_runtime_hw() for more details
1862 if (!snd_soc_dai_stream_valid(dai, stream))
1865 codec_dai_drv = dai->driver;
1866 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1867 codec_stream = &codec_dai_drv->playback;
1869 codec_stream = &codec_dai_drv->capture;
1871 *rate_min = max(*rate_min, codec_stream->rate_min);
1872 *rate_max = min_not_zero(*rate_max,
1873 codec_stream->rate_max);
1874 *rates = snd_pcm_rate_mask_intersect(*rates,
1875 codec_stream->rates);
1880 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1882 struct snd_pcm_runtime *runtime = substream->runtime;
1883 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1884 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1885 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1887 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1888 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1890 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1892 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1893 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1894 &runtime->hw.channels_max);
1895 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1896 &runtime->hw.rate_min, &runtime->hw.rate_max);
1899 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1901 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1902 * for avoiding the race with trigger callback.
1903 * If the state is unset and a trigger is pending while the previous operation,
1904 * process the pending trigger action here.
1906 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1907 int stream, enum snd_soc_dpcm_update state)
1909 struct snd_pcm_substream *substream =
1910 snd_soc_dpcm_get_substream(fe, stream);
1912 snd_pcm_stream_lock_irq(substream);
1913 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1914 dpcm_fe_dai_do_trigger(substream,
1915 fe->dpcm[stream].trigger_pending - 1);
1916 fe->dpcm[stream].trigger_pending = 0;
1918 fe->dpcm[stream].runtime_update = state;
1919 snd_pcm_stream_unlock_irq(substream);
1922 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1925 struct snd_soc_dpcm *dpcm;
1926 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1927 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1930 /* apply symmetry for FE */
1931 if (soc_pcm_has_symmetry(fe_substream))
1932 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1934 /* Symmetry only applies if we've got an active stream. */
1935 if (fe_cpu_dai->active) {
1936 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1941 /* apply symmetry for BE */
1942 for_each_dpcm_be(fe, stream, dpcm) {
1943 struct snd_soc_pcm_runtime *be = dpcm->be;
1944 struct snd_pcm_substream *be_substream =
1945 snd_soc_dpcm_get_substream(be, stream);
1946 struct snd_soc_pcm_runtime *rtd;
1947 struct snd_soc_dai *codec_dai;
1950 /* A backend may not have the requested substream */
1954 rtd = be_substream->private_data;
1955 if (rtd->dai_link->be_hw_params_fixup)
1958 if (soc_pcm_has_symmetry(be_substream))
1959 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1961 /* Symmetry only applies if we've got an active stream. */
1962 if (rtd->cpu_dai->active) {
1963 err = soc_pcm_apply_symmetry(fe_substream,
1969 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1970 if (codec_dai->active) {
1971 err = soc_pcm_apply_symmetry(fe_substream,
1982 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1984 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1985 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1986 int stream = fe_substream->stream, ret = 0;
1988 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1990 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1992 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1996 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1998 /* start the DAI frontend */
1999 ret = soc_pcm_open(fe_substream);
2001 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
2005 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
2007 dpcm_set_fe_runtime(fe_substream);
2008 snd_pcm_limit_hw_rates(runtime);
2010 ret = dpcm_apply_symmetry(fe_substream, stream);
2012 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2017 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2021 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
2023 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2027 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2029 struct snd_soc_dpcm *dpcm;
2031 /* only shutdown BEs that are either sinks or sources to this FE DAI */
2032 for_each_dpcm_be(fe, stream, dpcm) {
2034 struct snd_soc_pcm_runtime *be = dpcm->be;
2035 struct snd_pcm_substream *be_substream =
2036 snd_soc_dpcm_get_substream(be, stream);
2038 /* is this op for this BE ? */
2039 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2042 if (be->dpcm[stream].users == 0)
2043 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2044 stream ? "capture" : "playback",
2045 be->dpcm[stream].state);
2047 if (--be->dpcm[stream].users != 0)
2050 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2052 soc_pcm_hw_free(be_substream);
2053 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2056 dev_dbg(be->dev, "ASoC: close BE %s\n",
2057 be->dai_link->name);
2059 soc_pcm_close(be_substream);
2060 be_substream->runtime = NULL;
2062 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2067 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2069 struct snd_soc_pcm_runtime *fe = substream->private_data;
2070 int stream = substream->stream;
2072 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2074 /* shutdown the BEs */
2075 dpcm_be_dai_shutdown(fe, substream->stream);
2077 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2079 /* now shutdown the frontend */
2080 soc_pcm_close(substream);
2082 /* run the stream event for each BE */
2083 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2085 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2086 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2090 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2092 struct snd_soc_dpcm *dpcm;
2094 /* only hw_params backends that are either sinks or sources
2095 * to this frontend DAI */
2096 for_each_dpcm_be(fe, stream, dpcm) {
2098 struct snd_soc_pcm_runtime *be = dpcm->be;
2099 struct snd_pcm_substream *be_substream =
2100 snd_soc_dpcm_get_substream(be, stream);
2102 /* is this op for this BE ? */
2103 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2106 /* only free hw when no longer used - check all FEs */
2107 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2110 /* do not free hw if this BE is used by other FE */
2111 if (be->dpcm[stream].users > 1)
2114 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2115 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2116 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2117 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2118 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2119 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2122 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2123 be->dai_link->name);
2125 soc_pcm_hw_free(be_substream);
2127 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2133 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2135 struct snd_soc_pcm_runtime *fe = substream->private_data;
2136 int err, stream = substream->stream;
2138 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2139 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2141 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2143 /* call hw_free on the frontend */
2144 err = soc_pcm_hw_free(substream);
2146 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2147 fe->dai_link->name);
2149 /* only hw_params backends that are either sinks or sources
2150 * to this frontend DAI */
2151 err = dpcm_be_dai_hw_free(fe, stream);
2153 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2154 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2156 mutex_unlock(&fe->card->mutex);
2160 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2162 struct snd_soc_dpcm *dpcm;
2165 for_each_dpcm_be(fe, stream, dpcm) {
2167 struct snd_soc_pcm_runtime *be = dpcm->be;
2168 struct snd_pcm_substream *be_substream =
2169 snd_soc_dpcm_get_substream(be, stream);
2171 /* is this op for this BE ? */
2172 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2175 /* copy params for each dpcm */
2176 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2177 sizeof(struct snd_pcm_hw_params));
2179 /* perform any hw_params fixups */
2180 if (be->dai_link->be_hw_params_fixup) {
2181 ret = be->dai_link->be_hw_params_fixup(be,
2185 "ASoC: hw_params BE fixup failed %d\n",
2191 /* copy the fixed-up hw params for BE dai */
2192 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2193 sizeof(struct snd_pcm_hw_params));
2195 /* only allow hw_params() if no connected FEs are running */
2196 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2199 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2200 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2201 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2204 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2205 be->dai_link->name);
2207 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2209 dev_err(dpcm->be->dev,
2210 "ASoC: hw_params BE failed %d\n", ret);
2214 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2219 /* disable any enabled and non active backends */
2220 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2221 struct snd_soc_pcm_runtime *be = dpcm->be;
2222 struct snd_pcm_substream *be_substream =
2223 snd_soc_dpcm_get_substream(be, stream);
2225 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2228 /* only allow hw_free() if no connected FEs are running */
2229 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2232 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2233 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2234 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2235 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2238 soc_pcm_hw_free(be_substream);
2244 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2245 struct snd_pcm_hw_params *params)
2247 struct snd_soc_pcm_runtime *fe = substream->private_data;
2248 int ret, stream = substream->stream;
2250 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2251 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2253 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2254 sizeof(struct snd_pcm_hw_params));
2255 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2257 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2261 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2262 fe->dai_link->name, params_rate(params),
2263 params_channels(params), params_format(params));
2265 /* call hw_params on the frontend */
2266 ret = soc_pcm_hw_params(substream, params);
2268 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2269 dpcm_be_dai_hw_free(fe, stream);
2271 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2274 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2275 mutex_unlock(&fe->card->mutex);
2279 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2280 struct snd_pcm_substream *substream, int cmd)
2284 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2285 dpcm->be->dai_link->name, cmd);
2287 ret = soc_pcm_trigger(substream, cmd);
2289 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2294 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2297 struct snd_soc_dpcm *dpcm;
2300 for_each_dpcm_be(fe, stream, dpcm) {
2302 struct snd_soc_pcm_runtime *be = dpcm->be;
2303 struct snd_pcm_substream *be_substream =
2304 snd_soc_dpcm_get_substream(be, stream);
2306 /* is this op for this BE ? */
2307 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2311 case SNDRV_PCM_TRIGGER_START:
2312 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2313 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2316 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2320 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2322 case SNDRV_PCM_TRIGGER_RESUME:
2323 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2326 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2330 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2332 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2333 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2336 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2340 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2342 case SNDRV_PCM_TRIGGER_STOP:
2343 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2346 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2349 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2353 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2355 case SNDRV_PCM_TRIGGER_SUSPEND:
2356 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2359 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2362 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2366 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2368 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2369 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2372 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2375 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2379 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2386 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2388 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2390 struct snd_soc_pcm_runtime *fe = substream->private_data;
2391 int stream = substream->stream, ret;
2392 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2394 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2397 case SND_SOC_DPCM_TRIGGER_PRE:
2398 /* call trigger on the frontend before the backend. */
2400 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2401 fe->dai_link->name, cmd);
2403 ret = soc_pcm_trigger(substream, cmd);
2405 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2409 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2411 case SND_SOC_DPCM_TRIGGER_POST:
2412 /* call trigger on the frontend after the backend. */
2414 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2416 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2420 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2421 fe->dai_link->name, cmd);
2423 ret = soc_pcm_trigger(substream, cmd);
2425 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2426 /* bespoke trigger() - handles both FE and BEs */
2428 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2429 fe->dai_link->name, cmd);
2431 ret = soc_pcm_bespoke_trigger(substream, cmd);
2433 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2438 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2439 fe->dai_link->name);
2445 case SNDRV_PCM_TRIGGER_START:
2446 case SNDRV_PCM_TRIGGER_RESUME:
2447 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2448 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2450 case SNDRV_PCM_TRIGGER_STOP:
2451 case SNDRV_PCM_TRIGGER_SUSPEND:
2452 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2454 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2455 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2460 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2464 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2466 struct snd_soc_pcm_runtime *fe = substream->private_data;
2467 int stream = substream->stream;
2469 /* if FE's runtime_update is already set, we're in race;
2470 * process this trigger later at exit
2472 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2473 fe->dpcm[stream].trigger_pending = cmd + 1;
2474 return 0; /* delayed, assuming it's successful */
2477 /* we're alone, let's trigger */
2478 return dpcm_fe_dai_do_trigger(substream, cmd);
2481 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2483 struct snd_soc_dpcm *dpcm;
2486 for_each_dpcm_be(fe, stream, dpcm) {
2488 struct snd_soc_pcm_runtime *be = dpcm->be;
2489 struct snd_pcm_substream *be_substream =
2490 snd_soc_dpcm_get_substream(be, stream);
2492 /* is this op for this BE ? */
2493 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2496 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2497 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2498 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2499 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2502 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2503 be->dai_link->name);
2505 ret = soc_pcm_prepare(be_substream);
2507 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2512 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2517 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2519 struct snd_soc_pcm_runtime *fe = substream->private_data;
2520 int stream = substream->stream, ret = 0;
2522 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2524 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2526 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2528 /* there is no point preparing this FE if there are no BEs */
2529 if (list_empty(&fe->dpcm[stream].be_clients)) {
2530 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2531 fe->dai_link->name);
2536 ret = dpcm_be_dai_prepare(fe, substream->stream);
2540 /* call prepare on the frontend */
2541 ret = soc_pcm_prepare(substream);
2543 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2544 fe->dai_link->name);
2548 /* run the stream event for each BE */
2549 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2550 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2553 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2554 mutex_unlock(&fe->card->mutex);
2559 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2560 unsigned int cmd, void *arg)
2562 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2563 struct snd_soc_component *component;
2564 struct snd_soc_rtdcom_list *rtdcom;
2566 for_each_rtdcom(rtd, rtdcom) {
2567 component = rtdcom->component;
2569 if (!component->driver->ops ||
2570 !component->driver->ops->ioctl)
2573 /* FIXME: use 1st ioctl */
2574 return component->driver->ops->ioctl(substream, cmd, arg);
2577 return snd_pcm_lib_ioctl(substream, cmd, arg);
2580 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2582 struct snd_pcm_substream *substream =
2583 snd_soc_dpcm_get_substream(fe, stream);
2584 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2587 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2588 stream ? "capture" : "playback", fe->dai_link->name);
2590 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2591 /* call bespoke trigger - FE takes care of all BE triggers */
2592 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2593 fe->dai_link->name);
2595 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2597 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2599 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2600 fe->dai_link->name);
2602 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2604 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2607 err = dpcm_be_dai_hw_free(fe, stream);
2609 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2611 err = dpcm_be_dai_shutdown(fe, stream);
2613 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2615 /* run the stream event for each BE */
2616 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2621 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2623 struct snd_pcm_substream *substream =
2624 snd_soc_dpcm_get_substream(fe, stream);
2625 struct snd_soc_dpcm *dpcm;
2626 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2628 unsigned long flags;
2630 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2631 stream ? "capture" : "playback", fe->dai_link->name);
2633 /* Only start the BE if the FE is ready */
2634 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2635 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2638 /* startup must always be called for new BEs */
2639 ret = dpcm_be_dai_startup(fe, stream);
2643 /* keep going if FE state is > open */
2644 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2647 ret = dpcm_be_dai_hw_params(fe, stream);
2651 /* keep going if FE state is > hw_params */
2652 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2656 ret = dpcm_be_dai_prepare(fe, stream);
2660 /* run the stream event for each BE */
2661 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2663 /* keep going if FE state is > prepare */
2664 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2665 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2668 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2669 /* call trigger on the frontend - FE takes care of all BE triggers */
2670 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2671 fe->dai_link->name);
2673 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2675 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2679 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2680 fe->dai_link->name);
2682 ret = dpcm_be_dai_trigger(fe, stream,
2683 SNDRV_PCM_TRIGGER_START);
2685 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2693 dpcm_be_dai_hw_free(fe, stream);
2695 dpcm_be_dai_shutdown(fe, stream);
2697 /* disconnect any non started BEs */
2698 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2699 for_each_dpcm_be(fe, stream, dpcm) {
2700 struct snd_soc_pcm_runtime *be = dpcm->be;
2701 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2702 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2704 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2709 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2713 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2714 ret = dpcm_run_update_startup(fe, stream);
2716 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2717 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2722 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2726 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2727 ret = dpcm_run_update_shutdown(fe, stream);
2729 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2730 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2735 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2737 struct snd_soc_dapm_widget_list *list;
2740 if (!fe->dai_link->dynamic)
2743 /* only check active links */
2744 if (!fe->cpu_dai->active)
2747 /* DAPM sync will call this to update DSP paths */
2748 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2749 new ? "new" : "old", fe->dai_link->name);
2751 /* skip if FE doesn't have playback capability */
2752 if (!fe->cpu_dai->driver->playback.channels_min ||
2753 !fe->codec_dai->driver->playback.channels_min)
2756 /* skip if FE isn't currently playing */
2757 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2760 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2762 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2763 fe->dai_link->name, "playback");
2767 /* update any playback paths */
2768 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2771 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2773 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2775 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2776 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2779 dpcm_path_put(&list);
2782 /* skip if FE doesn't have capture capability */
2783 if (!fe->cpu_dai->driver->capture.channels_min ||
2784 !fe->codec_dai->driver->capture.channels_min)
2787 /* skip if FE isn't currently capturing */
2788 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2791 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2793 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2794 fe->dai_link->name, "capture");
2798 /* update any old capture paths */
2799 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2802 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2804 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2806 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2807 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2810 dpcm_path_put(&list);
2815 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2818 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2820 struct snd_soc_pcm_runtime *fe;
2823 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2824 /* shutdown all old paths first */
2825 for_each_card_rtds(card, fe) {
2826 ret = soc_dpcm_fe_runtime_update(fe, 0);
2831 /* bring new paths up */
2832 for_each_card_rtds(card, fe) {
2833 ret = soc_dpcm_fe_runtime_update(fe, 1);
2839 mutex_unlock(&card->mutex);
2842 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2844 struct snd_soc_dpcm *dpcm;
2845 struct snd_soc_dai *dai;
2847 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2849 struct snd_soc_pcm_runtime *be = dpcm->be;
2852 if (be->dai_link->ignore_suspend)
2855 for_each_rtd_codec_dai(be, i, dai) {
2856 struct snd_soc_dai_driver *drv = dai->driver;
2858 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2859 be->dai_link->name);
2861 if (drv->ops && drv->ops->digital_mute &&
2862 dai->playback_active)
2863 drv->ops->digital_mute(dai, mute);
2870 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2872 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2873 struct snd_soc_dpcm *dpcm;
2874 struct snd_soc_dapm_widget_list *list;
2876 int stream = fe_substream->stream;
2878 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2879 fe->dpcm[stream].runtime = fe_substream->runtime;
2881 ret = dpcm_path_get(fe, stream, &list);
2883 mutex_unlock(&fe->card->mutex);
2885 } else if (ret == 0) {
2886 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2887 fe->dai_link->name, stream ? "capture" : "playback");
2890 /* calculate valid and active FE <-> BE dpcms */
2891 dpcm_process_paths(fe, stream, &list, 1);
2893 ret = dpcm_fe_dai_startup(fe_substream);
2895 /* clean up all links */
2896 for_each_dpcm_be(fe, stream, dpcm)
2897 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2899 dpcm_be_disconnect(fe, stream);
2900 fe->dpcm[stream].runtime = NULL;
2903 dpcm_clear_pending_state(fe, stream);
2904 dpcm_path_put(&list);
2905 mutex_unlock(&fe->card->mutex);
2909 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2911 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2912 struct snd_soc_dpcm *dpcm;
2913 int stream = fe_substream->stream, ret;
2915 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2916 ret = dpcm_fe_dai_shutdown(fe_substream);
2918 /* mark FE's links ready to prune */
2919 for_each_dpcm_be(fe, stream, dpcm)
2920 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2922 dpcm_be_disconnect(fe, stream);
2924 fe->dpcm[stream].runtime = NULL;
2925 mutex_unlock(&fe->card->mutex);
2929 static void soc_pcm_private_free(struct snd_pcm *pcm)
2931 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2932 struct snd_soc_rtdcom_list *rtdcom;
2933 struct snd_soc_component *component;
2935 /* need to sync the delayed work before releasing resources */
2936 flush_delayed_work(&rtd->delayed_work);
2937 for_each_rtdcom(rtd, rtdcom) {
2938 component = rtdcom->component;
2940 if (component->driver->pcm_free)
2941 component->driver->pcm_free(pcm);
2945 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2947 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2948 struct snd_soc_rtdcom_list *rtdcom;
2949 struct snd_soc_component *component;
2951 for_each_rtdcom(rtd, rtdcom) {
2952 component = rtdcom->component;
2954 if (!component->driver->ops ||
2955 !component->driver->ops->ack)
2958 /* FIXME. it returns 1st ask now */
2959 return component->driver->ops->ack(substream);
2965 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2966 unsigned long pos, void __user *buf,
2967 unsigned long bytes)
2969 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2970 struct snd_soc_rtdcom_list *rtdcom;
2971 struct snd_soc_component *component;
2973 for_each_rtdcom(rtd, rtdcom) {
2974 component = rtdcom->component;
2976 if (!component->driver->ops ||
2977 !component->driver->ops->copy_user)
2980 /* FIXME. it returns 1st copy now */
2981 return component->driver->ops->copy_user(substream, channel,
2988 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2989 unsigned long pos, void *buf, unsigned long bytes)
2991 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2992 struct snd_soc_rtdcom_list *rtdcom;
2993 struct snd_soc_component *component;
2995 for_each_rtdcom(rtd, rtdcom) {
2996 component = rtdcom->component;
2998 if (!component->driver->ops ||
2999 !component->driver->ops->copy_kernel)
3002 /* FIXME. it returns 1st copy now */
3003 return component->driver->ops->copy_kernel(substream, channel,
3010 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
3011 unsigned long pos, unsigned long bytes)
3013 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3014 struct snd_soc_rtdcom_list *rtdcom;
3015 struct snd_soc_component *component;
3017 for_each_rtdcom(rtd, rtdcom) {
3018 component = rtdcom->component;
3020 if (!component->driver->ops ||
3021 !component->driver->ops->fill_silence)
3024 /* FIXME. it returns 1st silence now */
3025 return component->driver->ops->fill_silence(substream, channel,
3032 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3033 unsigned long offset)
3035 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3036 struct snd_soc_rtdcom_list *rtdcom;
3037 struct snd_soc_component *component;
3040 for_each_rtdcom(rtd, rtdcom) {
3041 component = rtdcom->component;
3043 if (!component->driver->ops ||
3044 !component->driver->ops->page)
3047 /* FIXME. it returns 1st page now */
3048 page = component->driver->ops->page(substream, offset);
3056 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3057 struct vm_area_struct *vma)
3059 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3060 struct snd_soc_rtdcom_list *rtdcom;
3061 struct snd_soc_component *component;
3063 for_each_rtdcom(rtd, rtdcom) {
3064 component = rtdcom->component;
3066 if (!component->driver->ops ||
3067 !component->driver->ops->mmap)
3070 /* FIXME. it returns 1st mmap now */
3071 return component->driver->ops->mmap(substream, vma);
3077 /* create a new pcm */
3078 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3080 struct snd_soc_dai *codec_dai;
3081 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3082 struct snd_soc_component *component;
3083 struct snd_soc_rtdcom_list *rtdcom;
3084 struct snd_pcm *pcm;
3086 int ret = 0, playback = 0, capture = 0;
3089 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3090 playback = rtd->dai_link->dpcm_playback;
3091 capture = rtd->dai_link->dpcm_capture;
3093 for_each_rtd_codec_dai(rtd, i, codec_dai) {
3094 if (codec_dai->driver->playback.channels_min)
3096 if (codec_dai->driver->capture.channels_min)
3100 capture = capture && cpu_dai->driver->capture.channels_min;
3101 playback = playback && cpu_dai->driver->playback.channels_min;
3104 if (rtd->dai_link->playback_only) {
3109 if (rtd->dai_link->capture_only) {
3114 /* create the PCM */
3115 if (rtd->dai_link->no_pcm) {
3116 snprintf(new_name, sizeof(new_name), "(%s)",
3117 rtd->dai_link->stream_name);
3119 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3120 playback, capture, &pcm);
3122 if (rtd->dai_link->dynamic)
3123 snprintf(new_name, sizeof(new_name), "%s (*)",
3124 rtd->dai_link->stream_name);
3126 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3127 rtd->dai_link->stream_name,
3128 (rtd->num_codecs > 1) ?
3129 "multicodec" : rtd->codec_dai->name, num);
3131 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3135 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3136 rtd->dai_link->name);
3139 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3141 /* DAPM dai link stream work */
3142 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3144 pcm->nonatomic = rtd->dai_link->nonatomic;
3146 pcm->private_data = rtd;
3148 if (rtd->dai_link->no_pcm) {
3150 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3152 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3156 /* ASoC PCM operations */
3157 if (rtd->dai_link->dynamic) {
3158 rtd->ops.open = dpcm_fe_dai_open;
3159 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3160 rtd->ops.prepare = dpcm_fe_dai_prepare;
3161 rtd->ops.trigger = dpcm_fe_dai_trigger;
3162 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3163 rtd->ops.close = dpcm_fe_dai_close;
3164 rtd->ops.pointer = soc_pcm_pointer;
3165 rtd->ops.ioctl = soc_pcm_ioctl;
3167 rtd->ops.open = soc_pcm_open;
3168 rtd->ops.hw_params = soc_pcm_hw_params;
3169 rtd->ops.prepare = soc_pcm_prepare;
3170 rtd->ops.trigger = soc_pcm_trigger;
3171 rtd->ops.hw_free = soc_pcm_hw_free;
3172 rtd->ops.close = soc_pcm_close;
3173 rtd->ops.pointer = soc_pcm_pointer;
3174 rtd->ops.ioctl = soc_pcm_ioctl;
3177 for_each_rtdcom(rtd, rtdcom) {
3178 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3184 rtd->ops.ack = soc_rtdcom_ack;
3186 rtd->ops.copy_user = soc_rtdcom_copy_user;
3187 if (ops->copy_kernel)
3188 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3189 if (ops->fill_silence)
3190 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3192 rtd->ops.page = soc_rtdcom_page;
3194 rtd->ops.mmap = soc_rtdcom_mmap;
3198 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3201 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3203 for_each_rtdcom(rtd, rtdcom) {
3204 component = rtdcom->component;
3206 if (!component->driver->pcm_new)
3209 ret = component->driver->pcm_new(rtd);
3211 dev_err(component->dev,
3212 "ASoC: pcm constructor failed: %d\n",
3218 pcm->private_free = soc_pcm_private_free;
3219 pcm->no_device_suspend = true;
3221 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3222 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3227 /* is the current PCM operation for this FE ? */
3228 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3230 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3234 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3236 /* is the current PCM operation for this BE ? */
3237 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3238 struct snd_soc_pcm_runtime *be, int stream)
3240 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3241 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3242 be->dpcm[stream].runtime_update))
3246 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3248 /* get the substream for this BE */
3249 struct snd_pcm_substream *
3250 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3252 return be->pcm->streams[stream].substream;
3254 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3256 /* get the BE runtime state */
3257 enum snd_soc_dpcm_state
3258 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3260 return be->dpcm[stream].state;
3262 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3264 /* set the BE runtime state */
3265 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3266 int stream, enum snd_soc_dpcm_state state)
3268 be->dpcm[stream].state = state;
3270 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3273 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3274 * are not running, paused or suspended for the specified stream direction.
3276 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3277 struct snd_soc_pcm_runtime *be, int stream)
3279 struct snd_soc_dpcm *dpcm;
3282 unsigned long flags;
3284 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3285 for_each_dpcm_fe(be, stream, dpcm) {
3290 state = dpcm->fe->dpcm[stream].state;
3291 if (state == SND_SOC_DPCM_STATE_START ||
3292 state == SND_SOC_DPCM_STATE_PAUSED ||
3293 state == SND_SOC_DPCM_STATE_SUSPEND) {
3298 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3300 /* it's safe to free/stop this BE DAI */
3303 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3306 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3307 * running, paused or suspended for the specified stream direction.
3309 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3310 struct snd_soc_pcm_runtime *be, int stream)
3312 struct snd_soc_dpcm *dpcm;
3315 unsigned long flags;
3317 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3318 for_each_dpcm_fe(be, stream, dpcm) {
3323 state = dpcm->fe->dpcm[stream].state;
3324 if (state == SND_SOC_DPCM_STATE_START ||
3325 state == SND_SOC_DPCM_STATE_PAUSED ||
3326 state == SND_SOC_DPCM_STATE_SUSPEND ||
3327 state == SND_SOC_DPCM_STATE_PREPARE) {
3332 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3334 /* it's safe to change hw_params */
3337 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3339 #ifdef CONFIG_DEBUG_FS
3340 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3343 case SND_SOC_DPCM_STATE_NEW:
3345 case SND_SOC_DPCM_STATE_OPEN:
3347 case SND_SOC_DPCM_STATE_HW_PARAMS:
3349 case SND_SOC_DPCM_STATE_PREPARE:
3351 case SND_SOC_DPCM_STATE_START:
3353 case SND_SOC_DPCM_STATE_STOP:
3355 case SND_SOC_DPCM_STATE_SUSPEND:
3357 case SND_SOC_DPCM_STATE_PAUSED:
3359 case SND_SOC_DPCM_STATE_HW_FREE:
3361 case SND_SOC_DPCM_STATE_CLOSE:
3368 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3369 int stream, char *buf, size_t size)
3371 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3372 struct snd_soc_dpcm *dpcm;
3374 unsigned long flags;
3377 offset += snprintf(buf + offset, size - offset,
3378 "[%s - %s]\n", fe->dai_link->name,
3379 stream ? "Capture" : "Playback");
3381 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3382 dpcm_state_string(fe->dpcm[stream].state));
3384 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3385 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3386 offset += snprintf(buf + offset, size - offset,
3388 "Format = %s, Channels = %d, Rate = %d\n",
3389 snd_pcm_format_name(params_format(params)),
3390 params_channels(params),
3391 params_rate(params));
3394 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3396 if (list_empty(&fe->dpcm[stream].be_clients)) {
3397 offset += snprintf(buf + offset, size - offset,
3398 " No active DSP links\n");
3402 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3403 for_each_dpcm_be(fe, stream, dpcm) {
3404 struct snd_soc_pcm_runtime *be = dpcm->be;
3405 params = &dpcm->hw_params;
3407 offset += snprintf(buf + offset, size - offset,
3408 "- %s\n", be->dai_link->name);
3410 offset += snprintf(buf + offset, size - offset,
3412 dpcm_state_string(be->dpcm[stream].state));
3414 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3415 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3416 offset += snprintf(buf + offset, size - offset,
3417 " Hardware Params: "
3418 "Format = %s, Channels = %d, Rate = %d\n",
3419 snd_pcm_format_name(params_format(params)),
3420 params_channels(params),
3421 params_rate(params));
3423 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3428 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3429 size_t count, loff_t *ppos)
3431 struct snd_soc_pcm_runtime *fe = file->private_data;
3432 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3435 buf = kmalloc(out_count, GFP_KERNEL);
3439 if (fe->cpu_dai->driver->playback.channels_min)
3440 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3441 buf + offset, out_count - offset);
3443 if (fe->cpu_dai->driver->capture.channels_min)
3444 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3445 buf + offset, out_count - offset);
3447 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3453 static const struct file_operations dpcm_state_fops = {
3454 .open = simple_open,
3455 .read = dpcm_state_read_file,
3456 .llseek = default_llseek,
3459 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3464 if (!rtd->card->debugfs_card_root)
3467 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3468 rtd->card->debugfs_card_root);
3469 if (!rtd->debugfs_dpcm_root) {
3471 "ASoC: Failed to create dpcm debugfs directory %s\n",
3472 rtd->dai_link->name);
3476 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3477 rtd, &dpcm_state_fops);