2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
40 * Returns true if the DAI supports the indicated stream type.
42 static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
44 struct snd_soc_pcm_stream *codec_stream;
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
63 * Must be called with the rtd->pcm_mutex being held
65 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
70 lockdep_assert_held(&rtd->pcm_mutex);
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
83 cpu_dai->component->active++;
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
98 * Must be called with the rtd->pcm_mutex being held
100 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 lockdep_assert_held(&rtd->pcm_mutex);
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
118 cpu_dai->component->active--;
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
134 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
153 * Sets the substream runtime hardware parameters.
155 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
169 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171 /* DPCM stream event, send event to FE and all active BEs. */
172 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
175 struct snd_soc_dpcm *dpcm;
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
179 struct snd_soc_pcm_runtime *be = dpcm->be;
181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
182 be->dai_link->name, event, dir);
184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
188 snd_soc_dapm_stream_event(be, dir, event);
191 snd_soc_dapm_stream_event(fe, dir, event);
196 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
207 ret = snd_pcm_hw_constraint_single(substream->runtime,
208 SNDRV_PCM_HW_PARAM_RATE,
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
223 ret = snd_pcm_hw_constraint_single(substream->runtime,
224 SNDRV_PCM_HW_PARAM_CHANNELS,
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
239 ret = snd_pcm_hw_constraint_single(substream->runtime,
240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
241 soc_dai->sample_bits);
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
253 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
258 unsigned int rate, channels, sample_bits, symmetry, i;
260 rate = params_rate(params);
261 channels = params_channels(params);
262 sample_bits = snd_pcm_format_physical_width(params_format(params));
264 /* reject unmatched parameters when applying symmetry */
265 symmetry = cpu_dai->driver->symmetric_rates ||
266 rtd->dai_link->symmetric_rates;
268 for (i = 0; i < rtd->num_codecs; i++)
269 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
271 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
272 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai->rate, rate);
277 symmetry = cpu_dai->driver->symmetric_channels ||
278 rtd->dai_link->symmetric_channels;
280 for (i = 0; i < rtd->num_codecs; i++)
281 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
283 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
284 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai->channels, channels);
289 symmetry = cpu_dai->driver->symmetric_samplebits ||
290 rtd->dai_link->symmetric_samplebits;
292 for (i = 0; i < rtd->num_codecs; i++)
293 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
295 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
296 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai->sample_bits, sample_bits);
304 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
308 struct snd_soc_dai_link *link = rtd->dai_link;
309 unsigned int symmetry, i;
311 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
312 cpu_driver->symmetric_channels || link->symmetric_channels ||
313 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
315 for (i = 0; i < rtd->num_codecs; i++)
316 symmetry = symmetry ||
317 rtd->codec_dais[i]->driver->symmetric_rates ||
318 rtd->codec_dais[i]->driver->symmetric_channels ||
319 rtd->codec_dais[i]->driver->symmetric_samplebits;
324 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
338 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
340 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
342 struct snd_soc_dai *codec_dai;
344 unsigned int bits = 0, cpu_bits;
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
347 for (i = 0; i < rtd->num_codecs; i++) {
348 codec_dai = rtd->codec_dais[i];
349 if (codec_dai->driver->playback.sig_bits == 0) {
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
355 cpu_bits = cpu_dai->driver->playback.sig_bits;
357 for (i = 0; i < rtd->num_codecs; i++) {
358 codec_dai = rtd->codec_dais[i];
359 if (codec_dai->driver->capture.sig_bits == 0) {
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
365 cpu_bits = cpu_dai->driver->capture.sig_bits;
368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
372 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
374 struct snd_pcm_runtime *runtime = substream->runtime;
375 struct snd_pcm_hardware *hw = &runtime->hw;
376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
378 struct snd_soc_dai_driver *codec_dai_drv;
379 struct snd_soc_pcm_stream *codec_stream;
380 struct snd_soc_pcm_stream *cpu_stream;
381 unsigned int chan_min = 0, chan_max = UINT_MAX;
382 unsigned int rate_min = 0, rate_max = UINT_MAX;
383 unsigned int rates = UINT_MAX;
384 u64 formats = ULLONG_MAX;
387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
390 cpu_stream = &cpu_dai_drv->capture;
392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i = 0; i < rtd->num_codecs; i++) {
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
404 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
412 codec_stream = &codec_dai_drv->capture;
413 chan_min = max(chan_min, codec_stream->channels_min);
414 chan_max = min(chan_max, codec_stream->channels_max);
415 rate_min = max(rate_min, codec_stream->rate_min);
416 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
417 formats &= codec_stream->formats;
418 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
426 if (rtd->num_codecs > 1) {
427 chan_min = cpu_stream->channels_min;
428 chan_max = cpu_stream->channels_max;
431 hw->channels_min = max(chan_min, cpu_stream->channels_min);
432 hw->channels_max = min(chan_max, cpu_stream->channels_max);
434 hw->formats &= formats & cpu_stream->formats;
436 hw->formats = formats & cpu_stream->formats;
437 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
439 snd_pcm_limit_hw_rates(runtime);
441 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
442 hw->rate_min = max(hw->rate_min, rate_min);
443 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
444 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
452 static int soc_pcm_open(struct snd_pcm_substream *substream)
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct snd_soc_platform *platform = rtd->platform;
457 struct snd_soc_component *component;
458 struct snd_soc_rtdcom_list *rtdcom;
459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
460 struct snd_soc_dai *codec_dai;
461 const char *codec_dai_name = "multicodec";
462 int i, ret = 0, __ret;
464 pinctrl_pm_select_default_state(cpu_dai->dev);
465 for (i = 0; i < rtd->num_codecs; i++)
466 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
468 for_each_rtdcom(rtd, rtdcom) {
469 component = rtdcom->component;
471 pm_runtime_get_sync(component->dev);
474 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
476 /* startup the audio subsystem */
477 if (cpu_dai->driver->ops->startup) {
478 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
480 dev_err(cpu_dai->dev, "ASoC: can't open interface"
481 " %s: %d\n", cpu_dai->name, ret);
486 if (platform && platform->driver->ops && platform->driver->ops->open) {
487 ret = platform->driver->ops->open(substream);
489 dev_err(platform->dev, "ASoC: can't open platform"
490 " %s: %d\n", platform->component.name, ret);
496 for_each_rtdcom(rtd, rtdcom) {
497 component = rtdcom->component;
499 /* ignore duplication for now */
500 if (platform && (component == &platform->component))
503 if (!component->driver->ops ||
504 !component->driver->ops->open)
507 __ret = component->driver->ops->open(substream);
509 dev_err(component->dev,
510 "ASoC: can't open component %s: %d\n",
511 component->name, ret);
518 for (i = 0; i < rtd->num_codecs; i++) {
519 codec_dai = rtd->codec_dais[i];
520 if (codec_dai->driver->ops->startup) {
521 ret = codec_dai->driver->ops->startup(substream,
524 dev_err(codec_dai->dev,
525 "ASoC: can't open codec %s: %d\n",
526 codec_dai->name, ret);
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
532 codec_dai->tx_mask = 0;
534 codec_dai->rx_mask = 0;
537 if (rtd->dai_link->ops->startup) {
538 ret = rtd->dai_link->ops->startup(substream);
540 pr_err("ASoC: %s startup failed: %d\n",
541 rtd->dai_link->name, ret);
546 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
547 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
550 /* Check that the codec and cpu DAIs are compatible */
551 soc_pcm_init_runtime_hw(substream);
553 if (rtd->num_codecs == 1)
554 codec_dai_name = rtd->codec_dai->name;
556 if (soc_pcm_has_symmetry(substream))
557 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
560 if (!runtime->hw.rates) {
561 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
562 codec_dai_name, cpu_dai->name);
565 if (!runtime->hw.formats) {
566 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
567 codec_dai_name, cpu_dai->name);
570 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
571 runtime->hw.channels_min > runtime->hw.channels_max) {
572 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
573 codec_dai_name, cpu_dai->name);
577 soc_pcm_apply_msb(substream);
579 /* Symmetry only applies if we've already got an active stream. */
580 if (cpu_dai->active) {
581 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
586 for (i = 0; i < rtd->num_codecs; i++) {
587 if (rtd->codec_dais[i]->active) {
588 ret = soc_pcm_apply_symmetry(substream,
595 pr_debug("ASoC: %s <-> %s info:\n",
596 codec_dai_name, cpu_dai->name);
597 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
598 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
599 runtime->hw.channels_max);
600 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
601 runtime->hw.rate_max);
605 snd_soc_runtime_activate(rtd, substream->stream);
607 mutex_unlock(&rtd->pcm_mutex);
611 if (rtd->dai_link->ops->shutdown)
612 rtd->dai_link->ops->shutdown(substream);
619 codec_dai = rtd->codec_dais[i];
620 if (codec_dai->driver->ops->shutdown)
621 codec_dai->driver->ops->shutdown(substream, codec_dai);
625 for_each_rtdcom(rtd, rtdcom) {
626 component = rtdcom->component;
628 /* ignore duplication for now */
629 if (platform && (component == &platform->component))
632 if (!component->driver->ops ||
633 !component->driver->ops->close)
636 component->driver->ops->close(substream);
639 if (platform && platform->driver->ops && platform->driver->ops->close)
640 platform->driver->ops->close(substream);
643 if (cpu_dai->driver->ops->shutdown)
644 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
646 mutex_unlock(&rtd->pcm_mutex);
648 for_each_rtdcom(rtd, rtdcom) {
649 component = rtdcom->component;
651 pm_runtime_mark_last_busy(component->dev);
652 pm_runtime_put_autosuspend(component->dev);
655 for (i = 0; i < rtd->num_codecs; i++) {
656 if (!rtd->codec_dais[i]->active)
657 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
659 if (!cpu_dai->active)
660 pinctrl_pm_select_sleep_state(cpu_dai->dev);
666 * Power down the audio subsystem pmdown_time msecs after close is called.
667 * This is to ensure there are no pops or clicks in between any music tracks
668 * due to DAPM power cycling.
670 static void close_delayed_work(struct work_struct *work)
672 struct snd_soc_pcm_runtime *rtd =
673 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
674 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
676 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
678 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
679 codec_dai->driver->playback.stream_name,
680 codec_dai->playback_active ? "active" : "inactive",
681 rtd->pop_wait ? "yes" : "no");
683 /* are we waiting on this codec DAI stream */
684 if (rtd->pop_wait == 1) {
686 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
687 SND_SOC_DAPM_STREAM_STOP);
690 mutex_unlock(&rtd->pcm_mutex);
694 * Called by ALSA when a PCM substream is closed. Private data can be
695 * freed here. The cpu DAI, codec DAI, machine and platform are also
698 static int soc_pcm_close(struct snd_pcm_substream *substream)
700 struct snd_soc_pcm_runtime *rtd = substream->private_data;
701 struct snd_soc_platform *platform = rtd->platform;
702 struct snd_soc_component *component;
703 struct snd_soc_rtdcom_list *rtdcom;
704 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
705 struct snd_soc_dai *codec_dai;
708 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
710 snd_soc_runtime_deactivate(rtd, substream->stream);
712 /* clear the corresponding DAIs rate when inactive */
713 if (!cpu_dai->active)
716 for (i = 0; i < rtd->num_codecs; i++) {
717 codec_dai = rtd->codec_dais[i];
718 if (!codec_dai->active)
722 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
724 if (cpu_dai->driver->ops->shutdown)
725 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
727 for (i = 0; i < rtd->num_codecs; i++) {
728 codec_dai = rtd->codec_dais[i];
729 if (codec_dai->driver->ops->shutdown)
730 codec_dai->driver->ops->shutdown(substream, codec_dai);
733 if (rtd->dai_link->ops->shutdown)
734 rtd->dai_link->ops->shutdown(substream);
736 if (platform && platform->driver->ops && platform->driver->ops->close)
737 platform->driver->ops->close(substream);
739 for_each_rtdcom(rtd, rtdcom) {
740 component = rtdcom->component;
742 /* ignore duplication for now */
743 if (platform && (component == &platform->component))
746 if (!component->driver->ops ||
747 !component->driver->ops->close)
750 component->driver->ops->close(substream);
753 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
754 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
755 /* powered down playback stream now */
756 snd_soc_dapm_stream_event(rtd,
757 SNDRV_PCM_STREAM_PLAYBACK,
758 SND_SOC_DAPM_STREAM_STOP);
760 /* start delayed pop wq here for playback streams */
762 queue_delayed_work(system_power_efficient_wq,
764 msecs_to_jiffies(rtd->pmdown_time));
767 /* capture streams can be powered down now */
768 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
769 SND_SOC_DAPM_STREAM_STOP);
772 mutex_unlock(&rtd->pcm_mutex);
774 for_each_rtdcom(rtd, rtdcom) {
775 component = rtdcom->component;
777 pm_runtime_mark_last_busy(component->dev);
778 pm_runtime_put_autosuspend(component->dev);
781 for (i = 0; i < rtd->num_codecs; i++) {
782 if (!rtd->codec_dais[i]->active)
783 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
785 if (!cpu_dai->active)
786 pinctrl_pm_select_sleep_state(cpu_dai->dev);
792 * Called by ALSA when the PCM substream is prepared, can set format, sample
793 * rate, etc. This function is non atomic and can be called multiple times,
794 * it can refer to the runtime info.
796 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
798 struct snd_soc_pcm_runtime *rtd = substream->private_data;
799 struct snd_soc_platform *platform = rtd->platform;
800 struct snd_soc_component *component;
801 struct snd_soc_rtdcom_list *rtdcom;
802 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
803 struct snd_soc_dai *codec_dai;
806 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
808 if (rtd->dai_link->ops->prepare) {
809 ret = rtd->dai_link->ops->prepare(substream);
811 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
817 if (platform && platform->driver->ops && platform->driver->ops->prepare) {
818 ret = platform->driver->ops->prepare(substream);
820 dev_err(platform->dev, "ASoC: platform prepare error:"
826 for_each_rtdcom(rtd, rtdcom) {
827 component = rtdcom->component;
829 /* ignore duplication for now */
830 if (platform && (component == &platform->component))
833 if (!component->driver->ops ||
834 !component->driver->ops->prepare)
837 ret = component->driver->ops->prepare(substream);
839 dev_err(component->dev,
840 "ASoC: platform prepare error: %d\n", ret);
845 for (i = 0; i < rtd->num_codecs; i++) {
846 codec_dai = rtd->codec_dais[i];
847 if (codec_dai->driver->ops->prepare) {
848 ret = codec_dai->driver->ops->prepare(substream,
851 dev_err(codec_dai->dev,
852 "ASoC: codec DAI prepare error: %d\n",
859 if (cpu_dai->driver->ops->prepare) {
860 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
862 dev_err(cpu_dai->dev,
863 "ASoC: cpu DAI prepare error: %d\n", ret);
868 /* cancel any delayed stream shutdown that is pending */
869 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
872 cancel_delayed_work(&rtd->delayed_work);
875 snd_soc_dapm_stream_event(rtd, substream->stream,
876 SND_SOC_DAPM_STREAM_START);
878 for (i = 0; i < rtd->num_codecs; i++)
879 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
881 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
884 mutex_unlock(&rtd->pcm_mutex);
888 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
891 struct snd_interval *interval;
892 int channels = hweight_long(mask);
894 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
895 interval->min = channels;
896 interval->max = channels;
899 int soc_dai_hw_params(struct snd_pcm_substream *substream,
900 struct snd_pcm_hw_params *params,
901 struct snd_soc_dai *dai)
905 if (dai->driver->ops->hw_params) {
906 ret = dai->driver->ops->hw_params(substream, params, dai);
908 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
918 * Called by ALSA when the hardware params are set by application. This
919 * function can also be called multiple times and can allocate buffers
920 * (using snd_pcm_lib_* ). It's non-atomic.
922 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
923 struct snd_pcm_hw_params *params)
925 struct snd_soc_pcm_runtime *rtd = substream->private_data;
926 struct snd_soc_platform *platform = rtd->platform;
927 struct snd_soc_component *component;
928 struct snd_soc_rtdcom_list *rtdcom;
929 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
930 int i, ret = 0, __ret;
932 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
933 if (rtd->dai_link->ops->hw_params) {
934 ret = rtd->dai_link->ops->hw_params(substream, params);
936 dev_err(rtd->card->dev, "ASoC: machine hw_params"
937 " failed: %d\n", ret);
942 for (i = 0; i < rtd->num_codecs; i++) {
943 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
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 (codec_dai->tx_mask)
968 soc_pcm_codec_params_fixup(&codec_params,
970 if (codec_dai->rx_mask)
971 soc_pcm_codec_params_fixup(&codec_params,
974 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
978 codec_dai->rate = params_rate(&codec_params);
979 codec_dai->channels = params_channels(&codec_params);
980 codec_dai->sample_bits = snd_pcm_format_physical_width(
981 params_format(&codec_params));
984 ret = soc_dai_hw_params(substream, params, cpu_dai);
988 if (platform && platform->driver->ops && platform->driver->ops->hw_params) {
989 ret = platform->driver->ops->hw_params(substream, params);
991 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
992 platform->component.name, ret);
998 for_each_rtdcom(rtd, rtdcom) {
999 component = rtdcom->component;
1001 /* ignore duplication for now */
1002 if (platform && (component == &platform->component))
1005 if (!component->driver->ops ||
1006 !component->driver->ops->hw_params)
1009 __ret = component->driver->ops->hw_params(substream, params);
1011 dev_err(component->dev,
1012 "ASoC: %s hw params failed: %d\n",
1013 component->name, ret);
1020 /* store the parameters for each DAIs */
1021 cpu_dai->rate = params_rate(params);
1022 cpu_dai->channels = params_channels(params);
1023 cpu_dai->sample_bits =
1024 snd_pcm_format_physical_width(params_format(params));
1026 ret = soc_pcm_params_symmetry(substream, params);
1030 mutex_unlock(&rtd->pcm_mutex);
1034 for_each_rtdcom(rtd, rtdcom) {
1035 component = rtdcom->component;
1037 /* ignore duplication */
1038 if (platform && (component == &platform->component))
1041 if (!component->driver->ops ||
1042 !component->driver->ops->hw_free)
1045 component->driver->ops->hw_free(substream);
1048 if (platform && platform->driver->ops && platform->driver->ops->hw_free)
1049 platform->driver->ops->hw_free(substream);
1052 if (cpu_dai->driver->ops->hw_free)
1053 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1056 i = rtd->num_codecs;
1060 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1061 if (codec_dai->driver->ops->hw_free)
1062 codec_dai->driver->ops->hw_free(substream, codec_dai);
1063 codec_dai->rate = 0;
1066 if (rtd->dai_link->ops->hw_free)
1067 rtd->dai_link->ops->hw_free(substream);
1069 mutex_unlock(&rtd->pcm_mutex);
1074 * Frees resources allocated by hw_params, can be called multiple times
1076 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1078 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1079 struct snd_soc_platform *platform = rtd->platform;
1080 struct snd_soc_component *component;
1081 struct snd_soc_rtdcom_list *rtdcom;
1082 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1083 struct snd_soc_dai *codec_dai;
1084 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1087 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1089 /* clear the corresponding DAIs parameters when going to be inactive */
1090 if (cpu_dai->active == 1) {
1092 cpu_dai->channels = 0;
1093 cpu_dai->sample_bits = 0;
1096 for (i = 0; i < rtd->num_codecs; i++) {
1097 codec_dai = rtd->codec_dais[i];
1098 if (codec_dai->active == 1) {
1099 codec_dai->rate = 0;
1100 codec_dai->channels = 0;
1101 codec_dai->sample_bits = 0;
1105 /* apply codec digital mute */
1106 for (i = 0; i < rtd->num_codecs; i++) {
1107 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1108 (!playback && rtd->codec_dais[i]->capture_active == 1))
1109 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1113 /* free any machine hw params */
1114 if (rtd->dai_link->ops->hw_free)
1115 rtd->dai_link->ops->hw_free(substream);
1117 /* free any DMA resources */
1118 if (platform && platform->driver->ops && platform->driver->ops->hw_free)
1119 platform->driver->ops->hw_free(substream);
1121 /* free any component resources */
1122 for_each_rtdcom(rtd, rtdcom) {
1123 component = rtdcom->component;
1125 /* ignore duplication for now */
1126 if (platform && (component == &platform->component))
1129 if (!component->driver->ops ||
1130 !component->driver->ops->hw_free)
1133 component->driver->ops->hw_free(substream);
1136 /* now free hw params for the DAIs */
1137 for (i = 0; i < rtd->num_codecs; i++) {
1138 codec_dai = rtd->codec_dais[i];
1139 if (codec_dai->driver->ops->hw_free)
1140 codec_dai->driver->ops->hw_free(substream, codec_dai);
1143 if (cpu_dai->driver->ops->hw_free)
1144 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1146 mutex_unlock(&rtd->pcm_mutex);
1150 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1152 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1153 struct snd_soc_platform *platform = rtd->platform;
1154 struct snd_soc_component *component;
1155 struct snd_soc_rtdcom_list *rtdcom;
1156 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1157 struct snd_soc_dai *codec_dai;
1160 for (i = 0; i < rtd->num_codecs; i++) {
1161 codec_dai = rtd->codec_dais[i];
1162 if (codec_dai->driver->ops->trigger) {
1163 ret = codec_dai->driver->ops->trigger(substream,
1170 if (platform && platform->driver->ops && platform->driver->ops->trigger) {
1171 ret = platform->driver->ops->trigger(substream, cmd);
1176 for_each_rtdcom(rtd, rtdcom) {
1177 component = rtdcom->component;
1179 /* ignore duplication for now */
1180 if (platform && (component == &platform->component))
1183 if (!component->driver->ops ||
1184 !component->driver->ops->trigger)
1187 ret = component->driver->ops->trigger(substream, cmd);
1192 if (cpu_dai->driver->ops->trigger) {
1193 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1198 if (rtd->dai_link->ops->trigger) {
1199 ret = rtd->dai_link->ops->trigger(substream, cmd);
1207 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1210 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1211 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1212 struct snd_soc_dai *codec_dai;
1215 for (i = 0; i < rtd->num_codecs; i++) {
1216 codec_dai = rtd->codec_dais[i];
1217 if (codec_dai->driver->ops->bespoke_trigger) {
1218 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1225 if (cpu_dai->driver->ops->bespoke_trigger) {
1226 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1233 * soc level wrapper for pointer callback
1234 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1235 * the runtime->delay will be updated accordingly.
1237 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1239 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1240 struct snd_soc_platform *platform = rtd->platform;
1241 struct snd_soc_component *component;
1242 struct snd_soc_rtdcom_list *rtdcom;
1243 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1244 struct snd_soc_dai *codec_dai;
1245 struct snd_pcm_runtime *runtime = substream->runtime;
1246 snd_pcm_uframes_t offset = 0;
1247 snd_pcm_sframes_t delay = 0;
1248 snd_pcm_sframes_t codec_delay = 0;
1251 if (platform && platform->driver->ops && platform->driver->ops->pointer)
1252 offset = platform->driver->ops->pointer(substream);
1254 for_each_rtdcom(rtd, rtdcom) {
1255 component = rtdcom->component;
1257 /* ignore duplication for now */
1258 if (platform && (component == &platform->component))
1261 if (!component->driver->ops ||
1262 !component->driver->ops->pointer)
1265 /* FIXME: use 1st pointer */
1266 offset = component->driver->ops->pointer(substream);
1270 if (cpu_dai->driver->ops->delay)
1271 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1273 for (i = 0; i < rtd->num_codecs; i++) {
1274 codec_dai = rtd->codec_dais[i];
1275 if (codec_dai->driver->ops->delay)
1276 codec_delay = max(codec_delay,
1277 codec_dai->driver->ops->delay(substream,
1280 delay += codec_delay;
1282 runtime->delay = delay;
1287 /* connect a FE and BE */
1288 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1289 struct snd_soc_pcm_runtime *be, int stream)
1291 struct snd_soc_dpcm *dpcm;
1293 /* only add new dpcms */
1294 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1295 if (dpcm->be == be && dpcm->fe == fe)
1299 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1305 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1306 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1307 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1308 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1310 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1311 stream ? "capture" : "playback", fe->dai_link->name,
1312 stream ? "<-" : "->", be->dai_link->name);
1314 #ifdef CONFIG_DEBUG_FS
1315 if (fe->debugfs_dpcm_root)
1316 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1317 fe->debugfs_dpcm_root, &dpcm->state);
1322 /* reparent a BE onto another FE */
1323 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1324 struct snd_soc_pcm_runtime *be, int stream)
1326 struct snd_soc_dpcm *dpcm;
1327 struct snd_pcm_substream *fe_substream, *be_substream;
1329 /* reparent if BE is connected to other FEs */
1330 if (!be->dpcm[stream].users)
1333 be_substream = snd_soc_dpcm_get_substream(be, stream);
1335 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1339 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1340 stream ? "capture" : "playback",
1341 dpcm->fe->dai_link->name,
1342 stream ? "<-" : "->", dpcm->be->dai_link->name);
1344 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1345 be_substream->runtime = fe_substream->runtime;
1350 /* disconnect a BE and FE */
1351 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1353 struct snd_soc_dpcm *dpcm, *d;
1355 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1356 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1357 stream ? "capture" : "playback",
1358 dpcm->be->dai_link->name);
1360 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1363 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1364 stream ? "capture" : "playback", fe->dai_link->name,
1365 stream ? "<-" : "->", dpcm->be->dai_link->name);
1367 /* BEs still alive need new FE */
1368 dpcm_be_reparent(fe, dpcm->be, stream);
1370 #ifdef CONFIG_DEBUG_FS
1371 debugfs_remove(dpcm->debugfs_state);
1373 list_del(&dpcm->list_be);
1374 list_del(&dpcm->list_fe);
1379 /* get BE for DAI widget and stream */
1380 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1381 struct snd_soc_dapm_widget *widget, int stream)
1383 struct snd_soc_pcm_runtime *be;
1386 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1387 list_for_each_entry(be, &card->rtd_list, list) {
1389 if (!be->dai_link->no_pcm)
1392 if (be->cpu_dai->playback_widget == widget)
1395 for (i = 0; i < be->num_codecs; i++) {
1396 struct snd_soc_dai *dai = be->codec_dais[i];
1397 if (dai->playback_widget == widget)
1403 list_for_each_entry(be, &card->rtd_list, list) {
1405 if (!be->dai_link->no_pcm)
1408 if (be->cpu_dai->capture_widget == widget)
1411 for (i = 0; i < be->num_codecs; i++) {
1412 struct snd_soc_dai *dai = be->codec_dais[i];
1413 if (dai->capture_widget == widget)
1419 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1420 stream ? "capture" : "playback", widget->name);
1424 static inline struct snd_soc_dapm_widget *
1425 dai_get_widget(struct snd_soc_dai *dai, int stream)
1427 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1428 return dai->playback_widget;
1430 return dai->capture_widget;
1433 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1434 struct snd_soc_dapm_widget *widget)
1438 for (i = 0; i < list->num_widgets; i++) {
1439 if (widget == list->widgets[i])
1446 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1447 enum snd_soc_dapm_direction dir)
1449 struct snd_soc_card *card = widget->dapm->card;
1450 struct snd_soc_pcm_runtime *rtd;
1453 if (dir == SND_SOC_DAPM_DIR_OUT) {
1454 list_for_each_entry(rtd, &card->rtd_list, list) {
1455 if (!rtd->dai_link->no_pcm)
1458 if (rtd->cpu_dai->playback_widget == widget)
1461 for (i = 0; i < rtd->num_codecs; ++i) {
1462 struct snd_soc_dai *dai = rtd->codec_dais[i];
1463 if (dai->playback_widget == widget)
1467 } else { /* SND_SOC_DAPM_DIR_IN */
1468 list_for_each_entry(rtd, &card->rtd_list, list) {
1469 if (!rtd->dai_link->no_pcm)
1472 if (rtd->cpu_dai->capture_widget == widget)
1475 for (i = 0; i < rtd->num_codecs; ++i) {
1476 struct snd_soc_dai *dai = rtd->codec_dais[i];
1477 if (dai->capture_widget == widget)
1486 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1487 int stream, struct snd_soc_dapm_widget_list **list)
1489 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1492 /* get number of valid DAI paths and their widgets */
1493 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1494 dpcm_end_walk_at_be);
1496 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1497 stream ? "capture" : "playback");
1502 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1503 struct snd_soc_dapm_widget_list **list_)
1505 struct snd_soc_dpcm *dpcm;
1506 struct snd_soc_dapm_widget_list *list = *list_;
1507 struct snd_soc_dapm_widget *widget;
1510 /* Destroy any old FE <--> BE connections */
1511 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1514 /* is there a valid CPU DAI widget for this BE */
1515 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1517 /* prune the BE if it's no longer in our active list */
1518 if (widget && widget_in_list(list, widget))
1521 /* is there a valid CODEC DAI widget for this BE */
1522 for (i = 0; i < dpcm->be->num_codecs; i++) {
1523 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1524 widget = dai_get_widget(dai, stream);
1526 /* prune the BE if it's no longer in our active list */
1527 if (widget && widget_in_list(list, widget))
1531 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1532 stream ? "capture" : "playback",
1533 dpcm->be->dai_link->name, fe->dai_link->name);
1534 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1535 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1539 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1543 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1544 struct snd_soc_dapm_widget_list **list_)
1546 struct snd_soc_card *card = fe->card;
1547 struct snd_soc_dapm_widget_list *list = *list_;
1548 struct snd_soc_pcm_runtime *be;
1549 int i, new = 0, err;
1551 /* Create any new FE <--> BE connections */
1552 for (i = 0; i < list->num_widgets; i++) {
1554 switch (list->widgets[i]->id) {
1555 case snd_soc_dapm_dai_in:
1556 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1559 case snd_soc_dapm_dai_out:
1560 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1567 /* is there a valid BE rtd for this widget */
1568 be = dpcm_get_be(card, list->widgets[i], stream);
1570 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1571 list->widgets[i]->name);
1575 /* make sure BE is a real BE */
1576 if (!be->dai_link->no_pcm)
1579 /* don't connect if FE is not running */
1580 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1583 /* newly connected FE and BE */
1584 err = dpcm_be_connect(fe, be, stream);
1586 dev_err(fe->dev, "ASoC: can't connect %s\n",
1587 list->widgets[i]->name);
1589 } else if (err == 0) /* already connected */
1593 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1597 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1602 * Find the corresponding BE DAIs that source or sink audio to this
1605 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1606 int stream, struct snd_soc_dapm_widget_list **list, int new)
1609 return dpcm_add_paths(fe, stream, list);
1611 return dpcm_prune_paths(fe, stream, list);
1614 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1616 struct snd_soc_dpcm *dpcm;
1618 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1619 dpcm->be->dpcm[stream].runtime_update =
1620 SND_SOC_DPCM_UPDATE_NO;
1623 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1626 struct snd_soc_dpcm *dpcm;
1628 /* disable any enabled and non active backends */
1629 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1631 struct snd_soc_pcm_runtime *be = dpcm->be;
1632 struct snd_pcm_substream *be_substream =
1633 snd_soc_dpcm_get_substream(be, stream);
1635 if (be->dpcm[stream].users == 0)
1636 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1637 stream ? "capture" : "playback",
1638 be->dpcm[stream].state);
1640 if (--be->dpcm[stream].users != 0)
1643 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1646 soc_pcm_close(be_substream);
1647 be_substream->runtime = NULL;
1648 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1652 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1654 struct snd_soc_dpcm *dpcm;
1657 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1658 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1660 struct snd_soc_pcm_runtime *be = dpcm->be;
1661 struct snd_pcm_substream *be_substream =
1662 snd_soc_dpcm_get_substream(be, stream);
1664 if (!be_substream) {
1665 dev_err(be->dev, "ASoC: no backend %s stream\n",
1666 stream ? "capture" : "playback");
1670 /* is this op for this BE ? */
1671 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1674 /* first time the dpcm is open ? */
1675 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1676 dev_err(be->dev, "ASoC: too many users %s at open %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_NEW) &&
1684 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1687 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1688 stream ? "capture" : "playback", be->dai_link->name);
1690 be_substream->runtime = be->dpcm[stream].runtime;
1691 err = soc_pcm_open(be_substream);
1693 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1694 be->dpcm[stream].users--;
1695 if (be->dpcm[stream].users < 0)
1696 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1697 stream ? "capture" : "playback",
1698 be->dpcm[stream].state);
1700 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1704 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1711 /* disable any enabled and non active backends */
1712 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1713 struct snd_soc_pcm_runtime *be = dpcm->be;
1714 struct snd_pcm_substream *be_substream =
1715 snd_soc_dpcm_get_substream(be, stream);
1717 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1720 if (be->dpcm[stream].users == 0)
1721 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1722 stream ? "capture" : "playback",
1723 be->dpcm[stream].state);
1725 if (--be->dpcm[stream].users != 0)
1728 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1731 soc_pcm_close(be_substream);
1732 be_substream->runtime = NULL;
1733 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1739 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1740 struct snd_soc_pcm_stream *stream,
1743 runtime->hw.rate_min = stream->rate_min;
1744 runtime->hw.rate_max = stream->rate_max;
1745 runtime->hw.channels_min = stream->channels_min;
1746 runtime->hw.channels_max = stream->channels_max;
1747 if (runtime->hw.formats)
1748 runtime->hw.formats &= formats & stream->formats;
1750 runtime->hw.formats = formats & stream->formats;
1751 runtime->hw.rates = stream->rates;
1754 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1756 struct snd_soc_pcm_runtime *fe = substream->private_data;
1757 struct snd_soc_dpcm *dpcm;
1758 u64 formats = ULLONG_MAX;
1759 int stream = substream->stream;
1761 if (!fe->dai_link->dpcm_merged_format)
1765 * It returns merged BE codec format
1766 * if FE want to use it (= dpcm_merged_format)
1769 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1770 struct snd_soc_pcm_runtime *be = dpcm->be;
1771 struct snd_soc_dai_driver *codec_dai_drv;
1772 struct snd_soc_pcm_stream *codec_stream;
1775 for (i = 0; i < be->num_codecs; i++) {
1776 codec_dai_drv = be->codec_dais[i]->driver;
1777 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1778 codec_stream = &codec_dai_drv->playback;
1780 codec_stream = &codec_dai_drv->capture;
1782 formats &= codec_stream->formats;
1789 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1791 struct snd_pcm_runtime *runtime = substream->runtime;
1792 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1793 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1794 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1795 u64 format = dpcm_runtime_base_format(substream);
1797 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1798 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1800 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1803 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1805 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1806 * for avoiding the race with trigger callback.
1807 * If the state is unset and a trigger is pending while the previous operation,
1808 * process the pending trigger action here.
1810 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1811 int stream, enum snd_soc_dpcm_update state)
1813 struct snd_pcm_substream *substream =
1814 snd_soc_dpcm_get_substream(fe, stream);
1816 snd_pcm_stream_lock_irq(substream);
1817 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1818 dpcm_fe_dai_do_trigger(substream,
1819 fe->dpcm[stream].trigger_pending - 1);
1820 fe->dpcm[stream].trigger_pending = 0;
1822 fe->dpcm[stream].runtime_update = state;
1823 snd_pcm_stream_unlock_irq(substream);
1826 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1829 struct snd_soc_dpcm *dpcm;
1830 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1831 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1834 /* apply symmetry for FE */
1835 if (soc_pcm_has_symmetry(fe_substream))
1836 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1838 /* Symmetry only applies if we've got an active stream. */
1839 if (fe_cpu_dai->active) {
1840 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1845 /* apply symmetry for BE */
1846 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1847 struct snd_soc_pcm_runtime *be = dpcm->be;
1848 struct snd_pcm_substream *be_substream =
1849 snd_soc_dpcm_get_substream(be, stream);
1850 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1853 if (rtd->dai_link->be_hw_params_fixup)
1856 if (soc_pcm_has_symmetry(be_substream))
1857 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1859 /* Symmetry only applies if we've got an active stream. */
1860 if (rtd->cpu_dai->active) {
1861 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1866 for (i = 0; i < rtd->num_codecs; i++) {
1867 if (rtd->codec_dais[i]->active) {
1868 err = soc_pcm_apply_symmetry(be_substream,
1869 rtd->codec_dais[i]);
1879 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1881 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1882 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1883 int stream = fe_substream->stream, ret = 0;
1885 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1887 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1889 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1893 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1895 /* start the DAI frontend */
1896 ret = soc_pcm_open(fe_substream);
1898 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1902 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1904 dpcm_set_fe_runtime(fe_substream);
1905 snd_pcm_limit_hw_rates(runtime);
1907 ret = dpcm_apply_symmetry(fe_substream, stream);
1909 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1914 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1918 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1920 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1924 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1926 struct snd_soc_dpcm *dpcm;
1928 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1929 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1931 struct snd_soc_pcm_runtime *be = dpcm->be;
1932 struct snd_pcm_substream *be_substream =
1933 snd_soc_dpcm_get_substream(be, stream);
1935 /* is this op for this BE ? */
1936 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1939 if (be->dpcm[stream].users == 0)
1940 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1941 stream ? "capture" : "playback",
1942 be->dpcm[stream].state);
1944 if (--be->dpcm[stream].users != 0)
1947 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1948 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1951 dev_dbg(be->dev, "ASoC: close BE %s\n",
1952 be->dai_link->name);
1954 soc_pcm_close(be_substream);
1955 be_substream->runtime = NULL;
1957 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1962 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1964 struct snd_soc_pcm_runtime *fe = substream->private_data;
1965 int stream = substream->stream;
1967 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1969 /* shutdown the BEs */
1970 dpcm_be_dai_shutdown(fe, substream->stream);
1972 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1974 /* now shutdown the frontend */
1975 soc_pcm_close(substream);
1977 /* run the stream event for each BE */
1978 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1980 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1981 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1985 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1987 struct snd_soc_dpcm *dpcm;
1989 /* only hw_params backends that are either sinks or sources
1990 * to this frontend DAI */
1991 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1993 struct snd_soc_pcm_runtime *be = dpcm->be;
1994 struct snd_pcm_substream *be_substream =
1995 snd_soc_dpcm_get_substream(be, stream);
1997 /* is this op for this BE ? */
1998 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2001 /* only free hw when no longer used - check all FEs */
2002 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2005 /* do not free hw if this BE is used by other FE */
2006 if (be->dpcm[stream].users > 1)
2009 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2010 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2011 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2012 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2013 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2014 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2017 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2018 be->dai_link->name);
2020 soc_pcm_hw_free(be_substream);
2022 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2028 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2030 struct snd_soc_pcm_runtime *fe = substream->private_data;
2031 int err, stream = substream->stream;
2033 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2034 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2036 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2038 /* call hw_free on the frontend */
2039 err = soc_pcm_hw_free(substream);
2041 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2042 fe->dai_link->name);
2044 /* only hw_params backends that are either sinks or sources
2045 * to this frontend DAI */
2046 err = dpcm_be_dai_hw_free(fe, stream);
2048 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2049 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2051 mutex_unlock(&fe->card->mutex);
2055 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2057 struct snd_soc_dpcm *dpcm;
2060 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2062 struct snd_soc_pcm_runtime *be = dpcm->be;
2063 struct snd_pcm_substream *be_substream =
2064 snd_soc_dpcm_get_substream(be, stream);
2066 /* is this op for this BE ? */
2067 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2070 /* copy params for each dpcm */
2071 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2072 sizeof(struct snd_pcm_hw_params));
2074 /* perform any hw_params fixups */
2075 if (be->dai_link->be_hw_params_fixup) {
2076 ret = be->dai_link->be_hw_params_fixup(be,
2080 "ASoC: hw_params BE fixup failed %d\n",
2086 /* only allow hw_params() if no connected FEs are running */
2087 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2090 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2091 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2092 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2095 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2096 be->dai_link->name);
2098 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2100 dev_err(dpcm->be->dev,
2101 "ASoC: hw_params BE failed %d\n", ret);
2105 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2110 /* disable any enabled and non active backends */
2111 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2112 struct snd_soc_pcm_runtime *be = dpcm->be;
2113 struct snd_pcm_substream *be_substream =
2114 snd_soc_dpcm_get_substream(be, stream);
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2119 /* only allow hw_free() if no connected FEs are running */
2120 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2123 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2124 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2125 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2126 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2129 soc_pcm_hw_free(be_substream);
2135 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2136 struct snd_pcm_hw_params *params)
2138 struct snd_soc_pcm_runtime *fe = substream->private_data;
2139 int ret, stream = substream->stream;
2141 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2142 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2144 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2145 sizeof(struct snd_pcm_hw_params));
2146 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2148 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2152 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2153 fe->dai_link->name, params_rate(params),
2154 params_channels(params), params_format(params));
2156 /* call hw_params on the frontend */
2157 ret = soc_pcm_hw_params(substream, params);
2159 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2160 dpcm_be_dai_hw_free(fe, stream);
2162 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2165 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2166 mutex_unlock(&fe->card->mutex);
2170 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2171 struct snd_pcm_substream *substream, int cmd)
2175 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2176 dpcm->be->dai_link->name, cmd);
2178 ret = soc_pcm_trigger(substream, cmd);
2180 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2185 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2188 struct snd_soc_dpcm *dpcm;
2191 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2193 struct snd_soc_pcm_runtime *be = dpcm->be;
2194 struct snd_pcm_substream *be_substream =
2195 snd_soc_dpcm_get_substream(be, stream);
2197 /* is this op for this BE ? */
2198 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2202 case SNDRV_PCM_TRIGGER_START:
2203 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2204 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2207 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2211 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2213 case SNDRV_PCM_TRIGGER_RESUME:
2214 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2217 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2221 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2223 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2224 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2227 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2231 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2233 case SNDRV_PCM_TRIGGER_STOP:
2234 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2237 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2240 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2244 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2246 case SNDRV_PCM_TRIGGER_SUSPEND:
2247 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2250 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2253 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2257 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2259 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2260 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2263 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2266 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2270 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2277 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2279 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2281 struct snd_soc_pcm_runtime *fe = substream->private_data;
2282 int stream = substream->stream, ret;
2283 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2285 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2288 case SND_SOC_DPCM_TRIGGER_PRE:
2289 /* call trigger on the frontend before the backend. */
2291 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2292 fe->dai_link->name, cmd);
2294 ret = soc_pcm_trigger(substream, cmd);
2296 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2300 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2302 case SND_SOC_DPCM_TRIGGER_POST:
2303 /* call trigger on the frontend after the backend. */
2305 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2307 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2311 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2312 fe->dai_link->name, cmd);
2314 ret = soc_pcm_trigger(substream, cmd);
2316 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2317 /* bespoke trigger() - handles both FE and BEs */
2319 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2320 fe->dai_link->name, cmd);
2322 ret = soc_pcm_bespoke_trigger(substream, cmd);
2324 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2329 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2330 fe->dai_link->name);
2336 case SNDRV_PCM_TRIGGER_START:
2337 case SNDRV_PCM_TRIGGER_RESUME:
2338 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2339 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2341 case SNDRV_PCM_TRIGGER_STOP:
2342 case SNDRV_PCM_TRIGGER_SUSPEND:
2343 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2345 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2346 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2351 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2355 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2357 struct snd_soc_pcm_runtime *fe = substream->private_data;
2358 int stream = substream->stream;
2360 /* if FE's runtime_update is already set, we're in race;
2361 * process this trigger later at exit
2363 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2364 fe->dpcm[stream].trigger_pending = cmd + 1;
2365 return 0; /* delayed, assuming it's successful */
2368 /* we're alone, let's trigger */
2369 return dpcm_fe_dai_do_trigger(substream, cmd);
2372 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2374 struct snd_soc_dpcm *dpcm;
2377 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2379 struct snd_soc_pcm_runtime *be = dpcm->be;
2380 struct snd_pcm_substream *be_substream =
2381 snd_soc_dpcm_get_substream(be, stream);
2383 /* is this op for this BE ? */
2384 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2387 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2388 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2389 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2392 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2393 be->dai_link->name);
2395 ret = soc_pcm_prepare(be_substream);
2397 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2402 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2407 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2409 struct snd_soc_pcm_runtime *fe = substream->private_data;
2410 int stream = substream->stream, ret = 0;
2412 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2414 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2416 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2418 /* there is no point preparing this FE if there are no BEs */
2419 if (list_empty(&fe->dpcm[stream].be_clients)) {
2420 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2421 fe->dai_link->name);
2426 ret = dpcm_be_dai_prepare(fe, substream->stream);
2430 /* call prepare on the frontend */
2431 ret = soc_pcm_prepare(substream);
2433 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2434 fe->dai_link->name);
2438 /* run the stream event for each BE */
2439 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2440 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2443 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2444 mutex_unlock(&fe->card->mutex);
2449 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2450 unsigned int cmd, void *arg)
2452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2453 struct snd_soc_platform *platform = rtd->platform;
2454 struct snd_soc_component *component;
2455 struct snd_soc_rtdcom_list *rtdcom;
2457 if (platform && platform->driver->ops && platform->driver->ops->ioctl)
2458 return platform->driver->ops->ioctl(substream, cmd, arg);
2460 for_each_rtdcom(rtd, rtdcom) {
2461 component = rtdcom->component;
2463 /* ignore duplication for now */
2464 if (platform && (component == &platform->component))
2467 if (!component->driver->ops ||
2468 !component->driver->ops->ioctl)
2471 /* FIXME: use 1st ioctl */
2472 return component->driver->ops->ioctl(substream, cmd, arg);
2475 return snd_pcm_lib_ioctl(substream, cmd, arg);
2478 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2480 struct snd_pcm_substream *substream =
2481 snd_soc_dpcm_get_substream(fe, stream);
2482 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2485 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2486 stream ? "capture" : "playback", fe->dai_link->name);
2488 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2489 /* call bespoke trigger - FE takes care of all BE triggers */
2490 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2491 fe->dai_link->name);
2493 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2495 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2497 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2498 fe->dai_link->name);
2500 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2502 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2505 err = dpcm_be_dai_hw_free(fe, stream);
2507 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2509 err = dpcm_be_dai_shutdown(fe, stream);
2511 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2513 /* run the stream event for each BE */
2514 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2519 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2521 struct snd_pcm_substream *substream =
2522 snd_soc_dpcm_get_substream(fe, stream);
2523 struct snd_soc_dpcm *dpcm;
2524 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2527 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2528 stream ? "capture" : "playback", fe->dai_link->name);
2530 /* Only start the BE if the FE is ready */
2531 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2532 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2535 /* startup must always be called for new BEs */
2536 ret = dpcm_be_dai_startup(fe, stream);
2540 /* keep going if FE state is > open */
2541 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2544 ret = dpcm_be_dai_hw_params(fe, stream);
2548 /* keep going if FE state is > hw_params */
2549 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2553 ret = dpcm_be_dai_prepare(fe, stream);
2557 /* run the stream event for each BE */
2558 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2560 /* keep going if FE state is > prepare */
2561 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2562 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2565 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2566 /* call trigger on the frontend - FE takes care of all BE triggers */
2567 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2568 fe->dai_link->name);
2570 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2572 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2576 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2577 fe->dai_link->name);
2579 ret = dpcm_be_dai_trigger(fe, stream,
2580 SNDRV_PCM_TRIGGER_START);
2582 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2590 dpcm_be_dai_hw_free(fe, stream);
2592 dpcm_be_dai_shutdown(fe, stream);
2594 /* disconnect any non started BEs */
2595 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2596 struct snd_soc_pcm_runtime *be = dpcm->be;
2597 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2598 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2604 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2608 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2609 ret = dpcm_run_update_startup(fe, stream);
2611 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2612 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2617 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2621 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2622 ret = dpcm_run_update_shutdown(fe, stream);
2624 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2625 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2630 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2633 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2635 struct snd_soc_pcm_runtime *fe;
2636 int old, new, paths;
2638 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2639 list_for_each_entry(fe, &card->rtd_list, list) {
2640 struct snd_soc_dapm_widget_list *list;
2642 /* make sure link is FE */
2643 if (!fe->dai_link->dynamic)
2646 /* only check active links */
2647 if (!fe->cpu_dai->active)
2650 /* DAPM sync will call this to update DSP paths */
2651 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2652 fe->dai_link->name);
2654 /* skip if FE doesn't have playback capability */
2655 if (!fe->cpu_dai->driver->playback.channels_min
2656 || !fe->codec_dai->driver->playback.channels_min)
2659 /* skip if FE isn't currently playing */
2660 if (!fe->cpu_dai->playback_active
2661 || !fe->codec_dai->playback_active)
2664 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2666 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2667 fe->dai_link->name, "playback");
2668 mutex_unlock(&card->mutex);
2672 /* update any new playback paths */
2673 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2675 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2676 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2677 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2680 /* update any old playback paths */
2681 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2683 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2684 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2685 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2688 dpcm_path_put(&list);
2690 /* skip if FE doesn't have capture capability */
2691 if (!fe->cpu_dai->driver->capture.channels_min
2692 || !fe->codec_dai->driver->capture.channels_min)
2695 /* skip if FE isn't currently capturing */
2696 if (!fe->cpu_dai->capture_active
2697 || !fe->codec_dai->capture_active)
2700 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2702 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2703 fe->dai_link->name, "capture");
2704 mutex_unlock(&card->mutex);
2708 /* update any new capture paths */
2709 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2711 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2712 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2713 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2716 /* update any old capture paths */
2717 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2719 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2720 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2721 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2724 dpcm_path_put(&list);
2727 mutex_unlock(&card->mutex);
2730 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2732 struct snd_soc_dpcm *dpcm;
2733 struct list_head *clients =
2734 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2736 list_for_each_entry(dpcm, clients, list_be) {
2738 struct snd_soc_pcm_runtime *be = dpcm->be;
2741 if (be->dai_link->ignore_suspend)
2744 for (i = 0; i < be->num_codecs; i++) {
2745 struct snd_soc_dai *dai = be->codec_dais[i];
2746 struct snd_soc_dai_driver *drv = dai->driver;
2748 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2749 be->dai_link->name);
2751 if (drv->ops && drv->ops->digital_mute &&
2752 dai->playback_active)
2753 drv->ops->digital_mute(dai, mute);
2760 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2762 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2763 struct snd_soc_dpcm *dpcm;
2764 struct snd_soc_dapm_widget_list *list;
2766 int stream = fe_substream->stream;
2768 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2769 fe->dpcm[stream].runtime = fe_substream->runtime;
2771 ret = dpcm_path_get(fe, stream, &list);
2773 mutex_unlock(&fe->card->mutex);
2775 } else if (ret == 0) {
2776 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2777 fe->dai_link->name, stream ? "capture" : "playback");
2780 /* calculate valid and active FE <-> BE dpcms */
2781 dpcm_process_paths(fe, stream, &list, 1);
2783 ret = dpcm_fe_dai_startup(fe_substream);
2785 /* clean up all links */
2786 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2787 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2789 dpcm_be_disconnect(fe, stream);
2790 fe->dpcm[stream].runtime = NULL;
2793 dpcm_clear_pending_state(fe, stream);
2794 dpcm_path_put(&list);
2795 mutex_unlock(&fe->card->mutex);
2799 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2801 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2802 struct snd_soc_dpcm *dpcm;
2803 int stream = fe_substream->stream, ret;
2805 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2806 ret = dpcm_fe_dai_shutdown(fe_substream);
2808 /* mark FE's links ready to prune */
2809 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2810 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2812 dpcm_be_disconnect(fe, stream);
2814 fe->dpcm[stream].runtime = NULL;
2815 mutex_unlock(&fe->card->mutex);
2819 static void soc_pcm_private_free(struct snd_pcm *pcm)
2821 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2822 struct snd_soc_rtdcom_list *rtdcom;
2823 struct snd_soc_component *component;
2825 for_each_rtdcom(rtd, rtdcom) {
2826 /* need to sync the delayed work before releasing resources */
2828 flush_delayed_work(&rtd->delayed_work);
2829 component = rtdcom->component;
2831 if (component->pcm_free)
2832 component->pcm_free(component, pcm);
2836 static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2838 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2839 struct snd_soc_rtdcom_list *rtdcom;
2840 struct snd_soc_component *component;
2842 for_each_rtdcom(rtd, rtdcom) {
2843 component = rtdcom->component;
2845 if (!component->driver->ops ||
2846 !component->driver->ops->ack)
2849 /* FIXME. it returns 1st ask now */
2850 return component->driver->ops->ack(substream);
2856 static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2857 unsigned long pos, void __user *buf,
2858 unsigned long bytes)
2860 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2861 struct snd_soc_rtdcom_list *rtdcom;
2862 struct snd_soc_component *component;
2864 for_each_rtdcom(rtd, rtdcom) {
2865 component = rtdcom->component;
2867 if (!component->driver->ops ||
2868 !component->driver->ops->copy_user)
2871 /* FIXME. it returns 1st copy now */
2872 return component->driver->ops->copy_user(substream, channel,
2879 static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2880 unsigned long pos, void *buf, unsigned long bytes)
2882 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2883 struct snd_soc_rtdcom_list *rtdcom;
2884 struct snd_soc_component *component;
2886 for_each_rtdcom(rtd, rtdcom) {
2887 component = rtdcom->component;
2889 if (!component->driver->ops ||
2890 !component->driver->ops->copy_kernel)
2893 /* FIXME. it returns 1st copy now */
2894 return component->driver->ops->copy_kernel(substream, channel,
2901 static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2902 unsigned long pos, unsigned long bytes)
2904 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2905 struct snd_soc_rtdcom_list *rtdcom;
2906 struct snd_soc_component *component;
2908 for_each_rtdcom(rtd, rtdcom) {
2909 component = rtdcom->component;
2911 if (!component->driver->ops ||
2912 !component->driver->ops->fill_silence)
2915 /* FIXME. it returns 1st silence now */
2916 return component->driver->ops->fill_silence(substream, channel,
2923 static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2924 unsigned long offset)
2926 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2927 struct snd_soc_rtdcom_list *rtdcom;
2928 struct snd_soc_component *component;
2931 for_each_rtdcom(rtd, rtdcom) {
2932 component = rtdcom->component;
2934 if (!component->driver->ops ||
2935 !component->driver->ops->page)
2938 /* FIXME. it returns 1st page now */
2939 page = component->driver->ops->page(substream, offset);
2947 static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2948 struct vm_area_struct *vma)
2950 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2951 struct snd_soc_rtdcom_list *rtdcom;
2952 struct snd_soc_component *component;
2954 for_each_rtdcom(rtd, rtdcom) {
2955 component = rtdcom->component;
2957 if (!component->driver->ops ||
2958 !component->driver->ops->mmap)
2961 /* FIXME. it returns 1st mmap now */
2962 return component->driver->ops->mmap(substream, vma);
2968 /* create a new pcm */
2969 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2971 struct snd_soc_platform *platform = rtd->platform;
2972 struct snd_soc_dai *codec_dai;
2973 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2974 struct snd_soc_component *component;
2975 struct snd_soc_rtdcom_list *rtdcom;
2976 struct snd_pcm *pcm;
2978 int ret = 0, playback = 0, capture = 0;
2981 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2982 playback = rtd->dai_link->dpcm_playback;
2983 capture = rtd->dai_link->dpcm_capture;
2985 for (i = 0; i < rtd->num_codecs; i++) {
2986 codec_dai = rtd->codec_dais[i];
2987 if (codec_dai->driver->playback.channels_min)
2989 if (codec_dai->driver->capture.channels_min)
2993 capture = capture && cpu_dai->driver->capture.channels_min;
2994 playback = playback && cpu_dai->driver->playback.channels_min;
2997 if (rtd->dai_link->playback_only) {
3002 if (rtd->dai_link->capture_only) {
3007 /* create the PCM */
3008 if (rtd->dai_link->no_pcm) {
3009 snprintf(new_name, sizeof(new_name), "(%s)",
3010 rtd->dai_link->stream_name);
3012 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3013 playback, capture, &pcm);
3015 if (rtd->dai_link->dynamic)
3016 snprintf(new_name, sizeof(new_name), "%s (*)",
3017 rtd->dai_link->stream_name);
3019 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3020 rtd->dai_link->stream_name,
3021 (rtd->num_codecs > 1) ?
3022 "multicodec" : rtd->codec_dai->name, num);
3024 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3028 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3029 rtd->dai_link->name);
3032 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3034 /* DAPM dai link stream work */
3035 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3037 pcm->nonatomic = rtd->dai_link->nonatomic;
3039 pcm->private_data = rtd;
3041 if (rtd->dai_link->no_pcm) {
3043 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3045 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3049 /* ASoC PCM operations */
3050 if (rtd->dai_link->dynamic) {
3051 rtd->ops.open = dpcm_fe_dai_open;
3052 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3053 rtd->ops.prepare = dpcm_fe_dai_prepare;
3054 rtd->ops.trigger = dpcm_fe_dai_trigger;
3055 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3056 rtd->ops.close = dpcm_fe_dai_close;
3057 rtd->ops.pointer = soc_pcm_pointer;
3058 rtd->ops.ioctl = soc_pcm_ioctl;
3060 rtd->ops.open = soc_pcm_open;
3061 rtd->ops.hw_params = soc_pcm_hw_params;
3062 rtd->ops.prepare = soc_pcm_prepare;
3063 rtd->ops.trigger = soc_pcm_trigger;
3064 rtd->ops.hw_free = soc_pcm_hw_free;
3065 rtd->ops.close = soc_pcm_close;
3066 rtd->ops.pointer = soc_pcm_pointer;
3067 rtd->ops.ioctl = soc_pcm_ioctl;
3070 for_each_rtdcom(rtd, rtdcom) {
3071 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3077 rtd->ops.ack = soc_rtdcom_ack;
3079 rtd->ops.copy_user = soc_rtdcom_copy_user;
3080 if (ops->copy_kernel)
3081 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3082 if (ops->fill_silence)
3083 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3085 rtd->ops.page = soc_rtdcom_page;
3087 rtd->ops.mmap = soc_rtdcom_mmap;
3091 if (platform && platform->driver->ops) {
3092 rtd->ops.ack = platform->driver->ops->ack;
3093 rtd->ops.copy_user = platform->driver->ops->copy_user;
3094 rtd->ops.copy_kernel = platform->driver->ops->copy_kernel;
3095 rtd->ops.fill_silence = platform->driver->ops->fill_silence;
3096 rtd->ops.page = platform->driver->ops->page;
3097 rtd->ops.mmap = platform->driver->ops->mmap;
3101 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3104 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3106 for_each_rtdcom(rtd, rtdcom) {
3107 component = rtdcom->component;
3109 if (!component->pcm_new)
3112 ret = component->pcm_new(component, rtd);
3114 dev_err(component->dev,
3115 "ASoC: pcm constructor failed: %d\n",
3121 pcm->private_free = soc_pcm_private_free;
3123 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3124 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3129 /* is the current PCM operation for this FE ? */
3130 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3132 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3136 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3138 /* is the current PCM operation for this BE ? */
3139 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3140 struct snd_soc_pcm_runtime *be, int stream)
3142 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3143 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3144 be->dpcm[stream].runtime_update))
3148 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3150 /* get the substream for this BE */
3151 struct snd_pcm_substream *
3152 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3154 return be->pcm->streams[stream].substream;
3156 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3158 /* get the BE runtime state */
3159 enum snd_soc_dpcm_state
3160 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3162 return be->dpcm[stream].state;
3164 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3166 /* set the BE runtime state */
3167 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3168 int stream, enum snd_soc_dpcm_state state)
3170 be->dpcm[stream].state = state;
3172 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3175 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3176 * are not running, paused or suspended for the specified stream direction.
3178 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3179 struct snd_soc_pcm_runtime *be, int stream)
3181 struct snd_soc_dpcm *dpcm;
3184 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3189 state = dpcm->fe->dpcm[stream].state;
3190 if (state == SND_SOC_DPCM_STATE_START ||
3191 state == SND_SOC_DPCM_STATE_PAUSED ||
3192 state == SND_SOC_DPCM_STATE_SUSPEND)
3196 /* it's safe to free/stop this BE DAI */
3199 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3202 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3203 * running, paused or suspended for the specified stream direction.
3205 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3206 struct snd_soc_pcm_runtime *be, int stream)
3208 struct snd_soc_dpcm *dpcm;
3211 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3216 state = dpcm->fe->dpcm[stream].state;
3217 if (state == SND_SOC_DPCM_STATE_START ||
3218 state == SND_SOC_DPCM_STATE_PAUSED ||
3219 state == SND_SOC_DPCM_STATE_SUSPEND ||
3220 state == SND_SOC_DPCM_STATE_PREPARE)
3224 /* it's safe to change hw_params */
3227 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3229 #ifdef CONFIG_DEBUG_FS
3230 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3233 case SND_SOC_DPCM_STATE_NEW:
3235 case SND_SOC_DPCM_STATE_OPEN:
3237 case SND_SOC_DPCM_STATE_HW_PARAMS:
3239 case SND_SOC_DPCM_STATE_PREPARE:
3241 case SND_SOC_DPCM_STATE_START:
3243 case SND_SOC_DPCM_STATE_STOP:
3245 case SND_SOC_DPCM_STATE_SUSPEND:
3247 case SND_SOC_DPCM_STATE_PAUSED:
3249 case SND_SOC_DPCM_STATE_HW_FREE:
3251 case SND_SOC_DPCM_STATE_CLOSE:
3258 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3259 int stream, char *buf, size_t size)
3261 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3262 struct snd_soc_dpcm *dpcm;
3266 offset += snprintf(buf + offset, size - offset,
3267 "[%s - %s]\n", fe->dai_link->name,
3268 stream ? "Capture" : "Playback");
3270 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3271 dpcm_state_string(fe->dpcm[stream].state));
3273 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3274 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3275 offset += snprintf(buf + offset, size - offset,
3277 "Format = %s, Channels = %d, Rate = %d\n",
3278 snd_pcm_format_name(params_format(params)),
3279 params_channels(params),
3280 params_rate(params));
3283 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3285 if (list_empty(&fe->dpcm[stream].be_clients)) {
3286 offset += snprintf(buf + offset, size - offset,
3287 " No active DSP links\n");
3291 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3292 struct snd_soc_pcm_runtime *be = dpcm->be;
3293 params = &dpcm->hw_params;
3295 offset += snprintf(buf + offset, size - offset,
3296 "- %s\n", be->dai_link->name);
3298 offset += snprintf(buf + offset, size - offset,
3300 dpcm_state_string(be->dpcm[stream].state));
3302 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3303 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3304 offset += snprintf(buf + offset, size - offset,
3305 " Hardware Params: "
3306 "Format = %s, Channels = %d, Rate = %d\n",
3307 snd_pcm_format_name(params_format(params)),
3308 params_channels(params),
3309 params_rate(params));
3316 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3317 size_t count, loff_t *ppos)
3319 struct snd_soc_pcm_runtime *fe = file->private_data;
3320 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3323 buf = kmalloc(out_count, GFP_KERNEL);
3327 if (fe->cpu_dai->driver->playback.channels_min)
3328 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3329 buf + offset, out_count - offset);
3331 if (fe->cpu_dai->driver->capture.channels_min)
3332 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3333 buf + offset, out_count - offset);
3335 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3341 static const struct file_operations dpcm_state_fops = {
3342 .open = simple_open,
3343 .read = dpcm_state_read_file,
3344 .llseek = default_llseek,
3347 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3352 if (!rtd->card->debugfs_card_root)
3355 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3356 rtd->card->debugfs_card_root);
3357 if (!rtd->debugfs_dpcm_root) {
3359 "ASoC: Failed to create dpcm debugfs directory %s\n",
3360 rtd->dai_link->name);
3364 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3365 rtd, &dpcm_state_fops);