blob: e7f16b54a97d4f8aafb1839097d6995be8b9f70b [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>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
41 *
42 * Sets the substream runtime hardware parameters.
43 */
44int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
46{
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
56 return 0;
57}
58EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
59
Liam Girdwood01d75842012-04-25 12:12:49 +010060/* DPCM stream event, send event to FE and all active BEs. */
61static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
62 int event)
63{
64 struct snd_soc_dpcm *dpcm;
65
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
67
68 struct snd_soc_pcm_runtime *be = dpcm->be;
69
Liam Girdwood103d84a2012-11-19 14:39:15 +000070 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010071 be->dai_link->name, event, dir);
72
73 snd_soc_dapm_stream_event(be, dir, event);
74 }
75
76 snd_soc_dapm_stream_event(fe, dir, event);
77
78 return 0;
79}
80
Dong Aisheng17841022011-08-29 17:15:14 +080081static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010083{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010085 int ret;
86
Dong Aisheng17841022011-08-29 17:15:14 +080087 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010088 !rtd->dai_link->symmetric_rates)
89 return 0;
90
91 /* This can happen if multiple streams are starting simultaneously -
92 * the second can need to get its constraints before the first has
93 * picked a rate. Complain and allow the application to carry on.
94 */
Dong Aisheng17841022011-08-29 17:15:14 +080095 if (!soc_dai->rate) {
96 dev_warn(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +000097 "ASoC: Not enforcing symmetric_rates due to race\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +010098 return 0;
99 }
100
Liam Girdwood103d84a2012-11-19 14:39:15 +0000101 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100102
103 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
104 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +0800105 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100106 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +0800107 dev_err(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000108 "ASoC: Unable to apply rate symmetry constraint: %d\n",
109 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100110 return ret;
111 }
112
113 return 0;
114}
115
116/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000117 * List of sample sizes that might go over the bus for parameter
118 * application. There ought to be a wildcard sample size for things
119 * like the DAC/ADC resolution to use but there isn't right now.
120 */
121static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200122 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000123};
124
125static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
126 struct snd_soc_dai *dai)
127{
128 int ret, i, bits;
129
130 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
131 bits = dai->driver->playback.sig_bits;
132 else
133 bits = dai->driver->capture.sig_bits;
134
135 if (!bits)
136 return;
137
138 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000139 if (bits >= sample_sizes[i])
140 continue;
141
142 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
143 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000144 if (ret != 0)
145 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000146 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000147 bits, sample_sizes[i], ret);
148 }
149}
150
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200151static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
152 struct snd_soc_pcm_stream *codec_stream,
153 struct snd_soc_pcm_stream *cpu_stream)
154{
155 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
156 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
157 hw->channels_min = max(codec_stream->channels_min,
158 cpu_stream->channels_min);
159 hw->channels_max = min(codec_stream->channels_max,
160 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100161 if (hw->formats)
162 hw->formats &= codec_stream->formats & cpu_stream->formats;
163 else
164 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200165 hw->rates = codec_stream->rates & cpu_stream->rates;
166 if (codec_stream->rates
167 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
168 hw->rates |= cpu_stream->rates;
169 if (cpu_stream->rates
170 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
171 hw->rates |= codec_stream->rates;
172}
173
Mark Brown58ba9b22012-01-16 18:38:51 +0000174/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100175 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
176 * then initialized and any private data can be allocated. This also calls
177 * startup for the cpu DAI, platform, machine and codec DAI.
178 */
179static int soc_pcm_open(struct snd_pcm_substream *substream)
180{
181 struct snd_soc_pcm_runtime *rtd = substream->private_data;
182 struct snd_pcm_runtime *runtime = substream->runtime;
183 struct snd_soc_platform *platform = rtd->platform;
184 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
185 struct snd_soc_dai *codec_dai = rtd->codec_dai;
186 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
187 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
188 int ret = 0;
189
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800190 pinctrl_pm_select_default_state(cpu_dai->dev);
191 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000192 pm_runtime_get_sync(cpu_dai->dev);
193 pm_runtime_get_sync(codec_dai->dev);
194 pm_runtime_get_sync(platform->dev);
195
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100196 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100197
198 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700199 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100200 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
201 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000202 dev_err(cpu_dai->dev, "ASoC: can't open interface"
203 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100204 goto out;
205 }
206 }
207
208 if (platform->driver->ops && platform->driver->ops->open) {
209 ret = platform->driver->ops->open(substream);
210 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000211 dev_err(platform->dev, "ASoC: can't open platform"
212 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100213 goto platform_err;
214 }
215 }
216
Mark Brownc5914b02013-10-30 17:47:39 -0700217 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100218 ret = codec_dai->driver->ops->startup(substream, codec_dai);
219 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000220 dev_err(codec_dai->dev, "ASoC: can't open codec"
221 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100222 goto codec_dai_err;
223 }
224 }
225
226 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
227 ret = rtd->dai_link->ops->startup(substream);
228 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000229 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000230 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100231 goto machine_err;
232 }
233 }
234
Liam Girdwood01d75842012-04-25 12:12:49 +0100235 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
236 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
237 goto dynamic;
238
Liam Girdwoodddee6272011-06-09 14:45:53 +0100239 /* Check that the codec and cpu DAIs are compatible */
240 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200241 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
242 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100243 } else {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200244 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
245 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100246 }
247
248 ret = -EINVAL;
249 snd_pcm_limit_hw_rates(runtime);
250 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000251 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100252 codec_dai->name, cpu_dai->name);
253 goto config_err;
254 }
255 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000256 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100257 codec_dai->name, cpu_dai->name);
258 goto config_err;
259 }
260 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
261 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000262 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100263 codec_dai->name, cpu_dai->name);
264 goto config_err;
265 }
266
Mark Brown58ba9b22012-01-16 18:38:51 +0000267 soc_pcm_apply_msb(substream, codec_dai);
268 soc_pcm_apply_msb(substream, cpu_dai);
269
Liam Girdwoodddee6272011-06-09 14:45:53 +0100270 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800271 if (cpu_dai->active) {
272 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
273 if (ret != 0)
274 goto config_err;
275 }
276
277 if (codec_dai->active) {
278 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100279 if (ret != 0)
280 goto config_err;
281 }
282
Liam Girdwood103d84a2012-11-19 14:39:15 +0000283 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100284 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000285 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
286 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100287 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000288 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100289 runtime->hw.rate_max);
290
Liam Girdwood01d75842012-04-25 12:12:49 +0100291dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
293 cpu_dai->playback_active++;
294 codec_dai->playback_active++;
295 } else {
296 cpu_dai->capture_active++;
297 codec_dai->capture_active++;
298 }
299 cpu_dai->active++;
300 codec_dai->active++;
301 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100302 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100303 return 0;
304
305config_err:
306 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
307 rtd->dai_link->ops->shutdown(substream);
308
309machine_err:
310 if (codec_dai->driver->ops->shutdown)
311 codec_dai->driver->ops->shutdown(substream, codec_dai);
312
313codec_dai_err:
314 if (platform->driver->ops && platform->driver->ops->close)
315 platform->driver->ops->close(substream);
316
317platform_err:
318 if (cpu_dai->driver->ops->shutdown)
319 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
320out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100321 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000322
323 pm_runtime_put(platform->dev);
324 pm_runtime_put(codec_dai->dev);
325 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800326 if (!codec_dai->active)
327 pinctrl_pm_select_sleep_state(codec_dai->dev);
328 if (!cpu_dai->active)
329 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000330
Liam Girdwoodddee6272011-06-09 14:45:53 +0100331 return ret;
332}
333
334/*
335 * Power down the audio subsystem pmdown_time msecs after close is called.
336 * This is to ensure there are no pops or clicks in between any music tracks
337 * due to DAPM power cycling.
338 */
339static void close_delayed_work(struct work_struct *work)
340{
341 struct snd_soc_pcm_runtime *rtd =
342 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
343 struct snd_soc_dai *codec_dai = rtd->codec_dai;
344
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100345 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100346
Liam Girdwood103d84a2012-11-19 14:39:15 +0000347 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100348 codec_dai->driver->playback.stream_name,
349 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600350 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100351
352 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600353 if (rtd->pop_wait == 1) {
354 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800355 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000356 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100357 }
358
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100359 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100360}
361
362/*
363 * Called by ALSA when a PCM substream is closed. Private data can be
364 * freed here. The cpu DAI, codec DAI, machine and platform are also
365 * shutdown.
366 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100367static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100368{
369 struct snd_soc_pcm_runtime *rtd = substream->private_data;
370 struct snd_soc_platform *platform = rtd->platform;
371 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
372 struct snd_soc_dai *codec_dai = rtd->codec_dai;
373 struct snd_soc_codec *codec = rtd->codec;
374
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100375 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100376
377 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
378 cpu_dai->playback_active--;
379 codec_dai->playback_active--;
380 } else {
381 cpu_dai->capture_active--;
382 codec_dai->capture_active--;
383 }
384
385 cpu_dai->active--;
386 codec_dai->active--;
387 codec->active--;
388
Dong Aisheng17841022011-08-29 17:15:14 +0800389 /* clear the corresponding DAIs rate when inactive */
390 if (!cpu_dai->active)
391 cpu_dai->rate = 0;
392
393 if (!codec_dai->active)
394 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200395
Liam Girdwoodddee6272011-06-09 14:45:53 +0100396 /* Muting the DAC suppresses artifacts caused during digital
397 * shutdown, for example from stopping clocks.
398 */
Mark Brownda183962013-02-06 15:44:07 +0000399 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100400
401 if (cpu_dai->driver->ops->shutdown)
402 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
403
404 if (codec_dai->driver->ops->shutdown)
405 codec_dai->driver->ops->shutdown(substream, codec_dai);
406
407 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
408 rtd->dai_link->ops->shutdown(substream);
409
410 if (platform->driver->ops && platform->driver->ops->close)
411 platform->driver->ops->close(substream);
412 cpu_dai->runtime = NULL;
413
414 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000415 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200416 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300417 /* powered down playback stream now */
418 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800419 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800420 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300421 } else {
422 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600423 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100424 queue_delayed_work(system_power_efficient_wq,
425 &rtd->delayed_work,
426 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300427 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100428 } else {
429 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800430 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000431 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432 }
433
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100434 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000435
436 pm_runtime_put(platform->dev);
437 pm_runtime_put(codec_dai->dev);
438 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800439 if (!codec_dai->active)
440 pinctrl_pm_select_sleep_state(codec_dai->dev);
441 if (!cpu_dai->active)
442 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000443
Liam Girdwoodddee6272011-06-09 14:45:53 +0100444 return 0;
445}
446
447/*
448 * Called by ALSA when the PCM substream is prepared, can set format, sample
449 * rate, etc. This function is non atomic and can be called multiple times,
450 * it can refer to the runtime info.
451 */
452static int soc_pcm_prepare(struct snd_pcm_substream *substream)
453{
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_soc_platform *platform = rtd->platform;
456 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
457 struct snd_soc_dai *codec_dai = rtd->codec_dai;
458 int ret = 0;
459
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100460 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100461
462 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
463 ret = rtd->dai_link->ops->prepare(substream);
464 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000465 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
466 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100467 goto out;
468 }
469 }
470
471 if (platform->driver->ops && platform->driver->ops->prepare) {
472 ret = platform->driver->ops->prepare(substream);
473 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000474 dev_err(platform->dev, "ASoC: platform prepare error:"
475 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100476 goto out;
477 }
478 }
479
Mark Brownc5914b02013-10-30 17:47:39 -0700480 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100481 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
482 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000483 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000484 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 goto out;
486 }
487 }
488
Mark Brownc5914b02013-10-30 17:47:39 -0700489 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100490 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
491 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000492 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000493 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494 goto out;
495 }
496 }
497
498 /* cancel any delayed stream shutdown that is pending */
499 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600500 rtd->pop_wait) {
501 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100502 cancel_delayed_work(&rtd->delayed_work);
503 }
504
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000505 snd_soc_dapm_stream_event(rtd, substream->stream,
506 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100507
Mark Brownda183962013-02-06 15:44:07 +0000508 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100509
510out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100511 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100512 return ret;
513}
514
515/*
516 * Called by ALSA when the hardware params are set by application. This
517 * function can also be called multiple times and can allocate buffers
518 * (using snd_pcm_lib_* ). It's non-atomic.
519 */
520static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
521 struct snd_pcm_hw_params *params)
522{
523 struct snd_soc_pcm_runtime *rtd = substream->private_data;
524 struct snd_soc_platform *platform = rtd->platform;
525 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
526 struct snd_soc_dai *codec_dai = rtd->codec_dai;
527 int ret = 0;
528
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100529 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530
531 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
532 ret = rtd->dai_link->ops->hw_params(substream, params);
533 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000534 dev_err(rtd->card->dev, "ASoC: machine hw_params"
535 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536 goto out;
537 }
538 }
539
Mark Brownc5914b02013-10-30 17:47:39 -0700540 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100541 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
542 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000543 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
544 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545 goto codec_err;
546 }
547 }
548
Mark Brownc5914b02013-10-30 17:47:39 -0700549 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
551 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000552 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000553 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100554 goto interface_err;
555 }
556 }
557
558 if (platform->driver->ops && platform->driver->ops->hw_params) {
559 ret = platform->driver->ops->hw_params(substream, params);
560 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000561 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000562 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 goto platform_err;
564 }
565 }
566
Dong Aisheng17841022011-08-29 17:15:14 +0800567 /* store the rate for each DAIs */
568 cpu_dai->rate = params_rate(params);
569 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100570
571out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100572 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 return ret;
574
575platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700576 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
578
579interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700580 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100581 codec_dai->driver->ops->hw_free(substream, codec_dai);
582
583codec_err:
584 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
585 rtd->dai_link->ops->hw_free(substream);
586
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100587 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100588 return ret;
589}
590
591/*
592 * Frees resources allocated by hw_params, can be called multiple times
593 */
594static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
595{
596 struct snd_soc_pcm_runtime *rtd = substream->private_data;
597 struct snd_soc_platform *platform = rtd->platform;
598 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
599 struct snd_soc_dai *codec_dai = rtd->codec_dai;
600 struct snd_soc_codec *codec = rtd->codec;
601
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100602 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100603
604 /* apply codec digital mute */
605 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000606 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100607
608 /* free any machine hw params */
609 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
610 rtd->dai_link->ops->hw_free(substream);
611
612 /* free any DMA resources */
613 if (platform->driver->ops && platform->driver->ops->hw_free)
614 platform->driver->ops->hw_free(substream);
615
616 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700617 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618 codec_dai->driver->ops->hw_free(substream, codec_dai);
619
Mark Brownc5914b02013-10-30 17:47:39 -0700620 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100621 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
622
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100623 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 return 0;
625}
626
627static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
628{
629 struct snd_soc_pcm_runtime *rtd = substream->private_data;
630 struct snd_soc_platform *platform = rtd->platform;
631 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
632 struct snd_soc_dai *codec_dai = rtd->codec_dai;
633 int ret;
634
Mark Brownc5914b02013-10-30 17:47:39 -0700635 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100636 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
637 if (ret < 0)
638 return ret;
639 }
640
641 if (platform->driver->ops && platform->driver->ops->trigger) {
642 ret = platform->driver->ops->trigger(substream, cmd);
643 if (ret < 0)
644 return ret;
645 }
646
Mark Brownc5914b02013-10-30 17:47:39 -0700647 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100648 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
649 if (ret < 0)
650 return ret;
651 }
652 return 0;
653}
654
Mark Brown45c0a182012-05-09 21:46:27 +0100655static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
656 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100657{
658 struct snd_soc_pcm_runtime *rtd = substream->private_data;
659 struct snd_soc_platform *platform = rtd->platform;
660 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
661 struct snd_soc_dai *codec_dai = rtd->codec_dai;
662 int ret;
663
Mark Brownc5914b02013-10-30 17:47:39 -0700664 if (codec_dai->driver->ops &&
665 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100666 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
667 if (ret < 0)
668 return ret;
669 }
670
Mark Brownc5914b02013-10-30 17:47:39 -0700671 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100672 ret = platform->driver->bespoke_trigger(substream, cmd);
673 if (ret < 0)
674 return ret;
675 }
676
Mark Brownc5914b02013-10-30 17:47:39 -0700677 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100678 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
679 if (ret < 0)
680 return ret;
681 }
682 return 0;
683}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100684/*
685 * soc level wrapper for pointer callback
686 * If cpu_dai, codec_dai, platform driver has the delay callback, than
687 * the runtime->delay will be updated accordingly.
688 */
689static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
690{
691 struct snd_soc_pcm_runtime *rtd = substream->private_data;
692 struct snd_soc_platform *platform = rtd->platform;
693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
694 struct snd_soc_dai *codec_dai = rtd->codec_dai;
695 struct snd_pcm_runtime *runtime = substream->runtime;
696 snd_pcm_uframes_t offset = 0;
697 snd_pcm_sframes_t delay = 0;
698
699 if (platform->driver->ops && platform->driver->ops->pointer)
700 offset = platform->driver->ops->pointer(substream);
701
Mark Brownc5914b02013-10-30 17:47:39 -0700702 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
704
Mark Brownc5914b02013-10-30 17:47:39 -0700705 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100706 delay += codec_dai->driver->ops->delay(substream, codec_dai);
707
708 if (platform->driver->delay)
709 delay += platform->driver->delay(substream, codec_dai);
710
711 runtime->delay = delay;
712
713 return offset;
714}
715
Liam Girdwood01d75842012-04-25 12:12:49 +0100716/* connect a FE and BE */
717static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
718 struct snd_soc_pcm_runtime *be, int stream)
719{
720 struct snd_soc_dpcm *dpcm;
721
722 /* only add new dpcms */
723 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
724 if (dpcm->be == be && dpcm->fe == fe)
725 return 0;
726 }
727
728 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
729 if (!dpcm)
730 return -ENOMEM;
731
732 dpcm->be = be;
733 dpcm->fe = fe;
734 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
735 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
736 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
737 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
738
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300739 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100740 stream ? "capture" : "playback", fe->dai_link->name,
741 stream ? "<-" : "->", be->dai_link->name);
742
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100743#ifdef CONFIG_DEBUG_FS
744 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
745 fe->debugfs_dpcm_root, &dpcm->state);
746#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100747 return 1;
748}
749
750/* reparent a BE onto another FE */
751static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
752 struct snd_soc_pcm_runtime *be, int stream)
753{
754 struct snd_soc_dpcm *dpcm;
755 struct snd_pcm_substream *fe_substream, *be_substream;
756
757 /* reparent if BE is connected to other FEs */
758 if (!be->dpcm[stream].users)
759 return;
760
761 be_substream = snd_soc_dpcm_get_substream(be, stream);
762
763 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
764 if (dpcm->fe == fe)
765 continue;
766
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300767 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100768 stream ? "capture" : "playback",
769 dpcm->fe->dai_link->name,
770 stream ? "<-" : "->", dpcm->be->dai_link->name);
771
772 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
773 be_substream->runtime = fe_substream->runtime;
774 break;
775 }
776}
777
778/* disconnect a BE and FE */
779static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
780{
781 struct snd_soc_dpcm *dpcm, *d;
782
783 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000784 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100785 stream ? "capture" : "playback",
786 dpcm->be->dai_link->name);
787
788 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
789 continue;
790
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300791 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100792 stream ? "capture" : "playback", fe->dai_link->name,
793 stream ? "<-" : "->", dpcm->be->dai_link->name);
794
795 /* BEs still alive need new FE */
796 dpcm_be_reparent(fe, dpcm->be, stream);
797
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100798#ifdef CONFIG_DEBUG_FS
799 debugfs_remove(dpcm->debugfs_state);
800#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100801 list_del(&dpcm->list_be);
802 list_del(&dpcm->list_fe);
803 kfree(dpcm);
804 }
805}
806
807/* get BE for DAI widget and stream */
808static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
809 struct snd_soc_dapm_widget *widget, int stream)
810{
811 struct snd_soc_pcm_runtime *be;
812 int i;
813
814 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
815 for (i = 0; i < card->num_links; i++) {
816 be = &card->rtd[i];
817
Liam Girdwood35ea0652012-06-05 19:26:59 +0100818 if (!be->dai_link->no_pcm)
819 continue;
820
Liam Girdwood01d75842012-04-25 12:12:49 +0100821 if (be->cpu_dai->playback_widget == widget ||
822 be->codec_dai->playback_widget == widget)
823 return be;
824 }
825 } else {
826
827 for (i = 0; i < card->num_links; i++) {
828 be = &card->rtd[i];
829
Liam Girdwood35ea0652012-06-05 19:26:59 +0100830 if (!be->dai_link->no_pcm)
831 continue;
832
Liam Girdwood01d75842012-04-25 12:12:49 +0100833 if (be->cpu_dai->capture_widget == widget ||
834 be->codec_dai->capture_widget == widget)
835 return be;
836 }
837 }
838
Liam Girdwood103d84a2012-11-19 14:39:15 +0000839 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100840 stream ? "capture" : "playback", widget->name);
841 return NULL;
842}
843
844static inline struct snd_soc_dapm_widget *
845 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
846{
847 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
848 return rtd->cpu_dai->playback_widget;
849 else
850 return rtd->cpu_dai->capture_widget;
851}
852
853static inline struct snd_soc_dapm_widget *
854 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
855{
856 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
857 return rtd->codec_dai->playback_widget;
858 else
859 return rtd->codec_dai->capture_widget;
860}
861
862static int widget_in_list(struct snd_soc_dapm_widget_list *list,
863 struct snd_soc_dapm_widget *widget)
864{
865 int i;
866
867 for (i = 0; i < list->num_widgets; i++) {
868 if (widget == list->widgets[i])
869 return 1;
870 }
871
872 return 0;
873}
874
875static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
876 int stream, struct snd_soc_dapm_widget_list **list_)
877{
878 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
879 struct snd_soc_dapm_widget_list *list;
880 int paths;
881
882 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
883 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
884 if (list == NULL)
885 return -ENOMEM;
886
887 /* get number of valid DAI paths and their widgets */
888 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
889
Liam Girdwood103d84a2012-11-19 14:39:15 +0000890 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100891 stream ? "capture" : "playback");
892
893 *list_ = list;
894 return paths;
895}
896
897static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
898{
899 kfree(*list);
900}
901
902static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
903 struct snd_soc_dapm_widget_list **list_)
904{
905 struct snd_soc_dpcm *dpcm;
906 struct snd_soc_dapm_widget_list *list = *list_;
907 struct snd_soc_dapm_widget *widget;
908 int prune = 0;
909
910 /* Destroy any old FE <--> BE connections */
911 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
912
913 /* is there a valid CPU DAI widget for this BE */
914 widget = rtd_get_cpu_widget(dpcm->be, stream);
915
916 /* prune the BE if it's no longer in our active list */
917 if (widget && widget_in_list(list, widget))
918 continue;
919
920 /* is there a valid CODEC DAI widget for this BE */
921 widget = rtd_get_codec_widget(dpcm->be, stream);
922
923 /* prune the BE if it's no longer in our active list */
924 if (widget && widget_in_list(list, widget))
925 continue;
926
Liam Girdwood103d84a2012-11-19 14:39:15 +0000927 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100928 stream ? "capture" : "playback",
929 dpcm->be->dai_link->name, fe->dai_link->name);
930 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
931 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
932 prune++;
933 }
934
Liam Girdwood103d84a2012-11-19 14:39:15 +0000935 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100936 return prune;
937}
938
939static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
940 struct snd_soc_dapm_widget_list **list_)
941{
942 struct snd_soc_card *card = fe->card;
943 struct snd_soc_dapm_widget_list *list = *list_;
944 struct snd_soc_pcm_runtime *be;
945 int i, new = 0, err;
946
947 /* Create any new FE <--> BE connections */
948 for (i = 0; i < list->num_widgets; i++) {
949
Mark Brown46162742013-06-05 19:36:11 +0100950 switch (list->widgets[i]->id) {
951 case snd_soc_dapm_dai_in:
952 case snd_soc_dapm_dai_out:
953 break;
954 default:
Liam Girdwood01d75842012-04-25 12:12:49 +0100955 continue;
Mark Brown46162742013-06-05 19:36:11 +0100956 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100957
958 /* is there a valid BE rtd for this widget */
959 be = dpcm_get_be(card, list->widgets[i], stream);
960 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000961 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100962 list->widgets[i]->name);
963 continue;
964 }
965
966 /* make sure BE is a real BE */
967 if (!be->dai_link->no_pcm)
968 continue;
969
970 /* don't connect if FE is not running */
971 if (!fe->dpcm[stream].runtime)
972 continue;
973
974 /* newly connected FE and BE */
975 err = dpcm_be_connect(fe, be, stream);
976 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000977 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100978 list->widgets[i]->name);
979 break;
980 } else if (err == 0) /* already connected */
981 continue;
982
983 /* new */
984 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
985 new++;
986 }
987
Liam Girdwood103d84a2012-11-19 14:39:15 +0000988 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100989 return new;
990}
991
992/*
993 * Find the corresponding BE DAIs that source or sink audio to this
994 * FE substream.
995 */
996static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
997 int stream, struct snd_soc_dapm_widget_list **list, int new)
998{
999 if (new)
1000 return dpcm_add_paths(fe, stream, list);
1001 else
1002 return dpcm_prune_paths(fe, stream, list);
1003}
1004
1005static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1006{
1007 struct snd_soc_dpcm *dpcm;
1008
1009 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1010 dpcm->be->dpcm[stream].runtime_update =
1011 SND_SOC_DPCM_UPDATE_NO;
1012}
1013
1014static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1015 int stream)
1016{
1017 struct snd_soc_dpcm *dpcm;
1018
1019 /* disable any enabled and non active backends */
1020 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1021
1022 struct snd_soc_pcm_runtime *be = dpcm->be;
1023 struct snd_pcm_substream *be_substream =
1024 snd_soc_dpcm_get_substream(be, stream);
1025
1026 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001027 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001028 stream ? "capture" : "playback",
1029 be->dpcm[stream].state);
1030
1031 if (--be->dpcm[stream].users != 0)
1032 continue;
1033
1034 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1035 continue;
1036
1037 soc_pcm_close(be_substream);
1038 be_substream->runtime = NULL;
1039 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1040 }
1041}
1042
1043static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1044{
1045 struct snd_soc_dpcm *dpcm;
1046 int err, count = 0;
1047
1048 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1049 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1050
1051 struct snd_soc_pcm_runtime *be = dpcm->be;
1052 struct snd_pcm_substream *be_substream =
1053 snd_soc_dpcm_get_substream(be, stream);
1054
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001055 if (!be_substream) {
1056 dev_err(be->dev, "ASoC: no backend %s stream\n",
1057 stream ? "capture" : "playback");
1058 continue;
1059 }
1060
Liam Girdwood01d75842012-04-25 12:12:49 +01001061 /* is this op for this BE ? */
1062 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1063 continue;
1064
1065 /* first time the dpcm is open ? */
1066 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001067 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001068 stream ? "capture" : "playback",
1069 be->dpcm[stream].state);
1070
1071 if (be->dpcm[stream].users++ != 0)
1072 continue;
1073
1074 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1075 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1076 continue;
1077
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001078 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1079 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001080
1081 be_substream->runtime = be->dpcm[stream].runtime;
1082 err = soc_pcm_open(be_substream);
1083 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001084 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001085 be->dpcm[stream].users--;
1086 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001087 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001088 stream ? "capture" : "playback",
1089 be->dpcm[stream].state);
1090
1091 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1092 goto unwind;
1093 }
1094
1095 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1096 count++;
1097 }
1098
1099 return count;
1100
1101unwind:
1102 /* disable any enabled and non active backends */
1103 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1104 struct snd_soc_pcm_runtime *be = dpcm->be;
1105 struct snd_pcm_substream *be_substream =
1106 snd_soc_dpcm_get_substream(be, stream);
1107
1108 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1109 continue;
1110
1111 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001112 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001113 stream ? "capture" : "playback",
1114 be->dpcm[stream].state);
1115
1116 if (--be->dpcm[stream].users != 0)
1117 continue;
1118
1119 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1120 continue;
1121
1122 soc_pcm_close(be_substream);
1123 be_substream->runtime = NULL;
1124 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1125 }
1126
1127 return err;
1128}
1129
Mark Brown45c0a182012-05-09 21:46:27 +01001130static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001131{
1132 struct snd_pcm_runtime *runtime = substream->runtime;
1133 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1134 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1135 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1136
1137 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1138 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1139 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1140 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1141 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1142 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1143 runtime->hw.rates = cpu_dai_drv->playback.rates;
1144 } else {
1145 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1146 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1147 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1148 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1149 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1150 runtime->hw.rates = cpu_dai_drv->capture.rates;
1151 }
1152}
1153
1154static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1155{
1156 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1157 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1158 int stream = fe_substream->stream, ret = 0;
1159
1160 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1161
1162 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1163 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001164 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001165 goto be_err;
1166 }
1167
Liam Girdwood103d84a2012-11-19 14:39:15 +00001168 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001169
1170 /* start the DAI frontend */
1171 ret = soc_pcm_open(fe_substream);
1172 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001173 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001174 goto unwind;
1175 }
1176
1177 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1178
1179 dpcm_set_fe_runtime(fe_substream);
1180 snd_pcm_limit_hw_rates(runtime);
1181
1182 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1183 return 0;
1184
1185unwind:
1186 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1187be_err:
1188 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1189 return ret;
1190}
1191
1192static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1193{
1194 struct snd_soc_dpcm *dpcm;
1195
1196 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1197 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1198
1199 struct snd_soc_pcm_runtime *be = dpcm->be;
1200 struct snd_pcm_substream *be_substream =
1201 snd_soc_dpcm_get_substream(be, stream);
1202
1203 /* is this op for this BE ? */
1204 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1205 continue;
1206
1207 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001208 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001209 stream ? "capture" : "playback",
1210 be->dpcm[stream].state);
1211
1212 if (--be->dpcm[stream].users != 0)
1213 continue;
1214
1215 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1216 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1217 continue;
1218
Liam Girdwood103d84a2012-11-19 14:39:15 +00001219 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001220 dpcm->fe->dai_link->name);
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 return 0;
1228}
1229
1230static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1231{
1232 struct snd_soc_pcm_runtime *fe = substream->private_data;
1233 int stream = substream->stream;
1234
1235 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1236
1237 /* shutdown the BEs */
1238 dpcm_be_dai_shutdown(fe, substream->stream);
1239
Liam Girdwood103d84a2012-11-19 14:39:15 +00001240 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001241
1242 /* now shutdown the frontend */
1243 soc_pcm_close(substream);
1244
1245 /* run the stream event for each BE */
1246 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1247
1248 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1249 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1250 return 0;
1251}
1252
1253static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1254{
1255 struct snd_soc_dpcm *dpcm;
1256
1257 /* only hw_params backends that are either sinks or sources
1258 * to this frontend DAI */
1259 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1260
1261 struct snd_soc_pcm_runtime *be = dpcm->be;
1262 struct snd_pcm_substream *be_substream =
1263 snd_soc_dpcm_get_substream(be, stream);
1264
1265 /* is this op for this BE ? */
1266 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1267 continue;
1268
1269 /* only free hw when no longer used - check all FEs */
1270 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1271 continue;
1272
1273 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1274 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1275 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001276 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001277 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1278 continue;
1279
Liam Girdwood103d84a2012-11-19 14:39:15 +00001280 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001281 dpcm->fe->dai_link->name);
1282
1283 soc_pcm_hw_free(be_substream);
1284
1285 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1286 }
1287
1288 return 0;
1289}
1290
Mark Brown45c0a182012-05-09 21:46:27 +01001291static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001292{
1293 struct snd_soc_pcm_runtime *fe = substream->private_data;
1294 int err, stream = substream->stream;
1295
1296 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1297 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1298
Liam Girdwood103d84a2012-11-19 14:39:15 +00001299 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001300
1301 /* call hw_free on the frontend */
1302 err = soc_pcm_hw_free(substream);
1303 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001304 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001305 fe->dai_link->name);
1306
1307 /* only hw_params backends that are either sinks or sources
1308 * to this frontend DAI */
1309 err = dpcm_be_dai_hw_free(fe, stream);
1310
1311 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1312 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1313
1314 mutex_unlock(&fe->card->mutex);
1315 return 0;
1316}
1317
1318static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1319{
1320 struct snd_soc_dpcm *dpcm;
1321 int ret;
1322
1323 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1324
1325 struct snd_soc_pcm_runtime *be = dpcm->be;
1326 struct snd_pcm_substream *be_substream =
1327 snd_soc_dpcm_get_substream(be, stream);
1328
1329 /* is this op for this BE ? */
1330 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1331 continue;
1332
1333 /* only allow hw_params() if no connected FEs are running */
1334 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1335 continue;
1336
1337 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1338 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1339 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1340 continue;
1341
Liam Girdwood103d84a2012-11-19 14:39:15 +00001342 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001343 dpcm->fe->dai_link->name);
1344
1345 /* copy params for each dpcm */
1346 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1347 sizeof(struct snd_pcm_hw_params));
1348
1349 /* perform any hw_params fixups */
1350 if (be->dai_link->be_hw_params_fixup) {
1351 ret = be->dai_link->be_hw_params_fixup(be,
1352 &dpcm->hw_params);
1353 if (ret < 0) {
1354 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001355 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001356 ret);
1357 goto unwind;
1358 }
1359 }
1360
1361 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1362 if (ret < 0) {
1363 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001364 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001365 goto unwind;
1366 }
1367
1368 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1369 }
1370 return 0;
1371
1372unwind:
1373 /* disable any enabled and non active backends */
1374 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1375 struct snd_soc_pcm_runtime *be = dpcm->be;
1376 struct snd_pcm_substream *be_substream =
1377 snd_soc_dpcm_get_substream(be, stream);
1378
1379 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1380 continue;
1381
1382 /* only allow hw_free() if no connected FEs are running */
1383 if (!snd_soc_dpcm_can_be_free_stop(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 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1390 continue;
1391
1392 soc_pcm_hw_free(be_substream);
1393 }
1394
1395 return ret;
1396}
1397
Mark Brown45c0a182012-05-09 21:46:27 +01001398static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1399 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001400{
1401 struct snd_soc_pcm_runtime *fe = substream->private_data;
1402 int ret, stream = substream->stream;
1403
1404 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1405 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1406
1407 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1408 sizeof(struct snd_pcm_hw_params));
1409 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1410 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001411 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001412 goto out;
1413 }
1414
Liam Girdwood103d84a2012-11-19 14:39:15 +00001415 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001416 fe->dai_link->name, params_rate(params),
1417 params_channels(params), params_format(params));
1418
1419 /* call hw_params on the frontend */
1420 ret = soc_pcm_hw_params(substream, params);
1421 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001422 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001423 dpcm_be_dai_hw_free(fe, stream);
1424 } else
1425 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1426
1427out:
1428 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1429 mutex_unlock(&fe->card->mutex);
1430 return ret;
1431}
1432
1433static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1434 struct snd_pcm_substream *substream, int cmd)
1435{
1436 int ret;
1437
Liam Girdwood103d84a2012-11-19 14:39:15 +00001438 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001439 dpcm->fe->dai_link->name, cmd);
1440
1441 ret = soc_pcm_trigger(substream, cmd);
1442 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001443 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001444
1445 return ret;
1446}
1447
Mark Brown45c0a182012-05-09 21:46:27 +01001448static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1449 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001450{
1451 struct snd_soc_dpcm *dpcm;
1452 int ret = 0;
1453
1454 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1455
1456 struct snd_soc_pcm_runtime *be = dpcm->be;
1457 struct snd_pcm_substream *be_substream =
1458 snd_soc_dpcm_get_substream(be, stream);
1459
1460 /* is this op for this BE ? */
1461 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1462 continue;
1463
1464 switch (cmd) {
1465 case SNDRV_PCM_TRIGGER_START:
1466 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1467 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1468 continue;
1469
1470 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1471 if (ret)
1472 return ret;
1473
1474 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1475 break;
1476 case SNDRV_PCM_TRIGGER_RESUME:
1477 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1478 continue;
1479
1480 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1481 if (ret)
1482 return ret;
1483
1484 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1485 break;
1486 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1487 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1488 continue;
1489
1490 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1491 if (ret)
1492 return ret;
1493
1494 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1495 break;
1496 case SNDRV_PCM_TRIGGER_STOP:
1497 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1498 continue;
1499
1500 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1501 continue;
1502
1503 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1504 if (ret)
1505 return ret;
1506
1507 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1508 break;
1509 case SNDRV_PCM_TRIGGER_SUSPEND:
1510 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1511 continue;
1512
1513 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1514 continue;
1515
1516 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1517 if (ret)
1518 return ret;
1519
1520 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1521 break;
1522 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1523 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1524 continue;
1525
1526 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1527 continue;
1528
1529 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1530 if (ret)
1531 return ret;
1532
1533 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1534 break;
1535 }
1536 }
1537
1538 return ret;
1539}
1540EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1541
Mark Brown45c0a182012-05-09 21:46:27 +01001542static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001543{
1544 struct snd_soc_pcm_runtime *fe = substream->private_data;
1545 int stream = substream->stream, ret;
1546 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1547
1548 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1549
1550 switch (trigger) {
1551 case SND_SOC_DPCM_TRIGGER_PRE:
1552 /* call trigger on the frontend before the backend. */
1553
Liam Girdwood103d84a2012-11-19 14:39:15 +00001554 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001555 fe->dai_link->name, cmd);
1556
1557 ret = soc_pcm_trigger(substream, cmd);
1558 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001559 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001560 goto out;
1561 }
1562
1563 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1564 break;
1565 case SND_SOC_DPCM_TRIGGER_POST:
1566 /* call trigger on the frontend after the backend. */
1567
1568 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1569 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001570 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001571 goto out;
1572 }
1573
Liam Girdwood103d84a2012-11-19 14:39:15 +00001574 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001575 fe->dai_link->name, cmd);
1576
1577 ret = soc_pcm_trigger(substream, cmd);
1578 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001579 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1580 /* bespoke trigger() - handles both FE and BEs */
1581
Liam Girdwood103d84a2012-11-19 14:39:15 +00001582 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001583 fe->dai_link->name, cmd);
1584
1585 ret = soc_pcm_bespoke_trigger(substream, cmd);
1586 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001587 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001588 goto out;
1589 }
1590 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001591 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001592 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001593 fe->dai_link->name);
1594 ret = -EINVAL;
1595 goto out;
1596 }
1597
1598 switch (cmd) {
1599 case SNDRV_PCM_TRIGGER_START:
1600 case SNDRV_PCM_TRIGGER_RESUME:
1601 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1602 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1603 break;
1604 case SNDRV_PCM_TRIGGER_STOP:
1605 case SNDRV_PCM_TRIGGER_SUSPEND:
1606 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1607 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1608 break;
1609 }
1610
1611out:
1612 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1613 return ret;
1614}
1615
1616static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1617{
1618 struct snd_soc_dpcm *dpcm;
1619 int ret = 0;
1620
1621 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1622
1623 struct snd_soc_pcm_runtime *be = dpcm->be;
1624 struct snd_pcm_substream *be_substream =
1625 snd_soc_dpcm_get_substream(be, stream);
1626
1627 /* is this op for this BE ? */
1628 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1629 continue;
1630
1631 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1632 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1633 continue;
1634
Liam Girdwood103d84a2012-11-19 14:39:15 +00001635 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001636 dpcm->fe->dai_link->name);
1637
1638 ret = soc_pcm_prepare(be_substream);
1639 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001640 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001641 ret);
1642 break;
1643 }
1644
1645 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1646 }
1647 return ret;
1648}
1649
Mark Brown45c0a182012-05-09 21:46:27 +01001650static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001651{
1652 struct snd_soc_pcm_runtime *fe = substream->private_data;
1653 int stream = substream->stream, ret = 0;
1654
1655 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1656
Liam Girdwood103d84a2012-11-19 14:39:15 +00001657 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001658
1659 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1660
1661 /* there is no point preparing this FE if there are no BEs */
1662 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001663 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001664 fe->dai_link->name);
1665 ret = -EINVAL;
1666 goto out;
1667 }
1668
1669 ret = dpcm_be_dai_prepare(fe, substream->stream);
1670 if (ret < 0)
1671 goto out;
1672
1673 /* call prepare on the frontend */
1674 ret = soc_pcm_prepare(substream);
1675 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001676 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001677 fe->dai_link->name);
1678 goto out;
1679 }
1680
1681 /* run the stream event for each BE */
1682 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1683 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1684
1685out:
1686 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1687 mutex_unlock(&fe->card->mutex);
1688
1689 return ret;
1690}
1691
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001692static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1693 unsigned int cmd, void *arg)
1694{
1695 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1696 struct snd_soc_platform *platform = rtd->platform;
1697
Mark Brownc5914b02013-10-30 17:47:39 -07001698 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001699 return platform->driver->ops->ioctl(substream, cmd, arg);
1700 return snd_pcm_lib_ioctl(substream, cmd, arg);
1701}
1702
Liam Girdwood618dae12012-04-25 12:12:51 +01001703static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1704{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001705 struct snd_pcm_substream *substream =
1706 snd_soc_dpcm_get_substream(fe, stream);
1707 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001708 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001709
Liam Girdwood103d84a2012-11-19 14:39:15 +00001710 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001711 stream ? "capture" : "playback", fe->dai_link->name);
1712
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001713 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1714 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001715 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001716 fe->dai_link->name);
1717
1718 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1719 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001720 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001721 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001722 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001723 fe->dai_link->name);
1724
1725 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1726 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001727 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001728 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001729
1730 err = dpcm_be_dai_hw_free(fe, stream);
1731 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001732 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001733
1734 err = dpcm_be_dai_shutdown(fe, stream);
1735 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001736 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001737
1738 /* run the stream event for each BE */
1739 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1740
1741 return 0;
1742}
1743
1744static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1745{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001746 struct snd_pcm_substream *substream =
1747 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001748 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001749 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001750 int ret;
1751
Liam Girdwood103d84a2012-11-19 14:39:15 +00001752 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001753 stream ? "capture" : "playback", fe->dai_link->name);
1754
1755 /* Only start the BE if the FE is ready */
1756 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1757 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1758 return -EINVAL;
1759
1760 /* startup must always be called for new BEs */
1761 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001762 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001763 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001764
1765 /* keep going if FE state is > open */
1766 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1767 return 0;
1768
1769 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001770 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001771 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001772
1773 /* keep going if FE state is > hw_params */
1774 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1775 return 0;
1776
1777
1778 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001779 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001780 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001781
1782 /* run the stream event for each BE */
1783 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1784
1785 /* keep going if FE state is > prepare */
1786 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1787 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1788 return 0;
1789
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001790 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1791 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001792 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001793 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001794
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001795 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1796 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001797 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001798 goto hw_free;
1799 }
1800 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001801 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001802 fe->dai_link->name);
1803
1804 ret = dpcm_be_dai_trigger(fe, stream,
1805 SNDRV_PCM_TRIGGER_START);
1806 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001807 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001808 goto hw_free;
1809 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001810 }
1811
1812 return 0;
1813
1814hw_free:
1815 dpcm_be_dai_hw_free(fe, stream);
1816close:
1817 dpcm_be_dai_shutdown(fe, stream);
1818disconnect:
1819 /* disconnect any non started BEs */
1820 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1821 struct snd_soc_pcm_runtime *be = dpcm->be;
1822 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1823 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1824 }
1825
1826 return ret;
1827}
1828
1829static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1830{
1831 int ret;
1832
1833 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1834 ret = dpcm_run_update_startup(fe, stream);
1835 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001836 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001837 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1838
1839 return ret;
1840}
1841
1842static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1843{
1844 int ret;
1845
1846 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1847 ret = dpcm_run_update_shutdown(fe, stream);
1848 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001849 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001850 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1851
1852 return ret;
1853}
1854
1855/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1856 * any DAI links.
1857 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001858int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001859{
Liam Girdwood618dae12012-04-25 12:12:51 +01001860 int i, old, new, paths;
1861
Liam Girdwood618dae12012-04-25 12:12:51 +01001862 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1863 for (i = 0; i < card->num_rtd; i++) {
1864 struct snd_soc_dapm_widget_list *list;
1865 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1866
1867 /* make sure link is FE */
1868 if (!fe->dai_link->dynamic)
1869 continue;
1870
1871 /* only check active links */
1872 if (!fe->cpu_dai->active)
1873 continue;
1874
1875 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001876 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001877 fe->dai_link->name);
1878
1879 /* skip if FE doesn't have playback capability */
1880 if (!fe->cpu_dai->driver->playback.channels_min)
1881 goto capture;
1882
1883 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1884 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001885 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001886 fe->dai_link->name, "playback");
1887 mutex_unlock(&card->mutex);
1888 return paths;
1889 }
1890
1891 /* update any new playback paths */
1892 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1893 if (new) {
1894 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1895 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1896 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1897 }
1898
1899 /* update any old playback paths */
1900 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1901 if (old) {
1902 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1903 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1904 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1905 }
1906
1907capture:
1908 /* skip if FE doesn't have capture capability */
1909 if (!fe->cpu_dai->driver->capture.channels_min)
1910 continue;
1911
1912 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1913 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001914 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001915 fe->dai_link->name, "capture");
1916 mutex_unlock(&card->mutex);
1917 return paths;
1918 }
1919
1920 /* update any new capture paths */
1921 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1922 if (new) {
1923 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1924 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1925 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1926 }
1927
1928 /* update any old capture paths */
1929 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1930 if (old) {
1931 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1932 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1933 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1934 }
1935
1936 dpcm_path_put(&list);
1937 }
1938
1939 mutex_unlock(&card->mutex);
1940 return 0;
1941}
Liam Girdwood01d75842012-04-25 12:12:49 +01001942int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1943{
1944 struct snd_soc_dpcm *dpcm;
1945 struct list_head *clients =
1946 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1947
1948 list_for_each_entry(dpcm, clients, list_be) {
1949
1950 struct snd_soc_pcm_runtime *be = dpcm->be;
1951 struct snd_soc_dai *dai = be->codec_dai;
1952 struct snd_soc_dai_driver *drv = dai->driver;
1953
1954 if (be->dai_link->ignore_suspend)
1955 continue;
1956
Liam Girdwood103d84a2012-11-19 14:39:15 +00001957 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001958
Mark Brownc5914b02013-10-30 17:47:39 -07001959 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1960 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01001961 }
1962
1963 return 0;
1964}
1965
Mark Brown45c0a182012-05-09 21:46:27 +01001966static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001967{
1968 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1969 struct snd_soc_dpcm *dpcm;
1970 struct snd_soc_dapm_widget_list *list;
1971 int ret;
1972 int stream = fe_substream->stream;
1973
1974 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1975 fe->dpcm[stream].runtime = fe_substream->runtime;
1976
1977 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001978 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001979 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001980 }
1981
1982 /* calculate valid and active FE <-> BE dpcms */
1983 dpcm_process_paths(fe, stream, &list, 1);
1984
1985 ret = dpcm_fe_dai_startup(fe_substream);
1986 if (ret < 0) {
1987 /* clean up all links */
1988 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1989 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1990
1991 dpcm_be_disconnect(fe, stream);
1992 fe->dpcm[stream].runtime = NULL;
1993 }
1994
1995 dpcm_clear_pending_state(fe, stream);
1996 dpcm_path_put(&list);
1997 mutex_unlock(&fe->card->mutex);
1998 return ret;
1999}
2000
Mark Brown45c0a182012-05-09 21:46:27 +01002001static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002002{
2003 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2004 struct snd_soc_dpcm *dpcm;
2005 int stream = fe_substream->stream, ret;
2006
2007 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2008 ret = dpcm_fe_dai_shutdown(fe_substream);
2009
2010 /* mark FE's links ready to prune */
2011 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2012 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2013
2014 dpcm_be_disconnect(fe, stream);
2015
2016 fe->dpcm[stream].runtime = NULL;
2017 mutex_unlock(&fe->card->mutex);
2018 return ret;
2019}
2020
Liam Girdwoodddee6272011-06-09 14:45:53 +01002021/* create a new pcm */
2022int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2023{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002024 struct snd_soc_platform *platform = rtd->platform;
2025 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2026 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2027 struct snd_pcm *pcm;
2028 char new_name[64];
2029 int ret = 0, playback = 0, capture = 0;
2030
Liam Girdwood01d75842012-04-25 12:12:49 +01002031 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002032 playback = rtd->dai_link->dpcm_playback;
2033 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002034 } else {
Mark Brown05679092013-06-01 23:13:53 +01002035 if (codec_dai->driver->playback.channels_min &&
2036 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002037 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002038 if (codec_dai->driver->capture.channels_min &&
2039 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002040 capture = 1;
2041 }
Sangsu Parka5002312012-01-02 17:15:10 +09002042
Fabio Estevamd6bead02013-08-29 10:32:13 -03002043 if (rtd->dai_link->playback_only) {
2044 playback = 1;
2045 capture = 0;
2046 }
2047
2048 if (rtd->dai_link->capture_only) {
2049 playback = 0;
2050 capture = 1;
2051 }
2052
Liam Girdwood01d75842012-04-25 12:12:49 +01002053 /* create the PCM */
2054 if (rtd->dai_link->no_pcm) {
2055 snprintf(new_name, sizeof(new_name), "(%s)",
2056 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002057
Liam Girdwood01d75842012-04-25 12:12:49 +01002058 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2059 playback, capture, &pcm);
2060 } else {
2061 if (rtd->dai_link->dynamic)
2062 snprintf(new_name, sizeof(new_name), "%s (*)",
2063 rtd->dai_link->stream_name);
2064 else
2065 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2066 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002067
Liam Girdwood01d75842012-04-25 12:12:49 +01002068 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2069 capture, &pcm);
2070 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002071 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002072 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002073 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002074 return ret;
2075 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002076 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002077
2078 /* DAPM dai link stream work */
2079 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2080
2081 rtd->pcm = pcm;
2082 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002083
2084 if (rtd->dai_link->no_pcm) {
2085 if (playback)
2086 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2087 if (capture)
2088 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2089 goto out;
2090 }
2091
2092 /* ASoC PCM operations */
2093 if (rtd->dai_link->dynamic) {
2094 rtd->ops.open = dpcm_fe_dai_open;
2095 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2096 rtd->ops.prepare = dpcm_fe_dai_prepare;
2097 rtd->ops.trigger = dpcm_fe_dai_trigger;
2098 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2099 rtd->ops.close = dpcm_fe_dai_close;
2100 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002101 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002102 } else {
2103 rtd->ops.open = soc_pcm_open;
2104 rtd->ops.hw_params = soc_pcm_hw_params;
2105 rtd->ops.prepare = soc_pcm_prepare;
2106 rtd->ops.trigger = soc_pcm_trigger;
2107 rtd->ops.hw_free = soc_pcm_hw_free;
2108 rtd->ops.close = soc_pcm_close;
2109 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002110 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002111 }
2112
Liam Girdwoodddee6272011-06-09 14:45:53 +01002113 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002114 rtd->ops.ack = platform->driver->ops->ack;
2115 rtd->ops.copy = platform->driver->ops->copy;
2116 rtd->ops.silence = platform->driver->ops->silence;
2117 rtd->ops.page = platform->driver->ops->page;
2118 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002119 }
2120
2121 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002122 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002123
2124 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002125 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002126
2127 if (platform->driver->pcm_new) {
2128 ret = platform->driver->pcm_new(rtd);
2129 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002130 dev_err(platform->dev,
2131 "ASoC: pcm constructor failed: %d\n",
2132 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002133 return ret;
2134 }
2135 }
2136
2137 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002138out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002139 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002140 cpu_dai->name);
2141 return ret;
2142}
Liam Girdwood01d75842012-04-25 12:12:49 +01002143
2144/* is the current PCM operation for this FE ? */
2145int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2146{
2147 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2148 return 1;
2149 return 0;
2150}
2151EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2152
2153/* is the current PCM operation for this BE ? */
2154int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2155 struct snd_soc_pcm_runtime *be, int stream)
2156{
2157 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2158 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2159 be->dpcm[stream].runtime_update))
2160 return 1;
2161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2164
2165/* get the substream for this BE */
2166struct snd_pcm_substream *
2167 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2168{
2169 return be->pcm->streams[stream].substream;
2170}
2171EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2172
2173/* get the BE runtime state */
2174enum snd_soc_dpcm_state
2175 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2176{
2177 return be->dpcm[stream].state;
2178}
2179EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2180
2181/* set the BE runtime state */
2182void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2183 int stream, enum snd_soc_dpcm_state state)
2184{
2185 be->dpcm[stream].state = state;
2186}
2187EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2188
2189/*
2190 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2191 * are not running, paused or suspended for the specified stream direction.
2192 */
2193int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2194 struct snd_soc_pcm_runtime *be, int stream)
2195{
2196 struct snd_soc_dpcm *dpcm;
2197 int state;
2198
2199 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2200
2201 if (dpcm->fe == fe)
2202 continue;
2203
2204 state = dpcm->fe->dpcm[stream].state;
2205 if (state == SND_SOC_DPCM_STATE_START ||
2206 state == SND_SOC_DPCM_STATE_PAUSED ||
2207 state == SND_SOC_DPCM_STATE_SUSPEND)
2208 return 0;
2209 }
2210
2211 /* it's safe to free/stop this BE DAI */
2212 return 1;
2213}
2214EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2215
2216/*
2217 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2218 * running, paused or suspended for the specified stream direction.
2219 */
2220int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2221 struct snd_soc_pcm_runtime *be, int stream)
2222{
2223 struct snd_soc_dpcm *dpcm;
2224 int state;
2225
2226 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2227
2228 if (dpcm->fe == fe)
2229 continue;
2230
2231 state = dpcm->fe->dpcm[stream].state;
2232 if (state == SND_SOC_DPCM_STATE_START ||
2233 state == SND_SOC_DPCM_STATE_PAUSED ||
2234 state == SND_SOC_DPCM_STATE_SUSPEND ||
2235 state == SND_SOC_DPCM_STATE_PREPARE)
2236 return 0;
2237 }
2238
2239 /* it's safe to change hw_params */
2240 return 1;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002243
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002244int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2245 int cmd, struct snd_soc_platform *platform)
2246{
Mark Brownc5914b02013-10-30 17:47:39 -07002247 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002248 return platform->driver->ops->trigger(substream, cmd);
2249 return 0;
2250}
2251EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2252
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002253#ifdef CONFIG_DEBUG_FS
2254static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2255{
2256 switch (state) {
2257 case SND_SOC_DPCM_STATE_NEW:
2258 return "new";
2259 case SND_SOC_DPCM_STATE_OPEN:
2260 return "open";
2261 case SND_SOC_DPCM_STATE_HW_PARAMS:
2262 return "hw_params";
2263 case SND_SOC_DPCM_STATE_PREPARE:
2264 return "prepare";
2265 case SND_SOC_DPCM_STATE_START:
2266 return "start";
2267 case SND_SOC_DPCM_STATE_STOP:
2268 return "stop";
2269 case SND_SOC_DPCM_STATE_SUSPEND:
2270 return "suspend";
2271 case SND_SOC_DPCM_STATE_PAUSED:
2272 return "paused";
2273 case SND_SOC_DPCM_STATE_HW_FREE:
2274 return "hw_free";
2275 case SND_SOC_DPCM_STATE_CLOSE:
2276 return "close";
2277 }
2278
2279 return "unknown";
2280}
2281
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002282static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2283 int stream, char *buf, size_t size)
2284{
2285 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2286 struct snd_soc_dpcm *dpcm;
2287 ssize_t offset = 0;
2288
2289 /* FE state */
2290 offset += snprintf(buf + offset, size - offset,
2291 "[%s - %s]\n", fe->dai_link->name,
2292 stream ? "Capture" : "Playback");
2293
2294 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2295 dpcm_state_string(fe->dpcm[stream].state));
2296
2297 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2298 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2299 offset += snprintf(buf + offset, size - offset,
2300 "Hardware Params: "
2301 "Format = %s, Channels = %d, Rate = %d\n",
2302 snd_pcm_format_name(params_format(params)),
2303 params_channels(params),
2304 params_rate(params));
2305
2306 /* BEs state */
2307 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2308
2309 if (list_empty(&fe->dpcm[stream].be_clients)) {
2310 offset += snprintf(buf + offset, size - offset,
2311 " No active DSP links\n");
2312 goto out;
2313 }
2314
2315 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2316 struct snd_soc_pcm_runtime *be = dpcm->be;
2317 params = &dpcm->hw_params;
2318
2319 offset += snprintf(buf + offset, size - offset,
2320 "- %s\n", be->dai_link->name);
2321
2322 offset += snprintf(buf + offset, size - offset,
2323 " State: %s\n",
2324 dpcm_state_string(be->dpcm[stream].state));
2325
2326 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2327 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2328 offset += snprintf(buf + offset, size - offset,
2329 " Hardware Params: "
2330 "Format = %s, Channels = %d, Rate = %d\n",
2331 snd_pcm_format_name(params_format(params)),
2332 params_channels(params),
2333 params_rate(params));
2334 }
2335
2336out:
2337 return offset;
2338}
2339
2340static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2341 size_t count, loff_t *ppos)
2342{
2343 struct snd_soc_pcm_runtime *fe = file->private_data;
2344 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2345 char *buf;
2346
2347 buf = kmalloc(out_count, GFP_KERNEL);
2348 if (!buf)
2349 return -ENOMEM;
2350
2351 if (fe->cpu_dai->driver->playback.channels_min)
2352 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2353 buf + offset, out_count - offset);
2354
2355 if (fe->cpu_dai->driver->capture.channels_min)
2356 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2357 buf + offset, out_count - offset);
2358
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002359 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002360
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002361 kfree(buf);
2362 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002363}
2364
2365static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002366 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002367 .read = dpcm_state_read_file,
2368 .llseek = default_llseek,
2369};
2370
2371int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2372{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002373 if (!rtd->dai_link)
2374 return 0;
2375
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002376 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2377 rtd->card->debugfs_card_root);
2378 if (!rtd->debugfs_dpcm_root) {
2379 dev_dbg(rtd->dev,
2380 "ASoC: Failed to create dpcm debugfs directory %s\n",
2381 rtd->dai_link->name);
2382 return -EINVAL;
2383 }
2384
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002385 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002386 rtd->debugfs_dpcm_root,
2387 rtd, &dpcm_state_fops);
2388
2389 return 0;
2390}
2391#endif