blob: 54d18f22a33e4716b05292f252b53e553d85f8ba [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/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010038 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48{
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
50 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 codec_dai->playback_active++;
57 } else {
58 cpu_dai->capture_active++;
59 codec_dai->capture_active++;
60 }
61
62 cpu_dai->active++;
63 codec_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010064 cpu_dai->component->active++;
65 codec_dai->component->active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010066}
67
68/**
69 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
70 * @rtd: ASoC PCM runtime that is deactivated
71 * @stream: Direction of the PCM stream
72 *
73 * Decrements the active count for all the DAIs and components attached to a PCM
74 * runtime. Should typically be called when a stream is closed.
75 *
76 * Must be called with the rtd->pcm_mutex being held
77 */
78void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
79{
80 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
81 struct snd_soc_dai *codec_dai = rtd->codec_dai;
82
83 lockdep_assert_held(&rtd->pcm_mutex);
84
85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 cpu_dai->playback_active--;
87 codec_dai->playback_active--;
88 } else {
89 cpu_dai->capture_active--;
90 codec_dai->capture_active--;
91 }
92
93 cpu_dai->active--;
94 codec_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010095 cpu_dai->component->active--;
96 codec_dai->component->active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010097}
98
99/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100100 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
101 * @rtd: The ASoC PCM runtime that should be checked.
102 *
103 * This function checks whether the power down delay should be ignored for a
104 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
105 * been configured to ignore the delay, or if none of the components benefits
106 * from having the delay.
107 */
108bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
109{
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100110 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
111 return true;
112
Lars-Peter Clausen3d594002014-03-05 13:17:48 +0100113 return rtd->cpu_dai->component->ignore_pmdown_time &&
114 rtd->codec_dai->component->ignore_pmdown_time;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100115}
116
117/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200118 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
119 * @substream: the pcm substream
120 * @hw: the hardware parameters
121 *
122 * Sets the substream runtime hardware parameters.
123 */
124int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
125 const struct snd_pcm_hardware *hw)
126{
127 struct snd_pcm_runtime *runtime = substream->runtime;
128 runtime->hw.info = hw->info;
129 runtime->hw.formats = hw->formats;
130 runtime->hw.period_bytes_min = hw->period_bytes_min;
131 runtime->hw.period_bytes_max = hw->period_bytes_max;
132 runtime->hw.periods_min = hw->periods_min;
133 runtime->hw.periods_max = hw->periods_max;
134 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
135 runtime->hw.fifo_size = hw->fifo_size;
136 return 0;
137}
138EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
139
Liam Girdwood01d75842012-04-25 12:12:49 +0100140/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000141int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100142 int event)
143{
144 struct snd_soc_dpcm *dpcm;
145
146 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
147
148 struct snd_soc_pcm_runtime *be = dpcm->be;
149
Liam Girdwood103d84a2012-11-19 14:39:15 +0000150 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100151 be->dai_link->name, event, dir);
152
153 snd_soc_dapm_stream_event(be, dir, event);
154 }
155
156 snd_soc_dapm_stream_event(fe, dir, event);
157
158 return 0;
159}
160
Dong Aisheng17841022011-08-29 17:15:14 +0800161static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
162 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100163{
164 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100165 int ret;
166
Nicolin Chen3635bf02013-11-13 18:56:24 +0800167 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
168 rtd->dai_link->symmetric_rates)) {
169 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
170 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100171
Nicolin Chen3635bf02013-11-13 18:56:24 +0800172 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
173 SNDRV_PCM_HW_PARAM_RATE,
174 soc_dai->rate, soc_dai->rate);
175 if (ret < 0) {
176 dev_err(soc_dai->dev,
177 "ASoC: Unable to apply rate constraint: %d\n",
178 ret);
179 return ret;
180 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100181 }
182
Nicolin Chen3635bf02013-11-13 18:56:24 +0800183 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
184 rtd->dai_link->symmetric_channels)) {
185 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
186 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100187
Nicolin Chen3635bf02013-11-13 18:56:24 +0800188 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
189 SNDRV_PCM_HW_PARAM_CHANNELS,
190 soc_dai->channels,
191 soc_dai->channels);
192 if (ret < 0) {
193 dev_err(soc_dai->dev,
194 "ASoC: Unable to apply channel symmetry constraint: %d\n",
195 ret);
196 return ret;
197 }
198 }
199
200 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
201 rtd->dai_link->symmetric_samplebits)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
203 soc_dai->sample_bits);
204
205 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
206 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
207 soc_dai->sample_bits,
208 soc_dai->sample_bits);
209 if (ret < 0) {
210 dev_err(soc_dai->dev,
211 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
212 ret);
213 return ret;
214 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100215 }
216
217 return 0;
218}
219
Nicolin Chen3635bf02013-11-13 18:56:24 +0800220static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
221 struct snd_pcm_hw_params *params)
222{
223 struct snd_soc_pcm_runtime *rtd = substream->private_data;
224 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
225 struct snd_soc_dai *codec_dai = rtd->codec_dai;
226 unsigned int rate, channels, sample_bits, symmetry;
227
228 rate = params_rate(params);
229 channels = params_channels(params);
230 sample_bits = snd_pcm_format_physical_width(params_format(params));
231
232 /* reject unmatched parameters when applying symmetry */
233 symmetry = cpu_dai->driver->symmetric_rates ||
234 codec_dai->driver->symmetric_rates ||
235 rtd->dai_link->symmetric_rates;
236 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
237 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
238 cpu_dai->rate, rate);
239 return -EINVAL;
240 }
241
242 symmetry = cpu_dai->driver->symmetric_channels ||
243 codec_dai->driver->symmetric_channels ||
244 rtd->dai_link->symmetric_channels;
245 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
246 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
247 cpu_dai->channels, channels);
248 return -EINVAL;
249 }
250
251 symmetry = cpu_dai->driver->symmetric_samplebits ||
252 codec_dai->driver->symmetric_samplebits ||
253 rtd->dai_link->symmetric_samplebits;
254 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
255 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
256 cpu_dai->sample_bits, sample_bits);
257 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100258 }
259
260 return 0;
261}
262
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100263static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
264{
265 struct snd_soc_pcm_runtime *rtd = substream->private_data;
266 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
267 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
268 struct snd_soc_dai_link *link = rtd->dai_link;
269
270 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
271 link->symmetric_rates || cpu_driver->symmetric_channels ||
272 codec_driver->symmetric_channels || link->symmetric_channels ||
273 cpu_driver->symmetric_samplebits ||
274 codec_driver->symmetric_samplebits ||
275 link->symmetric_samplebits;
276}
277
Liam Girdwoodddee6272011-06-09 14:45:53 +0100278/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000279 * List of sample sizes that might go over the bus for parameter
280 * application. There ought to be a wildcard sample size for things
281 * like the DAC/ADC resolution to use but there isn't right now.
282 */
283static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200284 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000285};
286
287static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
288 struct snd_soc_dai *dai)
289{
290 int ret, i, bits;
291
292 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
293 bits = dai->driver->playback.sig_bits;
294 else
295 bits = dai->driver->capture.sig_bits;
296
297 if (!bits)
298 return;
299
300 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000301 if (bits >= sample_sizes[i])
302 continue;
303
304 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
305 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000306 if (ret != 0)
307 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000308 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000309 bits, sample_sizes[i], ret);
310 }
311}
312
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100313static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200314 struct snd_soc_pcm_stream *codec_stream,
315 struct snd_soc_pcm_stream *cpu_stream)
316{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100317 struct snd_pcm_hardware *hw = &runtime->hw;
318
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200319 hw->channels_min = max(codec_stream->channels_min,
320 cpu_stream->channels_min);
321 hw->channels_max = min(codec_stream->channels_max,
322 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100323 if (hw->formats)
324 hw->formats &= codec_stream->formats & cpu_stream->formats;
325 else
326 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausen55dcdb52014-01-11 10:24:44 +0100327 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates,
328 cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100329
Lars-Peter Clausen817873f2014-01-11 10:24:40 +0100330 hw->rate_min = 0;
331 hw->rate_max = UINT_MAX;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100332
333 snd_pcm_limit_hw_rates(runtime);
334
335 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
336 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
337 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
338 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200339}
340
Mark Brown58ba9b22012-01-16 18:38:51 +0000341/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100342 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
343 * then initialized and any private data can be allocated. This also calls
344 * startup for the cpu DAI, platform, machine and codec DAI.
345 */
346static int soc_pcm_open(struct snd_pcm_substream *substream)
347{
348 struct snd_soc_pcm_runtime *rtd = substream->private_data;
349 struct snd_pcm_runtime *runtime = substream->runtime;
350 struct snd_soc_platform *platform = rtd->platform;
351 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
352 struct snd_soc_dai *codec_dai = rtd->codec_dai;
353 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
354 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
355 int ret = 0;
356
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800357 pinctrl_pm_select_default_state(cpu_dai->dev);
358 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000359 pm_runtime_get_sync(cpu_dai->dev);
360 pm_runtime_get_sync(codec_dai->dev);
361 pm_runtime_get_sync(platform->dev);
362
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100363 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100364
365 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700366 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
368 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000369 dev_err(cpu_dai->dev, "ASoC: can't open interface"
370 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100371 goto out;
372 }
373 }
374
375 if (platform->driver->ops && platform->driver->ops->open) {
376 ret = platform->driver->ops->open(substream);
377 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000378 dev_err(platform->dev, "ASoC: can't open platform"
379 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100380 goto platform_err;
381 }
382 }
383
Mark Brownc5914b02013-10-30 17:47:39 -0700384 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100385 ret = codec_dai->driver->ops->startup(substream, codec_dai);
386 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000387 dev_err(codec_dai->dev, "ASoC: can't open codec"
388 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100389 goto codec_dai_err;
390 }
391 }
392
393 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
394 ret = rtd->dai_link->ops->startup(substream);
395 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000396 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000397 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100398 goto machine_err;
399 }
400 }
401
Liam Girdwood01d75842012-04-25 12:12:49 +0100402 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
403 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
404 goto dynamic;
405
Liam Girdwoodddee6272011-06-09 14:45:53 +0100406 /* Check that the codec and cpu DAIs are compatible */
407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100408 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200409 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100410 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100411 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200412 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100413 }
414
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100415 if (soc_pcm_has_symmetry(substream))
416 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
417
Liam Girdwoodddee6272011-06-09 14:45:53 +0100418 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100419 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000420 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100421 codec_dai->name, cpu_dai->name);
422 goto config_err;
423 }
424 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000425 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100426 codec_dai->name, cpu_dai->name);
427 goto config_err;
428 }
429 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
430 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000431 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432 codec_dai->name, cpu_dai->name);
433 goto config_err;
434 }
435
Mark Brown58ba9b22012-01-16 18:38:51 +0000436 soc_pcm_apply_msb(substream, codec_dai);
437 soc_pcm_apply_msb(substream, cpu_dai);
438
Liam Girdwoodddee6272011-06-09 14:45:53 +0100439 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800440 if (cpu_dai->active) {
441 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
442 if (ret != 0)
443 goto config_err;
444 }
445
446 if (codec_dai->active) {
447 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448 if (ret != 0)
449 goto config_err;
450 }
451
Liam Girdwood103d84a2012-11-19 14:39:15 +0000452 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000454 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
455 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100456 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000457 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100458 runtime->hw.rate_max);
459
Liam Girdwood01d75842012-04-25 12:12:49 +0100460dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100461
462 snd_soc_runtime_activate(rtd, substream->stream);
463
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100464 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100465 return 0;
466
467config_err:
468 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
469 rtd->dai_link->ops->shutdown(substream);
470
471machine_err:
472 if (codec_dai->driver->ops->shutdown)
473 codec_dai->driver->ops->shutdown(substream, codec_dai);
474
475codec_dai_err:
476 if (platform->driver->ops && platform->driver->ops->close)
477 platform->driver->ops->close(substream);
478
479platform_err:
480 if (cpu_dai->driver->ops->shutdown)
481 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
482out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100483 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000484
485 pm_runtime_put(platform->dev);
486 pm_runtime_put(codec_dai->dev);
487 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800488 if (!codec_dai->active)
489 pinctrl_pm_select_sleep_state(codec_dai->dev);
490 if (!cpu_dai->active)
491 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000492
Liam Girdwoodddee6272011-06-09 14:45:53 +0100493 return ret;
494}
495
496/*
497 * Power down the audio subsystem pmdown_time msecs after close is called.
498 * This is to ensure there are no pops or clicks in between any music tracks
499 * due to DAPM power cycling.
500 */
501static void close_delayed_work(struct work_struct *work)
502{
503 struct snd_soc_pcm_runtime *rtd =
504 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
505 struct snd_soc_dai *codec_dai = rtd->codec_dai;
506
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100507 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100508
Liam Girdwood103d84a2012-11-19 14:39:15 +0000509 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100510 codec_dai->driver->playback.stream_name,
511 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600512 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100513
514 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600515 if (rtd->pop_wait == 1) {
516 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800517 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000518 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519 }
520
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100521 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522}
523
524/*
525 * Called by ALSA when a PCM substream is closed. Private data can be
526 * freed here. The cpu DAI, codec DAI, machine and platform are also
527 * shutdown.
528 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100529static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100530{
531 struct snd_soc_pcm_runtime *rtd = substream->private_data;
532 struct snd_soc_platform *platform = rtd->platform;
533 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
534 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100535
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100536 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100537
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100538 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
Dong Aisheng17841022011-08-29 17:15:14 +0800540 /* clear the corresponding DAIs rate when inactive */
541 if (!cpu_dai->active)
542 cpu_dai->rate = 0;
543
544 if (!codec_dai->active)
545 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200546
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 if (cpu_dai->driver->ops->shutdown)
548 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
549
550 if (codec_dai->driver->ops->shutdown)
551 codec_dai->driver->ops->shutdown(substream, codec_dai);
552
553 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
554 rtd->dai_link->ops->shutdown(substream);
555
556 if (platform->driver->ops && platform->driver->ops->close)
557 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100558
559 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100560 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300561 /* powered down playback stream now */
562 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800563 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800564 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300565 } else {
566 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600567 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100568 queue_delayed_work(system_power_efficient_wq,
569 &rtd->delayed_work,
570 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300571 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100572 } else {
573 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800574 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000575 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100576 }
577
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100578 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000579
580 pm_runtime_put(platform->dev);
581 pm_runtime_put(codec_dai->dev);
582 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800583 if (!codec_dai->active)
584 pinctrl_pm_select_sleep_state(codec_dai->dev);
585 if (!cpu_dai->active)
586 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000587
Liam Girdwoodddee6272011-06-09 14:45:53 +0100588 return 0;
589}
590
591/*
592 * Called by ALSA when the PCM substream is prepared, can set format, sample
593 * rate, etc. This function is non atomic and can be called multiple times,
594 * it can refer to the runtime info.
595 */
596static int soc_pcm_prepare(struct snd_pcm_substream *substream)
597{
598 struct snd_soc_pcm_runtime *rtd = substream->private_data;
599 struct snd_soc_platform *platform = rtd->platform;
600 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
601 struct snd_soc_dai *codec_dai = rtd->codec_dai;
602 int ret = 0;
603
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100604 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100605
606 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
607 ret = rtd->dai_link->ops->prepare(substream);
608 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000609 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
610 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100611 goto out;
612 }
613 }
614
615 if (platform->driver->ops && platform->driver->ops->prepare) {
616 ret = platform->driver->ops->prepare(substream);
617 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000618 dev_err(platform->dev, "ASoC: platform prepare error:"
619 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100620 goto out;
621 }
622 }
623
Mark Brownc5914b02013-10-30 17:47:39 -0700624 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100625 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
626 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000627 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000628 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100629 goto out;
630 }
631 }
632
Mark Brownc5914b02013-10-30 17:47:39 -0700633 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100634 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
635 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000636 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000637 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100638 goto out;
639 }
640 }
641
642 /* cancel any delayed stream shutdown that is pending */
643 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600644 rtd->pop_wait) {
645 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100646 cancel_delayed_work(&rtd->delayed_work);
647 }
648
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000649 snd_soc_dapm_stream_event(rtd, substream->stream,
650 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651
Mark Brownda183962013-02-06 15:44:07 +0000652 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100653
654out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100655 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100656 return ret;
657}
658
659/*
660 * Called by ALSA when the hardware params are set by application. This
661 * function can also be called multiple times and can allocate buffers
662 * (using snd_pcm_lib_* ). It's non-atomic.
663 */
664static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
665 struct snd_pcm_hw_params *params)
666{
667 struct snd_soc_pcm_runtime *rtd = substream->private_data;
668 struct snd_soc_platform *platform = rtd->platform;
669 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
670 struct snd_soc_dai *codec_dai = rtd->codec_dai;
671 int ret = 0;
672
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100673 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100674
Nicolin Chen3635bf02013-11-13 18:56:24 +0800675 ret = soc_pcm_params_symmetry(substream, params);
676 if (ret)
677 goto out;
678
Liam Girdwoodddee6272011-06-09 14:45:53 +0100679 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
680 ret = rtd->dai_link->ops->hw_params(substream, params);
681 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000682 dev_err(rtd->card->dev, "ASoC: machine hw_params"
683 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100684 goto out;
685 }
686 }
687
Mark Brownc5914b02013-10-30 17:47:39 -0700688 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
690 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000691 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
692 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100693 goto codec_err;
694 }
695 }
696
Mark Brownc5914b02013-10-30 17:47:39 -0700697 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
699 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000700 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000701 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100702 goto interface_err;
703 }
704 }
705
706 if (platform->driver->ops && platform->driver->ops->hw_params) {
707 ret = platform->driver->ops->hw_params(substream, params);
708 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000709 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000710 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100711 goto platform_err;
712 }
713 }
714
Nicolin Chen3635bf02013-11-13 18:56:24 +0800715 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800716 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800717 cpu_dai->channels = params_channels(params);
718 cpu_dai->sample_bits =
719 snd_pcm_format_physical_width(params_format(params));
720
Dong Aisheng17841022011-08-29 17:15:14 +0800721 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800722 codec_dai->channels = params_channels(params);
723 codec_dai->sample_bits =
724 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100725
726out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100727 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100728 return ret;
729
730platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700731 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100732 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
733
734interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700735 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100736 codec_dai->driver->ops->hw_free(substream, codec_dai);
737
738codec_err:
739 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
740 rtd->dai_link->ops->hw_free(substream);
741
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100742 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100743 return ret;
744}
745
746/*
747 * Frees resources allocated by hw_params, can be called multiple times
748 */
749static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
750{
751 struct snd_soc_pcm_runtime *rtd = substream->private_data;
752 struct snd_soc_platform *platform = rtd->platform;
753 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
754 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800755 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100757 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100758
Nicolin Chend3383422013-11-20 18:37:09 +0800759 /* clear the corresponding DAIs parameters when going to be inactive */
760 if (cpu_dai->active == 1) {
761 cpu_dai->rate = 0;
762 cpu_dai->channels = 0;
763 cpu_dai->sample_bits = 0;
764 }
765
766 if (codec_dai->active == 1) {
767 codec_dai->rate = 0;
768 codec_dai->channels = 0;
769 codec_dai->sample_bits = 0;
770 }
771
Liam Girdwoodddee6272011-06-09 14:45:53 +0100772 /* apply codec digital mute */
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800773 if ((playback && codec_dai->playback_active == 1) ||
774 (!playback && codec_dai->capture_active == 1))
Mark Brownda183962013-02-06 15:44:07 +0000775 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100776
777 /* free any machine hw params */
778 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
779 rtd->dai_link->ops->hw_free(substream);
780
781 /* free any DMA resources */
782 if (platform->driver->ops && platform->driver->ops->hw_free)
783 platform->driver->ops->hw_free(substream);
784
785 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700786 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100787 codec_dai->driver->ops->hw_free(substream, codec_dai);
788
Mark Brownc5914b02013-10-30 17:47:39 -0700789 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100790 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
791
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100792 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100793 return 0;
794}
795
796static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
797{
798 struct snd_soc_pcm_runtime *rtd = substream->private_data;
799 struct snd_soc_platform *platform = rtd->platform;
800 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
801 struct snd_soc_dai *codec_dai = rtd->codec_dai;
802 int ret;
803
Mark Brownc5914b02013-10-30 17:47:39 -0700804 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100805 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
806 if (ret < 0)
807 return ret;
808 }
809
810 if (platform->driver->ops && platform->driver->ops->trigger) {
811 ret = platform->driver->ops->trigger(substream, cmd);
812 if (ret < 0)
813 return ret;
814 }
815
Mark Brownc5914b02013-10-30 17:47:39 -0700816 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100817 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
818 if (ret < 0)
819 return ret;
820 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +0200821
822 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
823 ret = rtd->dai_link->ops->trigger(substream, cmd);
824 if (ret < 0)
825 return ret;
826 }
827
Liam Girdwoodddee6272011-06-09 14:45:53 +0100828 return 0;
829}
830
Mark Brown45c0a182012-05-09 21:46:27 +0100831static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
832 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100833{
834 struct snd_soc_pcm_runtime *rtd = substream->private_data;
835 struct snd_soc_platform *platform = rtd->platform;
836 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
837 struct snd_soc_dai *codec_dai = rtd->codec_dai;
838 int ret;
839
Mark Brownc5914b02013-10-30 17:47:39 -0700840 if (codec_dai->driver->ops &&
841 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100842 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
843 if (ret < 0)
844 return ret;
845 }
846
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +0100847 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100848 ret = platform->driver->bespoke_trigger(substream, cmd);
849 if (ret < 0)
850 return ret;
851 }
852
Mark Brownc5914b02013-10-30 17:47:39 -0700853 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100854 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
855 if (ret < 0)
856 return ret;
857 }
858 return 0;
859}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100860/*
861 * soc level wrapper for pointer callback
862 * If cpu_dai, codec_dai, platform driver has the delay callback, than
863 * the runtime->delay will be updated accordingly.
864 */
865static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
866{
867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
868 struct snd_soc_platform *platform = rtd->platform;
869 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
870 struct snd_soc_dai *codec_dai = rtd->codec_dai;
871 struct snd_pcm_runtime *runtime = substream->runtime;
872 snd_pcm_uframes_t offset = 0;
873 snd_pcm_sframes_t delay = 0;
874
875 if (platform->driver->ops && platform->driver->ops->pointer)
876 offset = platform->driver->ops->pointer(substream);
877
Mark Brownc5914b02013-10-30 17:47:39 -0700878 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100879 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
880
Mark Brownc5914b02013-10-30 17:47:39 -0700881 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100882 delay += codec_dai->driver->ops->delay(substream, codec_dai);
883
884 if (platform->driver->delay)
885 delay += platform->driver->delay(substream, codec_dai);
886
887 runtime->delay = delay;
888
889 return offset;
890}
891
Liam Girdwood01d75842012-04-25 12:12:49 +0100892/* connect a FE and BE */
893static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
894 struct snd_soc_pcm_runtime *be, int stream)
895{
896 struct snd_soc_dpcm *dpcm;
897
898 /* only add new dpcms */
899 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
900 if (dpcm->be == be && dpcm->fe == fe)
901 return 0;
902 }
903
904 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
905 if (!dpcm)
906 return -ENOMEM;
907
908 dpcm->be = be;
909 dpcm->fe = fe;
910 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
911 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
912 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
913 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
914
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300915 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100916 stream ? "capture" : "playback", fe->dai_link->name,
917 stream ? "<-" : "->", be->dai_link->name);
918
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100919#ifdef CONFIG_DEBUG_FS
920 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
921 fe->debugfs_dpcm_root, &dpcm->state);
922#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100923 return 1;
924}
925
926/* reparent a BE onto another FE */
927static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
928 struct snd_soc_pcm_runtime *be, int stream)
929{
930 struct snd_soc_dpcm *dpcm;
931 struct snd_pcm_substream *fe_substream, *be_substream;
932
933 /* reparent if BE is connected to other FEs */
934 if (!be->dpcm[stream].users)
935 return;
936
937 be_substream = snd_soc_dpcm_get_substream(be, stream);
938
939 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
940 if (dpcm->fe == fe)
941 continue;
942
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300943 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100944 stream ? "capture" : "playback",
945 dpcm->fe->dai_link->name,
946 stream ? "<-" : "->", dpcm->be->dai_link->name);
947
948 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
949 be_substream->runtime = fe_substream->runtime;
950 break;
951 }
952}
953
954/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +0000955void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +0100956{
957 struct snd_soc_dpcm *dpcm, *d;
958
959 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000960 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100961 stream ? "capture" : "playback",
962 dpcm->be->dai_link->name);
963
964 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
965 continue;
966
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300967 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100968 stream ? "capture" : "playback", fe->dai_link->name,
969 stream ? "<-" : "->", dpcm->be->dai_link->name);
970
971 /* BEs still alive need new FE */
972 dpcm_be_reparent(fe, dpcm->be, stream);
973
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100974#ifdef CONFIG_DEBUG_FS
975 debugfs_remove(dpcm->debugfs_state);
976#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100977 list_del(&dpcm->list_be);
978 list_del(&dpcm->list_fe);
979 kfree(dpcm);
980 }
981}
982
983/* get BE for DAI widget and stream */
984static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
985 struct snd_soc_dapm_widget *widget, int stream)
986{
987 struct snd_soc_pcm_runtime *be;
988 int i;
989
990 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
991 for (i = 0; i < card->num_links; i++) {
992 be = &card->rtd[i];
993
Liam Girdwood35ea0652012-06-05 19:26:59 +0100994 if (!be->dai_link->no_pcm)
995 continue;
996
Liam Girdwood01d75842012-04-25 12:12:49 +0100997 if (be->cpu_dai->playback_widget == widget ||
998 be->codec_dai->playback_widget == widget)
999 return be;
1000 }
1001 } else {
1002
1003 for (i = 0; i < card->num_links; i++) {
1004 be = &card->rtd[i];
1005
Liam Girdwood35ea0652012-06-05 19:26:59 +01001006 if (!be->dai_link->no_pcm)
1007 continue;
1008
Liam Girdwood01d75842012-04-25 12:12:49 +01001009 if (be->cpu_dai->capture_widget == widget ||
1010 be->codec_dai->capture_widget == widget)
1011 return be;
1012 }
1013 }
1014
Liam Girdwood103d84a2012-11-19 14:39:15 +00001015 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001016 stream ? "capture" : "playback", widget->name);
1017 return NULL;
1018}
1019
1020static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001021 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001022{
1023 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001024 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001025 else
Benoit Cousson37018612014-04-24 14:01:45 +02001026 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001027}
1028
1029static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1030 struct snd_soc_dapm_widget *widget)
1031{
1032 int i;
1033
1034 for (i = 0; i < list->num_widgets; i++) {
1035 if (widget == list->widgets[i])
1036 return 1;
1037 }
1038
1039 return 0;
1040}
1041
Liam Girdwood23607022014-01-17 17:03:55 +00001042int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001043 int stream, struct snd_soc_dapm_widget_list **list_)
1044{
1045 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1046 struct snd_soc_dapm_widget_list *list;
1047 int paths;
1048
1049 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1050 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1051 if (list == NULL)
1052 return -ENOMEM;
1053
1054 /* get number of valid DAI paths and their widgets */
1055 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1056
Liam Girdwood103d84a2012-11-19 14:39:15 +00001057 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001058 stream ? "capture" : "playback");
1059
1060 *list_ = list;
1061 return paths;
1062}
1063
Liam Girdwood01d75842012-04-25 12:12:49 +01001064static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1065 struct snd_soc_dapm_widget_list **list_)
1066{
1067 struct snd_soc_dpcm *dpcm;
1068 struct snd_soc_dapm_widget_list *list = *list_;
1069 struct snd_soc_dapm_widget *widget;
1070 int prune = 0;
1071
1072 /* Destroy any old FE <--> BE connections */
1073 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1074
1075 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001076 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001077
1078 /* prune the BE if it's no longer in our active list */
1079 if (widget && widget_in_list(list, widget))
1080 continue;
1081
1082 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001083 widget = dai_get_widget(dpcm->be->codec_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001084
1085 /* prune the BE if it's no longer in our active list */
1086 if (widget && widget_in_list(list, widget))
1087 continue;
1088
Liam Girdwood103d84a2012-11-19 14:39:15 +00001089 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001090 stream ? "capture" : "playback",
1091 dpcm->be->dai_link->name, fe->dai_link->name);
1092 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1093 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1094 prune++;
1095 }
1096
Liam Girdwood103d84a2012-11-19 14:39:15 +00001097 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001098 return prune;
1099}
1100
1101static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1102 struct snd_soc_dapm_widget_list **list_)
1103{
1104 struct snd_soc_card *card = fe->card;
1105 struct snd_soc_dapm_widget_list *list = *list_;
1106 struct snd_soc_pcm_runtime *be;
1107 int i, new = 0, err;
1108
1109 /* Create any new FE <--> BE connections */
1110 for (i = 0; i < list->num_widgets; i++) {
1111
Mark Brown46162742013-06-05 19:36:11 +01001112 switch (list->widgets[i]->id) {
1113 case snd_soc_dapm_dai_in:
1114 case snd_soc_dapm_dai_out:
1115 break;
1116 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001117 continue;
Mark Brown46162742013-06-05 19:36:11 +01001118 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001119
1120 /* is there a valid BE rtd for this widget */
1121 be = dpcm_get_be(card, list->widgets[i], stream);
1122 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001123 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001124 list->widgets[i]->name);
1125 continue;
1126 }
1127
1128 /* make sure BE is a real BE */
1129 if (!be->dai_link->no_pcm)
1130 continue;
1131
1132 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001133 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001134 continue;
1135
1136 /* newly connected FE and BE */
1137 err = dpcm_be_connect(fe, be, stream);
1138 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001139 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001140 list->widgets[i]->name);
1141 break;
1142 } else if (err == 0) /* already connected */
1143 continue;
1144
1145 /* new */
1146 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1147 new++;
1148 }
1149
Liam Girdwood103d84a2012-11-19 14:39:15 +00001150 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001151 return new;
1152}
1153
1154/*
1155 * Find the corresponding BE DAIs that source or sink audio to this
1156 * FE substream.
1157 */
Liam Girdwood23607022014-01-17 17:03:55 +00001158int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001159 int stream, struct snd_soc_dapm_widget_list **list, int new)
1160{
1161 if (new)
1162 return dpcm_add_paths(fe, stream, list);
1163 else
1164 return dpcm_prune_paths(fe, stream, list);
1165}
1166
Liam Girdwood23607022014-01-17 17:03:55 +00001167void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001168{
1169 struct snd_soc_dpcm *dpcm;
1170
1171 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1172 dpcm->be->dpcm[stream].runtime_update =
1173 SND_SOC_DPCM_UPDATE_NO;
1174}
1175
1176static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1177 int stream)
1178{
1179 struct snd_soc_dpcm *dpcm;
1180
1181 /* disable any enabled and non active backends */
1182 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1183
1184 struct snd_soc_pcm_runtime *be = dpcm->be;
1185 struct snd_pcm_substream *be_substream =
1186 snd_soc_dpcm_get_substream(be, stream);
1187
1188 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001189 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001190 stream ? "capture" : "playback",
1191 be->dpcm[stream].state);
1192
1193 if (--be->dpcm[stream].users != 0)
1194 continue;
1195
1196 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1197 continue;
1198
1199 soc_pcm_close(be_substream);
1200 be_substream->runtime = NULL;
1201 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1202 }
1203}
1204
Liam Girdwood23607022014-01-17 17:03:55 +00001205int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001206{
1207 struct snd_soc_dpcm *dpcm;
1208 int err, count = 0;
1209
1210 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1211 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1212
1213 struct snd_soc_pcm_runtime *be = dpcm->be;
1214 struct snd_pcm_substream *be_substream =
1215 snd_soc_dpcm_get_substream(be, stream);
1216
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001217 if (!be_substream) {
1218 dev_err(be->dev, "ASoC: no backend %s stream\n",
1219 stream ? "capture" : "playback");
1220 continue;
1221 }
1222
Liam Girdwood01d75842012-04-25 12:12:49 +01001223 /* is this op for this BE ? */
1224 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1225 continue;
1226
1227 /* first time the dpcm is open ? */
1228 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001229 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001230 stream ? "capture" : "playback",
1231 be->dpcm[stream].state);
1232
1233 if (be->dpcm[stream].users++ != 0)
1234 continue;
1235
1236 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1237 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1238 continue;
1239
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001240 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1241 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001242
1243 be_substream->runtime = be->dpcm[stream].runtime;
1244 err = soc_pcm_open(be_substream);
1245 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001246 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001247 be->dpcm[stream].users--;
1248 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001249 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001250 stream ? "capture" : "playback",
1251 be->dpcm[stream].state);
1252
1253 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1254 goto unwind;
1255 }
1256
1257 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1258 count++;
1259 }
1260
1261 return count;
1262
1263unwind:
1264 /* disable any enabled and non active backends */
1265 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1266 struct snd_soc_pcm_runtime *be = dpcm->be;
1267 struct snd_pcm_substream *be_substream =
1268 snd_soc_dpcm_get_substream(be, stream);
1269
1270 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1271 continue;
1272
1273 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001274 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001275 stream ? "capture" : "playback",
1276 be->dpcm[stream].state);
1277
1278 if (--be->dpcm[stream].users != 0)
1279 continue;
1280
1281 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1282 continue;
1283
1284 soc_pcm_close(be_substream);
1285 be_substream->runtime = NULL;
1286 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1287 }
1288
1289 return err;
1290}
1291
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001292static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1293 struct snd_soc_pcm_stream *stream)
1294{
1295 runtime->hw.rate_min = stream->rate_min;
1296 runtime->hw.rate_max = stream->rate_max;
1297 runtime->hw.channels_min = stream->channels_min;
1298 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001299 if (runtime->hw.formats)
1300 runtime->hw.formats &= stream->formats;
1301 else
1302 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001303 runtime->hw.rates = stream->rates;
1304}
1305
Mark Brown45c0a182012-05-09 21:46:27 +01001306static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001307{
1308 struct snd_pcm_runtime *runtime = substream->runtime;
1309 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1310 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1311 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1312
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001313 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1314 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1315 else
1316 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001317}
1318
1319static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1320{
1321 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1322 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1323 int stream = fe_substream->stream, ret = 0;
1324
1325 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1326
1327 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1328 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001329 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001330 goto be_err;
1331 }
1332
Liam Girdwood103d84a2012-11-19 14:39:15 +00001333 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001334
1335 /* start the DAI frontend */
1336 ret = soc_pcm_open(fe_substream);
1337 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001338 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001339 goto unwind;
1340 }
1341
1342 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1343
1344 dpcm_set_fe_runtime(fe_substream);
1345 snd_pcm_limit_hw_rates(runtime);
1346
1347 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1348 return 0;
1349
1350unwind:
1351 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1352be_err:
1353 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1354 return ret;
1355}
1356
Liam Girdwood23607022014-01-17 17:03:55 +00001357int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001358{
1359 struct snd_soc_dpcm *dpcm;
1360
1361 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1362 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1363
1364 struct snd_soc_pcm_runtime *be = dpcm->be;
1365 struct snd_pcm_substream *be_substream =
1366 snd_soc_dpcm_get_substream(be, stream);
1367
1368 /* is this op for this BE ? */
1369 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1370 continue;
1371
1372 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001373 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001374 stream ? "capture" : "playback",
1375 be->dpcm[stream].state);
1376
1377 if (--be->dpcm[stream].users != 0)
1378 continue;
1379
1380 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1381 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1382 continue;
1383
Liam Girdwood103d84a2012-11-19 14:39:15 +00001384 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001385 dpcm->fe->dai_link->name);
1386
1387 soc_pcm_close(be_substream);
1388 be_substream->runtime = NULL;
1389
1390 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1391 }
1392 return 0;
1393}
1394
1395static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1396{
1397 struct snd_soc_pcm_runtime *fe = substream->private_data;
1398 int stream = substream->stream;
1399
1400 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1401
1402 /* shutdown the BEs */
1403 dpcm_be_dai_shutdown(fe, substream->stream);
1404
Liam Girdwood103d84a2012-11-19 14:39:15 +00001405 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001406
1407 /* now shutdown the frontend */
1408 soc_pcm_close(substream);
1409
1410 /* run the stream event for each BE */
1411 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1412
1413 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1414 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1415 return 0;
1416}
1417
Liam Girdwood23607022014-01-17 17:03:55 +00001418int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001419{
1420 struct snd_soc_dpcm *dpcm;
1421
1422 /* only hw_params backends that are either sinks or sources
1423 * to this frontend DAI */
1424 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1425
1426 struct snd_soc_pcm_runtime *be = dpcm->be;
1427 struct snd_pcm_substream *be_substream =
1428 snd_soc_dpcm_get_substream(be, stream);
1429
1430 /* is this op for this BE ? */
1431 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1432 continue;
1433
1434 /* only free hw when no longer used - check all FEs */
1435 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1436 continue;
1437
1438 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1439 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1440 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001441 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001442 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1443 continue;
1444
Liam Girdwood103d84a2012-11-19 14:39:15 +00001445 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001446 dpcm->fe->dai_link->name);
1447
1448 soc_pcm_hw_free(be_substream);
1449
1450 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1451 }
1452
1453 return 0;
1454}
1455
Mark Brown45c0a182012-05-09 21:46:27 +01001456static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001457{
1458 struct snd_soc_pcm_runtime *fe = substream->private_data;
1459 int err, stream = substream->stream;
1460
1461 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1462 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1463
Liam Girdwood103d84a2012-11-19 14:39:15 +00001464 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001465
1466 /* call hw_free on the frontend */
1467 err = soc_pcm_hw_free(substream);
1468 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001469 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001470 fe->dai_link->name);
1471
1472 /* only hw_params backends that are either sinks or sources
1473 * to this frontend DAI */
1474 err = dpcm_be_dai_hw_free(fe, stream);
1475
1476 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1477 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1478
1479 mutex_unlock(&fe->card->mutex);
1480 return 0;
1481}
1482
Liam Girdwood23607022014-01-17 17:03:55 +00001483int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001484{
1485 struct snd_soc_dpcm *dpcm;
1486 int ret;
1487
1488 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1489
1490 struct snd_soc_pcm_runtime *be = dpcm->be;
1491 struct snd_pcm_substream *be_substream =
1492 snd_soc_dpcm_get_substream(be, stream);
1493
1494 /* is this op for this BE ? */
1495 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1496 continue;
1497
1498 /* only allow hw_params() if no connected FEs are running */
1499 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1500 continue;
1501
1502 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1503 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1504 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1505 continue;
1506
Liam Girdwood103d84a2012-11-19 14:39:15 +00001507 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001508 dpcm->fe->dai_link->name);
1509
1510 /* copy params for each dpcm */
1511 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1512 sizeof(struct snd_pcm_hw_params));
1513
1514 /* perform any hw_params fixups */
1515 if (be->dai_link->be_hw_params_fixup) {
1516 ret = be->dai_link->be_hw_params_fixup(be,
1517 &dpcm->hw_params);
1518 if (ret < 0) {
1519 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001520 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001521 ret);
1522 goto unwind;
1523 }
1524 }
1525
1526 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1527 if (ret < 0) {
1528 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001529 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001530 goto unwind;
1531 }
1532
1533 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1534 }
1535 return 0;
1536
1537unwind:
1538 /* disable any enabled and non active backends */
1539 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1540 struct snd_soc_pcm_runtime *be = dpcm->be;
1541 struct snd_pcm_substream *be_substream =
1542 snd_soc_dpcm_get_substream(be, stream);
1543
1544 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1545 continue;
1546
1547 /* only allow hw_free() if no connected FEs are running */
1548 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1549 continue;
1550
1551 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1552 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1553 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1554 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1555 continue;
1556
1557 soc_pcm_hw_free(be_substream);
1558 }
1559
1560 return ret;
1561}
1562
Mark Brown45c0a182012-05-09 21:46:27 +01001563static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1564 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001565{
1566 struct snd_soc_pcm_runtime *fe = substream->private_data;
1567 int ret, stream = substream->stream;
1568
1569 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1570 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1571
1572 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1573 sizeof(struct snd_pcm_hw_params));
1574 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1575 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001576 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001577 goto out;
1578 }
1579
Liam Girdwood103d84a2012-11-19 14:39:15 +00001580 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001581 fe->dai_link->name, params_rate(params),
1582 params_channels(params), params_format(params));
1583
1584 /* call hw_params on the frontend */
1585 ret = soc_pcm_hw_params(substream, params);
1586 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001587 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001588 dpcm_be_dai_hw_free(fe, stream);
1589 } else
1590 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1591
1592out:
1593 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1594 mutex_unlock(&fe->card->mutex);
1595 return ret;
1596}
1597
1598static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1599 struct snd_pcm_substream *substream, int cmd)
1600{
1601 int ret;
1602
Liam Girdwood103d84a2012-11-19 14:39:15 +00001603 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001604 dpcm->fe->dai_link->name, cmd);
1605
1606 ret = soc_pcm_trigger(substream, cmd);
1607 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001608 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001609
1610 return ret;
1611}
1612
Liam Girdwood23607022014-01-17 17:03:55 +00001613int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001614 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001615{
1616 struct snd_soc_dpcm *dpcm;
1617 int ret = 0;
1618
1619 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1620
1621 struct snd_soc_pcm_runtime *be = dpcm->be;
1622 struct snd_pcm_substream *be_substream =
1623 snd_soc_dpcm_get_substream(be, stream);
1624
1625 /* is this op for this BE ? */
1626 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1627 continue;
1628
1629 switch (cmd) {
1630 case SNDRV_PCM_TRIGGER_START:
1631 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1632 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1633 continue;
1634
1635 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1636 if (ret)
1637 return ret;
1638
1639 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1640 break;
1641 case SNDRV_PCM_TRIGGER_RESUME:
1642 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1643 continue;
1644
1645 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1646 if (ret)
1647 return ret;
1648
1649 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1650 break;
1651 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1652 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1653 continue;
1654
1655 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1656 if (ret)
1657 return ret;
1658
1659 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1660 break;
1661 case SNDRV_PCM_TRIGGER_STOP:
1662 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1663 continue;
1664
1665 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1666 continue;
1667
1668 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1669 if (ret)
1670 return ret;
1671
1672 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1673 break;
1674 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08001675 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01001676 continue;
1677
1678 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1679 continue;
1680
1681 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1682 if (ret)
1683 return ret;
1684
1685 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1686 break;
1687 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1688 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1689 continue;
1690
1691 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1692 continue;
1693
1694 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1695 if (ret)
1696 return ret;
1697
1698 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1699 break;
1700 }
1701 }
1702
1703 return ret;
1704}
1705EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1706
Mark Brown45c0a182012-05-09 21:46:27 +01001707static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001708{
1709 struct snd_soc_pcm_runtime *fe = substream->private_data;
1710 int stream = substream->stream, ret;
1711 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1712
1713 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1714
1715 switch (trigger) {
1716 case SND_SOC_DPCM_TRIGGER_PRE:
1717 /* call trigger on the frontend before the backend. */
1718
Liam Girdwood103d84a2012-11-19 14:39:15 +00001719 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001720 fe->dai_link->name, cmd);
1721
1722 ret = soc_pcm_trigger(substream, cmd);
1723 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001724 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001725 goto out;
1726 }
1727
1728 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1729 break;
1730 case SND_SOC_DPCM_TRIGGER_POST:
1731 /* call trigger on the frontend after the backend. */
1732
1733 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1734 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001735 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001736 goto out;
1737 }
1738
Liam Girdwood103d84a2012-11-19 14:39:15 +00001739 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001740 fe->dai_link->name, cmd);
1741
1742 ret = soc_pcm_trigger(substream, cmd);
1743 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001744 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1745 /* bespoke trigger() - handles both FE and BEs */
1746
Liam Girdwood103d84a2012-11-19 14:39:15 +00001747 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001748 fe->dai_link->name, cmd);
1749
1750 ret = soc_pcm_bespoke_trigger(substream, cmd);
1751 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001752 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001753 goto out;
1754 }
1755 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001756 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001757 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001758 fe->dai_link->name);
1759 ret = -EINVAL;
1760 goto out;
1761 }
1762
1763 switch (cmd) {
1764 case SNDRV_PCM_TRIGGER_START:
1765 case SNDRV_PCM_TRIGGER_RESUME:
1766 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1767 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1768 break;
1769 case SNDRV_PCM_TRIGGER_STOP:
1770 case SNDRV_PCM_TRIGGER_SUSPEND:
1771 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1772 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1773 break;
1774 }
1775
1776out:
1777 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1778 return ret;
1779}
1780
Liam Girdwood23607022014-01-17 17:03:55 +00001781int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001782{
1783 struct snd_soc_dpcm *dpcm;
1784 int ret = 0;
1785
1786 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1787
1788 struct snd_soc_pcm_runtime *be = dpcm->be;
1789 struct snd_pcm_substream *be_substream =
1790 snd_soc_dpcm_get_substream(be, stream);
1791
1792 /* is this op for this BE ? */
1793 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1794 continue;
1795
1796 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1797 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1798 continue;
1799
Liam Girdwood103d84a2012-11-19 14:39:15 +00001800 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001801 dpcm->fe->dai_link->name);
1802
1803 ret = soc_pcm_prepare(be_substream);
1804 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001805 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001806 ret);
1807 break;
1808 }
1809
1810 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1811 }
1812 return ret;
1813}
1814
Mark Brown45c0a182012-05-09 21:46:27 +01001815static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001816{
1817 struct snd_soc_pcm_runtime *fe = substream->private_data;
1818 int stream = substream->stream, ret = 0;
1819
1820 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1821
Liam Girdwood103d84a2012-11-19 14:39:15 +00001822 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001823
1824 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1825
1826 /* there is no point preparing this FE if there are no BEs */
1827 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001828 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001829 fe->dai_link->name);
1830 ret = -EINVAL;
1831 goto out;
1832 }
1833
1834 ret = dpcm_be_dai_prepare(fe, substream->stream);
1835 if (ret < 0)
1836 goto out;
1837
1838 /* call prepare on the frontend */
1839 ret = soc_pcm_prepare(substream);
1840 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001841 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001842 fe->dai_link->name);
1843 goto out;
1844 }
1845
1846 /* run the stream event for each BE */
1847 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1848 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1849
1850out:
1851 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1852 mutex_unlock(&fe->card->mutex);
1853
1854 return ret;
1855}
1856
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001857static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1858 unsigned int cmd, void *arg)
1859{
1860 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1861 struct snd_soc_platform *platform = rtd->platform;
1862
Mark Brownc5914b02013-10-30 17:47:39 -07001863 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001864 return platform->driver->ops->ioctl(substream, cmd, arg);
1865 return snd_pcm_lib_ioctl(substream, cmd, arg);
1866}
1867
Liam Girdwood618dae12012-04-25 12:12:51 +01001868static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1869{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001870 struct snd_pcm_substream *substream =
1871 snd_soc_dpcm_get_substream(fe, stream);
1872 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001873 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001874
Liam Girdwood103d84a2012-11-19 14:39:15 +00001875 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001876 stream ? "capture" : "playback", fe->dai_link->name);
1877
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001878 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1879 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001880 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001881 fe->dai_link->name);
1882
1883 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1884 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001885 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001886 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001887 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001888 fe->dai_link->name);
1889
1890 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1891 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001892 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001893 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001894
1895 err = dpcm_be_dai_hw_free(fe, stream);
1896 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001897 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001898
1899 err = dpcm_be_dai_shutdown(fe, stream);
1900 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001901 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001902
1903 /* run the stream event for each BE */
1904 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1905
1906 return 0;
1907}
1908
1909static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1910{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001911 struct snd_pcm_substream *substream =
1912 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001913 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001914 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001915 int ret;
1916
Liam Girdwood103d84a2012-11-19 14:39:15 +00001917 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001918 stream ? "capture" : "playback", fe->dai_link->name);
1919
1920 /* Only start the BE if the FE is ready */
1921 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1922 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1923 return -EINVAL;
1924
1925 /* startup must always be called for new BEs */
1926 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001927 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001928 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001929
1930 /* keep going if FE state is > open */
1931 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1932 return 0;
1933
1934 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001935 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001936 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001937
1938 /* keep going if FE state is > hw_params */
1939 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1940 return 0;
1941
1942
1943 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001944 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001945 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001946
1947 /* run the stream event for each BE */
1948 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1949
1950 /* keep going if FE state is > prepare */
1951 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1952 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1953 return 0;
1954
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001955 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1956 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001957 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001958 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001959
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001960 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1961 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001962 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001963 goto hw_free;
1964 }
1965 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001966 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001967 fe->dai_link->name);
1968
1969 ret = dpcm_be_dai_trigger(fe, stream,
1970 SNDRV_PCM_TRIGGER_START);
1971 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001972 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001973 goto hw_free;
1974 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001975 }
1976
1977 return 0;
1978
1979hw_free:
1980 dpcm_be_dai_hw_free(fe, stream);
1981close:
1982 dpcm_be_dai_shutdown(fe, stream);
1983disconnect:
1984 /* disconnect any non started BEs */
1985 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1986 struct snd_soc_pcm_runtime *be = dpcm->be;
1987 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1988 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1989 }
1990
1991 return ret;
1992}
1993
1994static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1995{
1996 int ret;
1997
1998 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1999 ret = dpcm_run_update_startup(fe, stream);
2000 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002001 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002002 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2003
2004 return ret;
2005}
2006
2007static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2008{
2009 int ret;
2010
2011 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2012 ret = dpcm_run_update_shutdown(fe, stream);
2013 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002014 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002015 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2016
2017 return ret;
2018}
2019
2020/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2021 * any DAI links.
2022 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002023int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002024{
Liam Girdwood618dae12012-04-25 12:12:51 +01002025 int i, old, new, paths;
2026
Liam Girdwood618dae12012-04-25 12:12:51 +01002027 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2028 for (i = 0; i < card->num_rtd; i++) {
2029 struct snd_soc_dapm_widget_list *list;
2030 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2031
2032 /* make sure link is FE */
2033 if (!fe->dai_link->dynamic)
2034 continue;
2035
2036 /* only check active links */
2037 if (!fe->cpu_dai->active)
2038 continue;
2039
2040 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002041 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002042 fe->dai_link->name);
2043
2044 /* skip if FE doesn't have playback capability */
2045 if (!fe->cpu_dai->driver->playback.channels_min)
2046 goto capture;
2047
2048 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2049 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002050 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002051 fe->dai_link->name, "playback");
2052 mutex_unlock(&card->mutex);
2053 return paths;
2054 }
2055
2056 /* update any new playback paths */
2057 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2058 if (new) {
2059 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2060 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2061 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2062 }
2063
2064 /* update any old playback paths */
2065 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2066 if (old) {
2067 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2068 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2069 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2070 }
2071
2072capture:
2073 /* skip if FE doesn't have capture capability */
2074 if (!fe->cpu_dai->driver->capture.channels_min)
2075 continue;
2076
2077 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2078 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002079 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002080 fe->dai_link->name, "capture");
2081 mutex_unlock(&card->mutex);
2082 return paths;
2083 }
2084
2085 /* update any new capture paths */
2086 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2087 if (new) {
2088 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2089 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2090 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2091 }
2092
2093 /* update any old capture paths */
2094 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2095 if (old) {
2096 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2097 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2098 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2099 }
2100
2101 dpcm_path_put(&list);
2102 }
2103
2104 mutex_unlock(&card->mutex);
2105 return 0;
2106}
Liam Girdwood01d75842012-04-25 12:12:49 +01002107int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2108{
2109 struct snd_soc_dpcm *dpcm;
2110 struct list_head *clients =
2111 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2112
2113 list_for_each_entry(dpcm, clients, list_be) {
2114
2115 struct snd_soc_pcm_runtime *be = dpcm->be;
2116 struct snd_soc_dai *dai = be->codec_dai;
2117 struct snd_soc_dai_driver *drv = dai->driver;
2118
2119 if (be->dai_link->ignore_suspend)
2120 continue;
2121
Liam Girdwood103d84a2012-11-19 14:39:15 +00002122 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002123
Mark Brownc5914b02013-10-30 17:47:39 -07002124 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2125 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002126 }
2127
2128 return 0;
2129}
2130
Mark Brown45c0a182012-05-09 21:46:27 +01002131static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002132{
2133 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2134 struct snd_soc_dpcm *dpcm;
2135 struct snd_soc_dapm_widget_list *list;
2136 int ret;
2137 int stream = fe_substream->stream;
2138
2139 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2140 fe->dpcm[stream].runtime = fe_substream->runtime;
2141
2142 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002143 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002144 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002145 }
2146
2147 /* calculate valid and active FE <-> BE dpcms */
2148 dpcm_process_paths(fe, stream, &list, 1);
2149
2150 ret = dpcm_fe_dai_startup(fe_substream);
2151 if (ret < 0) {
2152 /* clean up all links */
2153 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2154 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2155
2156 dpcm_be_disconnect(fe, stream);
2157 fe->dpcm[stream].runtime = NULL;
2158 }
2159
2160 dpcm_clear_pending_state(fe, stream);
2161 dpcm_path_put(&list);
2162 mutex_unlock(&fe->card->mutex);
2163 return ret;
2164}
2165
Mark Brown45c0a182012-05-09 21:46:27 +01002166static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002167{
2168 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2169 struct snd_soc_dpcm *dpcm;
2170 int stream = fe_substream->stream, ret;
2171
2172 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2173 ret = dpcm_fe_dai_shutdown(fe_substream);
2174
2175 /* mark FE's links ready to prune */
2176 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2177 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2178
2179 dpcm_be_disconnect(fe, stream);
2180
2181 fe->dpcm[stream].runtime = NULL;
2182 mutex_unlock(&fe->card->mutex);
2183 return ret;
2184}
2185
Liam Girdwoodddee6272011-06-09 14:45:53 +01002186/* create a new pcm */
2187int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2188{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002189 struct snd_soc_platform *platform = rtd->platform;
2190 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2191 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2192 struct snd_pcm *pcm;
2193 char new_name[64];
2194 int ret = 0, playback = 0, capture = 0;
2195
Liam Girdwood01d75842012-04-25 12:12:49 +01002196 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002197 playback = rtd->dai_link->dpcm_playback;
2198 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002199 } else {
Mark Brown05679092013-06-01 23:13:53 +01002200 if (codec_dai->driver->playback.channels_min &&
2201 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002202 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002203 if (codec_dai->driver->capture.channels_min &&
2204 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002205 capture = 1;
2206 }
Sangsu Parka5002312012-01-02 17:15:10 +09002207
Fabio Estevamd6bead02013-08-29 10:32:13 -03002208 if (rtd->dai_link->playback_only) {
2209 playback = 1;
2210 capture = 0;
2211 }
2212
2213 if (rtd->dai_link->capture_only) {
2214 playback = 0;
2215 capture = 1;
2216 }
2217
Liam Girdwood01d75842012-04-25 12:12:49 +01002218 /* create the PCM */
2219 if (rtd->dai_link->no_pcm) {
2220 snprintf(new_name, sizeof(new_name), "(%s)",
2221 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002222
Liam Girdwood01d75842012-04-25 12:12:49 +01002223 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2224 playback, capture, &pcm);
2225 } else {
2226 if (rtd->dai_link->dynamic)
2227 snprintf(new_name, sizeof(new_name), "%s (*)",
2228 rtd->dai_link->stream_name);
2229 else
2230 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2231 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002232
Liam Girdwood01d75842012-04-25 12:12:49 +01002233 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2234 capture, &pcm);
2235 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002236 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002237 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002238 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002239 return ret;
2240 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002241 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002242
2243 /* DAPM dai link stream work */
2244 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2245
2246 rtd->pcm = pcm;
2247 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002248
2249 if (rtd->dai_link->no_pcm) {
2250 if (playback)
2251 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2252 if (capture)
2253 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2254 goto out;
2255 }
2256
2257 /* ASoC PCM operations */
2258 if (rtd->dai_link->dynamic) {
2259 rtd->ops.open = dpcm_fe_dai_open;
2260 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2261 rtd->ops.prepare = dpcm_fe_dai_prepare;
2262 rtd->ops.trigger = dpcm_fe_dai_trigger;
2263 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2264 rtd->ops.close = dpcm_fe_dai_close;
2265 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002266 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002267 } else {
2268 rtd->ops.open = soc_pcm_open;
2269 rtd->ops.hw_params = soc_pcm_hw_params;
2270 rtd->ops.prepare = soc_pcm_prepare;
2271 rtd->ops.trigger = soc_pcm_trigger;
2272 rtd->ops.hw_free = soc_pcm_hw_free;
2273 rtd->ops.close = soc_pcm_close;
2274 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002275 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002276 }
2277
Liam Girdwoodddee6272011-06-09 14:45:53 +01002278 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002279 rtd->ops.ack = platform->driver->ops->ack;
2280 rtd->ops.copy = platform->driver->ops->copy;
2281 rtd->ops.silence = platform->driver->ops->silence;
2282 rtd->ops.page = platform->driver->ops->page;
2283 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002284 }
2285
2286 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002287 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002288
2289 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002290 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002291
2292 if (platform->driver->pcm_new) {
2293 ret = platform->driver->pcm_new(rtd);
2294 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002295 dev_err(platform->dev,
2296 "ASoC: pcm constructor failed: %d\n",
2297 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002298 return ret;
2299 }
2300 }
2301
2302 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002303out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002304 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002305 cpu_dai->name);
2306 return ret;
2307}
Liam Girdwood01d75842012-04-25 12:12:49 +01002308
2309/* is the current PCM operation for this FE ? */
2310int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2311{
2312 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2313 return 1;
2314 return 0;
2315}
2316EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2317
2318/* is the current PCM operation for this BE ? */
2319int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2320 struct snd_soc_pcm_runtime *be, int stream)
2321{
2322 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2323 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2324 be->dpcm[stream].runtime_update))
2325 return 1;
2326 return 0;
2327}
2328EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2329
2330/* get the substream for this BE */
2331struct snd_pcm_substream *
2332 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2333{
2334 return be->pcm->streams[stream].substream;
2335}
2336EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2337
2338/* get the BE runtime state */
2339enum snd_soc_dpcm_state
2340 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2341{
2342 return be->dpcm[stream].state;
2343}
2344EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2345
2346/* set the BE runtime state */
2347void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2348 int stream, enum snd_soc_dpcm_state state)
2349{
2350 be->dpcm[stream].state = state;
2351}
2352EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2353
2354/*
2355 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2356 * are not running, paused or suspended for the specified stream direction.
2357 */
2358int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2359 struct snd_soc_pcm_runtime *be, int stream)
2360{
2361 struct snd_soc_dpcm *dpcm;
2362 int state;
2363
2364 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2365
2366 if (dpcm->fe == fe)
2367 continue;
2368
2369 state = dpcm->fe->dpcm[stream].state;
2370 if (state == SND_SOC_DPCM_STATE_START ||
2371 state == SND_SOC_DPCM_STATE_PAUSED ||
2372 state == SND_SOC_DPCM_STATE_SUSPEND)
2373 return 0;
2374 }
2375
2376 /* it's safe to free/stop this BE DAI */
2377 return 1;
2378}
2379EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2380
2381/*
2382 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2383 * running, paused or suspended for the specified stream direction.
2384 */
2385int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2386 struct snd_soc_pcm_runtime *be, int stream)
2387{
2388 struct snd_soc_dpcm *dpcm;
2389 int state;
2390
2391 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2392
2393 if (dpcm->fe == fe)
2394 continue;
2395
2396 state = dpcm->fe->dpcm[stream].state;
2397 if (state == SND_SOC_DPCM_STATE_START ||
2398 state == SND_SOC_DPCM_STATE_PAUSED ||
2399 state == SND_SOC_DPCM_STATE_SUSPEND ||
2400 state == SND_SOC_DPCM_STATE_PREPARE)
2401 return 0;
2402 }
2403
2404 /* it's safe to change hw_params */
2405 return 1;
2406}
2407EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002408
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002409int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2410 int cmd, struct snd_soc_platform *platform)
2411{
Mark Brownc5914b02013-10-30 17:47:39 -07002412 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002413 return platform->driver->ops->trigger(substream, cmd);
2414 return 0;
2415}
2416EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2417
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002418#ifdef CONFIG_DEBUG_FS
2419static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2420{
2421 switch (state) {
2422 case SND_SOC_DPCM_STATE_NEW:
2423 return "new";
2424 case SND_SOC_DPCM_STATE_OPEN:
2425 return "open";
2426 case SND_SOC_DPCM_STATE_HW_PARAMS:
2427 return "hw_params";
2428 case SND_SOC_DPCM_STATE_PREPARE:
2429 return "prepare";
2430 case SND_SOC_DPCM_STATE_START:
2431 return "start";
2432 case SND_SOC_DPCM_STATE_STOP:
2433 return "stop";
2434 case SND_SOC_DPCM_STATE_SUSPEND:
2435 return "suspend";
2436 case SND_SOC_DPCM_STATE_PAUSED:
2437 return "paused";
2438 case SND_SOC_DPCM_STATE_HW_FREE:
2439 return "hw_free";
2440 case SND_SOC_DPCM_STATE_CLOSE:
2441 return "close";
2442 }
2443
2444 return "unknown";
2445}
2446
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002447static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2448 int stream, char *buf, size_t size)
2449{
2450 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2451 struct snd_soc_dpcm *dpcm;
2452 ssize_t offset = 0;
2453
2454 /* FE state */
2455 offset += snprintf(buf + offset, size - offset,
2456 "[%s - %s]\n", fe->dai_link->name,
2457 stream ? "Capture" : "Playback");
2458
2459 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2460 dpcm_state_string(fe->dpcm[stream].state));
2461
2462 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2463 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2464 offset += snprintf(buf + offset, size - offset,
2465 "Hardware Params: "
2466 "Format = %s, Channels = %d, Rate = %d\n",
2467 snd_pcm_format_name(params_format(params)),
2468 params_channels(params),
2469 params_rate(params));
2470
2471 /* BEs state */
2472 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2473
2474 if (list_empty(&fe->dpcm[stream].be_clients)) {
2475 offset += snprintf(buf + offset, size - offset,
2476 " No active DSP links\n");
2477 goto out;
2478 }
2479
2480 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2481 struct snd_soc_pcm_runtime *be = dpcm->be;
2482 params = &dpcm->hw_params;
2483
2484 offset += snprintf(buf + offset, size - offset,
2485 "- %s\n", be->dai_link->name);
2486
2487 offset += snprintf(buf + offset, size - offset,
2488 " State: %s\n",
2489 dpcm_state_string(be->dpcm[stream].state));
2490
2491 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2492 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2493 offset += snprintf(buf + offset, size - offset,
2494 " Hardware Params: "
2495 "Format = %s, Channels = %d, Rate = %d\n",
2496 snd_pcm_format_name(params_format(params)),
2497 params_channels(params),
2498 params_rate(params));
2499 }
2500
2501out:
2502 return offset;
2503}
2504
2505static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2506 size_t count, loff_t *ppos)
2507{
2508 struct snd_soc_pcm_runtime *fe = file->private_data;
2509 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2510 char *buf;
2511
2512 buf = kmalloc(out_count, GFP_KERNEL);
2513 if (!buf)
2514 return -ENOMEM;
2515
2516 if (fe->cpu_dai->driver->playback.channels_min)
2517 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2518 buf + offset, out_count - offset);
2519
2520 if (fe->cpu_dai->driver->capture.channels_min)
2521 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2522 buf + offset, out_count - offset);
2523
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002524 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002525
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002526 kfree(buf);
2527 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002528}
2529
2530static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002531 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002532 .read = dpcm_state_read_file,
2533 .llseek = default_llseek,
2534};
2535
2536int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2537{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002538 if (!rtd->dai_link)
2539 return 0;
2540
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002541 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2542 rtd->card->debugfs_card_root);
2543 if (!rtd->debugfs_dpcm_root) {
2544 dev_dbg(rtd->dev,
2545 "ASoC: Failed to create dpcm debugfs directory %s\n",
2546 rtd->dai_link->name);
2547 return -EINVAL;
2548 }
2549
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002550 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002551 rtd->debugfs_dpcm_root,
2552 rtd, &dpcm_state_fops);
2553
2554 return 0;
2555}
2556#endif