blob: 2b8949647e32a1f2d7dde9ac382c37f58535382b [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
41 *
42 * Sets the substream runtime hardware parameters.
43 */
44int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
46{
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
56 return 0;
57}
58EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
59
Liam Girdwood01d75842012-04-25 12:12:49 +010060/* DPCM stream event, send event to FE and all active BEs. */
61static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
62 int event)
63{
64 struct snd_soc_dpcm *dpcm;
65
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
67
68 struct snd_soc_pcm_runtime *be = dpcm->be;
69
Liam Girdwood103d84a2012-11-19 14:39:15 +000070 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010071 be->dai_link->name, event, dir);
72
73 snd_soc_dapm_stream_event(be, dir, event);
74 }
75
76 snd_soc_dapm_stream_event(fe, dir, event);
77
78 return 0;
79}
80
Dong Aisheng17841022011-08-29 17:15:14 +080081static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010083{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010085 int ret;
86
Dong Aisheng17841022011-08-29 17:15:14 +080087 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010088 !rtd->dai_link->symmetric_rates)
89 return 0;
90
91 /* This can happen if multiple streams are starting simultaneously -
92 * the second can need to get its constraints before the first has
93 * picked a rate. Complain and allow the application to carry on.
94 */
Dong Aisheng17841022011-08-29 17:15:14 +080095 if (!soc_dai->rate) {
96 dev_warn(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +000097 "ASoC: Not enforcing symmetric_rates due to race\n");
Liam Girdwoodddee6272011-06-09 14:45:53 +010098 return 0;
99 }
100
Liam Girdwood103d84a2012-11-19 14:39:15 +0000101 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100102
103 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
104 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +0800105 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100106 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +0800107 dev_err(soc_dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000108 "ASoC: Unable to apply rate symmetry constraint: %d\n",
109 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100110 return ret;
111 }
112
113 return 0;
114}
115
116/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000117 * List of sample sizes that might go over the bus for parameter
118 * application. There ought to be a wildcard sample size for things
119 * like the DAC/ADC resolution to use but there isn't right now.
120 */
121static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200122 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000123};
124
125static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
126 struct snd_soc_dai *dai)
127{
128 int ret, i, bits;
129
130 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
131 bits = dai->driver->playback.sig_bits;
132 else
133 bits = dai->driver->capture.sig_bits;
134
135 if (!bits)
136 return;
137
138 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000139 if (bits >= sample_sizes[i])
140 continue;
141
142 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
143 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000144 if (ret != 0)
145 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000146 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000147 bits, sample_sizes[i], ret);
148 }
149}
150
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100151static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200152 struct snd_soc_pcm_stream *codec_stream,
153 struct snd_soc_pcm_stream *cpu_stream)
154{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100155 struct snd_pcm_hardware *hw = &runtime->hw;
156
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200157 hw->channels_min = max(codec_stream->channels_min,
158 cpu_stream->channels_min);
159 hw->channels_max = min(codec_stream->channels_max,
160 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100161 if (hw->formats)
162 hw->formats &= codec_stream->formats & cpu_stream->formats;
163 else
164 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200165 hw->rates = codec_stream->rates & cpu_stream->rates;
166 if (codec_stream->rates
167 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
168 hw->rates |= cpu_stream->rates;
169 if (cpu_stream->rates
170 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
171 hw->rates |= codec_stream->rates;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100172
Lars-Peter Clausen817873f2014-01-11 10:24:40 +0100173 hw->rate_min = 0;
174 hw->rate_max = UINT_MAX;
175
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100176 snd_pcm_limit_hw_rates(runtime);
177
178 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
179 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
180 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
181 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200182}
183
Mark Brown58ba9b22012-01-16 18:38:51 +0000184/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100185 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
186 * then initialized and any private data can be allocated. This also calls
187 * startup for the cpu DAI, platform, machine and codec DAI.
188 */
189static int soc_pcm_open(struct snd_pcm_substream *substream)
190{
191 struct snd_soc_pcm_runtime *rtd = substream->private_data;
192 struct snd_pcm_runtime *runtime = substream->runtime;
193 struct snd_soc_platform *platform = rtd->platform;
194 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
195 struct snd_soc_dai *codec_dai = rtd->codec_dai;
196 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
197 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
198 int ret = 0;
199
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800200 pinctrl_pm_select_default_state(cpu_dai->dev);
201 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000202 pm_runtime_get_sync(cpu_dai->dev);
203 pm_runtime_get_sync(codec_dai->dev);
204 pm_runtime_get_sync(platform->dev);
205
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100206 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100207
208 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700209 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100210 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
211 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000212 dev_err(cpu_dai->dev, "ASoC: can't open interface"
213 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100214 goto out;
215 }
216 }
217
218 if (platform->driver->ops && platform->driver->ops->open) {
219 ret = platform->driver->ops->open(substream);
220 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000221 dev_err(platform->dev, "ASoC: can't open platform"
222 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100223 goto platform_err;
224 }
225 }
226
Mark Brownc5914b02013-10-30 17:47:39 -0700227 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100228 ret = codec_dai->driver->ops->startup(substream, codec_dai);
229 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000230 dev_err(codec_dai->dev, "ASoC: can't open codec"
231 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100232 goto codec_dai_err;
233 }
234 }
235
236 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
237 ret = rtd->dai_link->ops->startup(substream);
238 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000239 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000240 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100241 goto machine_err;
242 }
243 }
244
Liam Girdwood01d75842012-04-25 12:12:49 +0100245 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
246 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
247 goto dynamic;
248
Liam Girdwoodddee6272011-06-09 14:45:53 +0100249 /* Check that the codec and cpu DAIs are compatible */
250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100251 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200252 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100253 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100254 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200255 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100256 }
257
258 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100259 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000260 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100261 codec_dai->name, cpu_dai->name);
262 goto config_err;
263 }
264 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000265 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100266 codec_dai->name, cpu_dai->name);
267 goto config_err;
268 }
269 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
270 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000271 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100272 codec_dai->name, cpu_dai->name);
273 goto config_err;
274 }
275
Mark Brown58ba9b22012-01-16 18:38:51 +0000276 soc_pcm_apply_msb(substream, codec_dai);
277 soc_pcm_apply_msb(substream, cpu_dai);
278
Liam Girdwoodddee6272011-06-09 14:45:53 +0100279 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800280 if (cpu_dai->active) {
281 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
282 if (ret != 0)
283 goto config_err;
284 }
285
286 if (codec_dai->active) {
287 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100288 if (ret != 0)
289 goto config_err;
290 }
291
Liam Girdwood103d84a2012-11-19 14:39:15 +0000292 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100293 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000294 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
295 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100296 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000297 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100298 runtime->hw.rate_max);
299
Liam Girdwood01d75842012-04-25 12:12:49 +0100300dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
302 cpu_dai->playback_active++;
303 codec_dai->playback_active++;
304 } else {
305 cpu_dai->capture_active++;
306 codec_dai->capture_active++;
307 }
308 cpu_dai->active++;
309 codec_dai->active++;
310 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100311 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100312 return 0;
313
314config_err:
315 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
316 rtd->dai_link->ops->shutdown(substream);
317
318machine_err:
319 if (codec_dai->driver->ops->shutdown)
320 codec_dai->driver->ops->shutdown(substream, codec_dai);
321
322codec_dai_err:
323 if (platform->driver->ops && platform->driver->ops->close)
324 platform->driver->ops->close(substream);
325
326platform_err:
327 if (cpu_dai->driver->ops->shutdown)
328 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
329out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100330 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000331
332 pm_runtime_put(platform->dev);
333 pm_runtime_put(codec_dai->dev);
334 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800335 if (!codec_dai->active)
336 pinctrl_pm_select_sleep_state(codec_dai->dev);
337 if (!cpu_dai->active)
338 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000339
Liam Girdwoodddee6272011-06-09 14:45:53 +0100340 return ret;
341}
342
343/*
344 * Power down the audio subsystem pmdown_time msecs after close is called.
345 * This is to ensure there are no pops or clicks in between any music tracks
346 * due to DAPM power cycling.
347 */
348static void close_delayed_work(struct work_struct *work)
349{
350 struct snd_soc_pcm_runtime *rtd =
351 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
352 struct snd_soc_dai *codec_dai = rtd->codec_dai;
353
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100354 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100355
Liam Girdwood103d84a2012-11-19 14:39:15 +0000356 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100357 codec_dai->driver->playback.stream_name,
358 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600359 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100360
361 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600362 if (rtd->pop_wait == 1) {
363 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800364 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000365 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100366 }
367
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100368 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100369}
370
371/*
372 * Called by ALSA when a PCM substream is closed. Private data can be
373 * freed here. The cpu DAI, codec DAI, machine and platform are also
374 * shutdown.
375 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100376static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100377{
378 struct snd_soc_pcm_runtime *rtd = substream->private_data;
379 struct snd_soc_platform *platform = rtd->platform;
380 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
381 struct snd_soc_dai *codec_dai = rtd->codec_dai;
382 struct snd_soc_codec *codec = rtd->codec;
383
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100384 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100385
386 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
387 cpu_dai->playback_active--;
388 codec_dai->playback_active--;
389 } else {
390 cpu_dai->capture_active--;
391 codec_dai->capture_active--;
392 }
393
394 cpu_dai->active--;
395 codec_dai->active--;
396 codec->active--;
397
Dong Aisheng17841022011-08-29 17:15:14 +0800398 /* clear the corresponding DAIs rate when inactive */
399 if (!cpu_dai->active)
400 cpu_dai->rate = 0;
401
402 if (!codec_dai->active)
403 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200404
Liam Girdwoodddee6272011-06-09 14:45:53 +0100405 /* Muting the DAC suppresses artifacts caused during digital
406 * shutdown, for example from stopping clocks.
407 */
Mark Brownda183962013-02-06 15:44:07 +0000408 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100409
410 if (cpu_dai->driver->ops->shutdown)
411 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
412
413 if (codec_dai->driver->ops->shutdown)
414 codec_dai->driver->ops->shutdown(substream, codec_dai);
415
416 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
417 rtd->dai_link->ops->shutdown(substream);
418
419 if (platform->driver->ops && platform->driver->ops->close)
420 platform->driver->ops->close(substream);
421 cpu_dai->runtime = NULL;
422
423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000424 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200425 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300426 /* powered down playback stream now */
427 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800428 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800429 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300430 } else {
431 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600432 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100433 queue_delayed_work(system_power_efficient_wq,
434 &rtd->delayed_work,
435 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300436 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100437 } else {
438 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800439 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000440 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100441 }
442
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100443 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000444
445 pm_runtime_put(platform->dev);
446 pm_runtime_put(codec_dai->dev);
447 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800448 if (!codec_dai->active)
449 pinctrl_pm_select_sleep_state(codec_dai->dev);
450 if (!cpu_dai->active)
451 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000452
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 return 0;
454}
455
456/*
457 * Called by ALSA when the PCM substream is prepared, can set format, sample
458 * rate, etc. This function is non atomic and can be called multiple times,
459 * it can refer to the runtime info.
460 */
461static int soc_pcm_prepare(struct snd_pcm_substream *substream)
462{
463 struct snd_soc_pcm_runtime *rtd = substream->private_data;
464 struct snd_soc_platform *platform = rtd->platform;
465 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
466 struct snd_soc_dai *codec_dai = rtd->codec_dai;
467 int ret = 0;
468
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100469 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100470
471 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
472 ret = rtd->dai_link->ops->prepare(substream);
473 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000474 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
475 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100476 goto out;
477 }
478 }
479
480 if (platform->driver->ops && platform->driver->ops->prepare) {
481 ret = platform->driver->ops->prepare(substream);
482 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000483 dev_err(platform->dev, "ASoC: platform prepare error:"
484 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 goto out;
486 }
487 }
488
Mark Brownc5914b02013-10-30 17:47:39 -0700489 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100490 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
491 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000492 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000493 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494 goto out;
495 }
496 }
497
Mark Brownc5914b02013-10-30 17:47:39 -0700498 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100499 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
500 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000501 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000502 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100503 goto out;
504 }
505 }
506
507 /* cancel any delayed stream shutdown that is pending */
508 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600509 rtd->pop_wait) {
510 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100511 cancel_delayed_work(&rtd->delayed_work);
512 }
513
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000514 snd_soc_dapm_stream_event(rtd, substream->stream,
515 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100516
Mark Brownda183962013-02-06 15:44:07 +0000517 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100518
519out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100520 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100521 return ret;
522}
523
524/*
525 * Called by ALSA when the hardware params are set by application. This
526 * function can also be called multiple times and can allocate buffers
527 * (using snd_pcm_lib_* ). It's non-atomic.
528 */
529static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
530 struct snd_pcm_hw_params *params)
531{
532 struct snd_soc_pcm_runtime *rtd = substream->private_data;
533 struct snd_soc_platform *platform = rtd->platform;
534 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
535 struct snd_soc_dai *codec_dai = rtd->codec_dai;
536 int ret = 0;
537
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100538 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
540 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
541 ret = rtd->dai_link->ops->hw_params(substream, params);
542 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000543 dev_err(rtd->card->dev, "ASoC: machine hw_params"
544 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545 goto out;
546 }
547 }
548
Mark Brownc5914b02013-10-30 17:47:39 -0700549 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
551 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000552 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
553 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100554 goto codec_err;
555 }
556 }
557
Mark Brownc5914b02013-10-30 17:47:39 -0700558 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100559 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
560 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000561 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000562 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 goto interface_err;
564 }
565 }
566
567 if (platform->driver->ops && platform->driver->ops->hw_params) {
568 ret = platform->driver->ops->hw_params(substream, params);
569 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000570 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000571 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100572 goto platform_err;
573 }
574 }
575
Dong Aisheng17841022011-08-29 17:15:14 +0800576 /* store the rate for each DAIs */
577 cpu_dai->rate = params_rate(params);
578 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100579
580out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100581 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100582 return ret;
583
584platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700585 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100586 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
587
588interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700589 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100590 codec_dai->driver->ops->hw_free(substream, codec_dai);
591
592codec_err:
593 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
594 rtd->dai_link->ops->hw_free(substream);
595
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100596 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100597 return ret;
598}
599
600/*
601 * Frees resources allocated by hw_params, can be called multiple times
602 */
603static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
604{
605 struct snd_soc_pcm_runtime *rtd = substream->private_data;
606 struct snd_soc_platform *platform = rtd->platform;
607 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
608 struct snd_soc_dai *codec_dai = rtd->codec_dai;
609 struct snd_soc_codec *codec = rtd->codec;
610
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100611 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612
613 /* apply codec digital mute */
614 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000615 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616
617 /* free any machine hw params */
618 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
619 rtd->dai_link->ops->hw_free(substream);
620
621 /* free any DMA resources */
622 if (platform->driver->ops && platform->driver->ops->hw_free)
623 platform->driver->ops->hw_free(substream);
624
625 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700626 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100627 codec_dai->driver->ops->hw_free(substream, codec_dai);
628
Mark Brownc5914b02013-10-30 17:47:39 -0700629 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100630 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
631
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100632 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100633 return 0;
634}
635
636static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
637{
638 struct snd_soc_pcm_runtime *rtd = substream->private_data;
639 struct snd_soc_platform *platform = rtd->platform;
640 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
642 int ret;
643
Mark Brownc5914b02013-10-30 17:47:39 -0700644 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100645 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
646 if (ret < 0)
647 return ret;
648 }
649
650 if (platform->driver->ops && platform->driver->ops->trigger) {
651 ret = platform->driver->ops->trigger(substream, cmd);
652 if (ret < 0)
653 return ret;
654 }
655
Mark Brownc5914b02013-10-30 17:47:39 -0700656 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
658 if (ret < 0)
659 return ret;
660 }
661 return 0;
662}
663
Mark Brown45c0a182012-05-09 21:46:27 +0100664static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
665 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100666{
667 struct snd_soc_pcm_runtime *rtd = substream->private_data;
668 struct snd_soc_platform *platform = rtd->platform;
669 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
670 struct snd_soc_dai *codec_dai = rtd->codec_dai;
671 int ret;
672
Mark Brownc5914b02013-10-30 17:47:39 -0700673 if (codec_dai->driver->ops &&
674 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100675 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
676 if (ret < 0)
677 return ret;
678 }
679
Mark Brownc5914b02013-10-30 17:47:39 -0700680 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100681 ret = platform->driver->bespoke_trigger(substream, cmd);
682 if (ret < 0)
683 return ret;
684 }
685
Mark Brownc5914b02013-10-30 17:47:39 -0700686 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100687 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
688 if (ret < 0)
689 return ret;
690 }
691 return 0;
692}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100693/*
694 * soc level wrapper for pointer callback
695 * If cpu_dai, codec_dai, platform driver has the delay callback, than
696 * the runtime->delay will be updated accordingly.
697 */
698static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
699{
700 struct snd_soc_pcm_runtime *rtd = substream->private_data;
701 struct snd_soc_platform *platform = rtd->platform;
702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
703 struct snd_soc_dai *codec_dai = rtd->codec_dai;
704 struct snd_pcm_runtime *runtime = substream->runtime;
705 snd_pcm_uframes_t offset = 0;
706 snd_pcm_sframes_t delay = 0;
707
708 if (platform->driver->ops && platform->driver->ops->pointer)
709 offset = platform->driver->ops->pointer(substream);
710
Mark Brownc5914b02013-10-30 17:47:39 -0700711 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100712 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
713
Mark Brownc5914b02013-10-30 17:47:39 -0700714 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100715 delay += codec_dai->driver->ops->delay(substream, codec_dai);
716
717 if (platform->driver->delay)
718 delay += platform->driver->delay(substream, codec_dai);
719
720 runtime->delay = delay;
721
722 return offset;
723}
724
Liam Girdwood01d75842012-04-25 12:12:49 +0100725/* connect a FE and BE */
726static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
727 struct snd_soc_pcm_runtime *be, int stream)
728{
729 struct snd_soc_dpcm *dpcm;
730
731 /* only add new dpcms */
732 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
733 if (dpcm->be == be && dpcm->fe == fe)
734 return 0;
735 }
736
737 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
738 if (!dpcm)
739 return -ENOMEM;
740
741 dpcm->be = be;
742 dpcm->fe = fe;
743 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
744 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
745 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
746 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
747
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300748 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100749 stream ? "capture" : "playback", fe->dai_link->name,
750 stream ? "<-" : "->", be->dai_link->name);
751
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100752#ifdef CONFIG_DEBUG_FS
753 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
754 fe->debugfs_dpcm_root, &dpcm->state);
755#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100756 return 1;
757}
758
759/* reparent a BE onto another FE */
760static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
761 struct snd_soc_pcm_runtime *be, int stream)
762{
763 struct snd_soc_dpcm *dpcm;
764 struct snd_pcm_substream *fe_substream, *be_substream;
765
766 /* reparent if BE is connected to other FEs */
767 if (!be->dpcm[stream].users)
768 return;
769
770 be_substream = snd_soc_dpcm_get_substream(be, stream);
771
772 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
773 if (dpcm->fe == fe)
774 continue;
775
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300776 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100777 stream ? "capture" : "playback",
778 dpcm->fe->dai_link->name,
779 stream ? "<-" : "->", dpcm->be->dai_link->name);
780
781 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
782 be_substream->runtime = fe_substream->runtime;
783 break;
784 }
785}
786
787/* disconnect a BE and FE */
788static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
789{
790 struct snd_soc_dpcm *dpcm, *d;
791
792 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000793 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100794 stream ? "capture" : "playback",
795 dpcm->be->dai_link->name);
796
797 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
798 continue;
799
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300800 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100801 stream ? "capture" : "playback", fe->dai_link->name,
802 stream ? "<-" : "->", dpcm->be->dai_link->name);
803
804 /* BEs still alive need new FE */
805 dpcm_be_reparent(fe, dpcm->be, stream);
806
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100807#ifdef CONFIG_DEBUG_FS
808 debugfs_remove(dpcm->debugfs_state);
809#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100810 list_del(&dpcm->list_be);
811 list_del(&dpcm->list_fe);
812 kfree(dpcm);
813 }
814}
815
816/* get BE for DAI widget and stream */
817static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
818 struct snd_soc_dapm_widget *widget, int stream)
819{
820 struct snd_soc_pcm_runtime *be;
821 int i;
822
823 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
824 for (i = 0; i < card->num_links; i++) {
825 be = &card->rtd[i];
826
Liam Girdwood35ea0652012-06-05 19:26:59 +0100827 if (!be->dai_link->no_pcm)
828 continue;
829
Liam Girdwood01d75842012-04-25 12:12:49 +0100830 if (be->cpu_dai->playback_widget == widget ||
831 be->codec_dai->playback_widget == widget)
832 return be;
833 }
834 } else {
835
836 for (i = 0; i < card->num_links; i++) {
837 be = &card->rtd[i];
838
Liam Girdwood35ea0652012-06-05 19:26:59 +0100839 if (!be->dai_link->no_pcm)
840 continue;
841
Liam Girdwood01d75842012-04-25 12:12:49 +0100842 if (be->cpu_dai->capture_widget == widget ||
843 be->codec_dai->capture_widget == widget)
844 return be;
845 }
846 }
847
Liam Girdwood103d84a2012-11-19 14:39:15 +0000848 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100849 stream ? "capture" : "playback", widget->name);
850 return NULL;
851}
852
853static inline struct snd_soc_dapm_widget *
854 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
855{
856 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
857 return rtd->cpu_dai->playback_widget;
858 else
859 return rtd->cpu_dai->capture_widget;
860}
861
862static inline struct snd_soc_dapm_widget *
863 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
864{
865 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
866 return rtd->codec_dai->playback_widget;
867 else
868 return rtd->codec_dai->capture_widget;
869}
870
871static int widget_in_list(struct snd_soc_dapm_widget_list *list,
872 struct snd_soc_dapm_widget *widget)
873{
874 int i;
875
876 for (i = 0; i < list->num_widgets; i++) {
877 if (widget == list->widgets[i])
878 return 1;
879 }
880
881 return 0;
882}
883
884static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
885 int stream, struct snd_soc_dapm_widget_list **list_)
886{
887 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
888 struct snd_soc_dapm_widget_list *list;
889 int paths;
890
891 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
892 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
893 if (list == NULL)
894 return -ENOMEM;
895
896 /* get number of valid DAI paths and their widgets */
897 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
898
Liam Girdwood103d84a2012-11-19 14:39:15 +0000899 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100900 stream ? "capture" : "playback");
901
902 *list_ = list;
903 return paths;
904}
905
906static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
907{
908 kfree(*list);
909}
910
911static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
912 struct snd_soc_dapm_widget_list **list_)
913{
914 struct snd_soc_dpcm *dpcm;
915 struct snd_soc_dapm_widget_list *list = *list_;
916 struct snd_soc_dapm_widget *widget;
917 int prune = 0;
918
919 /* Destroy any old FE <--> BE connections */
920 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
921
922 /* is there a valid CPU DAI widget for this BE */
923 widget = rtd_get_cpu_widget(dpcm->be, stream);
924
925 /* prune the BE if it's no longer in our active list */
926 if (widget && widget_in_list(list, widget))
927 continue;
928
929 /* is there a valid CODEC DAI widget for this BE */
930 widget = rtd_get_codec_widget(dpcm->be, stream);
931
932 /* prune the BE if it's no longer in our active list */
933 if (widget && widget_in_list(list, widget))
934 continue;
935
Liam Girdwood103d84a2012-11-19 14:39:15 +0000936 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100937 stream ? "capture" : "playback",
938 dpcm->be->dai_link->name, fe->dai_link->name);
939 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
940 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
941 prune++;
942 }
943
Liam Girdwood103d84a2012-11-19 14:39:15 +0000944 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +0100945 return prune;
946}
947
948static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
949 struct snd_soc_dapm_widget_list **list_)
950{
951 struct snd_soc_card *card = fe->card;
952 struct snd_soc_dapm_widget_list *list = *list_;
953 struct snd_soc_pcm_runtime *be;
954 int i, new = 0, err;
955
956 /* Create any new FE <--> BE connections */
957 for (i = 0; i < list->num_widgets; i++) {
958
Mark Brown46162742013-06-05 19:36:11 +0100959 switch (list->widgets[i]->id) {
960 case snd_soc_dapm_dai_in:
961 case snd_soc_dapm_dai_out:
962 break;
963 default:
Liam Girdwood01d75842012-04-25 12:12:49 +0100964 continue;
Mark Brown46162742013-06-05 19:36:11 +0100965 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100966
967 /* is there a valid BE rtd for this widget */
968 be = dpcm_get_be(card, list->widgets[i], stream);
969 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000970 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100971 list->widgets[i]->name);
972 continue;
973 }
974
975 /* make sure BE is a real BE */
976 if (!be->dai_link->no_pcm)
977 continue;
978
979 /* don't connect if FE is not running */
980 if (!fe->dpcm[stream].runtime)
981 continue;
982
983 /* newly connected FE and BE */
984 err = dpcm_be_connect(fe, be, stream);
985 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000986 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100987 list->widgets[i]->name);
988 break;
989 } else if (err == 0) /* already connected */
990 continue;
991
992 /* new */
993 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
994 new++;
995 }
996
Liam Girdwood103d84a2012-11-19 14:39:15 +0000997 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +0100998 return new;
999}
1000
1001/*
1002 * Find the corresponding BE DAIs that source or sink audio to this
1003 * FE substream.
1004 */
1005static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1006 int stream, struct snd_soc_dapm_widget_list **list, int new)
1007{
1008 if (new)
1009 return dpcm_add_paths(fe, stream, list);
1010 else
1011 return dpcm_prune_paths(fe, stream, list);
1012}
1013
1014static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1015{
1016 struct snd_soc_dpcm *dpcm;
1017
1018 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1019 dpcm->be->dpcm[stream].runtime_update =
1020 SND_SOC_DPCM_UPDATE_NO;
1021}
1022
1023static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1024 int stream)
1025{
1026 struct snd_soc_dpcm *dpcm;
1027
1028 /* disable any enabled and non active backends */
1029 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1030
1031 struct snd_soc_pcm_runtime *be = dpcm->be;
1032 struct snd_pcm_substream *be_substream =
1033 snd_soc_dpcm_get_substream(be, stream);
1034
1035 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001036 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001037 stream ? "capture" : "playback",
1038 be->dpcm[stream].state);
1039
1040 if (--be->dpcm[stream].users != 0)
1041 continue;
1042
1043 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1044 continue;
1045
1046 soc_pcm_close(be_substream);
1047 be_substream->runtime = NULL;
1048 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1049 }
1050}
1051
1052static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1053{
1054 struct snd_soc_dpcm *dpcm;
1055 int err, count = 0;
1056
1057 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1058 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1059
1060 struct snd_soc_pcm_runtime *be = dpcm->be;
1061 struct snd_pcm_substream *be_substream =
1062 snd_soc_dpcm_get_substream(be, stream);
1063
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001064 if (!be_substream) {
1065 dev_err(be->dev, "ASoC: no backend %s stream\n",
1066 stream ? "capture" : "playback");
1067 continue;
1068 }
1069
Liam Girdwood01d75842012-04-25 12:12:49 +01001070 /* is this op for this BE ? */
1071 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1072 continue;
1073
1074 /* first time the dpcm is open ? */
1075 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001076 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001077 stream ? "capture" : "playback",
1078 be->dpcm[stream].state);
1079
1080 if (be->dpcm[stream].users++ != 0)
1081 continue;
1082
1083 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1084 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1085 continue;
1086
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001087 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1088 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001089
1090 be_substream->runtime = be->dpcm[stream].runtime;
1091 err = soc_pcm_open(be_substream);
1092 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001093 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001094 be->dpcm[stream].users--;
1095 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001096 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001097 stream ? "capture" : "playback",
1098 be->dpcm[stream].state);
1099
1100 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1101 goto unwind;
1102 }
1103
1104 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1105 count++;
1106 }
1107
1108 return count;
1109
1110unwind:
1111 /* disable any enabled and non active backends */
1112 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1113 struct snd_soc_pcm_runtime *be = dpcm->be;
1114 struct snd_pcm_substream *be_substream =
1115 snd_soc_dpcm_get_substream(be, stream);
1116
1117 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1118 continue;
1119
1120 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001121 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001122 stream ? "capture" : "playback",
1123 be->dpcm[stream].state);
1124
1125 if (--be->dpcm[stream].users != 0)
1126 continue;
1127
1128 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1129 continue;
1130
1131 soc_pcm_close(be_substream);
1132 be_substream->runtime = NULL;
1133 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1134 }
1135
1136 return err;
1137}
1138
Mark Brown45c0a182012-05-09 21:46:27 +01001139static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001140{
1141 struct snd_pcm_runtime *runtime = substream->runtime;
1142 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1143 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1144 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1145
1146 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1147 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1148 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1149 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1150 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1151 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1152 runtime->hw.rates = cpu_dai_drv->playback.rates;
1153 } else {
1154 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1155 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1156 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1157 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1158 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1159 runtime->hw.rates = cpu_dai_drv->capture.rates;
1160 }
1161}
1162
1163static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1164{
1165 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1166 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1167 int stream = fe_substream->stream, ret = 0;
1168
1169 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1170
1171 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1172 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001173 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001174 goto be_err;
1175 }
1176
Liam Girdwood103d84a2012-11-19 14:39:15 +00001177 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001178
1179 /* start the DAI frontend */
1180 ret = soc_pcm_open(fe_substream);
1181 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001182 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001183 goto unwind;
1184 }
1185
1186 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1187
1188 dpcm_set_fe_runtime(fe_substream);
1189 snd_pcm_limit_hw_rates(runtime);
1190
1191 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1192 return 0;
1193
1194unwind:
1195 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1196be_err:
1197 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1198 return ret;
1199}
1200
1201static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1202{
1203 struct snd_soc_dpcm *dpcm;
1204
1205 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1206 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1207
1208 struct snd_soc_pcm_runtime *be = dpcm->be;
1209 struct snd_pcm_substream *be_substream =
1210 snd_soc_dpcm_get_substream(be, stream);
1211
1212 /* is this op for this BE ? */
1213 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1214 continue;
1215
1216 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001217 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001218 stream ? "capture" : "playback",
1219 be->dpcm[stream].state);
1220
1221 if (--be->dpcm[stream].users != 0)
1222 continue;
1223
1224 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1225 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1226 continue;
1227
Liam Girdwood103d84a2012-11-19 14:39:15 +00001228 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001229 dpcm->fe->dai_link->name);
1230
1231 soc_pcm_close(be_substream);
1232 be_substream->runtime = NULL;
1233
1234 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1235 }
1236 return 0;
1237}
1238
1239static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1240{
1241 struct snd_soc_pcm_runtime *fe = substream->private_data;
1242 int stream = substream->stream;
1243
1244 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1245
1246 /* shutdown the BEs */
1247 dpcm_be_dai_shutdown(fe, substream->stream);
1248
Liam Girdwood103d84a2012-11-19 14:39:15 +00001249 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001250
1251 /* now shutdown the frontend */
1252 soc_pcm_close(substream);
1253
1254 /* run the stream event for each BE */
1255 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1256
1257 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1258 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1259 return 0;
1260}
1261
1262static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1263{
1264 struct snd_soc_dpcm *dpcm;
1265
1266 /* only hw_params backends that are either sinks or sources
1267 * to this frontend DAI */
1268 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1269
1270 struct snd_soc_pcm_runtime *be = dpcm->be;
1271 struct snd_pcm_substream *be_substream =
1272 snd_soc_dpcm_get_substream(be, stream);
1273
1274 /* is this op for this BE ? */
1275 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1276 continue;
1277
1278 /* only free hw when no longer used - check all FEs */
1279 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1280 continue;
1281
1282 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1283 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1284 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001285 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001286 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1287 continue;
1288
Liam Girdwood103d84a2012-11-19 14:39:15 +00001289 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001290 dpcm->fe->dai_link->name);
1291
1292 soc_pcm_hw_free(be_substream);
1293
1294 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1295 }
1296
1297 return 0;
1298}
1299
Mark Brown45c0a182012-05-09 21:46:27 +01001300static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001301{
1302 struct snd_soc_pcm_runtime *fe = substream->private_data;
1303 int err, stream = substream->stream;
1304
1305 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1306 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1307
Liam Girdwood103d84a2012-11-19 14:39:15 +00001308 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001309
1310 /* call hw_free on the frontend */
1311 err = soc_pcm_hw_free(substream);
1312 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001313 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001314 fe->dai_link->name);
1315
1316 /* only hw_params backends that are either sinks or sources
1317 * to this frontend DAI */
1318 err = dpcm_be_dai_hw_free(fe, stream);
1319
1320 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1321 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1322
1323 mutex_unlock(&fe->card->mutex);
1324 return 0;
1325}
1326
1327static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1328{
1329 struct snd_soc_dpcm *dpcm;
1330 int ret;
1331
1332 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1333
1334 struct snd_soc_pcm_runtime *be = dpcm->be;
1335 struct snd_pcm_substream *be_substream =
1336 snd_soc_dpcm_get_substream(be, stream);
1337
1338 /* is this op for this BE ? */
1339 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1340 continue;
1341
1342 /* only allow hw_params() if no connected FEs are running */
1343 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1344 continue;
1345
1346 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1347 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1348 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1349 continue;
1350
Liam Girdwood103d84a2012-11-19 14:39:15 +00001351 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001352 dpcm->fe->dai_link->name);
1353
1354 /* copy params for each dpcm */
1355 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1356 sizeof(struct snd_pcm_hw_params));
1357
1358 /* perform any hw_params fixups */
1359 if (be->dai_link->be_hw_params_fixup) {
1360 ret = be->dai_link->be_hw_params_fixup(be,
1361 &dpcm->hw_params);
1362 if (ret < 0) {
1363 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001364 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001365 ret);
1366 goto unwind;
1367 }
1368 }
1369
1370 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1371 if (ret < 0) {
1372 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001373 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001374 goto unwind;
1375 }
1376
1377 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1378 }
1379 return 0;
1380
1381unwind:
1382 /* disable any enabled and non active backends */
1383 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1384 struct snd_soc_pcm_runtime *be = dpcm->be;
1385 struct snd_pcm_substream *be_substream =
1386 snd_soc_dpcm_get_substream(be, stream);
1387
1388 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1389 continue;
1390
1391 /* only allow hw_free() if no connected FEs are running */
1392 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1393 continue;
1394
1395 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1396 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1397 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1398 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1399 continue;
1400
1401 soc_pcm_hw_free(be_substream);
1402 }
1403
1404 return ret;
1405}
1406
Mark Brown45c0a182012-05-09 21:46:27 +01001407static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1408 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001409{
1410 struct snd_soc_pcm_runtime *fe = substream->private_data;
1411 int ret, stream = substream->stream;
1412
1413 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1414 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1415
1416 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1417 sizeof(struct snd_pcm_hw_params));
1418 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1419 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001420 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001421 goto out;
1422 }
1423
Liam Girdwood103d84a2012-11-19 14:39:15 +00001424 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001425 fe->dai_link->name, params_rate(params),
1426 params_channels(params), params_format(params));
1427
1428 /* call hw_params on the frontend */
1429 ret = soc_pcm_hw_params(substream, params);
1430 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001431 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001432 dpcm_be_dai_hw_free(fe, stream);
1433 } else
1434 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1435
1436out:
1437 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1438 mutex_unlock(&fe->card->mutex);
1439 return ret;
1440}
1441
1442static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1443 struct snd_pcm_substream *substream, int cmd)
1444{
1445 int ret;
1446
Liam Girdwood103d84a2012-11-19 14:39:15 +00001447 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001448 dpcm->fe->dai_link->name, cmd);
1449
1450 ret = soc_pcm_trigger(substream, cmd);
1451 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001452 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001453
1454 return ret;
1455}
1456
Mark Brown45c0a182012-05-09 21:46:27 +01001457static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1458 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001459{
1460 struct snd_soc_dpcm *dpcm;
1461 int ret = 0;
1462
1463 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1464
1465 struct snd_soc_pcm_runtime *be = dpcm->be;
1466 struct snd_pcm_substream *be_substream =
1467 snd_soc_dpcm_get_substream(be, stream);
1468
1469 /* is this op for this BE ? */
1470 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1471 continue;
1472
1473 switch (cmd) {
1474 case SNDRV_PCM_TRIGGER_START:
1475 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1476 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1477 continue;
1478
1479 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1480 if (ret)
1481 return ret;
1482
1483 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1484 break;
1485 case SNDRV_PCM_TRIGGER_RESUME:
1486 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1487 continue;
1488
1489 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1490 if (ret)
1491 return ret;
1492
1493 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1494 break;
1495 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1496 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1497 continue;
1498
1499 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1500 if (ret)
1501 return ret;
1502
1503 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1504 break;
1505 case SNDRV_PCM_TRIGGER_STOP:
1506 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1507 continue;
1508
1509 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1510 continue;
1511
1512 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1513 if (ret)
1514 return ret;
1515
1516 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1517 break;
1518 case SNDRV_PCM_TRIGGER_SUSPEND:
1519 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1520 continue;
1521
1522 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1523 continue;
1524
1525 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1526 if (ret)
1527 return ret;
1528
1529 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1530 break;
1531 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1532 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1533 continue;
1534
1535 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1536 continue;
1537
1538 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1539 if (ret)
1540 return ret;
1541
1542 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1543 break;
1544 }
1545 }
1546
1547 return ret;
1548}
1549EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1550
Mark Brown45c0a182012-05-09 21:46:27 +01001551static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001552{
1553 struct snd_soc_pcm_runtime *fe = substream->private_data;
1554 int stream = substream->stream, ret;
1555 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1556
1557 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1558
1559 switch (trigger) {
1560 case SND_SOC_DPCM_TRIGGER_PRE:
1561 /* call trigger on the frontend before the backend. */
1562
Liam Girdwood103d84a2012-11-19 14:39:15 +00001563 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001564 fe->dai_link->name, cmd);
1565
1566 ret = soc_pcm_trigger(substream, cmd);
1567 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001568 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001569 goto out;
1570 }
1571
1572 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1573 break;
1574 case SND_SOC_DPCM_TRIGGER_POST:
1575 /* call trigger on the frontend after the backend. */
1576
1577 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1578 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001579 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001580 goto out;
1581 }
1582
Liam Girdwood103d84a2012-11-19 14:39:15 +00001583 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001584 fe->dai_link->name, cmd);
1585
1586 ret = soc_pcm_trigger(substream, cmd);
1587 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001588 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1589 /* bespoke trigger() - handles both FE and BEs */
1590
Liam Girdwood103d84a2012-11-19 14:39:15 +00001591 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001592 fe->dai_link->name, cmd);
1593
1594 ret = soc_pcm_bespoke_trigger(substream, cmd);
1595 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001596 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001597 goto out;
1598 }
1599 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001600 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001601 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001602 fe->dai_link->name);
1603 ret = -EINVAL;
1604 goto out;
1605 }
1606
1607 switch (cmd) {
1608 case SNDRV_PCM_TRIGGER_START:
1609 case SNDRV_PCM_TRIGGER_RESUME:
1610 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1611 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1612 break;
1613 case SNDRV_PCM_TRIGGER_STOP:
1614 case SNDRV_PCM_TRIGGER_SUSPEND:
1615 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1616 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1617 break;
1618 }
1619
1620out:
1621 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1622 return ret;
1623}
1624
1625static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1626{
1627 struct snd_soc_dpcm *dpcm;
1628 int ret = 0;
1629
1630 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1631
1632 struct snd_soc_pcm_runtime *be = dpcm->be;
1633 struct snd_pcm_substream *be_substream =
1634 snd_soc_dpcm_get_substream(be, stream);
1635
1636 /* is this op for this BE ? */
1637 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1638 continue;
1639
1640 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1641 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1642 continue;
1643
Liam Girdwood103d84a2012-11-19 14:39:15 +00001644 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001645 dpcm->fe->dai_link->name);
1646
1647 ret = soc_pcm_prepare(be_substream);
1648 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001649 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001650 ret);
1651 break;
1652 }
1653
1654 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1655 }
1656 return ret;
1657}
1658
Mark Brown45c0a182012-05-09 21:46:27 +01001659static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001660{
1661 struct snd_soc_pcm_runtime *fe = substream->private_data;
1662 int stream = substream->stream, ret = 0;
1663
1664 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1665
Liam Girdwood103d84a2012-11-19 14:39:15 +00001666 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001667
1668 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1669
1670 /* there is no point preparing this FE if there are no BEs */
1671 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001672 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001673 fe->dai_link->name);
1674 ret = -EINVAL;
1675 goto out;
1676 }
1677
1678 ret = dpcm_be_dai_prepare(fe, substream->stream);
1679 if (ret < 0)
1680 goto out;
1681
1682 /* call prepare on the frontend */
1683 ret = soc_pcm_prepare(substream);
1684 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001685 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001686 fe->dai_link->name);
1687 goto out;
1688 }
1689
1690 /* run the stream event for each BE */
1691 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1692 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1693
1694out:
1695 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1696 mutex_unlock(&fe->card->mutex);
1697
1698 return ret;
1699}
1700
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001701static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1702 unsigned int cmd, void *arg)
1703{
1704 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1705 struct snd_soc_platform *platform = rtd->platform;
1706
Mark Brownc5914b02013-10-30 17:47:39 -07001707 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001708 return platform->driver->ops->ioctl(substream, cmd, arg);
1709 return snd_pcm_lib_ioctl(substream, cmd, arg);
1710}
1711
Liam Girdwood618dae12012-04-25 12:12:51 +01001712static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1713{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001714 struct snd_pcm_substream *substream =
1715 snd_soc_dpcm_get_substream(fe, stream);
1716 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001717 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001718
Liam Girdwood103d84a2012-11-19 14:39:15 +00001719 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001720 stream ? "capture" : "playback", fe->dai_link->name);
1721
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001722 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1723 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001724 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001725 fe->dai_link->name);
1726
1727 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1728 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001729 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001730 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001731 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001732 fe->dai_link->name);
1733
1734 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1735 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001736 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001737 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001738
1739 err = dpcm_be_dai_hw_free(fe, stream);
1740 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001741 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001742
1743 err = dpcm_be_dai_shutdown(fe, stream);
1744 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001745 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001746
1747 /* run the stream event for each BE */
1748 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1749
1750 return 0;
1751}
1752
1753static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1754{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001755 struct snd_pcm_substream *substream =
1756 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001757 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001758 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001759 int ret;
1760
Liam Girdwood103d84a2012-11-19 14:39:15 +00001761 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001762 stream ? "capture" : "playback", fe->dai_link->name);
1763
1764 /* Only start the BE if the FE is ready */
1765 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1766 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1767 return -EINVAL;
1768
1769 /* startup must always be called for new BEs */
1770 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001771 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001772 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001773
1774 /* keep going if FE state is > open */
1775 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1776 return 0;
1777
1778 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001779 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001780 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001781
1782 /* keep going if FE state is > hw_params */
1783 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1784 return 0;
1785
1786
1787 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001788 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001789 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001790
1791 /* run the stream event for each BE */
1792 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1793
1794 /* keep going if FE state is > prepare */
1795 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1796 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1797 return 0;
1798
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001799 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1800 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001801 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001802 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001803
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001804 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1805 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001806 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001807 goto hw_free;
1808 }
1809 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001810 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001811 fe->dai_link->name);
1812
1813 ret = dpcm_be_dai_trigger(fe, stream,
1814 SNDRV_PCM_TRIGGER_START);
1815 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001816 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001817 goto hw_free;
1818 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001819 }
1820
1821 return 0;
1822
1823hw_free:
1824 dpcm_be_dai_hw_free(fe, stream);
1825close:
1826 dpcm_be_dai_shutdown(fe, stream);
1827disconnect:
1828 /* disconnect any non started BEs */
1829 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1830 struct snd_soc_pcm_runtime *be = dpcm->be;
1831 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1832 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1833 }
1834
1835 return ret;
1836}
1837
1838static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1839{
1840 int ret;
1841
1842 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1843 ret = dpcm_run_update_startup(fe, stream);
1844 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001845 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001846 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1847
1848 return ret;
1849}
1850
1851static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1852{
1853 int ret;
1854
1855 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1856 ret = dpcm_run_update_shutdown(fe, stream);
1857 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001858 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001859 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1860
1861 return ret;
1862}
1863
1864/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1865 * any DAI links.
1866 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001867int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001868{
Liam Girdwood618dae12012-04-25 12:12:51 +01001869 int i, old, new, paths;
1870
Liam Girdwood618dae12012-04-25 12:12:51 +01001871 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1872 for (i = 0; i < card->num_rtd; i++) {
1873 struct snd_soc_dapm_widget_list *list;
1874 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1875
1876 /* make sure link is FE */
1877 if (!fe->dai_link->dynamic)
1878 continue;
1879
1880 /* only check active links */
1881 if (!fe->cpu_dai->active)
1882 continue;
1883
1884 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001885 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001886 fe->dai_link->name);
1887
1888 /* skip if FE doesn't have playback capability */
1889 if (!fe->cpu_dai->driver->playback.channels_min)
1890 goto capture;
1891
1892 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1893 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001894 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001895 fe->dai_link->name, "playback");
1896 mutex_unlock(&card->mutex);
1897 return paths;
1898 }
1899
1900 /* update any new playback paths */
1901 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1902 if (new) {
1903 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1904 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1905 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1906 }
1907
1908 /* update any old playback paths */
1909 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
1910 if (old) {
1911 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1912 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1913 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1914 }
1915
1916capture:
1917 /* skip if FE doesn't have capture capability */
1918 if (!fe->cpu_dai->driver->capture.channels_min)
1919 continue;
1920
1921 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
1922 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001923 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001924 fe->dai_link->name, "capture");
1925 mutex_unlock(&card->mutex);
1926 return paths;
1927 }
1928
1929 /* update any new capture paths */
1930 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
1931 if (new) {
1932 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1933 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1934 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1935 }
1936
1937 /* update any old capture paths */
1938 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
1939 if (old) {
1940 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
1941 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
1942 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
1943 }
1944
1945 dpcm_path_put(&list);
1946 }
1947
1948 mutex_unlock(&card->mutex);
1949 return 0;
1950}
Liam Girdwood01d75842012-04-25 12:12:49 +01001951int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
1952{
1953 struct snd_soc_dpcm *dpcm;
1954 struct list_head *clients =
1955 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
1956
1957 list_for_each_entry(dpcm, clients, list_be) {
1958
1959 struct snd_soc_pcm_runtime *be = dpcm->be;
1960 struct snd_soc_dai *dai = be->codec_dai;
1961 struct snd_soc_dai_driver *drv = dai->driver;
1962
1963 if (be->dai_link->ignore_suspend)
1964 continue;
1965
Liam Girdwood103d84a2012-11-19 14:39:15 +00001966 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001967
Mark Brownc5914b02013-10-30 17:47:39 -07001968 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
1969 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01001970 }
1971
1972 return 0;
1973}
1974
Mark Brown45c0a182012-05-09 21:46:27 +01001975static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001976{
1977 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1978 struct snd_soc_dpcm *dpcm;
1979 struct snd_soc_dapm_widget_list *list;
1980 int ret;
1981 int stream = fe_substream->stream;
1982
1983 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1984 fe->dpcm[stream].runtime = fe_substream->runtime;
1985
1986 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001987 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001988 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01001989 }
1990
1991 /* calculate valid and active FE <-> BE dpcms */
1992 dpcm_process_paths(fe, stream, &list, 1);
1993
1994 ret = dpcm_fe_dai_startup(fe_substream);
1995 if (ret < 0) {
1996 /* clean up all links */
1997 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1998 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1999
2000 dpcm_be_disconnect(fe, stream);
2001 fe->dpcm[stream].runtime = NULL;
2002 }
2003
2004 dpcm_clear_pending_state(fe, stream);
2005 dpcm_path_put(&list);
2006 mutex_unlock(&fe->card->mutex);
2007 return ret;
2008}
2009
Mark Brown45c0a182012-05-09 21:46:27 +01002010static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002011{
2012 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2013 struct snd_soc_dpcm *dpcm;
2014 int stream = fe_substream->stream, ret;
2015
2016 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2017 ret = dpcm_fe_dai_shutdown(fe_substream);
2018
2019 /* mark FE's links ready to prune */
2020 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2021 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2022
2023 dpcm_be_disconnect(fe, stream);
2024
2025 fe->dpcm[stream].runtime = NULL;
2026 mutex_unlock(&fe->card->mutex);
2027 return ret;
2028}
2029
Liam Girdwoodddee6272011-06-09 14:45:53 +01002030/* create a new pcm */
2031int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2032{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002033 struct snd_soc_platform *platform = rtd->platform;
2034 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2035 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2036 struct snd_pcm *pcm;
2037 char new_name[64];
2038 int ret = 0, playback = 0, capture = 0;
2039
Liam Girdwood01d75842012-04-25 12:12:49 +01002040 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002041 playback = rtd->dai_link->dpcm_playback;
2042 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002043 } else {
Mark Brown05679092013-06-01 23:13:53 +01002044 if (codec_dai->driver->playback.channels_min &&
2045 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002046 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002047 if (codec_dai->driver->capture.channels_min &&
2048 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002049 capture = 1;
2050 }
Sangsu Parka5002312012-01-02 17:15:10 +09002051
Fabio Estevamd6bead02013-08-29 10:32:13 -03002052 if (rtd->dai_link->playback_only) {
2053 playback = 1;
2054 capture = 0;
2055 }
2056
2057 if (rtd->dai_link->capture_only) {
2058 playback = 0;
2059 capture = 1;
2060 }
2061
Liam Girdwood01d75842012-04-25 12:12:49 +01002062 /* create the PCM */
2063 if (rtd->dai_link->no_pcm) {
2064 snprintf(new_name, sizeof(new_name), "(%s)",
2065 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002066
Liam Girdwood01d75842012-04-25 12:12:49 +01002067 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2068 playback, capture, &pcm);
2069 } else {
2070 if (rtd->dai_link->dynamic)
2071 snprintf(new_name, sizeof(new_name), "%s (*)",
2072 rtd->dai_link->stream_name);
2073 else
2074 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2075 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002076
Liam Girdwood01d75842012-04-25 12:12:49 +01002077 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2078 capture, &pcm);
2079 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002080 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002081 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002082 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002083 return ret;
2084 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002085 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002086
2087 /* DAPM dai link stream work */
2088 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2089
2090 rtd->pcm = pcm;
2091 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002092
2093 if (rtd->dai_link->no_pcm) {
2094 if (playback)
2095 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2096 if (capture)
2097 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2098 goto out;
2099 }
2100
2101 /* ASoC PCM operations */
2102 if (rtd->dai_link->dynamic) {
2103 rtd->ops.open = dpcm_fe_dai_open;
2104 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2105 rtd->ops.prepare = dpcm_fe_dai_prepare;
2106 rtd->ops.trigger = dpcm_fe_dai_trigger;
2107 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2108 rtd->ops.close = dpcm_fe_dai_close;
2109 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002110 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002111 } else {
2112 rtd->ops.open = soc_pcm_open;
2113 rtd->ops.hw_params = soc_pcm_hw_params;
2114 rtd->ops.prepare = soc_pcm_prepare;
2115 rtd->ops.trigger = soc_pcm_trigger;
2116 rtd->ops.hw_free = soc_pcm_hw_free;
2117 rtd->ops.close = soc_pcm_close;
2118 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002119 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002120 }
2121
Liam Girdwoodddee6272011-06-09 14:45:53 +01002122 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002123 rtd->ops.ack = platform->driver->ops->ack;
2124 rtd->ops.copy = platform->driver->ops->copy;
2125 rtd->ops.silence = platform->driver->ops->silence;
2126 rtd->ops.page = platform->driver->ops->page;
2127 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002128 }
2129
2130 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002131 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002132
2133 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002134 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002135
2136 if (platform->driver->pcm_new) {
2137 ret = platform->driver->pcm_new(rtd);
2138 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002139 dev_err(platform->dev,
2140 "ASoC: pcm constructor failed: %d\n",
2141 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002142 return ret;
2143 }
2144 }
2145
2146 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002147out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002148 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002149 cpu_dai->name);
2150 return ret;
2151}
Liam Girdwood01d75842012-04-25 12:12:49 +01002152
2153/* is the current PCM operation for this FE ? */
2154int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2155{
2156 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2157 return 1;
2158 return 0;
2159}
2160EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2161
2162/* is the current PCM operation for this BE ? */
2163int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2164 struct snd_soc_pcm_runtime *be, int stream)
2165{
2166 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2167 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2168 be->dpcm[stream].runtime_update))
2169 return 1;
2170 return 0;
2171}
2172EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2173
2174/* get the substream for this BE */
2175struct snd_pcm_substream *
2176 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2177{
2178 return be->pcm->streams[stream].substream;
2179}
2180EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2181
2182/* get the BE runtime state */
2183enum snd_soc_dpcm_state
2184 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2185{
2186 return be->dpcm[stream].state;
2187}
2188EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2189
2190/* set the BE runtime state */
2191void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2192 int stream, enum snd_soc_dpcm_state state)
2193{
2194 be->dpcm[stream].state = state;
2195}
2196EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2197
2198/*
2199 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2200 * are not running, paused or suspended for the specified stream direction.
2201 */
2202int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2203 struct snd_soc_pcm_runtime *be, int stream)
2204{
2205 struct snd_soc_dpcm *dpcm;
2206 int state;
2207
2208 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2209
2210 if (dpcm->fe == fe)
2211 continue;
2212
2213 state = dpcm->fe->dpcm[stream].state;
2214 if (state == SND_SOC_DPCM_STATE_START ||
2215 state == SND_SOC_DPCM_STATE_PAUSED ||
2216 state == SND_SOC_DPCM_STATE_SUSPEND)
2217 return 0;
2218 }
2219
2220 /* it's safe to free/stop this BE DAI */
2221 return 1;
2222}
2223EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2224
2225/*
2226 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2227 * running, paused or suspended for the specified stream direction.
2228 */
2229int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2230 struct snd_soc_pcm_runtime *be, int stream)
2231{
2232 struct snd_soc_dpcm *dpcm;
2233 int state;
2234
2235 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2236
2237 if (dpcm->fe == fe)
2238 continue;
2239
2240 state = dpcm->fe->dpcm[stream].state;
2241 if (state == SND_SOC_DPCM_STATE_START ||
2242 state == SND_SOC_DPCM_STATE_PAUSED ||
2243 state == SND_SOC_DPCM_STATE_SUSPEND ||
2244 state == SND_SOC_DPCM_STATE_PREPARE)
2245 return 0;
2246 }
2247
2248 /* it's safe to change hw_params */
2249 return 1;
2250}
2251EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002252
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002253int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2254 int cmd, struct snd_soc_platform *platform)
2255{
Mark Brownc5914b02013-10-30 17:47:39 -07002256 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002257 return platform->driver->ops->trigger(substream, cmd);
2258 return 0;
2259}
2260EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2261
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002262#ifdef CONFIG_DEBUG_FS
2263static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2264{
2265 switch (state) {
2266 case SND_SOC_DPCM_STATE_NEW:
2267 return "new";
2268 case SND_SOC_DPCM_STATE_OPEN:
2269 return "open";
2270 case SND_SOC_DPCM_STATE_HW_PARAMS:
2271 return "hw_params";
2272 case SND_SOC_DPCM_STATE_PREPARE:
2273 return "prepare";
2274 case SND_SOC_DPCM_STATE_START:
2275 return "start";
2276 case SND_SOC_DPCM_STATE_STOP:
2277 return "stop";
2278 case SND_SOC_DPCM_STATE_SUSPEND:
2279 return "suspend";
2280 case SND_SOC_DPCM_STATE_PAUSED:
2281 return "paused";
2282 case SND_SOC_DPCM_STATE_HW_FREE:
2283 return "hw_free";
2284 case SND_SOC_DPCM_STATE_CLOSE:
2285 return "close";
2286 }
2287
2288 return "unknown";
2289}
2290
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002291static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2292 int stream, char *buf, size_t size)
2293{
2294 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2295 struct snd_soc_dpcm *dpcm;
2296 ssize_t offset = 0;
2297
2298 /* FE state */
2299 offset += snprintf(buf + offset, size - offset,
2300 "[%s - %s]\n", fe->dai_link->name,
2301 stream ? "Capture" : "Playback");
2302
2303 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2304 dpcm_state_string(fe->dpcm[stream].state));
2305
2306 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2307 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2308 offset += snprintf(buf + offset, size - offset,
2309 "Hardware Params: "
2310 "Format = %s, Channels = %d, Rate = %d\n",
2311 snd_pcm_format_name(params_format(params)),
2312 params_channels(params),
2313 params_rate(params));
2314
2315 /* BEs state */
2316 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2317
2318 if (list_empty(&fe->dpcm[stream].be_clients)) {
2319 offset += snprintf(buf + offset, size - offset,
2320 " No active DSP links\n");
2321 goto out;
2322 }
2323
2324 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2325 struct snd_soc_pcm_runtime *be = dpcm->be;
2326 params = &dpcm->hw_params;
2327
2328 offset += snprintf(buf + offset, size - offset,
2329 "- %s\n", be->dai_link->name);
2330
2331 offset += snprintf(buf + offset, size - offset,
2332 " State: %s\n",
2333 dpcm_state_string(be->dpcm[stream].state));
2334
2335 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2336 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2337 offset += snprintf(buf + offset, size - offset,
2338 " Hardware Params: "
2339 "Format = %s, Channels = %d, Rate = %d\n",
2340 snd_pcm_format_name(params_format(params)),
2341 params_channels(params),
2342 params_rate(params));
2343 }
2344
2345out:
2346 return offset;
2347}
2348
2349static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2350 size_t count, loff_t *ppos)
2351{
2352 struct snd_soc_pcm_runtime *fe = file->private_data;
2353 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2354 char *buf;
2355
2356 buf = kmalloc(out_count, GFP_KERNEL);
2357 if (!buf)
2358 return -ENOMEM;
2359
2360 if (fe->cpu_dai->driver->playback.channels_min)
2361 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2362 buf + offset, out_count - offset);
2363
2364 if (fe->cpu_dai->driver->capture.channels_min)
2365 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2366 buf + offset, out_count - offset);
2367
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002368 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002369
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002370 kfree(buf);
2371 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002372}
2373
2374static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002375 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002376 .read = dpcm_state_read_file,
2377 .llseek = default_llseek,
2378};
2379
2380int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2381{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002382 if (!rtd->dai_link)
2383 return 0;
2384
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002385 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2386 rtd->card->debugfs_card_root);
2387 if (!rtd->debugfs_dpcm_root) {
2388 dev_dbg(rtd->dev,
2389 "ASoC: Failed to create dpcm debugfs directory %s\n",
2390 rtd->dai_link->name);
2391 return -EINVAL;
2392 }
2393
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002394 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002395 rtd->debugfs_dpcm_root,
2396 rtd, &dpcm_state_fops);
2397
2398 return 0;
2399}
2400#endif