blob: f3592f142832f155c11f4efa62667a78cfaaad6f [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 }
135 }
136
137 return 0;
138}
139
140static 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 Clausenbd477c32013-05-14 11:05:31 +0200233static void soc_pcm_init_runtime_hw(struct snd_pcm_hardware *hw,
234 struct snd_soc_pcm_stream *codec_stream,
235 struct snd_soc_pcm_stream *cpu_stream)
236{
237 hw->rate_min = max(codec_stream->rate_min, cpu_stream->rate_min);
238 hw->rate_max = max(codec_stream->rate_max, cpu_stream->rate_max);
239 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;
251}
252
Mark Brown58ba9b22012-01-16 18:38:51 +0000253/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100254 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
255 * then initialized and any private data can be allocated. This also calls
256 * startup for the cpu DAI, platform, machine and codec DAI.
257 */
258static int soc_pcm_open(struct snd_pcm_substream *substream)
259{
260 struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 struct snd_pcm_runtime *runtime = substream->runtime;
262 struct snd_soc_platform *platform = rtd->platform;
263 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
264 struct snd_soc_dai *codec_dai = rtd->codec_dai;
265 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
266 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
267 int ret = 0;
268
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800269 pinctrl_pm_select_default_state(cpu_dai->dev);
270 pinctrl_pm_select_default_state(codec_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000271 pm_runtime_get_sync(cpu_dai->dev);
272 pm_runtime_get_sync(codec_dai->dev);
273 pm_runtime_get_sync(platform->dev);
274
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100275 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100276
277 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700278 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100279 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
280 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000281 dev_err(cpu_dai->dev, "ASoC: can't open interface"
282 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100283 goto out;
284 }
285 }
286
287 if (platform->driver->ops && platform->driver->ops->open) {
288 ret = platform->driver->ops->open(substream);
289 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000290 dev_err(platform->dev, "ASoC: can't open platform"
291 " %s: %d\n", platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100292 goto platform_err;
293 }
294 }
295
Mark Brownc5914b02013-10-30 17:47:39 -0700296 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100297 ret = codec_dai->driver->ops->startup(substream, codec_dai);
298 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000299 dev_err(codec_dai->dev, "ASoC: can't open codec"
300 " %s: %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100301 goto codec_dai_err;
302 }
303 }
304
305 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
306 ret = rtd->dai_link->ops->startup(substream);
307 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000308 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000309 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100310 goto machine_err;
311 }
312 }
313
Liam Girdwood01d75842012-04-25 12:12:49 +0100314 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
315 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
316 goto dynamic;
317
Liam Girdwoodddee6272011-06-09 14:45:53 +0100318 /* Check that the codec and cpu DAIs are compatible */
319 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200320 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->playback,
321 &cpu_dai_drv->playback);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100322 } else {
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200323 soc_pcm_init_runtime_hw(&runtime->hw, &codec_dai_drv->capture,
324 &cpu_dai_drv->capture);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100325 }
326
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100327 if (soc_pcm_has_symmetry(substream))
328 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
329
Liam Girdwoodddee6272011-06-09 14:45:53 +0100330 ret = -EINVAL;
331 snd_pcm_limit_hw_rates(runtime);
332 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000333 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100334 codec_dai->name, cpu_dai->name);
335 goto config_err;
336 }
337 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000338 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100339 codec_dai->name, cpu_dai->name);
340 goto config_err;
341 }
342 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
343 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000344 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100345 codec_dai->name, cpu_dai->name);
346 goto config_err;
347 }
348
Mark Brown58ba9b22012-01-16 18:38:51 +0000349 soc_pcm_apply_msb(substream, codec_dai);
350 soc_pcm_apply_msb(substream, cpu_dai);
351
Liam Girdwoodddee6272011-06-09 14:45:53 +0100352 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800353 if (cpu_dai->active) {
354 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
355 if (ret != 0)
356 goto config_err;
357 }
358
359 if (codec_dai->active) {
360 ret = soc_pcm_apply_symmetry(substream, codec_dai);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100361 if (ret != 0)
362 goto config_err;
363 }
364
Liam Girdwood103d84a2012-11-19 14:39:15 +0000365 pr_debug("ASoC: %s <-> %s info:\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100366 codec_dai->name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000367 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
368 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100369 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000370 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100371 runtime->hw.rate_max);
372
Liam Girdwood01d75842012-04-25 12:12:49 +0100373dynamic:
Liam Girdwoodddee6272011-06-09 14:45:53 +0100374 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 cpu_dai->playback_active++;
376 codec_dai->playback_active++;
377 } else {
378 cpu_dai->capture_active++;
379 codec_dai->capture_active++;
380 }
381 cpu_dai->active++;
382 codec_dai->active++;
383 rtd->codec->active++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100384 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100385 return 0;
386
387config_err:
388 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
389 rtd->dai_link->ops->shutdown(substream);
390
391machine_err:
392 if (codec_dai->driver->ops->shutdown)
393 codec_dai->driver->ops->shutdown(substream, codec_dai);
394
395codec_dai_err:
396 if (platform->driver->ops && platform->driver->ops->close)
397 platform->driver->ops->close(substream);
398
399platform_err:
400 if (cpu_dai->driver->ops->shutdown)
401 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
402out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100403 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000404
405 pm_runtime_put(platform->dev);
406 pm_runtime_put(codec_dai->dev);
407 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800408 if (!codec_dai->active)
409 pinctrl_pm_select_sleep_state(codec_dai->dev);
410 if (!cpu_dai->active)
411 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000412
Liam Girdwoodddee6272011-06-09 14:45:53 +0100413 return ret;
414}
415
416/*
417 * Power down the audio subsystem pmdown_time msecs after close is called.
418 * This is to ensure there are no pops or clicks in between any music tracks
419 * due to DAPM power cycling.
420 */
421static void close_delayed_work(struct work_struct *work)
422{
423 struct snd_soc_pcm_runtime *rtd =
424 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
425 struct snd_soc_dai *codec_dai = rtd->codec_dai;
426
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100427 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100428
Liam Girdwood103d84a2012-11-19 14:39:15 +0000429 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100430 codec_dai->driver->playback.stream_name,
431 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600432 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100433
434 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600435 if (rtd->pop_wait == 1) {
436 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800437 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000438 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100439 }
440
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100441 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100442}
443
444/*
445 * Called by ALSA when a PCM substream is closed. Private data can be
446 * freed here. The cpu DAI, codec DAI, machine and platform are also
447 * shutdown.
448 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100449static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100450{
451 struct snd_soc_pcm_runtime *rtd = substream->private_data;
452 struct snd_soc_platform *platform = rtd->platform;
453 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
454 struct snd_soc_dai *codec_dai = rtd->codec_dai;
455 struct snd_soc_codec *codec = rtd->codec;
456
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100457 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100458
459 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
460 cpu_dai->playback_active--;
461 codec_dai->playback_active--;
462 } else {
463 cpu_dai->capture_active--;
464 codec_dai->capture_active--;
465 }
466
467 cpu_dai->active--;
468 codec_dai->active--;
469 codec->active--;
470
471 /* Muting the DAC suppresses artifacts caused during digital
472 * shutdown, for example from stopping clocks.
473 */
Mark Brownda183962013-02-06 15:44:07 +0000474 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100475
476 if (cpu_dai->driver->ops->shutdown)
477 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
478
479 if (codec_dai->driver->ops->shutdown)
480 codec_dai->driver->ops->shutdown(substream, codec_dai);
481
482 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
483 rtd->dai_link->ops->shutdown(substream);
484
485 if (platform->driver->ops && platform->driver->ops->close)
486 platform->driver->ops->close(substream);
487 cpu_dai->runtime = NULL;
488
489 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000490 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200491 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300492 /* powered down playback stream now */
493 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800494 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800495 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300496 } else {
497 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600498 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100499 queue_delayed_work(system_power_efficient_wq,
500 &rtd->delayed_work,
501 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300502 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100503 } else {
504 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800505 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000506 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100507 }
508
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100509 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000510
511 pm_runtime_put(platform->dev);
512 pm_runtime_put(codec_dai->dev);
513 pm_runtime_put(cpu_dai->dev);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800514 if (!codec_dai->active)
515 pinctrl_pm_select_sleep_state(codec_dai->dev);
516 if (!cpu_dai->active)
517 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000518
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519 return 0;
520}
521
522/*
523 * Called by ALSA when the PCM substream is prepared, can set format, sample
524 * rate, etc. This function is non atomic and can be called multiple times,
525 * it can refer to the runtime info.
526 */
527static int soc_pcm_prepare(struct snd_pcm_substream *substream)
528{
529 struct snd_soc_pcm_runtime *rtd = substream->private_data;
530 struct snd_soc_platform *platform = rtd->platform;
531 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
532 struct snd_soc_dai *codec_dai = rtd->codec_dai;
533 int ret = 0;
534
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100535 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100536
537 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
538 ret = rtd->dai_link->ops->prepare(substream);
539 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000540 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
541 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100542 goto out;
543 }
544 }
545
546 if (platform->driver->ops && platform->driver->ops->prepare) {
547 ret = platform->driver->ops->prepare(substream);
548 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000549 dev_err(platform->dev, "ASoC: platform prepare error:"
550 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100551 goto out;
552 }
553 }
554
Mark Brownc5914b02013-10-30 17:47:39 -0700555 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100556 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
557 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000558 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000559 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 goto out;
561 }
562 }
563
Mark Brownc5914b02013-10-30 17:47:39 -0700564 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100565 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
566 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000567 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000568 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100569 goto out;
570 }
571 }
572
573 /* cancel any delayed stream shutdown that is pending */
574 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600575 rtd->pop_wait) {
576 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577 cancel_delayed_work(&rtd->delayed_work);
578 }
579
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000580 snd_soc_dapm_stream_event(rtd, substream->stream,
581 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100582
Mark Brownda183962013-02-06 15:44:07 +0000583 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100584
585out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100586 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100587 return ret;
588}
589
590/*
591 * Called by ALSA when the hardware params are set by application. This
592 * function can also be called multiple times and can allocate buffers
593 * (using snd_pcm_lib_* ). It's non-atomic.
594 */
595static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
596 struct snd_pcm_hw_params *params)
597{
598 struct snd_soc_pcm_runtime *rtd = substream->private_data;
599 struct snd_soc_platform *platform = rtd->platform;
600 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
601 struct snd_soc_dai *codec_dai = rtd->codec_dai;
602 int ret = 0;
603
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100604 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100605
Nicolin Chen3635bf02013-11-13 18:56:24 +0800606 ret = soc_pcm_params_symmetry(substream, params);
607 if (ret)
608 goto out;
609
Liam Girdwoodddee6272011-06-09 14:45:53 +0100610 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
611 ret = rtd->dai_link->ops->hw_params(substream, params);
612 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000613 dev_err(rtd->card->dev, "ASoC: machine hw_params"
614 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100615 goto out;
616 }
617 }
618
Mark Brownc5914b02013-10-30 17:47:39 -0700619 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100620 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
621 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000622 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
623 " %d\n", codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100624 goto codec_err;
625 }
626 }
627
Mark Brownc5914b02013-10-30 17:47:39 -0700628 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100629 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
630 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000631 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000632 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100633 goto interface_err;
634 }
635 }
636
637 if (platform->driver->ops && platform->driver->ops->hw_params) {
638 ret = platform->driver->ops->hw_params(substream, params);
639 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000640 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000641 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100642 goto platform_err;
643 }
644 }
645
Nicolin Chen3635bf02013-11-13 18:56:24 +0800646 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800647 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800648 cpu_dai->channels = params_channels(params);
649 cpu_dai->sample_bits =
650 snd_pcm_format_physical_width(params_format(params));
651
Dong Aisheng17841022011-08-29 17:15:14 +0800652 codec_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800653 codec_dai->channels = params_channels(params);
654 codec_dai->sample_bits =
655 snd_pcm_format_physical_width(params_format(params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100656
657out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100658 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100659 return ret;
660
661platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700662 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100663 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
664
665interface_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700666 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100667 codec_dai->driver->ops->hw_free(substream, codec_dai);
668
669codec_err:
670 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
671 rtd->dai_link->ops->hw_free(substream);
672
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100673 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100674 return ret;
675}
676
677/*
678 * Frees resources allocated by hw_params, can be called multiple times
679 */
680static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
681{
682 struct snd_soc_pcm_runtime *rtd = substream->private_data;
683 struct snd_soc_platform *platform = rtd->platform;
684 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
685 struct snd_soc_dai *codec_dai = rtd->codec_dai;
686 struct snd_soc_codec *codec = rtd->codec;
687
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100688 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689
Nicolin Chend3383422013-11-20 18:37:09 +0800690 /* clear the corresponding DAIs parameters when going to be inactive */
691 if (cpu_dai->active == 1) {
692 cpu_dai->rate = 0;
693 cpu_dai->channels = 0;
694 cpu_dai->sample_bits = 0;
695 }
696
697 if (codec_dai->active == 1) {
698 codec_dai->rate = 0;
699 codec_dai->channels = 0;
700 codec_dai->sample_bits = 0;
701 }
702
Liam Girdwoodddee6272011-06-09 14:45:53 +0100703 /* apply codec digital mute */
704 if (!codec->active)
Mark Brownda183962013-02-06 15:44:07 +0000705 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100706
707 /* free any machine hw params */
708 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
709 rtd->dai_link->ops->hw_free(substream);
710
711 /* free any DMA resources */
712 if (platform->driver->ops && platform->driver->ops->hw_free)
713 platform->driver->ops->hw_free(substream);
714
715 /* now free hw params for the DAIs */
Mark Brownc5914b02013-10-30 17:47:39 -0700716 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100717 codec_dai->driver->ops->hw_free(substream, codec_dai);
718
Mark Brownc5914b02013-10-30 17:47:39 -0700719 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
721
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100722 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100723 return 0;
724}
725
726static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
727{
728 struct snd_soc_pcm_runtime *rtd = substream->private_data;
729 struct snd_soc_platform *platform = rtd->platform;
730 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
731 struct snd_soc_dai *codec_dai = rtd->codec_dai;
732 int ret;
733
Mark Brownc5914b02013-10-30 17:47:39 -0700734 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100735 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
736 if (ret < 0)
737 return ret;
738 }
739
740 if (platform->driver->ops && platform->driver->ops->trigger) {
741 ret = platform->driver->ops->trigger(substream, cmd);
742 if (ret < 0)
743 return ret;
744 }
745
Mark Brownc5914b02013-10-30 17:47:39 -0700746 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100747 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
748 if (ret < 0)
749 return ret;
750 }
751 return 0;
752}
753
Mark Brown45c0a182012-05-09 21:46:27 +0100754static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
755 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100756{
757 struct snd_soc_pcm_runtime *rtd = substream->private_data;
758 struct snd_soc_platform *platform = rtd->platform;
759 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
760 struct snd_soc_dai *codec_dai = rtd->codec_dai;
761 int ret;
762
Mark Brownc5914b02013-10-30 17:47:39 -0700763 if (codec_dai->driver->ops &&
764 codec_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100765 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
766 if (ret < 0)
767 return ret;
768 }
769
Mark Brownc5914b02013-10-30 17:47:39 -0700770 if (platform->driver->ops && platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100771 ret = platform->driver->bespoke_trigger(substream, cmd);
772 if (ret < 0)
773 return ret;
774 }
775
Mark Brownc5914b02013-10-30 17:47:39 -0700776 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100777 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
778 if (ret < 0)
779 return ret;
780 }
781 return 0;
782}
Liam Girdwoodddee6272011-06-09 14:45:53 +0100783/*
784 * soc level wrapper for pointer callback
785 * If cpu_dai, codec_dai, platform driver has the delay callback, than
786 * the runtime->delay will be updated accordingly.
787 */
788static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
789{
790 struct snd_soc_pcm_runtime *rtd = substream->private_data;
791 struct snd_soc_platform *platform = rtd->platform;
792 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
793 struct snd_soc_dai *codec_dai = rtd->codec_dai;
794 struct snd_pcm_runtime *runtime = substream->runtime;
795 snd_pcm_uframes_t offset = 0;
796 snd_pcm_sframes_t delay = 0;
797
798 if (platform->driver->ops && platform->driver->ops->pointer)
799 offset = platform->driver->ops->pointer(substream);
800
Mark Brownc5914b02013-10-30 17:47:39 -0700801 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100802 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
803
Mark Brownc5914b02013-10-30 17:47:39 -0700804 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100805 delay += codec_dai->driver->ops->delay(substream, codec_dai);
806
807 if (platform->driver->delay)
808 delay += platform->driver->delay(substream, codec_dai);
809
810 runtime->delay = delay;
811
812 return offset;
813}
814
Liam Girdwood01d75842012-04-25 12:12:49 +0100815/* connect a FE and BE */
816static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
817 struct snd_soc_pcm_runtime *be, int stream)
818{
819 struct snd_soc_dpcm *dpcm;
820
821 /* only add new dpcms */
822 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
823 if (dpcm->be == be && dpcm->fe == fe)
824 return 0;
825 }
826
827 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
828 if (!dpcm)
829 return -ENOMEM;
830
831 dpcm->be = be;
832 dpcm->fe = fe;
833 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
834 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
835 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
836 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
837
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300838 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100839 stream ? "capture" : "playback", fe->dai_link->name,
840 stream ? "<-" : "->", be->dai_link->name);
841
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100842#ifdef CONFIG_DEBUG_FS
843 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
844 fe->debugfs_dpcm_root, &dpcm->state);
845#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100846 return 1;
847}
848
849/* reparent a BE onto another FE */
850static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
851 struct snd_soc_pcm_runtime *be, int stream)
852{
853 struct snd_soc_dpcm *dpcm;
854 struct snd_pcm_substream *fe_substream, *be_substream;
855
856 /* reparent if BE is connected to other FEs */
857 if (!be->dpcm[stream].users)
858 return;
859
860 be_substream = snd_soc_dpcm_get_substream(be, stream);
861
862 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
863 if (dpcm->fe == fe)
864 continue;
865
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300866 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100867 stream ? "capture" : "playback",
868 dpcm->fe->dai_link->name,
869 stream ? "<-" : "->", dpcm->be->dai_link->name);
870
871 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
872 be_substream->runtime = fe_substream->runtime;
873 break;
874 }
875}
876
877/* disconnect a BE and FE */
878static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
879{
880 struct snd_soc_dpcm *dpcm, *d;
881
882 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000883 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100884 stream ? "capture" : "playback",
885 dpcm->be->dai_link->name);
886
887 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
888 continue;
889
Jarkko Nikula7cc302d2013-09-30 17:08:15 +0300890 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100891 stream ? "capture" : "playback", fe->dai_link->name,
892 stream ? "<-" : "->", dpcm->be->dai_link->name);
893
894 /* BEs still alive need new FE */
895 dpcm_be_reparent(fe, dpcm->be, stream);
896
Liam Girdwoodf86dcef2012-04-25 12:12:50 +0100897#ifdef CONFIG_DEBUG_FS
898 debugfs_remove(dpcm->debugfs_state);
899#endif
Liam Girdwood01d75842012-04-25 12:12:49 +0100900 list_del(&dpcm->list_be);
901 list_del(&dpcm->list_fe);
902 kfree(dpcm);
903 }
904}
905
906/* get BE for DAI widget and stream */
907static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
908 struct snd_soc_dapm_widget *widget, int stream)
909{
910 struct snd_soc_pcm_runtime *be;
911 int i;
912
913 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
914 for (i = 0; i < card->num_links; i++) {
915 be = &card->rtd[i];
916
Liam Girdwood35ea0652012-06-05 19:26:59 +0100917 if (!be->dai_link->no_pcm)
918 continue;
919
Liam Girdwood01d75842012-04-25 12:12:49 +0100920 if (be->cpu_dai->playback_widget == widget ||
921 be->codec_dai->playback_widget == widget)
922 return be;
923 }
924 } else {
925
926 for (i = 0; i < card->num_links; i++) {
927 be = &card->rtd[i];
928
Liam Girdwood35ea0652012-06-05 19:26:59 +0100929 if (!be->dai_link->no_pcm)
930 continue;
931
Liam Girdwood01d75842012-04-25 12:12:49 +0100932 if (be->cpu_dai->capture_widget == widget ||
933 be->codec_dai->capture_widget == widget)
934 return be;
935 }
936 }
937
Liam Girdwood103d84a2012-11-19 14:39:15 +0000938 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100939 stream ? "capture" : "playback", widget->name);
940 return NULL;
941}
942
943static inline struct snd_soc_dapm_widget *
944 rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
945{
946 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
947 return rtd->cpu_dai->playback_widget;
948 else
949 return rtd->cpu_dai->capture_widget;
950}
951
952static inline struct snd_soc_dapm_widget *
953 rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
954{
955 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
956 return rtd->codec_dai->playback_widget;
957 else
958 return rtd->codec_dai->capture_widget;
959}
960
961static int widget_in_list(struct snd_soc_dapm_widget_list *list,
962 struct snd_soc_dapm_widget *widget)
963{
964 int i;
965
966 for (i = 0; i < list->num_widgets; i++) {
967 if (widget == list->widgets[i])
968 return 1;
969 }
970
971 return 0;
972}
973
974static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
975 int stream, struct snd_soc_dapm_widget_list **list_)
976{
977 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
978 struct snd_soc_dapm_widget_list *list;
979 int paths;
980
981 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
982 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
983 if (list == NULL)
984 return -ENOMEM;
985
986 /* get number of valid DAI paths and their widgets */
987 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
988
Liam Girdwood103d84a2012-11-19 14:39:15 +0000989 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +0100990 stream ? "capture" : "playback");
991
992 *list_ = list;
993 return paths;
994}
995
996static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
997{
998 kfree(*list);
999}
1000
1001static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1002 struct snd_soc_dapm_widget_list **list_)
1003{
1004 struct snd_soc_dpcm *dpcm;
1005 struct snd_soc_dapm_widget_list *list = *list_;
1006 struct snd_soc_dapm_widget *widget;
1007 int prune = 0;
1008
1009 /* Destroy any old FE <--> BE connections */
1010 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1011
1012 /* is there a valid CPU DAI widget for this BE */
1013 widget = rtd_get_cpu_widget(dpcm->be, stream);
1014
1015 /* prune the BE if it's no longer in our active list */
1016 if (widget && widget_in_list(list, widget))
1017 continue;
1018
1019 /* is there a valid CODEC DAI widget for this BE */
1020 widget = rtd_get_codec_widget(dpcm->be, stream);
1021
1022 /* prune the BE if it's no longer in our active list */
1023 if (widget && widget_in_list(list, widget))
1024 continue;
1025
Liam Girdwood103d84a2012-11-19 14:39:15 +00001026 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001027 stream ? "capture" : "playback",
1028 dpcm->be->dai_link->name, fe->dai_link->name);
1029 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1030 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1031 prune++;
1032 }
1033
Liam Girdwood103d84a2012-11-19 14:39:15 +00001034 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001035 return prune;
1036}
1037
1038static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1039 struct snd_soc_dapm_widget_list **list_)
1040{
1041 struct snd_soc_card *card = fe->card;
1042 struct snd_soc_dapm_widget_list *list = *list_;
1043 struct snd_soc_pcm_runtime *be;
1044 int i, new = 0, err;
1045
1046 /* Create any new FE <--> BE connections */
1047 for (i = 0; i < list->num_widgets; i++) {
1048
Mark Brown46162742013-06-05 19:36:11 +01001049 switch (list->widgets[i]->id) {
1050 case snd_soc_dapm_dai_in:
1051 case snd_soc_dapm_dai_out:
1052 break;
1053 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001054 continue;
Mark Brown46162742013-06-05 19:36:11 +01001055 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001056
1057 /* is there a valid BE rtd for this widget */
1058 be = dpcm_get_be(card, list->widgets[i], stream);
1059 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001060 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001061 list->widgets[i]->name);
1062 continue;
1063 }
1064
1065 /* make sure BE is a real BE */
1066 if (!be->dai_link->no_pcm)
1067 continue;
1068
1069 /* don't connect if FE is not running */
1070 if (!fe->dpcm[stream].runtime)
1071 continue;
1072
1073 /* newly connected FE and BE */
1074 err = dpcm_be_connect(fe, be, stream);
1075 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001076 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001077 list->widgets[i]->name);
1078 break;
1079 } else if (err == 0) /* already connected */
1080 continue;
1081
1082 /* new */
1083 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1084 new++;
1085 }
1086
Liam Girdwood103d84a2012-11-19 14:39:15 +00001087 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001088 return new;
1089}
1090
1091/*
1092 * Find the corresponding BE DAIs that source or sink audio to this
1093 * FE substream.
1094 */
1095static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1096 int stream, struct snd_soc_dapm_widget_list **list, int new)
1097{
1098 if (new)
1099 return dpcm_add_paths(fe, stream, list);
1100 else
1101 return dpcm_prune_paths(fe, stream, list);
1102}
1103
1104static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1105{
1106 struct snd_soc_dpcm *dpcm;
1107
1108 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1109 dpcm->be->dpcm[stream].runtime_update =
1110 SND_SOC_DPCM_UPDATE_NO;
1111}
1112
1113static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1114 int stream)
1115{
1116 struct snd_soc_dpcm *dpcm;
1117
1118 /* disable any enabled and non active backends */
1119 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1120
1121 struct snd_soc_pcm_runtime *be = dpcm->be;
1122 struct snd_pcm_substream *be_substream =
1123 snd_soc_dpcm_get_substream(be, stream);
1124
1125 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001126 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001127 stream ? "capture" : "playback",
1128 be->dpcm[stream].state);
1129
1130 if (--be->dpcm[stream].users != 0)
1131 continue;
1132
1133 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1134 continue;
1135
1136 soc_pcm_close(be_substream);
1137 be_substream->runtime = NULL;
1138 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1139 }
1140}
1141
1142static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1143{
1144 struct snd_soc_dpcm *dpcm;
1145 int err, count = 0;
1146
1147 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1148 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1149
1150 struct snd_soc_pcm_runtime *be = dpcm->be;
1151 struct snd_pcm_substream *be_substream =
1152 snd_soc_dpcm_get_substream(be, stream);
1153
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001154 if (!be_substream) {
1155 dev_err(be->dev, "ASoC: no backend %s stream\n",
1156 stream ? "capture" : "playback");
1157 continue;
1158 }
1159
Liam Girdwood01d75842012-04-25 12:12:49 +01001160 /* is this op for this BE ? */
1161 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1162 continue;
1163
1164 /* first time the dpcm is open ? */
1165 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001166 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001167 stream ? "capture" : "playback",
1168 be->dpcm[stream].state);
1169
1170 if (be->dpcm[stream].users++ != 0)
1171 continue;
1172
1173 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1174 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1175 continue;
1176
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001177 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1178 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001179
1180 be_substream->runtime = be->dpcm[stream].runtime;
1181 err = soc_pcm_open(be_substream);
1182 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001183 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001184 be->dpcm[stream].users--;
1185 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001186 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001187 stream ? "capture" : "playback",
1188 be->dpcm[stream].state);
1189
1190 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1191 goto unwind;
1192 }
1193
1194 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1195 count++;
1196 }
1197
1198 return count;
1199
1200unwind:
1201 /* disable any enabled and non active backends */
1202 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1203 struct snd_soc_pcm_runtime *be = dpcm->be;
1204 struct snd_pcm_substream *be_substream =
1205 snd_soc_dpcm_get_substream(be, stream);
1206
1207 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1208 continue;
1209
1210 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001211 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001212 stream ? "capture" : "playback",
1213 be->dpcm[stream].state);
1214
1215 if (--be->dpcm[stream].users != 0)
1216 continue;
1217
1218 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1219 continue;
1220
1221 soc_pcm_close(be_substream);
1222 be_substream->runtime = NULL;
1223 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1224 }
1225
1226 return err;
1227}
1228
Mark Brown45c0a182012-05-09 21:46:27 +01001229static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001230{
1231 struct snd_pcm_runtime *runtime = substream->runtime;
1232 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1233 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1234 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1235
1236 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1237 runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
1238 runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
1239 runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
1240 runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
1241 runtime->hw.formats &= cpu_dai_drv->playback.formats;
1242 runtime->hw.rates = cpu_dai_drv->playback.rates;
1243 } else {
1244 runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
1245 runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
1246 runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
1247 runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
1248 runtime->hw.formats &= cpu_dai_drv->capture.formats;
1249 runtime->hw.rates = cpu_dai_drv->capture.rates;
1250 }
1251}
1252
1253static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1254{
1255 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1256 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1257 int stream = fe_substream->stream, ret = 0;
1258
1259 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1260
1261 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1262 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001263 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001264 goto be_err;
1265 }
1266
Liam Girdwood103d84a2012-11-19 14:39:15 +00001267 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001268
1269 /* start the DAI frontend */
1270 ret = soc_pcm_open(fe_substream);
1271 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001272 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001273 goto unwind;
1274 }
1275
1276 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1277
1278 dpcm_set_fe_runtime(fe_substream);
1279 snd_pcm_limit_hw_rates(runtime);
1280
1281 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1282 return 0;
1283
1284unwind:
1285 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1286be_err:
1287 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1288 return ret;
1289}
1290
1291static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1292{
1293 struct snd_soc_dpcm *dpcm;
1294
1295 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1296 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1297
1298 struct snd_soc_pcm_runtime *be = dpcm->be;
1299 struct snd_pcm_substream *be_substream =
1300 snd_soc_dpcm_get_substream(be, stream);
1301
1302 /* is this op for this BE ? */
1303 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1304 continue;
1305
1306 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001307 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001308 stream ? "capture" : "playback",
1309 be->dpcm[stream].state);
1310
1311 if (--be->dpcm[stream].users != 0)
1312 continue;
1313
1314 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1315 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1316 continue;
1317
Liam Girdwood103d84a2012-11-19 14:39:15 +00001318 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001319 dpcm->fe->dai_link->name);
1320
1321 soc_pcm_close(be_substream);
1322 be_substream->runtime = NULL;
1323
1324 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1325 }
1326 return 0;
1327}
1328
1329static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1330{
1331 struct snd_soc_pcm_runtime *fe = substream->private_data;
1332 int stream = substream->stream;
1333
1334 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1335
1336 /* shutdown the BEs */
1337 dpcm_be_dai_shutdown(fe, substream->stream);
1338
Liam Girdwood103d84a2012-11-19 14:39:15 +00001339 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001340
1341 /* now shutdown the frontend */
1342 soc_pcm_close(substream);
1343
1344 /* run the stream event for each BE */
1345 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1346
1347 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1348 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1349 return 0;
1350}
1351
1352static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1353{
1354 struct snd_soc_dpcm *dpcm;
1355
1356 /* only hw_params backends that are either sinks or sources
1357 * to this frontend DAI */
1358 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1359
1360 struct snd_soc_pcm_runtime *be = dpcm->be;
1361 struct snd_pcm_substream *be_substream =
1362 snd_soc_dpcm_get_substream(be, stream);
1363
1364 /* is this op for this BE ? */
1365 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1366 continue;
1367
1368 /* only free hw when no longer used - check all FEs */
1369 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1370 continue;
1371
1372 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1373 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1374 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001375 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001376 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1377 continue;
1378
Liam Girdwood103d84a2012-11-19 14:39:15 +00001379 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001380 dpcm->fe->dai_link->name);
1381
1382 soc_pcm_hw_free(be_substream);
1383
1384 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1385 }
1386
1387 return 0;
1388}
1389
Mark Brown45c0a182012-05-09 21:46:27 +01001390static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001391{
1392 struct snd_soc_pcm_runtime *fe = substream->private_data;
1393 int err, stream = substream->stream;
1394
1395 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1396 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1397
Liam Girdwood103d84a2012-11-19 14:39:15 +00001398 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001399
1400 /* call hw_free on the frontend */
1401 err = soc_pcm_hw_free(substream);
1402 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001403 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001404 fe->dai_link->name);
1405
1406 /* only hw_params backends that are either sinks or sources
1407 * to this frontend DAI */
1408 err = dpcm_be_dai_hw_free(fe, stream);
1409
1410 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1411 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1412
1413 mutex_unlock(&fe->card->mutex);
1414 return 0;
1415}
1416
1417static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1418{
1419 struct snd_soc_dpcm *dpcm;
1420 int ret;
1421
1422 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1423
1424 struct snd_soc_pcm_runtime *be = dpcm->be;
1425 struct snd_pcm_substream *be_substream =
1426 snd_soc_dpcm_get_substream(be, stream);
1427
1428 /* is this op for this BE ? */
1429 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1430 continue;
1431
1432 /* only allow hw_params() if no connected FEs are running */
1433 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1434 continue;
1435
1436 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1438 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1439 continue;
1440
Liam Girdwood103d84a2012-11-19 14:39:15 +00001441 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001442 dpcm->fe->dai_link->name);
1443
1444 /* copy params for each dpcm */
1445 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1446 sizeof(struct snd_pcm_hw_params));
1447
1448 /* perform any hw_params fixups */
1449 if (be->dai_link->be_hw_params_fixup) {
1450 ret = be->dai_link->be_hw_params_fixup(be,
1451 &dpcm->hw_params);
1452 if (ret < 0) {
1453 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001454 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001455 ret);
1456 goto unwind;
1457 }
1458 }
1459
1460 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1461 if (ret < 0) {
1462 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001463 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001464 goto unwind;
1465 }
1466
1467 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1468 }
1469 return 0;
1470
1471unwind:
1472 /* disable any enabled and non active backends */
1473 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1474 struct snd_soc_pcm_runtime *be = dpcm->be;
1475 struct snd_pcm_substream *be_substream =
1476 snd_soc_dpcm_get_substream(be, stream);
1477
1478 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1479 continue;
1480
1481 /* only allow hw_free() if no connected FEs are running */
1482 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1483 continue;
1484
1485 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1486 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1487 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1488 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1489 continue;
1490
1491 soc_pcm_hw_free(be_substream);
1492 }
1493
1494 return ret;
1495}
1496
Mark Brown45c0a182012-05-09 21:46:27 +01001497static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1498 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001499{
1500 struct snd_soc_pcm_runtime *fe = substream->private_data;
1501 int ret, stream = substream->stream;
1502
1503 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1504 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1505
1506 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1507 sizeof(struct snd_pcm_hw_params));
1508 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1509 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001510 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001511 goto out;
1512 }
1513
Liam Girdwood103d84a2012-11-19 14:39:15 +00001514 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001515 fe->dai_link->name, params_rate(params),
1516 params_channels(params), params_format(params));
1517
1518 /* call hw_params on the frontend */
1519 ret = soc_pcm_hw_params(substream, params);
1520 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001521 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001522 dpcm_be_dai_hw_free(fe, stream);
1523 } else
1524 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1525
1526out:
1527 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1528 mutex_unlock(&fe->card->mutex);
1529 return ret;
1530}
1531
1532static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1533 struct snd_pcm_substream *substream, int cmd)
1534{
1535 int ret;
1536
Liam Girdwood103d84a2012-11-19 14:39:15 +00001537 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001538 dpcm->fe->dai_link->name, cmd);
1539
1540 ret = soc_pcm_trigger(substream, cmd);
1541 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001542 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001543
1544 return ret;
1545}
1546
Mark Brown45c0a182012-05-09 21:46:27 +01001547static int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1548 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001549{
1550 struct snd_soc_dpcm *dpcm;
1551 int ret = 0;
1552
1553 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1554
1555 struct snd_soc_pcm_runtime *be = dpcm->be;
1556 struct snd_pcm_substream *be_substream =
1557 snd_soc_dpcm_get_substream(be, stream);
1558
1559 /* is this op for this BE ? */
1560 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1561 continue;
1562
1563 switch (cmd) {
1564 case SNDRV_PCM_TRIGGER_START:
1565 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1566 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1567 continue;
1568
1569 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1570 if (ret)
1571 return ret;
1572
1573 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1574 break;
1575 case SNDRV_PCM_TRIGGER_RESUME:
1576 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1577 continue;
1578
1579 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1580 if (ret)
1581 return ret;
1582
1583 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1584 break;
1585 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1586 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1587 continue;
1588
1589 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1590 if (ret)
1591 return ret;
1592
1593 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1594 break;
1595 case SNDRV_PCM_TRIGGER_STOP:
1596 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1597 continue;
1598
1599 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1600 continue;
1601
1602 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1603 if (ret)
1604 return ret;
1605
1606 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1607 break;
1608 case SNDRV_PCM_TRIGGER_SUSPEND:
1609 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
1610 continue;
1611
1612 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1613 continue;
1614
1615 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1616 if (ret)
1617 return ret;
1618
1619 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1620 break;
1621 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1622 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1623 continue;
1624
1625 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1626 continue;
1627
1628 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1629 if (ret)
1630 return ret;
1631
1632 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1633 break;
1634 }
1635 }
1636
1637 return ret;
1638}
1639EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1640
Mark Brown45c0a182012-05-09 21:46:27 +01001641static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001642{
1643 struct snd_soc_pcm_runtime *fe = substream->private_data;
1644 int stream = substream->stream, ret;
1645 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1646
1647 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1648
1649 switch (trigger) {
1650 case SND_SOC_DPCM_TRIGGER_PRE:
1651 /* call trigger on the frontend before the backend. */
1652
Liam Girdwood103d84a2012-11-19 14:39:15 +00001653 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001654 fe->dai_link->name, cmd);
1655
1656 ret = soc_pcm_trigger(substream, cmd);
1657 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001658 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001659 goto out;
1660 }
1661
1662 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1663 break;
1664 case SND_SOC_DPCM_TRIGGER_POST:
1665 /* call trigger on the frontend after the backend. */
1666
1667 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1668 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001669 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001670 goto out;
1671 }
1672
Liam Girdwood103d84a2012-11-19 14:39:15 +00001673 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001674 fe->dai_link->name, cmd);
1675
1676 ret = soc_pcm_trigger(substream, cmd);
1677 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001678 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1679 /* bespoke trigger() - handles both FE and BEs */
1680
Liam Girdwood103d84a2012-11-19 14:39:15 +00001681 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001682 fe->dai_link->name, cmd);
1683
1684 ret = soc_pcm_bespoke_trigger(substream, cmd);
1685 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001686 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001687 goto out;
1688 }
1689 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01001690 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00001691 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01001692 fe->dai_link->name);
1693 ret = -EINVAL;
1694 goto out;
1695 }
1696
1697 switch (cmd) {
1698 case SNDRV_PCM_TRIGGER_START:
1699 case SNDRV_PCM_TRIGGER_RESUME:
1700 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1701 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1702 break;
1703 case SNDRV_PCM_TRIGGER_STOP:
1704 case SNDRV_PCM_TRIGGER_SUSPEND:
1705 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1706 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1707 break;
1708 }
1709
1710out:
1711 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1712 return ret;
1713}
1714
1715static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1716{
1717 struct snd_soc_dpcm *dpcm;
1718 int ret = 0;
1719
1720 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1721
1722 struct snd_soc_pcm_runtime *be = dpcm->be;
1723 struct snd_pcm_substream *be_substream =
1724 snd_soc_dpcm_get_substream(be, stream);
1725
1726 /* is this op for this BE ? */
1727 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1728 continue;
1729
1730 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1731 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1732 continue;
1733
Liam Girdwood103d84a2012-11-19 14:39:15 +00001734 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001735 dpcm->fe->dai_link->name);
1736
1737 ret = soc_pcm_prepare(be_substream);
1738 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001739 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001740 ret);
1741 break;
1742 }
1743
1744 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1745 }
1746 return ret;
1747}
1748
Mark Brown45c0a182012-05-09 21:46:27 +01001749static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001750{
1751 struct snd_soc_pcm_runtime *fe = substream->private_data;
1752 int stream = substream->stream, ret = 0;
1753
1754 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1755
Liam Girdwood103d84a2012-11-19 14:39:15 +00001756 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001757
1758 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1759
1760 /* there is no point preparing this FE if there are no BEs */
1761 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001762 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001763 fe->dai_link->name);
1764 ret = -EINVAL;
1765 goto out;
1766 }
1767
1768 ret = dpcm_be_dai_prepare(fe, substream->stream);
1769 if (ret < 0)
1770 goto out;
1771
1772 /* call prepare on the frontend */
1773 ret = soc_pcm_prepare(substream);
1774 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001775 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001776 fe->dai_link->name);
1777 goto out;
1778 }
1779
1780 /* run the stream event for each BE */
1781 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1782 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1783
1784out:
1785 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1786 mutex_unlock(&fe->card->mutex);
1787
1788 return ret;
1789}
1790
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001791static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1792 unsigned int cmd, void *arg)
1793{
1794 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1795 struct snd_soc_platform *platform = rtd->platform;
1796
Mark Brownc5914b02013-10-30 17:47:39 -07001797 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01001798 return platform->driver->ops->ioctl(substream, cmd, arg);
1799 return snd_pcm_lib_ioctl(substream, cmd, arg);
1800}
1801
Liam Girdwood618dae12012-04-25 12:12:51 +01001802static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1803{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001804 struct snd_pcm_substream *substream =
1805 snd_soc_dpcm_get_substream(fe, stream);
1806 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001807 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01001808
Liam Girdwood103d84a2012-11-19 14:39:15 +00001809 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001810 stream ? "capture" : "playback", fe->dai_link->name);
1811
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001812 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1813 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001814 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001815 fe->dai_link->name);
1816
1817 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1818 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001819 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001820 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001821 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001822 fe->dai_link->name);
1823
1824 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1825 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001826 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001827 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001828
1829 err = dpcm_be_dai_hw_free(fe, stream);
1830 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001831 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001832
1833 err = dpcm_be_dai_shutdown(fe, stream);
1834 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01001836
1837 /* run the stream event for each BE */
1838 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1839
1840 return 0;
1841}
1842
1843static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1844{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001845 struct snd_pcm_substream *substream =
1846 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01001847 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001848 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01001849 int ret;
1850
Liam Girdwood103d84a2012-11-19 14:39:15 +00001851 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001852 stream ? "capture" : "playback", fe->dai_link->name);
1853
1854 /* Only start the BE if the FE is ready */
1855 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1856 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1857 return -EINVAL;
1858
1859 /* startup must always be called for new BEs */
1860 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001861 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001862 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01001863
1864 /* keep going if FE state is > open */
1865 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1866 return 0;
1867
1868 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001869 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001870 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01001871
1872 /* keep going if FE state is > hw_params */
1873 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1874 return 0;
1875
1876
1877 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03001878 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01001879 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01001880
1881 /* run the stream event for each BE */
1882 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1883
1884 /* keep going if FE state is > prepare */
1885 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1886 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1887 return 0;
1888
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001889 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1890 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001891 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001892 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01001893
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001894 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1895 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001896 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001897 goto hw_free;
1898 }
1899 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001900 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001901 fe->dai_link->name);
1902
1903 ret = dpcm_be_dai_trigger(fe, stream,
1904 SNDRV_PCM_TRIGGER_START);
1905 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001906 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001907 goto hw_free;
1908 }
Liam Girdwood618dae12012-04-25 12:12:51 +01001909 }
1910
1911 return 0;
1912
1913hw_free:
1914 dpcm_be_dai_hw_free(fe, stream);
1915close:
1916 dpcm_be_dai_shutdown(fe, stream);
1917disconnect:
1918 /* disconnect any non started BEs */
1919 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1920 struct snd_soc_pcm_runtime *be = dpcm->be;
1921 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1922 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1923 }
1924
1925 return ret;
1926}
1927
1928static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
1929{
1930 int ret;
1931
1932 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1933 ret = dpcm_run_update_startup(fe, stream);
1934 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001935 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001936 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1937
1938 return ret;
1939}
1940
1941static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
1942{
1943 int ret;
1944
1945 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1946 ret = dpcm_run_update_shutdown(fe, stream);
1947 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001948 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Liam Girdwood618dae12012-04-25 12:12:51 +01001949 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1950
1951 return ret;
1952}
1953
1954/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
1955 * any DAI links.
1956 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02001957int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01001958{
Liam Girdwood618dae12012-04-25 12:12:51 +01001959 int i, old, new, paths;
1960
Liam Girdwood618dae12012-04-25 12:12:51 +01001961 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1962 for (i = 0; i < card->num_rtd; i++) {
1963 struct snd_soc_dapm_widget_list *list;
1964 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
1965
1966 /* make sure link is FE */
1967 if (!fe->dai_link->dynamic)
1968 continue;
1969
1970 /* only check active links */
1971 if (!fe->cpu_dai->active)
1972 continue;
1973
1974 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00001975 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001976 fe->dai_link->name);
1977
1978 /* skip if FE doesn't have playback capability */
1979 if (!fe->cpu_dai->driver->playback.channels_min)
1980 goto capture;
1981
1982 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
1983 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001984 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01001985 fe->dai_link->name, "playback");
1986 mutex_unlock(&card->mutex);
1987 return paths;
1988 }
1989
1990 /* update any new playback paths */
1991 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
1992 if (new) {
1993 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
1994 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
1995 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
1996 }
1997
1998 /* update any old playback paths */
1999 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2000 if (old) {
2001 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2002 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2003 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2004 }
2005
2006capture:
2007 /* skip if FE doesn't have capture capability */
2008 if (!fe->cpu_dai->driver->capture.channels_min)
2009 continue;
2010
2011 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2012 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002013 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002014 fe->dai_link->name, "capture");
2015 mutex_unlock(&card->mutex);
2016 return paths;
2017 }
2018
2019 /* update any new capture paths */
2020 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2021 if (new) {
2022 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2023 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2024 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2025 }
2026
2027 /* update any old capture paths */
2028 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2029 if (old) {
2030 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2031 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2032 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2033 }
2034
2035 dpcm_path_put(&list);
2036 }
2037
2038 mutex_unlock(&card->mutex);
2039 return 0;
2040}
Liam Girdwood01d75842012-04-25 12:12:49 +01002041int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2042{
2043 struct snd_soc_dpcm *dpcm;
2044 struct list_head *clients =
2045 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2046
2047 list_for_each_entry(dpcm, clients, list_be) {
2048
2049 struct snd_soc_pcm_runtime *be = dpcm->be;
2050 struct snd_soc_dai *dai = be->codec_dai;
2051 struct snd_soc_dai_driver *drv = dai->driver;
2052
2053 if (be->dai_link->ignore_suspend)
2054 continue;
2055
Liam Girdwood103d84a2012-11-19 14:39:15 +00002056 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002057
Mark Brownc5914b02013-10-30 17:47:39 -07002058 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2059 drv->ops->digital_mute(dai, mute);
Liam Girdwood01d75842012-04-25 12:12:49 +01002060 }
2061
2062 return 0;
2063}
2064
Mark Brown45c0a182012-05-09 21:46:27 +01002065static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002066{
2067 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2068 struct snd_soc_dpcm *dpcm;
2069 struct snd_soc_dapm_widget_list *list;
2070 int ret;
2071 int stream = fe_substream->stream;
2072
2073 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2074 fe->dpcm[stream].runtime = fe_substream->runtime;
2075
2076 if (dpcm_path_get(fe, stream, &list) <= 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002077 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002078 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002079 }
2080
2081 /* calculate valid and active FE <-> BE dpcms */
2082 dpcm_process_paths(fe, stream, &list, 1);
2083
2084 ret = dpcm_fe_dai_startup(fe_substream);
2085 if (ret < 0) {
2086 /* clean up all links */
2087 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2088 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2089
2090 dpcm_be_disconnect(fe, stream);
2091 fe->dpcm[stream].runtime = NULL;
2092 }
2093
2094 dpcm_clear_pending_state(fe, stream);
2095 dpcm_path_put(&list);
2096 mutex_unlock(&fe->card->mutex);
2097 return ret;
2098}
2099
Mark Brown45c0a182012-05-09 21:46:27 +01002100static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002101{
2102 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2103 struct snd_soc_dpcm *dpcm;
2104 int stream = fe_substream->stream, ret;
2105
2106 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2107 ret = dpcm_fe_dai_shutdown(fe_substream);
2108
2109 /* mark FE's links ready to prune */
2110 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2111 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2112
2113 dpcm_be_disconnect(fe, stream);
2114
2115 fe->dpcm[stream].runtime = NULL;
2116 mutex_unlock(&fe->card->mutex);
2117 return ret;
2118}
2119
Liam Girdwoodddee6272011-06-09 14:45:53 +01002120/* create a new pcm */
2121int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2122{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002123 struct snd_soc_platform *platform = rtd->platform;
2124 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2125 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2126 struct snd_pcm *pcm;
2127 char new_name[64];
2128 int ret = 0, playback = 0, capture = 0;
2129
Liam Girdwood01d75842012-04-25 12:12:49 +01002130 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2131 if (cpu_dai->driver->playback.channels_min)
2132 playback = 1;
2133 if (cpu_dai->driver->capture.channels_min)
2134 capture = 1;
2135 } else {
Mark Brown05679092013-06-01 23:13:53 +01002136 if (codec_dai->driver->playback.channels_min &&
2137 cpu_dai->driver->playback.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002138 playback = 1;
Mark Brown05679092013-06-01 23:13:53 +01002139 if (codec_dai->driver->capture.channels_min &&
2140 cpu_dai->driver->capture.channels_min)
Liam Girdwood01d75842012-04-25 12:12:49 +01002141 capture = 1;
2142 }
Sangsu Parka5002312012-01-02 17:15:10 +09002143
Fabio Estevamd6bead02013-08-29 10:32:13 -03002144 if (rtd->dai_link->playback_only) {
2145 playback = 1;
2146 capture = 0;
2147 }
2148
2149 if (rtd->dai_link->capture_only) {
2150 playback = 0;
2151 capture = 1;
2152 }
2153
Liam Girdwood01d75842012-04-25 12:12:49 +01002154 /* create the PCM */
2155 if (rtd->dai_link->no_pcm) {
2156 snprintf(new_name, sizeof(new_name), "(%s)",
2157 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002158
Liam Girdwood01d75842012-04-25 12:12:49 +01002159 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2160 playback, capture, &pcm);
2161 } else {
2162 if (rtd->dai_link->dynamic)
2163 snprintf(new_name, sizeof(new_name), "%s (*)",
2164 rtd->dai_link->stream_name);
2165 else
2166 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2167 rtd->dai_link->stream_name, codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002168
Liam Girdwood01d75842012-04-25 12:12:49 +01002169 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2170 capture, &pcm);
2171 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002172 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002173 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002174 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002175 return ret;
2176 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002177 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002178
2179 /* DAPM dai link stream work */
2180 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2181
2182 rtd->pcm = pcm;
2183 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002184
2185 if (rtd->dai_link->no_pcm) {
2186 if (playback)
2187 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2188 if (capture)
2189 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2190 goto out;
2191 }
2192
2193 /* ASoC PCM operations */
2194 if (rtd->dai_link->dynamic) {
2195 rtd->ops.open = dpcm_fe_dai_open;
2196 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2197 rtd->ops.prepare = dpcm_fe_dai_prepare;
2198 rtd->ops.trigger = dpcm_fe_dai_trigger;
2199 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2200 rtd->ops.close = dpcm_fe_dai_close;
2201 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002202 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002203 } else {
2204 rtd->ops.open = soc_pcm_open;
2205 rtd->ops.hw_params = soc_pcm_hw_params;
2206 rtd->ops.prepare = soc_pcm_prepare;
2207 rtd->ops.trigger = soc_pcm_trigger;
2208 rtd->ops.hw_free = soc_pcm_hw_free;
2209 rtd->ops.close = soc_pcm_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 }
2213
Liam Girdwoodddee6272011-06-09 14:45:53 +01002214 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002215 rtd->ops.ack = platform->driver->ops->ack;
2216 rtd->ops.copy = platform->driver->ops->copy;
2217 rtd->ops.silence = platform->driver->ops->silence;
2218 rtd->ops.page = platform->driver->ops->page;
2219 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002220 }
2221
2222 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002223 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002224
2225 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002226 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002227
2228 if (platform->driver->pcm_new) {
2229 ret = platform->driver->pcm_new(rtd);
2230 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002231 dev_err(platform->dev,
2232 "ASoC: pcm constructor failed: %d\n",
2233 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002234 return ret;
2235 }
2236 }
2237
2238 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002239out:
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03002240 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
Liam Girdwoodddee6272011-06-09 14:45:53 +01002241 cpu_dai->name);
2242 return ret;
2243}
Liam Girdwood01d75842012-04-25 12:12:49 +01002244
2245/* is the current PCM operation for this FE ? */
2246int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2247{
2248 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2249 return 1;
2250 return 0;
2251}
2252EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2253
2254/* is the current PCM operation for this BE ? */
2255int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2256 struct snd_soc_pcm_runtime *be, int stream)
2257{
2258 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2259 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2260 be->dpcm[stream].runtime_update))
2261 return 1;
2262 return 0;
2263}
2264EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2265
2266/* get the substream for this BE */
2267struct snd_pcm_substream *
2268 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2269{
2270 return be->pcm->streams[stream].substream;
2271}
2272EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2273
2274/* get the BE runtime state */
2275enum snd_soc_dpcm_state
2276 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2277{
2278 return be->dpcm[stream].state;
2279}
2280EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2281
2282/* set the BE runtime state */
2283void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2284 int stream, enum snd_soc_dpcm_state state)
2285{
2286 be->dpcm[stream].state = state;
2287}
2288EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2289
2290/*
2291 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2292 * are not running, paused or suspended for the specified stream direction.
2293 */
2294int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2295 struct snd_soc_pcm_runtime *be, int stream)
2296{
2297 struct snd_soc_dpcm *dpcm;
2298 int state;
2299
2300 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2301
2302 if (dpcm->fe == fe)
2303 continue;
2304
2305 state = dpcm->fe->dpcm[stream].state;
2306 if (state == SND_SOC_DPCM_STATE_START ||
2307 state == SND_SOC_DPCM_STATE_PAUSED ||
2308 state == SND_SOC_DPCM_STATE_SUSPEND)
2309 return 0;
2310 }
2311
2312 /* it's safe to free/stop this BE DAI */
2313 return 1;
2314}
2315EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2316
2317/*
2318 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2319 * running, paused or suspended for the specified stream direction.
2320 */
2321int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2322 struct snd_soc_pcm_runtime *be, int stream)
2323{
2324 struct snd_soc_dpcm *dpcm;
2325 int state;
2326
2327 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2328
2329 if (dpcm->fe == fe)
2330 continue;
2331
2332 state = dpcm->fe->dpcm[stream].state;
2333 if (state == SND_SOC_DPCM_STATE_START ||
2334 state == SND_SOC_DPCM_STATE_PAUSED ||
2335 state == SND_SOC_DPCM_STATE_SUSPEND ||
2336 state == SND_SOC_DPCM_STATE_PREPARE)
2337 return 0;
2338 }
2339
2340 /* it's safe to change hw_params */
2341 return 1;
2342}
2343EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002344
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002345int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2346 int cmd, struct snd_soc_platform *platform)
2347{
Mark Brownc5914b02013-10-30 17:47:39 -07002348 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002349 return platform->driver->ops->trigger(substream, cmd);
2350 return 0;
2351}
2352EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2353
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002354#ifdef CONFIG_DEBUG_FS
2355static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2356{
2357 switch (state) {
2358 case SND_SOC_DPCM_STATE_NEW:
2359 return "new";
2360 case SND_SOC_DPCM_STATE_OPEN:
2361 return "open";
2362 case SND_SOC_DPCM_STATE_HW_PARAMS:
2363 return "hw_params";
2364 case SND_SOC_DPCM_STATE_PREPARE:
2365 return "prepare";
2366 case SND_SOC_DPCM_STATE_START:
2367 return "start";
2368 case SND_SOC_DPCM_STATE_STOP:
2369 return "stop";
2370 case SND_SOC_DPCM_STATE_SUSPEND:
2371 return "suspend";
2372 case SND_SOC_DPCM_STATE_PAUSED:
2373 return "paused";
2374 case SND_SOC_DPCM_STATE_HW_FREE:
2375 return "hw_free";
2376 case SND_SOC_DPCM_STATE_CLOSE:
2377 return "close";
2378 }
2379
2380 return "unknown";
2381}
2382
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002383static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2384 int stream, char *buf, size_t size)
2385{
2386 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2387 struct snd_soc_dpcm *dpcm;
2388 ssize_t offset = 0;
2389
2390 /* FE state */
2391 offset += snprintf(buf + offset, size - offset,
2392 "[%s - %s]\n", fe->dai_link->name,
2393 stream ? "Capture" : "Playback");
2394
2395 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2396 dpcm_state_string(fe->dpcm[stream].state));
2397
2398 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2399 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2400 offset += snprintf(buf + offset, size - offset,
2401 "Hardware Params: "
2402 "Format = %s, Channels = %d, Rate = %d\n",
2403 snd_pcm_format_name(params_format(params)),
2404 params_channels(params),
2405 params_rate(params));
2406
2407 /* BEs state */
2408 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2409
2410 if (list_empty(&fe->dpcm[stream].be_clients)) {
2411 offset += snprintf(buf + offset, size - offset,
2412 " No active DSP links\n");
2413 goto out;
2414 }
2415
2416 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2417 struct snd_soc_pcm_runtime *be = dpcm->be;
2418 params = &dpcm->hw_params;
2419
2420 offset += snprintf(buf + offset, size - offset,
2421 "- %s\n", be->dai_link->name);
2422
2423 offset += snprintf(buf + offset, size - offset,
2424 " State: %s\n",
2425 dpcm_state_string(be->dpcm[stream].state));
2426
2427 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2428 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2429 offset += snprintf(buf + offset, size - offset,
2430 " Hardware Params: "
2431 "Format = %s, Channels = %d, Rate = %d\n",
2432 snd_pcm_format_name(params_format(params)),
2433 params_channels(params),
2434 params_rate(params));
2435 }
2436
2437out:
2438 return offset;
2439}
2440
2441static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2442 size_t count, loff_t *ppos)
2443{
2444 struct snd_soc_pcm_runtime *fe = file->private_data;
2445 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2446 char *buf;
2447
2448 buf = kmalloc(out_count, GFP_KERNEL);
2449 if (!buf)
2450 return -ENOMEM;
2451
2452 if (fe->cpu_dai->driver->playback.channels_min)
2453 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2454 buf + offset, out_count - offset);
2455
2456 if (fe->cpu_dai->driver->capture.channels_min)
2457 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2458 buf + offset, out_count - offset);
2459
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002460 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002461
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002462 kfree(buf);
2463 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002464}
2465
2466static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002467 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002468 .read = dpcm_state_read_file,
2469 .llseek = default_llseek,
2470};
2471
2472int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2473{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002474 if (!rtd->dai_link)
2475 return 0;
2476
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002477 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2478 rtd->card->debugfs_card_root);
2479 if (!rtd->debugfs_dpcm_root) {
2480 dev_dbg(rtd->dev,
2481 "ASoC: Failed to create dpcm debugfs directory %s\n",
2482 rtd->dai_link->name);
2483 return -EINVAL;
2484 }
2485
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002486 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002487 rtd->debugfs_dpcm_root,
2488 rtd, &dpcm_state_fops);
2489
2490 return 0;
2491}
2492#endif