ASoC: pcm: Refactor soc_pcm_apply_msb for multicodecs
[deliverable/linux.git] / sound / soc / soc-pcm.c
1 /*
2 * soc-pcm.c -- ALSA SoC PCM
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Authors: Liam Girdwood <lrg@ti.com>
10 * Mark Brown <broonie@opensource.wolfsonmicro.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <linux/export.h>
27 #include <linux/debugfs.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dpcm.h>
33 #include <sound/initval.h>
34
35 #define DPCM_MAX_BE_USERS 8
36
37 /**
38 * snd_soc_runtime_activate() - Increment active count for PCM runtime components
39 * @rtd: ASoC PCM runtime that is activated
40 * @stream: Direction of the PCM stream
41 *
42 * Increments the active count for all the DAIs and components attached to a PCM
43 * runtime. Should typically be called when a stream is opened.
44 *
45 * Must be called with the rtd->pcm_mutex being held
46 */
47 void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
48 {
49 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
50 struct snd_soc_dai *codec_dai = rtd->codec_dai;
51
52 lockdep_assert_held(&rtd->pcm_mutex);
53
54 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
55 cpu_dai->playback_active++;
56 codec_dai->playback_active++;
57 } else {
58 cpu_dai->capture_active++;
59 codec_dai->capture_active++;
60 }
61
62 cpu_dai->active++;
63 codec_dai->active++;
64 cpu_dai->component->active++;
65 codec_dai->component->active++;
66 }
67
68 /**
69 * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
70 * @rtd: ASoC PCM runtime that is deactivated
71 * @stream: Direction of the PCM stream
72 *
73 * Decrements the active count for all the DAIs and components attached to a PCM
74 * runtime. Should typically be called when a stream is closed.
75 *
76 * Must be called with the rtd->pcm_mutex being held
77 */
78 void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
79 {
80 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
81 struct snd_soc_dai *codec_dai = rtd->codec_dai;
82
83 lockdep_assert_held(&rtd->pcm_mutex);
84
85 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
86 cpu_dai->playback_active--;
87 codec_dai->playback_active--;
88 } else {
89 cpu_dai->capture_active--;
90 codec_dai->capture_active--;
91 }
92
93 cpu_dai->active--;
94 codec_dai->active--;
95 cpu_dai->component->active--;
96 codec_dai->component->active--;
97 }
98
99 /**
100 * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
101 * @rtd: The ASoC PCM runtime that should be checked.
102 *
103 * This function checks whether the power down delay should be ignored for a
104 * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
105 * been configured to ignore the delay, or if none of the components benefits
106 * from having the delay.
107 */
108 bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
109 {
110 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
111 return true;
112
113 return rtd->cpu_dai->component->ignore_pmdown_time &&
114 rtd->codec_dai->component->ignore_pmdown_time;
115 }
116
117 /**
118 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
119 * @substream: the pcm substream
120 * @hw: the hardware parameters
121 *
122 * Sets the substream runtime hardware parameters.
123 */
124 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
125 const struct snd_pcm_hardware *hw)
126 {
127 struct snd_pcm_runtime *runtime = substream->runtime;
128 runtime->hw.info = hw->info;
129 runtime->hw.formats = hw->formats;
130 runtime->hw.period_bytes_min = hw->period_bytes_min;
131 runtime->hw.period_bytes_max = hw->period_bytes_max;
132 runtime->hw.periods_min = hw->periods_min;
133 runtime->hw.periods_max = hw->periods_max;
134 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
135 runtime->hw.fifo_size = hw->fifo_size;
136 return 0;
137 }
138 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
139
140 /* DPCM stream event, send event to FE and all active BEs. */
141 int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
142 int event)
143 {
144 struct snd_soc_dpcm *dpcm;
145
146 list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
147
148 struct snd_soc_pcm_runtime *be = dpcm->be;
149
150 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
151 be->dai_link->name, event, dir);
152
153 snd_soc_dapm_stream_event(be, dir, event);
154 }
155
156 snd_soc_dapm_stream_event(fe, dir, event);
157
158 return 0;
159 }
160
161 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
162 struct snd_soc_dai *soc_dai)
163 {
164 struct snd_soc_pcm_runtime *rtd = substream->private_data;
165 int ret;
166
167 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
168 rtd->dai_link->symmetric_rates)) {
169 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
170 soc_dai->rate);
171
172 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
173 SNDRV_PCM_HW_PARAM_RATE,
174 soc_dai->rate, soc_dai->rate);
175 if (ret < 0) {
176 dev_err(soc_dai->dev,
177 "ASoC: Unable to apply rate constraint: %d\n",
178 ret);
179 return ret;
180 }
181 }
182
183 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
184 rtd->dai_link->symmetric_channels)) {
185 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
186 soc_dai->channels);
187
188 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
189 SNDRV_PCM_HW_PARAM_CHANNELS,
190 soc_dai->channels,
191 soc_dai->channels);
192 if (ret < 0) {
193 dev_err(soc_dai->dev,
194 "ASoC: Unable to apply channel symmetry constraint: %d\n",
195 ret);
196 return ret;
197 }
198 }
199
200 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
201 rtd->dai_link->symmetric_samplebits)) {
202 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
203 soc_dai->sample_bits);
204
205 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
206 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
207 soc_dai->sample_bits,
208 soc_dai->sample_bits);
209 if (ret < 0) {
210 dev_err(soc_dai->dev,
211 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
212 ret);
213 return ret;
214 }
215 }
216
217 return 0;
218 }
219
220 static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
221 struct snd_pcm_hw_params *params)
222 {
223 struct snd_soc_pcm_runtime *rtd = substream->private_data;
224 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
225 struct snd_soc_dai *codec_dai = rtd->codec_dai;
226 unsigned int rate, channels, sample_bits, symmetry;
227
228 rate = params_rate(params);
229 channels = params_channels(params);
230 sample_bits = snd_pcm_format_physical_width(params_format(params));
231
232 /* reject unmatched parameters when applying symmetry */
233 symmetry = cpu_dai->driver->symmetric_rates ||
234 codec_dai->driver->symmetric_rates ||
235 rtd->dai_link->symmetric_rates;
236 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
237 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
238 cpu_dai->rate, rate);
239 return -EINVAL;
240 }
241
242 symmetry = cpu_dai->driver->symmetric_channels ||
243 codec_dai->driver->symmetric_channels ||
244 rtd->dai_link->symmetric_channels;
245 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
246 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
247 cpu_dai->channels, channels);
248 return -EINVAL;
249 }
250
251 symmetry = cpu_dai->driver->symmetric_samplebits ||
252 codec_dai->driver->symmetric_samplebits ||
253 rtd->dai_link->symmetric_samplebits;
254 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
255 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
256 cpu_dai->sample_bits, sample_bits);
257 return -EINVAL;
258 }
259
260 return 0;
261 }
262
263 static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
264 {
265 struct snd_soc_pcm_runtime *rtd = substream->private_data;
266 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
267 struct snd_soc_dai_driver *codec_driver = rtd->codec_dai->driver;
268 struct snd_soc_dai_link *link = rtd->dai_link;
269
270 return cpu_driver->symmetric_rates || codec_driver->symmetric_rates ||
271 link->symmetric_rates || cpu_driver->symmetric_channels ||
272 codec_driver->symmetric_channels || link->symmetric_channels ||
273 cpu_driver->symmetric_samplebits ||
274 codec_driver->symmetric_samplebits ||
275 link->symmetric_samplebits;
276 }
277
278 /*
279 * List of sample sizes that might go over the bus for parameter
280 * application. There ought to be a wildcard sample size for things
281 * like the DAC/ADC resolution to use but there isn't right now.
282 */
283 static int sample_sizes[] = {
284 24, 32,
285 };
286
287 static void soc_pcm_set_msb(struct snd_pcm_substream *substream,
288 struct snd_soc_dai *dai, int bits)
289 {
290 int ret, i;
291
292 if (!bits)
293 return;
294
295 for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
296 if (bits >= sample_sizes[i])
297 continue;
298
299 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
300 sample_sizes[i], bits);
301 if (ret != 0)
302 dev_warn(dai->dev,
303 "ASoC: Failed to set MSB %d/%d: %d\n",
304 bits, sample_sizes[i], ret);
305 }
306 }
307
308 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
309 {
310 struct snd_soc_pcm_runtime *rtd = substream->private_data;
311 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
312 struct snd_soc_dai *codec_dai = rtd->codec_dai;
313 unsigned int bits = 0, cpu_bits;
314
315 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
316 bits = codec_dai->driver->playback.sig_bits;
317 cpu_bits = cpu_dai->driver->playback.sig_bits;
318 } else {
319 bits = codec_dai->driver->capture.sig_bits;
320 cpu_bits = cpu_dai->driver->capture.sig_bits;
321 }
322
323 soc_pcm_set_msb(substream, codec_dai, bits);
324 soc_pcm_set_msb(substream, cpu_dai, cpu_bits);
325 }
326
327 static void soc_pcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
328 struct snd_soc_pcm_stream *codec_stream,
329 struct snd_soc_pcm_stream *cpu_stream)
330 {
331 struct snd_pcm_hardware *hw = &runtime->hw;
332
333 hw->channels_min = max(codec_stream->channels_min,
334 cpu_stream->channels_min);
335 hw->channels_max = min(codec_stream->channels_max,
336 cpu_stream->channels_max);
337 if (hw->formats)
338 hw->formats &= codec_stream->formats & cpu_stream->formats;
339 else
340 hw->formats = codec_stream->formats & cpu_stream->formats;
341 hw->rates = snd_pcm_rate_mask_intersect(codec_stream->rates,
342 cpu_stream->rates);
343
344 hw->rate_min = 0;
345 hw->rate_max = UINT_MAX;
346
347 snd_pcm_limit_hw_rates(runtime);
348
349 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
350 hw->rate_min = max(hw->rate_min, codec_stream->rate_min);
351 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
352 hw->rate_max = min_not_zero(hw->rate_max, codec_stream->rate_max);
353 }
354
355 /*
356 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
357 * then initialized and any private data can be allocated. This also calls
358 * startup for the cpu DAI, platform, machine and codec DAI.
359 */
360 static int soc_pcm_open(struct snd_pcm_substream *substream)
361 {
362 struct snd_soc_pcm_runtime *rtd = substream->private_data;
363 struct snd_pcm_runtime *runtime = substream->runtime;
364 struct snd_soc_platform *platform = rtd->platform;
365 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
366 struct snd_soc_dai *codec_dai = rtd->codec_dai;
367 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
368 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
369 int ret = 0;
370
371 pinctrl_pm_select_default_state(cpu_dai->dev);
372 pinctrl_pm_select_default_state(codec_dai->dev);
373 pm_runtime_get_sync(cpu_dai->dev);
374 pm_runtime_get_sync(codec_dai->dev);
375 pm_runtime_get_sync(platform->dev);
376
377 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
378
379 /* startup the audio subsystem */
380 if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
381 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
382 if (ret < 0) {
383 dev_err(cpu_dai->dev, "ASoC: can't open interface"
384 " %s: %d\n", cpu_dai->name, ret);
385 goto out;
386 }
387 }
388
389 if (platform->driver->ops && platform->driver->ops->open) {
390 ret = platform->driver->ops->open(substream);
391 if (ret < 0) {
392 dev_err(platform->dev, "ASoC: can't open platform"
393 " %s: %d\n", platform->name, ret);
394 goto platform_err;
395 }
396 }
397
398 if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
399 ret = codec_dai->driver->ops->startup(substream, codec_dai);
400 if (ret < 0) {
401 dev_err(codec_dai->dev, "ASoC: can't open codec"
402 " %s: %d\n", codec_dai->name, ret);
403 goto codec_dai_err;
404 }
405 }
406
407 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
408 ret = rtd->dai_link->ops->startup(substream);
409 if (ret < 0) {
410 pr_err("ASoC: %s startup failed: %d\n",
411 rtd->dai_link->name, ret);
412 goto machine_err;
413 }
414 }
415
416 /* Dynamic PCM DAI links compat checks use dynamic capabilities */
417 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
418 goto dynamic;
419
420 /* Check that the codec and cpu DAIs are compatible */
421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
422 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->playback,
423 &cpu_dai_drv->playback);
424 } else {
425 soc_pcm_init_runtime_hw(runtime, &codec_dai_drv->capture,
426 &cpu_dai_drv->capture);
427 }
428
429 if (soc_pcm_has_symmetry(substream))
430 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
431
432 ret = -EINVAL;
433 if (!runtime->hw.rates) {
434 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
435 codec_dai->name, cpu_dai->name);
436 goto config_err;
437 }
438 if (!runtime->hw.formats) {
439 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
440 codec_dai->name, cpu_dai->name);
441 goto config_err;
442 }
443 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
444 runtime->hw.channels_min > runtime->hw.channels_max) {
445 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
446 codec_dai->name, cpu_dai->name);
447 goto config_err;
448 }
449
450 soc_pcm_apply_msb(substream);
451
452 /* Symmetry only applies if we've already got an active stream. */
453 if (cpu_dai->active) {
454 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
455 if (ret != 0)
456 goto config_err;
457 }
458
459 if (codec_dai->active) {
460 ret = soc_pcm_apply_symmetry(substream, codec_dai);
461 if (ret != 0)
462 goto config_err;
463 }
464
465 pr_debug("ASoC: %s <-> %s info:\n",
466 codec_dai->name, cpu_dai->name);
467 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
468 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
469 runtime->hw.channels_max);
470 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
471 runtime->hw.rate_max);
472
473 dynamic:
474
475 snd_soc_runtime_activate(rtd, substream->stream);
476
477 mutex_unlock(&rtd->pcm_mutex);
478 return 0;
479
480 config_err:
481 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
482 rtd->dai_link->ops->shutdown(substream);
483
484 machine_err:
485 if (codec_dai->driver->ops->shutdown)
486 codec_dai->driver->ops->shutdown(substream, codec_dai);
487
488 codec_dai_err:
489 if (platform->driver->ops && platform->driver->ops->close)
490 platform->driver->ops->close(substream);
491
492 platform_err:
493 if (cpu_dai->driver->ops->shutdown)
494 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
495 out:
496 mutex_unlock(&rtd->pcm_mutex);
497
498 pm_runtime_put(platform->dev);
499 pm_runtime_put(codec_dai->dev);
500 pm_runtime_put(cpu_dai->dev);
501 if (!codec_dai->active)
502 pinctrl_pm_select_sleep_state(codec_dai->dev);
503 if (!cpu_dai->active)
504 pinctrl_pm_select_sleep_state(cpu_dai->dev);
505
506 return ret;
507 }
508
509 /*
510 * Power down the audio subsystem pmdown_time msecs after close is called.
511 * This is to ensure there are no pops or clicks in between any music tracks
512 * due to DAPM power cycling.
513 */
514 static void close_delayed_work(struct work_struct *work)
515 {
516 struct snd_soc_pcm_runtime *rtd =
517 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
518 struct snd_soc_dai *codec_dai = rtd->codec_dai;
519
520 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
521
522 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
523 codec_dai->driver->playback.stream_name,
524 codec_dai->playback_active ? "active" : "inactive",
525 rtd->pop_wait ? "yes" : "no");
526
527 /* are we waiting on this codec DAI stream */
528 if (rtd->pop_wait == 1) {
529 rtd->pop_wait = 0;
530 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
531 SND_SOC_DAPM_STREAM_STOP);
532 }
533
534 mutex_unlock(&rtd->pcm_mutex);
535 }
536
537 /*
538 * Called by ALSA when a PCM substream is closed. Private data can be
539 * freed here. The cpu DAI, codec DAI, machine and platform are also
540 * shutdown.
541 */
542 static int soc_pcm_close(struct snd_pcm_substream *substream)
543 {
544 struct snd_soc_pcm_runtime *rtd = substream->private_data;
545 struct snd_soc_platform *platform = rtd->platform;
546 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
547 struct snd_soc_dai *codec_dai = rtd->codec_dai;
548
549 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
550
551 snd_soc_runtime_deactivate(rtd, substream->stream);
552
553 /* clear the corresponding DAIs rate when inactive */
554 if (!cpu_dai->active)
555 cpu_dai->rate = 0;
556
557 if (!codec_dai->active)
558 codec_dai->rate = 0;
559
560 if (cpu_dai->driver->ops->shutdown)
561 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
562
563 if (codec_dai->driver->ops->shutdown)
564 codec_dai->driver->ops->shutdown(substream, codec_dai);
565
566 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
567 rtd->dai_link->ops->shutdown(substream);
568
569 if (platform->driver->ops && platform->driver->ops->close)
570 platform->driver->ops->close(substream);
571
572 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
573 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
574 /* powered down playback stream now */
575 snd_soc_dapm_stream_event(rtd,
576 SNDRV_PCM_STREAM_PLAYBACK,
577 SND_SOC_DAPM_STREAM_STOP);
578 } else {
579 /* start delayed pop wq here for playback streams */
580 rtd->pop_wait = 1;
581 queue_delayed_work(system_power_efficient_wq,
582 &rtd->delayed_work,
583 msecs_to_jiffies(rtd->pmdown_time));
584 }
585 } else {
586 /* capture streams can be powered down now */
587 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
588 SND_SOC_DAPM_STREAM_STOP);
589 }
590
591 mutex_unlock(&rtd->pcm_mutex);
592
593 pm_runtime_put(platform->dev);
594 pm_runtime_put(codec_dai->dev);
595 pm_runtime_put(cpu_dai->dev);
596 if (!codec_dai->active)
597 pinctrl_pm_select_sleep_state(codec_dai->dev);
598 if (!cpu_dai->active)
599 pinctrl_pm_select_sleep_state(cpu_dai->dev);
600
601 return 0;
602 }
603
604 /*
605 * Called by ALSA when the PCM substream is prepared, can set format, sample
606 * rate, etc. This function is non atomic and can be called multiple times,
607 * it can refer to the runtime info.
608 */
609 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
610 {
611 struct snd_soc_pcm_runtime *rtd = substream->private_data;
612 struct snd_soc_platform *platform = rtd->platform;
613 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
614 struct snd_soc_dai *codec_dai = rtd->codec_dai;
615 int ret = 0;
616
617 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
618
619 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
620 ret = rtd->dai_link->ops->prepare(substream);
621 if (ret < 0) {
622 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
623 " %d\n", ret);
624 goto out;
625 }
626 }
627
628 if (platform->driver->ops && platform->driver->ops->prepare) {
629 ret = platform->driver->ops->prepare(substream);
630 if (ret < 0) {
631 dev_err(platform->dev, "ASoC: platform prepare error:"
632 " %d\n", ret);
633 goto out;
634 }
635 }
636
637 if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
638 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
639 if (ret < 0) {
640 dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
641 ret);
642 goto out;
643 }
644 }
645
646 if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
647 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
648 if (ret < 0) {
649 dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
650 ret);
651 goto out;
652 }
653 }
654
655 /* cancel any delayed stream shutdown that is pending */
656 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
657 rtd->pop_wait) {
658 rtd->pop_wait = 0;
659 cancel_delayed_work(&rtd->delayed_work);
660 }
661
662 snd_soc_dapm_stream_event(rtd, substream->stream,
663 SND_SOC_DAPM_STREAM_START);
664
665 snd_soc_dai_digital_mute(codec_dai, 0, substream->stream);
666
667 out:
668 mutex_unlock(&rtd->pcm_mutex);
669 return ret;
670 }
671
672 /*
673 * Called by ALSA when the hardware params are set by application. This
674 * function can also be called multiple times and can allocate buffers
675 * (using snd_pcm_lib_* ). It's non-atomic.
676 */
677 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
678 struct snd_pcm_hw_params *params)
679 {
680 struct snd_soc_pcm_runtime *rtd = substream->private_data;
681 struct snd_soc_platform *platform = rtd->platform;
682 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
683 struct snd_soc_dai *codec_dai = rtd->codec_dai;
684 int ret = 0;
685
686 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
687
688 ret = soc_pcm_params_symmetry(substream, params);
689 if (ret)
690 goto out;
691
692 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
693 ret = rtd->dai_link->ops->hw_params(substream, params);
694 if (ret < 0) {
695 dev_err(rtd->card->dev, "ASoC: machine hw_params"
696 " failed: %d\n", ret);
697 goto out;
698 }
699 }
700
701 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_params) {
702 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
703 if (ret < 0) {
704 dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
705 " %d\n", codec_dai->name, ret);
706 goto codec_err;
707 }
708 }
709
710 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_params) {
711 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
712 if (ret < 0) {
713 dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
714 cpu_dai->name, ret);
715 goto interface_err;
716 }
717 }
718
719 if (platform->driver->ops && platform->driver->ops->hw_params) {
720 ret = platform->driver->ops->hw_params(substream, params);
721 if (ret < 0) {
722 dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
723 platform->name, ret);
724 goto platform_err;
725 }
726 }
727
728 /* store the parameters for each DAIs */
729 cpu_dai->rate = params_rate(params);
730 cpu_dai->channels = params_channels(params);
731 cpu_dai->sample_bits =
732 snd_pcm_format_physical_width(params_format(params));
733
734 codec_dai->rate = params_rate(params);
735 codec_dai->channels = params_channels(params);
736 codec_dai->sample_bits =
737 snd_pcm_format_physical_width(params_format(params));
738
739 out:
740 mutex_unlock(&rtd->pcm_mutex);
741 return ret;
742
743 platform_err:
744 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
745 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
746
747 interface_err:
748 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
749 codec_dai->driver->ops->hw_free(substream, codec_dai);
750
751 codec_err:
752 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
753 rtd->dai_link->ops->hw_free(substream);
754
755 mutex_unlock(&rtd->pcm_mutex);
756 return ret;
757 }
758
759 /*
760 * Frees resources allocated by hw_params, can be called multiple times
761 */
762 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
763 {
764 struct snd_soc_pcm_runtime *rtd = substream->private_data;
765 struct snd_soc_platform *platform = rtd->platform;
766 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
767 struct snd_soc_dai *codec_dai = rtd->codec_dai;
768 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
769
770 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
771
772 /* clear the corresponding DAIs parameters when going to be inactive */
773 if (cpu_dai->active == 1) {
774 cpu_dai->rate = 0;
775 cpu_dai->channels = 0;
776 cpu_dai->sample_bits = 0;
777 }
778
779 if (codec_dai->active == 1) {
780 codec_dai->rate = 0;
781 codec_dai->channels = 0;
782 codec_dai->sample_bits = 0;
783 }
784
785 /* apply codec digital mute */
786 if ((playback && codec_dai->playback_active == 1) ||
787 (!playback && codec_dai->capture_active == 1))
788 snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
789
790 /* free any machine hw params */
791 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
792 rtd->dai_link->ops->hw_free(substream);
793
794 /* free any DMA resources */
795 if (platform->driver->ops && platform->driver->ops->hw_free)
796 platform->driver->ops->hw_free(substream);
797
798 /* now free hw params for the DAIs */
799 if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
800 codec_dai->driver->ops->hw_free(substream, codec_dai);
801
802 if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
803 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
804
805 mutex_unlock(&rtd->pcm_mutex);
806 return 0;
807 }
808
809 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
810 {
811 struct snd_soc_pcm_runtime *rtd = substream->private_data;
812 struct snd_soc_platform *platform = rtd->platform;
813 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
814 struct snd_soc_dai *codec_dai = rtd->codec_dai;
815 int ret;
816
817 if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
818 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
819 if (ret < 0)
820 return ret;
821 }
822
823 if (platform->driver->ops && platform->driver->ops->trigger) {
824 ret = platform->driver->ops->trigger(substream, cmd);
825 if (ret < 0)
826 return ret;
827 }
828
829 if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
830 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
831 if (ret < 0)
832 return ret;
833 }
834
835 if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
836 ret = rtd->dai_link->ops->trigger(substream, cmd);
837 if (ret < 0)
838 return ret;
839 }
840
841 return 0;
842 }
843
844 static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
845 int cmd)
846 {
847 struct snd_soc_pcm_runtime *rtd = substream->private_data;
848 struct snd_soc_platform *platform = rtd->platform;
849 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
850 struct snd_soc_dai *codec_dai = rtd->codec_dai;
851 int ret;
852
853 if (codec_dai->driver->ops &&
854 codec_dai->driver->ops->bespoke_trigger) {
855 ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
856 if (ret < 0)
857 return ret;
858 }
859
860 if (platform->driver->bespoke_trigger) {
861 ret = platform->driver->bespoke_trigger(substream, cmd);
862 if (ret < 0)
863 return ret;
864 }
865
866 if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
867 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
868 if (ret < 0)
869 return ret;
870 }
871 return 0;
872 }
873 /*
874 * soc level wrapper for pointer callback
875 * If cpu_dai, codec_dai, platform driver has the delay callback, than
876 * the runtime->delay will be updated accordingly.
877 */
878 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
879 {
880 struct snd_soc_pcm_runtime *rtd = substream->private_data;
881 struct snd_soc_platform *platform = rtd->platform;
882 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
883 struct snd_soc_dai *codec_dai = rtd->codec_dai;
884 struct snd_pcm_runtime *runtime = substream->runtime;
885 snd_pcm_uframes_t offset = 0;
886 snd_pcm_sframes_t delay = 0;
887
888 if (platform->driver->ops && platform->driver->ops->pointer)
889 offset = platform->driver->ops->pointer(substream);
890
891 if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
892 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
893
894 if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
895 delay += codec_dai->driver->ops->delay(substream, codec_dai);
896
897 if (platform->driver->delay)
898 delay += platform->driver->delay(substream, codec_dai);
899
900 runtime->delay = delay;
901
902 return offset;
903 }
904
905 /* connect a FE and BE */
906 static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
907 struct snd_soc_pcm_runtime *be, int stream)
908 {
909 struct snd_soc_dpcm *dpcm;
910
911 /* only add new dpcms */
912 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
913 if (dpcm->be == be && dpcm->fe == fe)
914 return 0;
915 }
916
917 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
918 if (!dpcm)
919 return -ENOMEM;
920
921 dpcm->be = be;
922 dpcm->fe = fe;
923 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
924 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
925 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
926 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
927
928 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
929 stream ? "capture" : "playback", fe->dai_link->name,
930 stream ? "<-" : "->", be->dai_link->name);
931
932 #ifdef CONFIG_DEBUG_FS
933 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
934 fe->debugfs_dpcm_root, &dpcm->state);
935 #endif
936 return 1;
937 }
938
939 /* reparent a BE onto another FE */
940 static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
941 struct snd_soc_pcm_runtime *be, int stream)
942 {
943 struct snd_soc_dpcm *dpcm;
944 struct snd_pcm_substream *fe_substream, *be_substream;
945
946 /* reparent if BE is connected to other FEs */
947 if (!be->dpcm[stream].users)
948 return;
949
950 be_substream = snd_soc_dpcm_get_substream(be, stream);
951
952 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
953 if (dpcm->fe == fe)
954 continue;
955
956 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
957 stream ? "capture" : "playback",
958 dpcm->fe->dai_link->name,
959 stream ? "<-" : "->", dpcm->be->dai_link->name);
960
961 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
962 be_substream->runtime = fe_substream->runtime;
963 break;
964 }
965 }
966
967 /* disconnect a BE and FE */
968 void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
969 {
970 struct snd_soc_dpcm *dpcm, *d;
971
972 list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
973 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
974 stream ? "capture" : "playback",
975 dpcm->be->dai_link->name);
976
977 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
978 continue;
979
980 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
981 stream ? "capture" : "playback", fe->dai_link->name,
982 stream ? "<-" : "->", dpcm->be->dai_link->name);
983
984 /* BEs still alive need new FE */
985 dpcm_be_reparent(fe, dpcm->be, stream);
986
987 #ifdef CONFIG_DEBUG_FS
988 debugfs_remove(dpcm->debugfs_state);
989 #endif
990 list_del(&dpcm->list_be);
991 list_del(&dpcm->list_fe);
992 kfree(dpcm);
993 }
994 }
995
996 /* get BE for DAI widget and stream */
997 static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
998 struct snd_soc_dapm_widget *widget, int stream)
999 {
1000 struct snd_soc_pcm_runtime *be;
1001 int i;
1002
1003 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1004 for (i = 0; i < card->num_links; i++) {
1005 be = &card->rtd[i];
1006
1007 if (!be->dai_link->no_pcm)
1008 continue;
1009
1010 if (be->cpu_dai->playback_widget == widget ||
1011 be->codec_dai->playback_widget == widget)
1012 return be;
1013 }
1014 } else {
1015
1016 for (i = 0; i < card->num_links; i++) {
1017 be = &card->rtd[i];
1018
1019 if (!be->dai_link->no_pcm)
1020 continue;
1021
1022 if (be->cpu_dai->capture_widget == widget ||
1023 be->codec_dai->capture_widget == widget)
1024 return be;
1025 }
1026 }
1027
1028 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1029 stream ? "capture" : "playback", widget->name);
1030 return NULL;
1031 }
1032
1033 static inline struct snd_soc_dapm_widget *
1034 dai_get_widget(struct snd_soc_dai *dai, int stream)
1035 {
1036 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1037 return dai->playback_widget;
1038 else
1039 return dai->capture_widget;
1040 }
1041
1042 static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1043 struct snd_soc_dapm_widget *widget)
1044 {
1045 int i;
1046
1047 for (i = 0; i < list->num_widgets; i++) {
1048 if (widget == list->widgets[i])
1049 return 1;
1050 }
1051
1052 return 0;
1053 }
1054
1055 int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1056 int stream, struct snd_soc_dapm_widget_list **list_)
1057 {
1058 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1059 struct snd_soc_dapm_widget_list *list;
1060 int paths;
1061
1062 list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
1063 sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
1064 if (list == NULL)
1065 return -ENOMEM;
1066
1067 /* get number of valid DAI paths and their widgets */
1068 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
1069
1070 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1071 stream ? "capture" : "playback");
1072
1073 *list_ = list;
1074 return paths;
1075 }
1076
1077 static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1078 struct snd_soc_dapm_widget_list **list_)
1079 {
1080 struct snd_soc_dpcm *dpcm;
1081 struct snd_soc_dapm_widget_list *list = *list_;
1082 struct snd_soc_dapm_widget *widget;
1083 int prune = 0;
1084
1085 /* Destroy any old FE <--> BE connections */
1086 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1087
1088 /* is there a valid CPU DAI widget for this BE */
1089 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1090
1091 /* prune the BE if it's no longer in our active list */
1092 if (widget && widget_in_list(list, widget))
1093 continue;
1094
1095 /* is there a valid CODEC DAI widget for this BE */
1096 widget = dai_get_widget(dpcm->be->codec_dai, stream);
1097
1098 /* prune the BE if it's no longer in our active list */
1099 if (widget && widget_in_list(list, widget))
1100 continue;
1101
1102 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1103 stream ? "capture" : "playback",
1104 dpcm->be->dai_link->name, fe->dai_link->name);
1105 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1106 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1107 prune++;
1108 }
1109
1110 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1111 return prune;
1112 }
1113
1114 static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1115 struct snd_soc_dapm_widget_list **list_)
1116 {
1117 struct snd_soc_card *card = fe->card;
1118 struct snd_soc_dapm_widget_list *list = *list_;
1119 struct snd_soc_pcm_runtime *be;
1120 int i, new = 0, err;
1121
1122 /* Create any new FE <--> BE connections */
1123 for (i = 0; i < list->num_widgets; i++) {
1124
1125 switch (list->widgets[i]->id) {
1126 case snd_soc_dapm_dai_in:
1127 case snd_soc_dapm_dai_out:
1128 break;
1129 default:
1130 continue;
1131 }
1132
1133 /* is there a valid BE rtd for this widget */
1134 be = dpcm_get_be(card, list->widgets[i], stream);
1135 if (!be) {
1136 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1137 list->widgets[i]->name);
1138 continue;
1139 }
1140
1141 /* make sure BE is a real BE */
1142 if (!be->dai_link->no_pcm)
1143 continue;
1144
1145 /* don't connect if FE is not running */
1146 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1147 continue;
1148
1149 /* newly connected FE and BE */
1150 err = dpcm_be_connect(fe, be, stream);
1151 if (err < 0) {
1152 dev_err(fe->dev, "ASoC: can't connect %s\n",
1153 list->widgets[i]->name);
1154 break;
1155 } else if (err == 0) /* already connected */
1156 continue;
1157
1158 /* new */
1159 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1160 new++;
1161 }
1162
1163 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1164 return new;
1165 }
1166
1167 /*
1168 * Find the corresponding BE DAIs that source or sink audio to this
1169 * FE substream.
1170 */
1171 int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1172 int stream, struct snd_soc_dapm_widget_list **list, int new)
1173 {
1174 if (new)
1175 return dpcm_add_paths(fe, stream, list);
1176 else
1177 return dpcm_prune_paths(fe, stream, list);
1178 }
1179
1180 void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1181 {
1182 struct snd_soc_dpcm *dpcm;
1183
1184 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
1185 dpcm->be->dpcm[stream].runtime_update =
1186 SND_SOC_DPCM_UPDATE_NO;
1187 }
1188
1189 static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1190 int stream)
1191 {
1192 struct snd_soc_dpcm *dpcm;
1193
1194 /* disable any enabled and non active backends */
1195 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1196
1197 struct snd_soc_pcm_runtime *be = dpcm->be;
1198 struct snd_pcm_substream *be_substream =
1199 snd_soc_dpcm_get_substream(be, stream);
1200
1201 if (be->dpcm[stream].users == 0)
1202 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1203 stream ? "capture" : "playback",
1204 be->dpcm[stream].state);
1205
1206 if (--be->dpcm[stream].users != 0)
1207 continue;
1208
1209 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1210 continue;
1211
1212 soc_pcm_close(be_substream);
1213 be_substream->runtime = NULL;
1214 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1215 }
1216 }
1217
1218 int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1219 {
1220 struct snd_soc_dpcm *dpcm;
1221 int err, count = 0;
1222
1223 /* only startup BE DAIs that are either sinks or sources to this FE DAI */
1224 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1225
1226 struct snd_soc_pcm_runtime *be = dpcm->be;
1227 struct snd_pcm_substream *be_substream =
1228 snd_soc_dpcm_get_substream(be, stream);
1229
1230 if (!be_substream) {
1231 dev_err(be->dev, "ASoC: no backend %s stream\n",
1232 stream ? "capture" : "playback");
1233 continue;
1234 }
1235
1236 /* is this op for this BE ? */
1237 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1238 continue;
1239
1240 /* first time the dpcm is open ? */
1241 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1242 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1243 stream ? "capture" : "playback",
1244 be->dpcm[stream].state);
1245
1246 if (be->dpcm[stream].users++ != 0)
1247 continue;
1248
1249 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1250 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1251 continue;
1252
1253 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1254 stream ? "capture" : "playback", be->dai_link->name);
1255
1256 be_substream->runtime = be->dpcm[stream].runtime;
1257 err = soc_pcm_open(be_substream);
1258 if (err < 0) {
1259 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1260 be->dpcm[stream].users--;
1261 if (be->dpcm[stream].users < 0)
1262 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1263 stream ? "capture" : "playback",
1264 be->dpcm[stream].state);
1265
1266 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1267 goto unwind;
1268 }
1269
1270 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1271 count++;
1272 }
1273
1274 return count;
1275
1276 unwind:
1277 /* disable any enabled and non active backends */
1278 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1279 struct snd_soc_pcm_runtime *be = dpcm->be;
1280 struct snd_pcm_substream *be_substream =
1281 snd_soc_dpcm_get_substream(be, stream);
1282
1283 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1284 continue;
1285
1286 if (be->dpcm[stream].users == 0)
1287 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1288 stream ? "capture" : "playback",
1289 be->dpcm[stream].state);
1290
1291 if (--be->dpcm[stream].users != 0)
1292 continue;
1293
1294 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1295 continue;
1296
1297 soc_pcm_close(be_substream);
1298 be_substream->runtime = NULL;
1299 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1300 }
1301
1302 return err;
1303 }
1304
1305 static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1306 struct snd_soc_pcm_stream *stream)
1307 {
1308 runtime->hw.rate_min = stream->rate_min;
1309 runtime->hw.rate_max = stream->rate_max;
1310 runtime->hw.channels_min = stream->channels_min;
1311 runtime->hw.channels_max = stream->channels_max;
1312 if (runtime->hw.formats)
1313 runtime->hw.formats &= stream->formats;
1314 else
1315 runtime->hw.formats = stream->formats;
1316 runtime->hw.rates = stream->rates;
1317 }
1318
1319 static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1320 {
1321 struct snd_pcm_runtime *runtime = substream->runtime;
1322 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1323 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1324 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1325
1326 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1327 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1328 else
1329 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1330 }
1331
1332 static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1333 {
1334 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1335 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1336 int stream = fe_substream->stream, ret = 0;
1337
1338 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1339
1340 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1341 if (ret < 0) {
1342 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1343 goto be_err;
1344 }
1345
1346 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1347
1348 /* start the DAI frontend */
1349 ret = soc_pcm_open(fe_substream);
1350 if (ret < 0) {
1351 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
1352 goto unwind;
1353 }
1354
1355 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1356
1357 dpcm_set_fe_runtime(fe_substream);
1358 snd_pcm_limit_hw_rates(runtime);
1359
1360 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1361 return 0;
1362
1363 unwind:
1364 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
1365 be_err:
1366 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1367 return ret;
1368 }
1369
1370 int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1371 {
1372 struct snd_soc_dpcm *dpcm;
1373
1374 /* only shutdown BEs that are either sinks or sources to this FE DAI */
1375 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1376
1377 struct snd_soc_pcm_runtime *be = dpcm->be;
1378 struct snd_pcm_substream *be_substream =
1379 snd_soc_dpcm_get_substream(be, stream);
1380
1381 /* is this op for this BE ? */
1382 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1383 continue;
1384
1385 if (be->dpcm[stream].users == 0)
1386 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1387 stream ? "capture" : "playback",
1388 be->dpcm[stream].state);
1389
1390 if (--be->dpcm[stream].users != 0)
1391 continue;
1392
1393 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1394 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
1395 continue;
1396
1397 dev_dbg(be->dev, "ASoC: close BE %s\n",
1398 dpcm->fe->dai_link->name);
1399
1400 soc_pcm_close(be_substream);
1401 be_substream->runtime = NULL;
1402
1403 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1404 }
1405 return 0;
1406 }
1407
1408 static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
1409 {
1410 struct snd_soc_pcm_runtime *fe = substream->private_data;
1411 int stream = substream->stream;
1412
1413 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1414
1415 /* shutdown the BEs */
1416 dpcm_be_dai_shutdown(fe, substream->stream);
1417
1418 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
1419
1420 /* now shutdown the frontend */
1421 soc_pcm_close(substream);
1422
1423 /* run the stream event for each BE */
1424 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
1425
1426 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1427 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1428 return 0;
1429 }
1430
1431 int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
1432 {
1433 struct snd_soc_dpcm *dpcm;
1434
1435 /* only hw_params backends that are either sinks or sources
1436 * to this frontend DAI */
1437 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1438
1439 struct snd_soc_pcm_runtime *be = dpcm->be;
1440 struct snd_pcm_substream *be_substream =
1441 snd_soc_dpcm_get_substream(be, stream);
1442
1443 /* is this op for this BE ? */
1444 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1445 continue;
1446
1447 /* only free hw when no longer used - check all FEs */
1448 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1449 continue;
1450
1451 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1452 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1453 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1454 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
1455 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1456 continue;
1457
1458 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
1459 dpcm->fe->dai_link->name);
1460
1461 soc_pcm_hw_free(be_substream);
1462
1463 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1464 }
1465
1466 return 0;
1467 }
1468
1469 static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
1470 {
1471 struct snd_soc_pcm_runtime *fe = substream->private_data;
1472 int err, stream = substream->stream;
1473
1474 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1475 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1476
1477 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
1478
1479 /* call hw_free on the frontend */
1480 err = soc_pcm_hw_free(substream);
1481 if (err < 0)
1482 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
1483 fe->dai_link->name);
1484
1485 /* only hw_params backends that are either sinks or sources
1486 * to this frontend DAI */
1487 err = dpcm_be_dai_hw_free(fe, stream);
1488
1489 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
1490 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1491
1492 mutex_unlock(&fe->card->mutex);
1493 return 0;
1494 }
1495
1496 int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
1497 {
1498 struct snd_soc_dpcm *dpcm;
1499 int ret;
1500
1501 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1502
1503 struct snd_soc_pcm_runtime *be = dpcm->be;
1504 struct snd_pcm_substream *be_substream =
1505 snd_soc_dpcm_get_substream(be, stream);
1506
1507 /* is this op for this BE ? */
1508 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1509 continue;
1510
1511 /* only allow hw_params() if no connected FEs are running */
1512 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
1513 continue;
1514
1515 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1516 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1517 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
1518 continue;
1519
1520 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
1521 dpcm->fe->dai_link->name);
1522
1523 /* copy params for each dpcm */
1524 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
1525 sizeof(struct snd_pcm_hw_params));
1526
1527 /* perform any hw_params fixups */
1528 if (be->dai_link->be_hw_params_fixup) {
1529 ret = be->dai_link->be_hw_params_fixup(be,
1530 &dpcm->hw_params);
1531 if (ret < 0) {
1532 dev_err(be->dev,
1533 "ASoC: hw_params BE fixup failed %d\n",
1534 ret);
1535 goto unwind;
1536 }
1537 }
1538
1539 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
1540 if (ret < 0) {
1541 dev_err(dpcm->be->dev,
1542 "ASoC: hw_params BE failed %d\n", ret);
1543 goto unwind;
1544 }
1545
1546 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1547 }
1548 return 0;
1549
1550 unwind:
1551 /* disable any enabled and non active backends */
1552 list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1553 struct snd_soc_pcm_runtime *be = dpcm->be;
1554 struct snd_pcm_substream *be_substream =
1555 snd_soc_dpcm_get_substream(be, stream);
1556
1557 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1558 continue;
1559
1560 /* only allow hw_free() if no connected FEs are running */
1561 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1562 continue;
1563
1564 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
1565 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1566 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
1567 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1568 continue;
1569
1570 soc_pcm_hw_free(be_substream);
1571 }
1572
1573 return ret;
1574 }
1575
1576 static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
1577 struct snd_pcm_hw_params *params)
1578 {
1579 struct snd_soc_pcm_runtime *fe = substream->private_data;
1580 int ret, stream = substream->stream;
1581
1582 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1583 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1584
1585 memcpy(&fe->dpcm[substream->stream].hw_params, params,
1586 sizeof(struct snd_pcm_hw_params));
1587 ret = dpcm_be_dai_hw_params(fe, substream->stream);
1588 if (ret < 0) {
1589 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
1590 goto out;
1591 }
1592
1593 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
1594 fe->dai_link->name, params_rate(params),
1595 params_channels(params), params_format(params));
1596
1597 /* call hw_params on the frontend */
1598 ret = soc_pcm_hw_params(substream, params);
1599 if (ret < 0) {
1600 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
1601 dpcm_be_dai_hw_free(fe, stream);
1602 } else
1603 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
1604
1605 out:
1606 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1607 mutex_unlock(&fe->card->mutex);
1608 return ret;
1609 }
1610
1611 static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
1612 struct snd_pcm_substream *substream, int cmd)
1613 {
1614 int ret;
1615
1616 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
1617 dpcm->fe->dai_link->name, cmd);
1618
1619 ret = soc_pcm_trigger(substream, cmd);
1620 if (ret < 0)
1621 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
1622
1623 return ret;
1624 }
1625
1626 int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
1627 int cmd)
1628 {
1629 struct snd_soc_dpcm *dpcm;
1630 int ret = 0;
1631
1632 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1633
1634 struct snd_soc_pcm_runtime *be = dpcm->be;
1635 struct snd_pcm_substream *be_substream =
1636 snd_soc_dpcm_get_substream(be, stream);
1637
1638 /* is this op for this BE ? */
1639 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1640 continue;
1641
1642 switch (cmd) {
1643 case SNDRV_PCM_TRIGGER_START:
1644 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
1645 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1646 continue;
1647
1648 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1649 if (ret)
1650 return ret;
1651
1652 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1653 break;
1654 case SNDRV_PCM_TRIGGER_RESUME:
1655 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
1656 continue;
1657
1658 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1659 if (ret)
1660 return ret;
1661
1662 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1663 break;
1664 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1665 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
1666 continue;
1667
1668 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1669 if (ret)
1670 return ret;
1671
1672 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1673 break;
1674 case SNDRV_PCM_TRIGGER_STOP:
1675 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1676 continue;
1677
1678 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1679 continue;
1680
1681 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1682 if (ret)
1683 return ret;
1684
1685 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1686 break;
1687 case SNDRV_PCM_TRIGGER_SUSPEND:
1688 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1689 continue;
1690
1691 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1692 continue;
1693
1694 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1695 if (ret)
1696 return ret;
1697
1698 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
1699 break;
1700 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1701 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
1702 continue;
1703
1704 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
1705 continue;
1706
1707 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
1708 if (ret)
1709 return ret;
1710
1711 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
1712 break;
1713 }
1714 }
1715
1716 return ret;
1717 }
1718 EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
1719
1720 static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
1721 {
1722 struct snd_soc_pcm_runtime *fe = substream->private_data;
1723 int stream = substream->stream, ret;
1724 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1725
1726 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1727
1728 switch (trigger) {
1729 case SND_SOC_DPCM_TRIGGER_PRE:
1730 /* call trigger on the frontend before the backend. */
1731
1732 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
1733 fe->dai_link->name, cmd);
1734
1735 ret = soc_pcm_trigger(substream, cmd);
1736 if (ret < 0) {
1737 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1738 goto out;
1739 }
1740
1741 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1742 break;
1743 case SND_SOC_DPCM_TRIGGER_POST:
1744 /* call trigger on the frontend after the backend. */
1745
1746 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
1747 if (ret < 0) {
1748 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1749 goto out;
1750 }
1751
1752 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
1753 fe->dai_link->name, cmd);
1754
1755 ret = soc_pcm_trigger(substream, cmd);
1756 break;
1757 case SND_SOC_DPCM_TRIGGER_BESPOKE:
1758 /* bespoke trigger() - handles both FE and BEs */
1759
1760 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
1761 fe->dai_link->name, cmd);
1762
1763 ret = soc_pcm_bespoke_trigger(substream, cmd);
1764 if (ret < 0) {
1765 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1766 goto out;
1767 }
1768 break;
1769 default:
1770 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
1771 fe->dai_link->name);
1772 ret = -EINVAL;
1773 goto out;
1774 }
1775
1776 switch (cmd) {
1777 case SNDRV_PCM_TRIGGER_START:
1778 case SNDRV_PCM_TRIGGER_RESUME:
1779 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1780 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
1781 break;
1782 case SNDRV_PCM_TRIGGER_STOP:
1783 case SNDRV_PCM_TRIGGER_SUSPEND:
1784 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1785 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
1786 break;
1787 }
1788
1789 out:
1790 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1791 return ret;
1792 }
1793
1794 int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
1795 {
1796 struct snd_soc_dpcm *dpcm;
1797 int ret = 0;
1798
1799 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1800
1801 struct snd_soc_pcm_runtime *be = dpcm->be;
1802 struct snd_pcm_substream *be_substream =
1803 snd_soc_dpcm_get_substream(be, stream);
1804
1805 /* is this op for this BE ? */
1806 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1807 continue;
1808
1809 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
1810 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
1811 continue;
1812
1813 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
1814 dpcm->fe->dai_link->name);
1815
1816 ret = soc_pcm_prepare(be_substream);
1817 if (ret < 0) {
1818 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
1819 ret);
1820 break;
1821 }
1822
1823 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1824 }
1825 return ret;
1826 }
1827
1828 static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
1829 {
1830 struct snd_soc_pcm_runtime *fe = substream->private_data;
1831 int stream = substream->stream, ret = 0;
1832
1833 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
1834
1835 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
1836
1837 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
1838
1839 /* there is no point preparing this FE if there are no BEs */
1840 if (list_empty(&fe->dpcm[stream].be_clients)) {
1841 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
1842 fe->dai_link->name);
1843 ret = -EINVAL;
1844 goto out;
1845 }
1846
1847 ret = dpcm_be_dai_prepare(fe, substream->stream);
1848 if (ret < 0)
1849 goto out;
1850
1851 /* call prepare on the frontend */
1852 ret = soc_pcm_prepare(substream);
1853 if (ret < 0) {
1854 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
1855 fe->dai_link->name);
1856 goto out;
1857 }
1858
1859 /* run the stream event for each BE */
1860 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
1861 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
1862
1863 out:
1864 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
1865 mutex_unlock(&fe->card->mutex);
1866
1867 return ret;
1868 }
1869
1870 static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
1871 unsigned int cmd, void *arg)
1872 {
1873 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1874 struct snd_soc_platform *platform = rtd->platform;
1875
1876 if (platform->driver->ops && platform->driver->ops->ioctl)
1877 return platform->driver->ops->ioctl(substream, cmd, arg);
1878 return snd_pcm_lib_ioctl(substream, cmd, arg);
1879 }
1880
1881 static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
1882 {
1883 struct snd_pcm_substream *substream =
1884 snd_soc_dpcm_get_substream(fe, stream);
1885 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1886 int err;
1887
1888 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
1889 stream ? "capture" : "playback", fe->dai_link->name);
1890
1891 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1892 /* call bespoke trigger - FE takes care of all BE triggers */
1893 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
1894 fe->dai_link->name);
1895
1896 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1897 if (err < 0)
1898 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1899 } else {
1900 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
1901 fe->dai_link->name);
1902
1903 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
1904 if (err < 0)
1905 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
1906 }
1907
1908 err = dpcm_be_dai_hw_free(fe, stream);
1909 if (err < 0)
1910 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
1911
1912 err = dpcm_be_dai_shutdown(fe, stream);
1913 if (err < 0)
1914 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
1915
1916 /* run the stream event for each BE */
1917 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1918
1919 return 0;
1920 }
1921
1922 static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
1923 {
1924 struct snd_pcm_substream *substream =
1925 snd_soc_dpcm_get_substream(fe, stream);
1926 struct snd_soc_dpcm *dpcm;
1927 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
1928 int ret;
1929
1930 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
1931 stream ? "capture" : "playback", fe->dai_link->name);
1932
1933 /* Only start the BE if the FE is ready */
1934 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
1935 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
1936 return -EINVAL;
1937
1938 /* startup must always be called for new BEs */
1939 ret = dpcm_be_dai_startup(fe, stream);
1940 if (ret < 0)
1941 goto disconnect;
1942
1943 /* keep going if FE state is > open */
1944 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
1945 return 0;
1946
1947 ret = dpcm_be_dai_hw_params(fe, stream);
1948 if (ret < 0)
1949 goto close;
1950
1951 /* keep going if FE state is > hw_params */
1952 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
1953 return 0;
1954
1955
1956 ret = dpcm_be_dai_prepare(fe, stream);
1957 if (ret < 0)
1958 goto hw_free;
1959
1960 /* run the stream event for each BE */
1961 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
1962
1963 /* keep going if FE state is > prepare */
1964 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
1965 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
1966 return 0;
1967
1968 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
1969 /* call trigger on the frontend - FE takes care of all BE triggers */
1970 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
1971 fe->dai_link->name);
1972
1973 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
1974 if (ret < 0) {
1975 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
1976 goto hw_free;
1977 }
1978 } else {
1979 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
1980 fe->dai_link->name);
1981
1982 ret = dpcm_be_dai_trigger(fe, stream,
1983 SNDRV_PCM_TRIGGER_START);
1984 if (ret < 0) {
1985 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
1986 goto hw_free;
1987 }
1988 }
1989
1990 return 0;
1991
1992 hw_free:
1993 dpcm_be_dai_hw_free(fe, stream);
1994 close:
1995 dpcm_be_dai_shutdown(fe, stream);
1996 disconnect:
1997 /* disconnect any non started BEs */
1998 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
1999 struct snd_soc_pcm_runtime *be = dpcm->be;
2000 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2001 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2002 }
2003
2004 return ret;
2005 }
2006
2007 static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2008 {
2009 int ret;
2010
2011 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2012 ret = dpcm_run_update_startup(fe, stream);
2013 if (ret < 0)
2014 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2015 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2016
2017 return ret;
2018 }
2019
2020 static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2021 {
2022 int ret;
2023
2024 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
2025 ret = dpcm_run_update_shutdown(fe, stream);
2026 if (ret < 0)
2027 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2028 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2029
2030 return ret;
2031 }
2032
2033 /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
2034 * any DAI links.
2035 */
2036 int soc_dpcm_runtime_update(struct snd_soc_card *card)
2037 {
2038 int i, old, new, paths;
2039
2040 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2041 for (i = 0; i < card->num_rtd; i++) {
2042 struct snd_soc_dapm_widget_list *list;
2043 struct snd_soc_pcm_runtime *fe = &card->rtd[i];
2044
2045 /* make sure link is FE */
2046 if (!fe->dai_link->dynamic)
2047 continue;
2048
2049 /* only check active links */
2050 if (!fe->cpu_dai->active)
2051 continue;
2052
2053 /* DAPM sync will call this to update DSP paths */
2054 dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
2055 fe->dai_link->name);
2056
2057 /* skip if FE doesn't have playback capability */
2058 if (!fe->cpu_dai->driver->playback.channels_min)
2059 goto capture;
2060
2061 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2062 if (paths < 0) {
2063 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2064 fe->dai_link->name, "playback");
2065 mutex_unlock(&card->mutex);
2066 return paths;
2067 }
2068
2069 /* update any new playback paths */
2070 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
2071 if (new) {
2072 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2073 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2074 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2075 }
2076
2077 /* update any old playback paths */
2078 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
2079 if (old) {
2080 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2081 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2082 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2083 }
2084
2085 capture:
2086 /* skip if FE doesn't have capture capability */
2087 if (!fe->cpu_dai->driver->capture.channels_min)
2088 continue;
2089
2090 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2091 if (paths < 0) {
2092 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2093 fe->dai_link->name, "capture");
2094 mutex_unlock(&card->mutex);
2095 return paths;
2096 }
2097
2098 /* update any new capture paths */
2099 new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
2100 if (new) {
2101 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2102 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2103 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2104 }
2105
2106 /* update any old capture paths */
2107 old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
2108 if (old) {
2109 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2110 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2111 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2112 }
2113
2114 dpcm_path_put(&list);
2115 }
2116
2117 mutex_unlock(&card->mutex);
2118 return 0;
2119 }
2120 int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2121 {
2122 struct snd_soc_dpcm *dpcm;
2123 struct list_head *clients =
2124 &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
2125
2126 list_for_each_entry(dpcm, clients, list_be) {
2127
2128 struct snd_soc_pcm_runtime *be = dpcm->be;
2129 struct snd_soc_dai *dai = be->codec_dai;
2130 struct snd_soc_dai_driver *drv = dai->driver;
2131
2132 if (be->dai_link->ignore_suspend)
2133 continue;
2134
2135 dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
2136
2137 if (drv->ops && drv->ops->digital_mute && dai->playback_active)
2138 drv->ops->digital_mute(dai, mute);
2139 }
2140
2141 return 0;
2142 }
2143
2144 static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2145 {
2146 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2147 struct snd_soc_dpcm *dpcm;
2148 struct snd_soc_dapm_widget_list *list;
2149 int ret;
2150 int stream = fe_substream->stream;
2151
2152 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2153 fe->dpcm[stream].runtime = fe_substream->runtime;
2154
2155 if (dpcm_path_get(fe, stream, &list) <= 0) {
2156 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2157 fe->dai_link->name, stream ? "capture" : "playback");
2158 }
2159
2160 /* calculate valid and active FE <-> BE dpcms */
2161 dpcm_process_paths(fe, stream, &list, 1);
2162
2163 ret = dpcm_fe_dai_startup(fe_substream);
2164 if (ret < 0) {
2165 /* clean up all links */
2166 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2167 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2168
2169 dpcm_be_disconnect(fe, stream);
2170 fe->dpcm[stream].runtime = NULL;
2171 }
2172
2173 dpcm_clear_pending_state(fe, stream);
2174 dpcm_path_put(&list);
2175 mutex_unlock(&fe->card->mutex);
2176 return ret;
2177 }
2178
2179 static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2180 {
2181 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2182 struct snd_soc_dpcm *dpcm;
2183 int stream = fe_substream->stream, ret;
2184
2185 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2186 ret = dpcm_fe_dai_shutdown(fe_substream);
2187
2188 /* mark FE's links ready to prune */
2189 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
2190 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2191
2192 dpcm_be_disconnect(fe, stream);
2193
2194 fe->dpcm[stream].runtime = NULL;
2195 mutex_unlock(&fe->card->mutex);
2196 return ret;
2197 }
2198
2199 /* create a new pcm */
2200 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
2201 {
2202 struct snd_soc_platform *platform = rtd->platform;
2203 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2204 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
2205 struct snd_pcm *pcm;
2206 char new_name[64];
2207 int ret = 0, playback = 0, capture = 0;
2208
2209 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
2210 playback = rtd->dai_link->dpcm_playback;
2211 capture = rtd->dai_link->dpcm_capture;
2212 } else {
2213 if (codec_dai->driver->playback.channels_min &&
2214 cpu_dai->driver->playback.channels_min)
2215 playback = 1;
2216 if (codec_dai->driver->capture.channels_min &&
2217 cpu_dai->driver->capture.channels_min)
2218 capture = 1;
2219 }
2220
2221 if (rtd->dai_link->playback_only) {
2222 playback = 1;
2223 capture = 0;
2224 }
2225
2226 if (rtd->dai_link->capture_only) {
2227 playback = 0;
2228 capture = 1;
2229 }
2230
2231 /* create the PCM */
2232 if (rtd->dai_link->no_pcm) {
2233 snprintf(new_name, sizeof(new_name), "(%s)",
2234 rtd->dai_link->stream_name);
2235
2236 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
2237 playback, capture, &pcm);
2238 } else {
2239 if (rtd->dai_link->dynamic)
2240 snprintf(new_name, sizeof(new_name), "%s (*)",
2241 rtd->dai_link->stream_name);
2242 else
2243 snprintf(new_name, sizeof(new_name), "%s %s-%d",
2244 rtd->dai_link->stream_name, codec_dai->name, num);
2245
2246 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
2247 capture, &pcm);
2248 }
2249 if (ret < 0) {
2250 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
2251 rtd->dai_link->name);
2252 return ret;
2253 }
2254 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
2255
2256 /* DAPM dai link stream work */
2257 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
2258
2259 rtd->pcm = pcm;
2260 pcm->private_data = rtd;
2261
2262 if (rtd->dai_link->no_pcm) {
2263 if (playback)
2264 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
2265 if (capture)
2266 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
2267 goto out;
2268 }
2269
2270 /* ASoC PCM operations */
2271 if (rtd->dai_link->dynamic) {
2272 rtd->ops.open = dpcm_fe_dai_open;
2273 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
2274 rtd->ops.prepare = dpcm_fe_dai_prepare;
2275 rtd->ops.trigger = dpcm_fe_dai_trigger;
2276 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
2277 rtd->ops.close = dpcm_fe_dai_close;
2278 rtd->ops.pointer = soc_pcm_pointer;
2279 rtd->ops.ioctl = soc_pcm_ioctl;
2280 } else {
2281 rtd->ops.open = soc_pcm_open;
2282 rtd->ops.hw_params = soc_pcm_hw_params;
2283 rtd->ops.prepare = soc_pcm_prepare;
2284 rtd->ops.trigger = soc_pcm_trigger;
2285 rtd->ops.hw_free = soc_pcm_hw_free;
2286 rtd->ops.close = soc_pcm_close;
2287 rtd->ops.pointer = soc_pcm_pointer;
2288 rtd->ops.ioctl = soc_pcm_ioctl;
2289 }
2290
2291 if (platform->driver->ops) {
2292 rtd->ops.ack = platform->driver->ops->ack;
2293 rtd->ops.copy = platform->driver->ops->copy;
2294 rtd->ops.silence = platform->driver->ops->silence;
2295 rtd->ops.page = platform->driver->ops->page;
2296 rtd->ops.mmap = platform->driver->ops->mmap;
2297 }
2298
2299 if (playback)
2300 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
2301
2302 if (capture)
2303 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
2304
2305 if (platform->driver->pcm_new) {
2306 ret = platform->driver->pcm_new(rtd);
2307 if (ret < 0) {
2308 dev_err(platform->dev,
2309 "ASoC: pcm constructor failed: %d\n",
2310 ret);
2311 return ret;
2312 }
2313 }
2314
2315 pcm->private_free = platform->driver->pcm_free;
2316 out:
2317 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n", codec_dai->name,
2318 cpu_dai->name);
2319 return ret;
2320 }
2321
2322 /* is the current PCM operation for this FE ? */
2323 int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
2324 {
2325 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
2326 return 1;
2327 return 0;
2328 }
2329 EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
2330
2331 /* is the current PCM operation for this BE ? */
2332 int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
2333 struct snd_soc_pcm_runtime *be, int stream)
2334 {
2335 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
2336 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
2337 be->dpcm[stream].runtime_update))
2338 return 1;
2339 return 0;
2340 }
2341 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
2342
2343 /* get the substream for this BE */
2344 struct snd_pcm_substream *
2345 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
2346 {
2347 return be->pcm->streams[stream].substream;
2348 }
2349 EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
2350
2351 /* get the BE runtime state */
2352 enum snd_soc_dpcm_state
2353 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
2354 {
2355 return be->dpcm[stream].state;
2356 }
2357 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
2358
2359 /* set the BE runtime state */
2360 void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
2361 int stream, enum snd_soc_dpcm_state state)
2362 {
2363 be->dpcm[stream].state = state;
2364 }
2365 EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
2366
2367 /*
2368 * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
2369 * are not running, paused or suspended for the specified stream direction.
2370 */
2371 int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
2372 struct snd_soc_pcm_runtime *be, int stream)
2373 {
2374 struct snd_soc_dpcm *dpcm;
2375 int state;
2376
2377 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2378
2379 if (dpcm->fe == fe)
2380 continue;
2381
2382 state = dpcm->fe->dpcm[stream].state;
2383 if (state == SND_SOC_DPCM_STATE_START ||
2384 state == SND_SOC_DPCM_STATE_PAUSED ||
2385 state == SND_SOC_DPCM_STATE_SUSPEND)
2386 return 0;
2387 }
2388
2389 /* it's safe to free/stop this BE DAI */
2390 return 1;
2391 }
2392 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
2393
2394 /*
2395 * We can only change hw params a BE DAI if any of it's FE are not prepared,
2396 * running, paused or suspended for the specified stream direction.
2397 */
2398 int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
2399 struct snd_soc_pcm_runtime *be, int stream)
2400 {
2401 struct snd_soc_dpcm *dpcm;
2402 int state;
2403
2404 list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
2405
2406 if (dpcm->fe == fe)
2407 continue;
2408
2409 state = dpcm->fe->dpcm[stream].state;
2410 if (state == SND_SOC_DPCM_STATE_START ||
2411 state == SND_SOC_DPCM_STATE_PAUSED ||
2412 state == SND_SOC_DPCM_STATE_SUSPEND ||
2413 state == SND_SOC_DPCM_STATE_PREPARE)
2414 return 0;
2415 }
2416
2417 /* it's safe to change hw_params */
2418 return 1;
2419 }
2420 EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
2421
2422 int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
2423 int cmd, struct snd_soc_platform *platform)
2424 {
2425 if (platform->driver->ops && platform->driver->ops->trigger)
2426 return platform->driver->ops->trigger(substream, cmd);
2427 return 0;
2428 }
2429 EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
2430
2431 #ifdef CONFIG_DEBUG_FS
2432 static char *dpcm_state_string(enum snd_soc_dpcm_state state)
2433 {
2434 switch (state) {
2435 case SND_SOC_DPCM_STATE_NEW:
2436 return "new";
2437 case SND_SOC_DPCM_STATE_OPEN:
2438 return "open";
2439 case SND_SOC_DPCM_STATE_HW_PARAMS:
2440 return "hw_params";
2441 case SND_SOC_DPCM_STATE_PREPARE:
2442 return "prepare";
2443 case SND_SOC_DPCM_STATE_START:
2444 return "start";
2445 case SND_SOC_DPCM_STATE_STOP:
2446 return "stop";
2447 case SND_SOC_DPCM_STATE_SUSPEND:
2448 return "suspend";
2449 case SND_SOC_DPCM_STATE_PAUSED:
2450 return "paused";
2451 case SND_SOC_DPCM_STATE_HW_FREE:
2452 return "hw_free";
2453 case SND_SOC_DPCM_STATE_CLOSE:
2454 return "close";
2455 }
2456
2457 return "unknown";
2458 }
2459
2460 static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
2461 int stream, char *buf, size_t size)
2462 {
2463 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
2464 struct snd_soc_dpcm *dpcm;
2465 ssize_t offset = 0;
2466
2467 /* FE state */
2468 offset += snprintf(buf + offset, size - offset,
2469 "[%s - %s]\n", fe->dai_link->name,
2470 stream ? "Capture" : "Playback");
2471
2472 offset += snprintf(buf + offset, size - offset, "State: %s\n",
2473 dpcm_state_string(fe->dpcm[stream].state));
2474
2475 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2476 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2477 offset += snprintf(buf + offset, size - offset,
2478 "Hardware Params: "
2479 "Format = %s, Channels = %d, Rate = %d\n",
2480 snd_pcm_format_name(params_format(params)),
2481 params_channels(params),
2482 params_rate(params));
2483
2484 /* BEs state */
2485 offset += snprintf(buf + offset, size - offset, "Backends:\n");
2486
2487 if (list_empty(&fe->dpcm[stream].be_clients)) {
2488 offset += snprintf(buf + offset, size - offset,
2489 " No active DSP links\n");
2490 goto out;
2491 }
2492
2493 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
2494 struct snd_soc_pcm_runtime *be = dpcm->be;
2495 params = &dpcm->hw_params;
2496
2497 offset += snprintf(buf + offset, size - offset,
2498 "- %s\n", be->dai_link->name);
2499
2500 offset += snprintf(buf + offset, size - offset,
2501 " State: %s\n",
2502 dpcm_state_string(be->dpcm[stream].state));
2503
2504 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
2505 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
2506 offset += snprintf(buf + offset, size - offset,
2507 " Hardware Params: "
2508 "Format = %s, Channels = %d, Rate = %d\n",
2509 snd_pcm_format_name(params_format(params)),
2510 params_channels(params),
2511 params_rate(params));
2512 }
2513
2514 out:
2515 return offset;
2516 }
2517
2518 static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
2519 size_t count, loff_t *ppos)
2520 {
2521 struct snd_soc_pcm_runtime *fe = file->private_data;
2522 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
2523 char *buf;
2524
2525 buf = kmalloc(out_count, GFP_KERNEL);
2526 if (!buf)
2527 return -ENOMEM;
2528
2529 if (fe->cpu_dai->driver->playback.channels_min)
2530 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
2531 buf + offset, out_count - offset);
2532
2533 if (fe->cpu_dai->driver->capture.channels_min)
2534 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
2535 buf + offset, out_count - offset);
2536
2537 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
2538
2539 kfree(buf);
2540 return ret;
2541 }
2542
2543 static const struct file_operations dpcm_state_fops = {
2544 .open = simple_open,
2545 .read = dpcm_state_read_file,
2546 .llseek = default_llseek,
2547 };
2548
2549 int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
2550 {
2551 if (!rtd->dai_link)
2552 return 0;
2553
2554 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
2555 rtd->card->debugfs_card_root);
2556 if (!rtd->debugfs_dpcm_root) {
2557 dev_dbg(rtd->dev,
2558 "ASoC: Failed to create dpcm debugfs directory %s\n",
2559 rtd->dai_link->name);
2560 return -EINVAL;
2561 }
2562
2563 rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
2564 rtd->debugfs_dpcm_root,
2565 rtd, &dpcm_state_fops);
2566
2567 return 0;
2568 }
2569 #endif
This page took 0.125008 seconds and 5 git commands to generate.