blob: 1a9857519d65577a1156d4349df17b3b7b909ab8 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010038 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48{
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
50 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 codec_dai->playback_active++;
57 } else {
58 cpu_dai->capture_active++;
59 codec_dai->capture_active++;
60 }
61
62 cpu_dai->active++;
63 codec_dai->active++;
64 rtd->codec->active++;
65}
66
67/**
68 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
69 * @rtd: ASoC PCM runtime that is deactivated
70 * @stream: Direction of the PCM stream
71 *
72 * Decrements the active count for all the DAIs and components attached to a PCM
73 * runtime. Should typically be called when a stream is closed.
74 *
75 * Must be called with the rtd->pcm_mutex being held
76 */
77void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
78{
79 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
80 struct snd_soc_dai *codec_dai = rtd->codec_dai;
81
82 lockdep_assert_held(&rtd->pcm_mutex);
83
84 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
85 cpu_dai->playback_active--;
86 codec_dai->playback_active--;
87 } else {
88 cpu_dai->capture_active--;
89 codec_dai->capture_active--;
90 }
91
92 cpu_dai->active--;
93 codec_dai->active--;
94 rtd->codec->active--;
95}
96
97/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +010098 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
99 * @rtd: The ASoC PCM runtime that should be checked.
100 *
101 * This function checks whether the power down delay should be ignored for a
102 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
103 * been configured to ignore the delay, or if none of the components benefits
104 * from having the delay.
105 */
106bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
107{
108 bool ignore = true;
109
110 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
111 return true;
112
113 if (rtd->cpu_dai->codec)
114 ignore &= rtd->cpu_dai->codec->ignore_pmdown_time;
115
116 ignore &= rtd->codec_dai->codec->ignore_pmdown_time;
117
118 return ignore;
119}
120
121/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200122 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
123 * @substream: the pcm substream
124 * @hw: the hardware parameters
125 *
126 * Sets the substream runtime hardware parameters.
127 */
128int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
129 const struct snd_pcm_hardware *hw)
130{
131 struct snd_pcm_runtime *runtime = substream->runtime;
132 runtime->hw.info = hw->info;
133 runtime->hw.formats = hw->formats;
134 runtime->hw.period_bytes_min = hw->period_bytes_min;
135 runtime->hw.period_bytes_max = hw->period_bytes_max;
136 runtime->hw.periods_min = hw->periods_min;
137 runtime->hw.periods_max = hw->periods_max;
138 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
139 runtime->hw.fifo_size = hw->fifo_size;
140 return 0;
141}
142EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
143
Liam Girdwood01d75842012-04-25 12:12:49 +0100144/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000145int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100146 int event)
147{
148 struct snd_soc_dpcm *dpcm;
149
150 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
151
152 struct snd_soc_pcm_runtime *be = dpcm->be;
153
Liam Girdwood103d84a2012-11-19 14:39:15 +0000154 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100155 be->dai_link->name, event, dir);
156
157 snd_soc_dapm_stream_event(be, dir, event);
158 }
159
160 snd_soc_dapm_stream_event(fe, dir, event);
161
162 return 0;
163}
164
Dong Aisheng17841022011-08-29 17:15:14 +0800165static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
166 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100167{
168 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100169 int ret;
170
Nicolin Chen3635bf02013-11-13 18:56:24 +0800171 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
172 rtd->dai_link->symmetric_rates)) {
173 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
174 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100175
Nicolin Chen3635bf02013-11-13 18:56:24 +0800176 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
177 SNDRV_PCM_HW_PARAM_RATE,
178 soc_dai->rate, soc_dai->rate);
179 if (ret < 0) {
180 dev_err(soc_dai->dev,
181 "ASoC: Unable to apply rate constraint: %d\n",
182 ret);
183 return ret;
184 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100185 }
186
Nicolin Chen3635bf02013-11-13 18:56:24 +0800187 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
188 rtd->dai_link->symmetric_channels)) {
189 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
190 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100191
Nicolin Chen3635bf02013-11-13 18:56:24 +0800192 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
193 SNDRV_PCM_HW_PARAM_CHANNELS,
194 soc_dai->channels,
195 soc_dai->channels);
196 if (ret < 0) {
197 dev_err(soc_dai->dev,
198 "ASoC: Unable to apply channel symmetry constraint: %d\n",
199 ret);
200 return ret;
201 }
202 }
203
204 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
205 rtd->dai_link->symmetric_samplebits)) {
206 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
207 soc_dai->sample_bits);
208
209 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
210 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
211 soc_dai->sample_bits,
212 soc_dai->sample_bits);
213 if (ret < 0) {
214 dev_err(soc_dai->dev,
215 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
216 ret);
217 return ret;
218 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100219 }
220
221 return 0;
222}
223
Nicolin Chen3635bf02013-11-13 18:56:24 +0800224static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
225 struct snd_pcm_hw_params *params)
226{
227 struct snd_soc_pcm_runtime *rtd = substream->private_data;
228 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
229 struct snd_soc_dai *codec_dai = rtd->codec_dai;
230 unsigned int rate, channels, sample_bits, symmetry;
231
232 rate = params_rate(params);
233 channels = params_channels(params);
234 sample_bits = snd_pcm_format_physical_width(params_format(params));
235
236 /* reject unmatched parameters when applying symmetry */
237 symmetry = cpu_dai->driver->symmetric_rates ||
238 codec_dai->driver->symmetric_rates ||
239 rtd->dai_link->symmetric_rates;
240 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
241 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
242 cpu_dai->rate, rate);
243 return -EINVAL;
244 }
245
246 symmetry = cpu_dai->driver->symmetric_channels ||
247 codec_dai->driver->symmetric_channels ||
248 rtd->dai_link->symmetric_channels;
249 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
250 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
251 cpu_dai->channels, channels);
252 return -EINVAL;
253 }
254
255 symmetry = cpu_dai->driver->symmetric_samplebits ||
256 codec_dai->driver->symmetric_samplebits ||
257 rtd->dai_link->symmetric_samplebits;
258 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
259 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
260 cpu_dai->sample_bits, sample_bits);
261 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100262 }
263
264 return 0;
265}
266
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100267static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
268{
269 struct snd_soc_pcm_runtime *rtd = substream->private_data;
270 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
271 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
272 struct snd_soc_dai_link *link = rtd->dai_link;
273
274 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
275 link->symmetric_rates || cpu_driver->symmetric_channels ||
276 codec_driver->symmetric_channels || link->symmetric_channels ||
277 cpu_driver->symmetric_samplebits ||
278 codec_driver->symmetric_samplebits ||
279 link->symmetric_samplebits;
280}
281
Liam Girdwoodddee6272011-06-09 14:45:53 +0100282/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000283 * List of sample sizes that might go over the bus for parameter
284 * application. There ought to be a wildcard sample size for things
285 * like the DAC/ADC resolution to use but there isn't right now.
286 */
287static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200288 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000289};
290
291static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
292 struct snd_soc_dai *dai)
293{
294 int ret, i, bits;
295
296 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
297 bits = dai->driver->playback.sig_bits;
298 else
299 bits = dai->driver->capture.sig_bits;
300
301 if (!bits)
302 return;
303
304 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000305 if (bits >= sample_sizes[i])
306 continue;
307
308 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
309 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000310 if (ret != 0)
311 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000312 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000313 bits, sample_sizes[i], ret);
314 }
315}
316
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100317static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200318 struct snd_soc_pcm_stream *codec_stream,
319 struct snd_soc_pcm_stream *cpu_stream)
320{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100321 struct snd_pcm_hardware *hw = &runtime->hw;
322
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200323 hw->channels_min = max(codec_stream->channels_min,
324 cpu_stream->channels_min);
325 hw->channels_max = min(codec_stream->channels_max,
326 cpu_stream->channels_max);
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100327 if (hw->formats)
328 hw->formats &= codec_stream->formats & cpu_stream->formats;
329 else
330 hw->formats = codec_stream->formats & cpu_stream->formats;
Lars-Peter Clausen55dcdb52014-01-11 10:24:44 +0100331 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates,
332 cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100333
Lars-Peter Clausen817873f2014-01-11 10:24:40 +0100334 hw->rate_min = 0;
335 hw->rate_max = UINT_MAX;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100336
337 snd_pcm_limit_hw_rates(runtime);
338
339 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
340 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
341 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
342 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200343}
344
Mark Brown58ba9b22012-01-16 18:38:51 +0000345/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100346 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
347 * then initialized and any private data can be allocated. This also calls
348 * startup for the cpu DAI, platform, machine and codec DAI.
349 */
350static int soc_pcm_open(struct snd_pcm_substream *substream)
351{
352 struct snd_soc_pcm_runtime *rtd = substream->private_data;
353 struct snd_pcm_runtime *runtime = substream->runtime;
354 struct snd_soc_platform *platform = rtd->platform;
355 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
356 struct snd_soc_dai *codec_dai = rtd->codec_dai;
357 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
358 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
359 int ret = 0;
360
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800361 pinctrl_pm_select_default_state(cpu_dai->dev);
362 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000363 pm_runtime_get_sync(cpu_dai->dev);
364 pm_runtime_get_sync(codec_dai->dev);
365 pm_runtime_get_sync(platform->dev);
366
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100367 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100368
369 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700370 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100371 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
372 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000373 dev_err(cpu_dai->dev, "ASoC: can't open interface"
374 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100375 goto out;
376 }
377 }
378
379 if (platform->driver->ops && platform->driver->ops->open) {
380 ret = platform->driver->ops->open(substream);
381 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000382 dev_err(platform->dev, "ASoC: can't open platform"
383 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100384 goto platform_err;
385 }
386 }
387
Mark Brownc5914b02013-10-30 17:47:39 -0700388 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100389 ret = codec_dai->driver->ops->startup(substream, codec_dai);
390 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000391 dev_err(codec_dai->dev, "ASoC: can't open codec"
392 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100393 goto codec_dai_err;
394 }
395 }
396
397 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
398 ret = rtd->dai_link->ops->startup(substream);
399 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000400 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000401 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100402 goto machine_err;
403 }
404 }
405
Liam Girdwood01d75842012-04-25 12:12:49 +0100406 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
407 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
408 goto dynamic;
409
Liam Girdwoodddee6272011-06-09 14:45:53 +0100410 /* Check that the codec and cpu DAIs are compatible */
411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100412 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200413 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100414 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100415 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200416 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100417 }
418
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100419 if (soc_pcm_has_symmetry(substream))
420 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
421
Liam Girdwoodddee6272011-06-09 14:45:53 +0100422 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100423 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000424 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100425 codec_dai->name, cpu_dai->name);
426 goto config_err;
427 }
428 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000429 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100430 codec_dai->name, cpu_dai->name);
431 goto config_err;
432 }
433 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
434 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000435 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100436 codec_dai->name, cpu_dai->name);
437 goto config_err;
438 }
439
Mark Brown58ba9b22012-01-16 18:38:51 +0000440 soc_pcm_apply_msb(substream, codec_dai);
441 soc_pcm_apply_msb(substream, cpu_dai);
442
Liam Girdwoodddee6272011-06-09 14:45:53 +0100443 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800444 if (cpu_dai->active) {
445 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
446 if (ret != 0)
447 goto config_err;
448 }
449
450 if (codec_dai->active) {
451 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100452 if (ret != 0)
453 goto config_err;
454 }
455
Liam Girdwood103d84a2012-11-19 14:39:15 +0000456 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100457 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000458 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
459 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100460 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000461 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100462 runtime->hw.rate_max);
463
Liam Girdwood01d75842012-04-25 12:12:49 +0100464dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100465
466 snd_soc_runtime_activate(rtd, substream->stream);
467
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100468 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100469 return 0;
470
471config_err:
472 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
473 rtd->dai_link->ops->shutdown(substream);
474
475machine_err:
476 if (codec_dai->driver->ops->shutdown)
477 codec_dai->driver->ops->shutdown(substream, codec_dai);
478
479codec_dai_err:
480 if (platform->driver->ops && platform->driver->ops->close)
481 platform->driver->ops->close(substream);
482
483platform_err:
484 if (cpu_dai->driver->ops->shutdown)
485 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
486out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100487 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000488
489 pm_runtime_put(platform->dev);
490 pm_runtime_put(codec_dai->dev);
491 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800492 if (!codec_dai->active)
493 pinctrl_pm_select_sleep_state(codec_dai->dev);
494 if (!cpu_dai->active)
495 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000496
Liam Girdwoodddee6272011-06-09 14:45:53 +0100497 return ret;
498}
499
500/*
501 * Power down the audio subsystem pmdown_time msecs after close is called.
502 * This is to ensure there are no pops or clicks in between any music tracks
503 * due to DAPM power cycling.
504 */
505static void close_delayed_work(struct work_struct *work)
506{
507 struct snd_soc_pcm_runtime *rtd =
508 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
509 struct snd_soc_dai *codec_dai = rtd->codec_dai;
510
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100511 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100512
Liam Girdwood103d84a2012-11-19 14:39:15 +0000513 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100514 codec_dai->driver->playback.stream_name,
515 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600516 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100517
518 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600519 if (rtd->pop_wait == 1) {
520 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800521 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000522 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100523 }
524
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100525 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100526}
527
528/*
529 * Called by ALSA when a PCM substream is closed. Private data can be
530 * freed here. The cpu DAI, codec DAI, machine and platform are also
531 * shutdown.
532 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100533static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100534{
535 struct snd_soc_pcm_runtime *rtd = substream->private_data;
536 struct snd_soc_platform *platform = rtd->platform;
537 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
538 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100539
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100540 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100541
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100542 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100543
Dong Aisheng17841022011-08-29 17:15:14 +0800544 /* clear the corresponding DAIs rate when inactive */
545 if (!cpu_dai->active)
546 cpu_dai->rate = 0;
547
548 if (!codec_dai->active)
549 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200550
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 if (cpu_dai->driver->ops->shutdown)
552 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
553
554 if (codec_dai->driver->ops->shutdown)
555 codec_dai->driver->ops->shutdown(substream, codec_dai);
556
557 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
558 rtd->dai_link->ops->shutdown(substream);
559
560 if (platform->driver->ops && platform->driver->ops->close)
561 platform->driver->ops->close(substream);
562 cpu_dai->runtime = NULL;
563
564 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100565 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300566 /* powered down playback stream now */
567 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800568 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800569 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300570 } else {
571 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600572 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100573 queue_delayed_work(system_power_efficient_wq,
574 &rtd->delayed_work,
575 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300576 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577 } else {
578 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800579 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000580 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100581 }
582
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100583 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000584
585 pm_runtime_put(platform->dev);
586 pm_runtime_put(codec_dai->dev);
587 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800588 if (!codec_dai->active)
589 pinctrl_pm_select_sleep_state(codec_dai->dev);
590 if (!cpu_dai->active)
591 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000592
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593 return 0;
594}
595
596/*
597 * Called by ALSA when the PCM substream is prepared, can set format, sample
598 * rate, etc. This function is non atomic and can be called multiple times,
599 * it can refer to the runtime info.
600 */
601static int soc_pcm_prepare(struct snd_pcm_substream *substream)
602{
603 struct snd_soc_pcm_runtime *rtd = substream->private_data;
604 struct snd_soc_platform *platform = rtd->platform;
605 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
606 struct snd_soc_dai *codec_dai = rtd->codec_dai;
607 int ret = 0;
608
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100609 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100610
611 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
612 ret = rtd->dai_link->ops->prepare(substream);
613 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000614 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
615 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616 goto out;
617 }
618 }
619
620 if (platform->driver->ops && platform->driver->ops->prepare) {
621 ret = platform->driver->ops->prepare(substream);
622 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000623 dev_err(platform->dev, "ASoC: platform prepare error:"
624 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100625 goto out;
626 }
627 }
628
Mark Brownc5914b02013-10-30 17:47:39 -0700629 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100630 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
631 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000632 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000633 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100634 goto out;
635 }
636 }
637
Mark Brownc5914b02013-10-30 17:47:39 -0700638 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100639 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
640 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000641 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000642 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100643 goto out;
644 }
645 }
646
647 /* cancel any delayed stream shutdown that is pending */
648 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600649 rtd->pop_wait) {
650 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651 cancel_delayed_work(&rtd->delayed_work);
652 }
653
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000654 snd_soc_dapm_stream_event(rtd, substream->stream,
655 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100656
Mark Brownda183962013-02-06 15:44:07 +0000657 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100658
659out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100660 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100661 return ret;
662}
663
664/*
665 * Called by ALSA when the hardware params are set by application. This
666 * function can also be called multiple times and can allocate buffers
667 * (using snd_pcm_lib_* ). It's non-atomic.
668 */
669static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
670 struct snd_pcm_hw_params *params)
671{
672 struct snd_soc_pcm_runtime *rtd = substream->private_data;
673 struct snd_soc_platform *platform = rtd->platform;
674 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
675 struct snd_soc_dai *codec_dai = rtd->codec_dai;
676 int ret = 0;
677
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100678 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100679
Nicolin Chen3635bf02013-11-13 18:56:24 +0800680 ret = soc_pcm_params_symmetry(substream, params);
681 if (ret)
682 goto out;
683
Liam Girdwoodddee6272011-06-09 14:45:53 +0100684 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
685 ret = rtd->dai_link->ops->hw_params(substream, params);
686 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000687 dev_err(rtd->card->dev, "ASoC: machine hw_params"
688 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689 goto out;
690 }
691 }
692
Mark Brownc5914b02013-10-30 17:47:39 -0700693 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100694 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
695 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000696 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
697 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100698 goto codec_err;
699 }
700 }
701
Mark Brownc5914b02013-10-30 17:47:39 -0700702 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
704 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000705 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000706 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100707 goto interface_err;
708 }
709 }
710
711 if (platform->driver->ops && platform->driver->ops->hw_params) {
712 ret = platform->driver->ops->hw_params(substream, params);
713 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000714 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000715 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100716 goto platform_err;
717 }
718 }
719
Nicolin Chen3635bf02013-11-13 18:56:24 +0800720 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800721 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800722 cpu_dai->channels = params_channels(params);
723 cpu_dai->sample_bits =
724 snd_pcm_format_physical_width(params_format(params));
725
Dong Aisheng17841022011-08-29 17:15:14 +0800726 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800727 codec_dai->channels = params_channels(params);
728 codec_dai->sample_bits =
729 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100730
731out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100732 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100733 return ret;
734
735platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700736 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
738
739interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700740 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100741 codec_dai->driver->ops->hw_free(substream, codec_dai);
742
743codec_err:
744 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
745 rtd->dai_link->ops->hw_free(substream);
746
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100747 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100748 return ret;
749}
750
751/*
752 * Frees resources allocated by hw_params, can be called multiple times
753 */
754static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
755{
756 struct snd_soc_pcm_runtime *rtd = substream->private_data;
757 struct snd_soc_platform *platform = rtd->platform;
758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
759 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800760 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100761
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100762 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100763
Nicolin Chend3383422013-11-20 18:37:09 +0800764 /* clear the corresponding DAIs parameters when going to be inactive */
765 if (cpu_dai->active == 1) {
766 cpu_dai->rate = 0;
767 cpu_dai->channels = 0;
768 cpu_dai->sample_bits = 0;
769 }
770
771 if (codec_dai->active == 1) {
772 codec_dai->rate = 0;
773 codec_dai->channels = 0;
774 codec_dai->sample_bits = 0;
775 }
776
Liam Girdwoodddee6272011-06-09 14:45:53 +0100777 /* apply codec digital mute */
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800778 if ((playback && codec_dai->playback_active == 1) ||
779 (!playback && codec_dai->capture_active == 1))
Mark Brownda183962013-02-06 15:44:07 +0000780 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781
782 /* free any machine hw params */
783 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
784 rtd->dai_link->ops->hw_free(substream);
785
786 /* free any DMA resources */
787 if (platform->driver->ops && platform->driver->ops->hw_free)
788 platform->driver->ops->hw_free(substream);
789
790 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700791 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100792 codec_dai->driver->ops->hw_free(substream, codec_dai);
793
Mark Brownc5914b02013-10-30 17:47:39 -0700794 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100795 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
796
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100797 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100798 return 0;
799}
800
801static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
802{
803 struct snd_soc_pcm_runtime *rtd = substream->private_data;
804 struct snd_soc_platform *platform = rtd->platform;
805 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
806 struct snd_soc_dai *codec_dai = rtd->codec_dai;
807 int ret;
808
Mark Brownc5914b02013-10-30 17:47:39 -0700809 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100810 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
811 if (ret < 0)
812 return ret;
813 }
814
815 if (platform->driver->ops && platform->driver->ops->trigger) {
816 ret = platform->driver->ops->trigger(substream, cmd);
817 if (ret < 0)
818 return ret;
819 }
820
Mark Brownc5914b02013-10-30 17:47:39 -0700821 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
823 if (ret < 0)
824 return ret;
825 }
826 return 0;
827}
828
Mark Brown45c0a182012-05-09 21:46:27 +0100829static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
830 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100831{
832 struct snd_soc_pcm_runtime *rtd = substream->private_data;
833 struct snd_soc_platform *platform = rtd->platform;
834 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
835 struct snd_soc_dai *codec_dai = rtd->codec_dai;
836 int ret;
837
Mark Brownc5914b02013-10-30 17:47:39 -0700838 if (codec_dai->driver->ops &&
839 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100840 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
841 if (ret < 0)
842 return ret;
843 }
844
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +0100845 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100846 ret = platform->driver->bespoke_trigger(substream, cmd);
847 if (ret < 0)
848 return ret;
849 }
850
Mark Brownc5914b02013-10-30 17:47:39 -0700851 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100852 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
853 if (ret < 0)
854 return ret;
855 }
856 return 0;
857}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100858/*
859 * soc level wrapper for pointer callback
860 * If cpu_dai, codec_dai, platform driver has the delay callback, than
861 * the runtime->delay will be updated accordingly.
862 */
863static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
864{
865 struct snd_soc_pcm_runtime *rtd = substream->private_data;
866 struct snd_soc_platform *platform = rtd->platform;
867 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
868 struct snd_soc_dai *codec_dai = rtd->codec_dai;
869 struct snd_pcm_runtime *runtime = substream->runtime;
870 snd_pcm_uframes_t offset = 0;
871 snd_pcm_sframes_t delay = 0;
872
873 if (platform->driver->ops && platform->driver->ops->pointer)
874 offset = platform->driver->ops->pointer(substream);
875
Mark Brownc5914b02013-10-30 17:47:39 -0700876 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100877 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
878
Mark Brownc5914b02013-10-30 17:47:39 -0700879 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100880 delay += codec_dai->driver->ops->delay(substream, codec_dai);
881
882 if (platform->driver->delay)
883 delay += platform->driver->delay(substream, codec_dai);
884
885 runtime->delay = delay;
886
887 return offset;
888}
889
Liam Girdwood01d75842012-04-25 12:12:49 +0100890/* connect a FE and BE */
891static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
892 struct snd_soc_pcm_runtime *be, int stream)
893{
894 struct snd_soc_dpcm *dpcm;
895
896 /* only add new dpcms */
897 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
898 if (dpcm->be == be && dpcm->fe == fe)
899 return 0;
900 }
901
902 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
903 if (!dpcm)
904 return -ENOMEM;
905
906 dpcm->be = be;
907 dpcm->fe = fe;
908 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
909 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
910 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
911 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
912
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300913 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100914 stream ? "capture" : "playback", fe->dai_link->name,
915 stream ? "<-" : "->", be->dai_link->name);
916
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100917#ifdef CONFIG_DEBUG_FS
918 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
919 fe->debugfs_dpcm_root, &dpcm->state);
920#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100921 return 1;
922}
923
924/* reparent a BE onto another FE */
925static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
926 struct snd_soc_pcm_runtime *be, int stream)
927{
928 struct snd_soc_dpcm *dpcm;
929 struct snd_pcm_substream *fe_substream, *be_substream;
930
931 /* reparent if BE is connected to other FEs */
932 if (!be->dpcm[stream].users)
933 return;
934
935 be_substream = snd_soc_dpcm_get_substream(be, stream);
936
937 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
938 if (dpcm->fe == fe)
939 continue;
940
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300941 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100942 stream ? "capture" : "playback",
943 dpcm->fe->dai_link->name,
944 stream ? "<-" : "->", dpcm->be->dai_link->name);
945
946 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
947 be_substream->runtime = fe_substream->runtime;
948 break;
949 }
950}
951
952/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +0000953void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +0100954{
955 struct snd_soc_dpcm *dpcm, *d;
956
957 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000958 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100959 stream ? "capture" : "playback",
960 dpcm->be->dai_link->name);
961
962 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
963 continue;
964
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300965 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100966 stream ? "capture" : "playback", fe->dai_link->name,
967 stream ? "<-" : "->", dpcm->be->dai_link->name);
968
969 /* BEs still alive need new FE */
970 dpcm_be_reparent(fe, dpcm->be, stream);
971
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100972#ifdef CONFIG_DEBUG_FS
973 debugfs_remove(dpcm->debugfs_state);
974#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100975 list_del(&dpcm->list_be);
976 list_del(&dpcm->list_fe);
977 kfree(dpcm);
978 }
979}
980
981/* get BE for DAI widget and stream */
982static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
983 struct snd_soc_dapm_widget *widget, int stream)
984{
985 struct snd_soc_pcm_runtime *be;
986 int i;
987
988 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
989 for (i = 0; i < card->num_links; i++) {
990 be = &card->rtd[i];
991
Liam Girdwood35ea0652012-06-05 19:26:59 +0100992 if (!be->dai_link->no_pcm)
993 continue;
994
Liam Girdwood01d75842012-04-25 12:12:49 +0100995 if (be->cpu_dai->playback_widget == widget ||
996 be->codec_dai->playback_widget == widget)
997 return be;
998 }
999 } else {
1000
1001 for (i = 0; i < card->num_links; i++) {
1002 be = &card->rtd[i];
1003
Liam Girdwood35ea0652012-06-05 19:26:59 +01001004 if (!be->dai_link->no_pcm)
1005 continue;
1006
Liam Girdwood01d75842012-04-25 12:12:49 +01001007 if (be->cpu_dai->capture_widget == widget ||
1008 be->codec_dai->capture_widget == widget)
1009 return be;
1010 }
1011 }
1012
Liam Girdwood103d84a2012-11-19 14:39:15 +00001013 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001014 stream ? "capture" : "playback", widget->name);
1015 return NULL;
1016}
1017
1018static inline struct snd_soc_dapm_widget *
1019 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
1020{
1021 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1022 return rtd->cpu_dai->playback_widget;
1023 else
1024 return rtd->cpu_dai->capture_widget;
1025}
1026
1027static inline struct snd_soc_dapm_widget *
1028 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
1029{
1030 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1031 return rtd->codec_dai->playback_widget;
1032 else
1033 return rtd->codec_dai->capture_widget;
1034}
1035
1036static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1037 struct snd_soc_dapm_widget *widget)
1038{
1039 int i;
1040
1041 for (i = 0; i < list->num_widgets; i++) {
1042 if (widget == list->widgets[i])
1043 return 1;
1044 }
1045
1046 return 0;
1047}
1048
Liam Girdwood23607022014-01-17 17:03:55 +00001049int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001050 int stream, struct snd_soc_dapm_widget_list **list_)
1051{
1052 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1053 struct snd_soc_dapm_widget_list *list;
1054 int paths;
1055
1056 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1057 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1058 if (list == NULL)
1059 return -ENOMEM;
1060
1061 /* get number of valid DAI paths and their widgets */
1062 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1063
Liam Girdwood103d84a2012-11-19 14:39:15 +00001064 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001065 stream ? "capture" : "playback");
1066
1067 *list_ = list;
1068 return paths;
1069}
1070
Liam Girdwood01d75842012-04-25 12:12:49 +01001071static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1072 struct snd_soc_dapm_widget_list **list_)
1073{
1074 struct snd_soc_dpcm *dpcm;
1075 struct snd_soc_dapm_widget_list *list = *list_;
1076 struct snd_soc_dapm_widget *widget;
1077 int prune = 0;
1078
1079 /* Destroy any old FE <--> BE connections */
1080 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1081
1082 /* is there a valid CPU DAI widget for this BE */
1083 widget = rtd_get_cpu_widget(dpcm->be, stream);
1084
1085 /* prune the BE if it's no longer in our active list */
1086 if (widget && widget_in_list(list, widget))
1087 continue;
1088
1089 /* is there a valid CODEC DAI widget for this BE */
1090 widget = rtd_get_codec_widget(dpcm->be, stream);
1091
1092 /* prune the BE if it's no longer in our active list */
1093 if (widget && widget_in_list(list, widget))
1094 continue;
1095
Liam Girdwood103d84a2012-11-19 14:39:15 +00001096 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001097 stream ? "capture" : "playback",
1098 dpcm->be->dai_link->name, fe->dai_link->name);
1099 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1100 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1101 prune++;
1102 }
1103
Liam Girdwood103d84a2012-11-19 14:39:15 +00001104 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001105 return prune;
1106}
1107
1108static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1109 struct snd_soc_dapm_widget_list **list_)
1110{
1111 struct snd_soc_card *card = fe->card;
1112 struct snd_soc_dapm_widget_list *list = *list_;
1113 struct snd_soc_pcm_runtime *be;
1114 int i, new = 0, err;
1115
1116 /* Create any new FE <--> BE connections */
1117 for (i = 0; i < list->num_widgets; i++) {
1118
Mark Brown46162742013-06-05 19:36:11 +01001119 switch (list->widgets[i]->id) {
1120 case snd_soc_dapm_dai_in:
1121 case snd_soc_dapm_dai_out:
1122 break;
1123 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001124 continue;
Mark Brown46162742013-06-05 19:36:11 +01001125 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001126
1127 /* is there a valid BE rtd for this widget */
1128 be = dpcm_get_be(card, list->widgets[i], stream);
1129 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001130 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001131 list->widgets[i]->name);
1132 continue;
1133 }
1134
1135 /* make sure BE is a real BE */
1136 if (!be->dai_link->no_pcm)
1137 continue;
1138
1139 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001140 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001141 continue;
1142
1143 /* newly connected FE and BE */
1144 err = dpcm_be_connect(fe, be, stream);
1145 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001146 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001147 list->widgets[i]->name);
1148 break;
1149 } else if (err == 0) /* already connected */
1150 continue;
1151
1152 /* new */
1153 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1154 new++;
1155 }
1156
Liam Girdwood103d84a2012-11-19 14:39:15 +00001157 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001158 return new;
1159}
1160
1161/*
1162 * Find the corresponding BE DAIs that source or sink audio to this
1163 * FE substream.
1164 */
Liam Girdwood23607022014-01-17 17:03:55 +00001165int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001166 int stream, struct snd_soc_dapm_widget_list **list, int new)
1167{
1168 if (new)
1169 return dpcm_add_paths(fe, stream, list);
1170 else
1171 return dpcm_prune_paths(fe, stream, list);
1172}
1173
Liam Girdwood23607022014-01-17 17:03:55 +00001174void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001175{
1176 struct snd_soc_dpcm *dpcm;
1177
1178 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1179 dpcm->be->dpcm[stream].runtime_update =
1180 SND_SOC_DPCM_UPDATE_NO;
1181}
1182
1183static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1184 int stream)
1185{
1186 struct snd_soc_dpcm *dpcm;
1187
1188 /* disable any enabled and non active backends */
1189 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1190
1191 struct snd_soc_pcm_runtime *be = dpcm->be;
1192 struct snd_pcm_substream *be_substream =
1193 snd_soc_dpcm_get_substream(be, stream);
1194
1195 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001196 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001197 stream ? "capture" : "playback",
1198 be->dpcm[stream].state);
1199
1200 if (--be->dpcm[stream].users != 0)
1201 continue;
1202
1203 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1204 continue;
1205
1206 soc_pcm_close(be_substream);
1207 be_substream->runtime = NULL;
1208 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1209 }
1210}
1211
Liam Girdwood23607022014-01-17 17:03:55 +00001212int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001213{
1214 struct snd_soc_dpcm *dpcm;
1215 int err, count = 0;
1216
1217 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1218 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1219
1220 struct snd_soc_pcm_runtime *be = dpcm->be;
1221 struct snd_pcm_substream *be_substream =
1222 snd_soc_dpcm_get_substream(be, stream);
1223
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001224 if (!be_substream) {
1225 dev_err(be->dev, "ASoC: no backend %s stream\n",
1226 stream ? "capture" : "playback");
1227 continue;
1228 }
1229
Liam Girdwood01d75842012-04-25 12:12:49 +01001230 /* is this op for this BE ? */
1231 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1232 continue;
1233
1234 /* first time the dpcm is open ? */
1235 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001236 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001237 stream ? "capture" : "playback",
1238 be->dpcm[stream].state);
1239
1240 if (be->dpcm[stream].users++ != 0)
1241 continue;
1242
1243 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1244 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1245 continue;
1246
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001247 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1248 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001249
1250 be_substream->runtime = be->dpcm[stream].runtime;
1251 err = soc_pcm_open(be_substream);
1252 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001253 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001254 be->dpcm[stream].users--;
1255 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001256 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001257 stream ? "capture" : "playback",
1258 be->dpcm[stream].state);
1259
1260 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1261 goto unwind;
1262 }
1263
1264 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1265 count++;
1266 }
1267
1268 return count;
1269
1270unwind:
1271 /* disable any enabled and non active backends */
1272 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1273 struct snd_soc_pcm_runtime *be = dpcm->be;
1274 struct snd_pcm_substream *be_substream =
1275 snd_soc_dpcm_get_substream(be, stream);
1276
1277 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1278 continue;
1279
1280 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001281 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001282 stream ? "capture" : "playback",
1283 be->dpcm[stream].state);
1284
1285 if (--be->dpcm[stream].users != 0)
1286 continue;
1287
1288 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1289 continue;
1290
1291 soc_pcm_close(be_substream);
1292 be_substream->runtime = NULL;
1293 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1294 }
1295
1296 return err;
1297}
1298
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001299static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1300 struct snd_soc_pcm_stream *stream)
1301{
1302 runtime->hw.rate_min = stream->rate_min;
1303 runtime->hw.rate_max = stream->rate_max;
1304 runtime->hw.channels_min = stream->channels_min;
1305 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001306 if (runtime->hw.formats)
1307 runtime->hw.formats &= stream->formats;
1308 else
1309 runtime->hw.formats = stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001310 runtime->hw.rates = stream->rates;
1311}
1312
Mark Brown45c0a182012-05-09 21:46:27 +01001313static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001314{
1315 struct snd_pcm_runtime *runtime = substream->runtime;
1316 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1317 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1318 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1319
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001320 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1321 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1322 else
1323 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
Liam Girdwood01d75842012-04-25 12:12:49 +01001324}
1325
1326static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1327{
1328 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1329 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1330 int stream = fe_substream->stream, ret = 0;
1331
1332 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1333
1334 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1335 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001336 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001337 goto be_err;
1338 }
1339
Liam Girdwood103d84a2012-11-19 14:39:15 +00001340 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001341
1342 /* start the DAI frontend */
1343 ret = soc_pcm_open(fe_substream);
1344 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001345 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001346 goto unwind;
1347 }
1348
1349 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1350
1351 dpcm_set_fe_runtime(fe_substream);
1352 snd_pcm_limit_hw_rates(runtime);
1353
1354 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1355 return 0;
1356
1357unwind:
1358 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1359be_err:
1360 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1361 return ret;
1362}
1363
Liam Girdwood23607022014-01-17 17:03:55 +00001364int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001365{
1366 struct snd_soc_dpcm *dpcm;
1367
1368 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1369 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1370
1371 struct snd_soc_pcm_runtime *be = dpcm->be;
1372 struct snd_pcm_substream *be_substream =
1373 snd_soc_dpcm_get_substream(be, stream);
1374
1375 /* is this op for this BE ? */
1376 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1377 continue;
1378
1379 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001380 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001381 stream ? "capture" : "playback",
1382 be->dpcm[stream].state);
1383
1384 if (--be->dpcm[stream].users != 0)
1385 continue;
1386
1387 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1388 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1389 continue;
1390
Liam Girdwood103d84a2012-11-19 14:39:15 +00001391 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001392 dpcm->fe->dai_link->name);
1393
1394 soc_pcm_close(be_substream);
1395 be_substream->runtime = NULL;
1396
1397 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1398 }
1399 return 0;
1400}
1401
1402static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1403{
1404 struct snd_soc_pcm_runtime *fe = substream->private_data;
1405 int stream = substream->stream;
1406
1407 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1408
1409 /* shutdown the BEs */
1410 dpcm_be_dai_shutdown(fe, substream->stream);
1411
Liam Girdwood103d84a2012-11-19 14:39:15 +00001412 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001413
1414 /* now shutdown the frontend */
1415 soc_pcm_close(substream);
1416
1417 /* run the stream event for each BE */
1418 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1419
1420 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1421 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1422 return 0;
1423}
1424
Liam Girdwood23607022014-01-17 17:03:55 +00001425int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001426{
1427 struct snd_soc_dpcm *dpcm;
1428
1429 /* only hw_params backends that are either sinks or sources
1430 * to this frontend DAI */
1431 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1432
1433 struct snd_soc_pcm_runtime *be = dpcm->be;
1434 struct snd_pcm_substream *be_substream =
1435 snd_soc_dpcm_get_substream(be, stream);
1436
1437 /* is this op for this BE ? */
1438 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1439 continue;
1440
1441 /* only free hw when no longer used - check all FEs */
1442 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1443 continue;
1444
1445 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1447 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001448 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001449 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1450 continue;
1451
Liam Girdwood103d84a2012-11-19 14:39:15 +00001452 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001453 dpcm->fe->dai_link->name);
1454
1455 soc_pcm_hw_free(be_substream);
1456
1457 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1458 }
1459
1460 return 0;
1461}
1462
Mark Brown45c0a182012-05-09 21:46:27 +01001463static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001464{
1465 struct snd_soc_pcm_runtime *fe = substream->private_data;
1466 int err, stream = substream->stream;
1467
1468 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1469 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1470
Liam Girdwood103d84a2012-11-19 14:39:15 +00001471 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001472
1473 /* call hw_free on the frontend */
1474 err = soc_pcm_hw_free(substream);
1475 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001476 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001477 fe->dai_link->name);
1478
1479 /* only hw_params backends that are either sinks or sources
1480 * to this frontend DAI */
1481 err = dpcm_be_dai_hw_free(fe, stream);
1482
1483 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1484 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1485
1486 mutex_unlock(&fe->card->mutex);
1487 return 0;
1488}
1489
Liam Girdwood23607022014-01-17 17:03:55 +00001490int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001491{
1492 struct snd_soc_dpcm *dpcm;
1493 int ret;
1494
1495 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1496
1497 struct snd_soc_pcm_runtime *be = dpcm->be;
1498 struct snd_pcm_substream *be_substream =
1499 snd_soc_dpcm_get_substream(be, stream);
1500
1501 /* is this op for this BE ? */
1502 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1503 continue;
1504
1505 /* only allow hw_params() if no connected FEs are running */
1506 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1507 continue;
1508
1509 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1510 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1511 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1512 continue;
1513
Liam Girdwood103d84a2012-11-19 14:39:15 +00001514 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001515 dpcm->fe->dai_link->name);
1516
1517 /* copy params for each dpcm */
1518 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1519 sizeof(struct snd_pcm_hw_params));
1520
1521 /* perform any hw_params fixups */
1522 if (be->dai_link->be_hw_params_fixup) {
1523 ret = be->dai_link->be_hw_params_fixup(be,
1524 &dpcm->hw_params);
1525 if (ret < 0) {
1526 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001527 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001528 ret);
1529 goto unwind;
1530 }
1531 }
1532
1533 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1534 if (ret < 0) {
1535 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001536 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001537 goto unwind;
1538 }
1539
1540 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1541 }
1542 return 0;
1543
1544unwind:
1545 /* disable any enabled and non active backends */
1546 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1547 struct snd_soc_pcm_runtime *be = dpcm->be;
1548 struct snd_pcm_substream *be_substream =
1549 snd_soc_dpcm_get_substream(be, stream);
1550
1551 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1552 continue;
1553
1554 /* only allow hw_free() if no connected FEs are running */
1555 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1556 continue;
1557
1558 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1559 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1560 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1561 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1562 continue;
1563
1564 soc_pcm_hw_free(be_substream);
1565 }
1566
1567 return ret;
1568}
1569
Mark Brown45c0a182012-05-09 21:46:27 +01001570static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1571 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001572{
1573 struct snd_soc_pcm_runtime *fe = substream->private_data;
1574 int ret, stream = substream->stream;
1575
1576 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1577 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1578
1579 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1580 sizeof(struct snd_pcm_hw_params));
1581 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1582 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001583 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001584 goto out;
1585 }
1586
Liam Girdwood103d84a2012-11-19 14:39:15 +00001587 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001588 fe->dai_link->name, params_rate(params),
1589 params_channels(params), params_format(params));
1590
1591 /* call hw_params on the frontend */
1592 ret = soc_pcm_hw_params(substream, params);
1593 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001594 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001595 dpcm_be_dai_hw_free(fe, stream);
1596 } else
1597 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1598
1599out:
1600 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1601 mutex_unlock(&fe->card->mutex);
1602 return ret;
1603}
1604
1605static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1606 struct snd_pcm_substream *substream, int cmd)
1607{
1608 int ret;
1609
Liam Girdwood103d84a2012-11-19 14:39:15 +00001610 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001611 dpcm->fe->dai_link->name, cmd);
1612
1613 ret = soc_pcm_trigger(substream, cmd);
1614 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001615 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001616
1617 return ret;
1618}
1619
Liam Girdwood23607022014-01-17 17:03:55 +00001620int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001621 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001622{
1623 struct snd_soc_dpcm *dpcm;
1624 int ret = 0;
1625
1626 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1627
1628 struct snd_soc_pcm_runtime *be = dpcm->be;
1629 struct snd_pcm_substream *be_substream =
1630 snd_soc_dpcm_get_substream(be, stream);
1631
1632 /* is this op for this BE ? */
1633 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1634 continue;
1635
1636 switch (cmd) {
1637 case SNDRV_PCM_TRIGGER_START:
1638 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1639 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1640 continue;
1641
1642 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1643 if (ret)
1644 return ret;
1645
1646 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1647 break;
1648 case SNDRV_PCM_TRIGGER_RESUME:
1649 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1650 continue;
1651
1652 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1653 if (ret)
1654 return ret;
1655
1656 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1657 break;
1658 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1659 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1660 continue;
1661
1662 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1663 if (ret)
1664 return ret;
1665
1666 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1667 break;
1668 case SNDRV_PCM_TRIGGER_STOP:
1669 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1670 continue;
1671
1672 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1673 continue;
1674
1675 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1676 if (ret)
1677 return ret;
1678
1679 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1680 break;
1681 case SNDRV_PCM_TRIGGER_SUSPEND:
1682 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1683 continue;
1684
1685 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1686 continue;
1687
1688 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1689 if (ret)
1690 return ret;
1691
1692 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1693 break;
1694 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1695 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1696 continue;
1697
1698 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1699 continue;
1700
1701 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1702 if (ret)
1703 return ret;
1704
1705 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1706 break;
1707 }
1708 }
1709
1710 return ret;
1711}
1712EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1713
Mark Brown45c0a182012-05-09 21:46:27 +01001714static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001715{
1716 struct snd_soc_pcm_runtime *fe = substream->private_data;
1717 int stream = substream->stream, ret;
1718 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1719
1720 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1721
1722 switch (trigger) {
1723 case SND_SOC_DPCM_TRIGGER_PRE:
1724 /* call trigger on the frontend before the backend. */
1725
Liam Girdwood103d84a2012-11-19 14:39:15 +00001726 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001727 fe->dai_link->name, cmd);
1728
1729 ret = soc_pcm_trigger(substream, cmd);
1730 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001731 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001732 goto out;
1733 }
1734
1735 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1736 break;
1737 case SND_SOC_DPCM_TRIGGER_POST:
1738 /* call trigger on the frontend after the backend. */
1739
1740 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1741 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001742 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001743 goto out;
1744 }
1745
Liam Girdwood103d84a2012-11-19 14:39:15 +00001746 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001747 fe->dai_link->name, cmd);
1748
1749 ret = soc_pcm_trigger(substream, cmd);
1750 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001751 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1752 /* bespoke trigger() - handles both FE and BEs */
1753
Liam Girdwood103d84a2012-11-19 14:39:15 +00001754 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001755 fe->dai_link->name, cmd);
1756
1757 ret = soc_pcm_bespoke_trigger(substream, cmd);
1758 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001759 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001760 goto out;
1761 }
1762 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001763 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001764 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001765 fe->dai_link->name);
1766 ret = -EINVAL;
1767 goto out;
1768 }
1769
1770 switch (cmd) {
1771 case SNDRV_PCM_TRIGGER_START:
1772 case SNDRV_PCM_TRIGGER_RESUME:
1773 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1774 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1775 break;
1776 case SNDRV_PCM_TRIGGER_STOP:
1777 case SNDRV_PCM_TRIGGER_SUSPEND:
1778 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1779 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1780 break;
1781 }
1782
1783out:
1784 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1785 return ret;
1786}
1787
Liam Girdwood23607022014-01-17 17:03:55 +00001788int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001789{
1790 struct snd_soc_dpcm *dpcm;
1791 int ret = 0;
1792
1793 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1794
1795 struct snd_soc_pcm_runtime *be = dpcm->be;
1796 struct snd_pcm_substream *be_substream =
1797 snd_soc_dpcm_get_substream(be, stream);
1798
1799 /* is this op for this BE ? */
1800 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1801 continue;
1802
1803 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1804 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1805 continue;
1806
Liam Girdwood103d84a2012-11-19 14:39:15 +00001807 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001808 dpcm->fe->dai_link->name);
1809
1810 ret = soc_pcm_prepare(be_substream);
1811 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001812 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001813 ret);
1814 break;
1815 }
1816
1817 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1818 }
1819 return ret;
1820}
1821
Mark Brown45c0a182012-05-09 21:46:27 +01001822static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001823{
1824 struct snd_soc_pcm_runtime *fe = substream->private_data;
1825 int stream = substream->stream, ret = 0;
1826
1827 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1828
Liam Girdwood103d84a2012-11-19 14:39:15 +00001829 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001830
1831 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1832
1833 /* there is no point preparing this FE if there are no BEs */
1834 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 fe->dai_link->name);
1837 ret = -EINVAL;
1838 goto out;
1839 }
1840
1841 ret = dpcm_be_dai_prepare(fe, substream->stream);
1842 if (ret < 0)
1843 goto out;
1844
1845 /* call prepare on the frontend */
1846 ret = soc_pcm_prepare(substream);
1847 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001848 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001849 fe->dai_link->name);
1850 goto out;
1851 }
1852
1853 /* run the stream event for each BE */
1854 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1855 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1856
1857out:
1858 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1859 mutex_unlock(&fe->card->mutex);
1860
1861 return ret;
1862}
1863
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001864static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1865 unsigned int cmd, void *arg)
1866{
1867 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1868 struct snd_soc_platform *platform = rtd->platform;
1869
Mark Brownc5914b02013-10-30 17:47:39 -07001870 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001871 return platform->driver->ops->ioctl(substream, cmd, arg);
1872 return snd_pcm_lib_ioctl(substream, cmd, arg);
1873}
1874
Liam Girdwood618dae12012-04-25 12:12:51 +01001875static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1876{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001877 struct snd_pcm_substream *substream =
1878 snd_soc_dpcm_get_substream(fe, stream);
1879 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001880 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001881
Liam Girdwood103d84a2012-11-19 14:39:15 +00001882 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001883 stream ? "capture" : "playback", fe->dai_link->name);
1884
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001885 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1886 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001887 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001888 fe->dai_link->name);
1889
1890 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1891 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001892 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001893 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001894 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001895 fe->dai_link->name);
1896
1897 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1898 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001899 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001900 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001901
1902 err = dpcm_be_dai_hw_free(fe, stream);
1903 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001904 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001905
1906 err = dpcm_be_dai_shutdown(fe, stream);
1907 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001908 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001909
1910 /* run the stream event for each BE */
1911 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1912
1913 return 0;
1914}
1915
1916static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1917{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001918 struct snd_pcm_substream *substream =
1919 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001920 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001921 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001922 int ret;
1923
Liam Girdwood103d84a2012-11-19 14:39:15 +00001924 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001925 stream ? "capture" : "playback", fe->dai_link->name);
1926
1927 /* Only start the BE if the FE is ready */
1928 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1929 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1930 return -EINVAL;
1931
1932 /* startup must always be called for new BEs */
1933 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001934 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001935 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001936
1937 /* keep going if FE state is > open */
1938 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1939 return 0;
1940
1941 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001942 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001943 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001944
1945 /* keep going if FE state is > hw_params */
1946 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1947 return 0;
1948
1949
1950 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001951 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001952 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001953
1954 /* run the stream event for each BE */
1955 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1956
1957 /* keep going if FE state is > prepare */
1958 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1959 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1960 return 0;
1961
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001962 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1963 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001964 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001965 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001966
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001967 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1968 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001969 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001970 goto hw_free;
1971 }
1972 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001973 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001974 fe->dai_link->name);
1975
1976 ret = dpcm_be_dai_trigger(fe, stream,
1977 SNDRV_PCM_TRIGGER_START);
1978 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001979 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001980 goto hw_free;
1981 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001982 }
1983
1984 return 0;
1985
1986hw_free:
1987 dpcm_be_dai_hw_free(fe, stream);
1988close:
1989 dpcm_be_dai_shutdown(fe, stream);
1990disconnect:
1991 /* disconnect any non started BEs */
1992 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1993 struct snd_soc_pcm_runtime *be = dpcm->be;
1994 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1995 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1996 }
1997
1998 return ret;
1999}
2000
2001static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2002{
2003 int ret;
2004
2005 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2006 ret = dpcm_run_update_startup(fe, stream);
2007 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002008 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002009 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2010
2011 return ret;
2012}
2013
2014static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2015{
2016 int ret;
2017
2018 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2019 ret = dpcm_run_update_shutdown(fe, stream);
2020 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002021 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01002022 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2023
2024 return ret;
2025}
2026
2027/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2028 * any DAI links.
2029 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002030int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002031{
Liam Girdwood618dae12012-04-25 12:12:51 +01002032 int i, old, new, paths;
2033
Liam Girdwood618dae12012-04-25 12:12:51 +01002034 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2035 for (i = 0; i < card->num_rtd; i++) {
2036 struct snd_soc_dapm_widget_list *list;
2037 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2038
2039 /* make sure link is FE */
2040 if (!fe->dai_link->dynamic)
2041 continue;
2042
2043 /* only check active links */
2044 if (!fe->cpu_dai->active)
2045 continue;
2046
2047 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002048 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002049 fe->dai_link->name);
2050
2051 /* skip if FE doesn't have playback capability */
2052 if (!fe->cpu_dai->driver->playback.channels_min)
2053 goto capture;
2054
2055 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2056 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002057 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002058 fe->dai_link->name, "playback");
2059 mutex_unlock(&card->mutex);
2060 return paths;
2061 }
2062
2063 /* update any new playback paths */
2064 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2065 if (new) {
2066 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2067 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2068 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2069 }
2070
2071 /* update any old playback paths */
2072 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2073 if (old) {
2074 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2075 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2076 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2077 }
2078
2079capture:
2080 /* skip if FE doesn't have capture capability */
2081 if (!fe->cpu_dai->driver->capture.channels_min)
2082 continue;
2083
2084 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2085 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002086 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002087 fe->dai_link->name, "capture");
2088 mutex_unlock(&card->mutex);
2089 return paths;
2090 }
2091
2092 /* update any new capture paths */
2093 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2094 if (new) {
2095 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2096 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2097 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2098 }
2099
2100 /* update any old capture paths */
2101 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2102 if (old) {
2103 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2104 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2105 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2106 }
2107
2108 dpcm_path_put(&list);
2109 }
2110
2111 mutex_unlock(&card->mutex);
2112 return 0;
2113}
Liam Girdwood01d75842012-04-25 12:12:49 +01002114int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2115{
2116 struct snd_soc_dpcm *dpcm;
2117 struct list_head *clients =
2118 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2119
2120 list_for_each_entry(dpcm, clients, list_be) {
2121
2122 struct snd_soc_pcm_runtime *be = dpcm->be;
2123 struct snd_soc_dai *dai = be->codec_dai;
2124 struct snd_soc_dai_driver *drv = dai->driver;
2125
2126 if (be->dai_link->ignore_suspend)
2127 continue;
2128
Liam Girdwood103d84a2012-11-19 14:39:15 +00002129 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002130
Mark Brownc5914b02013-10-30 17:47:39 -07002131 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2132 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002133 }
2134
2135 return 0;
2136}
2137
Mark Brown45c0a182012-05-09 21:46:27 +01002138static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002139{
2140 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2141 struct snd_soc_dpcm *dpcm;
2142 struct snd_soc_dapm_widget_list *list;
2143 int ret;
2144 int stream = fe_substream->stream;
2145
2146 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2147 fe->dpcm[stream].runtime = fe_substream->runtime;
2148
2149 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002150 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002151 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002152 }
2153
2154 /* calculate valid and active FE <-> BE dpcms */
2155 dpcm_process_paths(fe, stream, &list, 1);
2156
2157 ret = dpcm_fe_dai_startup(fe_substream);
2158 if (ret < 0) {
2159 /* clean up all links */
2160 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2161 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2162
2163 dpcm_be_disconnect(fe, stream);
2164 fe->dpcm[stream].runtime = NULL;
2165 }
2166
2167 dpcm_clear_pending_state(fe, stream);
2168 dpcm_path_put(&list);
2169 mutex_unlock(&fe->card->mutex);
2170 return ret;
2171}
2172
Mark Brown45c0a182012-05-09 21:46:27 +01002173static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002174{
2175 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2176 struct snd_soc_dpcm *dpcm;
2177 int stream = fe_substream->stream, ret;
2178
2179 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2180 ret = dpcm_fe_dai_shutdown(fe_substream);
2181
2182 /* mark FE's links ready to prune */
2183 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2184 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2185
2186 dpcm_be_disconnect(fe, stream);
2187
2188 fe->dpcm[stream].runtime = NULL;
2189 mutex_unlock(&fe->card->mutex);
2190 return ret;
2191}
2192
Liam Girdwoodddee6272011-06-09 14:45:53 +01002193/* create a new pcm */
2194int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2195{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002196 struct snd_soc_platform *platform = rtd->platform;
2197 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2198 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2199 struct snd_pcm *pcm;
2200 char new_name[64];
2201 int ret = 0, playback = 0, capture = 0;
2202
Liam Girdwood01d75842012-04-25 12:12:49 +01002203 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002204 playback = rtd->dai_link->dpcm_playback;
2205 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002206 } else {
Mark Brown05679092013-06-01 23:13:53 +01002207 if (codec_dai->driver->playback.channels_min &&
2208 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002209 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002210 if (codec_dai->driver->capture.channels_min &&
2211 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 capture = 1;
2213 }
Sangsu Parka5002312012-01-02 17:15:10 +09002214
Fabio Estevamd6bead02013-08-29 10:32:13 -03002215 if (rtd->dai_link->playback_only) {
2216 playback = 1;
2217 capture = 0;
2218 }
2219
2220 if (rtd->dai_link->capture_only) {
2221 playback = 0;
2222 capture = 1;
2223 }
2224
Liam Girdwood01d75842012-04-25 12:12:49 +01002225 /* create the PCM */
2226 if (rtd->dai_link->no_pcm) {
2227 snprintf(new_name, sizeof(new_name), "(%s)",
2228 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002229
Liam Girdwood01d75842012-04-25 12:12:49 +01002230 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2231 playback, capture, &pcm);
2232 } else {
2233 if (rtd->dai_link->dynamic)
2234 snprintf(new_name, sizeof(new_name), "%s (*)",
2235 rtd->dai_link->stream_name);
2236 else
2237 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2238 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002239
Liam Girdwood01d75842012-04-25 12:12:49 +01002240 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2241 capture, &pcm);
2242 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002243 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002244 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002245 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002246 return ret;
2247 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002248 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002249
2250 /* DAPM dai link stream work */
2251 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2252
2253 rtd->pcm = pcm;
2254 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002255
2256 if (rtd->dai_link->no_pcm) {
2257 if (playback)
2258 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2259 if (capture)
2260 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2261 goto out;
2262 }
2263
2264 /* ASoC PCM operations */
2265 if (rtd->dai_link->dynamic) {
2266 rtd->ops.open = dpcm_fe_dai_open;
2267 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2268 rtd->ops.prepare = dpcm_fe_dai_prepare;
2269 rtd->ops.trigger = dpcm_fe_dai_trigger;
2270 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2271 rtd->ops.close = dpcm_fe_dai_close;
2272 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002273 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002274 } else {
2275 rtd->ops.open = soc_pcm_open;
2276 rtd->ops.hw_params = soc_pcm_hw_params;
2277 rtd->ops.prepare = soc_pcm_prepare;
2278 rtd->ops.trigger = soc_pcm_trigger;
2279 rtd->ops.hw_free = soc_pcm_hw_free;
2280 rtd->ops.close = soc_pcm_close;
2281 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002282 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002283 }
2284
Liam Girdwoodddee6272011-06-09 14:45:53 +01002285 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002286 rtd->ops.ack = platform->driver->ops->ack;
2287 rtd->ops.copy = platform->driver->ops->copy;
2288 rtd->ops.silence = platform->driver->ops->silence;
2289 rtd->ops.page = platform->driver->ops->page;
2290 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002291 }
2292
2293 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002294 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002295
2296 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002297 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002298
2299 if (platform->driver->pcm_new) {
2300 ret = platform->driver->pcm_new(rtd);
2301 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002302 dev_err(platform->dev,
2303 "ASoC: pcm constructor failed: %d\n",
2304 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002305 return ret;
2306 }
2307 }
2308
2309 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002310out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002311 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002312 cpu_dai->name);
2313 return ret;
2314}
Liam Girdwood01d75842012-04-25 12:12:49 +01002315
2316/* is the current PCM operation for this FE ? */
2317int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2318{
2319 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2320 return 1;
2321 return 0;
2322}
2323EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2324
2325/* is the current PCM operation for this BE ? */
2326int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2327 struct snd_soc_pcm_runtime *be, int stream)
2328{
2329 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2330 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2331 be->dpcm[stream].runtime_update))
2332 return 1;
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2336
2337/* get the substream for this BE */
2338struct snd_pcm_substream *
2339 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2340{
2341 return be->pcm->streams[stream].substream;
2342}
2343EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2344
2345/* get the BE runtime state */
2346enum snd_soc_dpcm_state
2347 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2348{
2349 return be->dpcm[stream].state;
2350}
2351EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2352
2353/* set the BE runtime state */
2354void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2355 int stream, enum snd_soc_dpcm_state state)
2356{
2357 be->dpcm[stream].state = state;
2358}
2359EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2360
2361/*
2362 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2363 * are not running, paused or suspended for the specified stream direction.
2364 */
2365int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2366 struct snd_soc_pcm_runtime *be, int stream)
2367{
2368 struct snd_soc_dpcm *dpcm;
2369 int state;
2370
2371 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2372
2373 if (dpcm->fe == fe)
2374 continue;
2375
2376 state = dpcm->fe->dpcm[stream].state;
2377 if (state == SND_SOC_DPCM_STATE_START ||
2378 state == SND_SOC_DPCM_STATE_PAUSED ||
2379 state == SND_SOC_DPCM_STATE_SUSPEND)
2380 return 0;
2381 }
2382
2383 /* it's safe to free/stop this BE DAI */
2384 return 1;
2385}
2386EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2387
2388/*
2389 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2390 * running, paused or suspended for the specified stream direction.
2391 */
2392int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2393 struct snd_soc_pcm_runtime *be, int stream)
2394{
2395 struct snd_soc_dpcm *dpcm;
2396 int state;
2397
2398 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2399
2400 if (dpcm->fe == fe)
2401 continue;
2402
2403 state = dpcm->fe->dpcm[stream].state;
2404 if (state == SND_SOC_DPCM_STATE_START ||
2405 state == SND_SOC_DPCM_STATE_PAUSED ||
2406 state == SND_SOC_DPCM_STATE_SUSPEND ||
2407 state == SND_SOC_DPCM_STATE_PREPARE)
2408 return 0;
2409 }
2410
2411 /* it's safe to change hw_params */
2412 return 1;
2413}
2414EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002415
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002416int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2417 int cmd, struct snd_soc_platform *platform)
2418{
Mark Brownc5914b02013-10-30 17:47:39 -07002419 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002420 return platform->driver->ops->trigger(substream, cmd);
2421 return 0;
2422}
2423EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2424
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002425#ifdef CONFIG_DEBUG_FS
2426static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2427{
2428 switch (state) {
2429 case SND_SOC_DPCM_STATE_NEW:
2430 return "new";
2431 case SND_SOC_DPCM_STATE_OPEN:
2432 return "open";
2433 case SND_SOC_DPCM_STATE_HW_PARAMS:
2434 return "hw_params";
2435 case SND_SOC_DPCM_STATE_PREPARE:
2436 return "prepare";
2437 case SND_SOC_DPCM_STATE_START:
2438 return "start";
2439 case SND_SOC_DPCM_STATE_STOP:
2440 return "stop";
2441 case SND_SOC_DPCM_STATE_SUSPEND:
2442 return "suspend";
2443 case SND_SOC_DPCM_STATE_PAUSED:
2444 return "paused";
2445 case SND_SOC_DPCM_STATE_HW_FREE:
2446 return "hw_free";
2447 case SND_SOC_DPCM_STATE_CLOSE:
2448 return "close";
2449 }
2450
2451 return "unknown";
2452}
2453
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002454static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2455 int stream, char *buf, size_t size)
2456{
2457 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2458 struct snd_soc_dpcm *dpcm;
2459 ssize_t offset = 0;
2460
2461 /* FE state */
2462 offset += snprintf(buf + offset, size - offset,
2463 "[%s - %s]\n", fe->dai_link->name,
2464 stream ? "Capture" : "Playback");
2465
2466 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2467 dpcm_state_string(fe->dpcm[stream].state));
2468
2469 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2470 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2471 offset += snprintf(buf + offset, size - offset,
2472 "Hardware Params: "
2473 "Format = %s, Channels = %d, Rate = %d\n",
2474 snd_pcm_format_name(params_format(params)),
2475 params_channels(params),
2476 params_rate(params));
2477
2478 /* BEs state */
2479 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2480
2481 if (list_empty(&fe->dpcm[stream].be_clients)) {
2482 offset += snprintf(buf + offset, size - offset,
2483 " No active DSP links\n");
2484 goto out;
2485 }
2486
2487 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2488 struct snd_soc_pcm_runtime *be = dpcm->be;
2489 params = &dpcm->hw_params;
2490
2491 offset += snprintf(buf + offset, size - offset,
2492 "- %s\n", be->dai_link->name);
2493
2494 offset += snprintf(buf + offset, size - offset,
2495 " State: %s\n",
2496 dpcm_state_string(be->dpcm[stream].state));
2497
2498 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2499 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2500 offset += snprintf(buf + offset, size - offset,
2501 " Hardware Params: "
2502 "Format = %s, Channels = %d, Rate = %d\n",
2503 snd_pcm_format_name(params_format(params)),
2504 params_channels(params),
2505 params_rate(params));
2506 }
2507
2508out:
2509 return offset;
2510}
2511
2512static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2513 size_t count, loff_t *ppos)
2514{
2515 struct snd_soc_pcm_runtime *fe = file->private_data;
2516 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2517 char *buf;
2518
2519 buf = kmalloc(out_count, GFP_KERNEL);
2520 if (!buf)
2521 return -ENOMEM;
2522
2523 if (fe->cpu_dai->driver->playback.channels_min)
2524 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2525 buf + offset, out_count - offset);
2526
2527 if (fe->cpu_dai->driver->capture.channels_min)
2528 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2529 buf + offset, out_count - offset);
2530
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002531 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002532
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002533 kfree(buf);
2534 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002535}
2536
2537static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002538 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002539 .read = dpcm_state_read_file,
2540 .llseek = default_llseek,
2541};
2542
2543int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2544{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002545 if (!rtd->dai_link)
2546 return 0;
2547
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002548 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2549 rtd->card->debugfs_card_root);
2550 if (!rtd->debugfs_dpcm_root) {
2551 dev_dbg(rtd->dev,
2552 "ASoC: Failed to create dpcm debugfs directory %s\n",
2553 rtd->dai_link->name);
2554 return -EINVAL;
2555 }
2556
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002557 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002558 rtd->debugfs_dpcm_root,
2559 rtd, &dpcm_state_fops);
2560
2561 return 0;
2562}
2563#endif