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)) {
526 ret = component->driver->ops->open(substream);
528 dev_err(component->dev,
529 "ASoC: can't open component %s: %d\n",
530 component->name, ret);
536 for_each_rtd_codec_dai(rtd, i, codec_dai) {
537 if (codec_dai->driver->ops->startup) {
538 ret = codec_dai->driver->ops->startup(substream,
541 dev_err(codec_dai->dev,
542 "ASoC: can't open codec %s: %d\n",
543 codec_dai->name, ret);
548 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
549 codec_dai->tx_mask = 0;
551 codec_dai->rx_mask = 0;
554 if (rtd->dai_link->ops->startup) {
555 ret = rtd->dai_link->ops->startup(substream);
557 pr_err("ASoC: %s startup failed: %d\n",
558 rtd->dai_link->name, ret);
563 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
564 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
567 /* Check that the codec and cpu DAIs are compatible */
568 soc_pcm_init_runtime_hw(substream);
570 if (rtd->num_codecs == 1)
571 codec_dai_name = rtd->codec_dai->name;
573 if (soc_pcm_has_symmetry(substream))
574 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
577 if (!runtime->hw.rates) {
578 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
579 codec_dai_name, cpu_dai->name);
582 if (!runtime->hw.formats) {
583 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
584 codec_dai_name, cpu_dai->name);
587 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
588 runtime->hw.channels_min > runtime->hw.channels_max) {
589 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
590 codec_dai_name, cpu_dai->name);
594 soc_pcm_apply_msb(substream);
596 /* Symmetry only applies if we've already got an active stream. */
597 if (cpu_dai->active) {
598 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
603 for_each_rtd_codec_dai(rtd, i, codec_dai) {
604 if (codec_dai->active) {
605 ret = soc_pcm_apply_symmetry(substream, codec_dai);
611 pr_debug("ASoC: %s <-> %s info:\n",
612 codec_dai_name, cpu_dai->name);
613 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
614 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
615 runtime->hw.channels_max);
616 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
617 runtime->hw.rate_max);
621 snd_soc_runtime_activate(rtd, substream->stream);
623 mutex_unlock(&rtd->pcm_mutex);
627 if (rtd->dai_link->ops->shutdown)
628 rtd->dai_link->ops->shutdown(substream);
634 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
635 if (codec_dai->driver->ops->shutdown)
636 codec_dai->driver->ops->shutdown(substream, codec_dai);
640 soc_pcm_components_close(substream, component);
642 if (cpu_dai->driver->ops->shutdown)
643 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
645 mutex_unlock(&rtd->pcm_mutex);
647 for_each_rtdcom(rtd, rtdcom) {
648 component = rtdcom->component;
650 pm_runtime_mark_last_busy(component->dev);
651 pm_runtime_put_autosuspend(component->dev);
654 for_each_rtd_codec_dai(rtd, i, codec_dai) {
655 if (!codec_dai->active)
656 pinctrl_pm_select_sleep_state(codec_dai->dev);
658 if (!cpu_dai->active)
659 pinctrl_pm_select_sleep_state(cpu_dai->dev);
665 * Power down the audio subsystem pmdown_time msecs after close is called.
666 * This is to ensure there are no pops or clicks in between any music tracks
667 * due to DAPM power cycling.
669 static void close_delayed_work(struct work_struct *work)
671 struct snd_soc_pcm_runtime *rtd =
672 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
673 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
675 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
677 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
678 codec_dai->driver->playback.stream_name,
679 codec_dai->playback_active ? "active" : "inactive",
680 rtd->pop_wait ? "yes" : "no");
682 /* are we waiting on this codec DAI stream */
683 if (rtd->pop_wait == 1) {
685 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
686 SND_SOC_DAPM_STREAM_STOP);
689 mutex_unlock(&rtd->pcm_mutex);
693 * Called by ALSA when a PCM substream is closed. Private data can be
694 * freed here. The cpu DAI, codec DAI, machine and components are also
697 static int soc_pcm_close(struct snd_pcm_substream *substream)
699 struct snd_soc_pcm_runtime *rtd = substream->private_data;
700 struct snd_soc_component *component;
701 struct snd_soc_rtdcom_list *rtdcom;
702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
703 struct snd_soc_dai *codec_dai;
706 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
708 snd_soc_runtime_deactivate(rtd, substream->stream);
710 /* clear the corresponding DAIs rate when inactive */
711 if (!cpu_dai->active)
714 for_each_rtd_codec_dai(rtd, i, codec_dai) {
715 if (!codec_dai->active)
719 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
721 if (cpu_dai->driver->ops->shutdown)
722 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
724 for_each_rtd_codec_dai(rtd, i, codec_dai) {
725 if (codec_dai->driver->ops->shutdown)
726 codec_dai->driver->ops->shutdown(substream, codec_dai);
729 if (rtd->dai_link->ops->shutdown)
730 rtd->dai_link->ops->shutdown(substream);
732 soc_pcm_components_close(substream, NULL);
734 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
735 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
736 /* powered down playback stream now */
737 snd_soc_dapm_stream_event(rtd,
738 SNDRV_PCM_STREAM_PLAYBACK,
739 SND_SOC_DAPM_STREAM_STOP);
741 /* start delayed pop wq here for playback streams */
743 queue_delayed_work(system_power_efficient_wq,
745 msecs_to_jiffies(rtd->pmdown_time));
748 /* capture streams can be powered down now */
749 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
750 SND_SOC_DAPM_STREAM_STOP);
753 mutex_unlock(&rtd->pcm_mutex);
755 for_each_rtdcom(rtd, rtdcom) {
756 component = rtdcom->component;
758 pm_runtime_mark_last_busy(component->dev);
759 pm_runtime_put_autosuspend(component->dev);
762 for_each_rtd_codec_dai(rtd, i, codec_dai) {
763 if (!codec_dai->active)
764 pinctrl_pm_select_sleep_state(codec_dai->dev);
766 if (!cpu_dai->active)
767 pinctrl_pm_select_sleep_state(cpu_dai->dev);
773 * Called by ALSA when the PCM substream is prepared, can set format, sample
774 * rate, etc. This function is non atomic and can be called multiple times,
775 * it can refer to the runtime info.
777 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
779 struct snd_soc_pcm_runtime *rtd = substream->private_data;
780 struct snd_soc_component *component;
781 struct snd_soc_rtdcom_list *rtdcom;
782 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
783 struct snd_soc_dai *codec_dai;
786 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
788 if (rtd->dai_link->ops->prepare) {
789 ret = rtd->dai_link->ops->prepare(substream);
791 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
797 for_each_rtdcom(rtd, rtdcom) {
798 component = rtdcom->component;
800 if (!component->driver->ops ||
801 !component->driver->ops->prepare)
804 ret = component->driver->ops->prepare(substream);
806 dev_err(component->dev,
807 "ASoC: platform prepare error: %d\n", ret);
812 for_each_rtd_codec_dai(rtd, i, codec_dai) {
813 if (codec_dai->driver->ops->prepare) {
814 ret = codec_dai->driver->ops->prepare(substream,
817 dev_err(codec_dai->dev,
818 "ASoC: codec DAI prepare error: %d\n",
825 if (cpu_dai->driver->ops->prepare) {
826 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
828 dev_err(cpu_dai->dev,
829 "ASoC: cpu DAI prepare error: %d\n", ret);
834 /* cancel any delayed stream shutdown that is pending */
835 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
838 cancel_delayed_work(&rtd->delayed_work);
841 snd_soc_dapm_stream_event(rtd, substream->stream,
842 SND_SOC_DAPM_STREAM_START);
844 for_each_rtd_codec_dai(rtd, i, codec_dai)
845 snd_soc_dai_digital_mute(codec_dai, 0,
847 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
850 mutex_unlock(&rtd->pcm_mutex);
854 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
857 struct snd_interval *interval;
858 int channels = hweight_long(mask);
860 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
861 interval->min = channels;
862 interval->max = channels;
865 int soc_dai_hw_params(struct snd_pcm_substream *substream,
866 struct snd_pcm_hw_params *params,
867 struct snd_soc_dai *dai)
869 struct snd_soc_pcm_runtime *rtd = substream->private_data;
872 /* perform any topology hw_params fixups before DAI */
873 if (rtd->dai_link->be_hw_params_fixup) {
874 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
877 "ASoC: hw_params topology fixup failed %d\n",
883 if (dai->driver->ops->hw_params) {
884 ret = dai->driver->ops->hw_params(substream, params, dai);
886 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
895 static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
896 struct snd_soc_component *last)
898 struct snd_soc_pcm_runtime *rtd = substream->private_data;
899 struct snd_soc_rtdcom_list *rtdcom;
900 struct snd_soc_component *component;
902 for_each_rtdcom(rtd, rtdcom) {
903 component = rtdcom->component;
905 if (component == last)
908 if (!component->driver->ops ||
909 !component->driver->ops->hw_free)
912 component->driver->ops->hw_free(substream);
919 * Called by ALSA when the hardware params are set by application. This
920 * function can also be called multiple times and can allocate buffers
921 * (using snd_pcm_lib_* ). It's non-atomic.
923 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
924 struct snd_pcm_hw_params *params)
926 struct snd_soc_pcm_runtime *rtd = substream->private_data;
927 struct snd_soc_component *component;
928 struct snd_soc_rtdcom_list *rtdcom;
929 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
930 struct snd_soc_dai *codec_dai;
933 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
934 if (rtd->dai_link->ops->hw_params) {
935 ret = rtd->dai_link->ops->hw_params(substream, params);
937 dev_err(rtd->card->dev, "ASoC: machine hw_params"
938 " failed: %d\n", ret);
943 for_each_rtd_codec_dai(rtd, i, codec_dai) {
944 struct snd_pcm_hw_params codec_params;
947 * Skip CODECs which don't support the current stream type,
948 * the idea being that if a CODEC is not used for the currently
949 * set up transfer direction, it should not need to be
950 * configured, especially since the configuration used might
951 * not even be supported by that CODEC. There may be cases
952 * however where a CODEC needs to be set up although it is
953 * actually not being used for the transfer, e.g. if a
954 * capture-only CODEC is acting as an LRCLK and/or BCLK master
955 * for the DAI link including a playback-only CODEC.
956 * If this becomes necessary, we will have to augment the
957 * machine driver setup with information on how to act, so
958 * we can do the right thing here.
960 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
963 /* copy params for each codec */
964 codec_params = *params;
966 /* fixup params based on TDM slot masks */
967 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
969 soc_pcm_codec_params_fixup(&codec_params,
972 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
974 soc_pcm_codec_params_fixup(&codec_params,
977 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
981 codec_dai->rate = params_rate(&codec_params);
982 codec_dai->channels = params_channels(&codec_params);
983 codec_dai->sample_bits = snd_pcm_format_physical_width(
984 params_format(&codec_params));
986 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
989 ret = soc_dai_hw_params(substream, params, cpu_dai);
993 for_each_rtdcom(rtd, rtdcom) {
994 component = rtdcom->component;
996 if (!component->driver->ops ||
997 !component->driver->ops->hw_params)
1000 ret = component->driver->ops->hw_params(substream, params);
1002 dev_err(component->dev,
1003 "ASoC: %s hw params failed: %d\n",
1004 component->name, ret);
1010 /* store the parameters for each DAIs */
1011 cpu_dai->rate = params_rate(params);
1012 cpu_dai->channels = params_channels(params);
1013 cpu_dai->sample_bits =
1014 snd_pcm_format_physical_width(params_format(params));
1016 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1018 ret = soc_pcm_params_symmetry(substream, params);
1022 mutex_unlock(&rtd->pcm_mutex);
1026 soc_pcm_components_hw_free(substream, component);
1028 if (cpu_dai->driver->ops->hw_free)
1029 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1032 i = rtd->num_codecs;
1035 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1036 if (codec_dai->driver->ops->hw_free)
1037 codec_dai->driver->ops->hw_free(substream, codec_dai);
1038 codec_dai->rate = 0;
1041 if (rtd->dai_link->ops->hw_free)
1042 rtd->dai_link->ops->hw_free(substream);
1044 mutex_unlock(&rtd->pcm_mutex);
1049 * Frees resources allocated by hw_params, can be called multiple times
1051 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1053 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1054 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1055 struct snd_soc_dai *codec_dai;
1056 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1059 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1061 /* clear the corresponding DAIs parameters when going to be inactive */
1062 if (cpu_dai->active == 1) {
1064 cpu_dai->channels = 0;
1065 cpu_dai->sample_bits = 0;
1068 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1069 if (codec_dai->active == 1) {
1070 codec_dai->rate = 0;
1071 codec_dai->channels = 0;
1072 codec_dai->sample_bits = 0;
1076 /* apply codec digital mute */
1077 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1078 if ((playback && codec_dai->playback_active == 1) ||
1079 (!playback && codec_dai->capture_active == 1))
1080 snd_soc_dai_digital_mute(codec_dai, 1,
1084 /* free any machine hw params */
1085 if (rtd->dai_link->ops->hw_free)
1086 rtd->dai_link->ops->hw_free(substream);
1088 /* free any component resources */
1089 soc_pcm_components_hw_free(substream, NULL);
1091 /* now free hw params for the DAIs */
1092 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1093 if (codec_dai->driver->ops->hw_free)
1094 codec_dai->driver->ops->hw_free(substream, codec_dai);
1097 if (cpu_dai->driver->ops->hw_free)
1098 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1100 mutex_unlock(&rtd->pcm_mutex);
1104 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1106 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1107 struct snd_soc_component *component;
1108 struct snd_soc_rtdcom_list *rtdcom;
1109 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1110 struct snd_soc_dai *codec_dai;
1113 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1114 if (codec_dai->driver->ops->trigger) {
1115 ret = codec_dai->driver->ops->trigger(substream,
1122 for_each_rtdcom(rtd, rtdcom) {
1123 component = rtdcom->component;
1125 if (!component->driver->ops ||
1126 !component->driver->ops->trigger)
1129 ret = component->driver->ops->trigger(substream, cmd);
1134 if (cpu_dai->driver->ops->trigger) {
1135 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1140 if (rtd->dai_link->ops->trigger) {
1141 ret = rtd->dai_link->ops->trigger(substream, cmd);
1149 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1152 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1153 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1154 struct snd_soc_dai *codec_dai;
1157 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1158 if (codec_dai->driver->ops->bespoke_trigger) {
1159 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1166 if (cpu_dai->driver->ops->bespoke_trigger) {
1167 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1174 * soc level wrapper for pointer callback
1175 * If cpu_dai, codec_dai, component driver has the delay callback, then
1176 * the runtime->delay will be updated accordingly.
1178 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1180 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1181 struct snd_soc_component *component;
1182 struct snd_soc_rtdcom_list *rtdcom;
1183 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1184 struct snd_soc_dai *codec_dai;
1185 struct snd_pcm_runtime *runtime = substream->runtime;
1186 snd_pcm_uframes_t offset = 0;
1187 snd_pcm_sframes_t delay = 0;
1188 snd_pcm_sframes_t codec_delay = 0;
1191 /* clearing the previous total delay */
1194 for_each_rtdcom(rtd, rtdcom) {
1195 component = rtdcom->component;
1197 if (!component->driver->ops ||
1198 !component->driver->ops->pointer)
1201 /* FIXME: use 1st pointer */
1202 offset = component->driver->ops->pointer(substream);
1205 /* base delay if assigned in pointer callback */
1206 delay = runtime->delay;
1208 if (cpu_dai->driver->ops->delay)
1209 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1211 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1212 if (codec_dai->driver->ops->delay)
1213 codec_delay = max(codec_delay,
1214 codec_dai->driver->ops->delay(substream,
1217 delay += codec_delay;
1219 runtime->delay = delay;
1224 /* connect a FE and BE */
1225 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1226 struct snd_soc_pcm_runtime *be, int stream)
1228 struct snd_soc_dpcm *dpcm;
1229 unsigned long flags;
1231 /* only add new dpcms */
1232 for_each_dpcm_be(fe, stream, dpcm) {
1233 if (dpcm->be == be && dpcm->fe == fe)
1237 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1243 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1244 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1245 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1246 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1247 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1248 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1250 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1251 stream ? "capture" : "playback", fe->dai_link->name,
1252 stream ? "<-" : "->", be->dai_link->name);
1254 #ifdef CONFIG_DEBUG_FS
1255 if (fe->debugfs_dpcm_root)
1256 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1257 fe->debugfs_dpcm_root, &dpcm->state);
1262 /* reparent a BE onto another FE */
1263 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1264 struct snd_soc_pcm_runtime *be, int stream)
1266 struct snd_soc_dpcm *dpcm;
1267 struct snd_pcm_substream *fe_substream, *be_substream;
1269 /* reparent if BE is connected to other FEs */
1270 if (!be->dpcm[stream].users)
1273 be_substream = snd_soc_dpcm_get_substream(be, stream);
1275 for_each_dpcm_fe(be, stream, dpcm) {
1279 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1280 stream ? "capture" : "playback",
1281 dpcm->fe->dai_link->name,
1282 stream ? "<-" : "->", dpcm->be->dai_link->name);
1284 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1285 be_substream->runtime = fe_substream->runtime;
1290 /* disconnect a BE and FE */
1291 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1293 struct snd_soc_dpcm *dpcm, *d;
1294 unsigned long flags;
1296 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1297 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1298 stream ? "capture" : "playback",
1299 dpcm->be->dai_link->name);
1301 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1304 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1305 stream ? "capture" : "playback", fe->dai_link->name,
1306 stream ? "<-" : "->", dpcm->be->dai_link->name);
1308 /* BEs still alive need new FE */
1309 dpcm_be_reparent(fe, dpcm->be, stream);
1311 #ifdef CONFIG_DEBUG_FS
1312 debugfs_remove(dpcm->debugfs_state);
1314 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1315 list_del(&dpcm->list_be);
1316 list_del(&dpcm->list_fe);
1317 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1322 /* get BE for DAI widget and stream */
1323 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1324 struct snd_soc_dapm_widget *widget, int stream)
1326 struct snd_soc_pcm_runtime *be;
1327 struct snd_soc_dai *dai;
1330 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1332 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1333 for_each_card_rtds(card, be) {
1335 if (!be->dai_link->no_pcm)
1338 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1339 be->cpu_dai->playback_widget ?
1340 be->cpu_dai->playback_widget->name : "(not set)");
1342 if (be->cpu_dai->playback_widget == widget)
1345 for_each_rtd_codec_dai(be, i, dai) {
1346 if (dai->playback_widget == widget)
1352 for_each_card_rtds(card, be) {
1354 if (!be->dai_link->no_pcm)
1357 dev_dbg(card->dev, "ASoC: try BE %s\n",
1358 be->cpu_dai->capture_widget ?
1359 be->cpu_dai->capture_widget->name : "(not set)");
1361 if (be->cpu_dai->capture_widget == widget)
1364 for_each_rtd_codec_dai(be, i, dai) {
1365 if (dai->capture_widget == widget)
1371 /* dai link name and stream name set correctly ? */
1372 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1373 stream ? "capture" : "playback", widget->name);
1377 static inline struct snd_soc_dapm_widget *
1378 dai_get_widget(struct snd_soc_dai *dai, int stream)
1380 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1381 return dai->playback_widget;
1383 return dai->capture_widget;
1386 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1387 struct snd_soc_dapm_widget *widget)
1391 for (i = 0; i < list->num_widgets; i++) {
1392 if (widget == list->widgets[i])
1399 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1400 enum snd_soc_dapm_direction dir)
1402 struct snd_soc_card *card = widget->dapm->card;
1403 struct snd_soc_pcm_runtime *rtd;
1404 struct snd_soc_dai *dai;
1407 if (dir == SND_SOC_DAPM_DIR_OUT) {
1408 for_each_card_rtds(card, rtd) {
1409 if (!rtd->dai_link->no_pcm)
1412 if (rtd->cpu_dai->playback_widget == widget)
1415 for_each_rtd_codec_dai(rtd, i, dai) {
1416 if (dai->playback_widget == widget)
1420 } else { /* SND_SOC_DAPM_DIR_IN */
1421 for_each_card_rtds(card, rtd) {
1422 if (!rtd->dai_link->no_pcm)
1425 if (rtd->cpu_dai->capture_widget == widget)
1428 for_each_rtd_codec_dai(rtd, i, dai) {
1429 if (dai->capture_widget == widget)
1438 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1439 int stream, struct snd_soc_dapm_widget_list **list)
1441 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1444 /* get number of valid DAI paths and their widgets */
1445 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1446 dpcm_end_walk_at_be);
1448 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1449 stream ? "capture" : "playback");
1454 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1455 struct snd_soc_dapm_widget_list **list_)
1457 struct snd_soc_dpcm *dpcm;
1458 struct snd_soc_dapm_widget_list *list = *list_;
1459 struct snd_soc_dapm_widget *widget;
1460 struct snd_soc_dai *dai;
1463 /* Destroy any old FE <--> BE connections */
1464 for_each_dpcm_be(fe, stream, dpcm) {
1467 /* is there a valid CPU DAI widget for this BE */
1468 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1470 /* prune the BE if it's no longer in our active list */
1471 if (widget && widget_in_list(list, widget))
1474 /* is there a valid CODEC DAI widget for this BE */
1475 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1476 widget = dai_get_widget(dai, stream);
1478 /* prune the BE if it's no longer in our active list */
1479 if (widget && widget_in_list(list, widget))
1483 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1484 stream ? "capture" : "playback",
1485 dpcm->be->dai_link->name, fe->dai_link->name);
1486 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1487 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1491 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1495 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1496 struct snd_soc_dapm_widget_list **list_)
1498 struct snd_soc_card *card = fe->card;
1499 struct snd_soc_dapm_widget_list *list = *list_;
1500 struct snd_soc_pcm_runtime *be;
1501 int i, new = 0, err;
1503 /* Create any new FE <--> BE connections */
1504 for (i = 0; i < list->num_widgets; i++) {
1506 switch (list->widgets[i]->id) {
1507 case snd_soc_dapm_dai_in:
1508 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1511 case snd_soc_dapm_dai_out:
1512 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1519 /* is there a valid BE rtd for this widget */
1520 be = dpcm_get_be(card, list->widgets[i], stream);
1522 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1523 list->widgets[i]->name);
1527 /* make sure BE is a real BE */
1528 if (!be->dai_link->no_pcm)
1531 /* don't connect if FE is not running */
1532 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1535 /* newly connected FE and BE */
1536 err = dpcm_be_connect(fe, be, stream);
1538 dev_err(fe->dev, "ASoC: can't connect %s\n",
1539 list->widgets[i]->name);
1541 } else if (err == 0) /* already connected */
1545 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1549 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1554 * Find the corresponding BE DAIs that source or sink audio to this
1557 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1558 int stream, struct snd_soc_dapm_widget_list **list, int new)
1561 return dpcm_add_paths(fe, stream, list);
1563 return dpcm_prune_paths(fe, stream, list);
1566 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1568 struct snd_soc_dpcm *dpcm;
1569 unsigned long flags;
1571 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1572 for_each_dpcm_be(fe, stream, dpcm)
1573 dpcm->be->dpcm[stream].runtime_update =
1574 SND_SOC_DPCM_UPDATE_NO;
1575 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1578 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1581 struct snd_soc_dpcm *dpcm;
1583 /* disable any enabled and non active backends */
1584 for_each_dpcm_be(fe, stream, dpcm) {
1586 struct snd_soc_pcm_runtime *be = dpcm->be;
1587 struct snd_pcm_substream *be_substream =
1588 snd_soc_dpcm_get_substream(be, stream);
1590 if (be->dpcm[stream].users == 0)
1591 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1592 stream ? "capture" : "playback",
1593 be->dpcm[stream].state);
1595 if (--be->dpcm[stream].users != 0)
1598 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1601 soc_pcm_close(be_substream);
1602 be_substream->runtime = NULL;
1603 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1607 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1609 struct snd_soc_dpcm *dpcm;
1612 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1613 for_each_dpcm_be(fe, stream, dpcm) {
1615 struct snd_soc_pcm_runtime *be = dpcm->be;
1616 struct snd_pcm_substream *be_substream =
1617 snd_soc_dpcm_get_substream(be, stream);
1619 if (!be_substream) {
1620 dev_err(be->dev, "ASoC: no backend %s stream\n",
1621 stream ? "capture" : "playback");
1625 /* is this op for this BE ? */
1626 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1629 /* first time the dpcm is open ? */
1630 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1631 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1632 stream ? "capture" : "playback",
1633 be->dpcm[stream].state);
1635 if (be->dpcm[stream].users++ != 0)
1638 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1639 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1642 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1643 stream ? "capture" : "playback", be->dai_link->name);
1645 be_substream->runtime = be->dpcm[stream].runtime;
1646 err = soc_pcm_open(be_substream);
1648 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1649 be->dpcm[stream].users--;
1650 if (be->dpcm[stream].users < 0)
1651 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1652 stream ? "capture" : "playback",
1653 be->dpcm[stream].state);
1655 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1659 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1666 /* disable any enabled and non active backends */
1667 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1668 struct snd_soc_pcm_runtime *be = dpcm->be;
1669 struct snd_pcm_substream *be_substream =
1670 snd_soc_dpcm_get_substream(be, stream);
1672 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1675 if (be->dpcm[stream].users == 0)
1676 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1677 stream ? "capture" : "playback",
1678 be->dpcm[stream].state);
1680 if (--be->dpcm[stream].users != 0)
1683 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1686 soc_pcm_close(be_substream);
1687 be_substream->runtime = NULL;
1688 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1694 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1695 struct snd_soc_pcm_stream *stream)
1697 runtime->hw.rate_min = stream->rate_min;
1698 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1699 runtime->hw.channels_min = stream->channels_min;
1700 runtime->hw.channels_max = stream->channels_max;
1701 if (runtime->hw.formats)
1702 runtime->hw.formats &= stream->formats;
1704 runtime->hw.formats = stream->formats;
1705 runtime->hw.rates = stream->rates;
1708 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1711 struct snd_soc_pcm_runtime *fe = substream->private_data;
1712 struct snd_soc_dpcm *dpcm;
1713 struct snd_soc_dai *dai;
1714 int stream = substream->stream;
1716 if (!fe->dai_link->dpcm_merged_format)
1720 * It returns merged BE codec format
1721 * if FE want to use it (= dpcm_merged_format)
1724 for_each_dpcm_be(fe, stream, dpcm) {
1725 struct snd_soc_pcm_runtime *be = dpcm->be;
1726 struct snd_soc_dai_driver *codec_dai_drv;
1727 struct snd_soc_pcm_stream *codec_stream;
1730 for_each_rtd_codec_dai(be, i, dai) {
1732 * Skip CODECs which don't support the current stream
1733 * type. See soc_pcm_init_runtime_hw() for more details
1735 if (!snd_soc_dai_stream_valid(dai, stream))
1738 codec_dai_drv = dai->driver;
1739 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1740 codec_stream = &codec_dai_drv->playback;
1742 codec_stream = &codec_dai_drv->capture;
1744 *formats &= codec_stream->formats;
1749 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1750 unsigned int *channels_min,
1751 unsigned int *channels_max)
1753 struct snd_soc_pcm_runtime *fe = substream->private_data;
1754 struct snd_soc_dpcm *dpcm;
1755 int stream = substream->stream;
1757 if (!fe->dai_link->dpcm_merged_chan)
1761 * It returns merged BE codec channel;
1762 * if FE want to use it (= dpcm_merged_chan)
1765 for_each_dpcm_be(fe, stream, dpcm) {
1766 struct snd_soc_pcm_runtime *be = dpcm->be;
1767 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1768 struct snd_soc_dai_driver *codec_dai_drv;
1769 struct snd_soc_pcm_stream *codec_stream;
1770 struct snd_soc_pcm_stream *cpu_stream;
1772 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1773 cpu_stream = &cpu_dai_drv->playback;
1775 cpu_stream = &cpu_dai_drv->capture;
1777 *channels_min = max(*channels_min, cpu_stream->channels_min);
1778 *channels_max = min(*channels_max, cpu_stream->channels_max);
1781 * chan min/max cannot be enforced if there are multiple CODEC
1782 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1784 if (be->num_codecs == 1) {
1785 codec_dai_drv = be->codec_dais[0]->driver;
1787 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1788 codec_stream = &codec_dai_drv->playback;
1790 codec_stream = &codec_dai_drv->capture;
1792 *channels_min = max(*channels_min,
1793 codec_stream->channels_min);
1794 *channels_max = min(*channels_max,
1795 codec_stream->channels_max);
1800 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1801 unsigned int *rates,
1802 unsigned int *rate_min,
1803 unsigned int *rate_max)
1805 struct snd_soc_pcm_runtime *fe = substream->private_data;
1806 struct snd_soc_dpcm *dpcm;
1807 int stream = substream->stream;
1809 if (!fe->dai_link->dpcm_merged_rate)
1813 * It returns merged BE codec channel;
1814 * if FE want to use it (= dpcm_merged_chan)
1817 for_each_dpcm_be(fe, stream, dpcm) {
1818 struct snd_soc_pcm_runtime *be = dpcm->be;
1819 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1820 struct snd_soc_dai_driver *codec_dai_drv;
1821 struct snd_soc_pcm_stream *codec_stream;
1822 struct snd_soc_pcm_stream *cpu_stream;
1823 struct snd_soc_dai *dai;
1826 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1827 cpu_stream = &cpu_dai_drv->playback;
1829 cpu_stream = &cpu_dai_drv->capture;
1831 *rate_min = max(*rate_min, cpu_stream->rate_min);
1832 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1833 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1835 for_each_rtd_codec_dai(be, i, dai) {
1837 * Skip CODECs which don't support the current stream
1838 * type. See soc_pcm_init_runtime_hw() for more details
1840 if (!snd_soc_dai_stream_valid(dai, stream))
1843 codec_dai_drv = dai->driver;
1844 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1845 codec_stream = &codec_dai_drv->playback;
1847 codec_stream = &codec_dai_drv->capture;
1849 *rate_min = max(*rate_min, codec_stream->rate_min);
1850 *rate_max = min_not_zero(*rate_max,
1851 codec_stream->rate_max);
1852 *rates = snd_pcm_rate_mask_intersect(*rates,
1853 codec_stream->rates);
1858 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1860 struct snd_pcm_runtime *runtime = substream->runtime;
1861 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1862 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1863 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1865 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1866 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1868 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1870 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1871 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1872 &runtime->hw.channels_max);
1873 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1874 &runtime->hw.rate_min, &runtime->hw.rate_max);
1877 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1879 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1880 * for avoiding the race with trigger callback.
1881 * If the state is unset and a trigger is pending while the previous operation,
1882 * process the pending trigger action here.
1884 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1885 int stream, enum snd_soc_dpcm_update state)
1887 struct snd_pcm_substream *substream =
1888 snd_soc_dpcm_get_substream(fe, stream);
1890 snd_pcm_stream_lock_irq(substream);
1891 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1892 dpcm_fe_dai_do_trigger(substream,
1893 fe->dpcm[stream].trigger_pending - 1);
1894 fe->dpcm[stream].trigger_pending = 0;
1896 fe->dpcm[stream].runtime_update = state;
1897 snd_pcm_stream_unlock_irq(substream);
1900 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1903 struct snd_soc_dpcm *dpcm;
1904 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1905 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1908 /* apply symmetry for FE */
1909 if (soc_pcm_has_symmetry(fe_substream))
1910 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1912 /* Symmetry only applies if we've got an active stream. */
1913 if (fe_cpu_dai->active) {
1914 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1919 /* apply symmetry for BE */
1920 for_each_dpcm_be(fe, stream, dpcm) {
1921 struct snd_soc_pcm_runtime *be = dpcm->be;
1922 struct snd_pcm_substream *be_substream =
1923 snd_soc_dpcm_get_substream(be, stream);
1924 struct snd_soc_pcm_runtime *rtd;
1925 struct snd_soc_dai *codec_dai;
1928 /* A backend may not have the requested substream */
1932 rtd = be_substream->private_data;
1933 if (rtd->dai_link->be_hw_params_fixup)
1936 if (soc_pcm_has_symmetry(be_substream))
1937 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1939 /* Symmetry only applies if we've got an active stream. */
1940 if (rtd->cpu_dai->active) {
1941 err = soc_pcm_apply_symmetry(fe_substream,
1947 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1948 if (codec_dai->active) {
1949 err = soc_pcm_apply_symmetry(fe_substream,
1960 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1962 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1963 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1964 int stream = fe_substream->stream, ret = 0;
1966 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1968 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1970 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1974 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1976 /* start the DAI frontend */
1977 ret = soc_pcm_open(fe_substream);
1979 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1983 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1985 dpcm_set_fe_runtime(fe_substream);
1986 snd_pcm_limit_hw_rates(runtime);
1988 ret = dpcm_apply_symmetry(fe_substream, stream);
1990 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1995 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1999 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
2001 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2005 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2007 struct snd_soc_dpcm *dpcm;
2009 /* only shutdown BEs that are either sinks or sources to this FE DAI */
2010 for_each_dpcm_be(fe, stream, dpcm) {
2012 struct snd_soc_pcm_runtime *be = dpcm->be;
2013 struct snd_pcm_substream *be_substream =
2014 snd_soc_dpcm_get_substream(be, stream);
2016 /* is this op for this BE ? */
2017 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2020 if (be->dpcm[stream].users == 0)
2021 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2022 stream ? "capture" : "playback",
2023 be->dpcm[stream].state);
2025 if (--be->dpcm[stream].users != 0)
2028 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2029 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2030 soc_pcm_hw_free(be_substream);
2031 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2034 dev_dbg(be->dev, "ASoC: close BE %s\n",
2035 be->dai_link->name);
2037 soc_pcm_close(be_substream);
2038 be_substream->runtime = NULL;
2040 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2045 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2047 struct snd_soc_pcm_runtime *fe = substream->private_data;
2048 int stream = substream->stream;
2050 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2052 /* shutdown the BEs */
2053 dpcm_be_dai_shutdown(fe, substream->stream);
2055 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2057 /* now shutdown the frontend */
2058 soc_pcm_close(substream);
2060 /* run the stream event for each BE */
2061 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2063 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2064 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2068 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2070 struct snd_soc_dpcm *dpcm;
2072 /* only hw_params backends that are either sinks or sources
2073 * to this frontend DAI */
2074 for_each_dpcm_be(fe, stream, dpcm) {
2076 struct snd_soc_pcm_runtime *be = dpcm->be;
2077 struct snd_pcm_substream *be_substream =
2078 snd_soc_dpcm_get_substream(be, stream);
2080 /* is this op for this BE ? */
2081 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2084 /* only free hw when no longer used - check all FEs */
2085 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2088 /* do not free hw if this BE is used by other FE */
2089 if (be->dpcm[stream].users > 1)
2092 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2093 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2094 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2095 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2096 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2097 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2100 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2101 be->dai_link->name);
2103 soc_pcm_hw_free(be_substream);
2105 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2111 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2113 struct snd_soc_pcm_runtime *fe = substream->private_data;
2114 int err, stream = substream->stream;
2116 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2117 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2119 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2121 /* call hw_free on the frontend */
2122 err = soc_pcm_hw_free(substream);
2124 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2125 fe->dai_link->name);
2127 /* only hw_params backends that are either sinks or sources
2128 * to this frontend DAI */
2129 err = dpcm_be_dai_hw_free(fe, stream);
2131 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2132 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2134 mutex_unlock(&fe->card->mutex);
2138 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2140 struct snd_soc_dpcm *dpcm;
2143 for_each_dpcm_be(fe, stream, dpcm) {
2145 struct snd_soc_pcm_runtime *be = dpcm->be;
2146 struct snd_pcm_substream *be_substream =
2147 snd_soc_dpcm_get_substream(be, stream);
2149 /* is this op for this BE ? */
2150 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2153 /* copy params for each dpcm */
2154 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2155 sizeof(struct snd_pcm_hw_params));
2157 /* perform any hw_params fixups */
2158 if (be->dai_link->be_hw_params_fixup) {
2159 ret = be->dai_link->be_hw_params_fixup(be,
2163 "ASoC: hw_params BE fixup failed %d\n",
2169 /* only allow hw_params() if no connected FEs are running */
2170 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2173 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2174 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2175 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2178 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2179 be->dai_link->name);
2181 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2183 dev_err(dpcm->be->dev,
2184 "ASoC: hw_params BE failed %d\n", ret);
2188 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2193 /* disable any enabled and non active backends */
2194 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2195 struct snd_soc_pcm_runtime *be = dpcm->be;
2196 struct snd_pcm_substream *be_substream =
2197 snd_soc_dpcm_get_substream(be, stream);
2199 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2202 /* only allow hw_free() if no connected FEs are running */
2203 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2206 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2207 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2208 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2209 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2212 soc_pcm_hw_free(be_substream);
2218 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2219 struct snd_pcm_hw_params *params)
2221 struct snd_soc_pcm_runtime *fe = substream->private_data;
2222 int ret, stream = substream->stream;
2224 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2225 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2227 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2228 sizeof(struct snd_pcm_hw_params));
2229 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2231 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2235 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2236 fe->dai_link->name, params_rate(params),
2237 params_channels(params), params_format(params));
2239 /* call hw_params on the frontend */
2240 ret = soc_pcm_hw_params(substream, params);
2242 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2243 dpcm_be_dai_hw_free(fe, stream);
2245 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2248 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2249 mutex_unlock(&fe->card->mutex);
2253 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2254 struct snd_pcm_substream *substream, int cmd)
2258 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2259 dpcm->be->dai_link->name, cmd);
2261 ret = soc_pcm_trigger(substream, cmd);
2263 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2268 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2271 struct snd_soc_dpcm *dpcm;
2274 for_each_dpcm_be(fe, stream, dpcm) {
2276 struct snd_soc_pcm_runtime *be = dpcm->be;
2277 struct snd_pcm_substream *be_substream =
2278 snd_soc_dpcm_get_substream(be, stream);
2280 /* is this op for this BE ? */
2281 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2285 case SNDRV_PCM_TRIGGER_START:
2286 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2287 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2290 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2294 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2296 case SNDRV_PCM_TRIGGER_RESUME:
2297 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2300 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2304 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2306 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2307 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2310 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2314 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2316 case SNDRV_PCM_TRIGGER_STOP:
2317 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2320 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2323 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2327 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2329 case SNDRV_PCM_TRIGGER_SUSPEND:
2330 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2333 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2336 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2340 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2342 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
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_PAUSED;
2360 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2362 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2364 struct snd_soc_pcm_runtime *fe = substream->private_data;
2365 int stream = substream->stream, ret;
2366 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2368 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2371 case SND_SOC_DPCM_TRIGGER_PRE:
2372 /* call trigger on the frontend before the backend. */
2374 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2375 fe->dai_link->name, cmd);
2377 ret = soc_pcm_trigger(substream, cmd);
2379 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2383 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2385 case SND_SOC_DPCM_TRIGGER_POST:
2386 /* call trigger on the frontend after the backend. */
2388 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2390 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2394 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2395 fe->dai_link->name, cmd);
2397 ret = soc_pcm_trigger(substream, cmd);
2399 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2400 /* bespoke trigger() - handles both FE and BEs */
2402 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2403 fe->dai_link->name, cmd);
2405 ret = soc_pcm_bespoke_trigger(substream, cmd);
2407 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2412 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2413 fe->dai_link->name);
2419 case SNDRV_PCM_TRIGGER_START:
2420 case SNDRV_PCM_TRIGGER_RESUME:
2421 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2422 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2424 case SNDRV_PCM_TRIGGER_STOP:
2425 case SNDRV_PCM_TRIGGER_SUSPEND:
2426 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2428 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2429 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2434 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2438 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2440 struct snd_soc_pcm_runtime *fe = substream->private_data;
2441 int stream = substream->stream;
2443 /* if FE's runtime_update is already set, we're in race;
2444 * process this trigger later at exit
2446 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2447 fe->dpcm[stream].trigger_pending = cmd + 1;
2448 return 0; /* delayed, assuming it's successful */
2451 /* we're alone, let's trigger */
2452 return dpcm_fe_dai_do_trigger(substream, cmd);
2455 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2457 struct snd_soc_dpcm *dpcm;
2460 for_each_dpcm_be(fe, stream, dpcm) {
2462 struct snd_soc_pcm_runtime *be = dpcm->be;
2463 struct snd_pcm_substream *be_substream =
2464 snd_soc_dpcm_get_substream(be, stream);
2466 /* is this op for this BE ? */
2467 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2470 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2471 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2472 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2475 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2476 be->dai_link->name);
2478 ret = soc_pcm_prepare(be_substream);
2480 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2485 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2490 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2492 struct snd_soc_pcm_runtime *fe = substream->private_data;
2493 int stream = substream->stream, ret = 0;
2495 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2497 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2499 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2501 /* there is no point preparing this FE if there are no BEs */
2502 if (list_empty(&fe->dpcm[stream].be_clients)) {
2503 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2504 fe->dai_link->name);
2509 ret = dpcm_be_dai_prepare(fe, substream->stream);
2513 /* call prepare on the frontend */
2514 ret = soc_pcm_prepare(substream);
2516 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2517 fe->dai_link->name);
2521 /* run the stream event for each BE */
2522 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2523 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2526 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2527 mutex_unlock(&fe->card->mutex);
2532 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2533 unsigned int cmd, void *arg)
2535 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2536 struct snd_soc_component *component;
2537 struct snd_soc_rtdcom_list *rtdcom;
2539 for_each_rtdcom(rtd, rtdcom) {
2540 component = rtdcom->component;
2542 if (!component->driver->ops ||
2543 !component->driver->ops->ioctl)
2546 /* FIXME: use 1st ioctl */
2547 return component->driver->ops->ioctl(substream, cmd, arg);
2550 return snd_pcm_lib_ioctl(substream, cmd, arg);
2553 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2555 struct snd_pcm_substream *substream =
2556 snd_soc_dpcm_get_substream(fe, stream);
2557 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2560 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2561 stream ? "capture" : "playback", fe->dai_link->name);
2563 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2564 /* call bespoke trigger - FE takes care of all BE triggers */
2565 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2566 fe->dai_link->name);
2568 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2570 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2572 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2573 fe->dai_link->name);
2575 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2577 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2580 err = dpcm_be_dai_hw_free(fe, stream);
2582 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2584 err = dpcm_be_dai_shutdown(fe, stream);
2586 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2588 /* run the stream event for each BE */
2589 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2594 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2596 struct snd_pcm_substream *substream =
2597 snd_soc_dpcm_get_substream(fe, stream);
2598 struct snd_soc_dpcm *dpcm;
2599 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2601 unsigned long flags;
2603 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2604 stream ? "capture" : "playback", fe->dai_link->name);
2606 /* Only start the BE if the FE is ready */
2607 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2608 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2611 /* startup must always be called for new BEs */
2612 ret = dpcm_be_dai_startup(fe, stream);
2616 /* keep going if FE state is > open */
2617 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2620 ret = dpcm_be_dai_hw_params(fe, stream);
2624 /* keep going if FE state is > hw_params */
2625 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2629 ret = dpcm_be_dai_prepare(fe, stream);
2633 /* run the stream event for each BE */
2634 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2636 /* keep going if FE state is > prepare */
2637 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2638 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2641 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2642 /* call trigger on the frontend - FE takes care of all BE triggers */
2643 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2644 fe->dai_link->name);
2646 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2648 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2652 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2653 fe->dai_link->name);
2655 ret = dpcm_be_dai_trigger(fe, stream,
2656 SNDRV_PCM_TRIGGER_START);
2658 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2666 dpcm_be_dai_hw_free(fe, stream);
2668 dpcm_be_dai_shutdown(fe, stream);
2670 /* disconnect any non started BEs */
2671 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2672 for_each_dpcm_be(fe, stream, dpcm) {
2673 struct snd_soc_pcm_runtime *be = dpcm->be;
2674 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2675 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2677 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2682 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2686 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2687 ret = dpcm_run_update_startup(fe, stream);
2689 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2690 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2695 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2699 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2700 ret = dpcm_run_update_shutdown(fe, stream);
2702 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2703 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2708 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2710 struct snd_soc_dapm_widget_list *list;
2713 if (!fe->dai_link->dynamic)
2716 /* only check active links */
2717 if (!fe->cpu_dai->active)
2720 /* DAPM sync will call this to update DSP paths */
2721 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2722 new ? "new" : "old", fe->dai_link->name);
2724 /* skip if FE doesn't have playback capability */
2725 if (!fe->cpu_dai->driver->playback.channels_min ||
2726 !fe->codec_dai->driver->playback.channels_min)
2729 /* skip if FE isn't currently playing */
2730 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2733 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2735 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2736 fe->dai_link->name, "playback");
2740 /* update any playback paths */
2741 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2744 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2746 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2748 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2749 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2752 dpcm_path_put(&list);
2755 /* skip if FE doesn't have capture capability */
2756 if (!fe->cpu_dai->driver->capture.channels_min ||
2757 !fe->codec_dai->driver->capture.channels_min)
2760 /* skip if FE isn't currently capturing */
2761 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2764 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2766 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2767 fe->dai_link->name, "capture");
2771 /* update any old capture paths */
2772 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2775 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2777 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2779 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2780 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2783 dpcm_path_put(&list);
2788 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2791 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2793 struct snd_soc_pcm_runtime *fe;
2796 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2797 /* shutdown all old paths first */
2798 for_each_card_rtds(card, fe) {
2799 ret = soc_dpcm_fe_runtime_update(fe, 0);
2804 /* bring new paths up */
2805 for_each_card_rtds(card, fe) {
2806 ret = soc_dpcm_fe_runtime_update(fe, 1);
2812 mutex_unlock(&card->mutex);
2815 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2817 struct snd_soc_dpcm *dpcm;
2818 struct snd_soc_dai *dai;
2820 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2822 struct snd_soc_pcm_runtime *be = dpcm->be;
2825 if (be->dai_link->ignore_suspend)
2828 for_each_rtd_codec_dai(be, i, dai) {
2829 struct snd_soc_dai_driver *drv = dai->driver;
2831 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2832 be->dai_link->name);
2834 if (drv->ops && drv->ops->digital_mute &&
2835 dai->playback_active)
2836 drv->ops->digital_mute(dai, mute);
2843 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2845 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2846 struct snd_soc_dpcm *dpcm;
2847 struct snd_soc_dapm_widget_list *list;
2849 int stream = fe_substream->stream;
2851 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2852 fe->dpcm[stream].runtime = fe_substream->runtime;
2854 ret = dpcm_path_get(fe, stream, &list);
2856 mutex_unlock(&fe->card->mutex);
2858 } else if (ret == 0) {
2859 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2860 fe->dai_link->name, stream ? "capture" : "playback");
2863 /* calculate valid and active FE <-> BE dpcms */
2864 dpcm_process_paths(fe, stream, &list, 1);
2866 ret = dpcm_fe_dai_startup(fe_substream);
2868 /* clean up all links */
2869 for_each_dpcm_be(fe, stream, dpcm)
2870 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2872 dpcm_be_disconnect(fe, stream);
2873 fe->dpcm[stream].runtime = NULL;
2876 dpcm_clear_pending_state(fe, stream);
2877 dpcm_path_put(&list);
2878 mutex_unlock(&fe->card->mutex);
2882 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2884 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2885 struct snd_soc_dpcm *dpcm;
2886 int stream = fe_substream->stream, ret;
2888 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2889 ret = dpcm_fe_dai_shutdown(fe_substream);
2891 /* mark FE's links ready to prune */
2892 for_each_dpcm_be(fe, stream, dpcm)
2893 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2895 dpcm_be_disconnect(fe, stream);
2897 fe->dpcm[stream].runtime = NULL;
2898 mutex_unlock(&fe->card->mutex);
2902 static void soc_pcm_private_free(struct snd_pcm *pcm)
2904 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2905 struct snd_soc_rtdcom_list *rtdcom;
2906 struct snd_soc_component *component;
2908 /* need to sync the delayed work before releasing resources */
2909 flush_delayed_work(&rtd->delayed_work);
2910 for_each_rtdcom(rtd, rtdcom) {
2911 component = rtdcom->component;
2913 if (component->driver->pcm_free)
2914 component->driver->pcm_free(pcm);
2918 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2920 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2921 struct snd_soc_rtdcom_list *rtdcom;
2922 struct snd_soc_component *component;
2924 for_each_rtdcom(rtd, rtdcom) {
2925 component = rtdcom->component;
2927 if (!component->driver->ops ||
2928 !component->driver->ops->ack)
2931 /* FIXME. it returns 1st ask now */
2932 return component->driver->ops->ack(substream);
2938 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2939 unsigned long pos, void __user *buf,
2940 unsigned long bytes)
2942 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2943 struct snd_soc_rtdcom_list *rtdcom;
2944 struct snd_soc_component *component;
2946 for_each_rtdcom(rtd, rtdcom) {
2947 component = rtdcom->component;
2949 if (!component->driver->ops ||
2950 !component->driver->ops->copy_user)
2953 /* FIXME. it returns 1st copy now */
2954 return component->driver->ops->copy_user(substream, channel,
2961 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2962 unsigned long pos, void *buf, unsigned long bytes)
2964 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2965 struct snd_soc_rtdcom_list *rtdcom;
2966 struct snd_soc_component *component;
2968 for_each_rtdcom(rtd, rtdcom) {
2969 component = rtdcom->component;
2971 if (!component->driver->ops ||
2972 !component->driver->ops->copy_kernel)
2975 /* FIXME. it returns 1st copy now */
2976 return component->driver->ops->copy_kernel(substream, channel,
2983 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2984 unsigned long pos, unsigned long bytes)
2986 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2987 struct snd_soc_rtdcom_list *rtdcom;
2988 struct snd_soc_component *component;
2990 for_each_rtdcom(rtd, rtdcom) {
2991 component = rtdcom->component;
2993 if (!component->driver->ops ||
2994 !component->driver->ops->fill_silence)
2997 /* FIXME. it returns 1st silence now */
2998 return component->driver->ops->fill_silence(substream, channel,
3005 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3006 unsigned long offset)
3008 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3009 struct snd_soc_rtdcom_list *rtdcom;
3010 struct snd_soc_component *component;
3013 for_each_rtdcom(rtd, rtdcom) {
3014 component = rtdcom->component;
3016 if (!component->driver->ops ||
3017 !component->driver->ops->page)
3020 /* FIXME. it returns 1st page now */
3021 page = component->driver->ops->page(substream, offset);
3029 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3030 struct vm_area_struct *vma)
3032 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3033 struct snd_soc_rtdcom_list *rtdcom;
3034 struct snd_soc_component *component;
3036 for_each_rtdcom(rtd, rtdcom) {
3037 component = rtdcom->component;
3039 if (!component->driver->ops ||
3040 !component->driver->ops->mmap)
3043 /* FIXME. it returns 1st mmap now */
3044 return component->driver->ops->mmap(substream, vma);
3050 /* create a new pcm */
3051 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3053 struct snd_soc_dai *codec_dai;
3054 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3055 struct snd_soc_component *component;
3056 struct snd_soc_rtdcom_list *rtdcom;
3057 struct snd_pcm *pcm;
3059 int ret = 0, playback = 0, capture = 0;
3062 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3063 playback = rtd->dai_link->dpcm_playback;
3064 capture = rtd->dai_link->dpcm_capture;
3066 for_each_rtd_codec_dai(rtd, i, codec_dai) {
3067 if (codec_dai->driver->playback.channels_min)
3069 if (codec_dai->driver->capture.channels_min)
3073 capture = capture && cpu_dai->driver->capture.channels_min;
3074 playback = playback && cpu_dai->driver->playback.channels_min;
3077 if (rtd->dai_link->playback_only) {
3082 if (rtd->dai_link->capture_only) {
3087 /* create the PCM */
3088 if (rtd->dai_link->no_pcm) {
3089 snprintf(new_name, sizeof(new_name), "(%s)",
3090 rtd->dai_link->stream_name);
3092 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3093 playback, capture, &pcm);
3095 if (rtd->dai_link->dynamic)
3096 snprintf(new_name, sizeof(new_name), "%s (*)",
3097 rtd->dai_link->stream_name);
3099 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3100 rtd->dai_link->stream_name,
3101 (rtd->num_codecs > 1) ?
3102 "multicodec" : rtd->codec_dai->name, num);
3104 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3108 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3109 rtd->dai_link->name);
3112 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3114 /* DAPM dai link stream work */
3115 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3117 pcm->nonatomic = rtd->dai_link->nonatomic;
3119 pcm->private_data = rtd;
3121 if (rtd->dai_link->no_pcm) {
3123 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3125 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3129 /* ASoC PCM operations */
3130 if (rtd->dai_link->dynamic) {
3131 rtd->ops.open = dpcm_fe_dai_open;
3132 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3133 rtd->ops.prepare = dpcm_fe_dai_prepare;
3134 rtd->ops.trigger = dpcm_fe_dai_trigger;
3135 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3136 rtd->ops.close = dpcm_fe_dai_close;
3137 rtd->ops.pointer = soc_pcm_pointer;
3138 rtd->ops.ioctl = soc_pcm_ioctl;
3140 rtd->ops.open = soc_pcm_open;
3141 rtd->ops.hw_params = soc_pcm_hw_params;
3142 rtd->ops.prepare = soc_pcm_prepare;
3143 rtd->ops.trigger = soc_pcm_trigger;
3144 rtd->ops.hw_free = soc_pcm_hw_free;
3145 rtd->ops.close = soc_pcm_close;
3146 rtd->ops.pointer = soc_pcm_pointer;
3147 rtd->ops.ioctl = soc_pcm_ioctl;
3150 for_each_rtdcom(rtd, rtdcom) {
3151 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3157 rtd->ops.ack = soc_rtdcom_ack;
3159 rtd->ops.copy_user = soc_rtdcom_copy_user;
3160 if (ops->copy_kernel)
3161 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3162 if (ops->fill_silence)
3163 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3165 rtd->ops.page = soc_rtdcom_page;
3167 rtd->ops.mmap = soc_rtdcom_mmap;
3171 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3174 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3176 for_each_rtdcom(rtd, rtdcom) {
3177 component = rtdcom->component;
3179 if (!component->driver->pcm_new)
3182 ret = component->driver->pcm_new(rtd);
3184 dev_err(component->dev,
3185 "ASoC: pcm constructor failed: %d\n",
3191 pcm->private_free = soc_pcm_private_free;
3192 pcm->no_device_suspend = true;
3194 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3195 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3200 /* is the current PCM operation for this FE ? */
3201 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3203 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3207 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3209 /* is the current PCM operation for this BE ? */
3210 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3211 struct snd_soc_pcm_runtime *be, int stream)
3213 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3214 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3215 be->dpcm[stream].runtime_update))
3219 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3221 /* get the substream for this BE */
3222 struct snd_pcm_substream *
3223 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3225 return be->pcm->streams[stream].substream;
3227 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3229 /* get the BE runtime state */
3230 enum snd_soc_dpcm_state
3231 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3233 return be->dpcm[stream].state;
3235 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3237 /* set the BE runtime state */
3238 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3239 int stream, enum snd_soc_dpcm_state state)
3241 be->dpcm[stream].state = state;
3243 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3246 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3247 * are not running, paused or suspended for the specified stream direction.
3249 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3250 struct snd_soc_pcm_runtime *be, int stream)
3252 struct snd_soc_dpcm *dpcm;
3255 unsigned long flags;
3257 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3258 for_each_dpcm_fe(be, stream, dpcm) {
3263 state = dpcm->fe->dpcm[stream].state;
3264 if (state == SND_SOC_DPCM_STATE_START ||
3265 state == SND_SOC_DPCM_STATE_PAUSED ||
3266 state == SND_SOC_DPCM_STATE_SUSPEND) {
3271 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3273 /* it's safe to free/stop this BE DAI */
3276 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3279 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3280 * running, paused or suspended for the specified stream direction.
3282 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3283 struct snd_soc_pcm_runtime *be, int stream)
3285 struct snd_soc_dpcm *dpcm;
3288 unsigned long flags;
3290 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3291 for_each_dpcm_fe(be, stream, dpcm) {
3296 state = dpcm->fe->dpcm[stream].state;
3297 if (state == SND_SOC_DPCM_STATE_START ||
3298 state == SND_SOC_DPCM_STATE_PAUSED ||
3299 state == SND_SOC_DPCM_STATE_SUSPEND ||
3300 state == SND_SOC_DPCM_STATE_PREPARE) {
3305 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3307 /* it's safe to change hw_params */
3310 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3312 #ifdef CONFIG_DEBUG_FS
3313 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3316 case SND_SOC_DPCM_STATE_NEW:
3318 case SND_SOC_DPCM_STATE_OPEN:
3320 case SND_SOC_DPCM_STATE_HW_PARAMS:
3322 case SND_SOC_DPCM_STATE_PREPARE:
3324 case SND_SOC_DPCM_STATE_START:
3326 case SND_SOC_DPCM_STATE_STOP:
3328 case SND_SOC_DPCM_STATE_SUSPEND:
3330 case SND_SOC_DPCM_STATE_PAUSED:
3332 case SND_SOC_DPCM_STATE_HW_FREE:
3334 case SND_SOC_DPCM_STATE_CLOSE:
3341 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3342 int stream, char *buf, size_t size)
3344 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3345 struct snd_soc_dpcm *dpcm;
3347 unsigned long flags;
3350 offset += snprintf(buf + offset, size - offset,
3351 "[%s - %s]\n", fe->dai_link->name,
3352 stream ? "Capture" : "Playback");
3354 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3355 dpcm_state_string(fe->dpcm[stream].state));
3357 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3358 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3359 offset += snprintf(buf + offset, size - offset,
3361 "Format = %s, Channels = %d, Rate = %d\n",
3362 snd_pcm_format_name(params_format(params)),
3363 params_channels(params),
3364 params_rate(params));
3367 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3369 if (list_empty(&fe->dpcm[stream].be_clients)) {
3370 offset += snprintf(buf + offset, size - offset,
3371 " No active DSP links\n");
3375 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3376 for_each_dpcm_be(fe, stream, dpcm) {
3377 struct snd_soc_pcm_runtime *be = dpcm->be;
3378 params = &dpcm->hw_params;
3380 offset += snprintf(buf + offset, size - offset,
3381 "- %s\n", be->dai_link->name);
3383 offset += snprintf(buf + offset, size - offset,
3385 dpcm_state_string(be->dpcm[stream].state));
3387 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3388 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3389 offset += snprintf(buf + offset, size - offset,
3390 " Hardware Params: "
3391 "Format = %s, Channels = %d, Rate = %d\n",
3392 snd_pcm_format_name(params_format(params)),
3393 params_channels(params),
3394 params_rate(params));
3396 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3401 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3402 size_t count, loff_t *ppos)
3404 struct snd_soc_pcm_runtime *fe = file->private_data;
3405 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3408 buf = kmalloc(out_count, GFP_KERNEL);
3412 if (fe->cpu_dai->driver->playback.channels_min)
3413 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3414 buf + offset, out_count - offset);
3416 if (fe->cpu_dai->driver->capture.channels_min)
3417 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3418 buf + offset, out_count - offset);
3420 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3426 static const struct file_operations dpcm_state_fops = {
3427 .open = simple_open,
3428 .read = dpcm_state_read_file,
3429 .llseek = default_llseek,
3432 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3437 if (!rtd->card->debugfs_card_root)
3440 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3441 rtd->card->debugfs_card_root);
3442 if (!rtd->debugfs_dpcm_root) {
3444 "ASoC: Failed to create dpcm debugfs directory %s\n",
3445 rtd->dai_link->name);
3449 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3450 rtd, &dpcm_state_fops);