blob: 320a904b6b69149a8e54a74384949b0735a667d1 [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>
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +020010 * Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010011 *
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
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200287static void soc_pcm_set_msb(struct snd_pcm_substream *substream,
288 struct snd_soc_dai *dai, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000289{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200290 int ret, i;
Mark Brown58ba9b22012-01-16 18:38:51 +0000291
292 if (!bits)
293 return;
294
295 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000296 if (bits >= sample_sizes[i])
297 continue;
298
299 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
300 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000301 if (ret != 0)
302 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000303 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000304 bits, sample_sizes[i], ret);
305 }
306}
307
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200308static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
309{
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
311 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
312 struct snd_soc_dai *codec_dai = rtd->codec_dai;
313 unsigned int bits = 0, cpu_bits;
314
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
316 bits = codec_dai->driver->playback.sig_bits;
317 cpu_bits = cpu_dai->driver->playback.sig_bits;
318 } else {
319 bits = codec_dai->driver->capture.sig_bits;
320 cpu_bits = cpu_dai->driver->capture.sig_bits;
321 }
322
323 soc_pcm_set_msb(substream, codec_dai, bits);
324 soc_pcm_set_msb(substream, cpu_dai, cpu_bits);
325}
326
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100327static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200328 struct snd_soc_pcm_stream *codec_stream,
329 struct snd_soc_pcm_stream *cpu_stream)
330{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100331 struct snd_pcm_hardware *hw = &runtime->hw;
332
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200333 hw->channels_min = max(codec_stream->channels_min,
334 cpu_stream->channels_min);
335 hw->channels_max = min(codec_stream->channels_max,
336 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100337 if (hw->formats)
338 hw->formats &= codec_stream->formats & cpu_stream->formats;
339 else
340 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausen55dcdb52014-01-11 10:24:44 +0100341 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates,
342 cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100343
Lars-Peter Clausen817873f2014-01-11 10:24:40 +0100344 hw->rate_min = 0;
345 hw->rate_max = UINT_MAX;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100346
347 snd_pcm_limit_hw_rates(runtime);
348
349 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
350 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
351 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
352 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200353}
354
Mark Brown58ba9b22012-01-16 18:38:51 +0000355/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100356 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
357 * then initialized and any private data can be allocated. This also calls
358 * startup for the cpu DAI, platform, machine and codec DAI.
359 */
360static int soc_pcm_open(struct snd_pcm_substream *substream)
361{
362 struct snd_soc_pcm_runtime *rtd = substream->private_data;
363 struct snd_pcm_runtime *runtime = substream->runtime;
364 struct snd_soc_platform *platform = rtd->platform;
365 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
366 struct snd_soc_dai *codec_dai = rtd->codec_dai;
367 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
368 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
369 int ret = 0;
370
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800371 pinctrl_pm_select_default_state(cpu_dai->dev);
372 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000373 pm_runtime_get_sync(cpu_dai->dev);
374 pm_runtime_get_sync(codec_dai->dev);
375 pm_runtime_get_sync(platform->dev);
376
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100377 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100378
379 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700380 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100381 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
382 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000383 dev_err(cpu_dai->dev, "ASoC: can't open interface"
384 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100385 goto out;
386 }
387 }
388
389 if (platform->driver->ops && platform->driver->ops->open) {
390 ret = platform->driver->ops->open(substream);
391 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000392 dev_err(platform->dev, "ASoC: can't open platform"
393 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100394 goto platform_err;
395 }
396 }
397
Mark Brownc5914b02013-10-30 17:47:39 -0700398 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100399 ret = codec_dai->driver->ops->startup(substream, codec_dai);
400 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000401 dev_err(codec_dai->dev, "ASoC: can't open codec"
402 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100403 goto codec_dai_err;
404 }
405 }
406
407 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
408 ret = rtd->dai_link->ops->startup(substream);
409 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000410 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000411 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100412 goto machine_err;
413 }
414 }
415
Liam Girdwood01d75842012-04-25 12:12:49 +0100416 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
417 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
418 goto dynamic;
419
Liam Girdwoodddee6272011-06-09 14:45:53 +0100420 /* Check that the codec and cpu DAIs are compatible */
421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100422 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200423 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100424 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100425 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200426 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100427 }
428
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100429 if (soc_pcm_has_symmetry(substream))
430 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
431
Liam Girdwoodddee6272011-06-09 14:45:53 +0100432 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100433 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000434 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100435 codec_dai->name, cpu_dai->name);
436 goto config_err;
437 }
438 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000439 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440 codec_dai->name, cpu_dai->name);
441 goto config_err;
442 }
443 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
444 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000445 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100446 codec_dai->name, cpu_dai->name);
447 goto config_err;
448 }
449
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200450 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000451
Liam Girdwoodddee6272011-06-09 14:45:53 +0100452 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800453 if (cpu_dai->active) {
454 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
455 if (ret != 0)
456 goto config_err;
457 }
458
459 if (codec_dai->active) {
460 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100461 if (ret != 0)
462 goto config_err;
463 }
464
Liam Girdwood103d84a2012-11-19 14:39:15 +0000465 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100466 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000467 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
468 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100469 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000470 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100471 runtime->hw.rate_max);
472
Liam Girdwood01d75842012-04-25 12:12:49 +0100473dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100474
475 snd_soc_runtime_activate(rtd, substream->stream);
476
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100477 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100478 return 0;
479
480config_err:
481 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
482 rtd->dai_link->ops->shutdown(substream);
483
484machine_err:
485 if (codec_dai->driver->ops->shutdown)
486 codec_dai->driver->ops->shutdown(substream, codec_dai);
487
488codec_dai_err:
489 if (platform->driver->ops && platform->driver->ops->close)
490 platform->driver->ops->close(substream);
491
492platform_err:
493 if (cpu_dai->driver->ops->shutdown)
494 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
495out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100496 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000497
498 pm_runtime_put(platform->dev);
499 pm_runtime_put(codec_dai->dev);
500 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800501 if (!codec_dai->active)
502 pinctrl_pm_select_sleep_state(codec_dai->dev);
503 if (!cpu_dai->active)
504 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000505
Liam Girdwoodddee6272011-06-09 14:45:53 +0100506 return ret;
507}
508
509/*
510 * Power down the audio subsystem pmdown_time msecs after close is called.
511 * This is to ensure there are no pops or clicks in between any music tracks
512 * due to DAPM power cycling.
513 */
514static void close_delayed_work(struct work_struct *work)
515{
516 struct snd_soc_pcm_runtime *rtd =
517 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
518 struct snd_soc_dai *codec_dai = rtd->codec_dai;
519
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100520 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100521
Liam Girdwood103d84a2012-11-19 14:39:15 +0000522 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100523 codec_dai->driver->playback.stream_name,
524 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600525 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100526
527 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600528 if (rtd->pop_wait == 1) {
529 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800530 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000531 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100532 }
533
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100534 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100535}
536
537/*
538 * Called by ALSA when a PCM substream is closed. Private data can be
539 * freed here. The cpu DAI, codec DAI, machine and platform are also
540 * shutdown.
541 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100542static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543{
544 struct snd_soc_pcm_runtime *rtd = substream->private_data;
545 struct snd_soc_platform *platform = rtd->platform;
546 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
547 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100548
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100549 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100551 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100552
Dong Aisheng17841022011-08-29 17:15:14 +0800553 /* clear the corresponding DAIs rate when inactive */
554 if (!cpu_dai->active)
555 cpu_dai->rate = 0;
556
557 if (!codec_dai->active)
558 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200559
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 if (cpu_dai->driver->ops->shutdown)
561 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
562
563 if (codec_dai->driver->ops->shutdown)
564 codec_dai->driver->ops->shutdown(substream, codec_dai);
565
566 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
567 rtd->dai_link->ops->shutdown(substream);
568
569 if (platform->driver->ops && platform->driver->ops->close)
570 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100571
572 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100573 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300574 /* powered down playback stream now */
575 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800576 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800577 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300578 } else {
579 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600580 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100581 queue_delayed_work(system_power_efficient_wq,
582 &rtd->delayed_work,
583 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300584 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100585 } else {
586 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800587 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000588 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100589 }
590
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100591 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000592
593 pm_runtime_put(platform->dev);
594 pm_runtime_put(codec_dai->dev);
595 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800596 if (!codec_dai->active)
597 pinctrl_pm_select_sleep_state(codec_dai->dev);
598 if (!cpu_dai->active)
599 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000600
Liam Girdwoodddee6272011-06-09 14:45:53 +0100601 return 0;
602}
603
604/*
605 * Called by ALSA when the PCM substream is prepared, can set format, sample
606 * rate, etc. This function is non atomic and can be called multiple times,
607 * it can refer to the runtime info.
608 */
609static int soc_pcm_prepare(struct snd_pcm_substream *substream)
610{
611 struct snd_soc_pcm_runtime *rtd = substream->private_data;
612 struct snd_soc_platform *platform = rtd->platform;
613 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
614 struct snd_soc_dai *codec_dai = rtd->codec_dai;
615 int ret = 0;
616
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100617 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618
619 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
620 ret = rtd->dai_link->ops->prepare(substream);
621 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000622 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
623 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 goto out;
625 }
626 }
627
628 if (platform->driver->ops && platform->driver->ops->prepare) {
629 ret = platform->driver->ops->prepare(substream);
630 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000631 dev_err(platform->dev, "ASoC: platform prepare error:"
632 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100633 goto out;
634 }
635 }
636
Mark Brownc5914b02013-10-30 17:47:39 -0700637 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100638 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
639 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000640 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000641 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 goto out;
643 }
644 }
645
Mark Brownc5914b02013-10-30 17:47:39 -0700646 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100647 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
648 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000649 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000650 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651 goto out;
652 }
653 }
654
655 /* cancel any delayed stream shutdown that is pending */
656 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600657 rtd->pop_wait) {
658 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100659 cancel_delayed_work(&rtd->delayed_work);
660 }
661
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000662 snd_soc_dapm_stream_event(rtd, substream->stream,
663 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100664
Mark Brownda183962013-02-06 15:44:07 +0000665 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100666
667out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100668 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669 return ret;
670}
671
672/*
673 * Called by ALSA when the hardware params are set by application. This
674 * function can also be called multiple times and can allocate buffers
675 * (using snd_pcm_lib_* ). It's non-atomic.
676 */
677static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
678 struct snd_pcm_hw_params *params)
679{
680 struct snd_soc_pcm_runtime *rtd = substream->private_data;
681 struct snd_soc_platform *platform = rtd->platform;
682 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
683 struct snd_soc_dai *codec_dai = rtd->codec_dai;
684 int ret = 0;
685
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100686 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100687
Nicolin Chen3635bf02013-11-13 18:56:24 +0800688 ret = soc_pcm_params_symmetry(substream, params);
689 if (ret)
690 goto out;
691
Liam Girdwoodddee6272011-06-09 14:45:53 +0100692 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
693 ret = rtd->dai_link->ops->hw_params(substream, params);
694 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000695 dev_err(rtd->card->dev, "ASoC: machine hw_params"
696 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100697 goto out;
698 }
699 }
700
Mark Brownc5914b02013-10-30 17:47:39 -0700701 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100702 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
703 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000704 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
705 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100706 goto codec_err;
707 }
708 }
709
Mark Brownc5914b02013-10-30 17:47:39 -0700710 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100711 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
712 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000713 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000714 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100715 goto interface_err;
716 }
717 }
718
719 if (platform->driver->ops && platform->driver->ops->hw_params) {
720 ret = platform->driver->ops->hw_params(substream, params);
721 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000722 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000723 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100724 goto platform_err;
725 }
726 }
727
Nicolin Chen3635bf02013-11-13 18:56:24 +0800728 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800729 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800730 cpu_dai->channels = params_channels(params);
731 cpu_dai->sample_bits =
732 snd_pcm_format_physical_width(params_format(params));
733
Dong Aisheng17841022011-08-29 17:15:14 +0800734 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800735 codec_dai->channels = params_channels(params);
736 codec_dai->sample_bits =
737 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100738
739out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100740 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100741 return ret;
742
743platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700744 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100745 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
746
747interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700748 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100749 codec_dai->driver->ops->hw_free(substream, codec_dai);
750
751codec_err:
752 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
753 rtd->dai_link->ops->hw_free(substream);
754
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100755 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756 return ret;
757}
758
759/*
760 * Frees resources allocated by hw_params, can be called multiple times
761 */
762static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
763{
764 struct snd_soc_pcm_runtime *rtd = substream->private_data;
765 struct snd_soc_platform *platform = rtd->platform;
766 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
767 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800768 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100769
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100770 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100771
Nicolin Chend3383422013-11-20 18:37:09 +0800772 /* clear the corresponding DAIs parameters when going to be inactive */
773 if (cpu_dai->active == 1) {
774 cpu_dai->rate = 0;
775 cpu_dai->channels = 0;
776 cpu_dai->sample_bits = 0;
777 }
778
779 if (codec_dai->active == 1) {
780 codec_dai->rate = 0;
781 codec_dai->channels = 0;
782 codec_dai->sample_bits = 0;
783 }
784
Liam Girdwoodddee6272011-06-09 14:45:53 +0100785 /* apply codec digital mute */
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800786 if ((playback && codec_dai->playback_active == 1) ||
787 (!playback && codec_dai->capture_active == 1))
Mark Brownda183962013-02-06 15:44:07 +0000788 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100789
790 /* free any machine hw params */
791 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
792 rtd->dai_link->ops->hw_free(substream);
793
794 /* free any DMA resources */
795 if (platform->driver->ops && platform->driver->ops->hw_free)
796 platform->driver->ops->hw_free(substream);
797
798 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700799 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100800 codec_dai->driver->ops->hw_free(substream, codec_dai);
801
Mark Brownc5914b02013-10-30 17:47:39 -0700802 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
804
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100805 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100806 return 0;
807}
808
809static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
810{
811 struct snd_soc_pcm_runtime *rtd = substream->private_data;
812 struct snd_soc_platform *platform = rtd->platform;
813 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
814 struct snd_soc_dai *codec_dai = rtd->codec_dai;
815 int ret;
816
Mark Brownc5914b02013-10-30 17:47:39 -0700817 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100818 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
819 if (ret < 0)
820 return ret;
821 }
822
823 if (platform->driver->ops && platform->driver->ops->trigger) {
824 ret = platform->driver->ops->trigger(substream, cmd);
825 if (ret < 0)
826 return ret;
827 }
828
Mark Brownc5914b02013-10-30 17:47:39 -0700829 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100830 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
831 if (ret < 0)
832 return ret;
833 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +0200834
835 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
836 ret = rtd->dai_link->ops->trigger(substream, cmd);
837 if (ret < 0)
838 return ret;
839 }
840
Liam Girdwoodddee6272011-06-09 14:45:53 +0100841 return 0;
842}
843
Mark Brown45c0a182012-05-09 21:46:27 +0100844static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
845 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100846{
847 struct snd_soc_pcm_runtime *rtd = substream->private_data;
848 struct snd_soc_platform *platform = rtd->platform;
849 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
850 struct snd_soc_dai *codec_dai = rtd->codec_dai;
851 int ret;
852
Mark Brownc5914b02013-10-30 17:47:39 -0700853 if (codec_dai->driver->ops &&
854 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100855 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
856 if (ret < 0)
857 return ret;
858 }
859
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +0100860 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100861 ret = platform->driver->bespoke_trigger(substream, cmd);
862 if (ret < 0)
863 return ret;
864 }
865
Mark Brownc5914b02013-10-30 17:47:39 -0700866 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100867 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
868 if (ret < 0)
869 return ret;
870 }
871 return 0;
872}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100873/*
874 * soc level wrapper for pointer callback
875 * If cpu_dai, codec_dai, platform driver has the delay callback, than
876 * the runtime->delay will be updated accordingly.
877 */
878static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
879{
880 struct snd_soc_pcm_runtime *rtd = substream->private_data;
881 struct snd_soc_platform *platform = rtd->platform;
882 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
883 struct snd_soc_dai *codec_dai = rtd->codec_dai;
884 struct snd_pcm_runtime *runtime = substream->runtime;
885 snd_pcm_uframes_t offset = 0;
886 snd_pcm_sframes_t delay = 0;
887
888 if (platform->driver->ops && platform->driver->ops->pointer)
889 offset = platform->driver->ops->pointer(substream);
890
Mark Brownc5914b02013-10-30 17:47:39 -0700891 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100892 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
893
Mark Brownc5914b02013-10-30 17:47:39 -0700894 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100895 delay += codec_dai->driver->ops->delay(substream, codec_dai);
896
897 if (platform->driver->delay)
898 delay += platform->driver->delay(substream, codec_dai);
899
900 runtime->delay = delay;
901
902 return offset;
903}
904
Liam Girdwood01d75842012-04-25 12:12:49 +0100905/* connect a FE and BE */
906static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
907 struct snd_soc_pcm_runtime *be, int stream)
908{
909 struct snd_soc_dpcm *dpcm;
910
911 /* only add new dpcms */
912 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
913 if (dpcm->be == be && dpcm->fe == fe)
914 return 0;
915 }
916
917 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
918 if (!dpcm)
919 return -ENOMEM;
920
921 dpcm->be = be;
922 dpcm->fe = fe;
923 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
924 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
925 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
926 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
927
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300928 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100929 stream ? "capture" : "playback", fe->dai_link->name,
930 stream ? "<-" : "->", be->dai_link->name);
931
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100932#ifdef CONFIG_DEBUG_FS
933 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
934 fe->debugfs_dpcm_root, &dpcm->state);
935#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100936 return 1;
937}
938
939/* reparent a BE onto another FE */
940static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
941 struct snd_soc_pcm_runtime *be, int stream)
942{
943 struct snd_soc_dpcm *dpcm;
944 struct snd_pcm_substream *fe_substream, *be_substream;
945
946 /* reparent if BE is connected to other FEs */
947 if (!be->dpcm[stream].users)
948 return;
949
950 be_substream = snd_soc_dpcm_get_substream(be, stream);
951
952 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
953 if (dpcm->fe == fe)
954 continue;
955
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300956 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100957 stream ? "capture" : "playback",
958 dpcm->fe->dai_link->name,
959 stream ? "<-" : "->", dpcm->be->dai_link->name);
960
961 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
962 be_substream->runtime = fe_substream->runtime;
963 break;
964 }
965}
966
967/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +0000968void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +0100969{
970 struct snd_soc_dpcm *dpcm, *d;
971
972 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000973 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100974 stream ? "capture" : "playback",
975 dpcm->be->dai_link->name);
976
977 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
978 continue;
979
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300980 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100981 stream ? "capture" : "playback", fe->dai_link->name,
982 stream ? "<-" : "->", dpcm->be->dai_link->name);
983
984 /* BEs still alive need new FE */
985 dpcm_be_reparent(fe, dpcm->be, stream);
986
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100987#ifdef CONFIG_DEBUG_FS
988 debugfs_remove(dpcm->debugfs_state);
989#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100990 list_del(&dpcm->list_be);
991 list_del(&dpcm->list_fe);
992 kfree(dpcm);
993 }
994}
995
996/* get BE for DAI widget and stream */
997static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
998 struct snd_soc_dapm_widget *widget, int stream)
999{
1000 struct snd_soc_pcm_runtime *be;
1001 int i;
1002
1003 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1004 for (i = 0; i < card->num_links; i++) {
1005 be = &card->rtd[i];
1006
Liam Girdwood35ea0652012-06-05 19:26:59 +01001007 if (!be->dai_link->no_pcm)
1008 continue;
1009
Liam Girdwood01d75842012-04-25 12:12:49 +01001010 if (be->cpu_dai->playback_widget == widget ||
1011 be->codec_dai->playback_widget == widget)
1012 return be;
1013 }
1014 } else {
1015
1016 for (i = 0; i < card->num_links; i++) {
1017 be = &card->rtd[i];
1018
Liam Girdwood35ea0652012-06-05 19:26:59 +01001019 if (!be->dai_link->no_pcm)
1020 continue;
1021
Liam Girdwood01d75842012-04-25 12:12:49 +01001022 if (be->cpu_dai->capture_widget == widget ||
1023 be->codec_dai->capture_widget == widget)
1024 return be;
1025 }
1026 }
1027
Liam Girdwood103d84a2012-11-19 14:39:15 +00001028 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001029 stream ? "capture" : "playback", widget->name);
1030 return NULL;
1031}
1032
1033static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001034 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001035{
1036 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001037 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001038 else
Benoit Cousson37018612014-04-24 14:01:45 +02001039 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001040}
1041
1042static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1043 struct snd_soc_dapm_widget *widget)
1044{
1045 int i;
1046
1047 for (i = 0; i < list->num_widgets; i++) {
1048 if (widget == list->widgets[i])
1049 return 1;
1050 }
1051
1052 return 0;
1053}
1054
Liam Girdwood23607022014-01-17 17:03:55 +00001055int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001056 int stream, struct snd_soc_dapm_widget_list **list_)
1057{
1058 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1059 struct snd_soc_dapm_widget_list *list;
1060 int paths;
1061
1062 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1063 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1064 if (list == NULL)
1065 return -ENOMEM;
1066
1067 /* get number of valid DAI paths and their widgets */
1068 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1069
Liam Girdwood103d84a2012-11-19 14:39:15 +00001070 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001071 stream ? "capture" : "playback");
1072
1073 *list_ = list;
1074 return paths;
1075}
1076
Liam Girdwood01d75842012-04-25 12:12:49 +01001077static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1078 struct snd_soc_dapm_widget_list **list_)
1079{
1080 struct snd_soc_dpcm *dpcm;
1081 struct snd_soc_dapm_widget_list *list = *list_;
1082 struct snd_soc_dapm_widget *widget;
1083 int prune = 0;
1084
1085 /* Destroy any old FE <--> BE connections */
1086 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1087
1088 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001089 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001090
1091 /* prune the BE if it's no longer in our active list */
1092 if (widget && widget_in_list(list, widget))
1093 continue;
1094
1095 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001096 widget = dai_get_widget(dpcm->be->codec_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001097
1098 /* prune the BE if it's no longer in our active list */
1099 if (widget && widget_in_list(list, widget))
1100 continue;
1101
Liam Girdwood103d84a2012-11-19 14:39:15 +00001102 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001103 stream ? "capture" : "playback",
1104 dpcm->be->dai_link->name, fe->dai_link->name);
1105 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1106 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1107 prune++;
1108 }
1109
Liam Girdwood103d84a2012-11-19 14:39:15 +00001110 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001111 return prune;
1112}
1113
1114static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1115 struct snd_soc_dapm_widget_list **list_)
1116{
1117 struct snd_soc_card *card = fe->card;
1118 struct snd_soc_dapm_widget_list *list = *list_;
1119 struct snd_soc_pcm_runtime *be;
1120 int i, new = 0, err;
1121
1122 /* Create any new FE <--> BE connections */
1123 for (i = 0; i < list->num_widgets; i++) {
1124
Mark Brown46162742013-06-05 19:36:11 +01001125 switch (list->widgets[i]->id) {
1126 case snd_soc_dapm_dai_in:
1127 case snd_soc_dapm_dai_out:
1128 break;
1129 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001130 continue;
Mark Brown46162742013-06-05 19:36:11 +01001131 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001132
1133 /* is there a valid BE rtd for this widget */
1134 be = dpcm_get_be(card, list->widgets[i], stream);
1135 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001136 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001137 list->widgets[i]->name);
1138 continue;
1139 }
1140
1141 /* make sure BE is a real BE */
1142 if (!be->dai_link->no_pcm)
1143 continue;
1144
1145 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001146 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001147 continue;
1148
1149 /* newly connected FE and BE */
1150 err = dpcm_be_connect(fe, be, stream);
1151 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001152 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001153 list->widgets[i]->name);
1154 break;
1155 } else if (err == 0) /* already connected */
1156 continue;
1157
1158 /* new */
1159 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1160 new++;
1161 }
1162
Liam Girdwood103d84a2012-11-19 14:39:15 +00001163 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001164 return new;
1165}
1166
1167/*
1168 * Find the corresponding BE DAIs that source or sink audio to this
1169 * FE substream.
1170 */
Liam Girdwood23607022014-01-17 17:03:55 +00001171int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001172 int stream, struct snd_soc_dapm_widget_list **list, int new)
1173{
1174 if (new)
1175 return dpcm_add_paths(fe, stream, list);
1176 else
1177 return dpcm_prune_paths(fe, stream, list);
1178}
1179
Liam Girdwood23607022014-01-17 17:03:55 +00001180void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001181{
1182 struct snd_soc_dpcm *dpcm;
1183
1184 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1185 dpcm->be->dpcm[stream].runtime_update =
1186 SND_SOC_DPCM_UPDATE_NO;
1187}
1188
1189static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1190 int stream)
1191{
1192 struct snd_soc_dpcm *dpcm;
1193
1194 /* disable any enabled and non active backends */
1195 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1196
1197 struct snd_soc_pcm_runtime *be = dpcm->be;
1198 struct snd_pcm_substream *be_substream =
1199 snd_soc_dpcm_get_substream(be, stream);
1200
1201 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001202 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001203 stream ? "capture" : "playback",
1204 be->dpcm[stream].state);
1205
1206 if (--be->dpcm[stream].users != 0)
1207 continue;
1208
1209 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1210 continue;
1211
1212 soc_pcm_close(be_substream);
1213 be_substream->runtime = NULL;
1214 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1215 }
1216}
1217
Liam Girdwood23607022014-01-17 17:03:55 +00001218int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001219{
1220 struct snd_soc_dpcm *dpcm;
1221 int err, count = 0;
1222
1223 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1224 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1225
1226 struct snd_soc_pcm_runtime *be = dpcm->be;
1227 struct snd_pcm_substream *be_substream =
1228 snd_soc_dpcm_get_substream(be, stream);
1229
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001230 if (!be_substream) {
1231 dev_err(be->dev, "ASoC: no backend %s stream\n",
1232 stream ? "capture" : "playback");
1233 continue;
1234 }
1235
Liam Girdwood01d75842012-04-25 12:12:49 +01001236 /* is this op for this BE ? */
1237 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1238 continue;
1239
1240 /* first time the dpcm is open ? */
1241 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001242 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001243 stream ? "capture" : "playback",
1244 be->dpcm[stream].state);
1245
1246 if (be->dpcm[stream].users++ != 0)
1247 continue;
1248
1249 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1250 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1251 continue;
1252
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001253 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1254 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001255
1256 be_substream->runtime = be->dpcm[stream].runtime;
1257 err = soc_pcm_open(be_substream);
1258 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001259 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001260 be->dpcm[stream].users--;
1261 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001262 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001263 stream ? "capture" : "playback",
1264 be->dpcm[stream].state);
1265
1266 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1267 goto unwind;
1268 }
1269
1270 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1271 count++;
1272 }
1273
1274 return count;
1275
1276unwind:
1277 /* disable any enabled and non active backends */
1278 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1279 struct snd_soc_pcm_runtime *be = dpcm->be;
1280 struct snd_pcm_substream *be_substream =
1281 snd_soc_dpcm_get_substream(be, stream);
1282
1283 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1284 continue;
1285
1286 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001287 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001288 stream ? "capture" : "playback",
1289 be->dpcm[stream].state);
1290
1291 if (--be->dpcm[stream].users != 0)
1292 continue;
1293
1294 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1295 continue;
1296
1297 soc_pcm_close(be_substream);
1298 be_substream->runtime = NULL;
1299 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1300 }
1301
1302 return err;
1303}
1304
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001305static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1306 struct snd_soc_pcm_stream *stream)
1307{
1308 runtime->hw.rate_min = stream->rate_min;
1309 runtime->hw.rate_max = stream->rate_max;
1310 runtime->hw.channels_min = stream->channels_min;
1311 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001312 if (runtime->hw.formats)
1313 runtime->hw.formats &= stream->formats;
1314 else
1315 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001316 runtime->hw.rates = stream->rates;
1317}
1318
Mark Brown45c0a182012-05-09 21:46:27 +01001319static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001320{
1321 struct snd_pcm_runtime *runtime = substream->runtime;
1322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1323 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1324 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1325
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1327 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1328 else
1329 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001330}
1331
1332static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1333{
1334 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1335 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1336 int stream = fe_substream->stream, ret = 0;
1337
1338 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1339
1340 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1341 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001342 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001343 goto be_err;
1344 }
1345
Liam Girdwood103d84a2012-11-19 14:39:15 +00001346 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001347
1348 /* start the DAI frontend */
1349 ret = soc_pcm_open(fe_substream);
1350 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001351 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001352 goto unwind;
1353 }
1354
1355 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1356
1357 dpcm_set_fe_runtime(fe_substream);
1358 snd_pcm_limit_hw_rates(runtime);
1359
1360 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1361 return 0;
1362
1363unwind:
1364 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1365be_err:
1366 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1367 return ret;
1368}
1369
Liam Girdwood23607022014-01-17 17:03:55 +00001370int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001371{
1372 struct snd_soc_dpcm *dpcm;
1373
1374 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1375 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1376
1377 struct snd_soc_pcm_runtime *be = dpcm->be;
1378 struct snd_pcm_substream *be_substream =
1379 snd_soc_dpcm_get_substream(be, stream);
1380
1381 /* is this op for this BE ? */
1382 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1383 continue;
1384
1385 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001386 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001387 stream ? "capture" : "playback",
1388 be->dpcm[stream].state);
1389
1390 if (--be->dpcm[stream].users != 0)
1391 continue;
1392
1393 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1394 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1395 continue;
1396
Liam Girdwood103d84a2012-11-19 14:39:15 +00001397 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001398 dpcm->fe->dai_link->name);
1399
1400 soc_pcm_close(be_substream);
1401 be_substream->runtime = NULL;
1402
1403 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1404 }
1405 return 0;
1406}
1407
1408static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1409{
1410 struct snd_soc_pcm_runtime *fe = substream->private_data;
1411 int stream = substream->stream;
1412
1413 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1414
1415 /* shutdown the BEs */
1416 dpcm_be_dai_shutdown(fe, substream->stream);
1417
Liam Girdwood103d84a2012-11-19 14:39:15 +00001418 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001419
1420 /* now shutdown the frontend */
1421 soc_pcm_close(substream);
1422
1423 /* run the stream event for each BE */
1424 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1425
1426 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1427 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1428 return 0;
1429}
1430
Liam Girdwood23607022014-01-17 17:03:55 +00001431int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001432{
1433 struct snd_soc_dpcm *dpcm;
1434
1435 /* only hw_params backends that are either sinks or sources
1436 * to this frontend DAI */
1437 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1438
1439 struct snd_soc_pcm_runtime *be = dpcm->be;
1440 struct snd_pcm_substream *be_substream =
1441 snd_soc_dpcm_get_substream(be, stream);
1442
1443 /* is this op for this BE ? */
1444 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1445 continue;
1446
1447 /* only free hw when no longer used - check all FEs */
1448 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1449 continue;
1450
1451 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1452 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1453 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001454 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001455 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1456 continue;
1457
Liam Girdwood103d84a2012-11-19 14:39:15 +00001458 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001459 dpcm->fe->dai_link->name);
1460
1461 soc_pcm_hw_free(be_substream);
1462
1463 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1464 }
1465
1466 return 0;
1467}
1468
Mark Brown45c0a182012-05-09 21:46:27 +01001469static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001470{
1471 struct snd_soc_pcm_runtime *fe = substream->private_data;
1472 int err, stream = substream->stream;
1473
1474 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1475 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1476
Liam Girdwood103d84a2012-11-19 14:39:15 +00001477 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001478
1479 /* call hw_free on the frontend */
1480 err = soc_pcm_hw_free(substream);
1481 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001482 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001483 fe->dai_link->name);
1484
1485 /* only hw_params backends that are either sinks or sources
1486 * to this frontend DAI */
1487 err = dpcm_be_dai_hw_free(fe, stream);
1488
1489 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1490 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1491
1492 mutex_unlock(&fe->card->mutex);
1493 return 0;
1494}
1495
Liam Girdwood23607022014-01-17 17:03:55 +00001496int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001497{
1498 struct snd_soc_dpcm *dpcm;
1499 int ret;
1500
1501 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1502
1503 struct snd_soc_pcm_runtime *be = dpcm->be;
1504 struct snd_pcm_substream *be_substream =
1505 snd_soc_dpcm_get_substream(be, stream);
1506
1507 /* is this op for this BE ? */
1508 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1509 continue;
1510
1511 /* only allow hw_params() if no connected FEs are running */
1512 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1513 continue;
1514
1515 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1516 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1517 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1518 continue;
1519
Liam Girdwood103d84a2012-11-19 14:39:15 +00001520 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001521 dpcm->fe->dai_link->name);
1522
1523 /* copy params for each dpcm */
1524 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1525 sizeof(struct snd_pcm_hw_params));
1526
1527 /* perform any hw_params fixups */
1528 if (be->dai_link->be_hw_params_fixup) {
1529 ret = be->dai_link->be_hw_params_fixup(be,
1530 &dpcm->hw_params);
1531 if (ret < 0) {
1532 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001533 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001534 ret);
1535 goto unwind;
1536 }
1537 }
1538
1539 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1540 if (ret < 0) {
1541 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001542 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001543 goto unwind;
1544 }
1545
1546 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1547 }
1548 return 0;
1549
1550unwind:
1551 /* disable any enabled and non active backends */
1552 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1553 struct snd_soc_pcm_runtime *be = dpcm->be;
1554 struct snd_pcm_substream *be_substream =
1555 snd_soc_dpcm_get_substream(be, stream);
1556
1557 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1558 continue;
1559
1560 /* only allow hw_free() if no connected FEs are running */
1561 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1562 continue;
1563
1564 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1565 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1566 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1567 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1568 continue;
1569
1570 soc_pcm_hw_free(be_substream);
1571 }
1572
1573 return ret;
1574}
1575
Mark Brown45c0a182012-05-09 21:46:27 +01001576static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1577 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001578{
1579 struct snd_soc_pcm_runtime *fe = substream->private_data;
1580 int ret, stream = substream->stream;
1581
1582 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1583 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1584
1585 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1586 sizeof(struct snd_pcm_hw_params));
1587 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1588 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001589 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001590 goto out;
1591 }
1592
Liam Girdwood103d84a2012-11-19 14:39:15 +00001593 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001594 fe->dai_link->name, params_rate(params),
1595 params_channels(params), params_format(params));
1596
1597 /* call hw_params on the frontend */
1598 ret = soc_pcm_hw_params(substream, params);
1599 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001600 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001601 dpcm_be_dai_hw_free(fe, stream);
1602 } else
1603 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1604
1605out:
1606 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1607 mutex_unlock(&fe->card->mutex);
1608 return ret;
1609}
1610
1611static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1612 struct snd_pcm_substream *substream, int cmd)
1613{
1614 int ret;
1615
Liam Girdwood103d84a2012-11-19 14:39:15 +00001616 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001617 dpcm->fe->dai_link->name, cmd);
1618
1619 ret = soc_pcm_trigger(substream, cmd);
1620 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001621 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001622
1623 return ret;
1624}
1625
Liam Girdwood23607022014-01-17 17:03:55 +00001626int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001627 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001628{
1629 struct snd_soc_dpcm *dpcm;
1630 int ret = 0;
1631
1632 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1633
1634 struct snd_soc_pcm_runtime *be = dpcm->be;
1635 struct snd_pcm_substream *be_substream =
1636 snd_soc_dpcm_get_substream(be, stream);
1637
1638 /* is this op for this BE ? */
1639 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1640 continue;
1641
1642 switch (cmd) {
1643 case SNDRV_PCM_TRIGGER_START:
1644 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1645 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1646 continue;
1647
1648 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1649 if (ret)
1650 return ret;
1651
1652 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1653 break;
1654 case SNDRV_PCM_TRIGGER_RESUME:
1655 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1656 continue;
1657
1658 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1659 if (ret)
1660 return ret;
1661
1662 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1663 break;
1664 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1665 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
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_START;
1673 break;
1674 case SNDRV_PCM_TRIGGER_STOP:
1675 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1676 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_STOP;
1686 break;
1687 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08001688 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01001689 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_SUSPEND;
1699 break;
1700 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1701 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1702 continue;
1703
1704 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1705 continue;
1706
1707 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1708 if (ret)
1709 return ret;
1710
1711 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1712 break;
1713 }
1714 }
1715
1716 return ret;
1717}
1718EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1719
Mark Brown45c0a182012-05-09 21:46:27 +01001720static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001721{
1722 struct snd_soc_pcm_runtime *fe = substream->private_data;
1723 int stream = substream->stream, ret;
1724 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1725
1726 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1727
1728 switch (trigger) {
1729 case SND_SOC_DPCM_TRIGGER_PRE:
1730 /* call trigger on the frontend before the backend. */
1731
Liam Girdwood103d84a2012-11-19 14:39:15 +00001732 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001733 fe->dai_link->name, cmd);
1734
1735 ret = soc_pcm_trigger(substream, cmd);
1736 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001737 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001738 goto out;
1739 }
1740
1741 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1742 break;
1743 case SND_SOC_DPCM_TRIGGER_POST:
1744 /* call trigger on the frontend after the backend. */
1745
1746 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1747 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001748 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001749 goto out;
1750 }
1751
Liam Girdwood103d84a2012-11-19 14:39:15 +00001752 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001753 fe->dai_link->name, cmd);
1754
1755 ret = soc_pcm_trigger(substream, cmd);
1756 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001757 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1758 /* bespoke trigger() - handles both FE and BEs */
1759
Liam Girdwood103d84a2012-11-19 14:39:15 +00001760 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001761 fe->dai_link->name, cmd);
1762
1763 ret = soc_pcm_bespoke_trigger(substream, cmd);
1764 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001765 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001766 goto out;
1767 }
1768 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001769 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001770 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001771 fe->dai_link->name);
1772 ret = -EINVAL;
1773 goto out;
1774 }
1775
1776 switch (cmd) {
1777 case SNDRV_PCM_TRIGGER_START:
1778 case SNDRV_PCM_TRIGGER_RESUME:
1779 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1780 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1781 break;
1782 case SNDRV_PCM_TRIGGER_STOP:
1783 case SNDRV_PCM_TRIGGER_SUSPEND:
1784 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1785 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1786 break;
1787 }
1788
1789out:
1790 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1791 return ret;
1792}
1793
Liam Girdwood23607022014-01-17 17:03:55 +00001794int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001795{
1796 struct snd_soc_dpcm *dpcm;
1797 int ret = 0;
1798
1799 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1800
1801 struct snd_soc_pcm_runtime *be = dpcm->be;
1802 struct snd_pcm_substream *be_substream =
1803 snd_soc_dpcm_get_substream(be, stream);
1804
1805 /* is this op for this BE ? */
1806 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1807 continue;
1808
1809 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1810 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1811 continue;
1812
Liam Girdwood103d84a2012-11-19 14:39:15 +00001813 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001814 dpcm->fe->dai_link->name);
1815
1816 ret = soc_pcm_prepare(be_substream);
1817 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001818 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001819 ret);
1820 break;
1821 }
1822
1823 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1824 }
1825 return ret;
1826}
1827
Mark Brown45c0a182012-05-09 21:46:27 +01001828static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001829{
1830 struct snd_soc_pcm_runtime *fe = substream->private_data;
1831 int stream = substream->stream, ret = 0;
1832
1833 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1834
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001836
1837 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1838
1839 /* there is no point preparing this FE if there are no BEs */
1840 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001841 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001842 fe->dai_link->name);
1843 ret = -EINVAL;
1844 goto out;
1845 }
1846
1847 ret = dpcm_be_dai_prepare(fe, substream->stream);
1848 if (ret < 0)
1849 goto out;
1850
1851 /* call prepare on the frontend */
1852 ret = soc_pcm_prepare(substream);
1853 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001854 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001855 fe->dai_link->name);
1856 goto out;
1857 }
1858
1859 /* run the stream event for each BE */
1860 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1861 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1862
1863out:
1864 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1865 mutex_unlock(&fe->card->mutex);
1866
1867 return ret;
1868}
1869
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001870static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1871 unsigned int cmd, void *arg)
1872{
1873 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1874 struct snd_soc_platform *platform = rtd->platform;
1875
Mark Brownc5914b02013-10-30 17:47:39 -07001876 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001877 return platform->driver->ops->ioctl(substream, cmd, arg);
1878 return snd_pcm_lib_ioctl(substream, cmd, arg);
1879}
1880
Liam Girdwood618dae12012-04-25 12:12:51 +01001881static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1882{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001883 struct snd_pcm_substream *substream =
1884 snd_soc_dpcm_get_substream(fe, stream);
1885 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001886 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001887
Liam Girdwood103d84a2012-11-19 14:39:15 +00001888 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001889 stream ? "capture" : "playback", fe->dai_link->name);
1890
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001891 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1892 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001893 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001894 fe->dai_link->name);
1895
1896 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1897 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001898 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001899 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001900 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001901 fe->dai_link->name);
1902
1903 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1904 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001905 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001906 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001907
1908 err = dpcm_be_dai_hw_free(fe, stream);
1909 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001910 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001911
1912 err = dpcm_be_dai_shutdown(fe, stream);
1913 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001914 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001915
1916 /* run the stream event for each BE */
1917 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1918
1919 return 0;
1920}
1921
1922static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1923{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001924 struct snd_pcm_substream *substream =
1925 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001926 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001927 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001928 int ret;
1929
Liam Girdwood103d84a2012-11-19 14:39:15 +00001930 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001931 stream ? "capture" : "playback", fe->dai_link->name);
1932
1933 /* Only start the BE if the FE is ready */
1934 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1935 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1936 return -EINVAL;
1937
1938 /* startup must always be called for new BEs */
1939 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001940 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001941 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001942
1943 /* keep going if FE state is > open */
1944 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1945 return 0;
1946
1947 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001948 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001949 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001950
1951 /* keep going if FE state is > hw_params */
1952 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1953 return 0;
1954
1955
1956 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001957 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001958 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001959
1960 /* run the stream event for each BE */
1961 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1962
1963 /* keep going if FE state is > prepare */
1964 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1965 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1966 return 0;
1967
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001968 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1969 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001970 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001971 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001972
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001973 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1974 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001975 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001976 goto hw_free;
1977 }
1978 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001979 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001980 fe->dai_link->name);
1981
1982 ret = dpcm_be_dai_trigger(fe, stream,
1983 SNDRV_PCM_TRIGGER_START);
1984 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001985 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001986 goto hw_free;
1987 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001988 }
1989
1990 return 0;
1991
1992hw_free:
1993 dpcm_be_dai_hw_free(fe, stream);
1994close:
1995 dpcm_be_dai_shutdown(fe, stream);
1996disconnect:
1997 /* disconnect any non started BEs */
1998 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1999 struct snd_soc_pcm_runtime *be = dpcm->be;
2000 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2001 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2002 }
2003
2004 return ret;
2005}
2006
2007static int dpcm_run_new_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_startup(fe, stream);
2013 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002014 dev_err(fe->dev, "ASoC: failed to startup 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
2020static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2021{
2022 int ret;
2023
2024 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2025 ret = dpcm_run_update_shutdown(fe, stream);
2026 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002027 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002028 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2029
2030 return ret;
2031}
2032
2033/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2034 * any DAI links.
2035 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002036int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002037{
Liam Girdwood618dae12012-04-25 12:12:51 +01002038 int i, old, new, paths;
2039
Liam Girdwood618dae12012-04-25 12:12:51 +01002040 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2041 for (i = 0; i < card->num_rtd; i++) {
2042 struct snd_soc_dapm_widget_list *list;
2043 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2044
2045 /* make sure link is FE */
2046 if (!fe->dai_link->dynamic)
2047 continue;
2048
2049 /* only check active links */
2050 if (!fe->cpu_dai->active)
2051 continue;
2052
2053 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002054 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002055 fe->dai_link->name);
2056
2057 /* skip if FE doesn't have playback capability */
2058 if (!fe->cpu_dai->driver->playback.channels_min)
2059 goto capture;
2060
2061 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2062 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002063 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002064 fe->dai_link->name, "playback");
2065 mutex_unlock(&card->mutex);
2066 return paths;
2067 }
2068
2069 /* update any new playback paths */
2070 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2071 if (new) {
2072 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2073 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2074 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2075 }
2076
2077 /* update any old playback paths */
2078 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2079 if (old) {
2080 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2081 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2082 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2083 }
2084
2085capture:
2086 /* skip if FE doesn't have capture capability */
2087 if (!fe->cpu_dai->driver->capture.channels_min)
2088 continue;
2089
2090 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2091 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002092 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002093 fe->dai_link->name, "capture");
2094 mutex_unlock(&card->mutex);
2095 return paths;
2096 }
2097
2098 /* update any new capture paths */
2099 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2100 if (new) {
2101 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2102 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2103 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2104 }
2105
2106 /* update any old capture paths */
2107 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2108 if (old) {
2109 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2110 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2111 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2112 }
2113
2114 dpcm_path_put(&list);
2115 }
2116
2117 mutex_unlock(&card->mutex);
2118 return 0;
2119}
Liam Girdwood01d75842012-04-25 12:12:49 +01002120int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2121{
2122 struct snd_soc_dpcm *dpcm;
2123 struct list_head *clients =
2124 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2125
2126 list_for_each_entry(dpcm, clients, list_be) {
2127
2128 struct snd_soc_pcm_runtime *be = dpcm->be;
2129 struct snd_soc_dai *dai = be->codec_dai;
2130 struct snd_soc_dai_driver *drv = dai->driver;
2131
2132 if (be->dai_link->ignore_suspend)
2133 continue;
2134
Liam Girdwood103d84a2012-11-19 14:39:15 +00002135 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002136
Mark Brownc5914b02013-10-30 17:47:39 -07002137 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2138 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002139 }
2140
2141 return 0;
2142}
2143
Mark Brown45c0a182012-05-09 21:46:27 +01002144static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002145{
2146 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2147 struct snd_soc_dpcm *dpcm;
2148 struct snd_soc_dapm_widget_list *list;
2149 int ret;
2150 int stream = fe_substream->stream;
2151
2152 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2153 fe->dpcm[stream].runtime = fe_substream->runtime;
2154
2155 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002156 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002157 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002158 }
2159
2160 /* calculate valid and active FE <-> BE dpcms */
2161 dpcm_process_paths(fe, stream, &list, 1);
2162
2163 ret = dpcm_fe_dai_startup(fe_substream);
2164 if (ret < 0) {
2165 /* clean up all links */
2166 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2167 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2168
2169 dpcm_be_disconnect(fe, stream);
2170 fe->dpcm[stream].runtime = NULL;
2171 }
2172
2173 dpcm_clear_pending_state(fe, stream);
2174 dpcm_path_put(&list);
2175 mutex_unlock(&fe->card->mutex);
2176 return ret;
2177}
2178
Mark Brown45c0a182012-05-09 21:46:27 +01002179static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002180{
2181 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2182 struct snd_soc_dpcm *dpcm;
2183 int stream = fe_substream->stream, ret;
2184
2185 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2186 ret = dpcm_fe_dai_shutdown(fe_substream);
2187
2188 /* mark FE's links ready to prune */
2189 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2190 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2191
2192 dpcm_be_disconnect(fe, stream);
2193
2194 fe->dpcm[stream].runtime = NULL;
2195 mutex_unlock(&fe->card->mutex);
2196 return ret;
2197}
2198
Liam Girdwoodddee6272011-06-09 14:45:53 +01002199/* create a new pcm */
2200int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2201{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002202 struct snd_soc_platform *platform = rtd->platform;
2203 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2204 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2205 struct snd_pcm *pcm;
2206 char new_name[64];
2207 int ret = 0, playback = 0, capture = 0;
2208
Liam Girdwood01d75842012-04-25 12:12:49 +01002209 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002210 playback = rtd->dai_link->dpcm_playback;
2211 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 } else {
Mark Brown05679092013-06-01 23:13:53 +01002213 if (codec_dai->driver->playback.channels_min &&
2214 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002215 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002216 if (codec_dai->driver->capture.channels_min &&
2217 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002218 capture = 1;
2219 }
Sangsu Parka5002312012-01-02 17:15:10 +09002220
Fabio Estevamd6bead02013-08-29 10:32:13 -03002221 if (rtd->dai_link->playback_only) {
2222 playback = 1;
2223 capture = 0;
2224 }
2225
2226 if (rtd->dai_link->capture_only) {
2227 playback = 0;
2228 capture = 1;
2229 }
2230
Liam Girdwood01d75842012-04-25 12:12:49 +01002231 /* create the PCM */
2232 if (rtd->dai_link->no_pcm) {
2233 snprintf(new_name, sizeof(new_name), "(%s)",
2234 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002235
Liam Girdwood01d75842012-04-25 12:12:49 +01002236 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2237 playback, capture, &pcm);
2238 } else {
2239 if (rtd->dai_link->dynamic)
2240 snprintf(new_name, sizeof(new_name), "%s (*)",
2241 rtd->dai_link->stream_name);
2242 else
2243 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2244 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002245
Liam Girdwood01d75842012-04-25 12:12:49 +01002246 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2247 capture, &pcm);
2248 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002249 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002250 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002251 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002252 return ret;
2253 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002254 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002255
2256 /* DAPM dai link stream work */
2257 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2258
2259 rtd->pcm = pcm;
2260 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002261
2262 if (rtd->dai_link->no_pcm) {
2263 if (playback)
2264 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2265 if (capture)
2266 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2267 goto out;
2268 }
2269
2270 /* ASoC PCM operations */
2271 if (rtd->dai_link->dynamic) {
2272 rtd->ops.open = dpcm_fe_dai_open;
2273 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2274 rtd->ops.prepare = dpcm_fe_dai_prepare;
2275 rtd->ops.trigger = dpcm_fe_dai_trigger;
2276 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2277 rtd->ops.close = dpcm_fe_dai_close;
2278 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002279 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002280 } else {
2281 rtd->ops.open = soc_pcm_open;
2282 rtd->ops.hw_params = soc_pcm_hw_params;
2283 rtd->ops.prepare = soc_pcm_prepare;
2284 rtd->ops.trigger = soc_pcm_trigger;
2285 rtd->ops.hw_free = soc_pcm_hw_free;
2286 rtd->ops.close = soc_pcm_close;
2287 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002288 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002289 }
2290
Liam Girdwoodddee6272011-06-09 14:45:53 +01002291 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002292 rtd->ops.ack = platform->driver->ops->ack;
2293 rtd->ops.copy = platform->driver->ops->copy;
2294 rtd->ops.silence = platform->driver->ops->silence;
2295 rtd->ops.page = platform->driver->ops->page;
2296 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002297 }
2298
2299 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002300 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002301
2302 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002303 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002304
2305 if (platform->driver->pcm_new) {
2306 ret = platform->driver->pcm_new(rtd);
2307 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002308 dev_err(platform->dev,
2309 "ASoC: pcm constructor failed: %d\n",
2310 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002311 return ret;
2312 }
2313 }
2314
2315 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002316out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002317 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002318 cpu_dai->name);
2319 return ret;
2320}
Liam Girdwood01d75842012-04-25 12:12:49 +01002321
2322/* is the current PCM operation for this FE ? */
2323int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2324{
2325 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2326 return 1;
2327 return 0;
2328}
2329EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2330
2331/* is the current PCM operation for this BE ? */
2332int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2333 struct snd_soc_pcm_runtime *be, int stream)
2334{
2335 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2336 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2337 be->dpcm[stream].runtime_update))
2338 return 1;
2339 return 0;
2340}
2341EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2342
2343/* get the substream for this BE */
2344struct snd_pcm_substream *
2345 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2346{
2347 return be->pcm->streams[stream].substream;
2348}
2349EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2350
2351/* get the BE runtime state */
2352enum snd_soc_dpcm_state
2353 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2354{
2355 return be->dpcm[stream].state;
2356}
2357EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2358
2359/* set the BE runtime state */
2360void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2361 int stream, enum snd_soc_dpcm_state state)
2362{
2363 be->dpcm[stream].state = state;
2364}
2365EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2366
2367/*
2368 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2369 * are not running, paused or suspended for the specified stream direction.
2370 */
2371int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2372 struct snd_soc_pcm_runtime *be, int stream)
2373{
2374 struct snd_soc_dpcm *dpcm;
2375 int state;
2376
2377 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2378
2379 if (dpcm->fe == fe)
2380 continue;
2381
2382 state = dpcm->fe->dpcm[stream].state;
2383 if (state == SND_SOC_DPCM_STATE_START ||
2384 state == SND_SOC_DPCM_STATE_PAUSED ||
2385 state == SND_SOC_DPCM_STATE_SUSPEND)
2386 return 0;
2387 }
2388
2389 /* it's safe to free/stop this BE DAI */
2390 return 1;
2391}
2392EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2393
2394/*
2395 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2396 * running, paused or suspended for the specified stream direction.
2397 */
2398int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2399 struct snd_soc_pcm_runtime *be, int stream)
2400{
2401 struct snd_soc_dpcm *dpcm;
2402 int state;
2403
2404 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2405
2406 if (dpcm->fe == fe)
2407 continue;
2408
2409 state = dpcm->fe->dpcm[stream].state;
2410 if (state == SND_SOC_DPCM_STATE_START ||
2411 state == SND_SOC_DPCM_STATE_PAUSED ||
2412 state == SND_SOC_DPCM_STATE_SUSPEND ||
2413 state == SND_SOC_DPCM_STATE_PREPARE)
2414 return 0;
2415 }
2416
2417 /* it's safe to change hw_params */
2418 return 1;
2419}
2420EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002421
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002422int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2423 int cmd, struct snd_soc_platform *platform)
2424{
Mark Brownc5914b02013-10-30 17:47:39 -07002425 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002426 return platform->driver->ops->trigger(substream, cmd);
2427 return 0;
2428}
2429EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2430
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002431#ifdef CONFIG_DEBUG_FS
2432static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2433{
2434 switch (state) {
2435 case SND_SOC_DPCM_STATE_NEW:
2436 return "new";
2437 case SND_SOC_DPCM_STATE_OPEN:
2438 return "open";
2439 case SND_SOC_DPCM_STATE_HW_PARAMS:
2440 return "hw_params";
2441 case SND_SOC_DPCM_STATE_PREPARE:
2442 return "prepare";
2443 case SND_SOC_DPCM_STATE_START:
2444 return "start";
2445 case SND_SOC_DPCM_STATE_STOP:
2446 return "stop";
2447 case SND_SOC_DPCM_STATE_SUSPEND:
2448 return "suspend";
2449 case SND_SOC_DPCM_STATE_PAUSED:
2450 return "paused";
2451 case SND_SOC_DPCM_STATE_HW_FREE:
2452 return "hw_free";
2453 case SND_SOC_DPCM_STATE_CLOSE:
2454 return "close";
2455 }
2456
2457 return "unknown";
2458}
2459
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002460static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2461 int stream, char *buf, size_t size)
2462{
2463 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2464 struct snd_soc_dpcm *dpcm;
2465 ssize_t offset = 0;
2466
2467 /* FE state */
2468 offset += snprintf(buf + offset, size - offset,
2469 "[%s - %s]\n", fe->dai_link->name,
2470 stream ? "Capture" : "Playback");
2471
2472 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2473 dpcm_state_string(fe->dpcm[stream].state));
2474
2475 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2476 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2477 offset += snprintf(buf + offset, size - offset,
2478 "Hardware Params: "
2479 "Format = %s, Channels = %d, Rate = %d\n",
2480 snd_pcm_format_name(params_format(params)),
2481 params_channels(params),
2482 params_rate(params));
2483
2484 /* BEs state */
2485 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2486
2487 if (list_empty(&fe->dpcm[stream].be_clients)) {
2488 offset += snprintf(buf + offset, size - offset,
2489 " No active DSP links\n");
2490 goto out;
2491 }
2492
2493 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2494 struct snd_soc_pcm_runtime *be = dpcm->be;
2495 params = &dpcm->hw_params;
2496
2497 offset += snprintf(buf + offset, size - offset,
2498 "- %s\n", be->dai_link->name);
2499
2500 offset += snprintf(buf + offset, size - offset,
2501 " State: %s\n",
2502 dpcm_state_string(be->dpcm[stream].state));
2503
2504 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2505 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2506 offset += snprintf(buf + offset, size - offset,
2507 " Hardware Params: "
2508 "Format = %s, Channels = %d, Rate = %d\n",
2509 snd_pcm_format_name(params_format(params)),
2510 params_channels(params),
2511 params_rate(params));
2512 }
2513
2514out:
2515 return offset;
2516}
2517
2518static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2519 size_t count, loff_t *ppos)
2520{
2521 struct snd_soc_pcm_runtime *fe = file->private_data;
2522 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2523 char *buf;
2524
2525 buf = kmalloc(out_count, GFP_KERNEL);
2526 if (!buf)
2527 return -ENOMEM;
2528
2529 if (fe->cpu_dai->driver->playback.channels_min)
2530 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2531 buf + offset, out_count - offset);
2532
2533 if (fe->cpu_dai->driver->capture.channels_min)
2534 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2535 buf + offset, out_count - offset);
2536
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002537 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002538
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002539 kfree(buf);
2540 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002541}
2542
2543static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002544 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002545 .read = dpcm_state_read_file,
2546 .llseek = default_llseek,
2547};
2548
2549int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2550{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002551 if (!rtd->dai_link)
2552 return 0;
2553
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002554 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2555 rtd->card->debugfs_card_root);
2556 if (!rtd->debugfs_dpcm_root) {
2557 dev_dbg(rtd->dev,
2558 "ASoC: Failed to create dpcm debugfs directory %s\n",
2559 rtd->dai_link->name);
2560 return -EINVAL;
2561 }
2562
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002563 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002564 rtd->debugfs_dpcm_root,
2565 rtd, &dpcm_state_fops);
2566
2567 return 0;
2568}
2569#endif