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