blob: a6d33136d7552f191088a61ef080b3dcbd57df67 [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
Lars-Peter Clausen90996f42013-05-14 11:05:30 +020037/**
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010038 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48{
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020050 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010051
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020056 for (i = 0; i < rtd->num_codecs; i++)
57 rtd->codec_dais[i]->playback_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010058 } else {
59 cpu_dai->capture_active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020060 for (i = 0; i < rtd->num_codecs; i++)
61 rtd->codec_dais[i]->capture_active++;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010062 }
63
64 cpu_dai->active++;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +010065 cpu_dai->component->active++;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020066 for (i = 0; i < rtd->num_codecs; i++) {
67 rtd->codec_dais[i]->active++;
68 rtd->codec_dais[i]->component->active++;
69 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010070}
71
72/**
73 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
74 * @rtd: ASoC PCM runtime that is deactivated
75 * @stream: Direction of the PCM stream
76 *
77 * Decrements the active count for all the DAIs and components attached to a PCM
78 * runtime. Should typically be called when a stream is closed.
79 *
80 * Must be called with the rtd->pcm_mutex being held
81 */
82void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
83{
84 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020085 int i;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010086
87 lockdep_assert_held(&rtd->pcm_mutex);
88
89 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
90 cpu_dai->playback_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020091 for (i = 0; i < rtd->num_codecs; i++)
92 rtd->codec_dais[i]->playback_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010093 } else {
94 cpu_dai->capture_active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +020095 for (i = 0; i < rtd->num_codecs; i++)
96 rtd->codec_dais[i]->capture_active--;
Lars-Peter Clausen24894b72014-03-05 13:17:43 +010097 }
98
99 cpu_dai->active--;
Lars-Peter Clausencdde4cc2014-03-05 13:17:47 +0100100 cpu_dai->component->active--;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200101 for (i = 0; i < rtd->num_codecs; i++) {
102 rtd->codec_dais[i]->component->active--;
103 rtd->codec_dais[i]->active--;
104 }
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100105}
106
107/**
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100108 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
109 * @rtd: The ASoC PCM runtime that should be checked.
110 *
111 * This function checks whether the power down delay should be ignored for a
112 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
113 * been configured to ignore the delay, or if none of the components benefits
114 * from having the delay.
115 */
116bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
117{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200118 int i;
119 bool ignore = true;
120
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100121 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
122 return true;
123
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200124 for (i = 0; i < rtd->num_codecs; i++)
125 ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
126
127 return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100128}
129
130/**
Lars-Peter Clausen90996f42013-05-14 11:05:30 +0200131 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
132 * @substream: the pcm substream
133 * @hw: the hardware parameters
134 *
135 * Sets the substream runtime hardware parameters.
136 */
137int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
138 const struct snd_pcm_hardware *hw)
139{
140 struct snd_pcm_runtime *runtime = substream->runtime;
141 runtime->hw.info = hw->info;
142 runtime->hw.formats = hw->formats;
143 runtime->hw.period_bytes_min = hw->period_bytes_min;
144 runtime->hw.period_bytes_max = hw->period_bytes_max;
145 runtime->hw.periods_min = hw->periods_min;
146 runtime->hw.periods_max = hw->periods_max;
147 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
148 runtime->hw.fifo_size = hw->fifo_size;
149 return 0;
150}
151EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
152
Liam Girdwood01d75842012-04-25 12:12:49 +0100153/* DPCM stream event, send event to FE and all active BEs. */
Liam Girdwood23607022014-01-17 17:03:55 +0000154int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
Liam Girdwood01d75842012-04-25 12:12:49 +0100155 int event)
156{
157 struct snd_soc_dpcm *dpcm;
158
159 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
160
161 struct snd_soc_pcm_runtime *be = dpcm->be;
162
Liam Girdwood103d84a2012-11-19 14:39:15 +0000163 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +0100164 be->dai_link->name, event, dir);
165
166 snd_soc_dapm_stream_event(be, dir, event);
167 }
168
169 snd_soc_dapm_stream_event(fe, dir, event);
170
171 return 0;
172}
173
Dong Aisheng17841022011-08-29 17:15:14 +0800174static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
175 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100176{
177 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100178 int ret;
179
Nicolin Chen3635bf02013-11-13 18:56:24 +0800180 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
181 rtd->dai_link->symmetric_rates)) {
182 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
183 soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100184
Nicolin Chen3635bf02013-11-13 18:56:24 +0800185 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
186 SNDRV_PCM_HW_PARAM_RATE,
187 soc_dai->rate, soc_dai->rate);
188 if (ret < 0) {
189 dev_err(soc_dai->dev,
190 "ASoC: Unable to apply rate constraint: %d\n",
191 ret);
192 return ret;
193 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100194 }
195
Nicolin Chen3635bf02013-11-13 18:56:24 +0800196 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
197 rtd->dai_link->symmetric_channels)) {
198 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
199 soc_dai->channels);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100200
Nicolin Chen3635bf02013-11-13 18:56:24 +0800201 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
202 SNDRV_PCM_HW_PARAM_CHANNELS,
203 soc_dai->channels,
204 soc_dai->channels);
205 if (ret < 0) {
206 dev_err(soc_dai->dev,
207 "ASoC: Unable to apply channel symmetry constraint: %d\n",
208 ret);
209 return ret;
210 }
211 }
212
213 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
214 rtd->dai_link->symmetric_samplebits)) {
215 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
216 soc_dai->sample_bits);
217
218 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
219 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
220 soc_dai->sample_bits,
221 soc_dai->sample_bits);
222 if (ret < 0) {
223 dev_err(soc_dai->dev,
224 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
225 ret);
226 return ret;
227 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100228 }
229
230 return 0;
231}
232
Nicolin Chen3635bf02013-11-13 18:56:24 +0800233static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
235{
236 struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200238 unsigned int rate, channels, sample_bits, symmetry, i;
Nicolin Chen3635bf02013-11-13 18:56:24 +0800239
240 rate = params_rate(params);
241 channels = params_channels(params);
242 sample_bits = snd_pcm_format_physical_width(params_format(params));
243
244 /* reject unmatched parameters when applying symmetry */
245 symmetry = cpu_dai->driver->symmetric_rates ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800246 rtd->dai_link->symmetric_rates;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200247
248 for (i = 0; i < rtd->num_codecs; i++)
249 symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
250
Nicolin Chen3635bf02013-11-13 18:56:24 +0800251 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
252 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
253 cpu_dai->rate, rate);
254 return -EINVAL;
255 }
256
257 symmetry = cpu_dai->driver->symmetric_channels ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800258 rtd->dai_link->symmetric_channels;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200259
260 for (i = 0; i < rtd->num_codecs; i++)
261 symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
262
Nicolin Chen3635bf02013-11-13 18:56:24 +0800263 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
264 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
265 cpu_dai->channels, channels);
266 return -EINVAL;
267 }
268
269 symmetry = cpu_dai->driver->symmetric_samplebits ||
Nicolin Chen3635bf02013-11-13 18:56:24 +0800270 rtd->dai_link->symmetric_samplebits;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200271
272 for (i = 0; i < rtd->num_codecs; i++)
273 symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
274
Nicolin Chen3635bf02013-11-13 18:56:24 +0800275 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
276 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
277 cpu_dai->sample_bits, sample_bits);
278 return -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100279 }
280
281 return 0;
282}
283
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100284static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
285{
286 struct snd_soc_pcm_runtime *rtd = substream->private_data;
287 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100288 struct snd_soc_dai_link *link = rtd->dai_link;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200289 unsigned int symmetry, i;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100290
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200291 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
292 cpu_driver->symmetric_channels || link->symmetric_channels ||
293 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
294
295 for (i = 0; i < rtd->num_codecs; i++)
296 symmetry = symmetry ||
297 rtd->codec_dais[i]->driver->symmetric_rates ||
298 rtd->codec_dais[i]->driver->symmetric_channels ||
299 rtd->codec_dais[i]->driver->symmetric_samplebits;
300
301 return symmetry;
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100302}
303
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200304static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
Mark Brown58ba9b22012-01-16 18:38:51 +0000305{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200306 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwaic6068d32014-12-31 17:10:34 +0100307 int ret;
Mark Brown58ba9b22012-01-16 18:38:51 +0000308
309 if (!bits)
310 return;
311
Lars-Peter Clausen0e2a3752014-12-29 18:43:38 +0100312 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
313 if (ret != 0)
314 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
315 bits, ret);
Mark Brown58ba9b22012-01-16 18:38:51 +0000316}
317
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200318static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200319{
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200320 struct snd_soc_pcm_runtime *rtd = substream->private_data;
321 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200322 struct snd_soc_dai *codec_dai;
323 int i;
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200324 unsigned int bits = 0, cpu_bits;
325
326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200327 for (i = 0; i < rtd->num_codecs; i++) {
328 codec_dai = rtd->codec_dais[i];
329 if (codec_dai->driver->playback.sig_bits == 0) {
330 bits = 0;
331 break;
332 }
333 bits = max(codec_dai->driver->playback.sig_bits, bits);
334 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200335 cpu_bits = cpu_dai->driver->playback.sig_bits;
336 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200337 for (i = 0; i < rtd->num_codecs; i++) {
338 codec_dai = rtd->codec_dais[i];
Daniel Mack5e63dfc2014-10-07 14:33:46 +0200339 if (codec_dai->driver->capture.sig_bits == 0) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200340 bits = 0;
341 break;
342 }
343 bits = max(codec_dai->driver->capture.sig_bits, bits);
344 }
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200345 cpu_bits = cpu_dai->driver->capture.sig_bits;
346 }
347
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200348 soc_pcm_set_msb(substream, bits);
349 soc_pcm_set_msb(substream, cpu_bits);
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200350}
351
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200352static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200353{
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200354 struct snd_pcm_runtime *runtime = substream->runtime;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100355 struct snd_pcm_hardware *hw = &runtime->hw;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
358 struct snd_soc_dai_driver *codec_dai_drv;
359 struct snd_soc_pcm_stream *codec_stream;
360 struct snd_soc_pcm_stream *cpu_stream;
361 unsigned int chan_min = 0, chan_max = UINT_MAX;
362 unsigned int rate_min = 0, rate_max = UINT_MAX;
363 unsigned int rates = UINT_MAX;
364 u64 formats = ULLONG_MAX;
365 int i;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100366
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
368 cpu_stream = &cpu_dai_drv->playback;
Lars-Peter Clausen16d7ea92014-01-06 14:19:16 +0100369 else
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200370 cpu_stream = &cpu_dai_drv->capture;
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100371
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200372 /* first calculate min/max only for CODECs in the DAI link */
373 for (i = 0; i < rtd->num_codecs; i++) {
374 codec_dai_drv = rtd->codec_dais[i]->driver;
375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
376 codec_stream = &codec_dai_drv->playback;
377 else
378 codec_stream = &codec_dai_drv->capture;
379 chan_min = max(chan_min, codec_stream->channels_min);
380 chan_max = min(chan_max, codec_stream->channels_max);
381 rate_min = max(rate_min, codec_stream->rate_min);
382 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
383 formats &= codec_stream->formats;
384 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
385 }
386
387 /*
388 * chan min/max cannot be enforced if there are multiple CODEC DAIs
389 * connected to a single CPU DAI, use CPU DAI's directly and let
390 * channel allocation be fixed up later
391 */
392 if (rtd->num_codecs > 1) {
393 chan_min = cpu_stream->channels_min;
394 chan_max = cpu_stream->channels_max;
395 }
396
397 hw->channels_min = max(chan_min, cpu_stream->channels_min);
398 hw->channels_max = min(chan_max, cpu_stream->channels_max);
399 if (hw->formats)
400 hw->formats &= formats & cpu_stream->formats;
401 else
402 hw->formats = formats & cpu_stream->formats;
403 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100404
405 snd_pcm_limit_hw_rates(runtime);
406
407 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200408 hw->rate_min = max(hw->rate_min, rate_min);
Lars-Peter Clausen78e45c92013-11-27 09:58:18 +0100409 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200410 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
Lars-Peter Clausenbd477c32013-05-14 11:05:31 +0200411}
412
Mark Brown58ba9b22012-01-16 18:38:51 +0000413/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100414 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
415 * then initialized and any private data can be allocated. This also calls
416 * startup for the cpu DAI, platform, machine and codec DAI.
417 */
418static int soc_pcm_open(struct snd_pcm_substream *substream)
419{
420 struct snd_soc_pcm_runtime *rtd = substream->private_data;
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 struct snd_soc_platform *platform = rtd->platform;
423 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200424 struct snd_soc_dai *codec_dai;
425 const char *codec_dai_name = "multicodec";
426 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100427
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800428 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200429 for (i = 0; i < rtd->num_codecs; i++)
430 pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000431 pm_runtime_get_sync(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200432 for (i = 0; i < rtd->num_codecs; i++)
433 pm_runtime_get_sync(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000434 pm_runtime_get_sync(platform->dev);
435
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100436 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100437
438 /* startup the audio subsystem */
Mark Brownc5914b02013-10-30 17:47:39 -0700439 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100440 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
441 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000442 dev_err(cpu_dai->dev, "ASoC: can't open interface"
443 " %s: %d\n", cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100444 goto out;
445 }
446 }
447
448 if (platform->driver->ops && platform->driver->ops->open) {
449 ret = platform->driver->ops->open(substream);
450 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000451 dev_err(platform->dev, "ASoC: can't open platform"
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200452 " %s: %d\n", platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100453 goto platform_err;
454 }
455 }
456
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200457 for (i = 0; i < rtd->num_codecs; i++) {
458 codec_dai = rtd->codec_dais[i];
459 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
460 ret = codec_dai->driver->ops->startup(substream,
461 codec_dai);
462 if (ret < 0) {
463 dev_err(codec_dai->dev,
464 "ASoC: can't open codec %s: %d\n",
465 codec_dai->name, ret);
466 goto codec_dai_err;
467 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100468 }
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200469
470 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
471 codec_dai->tx_mask = 0;
472 else
473 codec_dai->rx_mask = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100474 }
475
476 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
477 ret = rtd->dai_link->ops->startup(substream);
478 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000479 pr_err("ASoC: %s startup failed: %d\n",
Mark Brown25bfe662012-02-01 21:30:32 +0000480 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100481 goto machine_err;
482 }
483 }
484
Liam Girdwood01d75842012-04-25 12:12:49 +0100485 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
486 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
487 goto dynamic;
488
Liam Girdwoodddee6272011-06-09 14:45:53 +0100489 /* Check that the codec and cpu DAIs are compatible */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200490 soc_pcm_init_runtime_hw(substream);
491
492 if (rtd->num_codecs == 1)
493 codec_dai_name = rtd->codec_dai->name;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494
Lars-Peter Clausen62e5f672013-11-30 17:38:58 +0100495 if (soc_pcm_has_symmetry(substream))
496 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
497
Liam Girdwoodddee6272011-06-09 14:45:53 +0100498 ret = -EINVAL;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100499 if (!runtime->hw.rates) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000500 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200501 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100502 goto config_err;
503 }
504 if (!runtime->hw.formats) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000505 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200506 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100507 goto config_err;
508 }
509 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
510 runtime->hw.channels_min > runtime->hw.channels_max) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000511 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200512 codec_dai_name, cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100513 goto config_err;
514 }
515
Benoit Coussonc8dd1fe2014-07-01 09:47:55 +0200516 soc_pcm_apply_msb(substream);
Mark Brown58ba9b22012-01-16 18:38:51 +0000517
Liam Girdwoodddee6272011-06-09 14:45:53 +0100518 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800519 if (cpu_dai->active) {
520 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
521 if (ret != 0)
522 goto config_err;
523 }
524
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200525 for (i = 0; i < rtd->num_codecs; i++) {
526 if (rtd->codec_dais[i]->active) {
527 ret = soc_pcm_apply_symmetry(substream,
528 rtd->codec_dais[i]);
529 if (ret != 0)
530 goto config_err;
531 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100532 }
533
Liam Girdwood103d84a2012-11-19 14:39:15 +0000534 pr_debug("ASoC: %s <-> %s info:\n",
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200535 codec_dai_name, cpu_dai->name);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000536 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
537 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100538 runtime->hw.channels_max);
Liam Girdwood103d84a2012-11-19 14:39:15 +0000539 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
Liam Girdwoodddee6272011-06-09 14:45:53 +0100540 runtime->hw.rate_max);
541
Liam Girdwood01d75842012-04-25 12:12:49 +0100542dynamic:
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100543
544 snd_soc_runtime_activate(rtd, substream->stream);
545
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100546 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100547 return 0;
548
549config_err:
550 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
551 rtd->dai_link->ops->shutdown(substream);
552
553machine_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200554 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100555
556codec_dai_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200557 while (--i >= 0) {
558 codec_dai = rtd->codec_dais[i];
559 if (codec_dai->driver->ops->shutdown)
560 codec_dai->driver->ops->shutdown(substream, codec_dai);
561 }
562
Liam Girdwoodddee6272011-06-09 14:45:53 +0100563 if (platform->driver->ops && platform->driver->ops->close)
564 platform->driver->ops->close(substream);
565
566platform_err:
567 if (cpu_dai->driver->ops->shutdown)
568 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
569out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100570 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000571
572 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200573 for (i = 0; i < rtd->num_codecs; i++)
574 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000575 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200576 for (i = 0; i < rtd->num_codecs; i++) {
577 if (!rtd->codec_dais[i]->active)
578 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
579 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800580 if (!cpu_dai->active)
581 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000582
Liam Girdwoodddee6272011-06-09 14:45:53 +0100583 return ret;
584}
585
586/*
587 * Power down the audio subsystem pmdown_time msecs after close is called.
588 * This is to ensure there are no pops or clicks in between any music tracks
589 * due to DAPM power cycling.
590 */
591static void close_delayed_work(struct work_struct *work)
592{
593 struct snd_soc_pcm_runtime *rtd =
594 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200595 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
Liam Girdwoodddee6272011-06-09 14:45:53 +0100596
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100597 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100598
Liam Girdwood103d84a2012-11-19 14:39:15 +0000599 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
Liam Girdwoodddee6272011-06-09 14:45:53 +0100600 codec_dai->driver->playback.stream_name,
601 codec_dai->playback_active ? "active" : "inactive",
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600602 rtd->pop_wait ? "yes" : "no");
Liam Girdwoodddee6272011-06-09 14:45:53 +0100603
604 /* are we waiting on this codec DAI stream */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600605 if (rtd->pop_wait == 1) {
606 rtd->pop_wait = 0;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800607 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000608 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100609 }
610
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100611 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100612}
613
614/*
615 * Called by ALSA when a PCM substream is closed. Private data can be
616 * freed here. The cpu DAI, codec DAI, machine and platform are also
617 * shutdown.
618 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100619static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100620{
621 struct snd_soc_pcm_runtime *rtd = substream->private_data;
622 struct snd_soc_platform *platform = rtd->platform;
623 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200624 struct snd_soc_dai *codec_dai;
625 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100626
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100627 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100628
Lars-Peter Clausen24894b72014-03-05 13:17:43 +0100629 snd_soc_runtime_deactivate(rtd, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100630
Dong Aisheng17841022011-08-29 17:15:14 +0800631 /* clear the corresponding DAIs rate when inactive */
632 if (!cpu_dai->active)
633 cpu_dai->rate = 0;
634
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200635 for (i = 0; i < rtd->num_codecs; i++) {
636 codec_dai = rtd->codec_dais[i];
637 if (!codec_dai->active)
638 codec_dai->rate = 0;
639 }
Sascha Hauer25b76792011-08-17 09:20:01 +0200640
Ramesh Babuae116012014-10-15 12:34:59 +0530641 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
642
Liam Girdwoodddee6272011-06-09 14:45:53 +0100643 if (cpu_dai->driver->ops->shutdown)
644 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
645
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200646 for (i = 0; i < rtd->num_codecs; i++) {
647 codec_dai = rtd->codec_dais[i];
648 if (codec_dai->driver->ops->shutdown)
649 codec_dai->driver->ops->shutdown(substream, codec_dai);
650 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100651
652 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
653 rtd->dai_link->ops->shutdown(substream);
654
655 if (platform->driver->ops && platform->driver->ops->close)
656 platform->driver->ops->close(substream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100657
658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Lars-Peter Clausen208a1582014-03-05 13:17:42 +0100659 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300660 /* powered down playback stream now */
661 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800662 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800663 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300664 } else {
665 /* start delayed pop wq here for playback streams */
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600666 rtd->pop_wait = 1;
Mark Brownd4e1a732013-07-18 11:52:17 +0100667 queue_delayed_work(system_power_efficient_wq,
668 &rtd->delayed_work,
669 msecs_to_jiffies(rtd->pmdown_time));
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300670 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100671 } else {
672 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800673 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000674 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100675 }
676
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100677 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000678
679 pm_runtime_put(platform->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200680 for (i = 0; i < rtd->num_codecs; i++)
681 pm_runtime_put(rtd->codec_dais[i]->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000682 pm_runtime_put(cpu_dai->dev);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200683 for (i = 0; i < rtd->num_codecs; i++) {
684 if (!rtd->codec_dais[i]->active)
685 pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
686 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800687 if (!cpu_dai->active)
688 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Mark Brownd6652ef2011-12-03 20:14:31 +0000689
Liam Girdwoodddee6272011-06-09 14:45:53 +0100690 return 0;
691}
692
693/*
694 * Called by ALSA when the PCM substream is prepared, can set format, sample
695 * rate, etc. This function is non atomic and can be called multiple times,
696 * it can refer to the runtime info.
697 */
698static int soc_pcm_prepare(struct snd_pcm_substream *substream)
699{
700 struct snd_soc_pcm_runtime *rtd = substream->private_data;
701 struct snd_soc_platform *platform = rtd->platform;
702 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200703 struct snd_soc_dai *codec_dai;
704 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100705
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100706 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100707
708 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
709 ret = rtd->dai_link->ops->prepare(substream);
710 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000711 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
712 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100713 goto out;
714 }
715 }
716
717 if (platform->driver->ops && platform->driver->ops->prepare) {
718 ret = platform->driver->ops->prepare(substream);
719 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000720 dev_err(platform->dev, "ASoC: platform prepare error:"
721 " %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100722 goto out;
723 }
724 }
725
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200726 for (i = 0; i < rtd->num_codecs; i++) {
727 codec_dai = rtd->codec_dais[i];
728 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
729 ret = codec_dai->driver->ops->prepare(substream,
730 codec_dai);
731 if (ret < 0) {
732 dev_err(codec_dai->dev,
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200733 "ASoC: codec DAI prepare error: %d\n",
734 ret);
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200735 goto out;
736 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100737 }
738 }
739
Mark Brownc5914b02013-10-30 17:47:39 -0700740 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100741 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
742 if (ret < 0) {
Jarkko Nikula90cc7f12014-12-23 11:04:41 +0200743 dev_err(cpu_dai->dev,
744 "ASoC: cpu DAI prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100745 goto out;
746 }
747 }
748
749 /* cancel any delayed stream shutdown that is pending */
750 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Misael Lopez Cruz9bffb1f2012-12-13 12:23:05 -0600751 rtd->pop_wait) {
752 rtd->pop_wait = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100753 cancel_delayed_work(&rtd->delayed_work);
754 }
755
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000756 snd_soc_dapm_stream_event(rtd, substream->stream,
757 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100758
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200759 for (i = 0; i < rtd->num_codecs; i++)
760 snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
761 substream->stream);
Ramesh Babuae116012014-10-15 12:34:59 +0530762 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100763
764out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100765 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100766 return ret;
767}
768
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200769static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
770 unsigned int mask)
771{
772 struct snd_interval *interval;
773 int channels = hweight_long(mask);
774
775 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
776 interval->min = channels;
777 interval->max = channels;
778}
779
Benoit Cousson93e69582014-07-08 23:19:38 +0200780int soc_dai_hw_params(struct snd_pcm_substream *substream,
781 struct snd_pcm_hw_params *params,
782 struct snd_soc_dai *dai)
783{
784 int ret;
785
786 if (dai->driver->ops && dai->driver->ops->hw_params) {
787 ret = dai->driver->ops->hw_params(substream, params, dai);
788 if (ret < 0) {
789 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
790 dai->name, ret);
791 return ret;
792 }
793 }
794
795 return 0;
796}
797
Liam Girdwoodddee6272011-06-09 14:45:53 +0100798/*
799 * Called by ALSA when the hardware params are set by application. This
800 * function can also be called multiple times and can allocate buffers
801 * (using snd_pcm_lib_* ). It's non-atomic.
802 */
803static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
804 struct snd_pcm_hw_params *params)
805{
806 struct snd_soc_pcm_runtime *rtd = substream->private_data;
807 struct snd_soc_platform *platform = rtd->platform;
808 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200809 int i, ret = 0;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100810
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100811 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100812
Nicolin Chen3635bf02013-11-13 18:56:24 +0800813 ret = soc_pcm_params_symmetry(substream, params);
814 if (ret)
815 goto out;
816
Liam Girdwoodddee6272011-06-09 14:45:53 +0100817 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
818 ret = rtd->dai_link->ops->hw_params(substream, params);
819 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000820 dev_err(rtd->card->dev, "ASoC: machine hw_params"
821 " failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100822 goto out;
823 }
824 }
825
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200826 for (i = 0; i < rtd->num_codecs; i++) {
827 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
828 struct snd_pcm_hw_params codec_params;
829
830 /* copy params for each codec */
831 codec_params = *params;
832
833 /* fixup params based on TDM slot masks */
834 if (codec_dai->tx_mask)
835 soc_pcm_codec_params_fixup(&codec_params,
836 codec_dai->tx_mask);
837 if (codec_dai->rx_mask)
838 soc_pcm_codec_params_fixup(&codec_params,
839 codec_dai->rx_mask);
840
Benoit Cousson93e69582014-07-08 23:19:38 +0200841 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
842 if(ret < 0)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100843 goto codec_err;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200844
845 codec_dai->rate = params_rate(&codec_params);
846 codec_dai->channels = params_channels(&codec_params);
847 codec_dai->sample_bits = snd_pcm_format_physical_width(
848 params_format(&codec_params));
Liam Girdwoodddee6272011-06-09 14:45:53 +0100849 }
850
Benoit Cousson93e69582014-07-08 23:19:38 +0200851 ret = soc_dai_hw_params(substream, params, cpu_dai);
852 if (ret < 0)
853 goto interface_err;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100854
855 if (platform->driver->ops && platform->driver->ops->hw_params) {
856 ret = platform->driver->ops->hw_params(substream, params);
857 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +0000858 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200859 platform->component.name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100860 goto platform_err;
861 }
862 }
863
Nicolin Chen3635bf02013-11-13 18:56:24 +0800864 /* store the parameters for each DAIs */
Dong Aisheng17841022011-08-29 17:15:14 +0800865 cpu_dai->rate = params_rate(params);
Nicolin Chen3635bf02013-11-13 18:56:24 +0800866 cpu_dai->channels = params_channels(params);
867 cpu_dai->sample_bits =
868 snd_pcm_format_physical_width(params_format(params));
869
Liam Girdwoodddee6272011-06-09 14:45:53 +0100870out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100871 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100872 return ret;
873
874platform_err:
Mark Brownc5914b02013-10-30 17:47:39 -0700875 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100876 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
877
878interface_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200879 i = rtd->num_codecs;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100880
881codec_err:
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200882 while (--i >= 0) {
883 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
884 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
885 codec_dai->driver->ops->hw_free(substream, codec_dai);
886 codec_dai->rate = 0;
887 }
888
Liam Girdwoodddee6272011-06-09 14:45:53 +0100889 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
890 rtd->dai_link->ops->hw_free(substream);
891
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100892 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100893 return ret;
894}
895
896/*
897 * Frees resources allocated by hw_params, can be called multiple times
898 */
899static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
900{
901 struct snd_soc_pcm_runtime *rtd = substream->private_data;
902 struct snd_soc_platform *platform = rtd->platform;
903 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200904 struct snd_soc_dai *codec_dai;
Nicolin Chen7f62b6e2013-12-04 11:18:36 +0800905 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200906 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100907
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100908 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100909
Nicolin Chend3383422013-11-20 18:37:09 +0800910 /* clear the corresponding DAIs parameters when going to be inactive */
911 if (cpu_dai->active == 1) {
912 cpu_dai->rate = 0;
913 cpu_dai->channels = 0;
914 cpu_dai->sample_bits = 0;
915 }
916
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200917 for (i = 0; i < rtd->num_codecs; i++) {
918 codec_dai = rtd->codec_dais[i];
919 if (codec_dai->active == 1) {
920 codec_dai->rate = 0;
921 codec_dai->channels = 0;
922 codec_dai->sample_bits = 0;
923 }
Nicolin Chend3383422013-11-20 18:37:09 +0800924 }
925
Liam Girdwoodddee6272011-06-09 14:45:53 +0100926 /* apply codec digital mute */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200927 for (i = 0; i < rtd->num_codecs; i++) {
928 if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
929 (!playback && rtd->codec_dais[i]->capture_active == 1))
930 snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
931 substream->stream);
932 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100933
934 /* free any machine hw params */
935 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
936 rtd->dai_link->ops->hw_free(substream);
937
938 /* free any DMA resources */
939 if (platform->driver->ops && platform->driver->ops->hw_free)
940 platform->driver->ops->hw_free(substream);
941
942 /* now free hw params for the DAIs */
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200943 for (i = 0; i < rtd->num_codecs; i++) {
944 codec_dai = rtd->codec_dais[i];
945 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
946 codec_dai->driver->ops->hw_free(substream, codec_dai);
947 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100948
Mark Brownc5914b02013-10-30 17:47:39 -0700949 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100950 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
951
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100952 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100953 return 0;
954}
955
956static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
957{
958 struct snd_soc_pcm_runtime *rtd = substream->private_data;
959 struct snd_soc_platform *platform = rtd->platform;
960 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200961 struct snd_soc_dai *codec_dai;
962 int i, ret;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100963
Benoit Cousson2e5894d2014-07-08 23:19:35 +0200964 for (i = 0; i < rtd->num_codecs; i++) {
965 codec_dai = rtd->codec_dais[i];
966 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
967 ret = codec_dai->driver->ops->trigger(substream,
968 cmd, codec_dai);
969 if (ret < 0)
970 return ret;
971 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100972 }
973
974 if (platform->driver->ops && platform->driver->ops->trigger) {
975 ret = platform->driver->ops->trigger(substream, cmd);
976 if (ret < 0)
977 return ret;
978 }
979
Mark Brownc5914b02013-10-30 17:47:39 -0700980 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
Liam Girdwoodddee6272011-06-09 14:45:53 +0100981 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
982 if (ret < 0)
983 return ret;
984 }
Jarkko Nikula4792b0d2014-04-28 14:17:52 +0200985
986 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
987 ret = rtd->dai_link->ops->trigger(substream, cmd);
988 if (ret < 0)
989 return ret;
990 }
991
Liam Girdwoodddee6272011-06-09 14:45:53 +0100992 return 0;
993}
994
Mark Brown45c0a182012-05-09 21:46:27 +0100995static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
996 int cmd)
Liam Girdwood07bf84a2012-04-25 12:12:52 +0100997{
998 struct snd_soc_pcm_runtime *rtd = substream->private_data;
999 struct snd_soc_platform *platform = rtd->platform;
1000 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001001 struct snd_soc_dai *codec_dai;
1002 int i, ret;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001003
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001004 for (i = 0; i < rtd->num_codecs; i++) {
1005 codec_dai = rtd->codec_dais[i];
1006 if (codec_dai->driver->ops &&
1007 codec_dai->driver->ops->bespoke_trigger) {
1008 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1009 cmd, codec_dai);
1010 if (ret < 0)
1011 return ret;
1012 }
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001013 }
1014
Jean-Francois Moinedcf0fa22014-01-03 09:19:18 +01001015 if (platform->driver->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001016 ret = platform->driver->bespoke_trigger(substream, cmd);
1017 if (ret < 0)
1018 return ret;
1019 }
1020
Mark Brownc5914b02013-10-30 17:47:39 -07001021 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
Liam Girdwood07bf84a2012-04-25 12:12:52 +01001022 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1023 if (ret < 0)
1024 return ret;
1025 }
1026 return 0;
1027}
Liam Girdwoodddee6272011-06-09 14:45:53 +01001028/*
1029 * soc level wrapper for pointer callback
1030 * If cpu_dai, codec_dai, platform driver has the delay callback, than
1031 * the runtime->delay will be updated accordingly.
1032 */
1033static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1034{
1035 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1036 struct snd_soc_platform *platform = rtd->platform;
1037 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001038 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001039 struct snd_pcm_runtime *runtime = substream->runtime;
1040 snd_pcm_uframes_t offset = 0;
1041 snd_pcm_sframes_t delay = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001042 snd_pcm_sframes_t codec_delay = 0;
1043 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001044
1045 if (platform->driver->ops && platform->driver->ops->pointer)
1046 offset = platform->driver->ops->pointer(substream);
1047
Mark Brownc5914b02013-10-30 17:47:39 -07001048 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
Liam Girdwoodddee6272011-06-09 14:45:53 +01001049 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1050
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001051 for (i = 0; i < rtd->num_codecs; i++) {
1052 codec_dai = rtd->codec_dais[i];
1053 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
1054 codec_delay = max(codec_delay,
1055 codec_dai->driver->ops->delay(substream,
1056 codec_dai));
1057 }
1058 delay += codec_delay;
Liam Girdwoodddee6272011-06-09 14:45:53 +01001059
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001060 /*
1061 * None of the existing platform drivers implement delay(), so
1062 * for now the codec_dai of first multicodec entry is used
1063 */
Liam Girdwoodddee6272011-06-09 14:45:53 +01001064 if (platform->driver->delay)
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001065 delay += platform->driver->delay(substream, rtd->codec_dais[0]);
Liam Girdwoodddee6272011-06-09 14:45:53 +01001066
1067 runtime->delay = delay;
1068
1069 return offset;
1070}
1071
Liam Girdwood01d75842012-04-25 12:12:49 +01001072/* connect a FE and BE */
1073static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1074 struct snd_soc_pcm_runtime *be, int stream)
1075{
1076 struct snd_soc_dpcm *dpcm;
1077
1078 /* only add new dpcms */
1079 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1080 if (dpcm->be == be && dpcm->fe == fe)
1081 return 0;
1082 }
1083
1084 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1085 if (!dpcm)
1086 return -ENOMEM;
1087
1088 dpcm->be = be;
1089 dpcm->fe = fe;
1090 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1091 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1092 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1093 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1094
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001095 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001096 stream ? "capture" : "playback", fe->dai_link->name,
1097 stream ? "<-" : "->", be->dai_link->name);
1098
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001099#ifdef CONFIG_DEBUG_FS
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02001100 if (fe->debugfs_dpcm_root)
1101 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1102 fe->debugfs_dpcm_root, &dpcm->state);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001103#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001104 return 1;
1105}
1106
1107/* reparent a BE onto another FE */
1108static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1109 struct snd_soc_pcm_runtime *be, int stream)
1110{
1111 struct snd_soc_dpcm *dpcm;
1112 struct snd_pcm_substream *fe_substream, *be_substream;
1113
1114 /* reparent if BE is connected to other FEs */
1115 if (!be->dpcm[stream].users)
1116 return;
1117
1118 be_substream = snd_soc_dpcm_get_substream(be, stream);
1119
1120 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
1121 if (dpcm->fe == fe)
1122 continue;
1123
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001124 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001125 stream ? "capture" : "playback",
1126 dpcm->fe->dai_link->name,
1127 stream ? "<-" : "->", dpcm->be->dai_link->name);
1128
1129 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1130 be_substream->runtime = fe_substream->runtime;
1131 break;
1132 }
1133}
1134
1135/* disconnect a BE and FE */
Liam Girdwood23607022014-01-17 17:03:55 +00001136void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001137{
1138 struct snd_soc_dpcm *dpcm, *d;
1139
1140 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001141 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001142 stream ? "capture" : "playback",
1143 dpcm->be->dai_link->name);
1144
1145 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1146 continue;
1147
Jarkko Nikula7cc302d2013-09-30 17:08:15 +03001148 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001149 stream ? "capture" : "playback", fe->dai_link->name,
1150 stream ? "<-" : "->", dpcm->be->dai_link->name);
1151
1152 /* BEs still alive need new FE */
1153 dpcm_be_reparent(fe, dpcm->be, stream);
1154
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001155#ifdef CONFIG_DEBUG_FS
1156 debugfs_remove(dpcm->debugfs_state);
1157#endif
Liam Girdwood01d75842012-04-25 12:12:49 +01001158 list_del(&dpcm->list_be);
1159 list_del(&dpcm->list_fe);
1160 kfree(dpcm);
1161 }
1162}
1163
1164/* get BE for DAI widget and stream */
1165static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1166 struct snd_soc_dapm_widget *widget, int stream)
1167{
1168 struct snd_soc_pcm_runtime *be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001169 int i, j;
Liam Girdwood01d75842012-04-25 12:12:49 +01001170
1171 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1172 for (i = 0; i < card->num_links; i++) {
1173 be = &card->rtd[i];
1174
Liam Girdwood35ea0652012-06-05 19:26:59 +01001175 if (!be->dai_link->no_pcm)
1176 continue;
1177
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001178 if (be->cpu_dai->playback_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001179 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001180
1181 for (j = 0; j < be->num_codecs; j++) {
1182 struct snd_soc_dai *dai = be->codec_dais[j];
1183 if (dai->playback_widget == widget)
1184 return be;
1185 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001186 }
1187 } else {
1188
1189 for (i = 0; i < card->num_links; i++) {
1190 be = &card->rtd[i];
1191
Liam Girdwood35ea0652012-06-05 19:26:59 +01001192 if (!be->dai_link->no_pcm)
1193 continue;
1194
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001195 if (be->cpu_dai->capture_widget == widget)
Liam Girdwood01d75842012-04-25 12:12:49 +01001196 return be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001197
1198 for (j = 0; j < be->num_codecs; j++) {
1199 struct snd_soc_dai *dai = be->codec_dais[j];
1200 if (dai->capture_widget == widget)
1201 return be;
1202 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001203 }
1204 }
1205
Liam Girdwood103d84a2012-11-19 14:39:15 +00001206 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001207 stream ? "capture" : "playback", widget->name);
1208 return NULL;
1209}
1210
1211static inline struct snd_soc_dapm_widget *
Benoit Cousson37018612014-04-24 14:01:45 +02001212 dai_get_widget(struct snd_soc_dai *dai, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001213{
1214 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
Benoit Cousson37018612014-04-24 14:01:45 +02001215 return dai->playback_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001216 else
Benoit Cousson37018612014-04-24 14:01:45 +02001217 return dai->capture_widget;
Liam Girdwood01d75842012-04-25 12:12:49 +01001218}
1219
1220static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1221 struct snd_soc_dapm_widget *widget)
1222{
1223 int i;
1224
1225 for (i = 0; i < list->num_widgets; i++) {
1226 if (widget == list->widgets[i])
1227 return 1;
1228 }
1229
1230 return 0;
1231}
1232
Liam Girdwood23607022014-01-17 17:03:55 +00001233int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001234 int stream, struct snd_soc_dapm_widget_list **list_)
1235{
1236 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1237 struct snd_soc_dapm_widget_list *list;
1238 int paths;
1239
1240 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1241 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1242 if (list == NULL)
1243 return -ENOMEM;
1244
1245 /* get number of valid DAI paths and their widgets */
1246 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1247
Liam Girdwood103d84a2012-11-19 14:39:15 +00001248 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
Liam Girdwood01d75842012-04-25 12:12:49 +01001249 stream ? "capture" : "playback");
1250
1251 *list_ = list;
1252 return paths;
1253}
1254
Liam Girdwood01d75842012-04-25 12:12:49 +01001255static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1256 struct snd_soc_dapm_widget_list **list_)
1257{
1258 struct snd_soc_dpcm *dpcm;
1259 struct snd_soc_dapm_widget_list *list = *list_;
1260 struct snd_soc_dapm_widget *widget;
1261 int prune = 0;
1262
1263 /* Destroy any old FE <--> BE connections */
1264 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001265 unsigned int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01001266
1267 /* is there a valid CPU DAI widget for this BE */
Benoit Cousson37018612014-04-24 14:01:45 +02001268 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001269
1270 /* prune the BE if it's no longer in our active list */
1271 if (widget && widget_in_list(list, widget))
1272 continue;
1273
1274 /* is there a valid CODEC DAI widget for this BE */
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001275 for (i = 0; i < dpcm->be->num_codecs; i++) {
1276 struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
1277 widget = dai_get_widget(dai, stream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001278
Benoit Cousson2e5894d2014-07-08 23:19:35 +02001279 /* prune the BE if it's no longer in our active list */
1280 if (widget && widget_in_list(list, widget))
1281 continue;
1282 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001283
Liam Girdwood103d84a2012-11-19 14:39:15 +00001284 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001285 stream ? "capture" : "playback",
1286 dpcm->be->dai_link->name, fe->dai_link->name);
1287 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1288 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1289 prune++;
1290 }
1291
Liam Girdwood103d84a2012-11-19 14:39:15 +00001292 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
Liam Girdwood01d75842012-04-25 12:12:49 +01001293 return prune;
1294}
1295
1296static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1297 struct snd_soc_dapm_widget_list **list_)
1298{
1299 struct snd_soc_card *card = fe->card;
1300 struct snd_soc_dapm_widget_list *list = *list_;
1301 struct snd_soc_pcm_runtime *be;
1302 int i, new = 0, err;
1303
1304 /* Create any new FE <--> BE connections */
1305 for (i = 0; i < list->num_widgets; i++) {
1306
Mark Brown46162742013-06-05 19:36:11 +01001307 switch (list->widgets[i]->id) {
1308 case snd_soc_dapm_dai_in:
Koro Chenc5b85402015-07-06 10:02:10 +08001309 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1310 continue;
1311 break;
Mark Brown46162742013-06-05 19:36:11 +01001312 case snd_soc_dapm_dai_out:
Koro Chenc5b85402015-07-06 10:02:10 +08001313 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1314 continue;
Mark Brown46162742013-06-05 19:36:11 +01001315 break;
1316 default:
Liam Girdwood01d75842012-04-25 12:12:49 +01001317 continue;
Mark Brown46162742013-06-05 19:36:11 +01001318 }
Liam Girdwood01d75842012-04-25 12:12:49 +01001319
1320 /* is there a valid BE rtd for this widget */
1321 be = dpcm_get_be(card, list->widgets[i], stream);
1322 if (!be) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001323 dev_err(fe->dev, "ASoC: no BE found for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001324 list->widgets[i]->name);
1325 continue;
1326 }
1327
1328 /* make sure BE is a real BE */
1329 if (!be->dai_link->no_pcm)
1330 continue;
1331
1332 /* don't connect if FE is not running */
Liam Girdwood23607022014-01-17 17:03:55 +00001333 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
Liam Girdwood01d75842012-04-25 12:12:49 +01001334 continue;
1335
1336 /* newly connected FE and BE */
1337 err = dpcm_be_connect(fe, be, stream);
1338 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001339 dev_err(fe->dev, "ASoC: can't connect %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001340 list->widgets[i]->name);
1341 break;
1342 } else if (err == 0) /* already connected */
1343 continue;
1344
1345 /* new */
1346 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1347 new++;
1348 }
1349
Liam Girdwood103d84a2012-11-19 14:39:15 +00001350 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
Liam Girdwood01d75842012-04-25 12:12:49 +01001351 return new;
1352}
1353
1354/*
1355 * Find the corresponding BE DAIs that source or sink audio to this
1356 * FE substream.
1357 */
Liam Girdwood23607022014-01-17 17:03:55 +00001358int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
Liam Girdwood01d75842012-04-25 12:12:49 +01001359 int stream, struct snd_soc_dapm_widget_list **list, int new)
1360{
1361 if (new)
1362 return dpcm_add_paths(fe, stream, list);
1363 else
1364 return dpcm_prune_paths(fe, stream, list);
1365}
1366
Liam Girdwood23607022014-01-17 17:03:55 +00001367void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001368{
1369 struct snd_soc_dpcm *dpcm;
1370
1371 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1372 dpcm->be->dpcm[stream].runtime_update =
1373 SND_SOC_DPCM_UPDATE_NO;
1374}
1375
1376static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1377 int stream)
1378{
1379 struct snd_soc_dpcm *dpcm;
1380
1381 /* disable any enabled and non active backends */
1382 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1383
1384 struct snd_soc_pcm_runtime *be = dpcm->be;
1385 struct snd_pcm_substream *be_substream =
1386 snd_soc_dpcm_get_substream(be, stream);
1387
1388 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001389 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001390 stream ? "capture" : "playback",
1391 be->dpcm[stream].state);
1392
1393 if (--be->dpcm[stream].users != 0)
1394 continue;
1395
1396 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1397 continue;
1398
1399 soc_pcm_close(be_substream);
1400 be_substream->runtime = NULL;
1401 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1402 }
1403}
1404
Liam Girdwood23607022014-01-17 17:03:55 +00001405int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001406{
1407 struct snd_soc_dpcm *dpcm;
1408 int err, count = 0;
1409
1410 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1411 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1412
1413 struct snd_soc_pcm_runtime *be = dpcm->be;
1414 struct snd_pcm_substream *be_substream =
1415 snd_soc_dpcm_get_substream(be, stream);
1416
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001417 if (!be_substream) {
1418 dev_err(be->dev, "ASoC: no backend %s stream\n",
1419 stream ? "capture" : "playback");
1420 continue;
1421 }
1422
Liam Girdwood01d75842012-04-25 12:12:49 +01001423 /* is this op for this BE ? */
1424 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1425 continue;
1426
1427 /* first time the dpcm is open ? */
1428 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001429 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001430 stream ? "capture" : "playback",
1431 be->dpcm[stream].state);
1432
1433 if (be->dpcm[stream].users++ != 0)
1434 continue;
1435
1436 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1437 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1438 continue;
1439
Russell King - ARM Linux2062b4c2013-10-31 15:09:20 +00001440 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1441 stream ? "capture" : "playback", be->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001442
1443 be_substream->runtime = be->dpcm[stream].runtime;
1444 err = soc_pcm_open(be_substream);
1445 if (err < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001446 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
Liam Girdwood01d75842012-04-25 12:12:49 +01001447 be->dpcm[stream].users--;
1448 if (be->dpcm[stream].users < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001449 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001450 stream ? "capture" : "playback",
1451 be->dpcm[stream].state);
1452
1453 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1454 goto unwind;
1455 }
1456
1457 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1458 count++;
1459 }
1460
1461 return count;
1462
1463unwind:
1464 /* disable any enabled and non active backends */
1465 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1466 struct snd_soc_pcm_runtime *be = dpcm->be;
1467 struct snd_pcm_substream *be_substream =
1468 snd_soc_dpcm_get_substream(be, stream);
1469
1470 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1471 continue;
1472
1473 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001474 dev_err(be->dev, "ASoC: no users %s at close %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001475 stream ? "capture" : "playback",
1476 be->dpcm[stream].state);
1477
1478 if (--be->dpcm[stream].users != 0)
1479 continue;
1480
1481 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1482 continue;
1483
1484 soc_pcm_close(be_substream);
1485 be_substream->runtime = NULL;
1486 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1487 }
1488
1489 return err;
1490}
1491
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001492static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001493 struct snd_soc_pcm_stream *stream,
1494 u64 formats)
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001495{
1496 runtime->hw.rate_min = stream->rate_min;
1497 runtime->hw.rate_max = stream->rate_max;
1498 runtime->hw.channels_min = stream->channels_min;
1499 runtime->hw.channels_max = stream->channels_max;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001500 if (runtime->hw.formats)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001501 runtime->hw.formats &= formats & stream->formats;
Lars-Peter Clausen002220a2014-01-06 14:19:07 +01001502 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001503 runtime->hw.formats = formats & stream->formats;
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001504 runtime->hw.rates = stream->rates;
1505}
1506
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001507static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
1508{
1509 struct snd_soc_pcm_runtime *fe = substream->private_data;
1510 struct snd_soc_dpcm *dpcm;
1511 u64 formats = ULLONG_MAX;
1512 int stream = substream->stream;
1513
1514 if (!fe->dai_link->dpcm_merged_format)
1515 return formats;
1516
1517 /*
1518 * It returns merged BE codec format
1519 * if FE want to use it (= dpcm_merged_format)
1520 */
1521
1522 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1523 struct snd_soc_pcm_runtime *be = dpcm->be;
1524 struct snd_soc_dai_driver *codec_dai_drv;
1525 struct snd_soc_pcm_stream *codec_stream;
1526 int i;
1527
1528 for (i = 0; i < be->num_codecs; i++) {
1529 codec_dai_drv = be->codec_dais[i]->driver;
1530 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1531 codec_stream = &codec_dai_drv->playback;
1532 else
1533 codec_stream = &codec_dai_drv->capture;
1534
1535 formats &= codec_stream->formats;
1536 }
1537 }
1538
1539 return formats;
1540}
1541
Mark Brown45c0a182012-05-09 21:46:27 +01001542static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001543{
1544 struct snd_pcm_runtime *runtime = substream->runtime;
1545 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1546 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1547 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001548 u64 format = dpcm_runtime_base_format(substream);
Liam Girdwood01d75842012-04-25 12:12:49 +01001549
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001550 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001551 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
Lars-Peter Clausen08ae9b42014-01-06 14:19:06 +01001552 else
Kuninori Morimotob073ed42015-05-12 02:03:33 +00001553 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
Liam Girdwood01d75842012-04-25 12:12:49 +01001554}
1555
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001556static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1557
1558/* Set FE's runtime_update state; the state is protected via PCM stream lock
1559 * for avoiding the race with trigger callback.
1560 * If the state is unset and a trigger is pending while the previous operation,
1561 * process the pending trigger action here.
1562 */
1563static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1564 int stream, enum snd_soc_dpcm_update state)
1565{
1566 struct snd_pcm_substream *substream =
1567 snd_soc_dpcm_get_substream(fe, stream);
1568
1569 snd_pcm_stream_lock_irq(substream);
1570 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1571 dpcm_fe_dai_do_trigger(substream,
1572 fe->dpcm[stream].trigger_pending - 1);
1573 fe->dpcm[stream].trigger_pending = 0;
1574 }
1575 fe->dpcm[stream].runtime_update = state;
1576 snd_pcm_stream_unlock_irq(substream);
1577}
1578
Liam Girdwood01d75842012-04-25 12:12:49 +01001579static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1580{
1581 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1582 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1583 int stream = fe_substream->stream, ret = 0;
1584
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001585 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001586
1587 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1588 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001589 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001590 goto be_err;
1591 }
1592
Liam Girdwood103d84a2012-11-19 14:39:15 +00001593 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001594
1595 /* start the DAI frontend */
1596 ret = soc_pcm_open(fe_substream);
1597 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001598 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001599 goto unwind;
1600 }
1601
1602 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1603
1604 dpcm_set_fe_runtime(fe_substream);
1605 snd_pcm_limit_hw_rates(runtime);
1606
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001607 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001608 return 0;
1609
1610unwind:
1611 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1612be_err:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001613 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001614 return ret;
1615}
1616
Liam Girdwood23607022014-01-17 17:03:55 +00001617int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001618{
1619 struct snd_soc_dpcm *dpcm;
1620
1621 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1622 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1623
1624 struct snd_soc_pcm_runtime *be = dpcm->be;
1625 struct snd_pcm_substream *be_substream =
1626 snd_soc_dpcm_get_substream(be, stream);
1627
1628 /* is this op for this BE ? */
1629 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1630 continue;
1631
1632 if (be->dpcm[stream].users == 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001633 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001634 stream ? "capture" : "playback",
1635 be->dpcm[stream].state);
1636
1637 if (--be->dpcm[stream].users != 0)
1638 continue;
1639
1640 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1641 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1642 continue;
1643
Liam Girdwood103d84a2012-11-19 14:39:15 +00001644 dev_dbg(be->dev, "ASoC: close BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001645 dpcm->fe->dai_link->name);
1646
1647 soc_pcm_close(be_substream);
1648 be_substream->runtime = NULL;
1649
1650 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1651 }
1652 return 0;
1653}
1654
1655static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1656{
1657 struct snd_soc_pcm_runtime *fe = substream->private_data;
1658 int stream = substream->stream;
1659
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001660 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001661
1662 /* shutdown the BEs */
1663 dpcm_be_dai_shutdown(fe, substream->stream);
1664
Liam Girdwood103d84a2012-11-19 14:39:15 +00001665 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001666
1667 /* now shutdown the frontend */
1668 soc_pcm_close(substream);
1669
1670 /* run the stream event for each BE */
1671 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1672
1673 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001674 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001675 return 0;
1676}
1677
Liam Girdwood23607022014-01-17 17:03:55 +00001678int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001679{
1680 struct snd_soc_dpcm *dpcm;
1681
1682 /* only hw_params backends that are either sinks or sources
1683 * to this frontend DAI */
1684 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1685
1686 struct snd_soc_pcm_runtime *be = dpcm->be;
1687 struct snd_pcm_substream *be_substream =
1688 snd_soc_dpcm_get_substream(be, stream);
1689
1690 /* is this op for this BE ? */
1691 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1692 continue;
1693
1694 /* only free hw when no longer used - check all FEs */
1695 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1696 continue;
1697
Qiao Zhou36fba622014-12-03 10:13:43 +08001698 /* do not free hw if this BE is used by other FE */
1699 if (be->dpcm[stream].users > 1)
1700 continue;
1701
Liam Girdwood01d75842012-04-25 12:12:49 +01001702 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1703 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1704 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
Patrick Lai08b27842012-12-19 19:36:02 -08001705 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
Liam Girdwood01d75842012-04-25 12:12:49 +01001706 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1707 continue;
1708
Liam Girdwood103d84a2012-11-19 14:39:15 +00001709 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001710 dpcm->fe->dai_link->name);
1711
1712 soc_pcm_hw_free(be_substream);
1713
1714 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1715 }
1716
1717 return 0;
1718}
1719
Mark Brown45c0a182012-05-09 21:46:27 +01001720static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001721{
1722 struct snd_soc_pcm_runtime *fe = substream->private_data;
1723 int err, stream = substream->stream;
1724
1725 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001726 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001727
Liam Girdwood103d84a2012-11-19 14:39:15 +00001728 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01001729
1730 /* call hw_free on the frontend */
1731 err = soc_pcm_hw_free(substream);
1732 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001733 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001734 fe->dai_link->name);
1735
1736 /* only hw_params backends that are either sinks or sources
1737 * to this frontend DAI */
1738 err = dpcm_be_dai_hw_free(fe, stream);
1739
1740 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001741 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001742
1743 mutex_unlock(&fe->card->mutex);
1744 return 0;
1745}
1746
Liam Girdwood23607022014-01-17 17:03:55 +00001747int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01001748{
1749 struct snd_soc_dpcm *dpcm;
1750 int ret;
1751
1752 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1753
1754 struct snd_soc_pcm_runtime *be = dpcm->be;
1755 struct snd_pcm_substream *be_substream =
1756 snd_soc_dpcm_get_substream(be, stream);
1757
1758 /* is this op for this BE ? */
1759 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1760 continue;
1761
1762 /* only allow hw_params() if no connected FEs are running */
1763 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1764 continue;
1765
1766 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1767 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1768 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1769 continue;
1770
Liam Girdwood103d84a2012-11-19 14:39:15 +00001771 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001772 dpcm->fe->dai_link->name);
1773
1774 /* copy params for each dpcm */
1775 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1776 sizeof(struct snd_pcm_hw_params));
1777
1778 /* perform any hw_params fixups */
1779 if (be->dai_link->be_hw_params_fixup) {
1780 ret = be->dai_link->be_hw_params_fixup(be,
1781 &dpcm->hw_params);
1782 if (ret < 0) {
1783 dev_err(be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001784 "ASoC: hw_params BE fixup failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001785 ret);
1786 goto unwind;
1787 }
1788 }
1789
1790 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1791 if (ret < 0) {
1792 dev_err(dpcm->be->dev,
Liam Girdwood103d84a2012-11-19 14:39:15 +00001793 "ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001794 goto unwind;
1795 }
1796
1797 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1798 }
1799 return 0;
1800
1801unwind:
1802 /* disable any enabled and non active backends */
1803 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1804 struct snd_soc_pcm_runtime *be = dpcm->be;
1805 struct snd_pcm_substream *be_substream =
1806 snd_soc_dpcm_get_substream(be, stream);
1807
1808 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1809 continue;
1810
1811 /* only allow hw_free() if no connected FEs are running */
1812 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1813 continue;
1814
1815 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1816 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1817 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1818 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1819 continue;
1820
1821 soc_pcm_hw_free(be_substream);
1822 }
1823
1824 return ret;
1825}
1826
Mark Brown45c0a182012-05-09 21:46:27 +01001827static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1828 struct snd_pcm_hw_params *params)
Liam Girdwood01d75842012-04-25 12:12:49 +01001829{
1830 struct snd_soc_pcm_runtime *fe = substream->private_data;
1831 int ret, stream = substream->stream;
1832
1833 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001834 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01001835
1836 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1837 sizeof(struct snd_pcm_hw_params));
1838 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1839 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001840 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001841 goto out;
1842 }
1843
Liam Girdwood103d84a2012-11-19 14:39:15 +00001844 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001845 fe->dai_link->name, params_rate(params),
1846 params_channels(params), params_format(params));
1847
1848 /* call hw_params on the frontend */
1849 ret = soc_pcm_hw_params(substream, params);
1850 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001851 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001852 dpcm_be_dai_hw_free(fe, stream);
1853 } else
1854 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1855
1856out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001857 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01001858 mutex_unlock(&fe->card->mutex);
1859 return ret;
1860}
1861
1862static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1863 struct snd_pcm_substream *substream, int cmd)
1864{
1865 int ret;
1866
Liam Girdwood103d84a2012-11-19 14:39:15 +00001867 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001868 dpcm->fe->dai_link->name, cmd);
1869
1870 ret = soc_pcm_trigger(substream, cmd);
1871 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00001872 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001873
1874 return ret;
1875}
1876
Liam Girdwood23607022014-01-17 17:03:55 +00001877int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
Mark Brown45c0a182012-05-09 21:46:27 +01001878 int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001879{
1880 struct snd_soc_dpcm *dpcm;
1881 int ret = 0;
1882
1883 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1884
1885 struct snd_soc_pcm_runtime *be = dpcm->be;
1886 struct snd_pcm_substream *be_substream =
1887 snd_soc_dpcm_get_substream(be, stream);
1888
1889 /* is this op for this BE ? */
1890 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1891 continue;
1892
1893 switch (cmd) {
1894 case SNDRV_PCM_TRIGGER_START:
1895 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1896 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1897 continue;
1898
1899 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1900 if (ret)
1901 return ret;
1902
1903 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1904 break;
1905 case SNDRV_PCM_TRIGGER_RESUME:
1906 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1907 continue;
1908
1909 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1910 if (ret)
1911 return ret;
1912
1913 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1914 break;
1915 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1916 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1917 continue;
1918
1919 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1920 if (ret)
1921 return ret;
1922
1923 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1924 break;
1925 case SNDRV_PCM_TRIGGER_STOP:
1926 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1927 continue;
1928
1929 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1930 continue;
1931
1932 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1933 if (ret)
1934 return ret;
1935
1936 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1937 break;
1938 case SNDRV_PCM_TRIGGER_SUSPEND:
Nicolin Chen868a6ca2014-05-12 20:12:05 +08001939 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
Liam Girdwood01d75842012-04-25 12:12:49 +01001940 continue;
1941
1942 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1943 continue;
1944
1945 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1946 if (ret)
1947 return ret;
1948
1949 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1950 break;
1951 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1952 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1953 continue;
1954
1955 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1956 continue;
1957
1958 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1959 if (ret)
1960 return ret;
1961
1962 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1963 break;
1964 }
1965 }
1966
1967 return ret;
1968}
1969EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1970
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01001971static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
Liam Girdwood01d75842012-04-25 12:12:49 +01001972{
1973 struct snd_soc_pcm_runtime *fe = substream->private_data;
1974 int stream = substream->stream, ret;
1975 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1976
1977 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1978
1979 switch (trigger) {
1980 case SND_SOC_DPCM_TRIGGER_PRE:
1981 /* call trigger on the frontend before the backend. */
1982
Liam Girdwood103d84a2012-11-19 14:39:15 +00001983 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01001984 fe->dai_link->name, cmd);
1985
1986 ret = soc_pcm_trigger(substream, cmd);
1987 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001988 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01001989 goto out;
1990 }
1991
1992 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1993 break;
1994 case SND_SOC_DPCM_TRIGGER_POST:
1995 /* call trigger on the frontend after the backend. */
1996
1997 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1998 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00001999 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood01d75842012-04-25 12:12:49 +01002000 goto out;
2001 }
2002
Liam Girdwood103d84a2012-11-19 14:39:15 +00002003 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002004 fe->dai_link->name, cmd);
2005
2006 ret = soc_pcm_trigger(substream, cmd);
2007 break;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002008 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2009 /* bespoke trigger() - handles both FE and BEs */
2010
Liam Girdwood103d84a2012-11-19 14:39:15 +00002011 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002012 fe->dai_link->name, cmd);
2013
2014 ret = soc_pcm_bespoke_trigger(substream, cmd);
2015 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002016 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002017 goto out;
2018 }
2019 break;
Liam Girdwood01d75842012-04-25 12:12:49 +01002020 default:
Liam Girdwood103d84a2012-11-19 14:39:15 +00002021 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
Liam Girdwood01d75842012-04-25 12:12:49 +01002022 fe->dai_link->name);
2023 ret = -EINVAL;
2024 goto out;
2025 }
2026
2027 switch (cmd) {
2028 case SNDRV_PCM_TRIGGER_START:
2029 case SNDRV_PCM_TRIGGER_RESUME:
2030 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2031 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2032 break;
2033 case SNDRV_PCM_TRIGGER_STOP:
2034 case SNDRV_PCM_TRIGGER_SUSPEND:
2035 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2036 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2037 break;
2038 }
2039
2040out:
2041 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2042 return ret;
2043}
2044
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002045static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2046{
2047 struct snd_soc_pcm_runtime *fe = substream->private_data;
2048 int stream = substream->stream;
2049
2050 /* if FE's runtime_update is already set, we're in race;
2051 * process this trigger later at exit
2052 */
2053 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2054 fe->dpcm[stream].trigger_pending = cmd + 1;
2055 return 0; /* delayed, assuming it's successful */
2056 }
2057
2058 /* we're alone, let's trigger */
2059 return dpcm_fe_dai_do_trigger(substream, cmd);
2060}
2061
Liam Girdwood23607022014-01-17 17:03:55 +00002062int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002063{
2064 struct snd_soc_dpcm *dpcm;
2065 int ret = 0;
2066
2067 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2068
2069 struct snd_soc_pcm_runtime *be = dpcm->be;
2070 struct snd_pcm_substream *be_substream =
2071 snd_soc_dpcm_get_substream(be, stream);
2072
2073 /* is this op for this BE ? */
2074 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2075 continue;
2076
2077 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2078 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2079 continue;
2080
Liam Girdwood103d84a2012-11-19 14:39:15 +00002081 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002082 dpcm->fe->dai_link->name);
2083
2084 ret = soc_pcm_prepare(be_substream);
2085 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002086 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002087 ret);
2088 break;
2089 }
2090
2091 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2092 }
2093 return ret;
2094}
2095
Mark Brown45c0a182012-05-09 21:46:27 +01002096static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002097{
2098 struct snd_soc_pcm_runtime *fe = substream->private_data;
2099 int stream = substream->stream, ret = 0;
2100
2101 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2102
Liam Girdwood103d84a2012-11-19 14:39:15 +00002103 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
Liam Girdwood01d75842012-04-25 12:12:49 +01002104
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002105 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
Liam Girdwood01d75842012-04-25 12:12:49 +01002106
2107 /* there is no point preparing this FE if there are no BEs */
2108 if (list_empty(&fe->dpcm[stream].be_clients)) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002109 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002110 fe->dai_link->name);
2111 ret = -EINVAL;
2112 goto out;
2113 }
2114
2115 ret = dpcm_be_dai_prepare(fe, substream->stream);
2116 if (ret < 0)
2117 goto out;
2118
2119 /* call prepare on the frontend */
2120 ret = soc_pcm_prepare(substream);
2121 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002122 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002123 fe->dai_link->name);
2124 goto out;
2125 }
2126
2127 /* run the stream event for each BE */
2128 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2129 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2130
2131out:
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002132 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood01d75842012-04-25 12:12:49 +01002133 mutex_unlock(&fe->card->mutex);
2134
2135 return ret;
2136}
2137
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002138static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2139 unsigned int cmd, void *arg)
2140{
2141 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2142 struct snd_soc_platform *platform = rtd->platform;
2143
Mark Brownc5914b02013-10-30 17:47:39 -07002144 if (platform->driver->ops && platform->driver->ops->ioctl)
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002145 return platform->driver->ops->ioctl(substream, cmd, arg);
2146 return snd_pcm_lib_ioctl(substream, cmd, arg);
2147}
2148
Liam Girdwood618dae12012-04-25 12:12:51 +01002149static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2150{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002151 struct snd_pcm_substream *substream =
2152 snd_soc_dpcm_get_substream(fe, stream);
2153 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002154 int err;
Liam Girdwood01d75842012-04-25 12:12:49 +01002155
Liam Girdwood103d84a2012-11-19 14:39:15 +00002156 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002157 stream ? "capture" : "playback", fe->dai_link->name);
2158
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002159 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2160 /* call bespoke trigger - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002161 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002162 fe->dai_link->name);
2163
2164 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2165 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002166 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002167 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002168 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002169 fe->dai_link->name);
2170
2171 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2172 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002173 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002174 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002175
2176 err = dpcm_be_dai_hw_free(fe, stream);
2177 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002178 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002179
2180 err = dpcm_be_dai_shutdown(fe, stream);
2181 if (err < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002182 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
Liam Girdwood618dae12012-04-25 12:12:51 +01002183
2184 /* run the stream event for each BE */
2185 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2186
2187 return 0;
2188}
2189
2190static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2191{
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002192 struct snd_pcm_substream *substream =
2193 snd_soc_dpcm_get_substream(fe, stream);
Liam Girdwood618dae12012-04-25 12:12:51 +01002194 struct snd_soc_dpcm *dpcm;
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002195 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
Liam Girdwood618dae12012-04-25 12:12:51 +01002196 int ret;
2197
Liam Girdwood103d84a2012-11-19 14:39:15 +00002198 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002199 stream ? "capture" : "playback", fe->dai_link->name);
2200
2201 /* Only start the BE if the FE is ready */
2202 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2203 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2204 return -EINVAL;
2205
2206 /* startup must always be called for new BEs */
2207 ret = dpcm_be_dai_startup(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002208 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002209 goto disconnect;
Liam Girdwood618dae12012-04-25 12:12:51 +01002210
2211 /* keep going if FE state is > open */
2212 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2213 return 0;
2214
2215 ret = dpcm_be_dai_hw_params(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002216 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002217 goto close;
Liam Girdwood618dae12012-04-25 12:12:51 +01002218
2219 /* keep going if FE state is > hw_params */
2220 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2221 return 0;
2222
2223
2224 ret = dpcm_be_dai_prepare(fe, stream);
Dan Carpenterfffc0ca2013-01-10 11:59:57 +03002225 if (ret < 0)
Liam Girdwood618dae12012-04-25 12:12:51 +01002226 goto hw_free;
Liam Girdwood618dae12012-04-25 12:12:51 +01002227
2228 /* run the stream event for each BE */
2229 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2230
2231 /* keep going if FE state is > prepare */
2232 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2233 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2234 return 0;
2235
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002236 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2237 /* call trigger on the frontend - FE takes care of all BE triggers */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002238 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002239 fe->dai_link->name);
Liam Girdwood618dae12012-04-25 12:12:51 +01002240
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002241 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2242 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002243 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002244 goto hw_free;
2245 }
2246 } else {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002247 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002248 fe->dai_link->name);
2249
2250 ret = dpcm_be_dai_trigger(fe, stream,
2251 SNDRV_PCM_TRIGGER_START);
2252 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002253 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002254 goto hw_free;
2255 }
Liam Girdwood618dae12012-04-25 12:12:51 +01002256 }
2257
2258 return 0;
2259
2260hw_free:
2261 dpcm_be_dai_hw_free(fe, stream);
2262close:
2263 dpcm_be_dai_shutdown(fe, stream);
2264disconnect:
2265 /* disconnect any non started BEs */
2266 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2267 struct snd_soc_pcm_runtime *be = dpcm->be;
2268 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2269 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2270 }
2271
2272 return ret;
2273}
2274
2275static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2276{
2277 int ret;
2278
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002279 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002280 ret = dpcm_run_update_startup(fe, stream);
2281 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002282 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002283 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002284
2285 return ret;
2286}
2287
2288static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2289{
2290 int ret;
2291
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002292 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
Liam Girdwood618dae12012-04-25 12:12:51 +01002293 ret = dpcm_run_update_shutdown(fe, stream);
2294 if (ret < 0)
Liam Girdwood103d84a2012-11-19 14:39:15 +00002295 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
Takashi Iwaiea9d0d72014-11-04 16:52:28 +01002296 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
Liam Girdwood618dae12012-04-25 12:12:51 +01002297
2298 return ret;
2299}
2300
2301/* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2302 * any DAI links.
2303 */
Lars-Peter Clausenc3f48ae2013-07-24 15:27:36 +02002304int soc_dpcm_runtime_update(struct snd_soc_card *card)
Liam Girdwood618dae12012-04-25 12:12:51 +01002305{
Liam Girdwood618dae12012-04-25 12:12:51 +01002306 int i, old, new, paths;
2307
Liam Girdwood618dae12012-04-25 12:12:51 +01002308 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2309 for (i = 0; i < card->num_rtd; i++) {
2310 struct snd_soc_dapm_widget_list *list;
2311 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2312
2313 /* make sure link is FE */
2314 if (!fe->dai_link->dynamic)
2315 continue;
2316
2317 /* only check active links */
2318 if (!fe->cpu_dai->active)
2319 continue;
2320
2321 /* DAPM sync will call this to update DSP paths */
Liam Girdwood103d84a2012-11-19 14:39:15 +00002322 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002323 fe->dai_link->name);
2324
2325 /* skip if FE doesn't have playback capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002326 if (!fe->cpu_dai->driver->playback.channels_min
2327 || !fe->codec_dai->driver->playback.channels_min)
2328 goto capture;
2329
2330 /* skip if FE isn't currently playing */
2331 if (!fe->cpu_dai->playback_active
2332 || !fe->codec_dai->playback_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002333 goto capture;
2334
2335 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2336 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002337 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002338 fe->dai_link->name, "playback");
2339 mutex_unlock(&card->mutex);
2340 return paths;
2341 }
2342
2343 /* update any new playback paths */
2344 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2345 if (new) {
2346 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2347 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2348 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2349 }
2350
2351 /* update any old playback paths */
2352 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2353 if (old) {
2354 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2355 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2356 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2357 }
2358
Qiao Zhou7ed9de72014-06-04 19:42:06 +08002359 dpcm_path_put(&list);
Liam Girdwood618dae12012-04-25 12:12:51 +01002360capture:
2361 /* skip if FE doesn't have capture capability */
Qiao Zhou075207d2014-11-17 16:02:57 +08002362 if (!fe->cpu_dai->driver->capture.channels_min
2363 || !fe->codec_dai->driver->capture.channels_min)
2364 continue;
2365
2366 /* skip if FE isn't currently capturing */
2367 if (!fe->cpu_dai->capture_active
2368 || !fe->codec_dai->capture_active)
Liam Girdwood618dae12012-04-25 12:12:51 +01002369 continue;
2370
2371 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2372 if (paths < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002373 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
Liam Girdwood618dae12012-04-25 12:12:51 +01002374 fe->dai_link->name, "capture");
2375 mutex_unlock(&card->mutex);
2376 return paths;
2377 }
2378
2379 /* update any new capture paths */
2380 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2381 if (new) {
2382 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2383 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2384 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2385 }
2386
2387 /* update any old capture paths */
2388 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2389 if (old) {
2390 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2391 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2392 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2393 }
2394
2395 dpcm_path_put(&list);
2396 }
2397
2398 mutex_unlock(&card->mutex);
2399 return 0;
2400}
Liam Girdwood01d75842012-04-25 12:12:49 +01002401int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2402{
2403 struct snd_soc_dpcm *dpcm;
2404 struct list_head *clients =
2405 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2406
2407 list_for_each_entry(dpcm, clients, list_be) {
2408
2409 struct snd_soc_pcm_runtime *be = dpcm->be;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002410 int i;
Liam Girdwood01d75842012-04-25 12:12:49 +01002411
2412 if (be->dai_link->ignore_suspend)
2413 continue;
2414
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002415 for (i = 0; i < be->num_codecs; i++) {
2416 struct snd_soc_dai *dai = be->codec_dais[i];
2417 struct snd_soc_dai_driver *drv = dai->driver;
Liam Girdwood01d75842012-04-25 12:12:49 +01002418
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002419 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2420 be->dai_link->name);
2421
2422 if (drv->ops && drv->ops->digital_mute &&
2423 dai->playback_active)
2424 drv->ops->digital_mute(dai, mute);
2425 }
Liam Girdwood01d75842012-04-25 12:12:49 +01002426 }
2427
2428 return 0;
2429}
2430
Mark Brown45c0a182012-05-09 21:46:27 +01002431static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002432{
2433 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2434 struct snd_soc_dpcm *dpcm;
2435 struct snd_soc_dapm_widget_list *list;
2436 int ret;
2437 int stream = fe_substream->stream;
2438
2439 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2440 fe->dpcm[stream].runtime = fe_substream->runtime;
2441
Qiao Zhou8f70e512014-09-10 17:54:07 +08002442 ret = dpcm_path_get(fe, stream, &list);
2443 if (ret < 0) {
2444 mutex_unlock(&fe->card->mutex);
2445 return ret;
2446 } else if (ret == 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002447 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
Liam Girdwood01d75842012-04-25 12:12:49 +01002448 fe->dai_link->name, stream ? "capture" : "playback");
Liam Girdwood01d75842012-04-25 12:12:49 +01002449 }
2450
2451 /* calculate valid and active FE <-> BE dpcms */
2452 dpcm_process_paths(fe, stream, &list, 1);
2453
2454 ret = dpcm_fe_dai_startup(fe_substream);
2455 if (ret < 0) {
2456 /* clean up all links */
2457 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2458 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2459
2460 dpcm_be_disconnect(fe, stream);
2461 fe->dpcm[stream].runtime = NULL;
2462 }
2463
2464 dpcm_clear_pending_state(fe, stream);
2465 dpcm_path_put(&list);
2466 mutex_unlock(&fe->card->mutex);
2467 return ret;
2468}
2469
Mark Brown45c0a182012-05-09 21:46:27 +01002470static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
Liam Girdwood01d75842012-04-25 12:12:49 +01002471{
2472 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2473 struct snd_soc_dpcm *dpcm;
2474 int stream = fe_substream->stream, ret;
2475
2476 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2477 ret = dpcm_fe_dai_shutdown(fe_substream);
2478
2479 /* mark FE's links ready to prune */
2480 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2481 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2482
2483 dpcm_be_disconnect(fe, stream);
2484
2485 fe->dpcm[stream].runtime = NULL;
2486 mutex_unlock(&fe->card->mutex);
2487 return ret;
2488}
2489
Liam Girdwoodddee6272011-06-09 14:45:53 +01002490/* create a new pcm */
2491int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2492{
Liam Girdwoodddee6272011-06-09 14:45:53 +01002493 struct snd_soc_platform *platform = rtd->platform;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002494 struct snd_soc_dai *codec_dai;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002495 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2496 struct snd_pcm *pcm;
2497 char new_name[64];
2498 int ret = 0, playback = 0, capture = 0;
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002499 int i;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002500
Liam Girdwood01d75842012-04-25 12:12:49 +01002501 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
Liam Girdwood1e9de422014-01-07 17:51:42 +00002502 playback = rtd->dai_link->dpcm_playback;
2503 capture = rtd->dai_link->dpcm_capture;
Liam Girdwood01d75842012-04-25 12:12:49 +01002504 } else {
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002505 for (i = 0; i < rtd->num_codecs; i++) {
2506 codec_dai = rtd->codec_dais[i];
2507 if (codec_dai->driver->playback.channels_min)
2508 playback = 1;
2509 if (codec_dai->driver->capture.channels_min)
2510 capture = 1;
2511 }
2512
2513 capture = capture && cpu_dai->driver->capture.channels_min;
2514 playback = playback && cpu_dai->driver->playback.channels_min;
Liam Girdwood01d75842012-04-25 12:12:49 +01002515 }
Sangsu Parka5002312012-01-02 17:15:10 +09002516
Fabio Estevamd6bead02013-08-29 10:32:13 -03002517 if (rtd->dai_link->playback_only) {
2518 playback = 1;
2519 capture = 0;
2520 }
2521
2522 if (rtd->dai_link->capture_only) {
2523 playback = 0;
2524 capture = 1;
2525 }
2526
Liam Girdwood01d75842012-04-25 12:12:49 +01002527 /* create the PCM */
2528 if (rtd->dai_link->no_pcm) {
2529 snprintf(new_name, sizeof(new_name), "(%s)",
2530 rtd->dai_link->stream_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002531
Liam Girdwood01d75842012-04-25 12:12:49 +01002532 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2533 playback, capture, &pcm);
2534 } else {
2535 if (rtd->dai_link->dynamic)
2536 snprintf(new_name, sizeof(new_name), "%s (*)",
2537 rtd->dai_link->stream_name);
2538 else
2539 snprintf(new_name, sizeof(new_name), "%s %s-%d",
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002540 rtd->dai_link->stream_name,
2541 (rtd->num_codecs > 1) ?
2542 "multicodec" : rtd->codec_dai->name, num);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002543
Liam Girdwood01d75842012-04-25 12:12:49 +01002544 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2545 capture, &pcm);
2546 }
Liam Girdwoodddee6272011-06-09 14:45:53 +01002547 if (ret < 0) {
Liam Girdwood103d84a2012-11-19 14:39:15 +00002548 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
Liam Girdwood5cb9b742012-07-06 16:54:52 +01002549 rtd->dai_link->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002550 return ret;
2551 }
Liam Girdwood103d84a2012-11-19 14:39:15 +00002552 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002553
2554 /* DAPM dai link stream work */
2555 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2556
Vinod Koul48c76992015-02-12 09:59:53 +05302557 pcm->nonatomic = rtd->dai_link->nonatomic;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002558 rtd->pcm = pcm;
2559 pcm->private_data = rtd;
Liam Girdwood01d75842012-04-25 12:12:49 +01002560
2561 if (rtd->dai_link->no_pcm) {
2562 if (playback)
2563 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2564 if (capture)
2565 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2566 goto out;
2567 }
2568
2569 /* ASoC PCM operations */
2570 if (rtd->dai_link->dynamic) {
2571 rtd->ops.open = dpcm_fe_dai_open;
2572 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2573 rtd->ops.prepare = dpcm_fe_dai_prepare;
2574 rtd->ops.trigger = dpcm_fe_dai_trigger;
2575 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2576 rtd->ops.close = dpcm_fe_dai_close;
2577 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002578 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002579 } else {
2580 rtd->ops.open = soc_pcm_open;
2581 rtd->ops.hw_params = soc_pcm_hw_params;
2582 rtd->ops.prepare = soc_pcm_prepare;
2583 rtd->ops.trigger = soc_pcm_trigger;
2584 rtd->ops.hw_free = soc_pcm_hw_free;
2585 rtd->ops.close = soc_pcm_close;
2586 rtd->ops.pointer = soc_pcm_pointer;
Liam Girdwoodbe3f3f22012-04-26 16:16:10 +01002587 rtd->ops.ioctl = soc_pcm_ioctl;
Liam Girdwood01d75842012-04-25 12:12:49 +01002588 }
2589
Liam Girdwoodddee6272011-06-09 14:45:53 +01002590 if (platform->driver->ops) {
Liam Girdwood01d75842012-04-25 12:12:49 +01002591 rtd->ops.ack = platform->driver->ops->ack;
2592 rtd->ops.copy = platform->driver->ops->copy;
2593 rtd->ops.silence = platform->driver->ops->silence;
2594 rtd->ops.page = platform->driver->ops->page;
2595 rtd->ops.mmap = platform->driver->ops->mmap;
Liam Girdwoodddee6272011-06-09 14:45:53 +01002596 }
2597
2598 if (playback)
Liam Girdwood01d75842012-04-25 12:12:49 +01002599 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002600
2601 if (capture)
Liam Girdwood01d75842012-04-25 12:12:49 +01002602 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002603
2604 if (platform->driver->pcm_new) {
2605 ret = platform->driver->pcm_new(rtd);
2606 if (ret < 0) {
Mark Brownb1bc7b32012-09-26 14:25:17 +01002607 dev_err(platform->dev,
2608 "ASoC: pcm constructor failed: %d\n",
2609 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002610 return ret;
2611 }
2612 }
2613
2614 pcm->private_free = platform->driver->pcm_free;
Liam Girdwood01d75842012-04-25 12:12:49 +01002615out:
Benoit Cousson2e5894d2014-07-08 23:19:35 +02002616 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
2617 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
2618 cpu_dai->name);
Liam Girdwoodddee6272011-06-09 14:45:53 +01002619 return ret;
2620}
Liam Girdwood01d75842012-04-25 12:12:49 +01002621
2622/* is the current PCM operation for this FE ? */
2623int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2624{
2625 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2626 return 1;
2627 return 0;
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2630
2631/* is the current PCM operation for this BE ? */
2632int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2633 struct snd_soc_pcm_runtime *be, int stream)
2634{
2635 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2636 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2637 be->dpcm[stream].runtime_update))
2638 return 1;
2639 return 0;
2640}
2641EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2642
2643/* get the substream for this BE */
2644struct snd_pcm_substream *
2645 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2646{
2647 return be->pcm->streams[stream].substream;
2648}
2649EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2650
2651/* get the BE runtime state */
2652enum snd_soc_dpcm_state
2653 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2654{
2655 return be->dpcm[stream].state;
2656}
2657EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2658
2659/* set the BE runtime state */
2660void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2661 int stream, enum snd_soc_dpcm_state state)
2662{
2663 be->dpcm[stream].state = state;
2664}
2665EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2666
2667/*
2668 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2669 * are not running, paused or suspended for the specified stream direction.
2670 */
2671int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2672 struct snd_soc_pcm_runtime *be, int stream)
2673{
2674 struct snd_soc_dpcm *dpcm;
2675 int state;
2676
2677 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2678
2679 if (dpcm->fe == fe)
2680 continue;
2681
2682 state = dpcm->fe->dpcm[stream].state;
2683 if (state == SND_SOC_DPCM_STATE_START ||
2684 state == SND_SOC_DPCM_STATE_PAUSED ||
2685 state == SND_SOC_DPCM_STATE_SUSPEND)
2686 return 0;
2687 }
2688
2689 /* it's safe to free/stop this BE DAI */
2690 return 1;
2691}
2692EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2693
2694/*
2695 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2696 * running, paused or suspended for the specified stream direction.
2697 */
2698int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2699 struct snd_soc_pcm_runtime *be, int stream)
2700{
2701 struct snd_soc_dpcm *dpcm;
2702 int state;
2703
2704 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2705
2706 if (dpcm->fe == fe)
2707 continue;
2708
2709 state = dpcm->fe->dpcm[stream].state;
2710 if (state == SND_SOC_DPCM_STATE_START ||
2711 state == SND_SOC_DPCM_STATE_PAUSED ||
2712 state == SND_SOC_DPCM_STATE_SUSPEND ||
2713 state == SND_SOC_DPCM_STATE_PREPARE)
2714 return 0;
2715 }
2716
2717 /* it's safe to change hw_params */
2718 return 1;
2719}
2720EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002721
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002722int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2723 int cmd, struct snd_soc_platform *platform)
2724{
Mark Brownc5914b02013-10-30 17:47:39 -07002725 if (platform->driver->ops && platform->driver->ops->trigger)
Liam Girdwood07bf84a2012-04-25 12:12:52 +01002726 return platform->driver->ops->trigger(substream, cmd);
2727 return 0;
2728}
2729EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2730
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002731#ifdef CONFIG_DEBUG_FS
2732static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2733{
2734 switch (state) {
2735 case SND_SOC_DPCM_STATE_NEW:
2736 return "new";
2737 case SND_SOC_DPCM_STATE_OPEN:
2738 return "open";
2739 case SND_SOC_DPCM_STATE_HW_PARAMS:
2740 return "hw_params";
2741 case SND_SOC_DPCM_STATE_PREPARE:
2742 return "prepare";
2743 case SND_SOC_DPCM_STATE_START:
2744 return "start";
2745 case SND_SOC_DPCM_STATE_STOP:
2746 return "stop";
2747 case SND_SOC_DPCM_STATE_SUSPEND:
2748 return "suspend";
2749 case SND_SOC_DPCM_STATE_PAUSED:
2750 return "paused";
2751 case SND_SOC_DPCM_STATE_HW_FREE:
2752 return "hw_free";
2753 case SND_SOC_DPCM_STATE_CLOSE:
2754 return "close";
2755 }
2756
2757 return "unknown";
2758}
2759
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002760static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2761 int stream, char *buf, size_t size)
2762{
2763 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2764 struct snd_soc_dpcm *dpcm;
2765 ssize_t offset = 0;
2766
2767 /* FE state */
2768 offset += snprintf(buf + offset, size - offset,
2769 "[%s - %s]\n", fe->dai_link->name,
2770 stream ? "Capture" : "Playback");
2771
2772 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2773 dpcm_state_string(fe->dpcm[stream].state));
2774
2775 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2776 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2777 offset += snprintf(buf + offset, size - offset,
2778 "Hardware Params: "
2779 "Format = %s, Channels = %d, Rate = %d\n",
2780 snd_pcm_format_name(params_format(params)),
2781 params_channels(params),
2782 params_rate(params));
2783
2784 /* BEs state */
2785 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2786
2787 if (list_empty(&fe->dpcm[stream].be_clients)) {
2788 offset += snprintf(buf + offset, size - offset,
2789 " No active DSP links\n");
2790 goto out;
2791 }
2792
2793 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2794 struct snd_soc_pcm_runtime *be = dpcm->be;
2795 params = &dpcm->hw_params;
2796
2797 offset += snprintf(buf + offset, size - offset,
2798 "- %s\n", be->dai_link->name);
2799
2800 offset += snprintf(buf + offset, size - offset,
2801 " State: %s\n",
2802 dpcm_state_string(be->dpcm[stream].state));
2803
2804 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2805 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2806 offset += snprintf(buf + offset, size - offset,
2807 " Hardware Params: "
2808 "Format = %s, Channels = %d, Rate = %d\n",
2809 snd_pcm_format_name(params_format(params)),
2810 params_channels(params),
2811 params_rate(params));
2812 }
2813
2814out:
2815 return offset;
2816}
2817
2818static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2819 size_t count, loff_t *ppos)
2820{
2821 struct snd_soc_pcm_runtime *fe = file->private_data;
2822 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2823 char *buf;
2824
2825 buf = kmalloc(out_count, GFP_KERNEL);
2826 if (!buf)
2827 return -ENOMEM;
2828
2829 if (fe->cpu_dai->driver->playback.channels_min)
2830 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2831 buf + offset, out_count - offset);
2832
2833 if (fe->cpu_dai->driver->capture.channels_min)
2834 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2835 buf + offset, out_count - offset);
2836
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002837 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002838
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002839 kfree(buf);
2840 return ret;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002841}
2842
2843static const struct file_operations dpcm_state_fops = {
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002844 .open = simple_open,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002845 .read = dpcm_state_read_file,
2846 .llseek = default_llseek,
2847};
2848
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002849void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002850{
Mark Brownb3bba9a2012-05-08 10:33:47 +01002851 if (!rtd->dai_link)
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002852 return;
Mark Brownb3bba9a2012-05-08 10:33:47 +01002853
Lars-Peter Clausen6553bf062015-04-09 10:52:38 +02002854 if (!rtd->card->debugfs_card_root)
2855 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002856
2857 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2858 rtd->card->debugfs_card_root);
2859 if (!rtd->debugfs_dpcm_root) {
2860 dev_dbg(rtd->dev,
2861 "ASoC: Failed to create dpcm debugfs directory %s\n",
2862 rtd->dai_link->name);
Lars-Peter Clausen2e55b902015-04-09 10:52:37 +02002863 return;
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002864 }
2865
Liam Girdwoodf57b8482012-04-27 11:33:46 +01002866 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002867 rtd->debugfs_dpcm_root,
2868 rtd, &dpcm_state_fops);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01002869}
2870#endif