1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/delay.h>
16#include <linux/pinctrl/consumer.h>
17#include <linux/pm_runtime.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/workqueue.h>
21#include <linux/export.h>
22#include <linux/debugfs.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/soc-dpcm.h>
28#include <sound/initval.h>
29
30#define DPCM_MAX_BE_USERS 8
31
32
33
34
35
36
37static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
38{
39 struct snd_soc_pcm_stream *codec_stream;
40
41 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
42 codec_stream = &dai->driver->playback;
43 else
44 codec_stream = &dai->driver->capture;
45
46
47 return codec_stream->channels_min;
48}
49
50
51
52
53
54
55
56
57
58
59
60void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
61{
62 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
63 struct snd_soc_dai *codec_dai;
64 int i;
65
66 lockdep_assert_held(&rtd->pcm_mutex);
67
68 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
69 cpu_dai->playback_active++;
70 for_each_rtd_codec_dai(rtd, i, codec_dai)
71 codec_dai->playback_active++;
72 } else {
73 cpu_dai->capture_active++;
74 for_each_rtd_codec_dai(rtd, i, codec_dai)
75 codec_dai->capture_active++;
76 }
77
78 cpu_dai->active++;
79 cpu_dai->component->active++;
80 for_each_rtd_codec_dai(rtd, i, codec_dai) {
81 codec_dai->active++;
82 codec_dai->component->active++;
83 }
84}
85
86
87
88
89
90
91
92
93
94
95
96void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
97{
98 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
99 struct snd_soc_dai *codec_dai;
100 int i;
101
102 lockdep_assert_held(&rtd->pcm_mutex);
103
104 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
105 cpu_dai->playback_active--;
106 for_each_rtd_codec_dai(rtd, i, codec_dai)
107 codec_dai->playback_active--;
108 } else {
109 cpu_dai->capture_active--;
110 for_each_rtd_codec_dai(rtd, i, codec_dai)
111 codec_dai->capture_active--;
112 }
113
114 cpu_dai->active--;
115 cpu_dai->component->active--;
116 for_each_rtd_codec_dai(rtd, i, codec_dai) {
117 codec_dai->component->active--;
118 codec_dai->active--;
119 }
120}
121
122
123
124
125
126
127
128
129
130
131bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
132{
133 struct snd_soc_rtdcom_list *rtdcom;
134 struct snd_soc_component *component;
135 bool ignore = true;
136
137 if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
138 return true;
139
140 for_each_rtdcom(rtd, rtdcom) {
141 component = rtdcom->component;
142
143 ignore &= !component->driver->use_pmdown_time;
144 }
145
146 return ignore;
147}
148
149
150
151
152
153
154
155
156int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
157 const struct snd_pcm_hardware *hw)
158{
159 struct snd_pcm_runtime *runtime = substream->runtime;
160 runtime->hw.info = hw->info;
161 runtime->hw.formats = hw->formats;
162 runtime->hw.period_bytes_min = hw->period_bytes_min;
163 runtime->hw.period_bytes_max = hw->period_bytes_max;
164 runtime->hw.periods_min = hw->periods_min;
165 runtime->hw.periods_max = hw->periods_max;
166 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
167 runtime->hw.fifo_size = hw->fifo_size;
168 return 0;
169}
170EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
171
172
173int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
174 int event)
175{
176 struct snd_soc_dpcm *dpcm;
177
178 for_each_dpcm_be(fe, dir, dpcm) {
179
180 struct snd_soc_pcm_runtime *be = dpcm->be;
181
182 dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
183 be->dai_link->name, event, dir);
184
185 if ((event == SND_SOC_DAPM_STREAM_STOP) &&
186 (be->dpcm[dir].users >= 1))
187 continue;
188
189 snd_soc_dapm_stream_event(be, dir, event);
190 }
191
192 snd_soc_dapm_stream_event(fe, dir, event);
193
194 return 0;
195}
196
197static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
198 struct snd_soc_dai *soc_dai)
199{
200 struct snd_soc_pcm_runtime *rtd = substream->private_data;
201 int ret;
202
203 if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
204 rtd->dai_link->symmetric_rates)) {
205 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
206 soc_dai->rate);
207
208 ret = snd_pcm_hw_constraint_single(substream->runtime,
209 SNDRV_PCM_HW_PARAM_RATE,
210 soc_dai->rate);
211 if (ret < 0) {
212 dev_err(soc_dai->dev,
213 "ASoC: Unable to apply rate constraint: %d\n",
214 ret);
215 return ret;
216 }
217 }
218
219 if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
220 rtd->dai_link->symmetric_channels)) {
221 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
222 soc_dai->channels);
223
224 ret = snd_pcm_hw_constraint_single(substream->runtime,
225 SNDRV_PCM_HW_PARAM_CHANNELS,
226 soc_dai->channels);
227 if (ret < 0) {
228 dev_err(soc_dai->dev,
229 "ASoC: Unable to apply channel symmetry constraint: %d\n",
230 ret);
231 return ret;
232 }
233 }
234
235 if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
236 rtd->dai_link->symmetric_samplebits)) {
237 dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
238 soc_dai->sample_bits);
239
240 ret = snd_pcm_hw_constraint_single(substream->runtime,
241 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
242 soc_dai->sample_bits);
243 if (ret < 0) {
244 dev_err(soc_dai->dev,
245 "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
246 ret);
247 return ret;
248 }
249 }
250
251 return 0;
252}
253
254static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
255 struct snd_pcm_hw_params *params)
256{
257 struct snd_soc_pcm_runtime *rtd = substream->private_data;
258 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
259 struct snd_soc_dai *codec_dai;
260 unsigned int rate, channels, sample_bits, symmetry, i;
261
262 rate = params_rate(params);
263 channels = params_channels(params);
264 sample_bits = snd_pcm_format_physical_width(params_format(params));
265
266
267 symmetry = cpu_dai->driver->symmetric_rates ||
268 rtd->dai_link->symmetric_rates;
269
270 for_each_rtd_codec_dai(rtd, i, codec_dai)
271 symmetry |= codec_dai->driver->symmetric_rates;
272
273 if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
274 dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
275 cpu_dai->rate, rate);
276 return -EINVAL;
277 }
278
279 symmetry = cpu_dai->driver->symmetric_channels ||
280 rtd->dai_link->symmetric_channels;
281
282 for_each_rtd_codec_dai(rtd, i, codec_dai)
283 symmetry |= codec_dai->driver->symmetric_channels;
284
285 if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
286 dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
287 cpu_dai->channels, channels);
288 return -EINVAL;
289 }
290
291 symmetry = cpu_dai->driver->symmetric_samplebits ||
292 rtd->dai_link->symmetric_samplebits;
293
294 for_each_rtd_codec_dai(rtd, i, codec_dai)
295 symmetry |= codec_dai->driver->symmetric_samplebits;
296
297 if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
298 dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
299 cpu_dai->sample_bits, sample_bits);
300 return -EINVAL;
301 }
302
303 return 0;
304}
305
306static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
307{
308 struct snd_soc_pcm_runtime *rtd = substream->private_data;
309 struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
310 struct snd_soc_dai_link *link = rtd->dai_link;
311 struct snd_soc_dai *codec_dai;
312 unsigned int symmetry, i;
313
314 symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
315 cpu_driver->symmetric_channels || link->symmetric_channels ||
316 cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
317
318 for_each_rtd_codec_dai(rtd, i, codec_dai)
319 symmetry = symmetry ||
320 codec_dai->driver->symmetric_rates ||
321 codec_dai->driver->symmetric_channels ||
322 codec_dai->driver->symmetric_samplebits;
323
324 return symmetry;
325}
326
327static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
328{
329 struct snd_soc_pcm_runtime *rtd = substream->private_data;
330 int ret;
331
332 if (!bits)
333 return;
334
335 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
336 if (ret != 0)
337 dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
338 bits, ret);
339}
340
341static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
342{
343 struct snd_soc_pcm_runtime *rtd = substream->private_data;
344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
345 struct snd_soc_dai *codec_dai;
346 int i;
347 unsigned int bits = 0, cpu_bits;
348
349 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
350 for_each_rtd_codec_dai(rtd, i, codec_dai) {
351 if (codec_dai->driver->playback.sig_bits == 0) {
352 bits = 0;
353 break;
354 }
355 bits = max(codec_dai->driver->playback.sig_bits, bits);
356 }
357 cpu_bits = cpu_dai->driver->playback.sig_bits;
358 } else {
359 for_each_rtd_codec_dai(rtd, i, codec_dai) {
360 if (codec_dai->driver->capture.sig_bits == 0) {
361 bits = 0;
362 break;
363 }
364 bits = max(codec_dai->driver->capture.sig_bits, bits);
365 }
366 cpu_bits = cpu_dai->driver->capture.sig_bits;
367 }
368
369 soc_pcm_set_msb(substream, bits);
370 soc_pcm_set_msb(substream, cpu_bits);
371}
372
373static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
374{
375 struct snd_pcm_runtime *runtime = substream->runtime;
376 struct snd_pcm_hardware *hw = &runtime->hw;
377 struct snd_soc_pcm_runtime *rtd = substream->private_data;
378 struct snd_soc_dai *codec_dai;
379 struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
380 struct snd_soc_dai_driver *codec_dai_drv;
381 struct snd_soc_pcm_stream *codec_stream;
382 struct snd_soc_pcm_stream *cpu_stream;
383 unsigned int chan_min = 0, chan_max = UINT_MAX;
384 unsigned int rate_min = 0, rate_max = UINT_MAX;
385 unsigned int rates = UINT_MAX;
386 u64 formats = ULLONG_MAX;
387 int i;
388
389 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
390 cpu_stream = &cpu_dai_drv->playback;
391 else
392 cpu_stream = &cpu_dai_drv->capture;
393
394
395 for_each_rtd_codec_dai(rtd, i, codec_dai) {
396
397
398
399
400
401
402
403
404
405
406 if (!snd_soc_dai_stream_valid(codec_dai,
407 substream->stream))
408 continue;
409
410 codec_dai_drv = codec_dai->driver;
411 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
412 codec_stream = &codec_dai_drv->playback;
413 else
414 codec_stream = &codec_dai_drv->capture;
415 chan_min = max(chan_min, codec_stream->channels_min);
416 chan_max = min(chan_max, codec_stream->channels_max);
417 rate_min = max(rate_min, codec_stream->rate_min);
418 rate_max = min_not_zero(rate_max, codec_stream->rate_max);
419 formats &= codec_stream->formats;
420 rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
421 }
422
423
424
425
426
427
428 if (rtd->num_codecs > 1) {
429 chan_min = cpu_stream->channels_min;
430 chan_max = cpu_stream->channels_max;
431 }
432
433 hw->channels_min = max(chan_min, cpu_stream->channels_min);
434 hw->channels_max = min(chan_max, cpu_stream->channels_max);
435 if (hw->formats)
436 hw->formats &= formats & cpu_stream->formats;
437 else
438 hw->formats = formats & cpu_stream->formats;
439 hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
440
441 snd_pcm_limit_hw_rates(runtime);
442
443 hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
444 hw->rate_min = max(hw->rate_min, rate_min);
445 hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
446 hw->rate_max = min_not_zero(hw->rate_max, rate_max);
447}
448
449static int soc_pcm_components_open(struct snd_pcm_substream *substream,
450 struct snd_soc_component **last)
451{
452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
453 struct snd_soc_rtdcom_list *rtdcom;
454 struct snd_soc_component *component;
455 int ret = 0;
456
457 for_each_rtdcom(rtd, rtdcom) {
458 component = rtdcom->component;
459 *last = component;
460
461 if (component->driver->module_get_upon_open &&
462 !try_module_get(component->dev->driver->owner)) {
463 dev_err(component->dev,
464 "ASoC: can't get module %s\n",
465 component->name);
466 return -ENODEV;
467 }
468
469 if (!component->driver->ops ||
470 !component->driver->ops->open)
471 continue;
472
473 ret = component->driver->ops->open(substream);
474 if (ret < 0) {
475 dev_err(component->dev,
476 "ASoC: can't open component %s: %d\n",
477 component->name, ret);
478 return ret;
479 }
480 }
481 *last = NULL;
482 return 0;
483}
484
485static int soc_pcm_components_close(struct snd_pcm_substream *substream,
486 struct snd_soc_component *last)
487{
488 struct snd_soc_pcm_runtime *rtd = substream->private_data;
489 struct snd_soc_rtdcom_list *rtdcom;
490 struct snd_soc_component *component;
491
492 for_each_rtdcom(rtd, rtdcom) {
493 component = rtdcom->component;
494
495 if (component == last)
496 break;
497
498 if (component->driver->ops &&
499 component->driver->ops->close)
500 component->driver->ops->close(substream);
501
502 if (component->driver->module_get_upon_open)
503 module_put(component->dev->driver->owner);
504 }
505
506 return 0;
507}
508
509
510
511
512
513
514static int soc_pcm_open(struct snd_pcm_substream *substream)
515{
516 struct snd_soc_pcm_runtime *rtd = substream->private_data;
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 struct snd_soc_component *component;
519 struct snd_soc_rtdcom_list *rtdcom;
520 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
521 struct snd_soc_dai *codec_dai;
522 const char *codec_dai_name = "multicodec";
523 int i, ret = 0;
524
525 pinctrl_pm_select_default_state(cpu_dai->dev);
526 for_each_rtd_codec_dai(rtd, i, codec_dai)
527 pinctrl_pm_select_default_state(codec_dai->dev);
528
529 for_each_rtdcom(rtd, rtdcom) {
530 component = rtdcom->component;
531
532 pm_runtime_get_sync(component->dev);
533 }
534
535 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
536
537
538 if (cpu_dai->driver->ops->startup) {
539 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
540 if (ret < 0) {
541 dev_err(cpu_dai->dev, "ASoC: can't open interface"
542 " %s: %d\n", cpu_dai->name, ret);
543 goto out;
544 }
545 }
546
547 ret = soc_pcm_components_open(substream, &component);
548 if (ret < 0)
549 goto component_err;
550
551 for_each_rtd_codec_dai(rtd, i, codec_dai) {
552 if (codec_dai->driver->ops->startup) {
553 ret = codec_dai->driver->ops->startup(substream,
554 codec_dai);
555 if (ret < 0) {
556 dev_err(codec_dai->dev,
557 "ASoC: can't open codec %s: %d\n",
558 codec_dai->name, ret);
559 goto codec_dai_err;
560 }
561 }
562
563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
564 codec_dai->tx_mask = 0;
565 else
566 codec_dai->rx_mask = 0;
567 }
568
569 if (rtd->dai_link->ops->startup) {
570 ret = rtd->dai_link->ops->startup(substream);
571 if (ret < 0) {
572 pr_err("ASoC: %s startup failed: %d\n",
573 rtd->dai_link->name, ret);
574 goto machine_err;
575 }
576 }
577
578
579 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
580 goto dynamic;
581
582
583 soc_pcm_init_runtime_hw(substream);
584
585 if (rtd->num_codecs == 1)
586 codec_dai_name = rtd->codec_dai->name;
587
588 if (soc_pcm_has_symmetry(substream))
589 runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
590
591 ret = -EINVAL;
592 if (!runtime->hw.rates) {
593 printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
594 codec_dai_name, cpu_dai->name);
595 goto config_err;
596 }
597 if (!runtime->hw.formats) {
598 printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
599 codec_dai_name, cpu_dai->name);
600 goto config_err;
601 }
602 if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
603 runtime->hw.channels_min > runtime->hw.channels_max) {
604 printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
605 codec_dai_name, cpu_dai->name);
606 goto config_err;
607 }
608
609 soc_pcm_apply_msb(substream);
610
611
612 if (cpu_dai->active) {
613 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
614 if (ret != 0)
615 goto config_err;
616 }
617
618 for_each_rtd_codec_dai(rtd, i, codec_dai) {
619 if (codec_dai->active) {
620 ret = soc_pcm_apply_symmetry(substream, codec_dai);
621 if (ret != 0)
622 goto config_err;
623 }
624 }
625
626 pr_debug("ASoC: %s <-> %s info:\n",
627 codec_dai_name, cpu_dai->name);
628 pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
629 pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
630 runtime->hw.channels_max);
631 pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
632 runtime->hw.rate_max);
633
634dynamic:
635
636 snd_soc_runtime_activate(rtd, substream->stream);
637
638 mutex_unlock(&rtd->pcm_mutex);
639 return 0;
640
641config_err:
642 if (rtd->dai_link->ops->shutdown)
643 rtd->dai_link->ops->shutdown(substream);
644
645machine_err:
646 i = rtd->num_codecs;
647
648codec_dai_err:
649 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
650 if (codec_dai->driver->ops->shutdown)
651 codec_dai->driver->ops->shutdown(substream, codec_dai);
652 }
653
654component_err:
655 soc_pcm_components_close(substream, component);
656
657 if (cpu_dai->driver->ops->shutdown)
658 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
659out:
660 mutex_unlock(&rtd->pcm_mutex);
661
662 for_each_rtdcom(rtd, rtdcom) {
663 component = rtdcom->component;
664
665 pm_runtime_mark_last_busy(component->dev);
666 pm_runtime_put_autosuspend(component->dev);
667 }
668
669 for_each_rtd_codec_dai(rtd, i, codec_dai) {
670 if (!codec_dai->active)
671 pinctrl_pm_select_sleep_state(codec_dai->dev);
672 }
673 if (!cpu_dai->active)
674 pinctrl_pm_select_sleep_state(cpu_dai->dev);
675
676 return ret;
677}
678
679
680
681
682
683
684static void close_delayed_work(struct work_struct *work)
685{
686 struct snd_soc_pcm_runtime *rtd =
687 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
688 struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
689
690 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
691
692 dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
693 codec_dai->driver->playback.stream_name,
694 codec_dai->playback_active ? "active" : "inactive",
695 rtd->pop_wait ? "yes" : "no");
696
697
698 if (rtd->pop_wait == 1) {
699 rtd->pop_wait = 0;
700 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
701 SND_SOC_DAPM_STREAM_STOP);
702 }
703
704 mutex_unlock(&rtd->pcm_mutex);
705}
706
707
708
709
710
711
712static int soc_pcm_close(struct snd_pcm_substream *substream)
713{
714 struct snd_soc_pcm_runtime *rtd = substream->private_data;
715 struct snd_soc_component *component;
716 struct snd_soc_rtdcom_list *rtdcom;
717 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
718 struct snd_soc_dai *codec_dai;
719 int i;
720
721 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
722
723 snd_soc_runtime_deactivate(rtd, substream->stream);
724
725
726 if (!cpu_dai->active)
727 cpu_dai->rate = 0;
728
729 for_each_rtd_codec_dai(rtd, i, codec_dai) {
730 if (!codec_dai->active)
731 codec_dai->rate = 0;
732 }
733
734 snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
735
736 if (cpu_dai->driver->ops->shutdown)
737 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
738
739 for_each_rtd_codec_dai(rtd, i, codec_dai) {
740 if (codec_dai->driver->ops->shutdown)
741 codec_dai->driver->ops->shutdown(substream, codec_dai);
742 }
743
744 if (rtd->dai_link->ops->shutdown)
745 rtd->dai_link->ops->shutdown(substream);
746
747 soc_pcm_components_close(substream, NULL);
748
749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
750 if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
751
752 snd_soc_dapm_stream_event(rtd,
753 SNDRV_PCM_STREAM_PLAYBACK,
754 SND_SOC_DAPM_STREAM_STOP);
755 } else {
756
757 rtd->pop_wait = 1;
758 queue_delayed_work(system_power_efficient_wq,
759 &rtd->delayed_work,
760 msecs_to_jiffies(rtd->pmdown_time));
761 }
762 } else {
763
764 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
765 SND_SOC_DAPM_STREAM_STOP);
766 }
767
768 mutex_unlock(&rtd->pcm_mutex);
769
770 for_each_rtdcom(rtd, rtdcom) {
771 component = rtdcom->component;
772
773 pm_runtime_mark_last_busy(component->dev);
774 pm_runtime_put_autosuspend(component->dev);
775 }
776
777 for_each_rtd_codec_dai(rtd, i, codec_dai) {
778 if (!codec_dai->active)
779 pinctrl_pm_select_sleep_state(codec_dai->dev);
780 }
781 if (!cpu_dai->active)
782 pinctrl_pm_select_sleep_state(cpu_dai->dev);
783
784 return 0;
785}
786
787
788
789
790
791
792static int soc_pcm_prepare(struct snd_pcm_substream *substream)
793{
794 struct snd_soc_pcm_runtime *rtd = substream->private_data;
795 struct snd_soc_component *component;
796 struct snd_soc_rtdcom_list *rtdcom;
797 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
798 struct snd_soc_dai *codec_dai;
799 int i, ret = 0;
800
801 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
802
803 if (rtd->dai_link->ops->prepare) {
804 ret = rtd->dai_link->ops->prepare(substream);
805 if (ret < 0) {
806 dev_err(rtd->card->dev, "ASoC: machine prepare error:"
807 " %d\n", ret);
808 goto out;
809 }
810 }
811
812 for_each_rtdcom(rtd, rtdcom) {
813 component = rtdcom->component;
814
815 if (!component->driver->ops ||
816 !component->driver->ops->prepare)
817 continue;
818
819 ret = component->driver->ops->prepare(substream);
820 if (ret < 0) {
821 dev_err(component->dev,
822 "ASoC: platform prepare error: %d\n", ret);
823 goto out;
824 }
825 }
826
827 for_each_rtd_codec_dai(rtd, i, codec_dai) {
828 if (codec_dai->driver->ops->prepare) {
829 ret = codec_dai->driver->ops->prepare(substream,
830 codec_dai);
831 if (ret < 0) {
832 dev_err(codec_dai->dev,
833 "ASoC: codec DAI prepare error: %d\n",
834 ret);
835 goto out;
836 }
837 }
838 }
839
840 if (cpu_dai->driver->ops->prepare) {
841 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
842 if (ret < 0) {
843 dev_err(cpu_dai->dev,
844 "ASoC: cpu DAI prepare error: %d\n", ret);
845 goto out;
846 }
847 }
848
849
850 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
851 rtd->pop_wait) {
852 rtd->pop_wait = 0;
853 cancel_delayed_work(&rtd->delayed_work);
854 }
855
856 snd_soc_dapm_stream_event(rtd, substream->stream,
857 SND_SOC_DAPM_STREAM_START);
858
859 for_each_rtd_codec_dai(rtd, i, codec_dai)
860 snd_soc_dai_digital_mute(codec_dai, 0,
861 substream->stream);
862 snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
863
864out:
865 mutex_unlock(&rtd->pcm_mutex);
866 return ret;
867}
868
869static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
870 unsigned int mask)
871{
872 struct snd_interval *interval;
873 int channels = hweight_long(mask);
874
875 interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
876 interval->min = channels;
877 interval->max = channels;
878}
879
880int soc_dai_hw_params(struct snd_pcm_substream *substream,
881 struct snd_pcm_hw_params *params,
882 struct snd_soc_dai *dai)
883{
884 struct snd_soc_pcm_runtime *rtd = substream->private_data;
885 int ret;
886
887
888 if (rtd->dai_link->be_hw_params_fixup) {
889 ret = rtd->dai_link->be_hw_params_fixup(rtd, params);
890 if (ret < 0) {
891 dev_err(rtd->dev,
892 "ASoC: hw_params topology fixup failed %d\n",
893 ret);
894 return ret;
895 }
896 }
897
898 if (dai->driver->ops->hw_params) {
899 ret = dai->driver->ops->hw_params(substream, params, dai);
900 if (ret < 0) {
901 dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
902 dai->name, ret);
903 return ret;
904 }
905 }
906
907 return 0;
908}
909
910static int soc_pcm_components_hw_free(struct snd_pcm_substream *substream,
911 struct snd_soc_component *last)
912{
913 struct snd_soc_pcm_runtime *rtd = substream->private_data;
914 struct snd_soc_rtdcom_list *rtdcom;
915 struct snd_soc_component *component;
916
917 for_each_rtdcom(rtd, rtdcom) {
918 component = rtdcom->component;
919
920 if (component == last)
921 break;
922
923 if (!component->driver->ops ||
924 !component->driver->ops->hw_free)
925 continue;
926
927 component->driver->ops->hw_free(substream);
928 }
929
930 return 0;
931}
932
933
934
935
936
937
938static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
939 struct snd_pcm_hw_params *params)
940{
941 struct snd_soc_pcm_runtime *rtd = substream->private_data;
942 struct snd_soc_component *component;
943 struct snd_soc_rtdcom_list *rtdcom;
944 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
945 struct snd_soc_dai *codec_dai;
946 int i, ret = 0;
947
948 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
949 if (rtd->dai_link->ops->hw_params) {
950 ret = rtd->dai_link->ops->hw_params(substream, params);
951 if (ret < 0) {
952 dev_err(rtd->card->dev, "ASoC: machine hw_params"
953 " failed: %d\n", ret);
954 goto out;
955 }
956 }
957
958 for_each_rtd_codec_dai(rtd, i, codec_dai) {
959 struct snd_pcm_hw_params codec_params;
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
976 continue;
977
978
979 codec_params = *params;
980
981
982 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
983 codec_dai->tx_mask)
984 soc_pcm_codec_params_fixup(&codec_params,
985 codec_dai->tx_mask);
986
987 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
988 codec_dai->rx_mask)
989 soc_pcm_codec_params_fixup(&codec_params,
990 codec_dai->rx_mask);
991
992 ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
993 if(ret < 0)
994 goto codec_err;
995
996 codec_dai->rate = params_rate(&codec_params);
997 codec_dai->channels = params_channels(&codec_params);
998 codec_dai->sample_bits = snd_pcm_format_physical_width(
999 params_format(&codec_params));
1000
1001 snd_soc_dapm_update_dai(substream, &codec_params, codec_dai);
1002 }
1003
1004 ret = soc_dai_hw_params(substream, params, cpu_dai);
1005 if (ret < 0)
1006 goto interface_err;
1007
1008
1009 cpu_dai->rate = params_rate(params);
1010 cpu_dai->channels = params_channels(params);
1011 cpu_dai->sample_bits =
1012 snd_pcm_format_physical_width(params_format(params));
1013
1014 snd_soc_dapm_update_dai(substream, params, cpu_dai);
1015
1016 for_each_rtdcom(rtd, rtdcom) {
1017 component = rtdcom->component;
1018
1019 if (!component->driver->ops ||
1020 !component->driver->ops->hw_params)
1021 continue;
1022
1023 ret = component->driver->ops->hw_params(substream, params);
1024 if (ret < 0) {
1025 dev_err(component->dev,
1026 "ASoC: %s hw params failed: %d\n",
1027 component->name, ret);
1028 goto component_err;
1029 }
1030 }
1031 component = NULL;
1032
1033 ret = soc_pcm_params_symmetry(substream, params);
1034 if (ret)
1035 goto component_err;
1036out:
1037 mutex_unlock(&rtd->pcm_mutex);
1038 return ret;
1039
1040component_err:
1041 soc_pcm_components_hw_free(substream, component);
1042
1043 if (cpu_dai->driver->ops->hw_free)
1044 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1045 cpu_dai->rate = 0;
1046
1047interface_err:
1048 i = rtd->num_codecs;
1049
1050codec_err:
1051 for_each_rtd_codec_dai_rollback(rtd, i, codec_dai) {
1052 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1053 continue;
1054
1055 if (codec_dai->driver->ops->hw_free)
1056 codec_dai->driver->ops->hw_free(substream, codec_dai);
1057 codec_dai->rate = 0;
1058 }
1059
1060 if (rtd->dai_link->ops->hw_free)
1061 rtd->dai_link->ops->hw_free(substream);
1062
1063 mutex_unlock(&rtd->pcm_mutex);
1064 return ret;
1065}
1066
1067
1068
1069
1070static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
1071{
1072 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1073 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1074 struct snd_soc_dai *codec_dai;
1075 bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1076 int i;
1077
1078 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
1079
1080
1081 if (cpu_dai->active == 1) {
1082 cpu_dai->rate = 0;
1083 cpu_dai->channels = 0;
1084 cpu_dai->sample_bits = 0;
1085 }
1086
1087 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1088 if (codec_dai->active == 1) {
1089 codec_dai->rate = 0;
1090 codec_dai->channels = 0;
1091 codec_dai->sample_bits = 0;
1092 }
1093 }
1094
1095
1096 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1097 if ((playback && codec_dai->playback_active == 1) ||
1098 (!playback && codec_dai->capture_active == 1))
1099 snd_soc_dai_digital_mute(codec_dai, 1,
1100 substream->stream);
1101 }
1102
1103
1104 if (rtd->dai_link->ops->hw_free)
1105 rtd->dai_link->ops->hw_free(substream);
1106
1107
1108 soc_pcm_components_hw_free(substream, NULL);
1109
1110
1111 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1112 if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
1113 continue;
1114
1115 if (codec_dai->driver->ops->hw_free)
1116 codec_dai->driver->ops->hw_free(substream, codec_dai);
1117 }
1118
1119 if (cpu_dai->driver->ops->hw_free)
1120 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
1121
1122 mutex_unlock(&rtd->pcm_mutex);
1123 return 0;
1124}
1125
1126static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1127{
1128 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1129 struct snd_soc_component *component;
1130 struct snd_soc_rtdcom_list *rtdcom;
1131 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1132 struct snd_soc_dai *codec_dai;
1133 int i, ret;
1134
1135 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1136 if (codec_dai->driver->ops->trigger) {
1137 ret = codec_dai->driver->ops->trigger(substream,
1138 cmd, codec_dai);
1139 if (ret < 0)
1140 return ret;
1141 }
1142 }
1143
1144 for_each_rtdcom(rtd, rtdcom) {
1145 component = rtdcom->component;
1146
1147 if (!component->driver->ops ||
1148 !component->driver->ops->trigger)
1149 continue;
1150
1151 ret = component->driver->ops->trigger(substream, cmd);
1152 if (ret < 0)
1153 return ret;
1154 }
1155
1156 if (cpu_dai->driver->ops->trigger) {
1157 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
1158 if (ret < 0)
1159 return ret;
1160 }
1161
1162 if (rtd->dai_link->ops->trigger) {
1163 ret = rtd->dai_link->ops->trigger(substream, cmd);
1164 if (ret < 0)
1165 return ret;
1166 }
1167
1168 return 0;
1169}
1170
1171static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
1172 int cmd)
1173{
1174 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1175 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1176 struct snd_soc_dai *codec_dai;
1177 int i, ret;
1178
1179 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1180 if (codec_dai->driver->ops->bespoke_trigger) {
1181 ret = codec_dai->driver->ops->bespoke_trigger(substream,
1182 cmd, codec_dai);
1183 if (ret < 0)
1184 return ret;
1185 }
1186 }
1187
1188 if (cpu_dai->driver->ops->bespoke_trigger) {
1189 ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
1190 if (ret < 0)
1191 return ret;
1192 }
1193 return 0;
1194}
1195
1196
1197
1198
1199
1200static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
1201{
1202 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1203 struct snd_soc_component *component;
1204 struct snd_soc_rtdcom_list *rtdcom;
1205 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1206 struct snd_soc_dai *codec_dai;
1207 struct snd_pcm_runtime *runtime = substream->runtime;
1208 snd_pcm_uframes_t offset = 0;
1209 snd_pcm_sframes_t delay = 0;
1210 snd_pcm_sframes_t codec_delay = 0;
1211 int i;
1212
1213
1214 runtime->delay = 0;
1215
1216 for_each_rtdcom(rtd, rtdcom) {
1217 component = rtdcom->component;
1218
1219 if (!component->driver->ops ||
1220 !component->driver->ops->pointer)
1221 continue;
1222
1223
1224 offset = component->driver->ops->pointer(substream);
1225 break;
1226 }
1227
1228 delay = runtime->delay;
1229
1230 if (cpu_dai->driver->ops->delay)
1231 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
1232
1233 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1234 if (codec_dai->driver->ops->delay)
1235 codec_delay = max(codec_delay,
1236 codec_dai->driver->ops->delay(substream,
1237 codec_dai));
1238 }
1239 delay += codec_delay;
1240
1241 runtime->delay = delay;
1242
1243 return offset;
1244}
1245
1246
1247static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
1248 struct snd_soc_pcm_runtime *be, int stream)
1249{
1250 struct snd_soc_dpcm *dpcm;
1251 unsigned long flags;
1252
1253
1254 for_each_dpcm_be(fe, stream, dpcm) {
1255 if (dpcm->be == be && dpcm->fe == fe)
1256 return 0;
1257 }
1258
1259 dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
1260 if (!dpcm)
1261 return -ENOMEM;
1262
1263 dpcm->be = be;
1264 dpcm->fe = fe;
1265 be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
1266 dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
1267 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1268 list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
1269 list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
1270 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1271
1272 dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
1273 stream ? "capture" : "playback", fe->dai_link->name,
1274 stream ? "<-" : "->", be->dai_link->name);
1275
1276#ifdef CONFIG_DEBUG_FS
1277 if (fe->debugfs_dpcm_root)
1278 dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
1279 fe->debugfs_dpcm_root, &dpcm->state);
1280#endif
1281 return 1;
1282}
1283
1284
1285static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
1286 struct snd_soc_pcm_runtime *be, int stream)
1287{
1288 struct snd_soc_dpcm *dpcm;
1289 struct snd_pcm_substream *fe_substream, *be_substream;
1290
1291
1292 if (!be->dpcm[stream].users)
1293 return;
1294
1295 be_substream = snd_soc_dpcm_get_substream(be, stream);
1296
1297 for_each_dpcm_fe(be, stream, dpcm) {
1298 if (dpcm->fe == fe)
1299 continue;
1300
1301 dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
1302 stream ? "capture" : "playback",
1303 dpcm->fe->dai_link->name,
1304 stream ? "<-" : "->", dpcm->be->dai_link->name);
1305
1306 fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
1307 be_substream->runtime = fe_substream->runtime;
1308 break;
1309 }
1310}
1311
1312
1313void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
1314{
1315 struct snd_soc_dpcm *dpcm, *d;
1316 unsigned long flags;
1317
1318 for_each_dpcm_be_safe(fe, stream, dpcm, d) {
1319 dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
1320 stream ? "capture" : "playback",
1321 dpcm->be->dai_link->name);
1322
1323 if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
1324 continue;
1325
1326 dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
1327 stream ? "capture" : "playback", fe->dai_link->name,
1328 stream ? "<-" : "->", dpcm->be->dai_link->name);
1329
1330
1331 dpcm_be_reparent(fe, dpcm->be, stream);
1332
1333#ifdef CONFIG_DEBUG_FS
1334 debugfs_remove(dpcm->debugfs_state);
1335#endif
1336 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1337 list_del(&dpcm->list_be);
1338 list_del(&dpcm->list_fe);
1339 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1340 kfree(dpcm);
1341 }
1342}
1343
1344
1345static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
1346 struct snd_soc_dapm_widget *widget, int stream)
1347{
1348 struct snd_soc_pcm_runtime *be;
1349 struct snd_soc_dai *dai;
1350 int i;
1351
1352 dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
1353
1354 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1355 for_each_card_rtds(card, be) {
1356
1357 if (!be->dai_link->no_pcm)
1358 continue;
1359
1360 dev_dbg(card->dev, "ASoC: try BE : %s\n",
1361 be->cpu_dai->playback_widget ?
1362 be->cpu_dai->playback_widget->name : "(not set)");
1363
1364 if (be->cpu_dai->playback_widget == widget)
1365 return be;
1366
1367 for_each_rtd_codec_dai(be, i, dai) {
1368 if (dai->playback_widget == widget)
1369 return be;
1370 }
1371 }
1372 } else {
1373
1374 for_each_card_rtds(card, be) {
1375
1376 if (!be->dai_link->no_pcm)
1377 continue;
1378
1379 dev_dbg(card->dev, "ASoC: try BE %s\n",
1380 be->cpu_dai->capture_widget ?
1381 be->cpu_dai->capture_widget->name : "(not set)");
1382
1383 if (be->cpu_dai->capture_widget == widget)
1384 return be;
1385
1386 for_each_rtd_codec_dai(be, i, dai) {
1387 if (dai->capture_widget == widget)
1388 return be;
1389 }
1390 }
1391 }
1392
1393
1394 dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
1395 stream ? "capture" : "playback", widget->name);
1396 return NULL;
1397}
1398
1399static inline struct snd_soc_dapm_widget *
1400 dai_get_widget(struct snd_soc_dai *dai, int stream)
1401{
1402 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1403 return dai->playback_widget;
1404 else
1405 return dai->capture_widget;
1406}
1407
1408static int widget_in_list(struct snd_soc_dapm_widget_list *list,
1409 struct snd_soc_dapm_widget *widget)
1410{
1411 int i;
1412
1413 for (i = 0; i < list->num_widgets; i++) {
1414 if (widget == list->widgets[i])
1415 return 1;
1416 }
1417
1418 return 0;
1419}
1420
1421static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
1422 enum snd_soc_dapm_direction dir)
1423{
1424 struct snd_soc_card *card = widget->dapm->card;
1425 struct snd_soc_pcm_runtime *rtd;
1426 struct snd_soc_dai *dai;
1427 int i;
1428
1429 if (dir == SND_SOC_DAPM_DIR_OUT) {
1430 for_each_card_rtds(card, rtd) {
1431 if (!rtd->dai_link->no_pcm)
1432 continue;
1433
1434 if (rtd->cpu_dai->playback_widget == widget)
1435 return true;
1436
1437 for_each_rtd_codec_dai(rtd, i, dai) {
1438 if (dai->playback_widget == widget)
1439 return true;
1440 }
1441 }
1442 } else {
1443 for_each_card_rtds(card, rtd) {
1444 if (!rtd->dai_link->no_pcm)
1445 continue;
1446
1447 if (rtd->cpu_dai->capture_widget == widget)
1448 return true;
1449
1450 for_each_rtd_codec_dai(rtd, i, dai) {
1451 if (dai->capture_widget == widget)
1452 return true;
1453 }
1454 }
1455 }
1456
1457 return false;
1458}
1459
1460int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
1461 int stream, struct snd_soc_dapm_widget_list **list)
1462{
1463 struct snd_soc_dai *cpu_dai = fe->cpu_dai;
1464 int paths;
1465
1466
1467 paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
1468 dpcm_end_walk_at_be);
1469
1470 dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
1471 stream ? "capture" : "playback");
1472
1473 return paths;
1474}
1475
1476static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
1477 struct snd_soc_dapm_widget_list **list_)
1478{
1479 struct snd_soc_dpcm *dpcm;
1480 struct snd_soc_dapm_widget_list *list = *list_;
1481 struct snd_soc_dapm_widget *widget;
1482 struct snd_soc_dai *dai;
1483 int prune = 0;
1484
1485
1486 for_each_dpcm_be(fe, stream, dpcm) {
1487 unsigned int i;
1488
1489
1490 widget = dai_get_widget(dpcm->be->cpu_dai, stream);
1491
1492
1493 if (widget && widget_in_list(list, widget))
1494 continue;
1495
1496
1497 for_each_rtd_codec_dai(dpcm->be, i, dai) {
1498 widget = dai_get_widget(dai, stream);
1499
1500
1501 if (widget && widget_in_list(list, widget))
1502 continue;
1503 }
1504
1505 dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
1506 stream ? "capture" : "playback",
1507 dpcm->be->dai_link->name, fe->dai_link->name);
1508 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
1509 dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1510 prune++;
1511 }
1512
1513 dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
1514 return prune;
1515}
1516
1517static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
1518 struct snd_soc_dapm_widget_list **list_)
1519{
1520 struct snd_soc_card *card = fe->card;
1521 struct snd_soc_dapm_widget_list *list = *list_;
1522 struct snd_soc_pcm_runtime *be;
1523 int i, new = 0, err;
1524
1525
1526 for (i = 0; i < list->num_widgets; i++) {
1527
1528 switch (list->widgets[i]->id) {
1529 case snd_soc_dapm_dai_in:
1530 if (stream != SNDRV_PCM_STREAM_PLAYBACK)
1531 continue;
1532 break;
1533 case snd_soc_dapm_dai_out:
1534 if (stream != SNDRV_PCM_STREAM_CAPTURE)
1535 continue;
1536 break;
1537 default:
1538 continue;
1539 }
1540
1541
1542 be = dpcm_get_be(card, list->widgets[i], stream);
1543 if (!be) {
1544 dev_err(fe->dev, "ASoC: no BE found for %s\n",
1545 list->widgets[i]->name);
1546 continue;
1547 }
1548
1549
1550 if (!be->dai_link->no_pcm)
1551 continue;
1552
1553
1554 if (!fe->dpcm[stream].runtime && !fe->fe_compr)
1555 continue;
1556
1557
1558 err = dpcm_be_connect(fe, be, stream);
1559 if (err < 0) {
1560 dev_err(fe->dev, "ASoC: can't connect %s\n",
1561 list->widgets[i]->name);
1562 break;
1563 } else if (err == 0)
1564 continue;
1565
1566
1567 be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
1568 new++;
1569 }
1570
1571 dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
1572 return new;
1573}
1574
1575
1576
1577
1578
1579int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
1580 int stream, struct snd_soc_dapm_widget_list **list, int new)
1581{
1582 if (new)
1583 return dpcm_add_paths(fe, stream, list);
1584 else
1585 return dpcm_prune_paths(fe, stream, list);
1586}
1587
1588void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
1589{
1590 struct snd_soc_dpcm *dpcm;
1591 unsigned long flags;
1592
1593 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
1594 for_each_dpcm_be(fe, stream, dpcm)
1595 dpcm->be->dpcm[stream].runtime_update =
1596 SND_SOC_DPCM_UPDATE_NO;
1597 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
1598}
1599
1600static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
1601 int stream)
1602{
1603 struct snd_soc_dpcm *dpcm;
1604
1605
1606 for_each_dpcm_be(fe, stream, dpcm) {
1607
1608 struct snd_soc_pcm_runtime *be = dpcm->be;
1609 struct snd_pcm_substream *be_substream =
1610 snd_soc_dpcm_get_substream(be, stream);
1611
1612 if (be->dpcm[stream].users == 0)
1613 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
1614 stream ? "capture" : "playback",
1615 be->dpcm[stream].state);
1616
1617 if (--be->dpcm[stream].users != 0)
1618 continue;
1619
1620 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1621 continue;
1622
1623 soc_pcm_close(be_substream);
1624 be_substream->runtime = NULL;
1625 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1626 }
1627}
1628
1629int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
1630{
1631 struct snd_soc_dpcm *dpcm;
1632 int err, count = 0;
1633
1634
1635 for_each_dpcm_be(fe, stream, dpcm) {
1636
1637 struct snd_soc_pcm_runtime *be = dpcm->be;
1638 struct snd_pcm_substream *be_substream =
1639 snd_soc_dpcm_get_substream(be, stream);
1640
1641 if (!be_substream) {
1642 dev_err(be->dev, "ASoC: no backend %s stream\n",
1643 stream ? "capture" : "playback");
1644 continue;
1645 }
1646
1647
1648 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1649 continue;
1650
1651
1652 if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
1653 dev_err(be->dev, "ASoC: too many users %s at open %d\n",
1654 stream ? "capture" : "playback",
1655 be->dpcm[stream].state);
1656
1657 if (be->dpcm[stream].users++ != 0)
1658 continue;
1659
1660 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
1661 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
1662 continue;
1663
1664 dev_dbg(be->dev, "ASoC: open %s BE %s\n",
1665 stream ? "capture" : "playback", be->dai_link->name);
1666
1667 be_substream->runtime = be->dpcm[stream].runtime;
1668 err = soc_pcm_open(be_substream);
1669 if (err < 0) {
1670 dev_err(be->dev, "ASoC: BE open failed %d\n", err);
1671 be->dpcm[stream].users--;
1672 if (be->dpcm[stream].users < 0)
1673 dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
1674 stream ? "capture" : "playback",
1675 be->dpcm[stream].state);
1676
1677 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1678 goto unwind;
1679 }
1680
1681 be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
1682 count++;
1683 }
1684
1685 return count;
1686
1687unwind:
1688
1689 for_each_dpcm_be_rollback(fe, stream, dpcm) {
1690 struct snd_soc_pcm_runtime *be = dpcm->be;
1691 struct snd_pcm_substream *be_substream =
1692 snd_soc_dpcm_get_substream(be, stream);
1693
1694 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
1695 continue;
1696
1697 if (be->dpcm[stream].users == 0)
1698 dev_err(be->dev, "ASoC: no users %s at close %d\n",
1699 stream ? "capture" : "playback",
1700 be->dpcm[stream].state);
1701
1702 if (--be->dpcm[stream].users != 0)
1703 continue;
1704
1705 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
1706 continue;
1707
1708 soc_pcm_close(be_substream);
1709 be_substream->runtime = NULL;
1710 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
1711 }
1712
1713 return err;
1714}
1715
1716static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
1717 struct snd_soc_pcm_stream *stream)
1718{
1719 runtime->hw.rate_min = stream->rate_min;
1720 runtime->hw.rate_max = min_not_zero(stream->rate_max, UINT_MAX);
1721 runtime->hw.channels_min = stream->channels_min;
1722 runtime->hw.channels_max = stream->channels_max;
1723 if (runtime->hw.formats)
1724 runtime->hw.formats &= stream->formats;
1725 else
1726 runtime->hw.formats = stream->formats;
1727 runtime->hw.rates = stream->rates;
1728}
1729
1730static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
1731 u64 *formats)
1732{
1733 struct snd_soc_pcm_runtime *fe = substream->private_data;
1734 struct snd_soc_dpcm *dpcm;
1735 struct snd_soc_dai *dai;
1736 int stream = substream->stream;
1737
1738 if (!fe->dai_link->dpcm_merged_format)
1739 return;
1740
1741
1742
1743
1744
1745
1746 for_each_dpcm_be(fe, stream, dpcm) {
1747 struct snd_soc_pcm_runtime *be = dpcm->be;
1748 struct snd_soc_dai_driver *codec_dai_drv;
1749 struct snd_soc_pcm_stream *codec_stream;
1750 int i;
1751
1752 for_each_rtd_codec_dai(be, i, dai) {
1753
1754
1755
1756
1757 if (!snd_soc_dai_stream_valid(dai, stream))
1758 continue;
1759
1760 codec_dai_drv = dai->driver;
1761 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1762 codec_stream = &codec_dai_drv->playback;
1763 else
1764 codec_stream = &codec_dai_drv->capture;
1765
1766 *formats &= codec_stream->formats;
1767 }
1768 }
1769}
1770
1771static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
1772 unsigned int *channels_min,
1773 unsigned int *channels_max)
1774{
1775 struct snd_soc_pcm_runtime *fe = substream->private_data;
1776 struct snd_soc_dpcm *dpcm;
1777 int stream = substream->stream;
1778
1779 if (!fe->dai_link->dpcm_merged_chan)
1780 return;
1781
1782
1783
1784
1785
1786
1787 for_each_dpcm_be(fe, stream, dpcm) {
1788 struct snd_soc_pcm_runtime *be = dpcm->be;
1789 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1790 struct snd_soc_dai_driver *codec_dai_drv;
1791 struct snd_soc_pcm_stream *codec_stream;
1792 struct snd_soc_pcm_stream *cpu_stream;
1793
1794 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1795 cpu_stream = &cpu_dai_drv->playback;
1796 else
1797 cpu_stream = &cpu_dai_drv->capture;
1798
1799 *channels_min = max(*channels_min, cpu_stream->channels_min);
1800 *channels_max = min(*channels_max, cpu_stream->channels_max);
1801
1802
1803
1804
1805
1806 if (be->num_codecs == 1) {
1807 codec_dai_drv = be->codec_dais[0]->driver;
1808
1809 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1810 codec_stream = &codec_dai_drv->playback;
1811 else
1812 codec_stream = &codec_dai_drv->capture;
1813
1814 *channels_min = max(*channels_min,
1815 codec_stream->channels_min);
1816 *channels_max = min(*channels_max,
1817 codec_stream->channels_max);
1818 }
1819 }
1820}
1821
1822static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
1823 unsigned int *rates,
1824 unsigned int *rate_min,
1825 unsigned int *rate_max)
1826{
1827 struct snd_soc_pcm_runtime *fe = substream->private_data;
1828 struct snd_soc_dpcm *dpcm;
1829 int stream = substream->stream;
1830
1831 if (!fe->dai_link->dpcm_merged_rate)
1832 return;
1833
1834
1835
1836
1837
1838
1839 for_each_dpcm_be(fe, stream, dpcm) {
1840 struct snd_soc_pcm_runtime *be = dpcm->be;
1841 struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
1842 struct snd_soc_dai_driver *codec_dai_drv;
1843 struct snd_soc_pcm_stream *codec_stream;
1844 struct snd_soc_pcm_stream *cpu_stream;
1845 struct snd_soc_dai *dai;
1846 int i;
1847
1848 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1849 cpu_stream = &cpu_dai_drv->playback;
1850 else
1851 cpu_stream = &cpu_dai_drv->capture;
1852
1853 *rate_min = max(*rate_min, cpu_stream->rate_min);
1854 *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
1855 *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
1856
1857 for_each_rtd_codec_dai(be, i, dai) {
1858
1859
1860
1861
1862 if (!snd_soc_dai_stream_valid(dai, stream))
1863 continue;
1864
1865 codec_dai_drv = dai->driver;
1866 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1867 codec_stream = &codec_dai_drv->playback;
1868 else
1869 codec_stream = &codec_dai_drv->capture;
1870
1871 *rate_min = max(*rate_min, codec_stream->rate_min);
1872 *rate_max = min_not_zero(*rate_max,
1873 codec_stream->rate_max);
1874 *rates = snd_pcm_rate_mask_intersect(*rates,
1875 codec_stream->rates);
1876 }
1877 }
1878}
1879
1880static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
1881{
1882 struct snd_pcm_runtime *runtime = substream->runtime;
1883 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1884 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1885 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
1886
1887 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1888 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
1889 else
1890 dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
1891
1892 dpcm_runtime_merge_format(substream, &runtime->hw.formats);
1893 dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
1894 &runtime->hw.channels_max);
1895 dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
1896 &runtime->hw.rate_min, &runtime->hw.rate_max);
1897}
1898
1899static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
1900
1901
1902
1903
1904
1905
1906static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
1907 int stream, enum snd_soc_dpcm_update state)
1908{
1909 struct snd_pcm_substream *substream =
1910 snd_soc_dpcm_get_substream(fe, stream);
1911
1912 snd_pcm_stream_lock_irq(substream);
1913 if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
1914 dpcm_fe_dai_do_trigger(substream,
1915 fe->dpcm[stream].trigger_pending - 1);
1916 fe->dpcm[stream].trigger_pending = 0;
1917 }
1918 fe->dpcm[stream].runtime_update = state;
1919 snd_pcm_stream_unlock_irq(substream);
1920}
1921
1922static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
1923 int stream)
1924{
1925 struct snd_soc_dpcm *dpcm;
1926 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1927 struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
1928 int err;
1929
1930
1931 if (soc_pcm_has_symmetry(fe_substream))
1932 fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1933
1934
1935 if (fe_cpu_dai->active) {
1936 err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
1937 if (err < 0)
1938 return err;
1939 }
1940
1941
1942 for_each_dpcm_be(fe, stream, dpcm) {
1943 struct snd_soc_pcm_runtime *be = dpcm->be;
1944 struct snd_pcm_substream *be_substream =
1945 snd_soc_dpcm_get_substream(be, stream);
1946 struct snd_soc_pcm_runtime *rtd;
1947 struct snd_soc_dai *codec_dai;
1948 int i;
1949
1950
1951 if (!be_substream)
1952 continue;
1953
1954 rtd = be_substream->private_data;
1955 if (rtd->dai_link->be_hw_params_fixup)
1956 continue;
1957
1958 if (soc_pcm_has_symmetry(be_substream))
1959 be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1960
1961
1962 if (rtd->cpu_dai->active) {
1963 err = soc_pcm_apply_symmetry(fe_substream,
1964 rtd->cpu_dai);
1965 if (err < 0)
1966 return err;
1967 }
1968
1969 for_each_rtd_codec_dai(rtd, i, codec_dai) {
1970 if (codec_dai->active) {
1971 err = soc_pcm_apply_symmetry(fe_substream,
1972 codec_dai);
1973 if (err < 0)
1974 return err;
1975 }
1976 }
1977 }
1978
1979 return 0;
1980}
1981
1982static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
1983{
1984 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
1985 struct snd_pcm_runtime *runtime = fe_substream->runtime;
1986 int stream = fe_substream->stream, ret = 0;
1987
1988 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
1989
1990 ret = dpcm_be_dai_startup(fe, fe_substream->stream);
1991 if (ret < 0) {
1992 dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
1993 goto be_err;
1994 }
1995
1996 dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
1997
1998
1999 ret = soc_pcm_open(fe_substream);
2000 if (ret < 0) {
2001 dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
2002 goto unwind;
2003 }
2004
2005 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
2006
2007 dpcm_set_fe_runtime(fe_substream);
2008 snd_pcm_limit_hw_rates(runtime);
2009
2010 ret = dpcm_apply_symmetry(fe_substream, stream);
2011 if (ret < 0) {
2012 dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
2013 ret);
2014 goto unwind;
2015 }
2016
2017 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2018 return 0;
2019
2020unwind:
2021 dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
2022be_err:
2023 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2024 return ret;
2025}
2026
2027int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2028{
2029 struct snd_soc_dpcm *dpcm;
2030
2031
2032 for_each_dpcm_be(fe, stream, dpcm) {
2033
2034 struct snd_soc_pcm_runtime *be = dpcm->be;
2035 struct snd_pcm_substream *be_substream =
2036 snd_soc_dpcm_get_substream(be, stream);
2037
2038
2039 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2040 continue;
2041
2042 if (be->dpcm[stream].users == 0)
2043 dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
2044 stream ? "capture" : "playback",
2045 be->dpcm[stream].state);
2046
2047 if (--be->dpcm[stream].users != 0)
2048 continue;
2049
2050 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2051 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
2052 soc_pcm_hw_free(be_substream);
2053 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2054 }
2055
2056 dev_dbg(be->dev, "ASoC: close BE %s\n",
2057 be->dai_link->name);
2058
2059 soc_pcm_close(be_substream);
2060 be_substream->runtime = NULL;
2061
2062 be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2063 }
2064 return 0;
2065}
2066
2067static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
2068{
2069 struct snd_soc_pcm_runtime *fe = substream->private_data;
2070 int stream = substream->stream;
2071
2072 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2073
2074
2075 dpcm_be_dai_shutdown(fe, substream->stream);
2076
2077 dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
2078
2079
2080 soc_pcm_close(substream);
2081
2082
2083 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
2084
2085 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
2086 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2087 return 0;
2088}
2089
2090int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
2091{
2092 struct snd_soc_dpcm *dpcm;
2093
2094
2095
2096 for_each_dpcm_be(fe, stream, dpcm) {
2097
2098 struct snd_soc_pcm_runtime *be = dpcm->be;
2099 struct snd_pcm_substream *be_substream =
2100 snd_soc_dpcm_get_substream(be, stream);
2101
2102
2103 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2104 continue;
2105
2106
2107 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2108 continue;
2109
2110
2111 if (be->dpcm[stream].users > 1)
2112 continue;
2113
2114 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2115 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2116 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2117 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
2118 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2119 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2120 continue;
2121
2122 dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
2123 be->dai_link->name);
2124
2125 soc_pcm_hw_free(be_substream);
2126
2127 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2128 }
2129
2130 return 0;
2131}
2132
2133static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
2134{
2135 struct snd_soc_pcm_runtime *fe = substream->private_data;
2136 int err, stream = substream->stream;
2137
2138 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2139 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2140
2141 dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
2142
2143
2144 err = soc_pcm_hw_free(substream);
2145 if (err < 0)
2146 dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
2147 fe->dai_link->name);
2148
2149
2150
2151 err = dpcm_be_dai_hw_free(fe, stream);
2152
2153 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
2154 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2155
2156 mutex_unlock(&fe->card->mutex);
2157 return 0;
2158}
2159
2160int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
2161{
2162 struct snd_soc_dpcm *dpcm;
2163 int ret;
2164
2165 for_each_dpcm_be(fe, stream, dpcm) {
2166
2167 struct snd_soc_pcm_runtime *be = dpcm->be;
2168 struct snd_pcm_substream *be_substream =
2169 snd_soc_dpcm_get_substream(be, stream);
2170
2171
2172 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2173 continue;
2174
2175
2176 memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
2177 sizeof(struct snd_pcm_hw_params));
2178
2179
2180 if (be->dai_link->be_hw_params_fixup) {
2181 ret = be->dai_link->be_hw_params_fixup(be,
2182 &dpcm->hw_params);
2183 if (ret < 0) {
2184 dev_err(be->dev,
2185 "ASoC: hw_params BE fixup failed %d\n",
2186 ret);
2187 goto unwind;
2188 }
2189 }
2190
2191
2192 memcpy(&be->dpcm[stream].hw_params, &dpcm->hw_params,
2193 sizeof(struct snd_pcm_hw_params));
2194
2195
2196 if (!snd_soc_dpcm_can_be_params(fe, be, stream))
2197 continue;
2198
2199 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2200 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2201 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
2202 continue;
2203
2204 dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
2205 be->dai_link->name);
2206
2207 ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
2208 if (ret < 0) {
2209 dev_err(dpcm->be->dev,
2210 "ASoC: hw_params BE failed %d\n", ret);
2211 goto unwind;
2212 }
2213
2214 be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2215 }
2216 return 0;
2217
2218unwind:
2219
2220 for_each_dpcm_be_rollback(fe, stream, dpcm) {
2221 struct snd_soc_pcm_runtime *be = dpcm->be;
2222 struct snd_pcm_substream *be_substream =
2223 snd_soc_dpcm_get_substream(be, stream);
2224
2225 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2226 continue;
2227
2228
2229 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2230 continue;
2231
2232 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
2233 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2234 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
2235 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2236 continue;
2237
2238 soc_pcm_hw_free(be_substream);
2239 }
2240
2241 return ret;
2242}
2243
2244static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
2245 struct snd_pcm_hw_params *params)
2246{
2247 struct snd_soc_pcm_runtime *fe = substream->private_data;
2248 int ret, stream = substream->stream;
2249
2250 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2251 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2252
2253 memcpy(&fe->dpcm[substream->stream].hw_params, params,
2254 sizeof(struct snd_pcm_hw_params));
2255 ret = dpcm_be_dai_hw_params(fe, substream->stream);
2256 if (ret < 0) {
2257 dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
2258 goto out;
2259 }
2260
2261 dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
2262 fe->dai_link->name, params_rate(params),
2263 params_channels(params), params_format(params));
2264
2265
2266 ret = soc_pcm_hw_params(substream, params);
2267 if (ret < 0) {
2268 dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
2269 dpcm_be_dai_hw_free(fe, stream);
2270 } else
2271 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
2272
2273out:
2274 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2275 mutex_unlock(&fe->card->mutex);
2276 return ret;
2277}
2278
2279static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
2280 struct snd_pcm_substream *substream, int cmd)
2281{
2282 int ret;
2283
2284 dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
2285 dpcm->be->dai_link->name, cmd);
2286
2287 ret = soc_pcm_trigger(substream, cmd);
2288 if (ret < 0)
2289 dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
2290
2291 return ret;
2292}
2293
2294int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
2295 int cmd)
2296{
2297 struct snd_soc_dpcm *dpcm;
2298 int ret = 0;
2299
2300 for_each_dpcm_be(fe, stream, dpcm) {
2301
2302 struct snd_soc_pcm_runtime *be = dpcm->be;
2303 struct snd_pcm_substream *be_substream =
2304 snd_soc_dpcm_get_substream(be, stream);
2305
2306
2307 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2308 continue;
2309
2310 switch (cmd) {
2311 case SNDRV_PCM_TRIGGER_START:
2312 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
2313 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
2314 continue;
2315
2316 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2317 if (ret)
2318 return ret;
2319
2320 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2321 break;
2322 case SNDRV_PCM_TRIGGER_RESUME:
2323 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
2324 continue;
2325
2326 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2327 if (ret)
2328 return ret;
2329
2330 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2331 break;
2332 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2333 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2334 continue;
2335
2336 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2337 if (ret)
2338 return ret;
2339
2340 be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2341 break;
2342 case SNDRV_PCM_TRIGGER_STOP:
2343 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2344 continue;
2345
2346 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2347 continue;
2348
2349 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2350 if (ret)
2351 return ret;
2352
2353 be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2354 break;
2355 case SNDRV_PCM_TRIGGER_SUSPEND:
2356 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2357 continue;
2358
2359 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2360 continue;
2361
2362 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2363 if (ret)
2364 return ret;
2365
2366 be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
2367 break;
2368 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2369 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2370 continue;
2371
2372 if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
2373 continue;
2374
2375 ret = dpcm_do_trigger(dpcm, be_substream, cmd);
2376 if (ret)
2377 return ret;
2378
2379 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2380 break;
2381 }
2382 }
2383
2384 return ret;
2385}
2386EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
2387
2388static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
2389{
2390 struct snd_soc_pcm_runtime *fe = substream->private_data;
2391 int stream = substream->stream, ret;
2392 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2393
2394 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
2395
2396 switch (trigger) {
2397 case SND_SOC_DPCM_TRIGGER_PRE:
2398
2399
2400 dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
2401 fe->dai_link->name, cmd);
2402
2403 ret = soc_pcm_trigger(substream, cmd);
2404 if (ret < 0) {
2405 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2406 goto out;
2407 }
2408
2409 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2410 break;
2411 case SND_SOC_DPCM_TRIGGER_POST:
2412
2413
2414 ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
2415 if (ret < 0) {
2416 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2417 goto out;
2418 }
2419
2420 dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
2421 fe->dai_link->name, cmd);
2422
2423 ret = soc_pcm_trigger(substream, cmd);
2424 break;
2425 case SND_SOC_DPCM_TRIGGER_BESPOKE:
2426
2427
2428 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
2429 fe->dai_link->name, cmd);
2430
2431 ret = soc_pcm_bespoke_trigger(substream, cmd);
2432 if (ret < 0) {
2433 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2434 goto out;
2435 }
2436 break;
2437 default:
2438 dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
2439 fe->dai_link->name);
2440 ret = -EINVAL;
2441 goto out;
2442 }
2443
2444 switch (cmd) {
2445 case SNDRV_PCM_TRIGGER_START:
2446 case SNDRV_PCM_TRIGGER_RESUME:
2447 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2448 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
2449 break;
2450 case SNDRV_PCM_TRIGGER_STOP:
2451 case SNDRV_PCM_TRIGGER_SUSPEND:
2452 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
2453 break;
2454 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2455 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
2456 break;
2457 }
2458
2459out:
2460 fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
2461 return ret;
2462}
2463
2464static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
2465{
2466 struct snd_soc_pcm_runtime *fe = substream->private_data;
2467 int stream = substream->stream;
2468
2469
2470
2471
2472 if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
2473 fe->dpcm[stream].trigger_pending = cmd + 1;
2474 return 0;
2475 }
2476
2477
2478 return dpcm_fe_dai_do_trigger(substream, cmd);
2479}
2480
2481int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
2482{
2483 struct snd_soc_dpcm *dpcm;
2484 int ret = 0;
2485
2486 for_each_dpcm_be(fe, stream, dpcm) {
2487
2488 struct snd_soc_pcm_runtime *be = dpcm->be;
2489 struct snd_pcm_substream *be_substream =
2490 snd_soc_dpcm_get_substream(be, stream);
2491
2492
2493 if (!snd_soc_dpcm_be_can_update(fe, be, stream))
2494 continue;
2495
2496 if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
2497 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
2498 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
2499 (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
2500 continue;
2501
2502 dev_dbg(be->dev, "ASoC: prepare BE %s\n",
2503 be->dai_link->name);
2504
2505 ret = soc_pcm_prepare(be_substream);
2506 if (ret < 0) {
2507 dev_err(be->dev, "ASoC: backend prepare failed %d\n",
2508 ret);
2509 break;
2510 }
2511
2512 be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2513 }
2514 return ret;
2515}
2516
2517static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
2518{
2519 struct snd_soc_pcm_runtime *fe = substream->private_data;
2520 int stream = substream->stream, ret = 0;
2521
2522 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2523
2524 dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
2525
2526 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
2527
2528
2529 if (list_empty(&fe->dpcm[stream].be_clients)) {
2530 dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
2531 fe->dai_link->name);
2532 ret = -EINVAL;
2533 goto out;
2534 }
2535
2536 ret = dpcm_be_dai_prepare(fe, substream->stream);
2537 if (ret < 0)
2538 goto out;
2539
2540
2541 ret = soc_pcm_prepare(substream);
2542 if (ret < 0) {
2543 dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
2544 fe->dai_link->name);
2545 goto out;
2546 }
2547
2548
2549 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
2550 fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
2551
2552out:
2553 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2554 mutex_unlock(&fe->card->mutex);
2555
2556 return ret;
2557}
2558
2559static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
2560 unsigned int cmd, void *arg)
2561{
2562 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2563 struct snd_soc_component *component;
2564 struct snd_soc_rtdcom_list *rtdcom;
2565
2566 for_each_rtdcom(rtd, rtdcom) {
2567 component = rtdcom->component;
2568
2569 if (!component->driver->ops ||
2570 !component->driver->ops->ioctl)
2571 continue;
2572
2573
2574 return component->driver->ops->ioctl(substream, cmd, arg);
2575 }
2576
2577 return snd_pcm_lib_ioctl(substream, cmd, arg);
2578}
2579
2580static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
2581{
2582 struct snd_pcm_substream *substream =
2583 snd_soc_dpcm_get_substream(fe, stream);
2584 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2585 int err;
2586
2587 dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
2588 stream ? "capture" : "playback", fe->dai_link->name);
2589
2590 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2591
2592 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
2593 fe->dai_link->name);
2594
2595 err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
2596 if (err < 0)
2597 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2598 } else {
2599 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
2600 fe->dai_link->name);
2601
2602 err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
2603 if (err < 0)
2604 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
2605 }
2606
2607 err = dpcm_be_dai_hw_free(fe, stream);
2608 if (err < 0)
2609 dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
2610
2611 err = dpcm_be_dai_shutdown(fe, stream);
2612 if (err < 0)
2613 dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
2614
2615
2616 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2617
2618 return 0;
2619}
2620
2621static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
2622{
2623 struct snd_pcm_substream *substream =
2624 snd_soc_dpcm_get_substream(fe, stream);
2625 struct snd_soc_dpcm *dpcm;
2626 enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
2627 int ret;
2628 unsigned long flags;
2629
2630 dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
2631 stream ? "capture" : "playback", fe->dai_link->name);
2632
2633
2634 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
2635 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
2636 return -EINVAL;
2637
2638
2639 ret = dpcm_be_dai_startup(fe, stream);
2640 if (ret < 0)
2641 goto disconnect;
2642
2643
2644 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
2645 return 0;
2646
2647 ret = dpcm_be_dai_hw_params(fe, stream);
2648 if (ret < 0)
2649 goto close;
2650
2651
2652 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
2653 return 0;
2654
2655
2656 ret = dpcm_be_dai_prepare(fe, stream);
2657 if (ret < 0)
2658 goto hw_free;
2659
2660
2661 dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
2662
2663
2664 if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
2665 fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
2666 return 0;
2667
2668 if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
2669
2670 dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
2671 fe->dai_link->name);
2672
2673 ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
2674 if (ret < 0) {
2675 dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
2676 goto hw_free;
2677 }
2678 } else {
2679 dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
2680 fe->dai_link->name);
2681
2682 ret = dpcm_be_dai_trigger(fe, stream,
2683 SNDRV_PCM_TRIGGER_START);
2684 if (ret < 0) {
2685 dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
2686 goto hw_free;
2687 }
2688 }
2689
2690 return 0;
2691
2692hw_free:
2693 dpcm_be_dai_hw_free(fe, stream);
2694close:
2695 dpcm_be_dai_shutdown(fe, stream);
2696disconnect:
2697
2698 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
2699 for_each_dpcm_be(fe, stream, dpcm) {
2700 struct snd_soc_pcm_runtime *be = dpcm->be;
2701 if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
2702 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2703 }
2704 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
2705
2706 return ret;
2707}
2708
2709static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
2710{
2711 int ret;
2712
2713 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2714 ret = dpcm_run_update_startup(fe, stream);
2715 if (ret < 0)
2716 dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
2717 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2718
2719 return ret;
2720}
2721
2722static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
2723{
2724 int ret;
2725
2726 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
2727 ret = dpcm_run_update_shutdown(fe, stream);
2728 if (ret < 0)
2729 dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
2730 dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
2731
2732 return ret;
2733}
2734
2735static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
2736{
2737 struct snd_soc_dapm_widget_list *list;
2738 int count, paths;
2739
2740 if (!fe->dai_link->dynamic)
2741 return 0;
2742
2743
2744 if (!fe->cpu_dai->active)
2745 return 0;
2746
2747
2748 dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
2749 new ? "new" : "old", fe->dai_link->name);
2750
2751
2752 if (!fe->cpu_dai->driver->playback.channels_min ||
2753 !fe->codec_dai->driver->playback.channels_min)
2754 goto capture;
2755
2756
2757 if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
2758 goto capture;
2759
2760 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
2761 if (paths < 0) {
2762 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2763 fe->dai_link->name, "playback");
2764 return paths;
2765 }
2766
2767
2768 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
2769 if (count) {
2770 if (new)
2771 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2772 else
2773 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
2774
2775 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
2776 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
2777 }
2778
2779 dpcm_path_put(&list);
2780
2781capture:
2782
2783 if (!fe->cpu_dai->driver->capture.channels_min ||
2784 !fe->codec_dai->driver->capture.channels_min)
2785 return 0;
2786
2787
2788 if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
2789 return 0;
2790
2791 paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
2792 if (paths < 0) {
2793 dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
2794 fe->dai_link->name, "capture");
2795 return paths;
2796 }
2797
2798
2799 count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
2800 if (count) {
2801 if (new)
2802 dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2803 else
2804 dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
2805
2806 dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
2807 dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
2808 }
2809
2810 dpcm_path_put(&list);
2811
2812 return 0;
2813}
2814
2815
2816
2817
2818int soc_dpcm_runtime_update(struct snd_soc_card *card)
2819{
2820 struct snd_soc_pcm_runtime *fe;
2821 int ret = 0;
2822
2823 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2824
2825 for_each_card_rtds(card, fe) {
2826 ret = soc_dpcm_fe_runtime_update(fe, 0);
2827 if (ret)
2828 goto out;
2829 }
2830
2831
2832 for_each_card_rtds(card, fe) {
2833 ret = soc_dpcm_fe_runtime_update(fe, 1);
2834 if (ret)
2835 goto out;
2836 }
2837
2838out:
2839 mutex_unlock(&card->mutex);
2840 return ret;
2841}
2842int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
2843{
2844 struct snd_soc_dpcm *dpcm;
2845 struct snd_soc_dai *dai;
2846
2847 for_each_dpcm_be(fe, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
2848
2849 struct snd_soc_pcm_runtime *be = dpcm->be;
2850 int i;
2851
2852 if (be->dai_link->ignore_suspend)
2853 continue;
2854
2855 for_each_rtd_codec_dai(be, i, dai) {
2856 struct snd_soc_dai_driver *drv = dai->driver;
2857
2858 dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
2859 be->dai_link->name);
2860
2861 if (drv->ops && drv->ops->digital_mute &&
2862 dai->playback_active)
2863 drv->ops->digital_mute(dai, mute);
2864 }
2865 }
2866
2867 return 0;
2868}
2869
2870static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
2871{
2872 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2873 struct snd_soc_dpcm *dpcm;
2874 struct snd_soc_dapm_widget_list *list;
2875 int ret;
2876 int stream = fe_substream->stream;
2877
2878 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2879 fe->dpcm[stream].runtime = fe_substream->runtime;
2880
2881 ret = dpcm_path_get(fe, stream, &list);
2882 if (ret < 0) {
2883 mutex_unlock(&fe->card->mutex);
2884 return ret;
2885 } else if (ret == 0) {
2886 dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
2887 fe->dai_link->name, stream ? "capture" : "playback");
2888 }
2889
2890
2891 dpcm_process_paths(fe, stream, &list, 1);
2892
2893 ret = dpcm_fe_dai_startup(fe_substream);
2894 if (ret < 0) {
2895
2896 for_each_dpcm_be(fe, stream, dpcm)
2897 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2898
2899 dpcm_be_disconnect(fe, stream);
2900 fe->dpcm[stream].runtime = NULL;
2901 }
2902
2903 dpcm_clear_pending_state(fe, stream);
2904 dpcm_path_put(&list);
2905 mutex_unlock(&fe->card->mutex);
2906 return ret;
2907}
2908
2909static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
2910{
2911 struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
2912 struct snd_soc_dpcm *dpcm;
2913 int stream = fe_substream->stream, ret;
2914
2915 mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
2916 ret = dpcm_fe_dai_shutdown(fe_substream);
2917
2918
2919 for_each_dpcm_be(fe, stream, dpcm)
2920 dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
2921
2922 dpcm_be_disconnect(fe, stream);
2923
2924 fe->dpcm[stream].runtime = NULL;
2925 mutex_unlock(&fe->card->mutex);
2926 return ret;
2927}
2928
2929static void soc_pcm_private_free(struct snd_pcm *pcm)
2930{
2931 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
2932 struct snd_soc_rtdcom_list *rtdcom;
2933 struct snd_soc_component *component;
2934
2935
2936 flush_delayed_work(&rtd->delayed_work);
2937 for_each_rtdcom(rtd, rtdcom) {
2938 component = rtdcom->component;
2939
2940 if (component->driver->pcm_free)
2941 component->driver->pcm_free(pcm);
2942 }
2943}
2944
2945static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
2946{
2947 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2948 struct snd_soc_rtdcom_list *rtdcom;
2949 struct snd_soc_component *component;
2950
2951 for_each_rtdcom(rtd, rtdcom) {
2952 component = rtdcom->component;
2953
2954 if (!component->driver->ops ||
2955 !component->driver->ops->ack)
2956 continue;
2957
2958
2959 return component->driver->ops->ack(substream);
2960 }
2961
2962 return -EINVAL;
2963}
2964
2965static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
2966 unsigned long pos, void __user *buf,
2967 unsigned long bytes)
2968{
2969 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2970 struct snd_soc_rtdcom_list *rtdcom;
2971 struct snd_soc_component *component;
2972
2973 for_each_rtdcom(rtd, rtdcom) {
2974 component = rtdcom->component;
2975
2976 if (!component->driver->ops ||
2977 !component->driver->ops->copy_user)
2978 continue;
2979
2980
2981 return component->driver->ops->copy_user(substream, channel,
2982 pos, buf, bytes);
2983 }
2984
2985 return -EINVAL;
2986}
2987
2988static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
2989 unsigned long pos, void *buf, unsigned long bytes)
2990{
2991 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2992 struct snd_soc_rtdcom_list *rtdcom;
2993 struct snd_soc_component *component;
2994
2995 for_each_rtdcom(rtd, rtdcom) {
2996 component = rtdcom->component;
2997
2998 if (!component->driver->ops ||
2999 !component->driver->ops->copy_kernel)
3000 continue;
3001
3002
3003 return component->driver->ops->copy_kernel(substream, channel,
3004 pos, buf, bytes);
3005 }
3006
3007 return -EINVAL;
3008}
3009
3010static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
3011 unsigned long pos, unsigned long bytes)
3012{
3013 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3014 struct snd_soc_rtdcom_list *rtdcom;
3015 struct snd_soc_component *component;
3016
3017 for_each_rtdcom(rtd, rtdcom) {
3018 component = rtdcom->component;
3019
3020 if (!component->driver->ops ||
3021 !component->driver->ops->fill_silence)
3022 continue;
3023
3024
3025 return component->driver->ops->fill_silence(substream, channel,
3026 pos, bytes);
3027 }
3028
3029 return -EINVAL;
3030}
3031
3032static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
3033 unsigned long offset)
3034{
3035 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3036 struct snd_soc_rtdcom_list *rtdcom;
3037 struct snd_soc_component *component;
3038 struct page *page;
3039
3040 for_each_rtdcom(rtd, rtdcom) {
3041 component = rtdcom->component;
3042
3043 if (!component->driver->ops ||
3044 !component->driver->ops->page)
3045 continue;
3046
3047
3048 page = component->driver->ops->page(substream, offset);
3049 if (page)
3050 return page;
3051 }
3052
3053 return NULL;
3054}
3055
3056static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
3057 struct vm_area_struct *vma)
3058{
3059 struct snd_soc_pcm_runtime *rtd = substream->private_data;
3060 struct snd_soc_rtdcom_list *rtdcom;
3061 struct snd_soc_component *component;
3062
3063 for_each_rtdcom(rtd, rtdcom) {
3064 component = rtdcom->component;
3065
3066 if (!component->driver->ops ||
3067 !component->driver->ops->mmap)
3068 continue;
3069
3070
3071 return component->driver->ops->mmap(substream, vma);
3072 }
3073
3074 return -EINVAL;
3075}
3076
3077
3078int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
3079{
3080 struct snd_soc_dai *codec_dai;
3081 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3082 struct snd_soc_component *component;
3083 struct snd_soc_rtdcom_list *rtdcom;
3084 struct snd_pcm *pcm;
3085 char new_name[64];
3086 int ret = 0, playback = 0, capture = 0;
3087 int i;
3088
3089 if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
3090 playback = rtd->dai_link->dpcm_playback;
3091 capture = rtd->dai_link->dpcm_capture;
3092 } else {
3093 for_each_rtd_codec_dai(rtd, i, codec_dai) {
3094 if (codec_dai->driver->playback.channels_min)
3095 playback = 1;
3096 if (codec_dai->driver->capture.channels_min)
3097 capture = 1;
3098 }
3099
3100 capture = capture && cpu_dai->driver->capture.channels_min;
3101 playback = playback && cpu_dai->driver->playback.channels_min;
3102 }
3103
3104 if (rtd->dai_link->playback_only) {
3105 playback = 1;
3106 capture = 0;
3107 }
3108
3109 if (rtd->dai_link->capture_only) {
3110 playback = 0;
3111 capture = 1;
3112 }
3113
3114
3115 if (rtd->dai_link->no_pcm) {
3116 snprintf(new_name, sizeof(new_name), "(%s)",
3117 rtd->dai_link->stream_name);
3118
3119 ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
3120 playback, capture, &pcm);
3121 } else {
3122 if (rtd->dai_link->dynamic)
3123 snprintf(new_name, sizeof(new_name), "%s (*)",
3124 rtd->dai_link->stream_name);
3125 else
3126 snprintf(new_name, sizeof(new_name), "%s %s-%d",
3127 rtd->dai_link->stream_name,
3128 (rtd->num_codecs > 1) ?
3129 "multicodec" : rtd->codec_dai->name, num);
3130
3131 ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
3132 capture, &pcm);
3133 }
3134 if (ret < 0) {
3135 dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
3136 rtd->dai_link->name);
3137 return ret;
3138 }
3139 dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
3140
3141
3142 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
3143
3144 pcm->nonatomic = rtd->dai_link->nonatomic;
3145 rtd->pcm = pcm;
3146 pcm->private_data = rtd;
3147
3148 if (rtd->dai_link->no_pcm) {
3149 if (playback)
3150 pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
3151 if (capture)
3152 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
3153 goto out;
3154 }
3155
3156
3157 if (rtd->dai_link->dynamic) {
3158 rtd->ops.open = dpcm_fe_dai_open;
3159 rtd->ops.hw_params = dpcm_fe_dai_hw_params;
3160 rtd->ops.prepare = dpcm_fe_dai_prepare;
3161 rtd->ops.trigger = dpcm_fe_dai_trigger;
3162 rtd->ops.hw_free = dpcm_fe_dai_hw_free;
3163 rtd->ops.close = dpcm_fe_dai_close;
3164 rtd->ops.pointer = soc_pcm_pointer;
3165 rtd->ops.ioctl = soc_pcm_ioctl;
3166 } else {
3167 rtd->ops.open = soc_pcm_open;
3168 rtd->ops.hw_params = soc_pcm_hw_params;
3169 rtd->ops.prepare = soc_pcm_prepare;
3170 rtd->ops.trigger = soc_pcm_trigger;
3171 rtd->ops.hw_free = soc_pcm_hw_free;
3172 rtd->ops.close = soc_pcm_close;
3173 rtd->ops.pointer = soc_pcm_pointer;
3174 rtd->ops.ioctl = soc_pcm_ioctl;
3175 }
3176
3177 for_each_rtdcom(rtd, rtdcom) {
3178 const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
3179
3180 if (!ops)
3181 continue;
3182
3183 if (ops->ack)
3184 rtd->ops.ack = soc_rtdcom_ack;
3185 if (ops->copy_user)
3186 rtd->ops.copy_user = soc_rtdcom_copy_user;
3187 if (ops->copy_kernel)
3188 rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
3189 if (ops->fill_silence)
3190 rtd->ops.fill_silence = soc_rtdcom_fill_silence;
3191 if (ops->page)
3192 rtd->ops.page = soc_rtdcom_page;
3193 if (ops->mmap)
3194 rtd->ops.mmap = soc_rtdcom_mmap;
3195 }
3196
3197 if (playback)
3198 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
3199
3200 if (capture)
3201 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
3202
3203 for_each_rtdcom(rtd, rtdcom) {
3204 component = rtdcom->component;
3205
3206 if (!component->driver->pcm_new)
3207 continue;
3208
3209 ret = component->driver->pcm_new(rtd);
3210 if (ret < 0) {
3211 dev_err(component->dev,
3212 "ASoC: pcm constructor failed: %d\n",
3213 ret);
3214 return ret;
3215 }
3216 }
3217
3218 pcm->private_free = soc_pcm_private_free;
3219 pcm->no_device_suspend = true;
3220out:
3221 dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
3222 (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
3223 cpu_dai->name);
3224 return ret;
3225}
3226
3227
3228int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
3229{
3230 if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
3231 return 1;
3232 return 0;
3233}
3234EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
3235
3236
3237int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
3238 struct snd_soc_pcm_runtime *be, int stream)
3239{
3240 if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
3241 ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
3242 be->dpcm[stream].runtime_update))
3243 return 1;
3244 return 0;
3245}
3246EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
3247
3248
3249struct snd_pcm_substream *
3250 snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
3251{
3252 return be->pcm->streams[stream].substream;
3253}
3254EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
3255
3256
3257enum snd_soc_dpcm_state
3258 snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
3259{
3260 return be->dpcm[stream].state;
3261}
3262EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
3263
3264
3265void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
3266 int stream, enum snd_soc_dpcm_state state)
3267{
3268 be->dpcm[stream].state = state;
3269}
3270EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
3271
3272
3273
3274
3275
3276int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
3277 struct snd_soc_pcm_runtime *be, int stream)
3278{
3279 struct snd_soc_dpcm *dpcm;
3280 int state;
3281 int ret = 1;
3282 unsigned long flags;
3283
3284 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3285 for_each_dpcm_fe(be, stream, dpcm) {
3286
3287 if (dpcm->fe == fe)
3288 continue;
3289
3290 state = dpcm->fe->dpcm[stream].state;
3291 if (state == SND_SOC_DPCM_STATE_START ||
3292 state == SND_SOC_DPCM_STATE_PAUSED ||
3293 state == SND_SOC_DPCM_STATE_SUSPEND) {
3294 ret = 0;
3295 break;
3296 }
3297 }
3298 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3299
3300
3301 return ret;
3302}
3303EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
3304
3305
3306
3307
3308
3309int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
3310 struct snd_soc_pcm_runtime *be, int stream)
3311{
3312 struct snd_soc_dpcm *dpcm;
3313 int state;
3314 int ret = 1;
3315 unsigned long flags;
3316
3317 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3318 for_each_dpcm_fe(be, stream, dpcm) {
3319
3320 if (dpcm->fe == fe)
3321 continue;
3322
3323 state = dpcm->fe->dpcm[stream].state;
3324 if (state == SND_SOC_DPCM_STATE_START ||
3325 state == SND_SOC_DPCM_STATE_PAUSED ||
3326 state == SND_SOC_DPCM_STATE_SUSPEND ||
3327 state == SND_SOC_DPCM_STATE_PREPARE) {
3328 ret = 0;
3329 break;
3330 }
3331 }
3332 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3333
3334
3335 return ret;
3336}
3337EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
3338
3339#ifdef CONFIG_DEBUG_FS
3340static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
3341{
3342 switch (state) {
3343 case SND_SOC_DPCM_STATE_NEW:
3344 return "new";
3345 case SND_SOC_DPCM_STATE_OPEN:
3346 return "open";
3347 case SND_SOC_DPCM_STATE_HW_PARAMS:
3348 return "hw_params";
3349 case SND_SOC_DPCM_STATE_PREPARE:
3350 return "prepare";
3351 case SND_SOC_DPCM_STATE_START:
3352 return "start";
3353 case SND_SOC_DPCM_STATE_STOP:
3354 return "stop";
3355 case SND_SOC_DPCM_STATE_SUSPEND:
3356 return "suspend";
3357 case SND_SOC_DPCM_STATE_PAUSED:
3358 return "paused";
3359 case SND_SOC_DPCM_STATE_HW_FREE:
3360 return "hw_free";
3361 case SND_SOC_DPCM_STATE_CLOSE:
3362 return "close";
3363 }
3364
3365 return "unknown";
3366}
3367
3368static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
3369 int stream, char *buf, size_t size)
3370{
3371 struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
3372 struct snd_soc_dpcm *dpcm;
3373 ssize_t offset = 0;
3374 unsigned long flags;
3375
3376
3377 offset += snprintf(buf + offset, size - offset,
3378 "[%s - %s]\n", fe->dai_link->name,
3379 stream ? "Capture" : "Playback");
3380
3381 offset += snprintf(buf + offset, size - offset, "State: %s\n",
3382 dpcm_state_string(fe->dpcm[stream].state));
3383
3384 if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3385 (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3386 offset += snprintf(buf + offset, size - offset,
3387 "Hardware Params: "
3388 "Format = %s, Channels = %d, Rate = %d\n",
3389 snd_pcm_format_name(params_format(params)),
3390 params_channels(params),
3391 params_rate(params));
3392
3393
3394 offset += snprintf(buf + offset, size - offset, "Backends:\n");
3395
3396 if (list_empty(&fe->dpcm[stream].be_clients)) {
3397 offset += snprintf(buf + offset, size - offset,
3398 " No active DSP links\n");
3399 goto out;
3400 }
3401
3402 spin_lock_irqsave(&fe->card->dpcm_lock, flags);
3403 for_each_dpcm_be(fe, stream, dpcm) {
3404 struct snd_soc_pcm_runtime *be = dpcm->be;
3405 params = &dpcm->hw_params;
3406
3407 offset += snprintf(buf + offset, size - offset,
3408 "- %s\n", be->dai_link->name);
3409
3410 offset += snprintf(buf + offset, size - offset,
3411 " State: %s\n",
3412 dpcm_state_string(be->dpcm[stream].state));
3413
3414 if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
3415 (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
3416 offset += snprintf(buf + offset, size - offset,
3417 " Hardware Params: "
3418 "Format = %s, Channels = %d, Rate = %d\n",
3419 snd_pcm_format_name(params_format(params)),
3420 params_channels(params),
3421 params_rate(params));
3422 }
3423 spin_unlock_irqrestore(&fe->card->dpcm_lock, flags);
3424out:
3425 return offset;
3426}
3427
3428static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
3429 size_t count, loff_t *ppos)
3430{
3431 struct snd_soc_pcm_runtime *fe = file->private_data;
3432 ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
3433 char *buf;
3434
3435 buf = kmalloc(out_count, GFP_KERNEL);
3436 if (!buf)
3437 return -ENOMEM;
3438
3439 if (fe->cpu_dai->driver->playback.channels_min)
3440 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
3441 buf + offset, out_count - offset);
3442
3443 if (fe->cpu_dai->driver->capture.channels_min)
3444 offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
3445 buf + offset, out_count - offset);
3446
3447 ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
3448
3449 kfree(buf);
3450 return ret;
3451}
3452
3453static const struct file_operations dpcm_state_fops = {
3454 .open = simple_open,
3455 .read = dpcm_state_read_file,
3456 .llseek = default_llseek,
3457};
3458
3459void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
3460{
3461 if (!rtd->dai_link)
3462 return;
3463
3464 if (!rtd->card->debugfs_card_root)
3465 return;
3466
3467 rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
3468 rtd->card->debugfs_card_root);
3469 if (!rtd->debugfs_dpcm_root) {
3470 dev_dbg(rtd->dev,
3471 "ASoC: Failed to create dpcm debugfs directory %s\n",
3472 rtd->dai_link->name);
3473 return;
3474 }
3475
3476 debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
3477 rtd, &dpcm_state_fops);
3478}
3479#endif
3480