blob: 141a302e4e77a5b382ec589a690e625857037052 [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);
161 hw->formats = codec_stream->formats & cpu_stream->formats;
162 hw->rates = codec_stream->rates & cpu_stream->rates;
163 if (codec_stream->rates
164 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
165 hw->rates |= cpu_stream->rates;
166 if (cpu_stream->rates
167 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
168 hw->rates |= codec_stream->rates;
169}
170
Mark Brown58ba9b22012-01-16 18:38:51 +0000171/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100172 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
173 * then initialized and any private data can be allocated. This also calls
174 * startup for the cpu DAI, platform, machine and codec DAI.
175 */
176static int soc_pcm_open(struct snd_pcm_substream *substream)
177{
178 struct snd_soc_pcm_runtime *rtd = substream->private_data;
179 struct snd_pcm_runtime *runtime = substream->runtime;
180 struct snd_soc_platform *platform = rtd->platform;
181 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
182 struct snd_soc_dai *codec_dai = rtd->codec_dai;
183 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
184 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
185 int ret = 0;
186
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800187 pinctrl_pm_select_default_state(cpu_dai->dev);
188 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000189 pm_runtime_get_sync(cpu_dai->dev);
190 pm_runtime_get_sync(codec_dai->dev);
191 pm_runtime_get_sync(platform->dev);
192
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100193 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194
195 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700196 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100197 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
198 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000199 dev_err(cpu_dai->dev, "ASoC: can't open interface"
200 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100201 goto out;
202 }
203 }
204
205 if (platform->driver->ops && platform->driver->ops->open) {
206 ret = platform->driver->ops->open(substream);
207 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000208 dev_err(platform->dev, "ASoC: can't open platform"
209 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100210 goto platform_err;
211 }
212 }
213
Mark Brownc5914b02013-10-30 17:47:39 -0700214 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100215 ret = codec_dai->driver->ops->startup(substream, codec_dai);
216 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000217 dev_err(codec_dai->dev, "ASoC: can't open codec"
218 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100219 goto codec_dai_err;
220 }
221 }
222
223 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
224 ret = rtd->dai_link->ops->startup(substream);
225 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000226 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000227 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100228 goto machine_err;
229 }
230 }
231
Liam Girdwood01d75842012-04-25 12:12:49 +0100232 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
233 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
234 goto dynamic;
235
Liam Girdwoodddee6272011-06-09 14:45:53 +0100236 /* Check that the codec and cpu DAIs are compatible */
237 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200238 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
239 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100240 } else {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200241 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
242 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100243 }
244
245 ret = -EINVAL;
246 snd_pcm_limit_hw_rates(runtime);
247 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000248 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100249 codec_dai->name, cpu_dai->name);
250 goto config_err;
251 }
252 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000253 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100254 codec_dai->name, cpu_dai->name);
255 goto config_err;
256 }
257 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
258 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000259 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100260 codec_dai->name, cpu_dai->name);
261 goto config_err;
262 }
263
Mark Brown58ba9b22012-01-16 18:38:51 +0000264 soc_pcm_apply_msb(substream, codec_dai);
265 soc_pcm_apply_msb(substream, cpu_dai);
266
Liam Girdwoodddee6272011-06-09 14:45:53 +0100267 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800268 if (cpu_dai->active) {
269 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
270 if (ret != 0)
271 goto config_err;
272 }
273
274 if (codec_dai->active) {
275 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100276 if (ret != 0)
277 goto config_err;
278 }
279
Liam Girdwood103d84a2012-11-19 14:39:15 +0000280 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100281 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000282 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
283 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100284 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000285 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100286 runtime->hw.rate_max);
287
Liam Girdwood01d75842012-04-25 12:12:49 +0100288dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100289 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
290 cpu_dai->playback_active++;
291 codec_dai->playback_active++;
292 } else {
293 cpu_dai->capture_active++;
294 codec_dai->capture_active++;
295 }
296 cpu_dai->active++;
297 codec_dai->active++;
298 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100299 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100300 return 0;
301
302config_err:
303 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
304 rtd->dai_link->ops->shutdown(substream);
305
306machine_err:
307 if (codec_dai->driver->ops->shutdown)
308 codec_dai->driver->ops->shutdown(substream, codec_dai);
309
310codec_dai_err:
311 if (platform->driver->ops && platform->driver->ops->close)
312 platform->driver->ops->close(substream);
313
314platform_err:
315 if (cpu_dai->driver->ops->shutdown)
316 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
317out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100318 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000319
320 pm_runtime_put(platform->dev);
321 pm_runtime_put(codec_dai->dev);
322 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800323 if (!codec_dai->active)
324 pinctrl_pm_select_sleep_state(codec_dai->dev);
325 if (!cpu_dai->active)
326 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000327
Liam Girdwoodddee6272011-06-09 14:45:53 +0100328 return ret;
329}
330
331/*
332 * Power down the audio subsystem pmdown_time msecs after close is called.
333 * This is to ensure there are no pops or clicks in between any music tracks
334 * due to DAPM power cycling.
335 */
336static void close_delayed_work(struct work_struct *work)
337{
338 struct snd_soc_pcm_runtime *rtd =
339 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
340 struct snd_soc_dai *codec_dai = rtd->codec_dai;
341
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100342 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100343
Liam Girdwood103d84a2012-11-19 14:39:15 +0000344 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100345 codec_dai->driver->playback.stream_name,
346 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600347 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100348
349 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600350 if (rtd->pop_wait == 1) {
351 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800352 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000353 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100354 }
355
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100356 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100357}
358
359/*
360 * Called by ALSA when a PCM substream is closed. Private data can be
361 * freed here. The cpu DAI, codec DAI, machine and platform are also
362 * shutdown.
363 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100364static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100365{
366 struct snd_soc_pcm_runtime *rtd = substream->private_data;
367 struct snd_soc_platform *platform = rtd->platform;
368 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
369 struct snd_soc_dai *codec_dai = rtd->codec_dai;
370 struct snd_soc_codec *codec = rtd->codec;
371
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100372 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100373
374 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 cpu_dai->playback_active--;
376 codec_dai->playback_active--;
377 } else {
378 cpu_dai->capture_active--;
379 codec_dai->capture_active--;
380 }
381
382 cpu_dai->active--;
383 codec_dai->active--;
384 codec->active--;
385
Dong Aisheng17841022011-08-29 17:15:14 +0800386 /* clear the corresponding DAIs rate when inactive */
387 if (!cpu_dai->active)
388 cpu_dai->rate = 0;
389
390 if (!codec_dai->active)
391 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200392
Liam Girdwoodddee6272011-06-09 14:45:53 +0100393 /* Muting the DAC suppresses artifacts caused during digital
394 * shutdown, for example from stopping clocks.
395 */
Mark Brownda183962013-02-06 15:44:07 +0000396 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100397
398 if (cpu_dai->driver->ops->shutdown)
399 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
400
401 if (codec_dai->driver->ops->shutdown)
402 codec_dai->driver->ops->shutdown(substream, codec_dai);
403
404 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
405 rtd->dai_link->ops->shutdown(substream);
406
407 if (platform->driver->ops && platform->driver->ops->close)
408 platform->driver->ops->close(substream);
409 cpu_dai->runtime = NULL;
410
411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000412 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200413 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300414 /* powered down playback stream now */
415 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800416 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800417 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300418 } else {
419 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600420 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100421 queue_delayed_work(system_power_efficient_wq,
422 &rtd->delayed_work,
423 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300424 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100425 } else {
426 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800427 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000428 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100429 }
430
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100431 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000432
433 pm_runtime_put(platform->dev);
434 pm_runtime_put(codec_dai->dev);
435 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800436 if (!codec_dai->active)
437 pinctrl_pm_select_sleep_state(codec_dai->dev);
438 if (!cpu_dai->active)
439 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000440
Liam Girdwoodddee6272011-06-09 14:45:53 +0100441 return 0;
442}
443
444/*
445 * Called by ALSA when the PCM substream is prepared, can set format, sample
446 * rate, etc. This function is non atomic and can be called multiple times,
447 * it can refer to the runtime info.
448 */
449static int soc_pcm_prepare(struct snd_pcm_substream *substream)
450{
451 struct snd_soc_pcm_runtime *rtd = substream->private_data;
452 struct snd_soc_platform *platform = rtd->platform;
453 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454 struct snd_soc_dai *codec_dai = rtd->codec_dai;
455 int ret = 0;
456
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100457 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100458
459 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
460 ret = rtd->dai_link->ops->prepare(substream);
461 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000462 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
463 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100464 goto out;
465 }
466 }
467
468 if (platform->driver->ops && platform->driver->ops->prepare) {
469 ret = platform->driver->ops->prepare(substream);
470 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000471 dev_err(platform->dev, "ASoC: platform prepare error:"
472 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100473 goto out;
474 }
475 }
476
Mark Brownc5914b02013-10-30 17:47:39 -0700477 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100478 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
479 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000480 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000481 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100482 goto out;
483 }
484 }
485
Mark Brownc5914b02013-10-30 17:47:39 -0700486 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
488 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000489 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000490 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100491 goto out;
492 }
493 }
494
495 /* cancel any delayed stream shutdown that is pending */
496 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600497 rtd->pop_wait) {
498 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100499 cancel_delayed_work(&rtd->delayed_work);
500 }
501
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000502 snd_soc_dapm_stream_event(rtd, substream->stream,
503 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100504
Mark Brownda183962013-02-06 15:44:07 +0000505 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100506
507out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100508 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100509 return ret;
510}
511
512/*
513 * Called by ALSA when the hardware params are set by application. This
514 * function can also be called multiple times and can allocate buffers
515 * (using snd_pcm_lib_* ). It's non-atomic.
516 */
517static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
518 struct snd_pcm_hw_params *params)
519{
520 struct snd_soc_pcm_runtime *rtd = substream->private_data;
521 struct snd_soc_platform *platform = rtd->platform;
522 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
523 struct snd_soc_dai *codec_dai = rtd->codec_dai;
524 int ret = 0;
525
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100526 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100527
528 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
529 ret = rtd->dai_link->ops->hw_params(substream, params);
530 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000531 dev_err(rtd->card->dev, "ASoC: machine hw_params"
532 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100533 goto out;
534 }
535 }
536
Mark Brownc5914b02013-10-30 17:47:39 -0700537 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
539 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000540 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
541 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100542 goto codec_err;
543 }
544 }
545
Mark Brownc5914b02013-10-30 17:47:39 -0700546 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
548 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000549 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000550 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 goto interface_err;
552 }
553 }
554
555 if (platform->driver->ops && platform->driver->ops->hw_params) {
556 ret = platform->driver->ops->hw_params(substream, params);
557 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000558 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000559 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 goto platform_err;
561 }
562 }
563
Dong Aisheng17841022011-08-29 17:15:14 +0800564 /* store the rate for each DAIs */
565 cpu_dai->rate = params_rate(params);
566 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100567
568out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100569 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100570 return ret;
571
572platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700573 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100574 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
575
576interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700577 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100578 codec_dai->driver->ops->hw_free(substream, codec_dai);
579
580codec_err:
581 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
582 rtd->dai_link->ops->hw_free(substream);
583
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100584 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100585 return ret;
586}
587
588/*
589 * Frees resources allocated by hw_params, can be called multiple times
590 */
591static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
592{
593 struct snd_soc_pcm_runtime *rtd = substream->private_data;
594 struct snd_soc_platform *platform = rtd->platform;
595 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
596 struct snd_soc_dai *codec_dai = rtd->codec_dai;
597 struct snd_soc_codec *codec = rtd->codec;
598
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100599 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100600
601 /* apply codec digital mute */
602 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000603 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100604
605 /* free any machine hw params */
606 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
607 rtd->dai_link->ops->hw_free(substream);
608
609 /* free any DMA resources */
610 if (platform->driver->ops && platform->driver->ops->hw_free)
611 platform->driver->ops->hw_free(substream);
612
613 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700614 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100615 codec_dai->driver->ops->hw_free(substream, codec_dai);
616
Mark Brownc5914b02013-10-30 17:47:39 -0700617 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
619
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100620 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100621 return 0;
622}
623
624static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
625{
626 struct snd_soc_pcm_runtime *rtd = substream->private_data;
627 struct snd_soc_platform *platform = rtd->platform;
628 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
629 struct snd_soc_dai *codec_dai = rtd->codec_dai;
630 int ret;
631
Mark Brownc5914b02013-10-30 17:47:39 -0700632 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100633 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
634 if (ret < 0)
635 return ret;
636 }
637
638 if (platform->driver->ops && platform->driver->ops->trigger) {
639 ret = platform->driver->ops->trigger(substream, cmd);
640 if (ret < 0)
641 return ret;
642 }
643
Mark Brownc5914b02013-10-30 17:47:39 -0700644 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100645 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
646 if (ret < 0)
647 return ret;
648 }
649 return 0;
650}
651
Mark Brown45c0a182012-05-09 21:46:27 +0100652static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
653 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100654{
655 struct snd_soc_pcm_runtime *rtd = substream->private_data;
656 struct snd_soc_platform *platform = rtd->platform;
657 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
658 struct snd_soc_dai *codec_dai = rtd->codec_dai;
659 int ret;
660
Mark Brownc5914b02013-10-30 17:47:39 -0700661 if (codec_dai->driver->ops &&
662 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100663 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
664 if (ret < 0)
665 return ret;
666 }
667
Mark Brownc5914b02013-10-30 17:47:39 -0700668 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100669 ret = platform->driver->bespoke_trigger(substream, cmd);
670 if (ret < 0)
671 return ret;
672 }
673
Mark Brownc5914b02013-10-30 17:47:39 -0700674 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100675 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
676 if (ret < 0)
677 return ret;
678 }
679 return 0;
680}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100681/*
682 * soc level wrapper for pointer callback
683 * If cpu_dai, codec_dai, platform driver has the delay callback, than
684 * the runtime->delay will be updated accordingly.
685 */
686static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
687{
688 struct snd_soc_pcm_runtime *rtd = substream->private_data;
689 struct snd_soc_platform *platform = rtd->platform;
690 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
691 struct snd_soc_dai *codec_dai = rtd->codec_dai;
692 struct snd_pcm_runtime *runtime = substream->runtime;
693 snd_pcm_uframes_t offset = 0;
694 snd_pcm_sframes_t delay = 0;
695
696 if (platform->driver->ops && platform->driver->ops->pointer)
697 offset = platform->driver->ops->pointer(substream);
698
Mark Brownc5914b02013-10-30 17:47:39 -0700699 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100700 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
701
Mark Brownc5914b02013-10-30 17:47:39 -0700702 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703 delay += codec_dai->driver->ops->delay(substream, codec_dai);
704
705 if (platform->driver->delay)
706 delay += platform->driver->delay(substream, codec_dai);
707
708 runtime->delay = delay;
709
710 return offset;
711}
712
Liam Girdwood01d75842012-04-25 12:12:49 +0100713/* connect a FE and BE */
714static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
715 struct snd_soc_pcm_runtime *be, int stream)
716{
717 struct snd_soc_dpcm *dpcm;
718
719 /* only add new dpcms */
720 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
721 if (dpcm->be == be && dpcm->fe == fe)
722 return 0;
723 }
724
725 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
726 if (!dpcm)
727 return -ENOMEM;
728
729 dpcm->be = be;
730 dpcm->fe = fe;
731 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
732 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
733 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
734 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
735
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300736 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100737 stream ? "capture" : "playback", fe->dai_link->name,
738 stream ? "<-" : "->", be->dai_link->name);
739
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100740#ifdef CONFIG_DEBUG_FS
741 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
742 fe->debugfs_dpcm_root, &dpcm->state);
743#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100744 return 1;
745}
746
747/* reparent a BE onto another FE */
748static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
749 struct snd_soc_pcm_runtime *be, int stream)
750{
751 struct snd_soc_dpcm *dpcm;
752 struct snd_pcm_substream *fe_substream, *be_substream;
753
754 /* reparent if BE is connected to other FEs */
755 if (!be->dpcm[stream].users)
756 return;
757
758 be_substream = snd_soc_dpcm_get_substream(be, stream);
759
760 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
761 if (dpcm->fe == fe)
762 continue;
763
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300764 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100765 stream ? "capture" : "playback",
766 dpcm->fe->dai_link->name,
767 stream ? "<-" : "->", dpcm->be->dai_link->name);
768
769 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
770 be_substream->runtime = fe_substream->runtime;
771 break;
772 }
773}
774
775/* disconnect a BE and FE */
776static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
777{
778 struct snd_soc_dpcm *dpcm, *d;
779
780 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000781 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100782 stream ? "capture" : "playback",
783 dpcm->be->dai_link->name);
784
785 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
786 continue;
787
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300788 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100789 stream ? "capture" : "playback", fe->dai_link->name,
790 stream ? "<-" : "->", dpcm->be->dai_link->name);
791
792 /* BEs still alive need new FE */
793 dpcm_be_reparent(fe, dpcm->be, stream);
794
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100795#ifdef CONFIG_DEBUG_FS
796 debugfs_remove(dpcm->debugfs_state);
797#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100798 list_del(&dpcm->list_be);
799 list_del(&dpcm->list_fe);
800 kfree(dpcm);
801 }
802}
803
804/* get BE for DAI widget and stream */
805static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
806 struct snd_soc_dapm_widget *widget, int stream)
807{
808 struct snd_soc_pcm_runtime *be;
809 int i;
810
811 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
812 for (i = 0; i < card->num_links; i++) {
813 be = &card->rtd[i];
814
Liam Girdwood35ea0652012-06-05 19:26:59 +0100815 if (!be->dai_link->no_pcm)
816 continue;
817
Liam Girdwood01d75842012-04-25 12:12:49 +0100818 if (be->cpu_dai->playback_widget == widget ||
819 be->codec_dai->playback_widget == widget)
820 return be;
821 }
822 } else {
823
824 for (i = 0; i < card->num_links; i++) {
825 be = &card->rtd[i];
826
Liam Girdwood35ea0652012-06-05 19:26:59 +0100827 if (!be->dai_link->no_pcm)
828 continue;
829
Liam Girdwood01d75842012-04-25 12:12:49 +0100830 if (be->cpu_dai->capture_widget == widget ||
831 be->codec_dai->capture_widget == widget)
832 return be;
833 }
834 }
835
Liam Girdwood103d84a2012-11-19 14:39:15 +0000836 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100837 stream ? "capture" : "playback", widget->name);
838 return NULL;
839}
840
841static inline struct snd_soc_dapm_widget *
842 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
843{
844 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
845 return rtd->cpu_dai->playback_widget;
846 else
847 return rtd->cpu_dai->capture_widget;
848}
849
850static inline struct snd_soc_dapm_widget *
851 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
852{
853 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
854 return rtd->codec_dai->playback_widget;
855 else
856 return rtd->codec_dai->capture_widget;
857}
858
859static int widget_in_list(struct snd_soc_dapm_widget_list *list,
860 struct snd_soc_dapm_widget *widget)
861{
862 int i;
863
864 for (i = 0; i < list->num_widgets; i++) {
865 if (widget == list->widgets[i])
866 return 1;
867 }
868
869 return 0;
870}
871
872static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
873 int stream, struct snd_soc_dapm_widget_list **list_)
874{
875 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
876 struct snd_soc_dapm_widget_list *list;
877 int paths;
878
879 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
880 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
881 if (list == NULL)
882 return -ENOMEM;
883
884 /* get number of valid DAI paths and their widgets */
885 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
886
Liam Girdwood103d84a2012-11-19 14:39:15 +0000887 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100888 stream ? "capture" : "playback");
889
890 *list_ = list;
891 return paths;
892}
893
894static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
895{
896 kfree(*list);
897}
898
899static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
900 struct snd_soc_dapm_widget_list **list_)
901{
902 struct snd_soc_dpcm *dpcm;
903 struct snd_soc_dapm_widget_list *list = *list_;
904 struct snd_soc_dapm_widget *widget;
905 int prune = 0;
906
907 /* Destroy any old FE <--> BE connections */
908 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
909
910 /* is there a valid CPU DAI widget for this BE */
911 widget = rtd_get_cpu_widget(dpcm->be, stream);
912
913 /* prune the BE if it's no longer in our active list */
914 if (widget && widget_in_list(list, widget))
915 continue;
916
917 /* is there a valid CODEC DAI widget for this BE */
918 widget = rtd_get_codec_widget(dpcm->be, stream);
919
920 /* prune the BE if it's no longer in our active list */
921 if (widget && widget_in_list(list, widget))
922 continue;
923
Liam Girdwood103d84a2012-11-19 14:39:15 +0000924 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100925 stream ? "capture" : "playback",
926 dpcm->be->dai_link->name, fe->dai_link->name);
927 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
928 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
929 prune++;
930 }
931
Liam Girdwood103d84a2012-11-19 14:39:15 +0000932 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100933 return prune;
934}
935
936static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
937 struct snd_soc_dapm_widget_list **list_)
938{
939 struct snd_soc_card *card = fe->card;
940 struct snd_soc_dapm_widget_list *list = *list_;
941 struct snd_soc_pcm_runtime *be;
942 int i, new = 0, err;
943
944 /* Create any new FE <--> BE connections */
945 for (i = 0; i < list->num_widgets; i++) {
946
Mark Brown46162742013-06-05 19:36:11 +0100947 switch (list->widgets[i]->id) {
948 case snd_soc_dapm_dai_in:
949 case snd_soc_dapm_dai_out:
950 break;
951 default:
Liam Girdwood01d75842012-04-25 12:12:49 +0100952 continue;
Mark Brown46162742013-06-05 19:36:11 +0100953 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100954
955 /* is there a valid BE rtd for this widget */
956 be = dpcm_get_be(card, list->widgets[i], stream);
957 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000958 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100959 list->widgets[i]->name);
960 continue;
961 }
962
963 /* make sure BE is a real BE */
964 if (!be->dai_link->no_pcm)
965 continue;
966
967 /* don't connect if FE is not running */
968 if (!fe->dpcm[stream].runtime)
969 continue;
970
971 /* newly connected FE and BE */
972 err = dpcm_be_connect(fe, be, stream);
973 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000974 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100975 list->widgets[i]->name);
976 break;
977 } else if (err == 0) /* already connected */
978 continue;
979
980 /* new */
981 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
982 new++;
983 }
984
Liam Girdwood103d84a2012-11-19 14:39:15 +0000985 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100986 return new;
987}
988
989/*
990 * Find the corresponding BE DAIs that source or sink audio to this
991 * FE substream.
992 */
993static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
994 int stream, struct snd_soc_dapm_widget_list **list, int new)
995{
996 if (new)
997 return dpcm_add_paths(fe, stream, list);
998 else
999 return dpcm_prune_paths(fe, stream, list);
1000}
1001
1002static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1003{
1004 struct snd_soc_dpcm *dpcm;
1005
1006 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1007 dpcm->be->dpcm[stream].runtime_update =
1008 SND_SOC_DPCM_UPDATE_NO;
1009}
1010
1011static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1012 int stream)
1013{
1014 struct snd_soc_dpcm *dpcm;
1015
1016 /* disable any enabled and non active backends */
1017 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1018
1019 struct snd_soc_pcm_runtime *be = dpcm->be;
1020 struct snd_pcm_substream *be_substream =
1021 snd_soc_dpcm_get_substream(be, stream);
1022
1023 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001024 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001025 stream ? "capture" : "playback",
1026 be->dpcm[stream].state);
1027
1028 if (--be->dpcm[stream].users != 0)
1029 continue;
1030
1031 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1032 continue;
1033
1034 soc_pcm_close(be_substream);
1035 be_substream->runtime = NULL;
1036 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1037 }
1038}
1039
1040static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1041{
1042 struct snd_soc_dpcm *dpcm;
1043 int err, count = 0;
1044
1045 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1046 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1047
1048 struct snd_soc_pcm_runtime *be = dpcm->be;
1049 struct snd_pcm_substream *be_substream =
1050 snd_soc_dpcm_get_substream(be, stream);
1051
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001052 if (!be_substream) {
1053 dev_err(be->dev, "ASoC: no backend %s stream\n",
1054 stream ? "capture" : "playback");
1055 continue;
1056 }
1057
Liam Girdwood01d75842012-04-25 12:12:49 +01001058 /* is this op for this BE ? */
1059 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1060 continue;
1061
1062 /* first time the dpcm is open ? */
1063 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001064 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001065 stream ? "capture" : "playback",
1066 be->dpcm[stream].state);
1067
1068 if (be->dpcm[stream].users++ != 0)
1069 continue;
1070
1071 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1072 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1073 continue;
1074
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001075 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1076 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001077
1078 be_substream->runtime = be->dpcm[stream].runtime;
1079 err = soc_pcm_open(be_substream);
1080 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001081 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001082 be->dpcm[stream].users--;
1083 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001084 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001085 stream ? "capture" : "playback",
1086 be->dpcm[stream].state);
1087
1088 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1089 goto unwind;
1090 }
1091
1092 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1093 count++;
1094 }
1095
1096 return count;
1097
1098unwind:
1099 /* disable any enabled and non active backends */
1100 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1101 struct snd_soc_pcm_runtime *be = dpcm->be;
1102 struct snd_pcm_substream *be_substream =
1103 snd_soc_dpcm_get_substream(be, stream);
1104
1105 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1106 continue;
1107
1108 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001109 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001110 stream ? "capture" : "playback",
1111 be->dpcm[stream].state);
1112
1113 if (--be->dpcm[stream].users != 0)
1114 continue;
1115
1116 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1117 continue;
1118
1119 soc_pcm_close(be_substream);
1120 be_substream->runtime = NULL;
1121 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1122 }
1123
1124 return err;
1125}
1126
Mark Brown45c0a182012-05-09 21:46:27 +01001127static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001128{
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1132 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1133
1134 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1135 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1136 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1137 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1138 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1139 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1140 runtime->hw.rates = cpu_dai_drv->playback.rates;
1141 } else {
1142 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1143 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1144 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1145 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1146 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1147 runtime->hw.rates = cpu_dai_drv->capture.rates;
1148 }
1149}
1150
1151static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1152{
1153 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1154 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1155 int stream = fe_substream->stream, ret = 0;
1156
1157 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1158
1159 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1160 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001161 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001162 goto be_err;
1163 }
1164
Liam Girdwood103d84a2012-11-19 14:39:15 +00001165 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001166
1167 /* start the DAI frontend */
1168 ret = soc_pcm_open(fe_substream);
1169 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001170 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001171 goto unwind;
1172 }
1173
1174 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1175
1176 dpcm_set_fe_runtime(fe_substream);
1177 snd_pcm_limit_hw_rates(runtime);
1178
1179 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1180 return 0;
1181
1182unwind:
1183 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1184be_err:
1185 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1186 return ret;
1187}
1188
1189static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1190{
1191 struct snd_soc_dpcm *dpcm;
1192
1193 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1194 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1195
1196 struct snd_soc_pcm_runtime *be = dpcm->be;
1197 struct snd_pcm_substream *be_substream =
1198 snd_soc_dpcm_get_substream(be, stream);
1199
1200 /* is this op for this BE ? */
1201 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1202 continue;
1203
1204 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001205 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001206 stream ? "capture" : "playback",
1207 be->dpcm[stream].state);
1208
1209 if (--be->dpcm[stream].users != 0)
1210 continue;
1211
1212 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1213 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1214 continue;
1215
Liam Girdwood103d84a2012-11-19 14:39:15 +00001216 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001217 dpcm->fe->dai_link->name);
1218
1219 soc_pcm_close(be_substream);
1220 be_substream->runtime = NULL;
1221
1222 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1223 }
1224 return 0;
1225}
1226
1227static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1228{
1229 struct snd_soc_pcm_runtime *fe = substream->private_data;
1230 int stream = substream->stream;
1231
1232 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1233
1234 /* shutdown the BEs */
1235 dpcm_be_dai_shutdown(fe, substream->stream);
1236
Liam Girdwood103d84a2012-11-19 14:39:15 +00001237 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001238
1239 /* now shutdown the frontend */
1240 soc_pcm_close(substream);
1241
1242 /* run the stream event for each BE */
1243 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1244
1245 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1246 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1247 return 0;
1248}
1249
1250static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1251{
1252 struct snd_soc_dpcm *dpcm;
1253
1254 /* only hw_params backends that are either sinks or sources
1255 * to this frontend DAI */
1256 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1257
1258 struct snd_soc_pcm_runtime *be = dpcm->be;
1259 struct snd_pcm_substream *be_substream =
1260 snd_soc_dpcm_get_substream(be, stream);
1261
1262 /* is this op for this BE ? */
1263 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1264 continue;
1265
1266 /* only free hw when no longer used - check all FEs */
1267 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1268 continue;
1269
1270 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1271 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1272 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001273 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001274 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1275 continue;
1276
Liam Girdwood103d84a2012-11-19 14:39:15 +00001277 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001278 dpcm->fe->dai_link->name);
1279
1280 soc_pcm_hw_free(be_substream);
1281
1282 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1283 }
1284
1285 return 0;
1286}
1287
Mark Brown45c0a182012-05-09 21:46:27 +01001288static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001289{
1290 struct snd_soc_pcm_runtime *fe = substream->private_data;
1291 int err, stream = substream->stream;
1292
1293 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1294 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1295
Liam Girdwood103d84a2012-11-19 14:39:15 +00001296 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001297
1298 /* call hw_free on the frontend */
1299 err = soc_pcm_hw_free(substream);
1300 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001301 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001302 fe->dai_link->name);
1303
1304 /* only hw_params backends that are either sinks or sources
1305 * to this frontend DAI */
1306 err = dpcm_be_dai_hw_free(fe, stream);
1307
1308 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1309 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1310
1311 mutex_unlock(&fe->card->mutex);
1312 return 0;
1313}
1314
1315static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1316{
1317 struct snd_soc_dpcm *dpcm;
1318 int ret;
1319
1320 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1321
1322 struct snd_soc_pcm_runtime *be = dpcm->be;
1323 struct snd_pcm_substream *be_substream =
1324 snd_soc_dpcm_get_substream(be, stream);
1325
1326 /* is this op for this BE ? */
1327 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1328 continue;
1329
1330 /* only allow hw_params() if no connected FEs are running */
1331 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1332 continue;
1333
1334 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1335 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1336 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1337 continue;
1338
Liam Girdwood103d84a2012-11-19 14:39:15 +00001339 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001340 dpcm->fe->dai_link->name);
1341
1342 /* copy params for each dpcm */
1343 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1344 sizeof(struct snd_pcm_hw_params));
1345
1346 /* perform any hw_params fixups */
1347 if (be->dai_link->be_hw_params_fixup) {
1348 ret = be->dai_link->be_hw_params_fixup(be,
1349 &dpcm->hw_params);
1350 if (ret < 0) {
1351 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001352 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001353 ret);
1354 goto unwind;
1355 }
1356 }
1357
1358 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1359 if (ret < 0) {
1360 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001361 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001362 goto unwind;
1363 }
1364
1365 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1366 }
1367 return 0;
1368
1369unwind:
1370 /* disable any enabled and non active backends */
1371 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1372 struct snd_soc_pcm_runtime *be = dpcm->be;
1373 struct snd_pcm_substream *be_substream =
1374 snd_soc_dpcm_get_substream(be, stream);
1375
1376 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1377 continue;
1378
1379 /* only allow hw_free() if no connected FEs are running */
1380 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1381 continue;
1382
1383 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1384 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1385 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1386 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1387 continue;
1388
1389 soc_pcm_hw_free(be_substream);
1390 }
1391
1392 return ret;
1393}
1394
Mark Brown45c0a182012-05-09 21:46:27 +01001395static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1396 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001397{
1398 struct snd_soc_pcm_runtime *fe = substream->private_data;
1399 int ret, stream = substream->stream;
1400
1401 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1402 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1403
1404 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1405 sizeof(struct snd_pcm_hw_params));
1406 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1407 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001408 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001409 goto out;
1410 }
1411
Liam Girdwood103d84a2012-11-19 14:39:15 +00001412 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001413 fe->dai_link->name, params_rate(params),
1414 params_channels(params), params_format(params));
1415
1416 /* call hw_params on the frontend */
1417 ret = soc_pcm_hw_params(substream, params);
1418 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001419 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001420 dpcm_be_dai_hw_free(fe, stream);
1421 } else
1422 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1423
1424out:
1425 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1426 mutex_unlock(&fe->card->mutex);
1427 return ret;
1428}
1429
1430static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1431 struct snd_pcm_substream *substream, int cmd)
1432{
1433 int ret;
1434
Liam Girdwood103d84a2012-11-19 14:39:15 +00001435 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001436 dpcm->fe->dai_link->name, cmd);
1437
1438 ret = soc_pcm_trigger(substream, cmd);
1439 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001440 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001441
1442 return ret;
1443}
1444
Mark Brown45c0a182012-05-09 21:46:27 +01001445static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1446 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001447{
1448 struct snd_soc_dpcm *dpcm;
1449 int ret = 0;
1450
1451 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1452
1453 struct snd_soc_pcm_runtime *be = dpcm->be;
1454 struct snd_pcm_substream *be_substream =
1455 snd_soc_dpcm_get_substream(be, stream);
1456
1457 /* is this op for this BE ? */
1458 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1459 continue;
1460
1461 switch (cmd) {
1462 case SNDRV_PCM_TRIGGER_START:
1463 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1464 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1465 continue;
1466
1467 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1468 if (ret)
1469 return ret;
1470
1471 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1472 break;
1473 case SNDRV_PCM_TRIGGER_RESUME:
1474 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1475 continue;
1476
1477 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1478 if (ret)
1479 return ret;
1480
1481 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1482 break;
1483 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1484 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1485 continue;
1486
1487 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1488 if (ret)
1489 return ret;
1490
1491 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1492 break;
1493 case SNDRV_PCM_TRIGGER_STOP:
1494 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1495 continue;
1496
1497 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1498 continue;
1499
1500 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1501 if (ret)
1502 return ret;
1503
1504 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1505 break;
1506 case SNDRV_PCM_TRIGGER_SUSPEND:
1507 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1508 continue;
1509
1510 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1511 continue;
1512
1513 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1514 if (ret)
1515 return ret;
1516
1517 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1518 break;
1519 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1520 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1521 continue;
1522
1523 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1524 continue;
1525
1526 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1527 if (ret)
1528 return ret;
1529
1530 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1531 break;
1532 }
1533 }
1534
1535 return ret;
1536}
1537EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1538
Mark Brown45c0a182012-05-09 21:46:27 +01001539static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001540{
1541 struct snd_soc_pcm_runtime *fe = substream->private_data;
1542 int stream = substream->stream, ret;
1543 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1544
1545 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1546
1547 switch (trigger) {
1548 case SND_SOC_DPCM_TRIGGER_PRE:
1549 /* call trigger on the frontend before the backend. */
1550
Liam Girdwood103d84a2012-11-19 14:39:15 +00001551 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001552 fe->dai_link->name, cmd);
1553
1554 ret = soc_pcm_trigger(substream, cmd);
1555 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001556 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001557 goto out;
1558 }
1559
1560 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1561 break;
1562 case SND_SOC_DPCM_TRIGGER_POST:
1563 /* call trigger on the frontend after the backend. */
1564
1565 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1566 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001567 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001568 goto out;
1569 }
1570
Liam Girdwood103d84a2012-11-19 14:39:15 +00001571 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001572 fe->dai_link->name, cmd);
1573
1574 ret = soc_pcm_trigger(substream, cmd);
1575 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001576 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1577 /* bespoke trigger() - handles both FE and BEs */
1578
Liam Girdwood103d84a2012-11-19 14:39:15 +00001579 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001580 fe->dai_link->name, cmd);
1581
1582 ret = soc_pcm_bespoke_trigger(substream, cmd);
1583 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001584 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001585 goto out;
1586 }
1587 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001588 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001589 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001590 fe->dai_link->name);
1591 ret = -EINVAL;
1592 goto out;
1593 }
1594
1595 switch (cmd) {
1596 case SNDRV_PCM_TRIGGER_START:
1597 case SNDRV_PCM_TRIGGER_RESUME:
1598 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1599 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1600 break;
1601 case SNDRV_PCM_TRIGGER_STOP:
1602 case SNDRV_PCM_TRIGGER_SUSPEND:
1603 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1604 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1605 break;
1606 }
1607
1608out:
1609 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1610 return ret;
1611}
1612
1613static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1614{
1615 struct snd_soc_dpcm *dpcm;
1616 int ret = 0;
1617
1618 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1619
1620 struct snd_soc_pcm_runtime *be = dpcm->be;
1621 struct snd_pcm_substream *be_substream =
1622 snd_soc_dpcm_get_substream(be, stream);
1623
1624 /* is this op for this BE ? */
1625 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1626 continue;
1627
1628 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1629 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1630 continue;
1631
Liam Girdwood103d84a2012-11-19 14:39:15 +00001632 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001633 dpcm->fe->dai_link->name);
1634
1635 ret = soc_pcm_prepare(be_substream);
1636 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001637 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001638 ret);
1639 break;
1640 }
1641
1642 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1643 }
1644 return ret;
1645}
1646
Mark Brown45c0a182012-05-09 21:46:27 +01001647static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001648{
1649 struct snd_soc_pcm_runtime *fe = substream->private_data;
1650 int stream = substream->stream, ret = 0;
1651
1652 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1653
Liam Girdwood103d84a2012-11-19 14:39:15 +00001654 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001655
1656 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1657
1658 /* there is no point preparing this FE if there are no BEs */
1659 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001660 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001661 fe->dai_link->name);
1662 ret = -EINVAL;
1663 goto out;
1664 }
1665
1666 ret = dpcm_be_dai_prepare(fe, substream->stream);
1667 if (ret < 0)
1668 goto out;
1669
1670 /* call prepare on the frontend */
1671 ret = soc_pcm_prepare(substream);
1672 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001673 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001674 fe->dai_link->name);
1675 goto out;
1676 }
1677
1678 /* run the stream event for each BE */
1679 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1680 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1681
1682out:
1683 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1684 mutex_unlock(&fe->card->mutex);
1685
1686 return ret;
1687}
1688
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001689static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1690 unsigned int cmd, void *arg)
1691{
1692 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1693 struct snd_soc_platform *platform = rtd->platform;
1694
Mark Brownc5914b02013-10-30 17:47:39 -07001695 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001696 return platform->driver->ops->ioctl(substream, cmd, arg);
1697 return snd_pcm_lib_ioctl(substream, cmd, arg);
1698}
1699
Liam Girdwood618dae12012-04-25 12:12:51 +01001700static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1701{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001702 struct snd_pcm_substream *substream =
1703 snd_soc_dpcm_get_substream(fe, stream);
1704 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001705 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001706
Liam Girdwood103d84a2012-11-19 14:39:15 +00001707 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001708 stream ? "capture" : "playback", fe->dai_link->name);
1709
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001710 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1711 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001712 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001713 fe->dai_link->name);
1714
1715 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1716 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001717 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001718 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001719 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001720 fe->dai_link->name);
1721
1722 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1723 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001724 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001725 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001726
1727 err = dpcm_be_dai_hw_free(fe, stream);
1728 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001729 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001730
1731 err = dpcm_be_dai_shutdown(fe, stream);
1732 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001733 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001734
1735 /* run the stream event for each BE */
1736 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1737
1738 return 0;
1739}
1740
1741static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1742{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001743 struct snd_pcm_substream *substream =
1744 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001745 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001746 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001747 int ret;
1748
Liam Girdwood103d84a2012-11-19 14:39:15 +00001749 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001750 stream ? "capture" : "playback", fe->dai_link->name);
1751
1752 /* Only start the BE if the FE is ready */
1753 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1754 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1755 return -EINVAL;
1756
1757 /* startup must always be called for new BEs */
1758 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001759 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001760 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001761
1762 /* keep going if FE state is > open */
1763 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1764 return 0;
1765
1766 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001767 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001768 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001769
1770 /* keep going if FE state is > hw_params */
1771 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1772 return 0;
1773
1774
1775 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001776 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001777 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001778
1779 /* run the stream event for each BE */
1780 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1781
1782 /* keep going if FE state is > prepare */
1783 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1784 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1785 return 0;
1786
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001787 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1788 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001789 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001790 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001791
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001792 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1793 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001794 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001795 goto hw_free;
1796 }
1797 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001798 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001799 fe->dai_link->name);
1800
1801 ret = dpcm_be_dai_trigger(fe, stream,
1802 SNDRV_PCM_TRIGGER_START);
1803 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001804 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001805 goto hw_free;
1806 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001807 }
1808
1809 return 0;
1810
1811hw_free:
1812 dpcm_be_dai_hw_free(fe, stream);
1813close:
1814 dpcm_be_dai_shutdown(fe, stream);
1815disconnect:
1816 /* disconnect any non started BEs */
1817 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1818 struct snd_soc_pcm_runtime *be = dpcm->be;
1819 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1820 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1821 }
1822
1823 return ret;
1824}
1825
1826static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1827{
1828 int ret;
1829
1830 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1831 ret = dpcm_run_update_startup(fe, stream);
1832 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001833 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001834 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1835
1836 return ret;
1837}
1838
1839static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1840{
1841 int ret;
1842
1843 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1844 ret = dpcm_run_update_shutdown(fe, stream);
1845 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001846 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001847 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1848
1849 return ret;
1850}
1851
1852/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1853 * any DAI links.
1854 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001855int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001856{
Liam Girdwood618dae12012-04-25 12:12:51 +01001857 int i, old, new, paths;
1858
Liam Girdwood618dae12012-04-25 12:12:51 +01001859 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1860 for (i = 0; i < card->num_rtd; i++) {
1861 struct snd_soc_dapm_widget_list *list;
1862 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1863
1864 /* make sure link is FE */
1865 if (!fe->dai_link->dynamic)
1866 continue;
1867
1868 /* only check active links */
1869 if (!fe->cpu_dai->active)
1870 continue;
1871
1872 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001873 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001874 fe->dai_link->name);
1875
1876 /* skip if FE doesn't have playback capability */
1877 if (!fe->cpu_dai->driver->playback.channels_min)
1878 goto capture;
1879
1880 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1881 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001882 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001883 fe->dai_link->name, "playback");
1884 mutex_unlock(&card->mutex);
1885 return paths;
1886 }
1887
1888 /* update any new playback paths */
1889 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1890 if (new) {
1891 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1892 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1893 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1894 }
1895
1896 /* update any old playback paths */
1897 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1898 if (old) {
1899 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1900 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1901 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1902 }
1903
1904capture:
1905 /* skip if FE doesn't have capture capability */
1906 if (!fe->cpu_dai->driver->capture.channels_min)
1907 continue;
1908
1909 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1910 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001911 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001912 fe->dai_link->name, "capture");
1913 mutex_unlock(&card->mutex);
1914 return paths;
1915 }
1916
1917 /* update any new capture paths */
1918 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1919 if (new) {
1920 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1921 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1922 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1923 }
1924
1925 /* update any old capture paths */
1926 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1927 if (old) {
1928 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1929 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1930 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1931 }
1932
1933 dpcm_path_put(&list);
1934 }
1935
1936 mutex_unlock(&card->mutex);
1937 return 0;
1938}
Liam Girdwood01d75842012-04-25 12:12:49 +01001939int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1940{
1941 struct snd_soc_dpcm *dpcm;
1942 struct list_head *clients =
1943 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1944
1945 list_for_each_entry(dpcm, clients, list_be) {
1946
1947 struct snd_soc_pcm_runtime *be = dpcm->be;
1948 struct snd_soc_dai *dai = be->codec_dai;
1949 struct snd_soc_dai_driver *drv = dai->driver;
1950
1951 if (be->dai_link->ignore_suspend)
1952 continue;
1953
Liam Girdwood103d84a2012-11-19 14:39:15 +00001954 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001955
Mark Brownc5914b02013-10-30 17:47:39 -07001956 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1957 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01001958 }
1959
1960 return 0;
1961}
1962
Mark Brown45c0a182012-05-09 21:46:27 +01001963static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001964{
1965 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1966 struct snd_soc_dpcm *dpcm;
1967 struct snd_soc_dapm_widget_list *list;
1968 int ret;
1969 int stream = fe_substream->stream;
1970
1971 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1972 fe->dpcm[stream].runtime = fe_substream->runtime;
1973
1974 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001975 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001976 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001977 }
1978
1979 /* calculate valid and active FE <-> BE dpcms */
1980 dpcm_process_paths(fe, stream, &list, 1);
1981
1982 ret = dpcm_fe_dai_startup(fe_substream);
1983 if (ret < 0) {
1984 /* clean up all links */
1985 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1986 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1987
1988 dpcm_be_disconnect(fe, stream);
1989 fe->dpcm[stream].runtime = NULL;
1990 }
1991
1992 dpcm_clear_pending_state(fe, stream);
1993 dpcm_path_put(&list);
1994 mutex_unlock(&fe->card->mutex);
1995 return ret;
1996}
1997
Mark Brown45c0a182012-05-09 21:46:27 +01001998static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001999{
2000 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2001 struct snd_soc_dpcm *dpcm;
2002 int stream = fe_substream->stream, ret;
2003
2004 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2005 ret = dpcm_fe_dai_shutdown(fe_substream);
2006
2007 /* mark FE's links ready to prune */
2008 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2009 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2010
2011 dpcm_be_disconnect(fe, stream);
2012
2013 fe->dpcm[stream].runtime = NULL;
2014 mutex_unlock(&fe->card->mutex);
2015 return ret;
2016}
2017
Liam Girdwoodddee6272011-06-09 14:45:53 +01002018/* create a new pcm */
2019int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2020{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002021 struct snd_soc_platform *platform = rtd->platform;
2022 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2023 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2024 struct snd_pcm *pcm;
2025 char new_name[64];
2026 int ret = 0, playback = 0, capture = 0;
2027
Liam Girdwood01d75842012-04-25 12:12:49 +01002028 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002029 playback = rtd->dai_link->dpcm_playback;
2030 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002031 } else {
Mark Brown05679092013-06-01 23:13:53 +01002032 if (codec_dai->driver->playback.channels_min &&
2033 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002034 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002035 if (codec_dai->driver->capture.channels_min &&
2036 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002037 capture = 1;
2038 }
Sangsu Parka5002312012-01-02 17:15:10 +09002039
Fabio Estevamd6bead02013-08-29 10:32:13 -03002040 if (rtd->dai_link->playback_only) {
2041 playback = 1;
2042 capture = 0;
2043 }
2044
2045 if (rtd->dai_link->capture_only) {
2046 playback = 0;
2047 capture = 1;
2048 }
2049
Liam Girdwood01d75842012-04-25 12:12:49 +01002050 /* create the PCM */
2051 if (rtd->dai_link->no_pcm) {
2052 snprintf(new_name, sizeof(new_name), "(%s)",
2053 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002054
Liam Girdwood01d75842012-04-25 12:12:49 +01002055 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2056 playback, capture, &pcm);
2057 } else {
2058 if (rtd->dai_link->dynamic)
2059 snprintf(new_name, sizeof(new_name), "%s (*)",
2060 rtd->dai_link->stream_name);
2061 else
2062 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2063 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002064
Liam Girdwood01d75842012-04-25 12:12:49 +01002065 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2066 capture, &pcm);
2067 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002068 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002069 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002070 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002071 return ret;
2072 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002073 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002074
2075 /* DAPM dai link stream work */
2076 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2077
2078 rtd->pcm = pcm;
2079 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002080
2081 if (rtd->dai_link->no_pcm) {
2082 if (playback)
2083 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2084 if (capture)
2085 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2086 goto out;
2087 }
2088
2089 /* ASoC PCM operations */
2090 if (rtd->dai_link->dynamic) {
2091 rtd->ops.open = dpcm_fe_dai_open;
2092 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2093 rtd->ops.prepare = dpcm_fe_dai_prepare;
2094 rtd->ops.trigger = dpcm_fe_dai_trigger;
2095 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2096 rtd->ops.close = dpcm_fe_dai_close;
2097 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002098 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002099 } else {
2100 rtd->ops.open = soc_pcm_open;
2101 rtd->ops.hw_params = soc_pcm_hw_params;
2102 rtd->ops.prepare = soc_pcm_prepare;
2103 rtd->ops.trigger = soc_pcm_trigger;
2104 rtd->ops.hw_free = soc_pcm_hw_free;
2105 rtd->ops.close = soc_pcm_close;
2106 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002107 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002108 }
2109
Liam Girdwoodddee6272011-06-09 14:45:53 +01002110 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002111 rtd->ops.ack = platform->driver->ops->ack;
2112 rtd->ops.copy = platform->driver->ops->copy;
2113 rtd->ops.silence = platform->driver->ops->silence;
2114 rtd->ops.page = platform->driver->ops->page;
2115 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002116 }
2117
2118 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002119 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002120
2121 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002122 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002123
2124 if (platform->driver->pcm_new) {
2125 ret = platform->driver->pcm_new(rtd);
2126 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002127 dev_err(platform->dev,
2128 "ASoC: pcm constructor failed: %d\n",
2129 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002130 return ret;
2131 }
2132 }
2133
2134 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002135out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002136 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002137 cpu_dai->name);
2138 return ret;
2139}
Liam Girdwood01d75842012-04-25 12:12:49 +01002140
2141/* is the current PCM operation for this FE ? */
2142int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2143{
2144 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2145 return 1;
2146 return 0;
2147}
2148EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2149
2150/* is the current PCM operation for this BE ? */
2151int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2152 struct snd_soc_pcm_runtime *be, int stream)
2153{
2154 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2155 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2156 be->dpcm[stream].runtime_update))
2157 return 1;
2158 return 0;
2159}
2160EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2161
2162/* get the substream for this BE */
2163struct snd_pcm_substream *
2164 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2165{
2166 return be->pcm->streams[stream].substream;
2167}
2168EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2169
2170/* get the BE runtime state */
2171enum snd_soc_dpcm_state
2172 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2173{
2174 return be->dpcm[stream].state;
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2177
2178/* set the BE runtime state */
2179void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2180 int stream, enum snd_soc_dpcm_state state)
2181{
2182 be->dpcm[stream].state = state;
2183}
2184EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2185
2186/*
2187 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2188 * are not running, paused or suspended for the specified stream direction.
2189 */
2190int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2191 struct snd_soc_pcm_runtime *be, int stream)
2192{
2193 struct snd_soc_dpcm *dpcm;
2194 int state;
2195
2196 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2197
2198 if (dpcm->fe == fe)
2199 continue;
2200
2201 state = dpcm->fe->dpcm[stream].state;
2202 if (state == SND_SOC_DPCM_STATE_START ||
2203 state == SND_SOC_DPCM_STATE_PAUSED ||
2204 state == SND_SOC_DPCM_STATE_SUSPEND)
2205 return 0;
2206 }
2207
2208 /* it's safe to free/stop this BE DAI */
2209 return 1;
2210}
2211EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2212
2213/*
2214 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2215 * running, paused or suspended for the specified stream direction.
2216 */
2217int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2218 struct snd_soc_pcm_runtime *be, int stream)
2219{
2220 struct snd_soc_dpcm *dpcm;
2221 int state;
2222
2223 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2224
2225 if (dpcm->fe == fe)
2226 continue;
2227
2228 state = dpcm->fe->dpcm[stream].state;
2229 if (state == SND_SOC_DPCM_STATE_START ||
2230 state == SND_SOC_DPCM_STATE_PAUSED ||
2231 state == SND_SOC_DPCM_STATE_SUSPEND ||
2232 state == SND_SOC_DPCM_STATE_PREPARE)
2233 return 0;
2234 }
2235
2236 /* it's safe to change hw_params */
2237 return 1;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002240
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002241int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2242 int cmd, struct snd_soc_platform *platform)
2243{
Mark Brownc5914b02013-10-30 17:47:39 -07002244 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002245 return platform->driver->ops->trigger(substream, cmd);
2246 return 0;
2247}
2248EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2249
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002250#ifdef CONFIG_DEBUG_FS
2251static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2252{
2253 switch (state) {
2254 case SND_SOC_DPCM_STATE_NEW:
2255 return "new";
2256 case SND_SOC_DPCM_STATE_OPEN:
2257 return "open";
2258 case SND_SOC_DPCM_STATE_HW_PARAMS:
2259 return "hw_params";
2260 case SND_SOC_DPCM_STATE_PREPARE:
2261 return "prepare";
2262 case SND_SOC_DPCM_STATE_START:
2263 return "start";
2264 case SND_SOC_DPCM_STATE_STOP:
2265 return "stop";
2266 case SND_SOC_DPCM_STATE_SUSPEND:
2267 return "suspend";
2268 case SND_SOC_DPCM_STATE_PAUSED:
2269 return "paused";
2270 case SND_SOC_DPCM_STATE_HW_FREE:
2271 return "hw_free";
2272 case SND_SOC_DPCM_STATE_CLOSE:
2273 return "close";
2274 }
2275
2276 return "unknown";
2277}
2278
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002279static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2280 int stream, char *buf, size_t size)
2281{
2282 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2283 struct snd_soc_dpcm *dpcm;
2284 ssize_t offset = 0;
2285
2286 /* FE state */
2287 offset += snprintf(buf + offset, size - offset,
2288 "[%s - %s]\n", fe->dai_link->name,
2289 stream ? "Capture" : "Playback");
2290
2291 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2292 dpcm_state_string(fe->dpcm[stream].state));
2293
2294 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2295 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2296 offset += snprintf(buf + offset, size - offset,
2297 "Hardware Params: "
2298 "Format = %s, Channels = %d, Rate = %d\n",
2299 snd_pcm_format_name(params_format(params)),
2300 params_channels(params),
2301 params_rate(params));
2302
2303 /* BEs state */
2304 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2305
2306 if (list_empty(&fe->dpcm[stream].be_clients)) {
2307 offset += snprintf(buf + offset, size - offset,
2308 " No active DSP links\n");
2309 goto out;
2310 }
2311
2312 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2313 struct snd_soc_pcm_runtime *be = dpcm->be;
2314 params = &dpcm->hw_params;
2315
2316 offset += snprintf(buf + offset, size - offset,
2317 "- %s\n", be->dai_link->name);
2318
2319 offset += snprintf(buf + offset, size - offset,
2320 " State: %s\n",
2321 dpcm_state_string(be->dpcm[stream].state));
2322
2323 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2324 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2325 offset += snprintf(buf + offset, size - offset,
2326 " Hardware Params: "
2327 "Format = %s, Channels = %d, Rate = %d\n",
2328 snd_pcm_format_name(params_format(params)),
2329 params_channels(params),
2330 params_rate(params));
2331 }
2332
2333out:
2334 return offset;
2335}
2336
2337static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2338 size_t count, loff_t *ppos)
2339{
2340 struct snd_soc_pcm_runtime *fe = file->private_data;
2341 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2342 char *buf;
2343
2344 buf = kmalloc(out_count, GFP_KERNEL);
2345 if (!buf)
2346 return -ENOMEM;
2347
2348 if (fe->cpu_dai->driver->playback.channels_min)
2349 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2350 buf + offset, out_count - offset);
2351
2352 if (fe->cpu_dai->driver->capture.channels_min)
2353 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2354 buf + offset, out_count - offset);
2355
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002356 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002357
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002358 kfree(buf);
2359 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002360}
2361
2362static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002363 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002364 .read = dpcm_state_read_file,
2365 .llseek = default_llseek,
2366};
2367
2368int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2369{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002370 if (!rtd->dai_link)
2371 return 0;
2372
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002373 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2374 rtd->card->debugfs_card_root);
2375 if (!rtd->debugfs_dpcm_root) {
2376 dev_dbg(rtd->dev,
2377 "ASoC: Failed to create dpcm debugfs directory %s\n",
2378 rtd->dai_link->name);
2379 return -EINVAL;
2380 }
2381
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002382 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002383 rtd->debugfs_dpcm_root,
2384 rtd, &dpcm_state_fops);
2385
2386 return 0;
2387}
2388#endif