dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / sound / soc / soc-pcm.c
blobf99eb8f442829c648931cfec449aec8f856bf65c
1 /*
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;
48 else
49 codec_stream = &dai->driver->capture;
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
55 /**
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;
68 int i;
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++;
76 } else {
77 cpu_dai->capture_active++;
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
82 cpu_dai->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++;
90 /**
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;
103 int i;
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--;
111 } else {
112 cpu_dai->capture_active--;
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
117 cpu_dai->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)
136 int i;
137 bool ignore = true;
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140 return true;
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;
167 return 0;
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,
173 int event)
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))
186 continue;
188 snd_soc_dapm_stream_event(be, dir, event);
191 snd_soc_dapm_stream_event(fe, dir, event);
193 return 0;
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;
200 int ret;
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",
205 soc_dai->rate);
207 ret = snd_pcm_hw_constraint_single(substream->runtime,
208 SNDRV_PCM_HW_PARAM_RATE,
209 soc_dai->rate);
210 if (ret < 0) {
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
213 ret);
214 return ret;
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",
221 soc_dai->channels);
223 ret = snd_pcm_hw_constraint_single(substream->runtime,
224 SNDRV_PCM_HW_PARAM_CHANNELS,
225 soc_dai->channels);
226 if (ret < 0) {
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
229 ret);
230 return ret;
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);
242 if (ret < 0) {
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
245 ret);
246 return ret;
250 return 0;
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);
274 return -EINVAL;
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);
286 return -EINVAL;
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);
298 return -EINVAL;
301 return 0;
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;
321 return symmetry;
324 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
327 int ret;
329 if (!bits)
330 return;
332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
333 if (ret != 0)
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
335 bits, ret);
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;
343 int i;
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) {
350 bits = 0;
351 break;
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
355 cpu_bits = cpu_dai->driver->playback.sig_bits;
356 } else {
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) {
360 bits = 0;
361 break;
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;
385 int i;
387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
389 else
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],
405 substream->stream))
406 continue;
408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
411 else
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);
433 if (hw->formats)
434 hw->formats &= formats & cpu_stream->formats;
435 else
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_dai *cpu_dai = rtd->cpu_dai;
458 struct snd_soc_dai *codec_dai;
459 const char *codec_dai_name = "multicodec";
460 int i, ret = 0;
462 pinctrl_pm_select_default_state(cpu_dai->dev);
463 for (i = 0; i < rtd->num_codecs; i++)
464 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
465 pm_runtime_get_sync(cpu_dai->dev);
466 for (i = 0; i < rtd->num_codecs; i++)
467 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
468 pm_runtime_get_sync(platform->dev);
470 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
472 /* startup the audio subsystem */
473 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
474 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
475 if (ret < 0) {
476 dev_err(cpu_dai->dev, "ASoC: can't open interface"
477 " %s: %d\n", cpu_dai->name, ret);
478 goto out;
482 if (platform->driver->ops && platform->driver->ops->open) {
483 ret = platform->driver->ops->open(substream);
484 if (ret < 0) {
485 dev_err(platform->dev, "ASoC: can't open platform"
486 " %s: %d\n", platform->component.name, ret);
487 goto platform_err;
491 for (i = 0; i < rtd->num_codecs; i++) {
492 codec_dai = rtd->codec_dais[i];
493 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
494 ret = codec_dai->driver->ops->startup(substream,
495 codec_dai);
496 if (ret < 0) {
497 dev_err(codec_dai->dev,
498 "ASoC: can't open codec %s: %d\n",
499 codec_dai->name, ret);
500 goto codec_dai_err;
504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
505 codec_dai->tx_mask = 0;
506 else
507 codec_dai->rx_mask = 0;
510 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
511 ret = rtd->dai_link->ops->startup(substream);
512 if (ret < 0) {
513 pr_err("ASoC: %s startup failed: %d\n",
514 rtd->dai_link->name, ret);
515 goto machine_err;
519 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
520 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
521 goto dynamic;
523 /* Check that the codec and cpu DAIs are compatible */
524 soc_pcm_init_runtime_hw(substream);
526 if (rtd->num_codecs == 1)
527 codec_dai_name = rtd->codec_dai->name;
529 if (soc_pcm_has_symmetry(substream))
530 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
532 ret = -EINVAL;
533 if (!runtime->hw.rates) {
534 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
535 codec_dai_name, cpu_dai->name);
536 goto config_err;
538 if (!runtime->hw.formats) {
539 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
540 codec_dai_name, cpu_dai->name);
541 goto config_err;
543 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
544 runtime->hw.channels_min > runtime->hw.channels_max) {
545 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
546 codec_dai_name, cpu_dai->name);
547 goto config_err;
550 soc_pcm_apply_msb(substream);
552 /* Symmetry only applies if we've already got an active stream. */
553 if (cpu_dai->active) {
554 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
555 if (ret != 0)
556 goto config_err;
559 for (i = 0; i < rtd->num_codecs; i++) {
560 if (rtd->codec_dais[i]->active) {
561 ret = soc_pcm_apply_symmetry(substream,
562 rtd->codec_dais[i]);
563 if (ret != 0)
564 goto config_err;
568 pr_debug("ASoC: %s <-> %s info:\n",
569 codec_dai_name, cpu_dai->name);
570 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
571 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
572 runtime->hw.channels_max);
573 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
574 runtime->hw.rate_max);
576 dynamic:
578 snd_soc_runtime_activate(rtd, substream->stream);
580 mutex_unlock(&rtd->pcm_mutex);
581 return 0;
583 config_err:
584 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
585 rtd->dai_link->ops->shutdown(substream);
587 machine_err:
588 i = rtd->num_codecs;
590 codec_dai_err:
591 while (--i >= 0) {
592 codec_dai = rtd->codec_dais[i];
593 if (codec_dai->driver->ops->shutdown)
594 codec_dai->driver->ops->shutdown(substream, codec_dai);
597 if (platform->driver->ops && platform->driver->ops->close)
598 platform->driver->ops->close(substream);
600 platform_err:
601 if (cpu_dai->driver->ops->shutdown)
602 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
603 out:
604 mutex_unlock(&rtd->pcm_mutex);
606 pm_runtime_put(platform->dev);
607 for (i = 0; i < rtd->num_codecs; i++)
608 pm_runtime_put(rtd->codec_dais[i]->dev);
609 pm_runtime_put(cpu_dai->dev);
610 for (i = 0; i < rtd->num_codecs; i++) {
611 if (!rtd->codec_dais[i]->active)
612 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
614 if (!cpu_dai->active)
615 pinctrl_pm_select_sleep_state(cpu_dai->dev);
617 return ret;
621 * Power down the audio subsystem pmdown_time msecs after close is called.
622 * This is to ensure there are no pops or clicks in between any music tracks
623 * due to DAPM power cycling.
625 static void close_delayed_work(struct work_struct *work)
627 struct snd_soc_pcm_runtime *rtd =
628 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
629 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
631 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
633 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
634 codec_dai->driver->playback.stream_name,
635 codec_dai->playback_active ? "active" : "inactive",
636 rtd->pop_wait ? "yes" : "no");
638 /* are we waiting on this codec DAI stream */
639 if (rtd->pop_wait == 1) {
640 rtd->pop_wait = 0;
641 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
642 SND_SOC_DAPM_STREAM_STOP);
645 mutex_unlock(&rtd->pcm_mutex);
649 * Called by ALSA when a PCM substream is closed. Private data can be
650 * freed here. The cpu DAI, codec DAI, machine and platform are also
651 * shutdown.
653 static int soc_pcm_close(struct snd_pcm_substream *substream)
655 struct snd_soc_pcm_runtime *rtd = substream->private_data;
656 struct snd_soc_platform *platform = rtd->platform;
657 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
658 struct snd_soc_dai *codec_dai;
659 int i;
661 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
663 snd_soc_runtime_deactivate(rtd, substream->stream);
665 /* clear the corresponding DAIs rate when inactive */
666 if (!cpu_dai->active)
667 cpu_dai->rate = 0;
669 for (i = 0; i < rtd->num_codecs; i++) {
670 codec_dai = rtd->codec_dais[i];
671 if (!codec_dai->active)
672 codec_dai->rate = 0;
675 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
677 if (cpu_dai->driver->ops->shutdown)
678 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
680 for (i = 0; i < rtd->num_codecs; i++) {
681 codec_dai = rtd->codec_dais[i];
682 if (codec_dai->driver->ops->shutdown)
683 codec_dai->driver->ops->shutdown(substream, codec_dai);
686 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
687 rtd->dai_link->ops->shutdown(substream);
689 if (platform->driver->ops && platform->driver->ops->close)
690 platform->driver->ops->close(substream);
692 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
693 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
694 /* powered down playback stream now */
695 snd_soc_dapm_stream_event(rtd,
696 SNDRV_PCM_STREAM_PLAYBACK,
697 SND_SOC_DAPM_STREAM_STOP);
698 } else {
699 /* start delayed pop wq here for playback streams */
700 rtd->pop_wait = 1;
701 queue_delayed_work(system_power_efficient_wq,
702 &rtd->delayed_work,
703 msecs_to_jiffies(rtd->pmdown_time));
705 } else {
706 /* capture streams can be powered down now */
707 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
708 SND_SOC_DAPM_STREAM_STOP);
711 mutex_unlock(&rtd->pcm_mutex);
713 pm_runtime_put(platform->dev);
714 for (i = 0; i < rtd->num_codecs; i++)
715 pm_runtime_put(rtd->codec_dais[i]->dev);
716 pm_runtime_put(cpu_dai->dev);
717 for (i = 0; i < rtd->num_codecs; i++) {
718 if (!rtd->codec_dais[i]->active)
719 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
721 if (!cpu_dai->active)
722 pinctrl_pm_select_sleep_state(cpu_dai->dev);
724 return 0;
728 * Called by ALSA when the PCM substream is prepared, can set format, sample
729 * rate, etc. This function is non atomic and can be called multiple times,
730 * it can refer to the runtime info.
732 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
734 struct snd_soc_pcm_runtime *rtd = substream->private_data;
735 struct snd_soc_platform *platform = rtd->platform;
736 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
737 struct snd_soc_dai *codec_dai;
738 int i, ret = 0;
740 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
742 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
743 ret = rtd->dai_link->ops->prepare(substream);
744 if (ret < 0) {
745 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
746 " %d\n", ret);
747 goto out;
751 if (platform->driver->ops && platform->driver->ops->prepare) {
752 ret = platform->driver->ops->prepare(substream);
753 if (ret < 0) {
754 dev_err(platform->dev, "ASoC: platform prepare error:"
755 " %d\n", ret);
756 goto out;
760 for (i = 0; i < rtd->num_codecs; i++) {
761 codec_dai = rtd->codec_dais[i];
762 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
763 ret = codec_dai->driver->ops->prepare(substream,
764 codec_dai);
765 if (ret < 0) {
766 dev_err(codec_dai->dev,
767 "ASoC: codec DAI prepare error: %d\n",
768 ret);
769 goto out;
774 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
775 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
776 if (ret < 0) {
777 dev_err(cpu_dai->dev,
778 "ASoC: cpu DAI prepare error: %d\n", ret);
779 goto out;
783 /* cancel any delayed stream shutdown that is pending */
784 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
785 rtd->pop_wait) {
786 rtd->pop_wait = 0;
787 cancel_delayed_work(&rtd->delayed_work);
790 snd_soc_dapm_stream_event(rtd, substream->stream,
791 SND_SOC_DAPM_STREAM_START);
793 for (i = 0; i < rtd->num_codecs; i++)
794 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
795 substream->stream);
796 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
798 out:
799 mutex_unlock(&rtd->pcm_mutex);
800 return ret;
803 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
804 unsigned int mask)
806 struct snd_interval *interval;
807 int channels = hweight_long(mask);
809 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
810 interval->min = channels;
811 interval->max = channels;
814 int soc_dai_hw_params(struct snd_pcm_substream *substream,
815 struct snd_pcm_hw_params *params,
816 struct snd_soc_dai *dai)
818 int ret;
820 if (dai->driver->ops && dai->driver->ops->hw_params) {
821 ret = dai->driver->ops->hw_params(substream, params, dai);
822 if (ret < 0) {
823 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
824 dai->name, ret);
825 return ret;
829 return 0;
833 * Called by ALSA when the hardware params are set by application. This
834 * function can also be called multiple times and can allocate buffers
835 * (using snd_pcm_lib_* ). It's non-atomic.
837 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
838 struct snd_pcm_hw_params *params)
840 struct snd_soc_pcm_runtime *rtd = substream->private_data;
841 struct snd_soc_platform *platform = rtd->platform;
842 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
843 int i, ret = 0;
845 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
847 ret = soc_pcm_params_symmetry(substream, params);
848 if (ret)
849 goto out;
851 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
852 ret = rtd->dai_link->ops->hw_params(substream, params);
853 if (ret < 0) {
854 dev_err(rtd->card->dev, "ASoC: machine hw_params"
855 " failed: %d\n", ret);
856 goto out;
860 for (i = 0; i < rtd->num_codecs; i++) {
861 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
862 struct snd_pcm_hw_params codec_params;
865 * Skip CODECs which don't support the current stream type,
866 * the idea being that if a CODEC is not used for the currently
867 * set up transfer direction, it should not need to be
868 * configured, especially since the configuration used might
869 * not even be supported by that CODEC. There may be cases
870 * however where a CODEC needs to be set up although it is
871 * actually not being used for the transfer, e.g. if a
872 * capture-only CODEC is acting as an LRCLK and/or BCLK master
873 * for the DAI link including a playback-only CODEC.
874 * If this becomes necessary, we will have to augment the
875 * machine driver setup with information on how to act, so
876 * we can do the right thing here.
878 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
879 continue;
881 /* copy params for each codec */
882 codec_params = *params;
884 /* fixup params based on TDM slot masks */
885 if (codec_dai->tx_mask)
886 soc_pcm_codec_params_fixup(&codec_params,
887 codec_dai->tx_mask);
888 if (codec_dai->rx_mask)
889 soc_pcm_codec_params_fixup(&codec_params,
890 codec_dai->rx_mask);
892 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
893 if(ret < 0)
894 goto codec_err;
896 codec_dai->rate = params_rate(&codec_params);
897 codec_dai->channels = params_channels(&codec_params);
898 codec_dai->sample_bits = snd_pcm_format_physical_width(
899 params_format(&codec_params));
902 ret = soc_dai_hw_params(substream, params, cpu_dai);
903 if (ret < 0)
904 goto interface_err;
906 if (platform->driver->ops && platform->driver->ops->hw_params) {
907 ret = platform->driver->ops->hw_params(substream, params);
908 if (ret < 0) {
909 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
910 platform->component.name, ret);
911 goto platform_err;
915 /* store the parameters for each DAIs */
916 cpu_dai->rate = params_rate(params);
917 cpu_dai->channels = params_channels(params);
918 cpu_dai->sample_bits =
919 snd_pcm_format_physical_width(params_format(params));
921 out:
922 mutex_unlock(&rtd->pcm_mutex);
923 return ret;
925 platform_err:
926 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
927 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
929 interface_err:
930 i = rtd->num_codecs;
932 codec_err:
933 while (--i >= 0) {
934 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
935 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
936 codec_dai->driver->ops->hw_free(substream, codec_dai);
937 codec_dai->rate = 0;
940 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
941 rtd->dai_link->ops->hw_free(substream);
943 mutex_unlock(&rtd->pcm_mutex);
944 return ret;
948 * Frees resources allocated by hw_params, can be called multiple times
950 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
952 struct snd_soc_pcm_runtime *rtd = substream->private_data;
953 struct snd_soc_platform *platform = rtd->platform;
954 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
955 struct snd_soc_dai *codec_dai;
956 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
957 int i;
959 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
961 /* clear the corresponding DAIs parameters when going to be inactive */
962 if (cpu_dai->active == 1) {
963 cpu_dai->rate = 0;
964 cpu_dai->channels = 0;
965 cpu_dai->sample_bits = 0;
968 for (i = 0; i < rtd->num_codecs; i++) {
969 codec_dai = rtd->codec_dais[i];
970 if (codec_dai->active == 1) {
971 codec_dai->rate = 0;
972 codec_dai->channels = 0;
973 codec_dai->sample_bits = 0;
977 /* apply codec digital mute */
978 for (i = 0; i < rtd->num_codecs; i++) {
979 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
980 (!playback && rtd->codec_dais[i]->capture_active == 1))
981 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
982 substream->stream);
985 /* free any machine hw params */
986 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
987 rtd->dai_link->ops->hw_free(substream);
989 /* free any DMA resources */
990 if (platform->driver->ops && platform->driver->ops->hw_free)
991 platform->driver->ops->hw_free(substream);
993 /* now free hw params for the DAIs */
994 for (i = 0; i < rtd->num_codecs; i++) {
995 codec_dai = rtd->codec_dais[i];
996 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
997 codec_dai->driver->ops->hw_free(substream, codec_dai);
1000 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
1001 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1003 mutex_unlock(&rtd->pcm_mutex);
1004 return 0;
1007 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1009 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1010 struct snd_soc_platform *platform = rtd->platform;
1011 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1012 struct snd_soc_dai *codec_dai;
1013 int i, ret;
1015 for (i = 0; i < rtd->num_codecs; i++) {
1016 codec_dai = rtd->codec_dais[i];
1017 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1018 ret = codec_dai->driver->ops->trigger(substream,
1019 cmd, codec_dai);
1020 if (ret < 0)
1021 return ret;
1025 if (platform->driver->ops && platform->driver->ops->trigger) {
1026 ret = platform->driver->ops->trigger(substream, cmd);
1027 if (ret < 0)
1028 return ret;
1031 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
1032 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1033 if (ret < 0)
1034 return ret;
1037 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1038 ret = rtd->dai_link->ops->trigger(substream, cmd);
1039 if (ret < 0)
1040 return ret;
1043 return 0;
1046 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1047 int cmd)
1049 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1050 struct snd_soc_platform *platform = rtd->platform;
1051 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1052 struct snd_soc_dai *codec_dai;
1053 int i, ret;
1055 for (i = 0; i < rtd->num_codecs; i++) {
1056 codec_dai = rtd->codec_dais[i];
1057 if (codec_dai->driver->ops &&
1058 codec_dai->driver->ops->bespoke_trigger) {
1059 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1060 cmd, codec_dai);
1061 if (ret < 0)
1062 return ret;
1066 if (platform->driver->bespoke_trigger) {
1067 ret = platform->driver->bespoke_trigger(substream, cmd);
1068 if (ret < 0)
1069 return ret;
1072 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
1073 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1074 if (ret < 0)
1075 return ret;
1077 return 0;
1080 * soc level wrapper for pointer callback
1081 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1082 * the runtime->delay will be updated accordingly.
1084 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1086 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1087 struct snd_soc_platform *platform = rtd->platform;
1088 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1089 struct snd_soc_dai *codec_dai;
1090 struct snd_pcm_runtime *runtime = substream->runtime;
1091 snd_pcm_uframes_t offset = 0;
1092 snd_pcm_sframes_t delay = 0;
1093 snd_pcm_sframes_t codec_delay = 0;
1094 int i;
1096 if (platform->driver->ops && platform->driver->ops->pointer)
1097 offset = platform->driver->ops->pointer(substream);
1099 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
1100 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1102 for (i = 0; i < rtd->num_codecs; i++) {
1103 codec_dai = rtd->codec_dais[i];
1104 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1105 codec_delay = max(codec_delay,
1106 codec_dai->driver->ops->delay(substream,
1107 codec_dai));
1109 delay += codec_delay;
1112 * None of the existing platform drivers implement delay(), so
1113 * for now the codec_dai of first multicodec entry is used
1115 if (platform->driver->delay)
1116 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
1118 runtime->delay = delay;
1120 return offset;
1123 /* connect a FE and BE */
1124 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1125 struct snd_soc_pcm_runtime *be, int stream)
1127 struct snd_soc_dpcm *dpcm;
1129 /* only add new dpcms */
1130 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1131 if (dpcm->be == be && dpcm->fe == fe)
1132 return 0;
1135 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1136 if (!dpcm)
1137 return -ENOMEM;
1139 dpcm->be = be;
1140 dpcm->fe = fe;
1141 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1142 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1143 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1144 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1146 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1147 stream ? "capture" : "playback", fe->dai_link->name,
1148 stream ? "<-" : "->", be->dai_link->name);
1150 #ifdef CONFIG_DEBUG_FS
1151 if (fe->debugfs_dpcm_root)
1152 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1153 fe->debugfs_dpcm_root, &dpcm->state);
1154 #endif
1155 return 1;
1158 /* reparent a BE onto another FE */
1159 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1160 struct snd_soc_pcm_runtime *be, int stream)
1162 struct snd_soc_dpcm *dpcm;
1163 struct snd_pcm_substream *fe_substream, *be_substream;
1165 /* reparent if BE is connected to other FEs */
1166 if (!be->dpcm[stream].users)
1167 return;
1169 be_substream = snd_soc_dpcm_get_substream(be, stream);
1171 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1172 if (dpcm->fe == fe)
1173 continue;
1175 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1176 stream ? "capture" : "playback",
1177 dpcm->fe->dai_link->name,
1178 stream ? "<-" : "->", dpcm->be->dai_link->name);
1180 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1181 be_substream->runtime = fe_substream->runtime;
1182 break;
1186 /* disconnect a BE and FE */
1187 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1189 struct snd_soc_dpcm *dpcm, *d;
1191 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
1192 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1193 stream ? "capture" : "playback",
1194 dpcm->be->dai_link->name);
1196 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1197 continue;
1199 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1200 stream ? "capture" : "playback", fe->dai_link->name,
1201 stream ? "<-" : "->", dpcm->be->dai_link->name);
1203 /* BEs still alive need new FE */
1204 dpcm_be_reparent(fe, dpcm->be, stream);
1206 #ifdef CONFIG_DEBUG_FS
1207 debugfs_remove(dpcm->debugfs_state);
1208 #endif
1209 list_del(&dpcm->list_be);
1210 list_del(&dpcm->list_fe);
1211 kfree(dpcm);
1215 /* get BE for DAI widget and stream */
1216 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1217 struct snd_soc_dapm_widget *widget, int stream)
1219 struct snd_soc_pcm_runtime *be;
1220 int i, j;
1222 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1223 for (i = 0; i < card->num_links; i++) {
1224 be = &card->rtd[i];
1226 if (!be->dai_link->no_pcm)
1227 continue;
1229 if (be->cpu_dai->playback_widget == widget)
1230 return be;
1232 for (j = 0; j < be->num_codecs; j++) {
1233 struct snd_soc_dai *dai = be->codec_dais[j];
1234 if (dai->playback_widget == widget)
1235 return be;
1238 } else {
1240 for (i = 0; i < card->num_links; i++) {
1241 be = &card->rtd[i];
1243 if (!be->dai_link->no_pcm)
1244 continue;
1246 if (be->cpu_dai->capture_widget == widget)
1247 return be;
1249 for (j = 0; j < be->num_codecs; j++) {
1250 struct snd_soc_dai *dai = be->codec_dais[j];
1251 if (dai->capture_widget == widget)
1252 return be;
1257 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1258 stream ? "capture" : "playback", widget->name);
1259 return NULL;
1262 static inline struct snd_soc_dapm_widget *
1263 dai_get_widget(struct snd_soc_dai *dai, int stream)
1265 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1266 return dai->playback_widget;
1267 else
1268 return dai->capture_widget;
1271 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1272 struct snd_soc_dapm_widget *widget)
1274 int i;
1276 for (i = 0; i < list->num_widgets; i++) {
1277 if (widget == list->widgets[i])
1278 return 1;
1281 return 0;
1284 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1285 int stream, struct snd_soc_dapm_widget_list **list)
1287 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1288 int paths;
1290 /* get number of valid DAI paths and their widgets */
1291 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
1293 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1294 stream ? "capture" : "playback");
1296 return paths;
1299 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1300 struct snd_soc_dapm_widget_list **list_)
1302 struct snd_soc_dpcm *dpcm;
1303 struct snd_soc_dapm_widget_list *list = *list_;
1304 struct snd_soc_dapm_widget *widget;
1305 int prune = 0;
1307 /* Destroy any old FE <--> BE connections */
1308 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1309 unsigned int i;
1311 /* is there a valid CPU DAI widget for this BE */
1312 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1314 /* prune the BE if it's no longer in our active list */
1315 if (widget && widget_in_list(list, widget))
1316 continue;
1318 /* is there a valid CODEC DAI widget for this BE */
1319 for (i = 0; i < dpcm->be->num_codecs; i++) {
1320 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1321 widget = dai_get_widget(dai, stream);
1323 /* prune the BE if it's no longer in our active list */
1324 if (widget && widget_in_list(list, widget))
1325 continue;
1328 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1329 stream ? "capture" : "playback",
1330 dpcm->be->dai_link->name, fe->dai_link->name);
1331 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1332 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1333 prune++;
1336 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1337 return prune;
1340 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1341 struct snd_soc_dapm_widget_list **list_)
1343 struct snd_soc_card *card = fe->card;
1344 struct snd_soc_dapm_widget_list *list = *list_;
1345 struct snd_soc_pcm_runtime *be;
1346 int i, new = 0, err;
1348 /* Create any new FE <--> BE connections */
1349 for (i = 0; i < list->num_widgets; i++) {
1351 switch (list->widgets[i]->id) {
1352 case snd_soc_dapm_dai_in:
1353 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1354 continue;
1355 break;
1356 case snd_soc_dapm_dai_out:
1357 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1358 continue;
1359 break;
1360 default:
1361 continue;
1364 /* is there a valid BE rtd for this widget */
1365 be = dpcm_get_be(card, list->widgets[i], stream);
1366 if (!be) {
1367 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1368 list->widgets[i]->name);
1369 continue;
1372 /* make sure BE is a real BE */
1373 if (!be->dai_link->no_pcm)
1374 continue;
1376 /* don't connect if FE is not running */
1377 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1378 continue;
1380 /* newly connected FE and BE */
1381 err = dpcm_be_connect(fe, be, stream);
1382 if (err < 0) {
1383 dev_err(fe->dev, "ASoC: can't connect %s\n",
1384 list->widgets[i]->name);
1385 break;
1386 } else if (err == 0) /* already connected */
1387 continue;
1389 /* new */
1390 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1391 new++;
1394 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1395 return new;
1399 * Find the corresponding BE DAIs that source or sink audio to this
1400 * FE substream.
1402 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1403 int stream, struct snd_soc_dapm_widget_list **list, int new)
1405 if (new)
1406 return dpcm_add_paths(fe, stream, list);
1407 else
1408 return dpcm_prune_paths(fe, stream, list);
1411 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1413 struct snd_soc_dpcm *dpcm;
1415 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1416 dpcm->be->dpcm[stream].runtime_update =
1417 SND_SOC_DPCM_UPDATE_NO;
1420 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1421 int stream)
1423 struct snd_soc_dpcm *dpcm;
1425 /* disable any enabled and non active backends */
1426 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1428 struct snd_soc_pcm_runtime *be = dpcm->be;
1429 struct snd_pcm_substream *be_substream =
1430 snd_soc_dpcm_get_substream(be, stream);
1432 if (be->dpcm[stream].users == 0)
1433 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1434 stream ? "capture" : "playback",
1435 be->dpcm[stream].state);
1437 if (--be->dpcm[stream].users != 0)
1438 continue;
1440 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1441 continue;
1443 soc_pcm_close(be_substream);
1444 be_substream->runtime = NULL;
1445 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1449 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1451 struct snd_soc_dpcm *dpcm;
1452 int err, count = 0;
1454 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1455 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1457 struct snd_soc_pcm_runtime *be = dpcm->be;
1458 struct snd_pcm_substream *be_substream =
1459 snd_soc_dpcm_get_substream(be, stream);
1461 if (!be_substream) {
1462 dev_err(be->dev, "ASoC: no backend %s stream\n",
1463 stream ? "capture" : "playback");
1464 continue;
1467 /* is this op for this BE ? */
1468 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1469 continue;
1471 /* first time the dpcm is open ? */
1472 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1473 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1474 stream ? "capture" : "playback",
1475 be->dpcm[stream].state);
1477 if (be->dpcm[stream].users++ != 0)
1478 continue;
1480 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1481 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1482 continue;
1484 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1485 stream ? "capture" : "playback", be->dai_link->name);
1487 be_substream->runtime = be->dpcm[stream].runtime;
1488 err = soc_pcm_open(be_substream);
1489 if (err < 0) {
1490 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1491 be->dpcm[stream].users--;
1492 if (be->dpcm[stream].users < 0)
1493 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1494 stream ? "capture" : "playback",
1495 be->dpcm[stream].state);
1497 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1498 goto unwind;
1501 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1502 count++;
1505 return count;
1507 unwind:
1508 /* disable any enabled and non active backends */
1509 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1510 struct snd_soc_pcm_runtime *be = dpcm->be;
1511 struct snd_pcm_substream *be_substream =
1512 snd_soc_dpcm_get_substream(be, stream);
1514 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1515 continue;
1517 if (be->dpcm[stream].users == 0)
1518 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1519 stream ? "capture" : "playback",
1520 be->dpcm[stream].state);
1522 if (--be->dpcm[stream].users != 0)
1523 continue;
1525 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1526 continue;
1528 soc_pcm_close(be_substream);
1529 be_substream->runtime = NULL;
1530 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1533 return err;
1536 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1537 struct snd_soc_pcm_stream *stream,
1538 u64 formats)
1540 runtime->hw.rate_min = stream->rate_min;
1541 runtime->hw.rate_max = stream->rate_max;
1542 runtime->hw.channels_min = stream->channels_min;
1543 runtime->hw.channels_max = stream->channels_max;
1544 if (runtime->hw.formats)
1545 runtime->hw.formats &= formats & stream->formats;
1546 else
1547 runtime->hw.formats = formats & stream->formats;
1548 runtime->hw.rates = stream->rates;
1551 static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1553 struct snd_soc_pcm_runtime *fe = substream->private_data;
1554 struct snd_soc_dpcm *dpcm;
1555 u64 formats = ULLONG_MAX;
1556 int stream = substream->stream;
1558 if (!fe->dai_link->dpcm_merged_format)
1559 return formats;
1562 * It returns merged BE codec format
1563 * if FE want to use it (= dpcm_merged_format)
1566 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1567 struct snd_soc_pcm_runtime *be = dpcm->be;
1568 struct snd_soc_dai_driver *codec_dai_drv;
1569 struct snd_soc_pcm_stream *codec_stream;
1570 int i;
1572 for (i = 0; i < be->num_codecs; i++) {
1574 * Skip CODECs which don't support the current stream
1575 * type. See soc_pcm_init_runtime_hw() for more details
1577 if (!snd_soc_dai_stream_valid(be->codec_dais[i],
1578 stream))
1579 continue;
1581 codec_dai_drv = be->codec_dais[i]->driver;
1582 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1583 codec_stream = &codec_dai_drv->playback;
1584 else
1585 codec_stream = &codec_dai_drv->capture;
1587 formats &= codec_stream->formats;
1591 return formats;
1594 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1596 struct snd_pcm_runtime *runtime = substream->runtime;
1597 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1598 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1599 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1600 u64 format = dpcm_runtime_base_format(substream);
1602 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1603 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
1604 else
1605 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
1608 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1610 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1611 * for avoiding the race with trigger callback.
1612 * If the state is unset and a trigger is pending while the previous operation,
1613 * process the pending trigger action here.
1615 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1616 int stream, enum snd_soc_dpcm_update state)
1618 struct snd_pcm_substream *substream =
1619 snd_soc_dpcm_get_substream(fe, stream);
1621 snd_pcm_stream_lock_irq(substream);
1622 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1623 dpcm_fe_dai_do_trigger(substream,
1624 fe->dpcm[stream].trigger_pending - 1);
1625 fe->dpcm[stream].trigger_pending = 0;
1627 fe->dpcm[stream].runtime_update = state;
1628 snd_pcm_stream_unlock_irq(substream);
1631 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1633 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1634 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1635 int stream = fe_substream->stream, ret = 0;
1637 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1639 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1640 if (ret < 0) {
1641 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1642 goto be_err;
1645 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1647 /* start the DAI frontend */
1648 ret = soc_pcm_open(fe_substream);
1649 if (ret < 0) {
1650 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1651 goto unwind;
1654 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1656 dpcm_set_fe_runtime(fe_substream);
1657 snd_pcm_limit_hw_rates(runtime);
1659 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1660 return 0;
1662 unwind:
1663 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1664 be_err:
1665 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1666 return ret;
1669 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1671 struct snd_soc_dpcm *dpcm;
1673 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1674 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1676 struct snd_soc_pcm_runtime *be = dpcm->be;
1677 struct snd_pcm_substream *be_substream =
1678 snd_soc_dpcm_get_substream(be, stream);
1680 /* is this op for this BE ? */
1681 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1682 continue;
1684 if (be->dpcm[stream].users == 0)
1685 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1686 stream ? "capture" : "playback",
1687 be->dpcm[stream].state);
1689 if (--be->dpcm[stream].users != 0)
1690 continue;
1692 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1693 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1694 soc_pcm_hw_free(be_substream);
1695 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1698 dev_dbg(be->dev, "ASoC: close BE %s\n",
1699 dpcm->fe->dai_link->name);
1701 soc_pcm_close(be_substream);
1702 be_substream->runtime = NULL;
1704 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1706 return 0;
1709 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1711 struct snd_soc_pcm_runtime *fe = substream->private_data;
1712 int stream = substream->stream;
1714 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1716 /* shutdown the BEs */
1717 dpcm_be_dai_shutdown(fe, substream->stream);
1719 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1721 /* now shutdown the frontend */
1722 soc_pcm_close(substream);
1724 /* run the stream event for each BE */
1725 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1727 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1728 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1729 return 0;
1732 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1734 struct snd_soc_dpcm *dpcm;
1736 /* only hw_params backends that are either sinks or sources
1737 * to this frontend DAI */
1738 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1740 struct snd_soc_pcm_runtime *be = dpcm->be;
1741 struct snd_pcm_substream *be_substream =
1742 snd_soc_dpcm_get_substream(be, stream);
1744 /* is this op for this BE ? */
1745 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1746 continue;
1748 /* only free hw when no longer used - check all FEs */
1749 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1750 continue;
1752 /* do not free hw if this BE is used by other FE */
1753 if (be->dpcm[stream].users > 1)
1754 continue;
1756 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1757 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1758 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1759 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1760 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1761 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1762 continue;
1764 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1765 dpcm->fe->dai_link->name);
1767 soc_pcm_hw_free(be_substream);
1769 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1772 return 0;
1775 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1777 struct snd_soc_pcm_runtime *fe = substream->private_data;
1778 int err, stream = substream->stream;
1780 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1781 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1783 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1785 /* call hw_free on the frontend */
1786 err = soc_pcm_hw_free(substream);
1787 if (err < 0)
1788 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1789 fe->dai_link->name);
1791 /* only hw_params backends that are either sinks or sources
1792 * to this frontend DAI */
1793 err = dpcm_be_dai_hw_free(fe, stream);
1795 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1796 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1798 mutex_unlock(&fe->card->mutex);
1799 return 0;
1802 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1804 struct snd_soc_dpcm *dpcm;
1805 int ret;
1807 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1809 struct snd_soc_pcm_runtime *be = dpcm->be;
1810 struct snd_pcm_substream *be_substream =
1811 snd_soc_dpcm_get_substream(be, stream);
1813 /* is this op for this BE ? */
1814 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1815 continue;
1817 /* only allow hw_params() if no connected FEs are running */
1818 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1819 continue;
1821 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1822 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1823 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1824 continue;
1826 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1827 dpcm->fe->dai_link->name);
1829 /* copy params for each dpcm */
1830 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1831 sizeof(struct snd_pcm_hw_params));
1833 /* perform any hw_params fixups */
1834 if (be->dai_link->be_hw_params_fixup) {
1835 ret = be->dai_link->be_hw_params_fixup(be,
1836 &dpcm->hw_params);
1837 if (ret < 0) {
1838 dev_err(be->dev,
1839 "ASoC: hw_params BE fixup failed %d\n",
1840 ret);
1841 goto unwind;
1845 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1846 if (ret < 0) {
1847 dev_err(dpcm->be->dev,
1848 "ASoC: hw_params BE failed %d\n", ret);
1849 goto unwind;
1852 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1854 return 0;
1856 unwind:
1857 /* disable any enabled and non active backends */
1858 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1859 struct snd_soc_pcm_runtime *be = dpcm->be;
1860 struct snd_pcm_substream *be_substream =
1861 snd_soc_dpcm_get_substream(be, stream);
1863 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1864 continue;
1866 /* only allow hw_free() if no connected FEs are running */
1867 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1868 continue;
1870 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1871 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1872 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1873 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1874 continue;
1876 soc_pcm_hw_free(be_substream);
1879 return ret;
1882 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1883 struct snd_pcm_hw_params *params)
1885 struct snd_soc_pcm_runtime *fe = substream->private_data;
1886 int ret, stream = substream->stream;
1888 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1889 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1891 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1892 sizeof(struct snd_pcm_hw_params));
1893 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1894 if (ret < 0) {
1895 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1896 goto out;
1899 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1900 fe->dai_link->name, params_rate(params),
1901 params_channels(params), params_format(params));
1903 /* call hw_params on the frontend */
1904 ret = soc_pcm_hw_params(substream, params);
1905 if (ret < 0) {
1906 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1907 dpcm_be_dai_hw_free(fe, stream);
1908 } else
1909 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1911 out:
1912 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1913 mutex_unlock(&fe->card->mutex);
1914 return ret;
1917 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1918 struct snd_pcm_substream *substream, int cmd)
1920 int ret;
1922 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1923 dpcm->fe->dai_link->name, cmd);
1925 ret = soc_pcm_trigger(substream, cmd);
1926 if (ret < 0)
1927 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1929 return ret;
1932 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1933 int cmd)
1935 struct snd_soc_dpcm *dpcm;
1936 int ret = 0;
1938 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1940 struct snd_soc_pcm_runtime *be = dpcm->be;
1941 struct snd_pcm_substream *be_substream =
1942 snd_soc_dpcm_get_substream(be, stream);
1944 /* is this op for this BE ? */
1945 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1946 continue;
1948 switch (cmd) {
1949 case SNDRV_PCM_TRIGGER_START:
1950 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1951 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1952 continue;
1954 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1955 if (ret)
1956 return ret;
1958 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1959 break;
1960 case SNDRV_PCM_TRIGGER_RESUME:
1961 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1962 continue;
1964 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1965 if (ret)
1966 return ret;
1968 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1969 break;
1970 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1971 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1972 continue;
1974 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1975 if (ret)
1976 return ret;
1978 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1979 break;
1980 case SNDRV_PCM_TRIGGER_STOP:
1981 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1982 continue;
1984 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1985 continue;
1987 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1988 if (ret)
1989 return ret;
1991 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1992 break;
1993 case SNDRV_PCM_TRIGGER_SUSPEND:
1994 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1995 continue;
1997 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1998 continue;
2000 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2001 if (ret)
2002 return ret;
2004 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2005 break;
2006 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2007 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2008 continue;
2010 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2011 continue;
2013 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2014 if (ret)
2015 return ret;
2017 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2018 break;
2022 return ret;
2024 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2026 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2028 struct snd_soc_pcm_runtime *fe = substream->private_data;
2029 int stream = substream->stream, ret;
2030 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2032 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2034 switch (trigger) {
2035 case SND_SOC_DPCM_TRIGGER_PRE:
2036 /* call trigger on the frontend before the backend. */
2038 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2039 fe->dai_link->name, cmd);
2041 ret = soc_pcm_trigger(substream, cmd);
2042 if (ret < 0) {
2043 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2044 goto out;
2047 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2048 break;
2049 case SND_SOC_DPCM_TRIGGER_POST:
2050 /* call trigger on the frontend after the backend. */
2052 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2053 if (ret < 0) {
2054 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2055 goto out;
2058 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2059 fe->dai_link->name, cmd);
2061 ret = soc_pcm_trigger(substream, cmd);
2062 break;
2063 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2064 /* bespoke trigger() - handles both FE and BEs */
2066 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2067 fe->dai_link->name, cmd);
2069 ret = soc_pcm_bespoke_trigger(substream, cmd);
2070 if (ret < 0) {
2071 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2072 goto out;
2074 break;
2075 default:
2076 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2077 fe->dai_link->name);
2078 ret = -EINVAL;
2079 goto out;
2082 switch (cmd) {
2083 case SNDRV_PCM_TRIGGER_START:
2084 case SNDRV_PCM_TRIGGER_RESUME:
2085 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2086 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2087 break;
2088 case SNDRV_PCM_TRIGGER_STOP:
2089 case SNDRV_PCM_TRIGGER_SUSPEND:
2090 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2091 break;
2092 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2093 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2094 break;
2097 out:
2098 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2099 return ret;
2102 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2104 struct snd_soc_pcm_runtime *fe = substream->private_data;
2105 int stream = substream->stream;
2107 /* if FE's runtime_update is already set, we're in race;
2108 * process this trigger later at exit
2110 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2111 fe->dpcm[stream].trigger_pending = cmd + 1;
2112 return 0; /* delayed, assuming it's successful */
2115 /* we're alone, let's trigger */
2116 return dpcm_fe_dai_do_trigger(substream, cmd);
2119 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2121 struct snd_soc_dpcm *dpcm;
2122 int ret = 0;
2124 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2126 struct snd_soc_pcm_runtime *be = dpcm->be;
2127 struct snd_pcm_substream *be_substream =
2128 snd_soc_dpcm_get_substream(be, stream);
2130 /* is this op for this BE ? */
2131 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2132 continue;
2134 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2135 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2136 continue;
2138 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2139 dpcm->fe->dai_link->name);
2141 ret = soc_pcm_prepare(be_substream);
2142 if (ret < 0) {
2143 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2144 ret);
2145 break;
2148 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2150 return ret;
2153 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2155 struct snd_soc_pcm_runtime *fe = substream->private_data;
2156 int stream = substream->stream, ret = 0;
2158 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2160 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2162 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2164 /* there is no point preparing this FE if there are no BEs */
2165 if (list_empty(&fe->dpcm[stream].be_clients)) {
2166 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2167 fe->dai_link->name);
2168 ret = -EINVAL;
2169 goto out;
2172 ret = dpcm_be_dai_prepare(fe, substream->stream);
2173 if (ret < 0)
2174 goto out;
2176 /* call prepare on the frontend */
2177 ret = soc_pcm_prepare(substream);
2178 if (ret < 0) {
2179 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2180 fe->dai_link->name);
2181 goto out;
2184 /* run the stream event for each BE */
2185 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2186 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2188 out:
2189 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2190 mutex_unlock(&fe->card->mutex);
2192 return ret;
2195 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2196 unsigned int cmd, void *arg)
2198 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2199 struct snd_soc_platform *platform = rtd->platform;
2201 if (platform->driver->ops && platform->driver->ops->ioctl)
2202 return platform->driver->ops->ioctl(substream, cmd, arg);
2203 return snd_pcm_lib_ioctl(substream, cmd, arg);
2206 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2208 struct snd_pcm_substream *substream =
2209 snd_soc_dpcm_get_substream(fe, stream);
2210 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2211 int err;
2213 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2214 stream ? "capture" : "playback", fe->dai_link->name);
2216 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2217 /* call bespoke trigger - FE takes care of all BE triggers */
2218 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2219 fe->dai_link->name);
2221 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2222 if (err < 0)
2223 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2224 } else {
2225 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2226 fe->dai_link->name);
2228 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2229 if (err < 0)
2230 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2233 err = dpcm_be_dai_hw_free(fe, stream);
2234 if (err < 0)
2235 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2237 err = dpcm_be_dai_shutdown(fe, stream);
2238 if (err < 0)
2239 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2241 /* run the stream event for each BE */
2242 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2244 return 0;
2247 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2249 struct snd_pcm_substream *substream =
2250 snd_soc_dpcm_get_substream(fe, stream);
2251 struct snd_soc_dpcm *dpcm;
2252 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2253 int ret;
2255 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2256 stream ? "capture" : "playback", fe->dai_link->name);
2258 /* Only start the BE if the FE is ready */
2259 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2260 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2261 return -EINVAL;
2263 /* startup must always be called for new BEs */
2264 ret = dpcm_be_dai_startup(fe, stream);
2265 if (ret < 0)
2266 goto disconnect;
2268 /* keep going if FE state is > open */
2269 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2270 return 0;
2272 ret = dpcm_be_dai_hw_params(fe, stream);
2273 if (ret < 0)
2274 goto close;
2276 /* keep going if FE state is > hw_params */
2277 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2278 return 0;
2281 ret = dpcm_be_dai_prepare(fe, stream);
2282 if (ret < 0)
2283 goto hw_free;
2285 /* run the stream event for each BE */
2286 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2288 /* keep going if FE state is > prepare */
2289 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2290 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2291 return 0;
2293 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2294 /* call trigger on the frontend - FE takes care of all BE triggers */
2295 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2296 fe->dai_link->name);
2298 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2299 if (ret < 0) {
2300 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2301 goto hw_free;
2303 } else {
2304 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2305 fe->dai_link->name);
2307 ret = dpcm_be_dai_trigger(fe, stream,
2308 SNDRV_PCM_TRIGGER_START);
2309 if (ret < 0) {
2310 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2311 goto hw_free;
2315 return 0;
2317 hw_free:
2318 dpcm_be_dai_hw_free(fe, stream);
2319 close:
2320 dpcm_be_dai_shutdown(fe, stream);
2321 disconnect:
2322 /* disconnect any non started BEs */
2323 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2324 struct snd_soc_pcm_runtime *be = dpcm->be;
2325 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2326 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2329 return ret;
2332 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2334 int ret;
2336 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2337 ret = dpcm_run_update_startup(fe, stream);
2338 if (ret < 0)
2339 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2340 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2342 return ret;
2345 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2347 int ret;
2349 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2350 ret = dpcm_run_update_shutdown(fe, stream);
2351 if (ret < 0)
2352 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2353 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2355 return ret;
2358 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2359 * any DAI links.
2361 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2363 int i, old, new, paths;
2365 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2366 for (i = 0; i < card->num_rtd; i++) {
2367 struct snd_soc_dapm_widget_list *list;
2368 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2370 /* make sure link is FE */
2371 if (!fe->dai_link->dynamic)
2372 continue;
2374 /* only check active links */
2375 if (!fe->cpu_dai->active)
2376 continue;
2378 /* DAPM sync will call this to update DSP paths */
2379 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2380 fe->dai_link->name);
2382 /* skip if FE doesn't have playback capability */
2383 if (!fe->cpu_dai->driver->playback.channels_min
2384 || !fe->codec_dai->driver->playback.channels_min)
2385 goto capture;
2387 /* skip if FE isn't currently playing */
2388 if (!fe->cpu_dai->playback_active
2389 || !fe->codec_dai->playback_active)
2390 goto capture;
2392 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2393 if (paths < 0) {
2394 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2395 fe->dai_link->name, "playback");
2396 mutex_unlock(&card->mutex);
2397 return paths;
2400 /* update any new playback paths */
2401 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2402 if (new) {
2403 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2404 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2405 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2408 /* update any old playback paths */
2409 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2410 if (old) {
2411 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2412 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2413 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2416 dpcm_path_put(&list);
2417 capture:
2418 /* skip if FE doesn't have capture capability */
2419 if (!fe->cpu_dai->driver->capture.channels_min
2420 || !fe->codec_dai->driver->capture.channels_min)
2421 continue;
2423 /* skip if FE isn't currently capturing */
2424 if (!fe->cpu_dai->capture_active
2425 || !fe->codec_dai->capture_active)
2426 continue;
2428 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2429 if (paths < 0) {
2430 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2431 fe->dai_link->name, "capture");
2432 mutex_unlock(&card->mutex);
2433 return paths;
2436 /* update any new capture paths */
2437 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2438 if (new) {
2439 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2440 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2441 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2444 /* update any old capture paths */
2445 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2446 if (old) {
2447 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2448 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2449 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2452 dpcm_path_put(&list);
2455 mutex_unlock(&card->mutex);
2456 return 0;
2458 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2460 struct snd_soc_dpcm *dpcm;
2461 struct list_head *clients =
2462 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2464 list_for_each_entry(dpcm, clients, list_be) {
2466 struct snd_soc_pcm_runtime *be = dpcm->be;
2467 int i;
2469 if (be->dai_link->ignore_suspend)
2470 continue;
2472 for (i = 0; i < be->num_codecs; i++) {
2473 struct snd_soc_dai *dai = be->codec_dais[i];
2474 struct snd_soc_dai_driver *drv = dai->driver;
2476 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2477 be->dai_link->name);
2479 if (drv->ops && drv->ops->digital_mute &&
2480 dai->playback_active)
2481 drv->ops->digital_mute(dai, mute);
2485 return 0;
2488 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2490 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2491 struct snd_soc_dpcm *dpcm;
2492 struct snd_soc_dapm_widget_list *list;
2493 int ret;
2494 int stream = fe_substream->stream;
2496 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2497 fe->dpcm[stream].runtime = fe_substream->runtime;
2499 ret = dpcm_path_get(fe, stream, &list);
2500 if (ret < 0) {
2501 mutex_unlock(&fe->card->mutex);
2502 return ret;
2503 } else if (ret == 0) {
2504 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2505 fe->dai_link->name, stream ? "capture" : "playback");
2508 /* calculate valid and active FE <-> BE dpcms */
2509 dpcm_process_paths(fe, stream, &list, 1);
2511 ret = dpcm_fe_dai_startup(fe_substream);
2512 if (ret < 0) {
2513 /* clean up all links */
2514 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2515 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2517 dpcm_be_disconnect(fe, stream);
2518 fe->dpcm[stream].runtime = NULL;
2521 dpcm_clear_pending_state(fe, stream);
2522 dpcm_path_put(&list);
2523 mutex_unlock(&fe->card->mutex);
2524 return ret;
2527 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2529 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2530 struct snd_soc_dpcm *dpcm;
2531 int stream = fe_substream->stream, ret;
2533 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2534 ret = dpcm_fe_dai_shutdown(fe_substream);
2536 /* mark FE's links ready to prune */
2537 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2538 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2540 dpcm_be_disconnect(fe, stream);
2542 fe->dpcm[stream].runtime = NULL;
2543 mutex_unlock(&fe->card->mutex);
2544 return ret;
2547 /* create a new pcm */
2548 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2550 struct snd_soc_platform *platform = rtd->platform;
2551 struct snd_soc_dai *codec_dai;
2552 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2553 struct snd_pcm *pcm;
2554 char new_name[64];
2555 int ret = 0, playback = 0, capture = 0;
2556 int i;
2558 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2559 playback = rtd->dai_link->dpcm_playback;
2560 capture = rtd->dai_link->dpcm_capture;
2561 } else {
2562 for (i = 0; i < rtd->num_codecs; i++) {
2563 codec_dai = rtd->codec_dais[i];
2564 if (codec_dai->driver->playback.channels_min)
2565 playback = 1;
2566 if (codec_dai->driver->capture.channels_min)
2567 capture = 1;
2570 capture = capture && cpu_dai->driver->capture.channels_min;
2571 playback = playback && cpu_dai->driver->playback.channels_min;
2574 if (rtd->dai_link->playback_only) {
2575 playback = 1;
2576 capture = 0;
2579 if (rtd->dai_link->capture_only) {
2580 playback = 0;
2581 capture = 1;
2584 /* create the PCM */
2585 if (rtd->dai_link->no_pcm) {
2586 snprintf(new_name, sizeof(new_name), "(%s)",
2587 rtd->dai_link->stream_name);
2589 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2590 playback, capture, &pcm);
2591 } else {
2592 if (rtd->dai_link->dynamic)
2593 snprintf(new_name, sizeof(new_name), "%s (*)",
2594 rtd->dai_link->stream_name);
2595 else
2596 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2597 rtd->dai_link->stream_name,
2598 (rtd->num_codecs > 1) ?
2599 "multicodec" : rtd->codec_dai->name, num);
2601 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2602 capture, &pcm);
2604 if (ret < 0) {
2605 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2606 rtd->dai_link->name);
2607 return ret;
2609 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2611 /* DAPM dai link stream work */
2612 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2614 pcm->nonatomic = rtd->dai_link->nonatomic;
2615 rtd->pcm = pcm;
2616 pcm->private_data = rtd;
2618 if (rtd->dai_link->no_pcm) {
2619 if (playback)
2620 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2621 if (capture)
2622 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2623 goto out;
2626 /* ASoC PCM operations */
2627 if (rtd->dai_link->dynamic) {
2628 rtd->ops.open = dpcm_fe_dai_open;
2629 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2630 rtd->ops.prepare = dpcm_fe_dai_prepare;
2631 rtd->ops.trigger = dpcm_fe_dai_trigger;
2632 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2633 rtd->ops.close = dpcm_fe_dai_close;
2634 rtd->ops.pointer = soc_pcm_pointer;
2635 rtd->ops.ioctl = soc_pcm_ioctl;
2636 } else {
2637 rtd->ops.open = soc_pcm_open;
2638 rtd->ops.hw_params = soc_pcm_hw_params;
2639 rtd->ops.prepare = soc_pcm_prepare;
2640 rtd->ops.trigger = soc_pcm_trigger;
2641 rtd->ops.hw_free = soc_pcm_hw_free;
2642 rtd->ops.close = soc_pcm_close;
2643 rtd->ops.pointer = soc_pcm_pointer;
2644 rtd->ops.ioctl = soc_pcm_ioctl;
2647 if (platform->driver->ops) {
2648 rtd->ops.ack = platform->driver->ops->ack;
2649 rtd->ops.copy = platform->driver->ops->copy;
2650 rtd->ops.silence = platform->driver->ops->silence;
2651 rtd->ops.page = platform->driver->ops->page;
2652 rtd->ops.mmap = platform->driver->ops->mmap;
2655 if (playback)
2656 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2658 if (capture)
2659 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2661 if (platform->driver->pcm_new) {
2662 ret = platform->driver->pcm_new(rtd);
2663 if (ret < 0) {
2664 dev_err(platform->dev,
2665 "ASoC: pcm constructor failed: %d\n",
2666 ret);
2667 return ret;
2671 pcm->private_free = platform->driver->pcm_free;
2672 out:
2673 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2674 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2675 cpu_dai->name);
2676 return ret;
2679 /* is the current PCM operation for this FE ? */
2680 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2682 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2683 return 1;
2684 return 0;
2686 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2688 /* is the current PCM operation for this BE ? */
2689 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2690 struct snd_soc_pcm_runtime *be, int stream)
2692 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2693 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2694 be->dpcm[stream].runtime_update))
2695 return 1;
2696 return 0;
2698 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2700 /* get the substream for this BE */
2701 struct snd_pcm_substream *
2702 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2704 return be->pcm->streams[stream].substream;
2706 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2708 /* get the BE runtime state */
2709 enum snd_soc_dpcm_state
2710 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2712 return be->dpcm[stream].state;
2714 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2716 /* set the BE runtime state */
2717 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2718 int stream, enum snd_soc_dpcm_state state)
2720 be->dpcm[stream].state = state;
2722 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2725 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2726 * are not running, paused or suspended for the specified stream direction.
2728 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2729 struct snd_soc_pcm_runtime *be, int stream)
2731 struct snd_soc_dpcm *dpcm;
2732 int state;
2734 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2736 if (dpcm->fe == fe)
2737 continue;
2739 state = dpcm->fe->dpcm[stream].state;
2740 if (state == SND_SOC_DPCM_STATE_START ||
2741 state == SND_SOC_DPCM_STATE_PAUSED ||
2742 state == SND_SOC_DPCM_STATE_SUSPEND)
2743 return 0;
2746 /* it's safe to free/stop this BE DAI */
2747 return 1;
2749 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2752 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2753 * running, paused or suspended for the specified stream direction.
2755 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2756 struct snd_soc_pcm_runtime *be, int stream)
2758 struct snd_soc_dpcm *dpcm;
2759 int state;
2761 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2763 if (dpcm->fe == fe)
2764 continue;
2766 state = dpcm->fe->dpcm[stream].state;
2767 if (state == SND_SOC_DPCM_STATE_START ||
2768 state == SND_SOC_DPCM_STATE_PAUSED ||
2769 state == SND_SOC_DPCM_STATE_SUSPEND ||
2770 state == SND_SOC_DPCM_STATE_PREPARE)
2771 return 0;
2774 /* it's safe to change hw_params */
2775 return 1;
2777 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2779 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2780 int cmd, struct snd_soc_platform *platform)
2782 if (platform->driver->ops && platform->driver->ops->trigger)
2783 return platform->driver->ops->trigger(substream, cmd);
2784 return 0;
2786 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2788 #ifdef CONFIG_DEBUG_FS
2789 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2791 switch (state) {
2792 case SND_SOC_DPCM_STATE_NEW:
2793 return "new";
2794 case SND_SOC_DPCM_STATE_OPEN:
2795 return "open";
2796 case SND_SOC_DPCM_STATE_HW_PARAMS:
2797 return "hw_params";
2798 case SND_SOC_DPCM_STATE_PREPARE:
2799 return "prepare";
2800 case SND_SOC_DPCM_STATE_START:
2801 return "start";
2802 case SND_SOC_DPCM_STATE_STOP:
2803 return "stop";
2804 case SND_SOC_DPCM_STATE_SUSPEND:
2805 return "suspend";
2806 case SND_SOC_DPCM_STATE_PAUSED:
2807 return "paused";
2808 case SND_SOC_DPCM_STATE_HW_FREE:
2809 return "hw_free";
2810 case SND_SOC_DPCM_STATE_CLOSE:
2811 return "close";
2814 return "unknown";
2817 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2818 int stream, char *buf, size_t size)
2820 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2821 struct snd_soc_dpcm *dpcm;
2822 ssize_t offset = 0;
2824 /* FE state */
2825 offset += snprintf(buf + offset, size - offset,
2826 "[%s - %s]\n", fe->dai_link->name,
2827 stream ? "Capture" : "Playback");
2829 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2830 dpcm_state_string(fe->dpcm[stream].state));
2832 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2833 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2834 offset += snprintf(buf + offset, size - offset,
2835 "Hardware Params: "
2836 "Format = %s, Channels = %d, Rate = %d\n",
2837 snd_pcm_format_name(params_format(params)),
2838 params_channels(params),
2839 params_rate(params));
2841 /* BEs state */
2842 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2844 if (list_empty(&fe->dpcm[stream].be_clients)) {
2845 offset += snprintf(buf + offset, size - offset,
2846 " No active DSP links\n");
2847 goto out;
2850 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2851 struct snd_soc_pcm_runtime *be = dpcm->be;
2852 params = &dpcm->hw_params;
2854 offset += snprintf(buf + offset, size - offset,
2855 "- %s\n", be->dai_link->name);
2857 offset += snprintf(buf + offset, size - offset,
2858 " State: %s\n",
2859 dpcm_state_string(be->dpcm[stream].state));
2861 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2862 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2863 offset += snprintf(buf + offset, size - offset,
2864 " Hardware Params: "
2865 "Format = %s, Channels = %d, Rate = %d\n",
2866 snd_pcm_format_name(params_format(params)),
2867 params_channels(params),
2868 params_rate(params));
2871 out:
2872 return offset;
2875 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2876 size_t count, loff_t *ppos)
2878 struct snd_soc_pcm_runtime *fe = file->private_data;
2879 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2880 char *buf;
2882 buf = kmalloc(out_count, GFP_KERNEL);
2883 if (!buf)
2884 return -ENOMEM;
2886 if (fe->cpu_dai->driver->playback.channels_min)
2887 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2888 buf + offset, out_count - offset);
2890 if (fe->cpu_dai->driver->capture.channels_min)
2891 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2892 buf + offset, out_count - offset);
2894 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2896 kfree(buf);
2897 return ret;
2900 static const struct file_operations dpcm_state_fops = {
2901 .open = simple_open,
2902 .read = dpcm_state_read_file,
2903 .llseek = default_llseek,
2906 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2908 if (!rtd->dai_link)
2909 return;
2911 if (!rtd->card->debugfs_card_root)
2912 return;
2914 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2915 rtd->card->debugfs_card_root);
2916 if (!rtd->debugfs_dpcm_root) {
2917 dev_dbg(rtd->dev,
2918 "ASoC: Failed to create dpcm debugfs directory %s\n",
2919 rtd->dai_link->name);
2920 return;
2923 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2924 rtd->debugfs_dpcm_root,
2925 rtd, &dpcm_state_fops);
2927 #endif