blob: 1aa175b26f17cfbd152e8258b5431bf6bdd49809 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +020010 * Mark Brown <broonie@opensource.wolfsonmicro.com>
Liam Girdwoodddee6272011-06-09 14:45:53 +010011 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/delay.h>
Nicolin Chen988e8cc2013-11-04 14:57:31 +080022#include <linux/pinctrl/consumer.h>
Mark Brownd6652ef2011-12-03 20:14:31 +000023#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010024#include <linux/slab.h>
25#include <linux/workqueue.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010026#include <linux/export.h>
Liam Girdwoodf86dcef2012-04-25 12:12:50 +010027#include <linux/debugfs.h>
Banajit Goswami7ffe84e2017-01-10 17:28:14 -080028#include <linux/dma-mapping.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010029#include <sound/core.h>
30#include <sound/pcm.h>
31#include <sound/pcm_params.h>
32#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010033#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010034#include <sound/initval.h>
35
Liam Girdwood01d75842012-04-25 12:12:49 +010036#define DPCM_MAX_BE_USERS 8
37
Ricard Wanderlofcde79032015-08-24 14:16:51 +020038/*
Liam Girdwoodf5b50e72017-01-10 17:10:17 -080039 * ASoC no host IO hardware.
40 * TODO: fine tune these values for all host less transfers.
41 */
42static const struct snd_pcm_hardware no_host_hardware = {
43 .info = SNDRV_PCM_INFO_MMAP |
44 SNDRV_PCM_INFO_MMAP_VALID |
45 SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_PAUSE |
47 SNDRV_PCM_INFO_RESUME,
48 .formats = SNDRV_PCM_FMTBIT_S16_LE |
49 SNDRV_PCM_FMTBIT_S32_LE,
50 .period_bytes_min = PAGE_SIZE >> 2,
51 .period_bytes_max = PAGE_SIZE >> 1,
52 .periods_min = 2,
53 .periods_max = 4,
Banajit Goswami7ffe84e2017-01-10 17:28:14 -080054 /*
55 * Increase the max buffer bytes as PAGE_SIZE bytes is
56 * not enough to encompass all the scenarios sent by
57 * userspapce.
58 */
59 .buffer_bytes_max = PAGE_SIZE * 4,
Liam Girdwoodf5b50e72017-01-10 17:10:17 -080060};
61
62/*
Ricard Wanderlofcde79032015-08-24 14:16:51 +020063 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
64 *
65 * Returns true if the DAI supports the indicated stream type.
66 */
67static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
68{
69 struct snd_soc_pcm_stream *codec_stream;
70
71 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
72 codec_stream = &dai->driver->playback;
73 else
74 codec_stream = &dai->driver->capture;
75
76 /* If the codec specifies any rate at all, it supports the stream. */
77 return codec_stream->rates;
78}
79
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020080/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010081 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
82 * @rtd: ASoC PCM runtime that is activated
83 * @stream: Direction of the PCM stream
84 *
85 * Increments the active count for all the DAIs and components attached to a PCM
86 * runtime. Should typically be called when a stream is opened.
87 *
88 * Must be called with the rtd->pcm_mutex being held
89 */
90void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
91{
92 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020093 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010094
95 lockdep_assert_held(&rtd->pcm_mutex);
96
97 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
98 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020099 for (i = 0; i < rtd->num_codecs; i++)
100 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100101 } else {
102 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200103 for (i = 0; i < rtd->num_codecs; i++)
104 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100105 }
106
107 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100108 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200109 for (i = 0; i < rtd->num_codecs; i++) {
110 rtd->codec_dais[i]->active++;
111 rtd->codec_dais[i]->component->active++;
112 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100113}
114
115/**
116 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
117 * @rtd: ASoC PCM runtime that is deactivated
118 * @stream: Direction of the PCM stream
119 *
120 * Decrements the active count for all the DAIs and components attached to a PCM
121 * runtime. Should typically be called when a stream is closed.
122 *
123 * Must be called with the rtd->pcm_mutex being held
124 */
125void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
126{
127 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200128 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100129
130 lockdep_assert_held(&rtd->pcm_mutex);
131
132 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
133 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200134 for (i = 0; i < rtd->num_codecs; i++)
135 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100136 } else {
137 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200138 for (i = 0; i < rtd->num_codecs; i++)
139 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100140 }
141
142 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100143 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200144 for (i = 0; i < rtd->num_codecs; i++) {
145 rtd->codec_dais[i]->component->active--;
146 rtd->codec_dais[i]->active--;
147 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100148}
149
150/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100151 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
152 * @rtd: The ASoC PCM runtime that should be checked.
153 *
154 * This function checks whether the power down delay should be ignored for a
155 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
156 * been configured to ignore the delay, or if none of the components benefits
157 * from having the delay.
158 */
159bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
160{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200161 int i;
162 bool ignore = true;
163
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100164 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
165 return true;
166
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200167 for (i = 0; i < rtd->num_codecs; i++)
168 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
169
170 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100171}
172
173/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200174 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
175 * @substream: the pcm substream
176 * @hw: the hardware parameters
177 *
178 * Sets the substream runtime hardware parameters.
179 */
180int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
181 const struct snd_pcm_hardware *hw)
182{
183 struct snd_pcm_runtime *runtime = substream->runtime;
Banajit Goswami7ffe84e2017-01-10 17:28:14 -0800184 if (!runtime)
185 return 0;
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200186 runtime->hw.info = hw->info;
187 runtime->hw.formats = hw->formats;
188 runtime->hw.period_bytes_min = hw->period_bytes_min;
189 runtime->hw.period_bytes_max = hw->period_bytes_max;
190 runtime->hw.periods_min = hw->periods_min;
191 runtime->hw.periods_max = hw->periods_max;
192 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
193 runtime->hw.fifo_size = hw->fifo_size;
194 return 0;
195}
196EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
197
Liam Girdwood01d75842012-04-25 12:12:49 +0100198/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000199int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100200 int event)
201{
202 struct snd_soc_dpcm *dpcm;
203
204 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
205
206 struct snd_soc_pcm_runtime *be = dpcm->be;
207
Liam Girdwood103d84a2012-11-19 14:39:15 +0000208 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100209 be->dai_link->name, event, dir);
Banajit Goswamia5945772014-08-20 18:23:46 -0700210 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
211 (be->dpcm[dir].users >= 1)) {
212 pr_debug("%s Don't close BE\n", __func__);
213 continue;
214 }
Liam Girdwood01d75842012-04-25 12:12:49 +0100215 snd_soc_dapm_stream_event(be, dir, event);
216 }
217
218 snd_soc_dapm_stream_event(fe, dir, event);
219
220 return 0;
221}
222
Dong Aisheng17841022011-08-29 17:15:14 +0800223static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
224 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100225{
226 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100227 int ret;
228
Nicolin Chen3635bf02013-11-13 18:56:24 +0800229 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
230 rtd->dai_link->symmetric_rates)) {
231 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
232 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100233
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200234 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800235 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200236 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800237 if (ret < 0) {
238 dev_err(soc_dai->dev,
239 "ASoC: Unable to apply rate constraint: %d\n",
240 ret);
241 return ret;
242 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100243 }
244
Nicolin Chen3635bf02013-11-13 18:56:24 +0800245 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
246 rtd->dai_link->symmetric_channels)) {
247 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
248 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100249
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200250 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800252 soc_dai->channels);
253 if (ret < 0) {
254 dev_err(soc_dai->dev,
255 "ASoC: Unable to apply channel symmetry constraint: %d\n",
256 ret);
257 return ret;
258 }
259 }
260
261 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
262 rtd->dai_link->symmetric_samplebits)) {
263 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
264 soc_dai->sample_bits);
265
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200266 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800267 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800268 soc_dai->sample_bits);
269 if (ret < 0) {
270 dev_err(soc_dai->dev,
271 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
272 ret);
273 return ret;
274 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100275 }
276
277 return 0;
278}
279
Nicolin Chen3635bf02013-11-13 18:56:24 +0800280static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params)
282{
283 struct snd_soc_pcm_runtime *rtd = substream->private_data;
284 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200285 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800286
287 rate = params_rate(params);
288 channels = params_channels(params);
289 sample_bits = snd_pcm_format_physical_width(params_format(params));
290
291 /* reject unmatched parameters when applying symmetry */
292 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800293 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200294
295 for (i = 0; i < rtd->num_codecs; i++)
296 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
297
Nicolin Chen3635bf02013-11-13 18:56:24 +0800298 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
299 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
300 cpu_dai->rate, rate);
301 return -EINVAL;
302 }
303
304 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800305 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200306
307 for (i = 0; i < rtd->num_codecs; i++)
308 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
309
Nicolin Chen3635bf02013-11-13 18:56:24 +0800310 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
311 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
312 cpu_dai->channels, channels);
313 return -EINVAL;
314 }
315
316 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800317 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200318
319 for (i = 0; i < rtd->num_codecs; i++)
320 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
321
Nicolin Chen3635bf02013-11-13 18:56:24 +0800322 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
323 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
324 cpu_dai->sample_bits, sample_bits);
325 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100326 }
327
328 return 0;
329}
330
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100331static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
332{
333 struct snd_soc_pcm_runtime *rtd = substream->private_data;
334 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100335 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200336 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100337
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200338 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
339 cpu_driver->symmetric_channels || link->symmetric_channels ||
340 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
341
342 for (i = 0; i < rtd->num_codecs; i++)
343 symmetry = symmetry ||
344 rtd->codec_dais[i]->driver->symmetric_rates ||
345 rtd->codec_dais[i]->driver->symmetric_channels ||
346 rtd->codec_dais[i]->driver->symmetric_samplebits;
347
348 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100349}
350
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200351static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000352{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200353 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100354 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000355
356 if (!bits)
357 return;
358
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100359 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
360 if (ret != 0)
361 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
362 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000363}
364
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200365static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200366{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200367 struct snd_soc_pcm_runtime *rtd = substream->private_data;
368 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200369 struct snd_soc_dai *codec_dai;
370 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200371 unsigned int bits = 0, cpu_bits;
372
373 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200374 for (i = 0; i < rtd->num_codecs; i++) {
375 codec_dai = rtd->codec_dais[i];
376 if (codec_dai->driver->playback.sig_bits == 0) {
377 bits = 0;
378 break;
379 }
380 bits = max(codec_dai->driver->playback.sig_bits, bits);
381 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200382 cpu_bits = cpu_dai->driver->playback.sig_bits;
383 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200384 for (i = 0; i < rtd->num_codecs; i++) {
385 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200386 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200387 bits = 0;
388 break;
389 }
390 bits = max(codec_dai->driver->capture.sig_bits, bits);
391 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200392 cpu_bits = cpu_dai->driver->capture.sig_bits;
393 }
394
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200395 soc_pcm_set_msb(substream, bits);
396 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200397}
398
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200399static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200400{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200401 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100402 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200403 struct snd_soc_pcm_runtime *rtd = substream->private_data;
404 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
405 struct snd_soc_dai_driver *codec_dai_drv;
406 struct snd_soc_pcm_stream *codec_stream;
407 struct snd_soc_pcm_stream *cpu_stream;
408 unsigned int chan_min = 0, chan_max = UINT_MAX;
409 unsigned int rate_min = 0, rate_max = UINT_MAX;
410 unsigned int rates = UINT_MAX;
411 u64 formats = ULLONG_MAX;
412 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100413
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200414 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
415 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100416 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200417 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100418
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200419 /* first calculate min/max only for CODECs in the DAI link */
420 for (i = 0; i < rtd->num_codecs; i++) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200421
422 /*
423 * Skip CODECs which don't support the current stream type.
424 * Otherwise, since the rate, channel, and format values will
425 * zero in that case, we would have no usable settings left,
426 * causing the resulting setup to fail.
427 * At least one CODEC should match, otherwise we should have
428 * bailed out on a higher level, since there would be no
429 * CODEC to support the transfer direction in that case.
430 */
431 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
432 substream->stream))
433 continue;
434
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200435 codec_dai_drv = rtd->codec_dais[i]->driver;
436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
437 codec_stream = &codec_dai_drv->playback;
438 else
439 codec_stream = &codec_dai_drv->capture;
440 chan_min = max(chan_min, codec_stream->channels_min);
441 chan_max = min(chan_max, codec_stream->channels_max);
442 rate_min = max(rate_min, codec_stream->rate_min);
443 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
444 formats &= codec_stream->formats;
445 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
446 }
447
448 /*
449 * chan min/max cannot be enforced if there are multiple CODEC DAIs
450 * connected to a single CPU DAI, use CPU DAI's directly and let
451 * channel allocation be fixed up later
452 */
453 if (rtd->num_codecs > 1) {
454 chan_min = cpu_stream->channels_min;
455 chan_max = cpu_stream->channels_max;
456 }
457
458 hw->channels_min = max(chan_min, cpu_stream->channels_min);
459 hw->channels_max = min(chan_max, cpu_stream->channels_max);
460 if (hw->formats)
461 hw->formats &= formats & cpu_stream->formats;
462 else
463 hw->formats = formats & cpu_stream->formats;
464 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100465
466 snd_pcm_limit_hw_rates(runtime);
467
468 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200469 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100470 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200471 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200472}
473
Mark Brown58ba9b22012-01-16 18:38:51 +0000474/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100475 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
476 * then initialized and any private data can be allocated. This also calls
477 * startup for the cpu DAI, platform, machine and codec DAI.
478 */
479static int soc_pcm_open(struct snd_pcm_substream *substream)
480{
481 struct snd_soc_pcm_runtime *rtd = substream->private_data;
482 struct snd_pcm_runtime *runtime = substream->runtime;
483 struct snd_soc_platform *platform = rtd->platform;
484 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200485 struct snd_soc_dai *codec_dai;
486 const char *codec_dai_name = "multicodec";
487 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100488
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800489 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200490 for (i = 0; i < rtd->num_codecs; i++)
491 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000492 pm_runtime_get_sync(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200493 for (i = 0; i < rtd->num_codecs; i++)
494 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000495 pm_runtime_get_sync(platform->dev);
496
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100497 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498
Liam Girdwoodf5b50e72017-01-10 17:10:17 -0800499 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
500 snd_soc_set_runtime_hwparams(substream, &no_host_hardware);
501
Liam Girdwoodddee6272011-06-09 14:45:53 +0100502 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700503 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100504 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
505 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000506 dev_err(cpu_dai->dev, "ASoC: can't open interface"
507 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100508 goto out;
509 }
510 }
511
512 if (platform->driver->ops && platform->driver->ops->open) {
513 ret = platform->driver->ops->open(substream);
514 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000515 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200516 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100517 goto platform_err;
518 }
519 }
520
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200521 for (i = 0; i < rtd->num_codecs; i++) {
522 codec_dai = rtd->codec_dais[i];
523 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
524 ret = codec_dai->driver->ops->startup(substream,
525 codec_dai);
526 if (ret < 0) {
527 dev_err(codec_dai->dev,
528 "ASoC: can't open codec %s: %d\n",
529 codec_dai->name, ret);
530 goto codec_dai_err;
531 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100532 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200533
534 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
535 codec_dai->tx_mask = 0;
536 else
537 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 }
539
540 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
541 ret = rtd->dai_link->ops->startup(substream);
542 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000543 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000544 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100545 goto machine_err;
546 }
547 }
548
Liam Girdwood01d75842012-04-25 12:12:49 +0100549 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
550 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
551 goto dynamic;
552
Liam Girdwoodddee6272011-06-09 14:45:53 +0100553 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200554 soc_pcm_init_runtime_hw(substream);
555
556 if (rtd->num_codecs == 1)
557 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100558
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100559 if (soc_pcm_has_symmetry(substream))
560 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
561
Liam Girdwoodddee6272011-06-09 14:45:53 +0100562 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000564 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200565 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100566 goto config_err;
567 }
568 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000569 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200570 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100571 goto config_err;
572 }
573 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
574 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000575 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200576 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100577 goto config_err;
578 }
579
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200580 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000581
Liam Girdwoodddee6272011-06-09 14:45:53 +0100582 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800583 if (cpu_dai->active) {
584 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
585 if (ret != 0)
586 goto config_err;
587 }
588
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200589 for (i = 0; i < rtd->num_codecs; i++) {
590 if (rtd->codec_dais[i]->active) {
591 ret = soc_pcm_apply_symmetry(substream,
592 rtd->codec_dais[i]);
593 if (ret != 0)
594 goto config_err;
595 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100596 }
597
Liam Girdwood103d84a2012-11-19 14:39:15 +0000598 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200599 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000600 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
601 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100602 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000603 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100604 runtime->hw.rate_max);
605
Liam Girdwood01d75842012-04-25 12:12:49 +0100606dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100607
608 snd_soc_runtime_activate(rtd, substream->stream);
609
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100610 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100611 return 0;
612
613config_err:
614 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
615 rtd->dai_link->ops->shutdown(substream);
616
617machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200618 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100619
620codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200621 while (--i >= 0) {
622 codec_dai = rtd->codec_dais[i];
623 if (codec_dai->driver->ops->shutdown)
624 codec_dai->driver->ops->shutdown(substream, codec_dai);
625 }
626
Liam Girdwoodddee6272011-06-09 14:45:53 +0100627 if (platform->driver->ops && platform->driver->ops->close)
628 platform->driver->ops->close(substream);
629
630platform_err:
631 if (cpu_dai->driver->ops->shutdown)
632 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
633out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100634 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000635
Sanyog Kale3f809782016-01-05 17:14:49 +0530636 pm_runtime_mark_last_busy(platform->dev);
637 pm_runtime_put_autosuspend(platform->dev);
638 for (i = 0; i < rtd->num_codecs; i++) {
639 pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
640 pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
641 }
642
643 pm_runtime_mark_last_busy(cpu_dai->dev);
644 pm_runtime_put_autosuspend(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200645 for (i = 0; i < rtd->num_codecs; i++) {
646 if (!rtd->codec_dais[i]->active)
647 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
648 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800649 if (!cpu_dai->active)
650 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000651
Liam Girdwoodddee6272011-06-09 14:45:53 +0100652 return ret;
653}
654
655/*
656 * Power down the audio subsystem pmdown_time msecs after close is called.
657 * This is to ensure there are no pops or clicks in between any music tracks
658 * due to DAPM power cycling.
659 */
660static void close_delayed_work(struct work_struct *work)
661{
662 struct snd_soc_pcm_runtime *rtd =
663 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200664 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100665
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100666 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100667
Liam Girdwood103d84a2012-11-19 14:39:15 +0000668 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100669 codec_dai->driver->playback.stream_name,
670 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600671 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100672
673 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600674 if (rtd->pop_wait == 1) {
675 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800676 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000677 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100678 }
679
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100680 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100681}
682
683/*
684 * Called by ALSA when a PCM substream is closed. Private data can be
685 * freed here. The cpu DAI, codec DAI, machine and platform are also
686 * shutdown.
687 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100688static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100689{
690 struct snd_soc_pcm_runtime *rtd = substream->private_data;
691 struct snd_soc_platform *platform = rtd->platform;
692 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200693 struct snd_soc_dai *codec_dai;
694 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100695
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100696 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100697
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100698 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100699
Dong Aisheng17841022011-08-29 17:15:14 +0800700 /* clear the corresponding DAIs rate when inactive */
701 if (!cpu_dai->active)
702 cpu_dai->rate = 0;
703
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200704 for (i = 0; i < rtd->num_codecs; i++) {
705 codec_dai = rtd->codec_dais[i];
706 if (!codec_dai->active)
707 codec_dai->rate = 0;
708 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200709
Ramesh Babuae116012014-10-15 12:34:59 +0530710 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
711
Liam Girdwoodddee6272011-06-09 14:45:53 +0100712 if (cpu_dai->driver->ops->shutdown)
713 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
714
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200715 for (i = 0; i < rtd->num_codecs; i++) {
716 codec_dai = rtd->codec_dais[i];
717 if (codec_dai->driver->ops->shutdown)
718 codec_dai->driver->ops->shutdown(substream, codec_dai);
719 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100720
721 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
722 rtd->dai_link->ops->shutdown(substream);
723
724 if (platform->driver->ops && platform->driver->ops->close)
725 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726
727 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100728 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300729 /* powered down playback stream now */
730 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800731 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800732 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300733 } else {
734 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600735 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100736 queue_delayed_work(system_power_efficient_wq,
737 &rtd->delayed_work,
738 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300739 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100740 } else {
741 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800742 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000743 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100744 }
745
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100746 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000747
Sanyog Kale3f809782016-01-05 17:14:49 +0530748 pm_runtime_mark_last_busy(platform->dev);
749 pm_runtime_put_autosuspend(platform->dev);
750
751 for (i = 0; i < rtd->num_codecs; i++) {
752 pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
753 pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
754 }
755
756 pm_runtime_mark_last_busy(cpu_dai->dev);
757 pm_runtime_put_autosuspend(cpu_dai->dev);
758
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200759 for (i = 0; i < rtd->num_codecs; i++) {
760 if (!rtd->codec_dais[i]->active)
761 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
762 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800763 if (!cpu_dai->active)
764 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000765
Liam Girdwoodddee6272011-06-09 14:45:53 +0100766 return 0;
767}
768
769/*
770 * Called by ALSA when the PCM substream is prepared, can set format, sample
771 * rate, etc. This function is non atomic and can be called multiple times,
772 * it can refer to the runtime info.
773 */
774static int soc_pcm_prepare(struct snd_pcm_substream *substream)
775{
776 struct snd_soc_pcm_runtime *rtd = substream->private_data;
777 struct snd_soc_platform *platform = rtd->platform;
778 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200779 struct snd_soc_dai *codec_dai;
780 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100781
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100782 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100783
Sudheer Papothi70ef6c22016-01-29 06:21:36 +0530784 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
785 snd_soc_dapm_stream_event(rtd,
786 SNDRV_PCM_STREAM_PLAYBACK,
787 SND_SOC_DAPM_STREAM_START);
788
Liam Girdwoodddee6272011-06-09 14:45:53 +0100789 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
790 ret = rtd->dai_link->ops->prepare(substream);
791 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000792 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
793 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100794 goto out;
795 }
796 }
797
798 if (platform->driver->ops && platform->driver->ops->prepare) {
799 ret = platform->driver->ops->prepare(substream);
800 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000801 dev_err(platform->dev, "ASoC: platform prepare error:"
802 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100803 goto out;
804 }
805 }
806
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200807 for (i = 0; i < rtd->num_codecs; i++) {
808 codec_dai = rtd->codec_dais[i];
809 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
810 ret = codec_dai->driver->ops->prepare(substream,
811 codec_dai);
812 if (ret < 0) {
813 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200814 "ASoC: codec DAI prepare error: %d\n",
815 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200816 goto out;
817 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100818 }
819 }
820
Mark Brownc5914b02013-10-30 17:47:39 -0700821 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
823 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200824 dev_err(cpu_dai->dev,
825 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100826 goto out;
827 }
828 }
829
830 /* cancel any delayed stream shutdown that is pending */
831 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600832 rtd->pop_wait) {
833 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100834 cancel_delayed_work(&rtd->delayed_work);
835 }
836
Sudheer Papothi70ef6c22016-01-29 06:21:36 +0530837 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
838 for (i = 0; i < rtd->num_codecs; i++) {
839 codec_dai = rtd->codec_dais[i];
840 if (codec_dai->capture_active == 1)
841 snd_soc_dapm_stream_event(rtd,
842 SNDRV_PCM_STREAM_CAPTURE,
843 SND_SOC_DAPM_STREAM_START);
844 }
845 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100846
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200847 for (i = 0; i < rtd->num_codecs; i++)
848 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
849 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530850 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100851
852out:
Sudheer Papothi70ef6c22016-01-29 06:21:36 +0530853 if (ret < 0 && substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
854 pr_err("%s: Issue stop stream for codec_dai due to op failure %d = ret\n",
855 __func__, ret);
856 snd_soc_dapm_stream_event(rtd,
857 SNDRV_PCM_STREAM_PLAYBACK,
858 SND_SOC_DAPM_STREAM_STOP);
859 }
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100860 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100861 return ret;
862}
863
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200864static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
865 unsigned int mask)
866{
867 struct snd_interval *interval;
868 int channels = hweight_long(mask);
869
870 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
871 interval->min = channels;
872 interval->max = channels;
873}
874
Benoit Cousson93e69582014-07-08 23:19:38 +0200875int soc_dai_hw_params(struct snd_pcm_substream *substream,
876 struct snd_pcm_hw_params *params,
877 struct snd_soc_dai *dai)
878{
879 int ret;
880
881 if (dai->driver->ops && dai->driver->ops->hw_params) {
882 ret = dai->driver->ops->hw_params(substream, params, dai);
883 if (ret < 0) {
884 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
885 dai->name, ret);
886 return ret;
887 }
888 }
889
890 return 0;
891}
892
Liam Girdwoodddee6272011-06-09 14:45:53 +0100893/*
894 * Called by ALSA when the hardware params are set by application. This
895 * function can also be called multiple times and can allocate buffers
896 * (using snd_pcm_lib_* ). It's non-atomic.
897 */
898static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
899 struct snd_pcm_hw_params *params)
900{
901 struct snd_soc_pcm_runtime *rtd = substream->private_data;
902 struct snd_soc_platform *platform = rtd->platform;
903 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200904 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100905
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100906 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100907
Banajit Goswami7ffe84e2017-01-10 17:28:14 -0800908 /* perform any hw_params fixups */
909 if ((rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) &&
910 rtd->dai_link->be_hw_params_fixup) {
911 ret = rtd->dai_link->be_hw_params_fixup(rtd,
912 params);
913 if (ret < 0)
914 dev_err(rtd->card->dev, "ASoC: fixup failed for %s\n",
915 rtd->dai_link->name);
916 }
917
Nicolin Chen3635bf02013-11-13 18:56:24 +0800918 ret = soc_pcm_params_symmetry(substream, params);
919 if (ret)
920 goto out;
921
Banajit Goswami7ffe84e2017-01-10 17:28:14 -0800922 /* perform any hw_params fixups */
923 if ((rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) &&
924 rtd->dai_link->be_hw_params_fixup) {
925 ret = rtd->dai_link->be_hw_params_fixup(rtd,
926 params);
927 if (ret < 0) {
928 dev_err(rtd->card->dev, "ASoC: fixup failed for %s\n",
929 rtd->dai_link->name);
930 }
931 }
932
Liam Girdwoodddee6272011-06-09 14:45:53 +0100933 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
934 ret = rtd->dai_link->ops->hw_params(substream, params);
935 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000936 dev_err(rtd->card->dev, "ASoC: machine hw_params"
937 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100938 goto out;
939 }
940 }
941
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200942 for (i = 0; i < rtd->num_codecs; i++) {
943 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
944 struct snd_pcm_hw_params codec_params;
945
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200946 /*
947 * Skip CODECs which don't support the current stream type,
948 * the idea being that if a CODEC is not used for the currently
949 * set up transfer direction, it should not need to be
950 * configured, especially since the configuration used might
951 * not even be supported by that CODEC. There may be cases
952 * however where a CODEC needs to be set up although it is
953 * actually not being used for the transfer, e.g. if a
954 * capture-only CODEC is acting as an LRCLK and/or BCLK master
955 * for the DAI link including a playback-only CODEC.
956 * If this becomes necessary, we will have to augment the
957 * machine driver setup with information on how to act, so
958 * we can do the right thing here.
959 */
960 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
961 continue;
962
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200963 /* copy params for each codec */
964 codec_params = *params;
965
966 /* fixup params based on TDM slot masks */
967 if (codec_dai->tx_mask)
968 soc_pcm_codec_params_fixup(&codec_params,
969 codec_dai->tx_mask);
970 if (codec_dai->rx_mask)
971 soc_pcm_codec_params_fixup(&codec_params,
972 codec_dai->rx_mask);
973
Benoit Cousson93e69582014-07-08 23:19:38 +0200974 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
975 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100976 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200977
978 codec_dai->rate = params_rate(&codec_params);
979 codec_dai->channels = params_channels(&codec_params);
980 codec_dai->sample_bits = snd_pcm_format_physical_width(
981 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100982 }
983
Benoit Cousson93e69582014-07-08 23:19:38 +0200984 ret = soc_dai_hw_params(substream, params, cpu_dai);
985 if (ret < 0)
986 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100987
988 if (platform->driver->ops && platform->driver->ops->hw_params) {
989 ret = platform->driver->ops->hw_params(substream, params);
990 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000991 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200992 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100993 goto platform_err;
994 }
995 }
996
Nicolin Chen3635bf02013-11-13 18:56:24 +0800997 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800998 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800999 cpu_dai->channels = params_channels(params);
1000 cpu_dai->sample_bits =
1001 snd_pcm_format_physical_width(params_format(params));
1002
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001003 /* malloc a page for hostless IO.
1004 * FIXME: rework with alsa-lib changes so that this malloc is
1005 * not required.
1006 */
1007 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
1008 substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
1009 substream->dma_buffer.dev.dev = rtd->dev;
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08001010 substream->dma_buffer.dev.dev->coherent_dma_mask =
1011 DMA_BIT_MASK(sizeof(dma_addr_t) * 8);
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001012 substream->dma_buffer.private_data = NULL;
1013
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08001014 arch_setup_dma_ops(substream->dma_buffer.dev.dev,
1015 0, 0, NULL, 0);
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001016 ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
1017 if (ret < 0)
1018 goto platform_err;
1019 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001020out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001021 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001022 return ret;
1023
1024platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -07001025 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001026 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1027
1028interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001029 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001030
1031codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001032 while (--i >= 0) {
1033 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1034 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1035 codec_dai->driver->ops->hw_free(substream, codec_dai);
1036 codec_dai->rate = 0;
1037 }
1038
Liam Girdwoodddee6272011-06-09 14:45:53 +01001039 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1040 rtd->dai_link->ops->hw_free(substream);
1041
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001042 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001043 return ret;
1044}
1045
1046/*
1047 * Frees resources allocated by hw_params, can be called multiple times
1048 */
1049static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1050{
1051 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1052 struct snd_soc_platform *platform = rtd->platform;
1053 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001054 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001055 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001056 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001057
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001058 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001059
Nicolin Chend3383422013-11-20 18:37:09 +08001060 /* clear the corresponding DAIs parameters when going to be inactive */
1061 if (cpu_dai->active == 1) {
1062 cpu_dai->rate = 0;
1063 cpu_dai->channels = 0;
1064 cpu_dai->sample_bits = 0;
1065 }
1066
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001067 for (i = 0; i < rtd->num_codecs; i++) {
1068 codec_dai = rtd->codec_dais[i];
1069 if (codec_dai->active == 1) {
1070 codec_dai->rate = 0;
1071 codec_dai->channels = 0;
1072 codec_dai->sample_bits = 0;
1073 }
Nicolin Chend3383422013-11-20 18:37:09 +08001074 }
1075
Liam Girdwoodddee6272011-06-09 14:45:53 +01001076 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001077 for (i = 0; i < rtd->num_codecs; i++) {
1078 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1079 (!playback && rtd->codec_dais[i]->capture_active == 1))
1080 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1081 substream->stream);
1082 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001083
1084 /* free any machine hw params */
1085 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1086 rtd->dai_link->ops->hw_free(substream);
1087
1088 /* free any DMA resources */
1089 if (platform->driver->ops && platform->driver->ops->hw_free)
1090 platform->driver->ops->hw_free(substream);
1091
1092 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001093 for (i = 0; i < rtd->num_codecs; i++) {
1094 codec_dai = rtd->codec_dais[i];
1095 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1096 codec_dai->driver->ops->hw_free(substream, codec_dai);
1097 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001098
Mark Brownc5914b02013-10-30 17:47:39 -07001099 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001100 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1101
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001102 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
1103 snd_pcm_lib_free_pages(substream);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001104 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001105 return 0;
1106}
1107
1108static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1109{
1110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1111 struct snd_soc_platform *platform = rtd->platform;
1112 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001113 struct snd_soc_dai *codec_dai;
1114 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001115
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001116 for (i = 0; i < rtd->num_codecs; i++) {
1117 codec_dai = rtd->codec_dais[i];
1118 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1119 ret = codec_dai->driver->ops->trigger(substream,
1120 cmd, codec_dai);
1121 if (ret < 0)
1122 return ret;
1123 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001124 }
1125
1126 if (platform->driver->ops && platform->driver->ops->trigger) {
1127 ret = platform->driver->ops->trigger(substream, cmd);
1128 if (ret < 0)
1129 return ret;
1130 }
1131
Mark Brownc5914b02013-10-30 17:47:39 -07001132 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001133 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1134 if (ret < 0)
1135 return ret;
1136 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001137
1138 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1139 ret = rtd->dai_link->ops->trigger(substream, cmd);
1140 if (ret < 0)
1141 return ret;
1142 }
1143
Liam Girdwoodddee6272011-06-09 14:45:53 +01001144 return 0;
1145}
1146
Mark Brown45c0a182012-05-09 21:46:27 +01001147static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1148 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001149{
1150 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1151 struct snd_soc_platform *platform = rtd->platform;
1152 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001153 struct snd_soc_dai *codec_dai;
1154 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001155
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001156 for (i = 0; i < rtd->num_codecs; i++) {
1157 codec_dai = rtd->codec_dais[i];
1158 if (codec_dai->driver->ops &&
1159 codec_dai->driver->ops->bespoke_trigger) {
1160 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1161 cmd, codec_dai);
1162 if (ret < 0)
1163 return ret;
1164 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001165 }
1166
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +01001167 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001168 ret = platform->driver->bespoke_trigger(substream, cmd);
1169 if (ret < 0)
1170 return ret;
1171 }
1172
Mark Brownc5914b02013-10-30 17:47:39 -07001173 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001174 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1175 if (ret < 0)
1176 return ret;
1177 }
1178 return 0;
1179}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001180/*
1181 * soc level wrapper for pointer callback
1182 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1183 * the runtime->delay will be updated accordingly.
1184 */
1185static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1186{
1187 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1188 struct snd_soc_platform *platform = rtd->platform;
1189 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001190 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001191 struct snd_pcm_runtime *runtime = substream->runtime;
1192 snd_pcm_uframes_t offset = 0;
1193 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001194 snd_pcm_sframes_t codec_delay = 0;
1195 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001196
1197 if (platform->driver->ops && platform->driver->ops->pointer)
1198 offset = platform->driver->ops->pointer(substream);
1199
Mark Brownc5914b02013-10-30 17:47:39 -07001200 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001201 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1202
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001203 for (i = 0; i < rtd->num_codecs; i++) {
1204 codec_dai = rtd->codec_dais[i];
1205 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1206 codec_delay = max(codec_delay,
1207 codec_dai->driver->ops->delay(substream,
1208 codec_dai));
1209 }
1210 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001211
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001212 /*
1213 * None of the existing platform drivers implement delay(), so
1214 * for now the codec_dai of first multicodec entry is used
1215 */
Liam Girdwoodddee6272011-06-09 14:45:53 +01001216 if (platform->driver->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001217 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001218
1219 runtime->delay = delay;
1220
1221 return offset;
1222}
1223
Liam Girdwood01d75842012-04-25 12:12:49 +01001224/* connect a FE and BE */
1225static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1226 struct snd_soc_pcm_runtime *be, int stream)
1227{
1228 struct snd_soc_dpcm *dpcm;
1229
1230 /* only add new dpcms */
1231 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1232 if (dpcm->be == be && dpcm->fe == fe)
1233 return 0;
1234 }
1235
1236 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1237 if (!dpcm)
1238 return -ENOMEM;
1239
1240 dpcm->be = be;
1241 dpcm->fe = fe;
1242 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1243 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1244 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1245 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1246
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001247 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001248 stream ? "capture" : "playback", fe->dai_link->name,
1249 stream ? "<-" : "->", be->dai_link->name);
1250
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001251#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001252 if (fe->debugfs_dpcm_root)
1253 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1254 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001255#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001256 return 1;
1257}
1258
1259/* reparent a BE onto another FE */
1260static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1261 struct snd_soc_pcm_runtime *be, int stream)
1262{
1263 struct snd_soc_dpcm *dpcm;
1264 struct snd_pcm_substream *fe_substream, *be_substream;
1265
1266 /* reparent if BE is connected to other FEs */
1267 if (!be->dpcm[stream].users)
1268 return;
1269
1270 be_substream = snd_soc_dpcm_get_substream(be, stream);
1271
1272 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1273 if (dpcm->fe == fe)
1274 continue;
1275
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001276 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001277 stream ? "capture" : "playback",
1278 dpcm->fe->dai_link->name,
1279 stream ? "<-" : "->", dpcm->be->dai_link->name);
1280
1281 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1282 be_substream->runtime = fe_substream->runtime;
1283 break;
1284 }
1285}
1286
1287/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001288void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001289{
1290 struct snd_soc_dpcm *dpcm, *d;
1291
1292 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001293 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001294 stream ? "capture" : "playback",
1295 dpcm->be->dai_link->name);
1296
1297 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1298 continue;
1299
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001300 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001301 stream ? "capture" : "playback", fe->dai_link->name,
1302 stream ? "<-" : "->", dpcm->be->dai_link->name);
1303
1304 /* BEs still alive need new FE */
1305 dpcm_be_reparent(fe, dpcm->be, stream);
1306
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001307#ifdef CONFIG_DEBUG_FS
1308 debugfs_remove(dpcm->debugfs_state);
1309#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001310 list_del(&dpcm->list_be);
1311 list_del(&dpcm->list_fe);
1312 kfree(dpcm);
1313 }
1314}
1315
1316/* get BE for DAI widget and stream */
1317static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1318 struct snd_soc_dapm_widget *widget, int stream)
1319{
1320 struct snd_soc_pcm_runtime *be;
Mengdong Lin1a497982015-11-18 02:34:11 -05001321 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001322
1323 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001324 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001325
Liam Girdwood35ea0652012-06-05 19:26:59 +01001326 if (!be->dai_link->no_pcm)
1327 continue;
1328
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001329 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001330 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001331
Mengdong Lin1a497982015-11-18 02:34:11 -05001332 for (i = 0; i < be->num_codecs; i++) {
1333 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001334 if (dai->playback_widget == widget)
1335 return be;
1336 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001337 }
1338 } else {
1339
Mengdong Lin1a497982015-11-18 02:34:11 -05001340 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001341
Liam Girdwood35ea0652012-06-05 19:26:59 +01001342 if (!be->dai_link->no_pcm)
1343 continue;
1344
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001345 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001346 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001347
Mengdong Lin1a497982015-11-18 02:34:11 -05001348 for (i = 0; i < be->num_codecs; i++) {
1349 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001350 if (dai->capture_widget == widget)
1351 return be;
1352 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001353 }
1354 }
1355
Liam Girdwood103d84a2012-11-19 14:39:15 +00001356 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001357 stream ? "capture" : "playback", widget->name);
1358 return NULL;
1359}
1360
1361static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001362 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001363{
1364 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001365 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001366 else
Benoit Cousson37018612014-04-24 14:01:45 +02001367 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001368}
1369
1370static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1371 struct snd_soc_dapm_widget *widget)
1372{
1373 int i;
1374
1375 for (i = 0; i < list->num_widgets; i++) {
1376 if (widget == list->widgets[i])
1377 return 1;
1378 }
1379
1380 return 0;
1381}
1382
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001383static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1384 enum snd_soc_dapm_direction dir)
1385{
1386 struct snd_soc_card *card = widget->dapm->card;
1387 struct snd_soc_pcm_runtime *rtd;
1388 int i;
1389
1390 if (dir == SND_SOC_DAPM_DIR_OUT) {
1391 list_for_each_entry(rtd, &card->rtd_list, list) {
1392 if (!rtd->dai_link->no_pcm)
1393 continue;
1394
1395 if (rtd->cpu_dai->playback_widget == widget)
1396 return true;
1397
1398 for (i = 0; i < rtd->num_codecs; ++i) {
1399 struct snd_soc_dai *dai = rtd->codec_dais[i];
1400 if (dai->playback_widget == widget)
1401 return true;
1402 }
1403 }
1404 } else { /* SND_SOC_DAPM_DIR_IN */
1405 list_for_each_entry(rtd, &card->rtd_list, list) {
1406 if (!rtd->dai_link->no_pcm)
1407 continue;
1408
1409 if (rtd->cpu_dai->capture_widget == widget)
1410 return true;
1411
1412 for (i = 0; i < rtd->num_codecs; ++i) {
1413 struct snd_soc_dai *dai = rtd->codec_dais[i];
1414 if (dai->capture_widget == widget)
1415 return true;
1416 }
1417 }
1418 }
1419
1420 return false;
1421}
1422
Liam Girdwood23607022014-01-17 17:03:55 +00001423int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001424 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001425{
1426 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001427 int paths;
1428
Liam Girdwood01d75842012-04-25 12:12:49 +01001429 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001430 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001431 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001432
Liam Girdwood103d84a2012-11-19 14:39:15 +00001433 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001434 stream ? "capture" : "playback");
1435
Liam Girdwood01d75842012-04-25 12:12:49 +01001436 return paths;
1437}
1438
Liam Girdwood01d75842012-04-25 12:12:49 +01001439static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1440 struct snd_soc_dapm_widget_list **list_)
1441{
1442 struct snd_soc_dpcm *dpcm;
1443 struct snd_soc_dapm_widget_list *list = *list_;
1444 struct snd_soc_dapm_widget *widget;
1445 int prune = 0;
1446
1447 /* Destroy any old FE <--> BE connections */
1448 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001449 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001450
1451 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001452 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001453
1454 /* prune the BE if it's no longer in our active list */
1455 if (widget && widget_in_list(list, widget))
1456 continue;
1457
1458 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001459 for (i = 0; i < dpcm->be->num_codecs; i++) {
1460 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1461 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001462
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001463 /* prune the BE if it's no longer in our active list */
1464 if (widget && widget_in_list(list, widget))
1465 continue;
1466 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001467
Liam Girdwood103d84a2012-11-19 14:39:15 +00001468 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001469 stream ? "capture" : "playback",
1470 dpcm->be->dai_link->name, fe->dai_link->name);
1471 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1472 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1473 prune++;
1474 }
1475
Liam Girdwood103d84a2012-11-19 14:39:15 +00001476 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001477 return prune;
1478}
1479
1480static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1481 struct snd_soc_dapm_widget_list **list_)
1482{
1483 struct snd_soc_card *card = fe->card;
1484 struct snd_soc_dapm_widget_list *list = *list_;
1485 struct snd_soc_pcm_runtime *be;
1486 int i, new = 0, err;
1487
1488 /* Create any new FE <--> BE connections */
1489 for (i = 0; i < list->num_widgets; i++) {
1490
Mark Brown46162742013-06-05 19:36:11 +01001491 switch (list->widgets[i]->id) {
1492 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001493 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1494 continue;
1495 break;
Mark Brown46162742013-06-05 19:36:11 +01001496 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001497 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1498 continue;
Mark Brown46162742013-06-05 19:36:11 +01001499 break;
1500 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001501 continue;
Mark Brown46162742013-06-05 19:36:11 +01001502 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001503
1504 /* is there a valid BE rtd for this widget */
1505 be = dpcm_get_be(card, list->widgets[i], stream);
1506 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001507 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001508 list->widgets[i]->name);
1509 continue;
1510 }
1511
1512 /* make sure BE is a real BE */
1513 if (!be->dai_link->no_pcm)
1514 continue;
1515
1516 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001517 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001518 continue;
1519
1520 /* newly connected FE and BE */
1521 err = dpcm_be_connect(fe, be, stream);
1522 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001523 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001524 list->widgets[i]->name);
1525 break;
1526 } else if (err == 0) /* already connected */
1527 continue;
1528
1529 /* new */
1530 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1531 new++;
1532 }
1533
Liam Girdwood103d84a2012-11-19 14:39:15 +00001534 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001535 return new;
1536}
1537
1538/*
1539 * Find the corresponding BE DAIs that source or sink audio to this
1540 * FE substream.
1541 */
Liam Girdwood23607022014-01-17 17:03:55 +00001542int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001543 int stream, struct snd_soc_dapm_widget_list **list, int new)
1544{
1545 if (new)
1546 return dpcm_add_paths(fe, stream, list);
1547 else
1548 return dpcm_prune_paths(fe, stream, list);
1549}
1550
Liam Girdwood23607022014-01-17 17:03:55 +00001551void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001552{
1553 struct snd_soc_dpcm *dpcm;
1554
1555 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1556 dpcm->be->dpcm[stream].runtime_update =
1557 SND_SOC_DPCM_UPDATE_NO;
1558}
1559
1560static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1561 int stream)
1562{
1563 struct snd_soc_dpcm *dpcm;
1564
1565 /* disable any enabled and non active backends */
1566 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1567
1568 struct snd_soc_pcm_runtime *be = dpcm->be;
1569 struct snd_pcm_substream *be_substream =
1570 snd_soc_dpcm_get_substream(be, stream);
1571
1572 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001573 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001574 stream ? "capture" : "playback",
1575 be->dpcm[stream].state);
1576
1577 if (--be->dpcm[stream].users != 0)
1578 continue;
1579
1580 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1581 continue;
1582
1583 soc_pcm_close(be_substream);
1584 be_substream->runtime = NULL;
1585 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1586 }
1587}
1588
Liam Girdwood23607022014-01-17 17:03:55 +00001589int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001590{
1591 struct snd_soc_dpcm *dpcm;
1592 int err, count = 0;
1593
1594 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1595 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1596
1597 struct snd_soc_pcm_runtime *be = dpcm->be;
1598 struct snd_pcm_substream *be_substream =
1599 snd_soc_dpcm_get_substream(be, stream);
1600
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001601 if (!be_substream) {
1602 dev_err(be->dev, "ASoC: no backend %s stream\n",
1603 stream ? "capture" : "playback");
1604 continue;
1605 }
1606
Liam Girdwood01d75842012-04-25 12:12:49 +01001607 /* is this op for this BE ? */
1608 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1609 continue;
1610
1611 /* first time the dpcm is open ? */
1612 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001613 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001614 stream ? "capture" : "playback",
1615 be->dpcm[stream].state);
1616
1617 if (be->dpcm[stream].users++ != 0)
1618 continue;
1619
1620 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1621 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1622 continue;
1623
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001624 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1625 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001626
1627 be_substream->runtime = be->dpcm[stream].runtime;
1628 err = soc_pcm_open(be_substream);
1629 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001630 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001631 be->dpcm[stream].users--;
1632 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001633 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001634 stream ? "capture" : "playback",
1635 be->dpcm[stream].state);
1636
1637 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1638 goto unwind;
1639 }
1640
1641 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1642 count++;
1643 }
1644
1645 return count;
1646
1647unwind:
1648 /* disable any enabled and non active backends */
1649 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1650 struct snd_soc_pcm_runtime *be = dpcm->be;
1651 struct snd_pcm_substream *be_substream =
1652 snd_soc_dpcm_get_substream(be, stream);
1653
1654 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1655 continue;
1656
1657 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001658 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001659 stream ? "capture" : "playback",
1660 be->dpcm[stream].state);
1661
1662 if (--be->dpcm[stream].users != 0)
1663 continue;
1664
1665 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1666 continue;
1667
1668 soc_pcm_close(be_substream);
1669 be_substream->runtime = NULL;
1670 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1671 }
1672
1673 return err;
1674}
1675
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001676static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001677 struct snd_soc_pcm_stream *stream,
1678 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001679{
1680 runtime->hw.rate_min = stream->rate_min;
1681 runtime->hw.rate_max = stream->rate_max;
1682 runtime->hw.channels_min = stream->channels_min;
1683 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001684 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001685 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001686 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001687 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001688 runtime->hw.rates = stream->rates;
1689}
1690
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001691static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1692{
1693 struct snd_soc_pcm_runtime *fe = substream->private_data;
1694 struct snd_soc_dpcm *dpcm;
1695 u64 formats = ULLONG_MAX;
1696 int stream = substream->stream;
1697
1698 if (!fe->dai_link->dpcm_merged_format)
1699 return formats;
1700
1701 /*
1702 * It returns merged BE codec format
1703 * if FE want to use it (= dpcm_merged_format)
1704 */
1705
1706 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1707 struct snd_soc_pcm_runtime *be = dpcm->be;
1708 struct snd_soc_dai_driver *codec_dai_drv;
1709 struct snd_soc_pcm_stream *codec_stream;
1710 int i;
1711
1712 for (i = 0; i < be->num_codecs; i++) {
1713 codec_dai_drv = be->codec_dais[i]->driver;
1714 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1715 codec_stream = &codec_dai_drv->playback;
1716 else
1717 codec_stream = &codec_dai_drv->capture;
1718
1719 formats &= codec_stream->formats;
1720 }
1721 }
1722
1723 return formats;
1724}
1725
Mark Brown45c0a182012-05-09 21:46:27 +01001726static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001727{
1728 struct snd_pcm_runtime *runtime = substream->runtime;
1729 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1730 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1731 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001732 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001733
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001734 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001735 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001736 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001737 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001738}
1739
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001740static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1741
1742/* Set FE's runtime_update state; the state is protected via PCM stream lock
1743 * for avoiding the race with trigger callback.
1744 * If the state is unset and a trigger is pending while the previous operation,
1745 * process the pending trigger action here.
1746 */
1747static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1748 int stream, enum snd_soc_dpcm_update state)
1749{
1750 struct snd_pcm_substream *substream =
1751 snd_soc_dpcm_get_substream(fe, stream);
1752
1753 snd_pcm_stream_lock_irq(substream);
1754 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1755 dpcm_fe_dai_do_trigger(substream,
1756 fe->dpcm[stream].trigger_pending - 1);
1757 fe->dpcm[stream].trigger_pending = 0;
1758 }
1759 fe->dpcm[stream].runtime_update = state;
1760 snd_pcm_stream_unlock_irq(substream);
1761}
1762
PC Liao906c7d62015-12-11 11:33:51 +08001763static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1764 int stream)
1765{
1766 struct snd_soc_dpcm *dpcm;
1767 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1768 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1769 int err;
1770
1771 /* apply symmetry for FE */
1772 if (soc_pcm_has_symmetry(fe_substream))
1773 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1774
1775 /* Symmetry only applies if we've got an active stream. */
1776 if (fe_cpu_dai->active) {
1777 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1778 if (err < 0)
1779 return err;
1780 }
1781
1782 /* apply symmetry for BE */
1783 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1784 struct snd_soc_pcm_runtime *be = dpcm->be;
1785 struct snd_pcm_substream *be_substream =
1786 snd_soc_dpcm_get_substream(be, stream);
1787 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1788 int i;
1789
Jeeja KPf1176612016-09-06 14:17:55 +05301790 if (rtd->dai_link->be_hw_params_fixup)
1791 continue;
1792
PC Liao906c7d62015-12-11 11:33:51 +08001793 if (soc_pcm_has_symmetry(be_substream))
1794 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1795
1796 /* Symmetry only applies if we've got an active stream. */
1797 if (rtd->cpu_dai->active) {
1798 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1799 if (err < 0)
1800 return err;
1801 }
1802
1803 for (i = 0; i < rtd->num_codecs; i++) {
1804 if (rtd->codec_dais[i]->active) {
1805 err = soc_pcm_apply_symmetry(be_substream,
1806 rtd->codec_dais[i]);
1807 if (err < 0)
1808 return err;
1809 }
1810 }
1811 }
1812
1813 return 0;
1814}
1815
Liam Girdwood01d75842012-04-25 12:12:49 +01001816static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1817{
1818 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1819 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1820 int stream = fe_substream->stream, ret = 0;
1821
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001822 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001823
1824 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1825 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001826 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001827 goto be_err;
1828 }
1829
Liam Girdwood103d84a2012-11-19 14:39:15 +00001830 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001831
1832 /* start the DAI frontend */
1833 ret = soc_pcm_open(fe_substream);
1834 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001835 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001836 goto unwind;
1837 }
1838
1839 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1840
1841 dpcm_set_fe_runtime(fe_substream);
1842 snd_pcm_limit_hw_rates(runtime);
1843
PC Liao906c7d62015-12-11 11:33:51 +08001844 ret = dpcm_apply_symmetry(fe_substream, stream);
1845 if (ret < 0) {
1846 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1847 ret);
1848 goto unwind;
1849 }
1850
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001851 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001852 return 0;
1853
1854unwind:
1855 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1856be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001857 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001858 return ret;
1859}
1860
Liam Girdwood23607022014-01-17 17:03:55 +00001861int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001862{
1863 struct snd_soc_dpcm *dpcm;
1864
1865 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1866 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1867
1868 struct snd_soc_pcm_runtime *be = dpcm->be;
1869 struct snd_pcm_substream *be_substream =
1870 snd_soc_dpcm_get_substream(be, stream);
1871
1872 /* is this op for this BE ? */
1873 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1874 continue;
1875
1876 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001877 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001878 stream ? "capture" : "playback",
1879 be->dpcm[stream].state);
1880
1881 if (--be->dpcm[stream].users != 0)
1882 continue;
1883
1884 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1885 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1886 continue;
1887
Liam Girdwood103d84a2012-11-19 14:39:15 +00001888 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001889 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001890
1891 soc_pcm_close(be_substream);
1892 be_substream->runtime = NULL;
1893
1894 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1895 }
1896 return 0;
1897}
1898
1899static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1900{
1901 struct snd_soc_pcm_runtime *fe = substream->private_data;
1902 int stream = substream->stream;
1903
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001904 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001905
1906 /* shutdown the BEs */
1907 dpcm_be_dai_shutdown(fe, substream->stream);
1908
Liam Girdwood103d84a2012-11-19 14:39:15 +00001909 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001910
1911 /* now shutdown the frontend */
1912 soc_pcm_close(substream);
1913
1914 /* run the stream event for each BE */
1915 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1916
1917 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001918 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001919 return 0;
1920}
1921
Liam Girdwood23607022014-01-17 17:03:55 +00001922int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001923{
1924 struct snd_soc_dpcm *dpcm;
1925
1926 /* only hw_params backends that are either sinks or sources
1927 * to this frontend DAI */
1928 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1929
1930 struct snd_soc_pcm_runtime *be = dpcm->be;
1931 struct snd_pcm_substream *be_substream =
1932 snd_soc_dpcm_get_substream(be, stream);
1933
1934 /* is this op for this BE ? */
1935 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1936 continue;
1937
1938 /* only free hw when no longer used - check all FEs */
1939 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1940 continue;
1941
Qiao Zhou36fba622014-12-03 10:13:43 +08001942 /* do not free hw if this BE is used by other FE */
1943 if (be->dpcm[stream].users > 1)
1944 continue;
1945
Liam Girdwood01d75842012-04-25 12:12:49 +01001946 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1947 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1948 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001949 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301950 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1951 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001952 continue;
1953
Liam Girdwood103d84a2012-11-19 14:39:15 +00001954 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001955 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001956
1957 soc_pcm_hw_free(be_substream);
1958
1959 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1960 }
1961
1962 return 0;
1963}
1964
Mark Brown45c0a182012-05-09 21:46:27 +01001965static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001966{
1967 struct snd_soc_pcm_runtime *fe = substream->private_data;
1968 int err, stream = substream->stream;
1969
1970 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001971 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001972
Liam Girdwood103d84a2012-11-19 14:39:15 +00001973 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001974
1975 /* call hw_free on the frontend */
1976 err = soc_pcm_hw_free(substream);
1977 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001978 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001979 fe->dai_link->name);
1980
1981 /* only hw_params backends that are either sinks or sources
1982 * to this frontend DAI */
1983 err = dpcm_be_dai_hw_free(fe, stream);
1984
1985 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001986 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001987
1988 mutex_unlock(&fe->card->mutex);
1989 return 0;
1990}
1991
Liam Girdwood23607022014-01-17 17:03:55 +00001992int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001993{
1994 struct snd_soc_dpcm *dpcm;
1995 int ret;
1996
1997 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1998
1999 struct snd_soc_pcm_runtime *be = dpcm->be;
2000 struct snd_pcm_substream *be_substream =
2001 snd_soc_dpcm_get_substream(be, stream);
2002
2003 /* is this op for this BE ? */
2004 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2005 continue;
2006
Liam Girdwood01d75842012-04-25 12:12:49 +01002007 /* copy params for each dpcm */
2008 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2009 sizeof(struct snd_pcm_hw_params));
2010
2011 /* perform any hw_params fixups */
2012 if (be->dai_link->be_hw_params_fixup) {
2013 ret = be->dai_link->be_hw_params_fixup(be,
2014 &dpcm->hw_params);
2015 if (ret < 0) {
2016 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002017 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002018 ret);
2019 goto unwind;
2020 }
2021 }
2022
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002023 /* only allow hw_params() if no connected FEs are running */
2024 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2025 continue;
2026
2027 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2028 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2029 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2030 continue;
2031
2032 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002033 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002034
Liam Girdwood01d75842012-04-25 12:12:49 +01002035 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2036 if (ret < 0) {
2037 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002038 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002039 goto unwind;
2040 }
2041
2042 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2043 }
2044 return 0;
2045
2046unwind:
2047 /* disable any enabled and non active backends */
2048 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2049 struct snd_soc_pcm_runtime *be = dpcm->be;
2050 struct snd_pcm_substream *be_substream =
2051 snd_soc_dpcm_get_substream(be, stream);
2052
2053 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2054 continue;
2055
2056 /* only allow hw_free() if no connected FEs are running */
2057 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2058 continue;
2059
2060 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2061 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2062 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2063 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2064 continue;
2065
2066 soc_pcm_hw_free(be_substream);
2067 }
2068
2069 return ret;
2070}
2071
Mark Brown45c0a182012-05-09 21:46:27 +01002072static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2073 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002074{
2075 struct snd_soc_pcm_runtime *fe = substream->private_data;
2076 int ret, stream = substream->stream;
2077
2078 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002079 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002080
2081 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2082 sizeof(struct snd_pcm_hw_params));
2083 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2084 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002085 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002086 goto out;
2087 }
2088
Liam Girdwood103d84a2012-11-19 14:39:15 +00002089 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002090 fe->dai_link->name, params_rate(params),
2091 params_channels(params), params_format(params));
2092
2093 /* call hw_params on the frontend */
2094 ret = soc_pcm_hw_params(substream, params);
2095 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002096 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002097 dpcm_be_dai_hw_free(fe, stream);
2098 } else
2099 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2100
2101out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002102 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002103 mutex_unlock(&fe->card->mutex);
2104 return ret;
2105}
2106
2107static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2108 struct snd_pcm_substream *substream, int cmd)
2109{
2110 int ret;
2111
Liam Girdwood103d84a2012-11-19 14:39:15 +00002112 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002113 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002114
2115 ret = soc_pcm_trigger(substream, cmd);
2116 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002117 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002118
2119 return ret;
2120}
2121
Liam Girdwood23607022014-01-17 17:03:55 +00002122int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002123 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002124{
2125 struct snd_soc_dpcm *dpcm;
2126 int ret = 0;
2127
2128 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2129
2130 struct snd_soc_pcm_runtime *be = dpcm->be;
2131 struct snd_pcm_substream *be_substream =
2132 snd_soc_dpcm_get_substream(be, stream);
2133
2134 /* is this op for this BE ? */
2135 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2136 continue;
2137
2138 switch (cmd) {
2139 case SNDRV_PCM_TRIGGER_START:
2140 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2141 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2142 continue;
2143
2144 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2145 if (ret)
2146 return ret;
2147
2148 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2149 break;
2150 case SNDRV_PCM_TRIGGER_RESUME:
2151 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2152 continue;
2153
2154 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2155 if (ret)
2156 return ret;
2157
2158 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2159 break;
2160 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2161 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2162 continue;
2163
2164 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2165 if (ret)
2166 return ret;
2167
2168 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2169 break;
2170 case SNDRV_PCM_TRIGGER_STOP:
2171 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2172 continue;
2173
2174 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2175 continue;
2176
2177 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2178 if (ret)
2179 return ret;
2180
2181 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2182 break;
2183 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002184 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002185 continue;
2186
2187 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2188 continue;
2189
2190 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2191 if (ret)
2192 return ret;
2193
2194 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2195 break;
2196 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2197 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2198 continue;
2199
2200 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2201 continue;
2202
2203 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2204 if (ret)
2205 return ret;
2206
2207 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2208 break;
2209 }
2210 }
2211
2212 return ret;
2213}
2214EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2215
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002216static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002217{
2218 struct snd_soc_pcm_runtime *fe = substream->private_data;
2219 int stream = substream->stream, ret;
2220 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2221
2222 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2223
2224 switch (trigger) {
2225 case SND_SOC_DPCM_TRIGGER_PRE:
2226 /* call trigger on the frontend before the backend. */
2227
Liam Girdwood103d84a2012-11-19 14:39:15 +00002228 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002229 fe->dai_link->name, cmd);
2230
2231 ret = soc_pcm_trigger(substream, cmd);
2232 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002233 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002234 goto out;
2235 }
2236
2237 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2238 break;
2239 case SND_SOC_DPCM_TRIGGER_POST:
2240 /* call trigger on the frontend after the backend. */
2241
2242 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2243 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002244 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002245 goto out;
2246 }
2247
Liam Girdwood103d84a2012-11-19 14:39:15 +00002248 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002249 fe->dai_link->name, cmd);
2250
2251 ret = soc_pcm_trigger(substream, cmd);
2252 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002253 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2254 /* bespoke trigger() - handles both FE and BEs */
2255
Liam Girdwood103d84a2012-11-19 14:39:15 +00002256 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002257 fe->dai_link->name, cmd);
2258
2259 ret = soc_pcm_bespoke_trigger(substream, cmd);
2260 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002261 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002262 goto out;
2263 }
2264 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002265 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002266 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002267 fe->dai_link->name);
2268 ret = -EINVAL;
2269 goto out;
2270 }
2271
2272 switch (cmd) {
2273 case SNDRV_PCM_TRIGGER_START:
2274 case SNDRV_PCM_TRIGGER_RESUME:
2275 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2276 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2277 break;
2278 case SNDRV_PCM_TRIGGER_STOP:
2279 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002280 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2281 break;
Banajit Goswami6175bce2013-02-14 18:24:08 -08002282 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2283 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2284 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002285 }
2286
2287out:
2288 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2289 return ret;
2290}
2291
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002292static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2293{
2294 struct snd_soc_pcm_runtime *fe = substream->private_data;
2295 int stream = substream->stream;
2296
2297 /* if FE's runtime_update is already set, we're in race;
2298 * process this trigger later at exit
2299 */
2300 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2301 fe->dpcm[stream].trigger_pending = cmd + 1;
2302 return 0; /* delayed, assuming it's successful */
2303 }
2304
2305 /* we're alone, let's trigger */
2306 return dpcm_fe_dai_do_trigger(substream, cmd);
2307}
2308
Liam Girdwood23607022014-01-17 17:03:55 +00002309int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002310{
2311 struct snd_soc_dpcm *dpcm;
2312 int ret = 0;
2313
2314 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2315
2316 struct snd_soc_pcm_runtime *be = dpcm->be;
2317 struct snd_pcm_substream *be_substream =
2318 snd_soc_dpcm_get_substream(be, stream);
2319
2320 /* is this op for this BE ? */
2321 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2322 continue;
2323
2324 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002325 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2326 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002327 continue;
2328
Liam Girdwood103d84a2012-11-19 14:39:15 +00002329 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002330 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002331
2332 ret = soc_pcm_prepare(be_substream);
2333 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002334 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002335 ret);
2336 break;
2337 }
2338
2339 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2340 }
2341 return ret;
2342}
2343
Mark Brown45c0a182012-05-09 21:46:27 +01002344static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002345{
2346 struct snd_soc_pcm_runtime *fe = substream->private_data;
2347 int stream = substream->stream, ret = 0;
2348
2349 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2350
Liam Girdwood103d84a2012-11-19 14:39:15 +00002351 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002352
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002353 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002354
2355 /* there is no point preparing this FE if there are no BEs */
2356 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002357 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002358 fe->dai_link->name);
2359 ret = -EINVAL;
2360 goto out;
2361 }
2362
2363 ret = dpcm_be_dai_prepare(fe, substream->stream);
2364 if (ret < 0)
2365 goto out;
2366
2367 /* call prepare on the frontend */
2368 ret = soc_pcm_prepare(substream);
2369 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002370 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002371 fe->dai_link->name);
2372 goto out;
2373 }
2374
2375 /* run the stream event for each BE */
2376 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2377 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2378
2379out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002380 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002381 mutex_unlock(&fe->card->mutex);
2382
2383 return ret;
2384}
2385
Gopikrishnaiah Anandbdc375e2014-05-30 11:29:56 -07002386static int soc_pcm_compat_ioctl(struct snd_pcm_substream *substream,
2387 unsigned int cmd, void *arg)
2388{
2389 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2390 struct snd_soc_platform *platform = rtd->platform;
2391
2392 if (platform->driver->ops->compat_ioctl)
2393 return platform->driver->ops->compat_ioctl(substream,
2394 cmd, arg);
2395 return snd_pcm_lib_ioctl(substream, cmd, arg);
2396}
2397
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002398static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2399 unsigned int cmd, void *arg)
2400{
2401 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2402 struct snd_soc_platform *platform = rtd->platform;
2403
Mark Brownc5914b02013-10-30 17:47:39 -07002404 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002405 return platform->driver->ops->ioctl(substream, cmd, arg);
2406 return snd_pcm_lib_ioctl(substream, cmd, arg);
2407}
2408
Liam Girdwood618dae12012-04-25 12:12:51 +01002409static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2410{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002411 struct snd_pcm_substream *substream =
2412 snd_soc_dpcm_get_substream(fe, stream);
2413 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002414 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002415
Liam Girdwood103d84a2012-11-19 14:39:15 +00002416 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002417 stream ? "capture" : "playback", fe->dai_link->name);
2418
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002419 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2420 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002421 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002422 fe->dai_link->name);
2423
2424 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2425 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002426 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002427 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002428 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002429 fe->dai_link->name);
2430
2431 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2432 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002433 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002434 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002435
2436 err = dpcm_be_dai_hw_free(fe, stream);
2437 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002438 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002439
2440 err = dpcm_be_dai_shutdown(fe, stream);
2441 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002442 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002443
2444 /* run the stream event for each BE */
2445 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2446
2447 return 0;
2448}
2449
2450static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2451{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002452 struct snd_pcm_substream *substream =
2453 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002454 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002455 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002456 int ret;
2457
Liam Girdwood103d84a2012-11-19 14:39:15 +00002458 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002459 stream ? "capture" : "playback", fe->dai_link->name);
2460
2461 /* Only start the BE if the FE is ready */
2462 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2463 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2464 return -EINVAL;
2465
2466 /* startup must always be called for new BEs */
2467 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002468 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002469 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002470
2471 /* keep going if FE state is > open */
2472 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2473 return 0;
2474
2475 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002476 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002477 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002478
2479 /* keep going if FE state is > hw_params */
2480 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2481 return 0;
2482
2483
2484 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002485 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002486 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002487
2488 /* run the stream event for each BE */
2489 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2490
2491 /* keep going if FE state is > prepare */
2492 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2493 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2494 return 0;
2495
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002496 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2497 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002498 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002499 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002500
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002501 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2502 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002503 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002504 goto hw_free;
2505 }
2506 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002507 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002508 fe->dai_link->name);
2509
2510 ret = dpcm_be_dai_trigger(fe, stream,
2511 SNDRV_PCM_TRIGGER_START);
2512 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002513 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002514 goto hw_free;
2515 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002516 }
2517
2518 return 0;
2519
2520hw_free:
2521 dpcm_be_dai_hw_free(fe, stream);
2522close:
2523 dpcm_be_dai_shutdown(fe, stream);
2524disconnect:
2525 /* disconnect any non started BEs */
2526 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2527 struct snd_soc_pcm_runtime *be = dpcm->be;
2528 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2529 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2530 }
2531
2532 return ret;
2533}
2534
2535static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2536{
2537 int ret;
2538
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002539 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002540 ret = dpcm_run_update_startup(fe, stream);
2541 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002542 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002543 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002544
2545 return ret;
2546}
2547
2548static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2549{
2550 int ret;
2551
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002552 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002553 ret = dpcm_run_update_shutdown(fe, stream);
2554 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002555 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002556 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002557
2558 return ret;
2559}
2560
2561/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2562 * any DAI links.
2563 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002564int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002565{
Mengdong Lin1a497982015-11-18 02:34:11 -05002566 struct snd_soc_pcm_runtime *fe;
2567 int old, new, paths;
Liam Girdwood618dae12012-04-25 12:12:51 +01002568
Liam Girdwood618dae12012-04-25 12:12:51 +01002569 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Mengdong Lin1a497982015-11-18 02:34:11 -05002570 list_for_each_entry(fe, &card->rtd_list, list) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002571 struct snd_soc_dapm_widget_list *list;
Liam Girdwood618dae12012-04-25 12:12:51 +01002572
2573 /* make sure link is FE */
2574 if (!fe->dai_link->dynamic)
2575 continue;
2576
2577 /* only check active links */
2578 if (!fe->cpu_dai->active)
2579 continue;
2580
2581 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002582 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002583 fe->dai_link->name);
2584
2585 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002586 if (!fe->cpu_dai->driver->playback.channels_min
2587 || !fe->codec_dai->driver->playback.channels_min)
2588 goto capture;
2589
2590 /* skip if FE isn't currently playing */
2591 if (!fe->cpu_dai->playback_active
2592 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002593 goto capture;
2594
2595 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2596 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002597 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002598 fe->dai_link->name, "playback");
2599 mutex_unlock(&card->mutex);
2600 return paths;
2601 }
2602
2603 /* update any new playback paths */
2604 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2605 if (new) {
2606 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2607 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2608 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2609 }
2610
2611 /* update any old playback paths */
2612 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2613 if (old) {
2614 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2615 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2616 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2617 }
2618
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002619 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002620capture:
2621 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002622 if (!fe->cpu_dai->driver->capture.channels_min
2623 || !fe->codec_dai->driver->capture.channels_min)
2624 continue;
2625
2626 /* skip if FE isn't currently capturing */
2627 if (!fe->cpu_dai->capture_active
2628 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002629 continue;
2630
2631 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2632 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002633 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002634 fe->dai_link->name, "capture");
2635 mutex_unlock(&card->mutex);
2636 return paths;
2637 }
2638
2639 /* update any new capture paths */
2640 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2641 if (new) {
2642 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2643 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2644 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2645 }
2646
2647 /* update any old capture paths */
2648 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2649 if (old) {
2650 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2651 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2652 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2653 }
2654
2655 dpcm_path_put(&list);
2656 }
2657
2658 mutex_unlock(&card->mutex);
2659 return 0;
2660}
Liam Girdwood01d75842012-04-25 12:12:49 +01002661int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2662{
2663 struct snd_soc_dpcm *dpcm;
2664 struct list_head *clients =
2665 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2666
2667 list_for_each_entry(dpcm, clients, list_be) {
2668
2669 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002670 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002671
2672 if (be->dai_link->ignore_suspend)
2673 continue;
2674
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002675 for (i = 0; i < be->num_codecs; i++) {
2676 struct snd_soc_dai *dai = be->codec_dais[i];
2677 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002678
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002679 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2680 be->dai_link->name);
2681
2682 if (drv->ops && drv->ops->digital_mute &&
2683 dai->playback_active)
2684 drv->ops->digital_mute(dai, mute);
2685 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002686 }
2687
2688 return 0;
2689}
2690
Mark Brown45c0a182012-05-09 21:46:27 +01002691static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002692{
2693 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2694 struct snd_soc_dpcm *dpcm;
2695 struct snd_soc_dapm_widget_list *list;
2696 int ret;
2697 int stream = fe_substream->stream;
2698
2699 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2700 fe->dpcm[stream].runtime = fe_substream->runtime;
2701
Qiao Zhou8f70e512014-09-10 17:54:07 +08002702 ret = dpcm_path_get(fe, stream, &list);
2703 if (ret < 0) {
2704 mutex_unlock(&fe->card->mutex);
2705 return ret;
2706 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002707 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002708 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002709 }
2710
2711 /* calculate valid and active FE <-> BE dpcms */
2712 dpcm_process_paths(fe, stream, &list, 1);
2713
2714 ret = dpcm_fe_dai_startup(fe_substream);
2715 if (ret < 0) {
2716 /* clean up all links */
2717 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2718 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2719
2720 dpcm_be_disconnect(fe, stream);
2721 fe->dpcm[stream].runtime = NULL;
2722 }
2723
2724 dpcm_clear_pending_state(fe, stream);
2725 dpcm_path_put(&list);
2726 mutex_unlock(&fe->card->mutex);
2727 return ret;
2728}
2729
Mark Brown45c0a182012-05-09 21:46:27 +01002730static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002731{
2732 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2733 struct snd_soc_dpcm *dpcm;
2734 int stream = fe_substream->stream, ret;
2735
2736 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2737 ret = dpcm_fe_dai_shutdown(fe_substream);
2738
2739 /* mark FE's links ready to prune */
2740 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2741 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2742
2743 dpcm_be_disconnect(fe, stream);
2744
2745 fe->dpcm[stream].runtime = NULL;
2746 mutex_unlock(&fe->card->mutex);
2747 return ret;
2748}
2749
Liam Girdwoodddee6272011-06-09 14:45:53 +01002750/* create a new pcm */
2751int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2752{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002753 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002754 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002755 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2756 struct snd_pcm *pcm;
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08002757 struct snd_pcm_str *stream;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002758 char new_name[64];
2759 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002760 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002761
Liam Girdwood01d75842012-04-25 12:12:49 +01002762 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002763 playback = rtd->dai_link->dpcm_playback;
2764 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002765 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002766 for (i = 0; i < rtd->num_codecs; i++) {
2767 codec_dai = rtd->codec_dais[i];
2768 if (codec_dai->driver->playback.channels_min)
2769 playback = 1;
2770 if (codec_dai->driver->capture.channels_min)
2771 capture = 1;
2772 }
2773
2774 capture = capture && cpu_dai->driver->capture.channels_min;
2775 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002776 }
Sangsu Parka5002312012-01-02 17:15:10 +09002777
Fabio Estevamd6bead02013-08-29 10:32:13 -03002778 if (rtd->dai_link->playback_only) {
2779 playback = 1;
2780 capture = 0;
2781 }
2782
2783 if (rtd->dai_link->capture_only) {
2784 playback = 0;
2785 capture = 1;
2786 }
2787
Liam Girdwood01d75842012-04-25 12:12:49 +01002788 /* create the PCM */
2789 if (rtd->dai_link->no_pcm) {
2790 snprintf(new_name, sizeof(new_name), "(%s)",
2791 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002792
Liam Girdwood01d75842012-04-25 12:12:49 +01002793 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2794 playback, capture, &pcm);
2795 } else {
2796 if (rtd->dai_link->dynamic)
2797 snprintf(new_name, sizeof(new_name), "%s (*)",
2798 rtd->dai_link->stream_name);
2799 else
2800 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002801 rtd->dai_link->stream_name,
2802 (rtd->num_codecs > 1) ?
2803 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002804
Liam Girdwood01d75842012-04-25 12:12:49 +01002805 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2806 capture, &pcm);
2807 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002808 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002809 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002810 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002811 return ret;
2812 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002813 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002814
2815 /* DAPM dai link stream work */
2816 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2817
Vinod Koul48c76992015-02-12 09:59:53 +05302818 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002819 rtd->pcm = pcm;
2820 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002821
2822 if (rtd->dai_link->no_pcm) {
2823 if (playback)
2824 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2825 if (capture)
2826 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
Banajit Goswamia19486b2016-11-21 17:54:51 -08002827 if (platform->driver->pcm_new)
2828 rtd->platform->driver->pcm_new(rtd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002829 goto out;
2830 }
2831
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08002832 /* setup any hostless PCMs - i.e. no host IO is performed */
2833 if (rtd->dai_link->no_host_mode) {
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08002834 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
2835 stream = &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
2836 stream->substream->hw_no_buffer = 1;
2837 snd_soc_set_runtime_hwparams(stream->substream,
2838 &no_host_hardware);
2839 }
2840 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
2841 stream = &pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
2842 stream->substream->hw_no_buffer = 1;
2843 snd_soc_set_runtime_hwparams(stream->substream,
2844 &no_host_hardware);
2845 }
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08002846 }
2847
Liam Girdwood01d75842012-04-25 12:12:49 +01002848 /* ASoC PCM operations */
2849 if (rtd->dai_link->dynamic) {
2850 rtd->ops.open = dpcm_fe_dai_open;
2851 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2852 rtd->ops.prepare = dpcm_fe_dai_prepare;
2853 rtd->ops.trigger = dpcm_fe_dai_trigger;
2854 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2855 rtd->ops.close = dpcm_fe_dai_close;
2856 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002857 rtd->ops.ioctl = soc_pcm_ioctl;
Gopikrishnaiah Anandbdc375e2014-05-30 11:29:56 -07002858 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002859 } else {
2860 rtd->ops.open = soc_pcm_open;
2861 rtd->ops.hw_params = soc_pcm_hw_params;
2862 rtd->ops.prepare = soc_pcm_prepare;
2863 rtd->ops.trigger = soc_pcm_trigger;
2864 rtd->ops.hw_free = soc_pcm_hw_free;
2865 rtd->ops.close = soc_pcm_close;
2866 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002867 rtd->ops.ioctl = soc_pcm_ioctl;
Gopikrishnaiah Anandbdc375e2014-05-30 11:29:56 -07002868 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002869 }
2870
Liam Girdwoodddee6272011-06-09 14:45:53 +01002871 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002872 rtd->ops.ack = platform->driver->ops->ack;
2873 rtd->ops.copy = platform->driver->ops->copy;
2874 rtd->ops.silence = platform->driver->ops->silence;
2875 rtd->ops.page = platform->driver->ops->page;
2876 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002877 }
2878
2879 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002880 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002881
2882 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002883 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002884
2885 if (platform->driver->pcm_new) {
2886 ret = platform->driver->pcm_new(rtd);
2887 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002888 dev_err(platform->dev,
2889 "ASoC: pcm constructor failed: %d\n",
2890 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002891 return ret;
2892 }
2893 }
2894
2895 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002896out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002897 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2898 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2899 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002900 return ret;
2901}
Liam Girdwood01d75842012-04-25 12:12:49 +01002902
2903/* is the current PCM operation for this FE ? */
2904int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2905{
2906 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2907 return 1;
2908 return 0;
2909}
2910EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2911
2912/* is the current PCM operation for this BE ? */
2913int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2914 struct snd_soc_pcm_runtime *be, int stream)
2915{
2916 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2917 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2918 be->dpcm[stream].runtime_update))
2919 return 1;
2920 return 0;
2921}
2922EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2923
2924/* get the substream for this BE */
2925struct snd_pcm_substream *
2926 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2927{
2928 return be->pcm->streams[stream].substream;
2929}
2930EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2931
2932/* get the BE runtime state */
2933enum snd_soc_dpcm_state
2934 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2935{
2936 return be->dpcm[stream].state;
2937}
2938EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2939
2940/* set the BE runtime state */
2941void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2942 int stream, enum snd_soc_dpcm_state state)
2943{
2944 be->dpcm[stream].state = state;
2945}
2946EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2947
2948/*
2949 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2950 * are not running, paused or suspended for the specified stream direction.
2951 */
2952int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2953 struct snd_soc_pcm_runtime *be, int stream)
2954{
2955 struct snd_soc_dpcm *dpcm;
2956 int state;
2957
2958 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2959
2960 if (dpcm->fe == fe)
2961 continue;
2962
2963 state = dpcm->fe->dpcm[stream].state;
2964 if (state == SND_SOC_DPCM_STATE_START ||
2965 state == SND_SOC_DPCM_STATE_PAUSED ||
2966 state == SND_SOC_DPCM_STATE_SUSPEND)
2967 return 0;
2968 }
2969
2970 /* it's safe to free/stop this BE DAI */
2971 return 1;
2972}
2973EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2974
2975/*
2976 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2977 * running, paused or suspended for the specified stream direction.
2978 */
2979int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2980 struct snd_soc_pcm_runtime *be, int stream)
2981{
2982 struct snd_soc_dpcm *dpcm;
2983 int state;
2984
2985 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2986
2987 if (dpcm->fe == fe)
2988 continue;
2989
2990 state = dpcm->fe->dpcm[stream].state;
2991 if (state == SND_SOC_DPCM_STATE_START ||
2992 state == SND_SOC_DPCM_STATE_PAUSED ||
2993 state == SND_SOC_DPCM_STATE_SUSPEND ||
2994 state == SND_SOC_DPCM_STATE_PREPARE)
2995 return 0;
2996 }
2997
2998 /* it's safe to change hw_params */
2999 return 1;
3000}
3001EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003002
Liam Girdwood07bf84a2012-04-25 12:12:52 +01003003int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
3004 int cmd, struct snd_soc_platform *platform)
3005{
Mark Brownc5914b02013-10-30 17:47:39 -07003006 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01003007 return platform->driver->ops->trigger(substream, cmd);
3008 return 0;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
3011
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003012#ifdef CONFIG_DEBUG_FS
3013static char *dpcm_state_string(enum snd_soc_dpcm_state state)
3014{
3015 switch (state) {
3016 case SND_SOC_DPCM_STATE_NEW:
3017 return "new";
3018 case SND_SOC_DPCM_STATE_OPEN:
3019 return "open";
3020 case SND_SOC_DPCM_STATE_HW_PARAMS:
3021 return "hw_params";
3022 case SND_SOC_DPCM_STATE_PREPARE:
3023 return "prepare";
3024 case SND_SOC_DPCM_STATE_START:
3025 return "start";
3026 case SND_SOC_DPCM_STATE_STOP:
3027 return "stop";
3028 case SND_SOC_DPCM_STATE_SUSPEND:
3029 return "suspend";
3030 case SND_SOC_DPCM_STATE_PAUSED:
3031 return "paused";
3032 case SND_SOC_DPCM_STATE_HW_FREE:
3033 return "hw_free";
3034 case SND_SOC_DPCM_STATE_CLOSE:
3035 return "close";
3036 }
3037
3038 return "unknown";
3039}
3040
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003041static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3042 int stream, char *buf, size_t size)
3043{
3044 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3045 struct snd_soc_dpcm *dpcm;
3046 ssize_t offset = 0;
3047
3048 /* FE state */
3049 offset += snprintf(buf + offset, size - offset,
3050 "[%s - %s]\n", fe->dai_link->name,
3051 stream ? "Capture" : "Playback");
3052
3053 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3054 dpcm_state_string(fe->dpcm[stream].state));
3055
3056 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3057 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3058 offset += snprintf(buf + offset, size - offset,
3059 "Hardware Params: "
3060 "Format = %s, Channels = %d, Rate = %d\n",
3061 snd_pcm_format_name(params_format(params)),
3062 params_channels(params),
3063 params_rate(params));
3064
3065 /* BEs state */
3066 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3067
3068 if (list_empty(&fe->dpcm[stream].be_clients)) {
3069 offset += snprintf(buf + offset, size - offset,
3070 " No active DSP links\n");
3071 goto out;
3072 }
3073
3074 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3075 struct snd_soc_pcm_runtime *be = dpcm->be;
3076 params = &dpcm->hw_params;
3077
3078 offset += snprintf(buf + offset, size - offset,
3079 "- %s\n", be->dai_link->name);
3080
3081 offset += snprintf(buf + offset, size - offset,
3082 " State: %s\n",
3083 dpcm_state_string(be->dpcm[stream].state));
3084
3085 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3086 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3087 offset += snprintf(buf + offset, size - offset,
3088 " Hardware Params: "
3089 "Format = %s, Channels = %d, Rate = %d\n",
3090 snd_pcm_format_name(params_format(params)),
3091 params_channels(params),
3092 params_rate(params));
3093 }
3094
3095out:
3096 return offset;
3097}
3098
3099static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3100 size_t count, loff_t *ppos)
3101{
3102 struct snd_soc_pcm_runtime *fe = file->private_data;
3103 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3104 char *buf;
3105
3106 buf = kmalloc(out_count, GFP_KERNEL);
3107 if (!buf)
3108 return -ENOMEM;
3109
3110 if (fe->cpu_dai->driver->playback.channels_min)
3111 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3112 buf + offset, out_count - offset);
3113
3114 if (fe->cpu_dai->driver->capture.channels_min)
3115 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3116 buf + offset, out_count - offset);
3117
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003118 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003119
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003120 kfree(buf);
3121 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003122}
3123
3124static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003125 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003126 .read = dpcm_state_read_file,
3127 .llseek = default_llseek,
3128};
3129
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003130void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003131{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003132 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003133 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003134
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003135 if (!rtd->card->debugfs_card_root)
3136 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003137
3138 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3139 rtd->card->debugfs_card_root);
3140 if (!rtd->debugfs_dpcm_root) {
3141 dev_dbg(rtd->dev,
3142 "ASoC: Failed to create dpcm debugfs directory %s\n",
3143 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003144 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003145 }
3146
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003147 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003148 rtd->debugfs_dpcm_root,
3149 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003150}
3151#endif