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