Merge remote-tracking branches 'asoc/topic/nau8825' and 'asoc/topic/pxa' into asoc...
[deliverable/linux.git] / sound / soc / soc-pcm.c
CommitLineData
ddee627c
LG
1/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
c8dd1fec 10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
ddee627c
LG
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
988e8cc4 22#include <linux/pinctrl/consumer.h>
d6652ef8 23#include <linux/pm_runtime.h>
ddee627c
LG
24#include <linux/slab.h>
25#include <linux/workqueue.h>
01d7584c 26#include <linux/export.h>
f86dcef8 27#include <linux/debugfs.h>
ddee627c
LG
28#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
01d7584c 32#include <sound/soc-dpcm.h>
ddee627c
LG
33#include <sound/initval.h>
34
01d7584c
LG
35#define DPCM_MAX_BE_USERS 8
36
cde79035
RW
37/*
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39 *
40 * Returns true if the DAI supports the indicated stream type.
41 */
42static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43{
44 struct snd_soc_pcm_stream *codec_stream;
45
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
48 else
49 codec_stream = &dai->driver->capture;
50
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
53}
54
24894b76
LPC
55/**
56 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
59 *
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
62 *
63 * Must be called with the rtd->pcm_mutex being held
64 */
65void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
66{
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7 68 int i;
24894b76
LPC
69
70 lockdep_assert_held(&rtd->pcm_mutex);
71
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
2e5894d7
BC
74 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
24894b76
LPC
76 } else {
77 cpu_dai->capture_active++;
2e5894d7
BC
78 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
24894b76
LPC
80 }
81
82 cpu_dai->active++;
cdde4ccb 83 cpu_dai->component->active++;
2e5894d7
BC
84 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
87 }
24894b76
LPC
88}
89
90/**
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
94 *
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
97 *
98 * Must be called with the rtd->pcm_mutex being held
99 */
100void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
101{
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7 103 int i;
24894b76
LPC
104
105 lockdep_assert_held(&rtd->pcm_mutex);
106
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
2e5894d7
BC
109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
24894b76
LPC
111 } else {
112 cpu_dai->capture_active--;
2e5894d7
BC
113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
24894b76
LPC
115 }
116
117 cpu_dai->active--;
cdde4ccb 118 cpu_dai->component->active--;
2e5894d7
BC
119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
122 }
24894b76
LPC
123}
124
208a1589
LPC
125/**
126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
128 *
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
133 */
134bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135{
2e5894d7
BC
136 int i;
137 bool ignore = true;
138
208a1589
LPC
139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140 return true;
141
2e5894d7
BC
142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
144
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
208a1589
LPC
146}
147
90996f43
LPC
148/**
149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
152 *
153 * Sets the substream runtime hardware parameters.
154 */
155int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
157{
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
167 return 0;
168}
169EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
170
01d7584c 171/* DPCM stream event, send event to FE and all active BEs. */
23607025 172int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
01d7584c
LG
173 int event)
174{
175 struct snd_soc_dpcm *dpcm;
176
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
178
179 struct snd_soc_pcm_runtime *be = dpcm->be;
180
103d84a3 181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
01d7584c
LG
182 be->dai_link->name, event, dir);
183
184 snd_soc_dapm_stream_event(be, dir, event);
185 }
186
187 snd_soc_dapm_stream_event(fe, dir, event);
188
189 return 0;
190}
191
17841020
DA
192static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
193 struct snd_soc_dai *soc_dai)
ddee627c
LG
194{
195 struct snd_soc_pcm_runtime *rtd = substream->private_data;
ddee627c
LG
196 int ret;
197
3635bf09
NC
198 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
199 rtd->dai_link->symmetric_rates)) {
200 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
201 soc_dai->rate);
202
203 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
204 SNDRV_PCM_HW_PARAM_RATE,
205 soc_dai->rate, soc_dai->rate);
206 if (ret < 0) {
207 dev_err(soc_dai->dev,
208 "ASoC: Unable to apply rate constraint: %d\n",
209 ret);
210 return ret;
211 }
212 }
ddee627c 213
3635bf09
NC
214 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
215 rtd->dai_link->symmetric_channels)) {
216 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
217 soc_dai->channels);
ddee627c 218
3635bf09
NC
219 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
220 SNDRV_PCM_HW_PARAM_CHANNELS,
221 soc_dai->channels,
222 soc_dai->channels);
223 if (ret < 0) {
224 dev_err(soc_dai->dev,
225 "ASoC: Unable to apply channel symmetry constraint: %d\n",
226 ret);
227 return ret;
228 }
ddee627c
LG
229 }
230
3635bf09
NC
231 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
232 rtd->dai_link->symmetric_samplebits)) {
233 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
234 soc_dai->sample_bits);
ddee627c 235
3635bf09
NC
236 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
237 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
238 soc_dai->sample_bits,
239 soc_dai->sample_bits);
240 if (ret < 0) {
241 dev_err(soc_dai->dev,
242 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
243 ret);
244 return ret;
245 }
ddee627c
LG
246 }
247
248 return 0;
249}
250
3635bf09
NC
251static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
252 struct snd_pcm_hw_params *params)
253{
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7 256 unsigned int rate, channels, sample_bits, symmetry, i;
3635bf09
NC
257
258 rate = params_rate(params);
259 channels = params_channels(params);
260 sample_bits = snd_pcm_format_physical_width(params_format(params));
261
262 /* reject unmatched parameters when applying symmetry */
263 symmetry = cpu_dai->driver->symmetric_rates ||
3635bf09 264 rtd->dai_link->symmetric_rates;
2e5894d7
BC
265
266 for (i = 0; i < rtd->num_codecs; i++)
267 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
268
3635bf09
NC
269 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
270 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
271 cpu_dai->rate, rate);
272 return -EINVAL;
ddee627c
LG
273 }
274
3635bf09 275 symmetry = cpu_dai->driver->symmetric_channels ||
3635bf09 276 rtd->dai_link->symmetric_channels;
2e5894d7
BC
277
278 for (i = 0; i < rtd->num_codecs; i++)
279 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
280
3635bf09
NC
281 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
282 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
283 cpu_dai->channels, channels);
284 return -EINVAL;
285 }
ddee627c 286
3635bf09 287 symmetry = cpu_dai->driver->symmetric_samplebits ||
3635bf09 288 rtd->dai_link->symmetric_samplebits;
2e5894d7
BC
289
290 for (i = 0; i < rtd->num_codecs; i++)
291 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
292
3635bf09
NC
293 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
294 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
295 cpu_dai->sample_bits, sample_bits);
296 return -EINVAL;
ddee627c
LG
297 }
298
299 return 0;
300}
301
62e5f676
LPC
302static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
303{
304 struct snd_soc_pcm_runtime *rtd = substream->private_data;
305 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
62e5f676 306 struct snd_soc_dai_link *link = rtd->dai_link;
2e5894d7 307 unsigned int symmetry, i;
62e5f676 308
2e5894d7
BC
309 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
310 cpu_driver->symmetric_channels || link->symmetric_channels ||
311 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
62e5f676 312
2e5894d7
BC
313 for (i = 0; i < rtd->num_codecs; i++)
314 symmetry = symmetry ||
315 rtd->codec_dais[i]->driver->symmetric_rates ||
316 rtd->codec_dais[i]->driver->symmetric_channels ||
317 rtd->codec_dais[i]->driver->symmetric_samplebits;
318
319 return symmetry;
62e5f676
LPC
320}
321
2e5894d7 322static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
58ba9b25 323{
2e5894d7 324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
c6068d3a 325 int ret;
58ba9b25
MB
326
327 if (!bits)
328 return;
329
0e2a3751
LPC
330 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
331 if (ret != 0)
332 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
333 bits, ret);
58ba9b25
MB
334}
335
c8dd1fec
BC
336static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
337{
338 struct snd_soc_pcm_runtime *rtd = substream->private_data;
339 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7
BC
340 struct snd_soc_dai *codec_dai;
341 int i;
c8dd1fec
BC
342 unsigned int bits = 0, cpu_bits;
343
344 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
2e5894d7
BC
345 for (i = 0; i < rtd->num_codecs; i++) {
346 codec_dai = rtd->codec_dais[i];
347 if (codec_dai->driver->playback.sig_bits == 0) {
348 bits = 0;
349 break;
350 }
351 bits = max(codec_dai->driver->playback.sig_bits, bits);
352 }
c8dd1fec
BC
353 cpu_bits = cpu_dai->driver->playback.sig_bits;
354 } else {
2e5894d7
BC
355 for (i = 0; i < rtd->num_codecs; i++) {
356 codec_dai = rtd->codec_dais[i];
5e63dfcc 357 if (codec_dai->driver->capture.sig_bits == 0) {
2e5894d7
BC
358 bits = 0;
359 break;
360 }
361 bits = max(codec_dai->driver->capture.sig_bits, bits);
362 }
c8dd1fec
BC
363 cpu_bits = cpu_dai->driver->capture.sig_bits;
364 }
365
2e5894d7
BC
366 soc_pcm_set_msb(substream, bits);
367 soc_pcm_set_msb(substream, cpu_bits);
c8dd1fec
BC
368}
369
2e5894d7 370static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
bd477c31 371{
2e5894d7 372 struct snd_pcm_runtime *runtime = substream->runtime;
78e45c99 373 struct snd_pcm_hardware *hw = &runtime->hw;
2e5894d7
BC
374 struct snd_soc_pcm_runtime *rtd = substream->private_data;
375 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
376 struct snd_soc_dai_driver *codec_dai_drv;
377 struct snd_soc_pcm_stream *codec_stream;
378 struct snd_soc_pcm_stream *cpu_stream;
379 unsigned int chan_min = 0, chan_max = UINT_MAX;
380 unsigned int rate_min = 0, rate_max = UINT_MAX;
381 unsigned int rates = UINT_MAX;
382 u64 formats = ULLONG_MAX;
383 int i;
78e45c99 384
2e5894d7
BC
385 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
386 cpu_stream = &cpu_dai_drv->playback;
16d7ea91 387 else
2e5894d7 388 cpu_stream = &cpu_dai_drv->capture;
78e45c99 389
2e5894d7
BC
390 /* first calculate min/max only for CODECs in the DAI link */
391 for (i = 0; i < rtd->num_codecs; i++) {
cde79035
RW
392
393 /*
394 * Skip CODECs which don't support the current stream type.
395 * Otherwise, since the rate, channel, and format values will
396 * zero in that case, we would have no usable settings left,
397 * causing the resulting setup to fail.
398 * At least one CODEC should match, otherwise we should have
399 * bailed out on a higher level, since there would be no
400 * CODEC to support the transfer direction in that case.
401 */
402 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
403 substream->stream))
404 continue;
405
2e5894d7
BC
406 codec_dai_drv = rtd->codec_dais[i]->driver;
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
408 codec_stream = &codec_dai_drv->playback;
409 else
410 codec_stream = &codec_dai_drv->capture;
411 chan_min = max(chan_min, codec_stream->channels_min);
412 chan_max = min(chan_max, codec_stream->channels_max);
413 rate_min = max(rate_min, codec_stream->rate_min);
414 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
415 formats &= codec_stream->formats;
416 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
417 }
418
419 /*
420 * chan min/max cannot be enforced if there are multiple CODEC DAIs
421 * connected to a single CPU DAI, use CPU DAI's directly and let
422 * channel allocation be fixed up later
423 */
424 if (rtd->num_codecs > 1) {
425 chan_min = cpu_stream->channels_min;
426 chan_max = cpu_stream->channels_max;
427 }
428
429 hw->channels_min = max(chan_min, cpu_stream->channels_min);
430 hw->channels_max = min(chan_max, cpu_stream->channels_max);
431 if (hw->formats)
432 hw->formats &= formats & cpu_stream->formats;
433 else
434 hw->formats = formats & cpu_stream->formats;
435 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
78e45c99
LPC
436
437 snd_pcm_limit_hw_rates(runtime);
438
439 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
2e5894d7 440 hw->rate_min = max(hw->rate_min, rate_min);
78e45c99 441 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
2e5894d7 442 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
bd477c31
LPC
443}
444
ddee627c
LG
445/*
446 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
447 * then initialized and any private data can be allocated. This also calls
448 * startup for the cpu DAI, platform, machine and codec DAI.
449 */
450static int soc_pcm_open(struct snd_pcm_substream *substream)
451{
452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
453 struct snd_pcm_runtime *runtime = substream->runtime;
454 struct snd_soc_platform *platform = rtd->platform;
455 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7
BC
456 struct snd_soc_dai *codec_dai;
457 const char *codec_dai_name = "multicodec";
458 int i, ret = 0;
ddee627c 459
988e8cc4 460 pinctrl_pm_select_default_state(cpu_dai->dev);
2e5894d7
BC
461 for (i = 0; i < rtd->num_codecs; i++)
462 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
d6652ef8 463 pm_runtime_get_sync(cpu_dai->dev);
2e5894d7
BC
464 for (i = 0; i < rtd->num_codecs; i++)
465 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
d6652ef8
MB
466 pm_runtime_get_sync(platform->dev);
467
b8c0dab9 468 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
469
470 /* startup the audio subsystem */
c5914b0a 471 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
ddee627c
LG
472 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
473 if (ret < 0) {
103d84a3
LG
474 dev_err(cpu_dai->dev, "ASoC: can't open interface"
475 " %s: %d\n", cpu_dai->name, ret);
ddee627c
LG
476 goto out;
477 }
478 }
479
480 if (platform->driver->ops && platform->driver->ops->open) {
481 ret = platform->driver->ops->open(substream);
482 if (ret < 0) {
103d84a3 483 dev_err(platform->dev, "ASoC: can't open platform"
f4333203 484 " %s: %d\n", platform->component.name, ret);
ddee627c
LG
485 goto platform_err;
486 }
487 }
488
2e5894d7
BC
489 for (i = 0; i < rtd->num_codecs; i++) {
490 codec_dai = rtd->codec_dais[i];
491 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
492 ret = codec_dai->driver->ops->startup(substream,
493 codec_dai);
494 if (ret < 0) {
495 dev_err(codec_dai->dev,
496 "ASoC: can't open codec %s: %d\n",
497 codec_dai->name, ret);
498 goto codec_dai_err;
499 }
ddee627c 500 }
2e5894d7
BC
501
502 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
503 codec_dai->tx_mask = 0;
504 else
505 codec_dai->rx_mask = 0;
ddee627c
LG
506 }
507
508 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
509 ret = rtd->dai_link->ops->startup(substream);
510 if (ret < 0) {
103d84a3 511 pr_err("ASoC: %s startup failed: %d\n",
25bfe662 512 rtd->dai_link->name, ret);
ddee627c
LG
513 goto machine_err;
514 }
515 }
516
01d7584c
LG
517 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
518 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
519 goto dynamic;
520
ddee627c 521 /* Check that the codec and cpu DAIs are compatible */
2e5894d7
BC
522 soc_pcm_init_runtime_hw(substream);
523
524 if (rtd->num_codecs == 1)
525 codec_dai_name = rtd->codec_dai->name;
ddee627c 526
62e5f676
LPC
527 if (soc_pcm_has_symmetry(substream))
528 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
529
ddee627c 530 ret = -EINVAL;
ddee627c 531 if (!runtime->hw.rates) {
103d84a3 532 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
2e5894d7 533 codec_dai_name, cpu_dai->name);
ddee627c
LG
534 goto config_err;
535 }
536 if (!runtime->hw.formats) {
103d84a3 537 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
2e5894d7 538 codec_dai_name, cpu_dai->name);
ddee627c
LG
539 goto config_err;
540 }
541 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
542 runtime->hw.channels_min > runtime->hw.channels_max) {
103d84a3 543 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
2e5894d7 544 codec_dai_name, cpu_dai->name);
ddee627c
LG
545 goto config_err;
546 }
547
c8dd1fec 548 soc_pcm_apply_msb(substream);
58ba9b25 549
ddee627c 550 /* Symmetry only applies if we've already got an active stream. */
17841020
DA
551 if (cpu_dai->active) {
552 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
553 if (ret != 0)
554 goto config_err;
555 }
556
2e5894d7
BC
557 for (i = 0; i < rtd->num_codecs; i++) {
558 if (rtd->codec_dais[i]->active) {
559 ret = soc_pcm_apply_symmetry(substream,
560 rtd->codec_dais[i]);
561 if (ret != 0)
562 goto config_err;
563 }
ddee627c
LG
564 }
565
103d84a3 566 pr_debug("ASoC: %s <-> %s info:\n",
2e5894d7 567 codec_dai_name, cpu_dai->name);
103d84a3
LG
568 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
569 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
ddee627c 570 runtime->hw.channels_max);
103d84a3 571 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
ddee627c
LG
572 runtime->hw.rate_max);
573
01d7584c 574dynamic:
24894b76
LPC
575
576 snd_soc_runtime_activate(rtd, substream->stream);
577
b8c0dab9 578 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
579 return 0;
580
581config_err:
582 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
583 rtd->dai_link->ops->shutdown(substream);
584
585machine_err:
2e5894d7 586 i = rtd->num_codecs;
ddee627c
LG
587
588codec_dai_err:
2e5894d7
BC
589 while (--i >= 0) {
590 codec_dai = rtd->codec_dais[i];
591 if (codec_dai->driver->ops->shutdown)
592 codec_dai->driver->ops->shutdown(substream, codec_dai);
593 }
594
ddee627c
LG
595 if (platform->driver->ops && platform->driver->ops->close)
596 platform->driver->ops->close(substream);
597
598platform_err:
599 if (cpu_dai->driver->ops->shutdown)
600 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
601out:
b8c0dab9 602 mutex_unlock(&rtd->pcm_mutex);
d6652ef8
MB
603
604 pm_runtime_put(platform->dev);
2e5894d7
BC
605 for (i = 0; i < rtd->num_codecs; i++)
606 pm_runtime_put(rtd->codec_dais[i]->dev);
d6652ef8 607 pm_runtime_put(cpu_dai->dev);
2e5894d7
BC
608 for (i = 0; i < rtd->num_codecs; i++) {
609 if (!rtd->codec_dais[i]->active)
610 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
611 }
988e8cc4
NC
612 if (!cpu_dai->active)
613 pinctrl_pm_select_sleep_state(cpu_dai->dev);
d6652ef8 614
ddee627c
LG
615 return ret;
616}
617
618/*
619 * Power down the audio subsystem pmdown_time msecs after close is called.
620 * This is to ensure there are no pops or clicks in between any music tracks
621 * due to DAPM power cycling.
622 */
623static void close_delayed_work(struct work_struct *work)
624{
625 struct snd_soc_pcm_runtime *rtd =
626 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
2e5894d7 627 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
ddee627c 628
b8c0dab9 629 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c 630
103d84a3 631 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
ddee627c
LG
632 codec_dai->driver->playback.stream_name,
633 codec_dai->playback_active ? "active" : "inactive",
9bffb1fb 634 rtd->pop_wait ? "yes" : "no");
ddee627c
LG
635
636 /* are we waiting on this codec DAI stream */
9bffb1fb
MLC
637 if (rtd->pop_wait == 1) {
638 rtd->pop_wait = 0;
7bd3a6f3 639 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
d9b0951b 640 SND_SOC_DAPM_STREAM_STOP);
ddee627c
LG
641 }
642
b8c0dab9 643 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
644}
645
646/*
647 * Called by ALSA when a PCM substream is closed. Private data can be
648 * freed here. The cpu DAI, codec DAI, machine and platform are also
649 * shutdown.
650 */
91d5e6b4 651static int soc_pcm_close(struct snd_pcm_substream *substream)
ddee627c
LG
652{
653 struct snd_soc_pcm_runtime *rtd = substream->private_data;
654 struct snd_soc_platform *platform = rtd->platform;
655 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7
BC
656 struct snd_soc_dai *codec_dai;
657 int i;
ddee627c 658
b8c0dab9 659 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c 660
24894b76 661 snd_soc_runtime_deactivate(rtd, substream->stream);
ddee627c 662
17841020
DA
663 /* clear the corresponding DAIs rate when inactive */
664 if (!cpu_dai->active)
665 cpu_dai->rate = 0;
666
2e5894d7
BC
667 for (i = 0; i < rtd->num_codecs; i++) {
668 codec_dai = rtd->codec_dais[i];
669 if (!codec_dai->active)
670 codec_dai->rate = 0;
671 }
25b76791 672
ae11601b
RB
673 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
674
ddee627c
LG
675 if (cpu_dai->driver->ops->shutdown)
676 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
677
2e5894d7
BC
678 for (i = 0; i < rtd->num_codecs; i++) {
679 codec_dai = rtd->codec_dais[i];
680 if (codec_dai->driver->ops->shutdown)
681 codec_dai->driver->ops->shutdown(substream, codec_dai);
682 }
ddee627c
LG
683
684 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
685 rtd->dai_link->ops->shutdown(substream);
686
687 if (platform->driver->ops && platform->driver->ops->close)
688 platform->driver->ops->close(substream);
ddee627c
LG
689
690 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
208a1589 691 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
1d69c5c5
PU
692 /* powered down playback stream now */
693 snd_soc_dapm_stream_event(rtd,
7bd3a6f3 694 SNDRV_PCM_STREAM_PLAYBACK,
7bd3a6f3 695 SND_SOC_DAPM_STREAM_STOP);
1d69c5c5
PU
696 } else {
697 /* start delayed pop wq here for playback streams */
9bffb1fb 698 rtd->pop_wait = 1;
d4e1a73a
MB
699 queue_delayed_work(system_power_efficient_wq,
700 &rtd->delayed_work,
701 msecs_to_jiffies(rtd->pmdown_time));
1d69c5c5 702 }
ddee627c
LG
703 } else {
704 /* capture streams can be powered down now */
7bd3a6f3 705 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
d9b0951b 706 SND_SOC_DAPM_STREAM_STOP);
ddee627c
LG
707 }
708
b8c0dab9 709 mutex_unlock(&rtd->pcm_mutex);
d6652ef8
MB
710
711 pm_runtime_put(platform->dev);
2e5894d7
BC
712 for (i = 0; i < rtd->num_codecs; i++)
713 pm_runtime_put(rtd->codec_dais[i]->dev);
d6652ef8 714 pm_runtime_put(cpu_dai->dev);
2e5894d7
BC
715 for (i = 0; i < rtd->num_codecs; i++) {
716 if (!rtd->codec_dais[i]->active)
717 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
718 }
988e8cc4
NC
719 if (!cpu_dai->active)
720 pinctrl_pm_select_sleep_state(cpu_dai->dev);
d6652ef8 721
ddee627c
LG
722 return 0;
723}
724
725/*
726 * Called by ALSA when the PCM substream is prepared, can set format, sample
727 * rate, etc. This function is non atomic and can be called multiple times,
728 * it can refer to the runtime info.
729 */
730static int soc_pcm_prepare(struct snd_pcm_substream *substream)
731{
732 struct snd_soc_pcm_runtime *rtd = substream->private_data;
733 struct snd_soc_platform *platform = rtd->platform;
734 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7
BC
735 struct snd_soc_dai *codec_dai;
736 int i, ret = 0;
ddee627c 737
b8c0dab9 738 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c
LG
739
740 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
741 ret = rtd->dai_link->ops->prepare(substream);
742 if (ret < 0) {
103d84a3
LG
743 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
744 " %d\n", ret);
ddee627c
LG
745 goto out;
746 }
747 }
748
749 if (platform->driver->ops && platform->driver->ops->prepare) {
750 ret = platform->driver->ops->prepare(substream);
751 if (ret < 0) {
103d84a3
LG
752 dev_err(platform->dev, "ASoC: platform prepare error:"
753 " %d\n", ret);
ddee627c
LG
754 goto out;
755 }
756 }
757
2e5894d7
BC
758 for (i = 0; i < rtd->num_codecs; i++) {
759 codec_dai = rtd->codec_dais[i];
760 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
761 ret = codec_dai->driver->ops->prepare(substream,
762 codec_dai);
763 if (ret < 0) {
764 dev_err(codec_dai->dev,
90cc7f1c
JN
765 "ASoC: codec DAI prepare error: %d\n",
766 ret);
2e5894d7
BC
767 goto out;
768 }
ddee627c
LG
769 }
770 }
771
c5914b0a 772 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
ddee627c
LG
773 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
774 if (ret < 0) {
90cc7f1c
JN
775 dev_err(cpu_dai->dev,
776 "ASoC: cpu DAI prepare error: %d\n", ret);
ddee627c
LG
777 goto out;
778 }
779 }
780
781 /* cancel any delayed stream shutdown that is pending */
782 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
9bffb1fb
MLC
783 rtd->pop_wait) {
784 rtd->pop_wait = 0;
ddee627c
LG
785 cancel_delayed_work(&rtd->delayed_work);
786 }
787
d9b0951b
LG
788 snd_soc_dapm_stream_event(rtd, substream->stream,
789 SND_SOC_DAPM_STREAM_START);
ddee627c 790
2e5894d7
BC
791 for (i = 0; i < rtd->num_codecs; i++)
792 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
793 substream->stream);
ae11601b 794 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
ddee627c
LG
795
796out:
b8c0dab9 797 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
798 return ret;
799}
800
2e5894d7
BC
801static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
802 unsigned int mask)
803{
804 struct snd_interval *interval;
805 int channels = hweight_long(mask);
806
807 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
808 interval->min = channels;
809 interval->max = channels;
810}
811
93e6958a
BC
812int soc_dai_hw_params(struct snd_pcm_substream *substream,
813 struct snd_pcm_hw_params *params,
814 struct snd_soc_dai *dai)
815{
816 int ret;
817
818 if (dai->driver->ops && dai->driver->ops->hw_params) {
819 ret = dai->driver->ops->hw_params(substream, params, dai);
820 if (ret < 0) {
821 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
822 dai->name, ret);
823 return ret;
824 }
825 }
826
827 return 0;
828}
829
ddee627c
LG
830/*
831 * Called by ALSA when the hardware params are set by application. This
832 * function can also be called multiple times and can allocate buffers
833 * (using snd_pcm_lib_* ). It's non-atomic.
834 */
835static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
836 struct snd_pcm_hw_params *params)
837{
838 struct snd_soc_pcm_runtime *rtd = substream->private_data;
839 struct snd_soc_platform *platform = rtd->platform;
840 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7 841 int i, ret = 0;
ddee627c 842
b8c0dab9 843 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c 844
3635bf09
NC
845 ret = soc_pcm_params_symmetry(substream, params);
846 if (ret)
847 goto out;
848
ddee627c
LG
849 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
850 ret = rtd->dai_link->ops->hw_params(substream, params);
851 if (ret < 0) {
103d84a3
LG
852 dev_err(rtd->card->dev, "ASoC: machine hw_params"
853 " failed: %d\n", ret);
ddee627c
LG
854 goto out;
855 }
856 }
857
2e5894d7
BC
858 for (i = 0; i < rtd->num_codecs; i++) {
859 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
860 struct snd_pcm_hw_params codec_params;
861
cde79035
RW
862 /*
863 * Skip CODECs which don't support the current stream type,
864 * the idea being that if a CODEC is not used for the currently
865 * set up transfer direction, it should not need to be
866 * configured, especially since the configuration used might
867 * not even be supported by that CODEC. There may be cases
868 * however where a CODEC needs to be set up although it is
869 * actually not being used for the transfer, e.g. if a
870 * capture-only CODEC is acting as an LRCLK and/or BCLK master
871 * for the DAI link including a playback-only CODEC.
872 * If this becomes necessary, we will have to augment the
873 * machine driver setup with information on how to act, so
874 * we can do the right thing here.
875 */
876 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
877 continue;
878
2e5894d7
BC
879 /* copy params for each codec */
880 codec_params = *params;
881
882 /* fixup params based on TDM slot masks */
883 if (codec_dai->tx_mask)
884 soc_pcm_codec_params_fixup(&codec_params,
885 codec_dai->tx_mask);
886 if (codec_dai->rx_mask)
887 soc_pcm_codec_params_fixup(&codec_params,
888 codec_dai->rx_mask);
889
93e6958a
BC
890 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
891 if(ret < 0)
ddee627c 892 goto codec_err;
ddee627c 893
2e5894d7
BC
894 codec_dai->rate = params_rate(&codec_params);
895 codec_dai->channels = params_channels(&codec_params);
896 codec_dai->sample_bits = snd_pcm_format_physical_width(
897 params_format(&codec_params));
ddee627c
LG
898 }
899
93e6958a
BC
900 ret = soc_dai_hw_params(substream, params, cpu_dai);
901 if (ret < 0)
902 goto interface_err;
ddee627c
LG
903
904 if (platform->driver->ops && platform->driver->ops->hw_params) {
905 ret = platform->driver->ops->hw_params(substream, params);
906 if (ret < 0) {
103d84a3 907 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
f4333203 908 platform->component.name, ret);
ddee627c
LG
909 goto platform_err;
910 }
911 }
912
3635bf09 913 /* store the parameters for each DAIs */
17841020 914 cpu_dai->rate = params_rate(params);
3635bf09
NC
915 cpu_dai->channels = params_channels(params);
916 cpu_dai->sample_bits =
917 snd_pcm_format_physical_width(params_format(params));
918
ddee627c 919out:
b8c0dab9 920 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
921 return ret;
922
923platform_err:
c5914b0a 924 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
ddee627c
LG
925 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
926
927interface_err:
2e5894d7 928 i = rtd->num_codecs;
ddee627c
LG
929
930codec_err:
2e5894d7
BC
931 while (--i >= 0) {
932 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
933 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
934 codec_dai->driver->ops->hw_free(substream, codec_dai);
935 codec_dai->rate = 0;
936 }
937
ddee627c
LG
938 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
939 rtd->dai_link->ops->hw_free(substream);
940
b8c0dab9 941 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
942 return ret;
943}
944
945/*
946 * Frees resources allocated by hw_params, can be called multiple times
947 */
948static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
949{
950 struct snd_soc_pcm_runtime *rtd = substream->private_data;
951 struct snd_soc_platform *platform = rtd->platform;
952 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7 953 struct snd_soc_dai *codec_dai;
7f62b6ee 954 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
2e5894d7 955 int i;
ddee627c 956
b8c0dab9 957 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
ddee627c 958
d3383420
NC
959 /* clear the corresponding DAIs parameters when going to be inactive */
960 if (cpu_dai->active == 1) {
961 cpu_dai->rate = 0;
962 cpu_dai->channels = 0;
963 cpu_dai->sample_bits = 0;
964 }
965
2e5894d7
BC
966 for (i = 0; i < rtd->num_codecs; i++) {
967 codec_dai = rtd->codec_dais[i];
968 if (codec_dai->active == 1) {
969 codec_dai->rate = 0;
970 codec_dai->channels = 0;
971 codec_dai->sample_bits = 0;
972 }
d3383420
NC
973 }
974
ddee627c 975 /* apply codec digital mute */
2e5894d7
BC
976 for (i = 0; i < rtd->num_codecs; i++) {
977 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
978 (!playback && rtd->codec_dais[i]->capture_active == 1))
979 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
980 substream->stream);
981 }
ddee627c
LG
982
983 /* free any machine hw params */
984 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
985 rtd->dai_link->ops->hw_free(substream);
986
987 /* free any DMA resources */
988 if (platform->driver->ops && platform->driver->ops->hw_free)
989 platform->driver->ops->hw_free(substream);
990
991 /* now free hw params for the DAIs */
2e5894d7
BC
992 for (i = 0; i < rtd->num_codecs; i++) {
993 codec_dai = rtd->codec_dais[i];
994 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
995 codec_dai->driver->ops->hw_free(substream, codec_dai);
996 }
ddee627c 997
c5914b0a 998 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
ddee627c
LG
999 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1000
b8c0dab9 1001 mutex_unlock(&rtd->pcm_mutex);
ddee627c
LG
1002 return 0;
1003}
1004
1005static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1006{
1007 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1008 struct snd_soc_platform *platform = rtd->platform;
1009 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7
BC
1010 struct snd_soc_dai *codec_dai;
1011 int i, ret;
1012
1013 for (i = 0; i < rtd->num_codecs; i++) {
1014 codec_dai = rtd->codec_dais[i];
1015 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1016 ret = codec_dai->driver->ops->trigger(substream,
1017 cmd, codec_dai);
1018 if (ret < 0)
1019 return ret;
1020 }
ddee627c
LG
1021 }
1022
1023 if (platform->driver->ops && platform->driver->ops->trigger) {
1024 ret = platform->driver->ops->trigger(substream, cmd);
1025 if (ret < 0)
1026 return ret;
1027 }
1028
c5914b0a 1029 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
ddee627c
LG
1030 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1031 if (ret < 0)
1032 return ret;
1033 }
4792b0db
JN
1034
1035 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1036 ret = rtd->dai_link->ops->trigger(substream, cmd);
1037 if (ret < 0)
1038 return ret;
1039 }
1040
ddee627c
LG
1041 return 0;
1042}
1043
45c0a188
MB
1044static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1045 int cmd)
07bf84aa
LG
1046{
1047 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1048 struct snd_soc_platform *platform = rtd->platform;
1049 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7
BC
1050 struct snd_soc_dai *codec_dai;
1051 int i, ret;
1052
1053 for (i = 0; i < rtd->num_codecs; i++) {
1054 codec_dai = rtd->codec_dais[i];
1055 if (codec_dai->driver->ops &&
1056 codec_dai->driver->ops->bespoke_trigger) {
1057 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1058 cmd, codec_dai);
1059 if (ret < 0)
1060 return ret;
1061 }
07bf84aa
LG
1062 }
1063
dcf0fa27 1064 if (platform->driver->bespoke_trigger) {
07bf84aa
LG
1065 ret = platform->driver->bespoke_trigger(substream, cmd);
1066 if (ret < 0)
1067 return ret;
1068 }
1069
c5914b0a 1070 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
07bf84aa
LG
1071 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1072 if (ret < 0)
1073 return ret;
1074 }
1075 return 0;
1076}
ddee627c
LG
1077/*
1078 * soc level wrapper for pointer callback
1079 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1080 * the runtime->delay will be updated accordingly.
1081 */
1082static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1083{
1084 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1085 struct snd_soc_platform *platform = rtd->platform;
1086 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2e5894d7 1087 struct snd_soc_dai *codec_dai;
ddee627c
LG
1088 struct snd_pcm_runtime *runtime = substream->runtime;
1089 snd_pcm_uframes_t offset = 0;
1090 snd_pcm_sframes_t delay = 0;
2e5894d7
BC
1091 snd_pcm_sframes_t codec_delay = 0;
1092 int i;
ddee627c
LG
1093
1094 if (platform->driver->ops && platform->driver->ops->pointer)
1095 offset = platform->driver->ops->pointer(substream);
1096
c5914b0a 1097 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
ddee627c
LG
1098 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1099
2e5894d7
BC
1100 for (i = 0; i < rtd->num_codecs; i++) {
1101 codec_dai = rtd->codec_dais[i];
1102 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1103 codec_delay = max(codec_delay,
1104 codec_dai->driver->ops->delay(substream,
1105 codec_dai));
1106 }
1107 delay += codec_delay;
ddee627c 1108
2e5894d7
BC
1109 /*
1110 * None of the existing platform drivers implement delay(), so
1111 * for now the codec_dai of first multicodec entry is used
1112 */
ddee627c 1113 if (platform->driver->delay)
2e5894d7 1114 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
ddee627c
LG
1115
1116 runtime->delay = delay;
1117
1118 return offset;
1119}
1120
01d7584c
LG
1121/* connect a FE and BE */
1122static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1123 struct snd_soc_pcm_runtime *be, int stream)
1124{
1125 struct snd_soc_dpcm *dpcm;
1126
1127 /* only add new dpcms */
1128 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1129 if (dpcm->be == be && dpcm->fe == fe)
1130 return 0;
1131 }
1132
1133 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1134 if (!dpcm)
1135 return -ENOMEM;
1136
1137 dpcm->be = be;
1138 dpcm->fe = fe;
1139 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1140 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1141 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1142 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1143
7cc302d2 1144 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
01d7584c
LG
1145 stream ? "capture" : "playback", fe->dai_link->name,
1146 stream ? "<-" : "->", be->dai_link->name);
1147
f86dcef8 1148#ifdef CONFIG_DEBUG_FS
6553bf06
LPC
1149 if (fe->debugfs_dpcm_root)
1150 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1151 fe->debugfs_dpcm_root, &dpcm->state);
f86dcef8 1152#endif
01d7584c
LG
1153 return 1;
1154}
1155
1156/* reparent a BE onto another FE */
1157static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1158 struct snd_soc_pcm_runtime *be, int stream)
1159{
1160 struct snd_soc_dpcm *dpcm;
1161 struct snd_pcm_substream *fe_substream, *be_substream;
1162
1163 /* reparent if BE is connected to other FEs */
1164 if (!be->dpcm[stream].users)
1165 return;
1166
1167 be_substream = snd_soc_dpcm_get_substream(be, stream);
1168
1169 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1170 if (dpcm->fe == fe)
1171 continue;
1172
7cc302d2 1173 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
01d7584c
LG
1174 stream ? "capture" : "playback",
1175 dpcm->fe->dai_link->name,
1176 stream ? "<-" : "->", dpcm->be->dai_link->name);
1177
1178 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1179 be_substream->runtime = fe_substream->runtime;
1180 break;
1181 }
1182}
1183
1184/* disconnect a BE and FE */
23607025 1185void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1186{
1187 struct snd_soc_dpcm *dpcm, *d;
1188
1189 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
103d84a3 1190 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
01d7584c
LG
1191 stream ? "capture" : "playback",
1192 dpcm->be->dai_link->name);
1193
1194 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1195 continue;
1196
7cc302d2 1197 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
01d7584c
LG
1198 stream ? "capture" : "playback", fe->dai_link->name,
1199 stream ? "<-" : "->", dpcm->be->dai_link->name);
1200
1201 /* BEs still alive need new FE */
1202 dpcm_be_reparent(fe, dpcm->be, stream);
1203
f86dcef8
LG
1204#ifdef CONFIG_DEBUG_FS
1205 debugfs_remove(dpcm->debugfs_state);
1206#endif
01d7584c
LG
1207 list_del(&dpcm->list_be);
1208 list_del(&dpcm->list_fe);
1209 kfree(dpcm);
1210 }
1211}
1212
1213/* get BE for DAI widget and stream */
1214static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1215 struct snd_soc_dapm_widget *widget, int stream)
1216{
1217 struct snd_soc_pcm_runtime *be;
2e5894d7 1218 int i, j;
01d7584c
LG
1219
1220 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1221 for (i = 0; i < card->num_links; i++) {
1222 be = &card->rtd[i];
1223
35ea0655
LG
1224 if (!be->dai_link->no_pcm)
1225 continue;
1226
2e5894d7 1227 if (be->cpu_dai->playback_widget == widget)
01d7584c 1228 return be;
2e5894d7
BC
1229
1230 for (j = 0; j < be->num_codecs; j++) {
1231 struct snd_soc_dai *dai = be->codec_dais[j];
1232 if (dai->playback_widget == widget)
1233 return be;
1234 }
01d7584c
LG
1235 }
1236 } else {
1237
1238 for (i = 0; i < card->num_links; i++) {
1239 be = &card->rtd[i];
1240
35ea0655
LG
1241 if (!be->dai_link->no_pcm)
1242 continue;
1243
2e5894d7 1244 if (be->cpu_dai->capture_widget == widget)
01d7584c 1245 return be;
2e5894d7
BC
1246
1247 for (j = 0; j < be->num_codecs; j++) {
1248 struct snd_soc_dai *dai = be->codec_dais[j];
1249 if (dai->capture_widget == widget)
1250 return be;
1251 }
01d7584c
LG
1252 }
1253 }
1254
103d84a3 1255 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
01d7584c
LG
1256 stream ? "capture" : "playback", widget->name);
1257 return NULL;
1258}
1259
1260static inline struct snd_soc_dapm_widget *
37018610 1261 dai_get_widget(struct snd_soc_dai *dai, int stream)
01d7584c
LG
1262{
1263 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
37018610 1264 return dai->playback_widget;
01d7584c 1265 else
37018610 1266 return dai->capture_widget;
01d7584c
LG
1267}
1268
1269static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1270 struct snd_soc_dapm_widget *widget)
1271{
1272 int i;
1273
1274 for (i = 0; i < list->num_widgets; i++) {
1275 if (widget == list->widgets[i])
1276 return 1;
1277 }
1278
1279 return 0;
1280}
1281
23607025 1282int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1ce43acf 1283 int stream, struct snd_soc_dapm_widget_list **list)
01d7584c
LG
1284{
1285 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
01d7584c
LG
1286 int paths;
1287
01d7584c 1288 /* get number of valid DAI paths and their widgets */
1ce43acf 1289 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
01d7584c 1290
103d84a3 1291 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
01d7584c
LG
1292 stream ? "capture" : "playback");
1293
01d7584c
LG
1294 return paths;
1295}
1296
01d7584c
LG
1297static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1298 struct snd_soc_dapm_widget_list **list_)
1299{
1300 struct snd_soc_dpcm *dpcm;
1301 struct snd_soc_dapm_widget_list *list = *list_;
1302 struct snd_soc_dapm_widget *widget;
1303 int prune = 0;
1304
1305 /* Destroy any old FE <--> BE connections */
1306 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2e5894d7 1307 unsigned int i;
01d7584c
LG
1308
1309 /* is there a valid CPU DAI widget for this BE */
37018610 1310 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
01d7584c
LG
1311
1312 /* prune the BE if it's no longer in our active list */
1313 if (widget && widget_in_list(list, widget))
1314 continue;
1315
1316 /* is there a valid CODEC DAI widget for this BE */
2e5894d7
BC
1317 for (i = 0; i < dpcm->be->num_codecs; i++) {
1318 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1319 widget = dai_get_widget(dai, stream);
01d7584c 1320
2e5894d7
BC
1321 /* prune the BE if it's no longer in our active list */
1322 if (widget && widget_in_list(list, widget))
1323 continue;
1324 }
01d7584c 1325
103d84a3 1326 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
01d7584c
LG
1327 stream ? "capture" : "playback",
1328 dpcm->be->dai_link->name, fe->dai_link->name);
1329 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1330 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1331 prune++;
1332 }
1333
103d84a3 1334 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
01d7584c
LG
1335 return prune;
1336}
1337
1338static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1339 struct snd_soc_dapm_widget_list **list_)
1340{
1341 struct snd_soc_card *card = fe->card;
1342 struct snd_soc_dapm_widget_list *list = *list_;
1343 struct snd_soc_pcm_runtime *be;
1344 int i, new = 0, err;
1345
1346 /* Create any new FE <--> BE connections */
1347 for (i = 0; i < list->num_widgets; i++) {
1348
4616274d
MB
1349 switch (list->widgets[i]->id) {
1350 case snd_soc_dapm_dai_in:
c5b8540d
KC
1351 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1352 continue;
1353 break;
4616274d 1354 case snd_soc_dapm_dai_out:
c5b8540d
KC
1355 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1356 continue;
4616274d
MB
1357 break;
1358 default:
01d7584c 1359 continue;
4616274d 1360 }
01d7584c
LG
1361
1362 /* is there a valid BE rtd for this widget */
1363 be = dpcm_get_be(card, list->widgets[i], stream);
1364 if (!be) {
103d84a3 1365 dev_err(fe->dev, "ASoC: no BE found for %s\n",
01d7584c
LG
1366 list->widgets[i]->name);
1367 continue;
1368 }
1369
1370 /* make sure BE is a real BE */
1371 if (!be->dai_link->no_pcm)
1372 continue;
1373
1374 /* don't connect if FE is not running */
23607025 1375 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
01d7584c
LG
1376 continue;
1377
1378 /* newly connected FE and BE */
1379 err = dpcm_be_connect(fe, be, stream);
1380 if (err < 0) {
103d84a3 1381 dev_err(fe->dev, "ASoC: can't connect %s\n",
01d7584c
LG
1382 list->widgets[i]->name);
1383 break;
1384 } else if (err == 0) /* already connected */
1385 continue;
1386
1387 /* new */
1388 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1389 new++;
1390 }
1391
103d84a3 1392 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
01d7584c
LG
1393 return new;
1394}
1395
1396/*
1397 * Find the corresponding BE DAIs that source or sink audio to this
1398 * FE substream.
1399 */
23607025 1400int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
01d7584c
LG
1401 int stream, struct snd_soc_dapm_widget_list **list, int new)
1402{
1403 if (new)
1404 return dpcm_add_paths(fe, stream, list);
1405 else
1406 return dpcm_prune_paths(fe, stream, list);
1407}
1408
23607025 1409void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1410{
1411 struct snd_soc_dpcm *dpcm;
1412
1413 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1414 dpcm->be->dpcm[stream].runtime_update =
1415 SND_SOC_DPCM_UPDATE_NO;
1416}
1417
1418static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1419 int stream)
1420{
1421 struct snd_soc_dpcm *dpcm;
1422
1423 /* disable any enabled and non active backends */
1424 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1425
1426 struct snd_soc_pcm_runtime *be = dpcm->be;
1427 struct snd_pcm_substream *be_substream =
1428 snd_soc_dpcm_get_substream(be, stream);
1429
1430 if (be->dpcm[stream].users == 0)
103d84a3 1431 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
01d7584c
LG
1432 stream ? "capture" : "playback",
1433 be->dpcm[stream].state);
1434
1435 if (--be->dpcm[stream].users != 0)
1436 continue;
1437
1438 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1439 continue;
1440
1441 soc_pcm_close(be_substream);
1442 be_substream->runtime = NULL;
1443 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1444 }
1445}
1446
23607025 1447int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1448{
1449 struct snd_soc_dpcm *dpcm;
1450 int err, count = 0;
1451
1452 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1453 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1454
1455 struct snd_soc_pcm_runtime *be = dpcm->be;
1456 struct snd_pcm_substream *be_substream =
1457 snd_soc_dpcm_get_substream(be, stream);
1458
2062b4c5
RKAL
1459 if (!be_substream) {
1460 dev_err(be->dev, "ASoC: no backend %s stream\n",
1461 stream ? "capture" : "playback");
1462 continue;
1463 }
1464
01d7584c
LG
1465 /* is this op for this BE ? */
1466 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1467 continue;
1468
1469 /* first time the dpcm is open ? */
1470 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
103d84a3 1471 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
01d7584c
LG
1472 stream ? "capture" : "playback",
1473 be->dpcm[stream].state);
1474
1475 if (be->dpcm[stream].users++ != 0)
1476 continue;
1477
1478 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1479 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1480 continue;
1481
2062b4c5
RKAL
1482 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1483 stream ? "capture" : "playback", be->dai_link->name);
01d7584c
LG
1484
1485 be_substream->runtime = be->dpcm[stream].runtime;
1486 err = soc_pcm_open(be_substream);
1487 if (err < 0) {
103d84a3 1488 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
01d7584c
LG
1489 be->dpcm[stream].users--;
1490 if (be->dpcm[stream].users < 0)
103d84a3 1491 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
01d7584c
LG
1492 stream ? "capture" : "playback",
1493 be->dpcm[stream].state);
1494
1495 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1496 goto unwind;
1497 }
1498
1499 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1500 count++;
1501 }
1502
1503 return count;
1504
1505unwind:
1506 /* disable any enabled and non active backends */
1507 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1508 struct snd_soc_pcm_runtime *be = dpcm->be;
1509 struct snd_pcm_substream *be_substream =
1510 snd_soc_dpcm_get_substream(be, stream);
1511
1512 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1513 continue;
1514
1515 if (be->dpcm[stream].users == 0)
103d84a3 1516 dev_err(be->dev, "ASoC: no users %s at close %d\n",
01d7584c
LG
1517 stream ? "capture" : "playback",
1518 be->dpcm[stream].state);
1519
1520 if (--be->dpcm[stream].users != 0)
1521 continue;
1522
1523 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1524 continue;
1525
1526 soc_pcm_close(be_substream);
1527 be_substream->runtime = NULL;
1528 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1529 }
1530
1531 return err;
1532}
1533
08ae9b45 1534static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
b073ed4e
KM
1535 struct snd_soc_pcm_stream *stream,
1536 u64 formats)
08ae9b45
LPC
1537{
1538 runtime->hw.rate_min = stream->rate_min;
1539 runtime->hw.rate_max = stream->rate_max;
1540 runtime->hw.channels_min = stream->channels_min;
1541 runtime->hw.channels_max = stream->channels_max;
002220a9 1542 if (runtime->hw.formats)
b073ed4e 1543 runtime->hw.formats &= formats & stream->formats;
002220a9 1544 else
b073ed4e 1545 runtime->hw.formats = formats & stream->formats;
08ae9b45
LPC
1546 runtime->hw.rates = stream->rates;
1547}
1548
b073ed4e
KM
1549static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1550{
1551 struct snd_soc_pcm_runtime *fe = substream->private_data;
1552 struct snd_soc_dpcm *dpcm;
1553 u64 formats = ULLONG_MAX;
1554 int stream = substream->stream;
1555
1556 if (!fe->dai_link->dpcm_merged_format)
1557 return formats;
1558
1559 /*
1560 * It returns merged BE codec format
1561 * if FE want to use it (= dpcm_merged_format)
1562 */
1563
1564 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1565 struct snd_soc_pcm_runtime *be = dpcm->be;
1566 struct snd_soc_dai_driver *codec_dai_drv;
1567 struct snd_soc_pcm_stream *codec_stream;
1568 int i;
1569
1570 for (i = 0; i < be->num_codecs; i++) {
1571 codec_dai_drv = be->codec_dais[i]->driver;
1572 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1573 codec_stream = &codec_dai_drv->playback;
1574 else
1575 codec_stream = &codec_dai_drv->capture;
1576
1577 formats &= codec_stream->formats;
1578 }
1579 }
1580
1581 return formats;
1582}
1583
45c0a188 1584static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
01d7584c
LG
1585{
1586 struct snd_pcm_runtime *runtime = substream->runtime;
1587 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1588 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1589 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
b073ed4e 1590 u64 format = dpcm_runtime_base_format(substream);
01d7584c 1591
08ae9b45 1592 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
b073ed4e 1593 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
08ae9b45 1594 else
b073ed4e 1595 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
01d7584c
LG
1596}
1597
ea9d0d77
TI
1598static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1599
1600/* Set FE's runtime_update state; the state is protected via PCM stream lock
1601 * for avoiding the race with trigger callback.
1602 * If the state is unset and a trigger is pending while the previous operation,
1603 * process the pending trigger action here.
1604 */
1605static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1606 int stream, enum snd_soc_dpcm_update state)
1607{
1608 struct snd_pcm_substream *substream =
1609 snd_soc_dpcm_get_substream(fe, stream);
1610
1611 snd_pcm_stream_lock_irq(substream);
1612 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1613 dpcm_fe_dai_do_trigger(substream,
1614 fe->dpcm[stream].trigger_pending - 1);
1615 fe->dpcm[stream].trigger_pending = 0;
1616 }
1617 fe->dpcm[stream].runtime_update = state;
1618 snd_pcm_stream_unlock_irq(substream);
1619}
1620
01d7584c
LG
1621static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1622{
1623 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1624 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1625 int stream = fe_substream->stream, ret = 0;
1626
ea9d0d77 1627 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c
LG
1628
1629 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1630 if (ret < 0) {
103d84a3 1631 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
01d7584c
LG
1632 goto be_err;
1633 }
1634
103d84a3 1635 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
01d7584c
LG
1636
1637 /* start the DAI frontend */
1638 ret = soc_pcm_open(fe_substream);
1639 if (ret < 0) {
103d84a3 1640 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
01d7584c
LG
1641 goto unwind;
1642 }
1643
1644 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1645
1646 dpcm_set_fe_runtime(fe_substream);
1647 snd_pcm_limit_hw_rates(runtime);
1648
ea9d0d77 1649 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1650 return 0;
1651
1652unwind:
1653 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1654be_err:
ea9d0d77 1655 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1656 return ret;
1657}
1658
23607025 1659int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1660{
1661 struct snd_soc_dpcm *dpcm;
1662
1663 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1664 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1665
1666 struct snd_soc_pcm_runtime *be = dpcm->be;
1667 struct snd_pcm_substream *be_substream =
1668 snd_soc_dpcm_get_substream(be, stream);
1669
1670 /* is this op for this BE ? */
1671 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1672 continue;
1673
1674 if (be->dpcm[stream].users == 0)
103d84a3 1675 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
01d7584c
LG
1676 stream ? "capture" : "playback",
1677 be->dpcm[stream].state);
1678
1679 if (--be->dpcm[stream].users != 0)
1680 continue;
1681
1682 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1683 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1684 continue;
1685
103d84a3 1686 dev_dbg(be->dev, "ASoC: close BE %s\n",
01d7584c
LG
1687 dpcm->fe->dai_link->name);
1688
1689 soc_pcm_close(be_substream);
1690 be_substream->runtime = NULL;
1691
1692 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1693 }
1694 return 0;
1695}
1696
1697static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1698{
1699 struct snd_soc_pcm_runtime *fe = substream->private_data;
1700 int stream = substream->stream;
1701
ea9d0d77 1702 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c
LG
1703
1704 /* shutdown the BEs */
1705 dpcm_be_dai_shutdown(fe, substream->stream);
1706
103d84a3 1707 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
01d7584c
LG
1708
1709 /* now shutdown the frontend */
1710 soc_pcm_close(substream);
1711
1712 /* run the stream event for each BE */
1713 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1714
1715 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
ea9d0d77 1716 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1717 return 0;
1718}
1719
23607025 1720int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1721{
1722 struct snd_soc_dpcm *dpcm;
1723
1724 /* only hw_params backends that are either sinks or sources
1725 * to this frontend DAI */
1726 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1727
1728 struct snd_soc_pcm_runtime *be = dpcm->be;
1729 struct snd_pcm_substream *be_substream =
1730 snd_soc_dpcm_get_substream(be, stream);
1731
1732 /* is this op for this BE ? */
1733 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1734 continue;
1735
1736 /* only free hw when no longer used - check all FEs */
1737 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1738 continue;
1739
36fba62c
QZ
1740 /* do not free hw if this BE is used by other FE */
1741 if (be->dpcm[stream].users > 1)
1742 continue;
1743
01d7584c
LG
1744 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1745 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1746 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
08b27848 1747 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
01d7584c
LG
1748 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1749 continue;
1750
103d84a3 1751 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
01d7584c
LG
1752 dpcm->fe->dai_link->name);
1753
1754 soc_pcm_hw_free(be_substream);
1755
1756 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1757 }
1758
1759 return 0;
1760}
1761
45c0a188 1762static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
01d7584c
LG
1763{
1764 struct snd_soc_pcm_runtime *fe = substream->private_data;
1765 int err, stream = substream->stream;
1766
1767 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
ea9d0d77 1768 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c 1769
103d84a3 1770 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
01d7584c
LG
1771
1772 /* call hw_free on the frontend */
1773 err = soc_pcm_hw_free(substream);
1774 if (err < 0)
103d84a3 1775 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
01d7584c
LG
1776 fe->dai_link->name);
1777
1778 /* only hw_params backends that are either sinks or sources
1779 * to this frontend DAI */
1780 err = dpcm_be_dai_hw_free(fe, stream);
1781
1782 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
ea9d0d77 1783 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1784
1785 mutex_unlock(&fe->card->mutex);
1786 return 0;
1787}
1788
23607025 1789int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
1790{
1791 struct snd_soc_dpcm *dpcm;
1792 int ret;
1793
1794 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1795
1796 struct snd_soc_pcm_runtime *be = dpcm->be;
1797 struct snd_pcm_substream *be_substream =
1798 snd_soc_dpcm_get_substream(be, stream);
1799
1800 /* is this op for this BE ? */
1801 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1802 continue;
1803
1804 /* only allow hw_params() if no connected FEs are running */
1805 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1806 continue;
1807
1808 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1809 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1810 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1811 continue;
1812
103d84a3 1813 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
01d7584c
LG
1814 dpcm->fe->dai_link->name);
1815
1816 /* copy params for each dpcm */
1817 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1818 sizeof(struct snd_pcm_hw_params));
1819
1820 /* perform any hw_params fixups */
1821 if (be->dai_link->be_hw_params_fixup) {
1822 ret = be->dai_link->be_hw_params_fixup(be,
1823 &dpcm->hw_params);
1824 if (ret < 0) {
1825 dev_err(be->dev,
103d84a3 1826 "ASoC: hw_params BE fixup failed %d\n",
01d7584c
LG
1827 ret);
1828 goto unwind;
1829 }
1830 }
1831
1832 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1833 if (ret < 0) {
1834 dev_err(dpcm->be->dev,
103d84a3 1835 "ASoC: hw_params BE failed %d\n", ret);
01d7584c
LG
1836 goto unwind;
1837 }
1838
1839 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1840 }
1841 return 0;
1842
1843unwind:
1844 /* disable any enabled and non active backends */
1845 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1846 struct snd_soc_pcm_runtime *be = dpcm->be;
1847 struct snd_pcm_substream *be_substream =
1848 snd_soc_dpcm_get_substream(be, stream);
1849
1850 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1851 continue;
1852
1853 /* only allow hw_free() if no connected FEs are running */
1854 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1855 continue;
1856
1857 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1858 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1859 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1860 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1861 continue;
1862
1863 soc_pcm_hw_free(be_substream);
1864 }
1865
1866 return ret;
1867}
1868
45c0a188
MB
1869static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1870 struct snd_pcm_hw_params *params)
01d7584c
LG
1871{
1872 struct snd_soc_pcm_runtime *fe = substream->private_data;
1873 int ret, stream = substream->stream;
1874
1875 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
ea9d0d77 1876 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c
LG
1877
1878 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1879 sizeof(struct snd_pcm_hw_params));
1880 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1881 if (ret < 0) {
103d84a3 1882 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
01d7584c
LG
1883 goto out;
1884 }
1885
103d84a3 1886 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
01d7584c
LG
1887 fe->dai_link->name, params_rate(params),
1888 params_channels(params), params_format(params));
1889
1890 /* call hw_params on the frontend */
1891 ret = soc_pcm_hw_params(substream, params);
1892 if (ret < 0) {
103d84a3 1893 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
01d7584c
LG
1894 dpcm_be_dai_hw_free(fe, stream);
1895 } else
1896 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1897
1898out:
ea9d0d77 1899 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
1900 mutex_unlock(&fe->card->mutex);
1901 return ret;
1902}
1903
1904static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1905 struct snd_pcm_substream *substream, int cmd)
1906{
1907 int ret;
1908
103d84a3 1909 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
01d7584c
LG
1910 dpcm->fe->dai_link->name, cmd);
1911
1912 ret = soc_pcm_trigger(substream, cmd);
1913 if (ret < 0)
103d84a3 1914 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
01d7584c
LG
1915
1916 return ret;
1917}
1918
23607025 1919int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
45c0a188 1920 int cmd)
01d7584c
LG
1921{
1922 struct snd_soc_dpcm *dpcm;
1923 int ret = 0;
1924
1925 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1926
1927 struct snd_soc_pcm_runtime *be = dpcm->be;
1928 struct snd_pcm_substream *be_substream =
1929 snd_soc_dpcm_get_substream(be, stream);
1930
1931 /* is this op for this BE ? */
1932 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1933 continue;
1934
1935 switch (cmd) {
1936 case SNDRV_PCM_TRIGGER_START:
1937 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1938 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1939 continue;
1940
1941 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1942 if (ret)
1943 return ret;
1944
1945 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1946 break;
1947 case SNDRV_PCM_TRIGGER_RESUME:
1948 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1949 continue;
1950
1951 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1952 if (ret)
1953 return ret;
1954
1955 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1956 break;
1957 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1958 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1959 continue;
1960
1961 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1962 if (ret)
1963 return ret;
1964
1965 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1966 break;
1967 case SNDRV_PCM_TRIGGER_STOP:
1968 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1969 continue;
1970
1971 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1972 continue;
1973
1974 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1975 if (ret)
1976 return ret;
1977
1978 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1979 break;
1980 case SNDRV_PCM_TRIGGER_SUSPEND:
868a6ca8 1981 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
01d7584c
LG
1982 continue;
1983
1984 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1985 continue;
1986
1987 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1988 if (ret)
1989 return ret;
1990
1991 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1992 break;
1993 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1994 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1995 continue;
1996
1997 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1998 continue;
1999
2000 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2001 if (ret)
2002 return ret;
2003
2004 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2005 break;
2006 }
2007 }
2008
2009 return ret;
2010}
2011EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2012
ea9d0d77 2013static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
01d7584c
LG
2014{
2015 struct snd_soc_pcm_runtime *fe = substream->private_data;
2016 int stream = substream->stream, ret;
2017 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2018
2019 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2020
2021 switch (trigger) {
2022 case SND_SOC_DPCM_TRIGGER_PRE:
2023 /* call trigger on the frontend before the backend. */
2024
103d84a3 2025 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
01d7584c
LG
2026 fe->dai_link->name, cmd);
2027
2028 ret = soc_pcm_trigger(substream, cmd);
2029 if (ret < 0) {
103d84a3 2030 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
01d7584c
LG
2031 goto out;
2032 }
2033
2034 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2035 break;
2036 case SND_SOC_DPCM_TRIGGER_POST:
2037 /* call trigger on the frontend after the backend. */
2038
2039 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2040 if (ret < 0) {
103d84a3 2041 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
01d7584c
LG
2042 goto out;
2043 }
2044
103d84a3 2045 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
01d7584c
LG
2046 fe->dai_link->name, cmd);
2047
2048 ret = soc_pcm_trigger(substream, cmd);
2049 break;
07bf84aa
LG
2050 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2051 /* bespoke trigger() - handles both FE and BEs */
2052
103d84a3 2053 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
07bf84aa
LG
2054 fe->dai_link->name, cmd);
2055
2056 ret = soc_pcm_bespoke_trigger(substream, cmd);
2057 if (ret < 0) {
103d84a3 2058 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
07bf84aa
LG
2059 goto out;
2060 }
2061 break;
01d7584c 2062 default:
103d84a3 2063 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
01d7584c
LG
2064 fe->dai_link->name);
2065 ret = -EINVAL;
2066 goto out;
2067 }
2068
2069 switch (cmd) {
2070 case SNDRV_PCM_TRIGGER_START:
2071 case SNDRV_PCM_TRIGGER_RESUME:
2072 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2073 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2074 break;
2075 case SNDRV_PCM_TRIGGER_STOP:
2076 case SNDRV_PCM_TRIGGER_SUSPEND:
2077 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2078 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2079 break;
2080 }
2081
2082out:
2083 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2084 return ret;
2085}
2086
ea9d0d77
TI
2087static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2088{
2089 struct snd_soc_pcm_runtime *fe = substream->private_data;
2090 int stream = substream->stream;
2091
2092 /* if FE's runtime_update is already set, we're in race;
2093 * process this trigger later at exit
2094 */
2095 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2096 fe->dpcm[stream].trigger_pending = cmd + 1;
2097 return 0; /* delayed, assuming it's successful */
2098 }
2099
2100 /* we're alone, let's trigger */
2101 return dpcm_fe_dai_do_trigger(substream, cmd);
2102}
2103
23607025 2104int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
01d7584c
LG
2105{
2106 struct snd_soc_dpcm *dpcm;
2107 int ret = 0;
2108
2109 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2110
2111 struct snd_soc_pcm_runtime *be = dpcm->be;
2112 struct snd_pcm_substream *be_substream =
2113 snd_soc_dpcm_get_substream(be, stream);
2114
2115 /* is this op for this BE ? */
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2117 continue;
2118
2119 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2120 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2121 continue;
2122
103d84a3 2123 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
01d7584c
LG
2124 dpcm->fe->dai_link->name);
2125
2126 ret = soc_pcm_prepare(be_substream);
2127 if (ret < 0) {
103d84a3 2128 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
01d7584c
LG
2129 ret);
2130 break;
2131 }
2132
2133 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2134 }
2135 return ret;
2136}
2137
45c0a188 2138static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
01d7584c
LG
2139{
2140 struct snd_soc_pcm_runtime *fe = substream->private_data;
2141 int stream = substream->stream, ret = 0;
2142
2143 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2144
103d84a3 2145 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
01d7584c 2146
ea9d0d77 2147 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
01d7584c
LG
2148
2149 /* there is no point preparing this FE if there are no BEs */
2150 if (list_empty(&fe->dpcm[stream].be_clients)) {
103d84a3 2151 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
01d7584c
LG
2152 fe->dai_link->name);
2153 ret = -EINVAL;
2154 goto out;
2155 }
2156
2157 ret = dpcm_be_dai_prepare(fe, substream->stream);
2158 if (ret < 0)
2159 goto out;
2160
2161 /* call prepare on the frontend */
2162 ret = soc_pcm_prepare(substream);
2163 if (ret < 0) {
103d84a3 2164 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
01d7584c
LG
2165 fe->dai_link->name);
2166 goto out;
2167 }
2168
2169 /* run the stream event for each BE */
2170 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2171 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2172
2173out:
ea9d0d77 2174 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
01d7584c
LG
2175 mutex_unlock(&fe->card->mutex);
2176
2177 return ret;
2178}
2179
be3f3f2c
LG
2180static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2181 unsigned int cmd, void *arg)
2182{
2183 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2184 struct snd_soc_platform *platform = rtd->platform;
2185
c5914b0a 2186 if (platform->driver->ops && platform->driver->ops->ioctl)
be3f3f2c
LG
2187 return platform->driver->ops->ioctl(substream, cmd, arg);
2188 return snd_pcm_lib_ioctl(substream, cmd, arg);
2189}
2190
618dae11
LG
2191static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2192{
07bf84aa
LG
2193 struct snd_pcm_substream *substream =
2194 snd_soc_dpcm_get_substream(fe, stream);
2195 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
618dae11
LG
2196 int err;
2197
103d84a3 2198 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
618dae11
LG
2199 stream ? "capture" : "playback", fe->dai_link->name);
2200
07bf84aa
LG
2201 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2202 /* call bespoke trigger - FE takes care of all BE triggers */
103d84a3 2203 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
07bf84aa
LG
2204 fe->dai_link->name);
2205
2206 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2207 if (err < 0)
103d84a3 2208 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
07bf84aa 2209 } else {
103d84a3 2210 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
07bf84aa
LG
2211 fe->dai_link->name);
2212
2213 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2214 if (err < 0)
103d84a3 2215 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
07bf84aa 2216 }
618dae11
LG
2217
2218 err = dpcm_be_dai_hw_free(fe, stream);
2219 if (err < 0)
103d84a3 2220 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
618dae11
LG
2221
2222 err = dpcm_be_dai_shutdown(fe, stream);
2223 if (err < 0)
103d84a3 2224 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
618dae11
LG
2225
2226 /* run the stream event for each BE */
2227 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2228
2229 return 0;
2230}
2231
2232static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2233{
07bf84aa
LG
2234 struct snd_pcm_substream *substream =
2235 snd_soc_dpcm_get_substream(fe, stream);
618dae11 2236 struct snd_soc_dpcm *dpcm;
07bf84aa 2237 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
618dae11
LG
2238 int ret;
2239
103d84a3 2240 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
618dae11
LG
2241 stream ? "capture" : "playback", fe->dai_link->name);
2242
2243 /* Only start the BE if the FE is ready */
2244 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2245 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2246 return -EINVAL;
2247
2248 /* startup must always be called for new BEs */
2249 ret = dpcm_be_dai_startup(fe, stream);
fffc0ca2 2250 if (ret < 0)
618dae11 2251 goto disconnect;
618dae11
LG
2252
2253 /* keep going if FE state is > open */
2254 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2255 return 0;
01d7584c 2256
618dae11 2257 ret = dpcm_be_dai_hw_params(fe, stream);
fffc0ca2 2258 if (ret < 0)
618dae11 2259 goto close;
618dae11
LG
2260
2261 /* keep going if FE state is > hw_params */
2262 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2263 return 0;
2264
2265
2266 ret = dpcm_be_dai_prepare(fe, stream);
fffc0ca2 2267 if (ret < 0)
618dae11 2268 goto hw_free;
618dae11
LG
2269
2270 /* run the stream event for each BE */
2271 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2272
2273 /* keep going if FE state is > prepare */
2274 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2275 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2276 return 0;
2277
07bf84aa
LG
2278 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2279 /* call trigger on the frontend - FE takes care of all BE triggers */
103d84a3 2280 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
07bf84aa 2281 fe->dai_link->name);
618dae11 2282
07bf84aa
LG
2283 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2284 if (ret < 0) {
103d84a3 2285 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
07bf84aa
LG
2286 goto hw_free;
2287 }
2288 } else {
103d84a3 2289 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
07bf84aa
LG
2290 fe->dai_link->name);
2291
2292 ret = dpcm_be_dai_trigger(fe, stream,
2293 SNDRV_PCM_TRIGGER_START);
2294 if (ret < 0) {
103d84a3 2295 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
07bf84aa
LG
2296 goto hw_free;
2297 }
618dae11
LG
2298 }
2299
2300 return 0;
2301
2302hw_free:
2303 dpcm_be_dai_hw_free(fe, stream);
2304close:
2305 dpcm_be_dai_shutdown(fe, stream);
2306disconnect:
2307 /* disconnect any non started BEs */
2308 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2309 struct snd_soc_pcm_runtime *be = dpcm->be;
2310 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2311 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2312 }
2313
2314 return ret;
2315}
2316
2317static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2318{
2319 int ret;
2320
ea9d0d77 2321 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
618dae11
LG
2322 ret = dpcm_run_update_startup(fe, stream);
2323 if (ret < 0)
103d84a3 2324 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
ea9d0d77 2325 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
618dae11
LG
2326
2327 return ret;
2328}
2329
2330static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2331{
2332 int ret;
2333
ea9d0d77 2334 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
618dae11
LG
2335 ret = dpcm_run_update_shutdown(fe, stream);
2336 if (ret < 0)
103d84a3 2337 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
ea9d0d77 2338 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
618dae11
LG
2339
2340 return ret;
2341}
2342
2343/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2344 * any DAI links.
2345 */
c3f48ae6 2346int soc_dpcm_runtime_update(struct snd_soc_card *card)
618dae11 2347{
618dae11
LG
2348 int i, old, new, paths;
2349
618dae11
LG
2350 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2351 for (i = 0; i < card->num_rtd; i++) {
2352 struct snd_soc_dapm_widget_list *list;
2353 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2354
2355 /* make sure link is FE */
2356 if (!fe->dai_link->dynamic)
2357 continue;
2358
2359 /* only check active links */
2360 if (!fe->cpu_dai->active)
2361 continue;
2362
2363 /* DAPM sync will call this to update DSP paths */
103d84a3 2364 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
618dae11
LG
2365 fe->dai_link->name);
2366
2367 /* skip if FE doesn't have playback capability */
075207d2
QZ
2368 if (!fe->cpu_dai->driver->playback.channels_min
2369 || !fe->codec_dai->driver->playback.channels_min)
2370 goto capture;
2371
2372 /* skip if FE isn't currently playing */
2373 if (!fe->cpu_dai->playback_active
2374 || !fe->codec_dai->playback_active)
618dae11
LG
2375 goto capture;
2376
2377 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2378 if (paths < 0) {
103d84a3 2379 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
618dae11
LG
2380 fe->dai_link->name, "playback");
2381 mutex_unlock(&card->mutex);
2382 return paths;
2383 }
2384
2385 /* update any new playback paths */
2386 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2387 if (new) {
2388 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2389 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2390 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2391 }
2392
2393 /* update any old playback paths */
2394 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2395 if (old) {
2396 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2397 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2398 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2399 }
2400
7ed9de76 2401 dpcm_path_put(&list);
618dae11
LG
2402capture:
2403 /* skip if FE doesn't have capture capability */
075207d2
QZ
2404 if (!fe->cpu_dai->driver->capture.channels_min
2405 || !fe->codec_dai->driver->capture.channels_min)
2406 continue;
2407
2408 /* skip if FE isn't currently capturing */
2409 if (!fe->cpu_dai->capture_active
2410 || !fe->codec_dai->capture_active)
618dae11
LG
2411 continue;
2412
2413 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2414 if (paths < 0) {
103d84a3 2415 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
618dae11
LG
2416 fe->dai_link->name, "capture");
2417 mutex_unlock(&card->mutex);
2418 return paths;
2419 }
2420
2421 /* update any new capture paths */
2422 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2423 if (new) {
2424 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2425 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2426 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2427 }
2428
2429 /* update any old capture paths */
2430 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2431 if (old) {
2432 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2433 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2434 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2435 }
2436
2437 dpcm_path_put(&list);
2438 }
2439
2440 mutex_unlock(&card->mutex);
2441 return 0;
2442}
01d7584c
LG
2443int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2444{
2445 struct snd_soc_dpcm *dpcm;
2446 struct list_head *clients =
2447 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2448
2449 list_for_each_entry(dpcm, clients, list_be) {
2450
2451 struct snd_soc_pcm_runtime *be = dpcm->be;
2e5894d7 2452 int i;
01d7584c
LG
2453
2454 if (be->dai_link->ignore_suspend)
2455 continue;
2456
2e5894d7
BC
2457 for (i = 0; i < be->num_codecs; i++) {
2458 struct snd_soc_dai *dai = be->codec_dais[i];
2459 struct snd_soc_dai_driver *drv = dai->driver;
2460
2461 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2462 be->dai_link->name);
01d7584c 2463
2e5894d7
BC
2464 if (drv->ops && drv->ops->digital_mute &&
2465 dai->playback_active)
2466 drv->ops->digital_mute(dai, mute);
2467 }
01d7584c
LG
2468 }
2469
2470 return 0;
2471}
2472
45c0a188 2473static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
01d7584c
LG
2474{
2475 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2476 struct snd_soc_dpcm *dpcm;
2477 struct snd_soc_dapm_widget_list *list;
2478 int ret;
2479 int stream = fe_substream->stream;
2480
2481 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2482 fe->dpcm[stream].runtime = fe_substream->runtime;
2483
8f70e515
QZ
2484 ret = dpcm_path_get(fe, stream, &list);
2485 if (ret < 0) {
2486 mutex_unlock(&fe->card->mutex);
2487 return ret;
2488 } else if (ret == 0) {
103d84a3 2489 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
01d7584c 2490 fe->dai_link->name, stream ? "capture" : "playback");
01d7584c
LG
2491 }
2492
2493 /* calculate valid and active FE <-> BE dpcms */
2494 dpcm_process_paths(fe, stream, &list, 1);
2495
2496 ret = dpcm_fe_dai_startup(fe_substream);
2497 if (ret < 0) {
2498 /* clean up all links */
2499 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2500 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2501
2502 dpcm_be_disconnect(fe, stream);
2503 fe->dpcm[stream].runtime = NULL;
2504 }
2505
2506 dpcm_clear_pending_state(fe, stream);
2507 dpcm_path_put(&list);
2508 mutex_unlock(&fe->card->mutex);
2509 return ret;
2510}
2511
45c0a188 2512static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
01d7584c
LG
2513{
2514 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2515 struct snd_soc_dpcm *dpcm;
2516 int stream = fe_substream->stream, ret;
2517
2518 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2519 ret = dpcm_fe_dai_shutdown(fe_substream);
2520
2521 /* mark FE's links ready to prune */
2522 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2523 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2524
2525 dpcm_be_disconnect(fe, stream);
2526
2527 fe->dpcm[stream].runtime = NULL;
2528 mutex_unlock(&fe->card->mutex);
2529 return ret;
2530}
2531
ddee627c
LG
2532/* create a new pcm */
2533int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2534{
ddee627c 2535 struct snd_soc_platform *platform = rtd->platform;
2e5894d7 2536 struct snd_soc_dai *codec_dai;
ddee627c
LG
2537 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2538 struct snd_pcm *pcm;
2539 char new_name[64];
2540 int ret = 0, playback = 0, capture = 0;
2e5894d7 2541 int i;
ddee627c 2542
01d7584c 2543 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
1e9de42f
LG
2544 playback = rtd->dai_link->dpcm_playback;
2545 capture = rtd->dai_link->dpcm_capture;
01d7584c 2546 } else {
2e5894d7
BC
2547 for (i = 0; i < rtd->num_codecs; i++) {
2548 codec_dai = rtd->codec_dais[i];
2549 if (codec_dai->driver->playback.channels_min)
2550 playback = 1;
2551 if (codec_dai->driver->capture.channels_min)
2552 capture = 1;
2553 }
2554
2555 capture = capture && cpu_dai->driver->capture.channels_min;
2556 playback = playback && cpu_dai->driver->playback.channels_min;
01d7584c
LG
2557 }
2558
d6bead02
FE
2559 if (rtd->dai_link->playback_only) {
2560 playback = 1;
2561 capture = 0;
2562 }
2563
2564 if (rtd->dai_link->capture_only) {
2565 playback = 0;
2566 capture = 1;
2567 }
2568
01d7584c
LG
2569 /* create the PCM */
2570 if (rtd->dai_link->no_pcm) {
2571 snprintf(new_name, sizeof(new_name), "(%s)",
2572 rtd->dai_link->stream_name);
2573
2574 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2575 playback, capture, &pcm);
2576 } else {
2577 if (rtd->dai_link->dynamic)
2578 snprintf(new_name, sizeof(new_name), "%s (*)",
2579 rtd->dai_link->stream_name);
2580 else
2581 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2e5894d7
BC
2582 rtd->dai_link->stream_name,
2583 (rtd->num_codecs > 1) ?
2584 "multicodec" : rtd->codec_dai->name, num);
01d7584c
LG
2585
2586 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2587 capture, &pcm);
2588 }
ddee627c 2589 if (ret < 0) {
103d84a3 2590 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
5cb9b748 2591 rtd->dai_link->name);
ddee627c
LG
2592 return ret;
2593 }
103d84a3 2594 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
ddee627c
LG
2595
2596 /* DAPM dai link stream work */
2597 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2598
48c7699f 2599 pcm->nonatomic = rtd->dai_link->nonatomic;
ddee627c
LG
2600 rtd->pcm = pcm;
2601 pcm->private_data = rtd;
01d7584c
LG
2602
2603 if (rtd->dai_link->no_pcm) {
2604 if (playback)
2605 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2606 if (capture)
2607 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2608 goto out;
2609 }
2610
2611 /* ASoC PCM operations */
2612 if (rtd->dai_link->dynamic) {
2613 rtd->ops.open = dpcm_fe_dai_open;
2614 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2615 rtd->ops.prepare = dpcm_fe_dai_prepare;
2616 rtd->ops.trigger = dpcm_fe_dai_trigger;
2617 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2618 rtd->ops.close = dpcm_fe_dai_close;
2619 rtd->ops.pointer = soc_pcm_pointer;
be3f3f2c 2620 rtd->ops.ioctl = soc_pcm_ioctl;
01d7584c
LG
2621 } else {
2622 rtd->ops.open = soc_pcm_open;
2623 rtd->ops.hw_params = soc_pcm_hw_params;
2624 rtd->ops.prepare = soc_pcm_prepare;
2625 rtd->ops.trigger = soc_pcm_trigger;
2626 rtd->ops.hw_free = soc_pcm_hw_free;
2627 rtd->ops.close = soc_pcm_close;
2628 rtd->ops.pointer = soc_pcm_pointer;
be3f3f2c 2629 rtd->ops.ioctl = soc_pcm_ioctl;
01d7584c
LG
2630 }
2631
ddee627c 2632 if (platform->driver->ops) {
01d7584c
LG
2633 rtd->ops.ack = platform->driver->ops->ack;
2634 rtd->ops.copy = platform->driver->ops->copy;
2635 rtd->ops.silence = platform->driver->ops->silence;
2636 rtd->ops.page = platform->driver->ops->page;
2637 rtd->ops.mmap = platform->driver->ops->mmap;
ddee627c
LG
2638 }
2639
2640 if (playback)
01d7584c 2641 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
ddee627c
LG
2642
2643 if (capture)
01d7584c 2644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
ddee627c
LG
2645
2646 if (platform->driver->pcm_new) {
2647 ret = platform->driver->pcm_new(rtd);
2648 if (ret < 0) {
b1bc7b3c
MB
2649 dev_err(platform->dev,
2650 "ASoC: pcm constructor failed: %d\n",
2651 ret);
ddee627c
LG
2652 return ret;
2653 }
2654 }
2655
2656 pcm->private_free = platform->driver->pcm_free;
01d7584c 2657out:
2e5894d7
BC
2658 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2659 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2660 cpu_dai->name);
ddee627c
LG
2661 return ret;
2662}
01d7584c
LG
2663
2664/* is the current PCM operation for this FE ? */
2665int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2666{
2667 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2668 return 1;
2669 return 0;
2670}
2671EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2672
2673/* is the current PCM operation for this BE ? */
2674int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2675 struct snd_soc_pcm_runtime *be, int stream)
2676{
2677 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2678 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2679 be->dpcm[stream].runtime_update))
2680 return 1;
2681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2684
2685/* get the substream for this BE */
2686struct snd_pcm_substream *
2687 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2688{
2689 return be->pcm->streams[stream].substream;
2690}
2691EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2692
2693/* get the BE runtime state */
2694enum snd_soc_dpcm_state
2695 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2696{
2697 return be->dpcm[stream].state;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2700
2701/* set the BE runtime state */
2702void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2703 int stream, enum snd_soc_dpcm_state state)
2704{
2705 be->dpcm[stream].state = state;
2706}
2707EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2708
2709/*
2710 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2711 * are not running, paused or suspended for the specified stream direction.
2712 */
2713int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2714 struct snd_soc_pcm_runtime *be, int stream)
2715{
2716 struct snd_soc_dpcm *dpcm;
2717 int state;
2718
2719 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2720
2721 if (dpcm->fe == fe)
2722 continue;
2723
2724 state = dpcm->fe->dpcm[stream].state;
2725 if (state == SND_SOC_DPCM_STATE_START ||
2726 state == SND_SOC_DPCM_STATE_PAUSED ||
2727 state == SND_SOC_DPCM_STATE_SUSPEND)
2728 return 0;
2729 }
2730
2731 /* it's safe to free/stop this BE DAI */
2732 return 1;
2733}
2734EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2735
2736/*
2737 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2738 * running, paused or suspended for the specified stream direction.
2739 */
2740int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2741 struct snd_soc_pcm_runtime *be, int stream)
2742{
2743 struct snd_soc_dpcm *dpcm;
2744 int state;
2745
2746 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2747
2748 if (dpcm->fe == fe)
2749 continue;
2750
2751 state = dpcm->fe->dpcm[stream].state;
2752 if (state == SND_SOC_DPCM_STATE_START ||
2753 state == SND_SOC_DPCM_STATE_PAUSED ||
2754 state == SND_SOC_DPCM_STATE_SUSPEND ||
2755 state == SND_SOC_DPCM_STATE_PREPARE)
2756 return 0;
2757 }
2758
2759 /* it's safe to change hw_params */
2760 return 1;
2761}
2762EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
f86dcef8 2763
07bf84aa
LG
2764int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2765 int cmd, struct snd_soc_platform *platform)
2766{
c5914b0a 2767 if (platform->driver->ops && platform->driver->ops->trigger)
07bf84aa
LG
2768 return platform->driver->ops->trigger(substream, cmd);
2769 return 0;
2770}
2771EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2772
f86dcef8
LG
2773#ifdef CONFIG_DEBUG_FS
2774static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2775{
2776 switch (state) {
2777 case SND_SOC_DPCM_STATE_NEW:
2778 return "new";
2779 case SND_SOC_DPCM_STATE_OPEN:
2780 return "open";
2781 case SND_SOC_DPCM_STATE_HW_PARAMS:
2782 return "hw_params";
2783 case SND_SOC_DPCM_STATE_PREPARE:
2784 return "prepare";
2785 case SND_SOC_DPCM_STATE_START:
2786 return "start";
2787 case SND_SOC_DPCM_STATE_STOP:
2788 return "stop";
2789 case SND_SOC_DPCM_STATE_SUSPEND:
2790 return "suspend";
2791 case SND_SOC_DPCM_STATE_PAUSED:
2792 return "paused";
2793 case SND_SOC_DPCM_STATE_HW_FREE:
2794 return "hw_free";
2795 case SND_SOC_DPCM_STATE_CLOSE:
2796 return "close";
2797 }
2798
2799 return "unknown";
2800}
2801
f86dcef8
LG
2802static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2803 int stream, char *buf, size_t size)
2804{
2805 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2806 struct snd_soc_dpcm *dpcm;
2807 ssize_t offset = 0;
2808
2809 /* FE state */
2810 offset += snprintf(buf + offset, size - offset,
2811 "[%s - %s]\n", fe->dai_link->name,
2812 stream ? "Capture" : "Playback");
2813
2814 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2815 dpcm_state_string(fe->dpcm[stream].state));
2816
2817 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2818 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2819 offset += snprintf(buf + offset, size - offset,
2820 "Hardware Params: "
2821 "Format = %s, Channels = %d, Rate = %d\n",
2822 snd_pcm_format_name(params_format(params)),
2823 params_channels(params),
2824 params_rate(params));
2825
2826 /* BEs state */
2827 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2828
2829 if (list_empty(&fe->dpcm[stream].be_clients)) {
2830 offset += snprintf(buf + offset, size - offset,
2831 " No active DSP links\n");
2832 goto out;
2833 }
2834
2835 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2836 struct snd_soc_pcm_runtime *be = dpcm->be;
2837 params = &dpcm->hw_params;
2838
2839 offset += snprintf(buf + offset, size - offset,
2840 "- %s\n", be->dai_link->name);
2841
2842 offset += snprintf(buf + offset, size - offset,
2843 " State: %s\n",
2844 dpcm_state_string(be->dpcm[stream].state));
2845
2846 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2847 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2848 offset += snprintf(buf + offset, size - offset,
2849 " Hardware Params: "
2850 "Format = %s, Channels = %d, Rate = %d\n",
2851 snd_pcm_format_name(params_format(params)),
2852 params_channels(params),
2853 params_rate(params));
2854 }
2855
2856out:
2857 return offset;
2858}
2859
2860static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2861 size_t count, loff_t *ppos)
2862{
2863 struct snd_soc_pcm_runtime *fe = file->private_data;
2864 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2865 char *buf;
2866
2867 buf = kmalloc(out_count, GFP_KERNEL);
2868 if (!buf)
2869 return -ENOMEM;
2870
2871 if (fe->cpu_dai->driver->playback.channels_min)
2872 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2873 buf + offset, out_count - offset);
2874
2875 if (fe->cpu_dai->driver->capture.channels_min)
2876 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2877 buf + offset, out_count - offset);
2878
f57b8488 2879 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
f86dcef8 2880
f57b8488
LG
2881 kfree(buf);
2882 return ret;
f86dcef8
LG
2883}
2884
2885static const struct file_operations dpcm_state_fops = {
f57b8488 2886 .open = simple_open,
f86dcef8
LG
2887 .read = dpcm_state_read_file,
2888 .llseek = default_llseek,
2889};
2890
2e55b90a 2891void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
f86dcef8 2892{
b3bba9a1 2893 if (!rtd->dai_link)
2e55b90a 2894 return;
b3bba9a1 2895
6553bf06
LPC
2896 if (!rtd->card->debugfs_card_root)
2897 return;
b3bba9a1 2898
f86dcef8
LG
2899 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2900 rtd->card->debugfs_card_root);
2901 if (!rtd->debugfs_dpcm_root) {
2902 dev_dbg(rtd->dev,
2903 "ASoC: Failed to create dpcm debugfs directory %s\n",
2904 rtd->dai_link->name);
2e55b90a 2905 return;
f86dcef8
LG
2906 }
2907
f57b8488 2908 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
f86dcef8
LG
2909 rtd->debugfs_dpcm_root,
2910 rtd, &dpcm_state_fops);
f86dcef8
LG
2911}
2912#endif
This page took 0.441095 seconds and 5 git commands to generate.