2 * soc-pcm.c -- ALSA SoC PCM
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
35 #define DPCM_MAX_BE_USERS 8
38 * snd_soc_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
;
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
,
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
);
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
;
87 if (!soc_dai
->driver
->symmetric_rates
&&
88 !rtd
->dai_link
->symmetric_rates
)
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.
96 dev_warn(soc_dai
->dev
,
97 "ASoC: Not enforcing symmetric_rates due to race\n");
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
);
107 dev_err(soc_dai
->dev
,
108 "ASoC: Unable to apply rate symmetry constraint: %d\n",
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
[] = {
125 static void soc_pcm_apply_msb(struct snd_pcm_substream
*substream
,
126 struct snd_soc_dai
*dai
)
130 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
131 bits
= dai
->driver
->playback
.sig_bits
;
133 bits
= dai
->driver
->capture
.sig_bits
;
138 for (i
= 0; i
< ARRAY_SIZE(sample_sizes
); i
++) {
139 if (bits
>= sample_sizes
[i
])
142 ret
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0,
143 sample_sizes
[i
], bits
);
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
;
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
);
199 dev_err(cpu_dai
->dev
, "ASoC: can't open interface"
200 " %s: %d\n", cpu_dai
->name
, ret
);
205 if (platform
->driver
->ops
&& platform
->driver
->ops
->open
) {
206 ret
= platform
->driver
->ops
->open(substream
);
208 dev_err(platform
->dev
, "ASoC: can't open platform"
209 " %s: %d\n", platform
->name
, ret
);
214 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->startup
) {
215 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
217 dev_err(codec_dai
->dev
, "ASoC: can't open codec"
218 " %s: %d\n", codec_dai
->name
, ret
);
223 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
224 ret
= rtd
->dai_link
->ops
->startup(substream
);
226 pr_err("ASoC: %s startup failed: %d\n",
227 rtd
->dai_link
->name
, ret
);
232 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
233 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
)
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
);
241 soc_pcm_init_runtime_hw(&runtime
->hw
, &codec_dai_drv
->capture
,
242 &cpu_dai_drv
->capture
);
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
);
252 if (!runtime
->hw
.formats
) {
253 printk(KERN_ERR
"ASoC: %s <-> %s No matching formats\n",
254 codec_dai
->name
, cpu_dai
->name
);
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
);
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
);
274 if (codec_dai
->active
) {
275 ret
= soc_pcm_apply_symmetry(substream
, codec_dai
);
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
);
289 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
290 cpu_dai
->playback_active
++;
291 codec_dai
->playback_active
++;
293 cpu_dai
->capture_active
++;
294 codec_dai
->capture_active
++;
298 rtd
->codec
->active
++;
299 mutex_unlock(&rtd
->pcm_mutex
);
303 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
304 rtd
->dai_link
->ops
->shutdown(substream
);
307 if (codec_dai
->driver
->ops
->shutdown
)
308 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
311 if (platform
->driver
->ops
&& platform
->driver
->ops
->close
)
312 platform
->driver
->ops
->close(substream
);
315 if (cpu_dai
->driver
->ops
->shutdown
)
316 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
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
);
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) {
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
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
--;
378 cpu_dai
->capture_active
--;
379 codec_dai
->capture_active
--;
386 /* clear the corresponding DAIs rate when inactive */
387 if (!cpu_dai
->active
)
390 if (!codec_dai
->active
)
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
);
419 /* start delayed pop wq here for playback streams */
421 queue_delayed_work(system_power_efficient_wq
,
423 msecs_to_jiffies(rtd
->pmdown_time
));
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
);
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
;
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
);
462 dev_err(rtd
->card
->dev
, "ASoC: machine prepare error:"
468 if (platform
->driver
->ops
&& platform
->driver
->ops
->prepare
) {
469 ret
= platform
->driver
->ops
->prepare(substream
);
471 dev_err(platform
->dev
, "ASoC: platform prepare error:"
477 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->prepare
) {
478 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
480 dev_err(codec_dai
->dev
, "ASoC: DAI prepare error: %d\n",
486 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->prepare
) {
487 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
489 dev_err(cpu_dai
->dev
, "ASoC: DAI prepare error: %d\n",
495 /* cancel any delayed stream shutdown that is pending */
496 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
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
);
508 mutex_unlock(&rtd
->pcm_mutex
);
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
;
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
);
531 dev_err(rtd
->card
->dev
, "ASoC: machine hw_params"
532 " failed: %d\n", ret
);
537 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_params
) {
538 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
540 dev_err(codec_dai
->dev
, "ASoC: can't set %s hw params:"
541 " %d\n", codec_dai
->name
, ret
);
546 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_params
) {
547 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
549 dev_err(cpu_dai
->dev
, "ASoC: %s hw params failed: %d\n",
555 if (platform
->driver
->ops
&& platform
->driver
->ops
->hw_params
) {
556 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
558 dev_err(platform
->dev
, "ASoC: %s hw params failed: %d\n",
559 platform
->name
, ret
);
564 /* store the rate for each DAIs */
565 cpu_dai
->rate
= params_rate(params
);
566 codec_dai
->rate
= params_rate(params
);
569 mutex_unlock(&rtd
->pcm_mutex
);
573 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_free
)
574 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
577 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_free
)
578 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
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
);
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 */
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
);
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
;
632 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->trigger
) {
633 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
638 if (platform
->driver
->ops
&& platform
->driver
->ops
->trigger
) {
639 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
644 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->trigger
) {
645 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
652 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream
*substream
,
655 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
656 struct snd_soc_platform
*platform
= rtd
->platform
;
657 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
658 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
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
);
668 if (platform
->driver
->ops
&& platform
->driver
->bespoke_trigger
) {
669 ret
= platform
->driver
->bespoke_trigger(substream
, cmd
);
674 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->bespoke_trigger
) {
675 ret
= cpu_dai
->driver
->ops
->bespoke_trigger(substream
, cmd
, cpu_dai
);
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
;
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
)
725 dpcm
= kzalloc(sizeof(struct snd_soc_dpcm
), GFP_KERNEL
);
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
);
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
)
758 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
760 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
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
;
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
)
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
);
798 list_del(&dpcm
->list_be
);
799 list_del(&dpcm
->list_fe
);
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
;
811 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
812 for (i
= 0; i
< card
->num_links
; i
++) {
815 if (!be
->dai_link
->no_pcm
)
818 if (be
->cpu_dai
->playback_widget
== widget
||
819 be
->codec_dai
->playback_widget
== widget
)
824 for (i
= 0; i
< card
->num_links
; i
++) {
827 if (!be
->dai_link
->no_pcm
)
830 if (be
->cpu_dai
->capture_widget
== widget
||
831 be
->codec_dai
->capture_widget
== widget
)
836 dev_err(card
->dev
, "ASoC: can't get %s BE for %s\n",
837 stream
? "capture" : "playback", widget
->name
);
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
;
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
;
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
)
864 for (i
= 0; i
< list
->num_widgets
; i
++) {
865 if (widget
== list
->widgets
[i
])
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
;
879 list
= kzalloc(sizeof(struct snd_soc_dapm_widget_list
) +
880 sizeof(struct snd_soc_dapm_widget
*), GFP_KERNEL
);
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");
894 static inline void dpcm_path_put(struct snd_soc_dapm_widget_list
**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
;
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
))
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
))
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
;
932 dev_dbg(fe
->dev
, "ASoC: found %d old BE paths for pruning\n", 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
;
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
:
955 /* is there a valid BE rtd for this widget */
956 be
= dpcm_get_be(card
, list
->widgets
[i
], stream
);
958 dev_err(fe
->dev
, "ASoC: no BE found for %s\n",
959 list
->widgets
[i
]->name
);
963 /* make sure BE is a real BE */
964 if (!be
->dai_link
->no_pcm
)
967 /* don't connect if FE is not running */
968 if (!fe
->dpcm
[stream
].runtime
)
971 /* newly connected FE and BE */
972 err
= dpcm_be_connect(fe
, be
, stream
);
974 dev_err(fe
->dev
, "ASoC: can't connect %s\n",
975 list
->widgets
[i
]->name
);
977 } else if (err
== 0) /* already connected */
981 be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
985 dev_dbg(fe
->dev
, "ASoC: found %d new BE paths\n", new);
990 * Find the corresponding BE DAIs that source or sink audio to this
993 static int dpcm_process_paths(struct snd_soc_pcm_runtime
*fe
,
994 int stream
, struct snd_soc_dapm_widget_list
**list
, int new)
997 return dpcm_add_paths(fe
, stream
, list
);
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
,
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)
1031 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
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
;
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");
1058 /* is this op for this BE ? */
1059 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
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)
1071 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1072 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
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
);
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
;
1092 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
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
))
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)
1116 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1119 soc_pcm_close(be_substream
);
1120 be_substream
->runtime
= NULL
;
1121 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
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
;
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
);
1161 dev_err(fe
->dev
,"ASoC: failed to start some BEs %d\n", ret
);
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
);
1170 dev_err(fe
->dev
,"ASoC: failed to start FE %d\n", ret
);
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
;
1183 dpcm_be_dai_startup_unwind(fe
, fe_substream
->stream
);
1185 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
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
))
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)
1212 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1213 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
))
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
;
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
;
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
))
1266 /* only free hw when no longer used - check all FEs */
1267 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
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
))
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
;
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
);
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
);
1315 static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime
*fe
, int stream
)
1317 struct snd_soc_dpcm
*dpcm
;
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
))
1330 /* only allow hw_params() if no connected FEs are running */
1331 if (!snd_soc_dpcm_can_be_params(fe
, be
, stream
))
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
))
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
,
1352 "ASoC: hw_params BE fixup failed %d\n",
1358 ret
= soc_pcm_hw_params(be_substream
, &dpcm
->hw_params
);
1360 dev_err(dpcm
->be
->dev
,
1361 "ASoC: hw_params BE failed %d\n", ret
);
1365 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
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
))
1379 /* only allow hw_free() if no connected FEs are running */
1380 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
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
))
1389 soc_pcm_hw_free(be_substream
);
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
);
1408 dev_err(fe
->dev
,"ASoC: hw_params BE failed %d\n", ret
);
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
);
1419 dev_err(fe
->dev
,"ASoC: hw_params FE failed %d\n", ret
);
1420 dpcm_be_dai_hw_free(fe
, stream
);
1422 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
1425 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1426 mutex_unlock(&fe
->card
->mutex
);
1430 static int dpcm_do_trigger(struct snd_soc_dpcm
*dpcm
,
1431 struct snd_pcm_substream
*substream
, int cmd
)
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
);
1440 dev_err(dpcm
->be
->dev
,"ASoC: trigger BE failed %d\n", ret
);
1445 static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime
*fe
, int stream
,
1448 struct snd_soc_dpcm
*dpcm
;
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
))
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
))
1467 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1471 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1473 case SNDRV_PCM_TRIGGER_RESUME
:
1474 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
1477 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1481 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1483 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1484 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
1487 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1491 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1493 case SNDRV_PCM_TRIGGER_STOP
:
1494 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1497 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1500 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1504 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
1506 case SNDRV_PCM_TRIGGER_SUSPEND
:
1507 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
)
1510 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1513 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1517 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_SUSPEND
;
1519 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1520 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1523 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1526 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1530 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
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
;
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
);
1556 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1560 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
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
);
1567 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
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
);
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
);
1584 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1589 dev_err(fe
->dev
, "ASoC: invalid trigger cmd %d for %s\n", cmd
,
1590 fe
->dai_link
->name
);
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
;
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
;
1609 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1613 static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime
*fe
, int stream
)
1615 struct snd_soc_dpcm
*dpcm
;
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
))
1628 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1629 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1632 dev_dbg(be
->dev
, "ASoC: prepare BE %s\n",
1633 dpcm
->fe
->dai_link
->name
);
1635 ret
= soc_pcm_prepare(be_substream
);
1637 dev_err(be
->dev
, "ASoC: backend prepare failed %d\n",
1642 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
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
);
1666 ret
= dpcm_be_dai_prepare(fe
, substream
->stream
);
1670 /* call prepare on the frontend */
1671 ret
= soc_pcm_prepare(substream
);
1673 dev_err(fe
->dev
,"ASoC: prepare FE %s failed\n",
1674 fe
->dai_link
->name
);
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
;
1683 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1684 mutex_unlock(&fe
->card
->mutex
);
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
];
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
);
1717 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
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
);
1724 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
1727 err
= dpcm_be_dai_hw_free(fe
, stream
);
1729 dev_err(fe
->dev
,"ASoC: hw_free FE failed %d\n", err
);
1731 err
= dpcm_be_dai_shutdown(fe
, stream
);
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
);
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
];
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
)
1757 /* startup must always be called for new BEs */
1758 ret
= dpcm_be_dai_startup(fe
, stream
);
1762 /* keep going if FE state is > open */
1763 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_OPEN
)
1766 ret
= dpcm_be_dai_hw_params(fe
, stream
);
1770 /* keep going if FE state is > hw_params */
1771 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_PARAMS
)
1775 ret
= dpcm_be_dai_prepare(fe
, stream
);
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
)
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
);
1794 dev_err(fe
->dev
,"ASoC: bespoke trigger FE failed %d\n", ret
);
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
);
1804 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1812 dpcm_be_dai_hw_free(fe
, stream
);
1814 dpcm_be_dai_shutdown(fe
, stream
);
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
;
1826 static int dpcm_run_new_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
1830 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1831 ret
= dpcm_run_update_startup(fe
, stream
);
1833 dev_err(fe
->dev
, "ASoC: failed to startup some BEs\n");
1834 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1839 static int dpcm_run_old_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
1843 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1844 ret
= dpcm_run_update_shutdown(fe
, stream
);
1846 dev_err(fe
->dev
, "ASoC: failed to shutdown some BEs\n");
1847 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1852 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
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
)
1868 /* only check active links */
1869 if (!fe
->cpu_dai
->active
)
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
)
1880 paths
= dpcm_path_get(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
);
1882 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
1883 fe
->dai_link
->name
, "playback");
1884 mutex_unlock(&card
->mutex
);
1888 /* update any new playback paths */
1889 new = dpcm_process_paths(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
, 1);
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);
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
);
1905 /* skip if FE doesn't have capture capability */
1906 if (!fe
->cpu_dai
->driver
->capture
.channels_min
)
1909 paths
= dpcm_path_get(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
);
1911 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
1912 fe
->dai_link
->name
, "capture");
1913 mutex_unlock(&card
->mutex
);
1917 /* update any new capture paths */
1918 new = dpcm_process_paths(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
, 1);
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);
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
);
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
)
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
);
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
;
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
);
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
);
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
);
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
;
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
)
2031 if (cpu_dai
->driver
->capture
.channels_min
)
2034 if (codec_dai
->driver
->playback
.channels_min
&&
2035 cpu_dai
->driver
->playback
.channels_min
)
2037 if (codec_dai
->driver
->capture
.channels_min
&&
2038 cpu_dai
->driver
->capture
.channels_min
)
2042 if (rtd
->dai_link
->playback_only
) {
2047 if (rtd
->dai_link
->capture_only
) {
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
);
2060 if (rtd
->dai_link
->dynamic
)
2061 snprintf(new_name
, sizeof(new_name
), "%s (*)",
2062 rtd
->dai_link
->stream_name
);
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
,
2071 dev_err(rtd
->card
->dev
, "ASoC: can't create pcm for %s\n",
2072 rtd
->dai_link
->name
);
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
);
2081 pcm
->private_data
= rtd
;
2083 if (rtd
->dai_link
->no_pcm
) {
2085 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->private_data
= rtd
;
2087 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
->private_data
= rtd
;
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
;
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
;
2121 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &rtd
->ops
);
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
);
2129 dev_err(platform
->dev
,
2130 "ASoC: pcm constructor failed: %d\n",
2136 pcm
->private_free
= platform
->driver
->pcm_free
;
2138 dev_info(rtd
->card
->dev
, "%s <-> %s mapping ok\n", codec_dai
->name
,
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
)
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
))
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
;
2198 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
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
)
2210 /* it's safe to free/stop this BE DAI */
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
;
2225 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
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
)
2238 /* it's safe to change hw_params */
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
);
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
)
2256 case SND_SOC_DPCM_STATE_NEW
:
2258 case SND_SOC_DPCM_STATE_OPEN
:
2260 case SND_SOC_DPCM_STATE_HW_PARAMS
:
2262 case SND_SOC_DPCM_STATE_PREPARE
:
2264 case SND_SOC_DPCM_STATE_START
:
2266 case SND_SOC_DPCM_STATE_STOP
:
2268 case SND_SOC_DPCM_STATE_SUSPEND
:
2270 case SND_SOC_DPCM_STATE_PAUSED
:
2272 case SND_SOC_DPCM_STATE_HW_FREE
:
2274 case SND_SOC_DPCM_STATE_CLOSE
:
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
;
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
,
2300 "Format = %s, Channels = %d, Rate = %d\n",
2301 snd_pcm_format_name(params_format(params
)),
2302 params_channels(params
),
2303 params_rate(params
));
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");
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
,
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
));
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;
2346 buf
= kmalloc(out_count
, GFP_KERNEL
);
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
);
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
)
2375 rtd
->debugfs_dpcm_root
= debugfs_create_dir(rtd
->dai_link
->name
,
2376 rtd
->card
->debugfs_card_root
);
2377 if (!rtd
->debugfs_dpcm_root
) {
2379 "ASoC: Failed to create dpcm debugfs directory %s\n",
2380 rtd
->dai_link
->name
);
2384 rtd
->debugfs_dpcm_state
= debugfs_create_file("state", 0444,
2385 rtd
->debugfs_dpcm_root
,
2386 rtd
, &dpcm_state_fops
);