iwl3945: do not print RFKILL message
[linux/fpc-iii.git] / sound / soc / soc-pcm.c
blob42782c01e41320e924efb4b93c839802f0df5722
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
37 /**
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
42 * Sets the substream runtime hardware parameters.
44 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
56 return 0;
58 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
60 /* DPCM stream event, send event to FE and all active BEs. */
61 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
62 int event)
64 struct snd_soc_dpcm *dpcm;
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
68 struct snd_soc_pcm_runtime *be = dpcm->be;
70 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
71 be->dai_link->name, event, dir);
73 snd_soc_dapm_stream_event(be, dir, event);
76 snd_soc_dapm_stream_event(fe, dir, event);
78 return 0;
81 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
85 int ret;
87 if (!soc_dai->driver->symmetric_rates &&
88 !rtd->dai_link->symmetric_rates)
89 return 0;
91 /* This can happen if multiple streams are starting simultaneously -
92 * the second can need to get its constraints before the first has
93 * picked a rate. Complain and allow the application to carry on.
95 if (!soc_dai->rate) {
96 dev_warn(soc_dai->dev,
97 "ASoC: Not enforcing symmetric_rates due to race\n");
98 return 0;
101 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
103 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
104 SNDRV_PCM_HW_PARAM_RATE,
105 soc_dai->rate, soc_dai->rate);
106 if (ret < 0) {
107 dev_err(soc_dai->dev,
108 "ASoC: Unable to apply rate symmetry constraint: %d\n",
109 ret);
110 return ret;
113 return 0;
117 * List of sample sizes that might go over the bus for parameter
118 * application. There ought to be a wildcard sample size for things
119 * like the DAC/ADC resolution to use but there isn't right now.
121 static int sample_sizes[] = {
122 24, 32,
125 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
126 struct snd_soc_dai *dai)
128 int ret, i, bits;
130 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
131 bits = dai->driver->playback.sig_bits;
132 else
133 bits = dai->driver->capture.sig_bits;
135 if (!bits)
136 return;
138 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
139 if (bits >= sample_sizes[i])
140 continue;
142 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
143 sample_sizes[i], bits);
144 if (ret != 0)
145 dev_warn(dai->dev,
146 "ASoC: Failed to set MSB %d/%d: %d\n",
147 bits, sample_sizes[i], ret);
151 static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
152 struct snd_soc_pcm_stream *codec_stream,
153 struct snd_soc_pcm_stream *cpu_stream)
155 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
156 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
157 hw->channels_min = max(codec_stream->channels_min,
158 cpu_stream->channels_min);
159 hw->channels_max = min(codec_stream->channels_max,
160 cpu_stream->channels_max);
161 hw->formats = codec_stream->formats & cpu_stream->formats;
162 hw->rates = codec_stream->rates & cpu_stream->rates;
163 if (codec_stream->rates
164 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
165 hw->rates |= cpu_stream->rates;
166 if (cpu_stream->rates
167 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
168 hw->rates |= codec_stream->rates;
172 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
173 * then initialized and any private data can be allocated. This also calls
174 * startup for the cpu DAI, platform, machine and codec DAI.
176 static int soc_pcm_open(struct snd_pcm_substream *substream)
178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 struct snd_soc_platform *platform = rtd->platform;
181 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
182 struct snd_soc_dai *codec_dai = rtd->codec_dai;
183 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
184 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
185 int ret = 0;
187 pinctrl_pm_select_default_state(cpu_dai->dev);
188 pinctrl_pm_select_default_state(codec_dai->dev);
189 pm_runtime_get_sync(cpu_dai->dev);
190 pm_runtime_get_sync(codec_dai->dev);
191 pm_runtime_get_sync(platform->dev);
193 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
195 /* startup the audio subsystem */
196 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
197 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
198 if (ret < 0) {
199 dev_err(cpu_dai->dev, "ASoC: can't open interface"
200 " %s: %d\n", cpu_dai->name, ret);
201 goto out;
205 if (platform->driver->ops && platform->driver->ops->open) {
206 ret = platform->driver->ops->open(substream);
207 if (ret < 0) {
208 dev_err(platform->dev, "ASoC: can't open platform"
209 " %s: %d\n", platform->name, ret);
210 goto platform_err;
214 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
215 ret = codec_dai->driver->ops->startup(substream, codec_dai);
216 if (ret < 0) {
217 dev_err(codec_dai->dev, "ASoC: can't open codec"
218 " %s: %d\n", codec_dai->name, ret);
219 goto codec_dai_err;
223 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
224 ret = rtd->dai_link->ops->startup(substream);
225 if (ret < 0) {
226 pr_err("ASoC: %s startup failed: %d\n",
227 rtd->dai_link->name, ret);
228 goto machine_err;
232 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
233 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
234 goto dynamic;
236 /* Check that the codec and cpu DAIs are compatible */
237 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
238 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
239 &cpu_dai_drv->playback);
240 } else {
241 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
242 &cpu_dai_drv->capture);
245 ret = -EINVAL;
246 snd_pcm_limit_hw_rates(runtime);
247 if (!runtime->hw.rates) {
248 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
249 codec_dai->name, cpu_dai->name);
250 goto config_err;
252 if (!runtime->hw.formats) {
253 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
254 codec_dai->name, cpu_dai->name);
255 goto config_err;
257 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
258 runtime->hw.channels_min > runtime->hw.channels_max) {
259 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
260 codec_dai->name, cpu_dai->name);
261 goto config_err;
264 soc_pcm_apply_msb(substream, codec_dai);
265 soc_pcm_apply_msb(substream, cpu_dai);
267 /* Symmetry only applies if we've already got an active stream. */
268 if (cpu_dai->active) {
269 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
270 if (ret != 0)
271 goto config_err;
274 if (codec_dai->active) {
275 ret = soc_pcm_apply_symmetry(substream, codec_dai);
276 if (ret != 0)
277 goto config_err;
280 pr_debug("ASoC: %s <-> %s info:\n",
281 codec_dai->name, cpu_dai->name);
282 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
283 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
284 runtime->hw.channels_max);
285 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
286 runtime->hw.rate_max);
288 dynamic:
289 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
290 cpu_dai->playback_active++;
291 codec_dai->playback_active++;
292 } else {
293 cpu_dai->capture_active++;
294 codec_dai->capture_active++;
296 cpu_dai->active++;
297 codec_dai->active++;
298 rtd->codec->active++;
299 mutex_unlock(&rtd->pcm_mutex);
300 return 0;
302 config_err:
303 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
304 rtd->dai_link->ops->shutdown(substream);
306 machine_err:
307 if (codec_dai->driver->ops->shutdown)
308 codec_dai->driver->ops->shutdown(substream, codec_dai);
310 codec_dai_err:
311 if (platform->driver->ops && platform->driver->ops->close)
312 platform->driver->ops->close(substream);
314 platform_err:
315 if (cpu_dai->driver->ops->shutdown)
316 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
317 out:
318 mutex_unlock(&rtd->pcm_mutex);
320 pm_runtime_put(platform->dev);
321 pm_runtime_put(codec_dai->dev);
322 pm_runtime_put(cpu_dai->dev);
323 if (!codec_dai->active)
324 pinctrl_pm_select_sleep_state(codec_dai->dev);
325 if (!cpu_dai->active)
326 pinctrl_pm_select_sleep_state(cpu_dai->dev);
328 return ret;
332 * Power down the audio subsystem pmdown_time msecs after close is called.
333 * This is to ensure there are no pops or clicks in between any music tracks
334 * due to DAPM power cycling.
336 static void close_delayed_work(struct work_struct *work)
338 struct snd_soc_pcm_runtime *rtd =
339 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
340 struct snd_soc_dai *codec_dai = rtd->codec_dai;
342 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
344 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
345 codec_dai->driver->playback.stream_name,
346 codec_dai->playback_active ? "active" : "inactive",
347 rtd->pop_wait ? "yes" : "no");
349 /* are we waiting on this codec DAI stream */
350 if (rtd->pop_wait == 1) {
351 rtd->pop_wait = 0;
352 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
353 SND_SOC_DAPM_STREAM_STOP);
356 mutex_unlock(&rtd->pcm_mutex);
360 * Called by ALSA when a PCM substream is closed. Private data can be
361 * freed here. The cpu DAI, codec DAI, machine and platform are also
362 * shutdown.
364 static int soc_pcm_close(struct snd_pcm_substream *substream)
366 struct snd_soc_pcm_runtime *rtd = substream->private_data;
367 struct snd_soc_platform *platform = rtd->platform;
368 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
369 struct snd_soc_dai *codec_dai = rtd->codec_dai;
370 struct snd_soc_codec *codec = rtd->codec;
372 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
374 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 cpu_dai->playback_active--;
376 codec_dai->playback_active--;
377 } else {
378 cpu_dai->capture_active--;
379 codec_dai->capture_active--;
382 cpu_dai->active--;
383 codec_dai->active--;
384 codec->active--;
386 /* clear the corresponding DAIs rate when inactive */
387 if (!cpu_dai->active)
388 cpu_dai->rate = 0;
390 if (!codec_dai->active)
391 codec_dai->rate = 0;
393 /* Muting the DAC suppresses artifacts caused during digital
394 * shutdown, for example from stopping clocks.
396 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
398 if (cpu_dai->driver->ops->shutdown)
399 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
401 if (codec_dai->driver->ops->shutdown)
402 codec_dai->driver->ops->shutdown(substream, codec_dai);
404 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
405 rtd->dai_link->ops->shutdown(substream);
407 if (platform->driver->ops && platform->driver->ops->close)
408 platform->driver->ops->close(substream);
409 cpu_dai->runtime = NULL;
411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
412 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
413 rtd->dai_link->ignore_pmdown_time) {
414 /* powered down playback stream now */
415 snd_soc_dapm_stream_event(rtd,
416 SNDRV_PCM_STREAM_PLAYBACK,
417 SND_SOC_DAPM_STREAM_STOP);
418 } else {
419 /* start delayed pop wq here for playback streams */
420 rtd->pop_wait = 1;
421 queue_delayed_work(system_power_efficient_wq,
422 &rtd->delayed_work,
423 msecs_to_jiffies(rtd->pmdown_time));
425 } else {
426 /* capture streams can be powered down now */
427 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
428 SND_SOC_DAPM_STREAM_STOP);
431 mutex_unlock(&rtd->pcm_mutex);
433 pm_runtime_put(platform->dev);
434 pm_runtime_put(codec_dai->dev);
435 pm_runtime_put(cpu_dai->dev);
436 if (!codec_dai->active)
437 pinctrl_pm_select_sleep_state(codec_dai->dev);
438 if (!cpu_dai->active)
439 pinctrl_pm_select_sleep_state(cpu_dai->dev);
441 return 0;
445 * Called by ALSA when the PCM substream is prepared, can set format, sample
446 * rate, etc. This function is non atomic and can be called multiple times,
447 * it can refer to the runtime info.
449 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
451 struct snd_soc_pcm_runtime *rtd = substream->private_data;
452 struct snd_soc_platform *platform = rtd->platform;
453 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454 struct snd_soc_dai *codec_dai = rtd->codec_dai;
455 int ret = 0;
457 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
459 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
460 ret = rtd->dai_link->ops->prepare(substream);
461 if (ret < 0) {
462 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
463 " %d\n", ret);
464 goto out;
468 if (platform->driver->ops && platform->driver->ops->prepare) {
469 ret = platform->driver->ops->prepare(substream);
470 if (ret < 0) {
471 dev_err(platform->dev, "ASoC: platform prepare error:"
472 " %d\n", ret);
473 goto out;
477 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
478 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
479 if (ret < 0) {
480 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
481 ret);
482 goto out;
486 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
487 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
488 if (ret < 0) {
489 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
490 ret);
491 goto out;
495 /* cancel any delayed stream shutdown that is pending */
496 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
497 rtd->pop_wait) {
498 rtd->pop_wait = 0;
499 cancel_delayed_work(&rtd->delayed_work);
502 snd_soc_dapm_stream_event(rtd, substream->stream,
503 SND_SOC_DAPM_STREAM_START);
505 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
507 out:
508 mutex_unlock(&rtd->pcm_mutex);
509 return ret;
513 * Called by ALSA when the hardware params are set by application. This
514 * function can also be called multiple times and can allocate buffers
515 * (using snd_pcm_lib_* ). It's non-atomic.
517 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
518 struct snd_pcm_hw_params *params)
520 struct snd_soc_pcm_runtime *rtd = substream->private_data;
521 struct snd_soc_platform *platform = rtd->platform;
522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
523 struct snd_soc_dai *codec_dai = rtd->codec_dai;
524 int ret = 0;
526 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
528 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
529 ret = rtd->dai_link->ops->hw_params(substream, params);
530 if (ret < 0) {
531 dev_err(rtd->card->dev, "ASoC: machine hw_params"
532 " failed: %d\n", ret);
533 goto out;
537 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
538 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
539 if (ret < 0) {
540 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
541 " %d\n", codec_dai->name, ret);
542 goto codec_err;
546 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
547 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
548 if (ret < 0) {
549 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
550 cpu_dai->name, ret);
551 goto interface_err;
555 if (platform->driver->ops && platform->driver->ops->hw_params) {
556 ret = platform->driver->ops->hw_params(substream, params);
557 if (ret < 0) {
558 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
559 platform->name, ret);
560 goto platform_err;
564 /* store the rate for each DAIs */
565 cpu_dai->rate = params_rate(params);
566 codec_dai->rate = params_rate(params);
568 out:
569 mutex_unlock(&rtd->pcm_mutex);
570 return ret;
572 platform_err:
573 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
574 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
576 interface_err:
577 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
578 codec_dai->driver->ops->hw_free(substream, codec_dai);
580 codec_err:
581 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
582 rtd->dai_link->ops->hw_free(substream);
584 mutex_unlock(&rtd->pcm_mutex);
585 return ret;
589 * Frees resources allocated by hw_params, can be called multiple times
591 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
593 struct snd_soc_pcm_runtime *rtd = substream->private_data;
594 struct snd_soc_platform *platform = rtd->platform;
595 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
596 struct snd_soc_dai *codec_dai = rtd->codec_dai;
597 struct snd_soc_codec *codec = rtd->codec;
599 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
601 /* apply codec digital mute */
602 if (!codec->active)
603 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
605 /* free any machine hw params */
606 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
607 rtd->dai_link->ops->hw_free(substream);
609 /* free any DMA resources */
610 if (platform->driver->ops && platform->driver->ops->hw_free)
611 platform->driver->ops->hw_free(substream);
613 /* now free hw params for the DAIs */
614 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
615 codec_dai->driver->ops->hw_free(substream, codec_dai);
617 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
618 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
620 mutex_unlock(&rtd->pcm_mutex);
621 return 0;
624 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
626 struct snd_soc_pcm_runtime *rtd = substream->private_data;
627 struct snd_soc_platform *platform = rtd->platform;
628 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
629 struct snd_soc_dai *codec_dai = rtd->codec_dai;
630 int ret;
632 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
633 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
634 if (ret < 0)
635 return ret;
638 if (platform->driver->ops && platform->driver->ops->trigger) {
639 ret = platform->driver->ops->trigger(substream, cmd);
640 if (ret < 0)
641 return ret;
644 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
645 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
646 if (ret < 0)
647 return ret;
649 return 0;
652 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
653 int cmd)
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 = rtd->codec_dai;
659 int ret;
661 if (codec_dai->driver->ops &&
662 codec_dai->driver->ops->bespoke_trigger) {
663 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
664 if (ret < 0)
665 return ret;
668 if (platform->driver->ops && platform->driver->bespoke_trigger) {
669 ret = platform->driver->bespoke_trigger(substream, cmd);
670 if (ret < 0)
671 return ret;
674 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
675 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
676 if (ret < 0)
677 return ret;
679 return 0;
682 * soc level wrapper for pointer callback
683 * If cpu_dai, codec_dai, platform driver has the delay callback, than
684 * the runtime->delay will be updated accordingly.
686 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
688 struct snd_soc_pcm_runtime *rtd = substream->private_data;
689 struct snd_soc_platform *platform = rtd->platform;
690 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
691 struct snd_soc_dai *codec_dai = rtd->codec_dai;
692 struct snd_pcm_runtime *runtime = substream->runtime;
693 snd_pcm_uframes_t offset = 0;
694 snd_pcm_sframes_t delay = 0;
696 if (platform->driver->ops && platform->driver->ops->pointer)
697 offset = platform->driver->ops->pointer(substream);
699 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
700 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
702 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
703 delay += codec_dai->driver->ops->delay(substream, codec_dai);
705 if (platform->driver->delay)
706 delay += platform->driver->delay(substream, codec_dai);
708 runtime->delay = delay;
710 return offset;
713 /* connect a FE and BE */
714 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
715 struct snd_soc_pcm_runtime *be, int stream)
717 struct snd_soc_dpcm *dpcm;
719 /* only add new dpcms */
720 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
721 if (dpcm->be == be && dpcm->fe == fe)
722 return 0;
725 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
726 if (!dpcm)
727 return -ENOMEM;
729 dpcm->be = be;
730 dpcm->fe = fe;
731 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
732 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
733 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
734 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
736 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
737 stream ? "capture" : "playback", fe->dai_link->name,
738 stream ? "<-" : "->", be->dai_link->name);
740 #ifdef CONFIG_DEBUG_FS
741 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
742 fe->debugfs_dpcm_root, &dpcm->state);
743 #endif
744 return 1;
747 /* reparent a BE onto another FE */
748 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
749 struct snd_soc_pcm_runtime *be, int stream)
751 struct snd_soc_dpcm *dpcm;
752 struct snd_pcm_substream *fe_substream, *be_substream;
754 /* reparent if BE is connected to other FEs */
755 if (!be->dpcm[stream].users)
756 return;
758 be_substream = snd_soc_dpcm_get_substream(be, stream);
760 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
761 if (dpcm->fe == fe)
762 continue;
764 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
765 stream ? "capture" : "playback",
766 dpcm->fe->dai_link->name,
767 stream ? "<-" : "->", dpcm->be->dai_link->name);
769 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
770 be_substream->runtime = fe_substream->runtime;
771 break;
775 /* disconnect a BE and FE */
776 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
778 struct snd_soc_dpcm *dpcm, *d;
780 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
781 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
782 stream ? "capture" : "playback",
783 dpcm->be->dai_link->name);
785 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
786 continue;
788 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
789 stream ? "capture" : "playback", fe->dai_link->name,
790 stream ? "<-" : "->", dpcm->be->dai_link->name);
792 /* BEs still alive need new FE */
793 dpcm_be_reparent(fe, dpcm->be, stream);
795 #ifdef CONFIG_DEBUG_FS
796 debugfs_remove(dpcm->debugfs_state);
797 #endif
798 list_del(&dpcm->list_be);
799 list_del(&dpcm->list_fe);
800 kfree(dpcm);
804 /* get BE for DAI widget and stream */
805 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
806 struct snd_soc_dapm_widget *widget, int stream)
808 struct snd_soc_pcm_runtime *be;
809 int i;
811 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
812 for (i = 0; i < card->num_links; i++) {
813 be = &card->rtd[i];
815 if (!be->dai_link->no_pcm)
816 continue;
818 if (be->cpu_dai->playback_widget == widget ||
819 be->codec_dai->playback_widget == widget)
820 return be;
822 } else {
824 for (i = 0; i < card->num_links; i++) {
825 be = &card->rtd[i];
827 if (!be->dai_link->no_pcm)
828 continue;
830 if (be->cpu_dai->capture_widget == widget ||
831 be->codec_dai->capture_widget == widget)
832 return be;
836 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
837 stream ? "capture" : "playback", widget->name);
838 return NULL;
841 static inline struct snd_soc_dapm_widget *
842 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
844 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
845 return rtd->cpu_dai->playback_widget;
846 else
847 return rtd->cpu_dai->capture_widget;
850 static inline struct snd_soc_dapm_widget *
851 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
853 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
854 return rtd->codec_dai->playback_widget;
855 else
856 return rtd->codec_dai->capture_widget;
859 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
860 struct snd_soc_dapm_widget *widget)
862 int i;
864 for (i = 0; i < list->num_widgets; i++) {
865 if (widget == list->widgets[i])
866 return 1;
869 return 0;
872 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
873 int stream, struct snd_soc_dapm_widget_list **list_)
875 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
876 struct snd_soc_dapm_widget_list *list;
877 int paths;
879 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
880 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
881 if (list == NULL)
882 return -ENOMEM;
884 /* get number of valid DAI paths and their widgets */
885 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
887 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
888 stream ? "capture" : "playback");
890 *list_ = list;
891 return paths;
894 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
896 kfree(*list);
899 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
900 struct snd_soc_dapm_widget_list **list_)
902 struct snd_soc_dpcm *dpcm;
903 struct snd_soc_dapm_widget_list *list = *list_;
904 struct snd_soc_dapm_widget *widget;
905 int prune = 0;
907 /* Destroy any old FE <--> BE connections */
908 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
910 /* is there a valid CPU DAI widget for this BE */
911 widget = rtd_get_cpu_widget(dpcm->be, stream);
913 /* prune the BE if it's no longer in our active list */
914 if (widget && widget_in_list(list, widget))
915 continue;
917 /* is there a valid CODEC DAI widget for this BE */
918 widget = rtd_get_codec_widget(dpcm->be, stream);
920 /* prune the BE if it's no longer in our active list */
921 if (widget && widget_in_list(list, widget))
922 continue;
924 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
925 stream ? "capture" : "playback",
926 dpcm->be->dai_link->name, fe->dai_link->name);
927 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
928 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
929 prune++;
932 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
933 return prune;
936 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
937 struct snd_soc_dapm_widget_list **list_)
939 struct snd_soc_card *card = fe->card;
940 struct snd_soc_dapm_widget_list *list = *list_;
941 struct snd_soc_pcm_runtime *be;
942 int i, new = 0, err;
944 /* Create any new FE <--> BE connections */
945 for (i = 0; i < list->num_widgets; i++) {
947 switch (list->widgets[i]->id) {
948 case snd_soc_dapm_dai_in:
949 case snd_soc_dapm_dai_out:
950 break;
951 default:
952 continue;
955 /* is there a valid BE rtd for this widget */
956 be = dpcm_get_be(card, list->widgets[i], stream);
957 if (!be) {
958 dev_err(fe->dev, "ASoC: no BE found for %s\n",
959 list->widgets[i]->name);
960 continue;
963 /* make sure BE is a real BE */
964 if (!be->dai_link->no_pcm)
965 continue;
967 /* don't connect if FE is not running */
968 if (!fe->dpcm[stream].runtime)
969 continue;
971 /* newly connected FE and BE */
972 err = dpcm_be_connect(fe, be, stream);
973 if (err < 0) {
974 dev_err(fe->dev, "ASoC: can't connect %s\n",
975 list->widgets[i]->name);
976 break;
977 } else if (err == 0) /* already connected */
978 continue;
980 /* new */
981 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
982 new++;
985 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
986 return new;
990 * Find the corresponding BE DAIs that source or sink audio to this
991 * FE substream.
993 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
994 int stream, struct snd_soc_dapm_widget_list **list, int new)
996 if (new)
997 return dpcm_add_paths(fe, stream, list);
998 else
999 return dpcm_prune_paths(fe, stream, list);
1002 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1004 struct snd_soc_dpcm *dpcm;
1006 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1007 dpcm->be->dpcm[stream].runtime_update =
1008 SND_SOC_DPCM_UPDATE_NO;
1011 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1012 int stream)
1014 struct snd_soc_dpcm *dpcm;
1016 /* disable any enabled and non active backends */
1017 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1019 struct snd_soc_pcm_runtime *be = dpcm->be;
1020 struct snd_pcm_substream *be_substream =
1021 snd_soc_dpcm_get_substream(be, stream);
1023 if (be->dpcm[stream].users == 0)
1024 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1025 stream ? "capture" : "playback",
1026 be->dpcm[stream].state);
1028 if (--be->dpcm[stream].users != 0)
1029 continue;
1031 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1032 continue;
1034 soc_pcm_close(be_substream);
1035 be_substream->runtime = NULL;
1036 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1040 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1042 struct snd_soc_dpcm *dpcm;
1043 int err, count = 0;
1045 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1046 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1048 struct snd_soc_pcm_runtime *be = dpcm->be;
1049 struct snd_pcm_substream *be_substream =
1050 snd_soc_dpcm_get_substream(be, stream);
1052 if (!be_substream) {
1053 dev_err(be->dev, "ASoC: no backend %s stream\n",
1054 stream ? "capture" : "playback");
1055 continue;
1058 /* is this op for this BE ? */
1059 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1060 continue;
1062 /* first time the dpcm is open ? */
1063 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1064 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1065 stream ? "capture" : "playback",
1066 be->dpcm[stream].state);
1068 if (be->dpcm[stream].users++ != 0)
1069 continue;
1071 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1072 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1073 continue;
1075 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1076 stream ? "capture" : "playback", be->dai_link->name);
1078 be_substream->runtime = be->dpcm[stream].runtime;
1079 err = soc_pcm_open(be_substream);
1080 if (err < 0) {
1081 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1082 be->dpcm[stream].users--;
1083 if (be->dpcm[stream].users < 0)
1084 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1085 stream ? "capture" : "playback",
1086 be->dpcm[stream].state);
1088 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1089 goto unwind;
1092 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1093 count++;
1096 return count;
1098 unwind:
1099 /* disable any enabled and non active backends */
1100 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1101 struct snd_soc_pcm_runtime *be = dpcm->be;
1102 struct snd_pcm_substream *be_substream =
1103 snd_soc_dpcm_get_substream(be, stream);
1105 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1106 continue;
1108 if (be->dpcm[stream].users == 0)
1109 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1110 stream ? "capture" : "playback",
1111 be->dpcm[stream].state);
1113 if (--be->dpcm[stream].users != 0)
1114 continue;
1116 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1117 continue;
1119 soc_pcm_close(be_substream);
1120 be_substream->runtime = NULL;
1121 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1124 return err;
1127 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1132 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1134 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1135 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1136 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1137 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1138 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1139 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1140 runtime->hw.rates = cpu_dai_drv->playback.rates;
1141 } else {
1142 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1143 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1144 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1145 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1146 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1147 runtime->hw.rates = cpu_dai_drv->capture.rates;
1151 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1153 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1154 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1155 int stream = fe_substream->stream, ret = 0;
1157 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1159 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1160 if (ret < 0) {
1161 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1162 goto be_err;
1165 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1167 /* start the DAI frontend */
1168 ret = soc_pcm_open(fe_substream);
1169 if (ret < 0) {
1170 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1171 goto unwind;
1174 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1176 dpcm_set_fe_runtime(fe_substream);
1177 snd_pcm_limit_hw_rates(runtime);
1179 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1180 return 0;
1182 unwind:
1183 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1184 be_err:
1185 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1186 return ret;
1189 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1191 struct snd_soc_dpcm *dpcm;
1193 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1194 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1196 struct snd_soc_pcm_runtime *be = dpcm->be;
1197 struct snd_pcm_substream *be_substream =
1198 snd_soc_dpcm_get_substream(be, stream);
1200 /* is this op for this BE ? */
1201 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1202 continue;
1204 if (be->dpcm[stream].users == 0)
1205 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1206 stream ? "capture" : "playback",
1207 be->dpcm[stream].state);
1209 if (--be->dpcm[stream].users != 0)
1210 continue;
1212 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1213 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1214 continue;
1216 dev_dbg(be->dev, "ASoC: close BE %s\n",
1217 dpcm->fe->dai_link->name);
1219 soc_pcm_close(be_substream);
1220 be_substream->runtime = NULL;
1222 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1224 return 0;
1227 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1229 struct snd_soc_pcm_runtime *fe = substream->private_data;
1230 int stream = substream->stream;
1232 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1234 /* shutdown the BEs */
1235 dpcm_be_dai_shutdown(fe, substream->stream);
1237 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1239 /* now shutdown the frontend */
1240 soc_pcm_close(substream);
1242 /* run the stream event for each BE */
1243 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1245 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1246 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1247 return 0;
1250 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1252 struct snd_soc_dpcm *dpcm;
1254 /* only hw_params backends that are either sinks or sources
1255 * to this frontend DAI */
1256 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1258 struct snd_soc_pcm_runtime *be = dpcm->be;
1259 struct snd_pcm_substream *be_substream =
1260 snd_soc_dpcm_get_substream(be, stream);
1262 /* is this op for this BE ? */
1263 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1264 continue;
1266 /* only free hw when no longer used - check all FEs */
1267 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1268 continue;
1270 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1271 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1272 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1273 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1274 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1275 continue;
1277 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1278 dpcm->fe->dai_link->name);
1280 soc_pcm_hw_free(be_substream);
1282 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1285 return 0;
1288 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1290 struct snd_soc_pcm_runtime *fe = substream->private_data;
1291 int err, stream = substream->stream;
1293 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1294 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1296 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1298 /* call hw_free on the frontend */
1299 err = soc_pcm_hw_free(substream);
1300 if (err < 0)
1301 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1302 fe->dai_link->name);
1304 /* only hw_params backends that are either sinks or sources
1305 * to this frontend DAI */
1306 err = dpcm_be_dai_hw_free(fe, stream);
1308 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1309 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1311 mutex_unlock(&fe->card->mutex);
1312 return 0;
1315 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1317 struct snd_soc_dpcm *dpcm;
1318 int ret;
1320 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1322 struct snd_soc_pcm_runtime *be = dpcm->be;
1323 struct snd_pcm_substream *be_substream =
1324 snd_soc_dpcm_get_substream(be, stream);
1326 /* is this op for this BE ? */
1327 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1328 continue;
1330 /* only allow hw_params() if no connected FEs are running */
1331 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1332 continue;
1334 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1335 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1336 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1337 continue;
1339 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1340 dpcm->fe->dai_link->name);
1342 /* copy params for each dpcm */
1343 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1344 sizeof(struct snd_pcm_hw_params));
1346 /* perform any hw_params fixups */
1347 if (be->dai_link->be_hw_params_fixup) {
1348 ret = be->dai_link->be_hw_params_fixup(be,
1349 &dpcm->hw_params);
1350 if (ret < 0) {
1351 dev_err(be->dev,
1352 "ASoC: hw_params BE fixup failed %d\n",
1353 ret);
1354 goto unwind;
1358 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1359 if (ret < 0) {
1360 dev_err(dpcm->be->dev,
1361 "ASoC: hw_params BE failed %d\n", ret);
1362 goto unwind;
1365 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1367 return 0;
1369 unwind:
1370 /* disable any enabled and non active backends */
1371 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1372 struct snd_soc_pcm_runtime *be = dpcm->be;
1373 struct snd_pcm_substream *be_substream =
1374 snd_soc_dpcm_get_substream(be, stream);
1376 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1377 continue;
1379 /* only allow hw_free() if no connected FEs are running */
1380 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1381 continue;
1383 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1384 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1385 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1386 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1387 continue;
1389 soc_pcm_hw_free(be_substream);
1392 return ret;
1395 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1396 struct snd_pcm_hw_params *params)
1398 struct snd_soc_pcm_runtime *fe = substream->private_data;
1399 int ret, stream = substream->stream;
1401 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1402 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1404 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1405 sizeof(struct snd_pcm_hw_params));
1406 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1407 if (ret < 0) {
1408 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1409 goto out;
1412 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1413 fe->dai_link->name, params_rate(params),
1414 params_channels(params), params_format(params));
1416 /* call hw_params on the frontend */
1417 ret = soc_pcm_hw_params(substream, params);
1418 if (ret < 0) {
1419 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1420 dpcm_be_dai_hw_free(fe, stream);
1421 } else
1422 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1424 out:
1425 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1426 mutex_unlock(&fe->card->mutex);
1427 return ret;
1430 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1431 struct snd_pcm_substream *substream, int cmd)
1433 int ret;
1435 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1436 dpcm->fe->dai_link->name, cmd);
1438 ret = soc_pcm_trigger(substream, cmd);
1439 if (ret < 0)
1440 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1442 return ret;
1445 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1446 int cmd)
1448 struct snd_soc_dpcm *dpcm;
1449 int ret = 0;
1451 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1453 struct snd_soc_pcm_runtime *be = dpcm->be;
1454 struct snd_pcm_substream *be_substream =
1455 snd_soc_dpcm_get_substream(be, stream);
1457 /* is this op for this BE ? */
1458 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1459 continue;
1461 switch (cmd) {
1462 case SNDRV_PCM_TRIGGER_START:
1463 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1464 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1465 continue;
1467 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1468 if (ret)
1469 return ret;
1471 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1472 break;
1473 case SNDRV_PCM_TRIGGER_RESUME:
1474 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1475 continue;
1477 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1478 if (ret)
1479 return ret;
1481 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1482 break;
1483 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1484 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1485 continue;
1487 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1488 if (ret)
1489 return ret;
1491 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1492 break;
1493 case SNDRV_PCM_TRIGGER_STOP:
1494 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1495 continue;
1497 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1498 continue;
1500 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1501 if (ret)
1502 return ret;
1504 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1505 break;
1506 case SNDRV_PCM_TRIGGER_SUSPEND:
1507 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1508 continue;
1510 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1511 continue;
1513 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1514 if (ret)
1515 return ret;
1517 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1518 break;
1519 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1520 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1521 continue;
1523 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1524 continue;
1526 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1527 if (ret)
1528 return ret;
1530 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1531 break;
1535 return ret;
1537 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1539 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1541 struct snd_soc_pcm_runtime *fe = substream->private_data;
1542 int stream = substream->stream, ret;
1543 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1545 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1547 switch (trigger) {
1548 case SND_SOC_DPCM_TRIGGER_PRE:
1549 /* call trigger on the frontend before the backend. */
1551 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1552 fe->dai_link->name, cmd);
1554 ret = soc_pcm_trigger(substream, cmd);
1555 if (ret < 0) {
1556 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1557 goto out;
1560 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1561 break;
1562 case SND_SOC_DPCM_TRIGGER_POST:
1563 /* call trigger on the frontend after the backend. */
1565 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1566 if (ret < 0) {
1567 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1568 goto out;
1571 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1572 fe->dai_link->name, cmd);
1574 ret = soc_pcm_trigger(substream, cmd);
1575 break;
1576 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1577 /* bespoke trigger() - handles both FE and BEs */
1579 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1580 fe->dai_link->name, cmd);
1582 ret = soc_pcm_bespoke_trigger(substream, cmd);
1583 if (ret < 0) {
1584 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1585 goto out;
1587 break;
1588 default:
1589 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1590 fe->dai_link->name);
1591 ret = -EINVAL;
1592 goto out;
1595 switch (cmd) {
1596 case SNDRV_PCM_TRIGGER_START:
1597 case SNDRV_PCM_TRIGGER_RESUME:
1598 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1599 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1600 break;
1601 case SNDRV_PCM_TRIGGER_STOP:
1602 case SNDRV_PCM_TRIGGER_SUSPEND:
1603 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1604 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1605 break;
1608 out:
1609 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1610 return ret;
1613 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1615 struct snd_soc_dpcm *dpcm;
1616 int ret = 0;
1618 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1620 struct snd_soc_pcm_runtime *be = dpcm->be;
1621 struct snd_pcm_substream *be_substream =
1622 snd_soc_dpcm_get_substream(be, stream);
1624 /* is this op for this BE ? */
1625 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1626 continue;
1628 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1629 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1630 continue;
1632 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1633 dpcm->fe->dai_link->name);
1635 ret = soc_pcm_prepare(be_substream);
1636 if (ret < 0) {
1637 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1638 ret);
1639 break;
1642 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1644 return ret;
1647 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1649 struct snd_soc_pcm_runtime *fe = substream->private_data;
1650 int stream = substream->stream, ret = 0;
1652 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1654 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1656 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1658 /* there is no point preparing this FE if there are no BEs */
1659 if (list_empty(&fe->dpcm[stream].be_clients)) {
1660 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1661 fe->dai_link->name);
1662 ret = -EINVAL;
1663 goto out;
1666 ret = dpcm_be_dai_prepare(fe, substream->stream);
1667 if (ret < 0)
1668 goto out;
1670 /* call prepare on the frontend */
1671 ret = soc_pcm_prepare(substream);
1672 if (ret < 0) {
1673 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1674 fe->dai_link->name);
1675 goto out;
1678 /* run the stream event for each BE */
1679 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1680 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1682 out:
1683 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1684 mutex_unlock(&fe->card->mutex);
1686 return ret;
1689 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1690 unsigned int cmd, void *arg)
1692 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1693 struct snd_soc_platform *platform = rtd->platform;
1695 if (platform->driver->ops && platform->driver->ops->ioctl)
1696 return platform->driver->ops->ioctl(substream, cmd, arg);
1697 return snd_pcm_lib_ioctl(substream, cmd, arg);
1700 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1702 struct snd_pcm_substream *substream =
1703 snd_soc_dpcm_get_substream(fe, stream);
1704 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1705 int err;
1707 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1708 stream ? "capture" : "playback", fe->dai_link->name);
1710 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1711 /* call bespoke trigger - FE takes care of all BE triggers */
1712 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1713 fe->dai_link->name);
1715 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1716 if (err < 0)
1717 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1718 } else {
1719 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1720 fe->dai_link->name);
1722 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1723 if (err < 0)
1724 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1727 err = dpcm_be_dai_hw_free(fe, stream);
1728 if (err < 0)
1729 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1731 err = dpcm_be_dai_shutdown(fe, stream);
1732 if (err < 0)
1733 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1735 /* run the stream event for each BE */
1736 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1738 return 0;
1741 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1743 struct snd_pcm_substream *substream =
1744 snd_soc_dpcm_get_substream(fe, stream);
1745 struct snd_soc_dpcm *dpcm;
1746 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1747 int ret;
1749 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1750 stream ? "capture" : "playback", fe->dai_link->name);
1752 /* Only start the BE if the FE is ready */
1753 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1754 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1755 return -EINVAL;
1757 /* startup must always be called for new BEs */
1758 ret = dpcm_be_dai_startup(fe, stream);
1759 if (ret < 0)
1760 goto disconnect;
1762 /* keep going if FE state is > open */
1763 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1764 return 0;
1766 ret = dpcm_be_dai_hw_params(fe, stream);
1767 if (ret < 0)
1768 goto close;
1770 /* keep going if FE state is > hw_params */
1771 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1772 return 0;
1775 ret = dpcm_be_dai_prepare(fe, stream);
1776 if (ret < 0)
1777 goto hw_free;
1779 /* run the stream event for each BE */
1780 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1782 /* keep going if FE state is > prepare */
1783 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1784 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1785 return 0;
1787 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1788 /* call trigger on the frontend - FE takes care of all BE triggers */
1789 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1790 fe->dai_link->name);
1792 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1793 if (ret < 0) {
1794 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1795 goto hw_free;
1797 } else {
1798 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1799 fe->dai_link->name);
1801 ret = dpcm_be_dai_trigger(fe, stream,
1802 SNDRV_PCM_TRIGGER_START);
1803 if (ret < 0) {
1804 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1805 goto hw_free;
1809 return 0;
1811 hw_free:
1812 dpcm_be_dai_hw_free(fe, stream);
1813 close:
1814 dpcm_be_dai_shutdown(fe, stream);
1815 disconnect:
1816 /* disconnect any non started BEs */
1817 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1818 struct snd_soc_pcm_runtime *be = dpcm->be;
1819 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1820 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1823 return ret;
1826 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1828 int ret;
1830 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1831 ret = dpcm_run_update_startup(fe, stream);
1832 if (ret < 0)
1833 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
1834 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1836 return ret;
1839 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1841 int ret;
1843 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1844 ret = dpcm_run_update_shutdown(fe, stream);
1845 if (ret < 0)
1846 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
1847 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1849 return ret;
1852 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1853 * any DAI links.
1855 int soc_dpcm_runtime_update(struct snd_soc_card *card)
1857 int i, old, new, paths;
1859 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1860 for (i = 0; i < card->num_rtd; i++) {
1861 struct snd_soc_dapm_widget_list *list;
1862 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1864 /* make sure link is FE */
1865 if (!fe->dai_link->dynamic)
1866 continue;
1868 /* only check active links */
1869 if (!fe->cpu_dai->active)
1870 continue;
1872 /* DAPM sync will call this to update DSP paths */
1873 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
1874 fe->dai_link->name);
1876 /* skip if FE doesn't have playback capability */
1877 if (!fe->cpu_dai->driver->playback.channels_min)
1878 goto capture;
1880 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1881 if (paths < 0) {
1882 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1883 fe->dai_link->name, "playback");
1884 mutex_unlock(&card->mutex);
1885 return paths;
1888 /* update any new playback paths */
1889 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1890 if (new) {
1891 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1892 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1893 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1896 /* update any old playback paths */
1897 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1898 if (old) {
1899 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1900 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1901 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1904 capture:
1905 /* skip if FE doesn't have capture capability */
1906 if (!fe->cpu_dai->driver->capture.channels_min)
1907 continue;
1909 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1910 if (paths < 0) {
1911 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
1912 fe->dai_link->name, "capture");
1913 mutex_unlock(&card->mutex);
1914 return paths;
1917 /* update any new capture paths */
1918 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1919 if (new) {
1920 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1921 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1922 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1925 /* update any old capture paths */
1926 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1927 if (old) {
1928 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1929 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1930 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1933 dpcm_path_put(&list);
1936 mutex_unlock(&card->mutex);
1937 return 0;
1939 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1941 struct snd_soc_dpcm *dpcm;
1942 struct list_head *clients =
1943 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1945 list_for_each_entry(dpcm, clients, list_be) {
1947 struct snd_soc_pcm_runtime *be = dpcm->be;
1948 struct snd_soc_dai *dai = be->codec_dai;
1949 struct snd_soc_dai_driver *drv = dai->driver;
1951 if (be->dai_link->ignore_suspend)
1952 continue;
1954 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
1956 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1957 drv->ops->digital_mute(dai, mute);
1960 return 0;
1963 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1965 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1966 struct snd_soc_dpcm *dpcm;
1967 struct snd_soc_dapm_widget_list *list;
1968 int ret;
1969 int stream = fe_substream->stream;
1971 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1972 fe->dpcm[stream].runtime = fe_substream->runtime;
1974 if (dpcm_path_get(fe, stream, &list) <= 0) {
1975 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
1976 fe->dai_link->name, stream ? "capture" : "playback");
1979 /* calculate valid and active FE <-> BE dpcms */
1980 dpcm_process_paths(fe, stream, &list, 1);
1982 ret = dpcm_fe_dai_startup(fe_substream);
1983 if (ret < 0) {
1984 /* clean up all links */
1985 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1986 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1988 dpcm_be_disconnect(fe, stream);
1989 fe->dpcm[stream].runtime = NULL;
1992 dpcm_clear_pending_state(fe, stream);
1993 dpcm_path_put(&list);
1994 mutex_unlock(&fe->card->mutex);
1995 return ret;
1998 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2000 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2001 struct snd_soc_dpcm *dpcm;
2002 int stream = fe_substream->stream, ret;
2004 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2005 ret = dpcm_fe_dai_shutdown(fe_substream);
2007 /* mark FE's links ready to prune */
2008 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2009 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2011 dpcm_be_disconnect(fe, stream);
2013 fe->dpcm[stream].runtime = NULL;
2014 mutex_unlock(&fe->card->mutex);
2015 return ret;
2018 /* create a new pcm */
2019 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2021 struct snd_soc_platform *platform = rtd->platform;
2022 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2023 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2024 struct snd_pcm *pcm;
2025 char new_name[64];
2026 int ret = 0, playback = 0, capture = 0;
2028 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2029 if (cpu_dai->driver->playback.channels_min)
2030 playback = 1;
2031 if (cpu_dai->driver->capture.channels_min)
2032 capture = 1;
2033 } else {
2034 if (codec_dai->driver->playback.channels_min &&
2035 cpu_dai->driver->playback.channels_min)
2036 playback = 1;
2037 if (codec_dai->driver->capture.channels_min &&
2038 cpu_dai->driver->capture.channels_min)
2039 capture = 1;
2042 if (rtd->dai_link->playback_only) {
2043 playback = 1;
2044 capture = 0;
2047 if (rtd->dai_link->capture_only) {
2048 playback = 0;
2049 capture = 1;
2052 /* create the PCM */
2053 if (rtd->dai_link->no_pcm) {
2054 snprintf(new_name, sizeof(new_name), "(%s)",
2055 rtd->dai_link->stream_name);
2057 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2058 playback, capture, &pcm);
2059 } else {
2060 if (rtd->dai_link->dynamic)
2061 snprintf(new_name, sizeof(new_name), "%s (*)",
2062 rtd->dai_link->stream_name);
2063 else
2064 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2065 rtd->dai_link->stream_name, codec_dai->name, num);
2067 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2068 capture, &pcm);
2070 if (ret < 0) {
2071 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2072 rtd->dai_link->name);
2073 return ret;
2075 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2077 /* DAPM dai link stream work */
2078 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2080 rtd->pcm = pcm;
2081 pcm->private_data = rtd;
2083 if (rtd->dai_link->no_pcm) {
2084 if (playback)
2085 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2086 if (capture)
2087 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2088 goto out;
2091 /* ASoC PCM operations */
2092 if (rtd->dai_link->dynamic) {
2093 rtd->ops.open = dpcm_fe_dai_open;
2094 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2095 rtd->ops.prepare = dpcm_fe_dai_prepare;
2096 rtd->ops.trigger = dpcm_fe_dai_trigger;
2097 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2098 rtd->ops.close = dpcm_fe_dai_close;
2099 rtd->ops.pointer = soc_pcm_pointer;
2100 rtd->ops.ioctl = soc_pcm_ioctl;
2101 } else {
2102 rtd->ops.open = soc_pcm_open;
2103 rtd->ops.hw_params = soc_pcm_hw_params;
2104 rtd->ops.prepare = soc_pcm_prepare;
2105 rtd->ops.trigger = soc_pcm_trigger;
2106 rtd->ops.hw_free = soc_pcm_hw_free;
2107 rtd->ops.close = soc_pcm_close;
2108 rtd->ops.pointer = soc_pcm_pointer;
2109 rtd->ops.ioctl = soc_pcm_ioctl;
2112 if (platform->driver->ops) {
2113 rtd->ops.ack = platform->driver->ops->ack;
2114 rtd->ops.copy = platform->driver->ops->copy;
2115 rtd->ops.silence = platform->driver->ops->silence;
2116 rtd->ops.page = platform->driver->ops->page;
2117 rtd->ops.mmap = platform->driver->ops->mmap;
2120 if (playback)
2121 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2123 if (capture)
2124 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2126 if (platform->driver->pcm_new) {
2127 ret = platform->driver->pcm_new(rtd);
2128 if (ret < 0) {
2129 dev_err(platform->dev,
2130 "ASoC: pcm constructor failed: %d\n",
2131 ret);
2132 return ret;
2136 pcm->private_free = platform->driver->pcm_free;
2137 out:
2138 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
2139 cpu_dai->name);
2140 return ret;
2143 /* is the current PCM operation for this FE ? */
2144 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2146 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2147 return 1;
2148 return 0;
2150 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2152 /* is the current PCM operation for this BE ? */
2153 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2154 struct snd_soc_pcm_runtime *be, int stream)
2156 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2157 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2158 be->dpcm[stream].runtime_update))
2159 return 1;
2160 return 0;
2162 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2164 /* get the substream for this BE */
2165 struct snd_pcm_substream *
2166 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2168 return be->pcm->streams[stream].substream;
2170 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2172 /* get the BE runtime state */
2173 enum snd_soc_dpcm_state
2174 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2176 return be->dpcm[stream].state;
2178 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2180 /* set the BE runtime state */
2181 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2182 int stream, enum snd_soc_dpcm_state state)
2184 be->dpcm[stream].state = state;
2186 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2189 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2190 * are not running, paused or suspended for the specified stream direction.
2192 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2193 struct snd_soc_pcm_runtime *be, int stream)
2195 struct snd_soc_dpcm *dpcm;
2196 int state;
2198 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2200 if (dpcm->fe == fe)
2201 continue;
2203 state = dpcm->fe->dpcm[stream].state;
2204 if (state == SND_SOC_DPCM_STATE_START ||
2205 state == SND_SOC_DPCM_STATE_PAUSED ||
2206 state == SND_SOC_DPCM_STATE_SUSPEND)
2207 return 0;
2210 /* it's safe to free/stop this BE DAI */
2211 return 1;
2213 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2216 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2217 * running, paused or suspended for the specified stream direction.
2219 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2220 struct snd_soc_pcm_runtime *be, int stream)
2222 struct snd_soc_dpcm *dpcm;
2223 int state;
2225 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2227 if (dpcm->fe == fe)
2228 continue;
2230 state = dpcm->fe->dpcm[stream].state;
2231 if (state == SND_SOC_DPCM_STATE_START ||
2232 state == SND_SOC_DPCM_STATE_PAUSED ||
2233 state == SND_SOC_DPCM_STATE_SUSPEND ||
2234 state == SND_SOC_DPCM_STATE_PREPARE)
2235 return 0;
2238 /* it's safe to change hw_params */
2239 return 1;
2241 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2243 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2244 int cmd, struct snd_soc_platform *platform)
2246 if (platform->driver->ops && platform->driver->ops->trigger)
2247 return platform->driver->ops->trigger(substream, cmd);
2248 return 0;
2250 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2252 #ifdef CONFIG_DEBUG_FS
2253 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2255 switch (state) {
2256 case SND_SOC_DPCM_STATE_NEW:
2257 return "new";
2258 case SND_SOC_DPCM_STATE_OPEN:
2259 return "open";
2260 case SND_SOC_DPCM_STATE_HW_PARAMS:
2261 return "hw_params";
2262 case SND_SOC_DPCM_STATE_PREPARE:
2263 return "prepare";
2264 case SND_SOC_DPCM_STATE_START:
2265 return "start";
2266 case SND_SOC_DPCM_STATE_STOP:
2267 return "stop";
2268 case SND_SOC_DPCM_STATE_SUSPEND:
2269 return "suspend";
2270 case SND_SOC_DPCM_STATE_PAUSED:
2271 return "paused";
2272 case SND_SOC_DPCM_STATE_HW_FREE:
2273 return "hw_free";
2274 case SND_SOC_DPCM_STATE_CLOSE:
2275 return "close";
2278 return "unknown";
2281 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2282 int stream, char *buf, size_t size)
2284 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2285 struct snd_soc_dpcm *dpcm;
2286 ssize_t offset = 0;
2288 /* FE state */
2289 offset += snprintf(buf + offset, size - offset,
2290 "[%s - %s]\n", fe->dai_link->name,
2291 stream ? "Capture" : "Playback");
2293 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2294 dpcm_state_string(fe->dpcm[stream].state));
2296 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2297 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2298 offset += snprintf(buf + offset, size - offset,
2299 "Hardware Params: "
2300 "Format = %s, Channels = %d, Rate = %d\n",
2301 snd_pcm_format_name(params_format(params)),
2302 params_channels(params),
2303 params_rate(params));
2305 /* BEs state */
2306 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2308 if (list_empty(&fe->dpcm[stream].be_clients)) {
2309 offset += snprintf(buf + offset, size - offset,
2310 " No active DSP links\n");
2311 goto out;
2314 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2315 struct snd_soc_pcm_runtime *be = dpcm->be;
2316 params = &dpcm->hw_params;
2318 offset += snprintf(buf + offset, size - offset,
2319 "- %s\n", be->dai_link->name);
2321 offset += snprintf(buf + offset, size - offset,
2322 " State: %s\n",
2323 dpcm_state_string(be->dpcm[stream].state));
2325 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2326 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2327 offset += snprintf(buf + offset, size - offset,
2328 " Hardware Params: "
2329 "Format = %s, Channels = %d, Rate = %d\n",
2330 snd_pcm_format_name(params_format(params)),
2331 params_channels(params),
2332 params_rate(params));
2335 out:
2336 return offset;
2339 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2340 size_t count, loff_t *ppos)
2342 struct snd_soc_pcm_runtime *fe = file->private_data;
2343 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2344 char *buf;
2346 buf = kmalloc(out_count, GFP_KERNEL);
2347 if (!buf)
2348 return -ENOMEM;
2350 if (fe->cpu_dai->driver->playback.channels_min)
2351 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2352 buf + offset, out_count - offset);
2354 if (fe->cpu_dai->driver->capture.channels_min)
2355 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2356 buf + offset, out_count - offset);
2358 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2360 kfree(buf);
2361 return ret;
2364 static const struct file_operations dpcm_state_fops = {
2365 .open = simple_open,
2366 .read = dpcm_state_read_file,
2367 .llseek = default_llseek,
2370 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2372 if (!rtd->dai_link)
2373 return 0;
2375 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2376 rtd->card->debugfs_card_root);
2377 if (!rtd->debugfs_dpcm_root) {
2378 dev_dbg(rtd->dev,
2379 "ASoC: Failed to create dpcm debugfs directory %s\n",
2380 rtd->dai_link->name);
2381 return -EINVAL;
2384 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2385 rtd->debugfs_dpcm_root,
2386 rtd, &dpcm_state_fops);
2388 return 0;
2390 #endif