blob: 47e1ce771e65e0403e1ab743c677083c98452a8c [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. */
Liam Girdwood23607022014-01-17 17:03:55 +000061int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +010062 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
Nicolin Chen3635bf02013-11-13 18:56:24 +080087 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
88 rtd->dai_link->symmetric_rates)) {
89 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
90 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010091
Nicolin Chen3635bf02013-11-13 18:56:24 +080092 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
93 SNDRV_PCM_HW_PARAM_RATE,
94 soc_dai->rate, soc_dai->rate);
95 if (ret < 0) {
96 dev_err(soc_dai->dev,
97 "ASoC: Unable to apply rate constraint: %d\n",
98 ret);
99 return ret;
100 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100101 }
102
Nicolin Chen3635bf02013-11-13 18:56:24 +0800103 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
104 rtd->dai_link->symmetric_channels)) {
105 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
106 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100107
Nicolin Chen3635bf02013-11-13 18:56:24 +0800108 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
109 SNDRV_PCM_HW_PARAM_CHANNELS,
110 soc_dai->channels,
111 soc_dai->channels);
112 if (ret < 0) {
113 dev_err(soc_dai->dev,
114 "ASoC: Unable to apply channel symmetry constraint: %d\n",
115 ret);
116 return ret;
117 }
118 }
119
120 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
121 rtd->dai_link->symmetric_samplebits)) {
122 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
123 soc_dai->sample_bits);
124
125 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
126 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
127 soc_dai->sample_bits,
128 soc_dai->sample_bits);
129 if (ret < 0) {
130 dev_err(soc_dai->dev,
131 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
132 ret);
133 return ret;
134 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100135 }
136
137 return 0;
138}
139
Nicolin Chen3635bf02013-11-13 18:56:24 +0800140static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *params)
142{
143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
144 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
145 struct snd_soc_dai *codec_dai = rtd->codec_dai;
146 unsigned int rate, channels, sample_bits, symmetry;
147
148 rate = params_rate(params);
149 channels = params_channels(params);
150 sample_bits = snd_pcm_format_physical_width(params_format(params));
151
152 /* reject unmatched parameters when applying symmetry */
153 symmetry = cpu_dai->driver->symmetric_rates ||
154 codec_dai->driver->symmetric_rates ||
155 rtd->dai_link->symmetric_rates;
156 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
157 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
158 cpu_dai->rate, rate);
159 return -EINVAL;
160 }
161
162 symmetry = cpu_dai->driver->symmetric_channels ||
163 codec_dai->driver->symmetric_channels ||
164 rtd->dai_link->symmetric_channels;
165 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
166 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
167 cpu_dai->channels, channels);
168 return -EINVAL;
169 }
170
171 symmetry = cpu_dai->driver->symmetric_samplebits ||
172 codec_dai->driver->symmetric_samplebits ||
173 rtd->dai_link->symmetric_samplebits;
174 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
175 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
176 cpu_dai->sample_bits, sample_bits);
177 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100178 }
179
180 return 0;
181}
182
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100183static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
184{
185 struct snd_soc_pcm_runtime *rtd = substream->private_data;
186 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
187 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
188 struct snd_soc_dai_link *link = rtd->dai_link;
189
190 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
191 link->symmetric_rates || cpu_driver->symmetric_channels ||
192 codec_driver->symmetric_channels || link->symmetric_channels ||
193 cpu_driver->symmetric_samplebits ||
194 codec_driver->symmetric_samplebits ||
195 link->symmetric_samplebits;
196}
197
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000199 * List of sample sizes that might go over the bus for parameter
200 * application. There ought to be a wildcard sample size for things
201 * like the DAC/ADC resolution to use but there isn't right now.
202 */
203static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200204 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000205};
206
207static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
208 struct snd_soc_dai *dai)
209{
210 int ret, i, bits;
211
212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
213 bits = dai->driver->playback.sig_bits;
214 else
215 bits = dai->driver->capture.sig_bits;
216
217 if (!bits)
218 return;
219
220 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000221 if (bits >= sample_sizes[i])
222 continue;
223
224 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
225 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000226 if (ret != 0)
227 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000228 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000229 bits, sample_sizes[i], ret);
230 }
231}
232
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100233static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200234 struct snd_soc_pcm_stream *codec_stream,
235 struct snd_soc_pcm_stream *cpu_stream)
236{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100237 struct snd_pcm_hardware *hw = &runtime->hw;
238
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200239 hw->channels_min = max(codec_stream->channels_min,
240 cpu_stream->channels_min);
241 hw->channels_max = min(codec_stream->channels_max,
242 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100243 if (hw->formats)
244 hw->formats &= codec_stream->formats & cpu_stream->formats;
245 else
246 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausen55dcdb52014-01-11 10:24:44 +0100247 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates,
248 cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100249
Lars-Peter Clausen817873f2014-01-11 10:24:40 +0100250 hw->rate_min = 0;
251 hw->rate_max = UINT_MAX;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100252
253 snd_pcm_limit_hw_rates(runtime);
254
255 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
256 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
257 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
258 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200259}
260
Mark Brown58ba9b22012-01-16 18:38:51 +0000261/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100262 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
263 * then initialized and any private data can be allocated. This also calls
264 * startup for the cpu DAI, platform, machine and codec DAI.
265 */
266static int soc_pcm_open(struct snd_pcm_substream *substream)
267{
268 struct snd_soc_pcm_runtime *rtd = substream->private_data;
269 struct snd_pcm_runtime *runtime = substream->runtime;
270 struct snd_soc_platform *platform = rtd->platform;
271 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
272 struct snd_soc_dai *codec_dai = rtd->codec_dai;
273 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
274 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
275 int ret = 0;
276
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800277 pinctrl_pm_select_default_state(cpu_dai->dev);
278 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000279 pm_runtime_get_sync(cpu_dai->dev);
280 pm_runtime_get_sync(codec_dai->dev);
281 pm_runtime_get_sync(platform->dev);
282
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100283 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100284
285 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700286 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100287 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
288 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000289 dev_err(cpu_dai->dev, "ASoC: can't open interface"
290 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100291 goto out;
292 }
293 }
294
295 if (platform->driver->ops && platform->driver->ops->open) {
296 ret = platform->driver->ops->open(substream);
297 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000298 dev_err(platform->dev, "ASoC: can't open platform"
299 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100300 goto platform_err;
301 }
302 }
303
Mark Brownc5914b02013-10-30 17:47:39 -0700304 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100305 ret = codec_dai->driver->ops->startup(substream, codec_dai);
306 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000307 dev_err(codec_dai->dev, "ASoC: can't open codec"
308 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100309 goto codec_dai_err;
310 }
311 }
312
313 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
314 ret = rtd->dai_link->ops->startup(substream);
315 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000316 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000317 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100318 goto machine_err;
319 }
320 }
321
Liam Girdwood01d75842012-04-25 12:12:49 +0100322 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
323 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
324 goto dynamic;
325
Liam Girdwoodddee6272011-06-09 14:45:53 +0100326 /* Check that the codec and cpu DAIs are compatible */
327 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100328 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200329 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100330 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100331 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200332 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100333 }
334
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100335 if (soc_pcm_has_symmetry(substream))
336 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
337
Liam Girdwoodddee6272011-06-09 14:45:53 +0100338 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100339 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000340 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100341 codec_dai->name, cpu_dai->name);
342 goto config_err;
343 }
344 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000345 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100346 codec_dai->name, cpu_dai->name);
347 goto config_err;
348 }
349 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
350 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000351 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100352 codec_dai->name, cpu_dai->name);
353 goto config_err;
354 }
355
Mark Brown58ba9b22012-01-16 18:38:51 +0000356 soc_pcm_apply_msb(substream, codec_dai);
357 soc_pcm_apply_msb(substream, cpu_dai);
358
Liam Girdwoodddee6272011-06-09 14:45:53 +0100359 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800360 if (cpu_dai->active) {
361 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
362 if (ret != 0)
363 goto config_err;
364 }
365
366 if (codec_dai->active) {
367 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100368 if (ret != 0)
369 goto config_err;
370 }
371
Liam Girdwood103d84a2012-11-19 14:39:15 +0000372 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100373 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000374 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
375 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100376 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000377 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100378 runtime->hw.rate_max);
379
Liam Girdwood01d75842012-04-25 12:12:49 +0100380dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100381 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
382 cpu_dai->playback_active++;
383 codec_dai->playback_active++;
384 } else {
385 cpu_dai->capture_active++;
386 codec_dai->capture_active++;
387 }
388 cpu_dai->active++;
389 codec_dai->active++;
390 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100391 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100392 return 0;
393
394config_err:
395 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
396 rtd->dai_link->ops->shutdown(substream);
397
398machine_err:
399 if (codec_dai->driver->ops->shutdown)
400 codec_dai->driver->ops->shutdown(substream, codec_dai);
401
402codec_dai_err:
403 if (platform->driver->ops && platform->driver->ops->close)
404 platform->driver->ops->close(substream);
405
406platform_err:
407 if (cpu_dai->driver->ops->shutdown)
408 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
409out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100410 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000411
412 pm_runtime_put(platform->dev);
413 pm_runtime_put(codec_dai->dev);
414 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800415 if (!codec_dai->active)
416 pinctrl_pm_select_sleep_state(codec_dai->dev);
417 if (!cpu_dai->active)
418 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000419
Liam Girdwoodddee6272011-06-09 14:45:53 +0100420 return ret;
421}
422
423/*
424 * Power down the audio subsystem pmdown_time msecs after close is called.
425 * This is to ensure there are no pops or clicks in between any music tracks
426 * due to DAPM power cycling.
427 */
428static void close_delayed_work(struct work_struct *work)
429{
430 struct snd_soc_pcm_runtime *rtd =
431 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
432 struct snd_soc_dai *codec_dai = rtd->codec_dai;
433
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100434 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100435
Liam Girdwood103d84a2012-11-19 14:39:15 +0000436 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100437 codec_dai->driver->playback.stream_name,
438 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600439 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440
441 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600442 if (rtd->pop_wait == 1) {
443 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800444 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000445 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100446 }
447
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100448 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100449}
450
451/*
452 * Called by ALSA when a PCM substream is closed. Private data can be
453 * freed here. The cpu DAI, codec DAI, machine and platform are also
454 * shutdown.
455 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100456static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100457{
458 struct snd_soc_pcm_runtime *rtd = substream->private_data;
459 struct snd_soc_platform *platform = rtd->platform;
460 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
461 struct snd_soc_dai *codec_dai = rtd->codec_dai;
462 struct snd_soc_codec *codec = rtd->codec;
463
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100464 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100465
466 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
467 cpu_dai->playback_active--;
468 codec_dai->playback_active--;
469 } else {
470 cpu_dai->capture_active--;
471 codec_dai->capture_active--;
472 }
473
474 cpu_dai->active--;
475 codec_dai->active--;
476 codec->active--;
477
Dong Aisheng17841022011-08-29 17:15:14 +0800478 /* clear the corresponding DAIs rate when inactive */
479 if (!cpu_dai->active)
480 cpu_dai->rate = 0;
481
482 if (!codec_dai->active)
483 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200484
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 if (cpu_dai->driver->ops->shutdown)
486 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
487
488 if (codec_dai->driver->ops->shutdown)
489 codec_dai->driver->ops->shutdown(substream, codec_dai);
490
491 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
492 rtd->dai_link->ops->shutdown(substream);
493
494 if (platform->driver->ops && platform->driver->ops->close)
495 platform->driver->ops->close(substream);
496 cpu_dai->runtime = NULL;
497
498 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000499 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200500 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300501 /* powered down playback stream now */
502 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800503 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800504 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300505 } else {
506 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600507 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100508 queue_delayed_work(system_power_efficient_wq,
509 &rtd->delayed_work,
510 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300511 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100512 } else {
513 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800514 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000515 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100516 }
517
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100518 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000519
520 pm_runtime_put(platform->dev);
521 pm_runtime_put(codec_dai->dev);
522 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800523 if (!codec_dai->active)
524 pinctrl_pm_select_sleep_state(codec_dai->dev);
525 if (!cpu_dai->active)
526 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000527
Liam Girdwoodddee6272011-06-09 14:45:53 +0100528 return 0;
529}
530
531/*
532 * Called by ALSA when the PCM substream is prepared, can set format, sample
533 * rate, etc. This function is non atomic and can be called multiple times,
534 * it can refer to the runtime info.
535 */
536static int soc_pcm_prepare(struct snd_pcm_substream *substream)
537{
538 struct snd_soc_pcm_runtime *rtd = substream->private_data;
539 struct snd_soc_platform *platform = rtd->platform;
540 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
541 struct snd_soc_dai *codec_dai = rtd->codec_dai;
542 int ret = 0;
543
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100544 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545
546 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
547 ret = rtd->dai_link->ops->prepare(substream);
548 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000549 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
550 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 goto out;
552 }
553 }
554
555 if (platform->driver->ops && platform->driver->ops->prepare) {
556 ret = platform->driver->ops->prepare(substream);
557 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000558 dev_err(platform->dev, "ASoC: platform prepare error:"
559 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 goto out;
561 }
562 }
563
Mark Brownc5914b02013-10-30 17:47:39 -0700564 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100565 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
566 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000567 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000568 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100569 goto out;
570 }
571 }
572
Mark Brownc5914b02013-10-30 17:47:39 -0700573 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100574 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
575 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000576 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000577 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100578 goto out;
579 }
580 }
581
582 /* cancel any delayed stream shutdown that is pending */
583 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600584 rtd->pop_wait) {
585 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100586 cancel_delayed_work(&rtd->delayed_work);
587 }
588
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000589 snd_soc_dapm_stream_event(rtd, substream->stream,
590 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100591
Mark Brownda183962013-02-06 15:44:07 +0000592 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593
594out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100595 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100596 return ret;
597}
598
599/*
600 * Called by ALSA when the hardware params are set by application. This
601 * function can also be called multiple times and can allocate buffers
602 * (using snd_pcm_lib_* ). It's non-atomic.
603 */
604static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
605 struct snd_pcm_hw_params *params)
606{
607 struct snd_soc_pcm_runtime *rtd = substream->private_data;
608 struct snd_soc_platform *platform = rtd->platform;
609 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
610 struct snd_soc_dai *codec_dai = rtd->codec_dai;
611 int ret = 0;
612
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100613 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100614
Nicolin Chen3635bf02013-11-13 18:56:24 +0800615 ret = soc_pcm_params_symmetry(substream, params);
616 if (ret)
617 goto out;
618
Liam Girdwoodddee6272011-06-09 14:45:53 +0100619 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
620 ret = rtd->dai_link->ops->hw_params(substream, params);
621 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000622 dev_err(rtd->card->dev, "ASoC: machine hw_params"
623 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 goto out;
625 }
626 }
627
Mark Brownc5914b02013-10-30 17:47:39 -0700628 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100629 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
630 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000631 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
632 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100633 goto codec_err;
634 }
635 }
636
Mark Brownc5914b02013-10-30 17:47:39 -0700637 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100638 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
639 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000640 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000641 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 goto interface_err;
643 }
644 }
645
646 if (platform->driver->ops && platform->driver->ops->hw_params) {
647 ret = platform->driver->ops->hw_params(substream, params);
648 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000649 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000650 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651 goto platform_err;
652 }
653 }
654
Nicolin Chen3635bf02013-11-13 18:56:24 +0800655 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800656 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800657 cpu_dai->channels = params_channels(params);
658 cpu_dai->sample_bits =
659 snd_pcm_format_physical_width(params_format(params));
660
Dong Aisheng17841022011-08-29 17:15:14 +0800661 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800662 codec_dai->channels = params_channels(params);
663 codec_dai->sample_bits =
664 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100665
666out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100667 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100668 return ret;
669
670platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700671 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100672 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
673
674interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700675 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100676 codec_dai->driver->ops->hw_free(substream, codec_dai);
677
678codec_err:
679 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
680 rtd->dai_link->ops->hw_free(substream);
681
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100682 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100683 return ret;
684}
685
686/*
687 * Frees resources allocated by hw_params, can be called multiple times
688 */
689static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
690{
691 struct snd_soc_pcm_runtime *rtd = substream->private_data;
692 struct snd_soc_platform *platform = rtd->platform;
693 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
694 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800695 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100696
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100697 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698
Nicolin Chend3383422013-11-20 18:37:09 +0800699 /* clear the corresponding DAIs parameters when going to be inactive */
700 if (cpu_dai->active == 1) {
701 cpu_dai->rate = 0;
702 cpu_dai->channels = 0;
703 cpu_dai->sample_bits = 0;
704 }
705
706 if (codec_dai->active == 1) {
707 codec_dai->rate = 0;
708 codec_dai->channels = 0;
709 codec_dai->sample_bits = 0;
710 }
711
Liam Girdwoodddee6272011-06-09 14:45:53 +0100712 /* apply codec digital mute */
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800713 if ((playback && codec_dai->playback_active == 1) ||
714 (!playback && codec_dai->capture_active == 1))
Mark Brownda183962013-02-06 15:44:07 +0000715 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100716
717 /* free any machine hw params */
718 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
719 rtd->dai_link->ops->hw_free(substream);
720
721 /* free any DMA resources */
722 if (platform->driver->ops && platform->driver->ops->hw_free)
723 platform->driver->ops->hw_free(substream);
724
725 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700726 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100727 codec_dai->driver->ops->hw_free(substream, codec_dai);
728
Mark Brownc5914b02013-10-30 17:47:39 -0700729 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100730 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
731
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100732 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100733 return 0;
734}
735
736static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
737{
738 struct snd_soc_pcm_runtime *rtd = substream->private_data;
739 struct snd_soc_platform *platform = rtd->platform;
740 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
741 struct snd_soc_dai *codec_dai = rtd->codec_dai;
742 int ret;
743
Mark Brownc5914b02013-10-30 17:47:39 -0700744 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100745 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
746 if (ret < 0)
747 return ret;
748 }
749
750 if (platform->driver->ops && platform->driver->ops->trigger) {
751 ret = platform->driver->ops->trigger(substream, cmd);
752 if (ret < 0)
753 return ret;
754 }
755
Mark Brownc5914b02013-10-30 17:47:39 -0700756 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100757 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
758 if (ret < 0)
759 return ret;
760 }
761 return 0;
762}
763
Mark Brown45c0a182012-05-09 21:46:27 +0100764static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
765 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100766{
767 struct snd_soc_pcm_runtime *rtd = substream->private_data;
768 struct snd_soc_platform *platform = rtd->platform;
769 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
770 struct snd_soc_dai *codec_dai = rtd->codec_dai;
771 int ret;
772
Mark Brownc5914b02013-10-30 17:47:39 -0700773 if (codec_dai->driver->ops &&
774 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100775 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
776 if (ret < 0)
777 return ret;
778 }
779
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +0100780 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100781 ret = platform->driver->bespoke_trigger(substream, cmd);
782 if (ret < 0)
783 return ret;
784 }
785
Mark Brownc5914b02013-10-30 17:47:39 -0700786 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100787 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
788 if (ret < 0)
789 return ret;
790 }
791 return 0;
792}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100793/*
794 * soc level wrapper for pointer callback
795 * If cpu_dai, codec_dai, platform driver has the delay callback, than
796 * the runtime->delay will be updated accordingly.
797 */
798static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
799{
800 struct snd_soc_pcm_runtime *rtd = substream->private_data;
801 struct snd_soc_platform *platform = rtd->platform;
802 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
803 struct snd_soc_dai *codec_dai = rtd->codec_dai;
804 struct snd_pcm_runtime *runtime = substream->runtime;
805 snd_pcm_uframes_t offset = 0;
806 snd_pcm_sframes_t delay = 0;
807
808 if (platform->driver->ops && platform->driver->ops->pointer)
809 offset = platform->driver->ops->pointer(substream);
810
Mark Brownc5914b02013-10-30 17:47:39 -0700811 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100812 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
813
Mark Brownc5914b02013-10-30 17:47:39 -0700814 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100815 delay += codec_dai->driver->ops->delay(substream, codec_dai);
816
817 if (platform->driver->delay)
818 delay += platform->driver->delay(substream, codec_dai);
819
820 runtime->delay = delay;
821
822 return offset;
823}
824
Liam Girdwood01d75842012-04-25 12:12:49 +0100825/* connect a FE and BE */
826static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
827 struct snd_soc_pcm_runtime *be, int stream)
828{
829 struct snd_soc_dpcm *dpcm;
830
831 /* only add new dpcms */
832 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
833 if (dpcm->be == be && dpcm->fe == fe)
834 return 0;
835 }
836
837 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
838 if (!dpcm)
839 return -ENOMEM;
840
841 dpcm->be = be;
842 dpcm->fe = fe;
843 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
844 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
845 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
846 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
847
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300848 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100849 stream ? "capture" : "playback", fe->dai_link->name,
850 stream ? "<-" : "->", be->dai_link->name);
851
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100852#ifdef CONFIG_DEBUG_FS
853 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
854 fe->debugfs_dpcm_root, &dpcm->state);
855#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100856 return 1;
857}
858
859/* reparent a BE onto another FE */
860static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
861 struct snd_soc_pcm_runtime *be, int stream)
862{
863 struct snd_soc_dpcm *dpcm;
864 struct snd_pcm_substream *fe_substream, *be_substream;
865
866 /* reparent if BE is connected to other FEs */
867 if (!be->dpcm[stream].users)
868 return;
869
870 be_substream = snd_soc_dpcm_get_substream(be, stream);
871
872 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
873 if (dpcm->fe == fe)
874 continue;
875
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300876 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100877 stream ? "capture" : "playback",
878 dpcm->fe->dai_link->name,
879 stream ? "<-" : "->", dpcm->be->dai_link->name);
880
881 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
882 be_substream->runtime = fe_substream->runtime;
883 break;
884 }
885}
886
887/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +0000888void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +0100889{
890 struct snd_soc_dpcm *dpcm, *d;
891
892 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000893 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100894 stream ? "capture" : "playback",
895 dpcm->be->dai_link->name);
896
897 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
898 continue;
899
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300900 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100901 stream ? "capture" : "playback", fe->dai_link->name,
902 stream ? "<-" : "->", dpcm->be->dai_link->name);
903
904 /* BEs still alive need new FE */
905 dpcm_be_reparent(fe, dpcm->be, stream);
906
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100907#ifdef CONFIG_DEBUG_FS
908 debugfs_remove(dpcm->debugfs_state);
909#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100910 list_del(&dpcm->list_be);
911 list_del(&dpcm->list_fe);
912 kfree(dpcm);
913 }
914}
915
916/* get BE for DAI widget and stream */
917static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
918 struct snd_soc_dapm_widget *widget, int stream)
919{
920 struct snd_soc_pcm_runtime *be;
921 int i;
922
923 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
924 for (i = 0; i < card->num_links; i++) {
925 be = &card->rtd[i];
926
Liam Girdwood35ea0652012-06-05 19:26:59 +0100927 if (!be->dai_link->no_pcm)
928 continue;
929
Liam Girdwood01d75842012-04-25 12:12:49 +0100930 if (be->cpu_dai->playback_widget == widget ||
931 be->codec_dai->playback_widget == widget)
932 return be;
933 }
934 } else {
935
936 for (i = 0; i < card->num_links; i++) {
937 be = &card->rtd[i];
938
Liam Girdwood35ea0652012-06-05 19:26:59 +0100939 if (!be->dai_link->no_pcm)
940 continue;
941
Liam Girdwood01d75842012-04-25 12:12:49 +0100942 if (be->cpu_dai->capture_widget == widget ||
943 be->codec_dai->capture_widget == widget)
944 return be;
945 }
946 }
947
Liam Girdwood103d84a2012-11-19 14:39:15 +0000948 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100949 stream ? "capture" : "playback", widget->name);
950 return NULL;
951}
952
953static inline struct snd_soc_dapm_widget *
954 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
955{
956 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
957 return rtd->cpu_dai->playback_widget;
958 else
959 return rtd->cpu_dai->capture_widget;
960}
961
962static inline struct snd_soc_dapm_widget *
963 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
964{
965 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
966 return rtd->codec_dai->playback_widget;
967 else
968 return rtd->codec_dai->capture_widget;
969}
970
971static int widget_in_list(struct snd_soc_dapm_widget_list *list,
972 struct snd_soc_dapm_widget *widget)
973{
974 int i;
975
976 for (i = 0; i < list->num_widgets; i++) {
977 if (widget == list->widgets[i])
978 return 1;
979 }
980
981 return 0;
982}
983
Liam Girdwood23607022014-01-17 17:03:55 +0000984int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +0100985 int stream, struct snd_soc_dapm_widget_list **list_)
986{
987 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
988 struct snd_soc_dapm_widget_list *list;
989 int paths;
990
991 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
992 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
993 if (list == NULL)
994 return -ENOMEM;
995
996 /* get number of valid DAI paths and their widgets */
997 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
998
Liam Girdwood103d84a2012-11-19 14:39:15 +0000999 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001000 stream ? "capture" : "playback");
1001
1002 *list_ = list;
1003 return paths;
1004}
1005
Liam Girdwood01d75842012-04-25 12:12:49 +01001006static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1007 struct snd_soc_dapm_widget_list **list_)
1008{
1009 struct snd_soc_dpcm *dpcm;
1010 struct snd_soc_dapm_widget_list *list = *list_;
1011 struct snd_soc_dapm_widget *widget;
1012 int prune = 0;
1013
1014 /* Destroy any old FE <--> BE connections */
1015 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1016
1017 /* is there a valid CPU DAI widget for this BE */
1018 widget = rtd_get_cpu_widget(dpcm->be, stream);
1019
1020 /* prune the BE if it's no longer in our active list */
1021 if (widget && widget_in_list(list, widget))
1022 continue;
1023
1024 /* is there a valid CODEC DAI widget for this BE */
1025 widget = rtd_get_codec_widget(dpcm->be, stream);
1026
1027 /* prune the BE if it's no longer in our active list */
1028 if (widget && widget_in_list(list, widget))
1029 continue;
1030
Liam Girdwood103d84a2012-11-19 14:39:15 +00001031 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001032 stream ? "capture" : "playback",
1033 dpcm->be->dai_link->name, fe->dai_link->name);
1034 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1035 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1036 prune++;
1037 }
1038
Liam Girdwood103d84a2012-11-19 14:39:15 +00001039 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001040 return prune;
1041}
1042
1043static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1044 struct snd_soc_dapm_widget_list **list_)
1045{
1046 struct snd_soc_card *card = fe->card;
1047 struct snd_soc_dapm_widget_list *list = *list_;
1048 struct snd_soc_pcm_runtime *be;
1049 int i, new = 0, err;
1050
1051 /* Create any new FE <--> BE connections */
1052 for (i = 0; i < list->num_widgets; i++) {
1053
Mark Brown46162742013-06-05 19:36:11 +01001054 switch (list->widgets[i]->id) {
1055 case snd_soc_dapm_dai_in:
1056 case snd_soc_dapm_dai_out:
1057 break;
1058 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001059 continue;
Mark Brown46162742013-06-05 19:36:11 +01001060 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001061
1062 /* is there a valid BE rtd for this widget */
1063 be = dpcm_get_be(card, list->widgets[i], stream);
1064 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001065 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001066 list->widgets[i]->name);
1067 continue;
1068 }
1069
1070 /* make sure BE is a real BE */
1071 if (!be->dai_link->no_pcm)
1072 continue;
1073
1074 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001075 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001076 continue;
1077
1078 /* newly connected FE and BE */
1079 err = dpcm_be_connect(fe, be, stream);
1080 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001081 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001082 list->widgets[i]->name);
1083 break;
1084 } else if (err == 0) /* already connected */
1085 continue;
1086
1087 /* new */
1088 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1089 new++;
1090 }
1091
Liam Girdwood103d84a2012-11-19 14:39:15 +00001092 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001093 return new;
1094}
1095
1096/*
1097 * Find the corresponding BE DAIs that source or sink audio to this
1098 * FE substream.
1099 */
Liam Girdwood23607022014-01-17 17:03:55 +00001100int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001101 int stream, struct snd_soc_dapm_widget_list **list, int new)
1102{
1103 if (new)
1104 return dpcm_add_paths(fe, stream, list);
1105 else
1106 return dpcm_prune_paths(fe, stream, list);
1107}
1108
Liam Girdwood23607022014-01-17 17:03:55 +00001109void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001110{
1111 struct snd_soc_dpcm *dpcm;
1112
1113 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1114 dpcm->be->dpcm[stream].runtime_update =
1115 SND_SOC_DPCM_UPDATE_NO;
1116}
1117
1118static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1119 int stream)
1120{
1121 struct snd_soc_dpcm *dpcm;
1122
1123 /* disable any enabled and non active backends */
1124 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1125
1126 struct snd_soc_pcm_runtime *be = dpcm->be;
1127 struct snd_pcm_substream *be_substream =
1128 snd_soc_dpcm_get_substream(be, stream);
1129
1130 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001131 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001132 stream ? "capture" : "playback",
1133 be->dpcm[stream].state);
1134
1135 if (--be->dpcm[stream].users != 0)
1136 continue;
1137
1138 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1139 continue;
1140
1141 soc_pcm_close(be_substream);
1142 be_substream->runtime = NULL;
1143 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1144 }
1145}
1146
Liam Girdwood23607022014-01-17 17:03:55 +00001147int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001148{
1149 struct snd_soc_dpcm *dpcm;
1150 int err, count = 0;
1151
1152 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1153 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1154
1155 struct snd_soc_pcm_runtime *be = dpcm->be;
1156 struct snd_pcm_substream *be_substream =
1157 snd_soc_dpcm_get_substream(be, stream);
1158
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001159 if (!be_substream) {
1160 dev_err(be->dev, "ASoC: no backend %s stream\n",
1161 stream ? "capture" : "playback");
1162 continue;
1163 }
1164
Liam Girdwood01d75842012-04-25 12:12:49 +01001165 /* is this op for this BE ? */
1166 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1167 continue;
1168
1169 /* first time the dpcm is open ? */
1170 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001171 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001172 stream ? "capture" : "playback",
1173 be->dpcm[stream].state);
1174
1175 if (be->dpcm[stream].users++ != 0)
1176 continue;
1177
1178 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1179 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1180 continue;
1181
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001182 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1183 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001184
1185 be_substream->runtime = be->dpcm[stream].runtime;
1186 err = soc_pcm_open(be_substream);
1187 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001188 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001189 be->dpcm[stream].users--;
1190 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001191 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001192 stream ? "capture" : "playback",
1193 be->dpcm[stream].state);
1194
1195 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1196 goto unwind;
1197 }
1198
1199 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1200 count++;
1201 }
1202
1203 return count;
1204
1205unwind:
1206 /* disable any enabled and non active backends */
1207 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
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 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1213 continue;
1214
1215 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001216 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001217 stream ? "capture" : "playback",
1218 be->dpcm[stream].state);
1219
1220 if (--be->dpcm[stream].users != 0)
1221 continue;
1222
1223 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1224 continue;
1225
1226 soc_pcm_close(be_substream);
1227 be_substream->runtime = NULL;
1228 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1229 }
1230
1231 return err;
1232}
1233
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001234static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1235 struct snd_soc_pcm_stream *stream)
1236{
1237 runtime->hw.rate_min = stream->rate_min;
1238 runtime->hw.rate_max = stream->rate_max;
1239 runtime->hw.channels_min = stream->channels_min;
1240 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001241 if (runtime->hw.formats)
1242 runtime->hw.formats &= stream->formats;
1243 else
1244 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001245 runtime->hw.rates = stream->rates;
1246}
1247
Mark Brown45c0a182012-05-09 21:46:27 +01001248static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001249{
1250 struct snd_pcm_runtime *runtime = substream->runtime;
1251 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1252 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1253 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1254
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001255 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1256 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1257 else
1258 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001259}
1260
1261static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1262{
1263 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1264 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1265 int stream = fe_substream->stream, ret = 0;
1266
1267 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1268
1269 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1270 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001271 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001272 goto be_err;
1273 }
1274
Liam Girdwood103d84a2012-11-19 14:39:15 +00001275 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001276
1277 /* start the DAI frontend */
1278 ret = soc_pcm_open(fe_substream);
1279 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001280 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001281 goto unwind;
1282 }
1283
1284 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1285
1286 dpcm_set_fe_runtime(fe_substream);
1287 snd_pcm_limit_hw_rates(runtime);
1288
1289 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1290 return 0;
1291
1292unwind:
1293 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1294be_err:
1295 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1296 return ret;
1297}
1298
Liam Girdwood23607022014-01-17 17:03:55 +00001299int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001300{
1301 struct snd_soc_dpcm *dpcm;
1302
1303 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1304 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1305
1306 struct snd_soc_pcm_runtime *be = dpcm->be;
1307 struct snd_pcm_substream *be_substream =
1308 snd_soc_dpcm_get_substream(be, stream);
1309
1310 /* is this op for this BE ? */
1311 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1312 continue;
1313
1314 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001315 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001316 stream ? "capture" : "playback",
1317 be->dpcm[stream].state);
1318
1319 if (--be->dpcm[stream].users != 0)
1320 continue;
1321
1322 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1323 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1324 continue;
1325
Liam Girdwood103d84a2012-11-19 14:39:15 +00001326 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001327 dpcm->fe->dai_link->name);
1328
1329 soc_pcm_close(be_substream);
1330 be_substream->runtime = NULL;
1331
1332 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1333 }
1334 return 0;
1335}
1336
1337static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1338{
1339 struct snd_soc_pcm_runtime *fe = substream->private_data;
1340 int stream = substream->stream;
1341
1342 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1343
1344 /* shutdown the BEs */
1345 dpcm_be_dai_shutdown(fe, substream->stream);
1346
Liam Girdwood103d84a2012-11-19 14:39:15 +00001347 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001348
1349 /* now shutdown the frontend */
1350 soc_pcm_close(substream);
1351
1352 /* run the stream event for each BE */
1353 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1354
1355 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1356 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1357 return 0;
1358}
1359
Liam Girdwood23607022014-01-17 17:03:55 +00001360int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001361{
1362 struct snd_soc_dpcm *dpcm;
1363
1364 /* only hw_params backends that are either sinks or sources
1365 * to this frontend DAI */
1366 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1367
1368 struct snd_soc_pcm_runtime *be = dpcm->be;
1369 struct snd_pcm_substream *be_substream =
1370 snd_soc_dpcm_get_substream(be, stream);
1371
1372 /* is this op for this BE ? */
1373 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1374 continue;
1375
1376 /* only free hw when no longer used - check all FEs */
1377 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1378 continue;
1379
1380 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1381 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1382 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001383 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001384 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1385 continue;
1386
Liam Girdwood103d84a2012-11-19 14:39:15 +00001387 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001388 dpcm->fe->dai_link->name);
1389
1390 soc_pcm_hw_free(be_substream);
1391
1392 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1393 }
1394
1395 return 0;
1396}
1397
Mark Brown45c0a182012-05-09 21:46:27 +01001398static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001399{
1400 struct snd_soc_pcm_runtime *fe = substream->private_data;
1401 int err, stream = substream->stream;
1402
1403 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1404 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1405
Liam Girdwood103d84a2012-11-19 14:39:15 +00001406 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001407
1408 /* call hw_free on the frontend */
1409 err = soc_pcm_hw_free(substream);
1410 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001411 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001412 fe->dai_link->name);
1413
1414 /* only hw_params backends that are either sinks or sources
1415 * to this frontend DAI */
1416 err = dpcm_be_dai_hw_free(fe, stream);
1417
1418 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1419 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1420
1421 mutex_unlock(&fe->card->mutex);
1422 return 0;
1423}
1424
Liam Girdwood23607022014-01-17 17:03:55 +00001425int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001426{
1427 struct snd_soc_dpcm *dpcm;
1428 int ret;
1429
1430 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1431
1432 struct snd_soc_pcm_runtime *be = dpcm->be;
1433 struct snd_pcm_substream *be_substream =
1434 snd_soc_dpcm_get_substream(be, stream);
1435
1436 /* is this op for this BE ? */
1437 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1438 continue;
1439
1440 /* only allow hw_params() if no connected FEs are running */
1441 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1442 continue;
1443
1444 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1445 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1447 continue;
1448
Liam Girdwood103d84a2012-11-19 14:39:15 +00001449 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001450 dpcm->fe->dai_link->name);
1451
1452 /* copy params for each dpcm */
1453 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1454 sizeof(struct snd_pcm_hw_params));
1455
1456 /* perform any hw_params fixups */
1457 if (be->dai_link->be_hw_params_fixup) {
1458 ret = be->dai_link->be_hw_params_fixup(be,
1459 &dpcm->hw_params);
1460 if (ret < 0) {
1461 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001462 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001463 ret);
1464 goto unwind;
1465 }
1466 }
1467
1468 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1469 if (ret < 0) {
1470 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001471 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001472 goto unwind;
1473 }
1474
1475 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1476 }
1477 return 0;
1478
1479unwind:
1480 /* disable any enabled and non active backends */
1481 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1482 struct snd_soc_pcm_runtime *be = dpcm->be;
1483 struct snd_pcm_substream *be_substream =
1484 snd_soc_dpcm_get_substream(be, stream);
1485
1486 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1487 continue;
1488
1489 /* only allow hw_free() if no connected FEs are running */
1490 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1491 continue;
1492
1493 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1494 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1495 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1496 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1497 continue;
1498
1499 soc_pcm_hw_free(be_substream);
1500 }
1501
1502 return ret;
1503}
1504
Mark Brown45c0a182012-05-09 21:46:27 +01001505static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1506 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001507{
1508 struct snd_soc_pcm_runtime *fe = substream->private_data;
1509 int ret, stream = substream->stream;
1510
1511 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1512 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1513
1514 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1515 sizeof(struct snd_pcm_hw_params));
1516 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1517 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001518 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001519 goto out;
1520 }
1521
Liam Girdwood103d84a2012-11-19 14:39:15 +00001522 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001523 fe->dai_link->name, params_rate(params),
1524 params_channels(params), params_format(params));
1525
1526 /* call hw_params on the frontend */
1527 ret = soc_pcm_hw_params(substream, params);
1528 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001529 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001530 dpcm_be_dai_hw_free(fe, stream);
1531 } else
1532 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1533
1534out:
1535 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1536 mutex_unlock(&fe->card->mutex);
1537 return ret;
1538}
1539
1540static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1541 struct snd_pcm_substream *substream, int cmd)
1542{
1543 int ret;
1544
Liam Girdwood103d84a2012-11-19 14:39:15 +00001545 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001546 dpcm->fe->dai_link->name, cmd);
1547
1548 ret = soc_pcm_trigger(substream, cmd);
1549 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001550 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001551
1552 return ret;
1553}
1554
Liam Girdwood23607022014-01-17 17:03:55 +00001555int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001556 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001557{
1558 struct snd_soc_dpcm *dpcm;
1559 int ret = 0;
1560
1561 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1562
1563 struct snd_soc_pcm_runtime *be = dpcm->be;
1564 struct snd_pcm_substream *be_substream =
1565 snd_soc_dpcm_get_substream(be, stream);
1566
1567 /* is this op for this BE ? */
1568 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1569 continue;
1570
1571 switch (cmd) {
1572 case SNDRV_PCM_TRIGGER_START:
1573 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1574 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1575 continue;
1576
1577 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1578 if (ret)
1579 return ret;
1580
1581 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1582 break;
1583 case SNDRV_PCM_TRIGGER_RESUME:
1584 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1585 continue;
1586
1587 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1588 if (ret)
1589 return ret;
1590
1591 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1592 break;
1593 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1594 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1595 continue;
1596
1597 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1598 if (ret)
1599 return ret;
1600
1601 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1602 break;
1603 case SNDRV_PCM_TRIGGER_STOP:
1604 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1605 continue;
1606
1607 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1608 continue;
1609
1610 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1611 if (ret)
1612 return ret;
1613
1614 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1615 break;
1616 case SNDRV_PCM_TRIGGER_SUSPEND:
1617 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1618 continue;
1619
1620 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1621 continue;
1622
1623 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1624 if (ret)
1625 return ret;
1626
1627 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1628 break;
1629 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1630 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1631 continue;
1632
1633 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1634 continue;
1635
1636 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1637 if (ret)
1638 return ret;
1639
1640 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1641 break;
1642 }
1643 }
1644
1645 return ret;
1646}
1647EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1648
Mark Brown45c0a182012-05-09 21:46:27 +01001649static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001650{
1651 struct snd_soc_pcm_runtime *fe = substream->private_data;
1652 int stream = substream->stream, ret;
1653 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1654
1655 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1656
1657 switch (trigger) {
1658 case SND_SOC_DPCM_TRIGGER_PRE:
1659 /* call trigger on the frontend before the backend. */
1660
Liam Girdwood103d84a2012-11-19 14:39:15 +00001661 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001662 fe->dai_link->name, cmd);
1663
1664 ret = soc_pcm_trigger(substream, cmd);
1665 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001666 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001667 goto out;
1668 }
1669
1670 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1671 break;
1672 case SND_SOC_DPCM_TRIGGER_POST:
1673 /* call trigger on the frontend after the backend. */
1674
1675 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1676 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001677 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001678 goto out;
1679 }
1680
Liam Girdwood103d84a2012-11-19 14:39:15 +00001681 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001682 fe->dai_link->name, cmd);
1683
1684 ret = soc_pcm_trigger(substream, cmd);
1685 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001686 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1687 /* bespoke trigger() - handles both FE and BEs */
1688
Liam Girdwood103d84a2012-11-19 14:39:15 +00001689 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001690 fe->dai_link->name, cmd);
1691
1692 ret = soc_pcm_bespoke_trigger(substream, cmd);
1693 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001694 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001695 goto out;
1696 }
1697 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001698 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001699 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001700 fe->dai_link->name);
1701 ret = -EINVAL;
1702 goto out;
1703 }
1704
1705 switch (cmd) {
1706 case SNDRV_PCM_TRIGGER_START:
1707 case SNDRV_PCM_TRIGGER_RESUME:
1708 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1709 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1710 break;
1711 case SNDRV_PCM_TRIGGER_STOP:
1712 case SNDRV_PCM_TRIGGER_SUSPEND:
1713 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1714 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1715 break;
1716 }
1717
1718out:
1719 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1720 return ret;
1721}
1722
Liam Girdwood23607022014-01-17 17:03:55 +00001723int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001724{
1725 struct snd_soc_dpcm *dpcm;
1726 int ret = 0;
1727
1728 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1729
1730 struct snd_soc_pcm_runtime *be = dpcm->be;
1731 struct snd_pcm_substream *be_substream =
1732 snd_soc_dpcm_get_substream(be, stream);
1733
1734 /* is this op for this BE ? */
1735 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1736 continue;
1737
1738 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1739 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1740 continue;
1741
Liam Girdwood103d84a2012-11-19 14:39:15 +00001742 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001743 dpcm->fe->dai_link->name);
1744
1745 ret = soc_pcm_prepare(be_substream);
1746 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001747 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001748 ret);
1749 break;
1750 }
1751
1752 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1753 }
1754 return ret;
1755}
1756
Mark Brown45c0a182012-05-09 21:46:27 +01001757static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001758{
1759 struct snd_soc_pcm_runtime *fe = substream->private_data;
1760 int stream = substream->stream, ret = 0;
1761
1762 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1763
Liam Girdwood103d84a2012-11-19 14:39:15 +00001764 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001765
1766 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1767
1768 /* there is no point preparing this FE if there are no BEs */
1769 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001770 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001771 fe->dai_link->name);
1772 ret = -EINVAL;
1773 goto out;
1774 }
1775
1776 ret = dpcm_be_dai_prepare(fe, substream->stream);
1777 if (ret < 0)
1778 goto out;
1779
1780 /* call prepare on the frontend */
1781 ret = soc_pcm_prepare(substream);
1782 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001783 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001784 fe->dai_link->name);
1785 goto out;
1786 }
1787
1788 /* run the stream event for each BE */
1789 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1790 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1791
1792out:
1793 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1794 mutex_unlock(&fe->card->mutex);
1795
1796 return ret;
1797}
1798
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001799static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1800 unsigned int cmd, void *arg)
1801{
1802 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1803 struct snd_soc_platform *platform = rtd->platform;
1804
Mark Brownc5914b02013-10-30 17:47:39 -07001805 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001806 return platform->driver->ops->ioctl(substream, cmd, arg);
1807 return snd_pcm_lib_ioctl(substream, cmd, arg);
1808}
1809
Liam Girdwood618dae12012-04-25 12:12:51 +01001810static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1811{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001812 struct snd_pcm_substream *substream =
1813 snd_soc_dpcm_get_substream(fe, stream);
1814 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001815 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001816
Liam Girdwood103d84a2012-11-19 14:39:15 +00001817 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001818 stream ? "capture" : "playback", fe->dai_link->name);
1819
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001820 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1821 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001822 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001823 fe->dai_link->name);
1824
1825 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1826 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001827 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001828 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001829 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001830 fe->dai_link->name);
1831
1832 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1833 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001834 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001835 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001836
1837 err = dpcm_be_dai_hw_free(fe, stream);
1838 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001839 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001840
1841 err = dpcm_be_dai_shutdown(fe, stream);
1842 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001843 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001844
1845 /* run the stream event for each BE */
1846 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1847
1848 return 0;
1849}
1850
1851static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1852{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001853 struct snd_pcm_substream *substream =
1854 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001855 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001856 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001857 int ret;
1858
Liam Girdwood103d84a2012-11-19 14:39:15 +00001859 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001860 stream ? "capture" : "playback", fe->dai_link->name);
1861
1862 /* Only start the BE if the FE is ready */
1863 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1864 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1865 return -EINVAL;
1866
1867 /* startup must always be called for new BEs */
1868 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001869 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001870 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001871
1872 /* keep going if FE state is > open */
1873 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1874 return 0;
1875
1876 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001877 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001878 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001879
1880 /* keep going if FE state is > hw_params */
1881 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1882 return 0;
1883
1884
1885 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001886 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001887 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001888
1889 /* run the stream event for each BE */
1890 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1891
1892 /* keep going if FE state is > prepare */
1893 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1894 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1895 return 0;
1896
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001897 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1898 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001899 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001900 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001901
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001902 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1903 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001904 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001905 goto hw_free;
1906 }
1907 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001908 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001909 fe->dai_link->name);
1910
1911 ret = dpcm_be_dai_trigger(fe, stream,
1912 SNDRV_PCM_TRIGGER_START);
1913 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001914 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001915 goto hw_free;
1916 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001917 }
1918
1919 return 0;
1920
1921hw_free:
1922 dpcm_be_dai_hw_free(fe, stream);
1923close:
1924 dpcm_be_dai_shutdown(fe, stream);
1925disconnect:
1926 /* disconnect any non started BEs */
1927 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1928 struct snd_soc_pcm_runtime *be = dpcm->be;
1929 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1930 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1931 }
1932
1933 return ret;
1934}
1935
1936static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1937{
1938 int ret;
1939
1940 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1941 ret = dpcm_run_update_startup(fe, stream);
1942 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001943 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001944 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1945
1946 return ret;
1947}
1948
1949static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1950{
1951 int ret;
1952
1953 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1954 ret = dpcm_run_update_shutdown(fe, stream);
1955 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001956 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001957 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1958
1959 return ret;
1960}
1961
1962/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1963 * any DAI links.
1964 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001965int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001966{
Liam Girdwood618dae12012-04-25 12:12:51 +01001967 int i, old, new, paths;
1968
Liam Girdwood618dae12012-04-25 12:12:51 +01001969 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1970 for (i = 0; i < card->num_rtd; i++) {
1971 struct snd_soc_dapm_widget_list *list;
1972 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1973
1974 /* make sure link is FE */
1975 if (!fe->dai_link->dynamic)
1976 continue;
1977
1978 /* only check active links */
1979 if (!fe->cpu_dai->active)
1980 continue;
1981
1982 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001983 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001984 fe->dai_link->name);
1985
1986 /* skip if FE doesn't have playback capability */
1987 if (!fe->cpu_dai->driver->playback.channels_min)
1988 goto capture;
1989
1990 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1991 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001992 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001993 fe->dai_link->name, "playback");
1994 mutex_unlock(&card->mutex);
1995 return paths;
1996 }
1997
1998 /* update any new playback paths */
1999 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2000 if (new) {
2001 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2002 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2003 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2004 }
2005
2006 /* update any old playback paths */
2007 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2008 if (old) {
2009 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2010 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2011 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2012 }
2013
2014capture:
2015 /* skip if FE doesn't have capture capability */
2016 if (!fe->cpu_dai->driver->capture.channels_min)
2017 continue;
2018
2019 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2020 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002021 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002022 fe->dai_link->name, "capture");
2023 mutex_unlock(&card->mutex);
2024 return paths;
2025 }
2026
2027 /* update any new capture paths */
2028 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2029 if (new) {
2030 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2031 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2032 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2033 }
2034
2035 /* update any old capture paths */
2036 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2037 if (old) {
2038 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2039 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2040 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2041 }
2042
2043 dpcm_path_put(&list);
2044 }
2045
2046 mutex_unlock(&card->mutex);
2047 return 0;
2048}
Liam Girdwood01d75842012-04-25 12:12:49 +01002049int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2050{
2051 struct snd_soc_dpcm *dpcm;
2052 struct list_head *clients =
2053 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2054
2055 list_for_each_entry(dpcm, clients, list_be) {
2056
2057 struct snd_soc_pcm_runtime *be = dpcm->be;
2058 struct snd_soc_dai *dai = be->codec_dai;
2059 struct snd_soc_dai_driver *drv = dai->driver;
2060
2061 if (be->dai_link->ignore_suspend)
2062 continue;
2063
Liam Girdwood103d84a2012-11-19 14:39:15 +00002064 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002065
Mark Brownc5914b02013-10-30 17:47:39 -07002066 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2067 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002068 }
2069
2070 return 0;
2071}
2072
Mark Brown45c0a182012-05-09 21:46:27 +01002073static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002074{
2075 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2076 struct snd_soc_dpcm *dpcm;
2077 struct snd_soc_dapm_widget_list *list;
2078 int ret;
2079 int stream = fe_substream->stream;
2080
2081 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2082 fe->dpcm[stream].runtime = fe_substream->runtime;
2083
2084 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002085 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002086 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002087 }
2088
2089 /* calculate valid and active FE <-> BE dpcms */
2090 dpcm_process_paths(fe, stream, &list, 1);
2091
2092 ret = dpcm_fe_dai_startup(fe_substream);
2093 if (ret < 0) {
2094 /* clean up all links */
2095 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2096 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2097
2098 dpcm_be_disconnect(fe, stream);
2099 fe->dpcm[stream].runtime = NULL;
2100 }
2101
2102 dpcm_clear_pending_state(fe, stream);
2103 dpcm_path_put(&list);
2104 mutex_unlock(&fe->card->mutex);
2105 return ret;
2106}
2107
Mark Brown45c0a182012-05-09 21:46:27 +01002108static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002109{
2110 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2111 struct snd_soc_dpcm *dpcm;
2112 int stream = fe_substream->stream, ret;
2113
2114 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2115 ret = dpcm_fe_dai_shutdown(fe_substream);
2116
2117 /* mark FE's links ready to prune */
2118 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2119 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2120
2121 dpcm_be_disconnect(fe, stream);
2122
2123 fe->dpcm[stream].runtime = NULL;
2124 mutex_unlock(&fe->card->mutex);
2125 return ret;
2126}
2127
Liam Girdwoodddee6272011-06-09 14:45:53 +01002128/* create a new pcm */
2129int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2130{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002131 struct snd_soc_platform *platform = rtd->platform;
2132 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2133 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2134 struct snd_pcm *pcm;
2135 char new_name[64];
2136 int ret = 0, playback = 0, capture = 0;
2137
Liam Girdwood01d75842012-04-25 12:12:49 +01002138 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002139 playback = rtd->dai_link->dpcm_playback;
2140 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002141 } else {
Mark Brown05679092013-06-01 23:13:53 +01002142 if (codec_dai->driver->playback.channels_min &&
2143 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002144 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002145 if (codec_dai->driver->capture.channels_min &&
2146 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002147 capture = 1;
2148 }
Sangsu Parka5002312012-01-02 17:15:10 +09002149
Fabio Estevamd6bead02013-08-29 10:32:13 -03002150 if (rtd->dai_link->playback_only) {
2151 playback = 1;
2152 capture = 0;
2153 }
2154
2155 if (rtd->dai_link->capture_only) {
2156 playback = 0;
2157 capture = 1;
2158 }
2159
Liam Girdwood01d75842012-04-25 12:12:49 +01002160 /* create the PCM */
2161 if (rtd->dai_link->no_pcm) {
2162 snprintf(new_name, sizeof(new_name), "(%s)",
2163 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002164
Liam Girdwood01d75842012-04-25 12:12:49 +01002165 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2166 playback, capture, &pcm);
2167 } else {
2168 if (rtd->dai_link->dynamic)
2169 snprintf(new_name, sizeof(new_name), "%s (*)",
2170 rtd->dai_link->stream_name);
2171 else
2172 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2173 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002174
Liam Girdwood01d75842012-04-25 12:12:49 +01002175 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2176 capture, &pcm);
2177 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002178 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002179 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002180 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002181 return ret;
2182 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002183 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002184
2185 /* DAPM dai link stream work */
2186 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2187
2188 rtd->pcm = pcm;
2189 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002190
2191 if (rtd->dai_link->no_pcm) {
2192 if (playback)
2193 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2194 if (capture)
2195 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2196 goto out;
2197 }
2198
2199 /* ASoC PCM operations */
2200 if (rtd->dai_link->dynamic) {
2201 rtd->ops.open = dpcm_fe_dai_open;
2202 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2203 rtd->ops.prepare = dpcm_fe_dai_prepare;
2204 rtd->ops.trigger = dpcm_fe_dai_trigger;
2205 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2206 rtd->ops.close = dpcm_fe_dai_close;
2207 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002208 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002209 } else {
2210 rtd->ops.open = soc_pcm_open;
2211 rtd->ops.hw_params = soc_pcm_hw_params;
2212 rtd->ops.prepare = soc_pcm_prepare;
2213 rtd->ops.trigger = soc_pcm_trigger;
2214 rtd->ops.hw_free = soc_pcm_hw_free;
2215 rtd->ops.close = soc_pcm_close;
2216 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002217 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002218 }
2219
Liam Girdwoodddee6272011-06-09 14:45:53 +01002220 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002221 rtd->ops.ack = platform->driver->ops->ack;
2222 rtd->ops.copy = platform->driver->ops->copy;
2223 rtd->ops.silence = platform->driver->ops->silence;
2224 rtd->ops.page = platform->driver->ops->page;
2225 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002226 }
2227
2228 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002229 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002230
2231 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002232 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002233
2234 if (platform->driver->pcm_new) {
2235 ret = platform->driver->pcm_new(rtd);
2236 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002237 dev_err(platform->dev,
2238 "ASoC: pcm constructor failed: %d\n",
2239 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002240 return ret;
2241 }
2242 }
2243
2244 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002245out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002246 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002247 cpu_dai->name);
2248 return ret;
2249}
Liam Girdwood01d75842012-04-25 12:12:49 +01002250
2251/* is the current PCM operation for this FE ? */
2252int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2253{
2254 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2255 return 1;
2256 return 0;
2257}
2258EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2259
2260/* is the current PCM operation for this BE ? */
2261int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2262 struct snd_soc_pcm_runtime *be, int stream)
2263{
2264 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2265 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2266 be->dpcm[stream].runtime_update))
2267 return 1;
2268 return 0;
2269}
2270EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2271
2272/* get the substream for this BE */
2273struct snd_pcm_substream *
2274 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2275{
2276 return be->pcm->streams[stream].substream;
2277}
2278EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2279
2280/* get the BE runtime state */
2281enum snd_soc_dpcm_state
2282 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2283{
2284 return be->dpcm[stream].state;
2285}
2286EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2287
2288/* set the BE runtime state */
2289void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2290 int stream, enum snd_soc_dpcm_state state)
2291{
2292 be->dpcm[stream].state = state;
2293}
2294EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2295
2296/*
2297 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2298 * are not running, paused or suspended for the specified stream direction.
2299 */
2300int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2301 struct snd_soc_pcm_runtime *be, int stream)
2302{
2303 struct snd_soc_dpcm *dpcm;
2304 int state;
2305
2306 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2307
2308 if (dpcm->fe == fe)
2309 continue;
2310
2311 state = dpcm->fe->dpcm[stream].state;
2312 if (state == SND_SOC_DPCM_STATE_START ||
2313 state == SND_SOC_DPCM_STATE_PAUSED ||
2314 state == SND_SOC_DPCM_STATE_SUSPEND)
2315 return 0;
2316 }
2317
2318 /* it's safe to free/stop this BE DAI */
2319 return 1;
2320}
2321EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2322
2323/*
2324 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2325 * running, paused or suspended for the specified stream direction.
2326 */
2327int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2328 struct snd_soc_pcm_runtime *be, int stream)
2329{
2330 struct snd_soc_dpcm *dpcm;
2331 int state;
2332
2333 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2334
2335 if (dpcm->fe == fe)
2336 continue;
2337
2338 state = dpcm->fe->dpcm[stream].state;
2339 if (state == SND_SOC_DPCM_STATE_START ||
2340 state == SND_SOC_DPCM_STATE_PAUSED ||
2341 state == SND_SOC_DPCM_STATE_SUSPEND ||
2342 state == SND_SOC_DPCM_STATE_PREPARE)
2343 return 0;
2344 }
2345
2346 /* it's safe to change hw_params */
2347 return 1;
2348}
2349EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002350
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002351int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2352 int cmd, struct snd_soc_platform *platform)
2353{
Mark Brownc5914b02013-10-30 17:47:39 -07002354 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002355 return platform->driver->ops->trigger(substream, cmd);
2356 return 0;
2357}
2358EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2359
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002360#ifdef CONFIG_DEBUG_FS
2361static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2362{
2363 switch (state) {
2364 case SND_SOC_DPCM_STATE_NEW:
2365 return "new";
2366 case SND_SOC_DPCM_STATE_OPEN:
2367 return "open";
2368 case SND_SOC_DPCM_STATE_HW_PARAMS:
2369 return "hw_params";
2370 case SND_SOC_DPCM_STATE_PREPARE:
2371 return "prepare";
2372 case SND_SOC_DPCM_STATE_START:
2373 return "start";
2374 case SND_SOC_DPCM_STATE_STOP:
2375 return "stop";
2376 case SND_SOC_DPCM_STATE_SUSPEND:
2377 return "suspend";
2378 case SND_SOC_DPCM_STATE_PAUSED:
2379 return "paused";
2380 case SND_SOC_DPCM_STATE_HW_FREE:
2381 return "hw_free";
2382 case SND_SOC_DPCM_STATE_CLOSE:
2383 return "close";
2384 }
2385
2386 return "unknown";
2387}
2388
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002389static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2390 int stream, char *buf, size_t size)
2391{
2392 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2393 struct snd_soc_dpcm *dpcm;
2394 ssize_t offset = 0;
2395
2396 /* FE state */
2397 offset += snprintf(buf + offset, size - offset,
2398 "[%s - %s]\n", fe->dai_link->name,
2399 stream ? "Capture" : "Playback");
2400
2401 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2402 dpcm_state_string(fe->dpcm[stream].state));
2403
2404 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2405 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2406 offset += snprintf(buf + offset, size - offset,
2407 "Hardware Params: "
2408 "Format = %s, Channels = %d, Rate = %d\n",
2409 snd_pcm_format_name(params_format(params)),
2410 params_channels(params),
2411 params_rate(params));
2412
2413 /* BEs state */
2414 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2415
2416 if (list_empty(&fe->dpcm[stream].be_clients)) {
2417 offset += snprintf(buf + offset, size - offset,
2418 " No active DSP links\n");
2419 goto out;
2420 }
2421
2422 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2423 struct snd_soc_pcm_runtime *be = dpcm->be;
2424 params = &dpcm->hw_params;
2425
2426 offset += snprintf(buf + offset, size - offset,
2427 "- %s\n", be->dai_link->name);
2428
2429 offset += snprintf(buf + offset, size - offset,
2430 " State: %s\n",
2431 dpcm_state_string(be->dpcm[stream].state));
2432
2433 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2434 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2435 offset += snprintf(buf + offset, size - offset,
2436 " Hardware Params: "
2437 "Format = %s, Channels = %d, Rate = %d\n",
2438 snd_pcm_format_name(params_format(params)),
2439 params_channels(params),
2440 params_rate(params));
2441 }
2442
2443out:
2444 return offset;
2445}
2446
2447static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2448 size_t count, loff_t *ppos)
2449{
2450 struct snd_soc_pcm_runtime *fe = file->private_data;
2451 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2452 char *buf;
2453
2454 buf = kmalloc(out_count, GFP_KERNEL);
2455 if (!buf)
2456 return -ENOMEM;
2457
2458 if (fe->cpu_dai->driver->playback.channels_min)
2459 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2460 buf + offset, out_count - offset);
2461
2462 if (fe->cpu_dai->driver->capture.channels_min)
2463 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2464 buf + offset, out_count - offset);
2465
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002466 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002467
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002468 kfree(buf);
2469 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002470}
2471
2472static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002473 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002474 .read = dpcm_state_read_file,
2475 .llseek = default_llseek,
2476};
2477
2478int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2479{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002480 if (!rtd->dai_link)
2481 return 0;
2482
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002483 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2484 rtd->card->debugfs_card_root);
2485 if (!rtd->debugfs_dpcm_root) {
2486 dev_dbg(rtd->dev,
2487 "ASoC: Failed to create dpcm debugfs directory %s\n",
2488 rtd->dai_link->name);
2489 return -EINVAL;
2490 }
2491
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002492 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002493 rtd->debugfs_dpcm_root,
2494 rtd, &dpcm_state_fops);
2495
2496 return 0;
2497}
2498#endif