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