blob: daaa670ee9b767bca5e305a1f85232912b55a951 [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>
Liam Girdwoodddee6272011-06-09 14:45:53 +010028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
Liam Girdwood01d75842012-04-25 12:12:49 +010032#include <sound/soc-dpcm.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010033#include <sound/initval.h>
34
Liam Girdwood01d75842012-04-25 12:12:49 +010035#define DPCM_MAX_BE_USERS 8
36
Ricard Wanderlofcde79032015-08-24 14:16:51 +020037/*
38 * snd_soc_dai_stream_valid() - check if a DAI supports the given stream
39 *
40 * Returns true if the DAI supports the indicated stream type.
41 */
42static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
43{
44 struct snd_soc_pcm_stream *codec_stream;
45
46 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
47 codec_stream = &dai->driver->playback;
48 else
49 codec_stream = &dai->driver->capture;
50
51 /* If the codec specifies any rate at all, it supports the stream. */
52 return codec_stream->rates;
53}
54
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020055/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010056 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
57 * @rtd: ASoC PCM runtime that is activated
58 * @stream: Direction of the PCM stream
59 *
60 * Increments the active count for all the DAIs and components attached to a PCM
61 * runtime. Should typically be called when a stream is opened.
62 *
63 * Must be called with the rtd->pcm_mutex being held
64 */
65void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
66{
67 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020068 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010069
70 lockdep_assert_held(&rtd->pcm_mutex);
71
72 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
73 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020074 for (i = 0; i < rtd->num_codecs; i++)
75 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010076 } else {
77 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020078 for (i = 0; i < rtd->num_codecs; i++)
79 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010080 }
81
82 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010083 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020084 for (i = 0; i < rtd->num_codecs; i++) {
85 rtd->codec_dais[i]->active++;
86 rtd->codec_dais[i]->component->active++;
87 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010088}
89
90/**
91 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
92 * @rtd: ASoC PCM runtime that is deactivated
93 * @stream: Direction of the PCM stream
94 *
95 * Decrements the active count for all the DAIs and components attached to a PCM
96 * runtime. Should typically be called when a stream is closed.
97 *
98 * Must be called with the rtd->pcm_mutex being held
99 */
100void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
101{
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200103 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100104
105 lockdep_assert_held(&rtd->pcm_mutex);
106
107 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
108 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200109 for (i = 0; i < rtd->num_codecs; i++)
110 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100111 } else {
112 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200113 for (i = 0; i < rtd->num_codecs; i++)
114 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100115 }
116
117 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100118 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200119 for (i = 0; i < rtd->num_codecs; i++) {
120 rtd->codec_dais[i]->component->active--;
121 rtd->codec_dais[i]->active--;
122 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100123}
124
125/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100126 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
127 * @rtd: The ASoC PCM runtime that should be checked.
128 *
129 * This function checks whether the power down delay should be ignored for a
130 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
131 * been configured to ignore the delay, or if none of the components benefits
132 * from having the delay.
133 */
134bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
135{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200136 int i;
137 bool ignore = true;
138
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100139 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
140 return true;
141
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200142 for (i = 0; i < rtd->num_codecs; i++)
143 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
144
145 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100146}
147
148/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200149 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
150 * @substream: the pcm substream
151 * @hw: the hardware parameters
152 *
153 * Sets the substream runtime hardware parameters.
154 */
155int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
156 const struct snd_pcm_hardware *hw)
157{
158 struct snd_pcm_runtime *runtime = substream->runtime;
159 runtime->hw.info = hw->info;
160 runtime->hw.formats = hw->formats;
161 runtime->hw.period_bytes_min = hw->period_bytes_min;
162 runtime->hw.period_bytes_max = hw->period_bytes_max;
163 runtime->hw.periods_min = hw->periods_min;
164 runtime->hw.periods_max = hw->periods_max;
165 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
166 runtime->hw.fifo_size = hw->fifo_size;
167 return 0;
168}
169EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
170
Liam Girdwood01d75842012-04-25 12:12:49 +0100171/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000172int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100173 int event)
174{
175 struct snd_soc_dpcm *dpcm;
176
177 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
178
179 struct snd_soc_pcm_runtime *be = dpcm->be;
180
Liam Girdwood103d84a2012-11-19 14:39:15 +0000181 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100182 be->dai_link->name, event, dir);
183
Banajit Goswamib1cd2e32017-07-14 23:15:05 -0700184 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
185 (be->dpcm[dir].users >= 1))
186 continue;
187
Liam Girdwood01d75842012-04-25 12:12:49 +0100188 snd_soc_dapm_stream_event(be, dir, event);
189 }
190
191 snd_soc_dapm_stream_event(fe, dir, event);
192
193 return 0;
194}
195
Dong Aisheng17841022011-08-29 17:15:14 +0800196static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
197 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100198{
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100200 int ret;
201
Nicolin Chen3635bf02013-11-13 18:56:24 +0800202 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
203 rtd->dai_link->symmetric_rates)) {
204 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
205 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100206
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200207 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800208 SNDRV_PCM_HW_PARAM_RATE,
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200209 soc_dai->rate);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800210 if (ret < 0) {
211 dev_err(soc_dai->dev,
212 "ASoC: Unable to apply rate constraint: %d\n",
213 ret);
214 return ret;
215 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100216 }
217
Nicolin Chen3635bf02013-11-13 18:56:24 +0800218 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
219 rtd->dai_link->symmetric_channels)) {
220 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
221 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100222
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200223 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800224 SNDRV_PCM_HW_PARAM_CHANNELS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800225 soc_dai->channels);
226 if (ret < 0) {
227 dev_err(soc_dai->dev,
228 "ASoC: Unable to apply channel symmetry constraint: %d\n",
229 ret);
230 return ret;
231 }
232 }
233
234 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
235 rtd->dai_link->symmetric_samplebits)) {
236 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
237 soc_dai->sample_bits);
238
Lars-Peter Clausen4dcdd432015-10-18 15:39:28 +0200239 ret = snd_pcm_hw_constraint_single(substream->runtime,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800240 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
Nicolin Chen3635bf02013-11-13 18:56:24 +0800241 soc_dai->sample_bits);
242 if (ret < 0) {
243 dev_err(soc_dai->dev,
244 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
245 ret);
246 return ret;
247 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100248 }
249
250 return 0;
251}
252
Nicolin Chen3635bf02013-11-13 18:56:24 +0800253static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params)
255{
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200258 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800259
260 rate = params_rate(params);
261 channels = params_channels(params);
262 sample_bits = snd_pcm_format_physical_width(params_format(params));
263
264 /* reject unmatched parameters when applying symmetry */
265 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800266 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200267
268 for (i = 0; i < rtd->num_codecs; i++)
269 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
270
Nicolin Chen3635bf02013-11-13 18:56:24 +0800271 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
272 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
273 cpu_dai->rate, rate);
274 return -EINVAL;
275 }
276
277 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800278 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200279
280 for (i = 0; i < rtd->num_codecs; i++)
281 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
282
Nicolin Chen3635bf02013-11-13 18:56:24 +0800283 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
284 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
285 cpu_dai->channels, channels);
286 return -EINVAL;
287 }
288
289 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800290 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200291
292 for (i = 0; i < rtd->num_codecs; i++)
293 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
294
Nicolin Chen3635bf02013-11-13 18:56:24 +0800295 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
296 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
297 cpu_dai->sample_bits, sample_bits);
298 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100299 }
300
301 return 0;
302}
303
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100304static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
305{
306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
307 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100308 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200309 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100310
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200311 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
312 cpu_driver->symmetric_channels || link->symmetric_channels ||
313 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
314
315 for (i = 0; i < rtd->num_codecs; i++)
316 symmetry = symmetry ||
317 rtd->codec_dais[i]->driver->symmetric_rates ||
318 rtd->codec_dais[i]->driver->symmetric_channels ||
319 rtd->codec_dais[i]->driver->symmetric_samplebits;
320
321 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100322}
323
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200324static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000325{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200326 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100327 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000328
329 if (!bits)
330 return;
331
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100332 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
333 if (ret != 0)
334 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
335 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000336}
337
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200338static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200339{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200340 struct snd_soc_pcm_runtime *rtd = substream->private_data;
341 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200342 struct snd_soc_dai *codec_dai;
343 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200344 unsigned int bits = 0, cpu_bits;
345
346 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200347 for (i = 0; i < rtd->num_codecs; i++) {
348 codec_dai = rtd->codec_dais[i];
349 if (codec_dai->driver->playback.sig_bits == 0) {
350 bits = 0;
351 break;
352 }
353 bits = max(codec_dai->driver->playback.sig_bits, bits);
354 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200355 cpu_bits = cpu_dai->driver->playback.sig_bits;
356 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200357 for (i = 0; i < rtd->num_codecs; i++) {
358 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200359 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200360 bits = 0;
361 break;
362 }
363 bits = max(codec_dai->driver->capture.sig_bits, bits);
364 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200365 cpu_bits = cpu_dai->driver->capture.sig_bits;
366 }
367
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200368 soc_pcm_set_msb(substream, bits);
369 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200370}
371
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200373{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200374 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100375 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200376 struct snd_soc_pcm_runtime *rtd = substream->private_data;
377 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
378 struct snd_soc_dai_driver *codec_dai_drv;
379 struct snd_soc_pcm_stream *codec_stream;
380 struct snd_soc_pcm_stream *cpu_stream;
381 unsigned int chan_min = 0, chan_max = UINT_MAX;
382 unsigned int rate_min = 0, rate_max = UINT_MAX;
383 unsigned int rates = UINT_MAX;
384 u64 formats = ULLONG_MAX;
385 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100386
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200387 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
388 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100389 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200390 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100391
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200392 /* first calculate min/max only for CODECs in the DAI link */
393 for (i = 0; i < rtd->num_codecs; i++) {
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200394
395 /*
396 * Skip CODECs which don't support the current stream type.
397 * Otherwise, since the rate, channel, and format values will
398 * zero in that case, we would have no usable settings left,
399 * causing the resulting setup to fail.
400 * At least one CODEC should match, otherwise we should have
401 * bailed out on a higher level, since there would be no
402 * CODEC to support the transfer direction in that case.
403 */
404 if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
405 substream->stream))
406 continue;
407
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200408 codec_dai_drv = rtd->codec_dais[i]->driver;
409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
410 codec_stream = &codec_dai_drv->playback;
411 else
412 codec_stream = &codec_dai_drv->capture;
413 chan_min = max(chan_min, codec_stream->channels_min);
414 chan_max = min(chan_max, codec_stream->channels_max);
415 rate_min = max(rate_min, codec_stream->rate_min);
416 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
417 formats &= codec_stream->formats;
418 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
419 }
420
421 /*
422 * chan min/max cannot be enforced if there are multiple CODEC DAIs
423 * connected to a single CPU DAI, use CPU DAI's directly and let
424 * channel allocation be fixed up later
425 */
426 if (rtd->num_codecs > 1) {
427 chan_min = cpu_stream->channels_min;
428 chan_max = cpu_stream->channels_max;
429 }
430
431 hw->channels_min = max(chan_min, cpu_stream->channels_min);
432 hw->channels_max = min(chan_max, cpu_stream->channels_max);
433 if (hw->formats)
434 hw->formats &= formats & cpu_stream->formats;
435 else
436 hw->formats = formats & cpu_stream->formats;
437 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100438
439 snd_pcm_limit_hw_rates(runtime);
440
441 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200442 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100443 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200444 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200445}
446
Mark Brown58ba9b22012-01-16 18:38:51 +0000447/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100448 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
449 * then initialized and any private data can be allocated. This also calls
450 * startup for the cpu DAI, platform, machine and codec DAI.
451 */
452static int soc_pcm_open(struct snd_pcm_substream *substream)
453{
454 struct snd_soc_pcm_runtime *rtd = substream->private_data;
455 struct snd_pcm_runtime *runtime = substream->runtime;
456 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000457 struct snd_soc_component *component;
458 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100459 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200460 struct snd_soc_dai *codec_dai;
461 const char *codec_dai_name = "multicodec";
Kuninori Morimotob8135862017-10-11 01:37:23 +0000462 int i, ret = 0, __ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100463
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800464 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200465 for (i = 0; i < rtd->num_codecs; i++)
466 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000467
468 for_each_rtdcom(rtd, rtdcom) {
469 component = rtdcom->component;
470
471 pm_runtime_get_sync(component->dev);
472 }
Mark Brownd6652ef2011-12-03 20:14:31 +0000473
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100474 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100475
476 /* startup the audio subsystem */
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000477 if (cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100478 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
479 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000480 dev_err(cpu_dai->dev, "ASoC: can't open interface"
481 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100482 goto out;
483 }
484 }
485
Kuninori Morimotob8135862017-10-11 01:37:23 +0000486 if (platform && platform->driver->ops && platform->driver->ops->open) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100487 ret = platform->driver->ops->open(substream);
488 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000489 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200490 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100491 goto platform_err;
492 }
493 }
494
Kuninori Morimotob8135862017-10-11 01:37:23 +0000495 ret = 0;
496 for_each_rtdcom(rtd, rtdcom) {
497 component = rtdcom->component;
498
499 /* ignore duplication for now */
500 if (platform && (component == &platform->component))
501 continue;
502
503 if (!component->driver->ops ||
504 !component->driver->ops->open)
505 continue;
506
507 __ret = component->driver->ops->open(substream);
508 if (__ret < 0) {
509 dev_err(component->dev,
510 "ASoC: can't open component %s: %d\n",
511 component->name, ret);
512 ret = __ret;
513 }
514 }
515 if (ret < 0)
516 goto component_err;
517
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200518 for (i = 0; i < rtd->num_codecs; i++) {
519 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000520 if (codec_dai->driver->ops->startup) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200521 ret = codec_dai->driver->ops->startup(substream,
522 codec_dai);
523 if (ret < 0) {
524 dev_err(codec_dai->dev,
525 "ASoC: can't open codec %s: %d\n",
526 codec_dai->name, ret);
527 goto codec_dai_err;
528 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100529 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200530
531 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
532 codec_dai->tx_mask = 0;
533 else
534 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100535 }
536
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000537 if (rtd->dai_link->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 ret = rtd->dai_link->ops->startup(substream);
539 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000540 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000541 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100542 goto machine_err;
543 }
544 }
545
Liam Girdwood01d75842012-04-25 12:12:49 +0100546 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
547 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
548 goto dynamic;
549
Liam Girdwoodddee6272011-06-09 14:45:53 +0100550 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200551 soc_pcm_init_runtime_hw(substream);
552
553 if (rtd->num_codecs == 1)
554 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100555
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100556 if (soc_pcm_has_symmetry(substream))
557 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
558
Liam Girdwoodddee6272011-06-09 14:45:53 +0100559 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100560 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000561 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200562 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 goto config_err;
564 }
565 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000566 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200567 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100568 goto config_err;
569 }
570 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
571 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000572 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200573 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100574 goto config_err;
575 }
576
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200577 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000578
Liam Girdwoodddee6272011-06-09 14:45:53 +0100579 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800580 if (cpu_dai->active) {
581 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
582 if (ret != 0)
583 goto config_err;
584 }
585
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200586 for (i = 0; i < rtd->num_codecs; i++) {
587 if (rtd->codec_dais[i]->active) {
588 ret = soc_pcm_apply_symmetry(substream,
589 rtd->codec_dais[i]);
590 if (ret != 0)
591 goto config_err;
592 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100593 }
594
Liam Girdwood103d84a2012-11-19 14:39:15 +0000595 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200596 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000597 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
598 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100599 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000600 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100601 runtime->hw.rate_max);
602
Liam Girdwood01d75842012-04-25 12:12:49 +0100603dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100604
605 snd_soc_runtime_activate(rtd, substream->stream);
606
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100607 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100608 return 0;
609
610config_err:
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000611 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612 rtd->dai_link->ops->shutdown(substream);
613
614machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200615 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100616
617codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200618 while (--i >= 0) {
619 codec_dai = rtd->codec_dais[i];
620 if (codec_dai->driver->ops->shutdown)
621 codec_dai->driver->ops->shutdown(substream, codec_dai);
622 }
623
Kuninori Morimotob8135862017-10-11 01:37:23 +0000624component_err:
625 for_each_rtdcom(rtd, rtdcom) {
626 component = rtdcom->component;
627
628 /* ignore duplication for now */
629 if (platform && (component == &platform->component))
630 continue;
631
632 if (!component->driver->ops ||
633 !component->driver->ops->close)
634 continue;
635
636 component->driver->ops->close(substream);
637 }
638
639 if (platform && platform->driver->ops && platform->driver->ops->close)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100640 platform->driver->ops->close(substream);
641
642platform_err:
643 if (cpu_dai->driver->ops->shutdown)
644 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
645out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100646 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000647
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000648 for_each_rtdcom(rtd, rtdcom) {
649 component = rtdcom->component;
650
651 pm_runtime_mark_last_busy(component->dev);
652 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530653 }
654
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200655 for (i = 0; i < rtd->num_codecs; i++) {
656 if (!rtd->codec_dais[i]->active)
657 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
658 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800659 if (!cpu_dai->active)
660 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000661
Liam Girdwoodddee6272011-06-09 14:45:53 +0100662 return ret;
663}
664
665/*
666 * Power down the audio subsystem pmdown_time msecs after close is called.
667 * This is to ensure there are no pops or clicks in between any music tracks
668 * due to DAPM power cycling.
669 */
670static void close_delayed_work(struct work_struct *work)
671{
672 struct snd_soc_pcm_runtime *rtd =
673 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200674 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100676 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100677
Liam Girdwood103d84a2012-11-19 14:39:15 +0000678 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100679 codec_dai->driver->playback.stream_name,
680 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600681 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100682
683 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600684 if (rtd->pop_wait == 1) {
685 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800686 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000687 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100688 }
689
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100690 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100691}
692
693/*
694 * Called by ALSA when a PCM substream is closed. Private data can be
695 * freed here. The cpu DAI, codec DAI, machine and platform are also
696 * shutdown.
697 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100698static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100699{
700 struct snd_soc_pcm_runtime *rtd = substream->private_data;
701 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000702 struct snd_soc_component *component;
703 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100704 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200705 struct snd_soc_dai *codec_dai;
706 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100707
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100708 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100709
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100710 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100711
Dong Aisheng17841022011-08-29 17:15:14 +0800712 /* clear the corresponding DAIs rate when inactive */
713 if (!cpu_dai->active)
714 cpu_dai->rate = 0;
715
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200716 for (i = 0; i < rtd->num_codecs; i++) {
717 codec_dai = rtd->codec_dais[i];
718 if (!codec_dai->active)
719 codec_dai->rate = 0;
720 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200721
Ramesh Babuae116012014-10-15 12:34:59 +0530722 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
723
Liam Girdwoodddee6272011-06-09 14:45:53 +0100724 if (cpu_dai->driver->ops->shutdown)
725 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
726
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200727 for (i = 0; i < rtd->num_codecs; i++) {
728 codec_dai = rtd->codec_dais[i];
729 if (codec_dai->driver->ops->shutdown)
730 codec_dai->driver->ops->shutdown(substream, codec_dai);
731 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100732
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000733 if (rtd->dai_link->ops->shutdown)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100734 rtd->dai_link->ops->shutdown(substream);
735
Kuninori Morimotob8135862017-10-11 01:37:23 +0000736 if (platform && platform->driver->ops && platform->driver->ops->close)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100738
Kuninori Morimotob8135862017-10-11 01:37:23 +0000739 for_each_rtdcom(rtd, rtdcom) {
740 component = rtdcom->component;
741
742 /* ignore duplication for now */
743 if (platform && (component == &platform->component))
744 continue;
745
746 if (!component->driver->ops ||
747 !component->driver->ops->close)
748 continue;
749
750 component->driver->ops->close(substream);
751 }
752
Liam Girdwoodddee6272011-06-09 14:45:53 +0100753 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100754 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300755 /* powered down playback stream now */
756 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800758 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300759 } else {
760 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600761 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100762 queue_delayed_work(system_power_efficient_wq,
763 &rtd->delayed_work,
764 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300765 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100766 } else {
767 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800768 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000769 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100770 }
771
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100772 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000773
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000774 for_each_rtdcom(rtd, rtdcom) {
775 component = rtdcom->component;
Sanyog Kale3f809782016-01-05 17:14:49 +0530776
Kuninori Morimoto90be7112017-08-08 06:18:10 +0000777 pm_runtime_mark_last_busy(component->dev);
778 pm_runtime_put_autosuspend(component->dev);
Sanyog Kale3f809782016-01-05 17:14:49 +0530779 }
780
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200781 for (i = 0; i < rtd->num_codecs; i++) {
782 if (!rtd->codec_dais[i]->active)
783 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
784 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800785 if (!cpu_dai->active)
786 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000787
Liam Girdwoodddee6272011-06-09 14:45:53 +0100788 return 0;
789}
790
791/*
792 * Called by ALSA when the PCM substream is prepared, can set format, sample
793 * rate, etc. This function is non atomic and can be called multiple times,
794 * it can refer to the runtime info.
795 */
796static int soc_pcm_prepare(struct snd_pcm_substream *substream)
797{
798 struct snd_soc_pcm_runtime *rtd = substream->private_data;
799 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000800 struct snd_soc_component *component;
801 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100802 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200803 struct snd_soc_dai *codec_dai;
804 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100805
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100806 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100807
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000808 if (rtd->dai_link->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100809 ret = rtd->dai_link->ops->prepare(substream);
810 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000811 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
812 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100813 goto out;
814 }
815 }
816
Kuninori Morimotob8135862017-10-11 01:37:23 +0000817 if (platform && platform->driver->ops && platform->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100818 ret = platform->driver->ops->prepare(substream);
819 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000820 dev_err(platform->dev, "ASoC: platform prepare error:"
821 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822 goto out;
823 }
824 }
825
Kuninori Morimotob8135862017-10-11 01:37:23 +0000826 for_each_rtdcom(rtd, rtdcom) {
827 component = rtdcom->component;
828
829 /* ignore duplication for now */
830 if (platform && (component == &platform->component))
831 continue;
832
833 if (!component->driver->ops ||
834 !component->driver->ops->prepare)
835 continue;
836
837 ret = component->driver->ops->prepare(substream);
838 if (ret < 0) {
839 dev_err(component->dev,
840 "ASoC: platform prepare error: %d\n", ret);
841 goto out;
842 }
843 }
844
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200845 for (i = 0; i < rtd->num_codecs; i++) {
846 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000847 if (codec_dai->driver->ops->prepare) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200848 ret = codec_dai->driver->ops->prepare(substream,
849 codec_dai);
850 if (ret < 0) {
851 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200852 "ASoC: codec DAI prepare error: %d\n",
853 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200854 goto out;
855 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100856 }
857 }
858
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000859 if (cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100860 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
861 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200862 dev_err(cpu_dai->dev,
863 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100864 goto out;
865 }
866 }
867
868 /* cancel any delayed stream shutdown that is pending */
869 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600870 rtd->pop_wait) {
871 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100872 cancel_delayed_work(&rtd->delayed_work);
873 }
874
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000875 snd_soc_dapm_stream_event(rtd, substream->stream,
876 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100877
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200878 for (i = 0; i < rtd->num_codecs; i++)
879 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
880 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530881 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100882
883out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100884 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100885 return ret;
886}
887
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200888static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
889 unsigned int mask)
890{
891 struct snd_interval *interval;
892 int channels = hweight_long(mask);
893
894 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
895 interval->min = channels;
896 interval->max = channels;
897}
898
Benoit Cousson93e69582014-07-08 23:19:38 +0200899int soc_dai_hw_params(struct snd_pcm_substream *substream,
900 struct snd_pcm_hw_params *params,
901 struct snd_soc_dai *dai)
902{
903 int ret;
904
Kuninori Morimoto9900a422017-09-25 01:38:54 +0000905 if (dai->driver->ops->hw_params) {
Benoit Cousson93e69582014-07-08 23:19:38 +0200906 ret = dai->driver->ops->hw_params(substream, params, dai);
907 if (ret < 0) {
908 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
909 dai->name, ret);
910 return ret;
911 }
912 }
913
914 return 0;
915}
916
Liam Girdwoodddee6272011-06-09 14:45:53 +0100917/*
918 * Called by ALSA when the hardware params are set by application. This
919 * function can also be called multiple times and can allocate buffers
920 * (using snd_pcm_lib_* ). It's non-atomic.
921 */
922static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
923 struct snd_pcm_hw_params *params)
924{
925 struct snd_soc_pcm_runtime *rtd = substream->private_data;
926 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000927 struct snd_soc_component *component;
928 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100929 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotob8135862017-10-11 01:37:23 +0000930 int i, ret = 0, __ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100931
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100932 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +0000933 if (rtd->dai_link->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100934 ret = rtd->dai_link->ops->hw_params(substream, params);
935 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000936 dev_err(rtd->card->dev, "ASoC: machine hw_params"
937 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100938 goto out;
939 }
940 }
941
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200942 for (i = 0; i < rtd->num_codecs; i++) {
943 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
944 struct snd_pcm_hw_params codec_params;
945
Ricard Wanderlofcde79032015-08-24 14:16:51 +0200946 /*
947 * Skip CODECs which don't support the current stream type,
948 * the idea being that if a CODEC is not used for the currently
949 * set up transfer direction, it should not need to be
950 * configured, especially since the configuration used might
951 * not even be supported by that CODEC. There may be cases
952 * however where a CODEC needs to be set up although it is
953 * actually not being used for the transfer, e.g. if a
954 * capture-only CODEC is acting as an LRCLK and/or BCLK master
955 * for the DAI link including a playback-only CODEC.
956 * If this becomes necessary, we will have to augment the
957 * machine driver setup with information on how to act, so
958 * we can do the right thing here.
959 */
960 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
961 continue;
962
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200963 /* copy params for each codec */
964 codec_params = *params;
965
966 /* fixup params based on TDM slot masks */
967 if (codec_dai->tx_mask)
968 soc_pcm_codec_params_fixup(&codec_params,
969 codec_dai->tx_mask);
970 if (codec_dai->rx_mask)
971 soc_pcm_codec_params_fixup(&codec_params,
972 codec_dai->rx_mask);
973
Benoit Cousson93e69582014-07-08 23:19:38 +0200974 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
975 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100976 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200977
978 codec_dai->rate = params_rate(&codec_params);
979 codec_dai->channels = params_channels(&codec_params);
980 codec_dai->sample_bits = snd_pcm_format_physical_width(
981 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100982 }
983
Benoit Cousson93e69582014-07-08 23:19:38 +0200984 ret = soc_dai_hw_params(substream, params, cpu_dai);
985 if (ret < 0)
986 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100987
Kuninori Morimotob8135862017-10-11 01:37:23 +0000988 if (platform && platform->driver->ops && platform->driver->ops->hw_params) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100989 ret = platform->driver->ops->hw_params(substream, params);
990 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000991 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200992 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100993 goto platform_err;
994 }
995 }
996
Kuninori Morimotob8135862017-10-11 01:37:23 +0000997 ret = 0;
998 for_each_rtdcom(rtd, rtdcom) {
999 component = rtdcom->component;
1000
1001 /* ignore duplication for now */
1002 if (platform && (component == &platform->component))
1003 continue;
1004
1005 if (!component->driver->ops ||
1006 !component->driver->ops->hw_params)
1007 continue;
1008
1009 __ret = component->driver->ops->hw_params(substream, params);
1010 if (__ret < 0) {
1011 dev_err(component->dev,
1012 "ASoC: %s hw params failed: %d\n",
1013 component->name, ret);
1014 ret = __ret;
1015 }
1016 }
1017 if (ret < 0)
1018 goto component_err;
1019
Nicolin Chen3635bf02013-11-13 18:56:24 +08001020 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +08001021 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +08001022 cpu_dai->channels = params_channels(params);
1023 cpu_dai->sample_bits =
1024 snd_pcm_format_physical_width(params_format(params));
1025
jiada wang957ce0c2017-09-20 15:25:30 +09001026 ret = soc_pcm_params_symmetry(substream, params);
1027 if (ret)
Kuninori Morimotob8135862017-10-11 01:37:23 +00001028 goto component_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001029out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001030 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001031 return ret;
1032
Kuninori Morimotob8135862017-10-11 01:37:23 +00001033component_err:
1034 for_each_rtdcom(rtd, rtdcom) {
1035 component = rtdcom->component;
1036
1037 /* ignore duplication */
1038 if (platform && (component == &platform->component))
1039 continue;
1040
1041 if (!component->driver->ops ||
1042 !component->driver->ops->hw_free)
1043 continue;
1044
1045 component->driver->ops->hw_free(substream);
1046 }
1047
1048 if (platform && platform->driver->ops && platform->driver->ops->hw_free)
1049 platform->driver->ops->hw_free(substream);
1050
Liam Girdwoodddee6272011-06-09 14:45:53 +01001051platform_err:
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001052 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001053 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1054
1055interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001056 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001057
1058codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001059 while (--i >= 0) {
1060 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001061 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001062 codec_dai->driver->ops->hw_free(substream, codec_dai);
1063 codec_dai->rate = 0;
1064 }
1065
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001066 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001067 rtd->dai_link->ops->hw_free(substream);
1068
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001069 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001070 return ret;
1071}
1072
1073/*
1074 * Frees resources allocated by hw_params, can be called multiple times
1075 */
1076static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1077{
1078 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1079 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001080 struct snd_soc_component *component;
1081 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001082 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001083 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +08001084 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001085 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001086
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001087 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001088
Nicolin Chend3383422013-11-20 18:37:09 +08001089 /* clear the corresponding DAIs parameters when going to be inactive */
1090 if (cpu_dai->active == 1) {
1091 cpu_dai->rate = 0;
1092 cpu_dai->channels = 0;
1093 cpu_dai->sample_bits = 0;
1094 }
1095
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001096 for (i = 0; i < rtd->num_codecs; i++) {
1097 codec_dai = rtd->codec_dais[i];
1098 if (codec_dai->active == 1) {
1099 codec_dai->rate = 0;
1100 codec_dai->channels = 0;
1101 codec_dai->sample_bits = 0;
1102 }
Nicolin Chend3383422013-11-20 18:37:09 +08001103 }
1104
Liam Girdwoodddee6272011-06-09 14:45:53 +01001105 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001106 for (i = 0; i < rtd->num_codecs; i++) {
1107 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
1108 (!playback && rtd->codec_dais[i]->capture_active == 1))
1109 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
1110 substream->stream);
1111 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001112
1113 /* free any machine hw params */
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001114 if (rtd->dai_link->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001115 rtd->dai_link->ops->hw_free(substream);
1116
1117 /* free any DMA resources */
Kuninori Morimotob8135862017-10-11 01:37:23 +00001118 if (platform && platform->driver->ops && platform->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001119 platform->driver->ops->hw_free(substream);
1120
Kuninori Morimotob8135862017-10-11 01:37:23 +00001121 /* free any component resources */
1122 for_each_rtdcom(rtd, rtdcom) {
1123 component = rtdcom->component;
1124
1125 /* ignore duplication for now */
1126 if (platform && (component == &platform->component))
1127 continue;
1128
1129 if (!component->driver->ops ||
1130 !component->driver->ops->hw_free)
1131 continue;
1132
1133 component->driver->ops->hw_free(substream);
1134 }
1135
Liam Girdwoodddee6272011-06-09 14:45:53 +01001136 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001137 for (i = 0; i < rtd->num_codecs; i++) {
1138 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001139 if (codec_dai->driver->ops->hw_free)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001140 codec_dai->driver->ops->hw_free(substream, codec_dai);
1141 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001142
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001143 if (cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001144 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1145
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001146 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001147 return 0;
1148}
1149
1150static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1151{
1152 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1153 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001154 struct snd_soc_component *component;
1155 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001156 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001157 struct snd_soc_dai *codec_dai;
1158 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001159
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001160 for (i = 0; i < rtd->num_codecs; i++) {
1161 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001162 if (codec_dai->driver->ops->trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001163 ret = codec_dai->driver->ops->trigger(substream,
1164 cmd, codec_dai);
1165 if (ret < 0)
1166 return ret;
1167 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01001168 }
1169
Kuninori Morimotob8135862017-10-11 01:37:23 +00001170 if (platform && platform->driver->ops && platform->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001171 ret = platform->driver->ops->trigger(substream, cmd);
1172 if (ret < 0)
1173 return ret;
1174 }
1175
Kuninori Morimotob8135862017-10-11 01:37:23 +00001176 for_each_rtdcom(rtd, rtdcom) {
1177 component = rtdcom->component;
1178
1179 /* ignore duplication for now */
1180 if (platform && (component == &platform->component))
1181 continue;
1182
1183 if (!component->driver->ops ||
1184 !component->driver->ops->trigger)
1185 continue;
1186
1187 ret = component->driver->ops->trigger(substream, cmd);
1188 if (ret < 0)
1189 return ret;
1190 }
1191
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001192 if (cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +01001193 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1194 if (ret < 0)
1195 return ret;
1196 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001197
Kuninori Morimoto75ab9eb2017-09-26 00:40:42 +00001198 if (rtd->dai_link->ops->trigger) {
Jarkko Nikula4792b0d2014-04-28 14:17:52 +02001199 ret = rtd->dai_link->ops->trigger(substream, cmd);
1200 if (ret < 0)
1201 return ret;
1202 }
1203
Liam Girdwoodddee6272011-06-09 14:45:53 +01001204 return 0;
1205}
1206
Mark Brown45c0a182012-05-09 21:46:27 +01001207static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1208 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001209{
1210 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001211 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001212 struct snd_soc_dai *codec_dai;
1213 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001214
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001215 for (i = 0; i < rtd->num_codecs; i++) {
1216 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001217 if (codec_dai->driver->ops->bespoke_trigger) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001218 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1219 cmd, codec_dai);
1220 if (ret < 0)
1221 return ret;
1222 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001223 }
1224
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001225 if (cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001226 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1227 if (ret < 0)
1228 return ret;
1229 }
1230 return 0;
1231}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001232/*
1233 * soc level wrapper for pointer callback
1234 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1235 * the runtime->delay will be updated accordingly.
1236 */
1237static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1238{
1239 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1240 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimotob8135862017-10-11 01:37:23 +00001241 struct snd_soc_component *component;
1242 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001243 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001244 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001245 struct snd_pcm_runtime *runtime = substream->runtime;
1246 snd_pcm_uframes_t offset = 0;
1247 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001248 snd_pcm_sframes_t codec_delay = 0;
1249 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001250
Kuninori Morimotob8135862017-10-11 01:37:23 +00001251 if (platform && platform->driver->ops && platform->driver->ops->pointer)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001252 offset = platform->driver->ops->pointer(substream);
1253
Kuninori Morimotob8135862017-10-11 01:37:23 +00001254 for_each_rtdcom(rtd, rtdcom) {
1255 component = rtdcom->component;
1256
1257 /* ignore duplication for now */
1258 if (platform && (component == &platform->component))
1259 continue;
1260
1261 if (!component->driver->ops ||
1262 !component->driver->ops->pointer)
1263 continue;
1264
1265 /* FIXME: use 1st pointer */
1266 offset = component->driver->ops->pointer(substream);
1267 break;
1268 }
1269
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001270 if (cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001271 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1272
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001273 for (i = 0; i < rtd->num_codecs; i++) {
1274 codec_dai = rtd->codec_dais[i];
Kuninori Morimoto9900a422017-09-25 01:38:54 +00001275 if (codec_dai->driver->ops->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001276 codec_delay = max(codec_delay,
1277 codec_dai->driver->ops->delay(substream,
1278 codec_dai));
1279 }
1280 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001281
Liam Girdwoodddee6272011-06-09 14:45:53 +01001282 runtime->delay = delay;
1283
1284 return offset;
1285}
1286
Liam Girdwood01d75842012-04-25 12:12:49 +01001287/* connect a FE and BE */
1288static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1289 struct snd_soc_pcm_runtime *be, int stream)
1290{
1291 struct snd_soc_dpcm *dpcm;
1292
1293 /* only add new dpcms */
1294 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1295 if (dpcm->be == be && dpcm->fe == fe)
1296 return 0;
1297 }
1298
1299 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1300 if (!dpcm)
1301 return -ENOMEM;
1302
1303 dpcm->be = be;
1304 dpcm->fe = fe;
1305 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1306 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1307 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1308 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1309
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001310 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001311 stream ? "capture" : "playback", fe->dai_link->name,
1312 stream ? "<-" : "->", be->dai_link->name);
1313
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001314#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001315 if (fe->debugfs_dpcm_root)
1316 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1317 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001318#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001319 return 1;
1320}
1321
1322/* reparent a BE onto another FE */
1323static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1324 struct snd_soc_pcm_runtime *be, int stream)
1325{
1326 struct snd_soc_dpcm *dpcm;
1327 struct snd_pcm_substream *fe_substream, *be_substream;
1328
1329 /* reparent if BE is connected to other FEs */
1330 if (!be->dpcm[stream].users)
1331 return;
1332
1333 be_substream = snd_soc_dpcm_get_substream(be, stream);
1334
1335 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1336 if (dpcm->fe == fe)
1337 continue;
1338
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001339 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001340 stream ? "capture" : "playback",
1341 dpcm->fe->dai_link->name,
1342 stream ? "<-" : "->", dpcm->be->dai_link->name);
1343
1344 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1345 be_substream->runtime = fe_substream->runtime;
1346 break;
1347 }
1348}
1349
1350/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001351void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001352{
1353 struct snd_soc_dpcm *dpcm, *d;
1354
1355 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001356 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001357 stream ? "capture" : "playback",
1358 dpcm->be->dai_link->name);
1359
1360 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1361 continue;
1362
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001363 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001364 stream ? "capture" : "playback", fe->dai_link->name,
1365 stream ? "<-" : "->", dpcm->be->dai_link->name);
1366
1367 /* BEs still alive need new FE */
1368 dpcm_be_reparent(fe, dpcm->be, stream);
1369
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001370#ifdef CONFIG_DEBUG_FS
1371 debugfs_remove(dpcm->debugfs_state);
1372#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001373 list_del(&dpcm->list_be);
1374 list_del(&dpcm->list_fe);
1375 kfree(dpcm);
1376 }
1377}
1378
1379/* get BE for DAI widget and stream */
1380static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1381 struct snd_soc_dapm_widget *widget, int stream)
1382{
1383 struct snd_soc_pcm_runtime *be;
Mengdong Lin1a497982015-11-18 02:34:11 -05001384 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001385
1386 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mengdong Lin1a497982015-11-18 02:34:11 -05001387 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001388
Liam Girdwood35ea0652012-06-05 19:26:59 +01001389 if (!be->dai_link->no_pcm)
1390 continue;
1391
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001392 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001393 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001394
Mengdong Lin1a497982015-11-18 02:34:11 -05001395 for (i = 0; i < be->num_codecs; i++) {
1396 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001397 if (dai->playback_widget == widget)
1398 return be;
1399 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001400 }
1401 } else {
1402
Mengdong Lin1a497982015-11-18 02:34:11 -05001403 list_for_each_entry(be, &card->rtd_list, list) {
Liam Girdwood01d75842012-04-25 12:12:49 +01001404
Liam Girdwood35ea0652012-06-05 19:26:59 +01001405 if (!be->dai_link->no_pcm)
1406 continue;
1407
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001408 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001409 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001410
Mengdong Lin1a497982015-11-18 02:34:11 -05001411 for (i = 0; i < be->num_codecs; i++) {
1412 struct snd_soc_dai *dai = be->codec_dais[i];
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001413 if (dai->capture_widget == widget)
1414 return be;
1415 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001416 }
1417 }
1418
Liam Girdwood103d84a2012-11-19 14:39:15 +00001419 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001420 stream ? "capture" : "playback", widget->name);
1421 return NULL;
1422}
1423
1424static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001425 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001426{
1427 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001428 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001429 else
Benoit Cousson37018612014-04-24 14:01:45 +02001430 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001431}
1432
1433static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1434 struct snd_soc_dapm_widget *widget)
1435{
1436 int i;
1437
1438 for (i = 0; i < list->num_widgets; i++) {
1439 if (widget == list->widgets[i])
1440 return 1;
1441 }
1442
1443 return 0;
1444}
1445
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001446static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1447 enum snd_soc_dapm_direction dir)
1448{
1449 struct snd_soc_card *card = widget->dapm->card;
1450 struct snd_soc_pcm_runtime *rtd;
1451 int i;
1452
1453 if (dir == SND_SOC_DAPM_DIR_OUT) {
1454 list_for_each_entry(rtd, &card->rtd_list, list) {
1455 if (!rtd->dai_link->no_pcm)
1456 continue;
1457
1458 if (rtd->cpu_dai->playback_widget == widget)
1459 return true;
1460
1461 for (i = 0; i < rtd->num_codecs; ++i) {
1462 struct snd_soc_dai *dai = rtd->codec_dais[i];
1463 if (dai->playback_widget == widget)
1464 return true;
1465 }
1466 }
1467 } else { /* SND_SOC_DAPM_DIR_IN */
1468 list_for_each_entry(rtd, &card->rtd_list, list) {
1469 if (!rtd->dai_link->no_pcm)
1470 continue;
1471
1472 if (rtd->cpu_dai->capture_widget == widget)
1473 return true;
1474
1475 for (i = 0; i < rtd->num_codecs; ++i) {
1476 struct snd_soc_dai *dai = rtd->codec_dais[i];
1477 if (dai->capture_widget == widget)
1478 return true;
1479 }
1480 }
1481 }
1482
1483 return false;
1484}
1485
Liam Girdwood23607022014-01-17 17:03:55 +00001486int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Lars-Peter Clausen1ce43ac2015-07-26 19:04:59 +02001487 int stream, struct snd_soc_dapm_widget_list **list)
Liam Girdwood01d75842012-04-25 12:12:49 +01001488{
1489 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
Liam Girdwood01d75842012-04-25 12:12:49 +01001490 int paths;
1491
Liam Girdwood01d75842012-04-25 12:12:49 +01001492 /* get number of valid DAI paths and their widgets */
Piotr Stankiewicz67420642016-05-13 17:03:55 +01001493 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
Piotr Stankiewicz5fdd0222016-05-13 17:03:56 +01001494 dpcm_end_walk_at_be);
Liam Girdwood01d75842012-04-25 12:12:49 +01001495
Liam Girdwood103d84a2012-11-19 14:39:15 +00001496 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001497 stream ? "capture" : "playback");
1498
Liam Girdwood01d75842012-04-25 12:12:49 +01001499 return paths;
1500}
1501
Liam Girdwood01d75842012-04-25 12:12:49 +01001502static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1503 struct snd_soc_dapm_widget_list **list_)
1504{
1505 struct snd_soc_dpcm *dpcm;
1506 struct snd_soc_dapm_widget_list *list = *list_;
1507 struct snd_soc_dapm_widget *widget;
1508 int prune = 0;
1509
1510 /* Destroy any old FE <--> BE connections */
1511 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001512 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001513
1514 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001515 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001516
1517 /* prune the BE if it's no longer in our active list */
1518 if (widget && widget_in_list(list, widget))
1519 continue;
1520
1521 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001522 for (i = 0; i < dpcm->be->num_codecs; i++) {
1523 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1524 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001525
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001526 /* prune the BE if it's no longer in our active list */
1527 if (widget && widget_in_list(list, widget))
1528 continue;
1529 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001530
Liam Girdwood103d84a2012-11-19 14:39:15 +00001531 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001532 stream ? "capture" : "playback",
1533 dpcm->be->dai_link->name, fe->dai_link->name);
1534 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1535 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1536 prune++;
1537 }
1538
Liam Girdwood103d84a2012-11-19 14:39:15 +00001539 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001540 return prune;
1541}
1542
1543static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1544 struct snd_soc_dapm_widget_list **list_)
1545{
1546 struct snd_soc_card *card = fe->card;
1547 struct snd_soc_dapm_widget_list *list = *list_;
1548 struct snd_soc_pcm_runtime *be;
1549 int i, new = 0, err;
1550
1551 /* Create any new FE <--> BE connections */
1552 for (i = 0; i < list->num_widgets; i++) {
1553
Mark Brown46162742013-06-05 19:36:11 +01001554 switch (list->widgets[i]->id) {
1555 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001556 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1557 continue;
1558 break;
Mark Brown46162742013-06-05 19:36:11 +01001559 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001560 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1561 continue;
Mark Brown46162742013-06-05 19:36:11 +01001562 break;
1563 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001564 continue;
Mark Brown46162742013-06-05 19:36:11 +01001565 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001566
1567 /* is there a valid BE rtd for this widget */
1568 be = dpcm_get_be(card, list->widgets[i], stream);
1569 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001570 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001571 list->widgets[i]->name);
1572 continue;
1573 }
1574
1575 /* make sure BE is a real BE */
1576 if (!be->dai_link->no_pcm)
1577 continue;
1578
1579 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001580 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001581 continue;
1582
1583 /* newly connected FE and BE */
1584 err = dpcm_be_connect(fe, be, stream);
1585 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001586 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001587 list->widgets[i]->name);
1588 break;
1589 } else if (err == 0) /* already connected */
1590 continue;
1591
1592 /* new */
1593 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1594 new++;
1595 }
1596
Liam Girdwood103d84a2012-11-19 14:39:15 +00001597 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001598 return new;
1599}
1600
1601/*
1602 * Find the corresponding BE DAIs that source or sink audio to this
1603 * FE substream.
1604 */
Liam Girdwood23607022014-01-17 17:03:55 +00001605int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001606 int stream, struct snd_soc_dapm_widget_list **list, int new)
1607{
1608 if (new)
1609 return dpcm_add_paths(fe, stream, list);
1610 else
1611 return dpcm_prune_paths(fe, stream, list);
1612}
1613
Liam Girdwood23607022014-01-17 17:03:55 +00001614void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001615{
1616 struct snd_soc_dpcm *dpcm;
1617
1618 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1619 dpcm->be->dpcm[stream].runtime_update =
1620 SND_SOC_DPCM_UPDATE_NO;
1621}
1622
1623static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1624 int stream)
1625{
1626 struct snd_soc_dpcm *dpcm;
1627
1628 /* disable any enabled and non active backends */
1629 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1630
1631 struct snd_soc_pcm_runtime *be = dpcm->be;
1632 struct snd_pcm_substream *be_substream =
1633 snd_soc_dpcm_get_substream(be, stream);
1634
1635 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001636 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001637 stream ? "capture" : "playback",
1638 be->dpcm[stream].state);
1639
1640 if (--be->dpcm[stream].users != 0)
1641 continue;
1642
1643 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1644 continue;
1645
1646 soc_pcm_close(be_substream);
1647 be_substream->runtime = NULL;
1648 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1649 }
1650}
1651
Liam Girdwood23607022014-01-17 17:03:55 +00001652int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001653{
1654 struct snd_soc_dpcm *dpcm;
1655 int err, count = 0;
1656
1657 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1658 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1659
1660 struct snd_soc_pcm_runtime *be = dpcm->be;
1661 struct snd_pcm_substream *be_substream =
1662 snd_soc_dpcm_get_substream(be, stream);
1663
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001664 if (!be_substream) {
1665 dev_err(be->dev, "ASoC: no backend %s stream\n",
1666 stream ? "capture" : "playback");
1667 continue;
1668 }
1669
Liam Girdwood01d75842012-04-25 12:12:49 +01001670 /* is this op for this BE ? */
1671 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1672 continue;
1673
1674 /* first time the dpcm is open ? */
1675 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001676 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001677 stream ? "capture" : "playback",
1678 be->dpcm[stream].state);
1679
1680 if (be->dpcm[stream].users++ != 0)
1681 continue;
1682
1683 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1684 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1685 continue;
1686
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001687 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1688 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001689
1690 be_substream->runtime = be->dpcm[stream].runtime;
1691 err = soc_pcm_open(be_substream);
1692 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001693 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001694 be->dpcm[stream].users--;
1695 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001696 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001697 stream ? "capture" : "playback",
1698 be->dpcm[stream].state);
1699
1700 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1701 goto unwind;
1702 }
1703
1704 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1705 count++;
1706 }
1707
1708 return count;
1709
1710unwind:
1711 /* disable any enabled and non active backends */
1712 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1713 struct snd_soc_pcm_runtime *be = dpcm->be;
1714 struct snd_pcm_substream *be_substream =
1715 snd_soc_dpcm_get_substream(be, stream);
1716
1717 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1718 continue;
1719
1720 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001721 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001722 stream ? "capture" : "playback",
1723 be->dpcm[stream].state);
1724
1725 if (--be->dpcm[stream].users != 0)
1726 continue;
1727
1728 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1729 continue;
1730
1731 soc_pcm_close(be_substream);
1732 be_substream->runtime = NULL;
1733 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1734 }
1735
1736 return err;
1737}
1738
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001739static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001740 struct snd_soc_pcm_stream *stream,
1741 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001742{
1743 runtime->hw.rate_min = stream->rate_min;
1744 runtime->hw.rate_max = stream->rate_max;
1745 runtime->hw.channels_min = stream->channels_min;
1746 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001747 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001748 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001749 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001750 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001751 runtime->hw.rates = stream->rates;
1752}
1753
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001754static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1755{
1756 struct snd_soc_pcm_runtime *fe = substream->private_data;
1757 struct snd_soc_dpcm *dpcm;
1758 u64 formats = ULLONG_MAX;
1759 int stream = substream->stream;
1760
1761 if (!fe->dai_link->dpcm_merged_format)
1762 return formats;
1763
1764 /*
1765 * It returns merged BE codec format
1766 * if FE want to use it (= dpcm_merged_format)
1767 */
1768
1769 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1770 struct snd_soc_pcm_runtime *be = dpcm->be;
1771 struct snd_soc_dai_driver *codec_dai_drv;
1772 struct snd_soc_pcm_stream *codec_stream;
1773 int i;
1774
1775 for (i = 0; i < be->num_codecs; i++) {
1776 codec_dai_drv = be->codec_dais[i]->driver;
1777 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1778 codec_stream = &codec_dai_drv->playback;
1779 else
1780 codec_stream = &codec_dai_drv->capture;
1781
1782 formats &= codec_stream->formats;
1783 }
1784 }
1785
1786 return formats;
1787}
1788
Mark Brown45c0a182012-05-09 21:46:27 +01001789static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001790{
1791 struct snd_pcm_runtime *runtime = substream->runtime;
1792 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1793 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1794 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001795 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001796
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001797 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001798 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001799 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001800 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001801}
1802
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001803static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1804
1805/* Set FE's runtime_update state; the state is protected via PCM stream lock
1806 * for avoiding the race with trigger callback.
1807 * If the state is unset and a trigger is pending while the previous operation,
1808 * process the pending trigger action here.
1809 */
1810static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1811 int stream, enum snd_soc_dpcm_update state)
1812{
1813 struct snd_pcm_substream *substream =
1814 snd_soc_dpcm_get_substream(fe, stream);
1815
1816 snd_pcm_stream_lock_irq(substream);
1817 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1818 dpcm_fe_dai_do_trigger(substream,
1819 fe->dpcm[stream].trigger_pending - 1);
1820 fe->dpcm[stream].trigger_pending = 0;
1821 }
1822 fe->dpcm[stream].runtime_update = state;
1823 snd_pcm_stream_unlock_irq(substream);
1824}
1825
PC Liao906c7d62015-12-11 11:33:51 +08001826static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1827 int stream)
1828{
1829 struct snd_soc_dpcm *dpcm;
1830 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1831 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1832 int err;
1833
1834 /* apply symmetry for FE */
1835 if (soc_pcm_has_symmetry(fe_substream))
1836 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1837
1838 /* Symmetry only applies if we've got an active stream. */
1839 if (fe_cpu_dai->active) {
1840 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1841 if (err < 0)
1842 return err;
1843 }
1844
1845 /* apply symmetry for BE */
1846 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1847 struct snd_soc_pcm_runtime *be = dpcm->be;
1848 struct snd_pcm_substream *be_substream =
1849 snd_soc_dpcm_get_substream(be, stream);
1850 struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
1851 int i;
1852
Jeeja KPf1176612016-09-06 14:17:55 +05301853 if (rtd->dai_link->be_hw_params_fixup)
1854 continue;
1855
PC Liao906c7d62015-12-11 11:33:51 +08001856 if (soc_pcm_has_symmetry(be_substream))
1857 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1858
1859 /* Symmetry only applies if we've got an active stream. */
1860 if (rtd->cpu_dai->active) {
1861 err = soc_pcm_apply_symmetry(be_substream, rtd->cpu_dai);
1862 if (err < 0)
1863 return err;
1864 }
1865
1866 for (i = 0; i < rtd->num_codecs; i++) {
1867 if (rtd->codec_dais[i]->active) {
1868 err = soc_pcm_apply_symmetry(be_substream,
1869 rtd->codec_dais[i]);
1870 if (err < 0)
1871 return err;
1872 }
1873 }
1874 }
1875
1876 return 0;
1877}
1878
Liam Girdwood01d75842012-04-25 12:12:49 +01001879static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1880{
1881 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1882 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1883 int stream = fe_substream->stream, ret = 0;
1884
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001885 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001886
1887 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1888 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001889 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001890 goto be_err;
1891 }
1892
Liam Girdwood103d84a2012-11-19 14:39:15 +00001893 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001894
1895 /* start the DAI frontend */
1896 ret = soc_pcm_open(fe_substream);
1897 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001898 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001899 goto unwind;
1900 }
1901
1902 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1903
1904 dpcm_set_fe_runtime(fe_substream);
1905 snd_pcm_limit_hw_rates(runtime);
1906
PC Liao906c7d62015-12-11 11:33:51 +08001907 ret = dpcm_apply_symmetry(fe_substream, stream);
1908 if (ret < 0) {
1909 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1910 ret);
1911 goto unwind;
1912 }
1913
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001914 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001915 return 0;
1916
1917unwind:
1918 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1919be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001920 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001921 return ret;
1922}
1923
Liam Girdwood23607022014-01-17 17:03:55 +00001924int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001925{
1926 struct snd_soc_dpcm *dpcm;
1927
1928 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1929 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1930
1931 struct snd_soc_pcm_runtime *be = dpcm->be;
1932 struct snd_pcm_substream *be_substream =
1933 snd_soc_dpcm_get_substream(be, stream);
1934
1935 /* is this op for this BE ? */
1936 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1937 continue;
1938
1939 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001940 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001941 stream ? "capture" : "playback",
1942 be->dpcm[stream].state);
1943
1944 if (--be->dpcm[stream].users != 0)
1945 continue;
1946
1947 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1948 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1949 continue;
1950
Liam Girdwood103d84a2012-11-19 14:39:15 +00001951 dev_dbg(be->dev, "ASoC: close BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00001952 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001953
1954 soc_pcm_close(be_substream);
1955 be_substream->runtime = NULL;
1956
1957 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1958 }
1959 return 0;
1960}
1961
1962static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1963{
1964 struct snd_soc_pcm_runtime *fe = substream->private_data;
1965 int stream = substream->stream;
1966
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001967 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001968
1969 /* shutdown the BEs */
1970 dpcm_be_dai_shutdown(fe, substream->stream);
1971
Liam Girdwood103d84a2012-11-19 14:39:15 +00001972 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001973
1974 /* now shutdown the frontend */
1975 soc_pcm_close(substream);
1976
1977 /* run the stream event for each BE */
1978 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1979
1980 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001981 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001982 return 0;
1983}
1984
Liam Girdwood23607022014-01-17 17:03:55 +00001985int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001986{
1987 struct snd_soc_dpcm *dpcm;
1988
1989 /* only hw_params backends that are either sinks or sources
1990 * to this frontend DAI */
1991 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1992
1993 struct snd_soc_pcm_runtime *be = dpcm->be;
1994 struct snd_pcm_substream *be_substream =
1995 snd_soc_dpcm_get_substream(be, stream);
1996
1997 /* is this op for this BE ? */
1998 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1999 continue;
2000
2001 /* only free hw when no longer used - check all FEs */
2002 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2003 continue;
2004
Qiao Zhou36fba622014-12-03 10:13:43 +08002005 /* do not free hw if this BE is used by other FE */
2006 if (be->dpcm[stream].users > 1)
2007 continue;
2008
Liam Girdwood01d75842012-04-25 12:12:49 +01002009 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2010 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2011 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08002012 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Vinod Koul5e82d2b2016-02-01 22:26:40 +05302013 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2014 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002015 continue;
2016
Liam Girdwood103d84a2012-11-19 14:39:15 +00002017 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002018 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002019
2020 soc_pcm_hw_free(be_substream);
2021
2022 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2023 }
2024
2025 return 0;
2026}
2027
Mark Brown45c0a182012-05-09 21:46:27 +01002028static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002029{
2030 struct snd_soc_pcm_runtime *fe = substream->private_data;
2031 int err, stream = substream->stream;
2032
2033 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002034 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002035
Liam Girdwood103d84a2012-11-19 14:39:15 +00002036 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002037
2038 /* call hw_free on the frontend */
2039 err = soc_pcm_hw_free(substream);
2040 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002041 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002042 fe->dai_link->name);
2043
2044 /* only hw_params backends that are either sinks or sources
2045 * to this frontend DAI */
2046 err = dpcm_be_dai_hw_free(fe, stream);
2047
2048 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002049 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002050
2051 mutex_unlock(&fe->card->mutex);
2052 return 0;
2053}
2054
Liam Girdwood23607022014-01-17 17:03:55 +00002055int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002056{
2057 struct snd_soc_dpcm *dpcm;
2058 int ret;
2059
2060 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2061
2062 struct snd_soc_pcm_runtime *be = dpcm->be;
2063 struct snd_pcm_substream *be_substream =
2064 snd_soc_dpcm_get_substream(be, stream);
2065
2066 /* is this op for this BE ? */
2067 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2068 continue;
2069
Liam Girdwood01d75842012-04-25 12:12:49 +01002070 /* copy params for each dpcm */
2071 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2072 sizeof(struct snd_pcm_hw_params));
2073
2074 /* perform any hw_params fixups */
2075 if (be->dai_link->be_hw_params_fixup) {
2076 ret = be->dai_link->be_hw_params_fixup(be,
2077 &dpcm->hw_params);
2078 if (ret < 0) {
2079 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002080 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002081 ret);
2082 goto unwind;
2083 }
2084 }
2085
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002086 /* only allow hw_params() if no connected FEs are running */
2087 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2088 continue;
2089
2090 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2091 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2092 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2093 continue;
2094
2095 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002096 be->dai_link->name);
Kuninori Morimotob0639bd2016-01-21 01:47:12 +00002097
Liam Girdwood01d75842012-04-25 12:12:49 +01002098 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2099 if (ret < 0) {
2100 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00002101 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002102 goto unwind;
2103 }
2104
2105 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2106 }
2107 return 0;
2108
2109unwind:
2110 /* disable any enabled and non active backends */
2111 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2112 struct snd_soc_pcm_runtime *be = dpcm->be;
2113 struct snd_pcm_substream *be_substream =
2114 snd_soc_dpcm_get_substream(be, stream);
2115
2116 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2117 continue;
2118
2119 /* only allow hw_free() if no connected FEs are running */
2120 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2121 continue;
2122
2123 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2124 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2125 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2126 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2127 continue;
2128
2129 soc_pcm_hw_free(be_substream);
2130 }
2131
2132 return ret;
2133}
2134
Mark Brown45c0a182012-05-09 21:46:27 +01002135static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2136 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01002137{
2138 struct snd_soc_pcm_runtime *fe = substream->private_data;
2139 int ret, stream = substream->stream;
2140
2141 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002142 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002143
2144 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2145 sizeof(struct snd_pcm_hw_params));
2146 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2147 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002148 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002149 goto out;
2150 }
2151
Liam Girdwood103d84a2012-11-19 14:39:15 +00002152 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002153 fe->dai_link->name, params_rate(params),
2154 params_channels(params), params_format(params));
2155
2156 /* call hw_params on the frontend */
2157 ret = soc_pcm_hw_params(substream, params);
2158 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002159 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002160 dpcm_be_dai_hw_free(fe, stream);
2161 } else
2162 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2163
2164out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002165 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002166 mutex_unlock(&fe->card->mutex);
2167 return ret;
2168}
2169
2170static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2171 struct snd_pcm_substream *substream, int cmd)
2172{
2173 int ret;
2174
Liam Girdwood103d84a2012-11-19 14:39:15 +00002175 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
彭东林94d215c2016-09-26 08:29:31 +00002176 dpcm->be->dai_link->name, cmd);
Liam Girdwood01d75842012-04-25 12:12:49 +01002177
2178 ret = soc_pcm_trigger(substream, cmd);
2179 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002180 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002181
2182 return ret;
2183}
2184
Liam Girdwood23607022014-01-17 17:03:55 +00002185int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01002186 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002187{
2188 struct snd_soc_dpcm *dpcm;
2189 int ret = 0;
2190
2191 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2192
2193 struct snd_soc_pcm_runtime *be = dpcm->be;
2194 struct snd_pcm_substream *be_substream =
2195 snd_soc_dpcm_get_substream(be, stream);
2196
2197 /* is this op for this BE ? */
2198 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2199 continue;
2200
2201 switch (cmd) {
2202 case SNDRV_PCM_TRIGGER_START:
2203 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2204 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2205 continue;
2206
2207 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2208 if (ret)
2209 return ret;
2210
2211 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2212 break;
2213 case SNDRV_PCM_TRIGGER_RESUME:
2214 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2215 continue;
2216
2217 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2218 if (ret)
2219 return ret;
2220
2221 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2222 break;
2223 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2224 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2225 continue;
2226
2227 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2228 if (ret)
2229 return ret;
2230
2231 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2232 break;
2233 case SNDRV_PCM_TRIGGER_STOP:
2234 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2235 continue;
2236
2237 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2238 continue;
2239
2240 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2241 if (ret)
2242 return ret;
2243
2244 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2245 break;
2246 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08002247 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01002248 continue;
2249
2250 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2251 continue;
2252
2253 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2254 if (ret)
2255 return ret;
2256
2257 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2258 break;
2259 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2260 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2261 continue;
2262
2263 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2264 continue;
2265
2266 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2267 if (ret)
2268 return ret;
2269
2270 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2271 break;
2272 }
2273 }
2274
2275 return ret;
2276}
2277EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2278
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002279static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01002280{
2281 struct snd_soc_pcm_runtime *fe = substream->private_data;
2282 int stream = substream->stream, ret;
2283 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2284
2285 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2286
2287 switch (trigger) {
2288 case SND_SOC_DPCM_TRIGGER_PRE:
2289 /* call trigger on the frontend before the backend. */
2290
Liam Girdwood103d84a2012-11-19 14:39:15 +00002291 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002292 fe->dai_link->name, cmd);
2293
2294 ret = soc_pcm_trigger(substream, cmd);
2295 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002296 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002297 goto out;
2298 }
2299
2300 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2301 break;
2302 case SND_SOC_DPCM_TRIGGER_POST:
2303 /* call trigger on the frontend after the backend. */
2304
2305 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2306 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002307 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002308 goto out;
2309 }
2310
Liam Girdwood103d84a2012-11-19 14:39:15 +00002311 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002312 fe->dai_link->name, cmd);
2313
2314 ret = soc_pcm_trigger(substream, cmd);
2315 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002316 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2317 /* bespoke trigger() - handles both FE and BEs */
2318
Liam Girdwood103d84a2012-11-19 14:39:15 +00002319 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002320 fe->dai_link->name, cmd);
2321
2322 ret = soc_pcm_bespoke_trigger(substream, cmd);
2323 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002324 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002325 goto out;
2326 }
2327 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002328 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002329 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002330 fe->dai_link->name);
2331 ret = -EINVAL;
2332 goto out;
2333 }
2334
2335 switch (cmd) {
2336 case SNDRV_PCM_TRIGGER_START:
2337 case SNDRV_PCM_TRIGGER_RESUME:
2338 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2339 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2340 break;
2341 case SNDRV_PCM_TRIGGER_STOP:
2342 case SNDRV_PCM_TRIGGER_SUSPEND:
Liam Girdwood01d75842012-04-25 12:12:49 +01002343 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2344 break;
Patrick Lai9f169b92016-12-31 22:44:39 -08002345 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2346 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2347 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002348 }
2349
2350out:
2351 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2352 return ret;
2353}
2354
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002355static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2356{
2357 struct snd_soc_pcm_runtime *fe = substream->private_data;
2358 int stream = substream->stream;
2359
2360 /* if FE's runtime_update is already set, we're in race;
2361 * process this trigger later at exit
2362 */
2363 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2364 fe->dpcm[stream].trigger_pending = cmd + 1;
2365 return 0; /* delayed, assuming it's successful */
2366 }
2367
2368 /* we're alone, let's trigger */
2369 return dpcm_fe_dai_do_trigger(substream, cmd);
2370}
2371
Liam Girdwood23607022014-01-17 17:03:55 +00002372int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002373{
2374 struct snd_soc_dpcm *dpcm;
2375 int ret = 0;
2376
2377 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2378
2379 struct snd_soc_pcm_runtime *be = dpcm->be;
2380 struct snd_pcm_substream *be_substream =
2381 snd_soc_dpcm_get_substream(be, stream);
2382
2383 /* is this op for this BE ? */
2384 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2385 continue;
2386
2387 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
Koro Chen95f444d2015-10-28 10:15:34 +08002388 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2389 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
Liam Girdwood01d75842012-04-25 12:12:49 +01002390 continue;
2391
Liam Girdwood103d84a2012-11-19 14:39:15 +00002392 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
彭东林94d215c2016-09-26 08:29:31 +00002393 be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002394
2395 ret = soc_pcm_prepare(be_substream);
2396 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002397 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002398 ret);
2399 break;
2400 }
2401
2402 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2403 }
2404 return ret;
2405}
2406
Mark Brown45c0a182012-05-09 21:46:27 +01002407static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002408{
2409 struct snd_soc_pcm_runtime *fe = substream->private_data;
2410 int stream = substream->stream, ret = 0;
2411
2412 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2413
Liam Girdwood103d84a2012-11-19 14:39:15 +00002414 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002415
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002416 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002417
2418 /* there is no point preparing this FE if there are no BEs */
2419 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002420 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002421 fe->dai_link->name);
2422 ret = -EINVAL;
2423 goto out;
2424 }
2425
2426 ret = dpcm_be_dai_prepare(fe, substream->stream);
2427 if (ret < 0)
2428 goto out;
2429
2430 /* call prepare on the frontend */
2431 ret = soc_pcm_prepare(substream);
2432 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002433 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002434 fe->dai_link->name);
2435 goto out;
2436 }
2437
2438 /* run the stream event for each BE */
2439 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2440 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2441
2442out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002443 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002444 mutex_unlock(&fe->card->mutex);
2445
2446 return ret;
2447}
2448
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002449static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2450 unsigned int cmd, void *arg)
2451{
2452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2453 struct snd_soc_platform *platform = rtd->platform;
Kuninori Morimotob8135862017-10-11 01:37:23 +00002454 struct snd_soc_component *component;
2455 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002456
Kuninori Morimotob8135862017-10-11 01:37:23 +00002457 if (platform && platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002458 return platform->driver->ops->ioctl(substream, cmd, arg);
Kuninori Morimotob8135862017-10-11 01:37:23 +00002459
2460 for_each_rtdcom(rtd, rtdcom) {
2461 component = rtdcom->component;
2462
2463 /* ignore duplication for now */
2464 if (platform && (component == &platform->component))
2465 continue;
2466
2467 if (!component->driver->ops ||
2468 !component->driver->ops->ioctl)
2469 continue;
2470
2471 /* FIXME: use 1st ioctl */
2472 return component->driver->ops->ioctl(substream, cmd, arg);
2473 }
2474
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002475 return snd_pcm_lib_ioctl(substream, cmd, arg);
2476}
2477
Liam Girdwood618dae12012-04-25 12:12:51 +01002478static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2479{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002480 struct snd_pcm_substream *substream =
2481 snd_soc_dpcm_get_substream(fe, stream);
2482 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002483 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002484
Liam Girdwood103d84a2012-11-19 14:39:15 +00002485 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002486 stream ? "capture" : "playback", fe->dai_link->name);
2487
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002488 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2489 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002490 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002491 fe->dai_link->name);
2492
2493 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2494 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002495 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002496 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002497 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002498 fe->dai_link->name);
2499
2500 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2501 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002502 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002503 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002504
2505 err = dpcm_be_dai_hw_free(fe, stream);
2506 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002507 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002508
2509 err = dpcm_be_dai_shutdown(fe, stream);
2510 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002511 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002512
2513 /* run the stream event for each BE */
2514 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2515
2516 return 0;
2517}
2518
2519static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2520{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002521 struct snd_pcm_substream *substream =
2522 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002523 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002524 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002525 int ret;
2526
Liam Girdwood103d84a2012-11-19 14:39:15 +00002527 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002528 stream ? "capture" : "playback", fe->dai_link->name);
2529
2530 /* Only start the BE if the FE is ready */
2531 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2532 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2533 return -EINVAL;
2534
2535 /* startup must always be called for new BEs */
2536 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002537 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002538 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002539
2540 /* keep going if FE state is > open */
2541 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2542 return 0;
2543
2544 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002545 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002546 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002547
2548 /* keep going if FE state is > hw_params */
2549 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2550 return 0;
2551
2552
2553 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002554 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002555 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002556
2557 /* run the stream event for each BE */
2558 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2559
2560 /* keep going if FE state is > prepare */
2561 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2562 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2563 return 0;
2564
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002565 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2566 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002567 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002568 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002569
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002570 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2571 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002572 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002573 goto hw_free;
2574 }
2575 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002576 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002577 fe->dai_link->name);
2578
2579 ret = dpcm_be_dai_trigger(fe, stream,
2580 SNDRV_PCM_TRIGGER_START);
2581 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002582 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002583 goto hw_free;
2584 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002585 }
2586
2587 return 0;
2588
2589hw_free:
2590 dpcm_be_dai_hw_free(fe, stream);
2591close:
2592 dpcm_be_dai_shutdown(fe, stream);
2593disconnect:
2594 /* disconnect any non started BEs */
2595 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2596 struct snd_soc_pcm_runtime *be = dpcm->be;
2597 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2598 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2599 }
2600
2601 return ret;
2602}
2603
2604static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2605{
2606 int ret;
2607
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002608 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002609 ret = dpcm_run_update_startup(fe, stream);
2610 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002611 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002612 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002613
2614 return ret;
2615}
2616
2617static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2618{
2619 int ret;
2620
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002621 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002622 ret = dpcm_run_update_shutdown(fe, stream);
2623 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002624 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002625 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002626
2627 return ret;
2628}
2629
2630/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2631 * any DAI links.
2632 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002633int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002634{
Mengdong Lin1a497982015-11-18 02:34:11 -05002635 struct snd_soc_pcm_runtime *fe;
2636 int old, new, paths;
Liam Girdwood618dae12012-04-25 12:12:51 +01002637
Liam Girdwood618dae12012-04-25 12:12:51 +01002638 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Mengdong Lin1a497982015-11-18 02:34:11 -05002639 list_for_each_entry(fe, &card->rtd_list, list) {
Liam Girdwood618dae12012-04-25 12:12:51 +01002640 struct snd_soc_dapm_widget_list *list;
Liam Girdwood618dae12012-04-25 12:12:51 +01002641
2642 /* make sure link is FE */
2643 if (!fe->dai_link->dynamic)
2644 continue;
2645
2646 /* only check active links */
2647 if (!fe->cpu_dai->active)
2648 continue;
2649
2650 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002651 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002652 fe->dai_link->name);
2653
2654 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002655 if (!fe->cpu_dai->driver->playback.channels_min
2656 || !fe->codec_dai->driver->playback.channels_min)
2657 goto capture;
2658
2659 /* skip if FE isn't currently playing */
2660 if (!fe->cpu_dai->playback_active
2661 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002662 goto capture;
2663
2664 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2665 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002666 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002667 fe->dai_link->name, "playback");
2668 mutex_unlock(&card->mutex);
2669 return paths;
2670 }
2671
2672 /* update any new playback paths */
2673 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2674 if (new) {
2675 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2676 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2677 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2678 }
2679
2680 /* update any old playback paths */
2681 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2682 if (old) {
2683 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2684 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2685 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2686 }
2687
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002688 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002689capture:
2690 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002691 if (!fe->cpu_dai->driver->capture.channels_min
2692 || !fe->codec_dai->driver->capture.channels_min)
2693 continue;
2694
2695 /* skip if FE isn't currently capturing */
2696 if (!fe->cpu_dai->capture_active
2697 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002698 continue;
2699
2700 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2701 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002702 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002703 fe->dai_link->name, "capture");
2704 mutex_unlock(&card->mutex);
2705 return paths;
2706 }
2707
2708 /* update any new capture paths */
2709 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2710 if (new) {
2711 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2712 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2713 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2714 }
2715
2716 /* update any old capture paths */
2717 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2718 if (old) {
2719 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2720 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2721 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2722 }
2723
2724 dpcm_path_put(&list);
2725 }
2726
2727 mutex_unlock(&card->mutex);
2728 return 0;
2729}
Liam Girdwood01d75842012-04-25 12:12:49 +01002730int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2731{
2732 struct snd_soc_dpcm *dpcm;
2733 struct list_head *clients =
2734 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2735
2736 list_for_each_entry(dpcm, clients, list_be) {
2737
2738 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002739 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002740
2741 if (be->dai_link->ignore_suspend)
2742 continue;
2743
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002744 for (i = 0; i < be->num_codecs; i++) {
2745 struct snd_soc_dai *dai = be->codec_dais[i];
2746 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002747
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002748 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2749 be->dai_link->name);
2750
2751 if (drv->ops && drv->ops->digital_mute &&
2752 dai->playback_active)
2753 drv->ops->digital_mute(dai, mute);
2754 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002755 }
2756
2757 return 0;
2758}
2759
Mark Brown45c0a182012-05-09 21:46:27 +01002760static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002761{
2762 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2763 struct snd_soc_dpcm *dpcm;
2764 struct snd_soc_dapm_widget_list *list;
2765 int ret;
2766 int stream = fe_substream->stream;
2767
2768 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2769 fe->dpcm[stream].runtime = fe_substream->runtime;
2770
Qiao Zhou8f70e512014-09-10 17:54:07 +08002771 ret = dpcm_path_get(fe, stream, &list);
2772 if (ret < 0) {
2773 mutex_unlock(&fe->card->mutex);
2774 return ret;
2775 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002776 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002777 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002778 }
2779
2780 /* calculate valid and active FE <-> BE dpcms */
2781 dpcm_process_paths(fe, stream, &list, 1);
2782
2783 ret = dpcm_fe_dai_startup(fe_substream);
2784 if (ret < 0) {
2785 /* clean up all links */
2786 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2787 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2788
2789 dpcm_be_disconnect(fe, stream);
2790 fe->dpcm[stream].runtime = NULL;
2791 }
2792
2793 dpcm_clear_pending_state(fe, stream);
2794 dpcm_path_put(&list);
2795 mutex_unlock(&fe->card->mutex);
2796 return ret;
2797}
2798
Mark Brown45c0a182012-05-09 21:46:27 +01002799static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002800{
2801 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2802 struct snd_soc_dpcm *dpcm;
2803 int stream = fe_substream->stream, ret;
2804
2805 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2806 ret = dpcm_fe_dai_shutdown(fe_substream);
2807
2808 /* mark FE's links ready to prune */
2809 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2810 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2811
2812 dpcm_be_disconnect(fe, stream);
2813
2814 fe->dpcm[stream].runtime = NULL;
2815 mutex_unlock(&fe->card->mutex);
2816 return ret;
2817}
2818
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002819static void soc_pcm_private_free(struct snd_pcm *pcm)
2820{
2821 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002822 struct snd_soc_rtdcom_list *rtdcom;
2823 struct snd_soc_component *component;
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002824
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002825 for_each_rtdcom(rtd, rtdcom) {
2826 /* need to sync the delayed work before releasing resources */
2827
2828 flush_delayed_work(&rtd->delayed_work);
2829 component = rtdcom->component;
2830
2831 if (component->pcm_free)
2832 component->pcm_free(component, pcm);
2833 }
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02002834}
2835
Kuninori Morimotob8135862017-10-11 01:37:23 +00002836static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2837{
2838 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2839 struct snd_soc_rtdcom_list *rtdcom;
2840 struct snd_soc_component *component;
2841
2842 for_each_rtdcom(rtd, rtdcom) {
2843 component = rtdcom->component;
2844
2845 if (!component->driver->ops ||
2846 !component->driver->ops->ack)
2847 continue;
2848
2849 /* FIXME. it returns 1st ask now */
2850 return component->driver->ops->ack(substream);
2851 }
2852
2853 return -EINVAL;
2854}
2855
2856static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2857 unsigned long pos, void __user *buf,
2858 unsigned long bytes)
2859{
2860 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2861 struct snd_soc_rtdcom_list *rtdcom;
2862 struct snd_soc_component *component;
2863
2864 for_each_rtdcom(rtd, rtdcom) {
2865 component = rtdcom->component;
2866
2867 if (!component->driver->ops ||
2868 !component->driver->ops->copy_user)
2869 continue;
2870
2871 /* FIXME. it returns 1st copy now */
2872 return component->driver->ops->copy_user(substream, channel,
2873 pos, buf, bytes);
2874 }
2875
2876 return -EINVAL;
2877}
2878
2879static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2880 unsigned long pos, void *buf, unsigned long bytes)
2881{
2882 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2883 struct snd_soc_rtdcom_list *rtdcom;
2884 struct snd_soc_component *component;
2885
2886 for_each_rtdcom(rtd, rtdcom) {
2887 component = rtdcom->component;
2888
2889 if (!component->driver->ops ||
2890 !component->driver->ops->copy_kernel)
2891 continue;
2892
2893 /* FIXME. it returns 1st copy now */
2894 return component->driver->ops->copy_kernel(substream, channel,
2895 pos, buf, bytes);
2896 }
2897
2898 return -EINVAL;
2899}
2900
2901static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
2902 unsigned long pos, unsigned long bytes)
2903{
2904 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2905 struct snd_soc_rtdcom_list *rtdcom;
2906 struct snd_soc_component *component;
2907
2908 for_each_rtdcom(rtd, rtdcom) {
2909 component = rtdcom->component;
2910
2911 if (!component->driver->ops ||
2912 !component->driver->ops->fill_silence)
2913 continue;
2914
2915 /* FIXME. it returns 1st silence now */
2916 return component->driver->ops->fill_silence(substream, channel,
2917 pos, bytes);
2918 }
2919
2920 return -EINVAL;
2921}
2922
2923static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
2924 unsigned long offset)
2925{
2926 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2927 struct snd_soc_rtdcom_list *rtdcom;
2928 struct snd_soc_component *component;
2929 struct page *page;
2930
2931 for_each_rtdcom(rtd, rtdcom) {
2932 component = rtdcom->component;
2933
2934 if (!component->driver->ops ||
2935 !component->driver->ops->page)
2936 continue;
2937
2938 /* FIXME. it returns 1st page now */
2939 page = component->driver->ops->page(substream, offset);
2940 if (page)
2941 return page;
2942 }
2943
2944 return NULL;
2945}
2946
2947static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
2948 struct vm_area_struct *vma)
2949{
2950 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2951 struct snd_soc_rtdcom_list *rtdcom;
2952 struct snd_soc_component *component;
2953
2954 for_each_rtdcom(rtd, rtdcom) {
2955 component = rtdcom->component;
2956
2957 if (!component->driver->ops ||
2958 !component->driver->ops->mmap)
2959 continue;
2960
2961 /* FIXME. it returns 1st mmap now */
2962 return component->driver->ops->mmap(substream, vma);
2963 }
2964
2965 return -EINVAL;
2966}
2967
Liam Girdwoodddee6272011-06-09 14:45:53 +01002968/* create a new pcm */
2969int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2970{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002971 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002972 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002973 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Kuninori Morimotof523ace2017-09-26 01:00:53 +00002974 struct snd_soc_component *component;
2975 struct snd_soc_rtdcom_list *rtdcom;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002976 struct snd_pcm *pcm;
2977 char new_name[64];
2978 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002979 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002980
Liam Girdwood01d75842012-04-25 12:12:49 +01002981 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002982 playback = rtd->dai_link->dpcm_playback;
2983 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002984 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002985 for (i = 0; i < rtd->num_codecs; i++) {
2986 codec_dai = rtd->codec_dais[i];
2987 if (codec_dai->driver->playback.channels_min)
2988 playback = 1;
2989 if (codec_dai->driver->capture.channels_min)
2990 capture = 1;
2991 }
2992
2993 capture = capture && cpu_dai->driver->capture.channels_min;
2994 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002995 }
Sangsu Parka5002312012-01-02 17:15:10 +09002996
Fabio Estevamd6bead02013-08-29 10:32:13 -03002997 if (rtd->dai_link->playback_only) {
2998 playback = 1;
2999 capture = 0;
3000 }
3001
3002 if (rtd->dai_link->capture_only) {
3003 playback = 0;
3004 capture = 1;
3005 }
3006
Liam Girdwood01d75842012-04-25 12:12:49 +01003007 /* create the PCM */
3008 if (rtd->dai_link->no_pcm) {
3009 snprintf(new_name, sizeof(new_name), "(%s)",
3010 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003011
Liam Girdwood01d75842012-04-25 12:12:49 +01003012 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3013 playback, capture, &pcm);
3014 } else {
3015 if (rtd->dai_link->dynamic)
3016 snprintf(new_name, sizeof(new_name), "%s (*)",
3017 rtd->dai_link->stream_name);
3018 else
3019 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003020 rtd->dai_link->stream_name,
3021 (rtd->num_codecs > 1) ?
3022 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003023
Liam Girdwood01d75842012-04-25 12:12:49 +01003024 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3025 capture, &pcm);
3026 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01003027 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00003028 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01003029 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003030 return ret;
3031 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00003032 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003033
3034 /* DAPM dai link stream work */
3035 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3036
Vinod Koul48c76992015-02-12 09:59:53 +05303037 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003038 rtd->pcm = pcm;
3039 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01003040
3041 if (rtd->dai_link->no_pcm) {
3042 if (playback)
3043 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3044 if (capture)
3045 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3046 goto out;
3047 }
3048
3049 /* ASoC PCM operations */
3050 if (rtd->dai_link->dynamic) {
3051 rtd->ops.open = dpcm_fe_dai_open;
3052 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3053 rtd->ops.prepare = dpcm_fe_dai_prepare;
3054 rtd->ops.trigger = dpcm_fe_dai_trigger;
3055 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3056 rtd->ops.close = dpcm_fe_dai_close;
3057 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003058 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003059 } else {
3060 rtd->ops.open = soc_pcm_open;
3061 rtd->ops.hw_params = soc_pcm_hw_params;
3062 rtd->ops.prepare = soc_pcm_prepare;
3063 rtd->ops.trigger = soc_pcm_trigger;
3064 rtd->ops.hw_free = soc_pcm_hw_free;
3065 rtd->ops.close = soc_pcm_close;
3066 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01003067 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01003068 }
3069
Kuninori Morimotob8135862017-10-11 01:37:23 +00003070 for_each_rtdcom(rtd, rtdcom) {
3071 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3072
3073 if (!ops)
3074 continue;
3075
3076 if (ops->ack)
3077 rtd->ops.ack = soc_rtdcom_ack;
3078 if (ops->copy_user)
3079 rtd->ops.copy_user = soc_rtdcom_copy_user;
3080 if (ops->copy_kernel)
3081 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3082 if (ops->fill_silence)
3083 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3084 if (ops->page)
3085 rtd->ops.page = soc_rtdcom_page;
3086 if (ops->mmap)
3087 rtd->ops.mmap = soc_rtdcom_mmap;
3088 }
3089
3090 /* overwrite */
3091 if (platform && platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01003092 rtd->ops.ack = platform->driver->ops->ack;
Takashi Iwai29d1a872017-05-10 20:02:35 +02003093 rtd->ops.copy_user = platform->driver->ops->copy_user;
3094 rtd->ops.copy_kernel = platform->driver->ops->copy_kernel;
3095 rtd->ops.fill_silence = platform->driver->ops->fill_silence;
Liam Girdwood01d75842012-04-25 12:12:49 +01003096 rtd->ops.page = platform->driver->ops->page;
3097 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003098 }
3099
3100 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01003101 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003102
3103 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01003104 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003105
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003106 for_each_rtdcom(rtd, rtdcom) {
3107 component = rtdcom->component;
3108
3109 if (!component->pcm_new)
3110 continue;
3111
3112 ret = component->pcm_new(component, rtd);
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003113 if (ret < 0) {
Kuninori Morimotof523ace2017-09-26 01:00:53 +00003114 dev_err(component->dev,
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003115 "ASoC: pcm constructor failed: %d\n",
3116 ret);
3117 return ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +01003118 }
3119 }
Johan Hovoldc641e5b2017-07-12 17:55:29 +02003120
Takashi Iwai5d61f0b2017-08-25 12:04:07 +02003121 pcm->private_free = soc_pcm_private_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01003122out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02003123 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3124 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3125 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01003126 return ret;
3127}
Liam Girdwood01d75842012-04-25 12:12:49 +01003128
3129/* is the current PCM operation for this FE ? */
3130int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3131{
3132 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3133 return 1;
3134 return 0;
3135}
3136EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3137
3138/* is the current PCM operation for this BE ? */
3139int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3140 struct snd_soc_pcm_runtime *be, int stream)
3141{
3142 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3143 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3144 be->dpcm[stream].runtime_update))
3145 return 1;
3146 return 0;
3147}
3148EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3149
3150/* get the substream for this BE */
3151struct snd_pcm_substream *
3152 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3153{
3154 return be->pcm->streams[stream].substream;
3155}
3156EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3157
3158/* get the BE runtime state */
3159enum snd_soc_dpcm_state
3160 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3161{
3162 return be->dpcm[stream].state;
3163}
3164EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3165
3166/* set the BE runtime state */
3167void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3168 int stream, enum snd_soc_dpcm_state state)
3169{
3170 be->dpcm[stream].state = state;
3171}
3172EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3173
3174/*
3175 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
3176 * are not running, paused or suspended for the specified stream direction.
3177 */
3178int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3179 struct snd_soc_pcm_runtime *be, int stream)
3180{
3181 struct snd_soc_dpcm *dpcm;
3182 int state;
3183
3184 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3185
3186 if (dpcm->fe == fe)
3187 continue;
3188
3189 state = dpcm->fe->dpcm[stream].state;
3190 if (state == SND_SOC_DPCM_STATE_START ||
3191 state == SND_SOC_DPCM_STATE_PAUSED ||
3192 state == SND_SOC_DPCM_STATE_SUSPEND)
3193 return 0;
3194 }
3195
3196 /* it's safe to free/stop this BE DAI */
3197 return 1;
3198}
3199EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3200
3201/*
3202 * We can only change hw params a BE DAI if any of it's FE are not prepared,
3203 * running, paused or suspended for the specified stream direction.
3204 */
3205int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3206 struct snd_soc_pcm_runtime *be, int stream)
3207{
3208 struct snd_soc_dpcm *dpcm;
3209 int state;
3210
3211 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
3212
3213 if (dpcm->fe == fe)
3214 continue;
3215
3216 state = dpcm->fe->dpcm[stream].state;
3217 if (state == SND_SOC_DPCM_STATE_START ||
3218 state == SND_SOC_DPCM_STATE_PAUSED ||
3219 state == SND_SOC_DPCM_STATE_SUSPEND ||
3220 state == SND_SOC_DPCM_STATE_PREPARE)
3221 return 0;
3222 }
3223
3224 /* it's safe to change hw_params */
3225 return 1;
3226}
3227EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003228
3229#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen852801412016-11-22 11:29:14 +01003230static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003231{
3232 switch (state) {
3233 case SND_SOC_DPCM_STATE_NEW:
3234 return "new";
3235 case SND_SOC_DPCM_STATE_OPEN:
3236 return "open";
3237 case SND_SOC_DPCM_STATE_HW_PARAMS:
3238 return "hw_params";
3239 case SND_SOC_DPCM_STATE_PREPARE:
3240 return "prepare";
3241 case SND_SOC_DPCM_STATE_START:
3242 return "start";
3243 case SND_SOC_DPCM_STATE_STOP:
3244 return "stop";
3245 case SND_SOC_DPCM_STATE_SUSPEND:
3246 return "suspend";
3247 case SND_SOC_DPCM_STATE_PAUSED:
3248 return "paused";
3249 case SND_SOC_DPCM_STATE_HW_FREE:
3250 return "hw_free";
3251 case SND_SOC_DPCM_STATE_CLOSE:
3252 return "close";
3253 }
3254
3255 return "unknown";
3256}
3257
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003258static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3259 int stream, char *buf, size_t size)
3260{
3261 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3262 struct snd_soc_dpcm *dpcm;
3263 ssize_t offset = 0;
3264
3265 /* FE state */
3266 offset += snprintf(buf + offset, size - offset,
3267 "[%s - %s]\n", fe->dai_link->name,
3268 stream ? "Capture" : "Playback");
3269
3270 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3271 dpcm_state_string(fe->dpcm[stream].state));
3272
3273 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3274 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3275 offset += snprintf(buf + offset, size - offset,
3276 "Hardware Params: "
3277 "Format = %s, Channels = %d, Rate = %d\n",
3278 snd_pcm_format_name(params_format(params)),
3279 params_channels(params),
3280 params_rate(params));
3281
3282 /* BEs state */
3283 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3284
3285 if (list_empty(&fe->dpcm[stream].be_clients)) {
3286 offset += snprintf(buf + offset, size - offset,
3287 " No active DSP links\n");
3288 goto out;
3289 }
3290
3291 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
3292 struct snd_soc_pcm_runtime *be = dpcm->be;
3293 params = &dpcm->hw_params;
3294
3295 offset += snprintf(buf + offset, size - offset,
3296 "- %s\n", be->dai_link->name);
3297
3298 offset += snprintf(buf + offset, size - offset,
3299 " State: %s\n",
3300 dpcm_state_string(be->dpcm[stream].state));
3301
3302 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3303 (be->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
3312out:
3313 return offset;
3314}
3315
3316static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3317 size_t count, loff_t *ppos)
3318{
3319 struct snd_soc_pcm_runtime *fe = file->private_data;
3320 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3321 char *buf;
3322
3323 buf = kmalloc(out_count, GFP_KERNEL);
3324 if (!buf)
3325 return -ENOMEM;
3326
3327 if (fe->cpu_dai->driver->playback.channels_min)
3328 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3329 buf + offset, out_count - offset);
3330
3331 if (fe->cpu_dai->driver->capture.channels_min)
3332 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3333 buf + offset, out_count - offset);
3334
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003335 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003336
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003337 kfree(buf);
3338 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003339}
3340
3341static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01003342 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003343 .read = dpcm_state_read_file,
3344 .llseek = default_llseek,
3345};
3346
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003347void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003348{
Mark Brownb3bba9a2012-05-08 10:33:47 +01003349 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003350 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01003351
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02003352 if (!rtd->card->debugfs_card_root)
3353 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003354
3355 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3356 rtd->card->debugfs_card_root);
3357 if (!rtd->debugfs_dpcm_root) {
3358 dev_dbg(rtd->dev,
3359 "ASoC: Failed to create dpcm debugfs directory %s\n",
3360 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02003361 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003362 }
3363
Fabio Estevamf1e3f402017-07-29 11:40:55 -03003364 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3365 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01003366}
3367#endif