blob: be6290dc7c6293a495265d3ba0aff0a0d3ee75d8 [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:
Viraja Kommarajuffbda0f2016-01-21 17:32:01 +0530631 if (cpu_dai->driver->ops && cpu_dai->driver->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100632 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
Phani Kumar Uppalapati151230e2015-06-09 00:41:11 -0700712 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
713 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
714 /* powered down playback stream now */
715 snd_soc_dapm_stream_event(rtd,
716 SNDRV_PCM_STREAM_PLAYBACK,
717 SND_SOC_DAPM_STREAM_STOP);
718 } else {
719 /* start delayed pop wq here for playback streams */
720 rtd->pop_wait = 1;
721 queue_delayed_work(system_power_efficient_wq,
722 &rtd->delayed_work,
723 msecs_to_jiffies(rtd->pmdown_time));
724 }
725 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100726 if (cpu_dai->driver->ops->shutdown)
727 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
728
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200729 for (i = 0; i < rtd->num_codecs; i++) {
730 codec_dai = rtd->codec_dais[i];
731 if (codec_dai->driver->ops->shutdown)
732 codec_dai->driver->ops->shutdown(substream, codec_dai);
733 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100734
735 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
736 rtd->dai_link->ops->shutdown(substream);
737
738 if (platform->driver->ops && platform->driver->ops->close)
739 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100740
Phani Kumar Uppalapati151230e2015-06-09 00:41:11 -0700741 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100742 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800743 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000744 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100745 }
746
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100747 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000748
Sanyog Kale3f809782016-01-05 17:14:49 +0530749 pm_runtime_mark_last_busy(platform->dev);
750 pm_runtime_put_autosuspend(platform->dev);
751
752 for (i = 0; i < rtd->num_codecs; i++) {
753 pm_runtime_mark_last_busy(rtd->codec_dais[i]->dev);
754 pm_runtime_put_autosuspend(rtd->codec_dais[i]->dev);
755 }
756
757 pm_runtime_mark_last_busy(cpu_dai->dev);
758 pm_runtime_put_autosuspend(cpu_dai->dev);
759
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200760 for (i = 0; i < rtd->num_codecs; i++) {
761 if (!rtd->codec_dais[i]->active)
762 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
763 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800764 if (!cpu_dai->active)
765 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000766
Liam Girdwoodddee6272011-06-09 14:45:53 +0100767 return 0;
768}
769
770/*
771 * Called by ALSA when the PCM substream is prepared, can set format, sample
772 * rate, etc. This function is non atomic and can be called multiple times,
773 * it can refer to the runtime info.
774 */
775static int soc_pcm_prepare(struct snd_pcm_substream *substream)
776{
777 struct snd_soc_pcm_runtime *rtd = substream->private_data;
778 struct snd_soc_platform *platform = rtd->platform;
779 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200780 struct snd_soc_dai *codec_dai;
781 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100782
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100783 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100784
Sudheer Papothi70ef6c22016-01-29 06:21:36 +0530785 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
786 snd_soc_dapm_stream_event(rtd,
787 SNDRV_PCM_STREAM_PLAYBACK,
788 SND_SOC_DAPM_STREAM_START);
789
Liam Girdwoodddee6272011-06-09 14:45:53 +0100790 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
791 ret = rtd->dai_link->ops->prepare(substream);
792 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000793 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
794 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100795 goto out;
796 }
797 }
798
799 if (platform->driver->ops && platform->driver->ops->prepare) {
800 ret = platform->driver->ops->prepare(substream);
801 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000802 dev_err(platform->dev, "ASoC: platform prepare error:"
803 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100804 goto out;
805 }
806 }
807
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200808 for (i = 0; i < rtd->num_codecs; i++) {
809 codec_dai = rtd->codec_dais[i];
810 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
811 ret = codec_dai->driver->ops->prepare(substream,
812 codec_dai);
813 if (ret < 0) {
814 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200815 "ASoC: codec DAI prepare error: %d\n",
816 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200817 goto out;
818 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100819 }
820 }
821
Mark Brownc5914b02013-10-30 17:47:39 -0700822 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100823 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
824 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200825 dev_err(cpu_dai->dev,
826 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100827 goto out;
828 }
829 }
830
831 /* cancel any delayed stream shutdown that is pending */
832 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600833 rtd->pop_wait) {
834 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100835 cancel_delayed_work(&rtd->delayed_work);
836 }
837
Sudheer Papothi70ef6c22016-01-29 06:21:36 +0530838 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
839 for (i = 0; i < rtd->num_codecs; i++) {
840 codec_dai = rtd->codec_dais[i];
841 if (codec_dai->capture_active == 1)
842 snd_soc_dapm_stream_event(rtd,
843 SNDRV_PCM_STREAM_CAPTURE,
844 SND_SOC_DAPM_STREAM_START);
845 }
846 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100847
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200848 for (i = 0; i < rtd->num_codecs; i++)
849 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
850 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530851 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100852
853out:
Sudheer Papothi70ef6c22016-01-29 06:21:36 +0530854 if (ret < 0 && substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
855 pr_err("%s: Issue stop stream for codec_dai due to op failure %d = ret\n",
856 __func__, ret);
857 snd_soc_dapm_stream_event(rtd,
858 SNDRV_PCM_STREAM_PLAYBACK,
859 SND_SOC_DAPM_STREAM_STOP);
860 }
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100861 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100862 return ret;
863}
864
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200865static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
866 unsigned int mask)
867{
868 struct snd_interval *interval;
869 int channels = hweight_long(mask);
870
871 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
872 interval->min = channels;
873 interval->max = channels;
874}
875
Benoit Cousson93e69582014-07-08 23:19:38 +0200876int soc_dai_hw_params(struct snd_pcm_substream *substream,
877 struct snd_pcm_hw_params *params,
878 struct snd_soc_dai *dai)
879{
880 int ret;
881
882 if (dai->driver->ops && dai->driver->ops->hw_params) {
883 ret = dai->driver->ops->hw_params(substream, params, dai);
884 if (ret < 0) {
885 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
886 dai->name, ret);
887 return ret;
888 }
889 }
890
891 return 0;
892}
893
Liam Girdwoodddee6272011-06-09 14:45:53 +0100894/*
895 * Called by ALSA when the hardware params are set by application. This
896 * function can also be called multiple times and can allocate buffers
897 * (using snd_pcm_lib_* ). It's non-atomic.
898 */
899static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
900 struct snd_pcm_hw_params *params)
901{
902 struct snd_soc_pcm_runtime *rtd = substream->private_data;
903 struct snd_soc_platform *platform = rtd->platform;
904 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200905 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100906
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100907 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100908
Banajit Goswami7ffe84e2017-01-10 17:28:14 -0800909 /* perform any hw_params fixups */
910 if ((rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) &&
911 rtd->dai_link->be_hw_params_fixup) {
912 ret = rtd->dai_link->be_hw_params_fixup(rtd,
913 params);
914 if (ret < 0)
915 dev_err(rtd->card->dev, "ASoC: fixup failed for %s\n",
916 rtd->dai_link->name);
917 }
918
Nicolin Chen3635bf02013-11-13 18:56:24 +0800919 ret = soc_pcm_params_symmetry(substream, params);
920 if (ret)
921 goto out;
922
Banajit Goswami7ffe84e2017-01-10 17:28:14 -0800923 /* perform any hw_params fixups */
924 if ((rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) &&
925 rtd->dai_link->be_hw_params_fixup) {
926 ret = rtd->dai_link->be_hw_params_fixup(rtd,
927 params);
928 if (ret < 0) {
929 dev_err(rtd->card->dev, "ASoC: fixup failed for %s\n",
930 rtd->dai_link->name);
931 }
932 }
933
Liam Girdwoodddee6272011-06-09 14:45:53 +0100934 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
935 ret = rtd->dai_link->ops->hw_params(substream, params);
936 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000937 dev_err(rtd->card->dev, "ASoC: machine hw_params"
938 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100939 goto out;
940 }
941 }
942
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200943 for (i = 0; i < rtd->num_codecs; i++) {
944 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
945 struct snd_pcm_hw_params codec_params;
946
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200947 /*
948 * Skip CODECs which don't support the current stream type,
949 * the idea being that if a CODEC is not used for the currently
950 * set up transfer direction, it should not need to be
951 * configured, especially since the configuration used might
952 * not even be supported by that CODEC. There may be cases
953 * however where a CODEC needs to be set up although it is
954 * actually not being used for the transfer, e.g. if a
955 * capture-only CODEC is acting as an LRCLK and/or BCLK master
956 * for the DAI link including a playback-only CODEC.
957 * If this becomes necessary, we will have to augment the
958 * machine driver setup with information on how to act, so
959 * we can do the right thing here.
960 */
961 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
962 continue;
963
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200964 /* copy params for each codec */
965 codec_params = *params;
966
967 /* fixup params based on TDM slot masks */
968 if (codec_dai->tx_mask)
969 soc_pcm_codec_params_fixup(&codec_params,
970 codec_dai->tx_mask);
971 if (codec_dai->rx_mask)
972 soc_pcm_codec_params_fixup(&codec_params,
973 codec_dai->rx_mask);
974
Benoit Cousson93e69582014-07-08 23:19:38 +0200975 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
976 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100977 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200978
979 codec_dai->rate = params_rate(&codec_params);
980 codec_dai->channels = params_channels(&codec_params);
981 codec_dai->sample_bits = snd_pcm_format_physical_width(
982 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100983 }
984
Benoit Cousson93e69582014-07-08 23:19:38 +0200985 ret = soc_dai_hw_params(substream, params, cpu_dai);
986 if (ret < 0)
987 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100988
989 if (platform->driver->ops && platform->driver->ops->hw_params) {
990 ret = platform->driver->ops->hw_params(substream, params);
991 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000992 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200993 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100994 goto platform_err;
995 }
996 }
997
Nicolin Chen3635bf02013-11-13 18:56:24 +0800998 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800999 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +08001000 cpu_dai->channels = params_channels(params);
1001 cpu_dai->sample_bits =
1002 snd_pcm_format_physical_width(params_format(params));
1003
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001004 /* malloc a page for hostless IO.
1005 * FIXME: rework with alsa-lib changes so that this malloc is
1006 * not required.
1007 */
1008 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST) {
1009 substream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV;
1010 substream->dma_buffer.dev.dev = rtd->dev;
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08001011 substream->dma_buffer.dev.dev->coherent_dma_mask =
1012 DMA_BIT_MASK(sizeof(dma_addr_t) * 8);
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001013 substream->dma_buffer.private_data = NULL;
1014
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08001015 arch_setup_dma_ops(substream->dma_buffer.dev.dev,
1016 0, 0, NULL, 0);
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001017 ret = snd_pcm_lib_malloc_pages(substream, PAGE_SIZE);
1018 if (ret < 0)
1019 goto platform_err;
1020 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001021out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001022 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001023 return ret;
1024
1025platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -07001026 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001027 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1028
1029interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001030 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001031
1032codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001033 while (--i >= 0) {
1034 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1035 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1036 codec_dai->driver->ops->hw_free(substream, codec_dai);
1037 codec_dai->rate = 0;
1038 }
1039
Liam Girdwoodddee6272011-06-09 14:45:53 +01001040 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1041 rtd->dai_link->ops->hw_free(substream);
1042
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001043 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001044 return ret;
1045}
1046
1047/*
1048 * Frees resources allocated by hw_params, can be called multiple times
1049 */
1050static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1051{
1052 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1053 struct snd_soc_platform *platform = rtd->platform;
1054 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001055 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001056 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001057 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001058
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001059 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001060
Nicolin Chend3383422013-11-20 18:37:09 +08001061 /* clear the corresponding DAIs parameters when going to be inactive */
1062 if (cpu_dai->active == 1) {
1063 cpu_dai->rate = 0;
1064 cpu_dai->channels = 0;
1065 cpu_dai->sample_bits = 0;
1066 }
1067
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001068 for (i = 0; i < rtd->num_codecs; i++) {
1069 codec_dai = rtd->codec_dais[i];
1070 if (codec_dai->active == 1) {
1071 codec_dai->rate = 0;
1072 codec_dai->channels = 0;
1073 codec_dai->sample_bits = 0;
1074 }
Nicolin Chend3383422013-11-20 18:37:09 +08001075 }
1076
Liam Girdwoodddee6272011-06-09 14:45:53 +01001077 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001078 for (i = 0; i < rtd->num_codecs; i++) {
1079 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1080 (!playback && rtd->codec_dais[i]->capture_active == 1))
1081 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1082 substream->stream);
1083 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001084
1085 /* free any machine hw params */
1086 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
1087 rtd->dai_link->ops->hw_free(substream);
1088
1089 /* free any DMA resources */
1090 if (platform->driver->ops && platform->driver->ops->hw_free)
1091 platform->driver->ops->hw_free(substream);
1092
1093 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001094 for (i = 0; i < rtd->num_codecs; i++) {
1095 codec_dai = rtd->codec_dais[i];
1096 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
1097 codec_dai->driver->ops->hw_free(substream, codec_dai);
1098 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001099
Mark Brownc5914b02013-10-30 17:47:39 -07001100 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001101 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1102
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08001103 if (rtd->dai_link->no_host_mode == SND_SOC_DAI_LINK_NO_HOST)
1104 snd_pcm_lib_free_pages(substream);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001105 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001106 return 0;
1107}
1108
1109static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1110{
1111 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1112 struct snd_soc_platform *platform = rtd->platform;
1113 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001114 struct snd_soc_dai *codec_dai;
1115 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001116
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001117 for (i = 0; i < rtd->num_codecs; i++) {
1118 codec_dai = rtd->codec_dais[i];
1119 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
1120 ret = codec_dai->driver->ops->trigger(substream,
1121 cmd, codec_dai);
1122 if (ret < 0)
1123 return ret;
1124 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001125 }
1126
1127 if (platform->driver->ops && platform->driver->ops->trigger) {
1128 ret = platform->driver->ops->trigger(substream, cmd);
1129 if (ret < 0)
1130 return ret;
1131 }
1132
Mark Brownc5914b02013-10-30 17:47:39 -07001133 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001134 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1135 if (ret < 0)
1136 return ret;
1137 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001138
1139 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
1140 ret = rtd->dai_link->ops->trigger(substream, cmd);
1141 if (ret < 0)
1142 return ret;
1143 }
1144
Liam Girdwoodddee6272011-06-09 14:45:53 +01001145 return 0;
1146}
1147
Mark Brown45c0a182012-05-09 21:46:27 +01001148static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1149 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001150{
1151 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1152 struct snd_soc_platform *platform = rtd->platform;
1153 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001154 struct snd_soc_dai *codec_dai;
1155 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001156
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001157 for (i = 0; i < rtd->num_codecs; i++) {
1158 codec_dai = rtd->codec_dais[i];
1159 if (codec_dai->driver->ops &&
1160 codec_dai->driver->ops->bespoke_trigger) {
1161 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1162 cmd, codec_dai);
1163 if (ret < 0)
1164 return ret;
1165 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001166 }
1167
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +01001168 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001169 ret = platform->driver->bespoke_trigger(substream, cmd);
1170 if (ret < 0)
1171 return ret;
1172 }
1173
Mark Brownc5914b02013-10-30 17:47:39 -07001174 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001175 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1176 if (ret < 0)
1177 return ret;
1178 }
1179 return 0;
1180}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001181/*
1182 * soc level wrapper for pointer callback
1183 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1184 * the runtime->delay will be updated accordingly.
1185 */
1186static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1187{
1188 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1189 struct snd_soc_platform *platform = rtd->platform;
1190 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001191 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001192 struct snd_pcm_runtime *runtime = substream->runtime;
1193 snd_pcm_uframes_t offset = 0;
1194 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001195 snd_pcm_sframes_t codec_delay = 0;
1196 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001197
1198 if (platform->driver->ops && platform->driver->ops->pointer)
1199 offset = platform->driver->ops->pointer(substream);
1200
Kenneth Westfieldd6f99fa2015-05-19 12:03:55 -07001201 if (platform->driver->delay_blk)
1202 return offset;
1203
Mark Brownc5914b02013-10-30 17:47:39 -07001204 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001205 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1206
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001207 for (i = 0; i < rtd->num_codecs; i++) {
1208 codec_dai = rtd->codec_dais[i];
1209 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1210 codec_delay = max(codec_delay,
1211 codec_dai->driver->ops->delay(substream,
1212 codec_dai));
1213 }
1214 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001215
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001216 /*
1217 * None of the existing platform drivers implement delay(), so
1218 * for now the codec_dai of first multicodec entry is used
1219 */
Liam Girdwoodddee6272011-06-09 14:45:53 +01001220 if (platform->driver->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001221 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001222
1223 runtime->delay = delay;
1224
1225 return offset;
1226}
1227
Kenneth Westfieldd6f99fa2015-05-19 12:03:55 -07001228static int soc_pcm_delay_blk(struct snd_pcm_substream *substream)
1229{
1230 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1231 struct snd_soc_platform *platform = rtd->platform;
1232 struct snd_pcm_runtime *runtime = substream->runtime;
1233 snd_pcm_sframes_t delay = 0;
1234
1235 if (platform->driver->delay_blk)
1236 delay = platform->driver->delay_blk(substream,
1237 rtd->codec_dais[0]);
1238
1239 runtime->delay = delay;
1240
1241 return 0;
1242}
1243
Liam Girdwood01d75842012-04-25 12:12:49 +01001244/* connect a FE and BE */
1245static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1246 struct snd_soc_pcm_runtime *be, int stream)
1247{
1248 struct snd_soc_dpcm *dpcm;
1249
1250 /* only add new dpcms */
1251 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1252 if (dpcm->be == be && dpcm->fe == fe)
1253 return 0;
1254 }
1255
1256 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1257 if (!dpcm)
1258 return -ENOMEM;
1259
1260 dpcm->be = be;
1261 dpcm->fe = fe;
1262 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1263 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1264 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1265 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1266
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001267 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001268 stream ? "capture" : "playback", fe->dai_link->name,
1269 stream ? "<-" : "->", be->dai_link->name);
1270
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001271#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001272 if (fe->debugfs_dpcm_root)
1273 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1274 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001275#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001276 return 1;
1277}
1278
1279/* reparent a BE onto another FE */
1280static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1281 struct snd_soc_pcm_runtime *be, int stream)
1282{
1283 struct snd_soc_dpcm *dpcm;
1284 struct snd_pcm_substream *fe_substream, *be_substream;
1285
1286 /* reparent if BE is connected to other FEs */
1287 if (!be->dpcm[stream].users)
1288 return;
1289
1290 be_substream = snd_soc_dpcm_get_substream(be, stream);
1291
1292 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1293 if (dpcm->fe == fe)
1294 continue;
1295
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001296 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001297 stream ? "capture" : "playback",
1298 dpcm->fe->dai_link->name,
1299 stream ? "<-" : "->", dpcm->be->dai_link->name);
1300
1301 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1302 be_substream->runtime = fe_substream->runtime;
1303 break;
1304 }
1305}
1306
1307/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001308void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001309{
1310 struct snd_soc_dpcm *dpcm, *d;
1311
1312 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001313 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001314 stream ? "capture" : "playback",
1315 dpcm->be->dai_link->name);
1316
1317 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1318 continue;
1319
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001320 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001321 stream ? "capture" : "playback", fe->dai_link->name,
1322 stream ? "<-" : "->", dpcm->be->dai_link->name);
1323
1324 /* BEs still alive need new FE */
1325 dpcm_be_reparent(fe, dpcm->be, stream);
1326
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001327#ifdef CONFIG_DEBUG_FS
1328 debugfs_remove(dpcm->debugfs_state);
1329#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001330 list_del(&dpcm->list_be);
1331 list_del(&dpcm->list_fe);
1332 kfree(dpcm);
1333 }
1334}
1335
1336/* get BE for DAI widget and stream */
1337static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1338 struct snd_soc_dapm_widget *widget, int stream)
1339{
1340 struct snd_soc_pcm_runtime *be;
Mengdong Lin1a497982015-11-18 02:34:11 -05001341 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001342
1343 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001344 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001345
Liam Girdwood35ea0652012-06-05 19:26:59 +01001346 if (!be->dai_link->no_pcm)
1347 continue;
1348
Banajit Goswami4eff0792016-12-08 22:20:41 -08001349 if ((be->cpu_dai->playback_widget == widget &&
1350 (be->dai_link->stream_name &&
1351 !strcmp(be->dai_link->stream_name,
1352 be->cpu_dai->playback_widget->sname))) ||
1353 be->codec_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001354 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001355
Mengdong Lin1a497982015-11-18 02:34:11 -05001356 for (i = 0; i < be->num_codecs; i++) {
1357 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001358 if (dai->playback_widget == widget)
1359 return be;
1360 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001361 }
1362 } else {
1363
Mengdong Lin1a497982015-11-18 02:34:11 -05001364 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001365
Liam Girdwood35ea0652012-06-05 19:26:59 +01001366 if (!be->dai_link->no_pcm)
1367 continue;
1368
Banajit Goswami4eff0792016-12-08 22:20:41 -08001369 if ((be->cpu_dai->capture_widget == widget &&
1370 (be->dai_link->stream_name &&
1371 !strcmp(be->dai_link->stream_name,
1372 be->cpu_dai->capture_widget->sname))) ||
1373 be->codec_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001374 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001375
Mengdong Lin1a497982015-11-18 02:34:11 -05001376 for (i = 0; i < be->num_codecs; i++) {
1377 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001378 if (dai->capture_widget == widget)
1379 return be;
1380 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001381 }
1382 }
1383
Liam Girdwood103d84a2012-11-19 14:39:15 +00001384 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001385 stream ? "capture" : "playback", widget->name);
1386 return NULL;
1387}
1388
1389static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001390 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001391{
1392 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001393 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001394 else
Benoit Cousson37018612014-04-24 14:01:45 +02001395 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001396}
1397
1398static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1399 struct snd_soc_dapm_widget *widget)
1400{
1401 int i;
1402
1403 for (i = 0; i < list->num_widgets; i++) {
1404 if (widget == list->widgets[i])
1405 return 1;
1406 }
1407
1408 return 0;
1409}
1410
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001411static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1412 enum snd_soc_dapm_direction dir)
1413{
1414 struct snd_soc_card *card = widget->dapm->card;
1415 struct snd_soc_pcm_runtime *rtd;
1416 int i;
1417
1418 if (dir == SND_SOC_DAPM_DIR_OUT) {
1419 list_for_each_entry(rtd, &card->rtd_list, list) {
1420 if (!rtd->dai_link->no_pcm)
1421 continue;
1422
1423 if (rtd->cpu_dai->playback_widget == widget)
1424 return true;
1425
1426 for (i = 0; i < rtd->num_codecs; ++i) {
1427 struct snd_soc_dai *dai = rtd->codec_dais[i];
1428 if (dai->playback_widget == widget)
1429 return true;
1430 }
1431 }
1432 } else { /* SND_SOC_DAPM_DIR_IN */
1433 list_for_each_entry(rtd, &card->rtd_list, list) {
1434 if (!rtd->dai_link->no_pcm)
1435 continue;
1436
1437 if (rtd->cpu_dai->capture_widget == widget)
1438 return true;
1439
1440 for (i = 0; i < rtd->num_codecs; ++i) {
1441 struct snd_soc_dai *dai = rtd->codec_dais[i];
1442 if (dai->capture_widget == widget)
1443 return true;
1444 }
1445 }
1446 }
1447
1448 return false;
1449}
1450
Liam Girdwood23607022014-01-17 17:03:55 +00001451int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001452 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001453{
1454 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001455 int paths;
1456
Liam Girdwood01d75842012-04-25 12:12:49 +01001457 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001458 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001459 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001460
Liam Girdwood103d84a2012-11-19 14:39:15 +00001461 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001462 stream ? "capture" : "playback");
1463
Liam Girdwood01d75842012-04-25 12:12:49 +01001464 return paths;
1465}
1466
Liam Girdwood01d75842012-04-25 12:12:49 +01001467static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1468 struct snd_soc_dapm_widget_list **list_)
1469{
1470 struct snd_soc_dpcm *dpcm;
1471 struct snd_soc_dapm_widget_list *list = *list_;
1472 struct snd_soc_dapm_widget *widget;
1473 int prune = 0;
1474
1475 /* Destroy any old FE <--> BE connections */
1476 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001477 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001478
1479 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001480 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001481
1482 /* prune the BE if it's no longer in our active list */
1483 if (widget && widget_in_list(list, widget))
1484 continue;
1485
1486 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001487 for (i = 0; i < dpcm->be->num_codecs; i++) {
1488 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1489 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001490
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001491 /* prune the BE if it's no longer in our active list */
1492 if (widget && widget_in_list(list, widget))
1493 continue;
1494 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001495
Liam Girdwood103d84a2012-11-19 14:39:15 +00001496 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001497 stream ? "capture" : "playback",
1498 dpcm->be->dai_link->name, fe->dai_link->name);
1499 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1500 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1501 prune++;
1502 }
1503
Liam Girdwood103d84a2012-11-19 14:39:15 +00001504 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001505 return prune;
1506}
1507
1508static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1509 struct snd_soc_dapm_widget_list **list_)
1510{
1511 struct snd_soc_card *card = fe->card;
1512 struct snd_soc_dapm_widget_list *list = *list_;
1513 struct snd_soc_pcm_runtime *be;
1514 int i, new = 0, err;
1515
1516 /* Create any new FE <--> BE connections */
1517 for (i = 0; i < list->num_widgets; i++) {
1518
Mark Brown46162742013-06-05 19:36:11 +01001519 switch (list->widgets[i]->id) {
1520 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001521 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1522 continue;
1523 break;
Mark Brown46162742013-06-05 19:36:11 +01001524 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001525 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1526 continue;
Mark Brown46162742013-06-05 19:36:11 +01001527 break;
1528 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001529 continue;
Mark Brown46162742013-06-05 19:36:11 +01001530 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001531
1532 /* is there a valid BE rtd for this widget */
1533 be = dpcm_get_be(card, list->widgets[i], stream);
1534 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001535 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001536 list->widgets[i]->name);
1537 continue;
1538 }
1539
1540 /* make sure BE is a real BE */
1541 if (!be->dai_link->no_pcm)
1542 continue;
1543
1544 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001545 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001546 continue;
1547
1548 /* newly connected FE and BE */
1549 err = dpcm_be_connect(fe, be, stream);
1550 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001551 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001552 list->widgets[i]->name);
1553 break;
1554 } else if (err == 0) /* already connected */
1555 continue;
1556
1557 /* new */
1558 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1559 new++;
1560 }
1561
Liam Girdwood103d84a2012-11-19 14:39:15 +00001562 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001563 return new;
1564}
1565
1566/*
1567 * Find the corresponding BE DAIs that source or sink audio to this
1568 * FE substream.
1569 */
Liam Girdwood23607022014-01-17 17:03:55 +00001570int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001571 int stream, struct snd_soc_dapm_widget_list **list, int new)
1572{
1573 if (new)
1574 return dpcm_add_paths(fe, stream, list);
1575 else
1576 return dpcm_prune_paths(fe, stream, list);
1577}
1578
Liam Girdwood23607022014-01-17 17:03:55 +00001579void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001580{
1581 struct snd_soc_dpcm *dpcm;
1582
1583 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1584 dpcm->be->dpcm[stream].runtime_update =
1585 SND_SOC_DPCM_UPDATE_NO;
1586}
1587
1588static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1589 int stream)
1590{
1591 struct snd_soc_dpcm *dpcm;
1592
1593 /* disable any enabled and non active backends */
1594 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1595
1596 struct snd_soc_pcm_runtime *be = dpcm->be;
1597 struct snd_pcm_substream *be_substream =
1598 snd_soc_dpcm_get_substream(be, stream);
1599
1600 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001601 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001602 stream ? "capture" : "playback",
1603 be->dpcm[stream].state);
1604
1605 if (--be->dpcm[stream].users != 0)
1606 continue;
1607
1608 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1609 continue;
1610
1611 soc_pcm_close(be_substream);
1612 be_substream->runtime = NULL;
1613 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1614 }
1615}
1616
Liam Girdwood23607022014-01-17 17:03:55 +00001617int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001618{
1619 struct snd_soc_dpcm *dpcm;
1620 int err, count = 0;
1621
1622 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1623 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1624
1625 struct snd_soc_pcm_runtime *be = dpcm->be;
1626 struct snd_pcm_substream *be_substream =
1627 snd_soc_dpcm_get_substream(be, stream);
1628
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001629 if (!be_substream) {
1630 dev_err(be->dev, "ASoC: no backend %s stream\n",
1631 stream ? "capture" : "playback");
1632 continue;
1633 }
1634
Liam Girdwood01d75842012-04-25 12:12:49 +01001635 /* is this op for this BE ? */
1636 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1637 continue;
1638
1639 /* first time the dpcm is open ? */
1640 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001641 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001642 stream ? "capture" : "playback",
1643 be->dpcm[stream].state);
1644
1645 if (be->dpcm[stream].users++ != 0)
1646 continue;
1647
1648 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1649 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1650 continue;
1651
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001652 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1653 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001654
1655 be_substream->runtime = be->dpcm[stream].runtime;
1656 err = soc_pcm_open(be_substream);
1657 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001658 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001659 be->dpcm[stream].users--;
1660 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001661 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001662 stream ? "capture" : "playback",
1663 be->dpcm[stream].state);
1664
1665 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1666 goto unwind;
1667 }
1668
1669 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1670 count++;
1671 }
1672
1673 return count;
1674
1675unwind:
1676 /* disable any enabled and non active backends */
1677 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1678 struct snd_soc_pcm_runtime *be = dpcm->be;
1679 struct snd_pcm_substream *be_substream =
1680 snd_soc_dpcm_get_substream(be, stream);
1681
1682 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1683 continue;
1684
1685 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001686 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001687 stream ? "capture" : "playback",
1688 be->dpcm[stream].state);
1689
1690 if (--be->dpcm[stream].users != 0)
1691 continue;
1692
1693 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1694 continue;
1695
1696 soc_pcm_close(be_substream);
1697 be_substream->runtime = NULL;
1698 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1699 }
1700
1701 return err;
1702}
1703
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001704static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001705 struct snd_soc_pcm_stream *stream,
1706 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001707{
1708 runtime->hw.rate_min = stream->rate_min;
1709 runtime->hw.rate_max = stream->rate_max;
1710 runtime->hw.channels_min = stream->channels_min;
1711 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001712 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001713 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001714 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001715 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001716 runtime->hw.rates = stream->rates;
1717}
1718
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001719static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1720{
1721 struct snd_soc_pcm_runtime *fe = substream->private_data;
1722 struct snd_soc_dpcm *dpcm;
1723 u64 formats = ULLONG_MAX;
1724 int stream = substream->stream;
1725
1726 if (!fe->dai_link->dpcm_merged_format)
1727 return formats;
1728
1729 /*
1730 * It returns merged BE codec format
1731 * if FE want to use it (= dpcm_merged_format)
1732 */
1733
1734 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1735 struct snd_soc_pcm_runtime *be = dpcm->be;
1736 struct snd_soc_dai_driver *codec_dai_drv;
1737 struct snd_soc_pcm_stream *codec_stream;
1738 int i;
1739
1740 for (i = 0; i < be->num_codecs; i++) {
1741 codec_dai_drv = be->codec_dais[i]->driver;
1742 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1743 codec_stream = &codec_dai_drv->playback;
1744 else
1745 codec_stream = &codec_dai_drv->capture;
1746
1747 formats &= codec_stream->formats;
1748 }
1749 }
1750
1751 return formats;
1752}
1753
Mark Brown45c0a182012-05-09 21:46:27 +01001754static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001755{
1756 struct snd_pcm_runtime *runtime = substream->runtime;
1757 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1758 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1759 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001760 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001761
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001762 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001763 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001764 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001765 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001766}
1767
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001768static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1769
1770/* Set FE's runtime_update state; the state is protected via PCM stream lock
1771 * for avoiding the race with trigger callback.
1772 * If the state is unset and a trigger is pending while the previous operation,
1773 * process the pending trigger action here.
1774 */
1775static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1776 int stream, enum snd_soc_dpcm_update state)
1777{
1778 struct snd_pcm_substream *substream =
1779 snd_soc_dpcm_get_substream(fe, stream);
1780
1781 snd_pcm_stream_lock_irq(substream);
1782 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1783 dpcm_fe_dai_do_trigger(substream,
1784 fe->dpcm[stream].trigger_pending - 1);
1785 fe->dpcm[stream].trigger_pending = 0;
1786 }
1787 fe->dpcm[stream].runtime_update = state;
1788 snd_pcm_stream_unlock_irq(substream);
1789}
1790
PC Liao906c7d62015-12-11 11:33:51 +08001791static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1792 int stream)
1793{
1794 struct snd_soc_dpcm *dpcm;
1795 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1796 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1797 int err;
1798
1799 /* apply symmetry for FE */
1800 if (soc_pcm_has_symmetry(fe_substream))
1801 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1802
1803 /* Symmetry only applies if we've got an active stream. */
1804 if (fe_cpu_dai->active) {
1805 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1806 if (err < 0)
1807 return err;
1808 }
1809
1810 /* apply symmetry for BE */
1811 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1812 struct snd_soc_pcm_runtime *be = dpcm->be;
1813 struct snd_pcm_substream *be_substream =
1814 snd_soc_dpcm_get_substream(be, stream);
1815 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1816 int i;
1817
Jeeja KPf1176612016-09-06 14:17:55 +05301818 if (rtd->dai_link->be_hw_params_fixup)
1819 continue;
1820
PC Liao906c7d62015-12-11 11:33:51 +08001821 if (soc_pcm_has_symmetry(be_substream))
1822 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1823
1824 /* Symmetry only applies if we've got an active stream. */
1825 if (rtd->cpu_dai->active) {
1826 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1827 if (err < 0)
1828 return err;
1829 }
1830
1831 for (i = 0; i < rtd->num_codecs; i++) {
1832 if (rtd->codec_dais[i]->active) {
1833 err = soc_pcm_apply_symmetry(be_substream,
1834 rtd->codec_dais[i]);
1835 if (err < 0)
1836 return err;
1837 }
1838 }
1839 }
1840
1841 return 0;
1842}
1843
Liam Girdwood01d75842012-04-25 12:12:49 +01001844static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1845{
1846 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1847 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1848 int stream = fe_substream->stream, ret = 0;
1849
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001850 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001851
1852 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1853 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001854 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001855 goto be_err;
1856 }
1857
Liam Girdwood103d84a2012-11-19 14:39:15 +00001858 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001859
1860 /* start the DAI frontend */
1861 ret = soc_pcm_open(fe_substream);
1862 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001863 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001864 goto unwind;
1865 }
1866
1867 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1868
1869 dpcm_set_fe_runtime(fe_substream);
1870 snd_pcm_limit_hw_rates(runtime);
1871
PC Liao906c7d62015-12-11 11:33:51 +08001872 ret = dpcm_apply_symmetry(fe_substream, stream);
1873 if (ret < 0) {
1874 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1875 ret);
1876 goto unwind;
1877 }
1878
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001879 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001880 return 0;
1881
1882unwind:
1883 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1884be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001885 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001886 return ret;
1887}
1888
Liam Girdwood23607022014-01-17 17:03:55 +00001889int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001890{
1891 struct snd_soc_dpcm *dpcm;
1892
1893 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1894 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1895
1896 struct snd_soc_pcm_runtime *be = dpcm->be;
1897 struct snd_pcm_substream *be_substream =
1898 snd_soc_dpcm_get_substream(be, stream);
1899
1900 /* is this op for this BE ? */
1901 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1902 continue;
1903
1904 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001905 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001906 stream ? "capture" : "playback",
1907 be->dpcm[stream].state);
1908
1909 if (--be->dpcm[stream].users != 0)
1910 continue;
1911
1912 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1913 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1914 continue;
1915
Liam Girdwood103d84a2012-11-19 14:39:15 +00001916 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001917 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001918
1919 soc_pcm_close(be_substream);
1920 be_substream->runtime = NULL;
1921
1922 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1923 }
1924 return 0;
1925}
1926
1927static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1928{
1929 struct snd_soc_pcm_runtime *fe = substream->private_data;
1930 int stream = substream->stream;
1931
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001932 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001933
Liam Girdwood103d84a2012-11-19 14:39:15 +00001934 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001935
1936 /* now shutdown the frontend */
1937 soc_pcm_close(substream);
1938
Banajit Goswami1c7b7b42015-04-09 17:19:30 -07001939 /* shutdown the BEs */
1940 dpcm_be_dai_shutdown(fe, substream->stream);
1941
Liam Girdwood01d75842012-04-25 12:12:49 +01001942 /* run the stream event for each BE */
1943 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1944
1945 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001946 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001947 return 0;
1948}
1949
Liam Girdwood23607022014-01-17 17:03:55 +00001950int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001951{
1952 struct snd_soc_dpcm *dpcm;
1953
1954 /* only hw_params backends that are either sinks or sources
1955 * to this frontend DAI */
1956 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1957
1958 struct snd_soc_pcm_runtime *be = dpcm->be;
1959 struct snd_pcm_substream *be_substream =
1960 snd_soc_dpcm_get_substream(be, stream);
1961
1962 /* is this op for this BE ? */
1963 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1964 continue;
1965
1966 /* only free hw when no longer used - check all FEs */
1967 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1968 continue;
1969
Qiao Zhou36fba622014-12-03 10:13:43 +08001970 /* do not free hw if this BE is used by other FE */
1971 if (be->dpcm[stream].users > 1)
1972 continue;
1973
Liam Girdwood01d75842012-04-25 12:12:49 +01001974 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1975 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1976 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001977 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05301978 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1979 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01001980 continue;
1981
Liam Girdwood103d84a2012-11-19 14:39:15 +00001982 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001983 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001984
1985 soc_pcm_hw_free(be_substream);
1986
1987 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1988 }
1989
1990 return 0;
1991}
1992
Mark Brown45c0a182012-05-09 21:46:27 +01001993static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001994{
1995 struct snd_soc_pcm_runtime *fe = substream->private_data;
1996 int err, stream = substream->stream;
1997
1998 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001999 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002000
Liam Girdwood103d84a2012-11-19 14:39:15 +00002001 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002002
2003 /* call hw_free on the frontend */
2004 err = soc_pcm_hw_free(substream);
2005 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002006 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002007 fe->dai_link->name);
2008
2009 /* only hw_params backends that are either sinks or sources
2010 * to this frontend DAI */
2011 err = dpcm_be_dai_hw_free(fe, stream);
2012
2013 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002014 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002015
2016 mutex_unlock(&fe->card->mutex);
2017 return 0;
2018}
2019
Banajit Goswami8886fb02016-11-21 21:39:54 -08002020int dpcm_fe_dai_hw_params_be(struct snd_soc_pcm_runtime *fe,
2021 struct snd_soc_pcm_runtime *be,
2022 struct snd_pcm_hw_params *params, int stream)
2023{
2024 int ret;
2025 struct snd_soc_dpcm *dpcm;
2026 struct snd_pcm_substream *be_substream =
2027 snd_soc_dpcm_get_substream(be, stream);
2028
2029 /* is this op for this BE ? */
2030 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2031 return 0;
2032
2033 /* only allow hw_params() if no connected FEs are running */
2034 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2035 return 0;
2036
2037 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2038 (be->dpcm[stream].state !=
2039 SND_SOC_DPCM_STATE_HW_PARAMS) &&
2040 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2041 return 0;
2042
2043 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2044 fe->dai_link->name);
2045
2046 /* perform any hw_params fixups */
2047 if (be->dai_link->be_hw_params_fixup) {
2048 ret = be->dai_link->be_hw_params_fixup(be,
2049 params);
2050 if (ret < 0) {
2051 dev_err(be->dev,
2052 "ASoC: hw_params BE fixup failed %d\n",
2053 ret);
2054 goto unwind;
2055 }
2056 }
2057
2058 ret = soc_pcm_hw_params(be_substream, params);
2059 if (ret < 0) {
2060 dev_err(be->dev, "ASoC: hw_params BE failed %d\n", ret);
2061 goto unwind;
2062 }
2063
2064 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2065 return 0;
2066
2067unwind:
2068 /* disable any enabled and non active backends */
2069 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2070 struct snd_soc_pcm_runtime *be = dpcm->be;
2071 struct snd_pcm_substream *be_substream =
2072 snd_soc_dpcm_get_substream(be, stream);
2073
2074 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2075 continue;
2076
2077 /* only allow hw_free() if no connected FEs are running */
2078 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2079 continue;
2080
2081 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2082 (be->dpcm[stream].state
2083 != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2084 (be->dpcm[stream].state
2085 != SND_SOC_DPCM_STATE_HW_FREE) &&
2086 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2087 continue;
2088
2089 soc_pcm_hw_free(be_substream);
2090 }
2091
2092 return ret;
2093}
2094
Liam Girdwood23607022014-01-17 17:03:55 +00002095int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002096{
2097 struct snd_soc_dpcm *dpcm;
2098 int ret;
2099
2100 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2101
2102 struct snd_soc_pcm_runtime *be = dpcm->be;
2103 struct snd_pcm_substream *be_substream =
2104 snd_soc_dpcm_get_substream(be, stream);
2105
2106 /* is this op for this BE ? */
2107 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2108 continue;
2109
Liam Girdwood01d75842012-04-25 12:12:49 +01002110 /* copy params for each dpcm */
2111 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2112 sizeof(struct snd_pcm_hw_params));
2113
2114 /* perform any hw_params fixups */
2115 if (be->dai_link->be_hw_params_fixup) {
2116 ret = be->dai_link->be_hw_params_fixup(be,
2117 &dpcm->hw_params);
2118 if (ret < 0) {
2119 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002120 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002121 ret);
2122 goto unwind;
2123 }
2124 }
2125
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002126 /* only allow hw_params() if no connected FEs are running */
2127 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2128 continue;
2129
2130 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2131 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2132 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2133 continue;
2134
2135 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002136 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002137
Liam Girdwood01d75842012-04-25 12:12:49 +01002138 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2139 if (ret < 0) {
2140 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002141 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002142 goto unwind;
2143 }
2144
2145 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2146 }
2147 return 0;
2148
2149unwind:
2150 /* disable any enabled and non active backends */
2151 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2152 struct snd_soc_pcm_runtime *be = dpcm->be;
2153 struct snd_pcm_substream *be_substream =
2154 snd_soc_dpcm_get_substream(be, stream);
2155
2156 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2157 continue;
2158
2159 /* only allow hw_free() if no connected FEs are running */
2160 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2161 continue;
2162
2163 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2164 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2165 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2166 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2167 continue;
2168
2169 soc_pcm_hw_free(be_substream);
2170 }
2171
2172 return ret;
2173}
2174
Mark Brown45c0a182012-05-09 21:46:27 +01002175static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2176 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002177{
2178 struct snd_soc_pcm_runtime *fe = substream->private_data;
2179 int ret, stream = substream->stream;
2180
2181 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002182 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002183
2184 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2185 sizeof(struct snd_pcm_hw_params));
2186 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2187 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002188 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002189 goto out;
2190 }
2191
Liam Girdwood103d84a2012-11-19 14:39:15 +00002192 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002193 fe->dai_link->name, params_rate(params),
2194 params_channels(params), params_format(params));
2195
2196 /* call hw_params on the frontend */
2197 ret = soc_pcm_hw_params(substream, params);
2198 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002199 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002200 dpcm_be_dai_hw_free(fe, stream);
2201 } else
2202 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2203
2204out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002205 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002206 mutex_unlock(&fe->card->mutex);
2207 return ret;
2208}
2209
2210static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2211 struct snd_pcm_substream *substream, int cmd)
2212{
2213 int ret;
2214
Liam Girdwood103d84a2012-11-19 14:39:15 +00002215 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002216 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002217
2218 ret = soc_pcm_trigger(substream, cmd);
2219 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002220 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002221
2222 return ret;
2223}
2224
Liam Girdwood23607022014-01-17 17:03:55 +00002225int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002226 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002227{
2228 struct snd_soc_dpcm *dpcm;
2229 int ret = 0;
2230
2231 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2232
2233 struct snd_soc_pcm_runtime *be = dpcm->be;
2234 struct snd_pcm_substream *be_substream =
2235 snd_soc_dpcm_get_substream(be, stream);
2236
2237 /* is this op for this BE ? */
2238 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2239 continue;
2240
2241 switch (cmd) {
2242 case SNDRV_PCM_TRIGGER_START:
2243 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2244 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2245 continue;
2246
2247 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2248 if (ret)
2249 return ret;
2250
2251 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2252 break;
2253 case SNDRV_PCM_TRIGGER_RESUME:
2254 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2255 continue;
2256
2257 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2258 if (ret)
2259 return ret;
2260
2261 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2262 break;
2263 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2264 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2265 continue;
2266
2267 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2268 if (ret)
2269 return ret;
2270
2271 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2272 break;
2273 case SNDRV_PCM_TRIGGER_STOP:
2274 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2275 continue;
2276
2277 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2278 continue;
2279
2280 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2281 if (ret)
2282 return ret;
2283
2284 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2285 break;
2286 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002287 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002288 continue;
2289
2290 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2291 continue;
2292
2293 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2294 if (ret)
2295 return ret;
2296
2297 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2298 break;
2299 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2300 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2301 continue;
2302
2303 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2304 continue;
2305
2306 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2307 if (ret)
2308 return ret;
2309
2310 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2311 break;
2312 }
2313 }
2314
2315 return ret;
2316}
2317EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2318
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002319static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002320{
2321 struct snd_soc_pcm_runtime *fe = substream->private_data;
2322 int stream = substream->stream, ret;
2323 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2324
2325 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2326
2327 switch (trigger) {
2328 case SND_SOC_DPCM_TRIGGER_PRE:
2329 /* call trigger on the frontend before the backend. */
2330
Liam Girdwood103d84a2012-11-19 14:39:15 +00002331 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002332 fe->dai_link->name, cmd);
2333
2334 ret = soc_pcm_trigger(substream, cmd);
2335 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002336 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002337 goto out;
2338 }
2339
2340 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2341 break;
2342 case SND_SOC_DPCM_TRIGGER_POST:
2343 /* call trigger on the frontend after the backend. */
2344
2345 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2346 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002347 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002348 goto out;
2349 }
2350
Liam Girdwood103d84a2012-11-19 14:39:15 +00002351 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002352 fe->dai_link->name, cmd);
2353
2354 ret = soc_pcm_trigger(substream, cmd);
2355 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002356 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2357 /* bespoke trigger() - handles both FE and BEs */
2358
Liam Girdwood103d84a2012-11-19 14:39:15 +00002359 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002360 fe->dai_link->name, cmd);
2361
2362 ret = soc_pcm_bespoke_trigger(substream, cmd);
2363 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002364 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002365 goto out;
2366 }
2367 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002368 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002369 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002370 fe->dai_link->name);
2371 ret = -EINVAL;
2372 goto out;
2373 }
2374
2375 switch (cmd) {
2376 case SNDRV_PCM_TRIGGER_START:
2377 case SNDRV_PCM_TRIGGER_RESUME:
2378 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2379 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2380 break;
2381 case SNDRV_PCM_TRIGGER_STOP:
2382 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002383 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2384 break;
Banajit Goswami6175bce2013-02-14 18:24:08 -08002385 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2386 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2387 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002388 }
2389
2390out:
2391 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2392 return ret;
2393}
2394
Banajit Goswami8886fb02016-11-21 21:39:54 -08002395int dpcm_fe_dai_prepare_be(struct snd_soc_pcm_runtime *fe,
2396 struct snd_soc_pcm_runtime *be, int stream)
2397{
2398 struct snd_pcm_substream *be_substream =
2399 snd_soc_dpcm_get_substream(be, stream);
2400 int ret = 0;
2401
2402 /* is this op for this BE ? */
2403 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2404 return 0;
2405
2406 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2407 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2408 return 0;
2409
2410 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2411 fe->dai_link->name);
2412
2413 ret = soc_pcm_prepare(be_substream);
2414 if (ret < 0) {
2415 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2416 ret);
2417 return ret;
2418 }
2419
2420 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2421 return ret;
2422}
2423
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002424static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2425{
2426 struct snd_soc_pcm_runtime *fe = substream->private_data;
2427 int stream = substream->stream;
2428
2429 /* if FE's runtime_update is already set, we're in race;
2430 * process this trigger later at exit
2431 */
2432 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2433 fe->dpcm[stream].trigger_pending = cmd + 1;
2434 return 0; /* delayed, assuming it's successful */
2435 }
2436
2437 /* we're alone, let's trigger */
2438 return dpcm_fe_dai_do_trigger(substream, cmd);
2439}
2440
Liam Girdwood23607022014-01-17 17:03:55 +00002441int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002442{
2443 struct snd_soc_dpcm *dpcm;
2444 int ret = 0;
2445
2446 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2447
2448 struct snd_soc_pcm_runtime *be = dpcm->be;
2449 struct snd_pcm_substream *be_substream =
2450 snd_soc_dpcm_get_substream(be, stream);
2451
2452 /* is this op for this BE ? */
2453 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2454 continue;
2455
2456 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002457 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2458 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002459 continue;
2460
Liam Girdwood103d84a2012-11-19 14:39:15 +00002461 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002462 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002463
2464 ret = soc_pcm_prepare(be_substream);
2465 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002466 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002467 ret);
2468 break;
2469 }
2470
2471 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2472 }
2473 return ret;
2474}
2475
Banajit Goswami8886fb02016-11-21 21:39:54 -08002476static void dpcm_be_async_prepare(void *data, async_cookie_t cookie)
2477{
2478 struct snd_soc_dpcm *dpcm = data;
2479 struct snd_soc_pcm_runtime *be = dpcm->be;
2480 int stream = dpcm->stream;
2481 struct snd_pcm_substream *be_substream =
2482 snd_soc_dpcm_get_substream(be, stream);
2483 int ret;
2484
2485 dev_dbg(be->dev, "%s ASoC: prepare BE %s\n", __func__,
2486 dpcm->fe->dai_link->name);
2487 ret = soc_pcm_prepare(be_substream);
2488 if (ret < 0) {
2489 be->err_ops = ret;
2490 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2491 ret);
2492 return;
2493 }
2494 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2495}
2496
2497void dpcm_be_dai_prepare_async(struct snd_soc_pcm_runtime *fe, int stream,
2498 struct async_domain *domain)
2499{
2500 struct snd_soc_dpcm *dpcm;
2501 struct snd_soc_dpcm *dpcm_async[DPCM_MAX_BE_USERS];
2502 int i = 0, j;
2503
2504 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2505 struct snd_soc_pcm_runtime *be = dpcm->be;
2506
2507 be->err_ops = 0;
2508 /* is this op for this BE ? */
2509 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2510 continue;
2511
2512 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2513 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2514 continue;
2515
2516 /* does this BE support async op ?*/
2517 if ((fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE) &&
2518 (be->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE)) {
2519 dpcm->stream = stream;
2520 async_schedule_domain(dpcm_be_async_prepare,
2521 dpcm, domain);
2522 } else {
2523 dpcm_async[i++] = dpcm;
Walter Yange0c24ee2017-03-02 12:13:34 +08002524 if (i == DPCM_MAX_BE_USERS) {
2525 dev_dbg(fe->dev, "ASoC: MAX backend users!\n");
2526 break;
2527 }
Banajit Goswami8886fb02016-11-21 21:39:54 -08002528 }
2529 }
2530
2531 for (j = 0; j < i; j++) {
2532 struct snd_soc_dpcm *dpcm = dpcm_async[j];
2533 struct snd_soc_pcm_runtime *be = dpcm->be;
2534 struct snd_pcm_substream *be_substream =
2535 snd_soc_dpcm_get_substream(be, stream);
2536 int ret;
2537
2538 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2539 dpcm->fe->dai_link->name);
2540
2541 ret = soc_pcm_prepare(be_substream);
2542 if (ret < 0) {
2543 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2544 ret);
2545 be->err_ops = ret;
2546 return;
2547 }
2548
2549 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2550 }
2551}
2552
Mark Brown45c0a182012-05-09 21:46:27 +01002553static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002554{
2555 struct snd_soc_pcm_runtime *fe = substream->private_data;
Banajit Goswami8886fb02016-11-21 21:39:54 -08002556 struct snd_soc_dpcm *dpcm;
Liam Girdwood01d75842012-04-25 12:12:49 +01002557 int stream = substream->stream, ret = 0;
Banajit Goswami8886fb02016-11-21 21:39:54 -08002558 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood01d75842012-04-25 12:12:49 +01002559
2560 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2561
Banajit Goswami8886fb02016-11-21 21:39:54 -08002562 fe->err_ops = 0;
2563
Liam Girdwood103d84a2012-11-19 14:39:15 +00002564 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002565
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002566 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002567
2568 /* there is no point preparing this FE if there are no BEs */
2569 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002570 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002571 fe->dai_link->name);
2572 ret = -EINVAL;
2573 goto out;
2574 }
2575
Banajit Goswami8886fb02016-11-21 21:39:54 -08002576 if (!(fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_PREPARE)) {
2577 ret = dpcm_be_dai_prepare(fe, substream->stream);
2578 if (ret < 0)
2579 goto out;
2580 /* call prepare on the frontend */
2581 ret = soc_pcm_prepare(substream);
2582 if (ret < 0) {
2583 dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
2584 fe->dai_link->name);
2585 goto out;
2586 }
2587 } else {
2588 dpcm_be_dai_prepare_async(fe, substream->stream,
2589 &async_domain);
Liam Girdwood01d75842012-04-25 12:12:49 +01002590
Banajit Goswami8886fb02016-11-21 21:39:54 -08002591 /* call prepare on the frontend */
2592 ret = soc_pcm_prepare(substream);
2593 if (ret < 0) {
2594 fe->err_ops = ret;
2595 dev_err(fe->dev, "ASoC: prepare FE %s failed\n",
2596 fe->dai_link->name);
2597 }
2598
2599 async_synchronize_full_domain(&async_domain);
2600
2601 /* check if any BE failed */
2602 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients,
2603 list_be) {
2604 struct snd_soc_pcm_runtime *be = dpcm->be;
2605
2606 if (be->err_ops < 0) {
2607 ret = be->err_ops;
2608 goto out;
2609 }
2610 }
2611
2612 /* check if FE failed */
2613 if (fe->err_ops < 0) {
2614 ret = fe->err_ops;
2615 goto out;
2616 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002617 }
2618
2619 /* run the stream event for each BE */
2620 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2621 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2622
2623out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002624 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002625 mutex_unlock(&fe->card->mutex);
2626
2627 return ret;
2628}
2629
Gopikrishnaiah Anandbdc375e2014-05-30 11:29:56 -07002630static int soc_pcm_compat_ioctl(struct snd_pcm_substream *substream,
2631 unsigned int cmd, void *arg)
2632{
2633 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2634 struct snd_soc_platform *platform = rtd->platform;
2635
2636 if (platform->driver->ops->compat_ioctl)
2637 return platform->driver->ops->compat_ioctl(substream,
2638 cmd, arg);
2639 return snd_pcm_lib_ioctl(substream, cmd, arg);
2640}
2641
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002642static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2643 unsigned int cmd, void *arg)
2644{
2645 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2646 struct snd_soc_platform *platform = rtd->platform;
2647
Mark Brownc5914b02013-10-30 17:47:39 -07002648 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002649 return platform->driver->ops->ioctl(substream, cmd, arg);
2650 return snd_pcm_lib_ioctl(substream, cmd, arg);
2651}
2652
Liam Girdwood618dae12012-04-25 12:12:51 +01002653static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2654{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002655 struct snd_pcm_substream *substream =
2656 snd_soc_dpcm_get_substream(fe, stream);
2657 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002658 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002659
Liam Girdwood103d84a2012-11-19 14:39:15 +00002660 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002661 stream ? "capture" : "playback", fe->dai_link->name);
2662
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002663 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2664 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002665 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002666 fe->dai_link->name);
2667
2668 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2669 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002670 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002671 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002672 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002673 fe->dai_link->name);
2674
2675 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2676 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002677 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002678 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002679
2680 err = dpcm_be_dai_hw_free(fe, stream);
2681 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002682 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002683
2684 err = dpcm_be_dai_shutdown(fe, stream);
2685 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002686 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002687
2688 /* run the stream event for each BE */
2689 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2690
2691 return 0;
2692}
2693
2694static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2695{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002696 struct snd_pcm_substream *substream =
2697 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002698 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002699 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002700 int ret;
2701
Liam Girdwood103d84a2012-11-19 14:39:15 +00002702 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002703 stream ? "capture" : "playback", fe->dai_link->name);
2704
2705 /* Only start the BE if the FE is ready */
2706 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2707 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2708 return -EINVAL;
2709
2710 /* startup must always be called for new BEs */
2711 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002712 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002713 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002714
2715 /* keep going if FE state is > open */
2716 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2717 return 0;
2718
2719 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002720 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002721 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002722
2723 /* keep going if FE state is > hw_params */
2724 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2725 return 0;
2726
2727
2728 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002729 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002730 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002731
2732 /* run the stream event for each BE */
2733 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2734
2735 /* keep going if FE state is > prepare */
2736 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2737 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2738 return 0;
2739
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002740 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2741 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002742 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002743 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002744
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002745 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2746 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002747 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002748 goto hw_free;
2749 }
2750 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002751 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002752 fe->dai_link->name);
2753
2754 ret = dpcm_be_dai_trigger(fe, stream,
2755 SNDRV_PCM_TRIGGER_START);
2756 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002757 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002758 goto hw_free;
2759 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002760 }
2761
2762 return 0;
2763
2764hw_free:
2765 dpcm_be_dai_hw_free(fe, stream);
2766close:
2767 dpcm_be_dai_shutdown(fe, stream);
2768disconnect:
2769 /* disconnect any non started BEs */
2770 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2771 struct snd_soc_pcm_runtime *be = dpcm->be;
2772 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2773 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2774 }
2775
2776 return ret;
2777}
2778
2779static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2780{
2781 int ret;
2782
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002783 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002784 ret = dpcm_run_update_startup(fe, stream);
2785 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002786 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002787 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002788
2789 return ret;
2790}
2791
2792static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2793{
2794 int ret;
2795
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002796 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002797 ret = dpcm_run_update_shutdown(fe, stream);
2798 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002799 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002800 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002801
2802 return ret;
2803}
2804
2805/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2806 * any DAI links.
2807 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002808int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002809{
Mengdong Lin1a497982015-11-18 02:34:11 -05002810 struct snd_soc_pcm_runtime *fe;
2811 int old, new, paths;
Liam Girdwood618dae12012-04-25 12:12:51 +01002812
Liam Girdwood618dae12012-04-25 12:12:51 +01002813 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Mengdong Lin1a497982015-11-18 02:34:11 -05002814 list_for_each_entry(fe, &card->rtd_list, list) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002815 struct snd_soc_dapm_widget_list *list;
Liam Girdwood618dae12012-04-25 12:12:51 +01002816
2817 /* make sure link is FE */
2818 if (!fe->dai_link->dynamic)
2819 continue;
2820
2821 /* only check active links */
2822 if (!fe->cpu_dai->active)
2823 continue;
2824
2825 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002826 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002827 fe->dai_link->name);
2828
2829 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002830 if (!fe->cpu_dai->driver->playback.channels_min
2831 || !fe->codec_dai->driver->playback.channels_min)
2832 goto capture;
2833
2834 /* skip if FE isn't currently playing */
2835 if (!fe->cpu_dai->playback_active
2836 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002837 goto capture;
2838
2839 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2840 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002841 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002842 fe->dai_link->name, "playback");
2843 mutex_unlock(&card->mutex);
2844 return paths;
2845 }
2846
2847 /* update any new playback paths */
2848 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2849 if (new) {
2850 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2851 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2852 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2853 }
2854
2855 /* update any old playback paths */
2856 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2857 if (old) {
2858 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2859 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2860 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2861 }
2862
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002863 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002864capture:
2865 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002866 if (!fe->cpu_dai->driver->capture.channels_min
2867 || !fe->codec_dai->driver->capture.channels_min)
2868 continue;
2869
2870 /* skip if FE isn't currently capturing */
2871 if (!fe->cpu_dai->capture_active
2872 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002873 continue;
2874
2875 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2876 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002877 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002878 fe->dai_link->name, "capture");
2879 mutex_unlock(&card->mutex);
2880 return paths;
2881 }
2882
2883 /* update any new capture paths */
2884 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2885 if (new) {
2886 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2887 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2888 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2889 }
2890
2891 /* update any old capture paths */
2892 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2893 if (old) {
2894 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2895 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2896 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2897 }
2898
2899 dpcm_path_put(&list);
2900 }
2901
2902 mutex_unlock(&card->mutex);
2903 return 0;
2904}
Liam Girdwood01d75842012-04-25 12:12:49 +01002905int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2906{
2907 struct snd_soc_dpcm *dpcm;
2908 struct list_head *clients =
2909 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2910
2911 list_for_each_entry(dpcm, clients, list_be) {
2912
2913 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002914 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002915
2916 if (be->dai_link->ignore_suspend)
2917 continue;
2918
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002919 for (i = 0; i < be->num_codecs; i++) {
2920 struct snd_soc_dai *dai = be->codec_dais[i];
2921 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002922
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002923 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2924 be->dai_link->name);
2925
2926 if (drv->ops && drv->ops->digital_mute &&
2927 dai->playback_active)
2928 drv->ops->digital_mute(dai, mute);
2929 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002930 }
2931
2932 return 0;
2933}
2934
Mark Brown45c0a182012-05-09 21:46:27 +01002935static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002936{
2937 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2938 struct snd_soc_dpcm *dpcm;
2939 struct snd_soc_dapm_widget_list *list;
2940 int ret;
2941 int stream = fe_substream->stream;
2942
2943 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2944 fe->dpcm[stream].runtime = fe_substream->runtime;
2945
Qiao Zhou8f70e512014-09-10 17:54:07 +08002946 ret = dpcm_path_get(fe, stream, &list);
2947 if (ret < 0) {
2948 mutex_unlock(&fe->card->mutex);
2949 return ret;
2950 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002951 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002952 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002953 }
2954
2955 /* calculate valid and active FE <-> BE dpcms */
2956 dpcm_process_paths(fe, stream, &list, 1);
2957
2958 ret = dpcm_fe_dai_startup(fe_substream);
2959 if (ret < 0) {
2960 /* clean up all links */
2961 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2962 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2963
2964 dpcm_be_disconnect(fe, stream);
2965 fe->dpcm[stream].runtime = NULL;
2966 }
2967
2968 dpcm_clear_pending_state(fe, stream);
2969 dpcm_path_put(&list);
2970 mutex_unlock(&fe->card->mutex);
2971 return ret;
2972}
2973
Mark Brown45c0a182012-05-09 21:46:27 +01002974static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002975{
2976 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2977 struct snd_soc_dpcm *dpcm;
2978 int stream = fe_substream->stream, ret;
2979
2980 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2981 ret = dpcm_fe_dai_shutdown(fe_substream);
2982
2983 /* mark FE's links ready to prune */
2984 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2985 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2986
2987 dpcm_be_disconnect(fe, stream);
2988
2989 fe->dpcm[stream].runtime = NULL;
2990 mutex_unlock(&fe->card->mutex);
2991 return ret;
2992}
2993
Liam Girdwoodddee6272011-06-09 14:45:53 +01002994/* create a new pcm */
2995int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2996{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002997 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002998 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002999 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3000 struct snd_pcm *pcm;
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08003001 struct snd_pcm_str *stream;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003002 char new_name[64];
3003 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003004 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003005
Liam Girdwood01d75842012-04-25 12:12:49 +01003006 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00003007 playback = rtd->dai_link->dpcm_playback;
3008 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01003009 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003010 for (i = 0; i < rtd->num_codecs; i++) {
3011 codec_dai = rtd->codec_dais[i];
3012 if (codec_dai->driver->playback.channels_min)
3013 playback = 1;
3014 if (codec_dai->driver->capture.channels_min)
3015 capture = 1;
3016 }
3017
3018 capture = capture && cpu_dai->driver->capture.channels_min;
3019 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01003020 }
Sangsu Parka5002312012-01-02 17:15:10 +09003021
Fabio Estevamd6bead02013-08-29 10:32:13 -03003022 if (rtd->dai_link->playback_only) {
3023 playback = 1;
3024 capture = 0;
3025 }
3026
3027 if (rtd->dai_link->capture_only) {
3028 playback = 0;
3029 capture = 1;
3030 }
3031
Liam Girdwood01d75842012-04-25 12:12:49 +01003032 /* create the PCM */
3033 if (rtd->dai_link->no_pcm) {
3034 snprintf(new_name, sizeof(new_name), "(%s)",
3035 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003036
Liam Girdwood01d75842012-04-25 12:12:49 +01003037 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3038 playback, capture, &pcm);
3039 } else {
3040 if (rtd->dai_link->dynamic)
3041 snprintf(new_name, sizeof(new_name), "%s (*)",
3042 rtd->dai_link->stream_name);
3043 else
3044 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003045 rtd->dai_link->stream_name,
3046 (rtd->num_codecs > 1) ?
3047 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003048
Liam Girdwood01d75842012-04-25 12:12:49 +01003049 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3050 capture, &pcm);
3051 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01003052 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00003053 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01003054 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003055 return ret;
3056 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00003057 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003058
3059 /* DAPM dai link stream work */
3060 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3061
Vinod Koul48c76992015-02-12 09:59:53 +05303062 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003063 rtd->pcm = pcm;
3064 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01003065
3066 if (rtd->dai_link->no_pcm) {
3067 if (playback)
3068 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3069 if (capture)
3070 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
Banajit Goswamia19486b2016-11-21 17:54:51 -08003071 if (platform->driver->pcm_new)
3072 rtd->platform->driver->pcm_new(rtd);
Liam Girdwood01d75842012-04-25 12:12:49 +01003073 goto out;
3074 }
3075
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08003076 /* setup any hostless PCMs - i.e. no host IO is performed */
3077 if (rtd->dai_link->no_host_mode) {
Banajit Goswami7ffe84e2017-01-10 17:28:14 -08003078 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
3079 stream = &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
3080 stream->substream->hw_no_buffer = 1;
3081 snd_soc_set_runtime_hwparams(stream->substream,
3082 &no_host_hardware);
3083 }
3084 if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
3085 stream = &pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
3086 stream->substream->hw_no_buffer = 1;
3087 snd_soc_set_runtime_hwparams(stream->substream,
3088 &no_host_hardware);
3089 }
Liam Girdwoodf5b50e72017-01-10 17:10:17 -08003090 }
3091
Liam Girdwood01d75842012-04-25 12:12:49 +01003092 /* ASoC PCM operations */
3093 if (rtd->dai_link->dynamic) {
3094 rtd->ops.open = dpcm_fe_dai_open;
3095 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3096 rtd->ops.prepare = dpcm_fe_dai_prepare;
3097 rtd->ops.trigger = dpcm_fe_dai_trigger;
3098 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3099 rtd->ops.close = dpcm_fe_dai_close;
3100 rtd->ops.pointer = soc_pcm_pointer;
Kenneth Westfieldd6f99fa2015-05-19 12:03:55 -07003101 rtd->ops.delay_blk = soc_pcm_delay_blk;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003102 rtd->ops.ioctl = soc_pcm_ioctl;
Gopikrishnaiah Anandbdc375e2014-05-30 11:29:56 -07003103 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003104 } else {
3105 rtd->ops.open = soc_pcm_open;
3106 rtd->ops.hw_params = soc_pcm_hw_params;
3107 rtd->ops.prepare = soc_pcm_prepare;
3108 rtd->ops.trigger = soc_pcm_trigger;
3109 rtd->ops.hw_free = soc_pcm_hw_free;
3110 rtd->ops.close = soc_pcm_close;
3111 rtd->ops.pointer = soc_pcm_pointer;
Kenneth Westfieldd6f99fa2015-05-19 12:03:55 -07003112 rtd->ops.delay_blk = soc_pcm_delay_blk;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003113 rtd->ops.ioctl = soc_pcm_ioctl;
Gopikrishnaiah Anandbdc375e2014-05-30 11:29:56 -07003114 rtd->ops.compat_ioctl = soc_pcm_compat_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003115 }
3116
Liam Girdwoodddee6272011-06-09 14:45:53 +01003117 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003118 rtd->ops.ack = platform->driver->ops->ack;
3119 rtd->ops.copy = platform->driver->ops->copy;
3120 rtd->ops.silence = platform->driver->ops->silence;
3121 rtd->ops.page = platform->driver->ops->page;
3122 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003123 }
3124
3125 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003126 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003127
3128 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003129 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003130
3131 if (platform->driver->pcm_new) {
3132 ret = platform->driver->pcm_new(rtd);
3133 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01003134 dev_err(platform->dev,
3135 "ASoC: pcm constructor failed: %d\n",
3136 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003137 return ret;
3138 }
3139 }
3140
3141 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01003142out:
Siena Richard45cbe862016-11-04 13:21:16 -07003143 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003144 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3145 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003146 return ret;
3147}
Liam Girdwood01d75842012-04-25 12:12:49 +01003148
3149/* is the current PCM operation for this FE ? */
3150int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3151{
3152 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3153 return 1;
3154 return 0;
3155}
3156EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3157
3158/* is the current PCM operation for this BE ? */
3159int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3160 struct snd_soc_pcm_runtime *be, int stream)
3161{
3162 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3163 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3164 be->dpcm[stream].runtime_update))
3165 return 1;
3166 return 0;
3167}
3168EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3169
3170/* get the substream for this BE */
3171struct snd_pcm_substream *
3172 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3173{
3174 return be->pcm->streams[stream].substream;
3175}
3176EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3177
3178/* get the BE runtime state */
3179enum snd_soc_dpcm_state
3180 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3181{
3182 return be->dpcm[stream].state;
3183}
3184EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3185
3186/* set the BE runtime state */
3187void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3188 int stream, enum snd_soc_dpcm_state state)
3189{
3190 be->dpcm[stream].state = state;
3191}
3192EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3193
3194/*
3195 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3196 * are not running, paused or suspended for the specified stream direction.
3197 */
3198int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3199 struct snd_soc_pcm_runtime *be, int stream)
3200{
3201 struct snd_soc_dpcm *dpcm;
3202 int state;
3203
3204 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3205
3206 if (dpcm->fe == fe)
3207 continue;
3208
3209 state = dpcm->fe->dpcm[stream].state;
3210 if (state == SND_SOC_DPCM_STATE_START ||
3211 state == SND_SOC_DPCM_STATE_PAUSED ||
3212 state == SND_SOC_DPCM_STATE_SUSPEND)
3213 return 0;
3214 }
3215
3216 /* it's safe to free/stop this BE DAI */
3217 return 1;
3218}
3219EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3220
3221/*
3222 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3223 * running, paused or suspended for the specified stream direction.
3224 */
3225int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3226 struct snd_soc_pcm_runtime *be, int stream)
3227{
3228 struct snd_soc_dpcm *dpcm;
3229 int state;
3230
3231 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3232
3233 if (dpcm->fe == fe)
3234 continue;
3235
3236 state = dpcm->fe->dpcm[stream].state;
3237 if (state == SND_SOC_DPCM_STATE_START ||
3238 state == SND_SOC_DPCM_STATE_PAUSED ||
3239 state == SND_SOC_DPCM_STATE_SUSPEND ||
3240 state == SND_SOC_DPCM_STATE_PREPARE)
3241 return 0;
3242 }
3243
3244 /* it's safe to change hw_params */
3245 return 1;
3246}
3247EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003248
Liam Girdwood07bf84a2012-04-25 12:12:52 +01003249int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
3250 int cmd, struct snd_soc_platform *platform)
3251{
Mark Brownc5914b02013-10-30 17:47:39 -07003252 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01003253 return platform->driver->ops->trigger(substream, cmd);
3254 return 0;
3255}
3256EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
3257
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003258#ifdef CONFIG_DEBUG_FS
3259static char *dpcm_state_string(enum snd_soc_dpcm_state state)
3260{
3261 switch (state) {
3262 case SND_SOC_DPCM_STATE_NEW:
3263 return "new";
3264 case SND_SOC_DPCM_STATE_OPEN:
3265 return "open";
3266 case SND_SOC_DPCM_STATE_HW_PARAMS:
3267 return "hw_params";
3268 case SND_SOC_DPCM_STATE_PREPARE:
3269 return "prepare";
3270 case SND_SOC_DPCM_STATE_START:
3271 return "start";
3272 case SND_SOC_DPCM_STATE_STOP:
3273 return "stop";
3274 case SND_SOC_DPCM_STATE_SUSPEND:
3275 return "suspend";
3276 case SND_SOC_DPCM_STATE_PAUSED:
3277 return "paused";
3278 case SND_SOC_DPCM_STATE_HW_FREE:
3279 return "hw_free";
3280 case SND_SOC_DPCM_STATE_CLOSE:
3281 return "close";
3282 }
3283
3284 return "unknown";
3285}
3286
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003287static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3288 int stream, char *buf, size_t size)
3289{
3290 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3291 struct snd_soc_dpcm *dpcm;
3292 ssize_t offset = 0;
3293
3294 /* FE state */
3295 offset += snprintf(buf + offset, size - offset,
3296 "[%s - %s]\n", fe->dai_link->name,
3297 stream ? "Capture" : "Playback");
3298
3299 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3300 dpcm_state_string(fe->dpcm[stream].state));
3301
3302 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3303 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3304 offset += snprintf(buf + offset, size - offset,
3305 "Hardware Params: "
3306 "Format = %s, Channels = %d, Rate = %d\n",
3307 snd_pcm_format_name(params_format(params)),
3308 params_channels(params),
3309 params_rate(params));
3310
3311 /* BEs state */
3312 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3313
3314 if (list_empty(&fe->dpcm[stream].be_clients)) {
3315 offset += snprintf(buf + offset, size - offset,
3316 " No active DSP links\n");
3317 goto out;
3318 }
3319
3320 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3321 struct snd_soc_pcm_runtime *be = dpcm->be;
3322 params = &dpcm->hw_params;
3323
3324 offset += snprintf(buf + offset, size - offset,
3325 "- %s\n", be->dai_link->name);
3326
3327 offset += snprintf(buf + offset, size - offset,
3328 " State: %s\n",
3329 dpcm_state_string(be->dpcm[stream].state));
3330
3331 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3332 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3333 offset += snprintf(buf + offset, size - offset,
3334 " Hardware Params: "
3335 "Format = %s, Channels = %d, Rate = %d\n",
3336 snd_pcm_format_name(params_format(params)),
3337 params_channels(params),
3338 params_rate(params));
3339 }
3340
3341out:
3342 return offset;
3343}
3344
3345static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3346 size_t count, loff_t *ppos)
3347{
3348 struct snd_soc_pcm_runtime *fe = file->private_data;
3349 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3350 char *buf;
3351
3352 buf = kmalloc(out_count, GFP_KERNEL);
3353 if (!buf)
3354 return -ENOMEM;
3355
3356 if (fe->cpu_dai->driver->playback.channels_min)
3357 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3358 buf + offset, out_count - offset);
3359
3360 if (fe->cpu_dai->driver->capture.channels_min)
3361 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3362 buf + offset, out_count - offset);
3363
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003364 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003365
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003366 kfree(buf);
3367 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003368}
3369
3370static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003371 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003372 .read = dpcm_state_read_file,
3373 .llseek = default_llseek,
3374};
3375
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003376void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003377{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003378 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003379 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003380
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003381 if (!rtd->card->debugfs_card_root)
3382 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003383
3384 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3385 rtd->card->debugfs_card_root);
3386 if (!rtd->debugfs_dpcm_root) {
3387 dev_dbg(rtd->dev,
3388 "ASoC: Failed to create dpcm debugfs directory %s\n",
3389 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003390 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003391 }
3392
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003393 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003394 rtd->debugfs_dpcm_root,
3395 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003396}
3397#endif