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