blob: d2f908741238dacf1f6bf033fd7cfe04eb85543f [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
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>
Mark Brownd6652ef2011-12-03 20:14:31 +000022#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010023#include <linux/slab.h>
24#include <linux/workqueue.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070025#include <linux/debugfs.h>
26#include <linux/dma-mapping.h>
27#include <linux/export.h>
Iliyan Malchev734dc212012-10-02 09:22:36 -070028#include <linux/bug.h>
SathishKumar Manief4feb32012-10-09 13:31:13 -070029#include <linux/ratelimit.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010030#include <sound/core.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070034#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010035#include <sound/initval.h>
36
Steve Mucklef132c6c2012-06-06 18:30:57 -070037#define MAX_BE_USERS 8 /* adjust if too low for everday use */
38
39static int soc_dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream);
40
41/* ASoC no host IO hardware.
42 * TODO: fine tune these values for all host less transfers.
43 */
44static const struct snd_pcm_hardware no_host_hardware = {
45 .info = SNDRV_PCM_INFO_MMAP |
46 SNDRV_PCM_INFO_MMAP_VALID |
47 SNDRV_PCM_INFO_INTERLEAVED |
48 SNDRV_PCM_INFO_PAUSE |
49 SNDRV_PCM_INFO_RESUME,
50 .formats = SNDRV_PCM_FMTBIT_S16_LE |
51 SNDRV_PCM_FMTBIT_S32_LE,
52 .period_bytes_min = PAGE_SIZE >> 2,
53 .period_bytes_max = PAGE_SIZE >> 1,
54 .periods_min = 2,
55 .periods_max = 4,
56 .buffer_bytes_max = PAGE_SIZE,
57};
58
59/*
60 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
61 * are not running, paused or suspended for the specified stream direction.
62 */
63int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
64 struct snd_soc_pcm_runtime *be, int stream)
65{
66 struct snd_soc_dpcm_params *dpcm_params;
67
68 list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
69
70 if (dpcm_params->fe == fe)
71 continue;
72
73 if (dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_START ||
74 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED ||
75 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_SUSPEND)
76 return 0;
77 }
78 return 1;
79}
80EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
81
82/*
83 * We can only change hw params a BE DAI if any of it's FE are not prepared,
84 * running, paused or suspended for the specified stream direction.
85 */
86static int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
87 struct snd_soc_pcm_runtime *be, int stream)
88{
89 struct snd_soc_dpcm_params *dpcm_params;
90
91 list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
92
93 if (dpcm_params->fe == fe)
94 continue;
95
96 if (dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_START ||
97 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PAUSED ||
98 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_SUSPEND ||
99 dpcm_params->fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE)
100 return 0;
101 }
102 return 1;
103}
104
Dong Aisheng17841022011-08-29 17:15:14 +0800105static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
106 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100107{
108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100109 int ret;
110
Dong Aisheng17841022011-08-29 17:15:14 +0800111 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +0100112 !rtd->dai_link->symmetric_rates)
113 return 0;
114
115 /* This can happen if multiple streams are starting simultaneously -
116 * the second can need to get its constraints before the first has
117 * picked a rate. Complain and allow the application to carry on.
118 */
Dong Aisheng17841022011-08-29 17:15:14 +0800119 if (!soc_dai->rate) {
120 dev_warn(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100121 "Not enforcing symmetric_rates due to race\n");
122 return 0;
123 }
124
Dong Aisheng17841022011-08-29 17:15:14 +0800125 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100126
127 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
128 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +0800129 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100130 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +0800131 dev_err(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100132 "Unable to apply rate symmetry constraint: %d\n", ret);
133 return ret;
134 }
135
136 return 0;
137}
138
139/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000140 * List of sample sizes that might go over the bus for parameter
141 * application. There ought to be a wildcard sample size for things
142 * like the DAC/ADC resolution to use but there isn't right now.
143 */
144static int sample_sizes[] = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700145 8, 16, 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000146};
147
148static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
149 struct snd_soc_dai *dai)
150{
151 int ret, i, bits;
152
153 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
154 bits = dai->driver->playback.sig_bits;
155 else
156 bits = dai->driver->capture.sig_bits;
157
158 if (!bits)
159 return;
160
161 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000162 if (bits >= sample_sizes[i])
163 continue;
164
165 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
166 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000167 if (ret != 0)
168 dev_warn(dai->dev,
169 "Failed to set MSB %d/%d: %d\n",
170 bits, sample_sizes[i], ret);
171 }
172}
173
174/*
Steve Mucklef132c6c2012-06-06 18:30:57 -0700175 * stream event, send event to FE and all active BEs.
176 */
177int soc_dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe,
178 int dir, const char *stream, int event)
179{
180 struct snd_soc_dpcm_params *dpcm_params;
181
182 snd_soc_dapm_rtd_stream_event(fe, dir, event);
183
184 list_for_each_entry(dpcm_params, &fe->dpcm[dir].be_clients, list_be) {
185
186 struct snd_soc_pcm_runtime *be = dpcm_params->be;
187
188 dev_dbg(be->dev, "pm: BE %s stream %s event %d dir %d\n",
189 be->dai_link->name, stream, event, dir);
190
191 snd_soc_dapm_rtd_stream_event(be, dir, event);
192 }
193
194 return 0;
195}
196
197/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
199 * then initialized and any private data can be allocated. This also calls
200 * startup for the cpu DAI, platform, machine and codec DAI.
201 */
202static int soc_pcm_open(struct snd_pcm_substream *substream)
203{
204 struct snd_soc_pcm_runtime *rtd = substream->private_data;
205 struct snd_pcm_runtime *runtime = substream->runtime;
206 struct snd_soc_platform *platform = rtd->platform;
207 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
208 struct snd_soc_dai *codec_dai = rtd->codec_dai;
209 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
210 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
211 int ret = 0;
212
Mark Brownd6652ef2011-12-03 20:14:31 +0000213 pm_runtime_get_sync(cpu_dai->dev);
214 pm_runtime_get_sync(codec_dai->dev);
215 pm_runtime_get_sync(platform->dev);
216
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100217 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100218
Steve Mucklef132c6c2012-06-06 18:30:57 -0700219 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
220 snd_soc_set_runtime_hwparams(substream, &no_host_hardware);
221
Liam Girdwoodddee6272011-06-09 14:45:53 +0100222 /* startup the audio subsystem */
223 if (cpu_dai->driver->ops->startup) {
224 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
225 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700226 printk(KERN_ERR "asoc: can't open interface %s\n",
227 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100228 goto out;
229 }
230 }
231
232 if (platform->driver->ops && platform->driver->ops->open) {
233 ret = platform->driver->ops->open(substream);
234 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700235 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100236 goto platform_err;
237 }
238 }
239
240 if (codec_dai->driver->ops->startup) {
Helen Zeng5749b092012-06-10 11:50:29 -0700241 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
242 ret = codec_dai->driver->ops->startup(substream,
243 codec_dai);
244 if (ret < 0) {
245 printk(KERN_ERR "asoc: can't open codec %s\n",
246 codec_dai->name);
247 goto codec_dai_err;
248 }
249 } else {
250 if (!codec_dai->capture_active) {
251 ret = codec_dai->driver->ops->startup(substream,
252 codec_dai);
253 if (ret < 0) {
254 printk(KERN_ERR "can't open codec %s\n",
255 codec_dai->name);
256 goto codec_dai_err;
257 }
258 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100259 }
260 }
261
262 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
263 ret = rtd->dai_link->ops->startup(substream);
264 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700265 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100266 goto machine_err;
267 }
268 }
269
Steve Mucklef132c6c2012-06-06 18:30:57 -0700270 /* DSP DAI links compat checks are different */
271 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
272 goto dynamic;
273
Liam Girdwoodddee6272011-06-09 14:45:53 +0100274 /* Check that the codec and cpu DAIs are compatible */
275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
276 runtime->hw.rate_min =
277 max(codec_dai_drv->playback.rate_min,
278 cpu_dai_drv->playback.rate_min);
279 runtime->hw.rate_max =
280 min(codec_dai_drv->playback.rate_max,
281 cpu_dai_drv->playback.rate_max);
282 runtime->hw.channels_min =
283 max(codec_dai_drv->playback.channels_min,
284 cpu_dai_drv->playback.channels_min);
285 runtime->hw.channels_max =
286 min(codec_dai_drv->playback.channels_max,
287 cpu_dai_drv->playback.channels_max);
288 runtime->hw.formats =
289 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
290 runtime->hw.rates =
291 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
292 if (codec_dai_drv->playback.rates
293 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
294 runtime->hw.rates |= cpu_dai_drv->playback.rates;
295 if (cpu_dai_drv->playback.rates
296 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
297 runtime->hw.rates |= codec_dai_drv->playback.rates;
298 } else {
299 runtime->hw.rate_min =
300 max(codec_dai_drv->capture.rate_min,
301 cpu_dai_drv->capture.rate_min);
302 runtime->hw.rate_max =
303 min(codec_dai_drv->capture.rate_max,
304 cpu_dai_drv->capture.rate_max);
305 runtime->hw.channels_min =
306 max(codec_dai_drv->capture.channels_min,
307 cpu_dai_drv->capture.channels_min);
308 runtime->hw.channels_max =
309 min(codec_dai_drv->capture.channels_max,
310 cpu_dai_drv->capture.channels_max);
311 runtime->hw.formats =
312 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
313 runtime->hw.rates =
314 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
315 if (codec_dai_drv->capture.rates
316 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
317 runtime->hw.rates |= cpu_dai_drv->capture.rates;
318 if (cpu_dai_drv->capture.rates
319 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
320 runtime->hw.rates |= codec_dai_drv->capture.rates;
321 }
322
323 ret = -EINVAL;
324 snd_pcm_limit_hw_rates(runtime);
325 if (!runtime->hw.rates) {
326 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
327 codec_dai->name, cpu_dai->name);
328 goto config_err;
329 }
330 if (!runtime->hw.formats) {
331 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
332 codec_dai->name, cpu_dai->name);
333 goto config_err;
334 }
335 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
336 runtime->hw.channels_min > runtime->hw.channels_max) {
337 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
338 codec_dai->name, cpu_dai->name);
339 goto config_err;
340 }
341
Mark Brown58ba9b22012-01-16 18:38:51 +0000342 soc_pcm_apply_msb(substream, codec_dai);
343 soc_pcm_apply_msb(substream, cpu_dai);
344
Liam Girdwoodddee6272011-06-09 14:45:53 +0100345 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800346 if (cpu_dai->active) {
347 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
348 if (ret != 0)
349 goto config_err;
350 }
351
352 if (codec_dai->active) {
353 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100354 if (ret != 0)
355 goto config_err;
356 }
357
358 pr_debug("asoc: %s <-> %s info:\n",
359 codec_dai->name, cpu_dai->name);
360 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
361 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
362 runtime->hw.channels_max);
363 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
364 runtime->hw.rate_max);
365
Steve Mucklef132c6c2012-06-06 18:30:57 -0700366dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368 cpu_dai->playback_active++;
369 codec_dai->playback_active++;
370 } else {
371 cpu_dai->capture_active++;
372 codec_dai->capture_active++;
373 }
374 cpu_dai->active++;
375 codec_dai->active++;
376 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100377 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100378 return 0;
379
380config_err:
381 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
382 rtd->dai_link->ops->shutdown(substream);
383
384machine_err:
385 if (codec_dai->driver->ops->shutdown)
386 codec_dai->driver->ops->shutdown(substream, codec_dai);
387
388codec_dai_err:
389 if (platform->driver->ops && platform->driver->ops->close)
390 platform->driver->ops->close(substream);
391
392platform_err:
393 if (cpu_dai->driver->ops->shutdown)
394 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
395out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100396 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000397
398 pm_runtime_put(platform->dev);
399 pm_runtime_put(codec_dai->dev);
400 pm_runtime_put(cpu_dai->dev);
401
Liam Girdwoodddee6272011-06-09 14:45:53 +0100402 return ret;
403}
404
405/*
406 * Power down the audio subsystem pmdown_time msecs after close is called.
407 * This is to ensure there are no pops or clicks in between any music tracks
408 * due to DAPM power cycling.
409 */
410static void close_delayed_work(struct work_struct *work)
411{
412 struct snd_soc_pcm_runtime *rtd =
413 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
414 struct snd_soc_dai *codec_dai = rtd->codec_dai;
415
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100416 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100417
418 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
419 codec_dai->driver->playback.stream_name,
420 codec_dai->playback_active ? "active" : "inactive",
421 codec_dai->pop_wait ? "yes" : "no");
422
423 /* are we waiting on this codec DAI stream */
424 if (codec_dai->pop_wait == 1) {
425 codec_dai->pop_wait = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700426 snd_soc_dapm_stream_event(rtd,
427 codec_dai->driver->playback.stream_name,
428 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100429 }
430
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100431 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432}
433
434/*
435 * Called by ALSA when a PCM substream is closed. Private data can be
436 * freed here. The cpu DAI, codec DAI, machine and platform are also
437 * shutdown.
438 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100439static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440{
441 struct snd_soc_pcm_runtime *rtd = substream->private_data;
442 struct snd_soc_platform *platform = rtd->platform;
443 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
444 struct snd_soc_dai *codec_dai = rtd->codec_dai;
445 struct snd_soc_codec *codec = rtd->codec;
446
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100447 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448
449 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
450 cpu_dai->playback_active--;
451 codec_dai->playback_active--;
452 } else {
453 cpu_dai->capture_active--;
454 codec_dai->capture_active--;
455 }
456
457 cpu_dai->active--;
458 codec_dai->active--;
459 codec->active--;
460
Dong Aisheng17841022011-08-29 17:15:14 +0800461 /* clear the corresponding DAIs rate when inactive */
462 if (!cpu_dai->active)
463 cpu_dai->rate = 0;
464
465 if (!codec_dai->active)
466 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200467
Liam Girdwoodddee6272011-06-09 14:45:53 +0100468 /* Muting the DAC suppresses artifacts caused during digital
469 * shutdown, for example from stopping clocks.
470 */
471 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
472 snd_soc_dai_digital_mute(codec_dai, 1);
473
Helen Zengd5131792012-06-28 11:24:11 -0700474 if (cpu_dai->driver->ops->shutdown)
475 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
476
477 if (codec_dai->driver->ops->shutdown) {
Helen Zeng5749b092012-06-10 11:50:29 -0700478 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
479 codec_dai->driver->ops->shutdown(substream, codec_dai);
480 } else {
481 if (!codec_dai->capture_active)
482 codec_dai->driver->ops->shutdown(substream,
483 codec_dai);
484 }
485 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100486
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
488 rtd->dai_link->ops->shutdown(substream);
489
490 if (platform->driver->ops && platform->driver->ops->close)
491 platform->driver->ops->close(substream);
492 cpu_dai->runtime = NULL;
493
494 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700495 if (codec->ignore_pmdown_time ||
496 rtd->dai_link->ignore_pmdown_time ||
497 !rtd->pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300498 /* powered down playback stream now */
499 snd_soc_dapm_stream_event(rtd,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700500 codec_dai->driver->playback.stream_name,
501 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300502 } else {
503 /* start delayed pop wq here for playback streams */
504 codec_dai->pop_wait = 1;
505 schedule_delayed_work(&rtd->delayed_work,
506 msecs_to_jiffies(rtd->pmdown_time));
507 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100508 } else {
509 /* capture streams can be powered down now */
Helen Zeng5749b092012-06-10 11:50:29 -0700510 if (!codec_dai->capture_active)
511 snd_soc_dapm_stream_event(rtd,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700512 codec_dai->driver->capture.stream_name,
513 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100514 }
515
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100516 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000517
518 pm_runtime_put(platform->dev);
519 pm_runtime_put(codec_dai->dev);
520 pm_runtime_put(cpu_dai->dev);
521
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522 return 0;
523}
524
525/*
526 * Called by ALSA when the PCM substream is prepared, can set format, sample
527 * rate, etc. This function is non atomic and can be called multiple times,
528 * it can refer to the runtime info.
529 */
530static int soc_pcm_prepare(struct snd_pcm_substream *substream)
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
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100538 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
540 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
541 ret = rtd->dai_link->ops->prepare(substream);
542 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700543 printk(KERN_ERR "asoc: machine prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100544 goto out;
545 }
546 }
547
548 if (platform->driver->ops && platform->driver->ops->prepare) {
549 ret = platform->driver->ops->prepare(substream);
550 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700551 printk(KERN_ERR "asoc: platform prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100552 goto out;
553 }
554 }
555
556 if (codec_dai->driver->ops->prepare) {
557 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
558 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700559 printk(KERN_ERR "asoc: codec DAI prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 goto out;
561 }
562 }
563
564 if (cpu_dai->driver->ops->prepare) {
565 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
566 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700567 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100568 goto out;
569 }
570 }
571
572 /* cancel any delayed stream shutdown that is pending */
573 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
574 codec_dai->pop_wait) {
575 codec_dai->pop_wait = 0;
576 cancel_delayed_work(&rtd->delayed_work);
577 }
578
Steve Mucklef132c6c2012-06-06 18:30:57 -0700579 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
580 snd_soc_dapm_stream_event(rtd,
581 codec_dai->driver->playback.stream_name,
582 SND_SOC_DAPM_STREAM_START);
Helen Zeng5749b092012-06-10 11:50:29 -0700583 else {
584 if (codec_dai->capture_active == 1)
585 snd_soc_dapm_stream_event(rtd,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700586 codec_dai->driver->capture.stream_name,
587 SND_SOC_DAPM_STREAM_START);
Helen Zeng5749b092012-06-10 11:50:29 -0700588 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589 snd_soc_dai_digital_mute(codec_dai, 0);
590
591out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100592 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593 return ret;
594}
595
596/*
597 * Called by ALSA when the hardware params are set by application. This
598 * function can also be called multiple times and can allocate buffers
599 * (using snd_pcm_lib_* ). It's non-atomic.
600 */
601static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
602 struct snd_pcm_hw_params *params)
603{
604 struct snd_soc_pcm_runtime *rtd = substream->private_data;
605 struct snd_soc_platform *platform = rtd->platform;
606 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
607 struct snd_soc_dai *codec_dai = rtd->codec_dai;
608 int ret = 0;
609
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100610 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100611
612 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
613 ret = rtd->dai_link->ops->hw_params(substream, params);
614 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700615 printk(KERN_ERR "asoc: machine hw_params failed\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616 goto out;
617 }
618 }
619
620 if (codec_dai->driver->ops->hw_params) {
Helen Zeng5749b092012-06-10 11:50:29 -0700621 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
622 ret = codec_dai->driver->ops->hw_params(substream,
623 params, codec_dai);
624 if (ret < 0) {
625 printk(KERN_ERR "not set codec %s hw params\n",
626 codec_dai->name);
627 goto codec_err;
628 }
629 } else {
630 if (codec_dai->capture_active == 1) {
631 ret = codec_dai->driver->ops->hw_params(
632 substream, params, codec_dai);
633 if (ret < 0) {
634 printk(KERN_ERR "fail: %s hw params\n",
635 codec_dai->name);
636 goto codec_err;
637 }
638 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100639 }
640 }
641
642 if (cpu_dai->driver->ops->hw_params) {
643 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
644 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700645 printk(KERN_ERR "asoc: interface %s hw params failed\n",
646 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100647 goto interface_err;
648 }
649 }
650
651 if (platform->driver->ops && platform->driver->ops->hw_params) {
652 ret = platform->driver->ops->hw_params(substream, params);
653 if (ret < 0) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700654 printk(KERN_ERR "asoc: platform %s hw params failed\n",
655 platform->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100656 goto platform_err;
657 }
658 }
659
Dong Aisheng17841022011-08-29 17:15:14 +0800660 /* store the rate for each DAIs */
661 cpu_dai->rate = params_rate(params);
662 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100663
Steve Mucklef132c6c2012-06-06 18:30:57 -0700664 /* malloc a page for hostless IO.
665 * FIXME: rework with alsa-lib changes so that this malloc is not required.
666 */
667 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
668 substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
669 substream->dma_buffer.dev.dev = rtd->dev;
670 substream->dma_buffer.dev.dev->coherent_dma_mask = DMA_BIT_MASK(32);
671 substream->dma_buffer.private_data = NULL;
672
673 ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
674 if (ret < 0)
675 goto platform_err;
676 }
677
Liam Girdwoodddee6272011-06-09 14:45:53 +0100678out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100679 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100680 return ret;
681
682platform_err:
683 if (cpu_dai->driver->ops->hw_free)
684 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
685
686interface_err:
687 if (codec_dai->driver->ops->hw_free)
688 codec_dai->driver->ops->hw_free(substream, codec_dai);
689
690codec_err:
691 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
692 rtd->dai_link->ops->hw_free(substream);
693
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100694 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100695 return ret;
696}
697
698/*
699 * Frees resources allocated by hw_params, can be called multiple times
700 */
701static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
702{
703 struct snd_soc_pcm_runtime *rtd = substream->private_data;
704 struct snd_soc_platform *platform = rtd->platform;
705 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
706 struct snd_soc_dai *codec_dai = rtd->codec_dai;
707 struct snd_soc_codec *codec = rtd->codec;
708
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100709 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100710
711 /* apply codec digital mute */
712 if (!codec->active)
713 snd_soc_dai_digital_mute(codec_dai, 1);
714
715 /* free any machine hw params */
716 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
717 rtd->dai_link->ops->hw_free(substream);
718
719 /* free any DMA resources */
720 if (platform->driver->ops && platform->driver->ops->hw_free)
721 platform->driver->ops->hw_free(substream);
722
723 /* now free hw params for the DAIs */
724 if (codec_dai->driver->ops->hw_free)
725 codec_dai->driver->ops->hw_free(substream, codec_dai);
726
727 if (cpu_dai->driver->ops->hw_free)
728 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
729
Steve Mucklef132c6c2012-06-06 18:30:57 -0700730 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
731 snd_pcm_lib_free_pages(substream);
732
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100733 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100734 return 0;
735}
736
737static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
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
745 if (codec_dai->driver->ops->trigger) {
Helen Zeng5749b092012-06-10 11:50:29 -0700746 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
747 ret = codec_dai->driver->ops->trigger(substream,
748 cmd, codec_dai);
749 if (ret < 0)
750 return ret;
751 } else {
752 if (codec_dai->capture_active == 1) {
753 ret = codec_dai->driver->ops->trigger(
754 substream, cmd, codec_dai);
755 if (ret < 0)
756 return ret;
757 }
758 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100759 }
760
761 if (platform->driver->ops && platform->driver->ops->trigger) {
762 ret = platform->driver->ops->trigger(substream, cmd);
763 if (ret < 0)
764 return ret;
765 }
766
767 if (cpu_dai->driver->ops->trigger) {
768 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
769 if (ret < 0)
770 return ret;
771 }
772 return 0;
773}
774
Steve Mucklef132c6c2012-06-06 18:30:57 -0700775int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream, int cmd)
776{
777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
778 struct snd_soc_platform *platform = rtd->platform;
779 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
780 struct snd_soc_dai *codec_dai = rtd->codec_dai;
781 int ret;
782
783 if (codec_dai->driver->ops->bespoke_trigger) {
784 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
785 if (ret < 0)
786 return ret;
787 }
788
789 if (platform->driver->bespoke_trigger) {
790 ret = platform->driver->bespoke_trigger(substream, cmd);
791 if (ret < 0)
792 return ret;
793 }
794
795 if (cpu_dai->driver->ops->bespoke_trigger) {
796 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
797 if (ret < 0)
798 return ret;
799 }
800 return 0;
801}
802
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803/*
804 * soc level wrapper for pointer callback
805 * If cpu_dai, codec_dai, platform driver has the delay callback, than
806 * the runtime->delay will be updated accordingly.
807 */
808static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
809{
810 struct snd_soc_pcm_runtime *rtd = substream->private_data;
811 struct snd_soc_platform *platform = rtd->platform;
812 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
813 struct snd_soc_dai *codec_dai = rtd->codec_dai;
814 struct snd_pcm_runtime *runtime = substream->runtime;
815 snd_pcm_uframes_t offset = 0;
816 snd_pcm_sframes_t delay = 0;
817
818 if (platform->driver->ops && platform->driver->ops->pointer)
819 offset = platform->driver->ops->pointer(substream);
820
821 if (cpu_dai->driver->ops->delay)
822 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
823
824 if (codec_dai->driver->ops->delay)
825 delay += codec_dai->driver->ops->delay(substream, codec_dai);
826
827 if (platform->driver->delay)
828 delay += platform->driver->delay(substream, codec_dai);
829
830 runtime->delay = delay;
831
832 return offset;
833}
834
Steve Mucklef132c6c2012-06-06 18:30:57 -0700835static inline int be_connect(struct snd_soc_pcm_runtime *fe,
836 struct snd_soc_pcm_runtime *be, int stream)
837{
838 struct snd_soc_dpcm_params *dpcm_params;
839
840 if (!fe->dpcm[stream].runtime) {
841 dev_err(fe->dev, "%s no runtime\n", fe->dai_link->name);
842 return -ENODEV;
843 }
844
845 /* only add new dpcm_paramss */
846 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
847 if (dpcm_params->be == be && dpcm_params->fe == fe)
848 return 0;
849 }
850
851 dpcm_params = kzalloc(sizeof(struct snd_soc_dpcm_params), GFP_KERNEL);
852 if (!dpcm_params)
853 return -ENOMEM;
854
855 dpcm_params->be = be;
856 dpcm_params->fe = fe;
857 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
858 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_NEW;
859 list_add(&dpcm_params->list_be, &fe->dpcm[stream].be_clients);
860 list_add(&dpcm_params->list_fe, &be->dpcm[stream].fe_clients);
861
862 dev_dbg(fe->dev, " connected new DSP %s path %s %s %s\n",
863 stream ? "capture" : "playback", fe->dai_link->name,
864 stream ? "<-" : "->", be->dai_link->name);
865
866#ifdef CONFIG_DEBUG_FS
867 dpcm_params->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
868 fe->debugfs_dpcm_root, &dpcm_params->state);
869#endif
870
871 return 1;
872}
873
874static inline void be_reparent(struct snd_soc_pcm_runtime *fe,
875 struct snd_soc_pcm_runtime *be, int stream)
876{
877 struct snd_soc_dpcm_params *dpcm_params;
878 struct snd_pcm_substream *fe_substream, *be_substream;
879
880 /* reparent if BE is connected to other FEs */
881 if (!be->dpcm[stream].users)
882 return;
883
884 be_substream = snd_soc_dpcm_get_substream(be, stream);
885
886 list_for_each_entry(dpcm_params, &be->dpcm[stream].fe_clients, list_fe) {
887 if (dpcm_params->fe != fe) {
888
889 dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
890 stream ? "capture" : "playback",
891 dpcm_params->fe->dai_link->name,
892 stream ? "<-" : "->", dpcm_params->be->dai_link->name);
893
894 fe_substream = snd_soc_dpcm_get_substream(dpcm_params->fe,
895 stream);
896 be_substream->runtime = fe_substream->runtime;
897 break;
898 }
899 }
900}
901
902static inline void be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
903{
904 struct snd_soc_dpcm_params *dpcm_params, *d;
905
906 list_for_each_entry_safe(dpcm_params, d, &fe->dpcm[stream].be_clients, list_be) {
907 dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
908 stream ? "capture" : "playback",
909 dpcm_params->be->dai_link->name);
910
911 if (dpcm_params->state == SND_SOC_DPCM_LINK_STATE_FREE) {
912 dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
913 stream ? "capture" : "playback", fe->dai_link->name,
914 stream ? "<-" : "->", dpcm_params->be->dai_link->name);
915
916 /* BEs still alive need new FE */
917 be_reparent(fe, dpcm_params->be, stream);
918
919#ifdef CONFIG_DEBUG_FS
920 debugfs_remove(dpcm_params->debugfs_state);
921#endif
922
923 list_del(&dpcm_params->list_be);
924 list_del(&dpcm_params->list_fe);
925 kfree(dpcm_params);
926 }
927 }
928}
929
930static struct snd_soc_pcm_runtime *be_get_rtd(struct snd_soc_card *card,
931 struct snd_soc_dapm_widget *widget)
932{
933 struct snd_soc_pcm_runtime *be;
934 int i;
935
936 if (!widget->sname) {
937 dev_err(card->dev, "widget %s has no stream\n", widget->name);
938 return NULL;
939 }
940
941 for (i = 0; i < card->num_links; i++) {
942 be = &card->rtd[i];
943
944 if (!strcmp(widget->sname, be->dai_link->stream_name))
945 return be;
946 }
947
948 dev_err(card->dev, "can't get BE for %s\n", widget->name);
949 return NULL;
950}
951
952static struct snd_soc_dapm_widget *be_get_widget(struct snd_soc_card *card,
953 struct snd_soc_pcm_runtime *rtd)
954{
955 struct snd_soc_dapm_widget *widget;
956
957 list_for_each_entry(widget, &card->widgets, list) {
958
959 if (!widget->sname)
960 continue;
961
962 if (!strcmp(widget->sname, rtd->dai_link->stream_name))
963 return widget;
964 }
965
966 dev_err(card->dev, "can't get widget for %s\n",
967 rtd->dai_link->stream_name);
968 return NULL;
969}
970
971static int widget_in_list(struct snd_soc_dapm_widget_list *list,
972 struct snd_soc_dapm_widget *widget)
973{
974 int i;
975
976 for (i = 0; i < list->num_widgets; i++) {
977 if (widget == list->widgets[i])
978 return 1;
979 }
980
981 return 0;
982}
983
984static int fe_path_get(struct snd_soc_pcm_runtime *fe,
985 int stream, struct snd_soc_dapm_widget_list **list_)
986{
987 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
988 struct snd_soc_dapm_widget_list *list;
989 int paths;
990
991 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
992 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
993 if (list == NULL)
994 return -ENOMEM;
995
996 /* get number of valid DAI paths and their widgets */
997 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
998
999 dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
1000 stream ? "capture" : "playback");
1001
1002 *list_ = list;
1003 return paths;
1004}
1005
1006static inline void fe_path_put(struct snd_soc_dapm_widget_list **list)
1007{
1008 kfree(*list);
1009}
1010
1011static int be_prune_old(struct snd_soc_pcm_runtime *fe, int stream,
1012 struct snd_soc_dapm_widget_list **list_)
1013{
1014 struct snd_soc_card *card = fe->card;
1015 struct snd_soc_dpcm_params *dpcm_params;
1016 struct snd_soc_dapm_widget_list *list = *list_;
1017 struct snd_soc_dapm_widget *widget;
1018 int old = 0;
1019
1020 /* Destroy any old FE <--> BE connections */
1021 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1022
1023 /* is there a valid widget for this BE */
1024 widget = be_get_widget(card, dpcm_params->be);
1025 if (!widget) {
1026 dev_err(fe->dev, "no widget found for %s\n",
1027 dpcm_params->be->dai_link->name);
1028 continue;
1029 }
1030
1031 /* prune the BE if it's no longer in our active list */
1032 if (widget_in_list(list, widget))
1033 continue;
1034
1035 dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
1036 stream ? "capture" : "playback", dpcm_params->be->dai_link->name,
1037 fe->dai_link->name);
1038 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
1039 dpcm_params->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1040 old++;
1041 }
1042
1043 dev_dbg(fe->dev, "found %d old BEs\n", old);
1044 return old;
1045}
1046
1047static int be_add_new(struct snd_soc_pcm_runtime *fe, int stream,
1048 struct snd_soc_dapm_widget_list **list_)
1049{
1050 struct snd_soc_card *card = fe->card;
1051 struct snd_soc_dapm_widget_list *list = *list_;
1052 enum snd_soc_dapm_type be_type;
1053 int i, new = 0, err;
1054
1055 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1056 be_type = snd_soc_dapm_aif_out;
1057 else
1058 be_type = snd_soc_dapm_aif_in;
1059
1060 /* Create any new FE <--> BE connections */
1061 for (i = 0; i < list->num_widgets; i++) {
1062
1063 if (list->widgets[i]->id == be_type) {
1064 struct snd_soc_pcm_runtime *be;
1065
1066 /* is there a valid BE rtd for this widget */
1067 be = be_get_rtd(card, list->widgets[i]);
1068 if (!be) {
1069 dev_err(fe->dev, "no BE found for %s\n",
1070 list->widgets[i]->name);
1071 continue;
1072 }
1073
1074 /* don't connect if FE is not running */
1075 if (!fe->dpcm[stream].runtime)
1076 continue;
1077
1078 /* newly connected FE and BE */
1079 err = be_connect(fe, be, stream);
1080 if (err < 0) {
1081 dev_err(fe->dev, "can't connect %s\n", list->widgets[i]->name);
1082 break;
1083 } else if (err == 0) /* already connected */
1084 continue;
1085
1086 /* new */
1087 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1088 new++;
1089 }
1090 }
1091
1092 dev_dbg(fe->dev, "found %d new BEs\n", new);
1093 return new;
1094}
1095
1096/*
1097 * Find the corresponding BE DAIs that source or sink audio to this
1098 * FE substream.
1099 */
1100static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1101 int stream, struct snd_soc_dapm_widget_list **list, int new)
1102{
1103 if (new)
1104 return be_add_new(fe, stream, list);
1105 else
1106 return be_prune_old(fe, stream, list);
1107 return 0;
1108}
1109
1110/*
1111 * Clear the runtime pending state of all BE's.
1112 */
1113static void fe_clear_pending(struct snd_soc_pcm_runtime *fe, int stream)
1114{
1115 struct snd_soc_dpcm_params *dpcm_params;
1116
1117 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
1118 dpcm_params->be->dpcm[stream].runtime_update =
1119 SND_SOC_DPCM_UPDATE_NO;
1120}
1121
1122/* Unwind the BE startup */
1123static void soc_dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe, int stream)
1124{
1125 struct snd_soc_dpcm_params *dpcm_params;
1126
1127 /* disable any enabled and non active backends */
1128 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1129
1130 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1131 struct snd_pcm_substream *be_substream =
1132 snd_soc_dpcm_get_substream(be, stream);
1133
1134 if (be->dpcm[stream].users == 0)
1135 dev_err(be->dev, "no users %s at close - state %d\n",
1136 stream ? "capture" : "playback", be->dpcm[stream].state);
1137
1138 if (--be->dpcm[stream].users != 0)
1139 continue;
1140
1141 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1142 continue;
1143
1144 soc_pcm_close(be_substream);
1145 be_substream->runtime = NULL;
1146
1147 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1148 }
1149}
1150
1151/* Startup all new BE */
1152static int soc_dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1153{
1154 struct snd_soc_dpcm_params *dpcm_params;
1155 int err, count = 0;
1156
1157 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1158 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1159
1160 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1161 struct snd_pcm_substream *be_substream =
1162 snd_soc_dpcm_get_substream(be, stream);
1163
1164 /* is this op for this BE ? */
1165 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1166 continue;
1167
1168 /* first time the dpcm_params is open ? */
1169 if (be->dpcm[stream].users == MAX_BE_USERS)
1170 dev_err(be->dev, "too many users %s at open - state %d\n",
1171 stream ? "capture" : "playback", be->dpcm[stream].state);
1172
1173 if (be->dpcm[stream].users++ != 0)
1174 continue;
1175
1176 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1177 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1178 continue;
1179
1180 dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
1181
1182 be_substream->runtime = be->dpcm[stream].runtime;
1183 err = soc_pcm_open(be_substream);
1184 if (err < 0) {
1185 dev_err(be->dev, "BE open failed %d\n", err);
1186 be->dpcm[stream].users--;
1187 if (be->dpcm[stream].users < 0)
1188 dev_err(be->dev, "no users %s at unwind - state %d\n",
1189 stream ? "capture" : "playback",
1190 be->dpcm[stream].state);
1191
1192 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1193 goto unwind;
1194 }
1195
1196 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1197 count++;
1198 }
1199
1200 return count;
1201
1202unwind:
1203 /* disable any enabled and non active backends */
1204 list_for_each_entry_continue_reverse(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1205 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1206 struct snd_pcm_substream *be_substream =
1207 snd_soc_dpcm_get_substream(be, stream);
1208
1209 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1210 continue;
1211
1212 if (be->dpcm[stream].users == 0)
1213 dev_err(be->dev, "no users %s at close - state %d\n",
1214 stream ? "capture" : "playback", be->dpcm[stream].state);
1215
1216 if (--be->dpcm[stream].users != 0)
1217 continue;
1218
1219 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1220 continue;
1221
1222 soc_pcm_close(be_substream);
1223 be_substream->runtime = NULL;
1224
1225 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1226 }
1227
1228 return err;
1229}
1230
1231void soc_dpcm_set_dynamic_runtime(struct snd_pcm_substream *substream)
1232{
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1235 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1236 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1237
1238 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1239 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1240 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1241 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1242 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1243 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1244 runtime->hw.rates = cpu_dai_drv->playback.rates;
1245 } else {
1246 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1247 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1248 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1249 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1250 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1251 runtime->hw.rates = cpu_dai_drv->capture.rates;
1252 }
1253}
1254
1255static int soc_dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1256{
1257 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1258 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1259 int stream = fe_substream->stream, ret = 0;
1260
1261 mutex_lock(&fe->card->dpcm_mutex);
1262 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1263
1264 ret = soc_dpcm_be_dai_startup(fe, fe_substream->stream);
1265 if (ret < 0) {
1266 dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
1267 goto be_err;
1268 }
1269
1270 dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
1271
1272 /* start the DAI frontend */
1273 ret = soc_pcm_open(fe_substream);
1274 if (ret < 0) {
1275 dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
1276 goto unwind;
1277 }
1278
1279 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1280
1281 soc_dpcm_set_dynamic_runtime(fe_substream);
1282 snd_pcm_limit_hw_rates(runtime);
1283
1284 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1285 mutex_unlock(&fe->card->dpcm_mutex);
1286 return 0;
1287
1288unwind:
1289 soc_dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1290be_err:
1291 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1292 mutex_unlock(&fe->card->dpcm_mutex);
1293 return ret;
1294}
1295
1296/* BE shutdown - called on DAPM sync updates (i.e. FE is already running)*/
1297static int soc_dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1298{
1299 struct snd_soc_dpcm_params *dpcm_params;
1300
1301 /* only shutdown backends that are either sinks or sources to this frontend DAI */
1302 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1303
1304 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1305 struct snd_pcm_substream *be_substream =
1306 snd_soc_dpcm_get_substream(be, stream);
1307
1308 /* is this op for this BE ? */
1309 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1310 continue;
1311
1312 if (be->dpcm[stream].users == 0)
1313 dev_err(be->dev, "no users %s at close - state %d\n",
1314 stream ? "capture" : "playback", be->dpcm[stream].state);
1315
1316 if (--be->dpcm[stream].users != 0)
1317 continue;
1318
1319 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1320 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1321 continue;
1322
1323 dev_dbg(be->dev, "dpcm: close BE %s\n",
1324 dpcm_params->fe->dai_link->name);
1325
1326 soc_pcm_close(be_substream);
1327 be_substream->runtime = NULL;
1328
1329 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1330 }
1331 return 0;
1332}
1333
1334/* FE +BE shutdown - called on FE PCM ops */
1335static int soc_dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1336{
1337 struct snd_soc_pcm_runtime *fe = substream->private_data;
1338 int stream = substream->stream;
1339
1340 mutex_lock(&fe->card->dpcm_mutex);
1341 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1342
1343 dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
1344
1345 /* now shutdown the frontend */
1346 soc_pcm_close(substream);
1347
1348 /* shutdown the BEs */
1349 soc_dpcm_be_dai_shutdown(fe, substream->stream);
1350 /* run the stream event for each BE */
1351 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1352 soc_dpcm_dapm_stream_event(fe, stream,
1353 fe->cpu_dai->driver->playback.stream_name,
1354 SND_SOC_DAPM_STREAM_STOP);
1355 else
1356 soc_dpcm_dapm_stream_event(fe, stream,
1357 fe->cpu_dai->driver->capture.stream_name,
1358 SND_SOC_DAPM_STREAM_STOP);
1359
1360 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1361 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1362
1363 mutex_unlock(&fe->card->dpcm_mutex);
1364 return 0;
1365}
1366
1367static int soc_dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1368{
1369 struct snd_soc_dpcm_params *dpcm_params;
1370 int ret;
1371
1372 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1373
1374 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1375 struct snd_pcm_substream *be_substream =
1376 snd_soc_dpcm_get_substream(be, stream);
1377
1378 /* is this op for this BE ? */
1379 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1380 continue;
1381
1382 /* only allow hw_params() if no connected FEs are running */
1383 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1384 continue;
1385
1386 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1387 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1388 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1389 continue;
1390
1391 dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
1392 dpcm_params->fe->dai_link->name);
1393
1394 /* copy params for each dpcm_params */
1395 memcpy(&dpcm_params->hw_params, &fe->dpcm[stream].hw_params,
1396 sizeof(struct snd_pcm_hw_params));
1397
1398 /* perform any hw_params fixups */
1399 if (be->dai_link->be_hw_params_fixup) {
1400 ret = be->dai_link->be_hw_params_fixup(be,
1401 &dpcm_params->hw_params);
1402 if (ret < 0) {
1403 dev_err(be->dev,
1404 "dpcm: hw_params BE fixup failed %d\n",
1405 ret);
1406 goto unwind;
1407 }
1408 }
1409
1410 ret = soc_pcm_hw_params(be_substream, &dpcm_params->hw_params);
1411 if (ret < 0) {
1412 dev_err(dpcm_params->be->dev, "dpcm: hw_params BE failed %d\n", ret);
1413 goto unwind;
1414 }
1415
1416 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1417 }
1418 return 0;
1419
1420unwind:
1421 /* disable any enabled and non active backends */
1422 list_for_each_entry_continue_reverse(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1423 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1424 struct snd_pcm_substream *be_substream =
1425 snd_soc_dpcm_get_substream(be, stream);
1426
1427 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1428 continue;
1429
1430 /* only allow hw_free() if no connected FEs are running */
1431 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1432 continue;
1433
1434 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1435 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1436 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1438 continue;
1439
1440 soc_pcm_hw_free(be_substream);
1441 }
1442
1443 return ret;
1444}
1445
1446int soc_dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1447 struct snd_pcm_hw_params *params)
1448{
1449 struct snd_soc_pcm_runtime *fe = substream->private_data;
1450 int ret, stream = substream->stream;
1451
1452 mutex_lock(&fe->card->dpcm_mutex);
1453 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1454
1455 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1456 sizeof(struct snd_pcm_hw_params));
1457 ret = soc_dpcm_be_dai_hw_params(fe, substream->stream);
1458 if (ret < 0) {
1459 dev_err(fe->dev,"dpcm: hw_params failed for some BEs %d\n", ret);
1460 goto out;
1461 }
1462
1463 dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
1464 fe->dai_link->name, params_rate(params), params_channels(params),
1465 params_format(params));
1466
1467 /* call hw_params on the frontend */
1468 ret = soc_pcm_hw_params(substream, params);
1469 if (ret < 0) {
1470 dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
1471 soc_dpcm_be_dai_hw_free(fe, stream);
1472 } else
1473 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1474
1475out:
1476 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1477 mutex_unlock(&fe->card->dpcm_mutex);
1478 return ret;
1479}
1480
1481static int dpcm_do_trigger(struct snd_soc_dpcm_params *dpcm_params,
1482 struct snd_pcm_substream *substream, int cmd)
1483{
1484 int ret;
1485
1486 dev_dbg(dpcm_params->be->dev, "dpcm: trigger BE %s cmd %d\n",
1487 dpcm_params->fe->dai_link->name, cmd);
1488
1489 ret = soc_pcm_trigger(substream, cmd);
1490 if (ret < 0)
1491 dev_err(dpcm_params->be->dev,"dpcm: trigger BE failed %d\n", ret);
1492
1493 return ret;
1494}
1495
1496int soc_dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd)
1497{
1498 struct snd_soc_dpcm_params *dpcm_params;
1499 int ret = 0;
1500
Steve Mucklef132c6c2012-06-06 18:30:57 -07001501 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1502
1503 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1504 struct snd_pcm_substream *be_substream =
1505 snd_soc_dpcm_get_substream(be, stream);
1506
1507 /* is this op for this BE ? */
1508 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1509 continue;
1510
1511 switch (cmd) {
1512 case SNDRV_PCM_TRIGGER_START:
1513 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1514 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1515 continue;
1516
1517 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1518 if (ret)
1519 return ret;
1520
1521 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1522 break;
1523 case SNDRV_PCM_TRIGGER_RESUME:
1524 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1525 continue;
1526
1527 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1528 if (ret)
1529 return ret;
1530
1531 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1532 break;
1533 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1534 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1535 continue;
1536
1537 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1538 if (ret)
1539 return ret;
1540
1541 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1542 break;
1543 case SNDRV_PCM_TRIGGER_STOP:
1544 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1545 continue;
1546
1547 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1548 continue;
1549
1550 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1551 if (ret)
1552 return ret;
1553
1554 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1555 break;
1556 case SNDRV_PCM_TRIGGER_SUSPEND:
1557 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1558 continue;
1559
1560 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1561 continue;
1562
1563 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1564 if (ret)
1565 return ret;
1566
1567 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1568 break;
1569 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1570 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1571 continue;
1572
1573 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1574 continue;
1575
1576 ret = dpcm_do_trigger(dpcm_params, be_substream, cmd);
1577 if (ret)
1578 return ret;
1579
1580 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1581 break;
1582 }
1583 }
1584
1585 return ret;
1586}
1587EXPORT_SYMBOL_GPL(soc_dpcm_be_dai_trigger);
1588
1589int soc_dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1590{
1591 struct snd_soc_pcm_runtime *fe = substream->private_data;
1592 int stream = substream->stream, ret;
1593 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1594
1595 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1596
1597 switch (trigger) {
1598 case SND_SOC_DPCM_TRIGGER_PRE:
1599 /* call trigger on the frontend before the backend. */
1600
1601 dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
1602 fe->dai_link->name, cmd);
1603
1604 ret = soc_pcm_trigger(substream, cmd);
1605 if (ret < 0) {
1606 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1607 goto out;
1608 }
1609
1610 ret = soc_dpcm_be_dai_trigger(fe, substream->stream, cmd);
1611 break;
1612 case SND_SOC_DPCM_TRIGGER_POST:
1613 /* call trigger on the frontend after the backend. */
1614
1615 ret = soc_dpcm_be_dai_trigger(fe, substream->stream, cmd);
1616 if (ret < 0) {
1617 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1618 goto out;
1619 }
1620
1621 dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
1622 fe->dai_link->name, cmd);
1623
1624 ret = soc_pcm_trigger(substream, cmd);
1625 break;
1626 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1627 /* bespoke trigger() - handles both FE and BEs */
1628
1629 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd %d\n",
1630 fe->dai_link->name, cmd);
1631
1632 ret = soc_pcm_bespoke_trigger(substream, cmd);
1633 if (ret < 0) {
1634 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1635 goto out;
1636 }
1637 break;
1638 default:
1639 dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
1640 fe->dai_link->name);
1641 ret = -EINVAL;
1642 goto out;
1643 }
1644
1645 switch (cmd) {
1646 case SNDRV_PCM_TRIGGER_START:
1647 case SNDRV_PCM_TRIGGER_RESUME:
1648 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1649 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1650 break;
1651 case SNDRV_PCM_TRIGGER_STOP:
1652 case SNDRV_PCM_TRIGGER_SUSPEND:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001653 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1654 break;
Banajit Goswami9c5f4d12013-02-14 18:24:08 -08001655 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1656 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1657 break;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001658 }
1659
1660out:
1661 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1662 return ret;
1663}
1664
1665static int soc_dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1666{
1667 struct snd_soc_dpcm_params *dpcm_params;
1668 int ret = 0;
1669
1670 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1671
1672 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1673 struct snd_pcm_substream *be_substream =
1674 snd_soc_dpcm_get_substream(be, stream);
1675
1676 /* is this op for this BE ? */
1677 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1678 continue;
1679
1680 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1681 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1682 continue;
1683
1684 dev_dbg(be->dev, "dpcm: prepare BE %s\n",
1685 dpcm_params->fe->dai_link->name);
1686
1687 ret = soc_pcm_prepare(be_substream);
1688 if (ret < 0) {
1689 dev_err(be->dev, "dpcm: backend prepare failed %d\n",
1690 ret);
1691 break;
1692 }
1693
1694 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1695 }
1696 return ret;
1697}
1698
1699int soc_dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1700{
1701 struct snd_soc_pcm_runtime *fe = substream->private_data;
1702 int stream = substream->stream, ret = 0;
1703
1704 mutex_lock(&fe->card->dpcm_mutex);
1705
1706 dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
1707
1708 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1709
1710 /* there is no point preparing this FE if there are no BEs */
1711 if (list_empty(&fe->dpcm[stream].be_clients)) {
1712 dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
1713 fe->dai_link->name);
1714 ret = -EINVAL;
1715 goto out;
1716 }
1717
1718 ret = soc_dpcm_be_dai_prepare(fe, substream->stream);
1719 if (ret < 0)
1720 goto out;
1721
1722 /* call prepare on the frontend */
1723 ret = soc_pcm_prepare(substream);
1724 if (ret < 0) {
1725 dev_err(fe->dev,"dpcm: prepare FE %s failed\n", fe->dai_link->name);
1726 goto out;
1727 }
1728
1729 /* run the stream event for each BE */
1730 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1731 soc_dpcm_dapm_stream_event(fe, stream,
1732 fe->cpu_dai->driver->playback.stream_name,
1733 SND_SOC_DAPM_STREAM_START);
1734 else
1735 soc_dpcm_dapm_stream_event(fe, stream,
1736 fe->cpu_dai->driver->capture.stream_name,
1737 SND_SOC_DAPM_STREAM_START);
1738
1739 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1740
1741out:
1742 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1743 mutex_unlock(&fe->card->dpcm_mutex);
1744 return ret;
1745}
1746
1747static int soc_dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1748{
1749 struct snd_soc_dpcm_params *dpcm_params;
1750
1751 /* only hw_params backends that are either sinks or sources
1752 * to this frontend DAI */
1753 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1754
1755 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1756 struct snd_pcm_substream *be_substream =
1757 snd_soc_dpcm_get_substream(be, stream);
1758
1759 /* is this op for this BE ? */
1760 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1761 continue;
1762
1763 /* only free hw when no longer used - check all FEs */
1764 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1765 continue;
1766
1767 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1768 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1769 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Laxminath Kasamf55a6602013-04-03 14:03:17 +05301770 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1771 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1772 !((be->dpcm[stream].state == SND_SOC_DPCM_STATE_START) &&
1773 ((fe->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
1774 (fe->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1775 (fe->dpcm[stream].state !=
1776 SND_SOC_DPCM_STATE_SUSPEND))))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001777 continue;
1778
1779 dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
1780 dpcm_params->fe->dai_link->name);
1781
1782 soc_pcm_hw_free(be_substream);
1783
1784 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1785 }
1786
1787 return 0;
1788}
1789
1790int soc_dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1791{
1792 struct snd_soc_pcm_runtime *fe = substream->private_data;
1793 int err, stream = substream->stream;
1794
1795 mutex_lock(&fe->card->dpcm_mutex);
1796 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1797
1798 dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
1799
1800 /* call hw_free on the frontend */
1801 err = soc_pcm_hw_free(substream);
1802 if (err < 0)
1803 dev_err(fe->dev,"dpcm: hw_free FE %s failed\n", fe->dai_link->name);
1804
1805 /* only hw_params backends that are either sinks or sources
1806 * to this frontend DAI */
1807 err = soc_dpcm_be_dai_hw_free(fe, stream);
1808
1809 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1810 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1811
1812 mutex_unlock(&fe->card->dpcm_mutex);
1813 return 0;
1814}
1815
1816static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1817 unsigned int cmd, void *arg)
1818{
1819 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1820 struct snd_soc_platform *platform = rtd->platform;
1821
1822 if (platform->driver->ops->ioctl)
1823 return platform->driver->ops->ioctl(substream, cmd, arg);
1824 return snd_pcm_lib_ioctl(substream, cmd, arg);
1825}
1826
1827static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1828{
1829 struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1830 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1831 int err;
1832
1833 dev_dbg(fe->dev, "runtime %s close on FE %s\n",
1834 stream ? "capture" : "playback", fe->dai_link->name);
1835
1836 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1837 /* call bespoke trigger - FE takes care of all BE triggers */
1838 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd stop\n",
1839 fe->dai_link->name);
1840
1841 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1842 if (err < 0)
1843 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1844 } else {
1845 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd stop\n",
1846 fe->dai_link->name);
1847
1848 err = soc_dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1849 if (err < 0)
1850 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
1851 }
1852
1853 err = soc_dpcm_be_dai_hw_free(fe, stream);
1854 if (err < 0)
1855 dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
1856
1857 err = soc_dpcm_be_dai_shutdown(fe, stream);
1858 if (err < 0)
1859 dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
1860
1861 /* run the stream event for each BE */
1862 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1863 soc_dpcm_dapm_stream_event(fe, stream,
1864 fe->cpu_dai->driver->playback.stream_name,
1865 SND_SOC_DAPM_STREAM_NOP);
1866 else
1867 soc_dpcm_dapm_stream_event(fe, stream,
1868 fe->cpu_dai->driver->capture.stream_name,
1869 SND_SOC_DAPM_STREAM_NOP);
1870
1871 return 0;
1872}
1873
1874static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1875{
1876 struct snd_pcm_substream *substream = snd_soc_dpcm_get_substream(fe, stream);
1877 struct snd_soc_dpcm_params *dpcm_params;
1878 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1879 int ret;
1880
1881 dev_dbg(fe->dev, "runtime %s open on FE %s\n",
1882 stream ? "capture" : "playback", fe->dai_link->name);
1883
1884 /* Only start the BE if the FE is ready */
1885 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1886 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1887 return -EINVAL;
1888
1889 /* startup must always be called for new BEs */
1890 ret = soc_dpcm_be_dai_startup(fe, stream);
1891 if (ret < 0) {
1892 goto disconnect;
1893 return ret;
1894 }
1895
1896 /* keep going if FE state is > open */
1897 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1898 return 0;
1899
1900 ret = soc_dpcm_be_dai_hw_params(fe, stream);
1901 if (ret < 0) {
1902 goto close;
1903 return ret;
1904 }
1905
1906 /* keep going if FE state is > hw_params */
1907 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1908 return 0;
1909
1910
1911 ret = soc_dpcm_be_dai_prepare(fe, stream);
1912 if (ret < 0) {
1913 goto hw_free;
1914 return ret;
1915 }
1916
1917 /* run the stream event for each BE */
1918 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1919 soc_dpcm_dapm_stream_event(fe, stream,
1920 fe->cpu_dai->driver->playback.stream_name,
1921 SND_SOC_DAPM_STREAM_NOP);
1922 else
1923 soc_dpcm_dapm_stream_event(fe, stream,
1924 fe->cpu_dai->driver->capture.stream_name,
1925 SND_SOC_DAPM_STREAM_NOP);
1926
1927 /* keep going if FE state is > prepare */
1928 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1929 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1930 return 0;
1931
1932 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1933 /* call trigger on the frontend - FE takes care of all BE triggers */
1934 dev_dbg(fe->dev, "dpcm: bespoke trigger FE %s cmd start\n",
1935 fe->dai_link->name);
1936
1937 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1938 if (ret < 0) {
1939 dev_err(fe->dev,"dpcm: bespoke trigger FE failed %d\n", ret);
1940 goto hw_free;
1941 }
1942 } else {
1943 dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
1944 fe->dai_link->name);
1945
1946 ret = soc_dpcm_be_dai_trigger(fe, stream,
1947 SNDRV_PCM_TRIGGER_START);
1948 if (ret < 0) {
1949 dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
1950 goto hw_free;
1951 }
1952 }
1953
1954 return 0;
1955
1956hw_free:
1957 soc_dpcm_be_dai_hw_free(fe, stream);
1958close:
1959 soc_dpcm_be_dai_shutdown(fe, stream);
1960disconnect:
1961 /* disconnect any non started BEs */
1962 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
1963 struct snd_soc_pcm_runtime *be = dpcm_params->be;
1964 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1965 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
1966 }
1967
1968 return ret;
1969}
1970
1971static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1972{
1973 int ret;
1974
1975 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1976 ret = dpcm_run_update_startup(fe, stream);
1977 if (ret < 0)
1978 dev_err(fe->dev, "failed to startup some BEs\n");
1979 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1980
1981 return ret;
1982}
1983
1984static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1985{
1986 int ret;
1987
1988 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1989 ret = dpcm_run_update_shutdown(fe, stream);
1990 if (ret < 0)
1991 dev_err(fe->dev, "failed to shutdown some BEs\n");
1992 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1993
1994 return ret;
1995}
1996
1997/* called when any mixer updates change FE -> BE the stream */
1998int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
1999{
2000 struct snd_soc_card *card;
2001 int i, ret = 0, old, new, paths;
2002
2003 if (widget->codec)
2004 card = widget->codec->card;
2005 else if (widget->platform)
2006 card = widget->platform->card;
2007 else
2008 return -EINVAL;
2009
2010 mutex_lock(&card->dpcm_mutex);
2011
2012 for (i = 0; i < card->num_rtd; i++) {
2013 struct snd_soc_dapm_widget_list *list;
2014 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2015
2016 /* make sure link is FE */
2017 if (!fe->dai_link->dynamic)
2018 continue;
2019
2020 /* only check active links */
2021 if (!fe->cpu_dai->active)
2022 continue;
2023
2024 /* DAPM sync will call this to update DSP paths */
2025 dev_dbg(fe->dev, "DPCM runtime update for FE %s\n", fe->dai_link->name);
2026
2027 /* skip if FE doesn't have playback capability */
2028 if (!fe->cpu_dai->driver->playback.channels_min)
2029 goto capture;
2030
2031 paths = fe_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2032 if (paths < 0) {
SathishKumar Manief4feb32012-10-09 13:31:13 -07002033 pr_warn_ratelimited("%s no valid %s route from source to sink\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002034 fe->dai_link->name, "playback");
Iliyan Malchev734dc212012-10-02 09:22:36 -07002035 WARN_ON(1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002036 ret = paths;
2037 goto out;
2038 }
2039
2040 /* update any new playback paths */
2041 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2042 if (new) {
2043 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2044 fe_clear_pending(fe, SNDRV_PCM_STREAM_PLAYBACK);
2045 be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2046 }
2047
2048 /* update any old playback paths */
2049 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2050 if (old) {
2051 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2052 fe_clear_pending(fe, SNDRV_PCM_STREAM_PLAYBACK);
2053 be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2054 }
2055
Banajit Goswamiebeaafc2012-08-19 23:09:58 -07002056 fe_path_put(&list);
2057
Steve Mucklef132c6c2012-06-06 18:30:57 -07002058capture:
2059 /* skip if FE doesn't have capture capability */
2060 if (!fe->cpu_dai->driver->capture.channels_min)
2061 continue;
2062
2063 paths = fe_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2064 if (paths < 0) {
SathishKumar Manief4feb32012-10-09 13:31:13 -07002065 pr_warn_ratelimited("%s no valid %s route from source to sink\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002066 fe->dai_link->name, "capture");
2067 ret = paths;
2068 goto out;
2069 }
2070
2071 /* update any new capture paths */
2072 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2073 if (new) {
2074 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2075 fe_clear_pending(fe, SNDRV_PCM_STREAM_CAPTURE);
2076 be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2077 }
2078
2079 /* update any old capture paths */
2080 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2081 if (old) {
2082 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2083 fe_clear_pending(fe, SNDRV_PCM_STREAM_CAPTURE);
2084 be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2085 }
2086
2087 fe_path_put(&list);
2088 }
2089
2090out:
2091 mutex_unlock(&card->dpcm_mutex);
2092 return ret;
2093}
2094
2095int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2096{
2097 struct snd_soc_dpcm_params *dpcm_params;
2098
2099 list_for_each_entry(dpcm_params,
2100 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2101
2102 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2103 struct snd_soc_dai *dai = be->codec_dai;
2104 struct snd_soc_dai_driver *drv = dai->driver;
2105
2106 if (be->dai_link->ignore_suspend)
2107 continue;
2108
2109 dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
2110
2111 if (drv->ops->digital_mute && dai->playback_active)
2112 drv->ops->digital_mute(dai, mute);
2113 }
2114
2115 return 0;
2116}
2117
2118int soc_dpcm_be_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe)
2119{
2120 struct snd_soc_dpcm_params *dpcm_params;
2121
2122 /* suspend for playback */
2123 list_for_each_entry(dpcm_params,
2124 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2125
2126 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2127 struct snd_soc_dai *dai = be->cpu_dai;
2128 struct snd_soc_dai_driver *drv = dai->driver;
2129
2130 if (be->dai_link->ignore_suspend)
2131 continue;
2132
2133 dev_dbg(be->dev, "pm: BE CPU DAI playback suspend %s\n",
2134 be->dai_link->name);
2135
2136 if (drv->suspend && !drv->ac97_control)
2137 drv->suspend(dai);
2138 }
2139
2140 /* suspend for capture */
2141 list_for_each_entry(dpcm_params,
2142 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2143
2144 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2145 struct snd_soc_dai *dai = be->cpu_dai;
2146 struct snd_soc_dai_driver *drv = dai->driver;
2147
2148 if (be->dai_link->ignore_suspend)
2149 continue;
2150
2151 dev_dbg(be->dev, "pm: BE CPU DAI capture suspend %s\n",
2152 be->dai_link->name);
2153
2154 if (drv->suspend && !drv->ac97_control)
2155 drv->suspend(dai);
2156 }
2157
2158 return 0;
2159}
2160
2161int soc_dpcm_be_ac97_cpu_dai_suspend(struct snd_soc_pcm_runtime *fe)
2162{
2163 struct snd_soc_dpcm_params *dpcm_params;
2164
2165 /* suspend for playback */
2166 list_for_each_entry(dpcm_params,
2167 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2168
2169 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2170 struct snd_soc_dai *dai = be->cpu_dai;
2171 struct snd_soc_dai_driver *drv = dai->driver;
2172
2173 if (be->dai_link->ignore_suspend)
2174 continue;
2175
2176 dev_dbg(be->dev, "pm: BE CPU DAI playback suspend %s\n",
2177 be->dai_link->name);
2178
2179 if (drv->suspend && drv->ac97_control)
2180 drv->suspend(dai);
2181 }
2182
2183 /* suspend for capture */
2184 list_for_each_entry(dpcm_params,
2185 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2186
2187 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2188 struct snd_soc_dai *dai = be->cpu_dai;
2189 struct snd_soc_dai_driver *drv = dai->driver;
2190
2191 if (be->dai_link->ignore_suspend)
2192 continue;
2193
2194 dev_dbg(be->dev, "pm: BE CPU DAI capture suspend %s\n",
2195 be->dai_link->name);
2196
2197 if (drv->suspend && drv->ac97_control)
2198 drv->suspend(dai);
2199 }
2200
2201 return 0;
2202}
2203
2204int soc_dpcm_be_platform_suspend(struct snd_soc_pcm_runtime *fe)
2205{
2206 struct snd_soc_dpcm_params *dpcm_params;
2207
2208 /* suspend for playback */
2209 list_for_each_entry(dpcm_params,
2210 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2211
2212 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2213 struct snd_soc_platform *platform = be->platform;
2214 struct snd_soc_platform_driver *drv = platform->driver;
2215 struct snd_soc_dai *dai = be->cpu_dai;
2216
2217 if (be->dai_link->ignore_suspend)
2218 continue;
2219
2220 dev_dbg(be->dev, "pm: BE platform playback suspend %s\n",
2221 be->dai_link->name);
2222
2223 if (drv->suspend && !platform->suspended) {
2224 drv->suspend(dai);
2225 platform->suspended = 1;
2226 }
2227 }
2228
2229 /* suspend for capture */
2230 list_for_each_entry(dpcm_params,
2231 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2232
2233 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2234 struct snd_soc_platform *platform = be->platform;
2235 struct snd_soc_platform_driver *drv = platform->driver;
2236 struct snd_soc_dai *dai = be->cpu_dai;
2237
2238 if (be->dai_link->ignore_suspend)
2239 continue;
2240
2241 dev_dbg(be->dev, "pm: BE platform capture suspend %s\n",
2242 be->dai_link->name);
2243
2244 if (drv->suspend && !platform->suspended) {
2245 drv->suspend(dai);
2246 platform->suspended = 1;
2247 }
2248 }
2249 return 0;
2250}
2251
2252int soc_dpcm_fe_suspend(struct snd_soc_pcm_runtime *fe)
2253{
2254 struct snd_soc_dai *dai = fe->cpu_dai;
2255 struct snd_soc_dai_driver *dai_drv = dai->driver;
2256 struct snd_soc_platform *platform = fe->platform;
2257 struct snd_soc_platform_driver *plat_drv = platform->driver;
2258
2259 if (dai_drv->suspend && !dai_drv->ac97_control)
2260 dai_drv->suspend(dai);
2261
2262 if (plat_drv->suspend && !platform->suspended) {
2263 plat_drv->suspend(dai);
2264 platform->suspended = 1;
2265 }
2266
2267 soc_dpcm_be_cpu_dai_suspend(fe);
2268 soc_dpcm_be_platform_suspend(fe);
2269
2270 return 0;
2271}
2272
2273int soc_dpcm_be_cpu_dai_resume(struct snd_soc_pcm_runtime *fe)
2274{
2275 struct snd_soc_dpcm_params *dpcm_params;
2276
2277 /* resume for playback */
2278 list_for_each_entry(dpcm_params,
2279 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2280
2281 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2282 struct snd_soc_dai *dai = be->cpu_dai;
2283 struct snd_soc_dai_driver *drv = dai->driver;
2284
2285 if (be->dai_link->ignore_suspend)
2286 continue;
2287
2288 dev_dbg(be->dev, "pm: BE CPU DAI playback resume %s\n",
2289 be->dai_link->name);
2290
2291 if (drv->resume && !drv->ac97_control)
2292 drv->resume(dai);
2293 }
2294
2295 /* suspend for capture */
2296 list_for_each_entry(dpcm_params,
2297 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2298
2299 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2300 struct snd_soc_dai *dai = be->cpu_dai;
2301 struct snd_soc_dai_driver *drv = dai->driver;
2302
2303 if (be->dai_link->ignore_suspend)
2304 continue;
2305
2306 dev_dbg(be->dev, "pm: BE CPU DAI capture resume %s\n",
2307 be->dai_link->name);
2308
2309 if (drv->resume && !drv->ac97_control)
2310 drv->resume(dai);
2311 }
2312
2313 return 0;
2314}
2315
2316int soc_dpcm_be_ac97_cpu_dai_resume(struct snd_soc_pcm_runtime *fe)
2317{
2318 struct snd_soc_dpcm_params *dpcm_params;
2319
2320 /* resume for playback */
2321 list_for_each_entry(dpcm_params,
2322 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2323
2324 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2325 struct snd_soc_dai *dai = be->cpu_dai;
2326 struct snd_soc_dai_driver *drv = dai->driver;
2327
2328 if (be->dai_link->ignore_suspend)
2329 continue;
2330
2331 dev_dbg(be->dev, "pm: BE CPU DAI playback resume %s\n",
2332 be->dai_link->name);
2333
2334 if (drv->resume && drv->ac97_control)
2335 drv->resume(dai);
2336 }
2337
2338 /* suspend for capture */
2339 list_for_each_entry(dpcm_params,
2340 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2341
2342 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2343 struct snd_soc_dai *dai = be->cpu_dai;
2344 struct snd_soc_dai_driver *drv = dai->driver;
2345
2346 if (be->dai_link->ignore_suspend)
2347 continue;
2348
2349 dev_dbg(be->dev, "pm: BE CPU DAI capture resume %s\n",
2350 be->dai_link->name);
2351
2352 if (drv->resume && drv->ac97_control)
2353 drv->resume(dai);
2354 }
2355
2356 return 0;
2357}
2358
2359int soc_dpcm_be_platform_resume(struct snd_soc_pcm_runtime *fe)
2360{
2361 struct snd_soc_dpcm_params *dpcm_params;
2362
2363 /* resume for playback */
2364 list_for_each_entry(dpcm_params,
2365 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients, list_be) {
2366
2367 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2368 struct snd_soc_platform *platform = be->platform;
2369 struct snd_soc_platform_driver *drv = platform->driver;
2370 struct snd_soc_dai *dai = be->cpu_dai;
2371
2372 if (be->dai_link->ignore_suspend)
2373 continue;
2374
2375 dev_dbg(be->dev, "pm: BE platform playback resume %s\n",
2376 be->dai_link->name);
2377
2378 if (drv->resume && platform->suspended) {
2379 drv->resume(dai);
2380 platform->suspended = 0;
2381 }
2382 }
2383
2384 /* resume for capture */
2385 list_for_each_entry(dpcm_params,
2386 &fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients, list_be) {
2387
2388 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2389 struct snd_soc_platform *platform = be->platform;
2390 struct snd_soc_platform_driver *drv = platform->driver;
2391 struct snd_soc_dai *dai = be->cpu_dai;
2392
2393 if (be->dai_link->ignore_suspend)
2394 continue;
2395
2396 dev_dbg(be->dev, "pm: BE platform capture resume %s\n",
2397 be->dai_link->name);
2398
2399 if (drv->resume && platform->suspended) {
2400 drv->resume(dai);
2401 platform->suspended = 0;
2402 }
2403 }
2404
2405 return 0;
2406}
2407
2408int soc_dpcm_fe_resume(struct snd_soc_pcm_runtime *fe)
2409{
2410 struct snd_soc_dai *dai = fe->cpu_dai;
2411 struct snd_soc_dai_driver *dai_drv = dai->driver;
2412 struct snd_soc_platform *platform = fe->platform;
2413 struct snd_soc_platform_driver *plat_drv = platform->driver;
2414
2415 soc_dpcm_be_cpu_dai_resume(fe);
2416 soc_dpcm_be_platform_resume(fe);
2417
2418 if (dai_drv->resume && !dai_drv->ac97_control)
2419 dai_drv->resume(dai);
2420
2421 if (plat_drv->resume && platform->suspended) {
2422 plat_drv->resume(dai);
2423 platform->suspended = 0;
2424 }
2425
2426 return 0;
2427}
2428
2429/* called when opening FE stream */
2430int soc_dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2431{
2432 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2433 struct snd_soc_dpcm_params *dpcm_params;
2434 struct snd_soc_dapm_widget_list *list;
2435 int ret;
2436 int stream = fe_substream->stream;
2437
2438 fe->dpcm[stream].runtime = fe_substream->runtime;
2439
2440 if (fe_path_get(fe, stream, &list) <= 0) {
SathishKumar Manief4feb32012-10-09 13:31:13 -07002441 pr_warn_ratelimited("asoc: %s no valid %s route from source to sink\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002442 fe->dai_link->name, stream ? "capture" : "playback");
2443 return -EINVAL;
2444 }
2445
2446 /* calculate valid and active FE <-> BE dpcm_paramss */
2447 dpcm_process_paths(fe, stream, &list, 1);
2448
2449 ret = soc_dpcm_fe_dai_startup(fe_substream);
2450 if (ret < 0) {
2451 /* clean up all links */
2452 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
2453 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
2454
2455 be_disconnect(fe, stream);
2456 fe->dpcm[stream].runtime = NULL;
2457 }
2458
2459 fe_clear_pending(fe, stream);
2460 fe_path_put(&list);
2461 return ret;
2462}
2463
2464/* called when closing FE stream */
2465int soc_dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2466{
2467 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2468 struct snd_soc_dpcm_params *dpcm_params;
2469 int stream = fe_substream->stream, ret;
2470
2471 ret = soc_dpcm_fe_dai_shutdown(fe_substream);
2472
2473 /* mark FE's links ready to prune */
2474 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be)
2475 dpcm_params->state = SND_SOC_DPCM_LINK_STATE_FREE;
2476
2477 be_disconnect(fe, stream);
2478
2479 fe->dpcm[stream].runtime = NULL;
2480
2481 return ret;
2482}
2483
Liam Girdwoodddee6272011-06-09 14:45:53 +01002484/* create a new pcm */
2485int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2486{
2487 struct snd_soc_codec *codec = rtd->codec;
2488 struct snd_soc_platform *platform = rtd->platform;
2489 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2490 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002491 struct snd_pcm_substream *substream[2];
Liam Girdwoodddee6272011-06-09 14:45:53 +01002492 struct snd_pcm *pcm;
2493 char new_name[64];
2494 int ret = 0, playback = 0, capture = 0;
2495
Steve Mucklef132c6c2012-06-06 18:30:57 -07002496 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2497 if (cpu_dai->driver->playback.channels_min)
2498 playback = 1;
2499 if (cpu_dai->driver->capture.channels_min)
2500 capture = 1;
2501 } else {
2502 if (codec_dai->driver->playback.channels_min)
2503 playback = 1;
2504 if (codec_dai->driver->capture.channels_min)
2505 capture = 1;
2506 }
Sangsu Parka5002312012-01-02 17:15:10 +09002507
Steve Mucklef132c6c2012-06-06 18:30:57 -07002508 /* create the PCM */
2509 if (rtd->dai_link->no_pcm) {
2510 snprintf(new_name, sizeof(new_name), "(%s)",
2511 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002512
Steve Mucklef132c6c2012-06-06 18:30:57 -07002513 ret = snd_pcm_new_soc_be(rtd->card->snd_card, new_name, num,
2514 playback, capture, &pcm);
2515 } else {
2516 if (rtd->dai_link->dynamic)
2517 snprintf(new_name, sizeof(new_name), "%s (*)",
2518 rtd->dai_link->stream_name);
2519 else
2520 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2521 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002522
Steve Mucklef132c6c2012-06-06 18:30:57 -07002523 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2524 capture, &pcm);
2525 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002526 if (ret < 0) {
2527 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
2528 return ret;
2529 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002530 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002531
2532 rtd->pcm = pcm;
2533 pcm->private_data = rtd;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002534 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2535
2536 substream[SNDRV_PCM_STREAM_PLAYBACK] =
2537 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
2538 substream[SNDRV_PCM_STREAM_CAPTURE] =
2539 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
2540
2541 if (rtd->dai_link->no_pcm) {
2542 if (playback)
2543 substream[SNDRV_PCM_STREAM_PLAYBACK]->private_data = rtd;
2544 if (capture)
2545 substream[SNDRV_PCM_STREAM_CAPTURE]->private_data = rtd;
2546 goto out;
2547 }
2548
2549 /* setup any hostless PCMs - i.e. no host IO is performed */
2550 if (rtd->dai_link->no_host_mode) {
2551 if (substream[SNDRV_PCM_STREAM_PLAYBACK]) {
2552 substream[SNDRV_PCM_STREAM_PLAYBACK]->hw_no_buffer = 1;
2553 snd_soc_set_runtime_hwparams(
2554 substream[SNDRV_PCM_STREAM_PLAYBACK],
2555 &no_host_hardware);
2556 }
2557 if (substream[SNDRV_PCM_STREAM_CAPTURE]) {
2558 substream[SNDRV_PCM_STREAM_CAPTURE]->hw_no_buffer = 1;
2559 snd_soc_set_runtime_hwparams(
2560 substream[SNDRV_PCM_STREAM_CAPTURE],
2561 &no_host_hardware);
2562 }
2563 }
2564
2565 /* ASoC PCM operations */
2566 if (rtd->dai_link->dynamic) {
2567 rtd->ops.open = soc_dpcm_fe_dai_open;
2568 rtd->ops.hw_params = soc_dpcm_fe_dai_hw_params;
2569 rtd->ops.prepare = soc_dpcm_fe_dai_prepare;
2570 rtd->ops.trigger = soc_dpcm_fe_dai_trigger;
2571 rtd->ops.hw_free = soc_dpcm_fe_dai_hw_free;
2572 rtd->ops.close = soc_dpcm_fe_dai_close;
2573 rtd->ops.pointer = soc_pcm_pointer;
2574 rtd->ops.ioctl = soc_pcm_ioctl;
2575 } else {
2576 rtd->ops.open = soc_pcm_open;
2577 rtd->ops.hw_params = soc_pcm_hw_params;
2578 rtd->ops.prepare = soc_pcm_prepare;
2579 rtd->ops.trigger = soc_pcm_trigger;
2580 rtd->ops.hw_free = soc_pcm_hw_free;
2581 rtd->ops.close = soc_pcm_close;
2582 rtd->ops.pointer = soc_pcm_pointer;
2583 rtd->ops.ioctl = soc_pcm_ioctl;
2584 }
2585
Liam Girdwoodddee6272011-06-09 14:45:53 +01002586 if (platform->driver->ops) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002587 rtd->ops.ack = platform->driver->ops->ack;
2588 rtd->ops.copy = platform->driver->ops->copy;
2589 rtd->ops.silence = platform->driver->ops->silence;
2590 rtd->ops.page = platform->driver->ops->page;
2591 rtd->ops.mmap = platform->driver->ops->mmap;
Santosh Mardif86a70e2012-10-09 10:40:17 +05302592 rtd->ops.restart = platform->driver->ops->restart;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002593 }
2594
2595 if (playback)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002596 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002597
2598 if (capture)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002599 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002600
2601 if (platform->driver->pcm_new) {
2602 ret = platform->driver->pcm_new(rtd);
2603 if (ret < 0) {
2604 pr_err("asoc: platform pcm constructor failed\n");
2605 return ret;
2606 }
2607 }
2608
2609 pcm->private_free = platform->driver->pcm_free;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002610out:
Liam Girdwoodddee6272011-06-09 14:45:53 +01002611 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
2612 cpu_dai->name);
2613 return ret;
2614}
Steve Mucklef132c6c2012-06-06 18:30:57 -07002615
2616#ifdef CONFIG_DEBUG_FS
2617static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2618{
2619 switch (state) {
2620 case SND_SOC_DPCM_STATE_NEW:
2621 return "new";
2622 case SND_SOC_DPCM_STATE_OPEN:
2623 return "open";
2624 case SND_SOC_DPCM_STATE_HW_PARAMS:
2625 return "hw_params";
2626 case SND_SOC_DPCM_STATE_PREPARE:
2627 return "prepare";
2628 case SND_SOC_DPCM_STATE_START:
2629 return "start";
2630 case SND_SOC_DPCM_STATE_STOP:
2631 return "stop";
2632 case SND_SOC_DPCM_STATE_SUSPEND:
2633 return "suspend";
2634 case SND_SOC_DPCM_STATE_PAUSED:
2635 return "paused";
2636 case SND_SOC_DPCM_STATE_HW_FREE:
2637 return "hw_free";
2638 case SND_SOC_DPCM_STATE_CLOSE:
2639 return "close";
2640 }
2641
2642 return "unknown";
2643}
2644
2645static int soc_dpcm_state_open_file(struct inode *inode, struct file *file)
2646{
2647 file->private_data = inode->i_private;
2648 return 0;
2649}
2650
2651static ssize_t soc_dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2652 int stream, char *buf, size_t size)
2653{
2654 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2655 struct snd_soc_dpcm_params *dpcm_params;
2656 ssize_t offset = 0;
2657
2658 /* FE state */
2659 offset += snprintf(buf + offset, size - offset,
2660 "[%s - %s]\n", fe->dai_link->name,
2661 stream ? "Capture" : "Playback");
2662
2663 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2664 dpcm_state_string(fe->dpcm[stream].state));
2665
2666 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2667 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2668 offset += snprintf(buf + offset, size - offset,
2669 "Hardware Params: "
2670 "Format = %s, Channels = %d, Rate = %d\n",
2671 snd_pcm_format_name(params_format(params)),
2672 params_channels(params),
2673 params_rate(params));
2674
2675 /* BEs state */
2676 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2677
2678 if (list_empty(&fe->dpcm[stream].be_clients)) {
2679 offset += snprintf(buf + offset, size - offset,
2680 " No active DSP links\n");
2681 goto out;
2682 }
2683
2684 list_for_each_entry(dpcm_params, &fe->dpcm[stream].be_clients, list_be) {
2685 struct snd_soc_pcm_runtime *be = dpcm_params->be;
2686
2687 offset += snprintf(buf + offset, size - offset,
2688 "- %s\n", be->dai_link->name);
2689
2690 offset += snprintf(buf + offset, size - offset,
2691 " State: %s\n",
2692 dpcm_state_string(fe->dpcm[stream].state));
2693
2694 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2695 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2696 offset += snprintf(buf + offset, size - offset,
2697 " Hardware Params: "
2698 "Format = %s, Channels = %d, Rate = %d\n",
2699 snd_pcm_format_name(params_format(params)),
2700 params_channels(params),
2701 params_rate(params));
2702 }
2703
2704out:
2705 return offset;
2706}
2707
2708static ssize_t soc_dpcm_state_read_file(struct file *file, char __user *user_buf,
2709 size_t count, loff_t *ppos)
2710{
2711 struct snd_soc_pcm_runtime *fe = file->private_data;
2712 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2713 char *buf;
2714
2715 buf = kmalloc(out_count, GFP_KERNEL);
2716 if (!buf)
2717 return -ENOMEM;
2718
2719 if (fe->cpu_dai->driver->playback.channels_min)
2720 offset += soc_dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2721 buf + offset, out_count - offset);
2722
2723 if (fe->cpu_dai->driver->capture.channels_min)
2724 offset += soc_dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2725 buf + offset, out_count - offset);
2726
2727 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2728
2729 kfree(buf);
2730
2731 return ret;
2732}
2733
2734static const struct file_operations soc_dpcm_state_fops = {
2735 .open = soc_dpcm_state_open_file,
2736 .read = soc_dpcm_state_read_file,
2737 .llseek = default_llseek,
2738};
2739
2740int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2741{
2742 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2743 rtd->card->debugfs_card_root);
2744 if (!rtd->debugfs_dpcm_root) {
2745 dev_dbg(rtd->dev,
2746 "ASoC: Failed to create dpcm debugfs directory %s\n",
2747 rtd->dai_link->name);
2748 return -EINVAL;
2749 }
2750
2751 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644,
2752 rtd->debugfs_dpcm_root,
2753 rtd, &soc_dpcm_state_fops);
2754
2755 return 0;
2756}
2757#endif