blob: 591f0f3074c58b75eeb5b99a939bc9bf46ffae52 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000022#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010023#include <linux/slab.h>
24#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010025#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010026#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010027#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010031#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010032#include <sound/initval.h>
33
Liam Girdwood01d75842012-04-25 12:12:49 +010034#define DPCM_MAX_BE_USERS 8
35
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020036/**
37 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
38 * @substream: the pcm substream
39 * @hw: the hardware parameters
40 *
41 * Sets the substream runtime hardware parameters.
42 */
43int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
44 const struct snd_pcm_hardware *hw)
45{
46 struct snd_pcm_runtime *runtime = substream->runtime;
47 runtime->hw.info = hw->info;
48 runtime->hw.formats = hw->formats;
49 runtime->hw.period_bytes_min = hw->period_bytes_min;
50 runtime->hw.period_bytes_max = hw->period_bytes_max;
51 runtime->hw.periods_min = hw->periods_min;
52 runtime->hw.periods_max = hw->periods_max;
53 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
54 runtime->hw.fifo_size = hw->fifo_size;
55 return 0;
56}
57EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
58
Liam Girdwood01d75842012-04-25 12:12:49 +010059/* DPCM stream event, send event to FE and all active BEs. */
60static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
61 int event)
62{
63 struct snd_soc_dpcm *dpcm;
64
65 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
66
67 struct snd_soc_pcm_runtime *be = dpcm->be;
68
Liam Girdwood103d84a2012-11-19 14:39:15 +000069 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010070 be->dai_link->name, event, dir);
71
72 snd_soc_dapm_stream_event(be, dir, event);
73 }
74
75 snd_soc_dapm_stream_event(fe, dir, event);
76
77 return 0;
78}
79
Dong Aisheng17841022011-08-29 17:15:14 +080080static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
81 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010082{
83 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010084 int ret;
85
Dong Aisheng17841022011-08-29 17:15:14 +080086 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010087 !rtd->dai_link->symmetric_rates)
88 return 0;
89
90 /* This can happen if multiple streams are starting simultaneously -
91 * the second can need to get its constraints before the first has
92 * picked a rate. Complain and allow the application to carry on.
93 */
Dong Aisheng17841022011-08-29 17:15:14 +080094 if (!soc_dai->rate) {
95 dev_warn(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +000096 "ASoC: Not enforcing symmetric_rates due to race\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +010097 return 0;
98 }
99
Liam Girdwood103d84a2012-11-19 14:39:15 +0000100 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100101
102 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
103 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +0800104 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100105 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +0800106 dev_err(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000107 "ASoC: Unable to apply rate symmetry constraint: %d\n",
108 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100109 return ret;
110 }
111
112 return 0;
113}
114
115/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000116 * List of sample sizes that might go over the bus for parameter
117 * application. There ought to be a wildcard sample size for things
118 * like the DAC/ADC resolution to use but there isn't right now.
119 */
120static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200121 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000122};
123
124static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
125 struct snd_soc_dai *dai)
126{
127 int ret, i, bits;
128
129 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
130 bits = dai->driver->playback.sig_bits;
131 else
132 bits = dai->driver->capture.sig_bits;
133
134 if (!bits)
135 return;
136
137 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000138 if (bits >= sample_sizes[i])
139 continue;
140
141 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
142 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000143 if (ret != 0)
144 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000145 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000146 bits, sample_sizes[i], ret);
147 }
148}
149
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200150static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
151 struct snd_soc_pcm_stream *codec_stream,
152 struct snd_soc_pcm_stream *cpu_stream)
153{
154 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
155 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
156 hw->channels_min = max(codec_stream->channels_min,
157 cpu_stream->channels_min);
158 hw->channels_max = min(codec_stream->channels_max,
159 cpu_stream->channels_max);
160 hw->formats = codec_stream->formats & cpu_stream->formats;
161 hw->rates = codec_stream->rates & cpu_stream->rates;
162 if (codec_stream->rates
163 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
164 hw->rates |= cpu_stream->rates;
165 if (cpu_stream->rates
166 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
167 hw->rates |= codec_stream->rates;
168}
169
Mark Brown58ba9b22012-01-16 18:38:51 +0000170/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100171 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
172 * then initialized and any private data can be allocated. This also calls
173 * startup for the cpu DAI, platform, machine and codec DAI.
174 */
175static int soc_pcm_open(struct snd_pcm_substream *substream)
176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
178 struct snd_pcm_runtime *runtime = substream->runtime;
179 struct snd_soc_platform *platform = rtd->platform;
180 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
181 struct snd_soc_dai *codec_dai = rtd->codec_dai;
182 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
183 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
184 int ret = 0;
185
Mark Brownd6652ef2011-12-03 20:14:31 +0000186 pm_runtime_get_sync(cpu_dai->dev);
187 pm_runtime_get_sync(codec_dai->dev);
188 pm_runtime_get_sync(platform->dev);
189
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100190 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100191
192 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700193 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
195 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000196 dev_err(cpu_dai->dev, "ASoC: can't open interface"
197 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198 goto out;
199 }
200 }
201
202 if (platform->driver->ops && platform->driver->ops->open) {
203 ret = platform->driver->ops->open(substream);
204 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000205 dev_err(platform->dev, "ASoC: can't open platform"
206 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100207 goto platform_err;
208 }
209 }
210
Mark Brownc5914b02013-10-30 17:47:39 -0700211 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100212 ret = codec_dai->driver->ops->startup(substream, codec_dai);
213 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000214 dev_err(codec_dai->dev, "ASoC: can't open codec"
215 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100216 goto codec_dai_err;
217 }
218 }
219
220 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
221 ret = rtd->dai_link->ops->startup(substream);
222 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000223 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000224 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100225 goto machine_err;
226 }
227 }
228
Liam Girdwood01d75842012-04-25 12:12:49 +0100229 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
230 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
231 goto dynamic;
232
Liam Girdwoodddee6272011-06-09 14:45:53 +0100233 /* Check that the codec and cpu DAIs are compatible */
234 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200235 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
236 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100237 } else {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200238 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
239 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100240 }
241
242 ret = -EINVAL;
243 snd_pcm_limit_hw_rates(runtime);
244 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000245 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100246 codec_dai->name, cpu_dai->name);
247 goto config_err;
248 }
249 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000250 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100251 codec_dai->name, cpu_dai->name);
252 goto config_err;
253 }
254 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
255 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000256 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100257 codec_dai->name, cpu_dai->name);
258 goto config_err;
259 }
260
Mark Brown58ba9b22012-01-16 18:38:51 +0000261 soc_pcm_apply_msb(substream, codec_dai);
262 soc_pcm_apply_msb(substream, cpu_dai);
263
Liam Girdwoodddee6272011-06-09 14:45:53 +0100264 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800265 if (cpu_dai->active) {
266 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
267 if (ret != 0)
268 goto config_err;
269 }
270
271 if (codec_dai->active) {
272 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100273 if (ret != 0)
274 goto config_err;
275 }
276
Liam Girdwood103d84a2012-11-19 14:39:15 +0000277 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100278 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000279 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
280 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100281 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000282 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100283 runtime->hw.rate_max);
284
Liam Girdwood01d75842012-04-25 12:12:49 +0100285dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
287 cpu_dai->playback_active++;
288 codec_dai->playback_active++;
289 } else {
290 cpu_dai->capture_active++;
291 codec_dai->capture_active++;
292 }
293 cpu_dai->active++;
294 codec_dai->active++;
295 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100296 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100297 return 0;
298
299config_err:
300 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
301 rtd->dai_link->ops->shutdown(substream);
302
303machine_err:
304 if (codec_dai->driver->ops->shutdown)
305 codec_dai->driver->ops->shutdown(substream, codec_dai);
306
307codec_dai_err:
308 if (platform->driver->ops && platform->driver->ops->close)
309 platform->driver->ops->close(substream);
310
311platform_err:
312 if (cpu_dai->driver->ops->shutdown)
313 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
314out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100315 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000316
317 pm_runtime_put(platform->dev);
318 pm_runtime_put(codec_dai->dev);
319 pm_runtime_put(cpu_dai->dev);
320
Liam Girdwoodddee6272011-06-09 14:45:53 +0100321 return ret;
322}
323
324/*
325 * Power down the audio subsystem pmdown_time msecs after close is called.
326 * This is to ensure there are no pops or clicks in between any music tracks
327 * due to DAPM power cycling.
328 */
329static void close_delayed_work(struct work_struct *work)
330{
331 struct snd_soc_pcm_runtime *rtd =
332 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
333 struct snd_soc_dai *codec_dai = rtd->codec_dai;
334
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100335 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100336
Liam Girdwood103d84a2012-11-19 14:39:15 +0000337 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100338 codec_dai->driver->playback.stream_name,
339 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600340 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100341
342 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600343 if (rtd->pop_wait == 1) {
344 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800345 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000346 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100347 }
348
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100349 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100350}
351
352/*
353 * Called by ALSA when a PCM substream is closed. Private data can be
354 * freed here. The cpu DAI, codec DAI, machine and platform are also
355 * shutdown.
356 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100357static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100358{
359 struct snd_soc_pcm_runtime *rtd = substream->private_data;
360 struct snd_soc_platform *platform = rtd->platform;
361 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
362 struct snd_soc_dai *codec_dai = rtd->codec_dai;
363 struct snd_soc_codec *codec = rtd->codec;
364
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100365 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100366
367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
368 cpu_dai->playback_active--;
369 codec_dai->playback_active--;
370 } else {
371 cpu_dai->capture_active--;
372 codec_dai->capture_active--;
373 }
374
375 cpu_dai->active--;
376 codec_dai->active--;
377 codec->active--;
378
Dong Aisheng17841022011-08-29 17:15:14 +0800379 /* clear the corresponding DAIs rate when inactive */
380 if (!cpu_dai->active)
381 cpu_dai->rate = 0;
382
383 if (!codec_dai->active)
384 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200385
Liam Girdwoodddee6272011-06-09 14:45:53 +0100386 /* Muting the DAC suppresses artifacts caused during digital
387 * shutdown, for example from stopping clocks.
388 */
Mark Brownda183962013-02-06 15:44:07 +0000389 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100390
391 if (cpu_dai->driver->ops->shutdown)
392 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
393
394 if (codec_dai->driver->ops->shutdown)
395 codec_dai->driver->ops->shutdown(substream, codec_dai);
396
397 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
398 rtd->dai_link->ops->shutdown(substream);
399
400 if (platform->driver->ops && platform->driver->ops->close)
401 platform->driver->ops->close(substream);
402 cpu_dai->runtime = NULL;
403
404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000405 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200406 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300407 /* powered down playback stream now */
408 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800409 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800410 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300411 } else {
412 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600413 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100414 queue_delayed_work(system_power_efficient_wq,
415 &rtd->delayed_work,
416 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300417 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100418 } else {
419 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800420 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000421 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100422 }
423
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100424 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000425
426 pm_runtime_put(platform->dev);
427 pm_runtime_put(codec_dai->dev);
428 pm_runtime_put(cpu_dai->dev);
429
Liam Girdwoodddee6272011-06-09 14:45:53 +0100430 return 0;
431}
432
433/*
434 * Called by ALSA when the PCM substream is prepared, can set format, sample
435 * rate, etc. This function is non atomic and can be called multiple times,
436 * it can refer to the runtime info.
437 */
438static int soc_pcm_prepare(struct snd_pcm_substream *substream)
439{
440 struct snd_soc_pcm_runtime *rtd = substream->private_data;
441 struct snd_soc_platform *platform = rtd->platform;
442 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
443 struct snd_soc_dai *codec_dai = rtd->codec_dai;
444 int ret = 0;
445
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100446 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100447
448 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
449 ret = rtd->dai_link->ops->prepare(substream);
450 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000451 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
452 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 goto out;
454 }
455 }
456
457 if (platform->driver->ops && platform->driver->ops->prepare) {
458 ret = platform->driver->ops->prepare(substream);
459 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000460 dev_err(platform->dev, "ASoC: platform prepare error:"
461 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100462 goto out;
463 }
464 }
465
Mark Brownc5914b02013-10-30 17:47:39 -0700466 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100467 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
468 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000469 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000470 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100471 goto out;
472 }
473 }
474
Mark Brownc5914b02013-10-30 17:47:39 -0700475 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100476 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
477 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000478 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000479 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100480 goto out;
481 }
482 }
483
484 /* cancel any delayed stream shutdown that is pending */
485 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600486 rtd->pop_wait) {
487 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100488 cancel_delayed_work(&rtd->delayed_work);
489 }
490
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000491 snd_soc_dapm_stream_event(rtd, substream->stream,
492 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100493
Mark Brownda183962013-02-06 15:44:07 +0000494 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100495
496out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100497 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498 return ret;
499}
500
501/*
502 * Called by ALSA when the hardware params are set by application. This
503 * function can also be called multiple times and can allocate buffers
504 * (using snd_pcm_lib_* ). It's non-atomic.
505 */
506static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
507 struct snd_pcm_hw_params *params)
508{
509 struct snd_soc_pcm_runtime *rtd = substream->private_data;
510 struct snd_soc_platform *platform = rtd->platform;
511 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
512 struct snd_soc_dai *codec_dai = rtd->codec_dai;
513 int ret = 0;
514
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100515 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100516
517 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
518 ret = rtd->dai_link->ops->hw_params(substream, params);
519 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000520 dev_err(rtd->card->dev, "ASoC: machine hw_params"
521 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522 goto out;
523 }
524 }
525
Mark Brownc5914b02013-10-30 17:47:39 -0700526 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100527 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
528 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000529 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
530 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100531 goto codec_err;
532 }
533 }
534
Mark Brownc5914b02013-10-30 17:47:39 -0700535 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
537 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000538 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000539 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 goto interface_err;
541 }
542 }
543
544 if (platform->driver->ops && platform->driver->ops->hw_params) {
545 ret = platform->driver->ops->hw_params(substream, params);
546 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000547 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000548 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100549 goto platform_err;
550 }
551 }
552
Dong Aisheng17841022011-08-29 17:15:14 +0800553 /* store the rate for each DAIs */
554 cpu_dai->rate = params_rate(params);
555 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556
557out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100558 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100559 return ret;
560
561platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700562 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
564
565interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700566 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100567 codec_dai->driver->ops->hw_free(substream, codec_dai);
568
569codec_err:
570 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
571 rtd->dai_link->ops->hw_free(substream);
572
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100573 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100574 return ret;
575}
576
577/*
578 * Frees resources allocated by hw_params, can be called multiple times
579 */
580static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
581{
582 struct snd_soc_pcm_runtime *rtd = substream->private_data;
583 struct snd_soc_platform *platform = rtd->platform;
584 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
585 struct snd_soc_dai *codec_dai = rtd->codec_dai;
586 struct snd_soc_codec *codec = rtd->codec;
587
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100588 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589
590 /* apply codec digital mute */
591 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000592 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593
594 /* free any machine hw params */
595 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
596 rtd->dai_link->ops->hw_free(substream);
597
598 /* free any DMA resources */
599 if (platform->driver->ops && platform->driver->ops->hw_free)
600 platform->driver->ops->hw_free(substream);
601
602 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700603 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100604 codec_dai->driver->ops->hw_free(substream, codec_dai);
605
Mark Brownc5914b02013-10-30 17:47:39 -0700606 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100607 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
608
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100609 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100610 return 0;
611}
612
613static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
614{
615 struct snd_soc_pcm_runtime *rtd = substream->private_data;
616 struct snd_soc_platform *platform = rtd->platform;
617 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
618 struct snd_soc_dai *codec_dai = rtd->codec_dai;
619 int ret;
620
Mark Brownc5914b02013-10-30 17:47:39 -0700621 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100622 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
623 if (ret < 0)
624 return ret;
625 }
626
627 if (platform->driver->ops && platform->driver->ops->trigger) {
628 ret = platform->driver->ops->trigger(substream, cmd);
629 if (ret < 0)
630 return ret;
631 }
632
Mark Brownc5914b02013-10-30 17:47:39 -0700633 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100634 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
635 if (ret < 0)
636 return ret;
637 }
638 return 0;
639}
640
Mark Brown45c0a182012-05-09 21:46:27 +0100641static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
642 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100643{
644 struct snd_soc_pcm_runtime *rtd = substream->private_data;
645 struct snd_soc_platform *platform = rtd->platform;
646 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
647 struct snd_soc_dai *codec_dai = rtd->codec_dai;
648 int ret;
649
Mark Brownc5914b02013-10-30 17:47:39 -0700650 if (codec_dai->driver->ops &&
651 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100652 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
653 if (ret < 0)
654 return ret;
655 }
656
Mark Brownc5914b02013-10-30 17:47:39 -0700657 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100658 ret = platform->driver->bespoke_trigger(substream, cmd);
659 if (ret < 0)
660 return ret;
661 }
662
Mark Brownc5914b02013-10-30 17:47:39 -0700663 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100664 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
665 if (ret < 0)
666 return ret;
667 }
668 return 0;
669}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100670/*
671 * soc level wrapper for pointer callback
672 * If cpu_dai, codec_dai, platform driver has the delay callback, than
673 * the runtime->delay will be updated accordingly.
674 */
675static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
676{
677 struct snd_soc_pcm_runtime *rtd = substream->private_data;
678 struct snd_soc_platform *platform = rtd->platform;
679 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
680 struct snd_soc_dai *codec_dai = rtd->codec_dai;
681 struct snd_pcm_runtime *runtime = substream->runtime;
682 snd_pcm_uframes_t offset = 0;
683 snd_pcm_sframes_t delay = 0;
684
685 if (platform->driver->ops && platform->driver->ops->pointer)
686 offset = platform->driver->ops->pointer(substream);
687
Mark Brownc5914b02013-10-30 17:47:39 -0700688 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
690
Mark Brownc5914b02013-10-30 17:47:39 -0700691 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100692 delay += codec_dai->driver->ops->delay(substream, codec_dai);
693
694 if (platform->driver->delay)
695 delay += platform->driver->delay(substream, codec_dai);
696
697 runtime->delay = delay;
698
699 return offset;
700}
701
Liam Girdwood01d75842012-04-25 12:12:49 +0100702/* connect a FE and BE */
703static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
704 struct snd_soc_pcm_runtime *be, int stream)
705{
706 struct snd_soc_dpcm *dpcm;
707
708 /* only add new dpcms */
709 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
710 if (dpcm->be == be && dpcm->fe == fe)
711 return 0;
712 }
713
714 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
715 if (!dpcm)
716 return -ENOMEM;
717
718 dpcm->be = be;
719 dpcm->fe = fe;
720 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
721 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
722 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
723 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
724
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300725 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100726 stream ? "capture" : "playback", fe->dai_link->name,
727 stream ? "<-" : "->", be->dai_link->name);
728
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100729#ifdef CONFIG_DEBUG_FS
730 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
731 fe->debugfs_dpcm_root, &dpcm->state);
732#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100733 return 1;
734}
735
736/* reparent a BE onto another FE */
737static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
738 struct snd_soc_pcm_runtime *be, int stream)
739{
740 struct snd_soc_dpcm *dpcm;
741 struct snd_pcm_substream *fe_substream, *be_substream;
742
743 /* reparent if BE is connected to other FEs */
744 if (!be->dpcm[stream].users)
745 return;
746
747 be_substream = snd_soc_dpcm_get_substream(be, stream);
748
749 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
750 if (dpcm->fe == fe)
751 continue;
752
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300753 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100754 stream ? "capture" : "playback",
755 dpcm->fe->dai_link->name,
756 stream ? "<-" : "->", dpcm->be->dai_link->name);
757
758 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
759 be_substream->runtime = fe_substream->runtime;
760 break;
761 }
762}
763
764/* disconnect a BE and FE */
765static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
766{
767 struct snd_soc_dpcm *dpcm, *d;
768
769 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000770 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100771 stream ? "capture" : "playback",
772 dpcm->be->dai_link->name);
773
774 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
775 continue;
776
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300777 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100778 stream ? "capture" : "playback", fe->dai_link->name,
779 stream ? "<-" : "->", dpcm->be->dai_link->name);
780
781 /* BEs still alive need new FE */
782 dpcm_be_reparent(fe, dpcm->be, stream);
783
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100784#ifdef CONFIG_DEBUG_FS
785 debugfs_remove(dpcm->debugfs_state);
786#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100787 list_del(&dpcm->list_be);
788 list_del(&dpcm->list_fe);
789 kfree(dpcm);
790 }
791}
792
793/* get BE for DAI widget and stream */
794static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
795 struct snd_soc_dapm_widget *widget, int stream)
796{
797 struct snd_soc_pcm_runtime *be;
798 int i;
799
800 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
801 for (i = 0; i < card->num_links; i++) {
802 be = &card->rtd[i];
803
Liam Girdwood35ea0652012-06-05 19:26:59 +0100804 if (!be->dai_link->no_pcm)
805 continue;
806
Liam Girdwood01d75842012-04-25 12:12:49 +0100807 if (be->cpu_dai->playback_widget == widget ||
808 be->codec_dai->playback_widget == widget)
809 return be;
810 }
811 } else {
812
813 for (i = 0; i < card->num_links; i++) {
814 be = &card->rtd[i];
815
Liam Girdwood35ea0652012-06-05 19:26:59 +0100816 if (!be->dai_link->no_pcm)
817 continue;
818
Liam Girdwood01d75842012-04-25 12:12:49 +0100819 if (be->cpu_dai->capture_widget == widget ||
820 be->codec_dai->capture_widget == widget)
821 return be;
822 }
823 }
824
Liam Girdwood103d84a2012-11-19 14:39:15 +0000825 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100826 stream ? "capture" : "playback", widget->name);
827 return NULL;
828}
829
830static inline struct snd_soc_dapm_widget *
831 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
832{
833 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
834 return rtd->cpu_dai->playback_widget;
835 else
836 return rtd->cpu_dai->capture_widget;
837}
838
839static inline struct snd_soc_dapm_widget *
840 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
841{
842 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
843 return rtd->codec_dai->playback_widget;
844 else
845 return rtd->codec_dai->capture_widget;
846}
847
848static int widget_in_list(struct snd_soc_dapm_widget_list *list,
849 struct snd_soc_dapm_widget *widget)
850{
851 int i;
852
853 for (i = 0; i < list->num_widgets; i++) {
854 if (widget == list->widgets[i])
855 return 1;
856 }
857
858 return 0;
859}
860
861static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
862 int stream, struct snd_soc_dapm_widget_list **list_)
863{
864 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
865 struct snd_soc_dapm_widget_list *list;
866 int paths;
867
868 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
869 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
870 if (list == NULL)
871 return -ENOMEM;
872
873 /* get number of valid DAI paths and their widgets */
874 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
875
Liam Girdwood103d84a2012-11-19 14:39:15 +0000876 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100877 stream ? "capture" : "playback");
878
879 *list_ = list;
880 return paths;
881}
882
883static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
884{
885 kfree(*list);
886}
887
888static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
889 struct snd_soc_dapm_widget_list **list_)
890{
891 struct snd_soc_dpcm *dpcm;
892 struct snd_soc_dapm_widget_list *list = *list_;
893 struct snd_soc_dapm_widget *widget;
894 int prune = 0;
895
896 /* Destroy any old FE <--> BE connections */
897 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
898
899 /* is there a valid CPU DAI widget for this BE */
900 widget = rtd_get_cpu_widget(dpcm->be, stream);
901
902 /* prune the BE if it's no longer in our active list */
903 if (widget && widget_in_list(list, widget))
904 continue;
905
906 /* is there a valid CODEC DAI widget for this BE */
907 widget = rtd_get_codec_widget(dpcm->be, stream);
908
909 /* prune the BE if it's no longer in our active list */
910 if (widget && widget_in_list(list, widget))
911 continue;
912
Liam Girdwood103d84a2012-11-19 14:39:15 +0000913 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100914 stream ? "capture" : "playback",
915 dpcm->be->dai_link->name, fe->dai_link->name);
916 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
917 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
918 prune++;
919 }
920
Liam Girdwood103d84a2012-11-19 14:39:15 +0000921 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100922 return prune;
923}
924
925static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
926 struct snd_soc_dapm_widget_list **list_)
927{
928 struct snd_soc_card *card = fe->card;
929 struct snd_soc_dapm_widget_list *list = *list_;
930 struct snd_soc_pcm_runtime *be;
931 int i, new = 0, err;
932
933 /* Create any new FE <--> BE connections */
934 for (i = 0; i < list->num_widgets; i++) {
935
Mark Brown46162742013-06-05 19:36:11 +0100936 switch (list->widgets[i]->id) {
937 case snd_soc_dapm_dai_in:
938 case snd_soc_dapm_dai_out:
939 break;
940 default:
Liam Girdwood01d75842012-04-25 12:12:49 +0100941 continue;
Mark Brown46162742013-06-05 19:36:11 +0100942 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100943
944 /* is there a valid BE rtd for this widget */
945 be = dpcm_get_be(card, list->widgets[i], stream);
946 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000947 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100948 list->widgets[i]->name);
949 continue;
950 }
951
952 /* make sure BE is a real BE */
953 if (!be->dai_link->no_pcm)
954 continue;
955
956 /* don't connect if FE is not running */
957 if (!fe->dpcm[stream].runtime)
958 continue;
959
960 /* newly connected FE and BE */
961 err = dpcm_be_connect(fe, be, stream);
962 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000963 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100964 list->widgets[i]->name);
965 break;
966 } else if (err == 0) /* already connected */
967 continue;
968
969 /* new */
970 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
971 new++;
972 }
973
Liam Girdwood103d84a2012-11-19 14:39:15 +0000974 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100975 return new;
976}
977
978/*
979 * Find the corresponding BE DAIs that source or sink audio to this
980 * FE substream.
981 */
982static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
983 int stream, struct snd_soc_dapm_widget_list **list, int new)
984{
985 if (new)
986 return dpcm_add_paths(fe, stream, list);
987 else
988 return dpcm_prune_paths(fe, stream, list);
989}
990
991static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
992{
993 struct snd_soc_dpcm *dpcm;
994
995 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
996 dpcm->be->dpcm[stream].runtime_update =
997 SND_SOC_DPCM_UPDATE_NO;
998}
999
1000static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1001 int stream)
1002{
1003 struct snd_soc_dpcm *dpcm;
1004
1005 /* disable any enabled and non active backends */
1006 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1007
1008 struct snd_soc_pcm_runtime *be = dpcm->be;
1009 struct snd_pcm_substream *be_substream =
1010 snd_soc_dpcm_get_substream(be, stream);
1011
1012 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001013 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001014 stream ? "capture" : "playback",
1015 be->dpcm[stream].state);
1016
1017 if (--be->dpcm[stream].users != 0)
1018 continue;
1019
1020 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1021 continue;
1022
1023 soc_pcm_close(be_substream);
1024 be_substream->runtime = NULL;
1025 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1026 }
1027}
1028
1029static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1030{
1031 struct snd_soc_dpcm *dpcm;
1032 int err, count = 0;
1033
1034 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1035 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1036
1037 struct snd_soc_pcm_runtime *be = dpcm->be;
1038 struct snd_pcm_substream *be_substream =
1039 snd_soc_dpcm_get_substream(be, stream);
1040
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001041 if (!be_substream) {
1042 dev_err(be->dev, "ASoC: no backend %s stream\n",
1043 stream ? "capture" : "playback");
1044 continue;
1045 }
1046
Liam Girdwood01d75842012-04-25 12:12:49 +01001047 /* is this op for this BE ? */
1048 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1049 continue;
1050
1051 /* first time the dpcm is open ? */
1052 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001053 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001054 stream ? "capture" : "playback",
1055 be->dpcm[stream].state);
1056
1057 if (be->dpcm[stream].users++ != 0)
1058 continue;
1059
1060 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1061 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1062 continue;
1063
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001064 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1065 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001066
1067 be_substream->runtime = be->dpcm[stream].runtime;
1068 err = soc_pcm_open(be_substream);
1069 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001070 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001071 be->dpcm[stream].users--;
1072 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001073 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001074 stream ? "capture" : "playback",
1075 be->dpcm[stream].state);
1076
1077 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1078 goto unwind;
1079 }
1080
1081 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1082 count++;
1083 }
1084
1085 return count;
1086
1087unwind:
1088 /* disable any enabled and non active backends */
1089 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1090 struct snd_soc_pcm_runtime *be = dpcm->be;
1091 struct snd_pcm_substream *be_substream =
1092 snd_soc_dpcm_get_substream(be, stream);
1093
1094 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1095 continue;
1096
1097 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001098 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001099 stream ? "capture" : "playback",
1100 be->dpcm[stream].state);
1101
1102 if (--be->dpcm[stream].users != 0)
1103 continue;
1104
1105 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1106 continue;
1107
1108 soc_pcm_close(be_substream);
1109 be_substream->runtime = NULL;
1110 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1111 }
1112
1113 return err;
1114}
1115
Mark Brown45c0a182012-05-09 21:46:27 +01001116static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001117{
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1120 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1121 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1122
1123 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1124 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1125 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1126 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1127 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1128 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1129 runtime->hw.rates = cpu_dai_drv->playback.rates;
1130 } else {
1131 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1132 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1133 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1134 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1135 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1136 runtime->hw.rates = cpu_dai_drv->capture.rates;
1137 }
1138}
1139
1140static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1141{
1142 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1143 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1144 int stream = fe_substream->stream, ret = 0;
1145
1146 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1147
1148 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1149 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001150 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001151 goto be_err;
1152 }
1153
Liam Girdwood103d84a2012-11-19 14:39:15 +00001154 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001155
1156 /* start the DAI frontend */
1157 ret = soc_pcm_open(fe_substream);
1158 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001159 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001160 goto unwind;
1161 }
1162
1163 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1164
1165 dpcm_set_fe_runtime(fe_substream);
1166 snd_pcm_limit_hw_rates(runtime);
1167
1168 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1169 return 0;
1170
1171unwind:
1172 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1173be_err:
1174 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1175 return ret;
1176}
1177
1178static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1179{
1180 struct snd_soc_dpcm *dpcm;
1181
1182 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1183 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1184
1185 struct snd_soc_pcm_runtime *be = dpcm->be;
1186 struct snd_pcm_substream *be_substream =
1187 snd_soc_dpcm_get_substream(be, stream);
1188
1189 /* is this op for this BE ? */
1190 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1191 continue;
1192
1193 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001194 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001195 stream ? "capture" : "playback",
1196 be->dpcm[stream].state);
1197
1198 if (--be->dpcm[stream].users != 0)
1199 continue;
1200
1201 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1202 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1203 continue;
1204
Liam Girdwood103d84a2012-11-19 14:39:15 +00001205 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001206 dpcm->fe->dai_link->name);
1207
1208 soc_pcm_close(be_substream);
1209 be_substream->runtime = NULL;
1210
1211 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1212 }
1213 return 0;
1214}
1215
1216static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1217{
1218 struct snd_soc_pcm_runtime *fe = substream->private_data;
1219 int stream = substream->stream;
1220
1221 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1222
1223 /* shutdown the BEs */
1224 dpcm_be_dai_shutdown(fe, substream->stream);
1225
Liam Girdwood103d84a2012-11-19 14:39:15 +00001226 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001227
1228 /* now shutdown the frontend */
1229 soc_pcm_close(substream);
1230
1231 /* run the stream event for each BE */
1232 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1233
1234 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1235 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1236 return 0;
1237}
1238
1239static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1240{
1241 struct snd_soc_dpcm *dpcm;
1242
1243 /* only hw_params backends that are either sinks or sources
1244 * to this frontend DAI */
1245 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1246
1247 struct snd_soc_pcm_runtime *be = dpcm->be;
1248 struct snd_pcm_substream *be_substream =
1249 snd_soc_dpcm_get_substream(be, stream);
1250
1251 /* is this op for this BE ? */
1252 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1253 continue;
1254
1255 /* only free hw when no longer used - check all FEs */
1256 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1257 continue;
1258
1259 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1260 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1261 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001262 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001263 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1264 continue;
1265
Liam Girdwood103d84a2012-11-19 14:39:15 +00001266 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001267 dpcm->fe->dai_link->name);
1268
1269 soc_pcm_hw_free(be_substream);
1270
1271 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1272 }
1273
1274 return 0;
1275}
1276
Mark Brown45c0a182012-05-09 21:46:27 +01001277static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001278{
1279 struct snd_soc_pcm_runtime *fe = substream->private_data;
1280 int err, stream = substream->stream;
1281
1282 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1283 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1284
Liam Girdwood103d84a2012-11-19 14:39:15 +00001285 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001286
1287 /* call hw_free on the frontend */
1288 err = soc_pcm_hw_free(substream);
1289 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001290 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001291 fe->dai_link->name);
1292
1293 /* only hw_params backends that are either sinks or sources
1294 * to this frontend DAI */
1295 err = dpcm_be_dai_hw_free(fe, stream);
1296
1297 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1298 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1299
1300 mutex_unlock(&fe->card->mutex);
1301 return 0;
1302}
1303
1304static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1305{
1306 struct snd_soc_dpcm *dpcm;
1307 int ret;
1308
1309 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1310
1311 struct snd_soc_pcm_runtime *be = dpcm->be;
1312 struct snd_pcm_substream *be_substream =
1313 snd_soc_dpcm_get_substream(be, stream);
1314
1315 /* is this op for this BE ? */
1316 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1317 continue;
1318
1319 /* only allow hw_params() if no connected FEs are running */
1320 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1321 continue;
1322
1323 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1324 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1325 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1326 continue;
1327
Liam Girdwood103d84a2012-11-19 14:39:15 +00001328 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001329 dpcm->fe->dai_link->name);
1330
1331 /* copy params for each dpcm */
1332 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1333 sizeof(struct snd_pcm_hw_params));
1334
1335 /* perform any hw_params fixups */
1336 if (be->dai_link->be_hw_params_fixup) {
1337 ret = be->dai_link->be_hw_params_fixup(be,
1338 &dpcm->hw_params);
1339 if (ret < 0) {
1340 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001341 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001342 ret);
1343 goto unwind;
1344 }
1345 }
1346
1347 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1348 if (ret < 0) {
1349 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001350 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001351 goto unwind;
1352 }
1353
1354 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1355 }
1356 return 0;
1357
1358unwind:
1359 /* disable any enabled and non active backends */
1360 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1361 struct snd_soc_pcm_runtime *be = dpcm->be;
1362 struct snd_pcm_substream *be_substream =
1363 snd_soc_dpcm_get_substream(be, stream);
1364
1365 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1366 continue;
1367
1368 /* only allow hw_free() if no connected FEs are running */
1369 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1370 continue;
1371
1372 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1373 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1374 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1375 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1376 continue;
1377
1378 soc_pcm_hw_free(be_substream);
1379 }
1380
1381 return ret;
1382}
1383
Mark Brown45c0a182012-05-09 21:46:27 +01001384static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1385 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001386{
1387 struct snd_soc_pcm_runtime *fe = substream->private_data;
1388 int ret, stream = substream->stream;
1389
1390 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1391 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1392
1393 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1394 sizeof(struct snd_pcm_hw_params));
1395 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1396 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001397 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001398 goto out;
1399 }
1400
Liam Girdwood103d84a2012-11-19 14:39:15 +00001401 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001402 fe->dai_link->name, params_rate(params),
1403 params_channels(params), params_format(params));
1404
1405 /* call hw_params on the frontend */
1406 ret = soc_pcm_hw_params(substream, params);
1407 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001408 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001409 dpcm_be_dai_hw_free(fe, stream);
1410 } else
1411 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1412
1413out:
1414 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1415 mutex_unlock(&fe->card->mutex);
1416 return ret;
1417}
1418
1419static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1420 struct snd_pcm_substream *substream, int cmd)
1421{
1422 int ret;
1423
Liam Girdwood103d84a2012-11-19 14:39:15 +00001424 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001425 dpcm->fe->dai_link->name, cmd);
1426
1427 ret = soc_pcm_trigger(substream, cmd);
1428 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001429 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001430
1431 return ret;
1432}
1433
Mark Brown45c0a182012-05-09 21:46:27 +01001434static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1435 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001436{
1437 struct snd_soc_dpcm *dpcm;
1438 int ret = 0;
1439
1440 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1441
1442 struct snd_soc_pcm_runtime *be = dpcm->be;
1443 struct snd_pcm_substream *be_substream =
1444 snd_soc_dpcm_get_substream(be, stream);
1445
1446 /* is this op for this BE ? */
1447 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1448 continue;
1449
1450 switch (cmd) {
1451 case SNDRV_PCM_TRIGGER_START:
1452 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1453 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1454 continue;
1455
1456 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1457 if (ret)
1458 return ret;
1459
1460 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1461 break;
1462 case SNDRV_PCM_TRIGGER_RESUME:
1463 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1464 continue;
1465
1466 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1467 if (ret)
1468 return ret;
1469
1470 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1471 break;
1472 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1473 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1474 continue;
1475
1476 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1477 if (ret)
1478 return ret;
1479
1480 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1481 break;
1482 case SNDRV_PCM_TRIGGER_STOP:
1483 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1484 continue;
1485
1486 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1487 continue;
1488
1489 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1490 if (ret)
1491 return ret;
1492
1493 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1494 break;
1495 case SNDRV_PCM_TRIGGER_SUSPEND:
1496 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1497 continue;
1498
1499 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1500 continue;
1501
1502 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1503 if (ret)
1504 return ret;
1505
1506 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1507 break;
1508 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1509 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1510 continue;
1511
1512 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1513 continue;
1514
1515 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1516 if (ret)
1517 return ret;
1518
1519 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1520 break;
1521 }
1522 }
1523
1524 return ret;
1525}
1526EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1527
Mark Brown45c0a182012-05-09 21:46:27 +01001528static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001529{
1530 struct snd_soc_pcm_runtime *fe = substream->private_data;
1531 int stream = substream->stream, ret;
1532 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1533
1534 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1535
1536 switch (trigger) {
1537 case SND_SOC_DPCM_TRIGGER_PRE:
1538 /* call trigger on the frontend before the backend. */
1539
Liam Girdwood103d84a2012-11-19 14:39:15 +00001540 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001541 fe->dai_link->name, cmd);
1542
1543 ret = soc_pcm_trigger(substream, cmd);
1544 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001545 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001546 goto out;
1547 }
1548
1549 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1550 break;
1551 case SND_SOC_DPCM_TRIGGER_POST:
1552 /* call trigger on the frontend after the backend. */
1553
1554 ret = dpcm_be_dai_trigger(fe, substream->stream, 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
Liam Girdwood103d84a2012-11-19 14:39:15 +00001560 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001561 fe->dai_link->name, cmd);
1562
1563 ret = soc_pcm_trigger(substream, cmd);
1564 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001565 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1566 /* bespoke trigger() - handles both FE and BEs */
1567
Liam Girdwood103d84a2012-11-19 14:39:15 +00001568 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001569 fe->dai_link->name, cmd);
1570
1571 ret = soc_pcm_bespoke_trigger(substream, cmd);
1572 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001573 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001574 goto out;
1575 }
1576 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001577 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001578 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001579 fe->dai_link->name);
1580 ret = -EINVAL;
1581 goto out;
1582 }
1583
1584 switch (cmd) {
1585 case SNDRV_PCM_TRIGGER_START:
1586 case SNDRV_PCM_TRIGGER_RESUME:
1587 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1588 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1589 break;
1590 case SNDRV_PCM_TRIGGER_STOP:
1591 case SNDRV_PCM_TRIGGER_SUSPEND:
1592 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1593 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1594 break;
1595 }
1596
1597out:
1598 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1599 return ret;
1600}
1601
1602static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1603{
1604 struct snd_soc_dpcm *dpcm;
1605 int ret = 0;
1606
1607 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1608
1609 struct snd_soc_pcm_runtime *be = dpcm->be;
1610 struct snd_pcm_substream *be_substream =
1611 snd_soc_dpcm_get_substream(be, stream);
1612
1613 /* is this op for this BE ? */
1614 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1615 continue;
1616
1617 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1618 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1619 continue;
1620
Liam Girdwood103d84a2012-11-19 14:39:15 +00001621 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001622 dpcm->fe->dai_link->name);
1623
1624 ret = soc_pcm_prepare(be_substream);
1625 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001626 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001627 ret);
1628 break;
1629 }
1630
1631 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1632 }
1633 return ret;
1634}
1635
Mark Brown45c0a182012-05-09 21:46:27 +01001636static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001637{
1638 struct snd_soc_pcm_runtime *fe = substream->private_data;
1639 int stream = substream->stream, ret = 0;
1640
1641 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1642
Liam Girdwood103d84a2012-11-19 14:39:15 +00001643 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001644
1645 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1646
1647 /* there is no point preparing this FE if there are no BEs */
1648 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001649 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001650 fe->dai_link->name);
1651 ret = -EINVAL;
1652 goto out;
1653 }
1654
1655 ret = dpcm_be_dai_prepare(fe, substream->stream);
1656 if (ret < 0)
1657 goto out;
1658
1659 /* call prepare on the frontend */
1660 ret = soc_pcm_prepare(substream);
1661 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001662 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001663 fe->dai_link->name);
1664 goto out;
1665 }
1666
1667 /* run the stream event for each BE */
1668 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1669 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1670
1671out:
1672 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1673 mutex_unlock(&fe->card->mutex);
1674
1675 return ret;
1676}
1677
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001678static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1679 unsigned int cmd, void *arg)
1680{
1681 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1682 struct snd_soc_platform *platform = rtd->platform;
1683
Mark Brownc5914b02013-10-30 17:47:39 -07001684 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001685 return platform->driver->ops->ioctl(substream, cmd, arg);
1686 return snd_pcm_lib_ioctl(substream, cmd, arg);
1687}
1688
Liam Girdwood618dae12012-04-25 12:12:51 +01001689static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1690{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001691 struct snd_pcm_substream *substream =
1692 snd_soc_dpcm_get_substream(fe, stream);
1693 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001694 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001695
Liam Girdwood103d84a2012-11-19 14:39:15 +00001696 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001697 stream ? "capture" : "playback", fe->dai_link->name);
1698
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001699 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1700 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001701 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001702 fe->dai_link->name);
1703
1704 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1705 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001706 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001707 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001708 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001709 fe->dai_link->name);
1710
1711 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1712 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001713 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001714 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001715
1716 err = dpcm_be_dai_hw_free(fe, stream);
1717 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001718 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001719
1720 err = dpcm_be_dai_shutdown(fe, stream);
1721 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001722 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001723
1724 /* run the stream event for each BE */
1725 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1726
1727 return 0;
1728}
1729
1730static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1731{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001732 struct snd_pcm_substream *substream =
1733 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001734 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001735 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001736 int ret;
1737
Liam Girdwood103d84a2012-11-19 14:39:15 +00001738 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001739 stream ? "capture" : "playback", fe->dai_link->name);
1740
1741 /* Only start the BE if the FE is ready */
1742 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1743 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1744 return -EINVAL;
1745
1746 /* startup must always be called for new BEs */
1747 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001748 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001749 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001750
1751 /* keep going if FE state is > open */
1752 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1753 return 0;
1754
1755 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001756 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001757 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001758
1759 /* keep going if FE state is > hw_params */
1760 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1761 return 0;
1762
1763
1764 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001765 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001766 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001767
1768 /* run the stream event for each BE */
1769 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1770
1771 /* keep going if FE state is > prepare */
1772 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1773 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1774 return 0;
1775
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001776 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1777 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001778 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001779 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001780
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001781 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1782 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001783 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001784 goto hw_free;
1785 }
1786 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001787 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001788 fe->dai_link->name);
1789
1790 ret = dpcm_be_dai_trigger(fe, stream,
1791 SNDRV_PCM_TRIGGER_START);
1792 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001793 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001794 goto hw_free;
1795 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001796 }
1797
1798 return 0;
1799
1800hw_free:
1801 dpcm_be_dai_hw_free(fe, stream);
1802close:
1803 dpcm_be_dai_shutdown(fe, stream);
1804disconnect:
1805 /* disconnect any non started BEs */
1806 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1807 struct snd_soc_pcm_runtime *be = dpcm->be;
1808 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1809 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1810 }
1811
1812 return ret;
1813}
1814
1815static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1816{
1817 int ret;
1818
1819 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1820 ret = dpcm_run_update_startup(fe, stream);
1821 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001822 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001823 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1824
1825 return ret;
1826}
1827
1828static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1829{
1830 int ret;
1831
1832 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1833 ret = dpcm_run_update_shutdown(fe, stream);
1834 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001836 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1837
1838 return ret;
1839}
1840
1841/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1842 * any DAI links.
1843 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001844int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001845{
Liam Girdwood618dae12012-04-25 12:12:51 +01001846 int i, old, new, paths;
1847
Liam Girdwood618dae12012-04-25 12:12:51 +01001848 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1849 for (i = 0; i < card->num_rtd; i++) {
1850 struct snd_soc_dapm_widget_list *list;
1851 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1852
1853 /* make sure link is FE */
1854 if (!fe->dai_link->dynamic)
1855 continue;
1856
1857 /* only check active links */
1858 if (!fe->cpu_dai->active)
1859 continue;
1860
1861 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001862 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001863 fe->dai_link->name);
1864
1865 /* skip if FE doesn't have playback capability */
1866 if (!fe->cpu_dai->driver->playback.channels_min)
1867 goto capture;
1868
1869 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1870 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001871 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001872 fe->dai_link->name, "playback");
1873 mutex_unlock(&card->mutex);
1874 return paths;
1875 }
1876
1877 /* update any new playback paths */
1878 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1879 if (new) {
1880 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1881 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1883 }
1884
1885 /* update any old playback paths */
1886 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1887 if (old) {
1888 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1889 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1890 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1891 }
1892
1893capture:
1894 /* skip if FE doesn't have capture capability */
1895 if (!fe->cpu_dai->driver->capture.channels_min)
1896 continue;
1897
1898 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1899 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001900 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001901 fe->dai_link->name, "capture");
1902 mutex_unlock(&card->mutex);
1903 return paths;
1904 }
1905
1906 /* update any new capture paths */
1907 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1908 if (new) {
1909 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1910 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1912 }
1913
1914 /* update any old capture paths */
1915 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1916 if (old) {
1917 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1918 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1919 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1920 }
1921
1922 dpcm_path_put(&list);
1923 }
1924
1925 mutex_unlock(&card->mutex);
1926 return 0;
1927}
Liam Girdwood01d75842012-04-25 12:12:49 +01001928int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1929{
1930 struct snd_soc_dpcm *dpcm;
1931 struct list_head *clients =
1932 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1933
1934 list_for_each_entry(dpcm, clients, list_be) {
1935
1936 struct snd_soc_pcm_runtime *be = dpcm->be;
1937 struct snd_soc_dai *dai = be->codec_dai;
1938 struct snd_soc_dai_driver *drv = dai->driver;
1939
1940 if (be->dai_link->ignore_suspend)
1941 continue;
1942
Liam Girdwood103d84a2012-11-19 14:39:15 +00001943 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001944
Mark Brownc5914b02013-10-30 17:47:39 -07001945 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1946 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01001947 }
1948
1949 return 0;
1950}
1951
Mark Brown45c0a182012-05-09 21:46:27 +01001952static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001953{
1954 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1955 struct snd_soc_dpcm *dpcm;
1956 struct snd_soc_dapm_widget_list *list;
1957 int ret;
1958 int stream = fe_substream->stream;
1959
1960 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1961 fe->dpcm[stream].runtime = fe_substream->runtime;
1962
1963 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001964 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001965 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001966 }
1967
1968 /* calculate valid and active FE <-> BE dpcms */
1969 dpcm_process_paths(fe, stream, &list, 1);
1970
1971 ret = dpcm_fe_dai_startup(fe_substream);
1972 if (ret < 0) {
1973 /* clean up all links */
1974 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1975 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1976
1977 dpcm_be_disconnect(fe, stream);
1978 fe->dpcm[stream].runtime = NULL;
1979 }
1980
1981 dpcm_clear_pending_state(fe, stream);
1982 dpcm_path_put(&list);
1983 mutex_unlock(&fe->card->mutex);
1984 return ret;
1985}
1986
Mark Brown45c0a182012-05-09 21:46:27 +01001987static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001988{
1989 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1990 struct snd_soc_dpcm *dpcm;
1991 int stream = fe_substream->stream, ret;
1992
1993 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1994 ret = dpcm_fe_dai_shutdown(fe_substream);
1995
1996 /* mark FE's links ready to prune */
1997 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1998 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1999
2000 dpcm_be_disconnect(fe, stream);
2001
2002 fe->dpcm[stream].runtime = NULL;
2003 mutex_unlock(&fe->card->mutex);
2004 return ret;
2005}
2006
Liam Girdwoodddee6272011-06-09 14:45:53 +01002007/* create a new pcm */
2008int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2009{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002010 struct snd_soc_platform *platform = rtd->platform;
2011 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2012 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2013 struct snd_pcm *pcm;
2014 char new_name[64];
2015 int ret = 0, playback = 0, capture = 0;
2016
Liam Girdwood01d75842012-04-25 12:12:49 +01002017 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2018 if (cpu_dai->driver->playback.channels_min)
2019 playback = 1;
2020 if (cpu_dai->driver->capture.channels_min)
2021 capture = 1;
2022 } else {
Mark Brown05679092013-06-01 23:13:53 +01002023 if (codec_dai->driver->playback.channels_min &&
2024 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002025 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002026 if (codec_dai->driver->capture.channels_min &&
2027 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002028 capture = 1;
2029 }
Sangsu Parka5002312012-01-02 17:15:10 +09002030
Fabio Estevamd6bead02013-08-29 10:32:13 -03002031 if (rtd->dai_link->playback_only) {
2032 playback = 1;
2033 capture = 0;
2034 }
2035
2036 if (rtd->dai_link->capture_only) {
2037 playback = 0;
2038 capture = 1;
2039 }
2040
Liam Girdwood01d75842012-04-25 12:12:49 +01002041 /* create the PCM */
2042 if (rtd->dai_link->no_pcm) {
2043 snprintf(new_name, sizeof(new_name), "(%s)",
2044 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002045
Liam Girdwood01d75842012-04-25 12:12:49 +01002046 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2047 playback, capture, &pcm);
2048 } else {
2049 if (rtd->dai_link->dynamic)
2050 snprintf(new_name, sizeof(new_name), "%s (*)",
2051 rtd->dai_link->stream_name);
2052 else
2053 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2054 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002055
Liam Girdwood01d75842012-04-25 12:12:49 +01002056 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2057 capture, &pcm);
2058 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002059 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002060 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002061 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002062 return ret;
2063 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002064 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002065
2066 /* DAPM dai link stream work */
2067 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2068
2069 rtd->pcm = pcm;
2070 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002071
2072 if (rtd->dai_link->no_pcm) {
2073 if (playback)
2074 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2075 if (capture)
2076 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2077 goto out;
2078 }
2079
2080 /* ASoC PCM operations */
2081 if (rtd->dai_link->dynamic) {
2082 rtd->ops.open = dpcm_fe_dai_open;
2083 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2084 rtd->ops.prepare = dpcm_fe_dai_prepare;
2085 rtd->ops.trigger = dpcm_fe_dai_trigger;
2086 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2087 rtd->ops.close = dpcm_fe_dai_close;
2088 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002089 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002090 } else {
2091 rtd->ops.open = soc_pcm_open;
2092 rtd->ops.hw_params = soc_pcm_hw_params;
2093 rtd->ops.prepare = soc_pcm_prepare;
2094 rtd->ops.trigger = soc_pcm_trigger;
2095 rtd->ops.hw_free = soc_pcm_hw_free;
2096 rtd->ops.close = soc_pcm_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 }
2100
Liam Girdwoodddee6272011-06-09 14:45:53 +01002101 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002102 rtd->ops.ack = platform->driver->ops->ack;
2103 rtd->ops.copy = platform->driver->ops->copy;
2104 rtd->ops.silence = platform->driver->ops->silence;
2105 rtd->ops.page = platform->driver->ops->page;
2106 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002107 }
2108
2109 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002110 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002111
2112 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002113 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002114
2115 if (platform->driver->pcm_new) {
2116 ret = platform->driver->pcm_new(rtd);
2117 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002118 dev_err(platform->dev,
2119 "ASoC: pcm constructor failed: %d\n",
2120 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002121 return ret;
2122 }
2123 }
2124
2125 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002126out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002127 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002128 cpu_dai->name);
2129 return ret;
2130}
Liam Girdwood01d75842012-04-25 12:12:49 +01002131
2132/* is the current PCM operation for this FE ? */
2133int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2134{
2135 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2136 return 1;
2137 return 0;
2138}
2139EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2140
2141/* is the current PCM operation for this BE ? */
2142int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2143 struct snd_soc_pcm_runtime *be, int stream)
2144{
2145 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2146 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2147 be->dpcm[stream].runtime_update))
2148 return 1;
2149 return 0;
2150}
2151EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2152
2153/* get the substream for this BE */
2154struct snd_pcm_substream *
2155 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2156{
2157 return be->pcm->streams[stream].substream;
2158}
2159EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2160
2161/* get the BE runtime state */
2162enum snd_soc_dpcm_state
2163 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2164{
2165 return be->dpcm[stream].state;
2166}
2167EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2168
2169/* set the BE runtime state */
2170void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2171 int stream, enum snd_soc_dpcm_state state)
2172{
2173 be->dpcm[stream].state = state;
2174}
2175EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2176
2177/*
2178 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2179 * are not running, paused or suspended for the specified stream direction.
2180 */
2181int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2182 struct snd_soc_pcm_runtime *be, int stream)
2183{
2184 struct snd_soc_dpcm *dpcm;
2185 int state;
2186
2187 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2188
2189 if (dpcm->fe == fe)
2190 continue;
2191
2192 state = dpcm->fe->dpcm[stream].state;
2193 if (state == SND_SOC_DPCM_STATE_START ||
2194 state == SND_SOC_DPCM_STATE_PAUSED ||
2195 state == SND_SOC_DPCM_STATE_SUSPEND)
2196 return 0;
2197 }
2198
2199 /* it's safe to free/stop this BE DAI */
2200 return 1;
2201}
2202EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2203
2204/*
2205 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2206 * running, paused or suspended for the specified stream direction.
2207 */
2208int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2209 struct snd_soc_pcm_runtime *be, int stream)
2210{
2211 struct snd_soc_dpcm *dpcm;
2212 int state;
2213
2214 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2215
2216 if (dpcm->fe == fe)
2217 continue;
2218
2219 state = dpcm->fe->dpcm[stream].state;
2220 if (state == SND_SOC_DPCM_STATE_START ||
2221 state == SND_SOC_DPCM_STATE_PAUSED ||
2222 state == SND_SOC_DPCM_STATE_SUSPEND ||
2223 state == SND_SOC_DPCM_STATE_PREPARE)
2224 return 0;
2225 }
2226
2227 /* it's safe to change hw_params */
2228 return 1;
2229}
2230EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002231
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002232int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2233 int cmd, struct snd_soc_platform *platform)
2234{
Mark Brownc5914b02013-10-30 17:47:39 -07002235 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002236 return platform->driver->ops->trigger(substream, cmd);
2237 return 0;
2238}
2239EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2240
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002241#ifdef CONFIG_DEBUG_FS
2242static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2243{
2244 switch (state) {
2245 case SND_SOC_DPCM_STATE_NEW:
2246 return "new";
2247 case SND_SOC_DPCM_STATE_OPEN:
2248 return "open";
2249 case SND_SOC_DPCM_STATE_HW_PARAMS:
2250 return "hw_params";
2251 case SND_SOC_DPCM_STATE_PREPARE:
2252 return "prepare";
2253 case SND_SOC_DPCM_STATE_START:
2254 return "start";
2255 case SND_SOC_DPCM_STATE_STOP:
2256 return "stop";
2257 case SND_SOC_DPCM_STATE_SUSPEND:
2258 return "suspend";
2259 case SND_SOC_DPCM_STATE_PAUSED:
2260 return "paused";
2261 case SND_SOC_DPCM_STATE_HW_FREE:
2262 return "hw_free";
2263 case SND_SOC_DPCM_STATE_CLOSE:
2264 return "close";
2265 }
2266
2267 return "unknown";
2268}
2269
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002270static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2271 int stream, char *buf, size_t size)
2272{
2273 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2274 struct snd_soc_dpcm *dpcm;
2275 ssize_t offset = 0;
2276
2277 /* FE state */
2278 offset += snprintf(buf + offset, size - offset,
2279 "[%s - %s]\n", fe->dai_link->name,
2280 stream ? "Capture" : "Playback");
2281
2282 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2283 dpcm_state_string(fe->dpcm[stream].state));
2284
2285 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2286 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2287 offset += snprintf(buf + offset, size - offset,
2288 "Hardware Params: "
2289 "Format = %s, Channels = %d, Rate = %d\n",
2290 snd_pcm_format_name(params_format(params)),
2291 params_channels(params),
2292 params_rate(params));
2293
2294 /* BEs state */
2295 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2296
2297 if (list_empty(&fe->dpcm[stream].be_clients)) {
2298 offset += snprintf(buf + offset, size - offset,
2299 " No active DSP links\n");
2300 goto out;
2301 }
2302
2303 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2304 struct snd_soc_pcm_runtime *be = dpcm->be;
2305 params = &dpcm->hw_params;
2306
2307 offset += snprintf(buf + offset, size - offset,
2308 "- %s\n", be->dai_link->name);
2309
2310 offset += snprintf(buf + offset, size - offset,
2311 " State: %s\n",
2312 dpcm_state_string(be->dpcm[stream].state));
2313
2314 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2315 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2316 offset += snprintf(buf + offset, size - offset,
2317 " Hardware Params: "
2318 "Format = %s, Channels = %d, Rate = %d\n",
2319 snd_pcm_format_name(params_format(params)),
2320 params_channels(params),
2321 params_rate(params));
2322 }
2323
2324out:
2325 return offset;
2326}
2327
2328static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2329 size_t count, loff_t *ppos)
2330{
2331 struct snd_soc_pcm_runtime *fe = file->private_data;
2332 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2333 char *buf;
2334
2335 buf = kmalloc(out_count, GFP_KERNEL);
2336 if (!buf)
2337 return -ENOMEM;
2338
2339 if (fe->cpu_dai->driver->playback.channels_min)
2340 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2341 buf + offset, out_count - offset);
2342
2343 if (fe->cpu_dai->driver->capture.channels_min)
2344 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2345 buf + offset, out_count - offset);
2346
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002347 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002348
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002349 kfree(buf);
2350 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002351}
2352
2353static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002354 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002355 .read = dpcm_state_read_file,
2356 .llseek = default_llseek,
2357};
2358
2359int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2360{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002361 if (!rtd->dai_link)
2362 return 0;
2363
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002364 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2365 rtd->card->debugfs_card_root);
2366 if (!rtd->debugfs_dpcm_root) {
2367 dev_dbg(rtd->dev,
2368 "ASoC: Failed to create dpcm debugfs directory %s\n",
2369 rtd->dai_link->name);
2370 return -EINVAL;
2371 }
2372
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002373 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002374 rtd->debugfs_dpcm_root,
2375 rtd, &dpcm_state_fops);
2376
2377 return 0;
2378}
2379#endif