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_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
45 * Must be called with the rtd->pcm_mutex being held
47 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
52 lockdep_assert_held(&rtd->pcm_mutex);
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 for (i = 0; i < rtd->num_codecs; i++)
57 rtd->codec_dais[i]->playback_active++;
59 cpu_dai->capture_active++;
60 for (i = 0; i < rtd->num_codecs; i++)
61 rtd->codec_dais[i]->capture_active++;
65 cpu_dai->component->active++;
66 for (i = 0; i < rtd->num_codecs; i++) {
67 rtd->codec_dais[i]->active++;
68 rtd->codec_dais[i]->component->active++;
73 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
74 * @rtd: ASoC PCM runtime that is deactivated
75 * @stream: Direction of the PCM stream
77 * Decrements the active count for all the DAIs and components attached to a PCM
78 * runtime. Should typically be called when a stream is closed.
80 * Must be called with the rtd->pcm_mutex being held
82 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
84 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
87 lockdep_assert_held(&rtd->pcm_mutex);
89 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 cpu_dai->playback_active--;
91 for (i = 0; i < rtd->num_codecs; i++)
92 rtd->codec_dais[i]->playback_active--;
94 cpu_dai->capture_active--;
95 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->capture_active--;
100 cpu_dai->component->active--;
101 for (i = 0; i < rtd->num_codecs; i++) {
102 rtd->codec_dais[i]->component->active--;
103 rtd->codec_dais[i]->active--;
108 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109 * @rtd: The ASoC PCM runtime that should be checked.
111 * This function checks whether the power down delay should be ignored for a
112 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113 * been configured to ignore the delay, or if none of the components benefits
114 * from having the delay.
116 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
121 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
124 for (i = 0; i < rtd->num_codecs; i++)
125 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
127 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
135 * Sets the substream runtime hardware parameters.
137 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
151 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
153 /* DPCM stream event, send event to FE and all active BEs. */
154 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
157 struct snd_soc_dpcm *dpcm;
159 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
161 struct snd_soc_pcm_runtime *be = dpcm->be;
163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
164 be->dai_link->name, event, dir);
166 snd_soc_dapm_stream_event(be, dir, event);
169 snd_soc_dapm_stream_event(fe, dir, event);
174 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
175 struct snd_soc_dai *soc_dai)
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
180 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
181 rtd->dai_link->symmetric_rates)) {
182 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
185 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
186 SNDRV_PCM_HW_PARAM_RATE,
187 soc_dai->rate, soc_dai->rate);
189 dev_err(soc_dai->dev,
190 "ASoC: Unable to apply rate constraint: %d\n",
196 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
197 rtd->dai_link->symmetric_channels)) {
198 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
201 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
202 SNDRV_PCM_HW_PARAM_CHANNELS,
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
218 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 soc_dai->sample_bits,
221 soc_dai->sample_bits);
223 dev_err(soc_dai->dev,
224 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
233 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
236 struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
238 unsigned int rate, channels, sample_bits, symmetry, i;
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
246 rtd->dai_link->symmetric_rates;
248 for (i = 0; i < rtd->num_codecs; i++)
249 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
257 symmetry = cpu_dai->driver->symmetric_channels ||
258 rtd->dai_link->symmetric_channels;
260 for (i = 0; i < rtd->num_codecs; i++)
261 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
270 rtd->dai_link->symmetric_samplebits;
272 for (i = 0; i < rtd->num_codecs; i++)
273 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
284 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
288 struct snd_soc_dai_link *link = rtd->dai_link;
289 unsigned int symmetry, i;
291 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
292 cpu_driver->symmetric_channels || link->symmetric_channels ||
293 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
295 for (i = 0; i < rtd->num_codecs; i++)
296 symmetry = symmetry ||
297 rtd->codec_dais[i]->driver->symmetric_rates ||
298 rtd->codec_dais[i]->driver->symmetric_channels ||
299 rtd->codec_dais[i]->driver->symmetric_samplebits;
305 * List of sample sizes that might go over the bus for parameter
306 * application. There ought to be a wildcard sample size for things
307 * like the DAC/ADC resolution to use but there isn't right now.
309 static int sample_sizes[] = {
313 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
315 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
322 if (bits >= sample_sizes[i])
325 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
326 sample_sizes[i], bits);
329 "ASoC: Failed to set MSB %d/%d: %d\n",
330 bits, sample_sizes[i], ret);
334 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
336 struct snd_soc_pcm_runtime *rtd = substream->private_data;
337 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
338 struct snd_soc_dai *codec_dai;
340 unsigned int bits = 0, cpu_bits;
342 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
343 for (i = 0; i < rtd->num_codecs; i++) {
344 codec_dai = rtd->codec_dais[i];
345 if (codec_dai->driver->playback.sig_bits == 0) {
349 bits = max(codec_dai->driver->playback.sig_bits, bits);
351 cpu_bits = cpu_dai->driver->playback.sig_bits;
353 for (i = 0; i < rtd->num_codecs; i++) {
354 codec_dai = rtd->codec_dais[i];
355 if (codec_dai->driver->capture.sig_bits == 0) {
359 bits = max(codec_dai->driver->capture.sig_bits, bits);
361 cpu_bits = cpu_dai->driver->capture.sig_bits;
364 soc_pcm_set_msb(substream, bits);
365 soc_pcm_set_msb(substream, cpu_bits);
368 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
370 struct snd_pcm_runtime *runtime = substream->runtime;
371 struct snd_pcm_hardware *hw = &runtime->hw;
372 struct snd_soc_pcm_runtime *rtd = substream->private_data;
373 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
374 struct snd_soc_dai_driver *codec_dai_drv;
375 struct snd_soc_pcm_stream *codec_stream;
376 struct snd_soc_pcm_stream *cpu_stream;
377 unsigned int chan_min = 0, chan_max = UINT_MAX;
378 unsigned int rate_min = 0, rate_max = UINT_MAX;
379 unsigned int rates = UINT_MAX;
380 u64 formats = ULLONG_MAX;
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
384 cpu_stream = &cpu_dai_drv->playback;
386 cpu_stream = &cpu_dai_drv->capture;
388 /* first calculate min/max only for CODECs in the DAI link */
389 for (i = 0; i < rtd->num_codecs; i++) {
390 codec_dai_drv = rtd->codec_dais[i]->driver;
391 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
392 codec_stream = &codec_dai_drv->playback;
394 codec_stream = &codec_dai_drv->capture;
395 chan_min = max(chan_min, codec_stream->channels_min);
396 chan_max = min(chan_max, codec_stream->channels_max);
397 rate_min = max(rate_min, codec_stream->rate_min);
398 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
399 formats &= codec_stream->formats;
400 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
404 * chan min/max cannot be enforced if there are multiple CODEC DAIs
405 * connected to a single CPU DAI, use CPU DAI's directly and let
406 * channel allocation be fixed up later
408 if (rtd->num_codecs > 1) {
409 chan_min = cpu_stream->channels_min;
410 chan_max = cpu_stream->channels_max;
413 hw->channels_min = max(chan_min, cpu_stream->channels_min);
414 hw->channels_max = min(chan_max, cpu_stream->channels_max);
416 hw->formats &= formats & cpu_stream->formats;
418 hw->formats = formats & cpu_stream->formats;
419 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
421 snd_pcm_limit_hw_rates(runtime);
423 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
424 hw->rate_min = max(hw->rate_min, rate_min);
425 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
426 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
430 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
431 * then initialized and any private data can be allocated. This also calls
432 * startup for the cpu DAI, platform, machine and codec DAI.
434 static int soc_pcm_open(struct snd_pcm_substream *substream)
436 struct snd_soc_pcm_runtime *rtd = substream->private_data;
437 struct snd_pcm_runtime *runtime = substream->runtime;
438 struct snd_soc_platform *platform = rtd->platform;
439 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
440 struct snd_soc_dai *codec_dai;
441 const char *codec_dai_name = "multicodec";
444 pinctrl_pm_select_default_state(cpu_dai->dev);
445 for (i = 0; i < rtd->num_codecs; i++)
446 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
447 pm_runtime_get_sync(cpu_dai->dev);
448 for (i = 0; i < rtd->num_codecs; i++)
449 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
450 pm_runtime_get_sync(platform->dev);
452 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
454 /* startup the audio subsystem */
455 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
456 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
458 dev_err(cpu_dai->dev, "ASoC: can't open interface"
459 " %s: %d\n", cpu_dai->name, ret);
464 if (platform->driver->ops && platform->driver->ops->open) {
465 ret = platform->driver->ops->open(substream);
467 dev_err(platform->dev, "ASoC: can't open platform"
468 " %s: %d\n", platform->component.name, ret);
473 for (i = 0; i < rtd->num_codecs; i++) {
474 codec_dai = rtd->codec_dais[i];
475 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
476 ret = codec_dai->driver->ops->startup(substream,
479 dev_err(codec_dai->dev,
480 "ASoC: can't open codec %s: %d\n",
481 codec_dai->name, ret);
486 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
487 codec_dai->tx_mask = 0;
489 codec_dai->rx_mask = 0;
492 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
493 ret = rtd->dai_link->ops->startup(substream);
495 pr_err("ASoC: %s startup failed: %d\n",
496 rtd->dai_link->name, ret);
501 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
502 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
505 /* Check that the codec and cpu DAIs are compatible */
506 soc_pcm_init_runtime_hw(substream);
508 if (rtd->num_codecs == 1)
509 codec_dai_name = rtd->codec_dai->name;
511 if (soc_pcm_has_symmetry(substream))
512 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
515 if (!runtime->hw.rates) {
516 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
517 codec_dai_name, cpu_dai->name);
520 if (!runtime->hw.formats) {
521 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
522 codec_dai_name, cpu_dai->name);
525 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
526 runtime->hw.channels_min > runtime->hw.channels_max) {
527 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
528 codec_dai_name, cpu_dai->name);
532 soc_pcm_apply_msb(substream);
534 /* Symmetry only applies if we've already got an active stream. */
535 if (cpu_dai->active) {
536 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
541 for (i = 0; i < rtd->num_codecs; i++) {
542 if (rtd->codec_dais[i]->active) {
543 ret = soc_pcm_apply_symmetry(substream,
550 pr_debug("ASoC: %s <-> %s info:\n",
551 codec_dai_name, cpu_dai->name);
552 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
553 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
554 runtime->hw.channels_max);
555 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
556 runtime->hw.rate_max);
560 snd_soc_runtime_activate(rtd, substream->stream);
562 mutex_unlock(&rtd->pcm_mutex);
566 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
567 rtd->dai_link->ops->shutdown(substream);
574 codec_dai = rtd->codec_dais[i];
575 if (codec_dai->driver->ops->shutdown)
576 codec_dai->driver->ops->shutdown(substream, codec_dai);
579 if (platform->driver->ops && platform->driver->ops->close)
580 platform->driver->ops->close(substream);
583 if (cpu_dai->driver->ops->shutdown)
584 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
586 mutex_unlock(&rtd->pcm_mutex);
588 pm_runtime_put(platform->dev);
589 for (i = 0; i < rtd->num_codecs; i++)
590 pm_runtime_put(rtd->codec_dais[i]->dev);
591 pm_runtime_put(cpu_dai->dev);
592 for (i = 0; i < rtd->num_codecs; i++) {
593 if (!rtd->codec_dais[i]->active)
594 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
596 if (!cpu_dai->active)
597 pinctrl_pm_select_sleep_state(cpu_dai->dev);
603 * Power down the audio subsystem pmdown_time msecs after close is called.
604 * This is to ensure there are no pops or clicks in between any music tracks
605 * due to DAPM power cycling.
607 static void close_delayed_work(struct work_struct *work)
609 struct snd_soc_pcm_runtime *rtd =
610 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
611 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
613 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
615 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
616 codec_dai->driver->playback.stream_name,
617 codec_dai->playback_active ? "active" : "inactive",
618 rtd->pop_wait ? "yes" : "no");
620 /* are we waiting on this codec DAI stream */
621 if (rtd->pop_wait == 1) {
623 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
624 SND_SOC_DAPM_STREAM_STOP);
627 mutex_unlock(&rtd->pcm_mutex);
631 * Called by ALSA when a PCM substream is closed. Private data can be
632 * freed here. The cpu DAI, codec DAI, machine and platform are also
635 static int soc_pcm_close(struct snd_pcm_substream *substream)
637 struct snd_soc_pcm_runtime *rtd = substream->private_data;
638 struct snd_soc_platform *platform = rtd->platform;
639 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
640 struct snd_soc_dai *codec_dai;
643 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
645 snd_soc_runtime_deactivate(rtd, substream->stream);
647 /* clear the corresponding DAIs rate when inactive */
648 if (!cpu_dai->active)
651 for (i = 0; i < rtd->num_codecs; i++) {
652 codec_dai = rtd->codec_dais[i];
653 if (!codec_dai->active)
657 if (cpu_dai->driver->ops->shutdown)
658 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
660 for (i = 0; i < rtd->num_codecs; i++) {
661 codec_dai = rtd->codec_dais[i];
662 if (codec_dai->driver->ops->shutdown)
663 codec_dai->driver->ops->shutdown(substream, codec_dai);
666 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
667 rtd->dai_link->ops->shutdown(substream);
669 if (platform->driver->ops && platform->driver->ops->close)
670 platform->driver->ops->close(substream);
672 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
673 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
674 /* powered down playback stream now */
675 snd_soc_dapm_stream_event(rtd,
676 SNDRV_PCM_STREAM_PLAYBACK,
677 SND_SOC_DAPM_STREAM_STOP);
679 /* start delayed pop wq here for playback streams */
681 queue_delayed_work(system_power_efficient_wq,
683 msecs_to_jiffies(rtd->pmdown_time));
686 /* capture streams can be powered down now */
687 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
688 SND_SOC_DAPM_STREAM_STOP);
691 mutex_unlock(&rtd->pcm_mutex);
693 pm_runtime_put(platform->dev);
694 for (i = 0; i < rtd->num_codecs; i++)
695 pm_runtime_put(rtd->codec_dais[i]->dev);
696 pm_runtime_put(cpu_dai->dev);
697 for (i = 0; i < rtd->num_codecs; i++) {
698 if (!rtd->codec_dais[i]->active)
699 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
701 if (!cpu_dai->active)
702 pinctrl_pm_select_sleep_state(cpu_dai->dev);
708 * Called by ALSA when the PCM substream is prepared, can set format, sample
709 * rate, etc. This function is non atomic and can be called multiple times,
710 * it can refer to the runtime info.
712 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
714 struct snd_soc_pcm_runtime *rtd = substream->private_data;
715 struct snd_soc_platform *platform = rtd->platform;
716 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
717 struct snd_soc_dai *codec_dai;
720 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
722 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
723 ret = rtd->dai_link->ops->prepare(substream);
725 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
731 if (platform->driver->ops && platform->driver->ops->prepare) {
732 ret = platform->driver->ops->prepare(substream);
734 dev_err(platform->dev, "ASoC: platform prepare error:"
740 for (i = 0; i < rtd->num_codecs; i++) {
741 codec_dai = rtd->codec_dais[i];
742 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
743 ret = codec_dai->driver->ops->prepare(substream,
746 dev_err(codec_dai->dev,
747 "ASoC: DAI prepare error: %d\n", ret);
753 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
754 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
756 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
762 /* cancel any delayed stream shutdown that is pending */
763 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
766 cancel_delayed_work(&rtd->delayed_work);
769 snd_soc_dapm_stream_event(rtd, substream->stream,
770 SND_SOC_DAPM_STREAM_START);
772 for (i = 0; i < rtd->num_codecs; i++)
773 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
777 mutex_unlock(&rtd->pcm_mutex);
781 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
784 struct snd_interval *interval;
785 int channels = hweight_long(mask);
787 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
788 interval->min = channels;
789 interval->max = channels;
792 int soc_dai_hw_params(struct snd_pcm_substream *substream,
793 struct snd_pcm_hw_params *params,
794 struct snd_soc_dai *dai)
798 if (dai->driver->ops && dai->driver->ops->hw_params) {
799 ret = dai->driver->ops->hw_params(substream, params, dai);
801 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
811 * Called by ALSA when the hardware params are set by application. This
812 * function can also be called multiple times and can allocate buffers
813 * (using snd_pcm_lib_* ). It's non-atomic.
815 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
816 struct snd_pcm_hw_params *params)
818 struct snd_soc_pcm_runtime *rtd = substream->private_data;
819 struct snd_soc_platform *platform = rtd->platform;
820 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
823 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
825 ret = soc_pcm_params_symmetry(substream, params);
829 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
830 ret = rtd->dai_link->ops->hw_params(substream, params);
832 dev_err(rtd->card->dev, "ASoC: machine hw_params"
833 " failed: %d\n", ret);
838 for (i = 0; i < rtd->num_codecs; i++) {
839 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
840 struct snd_pcm_hw_params codec_params;
842 /* copy params for each codec */
843 codec_params = *params;
845 /* fixup params based on TDM slot masks */
846 if (codec_dai->tx_mask)
847 soc_pcm_codec_params_fixup(&codec_params,
849 if (codec_dai->rx_mask)
850 soc_pcm_codec_params_fixup(&codec_params,
853 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
857 codec_dai->rate = params_rate(&codec_params);
858 codec_dai->channels = params_channels(&codec_params);
859 codec_dai->sample_bits = snd_pcm_format_physical_width(
860 params_format(&codec_params));
863 ret = soc_dai_hw_params(substream, params, cpu_dai);
867 if (platform->driver->ops && platform->driver->ops->hw_params) {
868 ret = platform->driver->ops->hw_params(substream, params);
870 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
871 platform->component.name, ret);
876 /* store the parameters for each DAIs */
877 cpu_dai->rate = params_rate(params);
878 cpu_dai->channels = params_channels(params);
879 cpu_dai->sample_bits =
880 snd_pcm_format_physical_width(params_format(params));
883 mutex_unlock(&rtd->pcm_mutex);
887 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
888 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
895 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
896 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
897 codec_dai->driver->ops->hw_free(substream, codec_dai);
901 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
902 rtd->dai_link->ops->hw_free(substream);
904 mutex_unlock(&rtd->pcm_mutex);
909 * Frees resources allocated by hw_params, can be called multiple times
911 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
913 struct snd_soc_pcm_runtime *rtd = substream->private_data;
914 struct snd_soc_platform *platform = rtd->platform;
915 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
916 struct snd_soc_dai *codec_dai;
917 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
920 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
922 /* clear the corresponding DAIs parameters when going to be inactive */
923 if (cpu_dai->active == 1) {
925 cpu_dai->channels = 0;
926 cpu_dai->sample_bits = 0;
929 for (i = 0; i < rtd->num_codecs; i++) {
930 codec_dai = rtd->codec_dais[i];
931 if (codec_dai->active == 1) {
933 codec_dai->channels = 0;
934 codec_dai->sample_bits = 0;
938 /* apply codec digital mute */
939 for (i = 0; i < rtd->num_codecs; i++) {
940 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
941 (!playback && rtd->codec_dais[i]->capture_active == 1))
942 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
946 /* free any machine hw params */
947 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
948 rtd->dai_link->ops->hw_free(substream);
950 /* free any DMA resources */
951 if (platform->driver->ops && platform->driver->ops->hw_free)
952 platform->driver->ops->hw_free(substream);
954 /* now free hw params for the DAIs */
955 for (i = 0; i < rtd->num_codecs; i++) {
956 codec_dai = rtd->codec_dais[i];
957 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
958 codec_dai->driver->ops->hw_free(substream, codec_dai);
961 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
962 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
964 mutex_unlock(&rtd->pcm_mutex);
968 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
970 struct snd_soc_pcm_runtime *rtd = substream->private_data;
971 struct snd_soc_platform *platform = rtd->platform;
972 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
973 struct snd_soc_dai *codec_dai;
976 for (i = 0; i < rtd->num_codecs; i++) {
977 codec_dai = rtd->codec_dais[i];
978 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
979 ret = codec_dai->driver->ops->trigger(substream,
986 if (platform->driver->ops && platform->driver->ops->trigger) {
987 ret = platform->driver->ops->trigger(substream, cmd);
992 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
993 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
998 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
999 ret = rtd->dai_link->ops->trigger(substream, cmd);
1007 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1010 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1011 struct snd_soc_platform *platform = rtd->platform;
1012 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1013 struct snd_soc_dai *codec_dai;
1016 for (i = 0; i < rtd->num_codecs; i++) {
1017 codec_dai = rtd->codec_dais[i];
1018 if (codec_dai->driver->ops &&
1019 codec_dai->driver->ops->bespoke_trigger) {
1020 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1027 if (platform->driver->bespoke_trigger) {
1028 ret = platform->driver->bespoke_trigger(substream, cmd);
1033 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1034 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1041 * soc level wrapper for pointer callback
1042 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1043 * the runtime->delay will be updated accordingly.
1045 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1047 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1048 struct snd_soc_platform *platform = rtd->platform;
1049 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1050 struct snd_soc_dai *codec_dai;
1051 struct snd_pcm_runtime *runtime = substream->runtime;
1052 snd_pcm_uframes_t offset = 0;
1053 snd_pcm_sframes_t delay = 0;
1054 snd_pcm_sframes_t codec_delay = 0;
1057 if (platform->driver->ops && platform->driver->ops->pointer)
1058 offset = platform->driver->ops->pointer(substream);
1060 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1061 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1063 for (i = 0; i < rtd->num_codecs; i++) {
1064 codec_dai = rtd->codec_dais[i];
1065 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1066 codec_delay = max(codec_delay,
1067 codec_dai->driver->ops->delay(substream,
1070 delay += codec_delay;
1073 * None of the existing platform drivers implement delay(), so
1074 * for now the codec_dai of first multicodec entry is used
1076 if (platform->driver->delay)
1077 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1079 runtime->delay = delay;
1084 /* connect a FE and BE */
1085 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1086 struct snd_soc_pcm_runtime *be, int stream)
1088 struct snd_soc_dpcm *dpcm;
1090 /* only add new dpcms */
1091 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1092 if (dpcm->be == be && dpcm->fe == fe)
1096 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1102 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1103 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1104 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1105 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1107 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1108 stream ? "capture" : "playback", fe->dai_link->name,
1109 stream ? "<-" : "->", be->dai_link->name);
1111 #ifdef CONFIG_DEBUG_FS
1112 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1113 fe->debugfs_dpcm_root, &dpcm->state);
1118 /* reparent a BE onto another FE */
1119 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1120 struct snd_soc_pcm_runtime *be, int stream)
1122 struct snd_soc_dpcm *dpcm;
1123 struct snd_pcm_substream *fe_substream, *be_substream;
1125 /* reparent if BE is connected to other FEs */
1126 if (!be->dpcm[stream].users)
1129 be_substream = snd_soc_dpcm_get_substream(be, stream);
1131 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1135 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1136 stream ? "capture" : "playback",
1137 dpcm->fe->dai_link->name,
1138 stream ? "<-" : "->", dpcm->be->dai_link->name);
1140 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1141 be_substream->runtime = fe_substream->runtime;
1146 /* disconnect a BE and FE */
1147 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1149 struct snd_soc_dpcm *dpcm, *d;
1151 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1152 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1153 stream ? "capture" : "playback",
1154 dpcm->be->dai_link->name);
1156 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1159 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1160 stream ? "capture" : "playback", fe->dai_link->name,
1161 stream ? "<-" : "->", dpcm->be->dai_link->name);
1163 /* BEs still alive need new FE */
1164 dpcm_be_reparent(fe, dpcm->be, stream);
1166 #ifdef CONFIG_DEBUG_FS
1167 debugfs_remove(dpcm->debugfs_state);
1169 list_del(&dpcm->list_be);
1170 list_del(&dpcm->list_fe);
1175 /* get BE for DAI widget and stream */
1176 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1177 struct snd_soc_dapm_widget *widget, int stream)
1179 struct snd_soc_pcm_runtime *be;
1182 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1183 for (i = 0; i < card->num_links; i++) {
1186 if (!be->dai_link->no_pcm)
1189 if (be->cpu_dai->playback_widget == widget)
1192 for (j = 0; j < be->num_codecs; j++) {
1193 struct snd_soc_dai *dai = be->codec_dais[j];
1194 if (dai->playback_widget == widget)
1200 for (i = 0; i < card->num_links; i++) {
1203 if (!be->dai_link->no_pcm)
1206 if (be->cpu_dai->capture_widget == widget)
1209 for (j = 0; j < be->num_codecs; j++) {
1210 struct snd_soc_dai *dai = be->codec_dais[j];
1211 if (dai->capture_widget == widget)
1217 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1218 stream ? "capture" : "playback", widget->name);
1222 static inline struct snd_soc_dapm_widget *
1223 dai_get_widget(struct snd_soc_dai *dai, int stream)
1225 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1226 return dai->playback_widget;
1228 return dai->capture_widget;
1231 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1232 struct snd_soc_dapm_widget *widget)
1236 for (i = 0; i < list->num_widgets; i++) {
1237 if (widget == list->widgets[i])
1244 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1245 int stream, struct snd_soc_dapm_widget_list **list_)
1247 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1248 struct snd_soc_dapm_widget_list *list;
1251 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1252 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1256 /* get number of valid DAI paths and their widgets */
1257 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1259 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1260 stream ? "capture" : "playback");
1266 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1267 struct snd_soc_dapm_widget_list **list_)
1269 struct snd_soc_dpcm *dpcm;
1270 struct snd_soc_dapm_widget_list *list = *list_;
1271 struct snd_soc_dapm_widget *widget;
1274 /* Destroy any old FE <--> BE connections */
1275 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1278 /* is there a valid CPU DAI widget for this BE */
1279 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1281 /* prune the BE if it's no longer in our active list */
1282 if (widget && widget_in_list(list, widget))
1285 /* is there a valid CODEC DAI widget for this BE */
1286 for (i = 0; i < dpcm->be->num_codecs; i++) {
1287 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1288 widget = dai_get_widget(dai, stream);
1290 /* prune the BE if it's no longer in our active list */
1291 if (widget && widget_in_list(list, widget))
1295 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1296 stream ? "capture" : "playback",
1297 dpcm->be->dai_link->name, fe->dai_link->name);
1298 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1299 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1303 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1307 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1308 struct snd_soc_dapm_widget_list **list_)
1310 struct snd_soc_card *card = fe->card;
1311 struct snd_soc_dapm_widget_list *list = *list_;
1312 struct snd_soc_pcm_runtime *be;
1313 int i, new = 0, err;
1315 /* Create any new FE <--> BE connections */
1316 for (i = 0; i < list->num_widgets; i++) {
1318 switch (list->widgets[i]->id) {
1319 case snd_soc_dapm_dai_in:
1320 case snd_soc_dapm_dai_out:
1326 /* is there a valid BE rtd for this widget */
1327 be = dpcm_get_be(card, list->widgets[i], stream);
1329 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1330 list->widgets[i]->name);
1334 /* make sure BE is a real BE */
1335 if (!be->dai_link->no_pcm)
1338 /* don't connect if FE is not running */
1339 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1342 /* newly connected FE and BE */
1343 err = dpcm_be_connect(fe, be, stream);
1345 dev_err(fe->dev, "ASoC: can't connect %s\n",
1346 list->widgets[i]->name);
1348 } else if (err == 0) /* already connected */
1352 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1356 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1361 * Find the corresponding BE DAIs that source or sink audio to this
1364 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1365 int stream, struct snd_soc_dapm_widget_list **list, int new)
1368 return dpcm_add_paths(fe, stream, list);
1370 return dpcm_prune_paths(fe, stream, list);
1373 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1375 struct snd_soc_dpcm *dpcm;
1377 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1378 dpcm->be->dpcm[stream].runtime_update =
1379 SND_SOC_DPCM_UPDATE_NO;
1382 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1385 struct snd_soc_dpcm *dpcm;
1387 /* disable any enabled and non active backends */
1388 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1390 struct snd_soc_pcm_runtime *be = dpcm->be;
1391 struct snd_pcm_substream *be_substream =
1392 snd_soc_dpcm_get_substream(be, stream);
1394 if (be->dpcm[stream].users == 0)
1395 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1396 stream ? "capture" : "playback",
1397 be->dpcm[stream].state);
1399 if (--be->dpcm[stream].users != 0)
1402 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1405 soc_pcm_close(be_substream);
1406 be_substream->runtime = NULL;
1407 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1411 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1413 struct snd_soc_dpcm *dpcm;
1416 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1417 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1419 struct snd_soc_pcm_runtime *be = dpcm->be;
1420 struct snd_pcm_substream *be_substream =
1421 snd_soc_dpcm_get_substream(be, stream);
1423 if (!be_substream) {
1424 dev_err(be->dev, "ASoC: no backend %s stream\n",
1425 stream ? "capture" : "playback");
1429 /* is this op for this BE ? */
1430 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1433 /* first time the dpcm is open ? */
1434 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1435 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1436 stream ? "capture" : "playback",
1437 be->dpcm[stream].state);
1439 if (be->dpcm[stream].users++ != 0)
1442 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1443 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1446 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1447 stream ? "capture" : "playback", be->dai_link->name);
1449 be_substream->runtime = be->dpcm[stream].runtime;
1450 err = soc_pcm_open(be_substream);
1452 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1453 be->dpcm[stream].users--;
1454 if (be->dpcm[stream].users < 0)
1455 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1456 stream ? "capture" : "playback",
1457 be->dpcm[stream].state);
1459 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1463 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1470 /* disable any enabled and non active backends */
1471 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1472 struct snd_soc_pcm_runtime *be = dpcm->be;
1473 struct snd_pcm_substream *be_substream =
1474 snd_soc_dpcm_get_substream(be, stream);
1476 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1479 if (be->dpcm[stream].users == 0)
1480 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1481 stream ? "capture" : "playback",
1482 be->dpcm[stream].state);
1484 if (--be->dpcm[stream].users != 0)
1487 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1490 soc_pcm_close(be_substream);
1491 be_substream->runtime = NULL;
1492 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1498 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1499 struct snd_soc_pcm_stream *stream)
1501 runtime->hw.rate_min = stream->rate_min;
1502 runtime->hw.rate_max = stream->rate_max;
1503 runtime->hw.channels_min = stream->channels_min;
1504 runtime->hw.channels_max = stream->channels_max;
1505 if (runtime->hw.formats)
1506 runtime->hw.formats &= stream->formats;
1508 runtime->hw.formats = stream->formats;
1509 runtime->hw.rates = stream->rates;
1512 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1514 struct snd_pcm_runtime *runtime = substream->runtime;
1515 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1516 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1517 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1519 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1520 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1522 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1525 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1527 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1528 * for avoiding the race with trigger callback.
1529 * If the state is unset and a trigger is pending while the previous operation,
1530 * process the pending trigger action here.
1532 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1533 int stream, enum snd_soc_dpcm_update state)
1535 struct snd_pcm_substream *substream =
1536 snd_soc_dpcm_get_substream(fe, stream);
1538 snd_pcm_stream_lock_irq(substream);
1539 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1540 dpcm_fe_dai_do_trigger(substream,
1541 fe->dpcm[stream].trigger_pending - 1);
1542 fe->dpcm[stream].trigger_pending = 0;
1544 fe->dpcm[stream].runtime_update = state;
1545 snd_pcm_stream_unlock_irq(substream);
1548 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1550 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1551 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1552 int stream = fe_substream->stream, ret = 0;
1554 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1556 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1558 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1562 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1564 /* start the DAI frontend */
1565 ret = soc_pcm_open(fe_substream);
1567 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1571 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1573 dpcm_set_fe_runtime(fe_substream);
1574 snd_pcm_limit_hw_rates(runtime);
1576 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1580 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1582 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1586 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1588 struct snd_soc_dpcm *dpcm;
1590 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1591 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1593 struct snd_soc_pcm_runtime *be = dpcm->be;
1594 struct snd_pcm_substream *be_substream =
1595 snd_soc_dpcm_get_substream(be, stream);
1597 /* is this op for this BE ? */
1598 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1601 if (be->dpcm[stream].users == 0)
1602 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1603 stream ? "capture" : "playback",
1604 be->dpcm[stream].state);
1606 if (--be->dpcm[stream].users != 0)
1609 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1610 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1613 dev_dbg(be->dev, "ASoC: close BE %s\n",
1614 dpcm->fe->dai_link->name);
1616 soc_pcm_close(be_substream);
1617 be_substream->runtime = NULL;
1619 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1624 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1626 struct snd_soc_pcm_runtime *fe = substream->private_data;
1627 int stream = substream->stream;
1629 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1631 /* shutdown the BEs */
1632 dpcm_be_dai_shutdown(fe, substream->stream);
1634 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1636 /* now shutdown the frontend */
1637 soc_pcm_close(substream);
1639 /* run the stream event for each BE */
1640 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1642 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1643 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1647 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1649 struct snd_soc_dpcm *dpcm;
1651 /* only hw_params backends that are either sinks or sources
1652 * to this frontend DAI */
1653 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1655 struct snd_soc_pcm_runtime *be = dpcm->be;
1656 struct snd_pcm_substream *be_substream =
1657 snd_soc_dpcm_get_substream(be, stream);
1659 /* is this op for this BE ? */
1660 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1663 /* only free hw when no longer used - check all FEs */
1664 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1667 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1668 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1669 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1670 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1671 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1674 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1675 dpcm->fe->dai_link->name);
1677 soc_pcm_hw_free(be_substream);
1679 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1685 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1687 struct snd_soc_pcm_runtime *fe = substream->private_data;
1688 int err, stream = substream->stream;
1690 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1691 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1693 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1695 /* call hw_free on the frontend */
1696 err = soc_pcm_hw_free(substream);
1698 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1699 fe->dai_link->name);
1701 /* only hw_params backends that are either sinks or sources
1702 * to this frontend DAI */
1703 err = dpcm_be_dai_hw_free(fe, stream);
1705 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1706 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1708 mutex_unlock(&fe->card->mutex);
1712 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1714 struct snd_soc_dpcm *dpcm;
1717 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1719 struct snd_soc_pcm_runtime *be = dpcm->be;
1720 struct snd_pcm_substream *be_substream =
1721 snd_soc_dpcm_get_substream(be, stream);
1723 /* is this op for this BE ? */
1724 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1727 /* only allow hw_params() if no connected FEs are running */
1728 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1731 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1732 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1733 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1736 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1737 dpcm->fe->dai_link->name);
1739 /* copy params for each dpcm */
1740 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1741 sizeof(struct snd_pcm_hw_params));
1743 /* perform any hw_params fixups */
1744 if (be->dai_link->be_hw_params_fixup) {
1745 ret = be->dai_link->be_hw_params_fixup(be,
1749 "ASoC: hw_params BE fixup failed %d\n",
1755 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1757 dev_err(dpcm->be->dev,
1758 "ASoC: hw_params BE failed %d\n", ret);
1762 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1767 /* disable any enabled and non active backends */
1768 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1769 struct snd_soc_pcm_runtime *be = dpcm->be;
1770 struct snd_pcm_substream *be_substream =
1771 snd_soc_dpcm_get_substream(be, stream);
1773 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1776 /* only allow hw_free() if no connected FEs are running */
1777 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1780 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1781 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1782 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1783 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1786 soc_pcm_hw_free(be_substream);
1792 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1793 struct snd_pcm_hw_params *params)
1795 struct snd_soc_pcm_runtime *fe = substream->private_data;
1796 int ret, stream = substream->stream;
1798 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1799 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1801 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1802 sizeof(struct snd_pcm_hw_params));
1803 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1805 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1809 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1810 fe->dai_link->name, params_rate(params),
1811 params_channels(params), params_format(params));
1813 /* call hw_params on the frontend */
1814 ret = soc_pcm_hw_params(substream, params);
1816 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1817 dpcm_be_dai_hw_free(fe, stream);
1819 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1822 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1823 mutex_unlock(&fe->card->mutex);
1827 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1828 struct snd_pcm_substream *substream, int cmd)
1832 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1833 dpcm->fe->dai_link->name, cmd);
1835 ret = soc_pcm_trigger(substream, cmd);
1837 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1842 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1845 struct snd_soc_dpcm *dpcm;
1848 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1850 struct snd_soc_pcm_runtime *be = dpcm->be;
1851 struct snd_pcm_substream *be_substream =
1852 snd_soc_dpcm_get_substream(be, stream);
1854 /* is this op for this BE ? */
1855 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1859 case SNDRV_PCM_TRIGGER_START:
1860 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1861 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1864 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1868 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1870 case SNDRV_PCM_TRIGGER_RESUME:
1871 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1874 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1878 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1880 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1881 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1884 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1888 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1890 case SNDRV_PCM_TRIGGER_STOP:
1891 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1894 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1897 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1901 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1903 case SNDRV_PCM_TRIGGER_SUSPEND:
1904 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1907 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1910 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1914 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1916 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1917 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1920 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1923 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1927 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1934 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1936 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
1938 struct snd_soc_pcm_runtime *fe = substream->private_data;
1939 int stream = substream->stream, ret;
1940 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1942 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1945 case SND_SOC_DPCM_TRIGGER_PRE:
1946 /* call trigger on the frontend before the backend. */
1948 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1949 fe->dai_link->name, cmd);
1951 ret = soc_pcm_trigger(substream, cmd);
1953 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1957 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1959 case SND_SOC_DPCM_TRIGGER_POST:
1960 /* call trigger on the frontend after the backend. */
1962 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1964 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1968 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1969 fe->dai_link->name, cmd);
1971 ret = soc_pcm_trigger(substream, cmd);
1973 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1974 /* bespoke trigger() - handles both FE and BEs */
1976 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1977 fe->dai_link->name, cmd);
1979 ret = soc_pcm_bespoke_trigger(substream, cmd);
1981 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1986 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1987 fe->dai_link->name);
1993 case SNDRV_PCM_TRIGGER_START:
1994 case SNDRV_PCM_TRIGGER_RESUME:
1995 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1996 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1998 case SNDRV_PCM_TRIGGER_STOP:
1999 case SNDRV_PCM_TRIGGER_SUSPEND:
2000 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2001 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2006 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2010 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2012 struct snd_soc_pcm_runtime *fe = substream->private_data;
2013 int stream = substream->stream;
2015 /* if FE's runtime_update is already set, we're in race;
2016 * process this trigger later at exit
2018 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2019 fe->dpcm[stream].trigger_pending = cmd + 1;
2020 return 0; /* delayed, assuming it's successful */
2023 /* we're alone, let's trigger */
2024 return dpcm_fe_dai_do_trigger(substream, cmd);
2027 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2029 struct snd_soc_dpcm *dpcm;
2032 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2034 struct snd_soc_pcm_runtime *be = dpcm->be;
2035 struct snd_pcm_substream *be_substream =
2036 snd_soc_dpcm_get_substream(be, stream);
2038 /* is this op for this BE ? */
2039 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2042 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2043 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2046 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2047 dpcm->fe->dai_link->name);
2049 ret = soc_pcm_prepare(be_substream);
2051 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2056 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2061 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2063 struct snd_soc_pcm_runtime *fe = substream->private_data;
2064 int stream = substream->stream, ret = 0;
2066 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2068 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2070 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2072 /* there is no point preparing this FE if there are no BEs */
2073 if (list_empty(&fe->dpcm[stream].be_clients)) {
2074 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2075 fe->dai_link->name);
2080 ret = dpcm_be_dai_prepare(fe, substream->stream);
2084 /* call prepare on the frontend */
2085 ret = soc_pcm_prepare(substream);
2087 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2088 fe->dai_link->name);
2092 /* run the stream event for each BE */
2093 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2094 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2097 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2098 mutex_unlock(&fe->card->mutex);
2103 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2104 unsigned int cmd, void *arg)
2106 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2107 struct snd_soc_platform *platform = rtd->platform;
2109 if (platform->driver->ops && platform->driver->ops->ioctl)
2110 return platform->driver->ops->ioctl(substream, cmd, arg);
2111 return snd_pcm_lib_ioctl(substream, cmd, arg);
2114 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2116 struct snd_pcm_substream *substream =
2117 snd_soc_dpcm_get_substream(fe, stream);
2118 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2121 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2122 stream ? "capture" : "playback", fe->dai_link->name);
2124 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2125 /* call bespoke trigger - FE takes care of all BE triggers */
2126 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2127 fe->dai_link->name);
2129 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2131 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2133 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2134 fe->dai_link->name);
2136 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2138 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2141 err = dpcm_be_dai_hw_free(fe, stream);
2143 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2145 err = dpcm_be_dai_shutdown(fe, stream);
2147 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2149 /* run the stream event for each BE */
2150 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2155 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2157 struct snd_pcm_substream *substream =
2158 snd_soc_dpcm_get_substream(fe, stream);
2159 struct snd_soc_dpcm *dpcm;
2160 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2163 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2164 stream ? "capture" : "playback", fe->dai_link->name);
2166 /* Only start the BE if the FE is ready */
2167 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2168 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2171 /* startup must always be called for new BEs */
2172 ret = dpcm_be_dai_startup(fe, stream);
2176 /* keep going if FE state is > open */
2177 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2180 ret = dpcm_be_dai_hw_params(fe, stream);
2184 /* keep going if FE state is > hw_params */
2185 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2189 ret = dpcm_be_dai_prepare(fe, stream);
2193 /* run the stream event for each BE */
2194 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2196 /* keep going if FE state is > prepare */
2197 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2198 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2201 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2202 /* call trigger on the frontend - FE takes care of all BE triggers */
2203 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2204 fe->dai_link->name);
2206 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2208 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2212 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2213 fe->dai_link->name);
2215 ret = dpcm_be_dai_trigger(fe, stream,
2216 SNDRV_PCM_TRIGGER_START);
2218 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2226 dpcm_be_dai_hw_free(fe, stream);
2228 dpcm_be_dai_shutdown(fe, stream);
2230 /* disconnect any non started BEs */
2231 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2232 struct snd_soc_pcm_runtime *be = dpcm->be;
2233 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2234 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2240 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2244 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2245 ret = dpcm_run_update_startup(fe, stream);
2247 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2248 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2253 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2257 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2258 ret = dpcm_run_update_shutdown(fe, stream);
2260 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2261 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2266 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2269 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2271 int i, old, new, paths;
2273 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2274 for (i = 0; i < card->num_rtd; i++) {
2275 struct snd_soc_dapm_widget_list *list;
2276 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2278 /* make sure link is FE */
2279 if (!fe->dai_link->dynamic)
2282 /* only check active links */
2283 if (!fe->cpu_dai->active)
2286 /* DAPM sync will call this to update DSP paths */
2287 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2288 fe->dai_link->name);
2290 /* skip if FE doesn't have playback capability */
2291 if (!fe->cpu_dai->driver->playback.channels_min)
2294 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2296 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2297 fe->dai_link->name, "playback");
2298 mutex_unlock(&card->mutex);
2302 /* update any new playback paths */
2303 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2305 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2306 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2307 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2310 /* update any old playback paths */
2311 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2313 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2314 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2315 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2318 dpcm_path_put(&list);
2320 /* skip if FE doesn't have capture capability */
2321 if (!fe->cpu_dai->driver->capture.channels_min)
2324 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2326 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2327 fe->dai_link->name, "capture");
2328 mutex_unlock(&card->mutex);
2332 /* update any new capture paths */
2333 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2335 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2336 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2337 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2340 /* update any old capture paths */
2341 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2343 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2344 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2345 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2348 dpcm_path_put(&list);
2351 mutex_unlock(&card->mutex);
2354 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2356 struct snd_soc_dpcm *dpcm;
2357 struct list_head *clients =
2358 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2360 list_for_each_entry(dpcm, clients, list_be) {
2362 struct snd_soc_pcm_runtime *be = dpcm->be;
2365 if (be->dai_link->ignore_suspend)
2368 for (i = 0; i < be->num_codecs; i++) {
2369 struct snd_soc_dai *dai = be->codec_dais[i];
2370 struct snd_soc_dai_driver *drv = dai->driver;
2372 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2373 be->dai_link->name);
2375 if (drv->ops && drv->ops->digital_mute &&
2376 dai->playback_active)
2377 drv->ops->digital_mute(dai, mute);
2384 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2386 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2387 struct snd_soc_dpcm *dpcm;
2388 struct snd_soc_dapm_widget_list *list;
2390 int stream = fe_substream->stream;
2392 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2393 fe->dpcm[stream].runtime = fe_substream->runtime;
2395 ret = dpcm_path_get(fe, stream, &list);
2397 mutex_unlock(&fe->card->mutex);
2399 } else if (ret == 0) {
2400 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2401 fe->dai_link->name, stream ? "capture" : "playback");
2404 /* calculate valid and active FE <-> BE dpcms */
2405 dpcm_process_paths(fe, stream, &list, 1);
2407 ret = dpcm_fe_dai_startup(fe_substream);
2409 /* clean up all links */
2410 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2411 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2413 dpcm_be_disconnect(fe, stream);
2414 fe->dpcm[stream].runtime = NULL;
2417 dpcm_clear_pending_state(fe, stream);
2418 dpcm_path_put(&list);
2419 mutex_unlock(&fe->card->mutex);
2423 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2425 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2426 struct snd_soc_dpcm *dpcm;
2427 int stream = fe_substream->stream, ret;
2429 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2430 ret = dpcm_fe_dai_shutdown(fe_substream);
2432 /* mark FE's links ready to prune */
2433 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2434 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2436 dpcm_be_disconnect(fe, stream);
2438 fe->dpcm[stream].runtime = NULL;
2439 mutex_unlock(&fe->card->mutex);
2443 /* create a new pcm */
2444 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2446 struct snd_soc_platform *platform = rtd->platform;
2447 struct snd_soc_dai *codec_dai;
2448 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2449 struct snd_pcm *pcm;
2451 int ret = 0, playback = 0, capture = 0;
2454 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2455 playback = rtd->dai_link->dpcm_playback;
2456 capture = rtd->dai_link->dpcm_capture;
2458 for (i = 0; i < rtd->num_codecs; i++) {
2459 codec_dai = rtd->codec_dais[i];
2460 if (codec_dai->driver->playback.channels_min)
2462 if (codec_dai->driver->capture.channels_min)
2466 capture = capture && cpu_dai->driver->capture.channels_min;
2467 playback = playback && cpu_dai->driver->playback.channels_min;
2470 if (rtd->dai_link->playback_only) {
2475 if (rtd->dai_link->capture_only) {
2480 /* create the PCM */
2481 if (rtd->dai_link->no_pcm) {
2482 snprintf(new_name, sizeof(new_name), "(%s)",
2483 rtd->dai_link->stream_name);
2485 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2486 playback, capture, &pcm);
2488 if (rtd->dai_link->dynamic)
2489 snprintf(new_name, sizeof(new_name), "%s (*)",
2490 rtd->dai_link->stream_name);
2492 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2493 rtd->dai_link->stream_name,
2494 (rtd->num_codecs > 1) ?
2495 "multicodec" : rtd->codec_dai->name, num);
2497 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2501 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2502 rtd->dai_link->name);
2505 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2507 /* DAPM dai link stream work */
2508 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2511 pcm->private_data = rtd;
2513 if (rtd->dai_link->no_pcm) {
2515 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2517 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2521 /* ASoC PCM operations */
2522 if (rtd->dai_link->dynamic) {
2523 rtd->ops.open = dpcm_fe_dai_open;
2524 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2525 rtd->ops.prepare = dpcm_fe_dai_prepare;
2526 rtd->ops.trigger = dpcm_fe_dai_trigger;
2527 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2528 rtd->ops.close = dpcm_fe_dai_close;
2529 rtd->ops.pointer = soc_pcm_pointer;
2530 rtd->ops.ioctl = soc_pcm_ioctl;
2532 rtd->ops.open = soc_pcm_open;
2533 rtd->ops.hw_params = soc_pcm_hw_params;
2534 rtd->ops.prepare = soc_pcm_prepare;
2535 rtd->ops.trigger = soc_pcm_trigger;
2536 rtd->ops.hw_free = soc_pcm_hw_free;
2537 rtd->ops.close = soc_pcm_close;
2538 rtd->ops.pointer = soc_pcm_pointer;
2539 rtd->ops.ioctl = soc_pcm_ioctl;
2542 if (platform->driver->ops) {
2543 rtd->ops.ack = platform->driver->ops->ack;
2544 rtd->ops.copy = platform->driver->ops->copy;
2545 rtd->ops.silence = platform->driver->ops->silence;
2546 rtd->ops.page = platform->driver->ops->page;
2547 rtd->ops.mmap = platform->driver->ops->mmap;
2551 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2554 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2556 if (platform->driver->pcm_new) {
2557 ret = platform->driver->pcm_new(rtd);
2559 dev_err(platform->dev,
2560 "ASoC: pcm constructor failed: %d\n",
2566 pcm->private_free = platform->driver->pcm_free;
2568 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2569 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2574 /* is the current PCM operation for this FE ? */
2575 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2577 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2581 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2583 /* is the current PCM operation for this BE ? */
2584 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2585 struct snd_soc_pcm_runtime *be, int stream)
2587 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2588 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2589 be->dpcm[stream].runtime_update))
2593 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2595 /* get the substream for this BE */
2596 struct snd_pcm_substream *
2597 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2599 return be->pcm->streams[stream].substream;
2601 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2603 /* get the BE runtime state */
2604 enum snd_soc_dpcm_state
2605 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2607 return be->dpcm[stream].state;
2609 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2611 /* set the BE runtime state */
2612 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2613 int stream, enum snd_soc_dpcm_state state)
2615 be->dpcm[stream].state = state;
2617 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2620 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2621 * are not running, paused or suspended for the specified stream direction.
2623 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2624 struct snd_soc_pcm_runtime *be, int stream)
2626 struct snd_soc_dpcm *dpcm;
2629 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2634 state = dpcm->fe->dpcm[stream].state;
2635 if (state == SND_SOC_DPCM_STATE_START ||
2636 state == SND_SOC_DPCM_STATE_PAUSED ||
2637 state == SND_SOC_DPCM_STATE_SUSPEND)
2641 /* it's safe to free/stop this BE DAI */
2644 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2647 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2648 * running, paused or suspended for the specified stream direction.
2650 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2651 struct snd_soc_pcm_runtime *be, int stream)
2653 struct snd_soc_dpcm *dpcm;
2656 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2661 state = dpcm->fe->dpcm[stream].state;
2662 if (state == SND_SOC_DPCM_STATE_START ||
2663 state == SND_SOC_DPCM_STATE_PAUSED ||
2664 state == SND_SOC_DPCM_STATE_SUSPEND ||
2665 state == SND_SOC_DPCM_STATE_PREPARE)
2669 /* it's safe to change hw_params */
2672 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2674 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2675 int cmd, struct snd_soc_platform *platform)
2677 if (platform->driver->ops && platform->driver->ops->trigger)
2678 return platform->driver->ops->trigger(substream, cmd);
2681 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2683 #ifdef CONFIG_DEBUG_FS
2684 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2687 case SND_SOC_DPCM_STATE_NEW:
2689 case SND_SOC_DPCM_STATE_OPEN:
2691 case SND_SOC_DPCM_STATE_HW_PARAMS:
2693 case SND_SOC_DPCM_STATE_PREPARE:
2695 case SND_SOC_DPCM_STATE_START:
2697 case SND_SOC_DPCM_STATE_STOP:
2699 case SND_SOC_DPCM_STATE_SUSPEND:
2701 case SND_SOC_DPCM_STATE_PAUSED:
2703 case SND_SOC_DPCM_STATE_HW_FREE:
2705 case SND_SOC_DPCM_STATE_CLOSE:
2712 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2713 int stream, char *buf, size_t size)
2715 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2716 struct snd_soc_dpcm *dpcm;
2720 offset += snprintf(buf + offset, size - offset,
2721 "[%s - %s]\n", fe->dai_link->name,
2722 stream ? "Capture" : "Playback");
2724 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2725 dpcm_state_string(fe->dpcm[stream].state));
2727 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2728 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2729 offset += snprintf(buf + offset, size - offset,
2731 "Format = %s, Channels = %d, Rate = %d\n",
2732 snd_pcm_format_name(params_format(params)),
2733 params_channels(params),
2734 params_rate(params));
2737 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2739 if (list_empty(&fe->dpcm[stream].be_clients)) {
2740 offset += snprintf(buf + offset, size - offset,
2741 " No active DSP links\n");
2745 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2746 struct snd_soc_pcm_runtime *be = dpcm->be;
2747 params = &dpcm->hw_params;
2749 offset += snprintf(buf + offset, size - offset,
2750 "- %s\n", be->dai_link->name);
2752 offset += snprintf(buf + offset, size - offset,
2754 dpcm_state_string(be->dpcm[stream].state));
2756 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2757 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2758 offset += snprintf(buf + offset, size - offset,
2759 " Hardware Params: "
2760 "Format = %s, Channels = %d, Rate = %d\n",
2761 snd_pcm_format_name(params_format(params)),
2762 params_channels(params),
2763 params_rate(params));
2770 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2771 size_t count, loff_t *ppos)
2773 struct snd_soc_pcm_runtime *fe = file->private_data;
2774 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2777 buf = kmalloc(out_count, GFP_KERNEL);
2781 if (fe->cpu_dai->driver->playback.channels_min)
2782 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2783 buf + offset, out_count - offset);
2785 if (fe->cpu_dai->driver->capture.channels_min)
2786 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2787 buf + offset, out_count - offset);
2789 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2795 static const struct file_operations dpcm_state_fops = {
2796 .open = simple_open,
2797 .read = dpcm_state_read_file,
2798 .llseek = default_llseek,
2801 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2806 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2807 rtd->card->debugfs_card_root);
2808 if (!rtd->debugfs_dpcm_root) {
2810 "ASoC: Failed to create dpcm debugfs directory %s\n",
2811 rtd->dai_link->name);
2815 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2816 rtd->debugfs_dpcm_root,
2817 rtd, &dpcm_state_fops);