ixgbe: Initialize q_vector cpu and affinity masks correctly
[linux-2.6/libata-dev.git] / sound / soc / soc-pcm.c
blobef22d0bd9e9e62725f25f472aec279b75cf56e64
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/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/export.h>
26 #include <linux/debugfs.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dpcm.h>
32 #include <sound/initval.h>
34 #define DPCM_MAX_BE_USERS 8
36 /* DPCM stream event, send event to FE and all active BEs. */
37 static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
38 int event)
40 struct snd_soc_dpcm *dpcm;
42 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
44 struct snd_soc_pcm_runtime *be = dpcm->be;
46 dev_dbg(be->dev, "pm: BE %s event %d dir %d\n",
47 be->dai_link->name, event, dir);
49 snd_soc_dapm_stream_event(be, dir, event);
52 snd_soc_dapm_stream_event(fe, dir, event);
54 return 0;
57 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
58 struct snd_soc_dai *soc_dai)
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 int ret;
63 if (!soc_dai->driver->symmetric_rates &&
64 !rtd->dai_link->symmetric_rates)
65 return 0;
67 /* This can happen if multiple streams are starting simultaneously -
68 * the second can need to get its constraints before the first has
69 * picked a rate. Complain and allow the application to carry on.
71 if (!soc_dai->rate) {
72 dev_warn(soc_dai->dev,
73 "Not enforcing symmetric_rates due to race\n");
74 return 0;
77 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
79 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
80 SNDRV_PCM_HW_PARAM_RATE,
81 soc_dai->rate, soc_dai->rate);
82 if (ret < 0) {
83 dev_err(soc_dai->dev,
84 "Unable to apply rate symmetry constraint: %d\n", ret);
85 return ret;
88 return 0;
92 * List of sample sizes that might go over the bus for parameter
93 * application. There ought to be a wildcard sample size for things
94 * like the DAC/ADC resolution to use but there isn't right now.
96 static int sample_sizes[] = {
97 24, 32,
100 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
101 struct snd_soc_dai *dai)
103 int ret, i, bits;
105 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
106 bits = dai->driver->playback.sig_bits;
107 else
108 bits = dai->driver->capture.sig_bits;
110 if (!bits)
111 return;
113 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
114 if (bits >= sample_sizes[i])
115 continue;
117 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
118 sample_sizes[i], bits);
119 if (ret != 0)
120 dev_warn(dai->dev,
121 "Failed to set MSB %d/%d: %d\n",
122 bits, sample_sizes[i], ret);
127 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
128 * then initialized and any private data can be allocated. This also calls
129 * startup for the cpu DAI, platform, machine and codec DAI.
131 static int soc_pcm_open(struct snd_pcm_substream *substream)
133 struct snd_soc_pcm_runtime *rtd = substream->private_data;
134 struct snd_pcm_runtime *runtime = substream->runtime;
135 struct snd_soc_platform *platform = rtd->platform;
136 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
137 struct snd_soc_dai *codec_dai = rtd->codec_dai;
138 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
139 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
140 int ret = 0;
142 pm_runtime_get_sync(cpu_dai->dev);
143 pm_runtime_get_sync(codec_dai->dev);
144 pm_runtime_get_sync(platform->dev);
146 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
148 /* startup the audio subsystem */
149 if (cpu_dai->driver->ops->startup) {
150 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
151 if (ret < 0) {
152 dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
153 cpu_dai->name, ret);
154 goto out;
158 if (platform->driver->ops && platform->driver->ops->open) {
159 ret = platform->driver->ops->open(substream);
160 if (ret < 0) {
161 dev_err(platform->dev, "can't open platform %s: %d\n",
162 platform->name, ret);
163 goto platform_err;
167 if (codec_dai->driver->ops->startup) {
168 ret = codec_dai->driver->ops->startup(substream, codec_dai);
169 if (ret < 0) {
170 dev_err(codec_dai->dev, "can't open codec %s: %d\n",
171 codec_dai->name, ret);
172 goto codec_dai_err;
176 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
177 ret = rtd->dai_link->ops->startup(substream);
178 if (ret < 0) {
179 pr_err("asoc: %s startup failed: %d\n",
180 rtd->dai_link->name, ret);
181 goto machine_err;
185 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
186 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
187 goto dynamic;
189 /* Check that the codec and cpu DAIs are compatible */
190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
191 runtime->hw.rate_min =
192 max(codec_dai_drv->playback.rate_min,
193 cpu_dai_drv->playback.rate_min);
194 runtime->hw.rate_max =
195 min(codec_dai_drv->playback.rate_max,
196 cpu_dai_drv->playback.rate_max);
197 runtime->hw.channels_min =
198 max(codec_dai_drv->playback.channels_min,
199 cpu_dai_drv->playback.channels_min);
200 runtime->hw.channels_max =
201 min(codec_dai_drv->playback.channels_max,
202 cpu_dai_drv->playback.channels_max);
203 runtime->hw.formats =
204 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
205 runtime->hw.rates =
206 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
207 if (codec_dai_drv->playback.rates
208 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
209 runtime->hw.rates |= cpu_dai_drv->playback.rates;
210 if (cpu_dai_drv->playback.rates
211 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
212 runtime->hw.rates |= codec_dai_drv->playback.rates;
213 } else {
214 runtime->hw.rate_min =
215 max(codec_dai_drv->capture.rate_min,
216 cpu_dai_drv->capture.rate_min);
217 runtime->hw.rate_max =
218 min(codec_dai_drv->capture.rate_max,
219 cpu_dai_drv->capture.rate_max);
220 runtime->hw.channels_min =
221 max(codec_dai_drv->capture.channels_min,
222 cpu_dai_drv->capture.channels_min);
223 runtime->hw.channels_max =
224 min(codec_dai_drv->capture.channels_max,
225 cpu_dai_drv->capture.channels_max);
226 runtime->hw.formats =
227 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
228 runtime->hw.rates =
229 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
230 if (codec_dai_drv->capture.rates
231 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
232 runtime->hw.rates |= cpu_dai_drv->capture.rates;
233 if (cpu_dai_drv->capture.rates
234 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
235 runtime->hw.rates |= codec_dai_drv->capture.rates;
238 ret = -EINVAL;
239 snd_pcm_limit_hw_rates(runtime);
240 if (!runtime->hw.rates) {
241 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
242 codec_dai->name, cpu_dai->name);
243 goto config_err;
245 if (!runtime->hw.formats) {
246 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
247 codec_dai->name, cpu_dai->name);
248 goto config_err;
250 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
251 runtime->hw.channels_min > runtime->hw.channels_max) {
252 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
253 codec_dai->name, cpu_dai->name);
254 goto config_err;
257 soc_pcm_apply_msb(substream, codec_dai);
258 soc_pcm_apply_msb(substream, cpu_dai);
260 /* Symmetry only applies if we've already got an active stream. */
261 if (cpu_dai->active) {
262 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
263 if (ret != 0)
264 goto config_err;
267 if (codec_dai->active) {
268 ret = soc_pcm_apply_symmetry(substream, codec_dai);
269 if (ret != 0)
270 goto config_err;
273 pr_debug("asoc: %s <-> %s info:\n",
274 codec_dai->name, cpu_dai->name);
275 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
276 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
277 runtime->hw.channels_max);
278 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
279 runtime->hw.rate_max);
281 dynamic:
282 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
283 cpu_dai->playback_active++;
284 codec_dai->playback_active++;
285 } else {
286 cpu_dai->capture_active++;
287 codec_dai->capture_active++;
289 cpu_dai->active++;
290 codec_dai->active++;
291 rtd->codec->active++;
292 mutex_unlock(&rtd->pcm_mutex);
293 return 0;
295 config_err:
296 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
297 rtd->dai_link->ops->shutdown(substream);
299 machine_err:
300 if (codec_dai->driver->ops->shutdown)
301 codec_dai->driver->ops->shutdown(substream, codec_dai);
303 codec_dai_err:
304 if (platform->driver->ops && platform->driver->ops->close)
305 platform->driver->ops->close(substream);
307 platform_err:
308 if (cpu_dai->driver->ops->shutdown)
309 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
310 out:
311 mutex_unlock(&rtd->pcm_mutex);
313 pm_runtime_put(platform->dev);
314 pm_runtime_put(codec_dai->dev);
315 pm_runtime_put(cpu_dai->dev);
317 return ret;
321 * Power down the audio subsystem pmdown_time msecs after close is called.
322 * This is to ensure there are no pops or clicks in between any music tracks
323 * due to DAPM power cycling.
325 static void close_delayed_work(struct work_struct *work)
327 struct snd_soc_pcm_runtime *rtd =
328 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
329 struct snd_soc_dai *codec_dai = rtd->codec_dai;
331 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
333 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
334 codec_dai->driver->playback.stream_name,
335 codec_dai->playback_active ? "active" : "inactive",
336 codec_dai->pop_wait ? "yes" : "no");
338 /* are we waiting on this codec DAI stream */
339 if (codec_dai->pop_wait == 1) {
340 codec_dai->pop_wait = 0;
341 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
342 SND_SOC_DAPM_STREAM_STOP);
345 mutex_unlock(&rtd->pcm_mutex);
349 * Called by ALSA when a PCM substream is closed. Private data can be
350 * freed here. The cpu DAI, codec DAI, machine and platform are also
351 * shutdown.
353 static int soc_pcm_close(struct snd_pcm_substream *substream)
355 struct snd_soc_pcm_runtime *rtd = substream->private_data;
356 struct snd_soc_platform *platform = rtd->platform;
357 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
358 struct snd_soc_dai *codec_dai = rtd->codec_dai;
359 struct snd_soc_codec *codec = rtd->codec;
361 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
364 cpu_dai->playback_active--;
365 codec_dai->playback_active--;
366 } else {
367 cpu_dai->capture_active--;
368 codec_dai->capture_active--;
371 cpu_dai->active--;
372 codec_dai->active--;
373 codec->active--;
375 /* clear the corresponding DAIs rate when inactive */
376 if (!cpu_dai->active)
377 cpu_dai->rate = 0;
379 if (!codec_dai->active)
380 codec_dai->rate = 0;
382 /* Muting the DAC suppresses artifacts caused during digital
383 * shutdown, for example from stopping clocks.
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 snd_soc_dai_digital_mute(codec_dai, 1);
388 if (cpu_dai->driver->ops->shutdown)
389 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
391 if (codec_dai->driver->ops->shutdown)
392 codec_dai->driver->ops->shutdown(substream, codec_dai);
394 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
395 rtd->dai_link->ops->shutdown(substream);
397 if (platform->driver->ops && platform->driver->ops->close)
398 platform->driver->ops->close(substream);
399 cpu_dai->runtime = NULL;
401 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
402 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
403 rtd->dai_link->ignore_pmdown_time) {
404 /* powered down playback stream now */
405 snd_soc_dapm_stream_event(rtd,
406 SNDRV_PCM_STREAM_PLAYBACK,
407 SND_SOC_DAPM_STREAM_STOP);
408 } else {
409 /* start delayed pop wq here for playback streams */
410 codec_dai->pop_wait = 1;
411 schedule_delayed_work(&rtd->delayed_work,
412 msecs_to_jiffies(rtd->pmdown_time));
414 } else {
415 /* capture streams can be powered down now */
416 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
417 SND_SOC_DAPM_STREAM_STOP);
420 mutex_unlock(&rtd->pcm_mutex);
422 pm_runtime_put(platform->dev);
423 pm_runtime_put(codec_dai->dev);
424 pm_runtime_put(cpu_dai->dev);
426 return 0;
430 * Called by ALSA when the PCM substream is prepared, can set format, sample
431 * rate, etc. This function is non atomic and can be called multiple times,
432 * it can refer to the runtime info.
434 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
436 struct snd_soc_pcm_runtime *rtd = substream->private_data;
437 struct snd_soc_platform *platform = rtd->platform;
438 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
439 struct snd_soc_dai *codec_dai = rtd->codec_dai;
440 int ret = 0;
442 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
444 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
445 ret = rtd->dai_link->ops->prepare(substream);
446 if (ret < 0) {
447 pr_err("asoc: machine prepare error: %d\n", ret);
448 goto out;
452 if (platform->driver->ops && platform->driver->ops->prepare) {
453 ret = platform->driver->ops->prepare(substream);
454 if (ret < 0) {
455 dev_err(platform->dev, "platform prepare error: %d\n",
456 ret);
457 goto out;
461 if (codec_dai->driver->ops->prepare) {
462 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
463 if (ret < 0) {
464 dev_err(codec_dai->dev, "DAI prepare error: %d\n",
465 ret);
466 goto out;
470 if (cpu_dai->driver->ops->prepare) {
471 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
472 if (ret < 0) {
473 dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
474 ret);
475 goto out;
479 /* cancel any delayed stream shutdown that is pending */
480 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
481 codec_dai->pop_wait) {
482 codec_dai->pop_wait = 0;
483 cancel_delayed_work(&rtd->delayed_work);
486 snd_soc_dapm_stream_event(rtd, substream->stream,
487 SND_SOC_DAPM_STREAM_START);
489 snd_soc_dai_digital_mute(codec_dai, 0);
491 out:
492 mutex_unlock(&rtd->pcm_mutex);
493 return ret;
497 * Called by ALSA when the hardware params are set by application. This
498 * function can also be called multiple times and can allocate buffers
499 * (using snd_pcm_lib_* ). It's non-atomic.
501 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
502 struct snd_pcm_hw_params *params)
504 struct snd_soc_pcm_runtime *rtd = substream->private_data;
505 struct snd_soc_platform *platform = rtd->platform;
506 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
507 struct snd_soc_dai *codec_dai = rtd->codec_dai;
508 int ret = 0;
510 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
512 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
513 ret = rtd->dai_link->ops->hw_params(substream, params);
514 if (ret < 0) {
515 pr_err("asoc: machine hw_params failed: %d\n", ret);
516 goto out;
520 if (codec_dai->driver->ops->hw_params) {
521 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
522 if (ret < 0) {
523 dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
524 codec_dai->name, ret);
525 goto codec_err;
529 if (cpu_dai->driver->ops->hw_params) {
530 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
531 if (ret < 0) {
532 dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
533 cpu_dai->name, ret);
534 goto interface_err;
538 if (platform->driver->ops && platform->driver->ops->hw_params) {
539 ret = platform->driver->ops->hw_params(substream, params);
540 if (ret < 0) {
541 dev_err(platform->dev, "%s hw params failed: %d\n",
542 platform->name, ret);
543 goto platform_err;
547 /* store the rate for each DAIs */
548 cpu_dai->rate = params_rate(params);
549 codec_dai->rate = params_rate(params);
551 out:
552 mutex_unlock(&rtd->pcm_mutex);
553 return ret;
555 platform_err:
556 if (cpu_dai->driver->ops->hw_free)
557 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
559 interface_err:
560 if (codec_dai->driver->ops->hw_free)
561 codec_dai->driver->ops->hw_free(substream, codec_dai);
563 codec_err:
564 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
565 rtd->dai_link->ops->hw_free(substream);
567 mutex_unlock(&rtd->pcm_mutex);
568 return ret;
572 * Frees resources allocated by hw_params, can be called multiple times
574 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
576 struct snd_soc_pcm_runtime *rtd = substream->private_data;
577 struct snd_soc_platform *platform = rtd->platform;
578 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
579 struct snd_soc_dai *codec_dai = rtd->codec_dai;
580 struct snd_soc_codec *codec = rtd->codec;
582 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
584 /* apply codec digital mute */
585 if (!codec->active)
586 snd_soc_dai_digital_mute(codec_dai, 1);
588 /* free any machine hw params */
589 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
590 rtd->dai_link->ops->hw_free(substream);
592 /* free any DMA resources */
593 if (platform->driver->ops && platform->driver->ops->hw_free)
594 platform->driver->ops->hw_free(substream);
596 /* now free hw params for the DAIs */
597 if (codec_dai->driver->ops->hw_free)
598 codec_dai->driver->ops->hw_free(substream, codec_dai);
600 if (cpu_dai->driver->ops->hw_free)
601 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
603 mutex_unlock(&rtd->pcm_mutex);
604 return 0;
607 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
609 struct snd_soc_pcm_runtime *rtd = substream->private_data;
610 struct snd_soc_platform *platform = rtd->platform;
611 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
612 struct snd_soc_dai *codec_dai = rtd->codec_dai;
613 int ret;
615 if (codec_dai->driver->ops->trigger) {
616 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
617 if (ret < 0)
618 return ret;
621 if (platform->driver->ops && platform->driver->ops->trigger) {
622 ret = platform->driver->ops->trigger(substream, cmd);
623 if (ret < 0)
624 return ret;
627 if (cpu_dai->driver->ops->trigger) {
628 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
629 if (ret < 0)
630 return ret;
632 return 0;
635 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
636 int cmd)
638 struct snd_soc_pcm_runtime *rtd = substream->private_data;
639 struct snd_soc_platform *platform = rtd->platform;
640 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
642 int ret;
644 if (codec_dai->driver->ops->bespoke_trigger) {
645 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
646 if (ret < 0)
647 return ret;
650 if (platform->driver->bespoke_trigger) {
651 ret = platform->driver->bespoke_trigger(substream, cmd);
652 if (ret < 0)
653 return ret;
656 if (cpu_dai->driver->ops->bespoke_trigger) {
657 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
658 if (ret < 0)
659 return ret;
661 return 0;
664 * soc level wrapper for pointer callback
665 * If cpu_dai, codec_dai, platform driver has the delay callback, than
666 * the runtime->delay will be updated accordingly.
668 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
670 struct snd_soc_pcm_runtime *rtd = substream->private_data;
671 struct snd_soc_platform *platform = rtd->platform;
672 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
673 struct snd_soc_dai *codec_dai = rtd->codec_dai;
674 struct snd_pcm_runtime *runtime = substream->runtime;
675 snd_pcm_uframes_t offset = 0;
676 snd_pcm_sframes_t delay = 0;
678 if (platform->driver->ops && platform->driver->ops->pointer)
679 offset = platform->driver->ops->pointer(substream);
681 if (cpu_dai->driver->ops->delay)
682 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
684 if (codec_dai->driver->ops->delay)
685 delay += codec_dai->driver->ops->delay(substream, codec_dai);
687 if (platform->driver->delay)
688 delay += platform->driver->delay(substream, codec_dai);
690 runtime->delay = delay;
692 return offset;
695 /* connect a FE and BE */
696 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
697 struct snd_soc_pcm_runtime *be, int stream)
699 struct snd_soc_dpcm *dpcm;
701 /* only add new dpcms */
702 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
703 if (dpcm->be == be && dpcm->fe == fe)
704 return 0;
707 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
708 if (!dpcm)
709 return -ENOMEM;
711 dpcm->be = be;
712 dpcm->fe = fe;
713 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
714 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
715 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
716 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
718 dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
719 stream ? "capture" : "playback", fe->dai_link->name,
720 stream ? "<-" : "->", be->dai_link->name);
722 #ifdef CONFIG_DEBUG_FS
723 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
724 fe->debugfs_dpcm_root, &dpcm->state);
725 #endif
726 return 1;
729 /* reparent a BE onto another FE */
730 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
731 struct snd_soc_pcm_runtime *be, int stream)
733 struct snd_soc_dpcm *dpcm;
734 struct snd_pcm_substream *fe_substream, *be_substream;
736 /* reparent if BE is connected to other FEs */
737 if (!be->dpcm[stream].users)
738 return;
740 be_substream = snd_soc_dpcm_get_substream(be, stream);
742 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
743 if (dpcm->fe == fe)
744 continue;
746 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
747 stream ? "capture" : "playback",
748 dpcm->fe->dai_link->name,
749 stream ? "<-" : "->", dpcm->be->dai_link->name);
751 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
752 be_substream->runtime = fe_substream->runtime;
753 break;
757 /* disconnect a BE and FE */
758 static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
760 struct snd_soc_dpcm *dpcm, *d;
762 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
763 dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
764 stream ? "capture" : "playback",
765 dpcm->be->dai_link->name);
767 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
768 continue;
770 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
771 stream ? "capture" : "playback", fe->dai_link->name,
772 stream ? "<-" : "->", dpcm->be->dai_link->name);
774 /* BEs still alive need new FE */
775 dpcm_be_reparent(fe, dpcm->be, stream);
777 #ifdef CONFIG_DEBUG_FS
778 debugfs_remove(dpcm->debugfs_state);
779 #endif
780 list_del(&dpcm->list_be);
781 list_del(&dpcm->list_fe);
782 kfree(dpcm);
786 /* get BE for DAI widget and stream */
787 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
788 struct snd_soc_dapm_widget *widget, int stream)
790 struct snd_soc_pcm_runtime *be;
791 int i;
793 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
794 for (i = 0; i < card->num_links; i++) {
795 be = &card->rtd[i];
797 if (!be->dai_link->no_pcm)
798 continue;
800 if (be->cpu_dai->playback_widget == widget ||
801 be->codec_dai->playback_widget == widget)
802 return be;
804 } else {
806 for (i = 0; i < card->num_links; i++) {
807 be = &card->rtd[i];
809 if (!be->dai_link->no_pcm)
810 continue;
812 if (be->cpu_dai->capture_widget == widget ||
813 be->codec_dai->capture_widget == widget)
814 return be;
818 dev_err(card->dev, "can't get %s BE for %s\n",
819 stream ? "capture" : "playback", widget->name);
820 return NULL;
823 static inline struct snd_soc_dapm_widget *
824 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
826 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
827 return rtd->cpu_dai->playback_widget;
828 else
829 return rtd->cpu_dai->capture_widget;
832 static inline struct snd_soc_dapm_widget *
833 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
835 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
836 return rtd->codec_dai->playback_widget;
837 else
838 return rtd->codec_dai->capture_widget;
841 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
842 struct snd_soc_dapm_widget *widget)
844 int i;
846 for (i = 0; i < list->num_widgets; i++) {
847 if (widget == list->widgets[i])
848 return 1;
851 return 0;
854 static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
855 int stream, struct snd_soc_dapm_widget_list **list_)
857 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
858 struct snd_soc_dapm_widget_list *list;
859 int paths;
861 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
862 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
863 if (list == NULL)
864 return -ENOMEM;
866 /* get number of valid DAI paths and their widgets */
867 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
869 dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
870 stream ? "capture" : "playback");
872 *list_ = list;
873 return paths;
876 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
878 kfree(*list);
881 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
882 struct snd_soc_dapm_widget_list **list_)
884 struct snd_soc_dpcm *dpcm;
885 struct snd_soc_dapm_widget_list *list = *list_;
886 struct snd_soc_dapm_widget *widget;
887 int prune = 0;
889 /* Destroy any old FE <--> BE connections */
890 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
892 /* is there a valid CPU DAI widget for this BE */
893 widget = rtd_get_cpu_widget(dpcm->be, stream);
895 /* prune the BE if it's no longer in our active list */
896 if (widget && widget_in_list(list, widget))
897 continue;
899 /* is there a valid CODEC DAI widget for this BE */
900 widget = rtd_get_codec_widget(dpcm->be, stream);
902 /* prune the BE if it's no longer in our active list */
903 if (widget && widget_in_list(list, widget))
904 continue;
906 dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
907 stream ? "capture" : "playback",
908 dpcm->be->dai_link->name, fe->dai_link->name);
909 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
910 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
911 prune++;
914 dev_dbg(fe->dev, "found %d old BE paths for pruning\n", prune);
915 return prune;
918 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
919 struct snd_soc_dapm_widget_list **list_)
921 struct snd_soc_card *card = fe->card;
922 struct snd_soc_dapm_widget_list *list = *list_;
923 struct snd_soc_pcm_runtime *be;
924 int i, new = 0, err;
926 /* Create any new FE <--> BE connections */
927 for (i = 0; i < list->num_widgets; i++) {
929 if (list->widgets[i]->id != snd_soc_dapm_dai)
930 continue;
932 /* is there a valid BE rtd for this widget */
933 be = dpcm_get_be(card, list->widgets[i], stream);
934 if (!be) {
935 dev_err(fe->dev, "no BE found for %s\n",
936 list->widgets[i]->name);
937 continue;
940 /* make sure BE is a real BE */
941 if (!be->dai_link->no_pcm)
942 continue;
944 /* don't connect if FE is not running */
945 if (!fe->dpcm[stream].runtime)
946 continue;
948 /* newly connected FE and BE */
949 err = dpcm_be_connect(fe, be, stream);
950 if (err < 0) {
951 dev_err(fe->dev, "can't connect %s\n",
952 list->widgets[i]->name);
953 break;
954 } else if (err == 0) /* already connected */
955 continue;
957 /* new */
958 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
959 new++;
962 dev_dbg(fe->dev, "found %d new BE paths\n", new);
963 return new;
967 * Find the corresponding BE DAIs that source or sink audio to this
968 * FE substream.
970 static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
971 int stream, struct snd_soc_dapm_widget_list **list, int new)
973 if (new)
974 return dpcm_add_paths(fe, stream, list);
975 else
976 return dpcm_prune_paths(fe, stream, list);
979 static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
981 struct snd_soc_dpcm *dpcm;
983 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
984 dpcm->be->dpcm[stream].runtime_update =
985 SND_SOC_DPCM_UPDATE_NO;
988 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
989 int stream)
991 struct snd_soc_dpcm *dpcm;
993 /* disable any enabled and non active backends */
994 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
996 struct snd_soc_pcm_runtime *be = dpcm->be;
997 struct snd_pcm_substream *be_substream =
998 snd_soc_dpcm_get_substream(be, stream);
1000 if (be->dpcm[stream].users == 0)
1001 dev_err(be->dev, "no users %s at close - state %d\n",
1002 stream ? "capture" : "playback",
1003 be->dpcm[stream].state);
1005 if (--be->dpcm[stream].users != 0)
1006 continue;
1008 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1009 continue;
1011 soc_pcm_close(be_substream);
1012 be_substream->runtime = NULL;
1013 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1017 static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1019 struct snd_soc_dpcm *dpcm;
1020 int err, count = 0;
1022 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1023 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1025 struct snd_soc_pcm_runtime *be = dpcm->be;
1026 struct snd_pcm_substream *be_substream =
1027 snd_soc_dpcm_get_substream(be, stream);
1029 /* is this op for this BE ? */
1030 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1031 continue;
1033 /* first time the dpcm is open ? */
1034 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1035 dev_err(be->dev, "too many users %s at open %d\n",
1036 stream ? "capture" : "playback",
1037 be->dpcm[stream].state);
1039 if (be->dpcm[stream].users++ != 0)
1040 continue;
1042 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1043 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1044 continue;
1046 dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
1048 be_substream->runtime = be->dpcm[stream].runtime;
1049 err = soc_pcm_open(be_substream);
1050 if (err < 0) {
1051 dev_err(be->dev, "BE open failed %d\n", err);
1052 be->dpcm[stream].users--;
1053 if (be->dpcm[stream].users < 0)
1054 dev_err(be->dev, "no users %s at unwind %d\n",
1055 stream ? "capture" : "playback",
1056 be->dpcm[stream].state);
1058 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1059 goto unwind;
1062 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1063 count++;
1066 return count;
1068 unwind:
1069 /* disable any enabled and non active backends */
1070 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1071 struct snd_soc_pcm_runtime *be = dpcm->be;
1072 struct snd_pcm_substream *be_substream =
1073 snd_soc_dpcm_get_substream(be, stream);
1075 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1076 continue;
1078 if (be->dpcm[stream].users == 0)
1079 dev_err(be->dev, "no users %s at close %d\n",
1080 stream ? "capture" : "playback",
1081 be->dpcm[stream].state);
1083 if (--be->dpcm[stream].users != 0)
1084 continue;
1086 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1087 continue;
1089 soc_pcm_close(be_substream);
1090 be_substream->runtime = NULL;
1091 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1094 return err;
1097 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1099 struct snd_pcm_runtime *runtime = substream->runtime;
1100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1101 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1102 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1104 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1105 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1106 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1107 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1108 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1109 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1110 runtime->hw.rates = cpu_dai_drv->playback.rates;
1111 } else {
1112 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1113 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1114 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1115 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1116 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1117 runtime->hw.rates = cpu_dai_drv->capture.rates;
1121 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1123 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1124 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1125 int stream = fe_substream->stream, ret = 0;
1127 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1129 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1130 if (ret < 0) {
1131 dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
1132 goto be_err;
1135 dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
1137 /* start the DAI frontend */
1138 ret = soc_pcm_open(fe_substream);
1139 if (ret < 0) {
1140 dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
1141 goto unwind;
1144 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1146 dpcm_set_fe_runtime(fe_substream);
1147 snd_pcm_limit_hw_rates(runtime);
1149 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1150 return 0;
1152 unwind:
1153 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1154 be_err:
1155 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1156 return ret;
1159 static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1161 struct snd_soc_dpcm *dpcm;
1163 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1164 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1166 struct snd_soc_pcm_runtime *be = dpcm->be;
1167 struct snd_pcm_substream *be_substream =
1168 snd_soc_dpcm_get_substream(be, stream);
1170 /* is this op for this BE ? */
1171 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1172 continue;
1174 if (be->dpcm[stream].users == 0)
1175 dev_err(be->dev, "no users %s at close - state %d\n",
1176 stream ? "capture" : "playback",
1177 be->dpcm[stream].state);
1179 if (--be->dpcm[stream].users != 0)
1180 continue;
1182 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1183 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1184 continue;
1186 dev_dbg(be->dev, "dpcm: close BE %s\n",
1187 dpcm->fe->dai_link->name);
1189 soc_pcm_close(be_substream);
1190 be_substream->runtime = NULL;
1192 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1194 return 0;
1197 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1199 struct snd_soc_pcm_runtime *fe = substream->private_data;
1200 int stream = substream->stream;
1202 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1204 /* shutdown the BEs */
1205 dpcm_be_dai_shutdown(fe, substream->stream);
1207 dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
1209 /* now shutdown the frontend */
1210 soc_pcm_close(substream);
1212 /* run the stream event for each BE */
1213 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1215 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1216 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1217 return 0;
1220 static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1222 struct snd_soc_dpcm *dpcm;
1224 /* only hw_params backends that are either sinks or sources
1225 * to this frontend DAI */
1226 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1228 struct snd_soc_pcm_runtime *be = dpcm->be;
1229 struct snd_pcm_substream *be_substream =
1230 snd_soc_dpcm_get_substream(be, stream);
1232 /* is this op for this BE ? */
1233 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1234 continue;
1236 /* only free hw when no longer used - check all FEs */
1237 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1238 continue;
1240 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1241 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1242 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1243 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1244 continue;
1246 dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
1247 dpcm->fe->dai_link->name);
1249 soc_pcm_hw_free(be_substream);
1251 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1254 return 0;
1257 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1259 struct snd_soc_pcm_runtime *fe = substream->private_data;
1260 int err, stream = substream->stream;
1262 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1263 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1265 dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
1267 /* call hw_free on the frontend */
1268 err = soc_pcm_hw_free(substream);
1269 if (err < 0)
1270 dev_err(fe->dev,"dpcm: hw_free FE %s failed\n",
1271 fe->dai_link->name);
1273 /* only hw_params backends that are either sinks or sources
1274 * to this frontend DAI */
1275 err = dpcm_be_dai_hw_free(fe, stream);
1277 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1278 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1280 mutex_unlock(&fe->card->mutex);
1281 return 0;
1284 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1286 struct snd_soc_dpcm *dpcm;
1287 int ret;
1289 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1291 struct snd_soc_pcm_runtime *be = dpcm->be;
1292 struct snd_pcm_substream *be_substream =
1293 snd_soc_dpcm_get_substream(be, stream);
1295 /* is this op for this BE ? */
1296 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1297 continue;
1299 /* only allow hw_params() if no connected FEs are running */
1300 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1301 continue;
1303 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1304 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1305 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1306 continue;
1308 dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
1309 dpcm->fe->dai_link->name);
1311 /* copy params for each dpcm */
1312 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1313 sizeof(struct snd_pcm_hw_params));
1315 /* perform any hw_params fixups */
1316 if (be->dai_link->be_hw_params_fixup) {
1317 ret = be->dai_link->be_hw_params_fixup(be,
1318 &dpcm->hw_params);
1319 if (ret < 0) {
1320 dev_err(be->dev,
1321 "dpcm: hw_params BE fixup failed %d\n",
1322 ret);
1323 goto unwind;
1327 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1328 if (ret < 0) {
1329 dev_err(dpcm->be->dev,
1330 "dpcm: hw_params BE failed %d\n", ret);
1331 goto unwind;
1334 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1336 return 0;
1338 unwind:
1339 /* disable any enabled and non active backends */
1340 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1341 struct snd_soc_pcm_runtime *be = dpcm->be;
1342 struct snd_pcm_substream *be_substream =
1343 snd_soc_dpcm_get_substream(be, stream);
1345 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1346 continue;
1348 /* only allow hw_free() if no connected FEs are running */
1349 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1350 continue;
1352 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1353 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1354 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1355 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1356 continue;
1358 soc_pcm_hw_free(be_substream);
1361 return ret;
1364 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1365 struct snd_pcm_hw_params *params)
1367 struct snd_soc_pcm_runtime *fe = substream->private_data;
1368 int ret, stream = substream->stream;
1370 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1371 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1373 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1374 sizeof(struct snd_pcm_hw_params));
1375 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1376 if (ret < 0) {
1377 dev_err(fe->dev,"dpcm: hw_params BE failed %d\n", ret);
1378 goto out;
1381 dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
1382 fe->dai_link->name, params_rate(params),
1383 params_channels(params), params_format(params));
1385 /* call hw_params on the frontend */
1386 ret = soc_pcm_hw_params(substream, params);
1387 if (ret < 0) {
1388 dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
1389 dpcm_be_dai_hw_free(fe, stream);
1390 } else
1391 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1393 out:
1394 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1395 mutex_unlock(&fe->card->mutex);
1396 return ret;
1399 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1400 struct snd_pcm_substream *substream, int cmd)
1402 int ret;
1404 dev_dbg(dpcm->be->dev, "dpcm: trigger BE %s cmd %d\n",
1405 dpcm->fe->dai_link->name, cmd);
1407 ret = soc_pcm_trigger(substream, cmd);
1408 if (ret < 0)
1409 dev_err(dpcm->be->dev,"dpcm: trigger BE failed %d\n", ret);
1411 return ret;
1414 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1415 int cmd)
1417 struct snd_soc_dpcm *dpcm;
1418 int ret = 0;
1420 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1422 struct snd_soc_pcm_runtime *be = dpcm->be;
1423 struct snd_pcm_substream *be_substream =
1424 snd_soc_dpcm_get_substream(be, stream);
1426 /* is this op for this BE ? */
1427 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1428 continue;
1430 switch (cmd) {
1431 case SNDRV_PCM_TRIGGER_START:
1432 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1433 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1434 continue;
1436 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1437 if (ret)
1438 return ret;
1440 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1441 break;
1442 case SNDRV_PCM_TRIGGER_RESUME:
1443 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1444 continue;
1446 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1447 if (ret)
1448 return ret;
1450 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1451 break;
1452 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1453 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1454 continue;
1456 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1457 if (ret)
1458 return ret;
1460 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1461 break;
1462 case SNDRV_PCM_TRIGGER_STOP:
1463 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1464 continue;
1466 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1467 continue;
1469 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1470 if (ret)
1471 return ret;
1473 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1474 break;
1475 case SNDRV_PCM_TRIGGER_SUSPEND:
1476 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1477 continue;
1479 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1480 continue;
1482 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1483 if (ret)
1484 return ret;
1486 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1487 break;
1488 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1489 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1490 continue;
1492 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1493 continue;
1495 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1496 if (ret)
1497 return ret;
1499 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1500 break;
1504 return ret;
1506 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1508 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1510 struct snd_soc_pcm_runtime *fe = substream->private_data;
1511 int stream = substream->stream, ret;
1512 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1514 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1516 switch (trigger) {
1517 case SND_SOC_DPCM_TRIGGER_PRE:
1518 /* call trigger on the frontend before the backend. */
1520 dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
1521 fe->dai_link->name, cmd);
1523 ret = soc_pcm_trigger(substream, cmd);
1524 if (ret < 0) {
1525 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1526 goto out;
1529 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1530 break;
1531 case SND_SOC_DPCM_TRIGGER_POST:
1532 /* call trigger on the frontend after the backend. */
1534 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1535 if (ret < 0) {
1536 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1537 goto out;
1540 dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
1541 fe->dai_link->name, cmd);
1543 ret = soc_pcm_trigger(substream, cmd);
1544 break;
1545 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1546 /* bespoke trigger() - handles both FE and BEs */
1548 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd %d\n",
1549 fe->dai_link->name, cmd);
1551 ret = soc_pcm_bespoke_trigger(substream, cmd);
1552 if (ret < 0) {
1553 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1554 goto out;
1556 break;
1557 default:
1558 dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
1559 fe->dai_link->name);
1560 ret = -EINVAL;
1561 goto out;
1564 switch (cmd) {
1565 case SNDRV_PCM_TRIGGER_START:
1566 case SNDRV_PCM_TRIGGER_RESUME:
1567 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1568 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1569 break;
1570 case SNDRV_PCM_TRIGGER_STOP:
1571 case SNDRV_PCM_TRIGGER_SUSPEND:
1572 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1573 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1574 break;
1577 out:
1578 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1579 return ret;
1582 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1584 struct snd_soc_dpcm *dpcm;
1585 int ret = 0;
1587 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1589 struct snd_soc_pcm_runtime *be = dpcm->be;
1590 struct snd_pcm_substream *be_substream =
1591 snd_soc_dpcm_get_substream(be, stream);
1593 /* is this op for this BE ? */
1594 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1595 continue;
1597 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1598 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1599 continue;
1601 dev_dbg(be->dev, "dpcm: prepare BE %s\n",
1602 dpcm->fe->dai_link->name);
1604 ret = soc_pcm_prepare(be_substream);
1605 if (ret < 0) {
1606 dev_err(be->dev, "dpcm: backend prepare failed %d\n",
1607 ret);
1608 break;
1611 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1613 return ret;
1616 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1618 struct snd_soc_pcm_runtime *fe = substream->private_data;
1619 int stream = substream->stream, ret = 0;
1621 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1623 dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
1625 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1627 /* there is no point preparing this FE if there are no BEs */
1628 if (list_empty(&fe->dpcm[stream].be_clients)) {
1629 dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
1630 fe->dai_link->name);
1631 ret = -EINVAL;
1632 goto out;
1635 ret = dpcm_be_dai_prepare(fe, substream->stream);
1636 if (ret < 0)
1637 goto out;
1639 /* call prepare on the frontend */
1640 ret = soc_pcm_prepare(substream);
1641 if (ret < 0) {
1642 dev_err(fe->dev,"dpcm: prepare FE %s failed\n",
1643 fe->dai_link->name);
1644 goto out;
1647 /* run the stream event for each BE */
1648 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1649 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1651 out:
1652 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1653 mutex_unlock(&fe->card->mutex);
1655 return ret;
1658 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1659 unsigned int cmd, void *arg)
1661 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1662 struct snd_soc_platform *platform = rtd->platform;
1664 if (platform->driver->ops->ioctl)
1665 return platform->driver->ops->ioctl(substream, cmd, arg);
1666 return snd_pcm_lib_ioctl(substream, cmd, arg);
1669 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1671 struct snd_pcm_substream *substream =
1672 snd_soc_dpcm_get_substream(fe, stream);
1673 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1674 int err;
1676 dev_dbg(fe->dev, "runtime %s close on FE %s\n",
1677 stream ? "capture" : "playback", fe->dai_link->name);
1679 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1680 /* call bespoke trigger - FE takes care of all BE triggers */
1681 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd stop\n",
1682 fe->dai_link->name);
1684 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1685 if (err < 0)
1686 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1687 } else {
1688 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd stop\n",
1689 fe->dai_link->name);
1691 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1692 if (err < 0)
1693 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1696 err = dpcm_be_dai_hw_free(fe, stream);
1697 if (err < 0)
1698 dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
1700 err = dpcm_be_dai_shutdown(fe, stream);
1701 if (err < 0)
1702 dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
1704 /* run the stream event for each BE */
1705 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1707 return 0;
1710 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1712 struct snd_pcm_substream *substream =
1713 snd_soc_dpcm_get_substream(fe, stream);
1714 struct snd_soc_dpcm *dpcm;
1715 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1716 int ret;
1718 dev_dbg(fe->dev, "runtime %s open on FE %s\n",
1719 stream ? "capture" : "playback", fe->dai_link->name);
1721 /* Only start the BE if the FE is ready */
1722 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1723 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1724 return -EINVAL;
1726 /* startup must always be called for new BEs */
1727 ret = dpcm_be_dai_startup(fe, stream);
1728 if (ret < 0) {
1729 goto disconnect;
1730 return ret;
1733 /* keep going if FE state is > open */
1734 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1735 return 0;
1737 ret = dpcm_be_dai_hw_params(fe, stream);
1738 if (ret < 0) {
1739 goto close;
1740 return ret;
1743 /* keep going if FE state is > hw_params */
1744 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1745 return 0;
1748 ret = dpcm_be_dai_prepare(fe, stream);
1749 if (ret < 0) {
1750 goto hw_free;
1751 return ret;
1754 /* run the stream event for each BE */
1755 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1757 /* keep going if FE state is > prepare */
1758 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1759 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1760 return 0;
1762 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1763 /* call trigger on the frontend - FE takes care of all BE triggers */
1764 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd start\n",
1765 fe->dai_link->name);
1767 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1768 if (ret < 0) {
1769 dev_err(fe->dev,"dpcm: bespoke trigger FE failed %d\n", ret);
1770 goto hw_free;
1772 } else {
1773 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
1774 fe->dai_link->name);
1776 ret = dpcm_be_dai_trigger(fe, stream,
1777 SNDRV_PCM_TRIGGER_START);
1778 if (ret < 0) {
1779 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1780 goto hw_free;
1784 return 0;
1786 hw_free:
1787 dpcm_be_dai_hw_free(fe, stream);
1788 close:
1789 dpcm_be_dai_shutdown(fe, stream);
1790 disconnect:
1791 /* disconnect any non started BEs */
1792 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1793 struct snd_soc_pcm_runtime *be = dpcm->be;
1794 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1795 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1798 return ret;
1801 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1803 int ret;
1805 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1806 ret = dpcm_run_update_startup(fe, stream);
1807 if (ret < 0)
1808 dev_err(fe->dev, "failed to startup some BEs\n");
1809 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1811 return ret;
1814 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1816 int ret;
1818 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1819 ret = dpcm_run_update_shutdown(fe, stream);
1820 if (ret < 0)
1821 dev_err(fe->dev, "failed to shutdown some BEs\n");
1822 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1824 return ret;
1827 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1828 * any DAI links.
1830 int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1832 struct snd_soc_card *card;
1833 int i, old, new, paths;
1835 if (widget->codec)
1836 card = widget->codec->card;
1837 else if (widget->platform)
1838 card = widget->platform->card;
1839 else
1840 return -EINVAL;
1842 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1843 for (i = 0; i < card->num_rtd; i++) {
1844 struct snd_soc_dapm_widget_list *list;
1845 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1847 /* make sure link is FE */
1848 if (!fe->dai_link->dynamic)
1849 continue;
1851 /* only check active links */
1852 if (!fe->cpu_dai->active)
1853 continue;
1855 /* DAPM sync will call this to update DSP paths */
1856 dev_dbg(fe->dev, "DPCM runtime update for FE %s\n",
1857 fe->dai_link->name);
1859 /* skip if FE doesn't have playback capability */
1860 if (!fe->cpu_dai->driver->playback.channels_min)
1861 goto capture;
1863 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1864 if (paths < 0) {
1865 dev_warn(fe->dev, "%s no valid %s path\n",
1866 fe->dai_link->name, "playback");
1867 mutex_unlock(&card->mutex);
1868 return paths;
1871 /* update any new playback paths */
1872 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1873 if (new) {
1874 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1875 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1876 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1879 /* update any old playback paths */
1880 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1881 if (old) {
1882 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1883 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1884 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1887 capture:
1888 /* skip if FE doesn't have capture capability */
1889 if (!fe->cpu_dai->driver->capture.channels_min)
1890 continue;
1892 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1893 if (paths < 0) {
1894 dev_warn(fe->dev, "%s no valid %s path\n",
1895 fe->dai_link->name, "capture");
1896 mutex_unlock(&card->mutex);
1897 return paths;
1900 /* update any new capture paths */
1901 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1902 if (new) {
1903 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1904 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1905 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1908 /* update any old capture paths */
1909 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1910 if (old) {
1911 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1912 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1913 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1916 dpcm_path_put(&list);
1919 mutex_unlock(&card->mutex);
1920 return 0;
1922 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1924 struct snd_soc_dpcm *dpcm;
1925 struct list_head *clients =
1926 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1928 list_for_each_entry(dpcm, clients, list_be) {
1930 struct snd_soc_pcm_runtime *be = dpcm->be;
1931 struct snd_soc_dai *dai = be->codec_dai;
1932 struct snd_soc_dai_driver *drv = dai->driver;
1934 if (be->dai_link->ignore_suspend)
1935 continue;
1937 dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
1939 if (drv->ops->digital_mute && dai->playback_active)
1940 drv->ops->digital_mute(dai, mute);
1943 return 0;
1946 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
1948 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1949 struct snd_soc_dpcm *dpcm;
1950 struct snd_soc_dapm_widget_list *list;
1951 int ret;
1952 int stream = fe_substream->stream;
1954 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1955 fe->dpcm[stream].runtime = fe_substream->runtime;
1957 if (dpcm_path_get(fe, stream, &list) <= 0) {
1958 dev_dbg(fe->dev, "asoc: %s no valid %s route\n",
1959 fe->dai_link->name, stream ? "capture" : "playback");
1962 /* calculate valid and active FE <-> BE dpcms */
1963 dpcm_process_paths(fe, stream, &list, 1);
1965 ret = dpcm_fe_dai_startup(fe_substream);
1966 if (ret < 0) {
1967 /* clean up all links */
1968 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1969 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1971 dpcm_be_disconnect(fe, stream);
1972 fe->dpcm[stream].runtime = NULL;
1975 dpcm_clear_pending_state(fe, stream);
1976 dpcm_path_put(&list);
1977 mutex_unlock(&fe->card->mutex);
1978 return ret;
1981 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
1983 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1984 struct snd_soc_dpcm *dpcm;
1985 int stream = fe_substream->stream, ret;
1987 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1988 ret = dpcm_fe_dai_shutdown(fe_substream);
1990 /* mark FE's links ready to prune */
1991 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1992 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1994 dpcm_be_disconnect(fe, stream);
1996 fe->dpcm[stream].runtime = NULL;
1997 mutex_unlock(&fe->card->mutex);
1998 return ret;
2001 /* create a new pcm */
2002 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2004 struct snd_soc_platform *platform = rtd->platform;
2005 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2006 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2007 struct snd_pcm *pcm;
2008 char new_name[64];
2009 int ret = 0, playback = 0, capture = 0;
2011 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2012 if (cpu_dai->driver->playback.channels_min)
2013 playback = 1;
2014 if (cpu_dai->driver->capture.channels_min)
2015 capture = 1;
2016 } else {
2017 if (codec_dai->driver->playback.channels_min)
2018 playback = 1;
2019 if (codec_dai->driver->capture.channels_min)
2020 capture = 1;
2023 /* create the PCM */
2024 if (rtd->dai_link->no_pcm) {
2025 snprintf(new_name, sizeof(new_name), "(%s)",
2026 rtd->dai_link->stream_name);
2028 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2029 playback, capture, &pcm);
2030 } else {
2031 if (rtd->dai_link->dynamic)
2032 snprintf(new_name, sizeof(new_name), "%s (*)",
2033 rtd->dai_link->stream_name);
2034 else
2035 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2036 rtd->dai_link->stream_name, codec_dai->name, num);
2038 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2039 capture, &pcm);
2041 if (ret < 0) {
2042 dev_err(rtd->card->dev, "can't create pcm for %s\n",
2043 rtd->dai_link->name);
2044 return ret;
2046 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
2048 /* DAPM dai link stream work */
2049 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2051 rtd->pcm = pcm;
2052 pcm->private_data = rtd;
2054 if (rtd->dai_link->no_pcm) {
2055 if (playback)
2056 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2057 if (capture)
2058 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2059 goto out;
2062 /* ASoC PCM operations */
2063 if (rtd->dai_link->dynamic) {
2064 rtd->ops.open = dpcm_fe_dai_open;
2065 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2066 rtd->ops.prepare = dpcm_fe_dai_prepare;
2067 rtd->ops.trigger = dpcm_fe_dai_trigger;
2068 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2069 rtd->ops.close = dpcm_fe_dai_close;
2070 rtd->ops.pointer = soc_pcm_pointer;
2071 rtd->ops.ioctl = soc_pcm_ioctl;
2072 } else {
2073 rtd->ops.open = soc_pcm_open;
2074 rtd->ops.hw_params = soc_pcm_hw_params;
2075 rtd->ops.prepare = soc_pcm_prepare;
2076 rtd->ops.trigger = soc_pcm_trigger;
2077 rtd->ops.hw_free = soc_pcm_hw_free;
2078 rtd->ops.close = soc_pcm_close;
2079 rtd->ops.pointer = soc_pcm_pointer;
2080 rtd->ops.ioctl = soc_pcm_ioctl;
2083 if (platform->driver->ops) {
2084 rtd->ops.ack = platform->driver->ops->ack;
2085 rtd->ops.copy = platform->driver->ops->copy;
2086 rtd->ops.silence = platform->driver->ops->silence;
2087 rtd->ops.page = platform->driver->ops->page;
2088 rtd->ops.mmap = platform->driver->ops->mmap;
2091 if (playback)
2092 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2094 if (capture)
2095 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2097 if (platform->driver->pcm_new) {
2098 ret = platform->driver->pcm_new(rtd);
2099 if (ret < 0) {
2100 dev_err(platform->dev, "pcm constructor failed\n");
2101 return ret;
2105 pcm->private_free = platform->driver->pcm_free;
2106 out:
2107 dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
2108 cpu_dai->name);
2109 return ret;
2112 /* is the current PCM operation for this FE ? */
2113 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2115 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2116 return 1;
2117 return 0;
2119 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2121 /* is the current PCM operation for this BE ? */
2122 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2123 struct snd_soc_pcm_runtime *be, int stream)
2125 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2126 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2127 be->dpcm[stream].runtime_update))
2128 return 1;
2129 return 0;
2131 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2133 /* get the substream for this BE */
2134 struct snd_pcm_substream *
2135 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2137 return be->pcm->streams[stream].substream;
2139 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2141 /* get the BE runtime state */
2142 enum snd_soc_dpcm_state
2143 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2145 return be->dpcm[stream].state;
2147 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2149 /* set the BE runtime state */
2150 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2151 int stream, enum snd_soc_dpcm_state state)
2153 be->dpcm[stream].state = state;
2155 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2158 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2159 * are not running, paused or suspended for the specified stream direction.
2161 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2162 struct snd_soc_pcm_runtime *be, int stream)
2164 struct snd_soc_dpcm *dpcm;
2165 int state;
2167 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2169 if (dpcm->fe == fe)
2170 continue;
2172 state = dpcm->fe->dpcm[stream].state;
2173 if (state == SND_SOC_DPCM_STATE_START ||
2174 state == SND_SOC_DPCM_STATE_PAUSED ||
2175 state == SND_SOC_DPCM_STATE_SUSPEND)
2176 return 0;
2179 /* it's safe to free/stop this BE DAI */
2180 return 1;
2182 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2185 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2186 * running, paused or suspended for the specified stream direction.
2188 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2189 struct snd_soc_pcm_runtime *be, int stream)
2191 struct snd_soc_dpcm *dpcm;
2192 int state;
2194 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2196 if (dpcm->fe == fe)
2197 continue;
2199 state = dpcm->fe->dpcm[stream].state;
2200 if (state == SND_SOC_DPCM_STATE_START ||
2201 state == SND_SOC_DPCM_STATE_PAUSED ||
2202 state == SND_SOC_DPCM_STATE_SUSPEND ||
2203 state == SND_SOC_DPCM_STATE_PREPARE)
2204 return 0;
2207 /* it's safe to change hw_params */
2208 return 1;
2210 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2212 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2213 int cmd, struct snd_soc_platform *platform)
2215 if (platform->driver->ops->trigger)
2216 return platform->driver->ops->trigger(substream, cmd);
2217 return 0;
2219 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2221 #ifdef CONFIG_DEBUG_FS
2222 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2224 switch (state) {
2225 case SND_SOC_DPCM_STATE_NEW:
2226 return "new";
2227 case SND_SOC_DPCM_STATE_OPEN:
2228 return "open";
2229 case SND_SOC_DPCM_STATE_HW_PARAMS:
2230 return "hw_params";
2231 case SND_SOC_DPCM_STATE_PREPARE:
2232 return "prepare";
2233 case SND_SOC_DPCM_STATE_START:
2234 return "start";
2235 case SND_SOC_DPCM_STATE_STOP:
2236 return "stop";
2237 case SND_SOC_DPCM_STATE_SUSPEND:
2238 return "suspend";
2239 case SND_SOC_DPCM_STATE_PAUSED:
2240 return "paused";
2241 case SND_SOC_DPCM_STATE_HW_FREE:
2242 return "hw_free";
2243 case SND_SOC_DPCM_STATE_CLOSE:
2244 return "close";
2247 return "unknown";
2250 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2251 int stream, char *buf, size_t size)
2253 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2254 struct snd_soc_dpcm *dpcm;
2255 ssize_t offset = 0;
2257 /* FE state */
2258 offset += snprintf(buf + offset, size - offset,
2259 "[%s - %s]\n", fe->dai_link->name,
2260 stream ? "Capture" : "Playback");
2262 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2263 dpcm_state_string(fe->dpcm[stream].state));
2265 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2266 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2267 offset += snprintf(buf + offset, size - offset,
2268 "Hardware Params: "
2269 "Format = %s, Channels = %d, Rate = %d\n",
2270 snd_pcm_format_name(params_format(params)),
2271 params_channels(params),
2272 params_rate(params));
2274 /* BEs state */
2275 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2277 if (list_empty(&fe->dpcm[stream].be_clients)) {
2278 offset += snprintf(buf + offset, size - offset,
2279 " No active DSP links\n");
2280 goto out;
2283 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2284 struct snd_soc_pcm_runtime *be = dpcm->be;
2285 params = &dpcm->hw_params;
2287 offset += snprintf(buf + offset, size - offset,
2288 "- %s\n", be->dai_link->name);
2290 offset += snprintf(buf + offset, size - offset,
2291 " State: %s\n",
2292 dpcm_state_string(be->dpcm[stream].state));
2294 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2295 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2296 offset += snprintf(buf + offset, size - offset,
2297 " Hardware Params: "
2298 "Format = %s, Channels = %d, Rate = %d\n",
2299 snd_pcm_format_name(params_format(params)),
2300 params_channels(params),
2301 params_rate(params));
2304 out:
2305 return offset;
2308 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2309 size_t count, loff_t *ppos)
2311 struct snd_soc_pcm_runtime *fe = file->private_data;
2312 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2313 char *buf;
2315 buf = kmalloc(out_count, GFP_KERNEL);
2316 if (!buf)
2317 return -ENOMEM;
2319 if (fe->cpu_dai->driver->playback.channels_min)
2320 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2321 buf + offset, out_count - offset);
2323 if (fe->cpu_dai->driver->capture.channels_min)
2324 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2325 buf + offset, out_count - offset);
2327 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2329 kfree(buf);
2330 return ret;
2333 static const struct file_operations dpcm_state_fops = {
2334 .open = simple_open,
2335 .read = dpcm_state_read_file,
2336 .llseek = default_llseek,
2339 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2341 if (!rtd->dai_link)
2342 return 0;
2344 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2345 rtd->card->debugfs_card_root);
2346 if (!rtd->debugfs_dpcm_root) {
2347 dev_dbg(rtd->dev,
2348 "ASoC: Failed to create dpcm debugfs directory %s\n",
2349 rtd->dai_link->name);
2350 return -EINVAL;
2353 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2354 rtd->debugfs_dpcm_root,
2355 rtd, &dpcm_state_fops);
2357 return 0;
2359 #endif