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 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
->rate
&& (soc_dai
->driver
->symmetric_rates
||
88 rtd
->dai_link
->symmetric_rates
)) {
89 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %dHz rate\n",
92 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
93 SNDRV_PCM_HW_PARAM_RATE
,
94 soc_dai
->rate
, soc_dai
->rate
);
97 "ASoC: Unable to apply rate constraint: %d\n",
103 if (soc_dai
->channels
&& (soc_dai
->driver
->symmetric_channels
||
104 rtd
->dai_link
->symmetric_channels
)) {
105 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %d channel(s)\n",
108 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
109 SNDRV_PCM_HW_PARAM_CHANNELS
,
113 dev_err(soc_dai
->dev
,
114 "ASoC: Unable to apply channel symmetry constraint: %d\n",
120 if (soc_dai
->sample_bits
&& (soc_dai
->driver
->symmetric_samplebits
||
121 rtd
->dai_link
->symmetric_samplebits
)) {
122 dev_dbg(soc_dai
->dev
, "ASoC: Symmetry forces %d sample bits\n",
123 soc_dai
->sample_bits
);
125 ret
= snd_pcm_hw_constraint_minmax(substream
->runtime
,
126 SNDRV_PCM_HW_PARAM_SAMPLE_BITS
,
127 soc_dai
->sample_bits
,
128 soc_dai
->sample_bits
);
130 dev_err(soc_dai
->dev
,
131 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
140 static int soc_pcm_params_symmetry(struct snd_pcm_substream
*substream
,
141 struct snd_pcm_hw_params
*params
)
143 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
144 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
145 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
146 unsigned int rate
, channels
, sample_bits
, symmetry
;
148 rate
= params_rate(params
);
149 channels
= params_channels(params
);
150 sample_bits
= snd_pcm_format_physical_width(params_format(params
));
152 /* reject unmatched parameters when applying symmetry */
153 symmetry
= cpu_dai
->driver
->symmetric_rates
||
154 codec_dai
->driver
->symmetric_rates
||
155 rtd
->dai_link
->symmetric_rates
;
156 if (symmetry
&& cpu_dai
->rate
&& cpu_dai
->rate
!= rate
) {
157 dev_err(rtd
->dev
, "ASoC: unmatched rate symmetry: %d - %d\n",
158 cpu_dai
->rate
, rate
);
162 symmetry
= cpu_dai
->driver
->symmetric_channels
||
163 codec_dai
->driver
->symmetric_channels
||
164 rtd
->dai_link
->symmetric_channels
;
165 if (symmetry
&& cpu_dai
->channels
&& cpu_dai
->channels
!= channels
) {
166 dev_err(rtd
->dev
, "ASoC: unmatched channel symmetry: %d - %d\n",
167 cpu_dai
->channels
, channels
);
171 symmetry
= cpu_dai
->driver
->symmetric_samplebits
||
172 codec_dai
->driver
->symmetric_samplebits
||
173 rtd
->dai_link
->symmetric_samplebits
;
174 if (symmetry
&& cpu_dai
->sample_bits
&& cpu_dai
->sample_bits
!= sample_bits
) {
175 dev_err(rtd
->dev
, "ASoC: unmatched sample bits symmetry: %d - %d\n",
176 cpu_dai
->sample_bits
, sample_bits
);
183 static bool soc_pcm_has_symmetry(struct snd_pcm_substream
*substream
)
185 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
186 struct snd_soc_dai_driver
*cpu_driver
= rtd
->cpu_dai
->driver
;
187 struct snd_soc_dai_driver
*codec_driver
= rtd
->codec_dai
->driver
;
188 struct snd_soc_dai_link
*link
= rtd
->dai_link
;
190 return cpu_driver
->symmetric_rates
|| codec_driver
->symmetric_rates
||
191 link
->symmetric_rates
|| cpu_driver
->symmetric_channels
||
192 codec_driver
->symmetric_channels
|| link
->symmetric_channels
||
193 cpu_driver
->symmetric_samplebits
||
194 codec_driver
->symmetric_samplebits
||
195 link
->symmetric_samplebits
;
199 * List of sample sizes that might go over the bus for parameter
200 * application. There ought to be a wildcard sample size for things
201 * like the DAC/ADC resolution to use but there isn't right now.
203 static int sample_sizes
[] = {
207 static void soc_pcm_apply_msb(struct snd_pcm_substream
*substream
,
208 struct snd_soc_dai
*dai
)
212 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
213 bits
= dai
->driver
->playback
.sig_bits
;
215 bits
= dai
->driver
->capture
.sig_bits
;
220 for (i
= 0; i
< ARRAY_SIZE(sample_sizes
); i
++) {
221 if (bits
>= sample_sizes
[i
])
224 ret
= snd_pcm_hw_constraint_msbits(substream
->runtime
, 0,
225 sample_sizes
[i
], bits
);
228 "ASoC: Failed to set MSB %d/%d: %d\n",
229 bits
, sample_sizes
[i
], ret
);
233 static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime
*runtime
,
234 struct snd_soc_pcm_stream
*codec_stream
,
235 struct snd_soc_pcm_stream
*cpu_stream
)
237 struct snd_pcm_hardware
*hw
= &runtime
->hw
;
239 hw
->channels_min
= max(codec_stream
->channels_min
,
240 cpu_stream
->channels_min
);
241 hw
->channels_max
= min(codec_stream
->channels_max
,
242 cpu_stream
->channels_max
);
244 hw
->formats
&= codec_stream
->formats
& cpu_stream
->formats
;
246 hw
->formats
= codec_stream
->formats
& cpu_stream
->formats
;
247 hw
->rates
= snd_pcm_rate_mask_intersect(codec_stream
->rates
,
251 hw
->rate_max
= UINT_MAX
;
253 snd_pcm_limit_hw_rates(runtime
);
255 hw
->rate_min
= max(hw
->rate_min
, cpu_stream
->rate_min
);
256 hw
->rate_min
= max(hw
->rate_min
, codec_stream
->rate_min
);
257 hw
->rate_max
= min_not_zero(hw
->rate_max
, cpu_stream
->rate_max
);
258 hw
->rate_max
= min_not_zero(hw
->rate_max
, codec_stream
->rate_max
);
262 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
263 * then initialized and any private data can be allocated. This also calls
264 * startup for the cpu DAI, platform, machine and codec DAI.
266 static int soc_pcm_open(struct snd_pcm_substream
*substream
)
268 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
269 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
270 struct snd_soc_platform
*platform
= rtd
->platform
;
271 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
272 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
273 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
274 struct snd_soc_dai_driver
*codec_dai_drv
= codec_dai
->driver
;
277 pinctrl_pm_select_default_state(cpu_dai
->dev
);
278 pinctrl_pm_select_default_state(codec_dai
->dev
);
279 pm_runtime_get_sync(cpu_dai
->dev
);
280 pm_runtime_get_sync(codec_dai
->dev
);
281 pm_runtime_get_sync(platform
->dev
);
283 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
285 /* startup the audio subsystem */
286 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->startup
) {
287 ret
= cpu_dai
->driver
->ops
->startup(substream
, cpu_dai
);
289 dev_err(cpu_dai
->dev
, "ASoC: can't open interface"
290 " %s: %d\n", cpu_dai
->name
, ret
);
295 if (platform
->driver
->ops
&& platform
->driver
->ops
->open
) {
296 ret
= platform
->driver
->ops
->open(substream
);
298 dev_err(platform
->dev
, "ASoC: can't open platform"
299 " %s: %d\n", platform
->name
, ret
);
304 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->startup
) {
305 ret
= codec_dai
->driver
->ops
->startup(substream
, codec_dai
);
307 dev_err(codec_dai
->dev
, "ASoC: can't open codec"
308 " %s: %d\n", codec_dai
->name
, ret
);
313 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->startup
) {
314 ret
= rtd
->dai_link
->ops
->startup(substream
);
316 pr_err("ASoC: %s startup failed: %d\n",
317 rtd
->dai_link
->name
, ret
);
322 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
323 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
)
326 /* Check that the codec and cpu DAIs are compatible */
327 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
328 soc_pcm_init_runtime_hw(runtime
, &codec_dai_drv
->playback
,
329 &cpu_dai_drv
->playback
);
331 soc_pcm_init_runtime_hw(runtime
, &codec_dai_drv
->capture
,
332 &cpu_dai_drv
->capture
);
335 if (soc_pcm_has_symmetry(substream
))
336 runtime
->hw
.info
|= SNDRV_PCM_INFO_JOINT_DUPLEX
;
339 if (!runtime
->hw
.rates
) {
340 printk(KERN_ERR
"ASoC: %s <-> %s No matching rates\n",
341 codec_dai
->name
, cpu_dai
->name
);
344 if (!runtime
->hw
.formats
) {
345 printk(KERN_ERR
"ASoC: %s <-> %s No matching formats\n",
346 codec_dai
->name
, cpu_dai
->name
);
349 if (!runtime
->hw
.channels_min
|| !runtime
->hw
.channels_max
||
350 runtime
->hw
.channels_min
> runtime
->hw
.channels_max
) {
351 printk(KERN_ERR
"ASoC: %s <-> %s No matching channels\n",
352 codec_dai
->name
, cpu_dai
->name
);
356 soc_pcm_apply_msb(substream
, codec_dai
);
357 soc_pcm_apply_msb(substream
, cpu_dai
);
359 /* Symmetry only applies if we've already got an active stream. */
360 if (cpu_dai
->active
) {
361 ret
= soc_pcm_apply_symmetry(substream
, cpu_dai
);
366 if (codec_dai
->active
) {
367 ret
= soc_pcm_apply_symmetry(substream
, codec_dai
);
372 pr_debug("ASoC: %s <-> %s info:\n",
373 codec_dai
->name
, cpu_dai
->name
);
374 pr_debug("ASoC: rate mask 0x%x\n", runtime
->hw
.rates
);
375 pr_debug("ASoC: min ch %d max ch %d\n", runtime
->hw
.channels_min
,
376 runtime
->hw
.channels_max
);
377 pr_debug("ASoC: min rate %d max rate %d\n", runtime
->hw
.rate_min
,
378 runtime
->hw
.rate_max
);
381 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
382 cpu_dai
->playback_active
++;
383 codec_dai
->playback_active
++;
385 cpu_dai
->capture_active
++;
386 codec_dai
->capture_active
++;
390 rtd
->codec
->active
++;
391 mutex_unlock(&rtd
->pcm_mutex
);
395 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
396 rtd
->dai_link
->ops
->shutdown(substream
);
399 if (codec_dai
->driver
->ops
->shutdown
)
400 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
403 if (platform
->driver
->ops
&& platform
->driver
->ops
->close
)
404 platform
->driver
->ops
->close(substream
);
407 if (cpu_dai
->driver
->ops
->shutdown
)
408 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
410 mutex_unlock(&rtd
->pcm_mutex
);
412 pm_runtime_put(platform
->dev
);
413 pm_runtime_put(codec_dai
->dev
);
414 pm_runtime_put(cpu_dai
->dev
);
415 if (!codec_dai
->active
)
416 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
417 if (!cpu_dai
->active
)
418 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
424 * Power down the audio subsystem pmdown_time msecs after close is called.
425 * This is to ensure there are no pops or clicks in between any music tracks
426 * due to DAPM power cycling.
428 static void close_delayed_work(struct work_struct
*work
)
430 struct snd_soc_pcm_runtime
*rtd
=
431 container_of(work
, struct snd_soc_pcm_runtime
, delayed_work
.work
);
432 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
434 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
436 dev_dbg(rtd
->dev
, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
437 codec_dai
->driver
->playback
.stream_name
,
438 codec_dai
->playback_active
? "active" : "inactive",
439 rtd
->pop_wait
? "yes" : "no");
441 /* are we waiting on this codec DAI stream */
442 if (rtd
->pop_wait
== 1) {
444 snd_soc_dapm_stream_event(rtd
, SNDRV_PCM_STREAM_PLAYBACK
,
445 SND_SOC_DAPM_STREAM_STOP
);
448 mutex_unlock(&rtd
->pcm_mutex
);
452 * Called by ALSA when a PCM substream is closed. Private data can be
453 * freed here. The cpu DAI, codec DAI, machine and platform are also
456 static int soc_pcm_close(struct snd_pcm_substream
*substream
)
458 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
459 struct snd_soc_platform
*platform
= rtd
->platform
;
460 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
461 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
462 struct snd_soc_codec
*codec
= rtd
->codec
;
464 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
466 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
467 cpu_dai
->playback_active
--;
468 codec_dai
->playback_active
--;
470 cpu_dai
->capture_active
--;
471 codec_dai
->capture_active
--;
478 /* clear the corresponding DAIs rate when inactive */
479 if (!cpu_dai
->active
)
482 if (!codec_dai
->active
)
485 if (cpu_dai
->driver
->ops
->shutdown
)
486 cpu_dai
->driver
->ops
->shutdown(substream
, cpu_dai
);
488 if (codec_dai
->driver
->ops
->shutdown
)
489 codec_dai
->driver
->ops
->shutdown(substream
, codec_dai
);
491 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->shutdown
)
492 rtd
->dai_link
->ops
->shutdown(substream
);
494 if (platform
->driver
->ops
&& platform
->driver
->ops
->close
)
495 platform
->driver
->ops
->close(substream
);
496 cpu_dai
->runtime
= NULL
;
498 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
499 if (!rtd
->pmdown_time
|| codec
->ignore_pmdown_time
||
500 rtd
->dai_link
->ignore_pmdown_time
) {
501 /* powered down playback stream now */
502 snd_soc_dapm_stream_event(rtd
,
503 SNDRV_PCM_STREAM_PLAYBACK
,
504 SND_SOC_DAPM_STREAM_STOP
);
506 /* start delayed pop wq here for playback streams */
508 queue_delayed_work(system_power_efficient_wq
,
510 msecs_to_jiffies(rtd
->pmdown_time
));
513 /* capture streams can be powered down now */
514 snd_soc_dapm_stream_event(rtd
, SNDRV_PCM_STREAM_CAPTURE
,
515 SND_SOC_DAPM_STREAM_STOP
);
518 mutex_unlock(&rtd
->pcm_mutex
);
520 pm_runtime_put(platform
->dev
);
521 pm_runtime_put(codec_dai
->dev
);
522 pm_runtime_put(cpu_dai
->dev
);
523 if (!codec_dai
->active
)
524 pinctrl_pm_select_sleep_state(codec_dai
->dev
);
525 if (!cpu_dai
->active
)
526 pinctrl_pm_select_sleep_state(cpu_dai
->dev
);
532 * Called by ALSA when the PCM substream is prepared, can set format, sample
533 * rate, etc. This function is non atomic and can be called multiple times,
534 * it can refer to the runtime info.
536 static int soc_pcm_prepare(struct snd_pcm_substream
*substream
)
538 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
539 struct snd_soc_platform
*platform
= rtd
->platform
;
540 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
541 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
544 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
546 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->prepare
) {
547 ret
= rtd
->dai_link
->ops
->prepare(substream
);
549 dev_err(rtd
->card
->dev
, "ASoC: machine prepare error:"
555 if (platform
->driver
->ops
&& platform
->driver
->ops
->prepare
) {
556 ret
= platform
->driver
->ops
->prepare(substream
);
558 dev_err(platform
->dev
, "ASoC: platform prepare error:"
564 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->prepare
) {
565 ret
= codec_dai
->driver
->ops
->prepare(substream
, codec_dai
);
567 dev_err(codec_dai
->dev
, "ASoC: DAI prepare error: %d\n",
573 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->prepare
) {
574 ret
= cpu_dai
->driver
->ops
->prepare(substream
, cpu_dai
);
576 dev_err(cpu_dai
->dev
, "ASoC: DAI prepare error: %d\n",
582 /* cancel any delayed stream shutdown that is pending */
583 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&&
586 cancel_delayed_work(&rtd
->delayed_work
);
589 snd_soc_dapm_stream_event(rtd
, substream
->stream
,
590 SND_SOC_DAPM_STREAM_START
);
592 snd_soc_dai_digital_mute(codec_dai
, 0, substream
->stream
);
595 mutex_unlock(&rtd
->pcm_mutex
);
600 * Called by ALSA when the hardware params are set by application. This
601 * function can also be called multiple times and can allocate buffers
602 * (using snd_pcm_lib_* ). It's non-atomic.
604 static int soc_pcm_hw_params(struct snd_pcm_substream
*substream
,
605 struct snd_pcm_hw_params
*params
)
607 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
608 struct snd_soc_platform
*platform
= rtd
->platform
;
609 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
610 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
613 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
615 ret
= soc_pcm_params_symmetry(substream
, params
);
619 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_params
) {
620 ret
= rtd
->dai_link
->ops
->hw_params(substream
, params
);
622 dev_err(rtd
->card
->dev
, "ASoC: machine hw_params"
623 " failed: %d\n", ret
);
628 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_params
) {
629 ret
= codec_dai
->driver
->ops
->hw_params(substream
, params
, codec_dai
);
631 dev_err(codec_dai
->dev
, "ASoC: can't set %s hw params:"
632 " %d\n", codec_dai
->name
, ret
);
637 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_params
) {
638 ret
= cpu_dai
->driver
->ops
->hw_params(substream
, params
, cpu_dai
);
640 dev_err(cpu_dai
->dev
, "ASoC: %s hw params failed: %d\n",
646 if (platform
->driver
->ops
&& platform
->driver
->ops
->hw_params
) {
647 ret
= platform
->driver
->ops
->hw_params(substream
, params
);
649 dev_err(platform
->dev
, "ASoC: %s hw params failed: %d\n",
650 platform
->name
, ret
);
655 /* store the parameters for each DAIs */
656 cpu_dai
->rate
= params_rate(params
);
657 cpu_dai
->channels
= params_channels(params
);
658 cpu_dai
->sample_bits
=
659 snd_pcm_format_physical_width(params_format(params
));
661 codec_dai
->rate
= params_rate(params
);
662 codec_dai
->channels
= params_channels(params
);
663 codec_dai
->sample_bits
=
664 snd_pcm_format_physical_width(params_format(params
));
667 mutex_unlock(&rtd
->pcm_mutex
);
671 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_free
)
672 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
675 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_free
)
676 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
679 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
680 rtd
->dai_link
->ops
->hw_free(substream
);
682 mutex_unlock(&rtd
->pcm_mutex
);
687 * Frees resources allocated by hw_params, can be called multiple times
689 static int soc_pcm_hw_free(struct snd_pcm_substream
*substream
)
691 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
692 struct snd_soc_platform
*platform
= rtd
->platform
;
693 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
694 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
695 bool playback
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
697 mutex_lock_nested(&rtd
->pcm_mutex
, rtd
->pcm_subclass
);
699 /* clear the corresponding DAIs parameters when going to be inactive */
700 if (cpu_dai
->active
== 1) {
702 cpu_dai
->channels
= 0;
703 cpu_dai
->sample_bits
= 0;
706 if (codec_dai
->active
== 1) {
708 codec_dai
->channels
= 0;
709 codec_dai
->sample_bits
= 0;
712 /* apply codec digital mute */
713 if ((playback
&& codec_dai
->playback_active
== 1) ||
714 (!playback
&& codec_dai
->capture_active
== 1))
715 snd_soc_dai_digital_mute(codec_dai
, 1, substream
->stream
);
717 /* free any machine hw params */
718 if (rtd
->dai_link
->ops
&& rtd
->dai_link
->ops
->hw_free
)
719 rtd
->dai_link
->ops
->hw_free(substream
);
721 /* free any DMA resources */
722 if (platform
->driver
->ops
&& platform
->driver
->ops
->hw_free
)
723 platform
->driver
->ops
->hw_free(substream
);
725 /* now free hw params for the DAIs */
726 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->hw_free
)
727 codec_dai
->driver
->ops
->hw_free(substream
, codec_dai
);
729 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->hw_free
)
730 cpu_dai
->driver
->ops
->hw_free(substream
, cpu_dai
);
732 mutex_unlock(&rtd
->pcm_mutex
);
736 static int soc_pcm_trigger(struct snd_pcm_substream
*substream
, int cmd
)
738 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
739 struct snd_soc_platform
*platform
= rtd
->platform
;
740 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
741 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
744 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->trigger
) {
745 ret
= codec_dai
->driver
->ops
->trigger(substream
, cmd
, codec_dai
);
750 if (platform
->driver
->ops
&& platform
->driver
->ops
->trigger
) {
751 ret
= platform
->driver
->ops
->trigger(substream
, cmd
);
756 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->trigger
) {
757 ret
= cpu_dai
->driver
->ops
->trigger(substream
, cmd
, cpu_dai
);
764 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream
*substream
,
767 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
768 struct snd_soc_platform
*platform
= rtd
->platform
;
769 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
770 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
773 if (codec_dai
->driver
->ops
&&
774 codec_dai
->driver
->ops
->bespoke_trigger
) {
775 ret
= codec_dai
->driver
->ops
->bespoke_trigger(substream
, cmd
, codec_dai
);
780 if (platform
->driver
->bespoke_trigger
) {
781 ret
= platform
->driver
->bespoke_trigger(substream
, cmd
);
786 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->bespoke_trigger
) {
787 ret
= cpu_dai
->driver
->ops
->bespoke_trigger(substream
, cmd
, cpu_dai
);
794 * soc level wrapper for pointer callback
795 * If cpu_dai, codec_dai, platform driver has the delay callback, than
796 * the runtime->delay will be updated accordingly.
798 static snd_pcm_uframes_t
soc_pcm_pointer(struct snd_pcm_substream
*substream
)
800 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
801 struct snd_soc_platform
*platform
= rtd
->platform
;
802 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
803 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
804 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
805 snd_pcm_uframes_t offset
= 0;
806 snd_pcm_sframes_t delay
= 0;
808 if (platform
->driver
->ops
&& platform
->driver
->ops
->pointer
)
809 offset
= platform
->driver
->ops
->pointer(substream
);
811 if (cpu_dai
->driver
->ops
&& cpu_dai
->driver
->ops
->delay
)
812 delay
+= cpu_dai
->driver
->ops
->delay(substream
, cpu_dai
);
814 if (codec_dai
->driver
->ops
&& codec_dai
->driver
->ops
->delay
)
815 delay
+= codec_dai
->driver
->ops
->delay(substream
, codec_dai
);
817 if (platform
->driver
->delay
)
818 delay
+= platform
->driver
->delay(substream
, codec_dai
);
820 runtime
->delay
= delay
;
825 /* connect a FE and BE */
826 static int dpcm_be_connect(struct snd_soc_pcm_runtime
*fe
,
827 struct snd_soc_pcm_runtime
*be
, int stream
)
829 struct snd_soc_dpcm
*dpcm
;
831 /* only add new dpcms */
832 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
833 if (dpcm
->be
== be
&& dpcm
->fe
== fe
)
837 dpcm
= kzalloc(sizeof(struct snd_soc_dpcm
), GFP_KERNEL
);
843 be
->dpcm
[stream
].runtime
= fe
->dpcm
[stream
].runtime
;
844 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_NEW
;
845 list_add(&dpcm
->list_be
, &fe
->dpcm
[stream
].be_clients
);
846 list_add(&dpcm
->list_fe
, &be
->dpcm
[stream
].fe_clients
);
848 dev_dbg(fe
->dev
, "connected new DPCM %s path %s %s %s\n",
849 stream
? "capture" : "playback", fe
->dai_link
->name
,
850 stream
? "<-" : "->", be
->dai_link
->name
);
852 #ifdef CONFIG_DEBUG_FS
853 dpcm
->debugfs_state
= debugfs_create_u32(be
->dai_link
->name
, 0644,
854 fe
->debugfs_dpcm_root
, &dpcm
->state
);
859 /* reparent a BE onto another FE */
860 static void dpcm_be_reparent(struct snd_soc_pcm_runtime
*fe
,
861 struct snd_soc_pcm_runtime
*be
, int stream
)
863 struct snd_soc_dpcm
*dpcm
;
864 struct snd_pcm_substream
*fe_substream
, *be_substream
;
866 /* reparent if BE is connected to other FEs */
867 if (!be
->dpcm
[stream
].users
)
870 be_substream
= snd_soc_dpcm_get_substream(be
, stream
);
872 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
876 dev_dbg(fe
->dev
, "reparent %s path %s %s %s\n",
877 stream
? "capture" : "playback",
878 dpcm
->fe
->dai_link
->name
,
879 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
881 fe_substream
= snd_soc_dpcm_get_substream(dpcm
->fe
, stream
);
882 be_substream
->runtime
= fe_substream
->runtime
;
887 /* disconnect a BE and FE */
888 void dpcm_be_disconnect(struct snd_soc_pcm_runtime
*fe
, int stream
)
890 struct snd_soc_dpcm
*dpcm
, *d
;
892 list_for_each_entry_safe(dpcm
, d
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
893 dev_dbg(fe
->dev
, "ASoC: BE %s disconnect check for %s\n",
894 stream
? "capture" : "playback",
895 dpcm
->be
->dai_link
->name
);
897 if (dpcm
->state
!= SND_SOC_DPCM_LINK_STATE_FREE
)
900 dev_dbg(fe
->dev
, "freed DSP %s path %s %s %s\n",
901 stream
? "capture" : "playback", fe
->dai_link
->name
,
902 stream
? "<-" : "->", dpcm
->be
->dai_link
->name
);
904 /* BEs still alive need new FE */
905 dpcm_be_reparent(fe
, dpcm
->be
, stream
);
907 #ifdef CONFIG_DEBUG_FS
908 debugfs_remove(dpcm
->debugfs_state
);
910 list_del(&dpcm
->list_be
);
911 list_del(&dpcm
->list_fe
);
916 /* get BE for DAI widget and stream */
917 static struct snd_soc_pcm_runtime
*dpcm_get_be(struct snd_soc_card
*card
,
918 struct snd_soc_dapm_widget
*widget
, int stream
)
920 struct snd_soc_pcm_runtime
*be
;
923 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
924 for (i
= 0; i
< card
->num_links
; i
++) {
927 if (!be
->dai_link
->no_pcm
)
930 if (be
->cpu_dai
->playback_widget
== widget
||
931 be
->codec_dai
->playback_widget
== widget
)
936 for (i
= 0; i
< card
->num_links
; i
++) {
939 if (!be
->dai_link
->no_pcm
)
942 if (be
->cpu_dai
->capture_widget
== widget
||
943 be
->codec_dai
->capture_widget
== widget
)
948 dev_err(card
->dev
, "ASoC: can't get %s BE for %s\n",
949 stream
? "capture" : "playback", widget
->name
);
953 static inline struct snd_soc_dapm_widget
*
954 rtd_get_cpu_widget(struct snd_soc_pcm_runtime
*rtd
, int stream
)
956 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
957 return rtd
->cpu_dai
->playback_widget
;
959 return rtd
->cpu_dai
->capture_widget
;
962 static inline struct snd_soc_dapm_widget
*
963 rtd_get_codec_widget(struct snd_soc_pcm_runtime
*rtd
, int stream
)
965 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
966 return rtd
->codec_dai
->playback_widget
;
968 return rtd
->codec_dai
->capture_widget
;
971 static int widget_in_list(struct snd_soc_dapm_widget_list
*list
,
972 struct snd_soc_dapm_widget
*widget
)
976 for (i
= 0; i
< list
->num_widgets
; i
++) {
977 if (widget
== list
->widgets
[i
])
984 int dpcm_path_get(struct snd_soc_pcm_runtime
*fe
,
985 int stream
, struct snd_soc_dapm_widget_list
**list_
)
987 struct snd_soc_dai
*cpu_dai
= fe
->cpu_dai
;
988 struct snd_soc_dapm_widget_list
*list
;
991 list
= kzalloc(sizeof(struct snd_soc_dapm_widget_list
) +
992 sizeof(struct snd_soc_dapm_widget
*), GFP_KERNEL
);
996 /* get number of valid DAI paths and their widgets */
997 paths
= snd_soc_dapm_dai_get_connected_widgets(cpu_dai
, stream
, &list
);
999 dev_dbg(fe
->dev
, "ASoC: found %d audio %s paths\n", paths
,
1000 stream
? "capture" : "playback");
1006 static int dpcm_prune_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1007 struct snd_soc_dapm_widget_list
**list_
)
1009 struct snd_soc_dpcm
*dpcm
;
1010 struct snd_soc_dapm_widget_list
*list
= *list_
;
1011 struct snd_soc_dapm_widget
*widget
;
1014 /* Destroy any old FE <--> BE connections */
1015 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1017 /* is there a valid CPU DAI widget for this BE */
1018 widget
= rtd_get_cpu_widget(dpcm
->be
, stream
);
1020 /* prune the BE if it's no longer in our active list */
1021 if (widget
&& widget_in_list(list
, widget
))
1024 /* is there a valid CODEC DAI widget for this BE */
1025 widget
= rtd_get_codec_widget(dpcm
->be
, stream
);
1027 /* prune the BE if it's no longer in our active list */
1028 if (widget
&& widget_in_list(list
, widget
))
1031 dev_dbg(fe
->dev
, "ASoC: pruning %s BE %s for %s\n",
1032 stream
? "capture" : "playback",
1033 dpcm
->be
->dai_link
->name
, fe
->dai_link
->name
);
1034 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
1035 dpcm
->be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1039 dev_dbg(fe
->dev
, "ASoC: found %d old BE paths for pruning\n", prune
);
1043 static int dpcm_add_paths(struct snd_soc_pcm_runtime
*fe
, int stream
,
1044 struct snd_soc_dapm_widget_list
**list_
)
1046 struct snd_soc_card
*card
= fe
->card
;
1047 struct snd_soc_dapm_widget_list
*list
= *list_
;
1048 struct snd_soc_pcm_runtime
*be
;
1049 int i
, new = 0, err
;
1051 /* Create any new FE <--> BE connections */
1052 for (i
= 0; i
< list
->num_widgets
; i
++) {
1054 switch (list
->widgets
[i
]->id
) {
1055 case snd_soc_dapm_dai_in
:
1056 case snd_soc_dapm_dai_out
:
1062 /* is there a valid BE rtd for this widget */
1063 be
= dpcm_get_be(card
, list
->widgets
[i
], stream
);
1065 dev_err(fe
->dev
, "ASoC: no BE found for %s\n",
1066 list
->widgets
[i
]->name
);
1070 /* make sure BE is a real BE */
1071 if (!be
->dai_link
->no_pcm
)
1074 /* don't connect if FE is not running */
1075 if (!fe
->dpcm
[stream
].runtime
&& !fe
->fe_compr
)
1078 /* newly connected FE and BE */
1079 err
= dpcm_be_connect(fe
, be
, stream
);
1081 dev_err(fe
->dev
, "ASoC: can't connect %s\n",
1082 list
->widgets
[i
]->name
);
1084 } else if (err
== 0) /* already connected */
1088 be
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1092 dev_dbg(fe
->dev
, "ASoC: found %d new BE paths\n", new);
1097 * Find the corresponding BE DAIs that source or sink audio to this
1100 int dpcm_process_paths(struct snd_soc_pcm_runtime
*fe
,
1101 int stream
, struct snd_soc_dapm_widget_list
**list
, int new)
1104 return dpcm_add_paths(fe
, stream
, list
);
1106 return dpcm_prune_paths(fe
, stream
, list
);
1109 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime
*fe
, int stream
)
1111 struct snd_soc_dpcm
*dpcm
;
1113 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
)
1114 dpcm
->be
->dpcm
[stream
].runtime_update
=
1115 SND_SOC_DPCM_UPDATE_NO
;
1118 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime
*fe
,
1121 struct snd_soc_dpcm
*dpcm
;
1123 /* disable any enabled and non active backends */
1124 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1126 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1127 struct snd_pcm_substream
*be_substream
=
1128 snd_soc_dpcm_get_substream(be
, stream
);
1130 if (be
->dpcm
[stream
].users
== 0)
1131 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1132 stream
? "capture" : "playback",
1133 be
->dpcm
[stream
].state
);
1135 if (--be
->dpcm
[stream
].users
!= 0)
1138 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1141 soc_pcm_close(be_substream
);
1142 be_substream
->runtime
= NULL
;
1143 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1147 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
1149 struct snd_soc_dpcm
*dpcm
;
1152 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1153 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1155 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1156 struct snd_pcm_substream
*be_substream
=
1157 snd_soc_dpcm_get_substream(be
, stream
);
1159 if (!be_substream
) {
1160 dev_err(be
->dev
, "ASoC: no backend %s stream\n",
1161 stream
? "capture" : "playback");
1165 /* is this op for this BE ? */
1166 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1169 /* first time the dpcm is open ? */
1170 if (be
->dpcm
[stream
].users
== DPCM_MAX_BE_USERS
)
1171 dev_err(be
->dev
, "ASoC: too many users %s at open %d\n",
1172 stream
? "capture" : "playback",
1173 be
->dpcm
[stream
].state
);
1175 if (be
->dpcm
[stream
].users
++ != 0)
1178 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_NEW
) &&
1179 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_CLOSE
))
1182 dev_dbg(be
->dev
, "ASoC: open %s BE %s\n",
1183 stream
? "capture" : "playback", be
->dai_link
->name
);
1185 be_substream
->runtime
= be
->dpcm
[stream
].runtime
;
1186 err
= soc_pcm_open(be_substream
);
1188 dev_err(be
->dev
, "ASoC: BE open failed %d\n", err
);
1189 be
->dpcm
[stream
].users
--;
1190 if (be
->dpcm
[stream
].users
< 0)
1191 dev_err(be
->dev
, "ASoC: no users %s at unwind %d\n",
1192 stream
? "capture" : "playback",
1193 be
->dpcm
[stream
].state
);
1195 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1199 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1206 /* disable any enabled and non active backends */
1207 list_for_each_entry_continue_reverse(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1208 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1209 struct snd_pcm_substream
*be_substream
=
1210 snd_soc_dpcm_get_substream(be
, stream
);
1212 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1215 if (be
->dpcm
[stream
].users
== 0)
1216 dev_err(be
->dev
, "ASoC: no users %s at close %d\n",
1217 stream
? "capture" : "playback",
1218 be
->dpcm
[stream
].state
);
1220 if (--be
->dpcm
[stream
].users
!= 0)
1223 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
)
1226 soc_pcm_close(be_substream
);
1227 be_substream
->runtime
= NULL
;
1228 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1234 static void dpcm_init_runtime_hw(struct snd_pcm_runtime
*runtime
,
1235 struct snd_soc_pcm_stream
*stream
)
1237 runtime
->hw
.rate_min
= stream
->rate_min
;
1238 runtime
->hw
.rate_max
= stream
->rate_max
;
1239 runtime
->hw
.channels_min
= stream
->channels_min
;
1240 runtime
->hw
.channels_max
= stream
->channels_max
;
1241 if (runtime
->hw
.formats
)
1242 runtime
->hw
.formats
&= stream
->formats
;
1244 runtime
->hw
.formats
= stream
->formats
;
1245 runtime
->hw
.rates
= stream
->rates
;
1248 static void dpcm_set_fe_runtime(struct snd_pcm_substream
*substream
)
1250 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1251 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1252 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
1253 struct snd_soc_dai_driver
*cpu_dai_drv
= cpu_dai
->driver
;
1255 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1256 dpcm_init_runtime_hw(runtime
, &cpu_dai_drv
->playback
);
1258 dpcm_init_runtime_hw(runtime
, &cpu_dai_drv
->capture
);
1261 static int dpcm_fe_dai_startup(struct snd_pcm_substream
*fe_substream
)
1263 struct snd_soc_pcm_runtime
*fe
= fe_substream
->private_data
;
1264 struct snd_pcm_runtime
*runtime
= fe_substream
->runtime
;
1265 int stream
= fe_substream
->stream
, ret
= 0;
1267 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
1269 ret
= dpcm_be_dai_startup(fe
, fe_substream
->stream
);
1271 dev_err(fe
->dev
,"ASoC: failed to start some BEs %d\n", ret
);
1275 dev_dbg(fe
->dev
, "ASoC: open FE %s\n", fe
->dai_link
->name
);
1277 /* start the DAI frontend */
1278 ret
= soc_pcm_open(fe_substream
);
1280 dev_err(fe
->dev
,"ASoC: failed to start FE %d\n", ret
);
1284 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_OPEN
;
1286 dpcm_set_fe_runtime(fe_substream
);
1287 snd_pcm_limit_hw_rates(runtime
);
1289 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1293 dpcm_be_dai_startup_unwind(fe
, fe_substream
->stream
);
1295 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1299 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
1301 struct snd_soc_dpcm
*dpcm
;
1303 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1304 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1306 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1307 struct snd_pcm_substream
*be_substream
=
1308 snd_soc_dpcm_get_substream(be
, stream
);
1310 /* is this op for this BE ? */
1311 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1314 if (be
->dpcm
[stream
].users
== 0)
1315 dev_err(be
->dev
, "ASoC: no users %s at close - state %d\n",
1316 stream
? "capture" : "playback",
1317 be
->dpcm
[stream
].state
);
1319 if (--be
->dpcm
[stream
].users
!= 0)
1322 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1323 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
))
1326 dev_dbg(be
->dev
, "ASoC: close BE %s\n",
1327 dpcm
->fe
->dai_link
->name
);
1329 soc_pcm_close(be_substream
);
1330 be_substream
->runtime
= NULL
;
1332 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1337 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream
*substream
)
1339 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1340 int stream
= substream
->stream
;
1342 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
1344 /* shutdown the BEs */
1345 dpcm_be_dai_shutdown(fe
, substream
->stream
);
1347 dev_dbg(fe
->dev
, "ASoC: close FE %s\n", fe
->dai_link
->name
);
1349 /* now shutdown the frontend */
1350 soc_pcm_close(substream
);
1352 /* run the stream event for each BE */
1353 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_STOP
);
1355 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_CLOSE
;
1356 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1360 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime
*fe
, int stream
)
1362 struct snd_soc_dpcm
*dpcm
;
1364 /* only hw_params backends that are either sinks or sources
1365 * to this frontend DAI */
1366 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1368 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1369 struct snd_pcm_substream
*be_substream
=
1370 snd_soc_dpcm_get_substream(be
, stream
);
1372 /* is this op for this BE ? */
1373 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1376 /* only free hw when no longer used - check all FEs */
1377 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1380 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1381 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
1382 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1383 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
) &&
1384 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1387 dev_dbg(be
->dev
, "ASoC: hw_free BE %s\n",
1388 dpcm
->fe
->dai_link
->name
);
1390 soc_pcm_hw_free(be_substream
);
1392 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1398 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream
*substream
)
1400 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1401 int err
, stream
= substream
->stream
;
1403 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1404 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
1406 dev_dbg(fe
->dev
, "ASoC: hw_free FE %s\n", fe
->dai_link
->name
);
1408 /* call hw_free on the frontend */
1409 err
= soc_pcm_hw_free(substream
);
1411 dev_err(fe
->dev
,"ASoC: hw_free FE %s failed\n",
1412 fe
->dai_link
->name
);
1414 /* only hw_params backends that are either sinks or sources
1415 * to this frontend DAI */
1416 err
= dpcm_be_dai_hw_free(fe
, stream
);
1418 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_FREE
;
1419 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1421 mutex_unlock(&fe
->card
->mutex
);
1425 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime
*fe
, int stream
)
1427 struct snd_soc_dpcm
*dpcm
;
1430 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1432 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1433 struct snd_pcm_substream
*be_substream
=
1434 snd_soc_dpcm_get_substream(be
, stream
);
1436 /* is this op for this BE ? */
1437 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1440 /* only allow hw_params() if no connected FEs are running */
1441 if (!snd_soc_dpcm_can_be_params(fe
, be
, stream
))
1444 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
1445 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1446 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
))
1449 dev_dbg(be
->dev
, "ASoC: hw_params BE %s\n",
1450 dpcm
->fe
->dai_link
->name
);
1452 /* copy params for each dpcm */
1453 memcpy(&dpcm
->hw_params
, &fe
->dpcm
[stream
].hw_params
,
1454 sizeof(struct snd_pcm_hw_params
));
1456 /* perform any hw_params fixups */
1457 if (be
->dai_link
->be_hw_params_fixup
) {
1458 ret
= be
->dai_link
->be_hw_params_fixup(be
,
1462 "ASoC: hw_params BE fixup failed %d\n",
1468 ret
= soc_pcm_hw_params(be_substream
, &dpcm
->hw_params
);
1470 dev_err(dpcm
->be
->dev
,
1471 "ASoC: hw_params BE failed %d\n", ret
);
1475 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
1480 /* disable any enabled and non active backends */
1481 list_for_each_entry_continue_reverse(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1482 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1483 struct snd_pcm_substream
*be_substream
=
1484 snd_soc_dpcm_get_substream(be
, stream
);
1486 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1489 /* only allow hw_free() if no connected FEs are running */
1490 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1493 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_OPEN
) &&
1494 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1495 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_FREE
) &&
1496 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1499 soc_pcm_hw_free(be_substream
);
1505 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream
*substream
,
1506 struct snd_pcm_hw_params
*params
)
1508 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1509 int ret
, stream
= substream
->stream
;
1511 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1512 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
1514 memcpy(&fe
->dpcm
[substream
->stream
].hw_params
, params
,
1515 sizeof(struct snd_pcm_hw_params
));
1516 ret
= dpcm_be_dai_hw_params(fe
, substream
->stream
);
1518 dev_err(fe
->dev
,"ASoC: hw_params BE failed %d\n", ret
);
1522 dev_dbg(fe
->dev
, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1523 fe
->dai_link
->name
, params_rate(params
),
1524 params_channels(params
), params_format(params
));
1526 /* call hw_params on the frontend */
1527 ret
= soc_pcm_hw_params(substream
, params
);
1529 dev_err(fe
->dev
,"ASoC: hw_params FE failed %d\n", ret
);
1530 dpcm_be_dai_hw_free(fe
, stream
);
1532 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_HW_PARAMS
;
1535 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1536 mutex_unlock(&fe
->card
->mutex
);
1540 static int dpcm_do_trigger(struct snd_soc_dpcm
*dpcm
,
1541 struct snd_pcm_substream
*substream
, int cmd
)
1545 dev_dbg(dpcm
->be
->dev
, "ASoC: trigger BE %s cmd %d\n",
1546 dpcm
->fe
->dai_link
->name
, cmd
);
1548 ret
= soc_pcm_trigger(substream
, cmd
);
1550 dev_err(dpcm
->be
->dev
,"ASoC: trigger BE failed %d\n", ret
);
1555 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime
*fe
, int stream
,
1558 struct snd_soc_dpcm
*dpcm
;
1561 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1563 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1564 struct snd_pcm_substream
*be_substream
=
1565 snd_soc_dpcm_get_substream(be
, stream
);
1567 /* is this op for this BE ? */
1568 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1572 case SNDRV_PCM_TRIGGER_START
:
1573 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PREPARE
) &&
1574 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1577 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1581 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1583 case SNDRV_PCM_TRIGGER_RESUME
:
1584 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_SUSPEND
))
1587 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1591 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1593 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1594 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_PAUSED
))
1597 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1601 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1603 case SNDRV_PCM_TRIGGER_STOP
:
1604 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1607 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1610 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1614 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
1616 case SNDRV_PCM_TRIGGER_SUSPEND
:
1617 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
)
1620 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1623 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1627 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_SUSPEND
;
1629 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1630 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1633 if (!snd_soc_dpcm_can_be_free_stop(fe
, be
, stream
))
1636 ret
= dpcm_do_trigger(dpcm
, be_substream
, cmd
);
1640 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PAUSED
;
1647 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger
);
1649 static int dpcm_fe_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
)
1651 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1652 int stream
= substream
->stream
, ret
;
1653 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
1655 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
1658 case SND_SOC_DPCM_TRIGGER_PRE
:
1659 /* call trigger on the frontend before the backend. */
1661 dev_dbg(fe
->dev
, "ASoC: pre trigger FE %s cmd %d\n",
1662 fe
->dai_link
->name
, cmd
);
1664 ret
= soc_pcm_trigger(substream
, cmd
);
1666 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1670 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
1672 case SND_SOC_DPCM_TRIGGER_POST
:
1673 /* call trigger on the frontend after the backend. */
1675 ret
= dpcm_be_dai_trigger(fe
, substream
->stream
, cmd
);
1677 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1681 dev_dbg(fe
->dev
, "ASoC: post trigger FE %s cmd %d\n",
1682 fe
->dai_link
->name
, cmd
);
1684 ret
= soc_pcm_trigger(substream
, cmd
);
1686 case SND_SOC_DPCM_TRIGGER_BESPOKE
:
1687 /* bespoke trigger() - handles both FE and BEs */
1689 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd %d\n",
1690 fe
->dai_link
->name
, cmd
);
1692 ret
= soc_pcm_bespoke_trigger(substream
, cmd
);
1694 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1699 dev_err(fe
->dev
, "ASoC: invalid trigger cmd %d for %s\n", cmd
,
1700 fe
->dai_link
->name
);
1706 case SNDRV_PCM_TRIGGER_START
:
1707 case SNDRV_PCM_TRIGGER_RESUME
:
1708 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
1709 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_START
;
1711 case SNDRV_PCM_TRIGGER_STOP
:
1712 case SNDRV_PCM_TRIGGER_SUSPEND
:
1713 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
1714 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_STOP
;
1719 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1723 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime
*fe
, int stream
)
1725 struct snd_soc_dpcm
*dpcm
;
1728 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1730 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1731 struct snd_pcm_substream
*be_substream
=
1732 snd_soc_dpcm_get_substream(be
, stream
);
1734 /* is this op for this BE ? */
1735 if (!snd_soc_dpcm_be_can_update(fe
, be
, stream
))
1738 if ((be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
1739 (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_STOP
))
1742 dev_dbg(be
->dev
, "ASoC: prepare BE %s\n",
1743 dpcm
->fe
->dai_link
->name
);
1745 ret
= soc_pcm_prepare(be_substream
);
1747 dev_err(be
->dev
, "ASoC: backend prepare failed %d\n",
1752 be
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
1757 static int dpcm_fe_dai_prepare(struct snd_pcm_substream
*substream
)
1759 struct snd_soc_pcm_runtime
*fe
= substream
->private_data
;
1760 int stream
= substream
->stream
, ret
= 0;
1762 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1764 dev_dbg(fe
->dev
, "ASoC: prepare FE %s\n", fe
->dai_link
->name
);
1766 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_FE
;
1768 /* there is no point preparing this FE if there are no BEs */
1769 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
1770 dev_err(fe
->dev
, "ASoC: no backend DAIs enabled for %s\n",
1771 fe
->dai_link
->name
);
1776 ret
= dpcm_be_dai_prepare(fe
, substream
->stream
);
1780 /* call prepare on the frontend */
1781 ret
= soc_pcm_prepare(substream
);
1783 dev_err(fe
->dev
,"ASoC: prepare FE %s failed\n",
1784 fe
->dai_link
->name
);
1788 /* run the stream event for each BE */
1789 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_START
);
1790 fe
->dpcm
[stream
].state
= SND_SOC_DPCM_STATE_PREPARE
;
1793 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1794 mutex_unlock(&fe
->card
->mutex
);
1799 static int soc_pcm_ioctl(struct snd_pcm_substream
*substream
,
1800 unsigned int cmd
, void *arg
)
1802 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1803 struct snd_soc_platform
*platform
= rtd
->platform
;
1805 if (platform
->driver
->ops
&& platform
->driver
->ops
->ioctl
)
1806 return platform
->driver
->ops
->ioctl(substream
, cmd
, arg
);
1807 return snd_pcm_lib_ioctl(substream
, cmd
, arg
);
1810 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime
*fe
, int stream
)
1812 struct snd_pcm_substream
*substream
=
1813 snd_soc_dpcm_get_substream(fe
, stream
);
1814 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
1817 dev_dbg(fe
->dev
, "ASoC: runtime %s close on FE %s\n",
1818 stream
? "capture" : "playback", fe
->dai_link
->name
);
1820 if (trigger
== SND_SOC_DPCM_TRIGGER_BESPOKE
) {
1821 /* call bespoke trigger - FE takes care of all BE triggers */
1822 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd stop\n",
1823 fe
->dai_link
->name
);
1825 err
= soc_pcm_bespoke_trigger(substream
, SNDRV_PCM_TRIGGER_STOP
);
1827 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
1829 dev_dbg(fe
->dev
, "ASoC: trigger FE %s cmd stop\n",
1830 fe
->dai_link
->name
);
1832 err
= dpcm_be_dai_trigger(fe
, stream
, SNDRV_PCM_TRIGGER_STOP
);
1834 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", err
);
1837 err
= dpcm_be_dai_hw_free(fe
, stream
);
1839 dev_err(fe
->dev
,"ASoC: hw_free FE failed %d\n", err
);
1841 err
= dpcm_be_dai_shutdown(fe
, stream
);
1843 dev_err(fe
->dev
,"ASoC: shutdown FE failed %d\n", err
);
1845 /* run the stream event for each BE */
1846 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
1851 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime
*fe
, int stream
)
1853 struct snd_pcm_substream
*substream
=
1854 snd_soc_dpcm_get_substream(fe
, stream
);
1855 struct snd_soc_dpcm
*dpcm
;
1856 enum snd_soc_dpcm_trigger trigger
= fe
->dai_link
->trigger
[stream
];
1859 dev_dbg(fe
->dev
, "ASoC: runtime %s open on FE %s\n",
1860 stream
? "capture" : "playback", fe
->dai_link
->name
);
1862 /* Only start the BE if the FE is ready */
1863 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_FREE
||
1864 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_CLOSE
)
1867 /* startup must always be called for new BEs */
1868 ret
= dpcm_be_dai_startup(fe
, stream
);
1872 /* keep going if FE state is > open */
1873 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_OPEN
)
1876 ret
= dpcm_be_dai_hw_params(fe
, stream
);
1880 /* keep going if FE state is > hw_params */
1881 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_HW_PARAMS
)
1885 ret
= dpcm_be_dai_prepare(fe
, stream
);
1889 /* run the stream event for each BE */
1890 dpcm_dapm_stream_event(fe
, stream
, SND_SOC_DAPM_STREAM_NOP
);
1892 /* keep going if FE state is > prepare */
1893 if (fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_PREPARE
||
1894 fe
->dpcm
[stream
].state
== SND_SOC_DPCM_STATE_STOP
)
1897 if (trigger
== SND_SOC_DPCM_TRIGGER_BESPOKE
) {
1898 /* call trigger on the frontend - FE takes care of all BE triggers */
1899 dev_dbg(fe
->dev
, "ASoC: bespoke trigger FE %s cmd start\n",
1900 fe
->dai_link
->name
);
1902 ret
= soc_pcm_bespoke_trigger(substream
, SNDRV_PCM_TRIGGER_START
);
1904 dev_err(fe
->dev
,"ASoC: bespoke trigger FE failed %d\n", ret
);
1908 dev_dbg(fe
->dev
, "ASoC: trigger FE %s cmd start\n",
1909 fe
->dai_link
->name
);
1911 ret
= dpcm_be_dai_trigger(fe
, stream
,
1912 SNDRV_PCM_TRIGGER_START
);
1914 dev_err(fe
->dev
,"ASoC: trigger FE failed %d\n", ret
);
1922 dpcm_be_dai_hw_free(fe
, stream
);
1924 dpcm_be_dai_shutdown(fe
, stream
);
1926 /* disconnect any non started BEs */
1927 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
1928 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
1929 if (be
->dpcm
[stream
].state
!= SND_SOC_DPCM_STATE_START
)
1930 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
1936 static int dpcm_run_new_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
1940 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1941 ret
= dpcm_run_update_startup(fe
, stream
);
1943 dev_err(fe
->dev
, "ASoC: failed to startup some BEs\n");
1944 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1949 static int dpcm_run_old_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
1953 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_BE
;
1954 ret
= dpcm_run_update_shutdown(fe
, stream
);
1956 dev_err(fe
->dev
, "ASoC: failed to shutdown some BEs\n");
1957 fe
->dpcm
[stream
].runtime_update
= SND_SOC_DPCM_UPDATE_NO
;
1962 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1965 int soc_dpcm_runtime_update(struct snd_soc_card
*card
)
1967 int i
, old
, new, paths
;
1969 mutex_lock_nested(&card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
1970 for (i
= 0; i
< card
->num_rtd
; i
++) {
1971 struct snd_soc_dapm_widget_list
*list
;
1972 struct snd_soc_pcm_runtime
*fe
= &card
->rtd
[i
];
1974 /* make sure link is FE */
1975 if (!fe
->dai_link
->dynamic
)
1978 /* only check active links */
1979 if (!fe
->cpu_dai
->active
)
1982 /* DAPM sync will call this to update DSP paths */
1983 dev_dbg(fe
->dev
, "ASoC: DPCM runtime update for FE %s\n",
1984 fe
->dai_link
->name
);
1986 /* skip if FE doesn't have playback capability */
1987 if (!fe
->cpu_dai
->driver
->playback
.channels_min
)
1990 paths
= dpcm_path_get(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
);
1992 dpcm_path_put(&list
);
1993 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
1994 fe
->dai_link
->name
, "playback");
1995 mutex_unlock(&card
->mutex
);
1999 /* update any new playback paths */
2000 new = dpcm_process_paths(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
, 1);
2002 dpcm_run_new_update(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2003 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2004 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2007 /* update any old playback paths */
2008 old
= dpcm_process_paths(fe
, SNDRV_PCM_STREAM_PLAYBACK
, &list
, 0);
2010 dpcm_run_old_update(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2011 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2012 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_PLAYBACK
);
2016 /* skip if FE doesn't have capture capability */
2017 if (!fe
->cpu_dai
->driver
->capture
.channels_min
)
2020 paths
= dpcm_path_get(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
);
2022 dpcm_path_put(&list
);
2023 dev_warn(fe
->dev
, "ASoC: %s no valid %s path\n",
2024 fe
->dai_link
->name
, "capture");
2025 mutex_unlock(&card
->mutex
);
2029 /* update any new capture paths */
2030 new = dpcm_process_paths(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
, 1);
2032 dpcm_run_new_update(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2033 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2034 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2037 /* update any old capture paths */
2038 old
= dpcm_process_paths(fe
, SNDRV_PCM_STREAM_CAPTURE
, &list
, 0);
2040 dpcm_run_old_update(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2041 dpcm_clear_pending_state(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2042 dpcm_be_disconnect(fe
, SNDRV_PCM_STREAM_CAPTURE
);
2045 dpcm_path_put(&list
);
2048 mutex_unlock(&card
->mutex
);
2051 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime
*fe
, int mute
)
2053 struct snd_soc_dpcm
*dpcm
;
2054 struct list_head
*clients
=
2055 &fe
->dpcm
[SNDRV_PCM_STREAM_PLAYBACK
].be_clients
;
2057 list_for_each_entry(dpcm
, clients
, list_be
) {
2059 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2060 struct snd_soc_dai
*dai
= be
->codec_dai
;
2061 struct snd_soc_dai_driver
*drv
= dai
->driver
;
2063 if (be
->dai_link
->ignore_suspend
)
2066 dev_dbg(be
->dev
, "ASoC: BE digital mute %s\n", be
->dai_link
->name
);
2068 if (drv
->ops
&& drv
->ops
->digital_mute
&& dai
->playback_active
)
2069 drv
->ops
->digital_mute(dai
, mute
);
2075 static int dpcm_fe_dai_open(struct snd_pcm_substream
*fe_substream
)
2077 struct snd_soc_pcm_runtime
*fe
= fe_substream
->private_data
;
2078 struct snd_soc_dpcm
*dpcm
;
2079 struct snd_soc_dapm_widget_list
*list
;
2081 int stream
= fe_substream
->stream
;
2083 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2084 fe
->dpcm
[stream
].runtime
= fe_substream
->runtime
;
2086 if (dpcm_path_get(fe
, stream
, &list
) <= 0) {
2087 dpcm_path_put(&list
);
2088 dev_dbg(fe
->dev
, "ASoC: %s no valid %s route\n",
2089 fe
->dai_link
->name
, stream
? "capture" : "playback");
2092 /* calculate valid and active FE <-> BE dpcms */
2093 dpcm_process_paths(fe
, stream
, &list
, 1);
2095 ret
= dpcm_fe_dai_startup(fe_substream
);
2097 /* clean up all links */
2098 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
)
2099 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2101 dpcm_be_disconnect(fe
, stream
);
2102 fe
->dpcm
[stream
].runtime
= NULL
;
2105 dpcm_clear_pending_state(fe
, stream
);
2106 dpcm_path_put(&list
);
2107 mutex_unlock(&fe
->card
->mutex
);
2111 static int dpcm_fe_dai_close(struct snd_pcm_substream
*fe_substream
)
2113 struct snd_soc_pcm_runtime
*fe
= fe_substream
->private_data
;
2114 struct snd_soc_dpcm
*dpcm
;
2115 int stream
= fe_substream
->stream
, ret
;
2117 mutex_lock_nested(&fe
->card
->mutex
, SND_SOC_CARD_CLASS_RUNTIME
);
2118 ret
= dpcm_fe_dai_shutdown(fe_substream
);
2120 /* mark FE's links ready to prune */
2121 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
)
2122 dpcm
->state
= SND_SOC_DPCM_LINK_STATE_FREE
;
2124 dpcm_be_disconnect(fe
, stream
);
2126 fe
->dpcm
[stream
].runtime
= NULL
;
2127 mutex_unlock(&fe
->card
->mutex
);
2131 /* create a new pcm */
2132 int soc_new_pcm(struct snd_soc_pcm_runtime
*rtd
, int num
)
2134 struct snd_soc_platform
*platform
= rtd
->platform
;
2135 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
2136 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
2137 struct snd_pcm
*pcm
;
2139 int ret
= 0, playback
= 0, capture
= 0;
2141 if (rtd
->dai_link
->dynamic
|| rtd
->dai_link
->no_pcm
) {
2142 playback
= rtd
->dai_link
->dpcm_playback
;
2143 capture
= rtd
->dai_link
->dpcm_capture
;
2145 if (codec_dai
->driver
->playback
.channels_min
&&
2146 cpu_dai
->driver
->playback
.channels_min
)
2148 if (codec_dai
->driver
->capture
.channels_min
&&
2149 cpu_dai
->driver
->capture
.channels_min
)
2153 if (rtd
->dai_link
->playback_only
) {
2158 if (rtd
->dai_link
->capture_only
) {
2163 /* create the PCM */
2164 if (rtd
->dai_link
->no_pcm
) {
2165 snprintf(new_name
, sizeof(new_name
), "(%s)",
2166 rtd
->dai_link
->stream_name
);
2168 ret
= snd_pcm_new_internal(rtd
->card
->snd_card
, new_name
, num
,
2169 playback
, capture
, &pcm
);
2171 if (rtd
->dai_link
->dynamic
)
2172 snprintf(new_name
, sizeof(new_name
), "%s (*)",
2173 rtd
->dai_link
->stream_name
);
2175 snprintf(new_name
, sizeof(new_name
), "%s %s-%d",
2176 rtd
->dai_link
->stream_name
, codec_dai
->name
, num
);
2178 ret
= snd_pcm_new(rtd
->card
->snd_card
, new_name
, num
, playback
,
2182 dev_err(rtd
->card
->dev
, "ASoC: can't create pcm for %s\n",
2183 rtd
->dai_link
->name
);
2186 dev_dbg(rtd
->card
->dev
, "ASoC: registered pcm #%d %s\n",num
, new_name
);
2188 /* DAPM dai link stream work */
2189 INIT_DELAYED_WORK(&rtd
->delayed_work
, close_delayed_work
);
2192 pcm
->private_data
= rtd
;
2194 if (rtd
->dai_link
->no_pcm
) {
2196 pcm
->streams
[SNDRV_PCM_STREAM_PLAYBACK
].substream
->private_data
= rtd
;
2198 pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
->private_data
= rtd
;
2202 /* ASoC PCM operations */
2203 if (rtd
->dai_link
->dynamic
) {
2204 rtd
->ops
.open
= dpcm_fe_dai_open
;
2205 rtd
->ops
.hw_params
= dpcm_fe_dai_hw_params
;
2206 rtd
->ops
.prepare
= dpcm_fe_dai_prepare
;
2207 rtd
->ops
.trigger
= dpcm_fe_dai_trigger
;
2208 rtd
->ops
.hw_free
= dpcm_fe_dai_hw_free
;
2209 rtd
->ops
.close
= dpcm_fe_dai_close
;
2210 rtd
->ops
.pointer
= soc_pcm_pointer
;
2211 rtd
->ops
.ioctl
= soc_pcm_ioctl
;
2213 rtd
->ops
.open
= soc_pcm_open
;
2214 rtd
->ops
.hw_params
= soc_pcm_hw_params
;
2215 rtd
->ops
.prepare
= soc_pcm_prepare
;
2216 rtd
->ops
.trigger
= soc_pcm_trigger
;
2217 rtd
->ops
.hw_free
= soc_pcm_hw_free
;
2218 rtd
->ops
.close
= soc_pcm_close
;
2219 rtd
->ops
.pointer
= soc_pcm_pointer
;
2220 rtd
->ops
.ioctl
= soc_pcm_ioctl
;
2223 if (platform
->driver
->ops
) {
2224 rtd
->ops
.ack
= platform
->driver
->ops
->ack
;
2225 rtd
->ops
.copy
= platform
->driver
->ops
->copy
;
2226 rtd
->ops
.silence
= platform
->driver
->ops
->silence
;
2227 rtd
->ops
.page
= platform
->driver
->ops
->page
;
2228 rtd
->ops
.mmap
= platform
->driver
->ops
->mmap
;
2232 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &rtd
->ops
);
2235 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &rtd
->ops
);
2237 if (platform
->driver
->pcm_new
) {
2238 ret
= platform
->driver
->pcm_new(rtd
);
2240 dev_err(platform
->dev
,
2241 "ASoC: pcm constructor failed: %d\n",
2247 pcm
->private_free
= platform
->driver
->pcm_free
;
2249 dev_info(rtd
->card
->dev
, "%s <-> %s mapping ok\n", codec_dai
->name
,
2254 /* is the current PCM operation for this FE ? */
2255 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime
*fe
, int stream
)
2257 if (fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
)
2261 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update
);
2263 /* is the current PCM operation for this BE ? */
2264 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime
*fe
,
2265 struct snd_soc_pcm_runtime
*be
, int stream
)
2267 if ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_FE
) ||
2268 ((fe
->dpcm
[stream
].runtime_update
== SND_SOC_DPCM_UPDATE_BE
) &&
2269 be
->dpcm
[stream
].runtime_update
))
2273 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update
);
2275 /* get the substream for this BE */
2276 struct snd_pcm_substream
*
2277 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime
*be
, int stream
)
2279 return be
->pcm
->streams
[stream
].substream
;
2281 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream
);
2283 /* get the BE runtime state */
2284 enum snd_soc_dpcm_state
2285 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime
*be
, int stream
)
2287 return be
->dpcm
[stream
].state
;
2289 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state
);
2291 /* set the BE runtime state */
2292 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime
*be
,
2293 int stream
, enum snd_soc_dpcm_state state
)
2295 be
->dpcm
[stream
].state
= state
;
2297 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state
);
2300 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2301 * are not running, paused or suspended for the specified stream direction.
2303 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime
*fe
,
2304 struct snd_soc_pcm_runtime
*be
, int stream
)
2306 struct snd_soc_dpcm
*dpcm
;
2309 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
2314 state
= dpcm
->fe
->dpcm
[stream
].state
;
2315 if (state
== SND_SOC_DPCM_STATE_START
||
2316 state
== SND_SOC_DPCM_STATE_PAUSED
||
2317 state
== SND_SOC_DPCM_STATE_SUSPEND
)
2321 /* it's safe to free/stop this BE DAI */
2324 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop
);
2327 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2328 * running, paused or suspended for the specified stream direction.
2330 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime
*fe
,
2331 struct snd_soc_pcm_runtime
*be
, int stream
)
2333 struct snd_soc_dpcm
*dpcm
;
2336 list_for_each_entry(dpcm
, &be
->dpcm
[stream
].fe_clients
, list_fe
) {
2341 state
= dpcm
->fe
->dpcm
[stream
].state
;
2342 if (state
== SND_SOC_DPCM_STATE_START
||
2343 state
== SND_SOC_DPCM_STATE_PAUSED
||
2344 state
== SND_SOC_DPCM_STATE_SUSPEND
||
2345 state
== SND_SOC_DPCM_STATE_PREPARE
)
2349 /* it's safe to change hw_params */
2352 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params
);
2354 int snd_soc_platform_trigger(struct snd_pcm_substream
*substream
,
2355 int cmd
, struct snd_soc_platform
*platform
)
2357 if (platform
->driver
->ops
&& platform
->driver
->ops
->trigger
)
2358 return platform
->driver
->ops
->trigger(substream
, cmd
);
2361 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger
);
2363 #ifdef CONFIG_DEBUG_FS
2364 static char *dpcm_state_string(enum snd_soc_dpcm_state state
)
2367 case SND_SOC_DPCM_STATE_NEW
:
2369 case SND_SOC_DPCM_STATE_OPEN
:
2371 case SND_SOC_DPCM_STATE_HW_PARAMS
:
2373 case SND_SOC_DPCM_STATE_PREPARE
:
2375 case SND_SOC_DPCM_STATE_START
:
2377 case SND_SOC_DPCM_STATE_STOP
:
2379 case SND_SOC_DPCM_STATE_SUSPEND
:
2381 case SND_SOC_DPCM_STATE_PAUSED
:
2383 case SND_SOC_DPCM_STATE_HW_FREE
:
2385 case SND_SOC_DPCM_STATE_CLOSE
:
2392 static ssize_t
dpcm_show_state(struct snd_soc_pcm_runtime
*fe
,
2393 int stream
, char *buf
, size_t size
)
2395 struct snd_pcm_hw_params
*params
= &fe
->dpcm
[stream
].hw_params
;
2396 struct snd_soc_dpcm
*dpcm
;
2400 offset
+= snprintf(buf
+ offset
, size
- offset
,
2401 "[%s - %s]\n", fe
->dai_link
->name
,
2402 stream
? "Capture" : "Playback");
2404 offset
+= snprintf(buf
+ offset
, size
- offset
, "State: %s\n",
2405 dpcm_state_string(fe
->dpcm
[stream
].state
));
2407 if ((fe
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2408 (fe
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
2409 offset
+= snprintf(buf
+ offset
, size
- offset
,
2411 "Format = %s, Channels = %d, Rate = %d\n",
2412 snd_pcm_format_name(params_format(params
)),
2413 params_channels(params
),
2414 params_rate(params
));
2417 offset
+= snprintf(buf
+ offset
, size
- offset
, "Backends:\n");
2419 if (list_empty(&fe
->dpcm
[stream
].be_clients
)) {
2420 offset
+= snprintf(buf
+ offset
, size
- offset
,
2421 " No active DSP links\n");
2425 list_for_each_entry(dpcm
, &fe
->dpcm
[stream
].be_clients
, list_be
) {
2426 struct snd_soc_pcm_runtime
*be
= dpcm
->be
;
2427 params
= &dpcm
->hw_params
;
2429 offset
+= snprintf(buf
+ offset
, size
- offset
,
2430 "- %s\n", be
->dai_link
->name
);
2432 offset
+= snprintf(buf
+ offset
, size
- offset
,
2434 dpcm_state_string(be
->dpcm
[stream
].state
));
2436 if ((be
->dpcm
[stream
].state
>= SND_SOC_DPCM_STATE_HW_PARAMS
) &&
2437 (be
->dpcm
[stream
].state
<= SND_SOC_DPCM_STATE_STOP
))
2438 offset
+= snprintf(buf
+ offset
, size
- offset
,
2439 " Hardware Params: "
2440 "Format = %s, Channels = %d, Rate = %d\n",
2441 snd_pcm_format_name(params_format(params
)),
2442 params_channels(params
),
2443 params_rate(params
));
2450 static ssize_t
dpcm_state_read_file(struct file
*file
, char __user
*user_buf
,
2451 size_t count
, loff_t
*ppos
)
2453 struct snd_soc_pcm_runtime
*fe
= file
->private_data
;
2454 ssize_t out_count
= PAGE_SIZE
, offset
= 0, ret
= 0;
2457 buf
= kmalloc(out_count
, GFP_KERNEL
);
2461 if (fe
->cpu_dai
->driver
->playback
.channels_min
)
2462 offset
+= dpcm_show_state(fe
, SNDRV_PCM_STREAM_PLAYBACK
,
2463 buf
+ offset
, out_count
- offset
);
2465 if (fe
->cpu_dai
->driver
->capture
.channels_min
)
2466 offset
+= dpcm_show_state(fe
, SNDRV_PCM_STREAM_CAPTURE
,
2467 buf
+ offset
, out_count
- offset
);
2469 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, offset
);
2475 static const struct file_operations dpcm_state_fops
= {
2476 .open
= simple_open
,
2477 .read
= dpcm_state_read_file
,
2478 .llseek
= default_llseek
,
2481 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime
*rtd
)
2486 rtd
->debugfs_dpcm_root
= debugfs_create_dir(rtd
->dai_link
->name
,
2487 rtd
->card
->debugfs_card_root
);
2488 if (!rtd
->debugfs_dpcm_root
) {
2490 "ASoC: Failed to create dpcm debugfs directory %s\n",
2491 rtd
->dai_link
->name
);
2495 rtd
->debugfs_dpcm_state
= debugfs_create_file("state", 0444,
2496 rtd
->debugfs_dpcm_root
,
2497 rtd
, &dpcm_state_fops
);