blob: 604e7e9a2ef8d9b1a5bdb53e8551e3fb159062e0 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
38 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
39 * @substream: the pcm substream
40 * @hw: the hardware parameters
41 *
42 * Sets the substream runtime hardware parameters.
43 */
44int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
45 const struct snd_pcm_hardware *hw)
46{
47 struct snd_pcm_runtime *runtime = substream->runtime;
48 runtime->hw.info = hw->info;
49 runtime->hw.formats = hw->formats;
50 runtime->hw.period_bytes_min = hw->period_bytes_min;
51 runtime->hw.period_bytes_max = hw->period_bytes_max;
52 runtime->hw.periods_min = hw->periods_min;
53 runtime->hw.periods_max = hw->periods_max;
54 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
55 runtime->hw.fifo_size = hw->fifo_size;
56 return 0;
57}
58EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
59
Liam Girdwood01d75842012-04-25 12:12:49 +010060/* DPCM stream event, send event to FE and all active BEs. */
61static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
62 int event)
63{
64 struct snd_soc_dpcm *dpcm;
65
66 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
67
68 struct snd_soc_pcm_runtime *be = dpcm->be;
69
Liam Girdwood103d84a2012-11-19 14:39:15 +000070 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +010071 be->dai_link->name, event, dir);
72
73 snd_soc_dapm_stream_event(be, dir, event);
74 }
75
76 snd_soc_dapm_stream_event(fe, dir, event);
77
78 return 0;
79}
80
Dong Aisheng17841022011-08-29 17:15:14 +080081static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
82 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010083{
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010085 int ret;
86
Nicolin Chen3635bf02013-11-13 18:56:24 +080087 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
88 rtd->dai_link->symmetric_rates)) {
89 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
90 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010091
Nicolin Chen3635bf02013-11-13 18:56:24 +080092 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
93 SNDRV_PCM_HW_PARAM_RATE,
94 soc_dai->rate, soc_dai->rate);
95 if (ret < 0) {
96 dev_err(soc_dai->dev,
97 "ASoC: Unable to apply rate constraint: %d\n",
98 ret);
99 return ret;
100 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100101 }
102
Nicolin Chen3635bf02013-11-13 18:56:24 +0800103 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
104 rtd->dai_link->symmetric_channels)) {
105 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
106 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100107
Nicolin Chen3635bf02013-11-13 18:56:24 +0800108 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
109 SNDRV_PCM_HW_PARAM_CHANNELS,
110 soc_dai->channels,
111 soc_dai->channels);
112 if (ret < 0) {
113 dev_err(soc_dai->dev,
114 "ASoC: Unable to apply channel symmetry constraint: %d\n",
115 ret);
116 return ret;
117 }
118 }
119
120 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
121 rtd->dai_link->symmetric_samplebits)) {
122 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
123 soc_dai->sample_bits);
124
125 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
126 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
127 soc_dai->sample_bits,
128 soc_dai->sample_bits);
129 if (ret < 0) {
130 dev_err(soc_dai->dev,
131 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
132 ret);
133 return ret;
134 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100135 }
136
137 return 0;
138}
139
Nicolin Chen3635bf02013-11-13 18:56:24 +0800140static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *params)
142{
143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
144 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
145 struct snd_soc_dai *codec_dai = rtd->codec_dai;
146 unsigned int rate, channels, sample_bits, symmetry;
147
148 rate = params_rate(params);
149 channels = params_channels(params);
150 sample_bits = snd_pcm_format_physical_width(params_format(params));
151
152 /* reject unmatched parameters when applying symmetry */
153 symmetry = cpu_dai->driver->symmetric_rates ||
154 codec_dai->driver->symmetric_rates ||
155 rtd->dai_link->symmetric_rates;
156 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
157 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
158 cpu_dai->rate, rate);
159 return -EINVAL;
160 }
161
162 symmetry = cpu_dai->driver->symmetric_channels ||
163 codec_dai->driver->symmetric_channels ||
164 rtd->dai_link->symmetric_channels;
165 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
166 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
167 cpu_dai->channels, channels);
168 return -EINVAL;
169 }
170
171 symmetry = cpu_dai->driver->symmetric_samplebits ||
172 codec_dai->driver->symmetric_samplebits ||
173 rtd->dai_link->symmetric_samplebits;
174 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
175 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
176 cpu_dai->sample_bits, sample_bits);
177 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100178 }
179
180 return 0;
181}
182
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100183static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
184{
185 struct snd_soc_pcm_runtime *rtd = substream->private_data;
186 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
187 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
188 struct snd_soc_dai_link *link = rtd->dai_link;
189
190 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
191 link->symmetric_rates || cpu_driver->symmetric_channels ||
192 codec_driver->symmetric_channels || link->symmetric_channels ||
193 cpu_driver->symmetric_samplebits ||
194 codec_driver->symmetric_samplebits ||
195 link->symmetric_samplebits;
196}
197
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198/*
Mark Brown58ba9b22012-01-16 18:38:51 +0000199 * List of sample sizes that might go over the bus for parameter
200 * application. There ought to be a wildcard sample size for things
201 * like the DAC/ADC resolution to use but there isn't right now.
202 */
203static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +0200204 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +0000205};
206
207static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
208 struct snd_soc_dai *dai)
209{
210 int ret, i, bits;
211
212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
213 bits = dai->driver->playback.sig_bits;
214 else
215 bits = dai->driver->capture.sig_bits;
216
217 if (!bits)
218 return;
219
220 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
Mark Brown278047f2012-01-19 18:04:18 +0000221 if (bits >= sample_sizes[i])
222 continue;
223
224 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
225 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +0000226 if (ret != 0)
227 dev_warn(dai->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +0000228 "ASoC: Failed to set MSB %d/%d: %d\n",
Mark Brown58ba9b22012-01-16 18:38:51 +0000229 bits, sample_sizes[i], ret);
230 }
231}
232
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100233static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200234 struct snd_soc_pcm_stream *codec_stream,
235 struct snd_soc_pcm_stream *cpu_stream)
236{
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100237 struct snd_pcm_hardware *hw = &runtime->hw;
238
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200239 hw->channels_min = max(codec_stream->channels_min,
240 cpu_stream->channels_min);
241 hw->channels_max = min(codec_stream->channels_max,
242 cpu_stream->channels_max);
243 hw->formats = codec_stream->formats & cpu_stream->formats;
244 hw->rates = codec_stream->rates & cpu_stream->rates;
245 if (codec_stream->rates
246 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
247 hw->rates |= cpu_stream->rates;
248 if (cpu_stream->rates
249 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
250 hw->rates |= codec_stream->rates;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100251
252 snd_pcm_limit_hw_rates(runtime);
253
254 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
255 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
256 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
257 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200258}
259
Mark Brown58ba9b22012-01-16 18:38:51 +0000260/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100261 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
262 * then initialized and any private data can be allocated. This also calls
263 * startup for the cpu DAI, platform, machine and codec DAI.
264 */
265static int soc_pcm_open(struct snd_pcm_substream *substream)
266{
267 struct snd_soc_pcm_runtime *rtd = substream->private_data;
268 struct snd_pcm_runtime *runtime = substream->runtime;
269 struct snd_soc_platform *platform = rtd->platform;
270 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
271 struct snd_soc_dai *codec_dai = rtd->codec_dai;
272 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
273 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
274 int ret = 0;
275
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800276 pinctrl_pm_select_default_state(cpu_dai->dev);
277 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000278 pm_runtime_get_sync(cpu_dai->dev);
279 pm_runtime_get_sync(codec_dai->dev);
280 pm_runtime_get_sync(platform->dev);
281
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100282 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100283
284 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700285 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100286 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
287 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000288 dev_err(cpu_dai->dev, "ASoC: can't open interface"
289 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100290 goto out;
291 }
292 }
293
294 if (platform->driver->ops && platform->driver->ops->open) {
295 ret = platform->driver->ops->open(substream);
296 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000297 dev_err(platform->dev, "ASoC: can't open platform"
298 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100299 goto platform_err;
300 }
301 }
302
Mark Brownc5914b02013-10-30 17:47:39 -0700303 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100304 ret = codec_dai->driver->ops->startup(substream, codec_dai);
305 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000306 dev_err(codec_dai->dev, "ASoC: can't open codec"
307 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100308 goto codec_dai_err;
309 }
310 }
311
312 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
313 ret = rtd->dai_link->ops->startup(substream);
314 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000315 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000316 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100317 goto machine_err;
318 }
319 }
320
Liam Girdwood01d75842012-04-25 12:12:49 +0100321 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
322 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
323 goto dynamic;
324
Liam Girdwoodddee6272011-06-09 14:45:53 +0100325 /* Check that the codec and cpu DAIs are compatible */
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100327 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200328 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100329 } else {
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100330 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200331 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100332 }
333
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100334 if (soc_pcm_has_symmetry(substream))
335 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
336
Liam Girdwoodddee6272011-06-09 14:45:53 +0100337 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100338 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000339 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100340 codec_dai->name, cpu_dai->name);
341 goto config_err;
342 }
343 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000344 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100345 codec_dai->name, cpu_dai->name);
346 goto config_err;
347 }
348 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
349 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000350 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100351 codec_dai->name, cpu_dai->name);
352 goto config_err;
353 }
354
Mark Brown58ba9b22012-01-16 18:38:51 +0000355 soc_pcm_apply_msb(substream, codec_dai);
356 soc_pcm_apply_msb(substream, cpu_dai);
357
Liam Girdwoodddee6272011-06-09 14:45:53 +0100358 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800359 if (cpu_dai->active) {
360 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
361 if (ret != 0)
362 goto config_err;
363 }
364
365 if (codec_dai->active) {
366 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100367 if (ret != 0)
368 goto config_err;
369 }
370
Liam Girdwood103d84a2012-11-19 14:39:15 +0000371 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100372 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000373 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
374 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100375 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000376 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100377 runtime->hw.rate_max);
378
Liam Girdwood01d75842012-04-25 12:12:49 +0100379dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100380 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
381 cpu_dai->playback_active++;
382 codec_dai->playback_active++;
383 } else {
384 cpu_dai->capture_active++;
385 codec_dai->capture_active++;
386 }
387 cpu_dai->active++;
388 codec_dai->active++;
389 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100390 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100391 return 0;
392
393config_err:
394 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
395 rtd->dai_link->ops->shutdown(substream);
396
397machine_err:
398 if (codec_dai->driver->ops->shutdown)
399 codec_dai->driver->ops->shutdown(substream, codec_dai);
400
401codec_dai_err:
402 if (platform->driver->ops && platform->driver->ops->close)
403 platform->driver->ops->close(substream);
404
405platform_err:
406 if (cpu_dai->driver->ops->shutdown)
407 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
408out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100409 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000410
411 pm_runtime_put(platform->dev);
412 pm_runtime_put(codec_dai->dev);
413 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800414 if (!codec_dai->active)
415 pinctrl_pm_select_sleep_state(codec_dai->dev);
416 if (!cpu_dai->active)
417 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000418
Liam Girdwoodddee6272011-06-09 14:45:53 +0100419 return ret;
420}
421
422/*
423 * Power down the audio subsystem pmdown_time msecs after close is called.
424 * This is to ensure there are no pops or clicks in between any music tracks
425 * due to DAPM power cycling.
426 */
427static void close_delayed_work(struct work_struct *work)
428{
429 struct snd_soc_pcm_runtime *rtd =
430 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
431 struct snd_soc_dai *codec_dai = rtd->codec_dai;
432
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100433 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100434
Liam Girdwood103d84a2012-11-19 14:39:15 +0000435 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100436 codec_dai->driver->playback.stream_name,
437 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600438 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100439
440 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600441 if (rtd->pop_wait == 1) {
442 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800443 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000444 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100445 }
446
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100447 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448}
449
450/*
451 * Called by ALSA when a PCM substream is closed. Private data can be
452 * freed here. The cpu DAI, codec DAI, machine and platform are also
453 * shutdown.
454 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100455static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100456{
457 struct snd_soc_pcm_runtime *rtd = substream->private_data;
458 struct snd_soc_platform *platform = rtd->platform;
459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
460 struct snd_soc_dai *codec_dai = rtd->codec_dai;
461 struct snd_soc_codec *codec = rtd->codec;
462
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100463 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100464
465 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
466 cpu_dai->playback_active--;
467 codec_dai->playback_active--;
468 } else {
469 cpu_dai->capture_active--;
470 codec_dai->capture_active--;
471 }
472
473 cpu_dai->active--;
474 codec_dai->active--;
475 codec->active--;
476
Dong Aisheng17841022011-08-29 17:15:14 +0800477 /* clear the corresponding DAIs rate when inactive */
478 if (!cpu_dai->active)
479 cpu_dai->rate = 0;
480
481 if (!codec_dai->active)
482 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200483
Liam Girdwoodddee6272011-06-09 14:45:53 +0100484 if (cpu_dai->driver->ops->shutdown)
485 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
486
487 if (codec_dai->driver->ops->shutdown)
488 codec_dai->driver->ops->shutdown(substream, codec_dai);
489
490 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
491 rtd->dai_link->ops->shutdown(substream);
492
493 if (platform->driver->ops && platform->driver->ops->close)
494 platform->driver->ops->close(substream);
495 cpu_dai->runtime = NULL;
496
497 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000498 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200499 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300500 /* powered down playback stream now */
501 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800502 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800503 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300504 } else {
505 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600506 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100507 queue_delayed_work(system_power_efficient_wq,
508 &rtd->delayed_work,
509 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300510 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100511 } else {
512 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800513 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000514 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100515 }
516
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100517 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000518
519 pm_runtime_put(platform->dev);
520 pm_runtime_put(codec_dai->dev);
521 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800522 if (!codec_dai->active)
523 pinctrl_pm_select_sleep_state(codec_dai->dev);
524 if (!cpu_dai->active)
525 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000526
Liam Girdwoodddee6272011-06-09 14:45:53 +0100527 return 0;
528}
529
530/*
531 * Called by ALSA when the PCM substream is prepared, can set format, sample
532 * rate, etc. This function is non atomic and can be called multiple times,
533 * it can refer to the runtime info.
534 */
535static int soc_pcm_prepare(struct snd_pcm_substream *substream)
536{
537 struct snd_soc_pcm_runtime *rtd = substream->private_data;
538 struct snd_soc_platform *platform = rtd->platform;
539 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
540 struct snd_soc_dai *codec_dai = rtd->codec_dai;
541 int ret = 0;
542
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100543 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100544
545 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
546 ret = rtd->dai_link->ops->prepare(substream);
547 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000548 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
549 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550 goto out;
551 }
552 }
553
554 if (platform->driver->ops && platform->driver->ops->prepare) {
555 ret = platform->driver->ops->prepare(substream);
556 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000557 dev_err(platform->dev, "ASoC: platform prepare error:"
558 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100559 goto out;
560 }
561 }
562
Mark Brownc5914b02013-10-30 17:47:39 -0700563 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100564 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
565 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000566 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000567 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100568 goto out;
569 }
570 }
571
Mark Brownc5914b02013-10-30 17:47:39 -0700572 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
574 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000575 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000576 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577 goto out;
578 }
579 }
580
581 /* cancel any delayed stream shutdown that is pending */
582 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600583 rtd->pop_wait) {
584 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100585 cancel_delayed_work(&rtd->delayed_work);
586 }
587
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000588 snd_soc_dapm_stream_event(rtd, substream->stream,
589 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100590
Mark Brownda183962013-02-06 15:44:07 +0000591 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100592
593out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100594 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100595 return ret;
596}
597
598/*
599 * Called by ALSA when the hardware params are set by application. This
600 * function can also be called multiple times and can allocate buffers
601 * (using snd_pcm_lib_* ). It's non-atomic.
602 */
603static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
604 struct snd_pcm_hw_params *params)
605{
606 struct snd_soc_pcm_runtime *rtd = substream->private_data;
607 struct snd_soc_platform *platform = rtd->platform;
608 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
609 struct snd_soc_dai *codec_dai = rtd->codec_dai;
610 int ret = 0;
611
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100612 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100613
Nicolin Chen3635bf02013-11-13 18:56:24 +0800614 ret = soc_pcm_params_symmetry(substream, params);
615 if (ret)
616 goto out;
617
Liam Girdwoodddee6272011-06-09 14:45:53 +0100618 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
619 ret = rtd->dai_link->ops->hw_params(substream, params);
620 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000621 dev_err(rtd->card->dev, "ASoC: machine hw_params"
622 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100623 goto out;
624 }
625 }
626
Mark Brownc5914b02013-10-30 17:47:39 -0700627 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
629 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000630 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
631 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100632 goto codec_err;
633 }
634 }
635
Mark Brownc5914b02013-10-30 17:47:39 -0700636 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100637 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
638 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000639 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000640 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100641 goto interface_err;
642 }
643 }
644
645 if (platform->driver->ops && platform->driver->ops->hw_params) {
646 ret = platform->driver->ops->hw_params(substream, params);
647 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000648 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000649 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100650 goto platform_err;
651 }
652 }
653
Nicolin Chen3635bf02013-11-13 18:56:24 +0800654 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800655 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800656 cpu_dai->channels = params_channels(params);
657 cpu_dai->sample_bits =
658 snd_pcm_format_physical_width(params_format(params));
659
Dong Aisheng17841022011-08-29 17:15:14 +0800660 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800661 codec_dai->channels = params_channels(params);
662 codec_dai->sample_bits =
663 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100664
665out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100666 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100667 return ret;
668
669platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700670 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
672
673interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700674 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675 codec_dai->driver->ops->hw_free(substream, codec_dai);
676
677codec_err:
678 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
679 rtd->dai_link->ops->hw_free(substream);
680
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100681 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100682 return ret;
683}
684
685/*
686 * Frees resources allocated by hw_params, can be called multiple times
687 */
688static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
689{
690 struct snd_soc_pcm_runtime *rtd = substream->private_data;
691 struct snd_soc_platform *platform = rtd->platform;
692 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
693 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800694 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100695
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100696 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100697
Nicolin Chend3383422013-11-20 18:37:09 +0800698 /* clear the corresponding DAIs parameters when going to be inactive */
699 if (cpu_dai->active == 1) {
700 cpu_dai->rate = 0;
701 cpu_dai->channels = 0;
702 cpu_dai->sample_bits = 0;
703 }
704
705 if (codec_dai->active == 1) {
706 codec_dai->rate = 0;
707 codec_dai->channels = 0;
708 codec_dai->sample_bits = 0;
709 }
710
Liam Girdwoodddee6272011-06-09 14:45:53 +0100711 /* apply codec digital mute */
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800712 if ((playback && codec_dai->playback_active == 1) ||
713 (!playback && codec_dai->capture_active == 1))
Mark Brownda183962013-02-06 15:44:07 +0000714 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100715
716 /* free any machine hw params */
717 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
718 rtd->dai_link->ops->hw_free(substream);
719
720 /* free any DMA resources */
721 if (platform->driver->ops && platform->driver->ops->hw_free)
722 platform->driver->ops->hw_free(substream);
723
724 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700725 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726 codec_dai->driver->ops->hw_free(substream, codec_dai);
727
Mark Brownc5914b02013-10-30 17:47:39 -0700728 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100729 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
730
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100731 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100732 return 0;
733}
734
735static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
736{
737 struct snd_soc_pcm_runtime *rtd = substream->private_data;
738 struct snd_soc_platform *platform = rtd->platform;
739 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
740 struct snd_soc_dai *codec_dai = rtd->codec_dai;
741 int ret;
742
Mark Brownc5914b02013-10-30 17:47:39 -0700743 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100744 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
745 if (ret < 0)
746 return ret;
747 }
748
749 if (platform->driver->ops && platform->driver->ops->trigger) {
750 ret = platform->driver->ops->trigger(substream, cmd);
751 if (ret < 0)
752 return ret;
753 }
754
Mark Brownc5914b02013-10-30 17:47:39 -0700755 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100756 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
757 if (ret < 0)
758 return ret;
759 }
760 return 0;
761}
762
Mark Brown45c0a182012-05-09 21:46:27 +0100763static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
764 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100765{
766 struct snd_soc_pcm_runtime *rtd = substream->private_data;
767 struct snd_soc_platform *platform = rtd->platform;
768 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
769 struct snd_soc_dai *codec_dai = rtd->codec_dai;
770 int ret;
771
Mark Brownc5914b02013-10-30 17:47:39 -0700772 if (codec_dai->driver->ops &&
773 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100774 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
775 if (ret < 0)
776 return ret;
777 }
778
Mark Brownc5914b02013-10-30 17:47:39 -0700779 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100780 ret = platform->driver->bespoke_trigger(substream, cmd);
781 if (ret < 0)
782 return ret;
783 }
784
Mark Brownc5914b02013-10-30 17:47:39 -0700785 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100786 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
787 if (ret < 0)
788 return ret;
789 }
790 return 0;
791}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100792/*
793 * soc level wrapper for pointer callback
794 * If cpu_dai, codec_dai, platform driver has the delay callback, than
795 * the runtime->delay will be updated accordingly.
796 */
797static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
798{
799 struct snd_soc_pcm_runtime *rtd = substream->private_data;
800 struct snd_soc_platform *platform = rtd->platform;
801 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
802 struct snd_soc_dai *codec_dai = rtd->codec_dai;
803 struct snd_pcm_runtime *runtime = substream->runtime;
804 snd_pcm_uframes_t offset = 0;
805 snd_pcm_sframes_t delay = 0;
806
807 if (platform->driver->ops && platform->driver->ops->pointer)
808 offset = platform->driver->ops->pointer(substream);
809
Mark Brownc5914b02013-10-30 17:47:39 -0700810 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100811 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
812
Mark Brownc5914b02013-10-30 17:47:39 -0700813 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100814 delay += codec_dai->driver->ops->delay(substream, codec_dai);
815
816 if (platform->driver->delay)
817 delay += platform->driver->delay(substream, codec_dai);
818
819 runtime->delay = delay;
820
821 return offset;
822}
823
Liam Girdwood01d75842012-04-25 12:12:49 +0100824/* connect a FE and BE */
825static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
826 struct snd_soc_pcm_runtime *be, int stream)
827{
828 struct snd_soc_dpcm *dpcm;
829
830 /* only add new dpcms */
831 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
832 if (dpcm->be == be && dpcm->fe == fe)
833 return 0;
834 }
835
836 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
837 if (!dpcm)
838 return -ENOMEM;
839
840 dpcm->be = be;
841 dpcm->fe = fe;
842 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
843 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
844 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
845 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
846
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300847 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100848 stream ? "capture" : "playback", fe->dai_link->name,
849 stream ? "<-" : "->", be->dai_link->name);
850
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100851#ifdef CONFIG_DEBUG_FS
852 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
853 fe->debugfs_dpcm_root, &dpcm->state);
854#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100855 return 1;
856}
857
858/* reparent a BE onto another FE */
859static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
860 struct snd_soc_pcm_runtime *be, int stream)
861{
862 struct snd_soc_dpcm *dpcm;
863 struct snd_pcm_substream *fe_substream, *be_substream;
864
865 /* reparent if BE is connected to other FEs */
866 if (!be->dpcm[stream].users)
867 return;
868
869 be_substream = snd_soc_dpcm_get_substream(be, stream);
870
871 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
872 if (dpcm->fe == fe)
873 continue;
874
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300875 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100876 stream ? "capture" : "playback",
877 dpcm->fe->dai_link->name,
878 stream ? "<-" : "->", dpcm->be->dai_link->name);
879
880 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
881 be_substream->runtime = fe_substream->runtime;
882 break;
883 }
884}
885
886/* disconnect a BE and FE */
887static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
888{
889 struct snd_soc_dpcm *dpcm, *d;
890
891 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000892 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100893 stream ? "capture" : "playback",
894 dpcm->be->dai_link->name);
895
896 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
897 continue;
898
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300899 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100900 stream ? "capture" : "playback", fe->dai_link->name,
901 stream ? "<-" : "->", dpcm->be->dai_link->name);
902
903 /* BEs still alive need new FE */
904 dpcm_be_reparent(fe, dpcm->be, stream);
905
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100906#ifdef CONFIG_DEBUG_FS
907 debugfs_remove(dpcm->debugfs_state);
908#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100909 list_del(&dpcm->list_be);
910 list_del(&dpcm->list_fe);
911 kfree(dpcm);
912 }
913}
914
915/* get BE for DAI widget and stream */
916static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
917 struct snd_soc_dapm_widget *widget, int stream)
918{
919 struct snd_soc_pcm_runtime *be;
920 int i;
921
922 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
923 for (i = 0; i < card->num_links; i++) {
924 be = &card->rtd[i];
925
Liam Girdwood35ea0652012-06-05 19:26:59 +0100926 if (!be->dai_link->no_pcm)
927 continue;
928
Liam Girdwood01d75842012-04-25 12:12:49 +0100929 if (be->cpu_dai->playback_widget == widget ||
930 be->codec_dai->playback_widget == widget)
931 return be;
932 }
933 } else {
934
935 for (i = 0; i < card->num_links; i++) {
936 be = &card->rtd[i];
937
Liam Girdwood35ea0652012-06-05 19:26:59 +0100938 if (!be->dai_link->no_pcm)
939 continue;
940
Liam Girdwood01d75842012-04-25 12:12:49 +0100941 if (be->cpu_dai->capture_widget == widget ||
942 be->codec_dai->capture_widget == widget)
943 return be;
944 }
945 }
946
Liam Girdwood103d84a2012-11-19 14:39:15 +0000947 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100948 stream ? "capture" : "playback", widget->name);
949 return NULL;
950}
951
952static inline struct snd_soc_dapm_widget *
953 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
954{
955 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
956 return rtd->cpu_dai->playback_widget;
957 else
958 return rtd->cpu_dai->capture_widget;
959}
960
961static inline struct snd_soc_dapm_widget *
962 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
963{
964 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
965 return rtd->codec_dai->playback_widget;
966 else
967 return rtd->codec_dai->capture_widget;
968}
969
970static int widget_in_list(struct snd_soc_dapm_widget_list *list,
971 struct snd_soc_dapm_widget *widget)
972{
973 int i;
974
975 for (i = 0; i < list->num_widgets; i++) {
976 if (widget == list->widgets[i])
977 return 1;
978 }
979
980 return 0;
981}
982
983static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
984 int stream, struct snd_soc_dapm_widget_list **list_)
985{
986 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
987 struct snd_soc_dapm_widget_list *list;
988 int paths;
989
990 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
991 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
992 if (list == NULL)
993 return -ENOMEM;
994
995 /* get number of valid DAI paths and their widgets */
996 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
997
Liam Girdwood103d84a2012-11-19 14:39:15 +0000998 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100999 stream ? "capture" : "playback");
1000
1001 *list_ = list;
1002 return paths;
1003}
1004
1005static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1006{
1007 kfree(*list);
1008}
1009
1010static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1011 struct snd_soc_dapm_widget_list **list_)
1012{
1013 struct snd_soc_dpcm *dpcm;
1014 struct snd_soc_dapm_widget_list *list = *list_;
1015 struct snd_soc_dapm_widget *widget;
1016 int prune = 0;
1017
1018 /* Destroy any old FE <--> BE connections */
1019 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1020
1021 /* is there a valid CPU DAI widget for this BE */
1022 widget = rtd_get_cpu_widget(dpcm->be, stream);
1023
1024 /* prune the BE if it's no longer in our active list */
1025 if (widget && widget_in_list(list, widget))
1026 continue;
1027
1028 /* is there a valid CODEC DAI widget for this BE */
1029 widget = rtd_get_codec_widget(dpcm->be, stream);
1030
1031 /* prune the BE if it's no longer in our active list */
1032 if (widget && widget_in_list(list, widget))
1033 continue;
1034
Liam Girdwood103d84a2012-11-19 14:39:15 +00001035 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001036 stream ? "capture" : "playback",
1037 dpcm->be->dai_link->name, fe->dai_link->name);
1038 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1039 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1040 prune++;
1041 }
1042
Liam Girdwood103d84a2012-11-19 14:39:15 +00001043 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001044 return prune;
1045}
1046
1047static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1048 struct snd_soc_dapm_widget_list **list_)
1049{
1050 struct snd_soc_card *card = fe->card;
1051 struct snd_soc_dapm_widget_list *list = *list_;
1052 struct snd_soc_pcm_runtime *be;
1053 int i, new = 0, err;
1054
1055 /* Create any new FE <--> BE connections */
1056 for (i = 0; i < list->num_widgets; i++) {
1057
Mark Brown46162742013-06-05 19:36:11 +01001058 switch (list->widgets[i]->id) {
1059 case snd_soc_dapm_dai_in:
1060 case snd_soc_dapm_dai_out:
1061 break;
1062 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001063 continue;
Mark Brown46162742013-06-05 19:36:11 +01001064 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001065
1066 /* is there a valid BE rtd for this widget */
1067 be = dpcm_get_be(card, list->widgets[i], stream);
1068 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001069 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001070 list->widgets[i]->name);
1071 continue;
1072 }
1073
1074 /* make sure BE is a real BE */
1075 if (!be->dai_link->no_pcm)
1076 continue;
1077
1078 /* don't connect if FE is not running */
1079 if (!fe->dpcm[stream].runtime)
1080 continue;
1081
1082 /* newly connected FE and BE */
1083 err = dpcm_be_connect(fe, be, stream);
1084 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001085 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001086 list->widgets[i]->name);
1087 break;
1088 } else if (err == 0) /* already connected */
1089 continue;
1090
1091 /* new */
1092 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1093 new++;
1094 }
1095
Liam Girdwood103d84a2012-11-19 14:39:15 +00001096 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001097 return new;
1098}
1099
1100/*
1101 * Find the corresponding BE DAIs that source or sink audio to this
1102 * FE substream.
1103 */
1104static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1105 int stream, struct snd_soc_dapm_widget_list **list, int new)
1106{
1107 if (new)
1108 return dpcm_add_paths(fe, stream, list);
1109 else
1110 return dpcm_prune_paths(fe, stream, list);
1111}
1112
1113static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1114{
1115 struct snd_soc_dpcm *dpcm;
1116
1117 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1118 dpcm->be->dpcm[stream].runtime_update =
1119 SND_SOC_DPCM_UPDATE_NO;
1120}
1121
1122static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1123 int stream)
1124{
1125 struct snd_soc_dpcm *dpcm;
1126
1127 /* disable any enabled and non active backends */
1128 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1129
1130 struct snd_soc_pcm_runtime *be = dpcm->be;
1131 struct snd_pcm_substream *be_substream =
1132 snd_soc_dpcm_get_substream(be, stream);
1133
1134 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001135 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001136 stream ? "capture" : "playback",
1137 be->dpcm[stream].state);
1138
1139 if (--be->dpcm[stream].users != 0)
1140 continue;
1141
1142 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1143 continue;
1144
1145 soc_pcm_close(be_substream);
1146 be_substream->runtime = NULL;
1147 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1148 }
1149}
1150
1151static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1152{
1153 struct snd_soc_dpcm *dpcm;
1154 int err, count = 0;
1155
1156 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1157 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1158
1159 struct snd_soc_pcm_runtime *be = dpcm->be;
1160 struct snd_pcm_substream *be_substream =
1161 snd_soc_dpcm_get_substream(be, stream);
1162
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001163 if (!be_substream) {
1164 dev_err(be->dev, "ASoC: no backend %s stream\n",
1165 stream ? "capture" : "playback");
1166 continue;
1167 }
1168
Liam Girdwood01d75842012-04-25 12:12:49 +01001169 /* is this op for this BE ? */
1170 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1171 continue;
1172
1173 /* first time the dpcm is open ? */
1174 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001175 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001176 stream ? "capture" : "playback",
1177 be->dpcm[stream].state);
1178
1179 if (be->dpcm[stream].users++ != 0)
1180 continue;
1181
1182 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1183 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1184 continue;
1185
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001186 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1187 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001188
1189 be_substream->runtime = be->dpcm[stream].runtime;
1190 err = soc_pcm_open(be_substream);
1191 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001192 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001193 be->dpcm[stream].users--;
1194 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001195 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001196 stream ? "capture" : "playback",
1197 be->dpcm[stream].state);
1198
1199 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1200 goto unwind;
1201 }
1202
1203 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1204 count++;
1205 }
1206
1207 return count;
1208
1209unwind:
1210 /* disable any enabled and non active backends */
1211 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1212 struct snd_soc_pcm_runtime *be = dpcm->be;
1213 struct snd_pcm_substream *be_substream =
1214 snd_soc_dpcm_get_substream(be, stream);
1215
1216 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1217 continue;
1218
1219 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001220 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001221 stream ? "capture" : "playback",
1222 be->dpcm[stream].state);
1223
1224 if (--be->dpcm[stream].users != 0)
1225 continue;
1226
1227 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1228 continue;
1229
1230 soc_pcm_close(be_substream);
1231 be_substream->runtime = NULL;
1232 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1233 }
1234
1235 return err;
1236}
1237
Mark Brown45c0a182012-05-09 21:46:27 +01001238static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001239{
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1241 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1242 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1243 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1244
1245 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1246 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1247 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1248 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1249 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1250 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1251 runtime->hw.rates = cpu_dai_drv->playback.rates;
1252 } else {
1253 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1254 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1255 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1256 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1257 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1258 runtime->hw.rates = cpu_dai_drv->capture.rates;
1259 }
1260}
1261
1262static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1263{
1264 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1265 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1266 int stream = fe_substream->stream, ret = 0;
1267
1268 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1269
1270 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1271 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001272 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001273 goto be_err;
1274 }
1275
Liam Girdwood103d84a2012-11-19 14:39:15 +00001276 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001277
1278 /* start the DAI frontend */
1279 ret = soc_pcm_open(fe_substream);
1280 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001281 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001282 goto unwind;
1283 }
1284
1285 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1286
1287 dpcm_set_fe_runtime(fe_substream);
1288 snd_pcm_limit_hw_rates(runtime);
1289
1290 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1291 return 0;
1292
1293unwind:
1294 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1295be_err:
1296 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1297 return ret;
1298}
1299
1300static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1301{
1302 struct snd_soc_dpcm *dpcm;
1303
1304 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1305 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1306
1307 struct snd_soc_pcm_runtime *be = dpcm->be;
1308 struct snd_pcm_substream *be_substream =
1309 snd_soc_dpcm_get_substream(be, stream);
1310
1311 /* is this op for this BE ? */
1312 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1313 continue;
1314
1315 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001316 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001317 stream ? "capture" : "playback",
1318 be->dpcm[stream].state);
1319
1320 if (--be->dpcm[stream].users != 0)
1321 continue;
1322
1323 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1324 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1325 continue;
1326
Liam Girdwood103d84a2012-11-19 14:39:15 +00001327 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001328 dpcm->fe->dai_link->name);
1329
1330 soc_pcm_close(be_substream);
1331 be_substream->runtime = NULL;
1332
1333 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1334 }
1335 return 0;
1336}
1337
1338static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1339{
1340 struct snd_soc_pcm_runtime *fe = substream->private_data;
1341 int stream = substream->stream;
1342
1343 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1344
1345 /* shutdown the BEs */
1346 dpcm_be_dai_shutdown(fe, substream->stream);
1347
Liam Girdwood103d84a2012-11-19 14:39:15 +00001348 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001349
1350 /* now shutdown the frontend */
1351 soc_pcm_close(substream);
1352
1353 /* run the stream event for each BE */
1354 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1355
1356 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1357 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1358 return 0;
1359}
1360
1361static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1362{
1363 struct snd_soc_dpcm *dpcm;
1364
1365 /* only hw_params backends that are either sinks or sources
1366 * to this frontend DAI */
1367 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1368
1369 struct snd_soc_pcm_runtime *be = dpcm->be;
1370 struct snd_pcm_substream *be_substream =
1371 snd_soc_dpcm_get_substream(be, stream);
1372
1373 /* is this op for this BE ? */
1374 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1375 continue;
1376
1377 /* only free hw when no longer used - check all FEs */
1378 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1379 continue;
1380
1381 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1382 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1383 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001384 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001385 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1386 continue;
1387
Liam Girdwood103d84a2012-11-19 14:39:15 +00001388 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001389 dpcm->fe->dai_link->name);
1390
1391 soc_pcm_hw_free(be_substream);
1392
1393 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1394 }
1395
1396 return 0;
1397}
1398
Mark Brown45c0a182012-05-09 21:46:27 +01001399static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001400{
1401 struct snd_soc_pcm_runtime *fe = substream->private_data;
1402 int err, stream = substream->stream;
1403
1404 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1405 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1406
Liam Girdwood103d84a2012-11-19 14:39:15 +00001407 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001408
1409 /* call hw_free on the frontend */
1410 err = soc_pcm_hw_free(substream);
1411 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001412 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001413 fe->dai_link->name);
1414
1415 /* only hw_params backends that are either sinks or sources
1416 * to this frontend DAI */
1417 err = dpcm_be_dai_hw_free(fe, stream);
1418
1419 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1420 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1421
1422 mutex_unlock(&fe->card->mutex);
1423 return 0;
1424}
1425
1426static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1427{
1428 struct snd_soc_dpcm *dpcm;
1429 int ret;
1430
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 allow hw_params() if no connected FEs are running */
1442 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1443 continue;
1444
1445 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1446 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1447 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1448 continue;
1449
Liam Girdwood103d84a2012-11-19 14:39:15 +00001450 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001451 dpcm->fe->dai_link->name);
1452
1453 /* copy params for each dpcm */
1454 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1455 sizeof(struct snd_pcm_hw_params));
1456
1457 /* perform any hw_params fixups */
1458 if (be->dai_link->be_hw_params_fixup) {
1459 ret = be->dai_link->be_hw_params_fixup(be,
1460 &dpcm->hw_params);
1461 if (ret < 0) {
1462 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001463 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001464 ret);
1465 goto unwind;
1466 }
1467 }
1468
1469 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1470 if (ret < 0) {
1471 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001472 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001473 goto unwind;
1474 }
1475
1476 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1477 }
1478 return 0;
1479
1480unwind:
1481 /* disable any enabled and non active backends */
1482 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1483 struct snd_soc_pcm_runtime *be = dpcm->be;
1484 struct snd_pcm_substream *be_substream =
1485 snd_soc_dpcm_get_substream(be, stream);
1486
1487 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1488 continue;
1489
1490 /* only allow hw_free() if no connected FEs are running */
1491 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1492 continue;
1493
1494 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1495 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1496 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1497 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1498 continue;
1499
1500 soc_pcm_hw_free(be_substream);
1501 }
1502
1503 return ret;
1504}
1505
Mark Brown45c0a182012-05-09 21:46:27 +01001506static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1507 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001508{
1509 struct snd_soc_pcm_runtime *fe = substream->private_data;
1510 int ret, stream = substream->stream;
1511
1512 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1513 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1514
1515 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1516 sizeof(struct snd_pcm_hw_params));
1517 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1518 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001519 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001520 goto out;
1521 }
1522
Liam Girdwood103d84a2012-11-19 14:39:15 +00001523 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001524 fe->dai_link->name, params_rate(params),
1525 params_channels(params), params_format(params));
1526
1527 /* call hw_params on the frontend */
1528 ret = soc_pcm_hw_params(substream, params);
1529 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001530 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001531 dpcm_be_dai_hw_free(fe, stream);
1532 } else
1533 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1534
1535out:
1536 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1537 mutex_unlock(&fe->card->mutex);
1538 return ret;
1539}
1540
1541static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1542 struct snd_pcm_substream *substream, int cmd)
1543{
1544 int ret;
1545
Liam Girdwood103d84a2012-11-19 14:39:15 +00001546 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001547 dpcm->fe->dai_link->name, cmd);
1548
1549 ret = soc_pcm_trigger(substream, cmd);
1550 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001551 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001552
1553 return ret;
1554}
1555
Mark Brown45c0a182012-05-09 21:46:27 +01001556static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1557 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001558{
1559 struct snd_soc_dpcm *dpcm;
1560 int ret = 0;
1561
1562 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1563
1564 struct snd_soc_pcm_runtime *be = dpcm->be;
1565 struct snd_pcm_substream *be_substream =
1566 snd_soc_dpcm_get_substream(be, stream);
1567
1568 /* is this op for this BE ? */
1569 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1570 continue;
1571
1572 switch (cmd) {
1573 case SNDRV_PCM_TRIGGER_START:
1574 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1575 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1576 continue;
1577
1578 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1579 if (ret)
1580 return ret;
1581
1582 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1583 break;
1584 case SNDRV_PCM_TRIGGER_RESUME:
1585 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1586 continue;
1587
1588 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1589 if (ret)
1590 return ret;
1591
1592 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1593 break;
1594 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1595 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1596 continue;
1597
1598 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1599 if (ret)
1600 return ret;
1601
1602 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1603 break;
1604 case SNDRV_PCM_TRIGGER_STOP:
1605 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1606 continue;
1607
1608 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1609 continue;
1610
1611 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1612 if (ret)
1613 return ret;
1614
1615 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1616 break;
1617 case SNDRV_PCM_TRIGGER_SUSPEND:
1618 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1619 continue;
1620
1621 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1622 continue;
1623
1624 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1625 if (ret)
1626 return ret;
1627
1628 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1629 break;
1630 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1631 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1632 continue;
1633
1634 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1635 continue;
1636
1637 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1638 if (ret)
1639 return ret;
1640
1641 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1642 break;
1643 }
1644 }
1645
1646 return ret;
1647}
1648EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1649
Mark Brown45c0a182012-05-09 21:46:27 +01001650static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001651{
1652 struct snd_soc_pcm_runtime *fe = substream->private_data;
1653 int stream = substream->stream, ret;
1654 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1655
1656 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1657
1658 switch (trigger) {
1659 case SND_SOC_DPCM_TRIGGER_PRE:
1660 /* call trigger on the frontend before the backend. */
1661
Liam Girdwood103d84a2012-11-19 14:39:15 +00001662 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001663 fe->dai_link->name, cmd);
1664
1665 ret = soc_pcm_trigger(substream, cmd);
1666 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001667 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001668 goto out;
1669 }
1670
1671 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1672 break;
1673 case SND_SOC_DPCM_TRIGGER_POST:
1674 /* call trigger on the frontend after the backend. */
1675
1676 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1677 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001678 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001679 goto out;
1680 }
1681
Liam Girdwood103d84a2012-11-19 14:39:15 +00001682 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001683 fe->dai_link->name, cmd);
1684
1685 ret = soc_pcm_trigger(substream, cmd);
1686 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001687 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1688 /* bespoke trigger() - handles both FE and BEs */
1689
Liam Girdwood103d84a2012-11-19 14:39:15 +00001690 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001691 fe->dai_link->name, cmd);
1692
1693 ret = soc_pcm_bespoke_trigger(substream, cmd);
1694 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001695 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001696 goto out;
1697 }
1698 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001699 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001700 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001701 fe->dai_link->name);
1702 ret = -EINVAL;
1703 goto out;
1704 }
1705
1706 switch (cmd) {
1707 case SNDRV_PCM_TRIGGER_START:
1708 case SNDRV_PCM_TRIGGER_RESUME:
1709 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1710 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1711 break;
1712 case SNDRV_PCM_TRIGGER_STOP:
1713 case SNDRV_PCM_TRIGGER_SUSPEND:
1714 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1715 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1716 break;
1717 }
1718
1719out:
1720 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1721 return ret;
1722}
1723
1724static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1725{
1726 struct snd_soc_dpcm *dpcm;
1727 int ret = 0;
1728
1729 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1730
1731 struct snd_soc_pcm_runtime *be = dpcm->be;
1732 struct snd_pcm_substream *be_substream =
1733 snd_soc_dpcm_get_substream(be, stream);
1734
1735 /* is this op for this BE ? */
1736 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1737 continue;
1738
1739 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1740 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1741 continue;
1742
Liam Girdwood103d84a2012-11-19 14:39:15 +00001743 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001744 dpcm->fe->dai_link->name);
1745
1746 ret = soc_pcm_prepare(be_substream);
1747 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001748 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001749 ret);
1750 break;
1751 }
1752
1753 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1754 }
1755 return ret;
1756}
1757
Mark Brown45c0a182012-05-09 21:46:27 +01001758static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001759{
1760 struct snd_soc_pcm_runtime *fe = substream->private_data;
1761 int stream = substream->stream, ret = 0;
1762
1763 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1764
Liam Girdwood103d84a2012-11-19 14:39:15 +00001765 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001766
1767 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1768
1769 /* there is no point preparing this FE if there are no BEs */
1770 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001771 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001772 fe->dai_link->name);
1773 ret = -EINVAL;
1774 goto out;
1775 }
1776
1777 ret = dpcm_be_dai_prepare(fe, substream->stream);
1778 if (ret < 0)
1779 goto out;
1780
1781 /* call prepare on the frontend */
1782 ret = soc_pcm_prepare(substream);
1783 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001784 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001785 fe->dai_link->name);
1786 goto out;
1787 }
1788
1789 /* run the stream event for each BE */
1790 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1791 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1792
1793out:
1794 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1795 mutex_unlock(&fe->card->mutex);
1796
1797 return ret;
1798}
1799
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001800static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1801 unsigned int cmd, void *arg)
1802{
1803 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1804 struct snd_soc_platform *platform = rtd->platform;
1805
Mark Brownc5914b02013-10-30 17:47:39 -07001806 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001807 return platform->driver->ops->ioctl(substream, cmd, arg);
1808 return snd_pcm_lib_ioctl(substream, cmd, arg);
1809}
1810
Liam Girdwood618dae12012-04-25 12:12:51 +01001811static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1812{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001813 struct snd_pcm_substream *substream =
1814 snd_soc_dpcm_get_substream(fe, stream);
1815 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001816 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001817
Liam Girdwood103d84a2012-11-19 14:39:15 +00001818 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001819 stream ? "capture" : "playback", fe->dai_link->name);
1820
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001821 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1822 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001823 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001824 fe->dai_link->name);
1825
1826 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1827 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001828 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001829 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001830 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001831 fe->dai_link->name);
1832
1833 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1834 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001836 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001837
1838 err = dpcm_be_dai_hw_free(fe, stream);
1839 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001840 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001841
1842 err = dpcm_be_dai_shutdown(fe, stream);
1843 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001844 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001845
1846 /* run the stream event for each BE */
1847 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1848
1849 return 0;
1850}
1851
1852static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1853{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001854 struct snd_pcm_substream *substream =
1855 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001856 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001857 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001858 int ret;
1859
Liam Girdwood103d84a2012-11-19 14:39:15 +00001860 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001861 stream ? "capture" : "playback", fe->dai_link->name);
1862
1863 /* Only start the BE if the FE is ready */
1864 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1865 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1866 return -EINVAL;
1867
1868 /* startup must always be called for new BEs */
1869 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001870 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001871 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001872
1873 /* keep going if FE state is > open */
1874 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1875 return 0;
1876
1877 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001878 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001879 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001880
1881 /* keep going if FE state is > hw_params */
1882 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1883 return 0;
1884
1885
1886 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001887 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001888 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001889
1890 /* run the stream event for each BE */
1891 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1892
1893 /* keep going if FE state is > prepare */
1894 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1895 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1896 return 0;
1897
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001898 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1899 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001900 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001901 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001902
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001903 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1904 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001905 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001906 goto hw_free;
1907 }
1908 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001909 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001910 fe->dai_link->name);
1911
1912 ret = dpcm_be_dai_trigger(fe, stream,
1913 SNDRV_PCM_TRIGGER_START);
1914 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001915 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001916 goto hw_free;
1917 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001918 }
1919
1920 return 0;
1921
1922hw_free:
1923 dpcm_be_dai_hw_free(fe, stream);
1924close:
1925 dpcm_be_dai_shutdown(fe, stream);
1926disconnect:
1927 /* disconnect any non started BEs */
1928 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1929 struct snd_soc_pcm_runtime *be = dpcm->be;
1930 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1931 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1932 }
1933
1934 return ret;
1935}
1936
1937static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1938{
1939 int ret;
1940
1941 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1942 ret = dpcm_run_update_startup(fe, stream);
1943 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001944 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001945 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1946
1947 return ret;
1948}
1949
1950static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1951{
1952 int ret;
1953
1954 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1955 ret = dpcm_run_update_shutdown(fe, stream);
1956 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001957 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001958 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1959
1960 return ret;
1961}
1962
1963/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1964 * any DAI links.
1965 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001966int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001967{
Liam Girdwood618dae12012-04-25 12:12:51 +01001968 int i, old, new, paths;
1969
Liam Girdwood618dae12012-04-25 12:12:51 +01001970 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1971 for (i = 0; i < card->num_rtd; i++) {
1972 struct snd_soc_dapm_widget_list *list;
1973 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1974
1975 /* make sure link is FE */
1976 if (!fe->dai_link->dynamic)
1977 continue;
1978
1979 /* only check active links */
1980 if (!fe->cpu_dai->active)
1981 continue;
1982
1983 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001984 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001985 fe->dai_link->name);
1986
1987 /* skip if FE doesn't have playback capability */
1988 if (!fe->cpu_dai->driver->playback.channels_min)
1989 goto capture;
1990
1991 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1992 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001993 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001994 fe->dai_link->name, "playback");
1995 mutex_unlock(&card->mutex);
1996 return paths;
1997 }
1998
1999 /* update any new playback paths */
2000 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2001 if (new) {
2002 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2003 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2004 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2005 }
2006
2007 /* update any old playback paths */
2008 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2009 if (old) {
2010 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2011 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2012 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2013 }
2014
2015capture:
2016 /* skip if FE doesn't have capture capability */
2017 if (!fe->cpu_dai->driver->capture.channels_min)
2018 continue;
2019
2020 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2021 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002022 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002023 fe->dai_link->name, "capture");
2024 mutex_unlock(&card->mutex);
2025 return paths;
2026 }
2027
2028 /* update any new capture paths */
2029 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2030 if (new) {
2031 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2032 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2033 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2034 }
2035
2036 /* update any old capture paths */
2037 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2038 if (old) {
2039 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2040 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2041 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2042 }
2043
2044 dpcm_path_put(&list);
2045 }
2046
2047 mutex_unlock(&card->mutex);
2048 return 0;
2049}
Liam Girdwood01d75842012-04-25 12:12:49 +01002050int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2051{
2052 struct snd_soc_dpcm *dpcm;
2053 struct list_head *clients =
2054 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2055
2056 list_for_each_entry(dpcm, clients, list_be) {
2057
2058 struct snd_soc_pcm_runtime *be = dpcm->be;
2059 struct snd_soc_dai *dai = be->codec_dai;
2060 struct snd_soc_dai_driver *drv = dai->driver;
2061
2062 if (be->dai_link->ignore_suspend)
2063 continue;
2064
Liam Girdwood103d84a2012-11-19 14:39:15 +00002065 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002066
Mark Brownc5914b02013-10-30 17:47:39 -07002067 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2068 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002069 }
2070
2071 return 0;
2072}
2073
Mark Brown45c0a182012-05-09 21:46:27 +01002074static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002075{
2076 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2077 struct snd_soc_dpcm *dpcm;
2078 struct snd_soc_dapm_widget_list *list;
2079 int ret;
2080 int stream = fe_substream->stream;
2081
2082 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2083 fe->dpcm[stream].runtime = fe_substream->runtime;
2084
2085 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002086 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002087 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002088 }
2089
2090 /* calculate valid and active FE <-> BE dpcms */
2091 dpcm_process_paths(fe, stream, &list, 1);
2092
2093 ret = dpcm_fe_dai_startup(fe_substream);
2094 if (ret < 0) {
2095 /* clean up all links */
2096 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2097 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2098
2099 dpcm_be_disconnect(fe, stream);
2100 fe->dpcm[stream].runtime = NULL;
2101 }
2102
2103 dpcm_clear_pending_state(fe, stream);
2104 dpcm_path_put(&list);
2105 mutex_unlock(&fe->card->mutex);
2106 return ret;
2107}
2108
Mark Brown45c0a182012-05-09 21:46:27 +01002109static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002110{
2111 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2112 struct snd_soc_dpcm *dpcm;
2113 int stream = fe_substream->stream, ret;
2114
2115 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2116 ret = dpcm_fe_dai_shutdown(fe_substream);
2117
2118 /* mark FE's links ready to prune */
2119 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2120 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2121
2122 dpcm_be_disconnect(fe, stream);
2123
2124 fe->dpcm[stream].runtime = NULL;
2125 mutex_unlock(&fe->card->mutex);
2126 return ret;
2127}
2128
Liam Girdwoodddee6272011-06-09 14:45:53 +01002129/* create a new pcm */
2130int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2131{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002132 struct snd_soc_platform *platform = rtd->platform;
2133 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2134 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2135 struct snd_pcm *pcm;
2136 char new_name[64];
2137 int ret = 0, playback = 0, capture = 0;
2138
Liam Girdwood01d75842012-04-25 12:12:49 +01002139 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2140 if (cpu_dai->driver->playback.channels_min)
2141 playback = 1;
2142 if (cpu_dai->driver->capture.channels_min)
2143 capture = 1;
2144 } else {
Mark Brown05679092013-06-01 23:13:53 +01002145 if (codec_dai->driver->playback.channels_min &&
2146 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002147 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002148 if (codec_dai->driver->capture.channels_min &&
2149 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002150 capture = 1;
2151 }
Sangsu Parka5002312012-01-02 17:15:10 +09002152
Fabio Estevamd6bead02013-08-29 10:32:13 -03002153 if (rtd->dai_link->playback_only) {
2154 playback = 1;
2155 capture = 0;
2156 }
2157
2158 if (rtd->dai_link->capture_only) {
2159 playback = 0;
2160 capture = 1;
2161 }
2162
Liam Girdwood01d75842012-04-25 12:12:49 +01002163 /* create the PCM */
2164 if (rtd->dai_link->no_pcm) {
2165 snprintf(new_name, sizeof(new_name), "(%s)",
2166 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002167
Liam Girdwood01d75842012-04-25 12:12:49 +01002168 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2169 playback, capture, &pcm);
2170 } else {
2171 if (rtd->dai_link->dynamic)
2172 snprintf(new_name, sizeof(new_name), "%s (*)",
2173 rtd->dai_link->stream_name);
2174 else
2175 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2176 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002177
Liam Girdwood01d75842012-04-25 12:12:49 +01002178 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2179 capture, &pcm);
2180 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002181 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002182 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002183 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002184 return ret;
2185 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002186 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002187
2188 /* DAPM dai link stream work */
2189 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2190
2191 rtd->pcm = pcm;
2192 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002193
2194 if (rtd->dai_link->no_pcm) {
2195 if (playback)
2196 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2197 if (capture)
2198 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2199 goto out;
2200 }
2201
2202 /* ASoC PCM operations */
2203 if (rtd->dai_link->dynamic) {
2204 rtd->ops.open = dpcm_fe_dai_open;
2205 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2206 rtd->ops.prepare = dpcm_fe_dai_prepare;
2207 rtd->ops.trigger = dpcm_fe_dai_trigger;
2208 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2209 rtd->ops.close = dpcm_fe_dai_close;
2210 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002211 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002212 } else {
2213 rtd->ops.open = soc_pcm_open;
2214 rtd->ops.hw_params = soc_pcm_hw_params;
2215 rtd->ops.prepare = soc_pcm_prepare;
2216 rtd->ops.trigger = soc_pcm_trigger;
2217 rtd->ops.hw_free = soc_pcm_hw_free;
2218 rtd->ops.close = soc_pcm_close;
2219 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002220 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002221 }
2222
Liam Girdwoodddee6272011-06-09 14:45:53 +01002223 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002224 rtd->ops.ack = platform->driver->ops->ack;
2225 rtd->ops.copy = platform->driver->ops->copy;
2226 rtd->ops.silence = platform->driver->ops->silence;
2227 rtd->ops.page = platform->driver->ops->page;
2228 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002229 }
2230
2231 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002232 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002233
2234 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002235 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002236
2237 if (platform->driver->pcm_new) {
2238 ret = platform->driver->pcm_new(rtd);
2239 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002240 dev_err(platform->dev,
2241 "ASoC: pcm constructor failed: %d\n",
2242 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002243 return ret;
2244 }
2245 }
2246
2247 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002248out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002249 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002250 cpu_dai->name);
2251 return ret;
2252}
Liam Girdwood01d75842012-04-25 12:12:49 +01002253
2254/* is the current PCM operation for this FE ? */
2255int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2256{
2257 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2258 return 1;
2259 return 0;
2260}
2261EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2262
2263/* is the current PCM operation for this BE ? */
2264int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2265 struct snd_soc_pcm_runtime *be, int stream)
2266{
2267 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2268 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2269 be->dpcm[stream].runtime_update))
2270 return 1;
2271 return 0;
2272}
2273EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2274
2275/* get the substream for this BE */
2276struct snd_pcm_substream *
2277 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2278{
2279 return be->pcm->streams[stream].substream;
2280}
2281EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2282
2283/* get the BE runtime state */
2284enum snd_soc_dpcm_state
2285 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2286{
2287 return be->dpcm[stream].state;
2288}
2289EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2290
2291/* set the BE runtime state */
2292void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2293 int stream, enum snd_soc_dpcm_state state)
2294{
2295 be->dpcm[stream].state = state;
2296}
2297EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2298
2299/*
2300 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2301 * are not running, paused or suspended for the specified stream direction.
2302 */
2303int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2304 struct snd_soc_pcm_runtime *be, int stream)
2305{
2306 struct snd_soc_dpcm *dpcm;
2307 int state;
2308
2309 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2310
2311 if (dpcm->fe == fe)
2312 continue;
2313
2314 state = dpcm->fe->dpcm[stream].state;
2315 if (state == SND_SOC_DPCM_STATE_START ||
2316 state == SND_SOC_DPCM_STATE_PAUSED ||
2317 state == SND_SOC_DPCM_STATE_SUSPEND)
2318 return 0;
2319 }
2320
2321 /* it's safe to free/stop this BE DAI */
2322 return 1;
2323}
2324EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2325
2326/*
2327 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2328 * running, paused or suspended for the specified stream direction.
2329 */
2330int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2331 struct snd_soc_pcm_runtime *be, int stream)
2332{
2333 struct snd_soc_dpcm *dpcm;
2334 int state;
2335
2336 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2337
2338 if (dpcm->fe == fe)
2339 continue;
2340
2341 state = dpcm->fe->dpcm[stream].state;
2342 if (state == SND_SOC_DPCM_STATE_START ||
2343 state == SND_SOC_DPCM_STATE_PAUSED ||
2344 state == SND_SOC_DPCM_STATE_SUSPEND ||
2345 state == SND_SOC_DPCM_STATE_PREPARE)
2346 return 0;
2347 }
2348
2349 /* it's safe to change hw_params */
2350 return 1;
2351}
2352EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002353
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002354int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2355 int cmd, struct snd_soc_platform *platform)
2356{
Mark Brownc5914b02013-10-30 17:47:39 -07002357 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002358 return platform->driver->ops->trigger(substream, cmd);
2359 return 0;
2360}
2361EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2362
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002363#ifdef CONFIG_DEBUG_FS
2364static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2365{
2366 switch (state) {
2367 case SND_SOC_DPCM_STATE_NEW:
2368 return "new";
2369 case SND_SOC_DPCM_STATE_OPEN:
2370 return "open";
2371 case SND_SOC_DPCM_STATE_HW_PARAMS:
2372 return "hw_params";
2373 case SND_SOC_DPCM_STATE_PREPARE:
2374 return "prepare";
2375 case SND_SOC_DPCM_STATE_START:
2376 return "start";
2377 case SND_SOC_DPCM_STATE_STOP:
2378 return "stop";
2379 case SND_SOC_DPCM_STATE_SUSPEND:
2380 return "suspend";
2381 case SND_SOC_DPCM_STATE_PAUSED:
2382 return "paused";
2383 case SND_SOC_DPCM_STATE_HW_FREE:
2384 return "hw_free";
2385 case SND_SOC_DPCM_STATE_CLOSE:
2386 return "close";
2387 }
2388
2389 return "unknown";
2390}
2391
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002392static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2393 int stream, char *buf, size_t size)
2394{
2395 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2396 struct snd_soc_dpcm *dpcm;
2397 ssize_t offset = 0;
2398
2399 /* FE state */
2400 offset += snprintf(buf + offset, size - offset,
2401 "[%s - %s]\n", fe->dai_link->name,
2402 stream ? "Capture" : "Playback");
2403
2404 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2405 dpcm_state_string(fe->dpcm[stream].state));
2406
2407 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2408 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2409 offset += snprintf(buf + offset, size - offset,
2410 "Hardware Params: "
2411 "Format = %s, Channels = %d, Rate = %d\n",
2412 snd_pcm_format_name(params_format(params)),
2413 params_channels(params),
2414 params_rate(params));
2415
2416 /* BEs state */
2417 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2418
2419 if (list_empty(&fe->dpcm[stream].be_clients)) {
2420 offset += snprintf(buf + offset, size - offset,
2421 " No active DSP links\n");
2422 goto out;
2423 }
2424
2425 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2426 struct snd_soc_pcm_runtime *be = dpcm->be;
2427 params = &dpcm->hw_params;
2428
2429 offset += snprintf(buf + offset, size - offset,
2430 "- %s\n", be->dai_link->name);
2431
2432 offset += snprintf(buf + offset, size - offset,
2433 " State: %s\n",
2434 dpcm_state_string(be->dpcm[stream].state));
2435
2436 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2437 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2438 offset += snprintf(buf + offset, size - offset,
2439 " Hardware Params: "
2440 "Format = %s, Channels = %d, Rate = %d\n",
2441 snd_pcm_format_name(params_format(params)),
2442 params_channels(params),
2443 params_rate(params));
2444 }
2445
2446out:
2447 return offset;
2448}
2449
2450static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2451 size_t count, loff_t *ppos)
2452{
2453 struct snd_soc_pcm_runtime *fe = file->private_data;
2454 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2455 char *buf;
2456
2457 buf = kmalloc(out_count, GFP_KERNEL);
2458 if (!buf)
2459 return -ENOMEM;
2460
2461 if (fe->cpu_dai->driver->playback.channels_min)
2462 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2463 buf + offset, out_count - offset);
2464
2465 if (fe->cpu_dai->driver->capture.channels_min)
2466 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2467 buf + offset, out_count - offset);
2468
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002469 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002470
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002471 kfree(buf);
2472 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002473}
2474
2475static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002476 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002477 .read = dpcm_state_read_file,
2478 .llseek = default_llseek,
2479};
2480
2481int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2482{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002483 if (!rtd->dai_link)
2484 return 0;
2485
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002486 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2487 rtd->card->debugfs_card_root);
2488 if (!rtd->debugfs_dpcm_root) {
2489 dev_dbg(rtd->dev,
2490 "ASoC: Failed to create dpcm debugfs directory %s\n",
2491 rtd->dai_link->name);
2492 return -EINVAL;
2493 }
2494
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002495 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002496 rtd->debugfs_dpcm_root,
2497 rtd, &dpcm_state_fops);
2498
2499 return 0;
2500}
2501#endif