1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-pcm.c -- ALSA SoC PCM
4 //
5 // Copyright 2005 Wolfson Microelectronics PLC.
6 // Copyright 2005 Openedhand Ltd.
7 // Copyright (C) 2010 Slimlogic Ltd.
8 // Copyright (C) 2010 Texas Instruments Inc.
9 //
10 // Authors: Liam Girdwood <lrg@ti.com>
11 // Mark Brown <broonie@opensource.wolfsonmicro.com>
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/workqueue.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dpcm.h>
27 #include <sound/soc-link.h>
28 #include <sound/initval.h>
29
30 #define DPCM_MAX_BE_USERS 8
31
32 #ifdef CONFIG_DEBUG_FS
dpcm_state_string(enum snd_soc_dpcm_state state)33 static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
34 {
35 switch (state) {
36 case SND_SOC_DPCM_STATE_NEW:
37 return "new";
38 case SND_SOC_DPCM_STATE_OPEN:
39 return "open";
40 case SND_SOC_DPCM_STATE_HW_PARAMS:
41 return "hw_params";
42 case SND_SOC_DPCM_STATE_PREPARE:
43 return "prepare";
44 case SND_SOC_DPCM_STATE_START:
45 return "start";
46 case SND_SOC_DPCM_STATE_STOP:
47 return "stop";
48 case SND_SOC_DPCM_STATE_SUSPEND:
49 return "suspend";
50 case SND_SOC_DPCM_STATE_PAUSED:
51 return "paused";
52 case SND_SOC_DPCM_STATE_HW_FREE:
53 return "hw_free";
54 case SND_SOC_DPCM_STATE_CLOSE:
55 return "close";
56 }
57
58 return "unknown";
59 }
60
dpcm_show_state(struct snd_soc_pcm_runtime * fe,int stream,char * buf,size_t size)61 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
62 int stream, char *buf, size_t size)
63 {
64 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
65 struct snd_soc_dpcm *dpcm;
66 ssize_t offset = 0;
67 unsigned long flags;
68
69 /* FE state */
70 offset += scnprintf(buf + offset, size - offset,
71 "[%s - %s]\n", fe->dai_link->name,
72 stream ? "Capture" : "Playback");
73
74 offset += scnprintf(buf + offset, size - offset, "State: %s\n",
75 dpcm_state_string(fe->dpcm[stream].state));
76
77 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
78 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
79 offset += scnprintf(buf + offset, size - offset,
80 "Hardware Params: "
81 "Format = %s, Channels = %d, Rate = %d\n",
82 snd_pcm_format_name(params_format(params)),
83 params_channels(params),
84 params_rate(params));
85
86 /* BEs state */
87 offset += scnprintf(buf + offset, size - offset, "Backends:\n");
88
89 if (list_empty(&fe->dpcm[stream].be_clients)) {
90 offset += scnprintf(buf + offset, size - offset,
91 " No active DSP links\n");
92 goto out;
93 }
94
95 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
96 for_each_dpcm_be(fe, stream, dpcm) {
97 struct snd_soc_pcm_runtime *be = dpcm->be;
98 params = &dpcm->hw_params;
99
100 offset += scnprintf(buf + offset, size - offset,
101 "- %s\n", be->dai_link->name);
102
103 offset += scnprintf(buf + offset, size - offset,
104 " State: %s\n",
105 dpcm_state_string(be->dpcm[stream].state));
106
107 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
108 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
109 offset += scnprintf(buf + offset, size - offset,
110 " Hardware Params: "
111 "Format = %s, Channels = %d, Rate = %d\n",
112 snd_pcm_format_name(params_format(params)),
113 params_channels(params),
114 params_rate(params));
115 }
116 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
117 out:
118 return offset;
119 }
120
dpcm_state_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)121 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
122 size_t count, loff_t *ppos)
123 {
124 struct snd_soc_pcm_runtime *fe = file->private_data;
125 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
126 int stream;
127 char *buf;
128
129 if (fe->num_cpus > 1) {
130 dev_err(fe->dev,
131 "%s doesn't support Multi CPU yet\n", __func__);
132 return -EINVAL;
133 }
134
135 buf = kmalloc(out_count, GFP_KERNEL);
136 if (!buf)
137 return -ENOMEM;
138
139 for_each_pcm_streams(stream)
140 if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
141 offset += dpcm_show_state(fe, stream,
142 buf + offset,
143 out_count - offset);
144
145 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
146
147 kfree(buf);
148 return ret;
149 }
150
151 static const struct file_operations dpcm_state_fops = {
152 .open = simple_open,
153 .read = dpcm_state_read_file,
154 .llseek = default_llseek,
155 };
156
soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime * rtd)157 void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
158 {
159 if (!rtd->dai_link)
160 return;
161
162 if (!rtd->dai_link->dynamic)
163 return;
164
165 if (!rtd->card->debugfs_card_root)
166 return;
167
168 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
169 rtd->card->debugfs_card_root);
170
171 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
172 rtd, &dpcm_state_fops);
173 }
174
dpcm_create_debugfs_state(struct snd_soc_dpcm * dpcm,int stream)175 static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
176 {
177 char *name;
178
179 name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
180 stream ? "capture" : "playback");
181 if (name) {
182 dpcm->debugfs_state = debugfs_create_dir(
183 name, dpcm->fe->debugfs_dpcm_root);
184 debugfs_create_u32("state", 0644, dpcm->debugfs_state,
185 &dpcm->state);
186 kfree(name);
187 }
188 }
189
dpcm_remove_debugfs_state(struct snd_soc_dpcm * dpcm)190 static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
191 {
192 debugfs_remove_recursive(dpcm->debugfs_state);
193 }
194
195 #else
dpcm_create_debugfs_state(struct snd_soc_dpcm * dpcm,int stream)196 static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
197 int stream)
198 {
199 }
200
dpcm_remove_debugfs_state(struct snd_soc_dpcm * dpcm)201 static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
202 {
203 }
204 #endif
205
206 /**
207 * snd_soc_runtime_action() - Increment/Decrement active count for
208 * PCM runtime components
209 * @rtd: ASoC PCM runtime that is activated
210 * @stream: Direction of the PCM stream
211 * @action: Activate stream if 1. Deactivate if -1.
212 *
213 * Increments/Decrements the active count for all the DAIs and components
214 * attached to a PCM runtime.
215 * Should typically be called when a stream is opened.
216 *
217 * Must be called with the rtd->card->pcm_mutex being held
218 */
snd_soc_runtime_action(struct snd_soc_pcm_runtime * rtd,int stream,int action)219 void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
220 int stream, int action)
221 {
222 struct snd_soc_dai *dai;
223 int i;
224
225 lockdep_assert_held(&rtd->card->pcm_mutex);
226
227 for_each_rtd_dais(rtd, i, dai)
228 snd_soc_dai_action(dai, stream, action);
229 }
230 EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
231
232 /**
233 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
234 * @rtd: The ASoC PCM runtime that should be checked.
235 *
236 * This function checks whether the power down delay should be ignored for a
237 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
238 * been configured to ignore the delay, or if none of the components benefits
239 * from having the delay.
240 */
snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime * rtd)241 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
242 {
243 struct snd_soc_component *component;
244 bool ignore = true;
245 int i;
246
247 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
248 return true;
249
250 for_each_rtd_components(rtd, i, component)
251 ignore &= !component->driver->use_pmdown_time;
252
253 return ignore;
254 }
255
256 /**
257 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
258 * @substream: the pcm substream
259 * @hw: the hardware parameters
260 *
261 * Sets the substream runtime hardware parameters.
262 */
snd_soc_set_runtime_hwparams(struct snd_pcm_substream * substream,const struct snd_pcm_hardware * hw)263 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
264 const struct snd_pcm_hardware *hw)
265 {
266 struct snd_pcm_runtime *runtime = substream->runtime;
267 runtime->hw.info = hw->info;
268 runtime->hw.formats = hw->formats;
269 runtime->hw.period_bytes_min = hw->period_bytes_min;
270 runtime->hw.period_bytes_max = hw->period_bytes_max;
271 runtime->hw.periods_min = hw->periods_min;
272 runtime->hw.periods_max = hw->periods_max;
273 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
274 runtime->hw.fifo_size = hw->fifo_size;
275 return 0;
276 }
277 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
278
279 /* DPCM stream event, send event to FE and all active BEs. */
dpcm_dapm_stream_event(struct snd_soc_pcm_runtime * fe,int dir,int event)280 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
281 int event)
282 {
283 struct snd_soc_dpcm *dpcm;
284
285 for_each_dpcm_be(fe, dir, dpcm) {
286
287 struct snd_soc_pcm_runtime *be = dpcm->be;
288
289 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
290 be->dai_link->name, event, dir);
291
292 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
293 (be->dpcm[dir].users >= 1))
294 continue;
295
296 snd_soc_dapm_stream_event(be, dir, event);
297 }
298
299 snd_soc_dapm_stream_event(fe, dir, event);
300
301 return 0;
302 }
303
soc_pcm_apply_symmetry(struct snd_pcm_substream * substream,struct snd_soc_dai * soc_dai)304 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
305 struct snd_soc_dai *soc_dai)
306 {
307 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
308 int ret;
309
310 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
311 rtd->dai_link->symmetric_rates)) {
312 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
313 soc_dai->rate);
314
315 ret = snd_pcm_hw_constraint_single(substream->runtime,
316 SNDRV_PCM_HW_PARAM_RATE,
317 soc_dai->rate);
318 if (ret < 0) {
319 dev_err(soc_dai->dev,
320 "ASoC: Unable to apply rate constraint: %d\n",
321 ret);
322 return ret;
323 }
324 }
325
326 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
327 rtd->dai_link->symmetric_channels)) {
328 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
329 soc_dai->channels);
330
331 ret = snd_pcm_hw_constraint_single(substream->runtime,
332 SNDRV_PCM_HW_PARAM_CHANNELS,
333 soc_dai->channels);
334 if (ret < 0) {
335 dev_err(soc_dai->dev,
336 "ASoC: Unable to apply channel symmetry constraint: %d\n",
337 ret);
338 return ret;
339 }
340 }
341
342 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
343 rtd->dai_link->symmetric_samplebits)) {
344 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
345 soc_dai->sample_bits);
346
347 ret = snd_pcm_hw_constraint_single(substream->runtime,
348 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
349 soc_dai->sample_bits);
350 if (ret < 0) {
351 dev_err(soc_dai->dev,
352 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
353 ret);
354 return ret;
355 }
356 }
357
358 return 0;
359 }
360
soc_pcm_params_symmetry(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)361 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
362 struct snd_pcm_hw_params *params)
363 {
364 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
365 struct snd_soc_dai *dai;
366 struct snd_soc_dai *cpu_dai;
367 unsigned int rate, channels, sample_bits, symmetry, i;
368
369 rate = params_rate(params);
370 channels = params_channels(params);
371 sample_bits = snd_pcm_format_physical_width(params_format(params));
372
373 /* reject unmatched parameters when applying symmetry */
374 symmetry = rtd->dai_link->symmetric_rates;
375
376 for_each_rtd_cpu_dais(rtd, i, dai)
377 symmetry |= dai->driver->symmetric_rates;
378
379 if (symmetry) {
380 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
381 if (cpu_dai->rate && cpu_dai->rate != rate) {
382 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
383 cpu_dai->rate, rate);
384 return -EINVAL;
385 }
386 }
387 }
388
389 symmetry = rtd->dai_link->symmetric_channels;
390
391 for_each_rtd_dais(rtd, i, dai)
392 symmetry |= dai->driver->symmetric_channels;
393
394 if (symmetry) {
395 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
396 if (cpu_dai->channels &&
397 cpu_dai->channels != channels) {
398 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
399 cpu_dai->channels, channels);
400 return -EINVAL;
401 }
402 }
403 }
404
405 symmetry = rtd->dai_link->symmetric_samplebits;
406
407 for_each_rtd_dais(rtd, i, dai)
408 symmetry |= dai->driver->symmetric_samplebits;
409
410 if (symmetry) {
411 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
412 if (cpu_dai->sample_bits &&
413 cpu_dai->sample_bits != sample_bits) {
414 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
415 cpu_dai->sample_bits, sample_bits);
416 return -EINVAL;
417 }
418 }
419 }
420
421 return 0;
422 }
423
soc_pcm_has_symmetry(struct snd_pcm_substream * substream)424 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
425 {
426 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
427 struct snd_soc_dai_link *link = rtd->dai_link;
428 struct snd_soc_dai *dai;
429 unsigned int symmetry, i;
430
431 symmetry = link->symmetric_rates ||
432 link->symmetric_channels ||
433 link->symmetric_samplebits;
434
435 for_each_rtd_dais(rtd, i, dai)
436 symmetry = symmetry ||
437 dai->driver->symmetric_rates ||
438 dai->driver->symmetric_channels ||
439 dai->driver->symmetric_samplebits;
440
441 return symmetry;
442 }
443
soc_pcm_set_msb(struct snd_pcm_substream * substream,int bits)444 static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
445 {
446 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
447 int ret;
448
449 if (!bits)
450 return;
451
452 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
453 if (ret != 0)
454 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
455 bits, ret);
456 }
457
soc_pcm_apply_msb(struct snd_pcm_substream * substream)458 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
459 {
460 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
461 struct snd_soc_dai *cpu_dai;
462 struct snd_soc_dai *codec_dai;
463 struct snd_soc_pcm_stream *pcm_codec, *pcm_cpu;
464 int stream = substream->stream;
465 int i;
466 unsigned int bits = 0, cpu_bits = 0;
467
468 for_each_rtd_codec_dais(rtd, i, codec_dai) {
469 pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
470
471 if (pcm_codec->sig_bits == 0) {
472 bits = 0;
473 break;
474 }
475 bits = max(pcm_codec->sig_bits, bits);
476 }
477
478 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
479 pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
480
481 if (pcm_cpu->sig_bits == 0) {
482 cpu_bits = 0;
483 break;
484 }
485 cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
486 }
487
488 soc_pcm_set_msb(substream, bits);
489 soc_pcm_set_msb(substream, cpu_bits);
490 }
491
492 /**
493 * snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
494 * @rtd: ASoC PCM runtime
495 * @hw: PCM hardware parameters (output)
496 * @stream: Direction of the PCM stream
497 *
498 * Calculates the subset of stream parameters supported by all DAIs
499 * associated with the PCM stream.
500 */
snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hardware * hw,int stream)501 int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
502 struct snd_pcm_hardware *hw, int stream)
503 {
504 struct snd_soc_dai *codec_dai;
505 struct snd_soc_dai *cpu_dai;
506 struct snd_soc_pcm_stream *codec_stream;
507 struct snd_soc_pcm_stream *cpu_stream;
508 unsigned int chan_min = 0, chan_max = UINT_MAX;
509 unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
510 unsigned int rate_min = 0, rate_max = UINT_MAX;
511 unsigned int cpu_rate_min = 0, cpu_rate_max = UINT_MAX;
512 unsigned int rates = UINT_MAX, cpu_rates = UINT_MAX;
513 u64 formats = ULLONG_MAX;
514 int i;
515
516 /* first calculate min/max only for CPUs in the DAI link */
517 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
518
519 /*
520 * Skip CPUs which don't support the current stream type.
521 * Otherwise, since the rate, channel, and format values will
522 * zero in that case, we would have no usable settings left,
523 * causing the resulting setup to fail.
524 */
525 if (!snd_soc_dai_stream_valid(cpu_dai, stream))
526 continue;
527
528 cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
529
530 cpu_chan_min = max(cpu_chan_min, cpu_stream->channels_min);
531 cpu_chan_max = min(cpu_chan_max, cpu_stream->channels_max);
532 cpu_rate_min = max(cpu_rate_min, cpu_stream->rate_min);
533 cpu_rate_max = min_not_zero(cpu_rate_max, cpu_stream->rate_max);
534 formats &= cpu_stream->formats;
535 cpu_rates = snd_pcm_rate_mask_intersect(cpu_stream->rates,
536 cpu_rates);
537 }
538
539 /* second calculate min/max only for CODECs in the DAI link */
540 for_each_rtd_codec_dais(rtd, i, codec_dai) {
541
542 /*
543 * Skip CODECs which don't support the current stream type.
544 * Otherwise, since the rate, channel, and format values will
545 * zero in that case, we would have no usable settings left,
546 * causing the resulting setup to fail.
547 */
548 if (!snd_soc_dai_stream_valid(codec_dai, stream))
549 continue;
550
551 codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
552
553 chan_min = max(chan_min, codec_stream->channels_min);
554 chan_max = min(chan_max, codec_stream->channels_max);
555 rate_min = max(rate_min, codec_stream->rate_min);
556 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
557 formats &= codec_stream->formats;
558 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
559 }
560
561 /* Verify both a valid CPU DAI and a valid CODEC DAI were found */
562 if (!chan_min || !cpu_chan_min)
563 return -EINVAL;
564
565 /*
566 * chan min/max cannot be enforced if there are multiple CODEC DAIs
567 * connected to CPU DAI(s), use CPU DAI's directly and let
568 * channel allocation be fixed up later
569 */
570 if (rtd->num_codecs > 1) {
571 chan_min = cpu_chan_min;
572 chan_max = cpu_chan_max;
573 }
574
575 /* finally find a intersection between CODECs and CPUs */
576 hw->channels_min = max(chan_min, cpu_chan_min);
577 hw->channels_max = min(chan_max, cpu_chan_max);
578 hw->formats = formats;
579 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_rates);
580
581 snd_pcm_hw_limit_rates(hw);
582
583 hw->rate_min = max(hw->rate_min, cpu_rate_min);
584 hw->rate_min = max(hw->rate_min, rate_min);
585 hw->rate_max = min_not_zero(hw->rate_max, cpu_rate_max);
586 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
587
588 return 0;
589 }
590 EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
591
soc_pcm_init_runtime_hw(struct snd_pcm_substream * substream)592 static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
593 {
594 struct snd_pcm_hardware *hw = &substream->runtime->hw;
595 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
596 u64 formats = hw->formats;
597
598 /*
599 * At least one CPU and one CODEC should match. Otherwise, we should
600 * have bailed out on a higher level, since there would be no CPU or
601 * CODEC to support the transfer direction in that case.
602 */
603 snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
604
605 if (formats)
606 hw->formats &= formats;
607 }
608
soc_pcm_components_open(struct snd_pcm_substream * substream)609 static int soc_pcm_components_open(struct snd_pcm_substream *substream)
610 {
611 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
612 struct snd_soc_component *component;
613 int i, ret = 0;
614
615 for_each_rtd_components(rtd, i, component) {
616 ret = snd_soc_component_module_get_when_open(component, substream);
617 if (ret < 0)
618 break;
619
620 ret = snd_soc_component_open(component, substream);
621 if (ret < 0)
622 break;
623 }
624
625 return ret;
626 }
627
soc_pcm_components_close(struct snd_pcm_substream * substream,int rollback)628 static int soc_pcm_components_close(struct snd_pcm_substream *substream,
629 int rollback)
630 {
631 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
632 struct snd_soc_component *component;
633 int i, r, ret = 0;
634
635 for_each_rtd_components(rtd, i, component) {
636 r = snd_soc_component_close(component, substream, rollback);
637 if (r < 0)
638 ret = r; /* use last ret */
639
640 snd_soc_component_module_put_when_close(component, substream, rollback);
641 }
642
643 return ret;
644 }
645
soc_pcm_clean(struct snd_pcm_substream * substream,int rollback)646 static int soc_pcm_clean(struct snd_pcm_substream *substream, int rollback)
647 {
648 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
649 struct snd_soc_component *component;
650 struct snd_soc_dai *dai;
651 int i;
652
653 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
654
655 if (!rollback)
656 snd_soc_runtime_deactivate(rtd, substream->stream);
657
658 for_each_rtd_dais(rtd, i, dai)
659 snd_soc_dai_shutdown(dai, substream, rollback);
660
661 snd_soc_link_shutdown(substream, rollback);
662
663 soc_pcm_components_close(substream, rollback);
664
665 if (!rollback)
666 snd_soc_dapm_stream_stop(rtd, substream->stream);
667
668 mutex_unlock(&rtd->card->pcm_mutex);
669
670 snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
671
672 for_each_rtd_components(rtd, i, component)
673 if (!snd_soc_component_active(component))
674 pinctrl_pm_select_sleep_state(component->dev);
675
676 return 0;
677 }
678
679 /*
680 * Called by ALSA when a PCM substream is closed. Private data can be
681 * freed here. The cpu DAI, codec DAI, machine and components are also
682 * shutdown.
683 */
soc_pcm_close(struct snd_pcm_substream * substream)684 static int soc_pcm_close(struct snd_pcm_substream *substream)
685 {
686 return soc_pcm_clean(substream, 0);
687 }
688
689 /*
690 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
691 * then initialized and any private data can be allocated. This also calls
692 * startup for the cpu DAI, component, machine and codec DAI.
693 */
soc_pcm_open(struct snd_pcm_substream * substream)694 static int soc_pcm_open(struct snd_pcm_substream *substream)
695 {
696 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
697 struct snd_pcm_runtime *runtime = substream->runtime;
698 struct snd_soc_component *component;
699 struct snd_soc_dai *dai;
700 const char *codec_dai_name = "multicodec";
701 const char *cpu_dai_name = "multicpu";
702 int i, ret = 0;
703
704 for_each_rtd_components(rtd, i, component)
705 pinctrl_pm_select_default_state(component->dev);
706
707 ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
708 if (ret < 0)
709 goto pm_err;
710
711 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
712
713 ret = soc_pcm_components_open(substream);
714 if (ret < 0)
715 goto err;
716
717 ret = snd_soc_link_startup(substream);
718 if (ret < 0)
719 goto err;
720
721 /* startup the audio subsystem */
722 for_each_rtd_dais(rtd, i, dai) {
723 ret = snd_soc_dai_startup(dai, substream);
724 if (ret < 0)
725 goto err;
726 }
727
728 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
729 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
730 goto dynamic;
731
732 /* Check that the codec and cpu DAIs are compatible */
733 soc_pcm_init_runtime_hw(substream);
734
735 if (rtd->num_codecs == 1)
736 codec_dai_name = asoc_rtd_to_codec(rtd, 0)->name;
737
738 if (rtd->num_cpus == 1)
739 cpu_dai_name = asoc_rtd_to_cpu(rtd, 0)->name;
740
741 if (soc_pcm_has_symmetry(substream))
742 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
743
744 ret = -EINVAL;
745 if (!runtime->hw.rates) {
746 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
747 codec_dai_name, cpu_dai_name);
748 goto err;
749 }
750 if (!runtime->hw.formats) {
751 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
752 codec_dai_name, cpu_dai_name);
753 goto err;
754 }
755 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
756 runtime->hw.channels_min > runtime->hw.channels_max) {
757 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
758 codec_dai_name, cpu_dai_name);
759 goto err;
760 }
761
762 soc_pcm_apply_msb(substream);
763
764 /* Symmetry only applies if we've already got an active stream. */
765 for_each_rtd_dais(rtd, i, dai) {
766 if (snd_soc_dai_active(dai)) {
767 ret = soc_pcm_apply_symmetry(substream, dai);
768 if (ret != 0)
769 goto err;
770 }
771 }
772
773 pr_debug("ASoC: %s <-> %s info:\n",
774 codec_dai_name, cpu_dai_name);
775 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
776 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
777 runtime->hw.channels_max);
778 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
779 runtime->hw.rate_max);
780 dynamic:
781 snd_soc_runtime_activate(rtd, substream->stream);
782 ret = 0;
783 err:
784 mutex_unlock(&rtd->card->pcm_mutex);
785 pm_err:
786 if (ret < 0)
787 soc_pcm_clean(substream, 1);
788
789 return ret;
790 }
791
codec2codec_close_delayed_work(struct snd_soc_pcm_runtime * rtd)792 static void codec2codec_close_delayed_work(struct snd_soc_pcm_runtime *rtd)
793 {
794 /*
795 * Currently nothing to do for c2c links
796 * Since c2c links are internal nodes in the DAPM graph and
797 * don't interface with the outside world or application layer
798 * we don't have to do any special handling on close.
799 */
800 }
801
802 /*
803 * Called by ALSA when the PCM substream is prepared, can set format, sample
804 * rate, etc. This function is non atomic and can be called multiple times,
805 * it can refer to the runtime info.
806 */
soc_pcm_prepare(struct snd_pcm_substream * substream)807 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
808 {
809 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
810 struct snd_soc_dai *dai;
811 int i, ret = 0;
812
813 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
814
815 ret = snd_soc_link_prepare(substream);
816 if (ret < 0)
817 goto out;
818
819 ret = snd_soc_pcm_component_prepare(substream);
820 if (ret < 0)
821 goto out;
822
823 ret = snd_soc_pcm_dai_prepare(substream);
824 if (ret < 0) {
825 dev_err(rtd->dev, "ASoC: DAI prepare error: %d\n", ret);
826 goto out;
827 }
828
829 /* cancel any delayed stream shutdown that is pending */
830 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
831 rtd->pop_wait) {
832 rtd->pop_wait = 0;
833 cancel_delayed_work(&rtd->delayed_work);
834 }
835
836 snd_soc_dapm_stream_event(rtd, substream->stream,
837 SND_SOC_DAPM_STREAM_START);
838
839 for_each_rtd_dais(rtd, i, dai)
840 snd_soc_dai_digital_mute(dai, 0, substream->stream);
841
842 out:
843 mutex_unlock(&rtd->card->pcm_mutex);
844 return ret;
845 }
846
soc_pcm_codec_params_fixup(struct snd_pcm_hw_params * params,unsigned int mask)847 static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
848 unsigned int mask)
849 {
850 struct snd_interval *interval;
851 int channels = hweight_long(mask);
852
853 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
854 interval->min = channels;
855 interval->max = channels;
856 }
857
858 /*
859 * Called by ALSA when the hardware params are set by application. This
860 * function can also be called multiple times and can allocate buffers
861 * (using snd_pcm_lib_* ). It's non-atomic.
862 */
soc_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)863 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
864 struct snd_pcm_hw_params *params)
865 {
866 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
867 struct snd_soc_component *component;
868 struct snd_soc_dai *cpu_dai;
869 struct snd_soc_dai *codec_dai;
870 int i, ret = 0;
871
872 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
873
874 ret = soc_pcm_params_symmetry(substream, params);
875 if (ret)
876 goto out;
877
878 ret = snd_soc_link_hw_params(substream, params);
879 if (ret < 0)
880 goto out;
881
882 for_each_rtd_codec_dais(rtd, i, codec_dai) {
883 struct snd_pcm_hw_params codec_params;
884
885 /*
886 * Skip CODECs which don't support the current stream type,
887 * the idea being that if a CODEC is not used for the currently
888 * set up transfer direction, it should not need to be
889 * configured, especially since the configuration used might
890 * not even be supported by that CODEC. There may be cases
891 * however where a CODEC needs to be set up although it is
892 * actually not being used for the transfer, e.g. if a
893 * capture-only CODEC is acting as an LRCLK and/or BCLK master
894 * for the DAI link including a playback-only CODEC.
895 * If this becomes necessary, we will have to augment the
896 * machine driver setup with information on how to act, so
897 * we can do the right thing here.
898 */
899 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
900 continue;
901
902 /* copy params for each codec */
903 codec_params = *params;
904
905 /* fixup params based on TDM slot masks */
906 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
907 codec_dai->tx_mask)
908 soc_pcm_codec_params_fixup(&codec_params,
909 codec_dai->tx_mask);
910
911 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
912 codec_dai->rx_mask)
913 soc_pcm_codec_params_fixup(&codec_params,
914 codec_dai->rx_mask);
915
916 ret = snd_soc_dai_hw_params(codec_dai, substream,
917 &codec_params);
918 if(ret < 0)
919 goto codec_err;
920
921 codec_dai->rate = params_rate(&codec_params);
922 codec_dai->channels = params_channels(&codec_params);
923 codec_dai->sample_bits = snd_pcm_format_physical_width(
924 params_format(&codec_params));
925
926 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
927 }
928
929 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
930 /*
931 * Skip CPUs which don't support the current stream
932 * type. See soc_pcm_init_runtime_hw() for more details
933 */
934 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
935 continue;
936
937 ret = snd_soc_dai_hw_params(cpu_dai, substream, params);
938 if (ret < 0)
939 goto interface_err;
940
941 /* store the parameters for each DAI */
942 cpu_dai->rate = params_rate(params);
943 cpu_dai->channels = params_channels(params);
944 cpu_dai->sample_bits =
945 snd_pcm_format_physical_width(params_format(params));
946
947 snd_soc_dapm_update_dai(substream, params, cpu_dai);
948 }
949
950 ret = snd_soc_pcm_component_hw_params(substream, params, &component);
951 if (ret < 0)
952 goto component_err;
953
954 out:
955 mutex_unlock(&rtd->card->pcm_mutex);
956 return ret;
957
958 component_err:
959 snd_soc_pcm_component_hw_free(substream, component);
960
961 i = rtd->num_cpus;
962
963 interface_err:
964 for_each_rtd_cpu_dais_rollback(rtd, i, cpu_dai) {
965 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
966 continue;
967
968 snd_soc_dai_hw_free(cpu_dai, substream);
969 cpu_dai->rate = 0;
970 }
971
972 i = rtd->num_codecs;
973
974 codec_err:
975 for_each_rtd_codec_dais_rollback(rtd, i, codec_dai) {
976 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
977 continue;
978
979 snd_soc_dai_hw_free(codec_dai, substream);
980 codec_dai->rate = 0;
981 }
982
983 snd_soc_link_hw_free(substream);
984
985 mutex_unlock(&rtd->card->pcm_mutex);
986 return ret;
987 }
988
989 /*
990 * Frees resources allocated by hw_params, can be called multiple times
991 */
soc_pcm_hw_free(struct snd_pcm_substream * substream)992 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
993 {
994 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
995 struct snd_soc_dai *dai;
996 int i;
997
998 mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
999
1000 /* clear the corresponding DAIs parameters when going to be inactive */
1001 for_each_rtd_dais(rtd, i, dai) {
1002 int active = snd_soc_dai_stream_active(dai, substream->stream);
1003
1004 if (snd_soc_dai_active(dai) == 1) {
1005 dai->rate = 0;
1006 dai->channels = 0;
1007 dai->sample_bits = 0;
1008 }
1009
1010 if (active == 1)
1011 snd_soc_dai_digital_mute(dai, 1, substream->stream);
1012 }
1013
1014 /* free any machine hw params */
1015 snd_soc_link_hw_free(substream);
1016
1017 /* free any component resources */
1018 snd_soc_pcm_component_hw_free(substream, NULL);
1019
1020 /* now free hw params for the DAIs */
1021 for_each_rtd_dais(rtd, i, dai) {
1022 if (!snd_soc_dai_stream_valid(dai, substream->stream))
1023 continue;
1024
1025 snd_soc_dai_hw_free(dai, substream);
1026 }
1027
1028 mutex_unlock(&rtd->card->pcm_mutex);
1029 return 0;
1030 }
1031
soc_pcm_trigger(struct snd_pcm_substream * substream,int cmd)1032 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1033 {
1034 int ret = -EINVAL;
1035
1036 switch (cmd) {
1037 case SNDRV_PCM_TRIGGER_START:
1038 case SNDRV_PCM_TRIGGER_RESUME:
1039 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1040 ret = snd_soc_link_trigger(substream, cmd);
1041 if (ret < 0)
1042 break;
1043
1044 ret = snd_soc_pcm_component_trigger(substream, cmd);
1045 if (ret < 0)
1046 break;
1047
1048 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1049 break;
1050 case SNDRV_PCM_TRIGGER_STOP:
1051 case SNDRV_PCM_TRIGGER_SUSPEND:
1052 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1053 ret = snd_soc_pcm_dai_trigger(substream, cmd);
1054 if (ret < 0)
1055 break;
1056
1057 ret = snd_soc_pcm_component_trigger(substream, cmd);
1058 if (ret < 0)
1059 break;
1060
1061 ret = snd_soc_link_trigger(substream, cmd);
1062 break;
1063 }
1064
1065 return ret;
1066 }
1067
1068 /*
1069 * soc level wrapper for pointer callback
1070 * If cpu_dai, codec_dai, component driver has the delay callback, then
1071 * the runtime->delay will be updated accordingly.
1072 */
soc_pcm_pointer(struct snd_pcm_substream * substream)1073 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1074 {
1075 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1076 struct snd_soc_dai *cpu_dai;
1077 struct snd_soc_dai *codec_dai;
1078 struct snd_pcm_runtime *runtime = substream->runtime;
1079 snd_pcm_uframes_t offset = 0;
1080 snd_pcm_sframes_t delay = 0;
1081 snd_pcm_sframes_t codec_delay = 0;
1082 snd_pcm_sframes_t cpu_delay = 0;
1083 int i;
1084
1085 /* clearing the previous total delay */
1086 runtime->delay = 0;
1087
1088 offset = snd_soc_pcm_component_pointer(substream);
1089
1090 /* base delay if assigned in pointer callback */
1091 delay = runtime->delay;
1092
1093 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1094 cpu_delay = max(cpu_delay,
1095 snd_soc_dai_delay(cpu_dai, substream));
1096 }
1097 delay += cpu_delay;
1098
1099 for_each_rtd_codec_dais(rtd, i, codec_dai) {
1100 codec_delay = max(codec_delay,
1101 snd_soc_dai_delay(codec_dai, substream));
1102 }
1103 delay += codec_delay;
1104
1105 runtime->delay = delay;
1106
1107 return offset;
1108 }
1109
1110 /* connect a FE and BE */
dpcm_be_connect(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)1111 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1112 struct snd_soc_pcm_runtime *be, int stream)
1113 {
1114 struct snd_soc_dpcm *dpcm;
1115 unsigned long flags;
1116
1117 /* only add new dpcms */
1118 for_each_dpcm_be(fe, stream, dpcm) {
1119 if (dpcm->be == be && dpcm->fe == fe)
1120 return 0;
1121 }
1122
1123 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1124 if (!dpcm)
1125 return -ENOMEM;
1126
1127 dpcm->be = be;
1128 dpcm->fe = fe;
1129 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1130 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1131 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1132 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1133 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1134 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1135
1136 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1137 stream ? "capture" : "playback", fe->dai_link->name,
1138 stream ? "<-" : "->", be->dai_link->name);
1139
1140 dpcm_create_debugfs_state(dpcm, stream);
1141
1142 return 1;
1143 }
1144
1145 /* reparent a BE onto another FE */
dpcm_be_reparent(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)1146 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1147 struct snd_soc_pcm_runtime *be, int stream)
1148 {
1149 struct snd_soc_dpcm *dpcm;
1150 struct snd_pcm_substream *fe_substream, *be_substream;
1151
1152 /* reparent if BE is connected to other FEs */
1153 if (!be->dpcm[stream].users)
1154 return;
1155
1156 be_substream = snd_soc_dpcm_get_substream(be, stream);
1157 if (!be_substream)
1158 return;
1159
1160 for_each_dpcm_fe(be, stream, dpcm) {
1161 if (dpcm->fe == fe)
1162 continue;
1163
1164 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1165 stream ? "capture" : "playback",
1166 dpcm->fe->dai_link->name,
1167 stream ? "<-" : "->", dpcm->be->dai_link->name);
1168
1169 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1170 be_substream->runtime = fe_substream->runtime;
1171 break;
1172 }
1173 }
1174
1175 /* disconnect a BE and FE */
dpcm_be_disconnect(struct snd_soc_pcm_runtime * fe,int stream)1176 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1177 {
1178 struct snd_soc_dpcm *dpcm, *d;
1179 unsigned long flags;
1180
1181 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1182 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1183 stream ? "capture" : "playback",
1184 dpcm->be->dai_link->name);
1185
1186 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1187 continue;
1188
1189 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1190 stream ? "capture" : "playback", fe->dai_link->name,
1191 stream ? "<-" : "->", dpcm->be->dai_link->name);
1192
1193 /* BEs still alive need new FE */
1194 dpcm_be_reparent(fe, dpcm->be, stream);
1195
1196 dpcm_remove_debugfs_state(dpcm);
1197
1198 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1199 list_del(&dpcm->list_be);
1200 list_del(&dpcm->list_fe);
1201 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1202 kfree(dpcm);
1203 }
1204 }
1205
1206 /* get BE for DAI widget and stream */
dpcm_get_be(struct snd_soc_card * card,struct snd_soc_dapm_widget * widget,int stream)1207 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1208 struct snd_soc_dapm_widget *widget, int stream)
1209 {
1210 struct snd_soc_pcm_runtime *be;
1211 struct snd_soc_dapm_widget *w;
1212 struct snd_soc_dai *dai;
1213 int i;
1214
1215 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1216
1217 for_each_card_rtds(card, be) {
1218
1219 if (!be->dai_link->no_pcm)
1220 continue;
1221
1222 for_each_rtd_dais(be, i, dai) {
1223 w = snd_soc_dai_get_widget(dai, stream);
1224
1225 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1226 w ? w->name : "(not set)");
1227
1228 if (w == widget)
1229 return be;
1230 }
1231 }
1232
1233 /* Widget provided is not a BE */
1234 return NULL;
1235 }
1236
widget_in_list(struct snd_soc_dapm_widget_list * list,struct snd_soc_dapm_widget * widget)1237 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1238 struct snd_soc_dapm_widget *widget)
1239 {
1240 struct snd_soc_dapm_widget *w;
1241 int i;
1242
1243 for_each_dapm_widgets(list, i, w)
1244 if (widget == w)
1245 return 1;
1246
1247 return 0;
1248 }
1249
dpcm_end_walk_at_be(struct snd_soc_dapm_widget * widget,enum snd_soc_dapm_direction dir)1250 static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1251 enum snd_soc_dapm_direction dir)
1252 {
1253 struct snd_soc_card *card = widget->dapm->card;
1254 struct snd_soc_pcm_runtime *rtd;
1255 int stream;
1256
1257 /* adjust dir to stream */
1258 if (dir == SND_SOC_DAPM_DIR_OUT)
1259 stream = SNDRV_PCM_STREAM_PLAYBACK;
1260 else
1261 stream = SNDRV_PCM_STREAM_CAPTURE;
1262
1263 rtd = dpcm_get_be(card, widget, stream);
1264 if (rtd)
1265 return true;
1266
1267 return false;
1268 }
1269
dpcm_path_get(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list)1270 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1271 int stream, struct snd_soc_dapm_widget_list **list)
1272 {
1273 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0);
1274 int paths;
1275
1276 if (fe->num_cpus > 1) {
1277 dev_err(fe->dev,
1278 "%s doesn't support Multi CPU yet\n", __func__);
1279 return -EINVAL;
1280 }
1281
1282 /* get number of valid DAI paths and their widgets */
1283 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1284 fe->card->component_chaining ?
1285 NULL : dpcm_end_walk_at_be);
1286
1287 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1288 stream ? "capture" : "playback");
1289
1290 return paths;
1291 }
1292
dpcm_path_put(struct snd_soc_dapm_widget_list ** list)1293 void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
1294 {
1295 snd_soc_dapm_dai_free_widgets(list);
1296 }
1297
dpcm_be_is_active(struct snd_soc_dpcm * dpcm,int stream,struct snd_soc_dapm_widget_list * list)1298 static bool dpcm_be_is_active(struct snd_soc_dpcm *dpcm, int stream,
1299 struct snd_soc_dapm_widget_list *list)
1300 {
1301 struct snd_soc_dapm_widget *widget;
1302 struct snd_soc_dai *dai;
1303 unsigned int i;
1304
1305 /* is there a valid DAI widget for this BE */
1306 for_each_rtd_dais(dpcm->be, i, dai) {
1307 widget = snd_soc_dai_get_widget(dai, stream);
1308
1309 /*
1310 * The BE is pruned only if none of the dai
1311 * widgets are in the active list.
1312 */
1313 if (widget && widget_in_list(list, widget))
1314 return true;
1315 }
1316
1317 return false;
1318 }
1319
dpcm_prune_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list_)1320 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1321 struct snd_soc_dapm_widget_list **list_)
1322 {
1323 struct snd_soc_dpcm *dpcm;
1324 int prune = 0;
1325
1326 /* Destroy any old FE <--> BE connections */
1327 for_each_dpcm_be(fe, stream, dpcm) {
1328 if (dpcm_be_is_active(dpcm, stream, *list_))
1329 continue;
1330
1331 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1332 stream ? "capture" : "playback",
1333 dpcm->be->dai_link->name, fe->dai_link->name);
1334 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1335 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1336 prune++;
1337 }
1338
1339 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1340 return prune;
1341 }
1342
dpcm_add_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list_)1343 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1344 struct snd_soc_dapm_widget_list **list_)
1345 {
1346 struct snd_soc_card *card = fe->card;
1347 struct snd_soc_dapm_widget_list *list = *list_;
1348 struct snd_soc_pcm_runtime *be;
1349 struct snd_soc_dapm_widget *widget;
1350 int i, new = 0, err;
1351
1352 /* Create any new FE <--> BE connections */
1353 for_each_dapm_widgets(list, i, widget) {
1354
1355 switch (widget->id) {
1356 case snd_soc_dapm_dai_in:
1357 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1358 continue;
1359 break;
1360 case snd_soc_dapm_dai_out:
1361 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1362 continue;
1363 break;
1364 default:
1365 continue;
1366 }
1367
1368 /* is there a valid BE rtd for this widget */
1369 be = dpcm_get_be(card, widget, stream);
1370 if (!be) {
1371 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1372 widget->name);
1373 continue;
1374 }
1375
1376 /* don't connect if FE is not running */
1377 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1378 continue;
1379
1380 /* newly connected FE and BE */
1381 err = dpcm_be_connect(fe, be, stream);
1382 if (err < 0) {
1383 dev_err(fe->dev, "ASoC: can't connect %s\n",
1384 widget->name);
1385 break;
1386 } else if (err == 0) /* already connected */
1387 continue;
1388
1389 /* new */
1390 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1391 new++;
1392 }
1393
1394 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1395 return new;
1396 }
1397
1398 /*
1399 * Find the corresponding BE DAIs that source or sink audio to this
1400 * FE substream.
1401 */
dpcm_process_paths(struct snd_soc_pcm_runtime * fe,int stream,struct snd_soc_dapm_widget_list ** list,int new)1402 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1403 int stream, struct snd_soc_dapm_widget_list **list, int new)
1404 {
1405 if (new)
1406 return dpcm_add_paths(fe, stream, list);
1407 else
1408 return dpcm_prune_paths(fe, stream, list);
1409 }
1410
dpcm_clear_pending_state(struct snd_soc_pcm_runtime * fe,int stream)1411 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1412 {
1413 struct snd_soc_dpcm *dpcm;
1414 unsigned long flags;
1415
1416 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1417 for_each_dpcm_be(fe, stream, dpcm)
1418 dpcm->be->dpcm[stream].runtime_update =
1419 SND_SOC_DPCM_UPDATE_NO;
1420 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1421 }
1422
dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime * fe,int stream)1423 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1424 int stream)
1425 {
1426 struct snd_soc_dpcm *dpcm;
1427
1428 /* disable any enabled and non active backends */
1429 for_each_dpcm_be(fe, stream, dpcm) {
1430
1431 struct snd_soc_pcm_runtime *be = dpcm->be;
1432 struct snd_pcm_substream *be_substream =
1433 snd_soc_dpcm_get_substream(be, stream);
1434
1435 if (be->dpcm[stream].users == 0)
1436 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1437 stream ? "capture" : "playback",
1438 be->dpcm[stream].state);
1439
1440 if (--be->dpcm[stream].users != 0)
1441 continue;
1442
1443 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1444 continue;
1445
1446 soc_pcm_close(be_substream);
1447 be_substream->runtime = NULL;
1448 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1449 }
1450 }
1451
dpcm_be_dai_startup(struct snd_soc_pcm_runtime * fe,int stream)1452 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1453 {
1454 struct snd_soc_dpcm *dpcm;
1455 int err, count = 0;
1456
1457 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1458 for_each_dpcm_be(fe, stream, dpcm) {
1459
1460 struct snd_soc_pcm_runtime *be = dpcm->be;
1461 struct snd_pcm_substream *be_substream =
1462 snd_soc_dpcm_get_substream(be, stream);
1463
1464 if (!be_substream) {
1465 dev_err(be->dev, "ASoC: no backend %s stream\n",
1466 stream ? "capture" : "playback");
1467 continue;
1468 }
1469
1470 /* is this op for this BE ? */
1471 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1472 continue;
1473
1474 /* first time the dpcm is open ? */
1475 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1476 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1477 stream ? "capture" : "playback",
1478 be->dpcm[stream].state);
1479
1480 if (be->dpcm[stream].users++ != 0)
1481 continue;
1482
1483 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1484 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1485 continue;
1486
1487 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1488 stream ? "capture" : "playback", be->dai_link->name);
1489
1490 be_substream->runtime = be->dpcm[stream].runtime;
1491 err = soc_pcm_open(be_substream);
1492 if (err < 0) {
1493 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1494 be->dpcm[stream].users--;
1495 if (be->dpcm[stream].users < 0)
1496 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1497 stream ? "capture" : "playback",
1498 be->dpcm[stream].state);
1499
1500 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1501 goto unwind;
1502 }
1503
1504 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1505 count++;
1506 }
1507
1508 return count;
1509
1510 unwind:
1511 /* disable any enabled and non active backends */
1512 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1513 struct snd_soc_pcm_runtime *be = dpcm->be;
1514 struct snd_pcm_substream *be_substream =
1515 snd_soc_dpcm_get_substream(be, stream);
1516
1517 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1518 continue;
1519
1520 if (be->dpcm[stream].users == 0)
1521 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1522 stream ? "capture" : "playback",
1523 be->dpcm[stream].state);
1524
1525 if (--be->dpcm[stream].users != 0)
1526 continue;
1527
1528 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1529 continue;
1530
1531 soc_pcm_close(be_substream);
1532 be_substream->runtime = NULL;
1533 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1534 }
1535
1536 return err;
1537 }
1538
dpcm_init_runtime_hw(struct snd_pcm_runtime * runtime,struct snd_soc_pcm_stream * stream)1539 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1540 struct snd_soc_pcm_stream *stream)
1541 {
1542 runtime->hw.rate_min = stream->rate_min;
1543 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1544 runtime->hw.channels_min = stream->channels_min;
1545 runtime->hw.channels_max = stream->channels_max;
1546 if (runtime->hw.formats)
1547 runtime->hw.formats &= stream->formats;
1548 else
1549 runtime->hw.formats = stream->formats;
1550 runtime->hw.rates = stream->rates;
1551 }
1552
dpcm_runtime_merge_format(struct snd_pcm_substream * substream,u64 * formats)1553 static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1554 u64 *formats)
1555 {
1556 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1557 struct snd_soc_dpcm *dpcm;
1558 struct snd_soc_dai *dai;
1559 int stream = substream->stream;
1560
1561 if (!fe->dai_link->dpcm_merged_format)
1562 return;
1563
1564 /*
1565 * It returns merged BE codec format
1566 * if FE want to use it (= dpcm_merged_format)
1567 */
1568
1569 for_each_dpcm_be(fe, stream, dpcm) {
1570 struct snd_soc_pcm_runtime *be = dpcm->be;
1571 struct snd_soc_pcm_stream *codec_stream;
1572 int i;
1573
1574 for_each_rtd_codec_dais(be, i, dai) {
1575 /*
1576 * Skip CODECs which don't support the current stream
1577 * type. See soc_pcm_init_runtime_hw() for more details
1578 */
1579 if (!snd_soc_dai_stream_valid(dai, stream))
1580 continue;
1581
1582 codec_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1583
1584 *formats &= codec_stream->formats;
1585 }
1586 }
1587 }
1588
dpcm_runtime_merge_chan(struct snd_pcm_substream * substream,unsigned int * channels_min,unsigned int * channels_max)1589 static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1590 unsigned int *channels_min,
1591 unsigned int *channels_max)
1592 {
1593 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1594 struct snd_soc_dpcm *dpcm;
1595 int stream = substream->stream;
1596
1597 if (!fe->dai_link->dpcm_merged_chan)
1598 return;
1599
1600 /*
1601 * It returns merged BE codec channel;
1602 * if FE want to use it (= dpcm_merged_chan)
1603 */
1604
1605 for_each_dpcm_be(fe, stream, dpcm) {
1606 struct snd_soc_pcm_runtime *be = dpcm->be;
1607 struct snd_soc_pcm_stream *codec_stream;
1608 struct snd_soc_pcm_stream *cpu_stream;
1609 struct snd_soc_dai *dai;
1610 int i;
1611
1612 for_each_rtd_cpu_dais(be, i, dai) {
1613 /*
1614 * Skip CPUs which don't support the current stream
1615 * type. See soc_pcm_init_runtime_hw() for more details
1616 */
1617 if (!snd_soc_dai_stream_valid(dai, stream))
1618 continue;
1619
1620 cpu_stream = snd_soc_dai_get_pcm_stream(dai, stream);
1621
1622 *channels_min = max(*channels_min,
1623 cpu_stream->channels_min);
1624 *channels_max = min(*channels_max,
1625 cpu_stream->channels_max);
1626 }
1627
1628 /*
1629 * chan min/max cannot be enforced if there are multiple CODEC
1630 * DAIs connected to a single CPU DAI, use CPU DAI's directly
1631 */
1632 if (be->num_codecs == 1) {
1633 codec_stream = snd_soc_dai_get_pcm_stream(asoc_rtd_to_codec(be, 0), stream);
1634
1635 *channels_min = max(*channels_min,
1636 codec_stream->channels_min);
1637 *channels_max = min(*channels_max,
1638 codec_stream->channels_max);
1639 }
1640 }
1641 }
1642
dpcm_runtime_merge_rate(struct snd_pcm_substream * substream,unsigned int * rates,unsigned int * rate_min,unsigned int * rate_max)1643 static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1644 unsigned int *rates,
1645 unsigned int *rate_min,
1646 unsigned int *rate_max)
1647 {
1648 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1649 struct snd_soc_dpcm *dpcm;
1650 int stream = substream->stream;
1651
1652 if (!fe->dai_link->dpcm_merged_rate)
1653 return;
1654
1655 /*
1656 * It returns merged BE codec channel;
1657 * if FE want to use it (= dpcm_merged_chan)
1658 */
1659
1660 for_each_dpcm_be(fe, stream, dpcm) {
1661 struct snd_soc_pcm_runtime *be = dpcm->be;
1662 struct snd_soc_pcm_stream *pcm;
1663 struct snd_soc_dai *dai;
1664 int i;
1665
1666 for_each_rtd_dais(be, i, dai) {
1667 /*
1668 * Skip DAIs which don't support the current stream
1669 * type. See soc_pcm_init_runtime_hw() for more details
1670 */
1671 if (!snd_soc_dai_stream_valid(dai, stream))
1672 continue;
1673
1674 pcm = snd_soc_dai_get_pcm_stream(dai, stream);
1675
1676 *rate_min = max(*rate_min, pcm->rate_min);
1677 *rate_max = min_not_zero(*rate_max, pcm->rate_max);
1678 *rates = snd_pcm_rate_mask_intersect(*rates, pcm->rates);
1679 }
1680 }
1681 }
1682
dpcm_set_fe_runtime(struct snd_pcm_substream * substream)1683 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1684 {
1685 struct snd_pcm_runtime *runtime = substream->runtime;
1686 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1687 struct snd_soc_dai *cpu_dai;
1688 int i;
1689
1690 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
1691 /*
1692 * Skip CPUs which don't support the current stream
1693 * type. See soc_pcm_init_runtime_hw() for more details
1694 */
1695 if (!snd_soc_dai_stream_valid(cpu_dai, substream->stream))
1696 continue;
1697
1698 dpcm_init_runtime_hw(runtime,
1699 snd_soc_dai_get_pcm_stream(cpu_dai,
1700 substream->stream));
1701 }
1702
1703 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1704 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1705 &runtime->hw.channels_max);
1706 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1707 &runtime->hw.rate_min, &runtime->hw.rate_max);
1708 }
1709
1710 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1711
1712 /* Set FE's runtime_update state; the state is protected via PCM stream lock
1713 * for avoiding the race with trigger callback.
1714 * If the state is unset and a trigger is pending while the previous operation,
1715 * process the pending trigger action here.
1716 */
dpcm_set_fe_update_state(struct snd_soc_pcm_runtime * fe,int stream,enum snd_soc_dpcm_update state)1717 static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1718 int stream, enum snd_soc_dpcm_update state)
1719 {
1720 struct snd_pcm_substream *substream =
1721 snd_soc_dpcm_get_substream(fe, stream);
1722
1723 snd_pcm_stream_lock_irq(substream);
1724 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1725 dpcm_fe_dai_do_trigger(substream,
1726 fe->dpcm[stream].trigger_pending - 1);
1727 fe->dpcm[stream].trigger_pending = 0;
1728 }
1729 fe->dpcm[stream].runtime_update = state;
1730 snd_pcm_stream_unlock_irq(substream);
1731 }
1732
dpcm_apply_symmetry(struct snd_pcm_substream * fe_substream,int stream)1733 static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1734 int stream)
1735 {
1736 struct snd_soc_dpcm *dpcm;
1737 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1738 struct snd_soc_dai *fe_cpu_dai;
1739 int err = 0;
1740 int i;
1741
1742 /* apply symmetry for FE */
1743 if (soc_pcm_has_symmetry(fe_substream))
1744 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1745
1746 for_each_rtd_cpu_dais (fe, i, fe_cpu_dai) {
1747 /* Symmetry only applies if we've got an active stream. */
1748 if (snd_soc_dai_active(fe_cpu_dai)) {
1749 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1750 if (err < 0)
1751 return err;
1752 }
1753 }
1754
1755 /* apply symmetry for BE */
1756 for_each_dpcm_be(fe, stream, dpcm) {
1757 struct snd_soc_pcm_runtime *be = dpcm->be;
1758 struct snd_pcm_substream *be_substream =
1759 snd_soc_dpcm_get_substream(be, stream);
1760 struct snd_soc_pcm_runtime *rtd;
1761 struct snd_soc_dai *dai;
1762 int i;
1763
1764 /* A backend may not have the requested substream */
1765 if (!be_substream)
1766 continue;
1767
1768 rtd = asoc_substream_to_rtd(be_substream);
1769 if (rtd->dai_link->be_hw_params_fixup)
1770 continue;
1771
1772 if (soc_pcm_has_symmetry(be_substream))
1773 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1774
1775 /* Symmetry only applies if we've got an active stream. */
1776 for_each_rtd_dais(rtd, i, dai) {
1777 if (snd_soc_dai_active(dai)) {
1778 err = soc_pcm_apply_symmetry(fe_substream, dai);
1779 if (err < 0)
1780 return err;
1781 }
1782 }
1783 }
1784
1785 return 0;
1786 }
1787
dpcm_fe_dai_startup(struct snd_pcm_substream * fe_substream)1788 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1789 {
1790 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
1791 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1792 int stream = fe_substream->stream, ret = 0;
1793
1794 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1795
1796 ret = dpcm_be_dai_startup(fe, stream);
1797 if (ret < 0) {
1798 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1799 goto be_err;
1800 }
1801
1802 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1803
1804 /* start the DAI frontend */
1805 ret = soc_pcm_open(fe_substream);
1806 if (ret < 0) {
1807 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1808 goto unwind;
1809 }
1810
1811 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1812
1813 dpcm_set_fe_runtime(fe_substream);
1814 snd_pcm_limit_hw_rates(runtime);
1815
1816 ret = dpcm_apply_symmetry(fe_substream, stream);
1817 if (ret < 0)
1818 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
1819 ret);
1820
1821 unwind:
1822 if (ret < 0)
1823 dpcm_be_dai_startup_unwind(fe, stream);
1824 be_err:
1825 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1826 return ret;
1827 }
1828
dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime * fe,int stream)1829 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1830 {
1831 struct snd_soc_dpcm *dpcm;
1832
1833 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1834 for_each_dpcm_be(fe, stream, dpcm) {
1835
1836 struct snd_soc_pcm_runtime *be = dpcm->be;
1837 struct snd_pcm_substream *be_substream =
1838 snd_soc_dpcm_get_substream(be, stream);
1839
1840 /* is this op for this BE ? */
1841 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1842 continue;
1843
1844 if (be->dpcm[stream].users == 0)
1845 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1846 stream ? "capture" : "playback",
1847 be->dpcm[stream].state);
1848
1849 if (--be->dpcm[stream].users != 0)
1850 continue;
1851
1852 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1853 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
1854 soc_pcm_hw_free(be_substream);
1855 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1856 }
1857
1858 dev_dbg(be->dev, "ASoC: close BE %s\n",
1859 be->dai_link->name);
1860
1861 soc_pcm_close(be_substream);
1862 be_substream->runtime = NULL;
1863
1864 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1865 }
1866 return 0;
1867 }
1868
dpcm_fe_dai_shutdown(struct snd_pcm_substream * substream)1869 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1870 {
1871 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1872 int stream = substream->stream;
1873
1874 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1875
1876 /* shutdown the BEs */
1877 dpcm_be_dai_shutdown(fe, stream);
1878
1879 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1880
1881 /* now shutdown the frontend */
1882 soc_pcm_close(substream);
1883
1884 /* run the stream event for each BE */
1885 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1886
1887 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1888 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1889 return 0;
1890 }
1891
dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime * fe,int stream)1892 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1893 {
1894 struct snd_soc_dpcm *dpcm;
1895
1896 /* only hw_params backends that are either sinks or sources
1897 * to this frontend DAI */
1898 for_each_dpcm_be(fe, stream, dpcm) {
1899
1900 struct snd_soc_pcm_runtime *be = dpcm->be;
1901 struct snd_pcm_substream *be_substream =
1902 snd_soc_dpcm_get_substream(be, stream);
1903
1904 /* is this op for this BE ? */
1905 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1906 continue;
1907
1908 /* only free hw when no longer used - check all FEs */
1909 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1910 continue;
1911
1912 /* do not free hw if this BE is used by other FE */
1913 if (be->dpcm[stream].users > 1)
1914 continue;
1915
1916 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1917 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1918 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1919 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1920 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
1921 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1922 continue;
1923
1924 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1925 be->dai_link->name);
1926
1927 soc_pcm_hw_free(be_substream);
1928
1929 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1930 }
1931
1932 return 0;
1933 }
1934
dpcm_fe_dai_hw_free(struct snd_pcm_substream * substream)1935 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1936 {
1937 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1938 int err, stream = substream->stream;
1939
1940 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1941 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1942
1943 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1944
1945 /* call hw_free on the frontend */
1946 err = soc_pcm_hw_free(substream);
1947 if (err < 0)
1948 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1949 fe->dai_link->name);
1950
1951 /* only hw_params backends that are either sinks or sources
1952 * to this frontend DAI */
1953 err = dpcm_be_dai_hw_free(fe, stream);
1954
1955 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1956 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
1957
1958 mutex_unlock(&fe->card->mutex);
1959 return 0;
1960 }
1961
dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime * fe,int stream)1962 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1963 {
1964 struct snd_soc_dpcm *dpcm;
1965 int ret;
1966
1967 for_each_dpcm_be(fe, stream, dpcm) {
1968
1969 struct snd_soc_pcm_runtime *be = dpcm->be;
1970 struct snd_pcm_substream *be_substream =
1971 snd_soc_dpcm_get_substream(be, stream);
1972
1973 /* is this op for this BE ? */
1974 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1975 continue;
1976
1977 /* copy params for each dpcm */
1978 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1979 sizeof(struct snd_pcm_hw_params));
1980
1981 /* perform any hw_params fixups */
1982 ret = snd_soc_link_be_hw_params_fixup(be, &dpcm->hw_params);
1983 if (ret < 0)
1984 goto unwind;
1985
1986 /* copy the fixed-up hw params for BE dai */
1987 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
1988 sizeof(struct snd_pcm_hw_params));
1989
1990 /* only allow hw_params() if no connected FEs are running */
1991 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1992 continue;
1993
1994 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1995 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1996 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1997 continue;
1998
1999 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2000 be->dai_link->name);
2001
2002 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2003 if (ret < 0) {
2004 dev_err(dpcm->be->dev,
2005 "ASoC: hw_params BE failed %d\n", ret);
2006 goto unwind;
2007 }
2008
2009 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2010 }
2011 return 0;
2012
2013 unwind:
2014 /* disable any enabled and non active backends */
2015 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2016 struct snd_soc_pcm_runtime *be = dpcm->be;
2017 struct snd_pcm_substream *be_substream =
2018 snd_soc_dpcm_get_substream(be, stream);
2019
2020 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2021 continue;
2022
2023 /* only allow hw_free() if no connected FEs are running */
2024 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2025 continue;
2026
2027 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2028 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2029 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2030 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2031 continue;
2032
2033 soc_pcm_hw_free(be_substream);
2034 }
2035
2036 return ret;
2037 }
2038
dpcm_fe_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)2039 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2040 struct snd_pcm_hw_params *params)
2041 {
2042 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2043 int ret, stream = substream->stream;
2044
2045 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2046 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2047
2048 memcpy(&fe->dpcm[stream].hw_params, params,
2049 sizeof(struct snd_pcm_hw_params));
2050 ret = dpcm_be_dai_hw_params(fe, stream);
2051 if (ret < 0) {
2052 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2053 goto out;
2054 }
2055
2056 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2057 fe->dai_link->name, params_rate(params),
2058 params_channels(params), params_format(params));
2059
2060 /* call hw_params on the frontend */
2061 ret = soc_pcm_hw_params(substream, params);
2062 if (ret < 0) {
2063 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2064 dpcm_be_dai_hw_free(fe, stream);
2065 } else
2066 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2067
2068 out:
2069 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2070 mutex_unlock(&fe->card->mutex);
2071 return ret;
2072 }
2073
dpcm_do_trigger(struct snd_soc_dpcm * dpcm,struct snd_pcm_substream * substream,int cmd)2074 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2075 struct snd_pcm_substream *substream, int cmd)
2076 {
2077 int ret;
2078
2079 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2080 dpcm->be->dai_link->name, cmd);
2081
2082 ret = soc_pcm_trigger(substream, cmd);
2083 if (ret < 0)
2084 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2085
2086 return ret;
2087 }
2088
dpcm_be_dai_trigger(struct snd_soc_pcm_runtime * fe,int stream,int cmd)2089 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2090 int cmd)
2091 {
2092 struct snd_soc_dpcm *dpcm;
2093 int ret = 0;
2094
2095 for_each_dpcm_be(fe, stream, dpcm) {
2096
2097 struct snd_soc_pcm_runtime *be = dpcm->be;
2098 struct snd_pcm_substream *be_substream =
2099 snd_soc_dpcm_get_substream(be, stream);
2100
2101 /* is this op for this BE ? */
2102 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2103 continue;
2104
2105 switch (cmd) {
2106 case SNDRV_PCM_TRIGGER_START:
2107 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2108 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2109 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2110 continue;
2111
2112 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2113 if (ret)
2114 return ret;
2115
2116 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2117 break;
2118 case SNDRV_PCM_TRIGGER_RESUME:
2119 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2120 continue;
2121
2122 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2123 if (ret)
2124 return ret;
2125
2126 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2127 break;
2128 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2129 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2130 continue;
2131
2132 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2133 if (ret)
2134 return ret;
2135
2136 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2137 break;
2138 case SNDRV_PCM_TRIGGER_STOP:
2139 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_START) &&
2140 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2141 continue;
2142
2143 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2144 continue;
2145
2146 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2147 if (ret)
2148 return ret;
2149
2150 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2151 break;
2152 case SNDRV_PCM_TRIGGER_SUSPEND:
2153 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2154 continue;
2155
2156 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2157 continue;
2158
2159 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2160 if (ret)
2161 return ret;
2162
2163 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2164 break;
2165 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2166 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2167 continue;
2168
2169 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2170 continue;
2171
2172 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2173 if (ret)
2174 return ret;
2175
2176 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2177 break;
2178 }
2179 }
2180
2181 return ret;
2182 }
2183 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2184
dpcm_dai_trigger_fe_be(struct snd_pcm_substream * substream,int cmd,bool fe_first)2185 static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
2186 int cmd, bool fe_first)
2187 {
2188 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2189 int ret;
2190
2191 /* call trigger on the frontend before the backend. */
2192 if (fe_first) {
2193 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2194 fe->dai_link->name, cmd);
2195
2196 ret = soc_pcm_trigger(substream, cmd);
2197 if (ret < 0)
2198 return ret;
2199
2200 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2201 return ret;
2202 }
2203
2204 /* call trigger on the frontend after the backend. */
2205 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2206 if (ret < 0)
2207 return ret;
2208
2209 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2210 fe->dai_link->name, cmd);
2211
2212 ret = soc_pcm_trigger(substream, cmd);
2213
2214 return ret;
2215 }
2216
dpcm_fe_dai_do_trigger(struct snd_pcm_substream * substream,int cmd)2217 static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2218 {
2219 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2220 int stream = substream->stream;
2221 int ret = 0;
2222 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2223
2224 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2225
2226 switch (trigger) {
2227 case SND_SOC_DPCM_TRIGGER_PRE:
2228 switch (cmd) {
2229 case SNDRV_PCM_TRIGGER_START:
2230 case SNDRV_PCM_TRIGGER_RESUME:
2231 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2232 case SNDRV_PCM_TRIGGER_DRAIN:
2233 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2234 break;
2235 case SNDRV_PCM_TRIGGER_STOP:
2236 case SNDRV_PCM_TRIGGER_SUSPEND:
2237 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2238 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2239 break;
2240 default:
2241 ret = -EINVAL;
2242 break;
2243 }
2244 break;
2245 case SND_SOC_DPCM_TRIGGER_POST:
2246 switch (cmd) {
2247 case SNDRV_PCM_TRIGGER_START:
2248 case SNDRV_PCM_TRIGGER_RESUME:
2249 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2250 case SNDRV_PCM_TRIGGER_DRAIN:
2251 ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
2252 break;
2253 case SNDRV_PCM_TRIGGER_STOP:
2254 case SNDRV_PCM_TRIGGER_SUSPEND:
2255 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2256 ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
2257 break;
2258 default:
2259 ret = -EINVAL;
2260 break;
2261 }
2262 break;
2263 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2264 /* bespoke trigger() - handles both FE and BEs */
2265
2266 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2267 fe->dai_link->name, cmd);
2268
2269 ret = snd_soc_pcm_dai_bespoke_trigger(substream, cmd);
2270 break;
2271 default:
2272 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2273 fe->dai_link->name);
2274 ret = -EINVAL;
2275 goto out;
2276 }
2277
2278 if (ret < 0) {
2279 dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
2280 cmd, ret);
2281 goto out;
2282 }
2283
2284 switch (cmd) {
2285 case SNDRV_PCM_TRIGGER_START:
2286 case SNDRV_PCM_TRIGGER_RESUME:
2287 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2288 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2289 break;
2290 case SNDRV_PCM_TRIGGER_STOP:
2291 case SNDRV_PCM_TRIGGER_SUSPEND:
2292 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2293 break;
2294 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2295 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2296 break;
2297 }
2298
2299 out:
2300 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2301 return ret;
2302 }
2303
dpcm_fe_dai_trigger(struct snd_pcm_substream * substream,int cmd)2304 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2305 {
2306 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2307 int stream = substream->stream;
2308
2309 /* if FE's runtime_update is already set, we're in race;
2310 * process this trigger later at exit
2311 */
2312 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2313 fe->dpcm[stream].trigger_pending = cmd + 1;
2314 return 0; /* delayed, assuming it's successful */
2315 }
2316
2317 /* we're alone, let's trigger */
2318 return dpcm_fe_dai_do_trigger(substream, cmd);
2319 }
2320
dpcm_be_dai_prepare(struct snd_soc_pcm_runtime * fe,int stream)2321 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2322 {
2323 struct snd_soc_dpcm *dpcm;
2324 int ret = 0;
2325
2326 for_each_dpcm_be(fe, stream, dpcm) {
2327
2328 struct snd_soc_pcm_runtime *be = dpcm->be;
2329 struct snd_pcm_substream *be_substream =
2330 snd_soc_dpcm_get_substream(be, stream);
2331
2332 /* is this op for this BE ? */
2333 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2334 continue;
2335
2336 if (!snd_soc_dpcm_can_be_prepared(fe, be, stream))
2337 continue;
2338
2339 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2340 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2341 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2342 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2343 continue;
2344
2345 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2346 be->dai_link->name);
2347
2348 ret = soc_pcm_prepare(be_substream);
2349 if (ret < 0) {
2350 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2351 ret);
2352 break;
2353 }
2354
2355 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2356 }
2357 return ret;
2358 }
2359
dpcm_fe_dai_prepare(struct snd_pcm_substream * substream)2360 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2361 {
2362 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
2363 int stream = substream->stream, ret = 0;
2364
2365 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2366
2367 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2368
2369 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2370
2371 /* there is no point preparing this FE if there are no BEs */
2372 if (list_empty(&fe->dpcm[stream].be_clients)) {
2373 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2374 fe->dai_link->name);
2375 ret = -EINVAL;
2376 goto out;
2377 }
2378
2379 ret = dpcm_be_dai_prepare(fe, stream);
2380 if (ret < 0)
2381 goto out;
2382
2383 /* call prepare on the frontend */
2384 ret = soc_pcm_prepare(substream);
2385 if (ret < 0) {
2386 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2387 fe->dai_link->name);
2388 goto out;
2389 }
2390
2391 /* run the stream event for each BE */
2392 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2393 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2394
2395 out:
2396 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2397 mutex_unlock(&fe->card->mutex);
2398
2399 return ret;
2400 }
2401
dpcm_run_update_shutdown(struct snd_soc_pcm_runtime * fe,int stream)2402 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2403 {
2404 struct snd_pcm_substream *substream =
2405 snd_soc_dpcm_get_substream(fe, stream);
2406 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2407 int err;
2408
2409 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2410 stream ? "capture" : "playback", fe->dai_link->name);
2411
2412 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2413 /* call bespoke trigger - FE takes care of all BE triggers */
2414 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2415 fe->dai_link->name);
2416
2417 err = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2418 if (err < 0)
2419 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2420 } else {
2421 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2422 fe->dai_link->name);
2423
2424 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2425 if (err < 0)
2426 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2427 }
2428
2429 err = dpcm_be_dai_hw_free(fe, stream);
2430 if (err < 0)
2431 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2432
2433 err = dpcm_be_dai_shutdown(fe, stream);
2434 if (err < 0)
2435 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2436
2437 /* run the stream event for each BE */
2438 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2439
2440 return 0;
2441 }
2442
dpcm_run_update_startup(struct snd_soc_pcm_runtime * fe,int stream)2443 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2444 {
2445 struct snd_pcm_substream *substream =
2446 snd_soc_dpcm_get_substream(fe, stream);
2447 struct snd_soc_dpcm *dpcm;
2448 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2449 int ret;
2450 unsigned long flags;
2451
2452 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2453 stream ? "capture" : "playback", fe->dai_link->name);
2454
2455 /* Only start the BE if the FE is ready */
2456 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2457 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2458 return -EINVAL;
2459
2460 /* startup must always be called for new BEs */
2461 ret = dpcm_be_dai_startup(fe, stream);
2462 if (ret < 0)
2463 goto disconnect;
2464
2465 /* keep going if FE state is > open */
2466 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2467 return 0;
2468
2469 ret = dpcm_be_dai_hw_params(fe, stream);
2470 if (ret < 0)
2471 goto close;
2472
2473 /* keep going if FE state is > hw_params */
2474 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2475 return 0;
2476
2477
2478 ret = dpcm_be_dai_prepare(fe, stream);
2479 if (ret < 0)
2480 goto hw_free;
2481
2482 /* run the stream event for each BE */
2483 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2484
2485 /* keep going if FE state is > prepare */
2486 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2487 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2488 return 0;
2489
2490 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2491 /* call trigger on the frontend - FE takes care of all BE triggers */
2492 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2493 fe->dai_link->name);
2494
2495 ret = snd_soc_pcm_dai_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2496 if (ret < 0) {
2497 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2498 goto hw_free;
2499 }
2500 } else {
2501 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2502 fe->dai_link->name);
2503
2504 ret = dpcm_be_dai_trigger(fe, stream,
2505 SNDRV_PCM_TRIGGER_START);
2506 if (ret < 0) {
2507 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2508 goto hw_free;
2509 }
2510 }
2511
2512 return 0;
2513
2514 hw_free:
2515 dpcm_be_dai_hw_free(fe, stream);
2516 close:
2517 dpcm_be_dai_shutdown(fe, stream);
2518 disconnect:
2519 /* disconnect any closed BEs */
2520 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2521 for_each_dpcm_be(fe, stream, dpcm) {
2522 struct snd_soc_pcm_runtime *be = dpcm->be;
2523 if (be->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2524 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2525 }
2526 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2527
2528 return ret;
2529 }
2530
soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime * fe,int new)2531 static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2532 {
2533 struct snd_soc_dapm_widget_list *list;
2534 int stream;
2535 int count, paths;
2536 int ret;
2537
2538 if (!fe->dai_link->dynamic)
2539 return 0;
2540
2541 if (fe->num_cpus > 1) {
2542 dev_err(fe->dev,
2543 "%s doesn't support Multi CPU yet\n", __func__);
2544 return -EINVAL;
2545 }
2546
2547 /* only check active links */
2548 if (!snd_soc_dai_active(asoc_rtd_to_cpu(fe, 0)))
2549 return 0;
2550
2551 /* DAPM sync will call this to update DSP paths */
2552 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2553 new ? "new" : "old", fe->dai_link->name);
2554
2555 for_each_pcm_streams(stream) {
2556
2557 /* skip if FE doesn't have playback/capture capability */
2558 if (!snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream) ||
2559 !snd_soc_dai_stream_valid(asoc_rtd_to_codec(fe, 0), stream))
2560 continue;
2561
2562 /* skip if FE isn't currently playing/capturing */
2563 if (!snd_soc_dai_stream_active(asoc_rtd_to_cpu(fe, 0), stream) ||
2564 !snd_soc_dai_stream_active(asoc_rtd_to_codec(fe, 0), stream))
2565 continue;
2566
2567 paths = dpcm_path_get(fe, stream, &list);
2568 if (paths < 0) {
2569 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2570 fe->dai_link->name,
2571 stream == SNDRV_PCM_STREAM_PLAYBACK ?
2572 "playback" : "capture");
2573 return paths;
2574 }
2575
2576 /* update any playback/capture paths */
2577 count = dpcm_process_paths(fe, stream, &list, new);
2578 if (count) {
2579 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2580 if (new)
2581 ret = dpcm_run_update_startup(fe, stream);
2582 else
2583 ret = dpcm_run_update_shutdown(fe, stream);
2584 if (ret < 0)
2585 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2586 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2587
2588 dpcm_clear_pending_state(fe, stream);
2589 dpcm_be_disconnect(fe, stream);
2590 }
2591
2592 dpcm_path_put(&list);
2593 }
2594
2595 return 0;
2596 }
2597
2598 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2599 * any DAI links.
2600 */
snd_soc_dpcm_runtime_update(struct snd_soc_card * card)2601 int snd_soc_dpcm_runtime_update(struct snd_soc_card *card)
2602 {
2603 struct snd_soc_pcm_runtime *fe;
2604 int ret = 0;
2605
2606 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2607 /* shutdown all old paths first */
2608 for_each_card_rtds(card, fe) {
2609 ret = soc_dpcm_fe_runtime_update(fe, 0);
2610 if (ret)
2611 goto out;
2612 }
2613
2614 /* bring new paths up */
2615 for_each_card_rtds(card, fe) {
2616 ret = soc_dpcm_fe_runtime_update(fe, 1);
2617 if (ret)
2618 goto out;
2619 }
2620
2621 out:
2622 mutex_unlock(&card->mutex);
2623 return ret;
2624 }
2625 EXPORT_SYMBOL_GPL(snd_soc_dpcm_runtime_update);
2626
dpcm_fe_dai_cleanup(struct snd_pcm_substream * fe_substream)2627 static void dpcm_fe_dai_cleanup(struct snd_pcm_substream *fe_substream)
2628 {
2629 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2630 struct snd_soc_dpcm *dpcm;
2631 int stream = fe_substream->stream;
2632
2633 /* mark FE's links ready to prune */
2634 for_each_dpcm_be(fe, stream, dpcm)
2635 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2636
2637 dpcm_be_disconnect(fe, stream);
2638
2639 fe->dpcm[stream].runtime = NULL;
2640 }
2641
dpcm_fe_dai_close(struct snd_pcm_substream * fe_substream)2642 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2643 {
2644 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2645 int ret;
2646
2647 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2648 ret = dpcm_fe_dai_shutdown(fe_substream);
2649
2650 dpcm_fe_dai_cleanup(fe_substream);
2651
2652 mutex_unlock(&fe->card->mutex);
2653 return ret;
2654 }
2655
dpcm_fe_dai_open(struct snd_pcm_substream * fe_substream)2656 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2657 {
2658 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(fe_substream);
2659 struct snd_soc_dapm_widget_list *list;
2660 int ret;
2661 int stream = fe_substream->stream;
2662
2663 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2664 fe->dpcm[stream].runtime = fe_substream->runtime;
2665
2666 ret = dpcm_path_get(fe, stream, &list);
2667 if (ret < 0) {
2668 goto open_end;
2669 } else if (ret == 0) {
2670 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2671 fe->dai_link->name, stream ? "capture" : "playback");
2672 }
2673
2674 /* calculate valid and active FE <-> BE dpcms */
2675 dpcm_process_paths(fe, stream, &list, 1);
2676
2677 ret = dpcm_fe_dai_startup(fe_substream);
2678 if (ret < 0)
2679 dpcm_fe_dai_cleanup(fe_substream);
2680
2681 dpcm_clear_pending_state(fe, stream);
2682 dpcm_path_put(&list);
2683 open_end:
2684 mutex_unlock(&fe->card->mutex);
2685 return ret;
2686 }
2687
2688 /* create a new pcm */
soc_new_pcm(struct snd_soc_pcm_runtime * rtd,int num)2689 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2690 {
2691 struct snd_soc_dai *codec_dai;
2692 struct snd_soc_dai *cpu_dai;
2693 struct snd_soc_component *component;
2694 struct snd_pcm *pcm;
2695 char new_name[64];
2696 int ret = 0, playback = 0, capture = 0;
2697 int stream;
2698 int i;
2699
2700 if (rtd->dai_link->dynamic && rtd->num_cpus > 1) {
2701 dev_err(rtd->dev,
2702 "DPCM doesn't support Multi CPU for Front-Ends yet\n");
2703 return -EINVAL;
2704 }
2705
2706 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2707 if (rtd->dai_link->dpcm_playback) {
2708 stream = SNDRV_PCM_STREAM_PLAYBACK;
2709
2710 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2711 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2712 playback = 1;
2713 break;
2714 }
2715 }
2716
2717 if (!playback) {
2718 dev_err(rtd->card->dev,
2719 "No CPU DAIs support playback for stream %s\n",
2720 rtd->dai_link->stream_name);
2721 return -EINVAL;
2722 }
2723 }
2724 if (rtd->dai_link->dpcm_capture) {
2725 stream = SNDRV_PCM_STREAM_CAPTURE;
2726
2727 for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
2728 if (snd_soc_dai_stream_valid(cpu_dai, stream)) {
2729 capture = 1;
2730 break;
2731 }
2732 }
2733
2734 if (!capture) {
2735 dev_err(rtd->card->dev,
2736 "No CPU DAIs support capture for stream %s\n",
2737 rtd->dai_link->stream_name);
2738 return -EINVAL;
2739 }
2740 }
2741 } else {
2742 /* Adapt stream for codec2codec links */
2743 int cpu_capture = rtd->dai_link->params ?
2744 SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
2745 int cpu_playback = rtd->dai_link->params ?
2746 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
2747
2748 for_each_rtd_codec_dais(rtd, i, codec_dai) {
2749 if (rtd->num_cpus == 1) {
2750 cpu_dai = asoc_rtd_to_cpu(rtd, 0);
2751 } else if (rtd->num_cpus == rtd->num_codecs) {
2752 cpu_dai = asoc_rtd_to_cpu(rtd, i);
2753 } else {
2754 dev_err(rtd->card->dev,
2755 "N cpus to M codecs link is not supported yet\n");
2756 return -EINVAL;
2757 }
2758
2759 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
2760 snd_soc_dai_stream_valid(cpu_dai, cpu_playback))
2761 playback = 1;
2762 if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
2763 snd_soc_dai_stream_valid(cpu_dai, cpu_capture))
2764 capture = 1;
2765 }
2766 }
2767
2768 if (rtd->dai_link->playback_only) {
2769 playback = 1;
2770 capture = 0;
2771 }
2772
2773 if (rtd->dai_link->capture_only) {
2774 playback = 0;
2775 capture = 1;
2776 }
2777
2778 /* create the PCM */
2779 if (rtd->dai_link->params) {
2780 snprintf(new_name, sizeof(new_name), "codec2codec(%s)",
2781 rtd->dai_link->stream_name);
2782
2783 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2784 playback, capture, &pcm);
2785 } else if (rtd->dai_link->no_pcm) {
2786 snprintf(new_name, sizeof(new_name), "(%s)",
2787 rtd->dai_link->stream_name);
2788
2789 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2790 playback, capture, &pcm);
2791 } else {
2792 if (rtd->dai_link->dynamic)
2793 snprintf(new_name, sizeof(new_name), "%s (*)",
2794 rtd->dai_link->stream_name);
2795 else
2796 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2797 rtd->dai_link->stream_name,
2798 (rtd->num_codecs > 1) ?
2799 "multicodec" : asoc_rtd_to_codec(rtd, 0)->name, num);
2800
2801 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2802 capture, &pcm);
2803 }
2804 if (ret < 0) {
2805 dev_err(rtd->card->dev, "ASoC: can't create pcm %s for dailink %s: %d\n",
2806 new_name, rtd->dai_link->name, ret);
2807 return ret;
2808 }
2809 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2810
2811 /* DAPM dai link stream work */
2812 if (rtd->dai_link->params)
2813 rtd->close_delayed_work_func = codec2codec_close_delayed_work;
2814 else
2815 rtd->close_delayed_work_func = snd_soc_close_delayed_work;
2816
2817 pcm->nonatomic = rtd->dai_link->nonatomic;
2818 rtd->pcm = pcm;
2819 pcm->private_data = rtd;
2820
2821 if (rtd->dai_link->no_pcm || rtd->dai_link->params) {
2822 if (playback)
2823 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2824 if (capture)
2825 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2826 goto out;
2827 }
2828
2829 /* ASoC PCM operations */
2830 if (rtd->dai_link->dynamic) {
2831 rtd->ops.open = dpcm_fe_dai_open;
2832 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2833 rtd->ops.prepare = dpcm_fe_dai_prepare;
2834 rtd->ops.trigger = dpcm_fe_dai_trigger;
2835 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2836 rtd->ops.close = dpcm_fe_dai_close;
2837 rtd->ops.pointer = soc_pcm_pointer;
2838 } else {
2839 rtd->ops.open = soc_pcm_open;
2840 rtd->ops.hw_params = soc_pcm_hw_params;
2841 rtd->ops.prepare = soc_pcm_prepare;
2842 rtd->ops.trigger = soc_pcm_trigger;
2843 rtd->ops.hw_free = soc_pcm_hw_free;
2844 rtd->ops.close = soc_pcm_close;
2845 rtd->ops.pointer = soc_pcm_pointer;
2846 }
2847
2848 for_each_rtd_components(rtd, i, component) {
2849 const struct snd_soc_component_driver *drv = component->driver;
2850
2851 if (drv->ioctl)
2852 rtd->ops.ioctl = snd_soc_pcm_component_ioctl;
2853 if (drv->sync_stop)
2854 rtd->ops.sync_stop = snd_soc_pcm_component_sync_stop;
2855 if (drv->copy_user)
2856 rtd->ops.copy_user = snd_soc_pcm_component_copy_user;
2857 if (drv->page)
2858 rtd->ops.page = snd_soc_pcm_component_page;
2859 if (drv->mmap)
2860 rtd->ops.mmap = snd_soc_pcm_component_mmap;
2861 if (drv->ack)
2862 rtd->ops.ack = snd_soc_pcm_component_ack;
2863 }
2864
2865 if (playback)
2866 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2867
2868 if (capture)
2869 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2870
2871 ret = snd_soc_pcm_component_new(rtd);
2872 if (ret < 0) {
2873 dev_err(rtd->dev, "ASoC: pcm %s constructor failed for dailink %s: %d\n",
2874 new_name, rtd->dai_link->name, ret);
2875 return ret;
2876 }
2877
2878 pcm->no_device_suspend = true;
2879 out:
2880 dev_dbg(rtd->card->dev, "%s <-> %s mapping ok\n",
2881 (rtd->num_codecs > 1) ? "multicodec" : asoc_rtd_to_codec(rtd, 0)->name,
2882 (rtd->num_cpus > 1) ? "multicpu" : asoc_rtd_to_cpu(rtd, 0)->name);
2883 return ret;
2884 }
2885
2886 /* is the current PCM operation for this FE ? */
snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime * fe,int stream)2887 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2888 {
2889 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2890 return 1;
2891 return 0;
2892 }
2893 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2894
2895 /* is the current PCM operation for this BE ? */
snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)2896 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2897 struct snd_soc_pcm_runtime *be, int stream)
2898 {
2899 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2900 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2901 be->dpcm[stream].runtime_update))
2902 return 1;
2903 return 0;
2904 }
2905 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2906
2907 /* get the substream for this BE */
2908 struct snd_pcm_substream *
snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime * be,int stream)2909 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2910 {
2911 return be->pcm->streams[stream].substream;
2912 }
2913 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2914
snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream,const enum snd_soc_dpcm_state * states,int num_states)2915 static int snd_soc_dpcm_check_state(struct snd_soc_pcm_runtime *fe,
2916 struct snd_soc_pcm_runtime *be,
2917 int stream,
2918 const enum snd_soc_dpcm_state *states,
2919 int num_states)
2920 {
2921 struct snd_soc_dpcm *dpcm;
2922 int state;
2923 int ret = 1;
2924 unsigned long flags;
2925 int i;
2926
2927 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2928 for_each_dpcm_fe(be, stream, dpcm) {
2929
2930 if (dpcm->fe == fe)
2931 continue;
2932
2933 state = dpcm->fe->dpcm[stream].state;
2934 for (i = 0; i < num_states; i++) {
2935 if (state == states[i]) {
2936 ret = 0;
2937 break;
2938 }
2939 }
2940 }
2941 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2942
2943 /* it's safe to do this BE DAI */
2944 return ret;
2945 }
2946
2947 /*
2948 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2949 * are not running, paused or suspended for the specified stream direction.
2950 */
snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)2951 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2952 struct snd_soc_pcm_runtime *be, int stream)
2953 {
2954 const enum snd_soc_dpcm_state state[] = {
2955 SND_SOC_DPCM_STATE_START,
2956 SND_SOC_DPCM_STATE_PAUSED,
2957 SND_SOC_DPCM_STATE_SUSPEND,
2958 };
2959
2960 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2961 }
2962 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2963
2964 /*
2965 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2966 * running, paused or suspended for the specified stream direction.
2967 */
snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)2968 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2969 struct snd_soc_pcm_runtime *be, int stream)
2970 {
2971 const enum snd_soc_dpcm_state state[] = {
2972 SND_SOC_DPCM_STATE_START,
2973 SND_SOC_DPCM_STATE_PAUSED,
2974 SND_SOC_DPCM_STATE_SUSPEND,
2975 SND_SOC_DPCM_STATE_PREPARE,
2976 };
2977
2978 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2979 }
2980 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2981
2982 /*
2983 * We can only prepare a BE DAI if any of it's FE are not prepared,
2984 * running or paused for the specified stream direction.
2985 */
snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime * fe,struct snd_soc_pcm_runtime * be,int stream)2986 int snd_soc_dpcm_can_be_prepared(struct snd_soc_pcm_runtime *fe,
2987 struct snd_soc_pcm_runtime *be, int stream)
2988 {
2989 const enum snd_soc_dpcm_state state[] = {
2990 SND_SOC_DPCM_STATE_START,
2991 SND_SOC_DPCM_STATE_PAUSED,
2992 SND_SOC_DPCM_STATE_PREPARE,
2993 };
2994
2995 return snd_soc_dpcm_check_state(fe, be, stream, state, ARRAY_SIZE(state));
2996 }
2997 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_prepared);
2998