This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
forked from microsoft/WSL2-Linux-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsoc-pcm.c
3041 lines (2472 loc) · 79.6 KB
/
soc-pcm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
//
// soc-pcm.c -- ALSA SoC PCM
//
// Copyright 2005 Wolfson Microelectronics PLC.
// Copyright 2005 Openedhand Ltd.
// Copyright (C) 2010 Slimlogic Ltd.
// Copyright (C) 2010 Texas Instruments Inc.
//
// Authors: Liam Girdwood <lrg@ti.com>
// Mark Brown <broonie@opensource.wolfsonmicro.com>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/export.h>
#include <linux/debugfs.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dpcm.h>
#include <sound/soc-link.h>
#include <sound/initval.h>
#define soc_pcm_ret(rtd, ret) _soc_pcm_ret(rtd, __func__, ret)
static inline int _soc_pcm_ret(struct snd_soc_pcm_runtime *rtd,
const char *func, int ret)
{
/* Positive, Zero values are not errors */
if (ret >= 0)
return ret;
/* Negative values might be errors */
switch (ret) {
case -EPROBE_DEFER:
case -ENOTSUPP:
break;
default:
dev_err(rtd->dev,
"ASoC: error at %s on %s: %d\n",
func, rtd->dai_link->name, ret);
}
return ret;
}
static inline void snd_soc_dpcm_mutex_lock(struct snd_soc_pcm_runtime *rtd)
{
mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
}
static inline void snd_soc_dpcm_mutex_unlock(struct snd_soc_pcm_runtime *rtd)
{
mutex_unlock(&rtd->card->pcm_mutex);
}
#define snd_soc_dpcm_mutex_assert_held(rtd) \
lockdep_assert_held(&(rtd)->card->pcm_mutex)
static inline void snd_soc_dpcm_stream_lock_irq(struct snd_soc_pcm_runtime *rtd,
int stream)
{
snd_pcm_stream_lock_irq(snd_soc_dpcm_get_substream(rtd, stream));
}
#define snd_soc_dpcm_stream_lock_irqsave_nested(rtd, stream, flags) \
snd_pcm_stream_lock_irqsave_nested(snd_soc_dpcm_get_substream(rtd, stream), flags)
static inline void snd_soc_dpcm_stream_unlock_irq(struct snd_soc_pcm_runtime *rtd,
int stream)
{
snd_pcm_stream_unlock_irq(snd_soc_dpcm_get_substream(rtd, stream));
}
#define snd_soc_dpcm_stream_unlock_irqrestore(rtd, stream, flags) \
snd_pcm_stream_unlock_irqrestore(snd_soc_dpcm_get_substream(rtd, stream), flags)
#define DPCM_MAX_BE_USERS 8
static inline const char *soc_cpu_dai_name(struct snd_soc_pcm_runtime *rtd)
{
return (rtd)->dai_link->num_cpus == 1 ? asoc_rtd_to_cpu(rtd, 0)->name : "multicpu";
}
static inline const char *soc_codec_dai_name(struct snd_soc_pcm_runtime *rtd)
{
return (rtd)->dai_link->num_codecs == 1 ? asoc_rtd_to_codec(rtd, 0)->name : "multicodec";
}
#ifdef CONFIG_DEBUG_FS
static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
{
switch (state) {
case SND_SOC_DPCM_STATE_NEW:
return "new";
case SND_SOC_DPCM_STATE_OPEN:
return "open";
case SND_SOC_DPCM_STATE_HW_PARAMS:
return "hw_params";
case SND_SOC_DPCM_STATE_PREPARE:
return "prepare";
case SND_SOC_DPCM_STATE_START:
return "start";
case SND_SOC_DPCM_STATE_STOP:
return "stop";
case SND_SOC_DPCM_STATE_SUSPEND:
return "suspend";
case SND_SOC_DPCM_STATE_PAUSED:
return "paused";
case SND_SOC_DPCM_STATE_HW_FREE:
return "hw_free";
case SND_SOC_DPCM_STATE_CLOSE:
return "close";
}
return "unknown";
}
static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
int stream, char *buf, size_t size)
{
struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
struct snd_soc_dpcm *dpcm;
ssize_t offset = 0;
/* FE state */
offset += scnprintf(buf + offset, size - offset,
"[%s - %s]\n", fe->dai_link->name,
stream ? "Capture" : "Playback");
offset += scnprintf(buf + offset, size - offset, "State: %s\n",
dpcm_state_string(fe->dpcm[stream].state));
if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
(fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
offset += scnprintf(buf + offset, size - offset,
"Hardware Params: "
"Format = %s, Channels = %d, Rate = %d\n",
snd_pcm_format_name(params_format(params)),
params_channels(params),
params_rate(params));
/* BEs state */
offset += scnprintf(buf + offset, size - offset, "Backends:\n");
if (list_empty(&fe->dpcm[stream].be_clients)) {
offset += scnprintf(buf + offset, size - offset,
" No active DSP links\n");
goto out;
}
for_each_dpcm_be(fe, stream, dpcm) {
struct snd_soc_pcm_runtime *be = dpcm->be;
params = &be->dpcm[stream].hw_params;
offset += scnprintf(buf + offset, size - offset,
"- %s\n", be->dai_link->name);
offset += scnprintf(buf + offset, size - offset,
" State: %s\n",
dpcm_state_string(be->dpcm[stream].state));
if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
(be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
offset += scnprintf(buf + offset, size - offset,
" Hardware Params: "
"Format = %s, Channels = %d, Rate = %d\n",
snd_pcm_format_name(params_format(params)),
params_channels(params),
params_rate(params));
}
out:
return offset;
}
static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
size_t count, loff_t *ppos)
{
struct snd_soc_pcm_runtime *fe = file->private_data;
ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
int stream;
char *buf;
if (fe->dai_link->num_cpus > 1) {
dev_err(fe->dev,
"%s doesn't support Multi CPU yet\n", __func__);
return -EINVAL;
}
buf = kmalloc(out_count, GFP_KERNEL);
if (!buf)
return -ENOMEM;
snd_soc_dpcm_mutex_lock(fe);
for_each_pcm_streams(stream)
if (snd_soc_dai_stream_valid(asoc_rtd_to_cpu(fe, 0), stream))
offset += dpcm_show_state(fe, stream,
buf + offset,
out_count - offset);
snd_soc_dpcm_mutex_unlock(fe);
ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
kfree(buf);
return ret;
}
static const struct file_operations dpcm_state_fops = {
.open = simple_open,
.read = dpcm_state_read_file,
.llseek = default_llseek,
};
void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
{
if (!rtd->dai_link->dynamic)
return;
if (!rtd->card->debugfs_card_root)
return;
rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
rtd->card->debugfs_card_root);
debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
rtd, &dpcm_state_fops);
}
static void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm, int stream)
{
char *name;
name = kasprintf(GFP_KERNEL, "%s:%s", dpcm->be->dai_link->name,
stream ? "capture" : "playback");
if (name) {
dpcm->debugfs_state = debugfs_create_dir(
name, dpcm->fe->debugfs_dpcm_root);
debugfs_create_u32("state", 0644, dpcm->debugfs_state,
&dpcm->state);
kfree(name);
}
}
static void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
{
debugfs_remove_recursive(dpcm->debugfs_state);
}
#else
static inline void dpcm_create_debugfs_state(struct snd_soc_dpcm *dpcm,
int stream)
{
}
static inline void dpcm_remove_debugfs_state(struct snd_soc_dpcm *dpcm)
{
}
#endif
/* Set FE's runtime_update state; the state is protected via PCM stream lock
* for avoiding the race with trigger callback.
* If the state is unset and a trigger is pending while the previous operation,
* process the pending trigger action here.
*/
static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
int stream, enum snd_soc_dpcm_update state)
{
struct snd_pcm_substream *substream =
snd_soc_dpcm_get_substream(fe, stream);
snd_soc_dpcm_stream_lock_irq(fe, stream);
if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
dpcm_fe_dai_do_trigger(substream,
fe->dpcm[stream].trigger_pending - 1);
fe->dpcm[stream].trigger_pending = 0;
}
fe->dpcm[stream].runtime_update = state;
snd_soc_dpcm_stream_unlock_irq(fe, stream);
}
static void dpcm_set_be_update_state(struct snd_soc_pcm_runtime *be,
int stream, enum snd_soc_dpcm_update state)
{
be->dpcm[stream].runtime_update = state;
}
/**
* snd_soc_runtime_action() - Increment/Decrement active count for
* PCM runtime components
* @rtd: ASoC PCM runtime that is activated
* @stream: Direction of the PCM stream
* @action: Activate stream if 1. Deactivate if -1.
*
* Increments/Decrements the active count for all the DAIs and components
* attached to a PCM runtime.
* Should typically be called when a stream is opened.
*
* Must be called with the rtd->card->pcm_mutex being held
*/
void snd_soc_runtime_action(struct snd_soc_pcm_runtime *rtd,
int stream, int action)
{
struct snd_soc_dai *dai;
int i;
snd_soc_dpcm_mutex_assert_held(rtd);
for_each_rtd_dais(rtd, i, dai)
snd_soc_dai_action(dai, stream, action);
}
EXPORT_SYMBOL_GPL(snd_soc_runtime_action);
/**
* snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
* @rtd: The ASoC PCM runtime that should be checked.
*
* This function checks whether the power down delay should be ignored for a
* specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
* been configured to ignore the delay, or if none of the components benefits
* from having the delay.
*/
bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
{
struct snd_soc_component *component;
bool ignore = true;
int i;
if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
return true;
for_each_rtd_components(rtd, i, component)
ignore &= !component->driver->use_pmdown_time;
return ignore;
}
/**
* snd_soc_set_runtime_hwparams - set the runtime hardware parameters
* @substream: the pcm substream
* @hw: the hardware parameters
*
* Sets the substream runtime hardware parameters.
*/
int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
const struct snd_pcm_hardware *hw)
{
substream->runtime->hw = *hw;
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
/* DPCM stream event, send event to FE and all active BEs. */
int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
int event)
{
struct snd_soc_dpcm *dpcm;
snd_soc_dpcm_mutex_assert_held(fe);
for_each_dpcm_be(fe, dir, dpcm) {
struct snd_soc_pcm_runtime *be = dpcm->be;
dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
be->dai_link->name, event, dir);
if ((event == SND_SOC_DAPM_STREAM_STOP) &&
(be->dpcm[dir].users >= 1))
continue;
snd_soc_dapm_stream_event(be, dir, event);
}
snd_soc_dapm_stream_event(fe, dir, event);
return 0;
}
static void soc_pcm_set_dai_params(struct snd_soc_dai *dai,
struct snd_pcm_hw_params *params)
{
if (params) {
dai->rate = params_rate(params);
dai->channels = params_channels(params);
dai->sample_bits = snd_pcm_format_physical_width(params_format(params));
} else {
dai->rate = 0;
dai->channels = 0;
dai->sample_bits = 0;
}
}
static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
struct snd_soc_dai *soc_dai)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
int ret;
if (!snd_soc_dai_active(soc_dai))
return 0;
#define __soc_pcm_apply_symmetry(name, NAME) \
if (soc_dai->name && (soc_dai->driver->symmetric_##name || \
rtd->dai_link->symmetric_##name)) { \
dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %s to %d\n",\
#name, soc_dai->name); \
\
ret = snd_pcm_hw_constraint_single(substream->runtime, \
SNDRV_PCM_HW_PARAM_##NAME,\
soc_dai->name); \
if (ret < 0) { \
dev_err(soc_dai->dev, \
"ASoC: Unable to apply %s constraint: %d\n",\
#name, ret); \
return ret; \
} \
}
__soc_pcm_apply_symmetry(rate, RATE);
__soc_pcm_apply_symmetry(channels, CHANNELS);
__soc_pcm_apply_symmetry(sample_bits, SAMPLE_BITS);
return 0;
}
static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_soc_dai d;
struct snd_soc_dai *dai;
struct snd_soc_dai *cpu_dai;
unsigned int symmetry, i;
d.name = __func__;
soc_pcm_set_dai_params(&d, params);
#define __soc_pcm_params_symmetry(xxx) \
symmetry = rtd->dai_link->symmetric_##xxx; \
for_each_rtd_dais(rtd, i, dai) \
symmetry |= dai->driver->symmetric_##xxx; \
\
if (symmetry) \
for_each_rtd_cpu_dais(rtd, i, cpu_dai) \
if (!snd_soc_dai_is_dummy(cpu_dai) && \
cpu_dai->xxx && cpu_dai->xxx != d.xxx) { \
dev_err(rtd->dev, "ASoC: unmatched %s symmetry: %s:%d - %s:%d\n", \
#xxx, cpu_dai->name, cpu_dai->xxx, d.name, d.xxx); \
return -EINVAL; \
}
/* reject unmatched parameters when applying symmetry */
__soc_pcm_params_symmetry(rate);
__soc_pcm_params_symmetry(channels);
__soc_pcm_params_symmetry(sample_bits);
return 0;
}
static void soc_pcm_update_symmetry(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_soc_dai_link *link = rtd->dai_link;
struct snd_soc_dai *dai;
unsigned int symmetry, i;
symmetry = link->symmetric_rate ||
link->symmetric_channels ||
link->symmetric_sample_bits;
for_each_rtd_dais(rtd, i, dai)
symmetry = symmetry ||
dai->driver->symmetric_rate ||
dai->driver->symmetric_channels ||
dai->driver->symmetric_sample_bits;
if (symmetry)
substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
}
static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
int ret;
if (!bits)
return;
ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
if (ret != 0)
dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
bits, ret);
}
static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_soc_dai *cpu_dai;
struct snd_soc_dai *codec_dai;
int stream = substream->stream;
int i;
unsigned int bits = 0, cpu_bits = 0;
for_each_rtd_codec_dais(rtd, i, codec_dai) {
struct snd_soc_pcm_stream *pcm_codec = snd_soc_dai_get_pcm_stream(codec_dai, stream);
if (pcm_codec->sig_bits == 0) {
bits = 0;
break;
}
bits = max(pcm_codec->sig_bits, bits);
}
for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
struct snd_soc_pcm_stream *pcm_cpu = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
if (pcm_cpu->sig_bits == 0) {
cpu_bits = 0;
break;
}
cpu_bits = max(pcm_cpu->sig_bits, cpu_bits);
}
soc_pcm_set_msb(substream, bits);
soc_pcm_set_msb(substream, cpu_bits);
}
static void soc_pcm_hw_init(struct snd_pcm_hardware *hw)
{
hw->rates = UINT_MAX;
hw->rate_min = 0;
hw->rate_max = UINT_MAX;
hw->channels_min = 0;
hw->channels_max = UINT_MAX;
hw->formats = ULLONG_MAX;
}
static void soc_pcm_hw_update_rate(struct snd_pcm_hardware *hw,
struct snd_soc_pcm_stream *p)
{
hw->rates = snd_pcm_rate_mask_intersect(hw->rates, p->rates);
/* setup hw->rate_min/max via hw->rates first */
snd_pcm_hw_limit_rates(hw);
/* update hw->rate_min/max by snd_soc_pcm_stream */
hw->rate_min = max(hw->rate_min, p->rate_min);
hw->rate_max = min_not_zero(hw->rate_max, p->rate_max);
}
static void soc_pcm_hw_update_chan(struct snd_pcm_hardware *hw,
struct snd_soc_pcm_stream *p)
{
hw->channels_min = max(hw->channels_min, p->channels_min);
hw->channels_max = min(hw->channels_max, p->channels_max);
}
static void soc_pcm_hw_update_format(struct snd_pcm_hardware *hw,
struct snd_soc_pcm_stream *p)
{
hw->formats &= p->formats;
}
/**
* snd_soc_runtime_calc_hw() - Calculate hw limits for a PCM stream
* @rtd: ASoC PCM runtime
* @hw: PCM hardware parameters (output)
* @stream: Direction of the PCM stream
*
* Calculates the subset of stream parameters supported by all DAIs
* associated with the PCM stream.
*/
int snd_soc_runtime_calc_hw(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_hardware *hw, int stream)
{
struct snd_soc_dai *codec_dai;
struct snd_soc_dai *cpu_dai;
struct snd_soc_pcm_stream *codec_stream;
struct snd_soc_pcm_stream *cpu_stream;
unsigned int cpu_chan_min = 0, cpu_chan_max = UINT_MAX;
int i;
soc_pcm_hw_init(hw);
/* first calculate min/max only for CPUs in the DAI link */
for_each_rtd_cpu_dais(rtd, i, cpu_dai) {
/*
* Skip CPUs which don't support the current stream type.
* Otherwise, since the rate, channel, and format values will
* zero in that case, we would have no usable settings left,
* causing the resulting setup to fail.
*/
if (!snd_soc_dai_stream_valid(cpu_dai, stream))
continue;
cpu_stream = snd_soc_dai_get_pcm_stream(cpu_dai, stream);
soc_pcm_hw_update_chan(hw, cpu_stream);
soc_pcm_hw_update_rate(hw, cpu_stream);
soc_pcm_hw_update_format(hw, cpu_stream);
}
cpu_chan_min = hw->channels_min;
cpu_chan_max = hw->channels_max;
/* second calculate min/max only for CODECs in the DAI link */
for_each_rtd_codec_dais(rtd, i, codec_dai) {
/*
* Skip CODECs which don't support the current stream type.
* Otherwise, since the rate, channel, and format values will
* zero in that case, we would have no usable settings left,
* causing the resulting setup to fail.
*/
if (!snd_soc_dai_stream_valid(codec_dai, stream))
continue;
codec_stream = snd_soc_dai_get_pcm_stream(codec_dai, stream);
soc_pcm_hw_update_chan(hw, codec_stream);
soc_pcm_hw_update_rate(hw, codec_stream);
soc_pcm_hw_update_format(hw, codec_stream);
}
/* Verify both a valid CPU DAI and a valid CODEC DAI were found */
if (!hw->channels_min)
return -EINVAL;
/*
* chan min/max cannot be enforced if there are multiple CODEC DAIs
* connected to CPU DAI(s), use CPU DAI's directly and let
* channel allocation be fixed up later
*/
if (rtd->dai_link->num_codecs > 1) {
hw->channels_min = cpu_chan_min;
hw->channels_max = cpu_chan_max;
}
return 0;
}
EXPORT_SYMBOL_GPL(snd_soc_runtime_calc_hw);
static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
{
struct snd_pcm_hardware *hw = &substream->runtime->hw;
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
u64 formats = hw->formats;
/*
* At least one CPU and one CODEC should match. Otherwise, we should
* have bailed out on a higher level, since there would be no CPU or
* CODEC to support the transfer direction in that case.
*/
snd_soc_runtime_calc_hw(rtd, hw, substream->stream);
if (formats)
hw->formats &= formats;
}
static int soc_pcm_components_open(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_soc_component *component;
int i, ret = 0;
for_each_rtd_components(rtd, i, component) {
ret = snd_soc_component_module_get_when_open(component, substream);
if (ret < 0)
break;
ret = snd_soc_component_open(component, substream);
if (ret < 0)
break;
}
return ret;
}
static int soc_pcm_components_close(struct snd_pcm_substream *substream,
int rollback)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_soc_component *component;
int i, ret = 0;
for_each_rtd_components(rtd, i, component) {
int r = snd_soc_component_close(component, substream, rollback);
if (r < 0)
ret = r; /* use last ret */
snd_soc_component_module_put_when_close(component, substream, rollback);
}
return ret;
}
static int soc_pcm_clean(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream, int rollback)
{
struct snd_soc_component *component;
struct snd_soc_dai *dai;
int i;
snd_soc_dpcm_mutex_assert_held(rtd);
if (!rollback)
snd_soc_runtime_deactivate(rtd, substream->stream);
for_each_rtd_dais(rtd, i, dai)
snd_soc_dai_shutdown(dai, substream, rollback);
snd_soc_link_shutdown(substream, rollback);
soc_pcm_components_close(substream, rollback);
snd_soc_pcm_component_pm_runtime_put(rtd, substream, rollback);
for_each_rtd_components(rtd, i, component)
if (!snd_soc_component_active(component))
pinctrl_pm_select_sleep_state(component->dev);
return 0;
}
/*
* Called by ALSA when a PCM substream is closed. Private data can be
* freed here. The cpu DAI, codec DAI, machine and components are also
* shutdown.
*/
static int __soc_pcm_close(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream)
{
return soc_pcm_clean(rtd, substream, 0);
}
/* PCM close ops for non-DPCM streams */
static int soc_pcm_close(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
snd_soc_dpcm_mutex_lock(rtd);
__soc_pcm_close(rtd, substream);
snd_soc_dpcm_mutex_unlock(rtd);
return 0;
}
static int soc_hw_sanity_check(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_pcm_hardware *hw = &substream->runtime->hw;
const char *name_cpu = soc_cpu_dai_name(rtd);
const char *name_codec = soc_codec_dai_name(rtd);
const char *err_msg;
struct device *dev = rtd->dev;
err_msg = "rates";
if (!hw->rates)
goto config_err;
err_msg = "formats";
if (!hw->formats)
goto config_err;
err_msg = "channels";
if (!hw->channels_min || !hw->channels_max ||
hw->channels_min > hw->channels_max)
goto config_err;
dev_dbg(dev, "ASoC: %s <-> %s info:\n", name_codec,
name_cpu);
dev_dbg(dev, "ASoC: rate mask 0x%x\n", hw->rates);
dev_dbg(dev, "ASoC: ch min %d max %d\n", hw->channels_min,
hw->channels_max);
dev_dbg(dev, "ASoC: rate min %d max %d\n", hw->rate_min,
hw->rate_max);
return 0;
config_err:
dev_err(dev, "ASoC: %s <-> %s No matching %s\n",
name_codec, name_cpu, err_msg);
return -EINVAL;
}
/*
* Called by ALSA when a PCM substream is opened, the runtime->hw record is
* then initialized and any private data can be allocated. This also calls
* startup for the cpu DAI, component, machine and codec DAI.
*/
static int __soc_pcm_open(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream)
{
struct snd_soc_component *component;
struct snd_soc_dai *dai;
int i, ret = 0;
snd_soc_dpcm_mutex_assert_held(rtd);
for_each_rtd_components(rtd, i, component)
pinctrl_pm_select_default_state(component->dev);
ret = snd_soc_pcm_component_pm_runtime_get(rtd, substream);
if (ret < 0)
goto err;
ret = soc_pcm_components_open(substream);
if (ret < 0)
goto err;
ret = snd_soc_link_startup(substream);
if (ret < 0)
goto err;
/* startup the audio subsystem */
for_each_rtd_dais(rtd, i, dai) {
ret = snd_soc_dai_startup(dai, substream);
if (ret < 0)
goto err;
}
/* Dynamic PCM DAI links compat checks use dynamic capabilities */
if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
goto dynamic;
/* Check that the codec and cpu DAIs are compatible */
soc_pcm_init_runtime_hw(substream);
soc_pcm_update_symmetry(substream);
ret = soc_hw_sanity_check(substream);
if (ret < 0)
goto err;
soc_pcm_apply_msb(substream);
/* Symmetry only applies if we've already got an active stream. */
for_each_rtd_dais(rtd, i, dai) {
ret = soc_pcm_apply_symmetry(substream, dai);
if (ret != 0)
goto err;
}
dynamic:
snd_soc_runtime_activate(rtd, substream->stream);
ret = 0;
err:
if (ret < 0)
soc_pcm_clean(rtd, substream, 1);
return soc_pcm_ret(rtd, ret);
}
/* PCM open ops for non-DPCM streams */
static int soc_pcm_open(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
int ret;
snd_soc_dpcm_mutex_lock(rtd);
ret = __soc_pcm_open(rtd, substream);
snd_soc_dpcm_mutex_unlock(rtd);
return ret;
}
/*
* Called by ALSA when the PCM substream is prepared, can set format, sample
* rate, etc. This function is non atomic and can be called multiple times,
* it can refer to the runtime info.
*/
static int __soc_pcm_prepare(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream)
{
struct snd_soc_dai *dai;
int i, ret = 0;
snd_soc_dpcm_mutex_assert_held(rtd);
ret = snd_soc_link_prepare(substream);
if (ret < 0)
goto out;
ret = snd_soc_pcm_component_prepare(substream);
if (ret < 0)
goto out;
ret = snd_soc_pcm_dai_prepare(substream);
if (ret < 0)
goto out;
/* cancel any delayed stream shutdown that is pending */
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
rtd->pop_wait) {
rtd->pop_wait = 0;
cancel_delayed_work(&rtd->delayed_work);
}
snd_soc_dapm_stream_event(rtd, substream->stream,
SND_SOC_DAPM_STREAM_START);
for_each_rtd_dais(rtd, i, dai)
snd_soc_dai_digital_mute(dai, 0, substream->stream);
out:
return soc_pcm_ret(rtd, ret);
}
/* PCM prepare ops for non-DPCM streams */
static int soc_pcm_prepare(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
int ret;
snd_soc_dpcm_mutex_lock(rtd);
ret = __soc_pcm_prepare(rtd, substream);
snd_soc_dpcm_mutex_unlock(rtd);
return ret;
}
static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
unsigned int mask)
{
struct snd_interval *interval;
int channels = hweight_long(mask);
interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
interval->min = channels;
interval->max = channels;
}
static int soc_pcm_hw_clean(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream, int rollback)
{
struct snd_soc_dai *dai;
int i;
snd_soc_dpcm_mutex_assert_held(rtd);
/* clear the corresponding DAIs parameters when going to be inactive */
for_each_rtd_dais(rtd, i, dai) {
if (snd_soc_dai_active(dai) == 1)
soc_pcm_set_dai_params(dai, NULL);
if (snd_soc_dai_stream_active(dai, substream->stream) == 1)
snd_soc_dai_digital_mute(dai, 1, substream->stream);
}
/* run the stream event */
snd_soc_dapm_stream_stop(rtd, substream->stream);
/* free any machine hw params */
snd_soc_link_hw_free(substream, rollback);
/* free any component resources */
snd_soc_pcm_component_hw_free(substream, rollback);
/* now free hw params for the DAIs */
for_each_rtd_dais(rtd, i, dai)
if (snd_soc_dai_stream_valid(dai, substream->stream))
snd_soc_dai_hw_free(dai, substream, rollback);
return 0;
}
/*
* Frees resources allocated by hw_params, can be called multiple times
*/
static int __soc_pcm_hw_free(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream)
{
return soc_pcm_hw_clean(rtd, substream, 0);
}
/* hw_free PCM ops for non-DPCM streams */
static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
int ret;
snd_soc_dpcm_mutex_lock(rtd);
ret = __soc_pcm_hw_free(rtd, substream);
snd_soc_dpcm_mutex_unlock(rtd);
return ret;
}
/*
* Called by ALSA when the hardware params are set by application. This
* function can also be called multiple times and can allocate buffers
* (using snd_pcm_lib_* ). It's non-atomic.
*/
static int __soc_pcm_hw_params(struct snd_soc_pcm_runtime *rtd,
struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_dai *cpu_dai;
struct snd_soc_dai *codec_dai;