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