Merge tag 'v3.13-rc3' into asoc-pcm
[deliverable/linux.git] / sound / soc / soc-pcm.c
CommitLineData
ddee627c
LG
1/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
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.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
988e8cc4 22#include <linux/pinctrl/consumer.h>
d6652ef8 23#include <linux/pm_runtime.h>
ddee627c
LG
24#include <linux/slab.h>
25#include <linux/workqueue.h>
01d7584c 26#include <linux/export.h>
f86dcef8 27#include <linux/debugfs.h>
ddee627c
LG
28#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
01d7584c 32#include <sound/soc-dpcm.h>
ddee627c
LG
33#include <sound/initval.h>
34
01d7584c
LG
35#define DPCM_MAX_BE_USERS 8
36
90996f43
LPC
37/**
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
41 *
42 * Sets the substream runtime hardware parameters.
43 */
44int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
46{
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
56 return 0;
57}
58EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
59
01d7584c
LG
60/* DPCM stream event, send event to FE and all active BEs. */
61static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
62 int event)
63{
64 struct snd_soc_dpcm *dpcm;
65
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
67
68 struct snd_soc_pcm_runtime *be = dpcm->be;
69
103d84a3 70 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
01d7584c
LG
71 be->dai_link->name, event, dir);
72
73 snd_soc_dapm_stream_event(be, dir, event);
74 }
75
76 snd_soc_dapm_stream_event(fe, dir, event);
77
78 return 0;
79}
80
17841020
DA
81static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
ddee627c
LG
83{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
ddee627c
LG
85 int ret;
86
17841020 87 if (!soc_dai->driver->symmetric_rates &&
ddee627c
LG
88 !rtd->dai_link->symmetric_rates)
89 return 0;
90
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.
94 */
17841020
DA
95 if (!soc_dai->rate) {
96 dev_warn(soc_dai->dev,
103d84a3 97 "ASoC: Not enforcing symmetric_rates due to race\n");
ddee627c
LG
98 return 0;
99 }
100
103d84a3 101 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
ddee627c
LG
102
103 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
104 SNDRV_PCM_HW_PARAM_RATE,
17841020 105 soc_dai->rate, soc_dai->rate);
ddee627c 106 if (ret < 0) {
17841020 107 dev_err(soc_dai->dev,
103d84a3
LG
108 "ASoC: Unable to apply rate symmetry constraint: %d\n",
109 ret);
ddee627c
LG
110 return ret;
111 }
112
113 return 0;
114}
115
58ba9b25
MB
116/*
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.
120 */
121static int sample_sizes[] = {
88e33954 122 24, 32,
58ba9b25
MB
123};
124
125static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
126 struct snd_soc_dai *dai)
127{
128 int ret, i, bits;
129
130 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
131 bits = dai->driver->playback.sig_bits;
132 else
133 bits = dai->driver->capture.sig_bits;
134
135 if (!bits)
136 return;
137
138 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
278047fd
MB
139 if (bits >= sample_sizes[i])
140 continue;
141
142 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
143 sample_sizes[i], bits);
58ba9b25
MB
144 if (ret != 0)
145 dev_warn(dai->dev,
103d84a3 146 "ASoC: Failed to set MSB %d/%d: %d\n",
58ba9b25
MB
147 bits, sample_sizes[i], ret);
148 }
149}
150
78e45c99 151static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
bd477c31
LPC
152 struct snd_soc_pcm_stream *codec_stream,
153 struct snd_soc_pcm_stream *cpu_stream)
154{
78e45c99
LPC
155 struct snd_pcm_hardware *hw = &runtime->hw;
156
bd477c31
LPC
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);
16d7ea91
LPC
161 if (hw->formats)
162 hw->formats &= codec_stream->formats & cpu_stream->formats;
163 else
164 hw->formats = codec_stream->formats & cpu_stream->formats;
bd477c31
LPC
165 hw->rates = codec_stream->rates & cpu_stream->rates;
166 if (codec_stream->rates
167 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
168 hw->rates |= cpu_stream->rates;
169 if (cpu_stream->rates
170 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
171 hw->rates |= codec_stream->rates;
78e45c99
LPC
172
173 snd_pcm_limit_hw_rates(runtime);
174
175 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
176 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
177 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
178 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
bd477c31
LPC
179}
180
ddee627c
LG
181/*
182 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
183 * then initialized and any private data can be allocated. This also calls
184 * startup for the cpu DAI, platform, machine and codec DAI.
185 */
186static int soc_pcm_open(struct snd_pcm_substream *substream)
187{
188 struct snd_soc_pcm_runtime *rtd = substream->private_data;
189 struct snd_pcm_runtime *runtime = substream->runtime;
190 struct snd_soc_platform *platform = rtd->platform;
191 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
192 struct snd_soc_dai *codec_dai = rtd->codec_dai;
193 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
194 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
195 int ret = 0;
196
988e8cc4
NC
197 pinctrl_pm_select_default_state(cpu_dai->dev);
198 pinctrl_pm_select_default_state(codec_dai->dev);
d6652ef8
MB
199 pm_runtime_get_sync(cpu_dai->dev);
200 pm_runtime_get_sync(codec_dai->dev);
201 pm_runtime_get_sync(platform->dev);
202
b8c0dab9 203 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
204
205 /* startup the audio subsystem */
c5914b0a 206 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
ddee627c
LG
207 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
208 if (ret < 0) {
103d84a3
LG
209 dev_err(cpu_dai->dev, "ASoC: can't open interface"
210 " %s: %d\n", cpu_dai->name, ret);
ddee627c
LG
211 goto out;
212 }
213 }
214
215 if (platform->driver->ops && platform->driver->ops->open) {
216 ret = platform->driver->ops->open(substream);
217 if (ret < 0) {
103d84a3
LG
218 dev_err(platform->dev, "ASoC: can't open platform"
219 " %s: %d\n", platform->name, ret);
ddee627c
LG
220 goto platform_err;
221 }
222 }
223
c5914b0a 224 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
ddee627c
LG
225 ret = codec_dai->driver->ops->startup(substream, codec_dai);
226 if (ret < 0) {
103d84a3
LG
227 dev_err(codec_dai->dev, "ASoC: can't open codec"
228 " %s: %d\n", codec_dai->name, ret);
ddee627c
LG
229 goto codec_dai_err;
230 }
231 }
232
233 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
234 ret = rtd->dai_link->ops->startup(substream);
235 if (ret < 0) {
103d84a3 236 pr_err("ASoC: %s startup failed: %d\n",
25bfe662 237 rtd->dai_link->name, ret);
ddee627c
LG
238 goto machine_err;
239 }
240 }
241
01d7584c
LG
242 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
243 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
244 goto dynamic;
245
ddee627c
LG
246 /* Check that the codec and cpu DAIs are compatible */
247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
78e45c99 248 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
bd477c31 249 &cpu_dai_drv->playback);
ddee627c 250 } else {
78e45c99 251 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
bd477c31 252 &cpu_dai_drv->capture);
ddee627c
LG
253 }
254
255 ret = -EINVAL;
ddee627c 256 if (!runtime->hw.rates) {
103d84a3 257 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
ddee627c
LG
258 codec_dai->name, cpu_dai->name);
259 goto config_err;
260 }
261 if (!runtime->hw.formats) {
103d84a3 262 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
ddee627c
LG
263 codec_dai->name, cpu_dai->name);
264 goto config_err;
265 }
266 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
267 runtime->hw.channels_min > runtime->hw.channels_max) {
103d84a3 268 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
ddee627c
LG
269 codec_dai->name, cpu_dai->name);
270 goto config_err;
271 }
272
58ba9b25
MB
273 soc_pcm_apply_msb(substream, codec_dai);
274 soc_pcm_apply_msb(substream, cpu_dai);
275
ddee627c 276 /* Symmetry only applies if we've already got an active stream. */
17841020
DA
277 if (cpu_dai->active) {
278 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
279 if (ret != 0)
280 goto config_err;
281 }
282
283 if (codec_dai->active) {
284 ret = soc_pcm_apply_symmetry(substream, codec_dai);
ddee627c
LG
285 if (ret != 0)
286 goto config_err;
287 }
288
103d84a3 289 pr_debug("ASoC: %s <-> %s info:\n",
ddee627c 290 codec_dai->name, cpu_dai->name);
103d84a3
LG
291 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
292 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
ddee627c 293 runtime->hw.channels_max);
103d84a3 294 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
ddee627c
LG
295 runtime->hw.rate_max);
296
01d7584c 297dynamic:
ddee627c
LG
298 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
299 cpu_dai->playback_active++;
300 codec_dai->playback_active++;
301 } else {
302 cpu_dai->capture_active++;
303 codec_dai->capture_active++;
304 }
305 cpu_dai->active++;
306 codec_dai->active++;
307 rtd->codec->active++;
b8c0dab9 308 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
309 return 0;
310
311config_err:
312 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
313 rtd->dai_link->ops->shutdown(substream);
314
315machine_err:
316 if (codec_dai->driver->ops->shutdown)
317 codec_dai->driver->ops->shutdown(substream, codec_dai);
318
319codec_dai_err:
320 if (platform->driver->ops && platform->driver->ops->close)
321 platform->driver->ops->close(substream);
322
323platform_err:
324 if (cpu_dai->driver->ops->shutdown)
325 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
326out:
b8c0dab9 327 mutex_unlock(&rtd->pcm_mutex);
d6652ef8
MB
328
329 pm_runtime_put(platform->dev);
330 pm_runtime_put(codec_dai->dev);
331 pm_runtime_put(cpu_dai->dev);
988e8cc4
NC
332 if (!codec_dai->active)
333 pinctrl_pm_select_sleep_state(codec_dai->dev);
334 if (!cpu_dai->active)
335 pinctrl_pm_select_sleep_state(cpu_dai->dev);
d6652ef8 336
ddee627c
LG
337 return ret;
338}
339
340/*
341 * Power down the audio subsystem pmdown_time msecs after close is called.
342 * This is to ensure there are no pops or clicks in between any music tracks
343 * due to DAPM power cycling.
344 */
345static void close_delayed_work(struct work_struct *work)
346{
347 struct snd_soc_pcm_runtime *rtd =
348 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
349 struct snd_soc_dai *codec_dai = rtd->codec_dai;
350
b8c0dab9 351 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c 352
103d84a3 353 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
ddee627c
LG
354 codec_dai->driver->playback.stream_name,
355 codec_dai->playback_active ? "active" : "inactive",
9bffb1fb 356 rtd->pop_wait ? "yes" : "no");
ddee627c
LG
357
358 /* are we waiting on this codec DAI stream */
9bffb1fb
MLC
359 if (rtd->pop_wait == 1) {
360 rtd->pop_wait = 0;
7bd3a6f3 361 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
d9b0951b 362 SND_SOC_DAPM_STREAM_STOP);
ddee627c
LG
363 }
364
b8c0dab9 365 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
366}
367
368/*
369 * Called by ALSA when a PCM substream is closed. Private data can be
370 * freed here. The cpu DAI, codec DAI, machine and platform are also
371 * shutdown.
372 */
91d5e6b4 373static int soc_pcm_close(struct snd_pcm_substream *substream)
ddee627c
LG
374{
375 struct snd_soc_pcm_runtime *rtd = substream->private_data;
376 struct snd_soc_platform *platform = rtd->platform;
377 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
378 struct snd_soc_dai *codec_dai = rtd->codec_dai;
379 struct snd_soc_codec *codec = rtd->codec;
380
b8c0dab9 381 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
382
383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
384 cpu_dai->playback_active--;
385 codec_dai->playback_active--;
386 } else {
387 cpu_dai->capture_active--;
388 codec_dai->capture_active--;
389 }
390
391 cpu_dai->active--;
392 codec_dai->active--;
393 codec->active--;
394
17841020
DA
395 /* clear the corresponding DAIs rate when inactive */
396 if (!cpu_dai->active)
397 cpu_dai->rate = 0;
398
399 if (!codec_dai->active)
400 codec_dai->rate = 0;
25b76791 401
ddee627c
LG
402 /* Muting the DAC suppresses artifacts caused during digital
403 * shutdown, for example from stopping clocks.
404 */
da18396f 405 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
ddee627c
LG
406
407 if (cpu_dai->driver->ops->shutdown)
408 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
409
410 if (codec_dai->driver->ops->shutdown)
411 codec_dai->driver->ops->shutdown(substream, codec_dai);
412
413 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
414 rtd->dai_link->ops->shutdown(substream);
415
416 if (platform->driver->ops && platform->driver->ops->close)
417 platform->driver->ops->close(substream);
418 cpu_dai->runtime = NULL;
419
420 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
b5d1d036 421 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
5b6247ab 422 rtd->dai_link->ignore_pmdown_time) {
1d69c5c5
PU
423 /* powered down playback stream now */
424 snd_soc_dapm_stream_event(rtd,
7bd3a6f3 425 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 426 SND_SOC_DAPM_STREAM_STOP);
1d69c5c5
PU
427 } else {
428 /* start delayed pop wq here for playback streams */
9bffb1fb 429 rtd->pop_wait = 1;
d4e1a73a
MB
430 queue_delayed_work(system_power_efficient_wq,
431 &rtd->delayed_work,
432 msecs_to_jiffies(rtd->pmdown_time));
1d69c5c5 433 }
ddee627c
LG
434 } else {
435 /* capture streams can be powered down now */
7bd3a6f3 436 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
d9b0951b 437 SND_SOC_DAPM_STREAM_STOP);
ddee627c
LG
438 }
439
b8c0dab9 440 mutex_unlock(&rtd->pcm_mutex);
d6652ef8
MB
441
442 pm_runtime_put(platform->dev);
443 pm_runtime_put(codec_dai->dev);
444 pm_runtime_put(cpu_dai->dev);
988e8cc4
NC
445 if (!codec_dai->active)
446 pinctrl_pm_select_sleep_state(codec_dai->dev);
447 if (!cpu_dai->active)
448 pinctrl_pm_select_sleep_state(cpu_dai->dev);
d6652ef8 449
ddee627c
LG
450 return 0;
451}
452
453/*
454 * Called by ALSA when the PCM substream is prepared, can set format, sample
455 * rate, etc. This function is non atomic and can be called multiple times,
456 * it can refer to the runtime info.
457 */
458static int soc_pcm_prepare(struct snd_pcm_substream *substream)
459{
460 struct snd_soc_pcm_runtime *rtd = substream->private_data;
461 struct snd_soc_platform *platform = rtd->platform;
462 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
463 struct snd_soc_dai *codec_dai = rtd->codec_dai;
464 int ret = 0;
465
b8c0dab9 466 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
467
468 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
469 ret = rtd->dai_link->ops->prepare(substream);
470 if (ret < 0) {
103d84a3
LG
471 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
472 " %d\n", ret);
ddee627c
LG
473 goto out;
474 }
475 }
476
477 if (platform->driver->ops && platform->driver->ops->prepare) {
478 ret = platform->driver->ops->prepare(substream);
479 if (ret < 0) {
103d84a3
LG
480 dev_err(platform->dev, "ASoC: platform prepare error:"
481 " %d\n", ret);
ddee627c
LG
482 goto out;
483 }
484 }
485
c5914b0a 486 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
ddee627c
LG
487 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
488 if (ret < 0) {
103d84a3 489 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
25bfe662 490 ret);
ddee627c
LG
491 goto out;
492 }
493 }
494
c5914b0a 495 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
ddee627c
LG
496 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
497 if (ret < 0) {
103d84a3 498 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
25bfe662 499 ret);
ddee627c
LG
500 goto out;
501 }
502 }
503
504 /* cancel any delayed stream shutdown that is pending */
505 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
9bffb1fb
MLC
506 rtd->pop_wait) {
507 rtd->pop_wait = 0;
ddee627c
LG
508 cancel_delayed_work(&rtd->delayed_work);
509 }
510
d9b0951b
LG
511 snd_soc_dapm_stream_event(rtd, substream->stream,
512 SND_SOC_DAPM_STREAM_START);
ddee627c 513
da18396f 514 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
ddee627c
LG
515
516out:
b8c0dab9 517 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
518 return ret;
519}
520
521/*
522 * Called by ALSA when the hardware params are set by application. This
523 * function can also be called multiple times and can allocate buffers
524 * (using snd_pcm_lib_* ). It's non-atomic.
525 */
526static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
527 struct snd_pcm_hw_params *params)
528{
529 struct snd_soc_pcm_runtime *rtd = substream->private_data;
530 struct snd_soc_platform *platform = rtd->platform;
531 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
532 struct snd_soc_dai *codec_dai = rtd->codec_dai;
533 int ret = 0;
534
b8c0dab9 535 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
536
537 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
538 ret = rtd->dai_link->ops->hw_params(substream, params);
539 if (ret < 0) {
103d84a3
LG
540 dev_err(rtd->card->dev, "ASoC: machine hw_params"
541 " failed: %d\n", ret);
ddee627c
LG
542 goto out;
543 }
544 }
545
c5914b0a 546 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
ddee627c
LG
547 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
548 if (ret < 0) {
103d84a3
LG
549 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
550 " %d\n", codec_dai->name, ret);
ddee627c
LG
551 goto codec_err;
552 }
553 }
554
c5914b0a 555 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
ddee627c
LG
556 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
557 if (ret < 0) {
103d84a3 558 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
25bfe662 559 cpu_dai->name, ret);
ddee627c
LG
560 goto interface_err;
561 }
562 }
563
564 if (platform->driver->ops && platform->driver->ops->hw_params) {
565 ret = platform->driver->ops->hw_params(substream, params);
566 if (ret < 0) {
103d84a3 567 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
25bfe662 568 platform->name, ret);
ddee627c
LG
569 goto platform_err;
570 }
571 }
572
17841020
DA
573 /* store the rate for each DAIs */
574 cpu_dai->rate = params_rate(params);
575 codec_dai->rate = params_rate(params);
ddee627c
LG
576
577out:
b8c0dab9 578 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
579 return ret;
580
581platform_err:
c5914b0a 582 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
ddee627c
LG
583 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
584
585interface_err:
c5914b0a 586 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
ddee627c
LG
587 codec_dai->driver->ops->hw_free(substream, codec_dai);
588
589codec_err:
590 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
591 rtd->dai_link->ops->hw_free(substream);
592
b8c0dab9 593 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
594 return ret;
595}
596
597/*
598 * Frees resources allocated by hw_params, can be called multiple times
599 */
600static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
601{
602 struct snd_soc_pcm_runtime *rtd = substream->private_data;
603 struct snd_soc_platform *platform = rtd->platform;
604 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
605 struct snd_soc_dai *codec_dai = rtd->codec_dai;
606 struct snd_soc_codec *codec = rtd->codec;
607
b8c0dab9 608 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
609
610 /* apply codec digital mute */
611 if (!codec->active)
da18396f 612 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
ddee627c
LG
613
614 /* free any machine hw params */
615 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
616 rtd->dai_link->ops->hw_free(substream);
617
618 /* free any DMA resources */
619 if (platform->driver->ops && platform->driver->ops->hw_free)
620 platform->driver->ops->hw_free(substream);
621
622 /* now free hw params for the DAIs */
c5914b0a 623 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
ddee627c
LG
624 codec_dai->driver->ops->hw_free(substream, codec_dai);
625
c5914b0a 626 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
ddee627c
LG
627 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
628
b8c0dab9 629 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
630 return 0;
631}
632
633static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
634{
635 struct snd_soc_pcm_runtime *rtd = substream->private_data;
636 struct snd_soc_platform *platform = rtd->platform;
637 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
638 struct snd_soc_dai *codec_dai = rtd->codec_dai;
639 int ret;
640
c5914b0a 641 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
ddee627c
LG
642 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
643 if (ret < 0)
644 return ret;
645 }
646
647 if (platform->driver->ops && platform->driver->ops->trigger) {
648 ret = platform->driver->ops->trigger(substream, cmd);
649 if (ret < 0)
650 return ret;
651 }
652
c5914b0a 653 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
ddee627c
LG
654 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
655 if (ret < 0)
656 return ret;
657 }
658 return 0;
659}
660
45c0a188
MB
661static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
662 int cmd)
07bf84aa
LG
663{
664 struct snd_soc_pcm_runtime *rtd = substream->private_data;
665 struct snd_soc_platform *platform = rtd->platform;
666 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
667 struct snd_soc_dai *codec_dai = rtd->codec_dai;
668 int ret;
669
c5914b0a
MB
670 if (codec_dai->driver->ops &&
671 codec_dai->driver->ops->bespoke_trigger) {
07bf84aa
LG
672 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
673 if (ret < 0)
674 return ret;
675 }
676
c5914b0a 677 if (platform->driver->ops && platform->driver->bespoke_trigger) {
07bf84aa
LG
678 ret = platform->driver->bespoke_trigger(substream, cmd);
679 if (ret < 0)
680 return ret;
681 }
682
c5914b0a 683 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
07bf84aa
LG
684 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
685 if (ret < 0)
686 return ret;
687 }
688 return 0;
689}
ddee627c
LG
690/*
691 * soc level wrapper for pointer callback
692 * If cpu_dai, codec_dai, platform driver has the delay callback, than
693 * the runtime->delay will be updated accordingly.
694 */
695static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
696{
697 struct snd_soc_pcm_runtime *rtd = substream->private_data;
698 struct snd_soc_platform *platform = rtd->platform;
699 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
700 struct snd_soc_dai *codec_dai = rtd->codec_dai;
701 struct snd_pcm_runtime *runtime = substream->runtime;
702 snd_pcm_uframes_t offset = 0;
703 snd_pcm_sframes_t delay = 0;
704
705 if (platform->driver->ops && platform->driver->ops->pointer)
706 offset = platform->driver->ops->pointer(substream);
707
c5914b0a 708 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
ddee627c
LG
709 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
710
c5914b0a 711 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
ddee627c
LG
712 delay += codec_dai->driver->ops->delay(substream, codec_dai);
713
714 if (platform->driver->delay)
715 delay += platform->driver->delay(substream, codec_dai);
716
717 runtime->delay = delay;
718
719 return offset;
720}
721
01d7584c
LG
722/* connect a FE and BE */
723static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
724 struct snd_soc_pcm_runtime *be, int stream)
725{
726 struct snd_soc_dpcm *dpcm;
727
728 /* only add new dpcms */
729 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
730 if (dpcm->be == be && dpcm->fe == fe)
731 return 0;
732 }
733
734 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
735 if (!dpcm)
736 return -ENOMEM;
737
738 dpcm->be = be;
739 dpcm->fe = fe;
740 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
741 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
742 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
743 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
744
7cc302d2 745 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
01d7584c
LG
746 stream ? "capture" : "playback", fe->dai_link->name,
747 stream ? "<-" : "->", be->dai_link->name);
748
f86dcef8
LG
749#ifdef CONFIG_DEBUG_FS
750 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
751 fe->debugfs_dpcm_root, &dpcm->state);
752#endif
01d7584c
LG
753 return 1;
754}
755
756/* reparent a BE onto another FE */
757static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
758 struct snd_soc_pcm_runtime *be, int stream)
759{
760 struct snd_soc_dpcm *dpcm;
761 struct snd_pcm_substream *fe_substream, *be_substream;
762
763 /* reparent if BE is connected to other FEs */
764 if (!be->dpcm[stream].users)
765 return;
766
767 be_substream = snd_soc_dpcm_get_substream(be, stream);
768
769 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
770 if (dpcm->fe == fe)
771 continue;
772
7cc302d2 773 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
01d7584c
LG
774 stream ? "capture" : "playback",
775 dpcm->fe->dai_link->name,
776 stream ? "<-" : "->", dpcm->be->dai_link->name);
777
778 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
779 be_substream->runtime = fe_substream->runtime;
780 break;
781 }
782}
783
784/* disconnect a BE and FE */
785static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
786{
787 struct snd_soc_dpcm *dpcm, *d;
788
789 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
103d84a3 790 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
01d7584c
LG
791 stream ? "capture" : "playback",
792 dpcm->be->dai_link->name);
793
794 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
795 continue;
796
7cc302d2 797 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
01d7584c
LG
798 stream ? "capture" : "playback", fe->dai_link->name,
799 stream ? "<-" : "->", dpcm->be->dai_link->name);
800
801 /* BEs still alive need new FE */
802 dpcm_be_reparent(fe, dpcm->be, stream);
803
f86dcef8
LG
804#ifdef CONFIG_DEBUG_FS
805 debugfs_remove(dpcm->debugfs_state);
806#endif
01d7584c
LG
807 list_del(&dpcm->list_be);
808 list_del(&dpcm->list_fe);
809 kfree(dpcm);
810 }
811}
812
813/* get BE for DAI widget and stream */
814static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
815 struct snd_soc_dapm_widget *widget, int stream)
816{
817 struct snd_soc_pcm_runtime *be;
818 int i;
819
820 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
821 for (i = 0; i < card->num_links; i++) {
822 be = &card->rtd[i];
823
35ea0655
LG
824 if (!be->dai_link->no_pcm)
825 continue;
826
01d7584c
LG
827 if (be->cpu_dai->playback_widget == widget ||
828 be->codec_dai->playback_widget == widget)
829 return be;
830 }
831 } else {
832
833 for (i = 0; i < card->num_links; i++) {
834 be = &card->rtd[i];
835
35ea0655
LG
836 if (!be->dai_link->no_pcm)
837 continue;
838
01d7584c
LG
839 if (be->cpu_dai->capture_widget == widget ||
840 be->codec_dai->capture_widget == widget)
841 return be;
842 }
843 }
844
103d84a3 845 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
01d7584c
LG
846 stream ? "capture" : "playback", widget->name);
847 return NULL;
848}
849
850static inline struct snd_soc_dapm_widget *
851 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
852{
853 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
854 return rtd->cpu_dai->playback_widget;
855 else
856 return rtd->cpu_dai->capture_widget;
857}
858
859static inline struct snd_soc_dapm_widget *
860 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
861{
862 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
863 return rtd->codec_dai->playback_widget;
864 else
865 return rtd->codec_dai->capture_widget;
866}
867
868static int widget_in_list(struct snd_soc_dapm_widget_list *list,
869 struct snd_soc_dapm_widget *widget)
870{
871 int i;
872
873 for (i = 0; i < list->num_widgets; i++) {
874 if (widget == list->widgets[i])
875 return 1;
876 }
877
878 return 0;
879}
880
881static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
882 int stream, struct snd_soc_dapm_widget_list **list_)
883{
884 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
885 struct snd_soc_dapm_widget_list *list;
886 int paths;
887
888 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
889 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
890 if (list == NULL)
891 return -ENOMEM;
892
893 /* get number of valid DAI paths and their widgets */
894 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
895
103d84a3 896 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
01d7584c
LG
897 stream ? "capture" : "playback");
898
899 *list_ = list;
900 return paths;
901}
902
903static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
904{
905 kfree(*list);
906}
907
908static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
909 struct snd_soc_dapm_widget_list **list_)
910{
911 struct snd_soc_dpcm *dpcm;
912 struct snd_soc_dapm_widget_list *list = *list_;
913 struct snd_soc_dapm_widget *widget;
914 int prune = 0;
915
916 /* Destroy any old FE <--> BE connections */
917 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
918
919 /* is there a valid CPU DAI widget for this BE */
920 widget = rtd_get_cpu_widget(dpcm->be, stream);
921
922 /* prune the BE if it's no longer in our active list */
923 if (widget && widget_in_list(list, widget))
924 continue;
925
926 /* is there a valid CODEC DAI widget for this BE */
927 widget = rtd_get_codec_widget(dpcm->be, stream);
928
929 /* prune the BE if it's no longer in our active list */
930 if (widget && widget_in_list(list, widget))
931 continue;
932
103d84a3 933 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
01d7584c
LG
934 stream ? "capture" : "playback",
935 dpcm->be->dai_link->name, fe->dai_link->name);
936 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
937 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
938 prune++;
939 }
940
103d84a3 941 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
01d7584c
LG
942 return prune;
943}
944
945static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
946 struct snd_soc_dapm_widget_list **list_)
947{
948 struct snd_soc_card *card = fe->card;
949 struct snd_soc_dapm_widget_list *list = *list_;
950 struct snd_soc_pcm_runtime *be;
951 int i, new = 0, err;
952
953 /* Create any new FE <--> BE connections */
954 for (i = 0; i < list->num_widgets; i++) {
955
4616274d
MB
956 switch (list->widgets[i]->id) {
957 case snd_soc_dapm_dai_in:
958 case snd_soc_dapm_dai_out:
959 break;
960 default:
01d7584c 961 continue;
4616274d 962 }
01d7584c
LG
963
964 /* is there a valid BE rtd for this widget */
965 be = dpcm_get_be(card, list->widgets[i], stream);
966 if (!be) {
103d84a3 967 dev_err(fe->dev, "ASoC: no BE found for %s\n",
01d7584c
LG
968 list->widgets[i]->name);
969 continue;
970 }
971
972 /* make sure BE is a real BE */
973 if (!be->dai_link->no_pcm)
974 continue;
975
976 /* don't connect if FE is not running */
977 if (!fe->dpcm[stream].runtime)
978 continue;
979
980 /* newly connected FE and BE */
981 err = dpcm_be_connect(fe, be, stream);
982 if (err < 0) {
103d84a3 983 dev_err(fe->dev, "ASoC: can't connect %s\n",
01d7584c
LG
984 list->widgets[i]->name);
985 break;
986 } else if (err == 0) /* already connected */
987 continue;
988
989 /* new */
990 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
991 new++;
992 }
993
103d84a3 994 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
01d7584c
LG
995 return new;
996}
997
998/*
999 * Find the corresponding BE DAIs that source or sink audio to this
1000 * FE substream.
1001 */
1002static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1003 int stream, struct snd_soc_dapm_widget_list **list, int new)
1004{
1005 if (new)
1006 return dpcm_add_paths(fe, stream, list);
1007 else
1008 return dpcm_prune_paths(fe, stream, list);
1009}
1010
1011static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1012{
1013 struct snd_soc_dpcm *dpcm;
1014
1015 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1016 dpcm->be->dpcm[stream].runtime_update =
1017 SND_SOC_DPCM_UPDATE_NO;
1018}
1019
1020static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1021 int stream)
1022{
1023 struct snd_soc_dpcm *dpcm;
1024
1025 /* disable any enabled and non active backends */
1026 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1027
1028 struct snd_soc_pcm_runtime *be = dpcm->be;
1029 struct snd_pcm_substream *be_substream =
1030 snd_soc_dpcm_get_substream(be, stream);
1031
1032 if (be->dpcm[stream].users == 0)
103d84a3 1033 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
01d7584c
LG
1034 stream ? "capture" : "playback",
1035 be->dpcm[stream].state);
1036
1037 if (--be->dpcm[stream].users != 0)
1038 continue;
1039
1040 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1041 continue;
1042
1043 soc_pcm_close(be_substream);
1044 be_substream->runtime = NULL;
1045 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1046 }
1047}
1048
1049static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1050{
1051 struct snd_soc_dpcm *dpcm;
1052 int err, count = 0;
1053
1054 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1055 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1056
1057 struct snd_soc_pcm_runtime *be = dpcm->be;
1058 struct snd_pcm_substream *be_substream =
1059 snd_soc_dpcm_get_substream(be, stream);
1060
2062b4c5
RKAL
1061 if (!be_substream) {
1062 dev_err(be->dev, "ASoC: no backend %s stream\n",
1063 stream ? "capture" : "playback");
1064 continue;
1065 }
1066
01d7584c
LG
1067 /* is this op for this BE ? */
1068 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1069 continue;
1070
1071 /* first time the dpcm is open ? */
1072 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
103d84a3 1073 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
01d7584c
LG
1074 stream ? "capture" : "playback",
1075 be->dpcm[stream].state);
1076
1077 if (be->dpcm[stream].users++ != 0)
1078 continue;
1079
1080 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1081 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1082 continue;
1083
2062b4c5
RKAL
1084 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1085 stream ? "capture" : "playback", be->dai_link->name);
01d7584c
LG
1086
1087 be_substream->runtime = be->dpcm[stream].runtime;
1088 err = soc_pcm_open(be_substream);
1089 if (err < 0) {
103d84a3 1090 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
01d7584c
LG
1091 be->dpcm[stream].users--;
1092 if (be->dpcm[stream].users < 0)
103d84a3 1093 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
01d7584c
LG
1094 stream ? "capture" : "playback",
1095 be->dpcm[stream].state);
1096
1097 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1098 goto unwind;
1099 }
1100
1101 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1102 count++;
1103 }
1104
1105 return count;
1106
1107unwind:
1108 /* disable any enabled and non active backends */
1109 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1110 struct snd_soc_pcm_runtime *be = dpcm->be;
1111 struct snd_pcm_substream *be_substream =
1112 snd_soc_dpcm_get_substream(be, stream);
1113
1114 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1115 continue;
1116
1117 if (be->dpcm[stream].users == 0)
103d84a3 1118 dev_err(be->dev, "ASoC: no users %s at close %d\n",
01d7584c
LG
1119 stream ? "capture" : "playback",
1120 be->dpcm[stream].state);
1121
1122 if (--be->dpcm[stream].users != 0)
1123 continue;
1124
1125 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1126 continue;
1127
1128 soc_pcm_close(be_substream);
1129 be_substream->runtime = NULL;
1130 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1131 }
1132
1133 return err;
1134}
1135
45c0a188 1136static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
01d7584c
LG
1137{
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1139 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1140 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1141 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1142
1143 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1144 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1145 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1146 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1147 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1148 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1149 runtime->hw.rates = cpu_dai_drv->playback.rates;
1150 } else {
1151 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1152 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1153 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1154 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1155 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1156 runtime->hw.rates = cpu_dai_drv->capture.rates;
1157 }
1158}
1159
1160static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1161{
1162 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1163 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1164 int stream = fe_substream->stream, ret = 0;
1165
1166 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1167
1168 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1169 if (ret < 0) {
103d84a3 1170 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
01d7584c
LG
1171 goto be_err;
1172 }
1173
103d84a3 1174 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
01d7584c
LG
1175
1176 /* start the DAI frontend */
1177 ret = soc_pcm_open(fe_substream);
1178 if (ret < 0) {
103d84a3 1179 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
01d7584c
LG
1180 goto unwind;
1181 }
1182
1183 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1184
1185 dpcm_set_fe_runtime(fe_substream);
1186 snd_pcm_limit_hw_rates(runtime);
1187
1188 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1189 return 0;
1190
1191unwind:
1192 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1193be_err:
1194 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1195 return ret;
1196}
1197
1198static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1199{
1200 struct snd_soc_dpcm *dpcm;
1201
1202 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1203 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1204
1205 struct snd_soc_pcm_runtime *be = dpcm->be;
1206 struct snd_pcm_substream *be_substream =
1207 snd_soc_dpcm_get_substream(be, stream);
1208
1209 /* is this op for this BE ? */
1210 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1211 continue;
1212
1213 if (be->dpcm[stream].users == 0)
103d84a3 1214 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
01d7584c
LG
1215 stream ? "capture" : "playback",
1216 be->dpcm[stream].state);
1217
1218 if (--be->dpcm[stream].users != 0)
1219 continue;
1220
1221 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1222 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1223 continue;
1224
103d84a3 1225 dev_dbg(be->dev, "ASoC: close BE %s\n",
01d7584c
LG
1226 dpcm->fe->dai_link->name);
1227
1228 soc_pcm_close(be_substream);
1229 be_substream->runtime = NULL;
1230
1231 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1232 }
1233 return 0;
1234}
1235
1236static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1237{
1238 struct snd_soc_pcm_runtime *fe = substream->private_data;
1239 int stream = substream->stream;
1240
1241 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1242
1243 /* shutdown the BEs */
1244 dpcm_be_dai_shutdown(fe, substream->stream);
1245
103d84a3 1246 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
01d7584c
LG
1247
1248 /* now shutdown the frontend */
1249 soc_pcm_close(substream);
1250
1251 /* run the stream event for each BE */
1252 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1253
1254 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1255 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1256 return 0;
1257}
1258
1259static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1260{
1261 struct snd_soc_dpcm *dpcm;
1262
1263 /* only hw_params backends that are either sinks or sources
1264 * to this frontend DAI */
1265 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1266
1267 struct snd_soc_pcm_runtime *be = dpcm->be;
1268 struct snd_pcm_substream *be_substream =
1269 snd_soc_dpcm_get_substream(be, stream);
1270
1271 /* is this op for this BE ? */
1272 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1273 continue;
1274
1275 /* only free hw when no longer used - check all FEs */
1276 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1277 continue;
1278
1279 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1280 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1281 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
08b27848 1282 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
01d7584c
LG
1283 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1284 continue;
1285
103d84a3 1286 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
01d7584c
LG
1287 dpcm->fe->dai_link->name);
1288
1289 soc_pcm_hw_free(be_substream);
1290
1291 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1292 }
1293
1294 return 0;
1295}
1296
45c0a188 1297static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
01d7584c
LG
1298{
1299 struct snd_soc_pcm_runtime *fe = substream->private_data;
1300 int err, stream = substream->stream;
1301
1302 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1303 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1304
103d84a3 1305 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
01d7584c
LG
1306
1307 /* call hw_free on the frontend */
1308 err = soc_pcm_hw_free(substream);
1309 if (err < 0)
103d84a3 1310 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
01d7584c
LG
1311 fe->dai_link->name);
1312
1313 /* only hw_params backends that are either sinks or sources
1314 * to this frontend DAI */
1315 err = dpcm_be_dai_hw_free(fe, stream);
1316
1317 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1318 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1319
1320 mutex_unlock(&fe->card->mutex);
1321 return 0;
1322}
1323
1324static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1325{
1326 struct snd_soc_dpcm *dpcm;
1327 int ret;
1328
1329 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1330
1331 struct snd_soc_pcm_runtime *be = dpcm->be;
1332 struct snd_pcm_substream *be_substream =
1333 snd_soc_dpcm_get_substream(be, stream);
1334
1335 /* is this op for this BE ? */
1336 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1337 continue;
1338
1339 /* only allow hw_params() if no connected FEs are running */
1340 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1341 continue;
1342
1343 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1344 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1345 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1346 continue;
1347
103d84a3 1348 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
01d7584c
LG
1349 dpcm->fe->dai_link->name);
1350
1351 /* copy params for each dpcm */
1352 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1353 sizeof(struct snd_pcm_hw_params));
1354
1355 /* perform any hw_params fixups */
1356 if (be->dai_link->be_hw_params_fixup) {
1357 ret = be->dai_link->be_hw_params_fixup(be,
1358 &dpcm->hw_params);
1359 if (ret < 0) {
1360 dev_err(be->dev,
103d84a3 1361 "ASoC: hw_params BE fixup failed %d\n",
01d7584c
LG
1362 ret);
1363 goto unwind;
1364 }
1365 }
1366
1367 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1368 if (ret < 0) {
1369 dev_err(dpcm->be->dev,
103d84a3 1370 "ASoC: hw_params BE failed %d\n", ret);
01d7584c
LG
1371 goto unwind;
1372 }
1373
1374 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1375 }
1376 return 0;
1377
1378unwind:
1379 /* disable any enabled and non active backends */
1380 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1381 struct snd_soc_pcm_runtime *be = dpcm->be;
1382 struct snd_pcm_substream *be_substream =
1383 snd_soc_dpcm_get_substream(be, stream);
1384
1385 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1386 continue;
1387
1388 /* only allow hw_free() if no connected FEs are running */
1389 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1390 continue;
1391
1392 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1393 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1394 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1395 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1396 continue;
1397
1398 soc_pcm_hw_free(be_substream);
1399 }
1400
1401 return ret;
1402}
1403
45c0a188
MB
1404static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1405 struct snd_pcm_hw_params *params)
01d7584c
LG
1406{
1407 struct snd_soc_pcm_runtime *fe = substream->private_data;
1408 int ret, stream = substream->stream;
1409
1410 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1411 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1412
1413 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1414 sizeof(struct snd_pcm_hw_params));
1415 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1416 if (ret < 0) {
103d84a3 1417 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
01d7584c
LG
1418 goto out;
1419 }
1420
103d84a3 1421 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
01d7584c
LG
1422 fe->dai_link->name, params_rate(params),
1423 params_channels(params), params_format(params));
1424
1425 /* call hw_params on the frontend */
1426 ret = soc_pcm_hw_params(substream, params);
1427 if (ret < 0) {
103d84a3 1428 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
01d7584c
LG
1429 dpcm_be_dai_hw_free(fe, stream);
1430 } else
1431 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1432
1433out:
1434 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1435 mutex_unlock(&fe->card->mutex);
1436 return ret;
1437}
1438
1439static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1440 struct snd_pcm_substream *substream, int cmd)
1441{
1442 int ret;
1443
103d84a3 1444 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
01d7584c
LG
1445 dpcm->fe->dai_link->name, cmd);
1446
1447 ret = soc_pcm_trigger(substream, cmd);
1448 if (ret < 0)
103d84a3 1449 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
01d7584c
LG
1450
1451 return ret;
1452}
1453
45c0a188
MB
1454static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1455 int cmd)
01d7584c
LG
1456{
1457 struct snd_soc_dpcm *dpcm;
1458 int ret = 0;
1459
1460 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1461
1462 struct snd_soc_pcm_runtime *be = dpcm->be;
1463 struct snd_pcm_substream *be_substream =
1464 snd_soc_dpcm_get_substream(be, stream);
1465
1466 /* is this op for this BE ? */
1467 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1468 continue;
1469
1470 switch (cmd) {
1471 case SNDRV_PCM_TRIGGER_START:
1472 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1473 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1474 continue;
1475
1476 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1477 if (ret)
1478 return ret;
1479
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1481 break;
1482 case SNDRV_PCM_TRIGGER_RESUME:
1483 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1484 continue;
1485
1486 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1487 if (ret)
1488 return ret;
1489
1490 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1491 break;
1492 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1493 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1494 continue;
1495
1496 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1497 if (ret)
1498 return ret;
1499
1500 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1501 break;
1502 case SNDRV_PCM_TRIGGER_STOP:
1503 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1504 continue;
1505
1506 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1507 continue;
1508
1509 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1510 if (ret)
1511 return ret;
1512
1513 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1514 break;
1515 case SNDRV_PCM_TRIGGER_SUSPEND:
1516 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1517 continue;
1518
1519 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1520 continue;
1521
1522 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1523 if (ret)
1524 return ret;
1525
1526 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1527 break;
1528 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1529 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1530 continue;
1531
1532 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1533 continue;
1534
1535 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1536 if (ret)
1537 return ret;
1538
1539 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1540 break;
1541 }
1542 }
1543
1544 return ret;
1545}
1546EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1547
45c0a188 1548static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
01d7584c
LG
1549{
1550 struct snd_soc_pcm_runtime *fe = substream->private_data;
1551 int stream = substream->stream, ret;
1552 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1553
1554 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1555
1556 switch (trigger) {
1557 case SND_SOC_DPCM_TRIGGER_PRE:
1558 /* call trigger on the frontend before the backend. */
1559
103d84a3 1560 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
01d7584c
LG
1561 fe->dai_link->name, cmd);
1562
1563 ret = soc_pcm_trigger(substream, cmd);
1564 if (ret < 0) {
103d84a3 1565 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
01d7584c
LG
1566 goto out;
1567 }
1568
1569 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1570 break;
1571 case SND_SOC_DPCM_TRIGGER_POST:
1572 /* call trigger on the frontend after the backend. */
1573
1574 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1575 if (ret < 0) {
103d84a3 1576 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
01d7584c
LG
1577 goto out;
1578 }
1579
103d84a3 1580 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
01d7584c
LG
1581 fe->dai_link->name, cmd);
1582
1583 ret = soc_pcm_trigger(substream, cmd);
1584 break;
07bf84aa
LG
1585 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1586 /* bespoke trigger() - handles both FE and BEs */
1587
103d84a3 1588 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
07bf84aa
LG
1589 fe->dai_link->name, cmd);
1590
1591 ret = soc_pcm_bespoke_trigger(substream, cmd);
1592 if (ret < 0) {
103d84a3 1593 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
07bf84aa
LG
1594 goto out;
1595 }
1596 break;
01d7584c 1597 default:
103d84a3 1598 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
01d7584c
LG
1599 fe->dai_link->name);
1600 ret = -EINVAL;
1601 goto out;
1602 }
1603
1604 switch (cmd) {
1605 case SNDRV_PCM_TRIGGER_START:
1606 case SNDRV_PCM_TRIGGER_RESUME:
1607 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1608 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1609 break;
1610 case SNDRV_PCM_TRIGGER_STOP:
1611 case SNDRV_PCM_TRIGGER_SUSPEND:
1612 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1613 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1614 break;
1615 }
1616
1617out:
1618 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1619 return ret;
1620}
1621
1622static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1623{
1624 struct snd_soc_dpcm *dpcm;
1625 int ret = 0;
1626
1627 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1628
1629 struct snd_soc_pcm_runtime *be = dpcm->be;
1630 struct snd_pcm_substream *be_substream =
1631 snd_soc_dpcm_get_substream(be, stream);
1632
1633 /* is this op for this BE ? */
1634 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1635 continue;
1636
1637 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1638 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1639 continue;
1640
103d84a3 1641 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
01d7584c
LG
1642 dpcm->fe->dai_link->name);
1643
1644 ret = soc_pcm_prepare(be_substream);
1645 if (ret < 0) {
103d84a3 1646 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
01d7584c
LG
1647 ret);
1648 break;
1649 }
1650
1651 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1652 }
1653 return ret;
1654}
1655
45c0a188 1656static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
01d7584c
LG
1657{
1658 struct snd_soc_pcm_runtime *fe = substream->private_data;
1659 int stream = substream->stream, ret = 0;
1660
1661 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1662
103d84a3 1663 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
01d7584c
LG
1664
1665 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1666
1667 /* there is no point preparing this FE if there are no BEs */
1668 if (list_empty(&fe->dpcm[stream].be_clients)) {
103d84a3 1669 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
01d7584c
LG
1670 fe->dai_link->name);
1671 ret = -EINVAL;
1672 goto out;
1673 }
1674
1675 ret = dpcm_be_dai_prepare(fe, substream->stream);
1676 if (ret < 0)
1677 goto out;
1678
1679 /* call prepare on the frontend */
1680 ret = soc_pcm_prepare(substream);
1681 if (ret < 0) {
103d84a3 1682 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
01d7584c
LG
1683 fe->dai_link->name);
1684 goto out;
1685 }
1686
1687 /* run the stream event for each BE */
1688 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1689 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1690
1691out:
1692 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1693 mutex_unlock(&fe->card->mutex);
1694
1695 return ret;
1696}
1697
be3f3f2c
LG
1698static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1699 unsigned int cmd, void *arg)
1700{
1701 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1702 struct snd_soc_platform *platform = rtd->platform;
1703
c5914b0a 1704 if (platform->driver->ops && platform->driver->ops->ioctl)
be3f3f2c
LG
1705 return platform->driver->ops->ioctl(substream, cmd, arg);
1706 return snd_pcm_lib_ioctl(substream, cmd, arg);
1707}
1708
618dae11
LG
1709static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1710{
07bf84aa
LG
1711 struct snd_pcm_substream *substream =
1712 snd_soc_dpcm_get_substream(fe, stream);
1713 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
618dae11
LG
1714 int err;
1715
103d84a3 1716 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
618dae11
LG
1717 stream ? "capture" : "playback", fe->dai_link->name);
1718
07bf84aa
LG
1719 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1720 /* call bespoke trigger - FE takes care of all BE triggers */
103d84a3 1721 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
07bf84aa
LG
1722 fe->dai_link->name);
1723
1724 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1725 if (err < 0)
103d84a3 1726 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
07bf84aa 1727 } else {
103d84a3 1728 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
07bf84aa
LG
1729 fe->dai_link->name);
1730
1731 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1732 if (err < 0)
103d84a3 1733 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
07bf84aa 1734 }
618dae11
LG
1735
1736 err = dpcm_be_dai_hw_free(fe, stream);
1737 if (err < 0)
103d84a3 1738 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
618dae11
LG
1739
1740 err = dpcm_be_dai_shutdown(fe, stream);
1741 if (err < 0)
103d84a3 1742 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
618dae11
LG
1743
1744 /* run the stream event for each BE */
1745 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1746
1747 return 0;
1748}
1749
1750static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1751{
07bf84aa
LG
1752 struct snd_pcm_substream *substream =
1753 snd_soc_dpcm_get_substream(fe, stream);
618dae11 1754 struct snd_soc_dpcm *dpcm;
07bf84aa 1755 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
618dae11
LG
1756 int ret;
1757
103d84a3 1758 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
618dae11
LG
1759 stream ? "capture" : "playback", fe->dai_link->name);
1760
1761 /* Only start the BE if the FE is ready */
1762 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1763 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1764 return -EINVAL;
1765
1766 /* startup must always be called for new BEs */
1767 ret = dpcm_be_dai_startup(fe, stream);
fffc0ca2 1768 if (ret < 0)
618dae11 1769 goto disconnect;
618dae11
LG
1770
1771 /* keep going if FE state is > open */
1772 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1773 return 0;
01d7584c 1774
618dae11 1775 ret = dpcm_be_dai_hw_params(fe, stream);
fffc0ca2 1776 if (ret < 0)
618dae11 1777 goto close;
618dae11
LG
1778
1779 /* keep going if FE state is > hw_params */
1780 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1781 return 0;
1782
1783
1784 ret = dpcm_be_dai_prepare(fe, stream);
fffc0ca2 1785 if (ret < 0)
618dae11 1786 goto hw_free;
618dae11
LG
1787
1788 /* run the stream event for each BE */
1789 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1790
1791 /* keep going if FE state is > prepare */
1792 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1793 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1794 return 0;
1795
07bf84aa
LG
1796 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1797 /* call trigger on the frontend - FE takes care of all BE triggers */
103d84a3 1798 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
07bf84aa 1799 fe->dai_link->name);
618dae11 1800
07bf84aa
LG
1801 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1802 if (ret < 0) {
103d84a3 1803 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
07bf84aa
LG
1804 goto hw_free;
1805 }
1806 } else {
103d84a3 1807 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
07bf84aa
LG
1808 fe->dai_link->name);
1809
1810 ret = dpcm_be_dai_trigger(fe, stream,
1811 SNDRV_PCM_TRIGGER_START);
1812 if (ret < 0) {
103d84a3 1813 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
07bf84aa
LG
1814 goto hw_free;
1815 }
618dae11
LG
1816 }
1817
1818 return 0;
1819
1820hw_free:
1821 dpcm_be_dai_hw_free(fe, stream);
1822close:
1823 dpcm_be_dai_shutdown(fe, stream);
1824disconnect:
1825 /* disconnect any non started BEs */
1826 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1827 struct snd_soc_pcm_runtime *be = dpcm->be;
1828 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1829 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1830 }
1831
1832 return ret;
1833}
1834
1835static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1836{
1837 int ret;
1838
1839 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1840 ret = dpcm_run_update_startup(fe, stream);
1841 if (ret < 0)
103d84a3 1842 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
618dae11
LG
1843 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1844
1845 return ret;
1846}
1847
1848static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1849{
1850 int ret;
1851
1852 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1853 ret = dpcm_run_update_shutdown(fe, stream);
1854 if (ret < 0)
103d84a3 1855 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
618dae11
LG
1856 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1857
1858 return ret;
1859}
1860
1861/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1862 * any DAI links.
1863 */
c3f48ae6 1864int soc_dpcm_runtime_update(struct snd_soc_card *card)
618dae11 1865{
618dae11
LG
1866 int i, old, new, paths;
1867
618dae11
LG
1868 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1869 for (i = 0; i < card->num_rtd; i++) {
1870 struct snd_soc_dapm_widget_list *list;
1871 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1872
1873 /* make sure link is FE */
1874 if (!fe->dai_link->dynamic)
1875 continue;
1876
1877 /* only check active links */
1878 if (!fe->cpu_dai->active)
1879 continue;
1880
1881 /* DAPM sync will call this to update DSP paths */
103d84a3 1882 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
618dae11
LG
1883 fe->dai_link->name);
1884
1885 /* skip if FE doesn't have playback capability */
1886 if (!fe->cpu_dai->driver->playback.channels_min)
1887 goto capture;
1888
1889 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1890 if (paths < 0) {
103d84a3 1891 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
618dae11
LG
1892 fe->dai_link->name, "playback");
1893 mutex_unlock(&card->mutex);
1894 return paths;
1895 }
1896
1897 /* update any new playback paths */
1898 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1899 if (new) {
1900 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1901 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1902 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1903 }
1904
1905 /* update any old playback paths */
1906 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1907 if (old) {
1908 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1909 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1910 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1911 }
1912
1913capture:
1914 /* skip if FE doesn't have capture capability */
1915 if (!fe->cpu_dai->driver->capture.channels_min)
1916 continue;
1917
1918 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1919 if (paths < 0) {
103d84a3 1920 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
618dae11
LG
1921 fe->dai_link->name, "capture");
1922 mutex_unlock(&card->mutex);
1923 return paths;
1924 }
1925
1926 /* update any new capture paths */
1927 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1928 if (new) {
1929 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1930 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1931 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1932 }
1933
1934 /* update any old capture paths */
1935 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1936 if (old) {
1937 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1938 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1939 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1940 }
1941
1942 dpcm_path_put(&list);
1943 }
1944
1945 mutex_unlock(&card->mutex);
1946 return 0;
1947}
01d7584c
LG
1948int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1949{
1950 struct snd_soc_dpcm *dpcm;
1951 struct list_head *clients =
1952 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1953
1954 list_for_each_entry(dpcm, clients, list_be) {
1955
1956 struct snd_soc_pcm_runtime *be = dpcm->be;
1957 struct snd_soc_dai *dai = be->codec_dai;
1958 struct snd_soc_dai_driver *drv = dai->driver;
1959
1960 if (be->dai_link->ignore_suspend)
1961 continue;
1962
103d84a3 1963 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
01d7584c 1964
c5914b0a
MB
1965 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1966 drv->ops->digital_mute(dai, mute);
01d7584c
LG
1967 }
1968
1969 return 0;
1970}
1971
45c0a188 1972static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
01d7584c
LG
1973{
1974 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1975 struct snd_soc_dpcm *dpcm;
1976 struct snd_soc_dapm_widget_list *list;
1977 int ret;
1978 int stream = fe_substream->stream;
1979
1980 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1981 fe->dpcm[stream].runtime = fe_substream->runtime;
1982
1983 if (dpcm_path_get(fe, stream, &list) <= 0) {
103d84a3 1984 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
01d7584c 1985 fe->dai_link->name, stream ? "capture" : "playback");
01d7584c
LG
1986 }
1987
1988 /* calculate valid and active FE <-> BE dpcms */
1989 dpcm_process_paths(fe, stream, &list, 1);
1990
1991 ret = dpcm_fe_dai_startup(fe_substream);
1992 if (ret < 0) {
1993 /* clean up all links */
1994 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1995 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1996
1997 dpcm_be_disconnect(fe, stream);
1998 fe->dpcm[stream].runtime = NULL;
1999 }
2000
2001 dpcm_clear_pending_state(fe, stream);
2002 dpcm_path_put(&list);
2003 mutex_unlock(&fe->card->mutex);
2004 return ret;
2005}
2006
45c0a188 2007static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
01d7584c
LG
2008{
2009 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2010 struct snd_soc_dpcm *dpcm;
2011 int stream = fe_substream->stream, ret;
2012
2013 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2014 ret = dpcm_fe_dai_shutdown(fe_substream);
2015
2016 /* mark FE's links ready to prune */
2017 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2018 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2019
2020 dpcm_be_disconnect(fe, stream);
2021
2022 fe->dpcm[stream].runtime = NULL;
2023 mutex_unlock(&fe->card->mutex);
2024 return ret;
2025}
2026
ddee627c
LG
2027/* create a new pcm */
2028int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2029{
ddee627c
LG
2030 struct snd_soc_platform *platform = rtd->platform;
2031 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2032 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2033 struct snd_pcm *pcm;
2034 char new_name[64];
2035 int ret = 0, playback = 0, capture = 0;
2036
01d7584c 2037 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
1e9de42f
LG
2038 playback = rtd->dai_link->dpcm_playback;
2039 capture = rtd->dai_link->dpcm_capture;
01d7584c 2040 } else {
05679092
MB
2041 if (codec_dai->driver->playback.channels_min &&
2042 cpu_dai->driver->playback.channels_min)
01d7584c 2043 playback = 1;
05679092
MB
2044 if (codec_dai->driver->capture.channels_min &&
2045 cpu_dai->driver->capture.channels_min)
01d7584c
LG
2046 capture = 1;
2047 }
2048
d6bead02
FE
2049 if (rtd->dai_link->playback_only) {
2050 playback = 1;
2051 capture = 0;
2052 }
2053
2054 if (rtd->dai_link->capture_only) {
2055 playback = 0;
2056 capture = 1;
2057 }
2058
01d7584c
LG
2059 /* create the PCM */
2060 if (rtd->dai_link->no_pcm) {
2061 snprintf(new_name, sizeof(new_name), "(%s)",
2062 rtd->dai_link->stream_name);
2063
2064 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2065 playback, capture, &pcm);
2066 } else {
2067 if (rtd->dai_link->dynamic)
2068 snprintf(new_name, sizeof(new_name), "%s (*)",
2069 rtd->dai_link->stream_name);
2070 else
2071 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2072 rtd->dai_link->stream_name, codec_dai->name, num);
2073
2074 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2075 capture, &pcm);
2076 }
ddee627c 2077 if (ret < 0) {
103d84a3 2078 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
5cb9b748 2079 rtd->dai_link->name);
ddee627c
LG
2080 return ret;
2081 }
103d84a3 2082 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
ddee627c
LG
2083
2084 /* DAPM dai link stream work */
2085 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2086
2087 rtd->pcm = pcm;
2088 pcm->private_data = rtd;
01d7584c
LG
2089
2090 if (rtd->dai_link->no_pcm) {
2091 if (playback)
2092 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2093 if (capture)
2094 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2095 goto out;
2096 }
2097
2098 /* ASoC PCM operations */
2099 if (rtd->dai_link->dynamic) {
2100 rtd->ops.open = dpcm_fe_dai_open;
2101 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2102 rtd->ops.prepare = dpcm_fe_dai_prepare;
2103 rtd->ops.trigger = dpcm_fe_dai_trigger;
2104 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2105 rtd->ops.close = dpcm_fe_dai_close;
2106 rtd->ops.pointer = soc_pcm_pointer;
be3f3f2c 2107 rtd->ops.ioctl = soc_pcm_ioctl;
01d7584c
LG
2108 } else {
2109 rtd->ops.open = soc_pcm_open;
2110 rtd->ops.hw_params = soc_pcm_hw_params;
2111 rtd->ops.prepare = soc_pcm_prepare;
2112 rtd->ops.trigger = soc_pcm_trigger;
2113 rtd->ops.hw_free = soc_pcm_hw_free;
2114 rtd->ops.close = soc_pcm_close;
2115 rtd->ops.pointer = soc_pcm_pointer;
be3f3f2c 2116 rtd->ops.ioctl = soc_pcm_ioctl;
01d7584c
LG
2117 }
2118
ddee627c 2119 if (platform->driver->ops) {
01d7584c
LG
2120 rtd->ops.ack = platform->driver->ops->ack;
2121 rtd->ops.copy = platform->driver->ops->copy;
2122 rtd->ops.silence = platform->driver->ops->silence;
2123 rtd->ops.page = platform->driver->ops->page;
2124 rtd->ops.mmap = platform->driver->ops->mmap;
ddee627c
LG
2125 }
2126
2127 if (playback)
01d7584c 2128 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
ddee627c
LG
2129
2130 if (capture)
01d7584c 2131 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
ddee627c
LG
2132
2133 if (platform->driver->pcm_new) {
2134 ret = platform->driver->pcm_new(rtd);
2135 if (ret < 0) {
b1bc7b3c
MB
2136 dev_err(platform->dev,
2137 "ASoC: pcm constructor failed: %d\n",
2138 ret);
ddee627c
LG
2139 return ret;
2140 }
2141 }
2142
2143 pcm->private_free = platform->driver->pcm_free;
01d7584c 2144out:
7cc302d2 2145 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
ddee627c
LG
2146 cpu_dai->name);
2147 return ret;
2148}
01d7584c
LG
2149
2150/* is the current PCM operation for this FE ? */
2151int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2152{
2153 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2154 return 1;
2155 return 0;
2156}
2157EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2158
2159/* is the current PCM operation for this BE ? */
2160int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2161 struct snd_soc_pcm_runtime *be, int stream)
2162{
2163 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2164 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2165 be->dpcm[stream].runtime_update))
2166 return 1;
2167 return 0;
2168}
2169EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2170
2171/* get the substream for this BE */
2172struct snd_pcm_substream *
2173 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2174{
2175 return be->pcm->streams[stream].substream;
2176}
2177EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2178
2179/* get the BE runtime state */
2180enum snd_soc_dpcm_state
2181 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2182{
2183 return be->dpcm[stream].state;
2184}
2185EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2186
2187/* set the BE runtime state */
2188void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2189 int stream, enum snd_soc_dpcm_state state)
2190{
2191 be->dpcm[stream].state = state;
2192}
2193EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2194
2195/*
2196 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2197 * are not running, paused or suspended for the specified stream direction.
2198 */
2199int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2200 struct snd_soc_pcm_runtime *be, int stream)
2201{
2202 struct snd_soc_dpcm *dpcm;
2203 int state;
2204
2205 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2206
2207 if (dpcm->fe == fe)
2208 continue;
2209
2210 state = dpcm->fe->dpcm[stream].state;
2211 if (state == SND_SOC_DPCM_STATE_START ||
2212 state == SND_SOC_DPCM_STATE_PAUSED ||
2213 state == SND_SOC_DPCM_STATE_SUSPEND)
2214 return 0;
2215 }
2216
2217 /* it's safe to free/stop this BE DAI */
2218 return 1;
2219}
2220EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2221
2222/*
2223 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2224 * running, paused or suspended for the specified stream direction.
2225 */
2226int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2227 struct snd_soc_pcm_runtime *be, int stream)
2228{
2229 struct snd_soc_dpcm *dpcm;
2230 int state;
2231
2232 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2233
2234 if (dpcm->fe == fe)
2235 continue;
2236
2237 state = dpcm->fe->dpcm[stream].state;
2238 if (state == SND_SOC_DPCM_STATE_START ||
2239 state == SND_SOC_DPCM_STATE_PAUSED ||
2240 state == SND_SOC_DPCM_STATE_SUSPEND ||
2241 state == SND_SOC_DPCM_STATE_PREPARE)
2242 return 0;
2243 }
2244
2245 /* it's safe to change hw_params */
2246 return 1;
2247}
2248EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
f86dcef8 2249
07bf84aa
LG
2250int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2251 int cmd, struct snd_soc_platform *platform)
2252{
c5914b0a 2253 if (platform->driver->ops && platform->driver->ops->trigger)
07bf84aa
LG
2254 return platform->driver->ops->trigger(substream, cmd);
2255 return 0;
2256}
2257EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2258
f86dcef8
LG
2259#ifdef CONFIG_DEBUG_FS
2260static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2261{
2262 switch (state) {
2263 case SND_SOC_DPCM_STATE_NEW:
2264 return "new";
2265 case SND_SOC_DPCM_STATE_OPEN:
2266 return "open";
2267 case SND_SOC_DPCM_STATE_HW_PARAMS:
2268 return "hw_params";
2269 case SND_SOC_DPCM_STATE_PREPARE:
2270 return "prepare";
2271 case SND_SOC_DPCM_STATE_START:
2272 return "start";
2273 case SND_SOC_DPCM_STATE_STOP:
2274 return "stop";
2275 case SND_SOC_DPCM_STATE_SUSPEND:
2276 return "suspend";
2277 case SND_SOC_DPCM_STATE_PAUSED:
2278 return "paused";
2279 case SND_SOC_DPCM_STATE_HW_FREE:
2280 return "hw_free";
2281 case SND_SOC_DPCM_STATE_CLOSE:
2282 return "close";
2283 }
2284
2285 return "unknown";
2286}
2287
f86dcef8
LG
2288static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2289 int stream, char *buf, size_t size)
2290{
2291 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2292 struct snd_soc_dpcm *dpcm;
2293 ssize_t offset = 0;
2294
2295 /* FE state */
2296 offset += snprintf(buf + offset, size - offset,
2297 "[%s - %s]\n", fe->dai_link->name,
2298 stream ? "Capture" : "Playback");
2299
2300 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2301 dpcm_state_string(fe->dpcm[stream].state));
2302
2303 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2304 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2305 offset += snprintf(buf + offset, size - offset,
2306 "Hardware Params: "
2307 "Format = %s, Channels = %d, Rate = %d\n",
2308 snd_pcm_format_name(params_format(params)),
2309 params_channels(params),
2310 params_rate(params));
2311
2312 /* BEs state */
2313 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2314
2315 if (list_empty(&fe->dpcm[stream].be_clients)) {
2316 offset += snprintf(buf + offset, size - offset,
2317 " No active DSP links\n");
2318 goto out;
2319 }
2320
2321 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2322 struct snd_soc_pcm_runtime *be = dpcm->be;
2323 params = &dpcm->hw_params;
2324
2325 offset += snprintf(buf + offset, size - offset,
2326 "- %s\n", be->dai_link->name);
2327
2328 offset += snprintf(buf + offset, size - offset,
2329 " State: %s\n",
2330 dpcm_state_string(be->dpcm[stream].state));
2331
2332 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2333 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2334 offset += snprintf(buf + offset, size - offset,
2335 " Hardware Params: "
2336 "Format = %s, Channels = %d, Rate = %d\n",
2337 snd_pcm_format_name(params_format(params)),
2338 params_channels(params),
2339 params_rate(params));
2340 }
2341
2342out:
2343 return offset;
2344}
2345
2346static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2347 size_t count, loff_t *ppos)
2348{
2349 struct snd_soc_pcm_runtime *fe = file->private_data;
2350 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2351 char *buf;
2352
2353 buf = kmalloc(out_count, GFP_KERNEL);
2354 if (!buf)
2355 return -ENOMEM;
2356
2357 if (fe->cpu_dai->driver->playback.channels_min)
2358 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2359 buf + offset, out_count - offset);
2360
2361 if (fe->cpu_dai->driver->capture.channels_min)
2362 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2363 buf + offset, out_count - offset);
2364
f57b8488 2365 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
f86dcef8 2366
f57b8488
LG
2367 kfree(buf);
2368 return ret;
f86dcef8
LG
2369}
2370
2371static const struct file_operations dpcm_state_fops = {
f57b8488 2372 .open = simple_open,
f86dcef8
LG
2373 .read = dpcm_state_read_file,
2374 .llseek = default_llseek,
2375};
2376
2377int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2378{
b3bba9a1
MB
2379 if (!rtd->dai_link)
2380 return 0;
2381
f86dcef8
LG
2382 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2383 rtd->card->debugfs_card_root);
2384 if (!rtd->debugfs_dpcm_root) {
2385 dev_dbg(rtd->dev,
2386 "ASoC: Failed to create dpcm debugfs directory %s\n",
2387 rtd->dai_link->name);
2388 return -EINVAL;
2389 }
2390
f57b8488 2391 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
f86dcef8
LG
2392 rtd->debugfs_dpcm_root,
2393 rtd, &dpcm_state_fops);
2394
2395 return 0;
2396}
2397#endif
This page took 0.244267 seconds and 5 git commands to generate.