blob: d81b7925176006977f96df74cd61134c41294e41 [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
1041 /* is this op for this BE ? */
1042 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1043 continue;
1044
1045 /* first time the dpcm is open ? */
1046 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001047 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001048 stream ? "capture" : "playback",
1049 be->dpcm[stream].state);
1050
1051 if (be->dpcm[stream].users++ != 0)
1052 continue;
1053
1054 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1055 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1056 continue;
1057
Liam Girdwood103d84a2012-11-19 14:39:15 +00001058 dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001059
1060 be_substream->runtime = be->dpcm[stream].runtime;
1061 err = soc_pcm_open(be_substream);
1062 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001063 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001064 be->dpcm[stream].users--;
1065 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001066 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001067 stream ? "capture" : "playback",
1068 be->dpcm[stream].state);
1069
1070 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1071 goto unwind;
1072 }
1073
1074 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1075 count++;
1076 }
1077
1078 return count;
1079
1080unwind:
1081 /* disable any enabled and non active backends */
1082 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1083 struct snd_soc_pcm_runtime *be = dpcm->be;
1084 struct snd_pcm_substream *be_substream =
1085 snd_soc_dpcm_get_substream(be, stream);
1086
1087 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1088 continue;
1089
1090 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001091 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001092 stream ? "capture" : "playback",
1093 be->dpcm[stream].state);
1094
1095 if (--be->dpcm[stream].users != 0)
1096 continue;
1097
1098 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1099 continue;
1100
1101 soc_pcm_close(be_substream);
1102 be_substream->runtime = NULL;
1103 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1104 }
1105
1106 return err;
1107}
1108
Mark Brown45c0a182012-05-09 21:46:27 +01001109static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001110{
1111 struct snd_pcm_runtime *runtime = substream->runtime;
1112 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1113 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1114 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1115
1116 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1117 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1118 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1119 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1120 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1121 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1122 runtime->hw.rates = cpu_dai_drv->playback.rates;
1123 } else {
1124 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1125 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1126 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1127 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1128 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1129 runtime->hw.rates = cpu_dai_drv->capture.rates;
1130 }
1131}
1132
1133static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1134{
1135 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1136 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1137 int stream = fe_substream->stream, ret = 0;
1138
1139 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1140
1141 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1142 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001143 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001144 goto be_err;
1145 }
1146
Liam Girdwood103d84a2012-11-19 14:39:15 +00001147 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001148
1149 /* start the DAI frontend */
1150 ret = soc_pcm_open(fe_substream);
1151 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001152 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001153 goto unwind;
1154 }
1155
1156 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1157
1158 dpcm_set_fe_runtime(fe_substream);
1159 snd_pcm_limit_hw_rates(runtime);
1160
1161 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1162 return 0;
1163
1164unwind:
1165 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1166be_err:
1167 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1168 return ret;
1169}
1170
1171static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1172{
1173 struct snd_soc_dpcm *dpcm;
1174
1175 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1176 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1177
1178 struct snd_soc_pcm_runtime *be = dpcm->be;
1179 struct snd_pcm_substream *be_substream =
1180 snd_soc_dpcm_get_substream(be, stream);
1181
1182 /* is this op for this BE ? */
1183 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1184 continue;
1185
1186 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001187 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001188 stream ? "capture" : "playback",
1189 be->dpcm[stream].state);
1190
1191 if (--be->dpcm[stream].users != 0)
1192 continue;
1193
1194 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1195 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1196 continue;
1197
Liam Girdwood103d84a2012-11-19 14:39:15 +00001198 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001199 dpcm->fe->dai_link->name);
1200
1201 soc_pcm_close(be_substream);
1202 be_substream->runtime = NULL;
1203
1204 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1205 }
1206 return 0;
1207}
1208
1209static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1210{
1211 struct snd_soc_pcm_runtime *fe = substream->private_data;
1212 int stream = substream->stream;
1213
1214 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1215
1216 /* shutdown the BEs */
1217 dpcm_be_dai_shutdown(fe, substream->stream);
1218
Liam Girdwood103d84a2012-11-19 14:39:15 +00001219 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001220
1221 /* now shutdown the frontend */
1222 soc_pcm_close(substream);
1223
1224 /* run the stream event for each BE */
1225 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1226
1227 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1228 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1229 return 0;
1230}
1231
1232static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1233{
1234 struct snd_soc_dpcm *dpcm;
1235
1236 /* only hw_params backends that are either sinks or sources
1237 * to this frontend DAI */
1238 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1239
1240 struct snd_soc_pcm_runtime *be = dpcm->be;
1241 struct snd_pcm_substream *be_substream =
1242 snd_soc_dpcm_get_substream(be, stream);
1243
1244 /* is this op for this BE ? */
1245 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1246 continue;
1247
1248 /* only free hw when no longer used - check all FEs */
1249 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1250 continue;
1251
1252 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1253 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1254 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001255 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001256 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1257 continue;
1258
Liam Girdwood103d84a2012-11-19 14:39:15 +00001259 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001260 dpcm->fe->dai_link->name);
1261
1262 soc_pcm_hw_free(be_substream);
1263
1264 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1265 }
1266
1267 return 0;
1268}
1269
Mark Brown45c0a182012-05-09 21:46:27 +01001270static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001271{
1272 struct snd_soc_pcm_runtime *fe = substream->private_data;
1273 int err, stream = substream->stream;
1274
1275 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1276 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1277
Liam Girdwood103d84a2012-11-19 14:39:15 +00001278 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001279
1280 /* call hw_free on the frontend */
1281 err = soc_pcm_hw_free(substream);
1282 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001283 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001284 fe->dai_link->name);
1285
1286 /* only hw_params backends that are either sinks or sources
1287 * to this frontend DAI */
1288 err = dpcm_be_dai_hw_free(fe, stream);
1289
1290 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1291 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1292
1293 mutex_unlock(&fe->card->mutex);
1294 return 0;
1295}
1296
1297static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1298{
1299 struct snd_soc_dpcm *dpcm;
1300 int ret;
1301
1302 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1303
1304 struct snd_soc_pcm_runtime *be = dpcm->be;
1305 struct snd_pcm_substream *be_substream =
1306 snd_soc_dpcm_get_substream(be, stream);
1307
1308 /* is this op for this BE ? */
1309 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1310 continue;
1311
1312 /* only allow hw_params() if no connected FEs are running */
1313 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1314 continue;
1315
1316 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1317 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1318 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1319 continue;
1320
Liam Girdwood103d84a2012-11-19 14:39:15 +00001321 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001322 dpcm->fe->dai_link->name);
1323
1324 /* copy params for each dpcm */
1325 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1326 sizeof(struct snd_pcm_hw_params));
1327
1328 /* perform any hw_params fixups */
1329 if (be->dai_link->be_hw_params_fixup) {
1330 ret = be->dai_link->be_hw_params_fixup(be,
1331 &dpcm->hw_params);
1332 if (ret < 0) {
1333 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001334 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001335 ret);
1336 goto unwind;
1337 }
1338 }
1339
1340 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1341 if (ret < 0) {
1342 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001343 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001344 goto unwind;
1345 }
1346
1347 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1348 }
1349 return 0;
1350
1351unwind:
1352 /* disable any enabled and non active backends */
1353 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1354 struct snd_soc_pcm_runtime *be = dpcm->be;
1355 struct snd_pcm_substream *be_substream =
1356 snd_soc_dpcm_get_substream(be, stream);
1357
1358 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1359 continue;
1360
1361 /* only allow hw_free() if no connected FEs are running */
1362 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1363 continue;
1364
1365 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1366 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1367 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1368 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1369 continue;
1370
1371 soc_pcm_hw_free(be_substream);
1372 }
1373
1374 return ret;
1375}
1376
Mark Brown45c0a182012-05-09 21:46:27 +01001377static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1378 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001379{
1380 struct snd_soc_pcm_runtime *fe = substream->private_data;
1381 int ret, stream = substream->stream;
1382
1383 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1384 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1385
1386 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1387 sizeof(struct snd_pcm_hw_params));
1388 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1389 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001390 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001391 goto out;
1392 }
1393
Liam Girdwood103d84a2012-11-19 14:39:15 +00001394 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001395 fe->dai_link->name, params_rate(params),
1396 params_channels(params), params_format(params));
1397
1398 /* call hw_params on the frontend */
1399 ret = soc_pcm_hw_params(substream, params);
1400 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001401 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001402 dpcm_be_dai_hw_free(fe, stream);
1403 } else
1404 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1405
1406out:
1407 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1408 mutex_unlock(&fe->card->mutex);
1409 return ret;
1410}
1411
1412static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1413 struct snd_pcm_substream *substream, int cmd)
1414{
1415 int ret;
1416
Liam Girdwood103d84a2012-11-19 14:39:15 +00001417 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001418 dpcm->fe->dai_link->name, cmd);
1419
1420 ret = soc_pcm_trigger(substream, cmd);
1421 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001422 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001423
1424 return ret;
1425}
1426
Mark Brown45c0a182012-05-09 21:46:27 +01001427static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1428 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001429{
1430 struct snd_soc_dpcm *dpcm;
1431 int ret = 0;
1432
1433 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1434
1435 struct snd_soc_pcm_runtime *be = dpcm->be;
1436 struct snd_pcm_substream *be_substream =
1437 snd_soc_dpcm_get_substream(be, stream);
1438
1439 /* is this op for this BE ? */
1440 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1441 continue;
1442
1443 switch (cmd) {
1444 case SNDRV_PCM_TRIGGER_START:
1445 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1447 continue;
1448
1449 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1450 if (ret)
1451 return ret;
1452
1453 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1454 break;
1455 case SNDRV_PCM_TRIGGER_RESUME:
1456 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1457 continue;
1458
1459 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1460 if (ret)
1461 return ret;
1462
1463 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1464 break;
1465 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1466 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1467 continue;
1468
1469 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1470 if (ret)
1471 return ret;
1472
1473 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1474 break;
1475 case SNDRV_PCM_TRIGGER_STOP:
1476 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1477 continue;
1478
1479 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1480 continue;
1481
1482 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1483 if (ret)
1484 return ret;
1485
1486 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1487 break;
1488 case SNDRV_PCM_TRIGGER_SUSPEND:
1489 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1490 continue;
1491
1492 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1493 continue;
1494
1495 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1496 if (ret)
1497 return ret;
1498
1499 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1500 break;
1501 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1502 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1503 continue;
1504
1505 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1506 continue;
1507
1508 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1509 if (ret)
1510 return ret;
1511
1512 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1513 break;
1514 }
1515 }
1516
1517 return ret;
1518}
1519EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1520
Mark Brown45c0a182012-05-09 21:46:27 +01001521static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001522{
1523 struct snd_soc_pcm_runtime *fe = substream->private_data;
1524 int stream = substream->stream, ret;
1525 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1526
1527 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1528
1529 switch (trigger) {
1530 case SND_SOC_DPCM_TRIGGER_PRE:
1531 /* call trigger on the frontend before the backend. */
1532
Liam Girdwood103d84a2012-11-19 14:39:15 +00001533 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001534 fe->dai_link->name, cmd);
1535
1536 ret = soc_pcm_trigger(substream, cmd);
1537 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001538 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001539 goto out;
1540 }
1541
1542 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1543 break;
1544 case SND_SOC_DPCM_TRIGGER_POST:
1545 /* call trigger on the frontend after the backend. */
1546
1547 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1548 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001549 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001550 goto out;
1551 }
1552
Liam Girdwood103d84a2012-11-19 14:39:15 +00001553 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001554 fe->dai_link->name, cmd);
1555
1556 ret = soc_pcm_trigger(substream, cmd);
1557 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001558 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1559 /* bespoke trigger() - handles both FE and BEs */
1560
Liam Girdwood103d84a2012-11-19 14:39:15 +00001561 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001562 fe->dai_link->name, cmd);
1563
1564 ret = soc_pcm_bespoke_trigger(substream, cmd);
1565 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001566 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001567 goto out;
1568 }
1569 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001570 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001571 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001572 fe->dai_link->name);
1573 ret = -EINVAL;
1574 goto out;
1575 }
1576
1577 switch (cmd) {
1578 case SNDRV_PCM_TRIGGER_START:
1579 case SNDRV_PCM_TRIGGER_RESUME:
1580 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1581 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1582 break;
1583 case SNDRV_PCM_TRIGGER_STOP:
1584 case SNDRV_PCM_TRIGGER_SUSPEND:
1585 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1586 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1587 break;
1588 }
1589
1590out:
1591 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1592 return ret;
1593}
1594
1595static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1596{
1597 struct snd_soc_dpcm *dpcm;
1598 int ret = 0;
1599
1600 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1601
1602 struct snd_soc_pcm_runtime *be = dpcm->be;
1603 struct snd_pcm_substream *be_substream =
1604 snd_soc_dpcm_get_substream(be, stream);
1605
1606 /* is this op for this BE ? */
1607 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1608 continue;
1609
1610 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1611 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1612 continue;
1613
Liam Girdwood103d84a2012-11-19 14:39:15 +00001614 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001615 dpcm->fe->dai_link->name);
1616
1617 ret = soc_pcm_prepare(be_substream);
1618 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001619 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001620 ret);
1621 break;
1622 }
1623
1624 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1625 }
1626 return ret;
1627}
1628
Mark Brown45c0a182012-05-09 21:46:27 +01001629static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001630{
1631 struct snd_soc_pcm_runtime *fe = substream->private_data;
1632 int stream = substream->stream, ret = 0;
1633
1634 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1635
Liam Girdwood103d84a2012-11-19 14:39:15 +00001636 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001637
1638 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1639
1640 /* there is no point preparing this FE if there are no BEs */
1641 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001642 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001643 fe->dai_link->name);
1644 ret = -EINVAL;
1645 goto out;
1646 }
1647
1648 ret = dpcm_be_dai_prepare(fe, substream->stream);
1649 if (ret < 0)
1650 goto out;
1651
1652 /* call prepare on the frontend */
1653 ret = soc_pcm_prepare(substream);
1654 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001655 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001656 fe->dai_link->name);
1657 goto out;
1658 }
1659
1660 /* run the stream event for each BE */
1661 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1662 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1663
1664out:
1665 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1666 mutex_unlock(&fe->card->mutex);
1667
1668 return ret;
1669}
1670
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001671static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1672 unsigned int cmd, void *arg)
1673{
1674 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1675 struct snd_soc_platform *platform = rtd->platform;
1676
Mark Brownc5914b02013-10-30 17:47:39 -07001677 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001678 return platform->driver->ops->ioctl(substream, cmd, arg);
1679 return snd_pcm_lib_ioctl(substream, cmd, arg);
1680}
1681
Liam Girdwood618dae12012-04-25 12:12:51 +01001682static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1683{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001684 struct snd_pcm_substream *substream =
1685 snd_soc_dpcm_get_substream(fe, stream);
1686 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001687 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001688
Liam Girdwood103d84a2012-11-19 14:39:15 +00001689 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001690 stream ? "capture" : "playback", fe->dai_link->name);
1691
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001692 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1693 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001694 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001695 fe->dai_link->name);
1696
1697 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1698 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001699 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001700 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001701 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001702 fe->dai_link->name);
1703
1704 err = dpcm_be_dai_trigger(fe, stream, 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 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001708
1709 err = dpcm_be_dai_hw_free(fe, stream);
1710 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001711 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001712
1713 err = dpcm_be_dai_shutdown(fe, stream);
1714 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001715 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001716
1717 /* run the stream event for each BE */
1718 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1719
1720 return 0;
1721}
1722
1723static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1724{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001725 struct snd_pcm_substream *substream =
1726 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001727 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001728 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001729 int ret;
1730
Liam Girdwood103d84a2012-11-19 14:39:15 +00001731 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001732 stream ? "capture" : "playback", fe->dai_link->name);
1733
1734 /* Only start the BE if the FE is ready */
1735 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1736 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1737 return -EINVAL;
1738
1739 /* startup must always be called for new BEs */
1740 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001741 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001742 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001743
1744 /* keep going if FE state is > open */
1745 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1746 return 0;
1747
1748 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001749 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001750 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001751
1752 /* keep going if FE state is > hw_params */
1753 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1754 return 0;
1755
1756
1757 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001758 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001759 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001760
1761 /* run the stream event for each BE */
1762 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1763
1764 /* keep going if FE state is > prepare */
1765 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1766 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1767 return 0;
1768
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001769 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1770 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001771 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001772 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001773
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001774 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1775 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001776 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001777 goto hw_free;
1778 }
1779 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001780 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001781 fe->dai_link->name);
1782
1783 ret = dpcm_be_dai_trigger(fe, stream,
1784 SNDRV_PCM_TRIGGER_START);
1785 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001786 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001787 goto hw_free;
1788 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001789 }
1790
1791 return 0;
1792
1793hw_free:
1794 dpcm_be_dai_hw_free(fe, stream);
1795close:
1796 dpcm_be_dai_shutdown(fe, stream);
1797disconnect:
1798 /* disconnect any non started BEs */
1799 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1800 struct snd_soc_pcm_runtime *be = dpcm->be;
1801 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1802 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1803 }
1804
1805 return ret;
1806}
1807
1808static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1809{
1810 int ret;
1811
1812 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1813 ret = dpcm_run_update_startup(fe, stream);
1814 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001815 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001816 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1817
1818 return ret;
1819}
1820
1821static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1822{
1823 int ret;
1824
1825 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1826 ret = dpcm_run_update_shutdown(fe, stream);
1827 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001828 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001829 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1830
1831 return ret;
1832}
1833
1834/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1835 * any DAI links.
1836 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001837int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001838{
Liam Girdwood618dae12012-04-25 12:12:51 +01001839 int i, old, new, paths;
1840
Liam Girdwood618dae12012-04-25 12:12:51 +01001841 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1842 for (i = 0; i < card->num_rtd; i++) {
1843 struct snd_soc_dapm_widget_list *list;
1844 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1845
1846 /* make sure link is FE */
1847 if (!fe->dai_link->dynamic)
1848 continue;
1849
1850 /* only check active links */
1851 if (!fe->cpu_dai->active)
1852 continue;
1853
1854 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001855 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001856 fe->dai_link->name);
1857
1858 /* skip if FE doesn't have playback capability */
1859 if (!fe->cpu_dai->driver->playback.channels_min)
1860 goto capture;
1861
1862 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1863 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001864 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001865 fe->dai_link->name, "playback");
1866 mutex_unlock(&card->mutex);
1867 return paths;
1868 }
1869
1870 /* update any new playback paths */
1871 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1872 if (new) {
1873 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1874 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1875 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1876 }
1877
1878 /* update any old playback paths */
1879 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1880 if (old) {
1881 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1882 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1883 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1884 }
1885
1886capture:
1887 /* skip if FE doesn't have capture capability */
1888 if (!fe->cpu_dai->driver->capture.channels_min)
1889 continue;
1890
1891 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1892 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001893 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001894 fe->dai_link->name, "capture");
1895 mutex_unlock(&card->mutex);
1896 return paths;
1897 }
1898
1899 /* update any new capture paths */
1900 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1901 if (new) {
1902 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1903 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1904 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1905 }
1906
1907 /* update any old capture paths */
1908 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1909 if (old) {
1910 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1911 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1912 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1913 }
1914
1915 dpcm_path_put(&list);
1916 }
1917
1918 mutex_unlock(&card->mutex);
1919 return 0;
1920}
Liam Girdwood01d75842012-04-25 12:12:49 +01001921int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1922{
1923 struct snd_soc_dpcm *dpcm;
1924 struct list_head *clients =
1925 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1926
1927 list_for_each_entry(dpcm, clients, list_be) {
1928
1929 struct snd_soc_pcm_runtime *be = dpcm->be;
1930 struct snd_soc_dai *dai = be->codec_dai;
1931 struct snd_soc_dai_driver *drv = dai->driver;
1932
1933 if (be->dai_link->ignore_suspend)
1934 continue;
1935
Liam Girdwood103d84a2012-11-19 14:39:15 +00001936 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001937
Mark Brownc5914b02013-10-30 17:47:39 -07001938 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1939 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01001940 }
1941
1942 return 0;
1943}
1944
Mark Brown45c0a182012-05-09 21:46:27 +01001945static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001946{
1947 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1948 struct snd_soc_dpcm *dpcm;
1949 struct snd_soc_dapm_widget_list *list;
1950 int ret;
1951 int stream = fe_substream->stream;
1952
1953 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1954 fe->dpcm[stream].runtime = fe_substream->runtime;
1955
1956 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001957 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001958 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001959 }
1960
1961 /* calculate valid and active FE <-> BE dpcms */
1962 dpcm_process_paths(fe, stream, &list, 1);
1963
1964 ret = dpcm_fe_dai_startup(fe_substream);
1965 if (ret < 0) {
1966 /* clean up all links */
1967 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1968 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1969
1970 dpcm_be_disconnect(fe, stream);
1971 fe->dpcm[stream].runtime = NULL;
1972 }
1973
1974 dpcm_clear_pending_state(fe, stream);
1975 dpcm_path_put(&list);
1976 mutex_unlock(&fe->card->mutex);
1977 return ret;
1978}
1979
Mark Brown45c0a182012-05-09 21:46:27 +01001980static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001981{
1982 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1983 struct snd_soc_dpcm *dpcm;
1984 int stream = fe_substream->stream, ret;
1985
1986 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1987 ret = dpcm_fe_dai_shutdown(fe_substream);
1988
1989 /* mark FE's links ready to prune */
1990 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1991 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1992
1993 dpcm_be_disconnect(fe, stream);
1994
1995 fe->dpcm[stream].runtime = NULL;
1996 mutex_unlock(&fe->card->mutex);
1997 return ret;
1998}
1999
Liam Girdwoodddee6272011-06-09 14:45:53 +01002000/* create a new pcm */
2001int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2002{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002003 struct snd_soc_platform *platform = rtd->platform;
2004 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2005 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2006 struct snd_pcm *pcm;
2007 char new_name[64];
2008 int ret = 0, playback = 0, capture = 0;
2009
Liam Girdwood01d75842012-04-25 12:12:49 +01002010 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2011 if (cpu_dai->driver->playback.channels_min)
2012 playback = 1;
2013 if (cpu_dai->driver->capture.channels_min)
2014 capture = 1;
2015 } else {
Mark Brown05679092013-06-01 23:13:53 +01002016 if (codec_dai->driver->playback.channels_min &&
2017 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002018 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002019 if (codec_dai->driver->capture.channels_min &&
2020 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002021 capture = 1;
2022 }
Sangsu Parka5002312012-01-02 17:15:10 +09002023
Fabio Estevamd6bead02013-08-29 10:32:13 -03002024 if (rtd->dai_link->playback_only) {
2025 playback = 1;
2026 capture = 0;
2027 }
2028
2029 if (rtd->dai_link->capture_only) {
2030 playback = 0;
2031 capture = 1;
2032 }
2033
Liam Girdwood01d75842012-04-25 12:12:49 +01002034 /* create the PCM */
2035 if (rtd->dai_link->no_pcm) {
2036 snprintf(new_name, sizeof(new_name), "(%s)",
2037 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002038
Liam Girdwood01d75842012-04-25 12:12:49 +01002039 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2040 playback, capture, &pcm);
2041 } else {
2042 if (rtd->dai_link->dynamic)
2043 snprintf(new_name, sizeof(new_name), "%s (*)",
2044 rtd->dai_link->stream_name);
2045 else
2046 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2047 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002048
Liam Girdwood01d75842012-04-25 12:12:49 +01002049 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2050 capture, &pcm);
2051 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002052 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002053 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002054 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002055 return ret;
2056 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002057 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002058
2059 /* DAPM dai link stream work */
2060 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2061
2062 rtd->pcm = pcm;
2063 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002064
2065 if (rtd->dai_link->no_pcm) {
2066 if (playback)
2067 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2068 if (capture)
2069 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2070 goto out;
2071 }
2072
2073 /* ASoC PCM operations */
2074 if (rtd->dai_link->dynamic) {
2075 rtd->ops.open = dpcm_fe_dai_open;
2076 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2077 rtd->ops.prepare = dpcm_fe_dai_prepare;
2078 rtd->ops.trigger = dpcm_fe_dai_trigger;
2079 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2080 rtd->ops.close = dpcm_fe_dai_close;
2081 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002082 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002083 } else {
2084 rtd->ops.open = soc_pcm_open;
2085 rtd->ops.hw_params = soc_pcm_hw_params;
2086 rtd->ops.prepare = soc_pcm_prepare;
2087 rtd->ops.trigger = soc_pcm_trigger;
2088 rtd->ops.hw_free = soc_pcm_hw_free;
2089 rtd->ops.close = soc_pcm_close;
2090 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002091 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002092 }
2093
Liam Girdwoodddee6272011-06-09 14:45:53 +01002094 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002095 rtd->ops.ack = platform->driver->ops->ack;
2096 rtd->ops.copy = platform->driver->ops->copy;
2097 rtd->ops.silence = platform->driver->ops->silence;
2098 rtd->ops.page = platform->driver->ops->page;
2099 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002100 }
2101
2102 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002103 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002104
2105 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002106 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002107
2108 if (platform->driver->pcm_new) {
2109 ret = platform->driver->pcm_new(rtd);
2110 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002111 dev_err(platform->dev,
2112 "ASoC: pcm constructor failed: %d\n",
2113 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002114 return ret;
2115 }
2116 }
2117
2118 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002119out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002120 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002121 cpu_dai->name);
2122 return ret;
2123}
Liam Girdwood01d75842012-04-25 12:12:49 +01002124
2125/* is the current PCM operation for this FE ? */
2126int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2127{
2128 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2129 return 1;
2130 return 0;
2131}
2132EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2133
2134/* is the current PCM operation for this BE ? */
2135int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2136 struct snd_soc_pcm_runtime *be, int stream)
2137{
2138 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2139 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2140 be->dpcm[stream].runtime_update))
2141 return 1;
2142 return 0;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2145
2146/* get the substream for this BE */
2147struct snd_pcm_substream *
2148 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2149{
2150 return be->pcm->streams[stream].substream;
2151}
2152EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2153
2154/* get the BE runtime state */
2155enum snd_soc_dpcm_state
2156 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2157{
2158 return be->dpcm[stream].state;
2159}
2160EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2161
2162/* set the BE runtime state */
2163void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2164 int stream, enum snd_soc_dpcm_state state)
2165{
2166 be->dpcm[stream].state = state;
2167}
2168EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2169
2170/*
2171 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2172 * are not running, paused or suspended for the specified stream direction.
2173 */
2174int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2175 struct snd_soc_pcm_runtime *be, int stream)
2176{
2177 struct snd_soc_dpcm *dpcm;
2178 int state;
2179
2180 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2181
2182 if (dpcm->fe == fe)
2183 continue;
2184
2185 state = dpcm->fe->dpcm[stream].state;
2186 if (state == SND_SOC_DPCM_STATE_START ||
2187 state == SND_SOC_DPCM_STATE_PAUSED ||
2188 state == SND_SOC_DPCM_STATE_SUSPEND)
2189 return 0;
2190 }
2191
2192 /* it's safe to free/stop this BE DAI */
2193 return 1;
2194}
2195EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2196
2197/*
2198 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2199 * running, paused or suspended for the specified stream direction.
2200 */
2201int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2202 struct snd_soc_pcm_runtime *be, int stream)
2203{
2204 struct snd_soc_dpcm *dpcm;
2205 int state;
2206
2207 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2208
2209 if (dpcm->fe == fe)
2210 continue;
2211
2212 state = dpcm->fe->dpcm[stream].state;
2213 if (state == SND_SOC_DPCM_STATE_START ||
2214 state == SND_SOC_DPCM_STATE_PAUSED ||
2215 state == SND_SOC_DPCM_STATE_SUSPEND ||
2216 state == SND_SOC_DPCM_STATE_PREPARE)
2217 return 0;
2218 }
2219
2220 /* it's safe to change hw_params */
2221 return 1;
2222}
2223EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002224
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002225int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2226 int cmd, struct snd_soc_platform *platform)
2227{
Mark Brownc5914b02013-10-30 17:47:39 -07002228 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002229 return platform->driver->ops->trigger(substream, cmd);
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2233
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002234#ifdef CONFIG_DEBUG_FS
2235static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2236{
2237 switch (state) {
2238 case SND_SOC_DPCM_STATE_NEW:
2239 return "new";
2240 case SND_SOC_DPCM_STATE_OPEN:
2241 return "open";
2242 case SND_SOC_DPCM_STATE_HW_PARAMS:
2243 return "hw_params";
2244 case SND_SOC_DPCM_STATE_PREPARE:
2245 return "prepare";
2246 case SND_SOC_DPCM_STATE_START:
2247 return "start";
2248 case SND_SOC_DPCM_STATE_STOP:
2249 return "stop";
2250 case SND_SOC_DPCM_STATE_SUSPEND:
2251 return "suspend";
2252 case SND_SOC_DPCM_STATE_PAUSED:
2253 return "paused";
2254 case SND_SOC_DPCM_STATE_HW_FREE:
2255 return "hw_free";
2256 case SND_SOC_DPCM_STATE_CLOSE:
2257 return "close";
2258 }
2259
2260 return "unknown";
2261}
2262
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002263static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2264 int stream, char *buf, size_t size)
2265{
2266 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2267 struct snd_soc_dpcm *dpcm;
2268 ssize_t offset = 0;
2269
2270 /* FE state */
2271 offset += snprintf(buf + offset, size - offset,
2272 "[%s - %s]\n", fe->dai_link->name,
2273 stream ? "Capture" : "Playback");
2274
2275 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2276 dpcm_state_string(fe->dpcm[stream].state));
2277
2278 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2279 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2280 offset += snprintf(buf + offset, size - offset,
2281 "Hardware Params: "
2282 "Format = %s, Channels = %d, Rate = %d\n",
2283 snd_pcm_format_name(params_format(params)),
2284 params_channels(params),
2285 params_rate(params));
2286
2287 /* BEs state */
2288 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2289
2290 if (list_empty(&fe->dpcm[stream].be_clients)) {
2291 offset += snprintf(buf + offset, size - offset,
2292 " No active DSP links\n");
2293 goto out;
2294 }
2295
2296 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2297 struct snd_soc_pcm_runtime *be = dpcm->be;
2298 params = &dpcm->hw_params;
2299
2300 offset += snprintf(buf + offset, size - offset,
2301 "- %s\n", be->dai_link->name);
2302
2303 offset += snprintf(buf + offset, size - offset,
2304 " State: %s\n",
2305 dpcm_state_string(be->dpcm[stream].state));
2306
2307 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2308 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2309 offset += snprintf(buf + offset, size - offset,
2310 " Hardware Params: "
2311 "Format = %s, Channels = %d, Rate = %d\n",
2312 snd_pcm_format_name(params_format(params)),
2313 params_channels(params),
2314 params_rate(params));
2315 }
2316
2317out:
2318 return offset;
2319}
2320
2321static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2322 size_t count, loff_t *ppos)
2323{
2324 struct snd_soc_pcm_runtime *fe = file->private_data;
2325 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2326 char *buf;
2327
2328 buf = kmalloc(out_count, GFP_KERNEL);
2329 if (!buf)
2330 return -ENOMEM;
2331
2332 if (fe->cpu_dai->driver->playback.channels_min)
2333 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2334 buf + offset, out_count - offset);
2335
2336 if (fe->cpu_dai->driver->capture.channels_min)
2337 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2338 buf + offset, out_count - offset);
2339
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002340 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002341
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002342 kfree(buf);
2343 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002344}
2345
2346static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002347 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002348 .read = dpcm_state_read_file,
2349 .llseek = default_llseek,
2350};
2351
2352int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2353{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002354 if (!rtd->dai_link)
2355 return 0;
2356
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002357 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2358 rtd->card->debugfs_card_root);
2359 if (!rtd->debugfs_dpcm_root) {
2360 dev_dbg(rtd->dev,
2361 "ASoC: Failed to create dpcm debugfs directory %s\n",
2362 rtd->dai_link->name);
2363 return -EINVAL;
2364 }
2365
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002366 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002367 rtd->debugfs_dpcm_root,
2368 rtd, &dpcm_state_fops);
2369
2370 return 0;
2371}
2372#endif