forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctatc.c
1742 lines (1462 loc) · 41.8 KB
/
ctatc.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-only
/**
* Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
*
* @File ctatc.c
*
* @Brief
* This file contains the implementation of the device resource management
* object.
*
* @Author Liu Chun
* @Date Mar 28 2008
*/
#include "ctatc.h"
#include "ctpcm.h"
#include "ctmixer.h"
#include "ctsrc.h"
#include "ctamixer.h"
#include "ctdaio.h"
#include "cttimer.h"
#include <linux/delay.h>
#include <linux/slab.h>
#include <sound/pcm.h>
#include <sound/control.h>
#include <sound/asoundef.h>
#define MONO_SUM_SCALE 0x19a8 /* 2^(-0.5) in 14-bit floating format */
#define MAX_MULTI_CHN 8
#define IEC958_DEFAULT_CON ((IEC958_AES0_NONAUDIO \
| IEC958_AES0_CON_NOT_COPYRIGHT) \
| ((IEC958_AES1_CON_MIXER \
| IEC958_AES1_CON_ORIGINAL) << 8) \
| (0x10 << 16) \
| ((IEC958_AES3_CON_FS_48000) << 24))
static const struct snd_pci_quirk subsys_20k1_list[] = {
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0022, "SB055x", CTSB055X),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x002f, "SB055x", CTSB055X),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0029, "SB073x", CTSB073X),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, 0x0031, "SB073x", CTSB073X),
SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000, 0x6000,
"UAA", CTUAA),
{ } /* terminator */
};
static const struct snd_pci_quirk subsys_20k2_list[] = {
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB0760,
"SB0760", CTSB0760),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB1270,
"SB1270", CTSB1270),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08801,
"SB0880", CTSB0880),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08802,
"SB0880", CTSB0880),
SND_PCI_QUIRK(PCI_VENDOR_ID_CREATIVE, PCI_SUBDEVICE_ID_CREATIVE_SB08803,
"SB0880", CTSB0880),
SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_CREATIVE, 0xf000,
PCI_SUBDEVICE_ID_CREATIVE_HENDRIX, "HENDRIX",
CTHENDRIX),
{ } /* terminator */
};
static const char *ct_subsys_name[NUM_CTCARDS] = {
/* 20k1 models */
[CTSB055X] = "SB055x",
[CTSB073X] = "SB073x",
[CTUAA] = "UAA",
[CT20K1_UNKNOWN] = "Unknown",
/* 20k2 models */
[CTSB0760] = "SB076x",
[CTHENDRIX] = "Hendrix",
[CTSB0880] = "SB0880",
[CTSB1270] = "SB1270",
[CT20K2_UNKNOWN] = "Unknown",
};
static struct {
int (*create)(struct ct_atc *atc,
enum CTALSADEVS device, const char *device_name);
int (*destroy)(void *alsa_dev);
const char *public_name;
} alsa_dev_funcs[NUM_CTALSADEVS] = {
[FRONT] = { .create = ct_alsa_pcm_create,
.destroy = NULL,
.public_name = "Front/WaveIn"},
[SURROUND] = { .create = ct_alsa_pcm_create,
.destroy = NULL,
.public_name = "Surround"},
[CLFE] = { .create = ct_alsa_pcm_create,
.destroy = NULL,
.public_name = "Center/LFE"},
[SIDE] = { .create = ct_alsa_pcm_create,
.destroy = NULL,
.public_name = "Side"},
[IEC958] = { .create = ct_alsa_pcm_create,
.destroy = NULL,
.public_name = "IEC958 Non-audio"},
[MIXER] = { .create = ct_alsa_mix_create,
.destroy = NULL,
.public_name = "Mixer"}
};
typedef int (*create_t)(struct hw *, void **);
typedef int (*destroy_t)(void *);
static struct {
int (*create)(struct hw *hw, void **rmgr);
int (*destroy)(void *mgr);
} rsc_mgr_funcs[NUM_RSCTYP] = {
[SRC] = { .create = (create_t)src_mgr_create,
.destroy = (destroy_t)src_mgr_destroy },
[SRCIMP] = { .create = (create_t)srcimp_mgr_create,
.destroy = (destroy_t)srcimp_mgr_destroy },
[AMIXER] = { .create = (create_t)amixer_mgr_create,
.destroy = (destroy_t)amixer_mgr_destroy },
[SUM] = { .create = (create_t)sum_mgr_create,
.destroy = (destroy_t)sum_mgr_destroy },
[DAIO] = { .create = (create_t)daio_mgr_create,
.destroy = (destroy_t)daio_mgr_destroy }
};
static int
atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm);
/* *
* Only mono and interleaved modes are supported now.
* Always allocates a contiguous channel block.
* */
static int ct_map_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct snd_pcm_runtime *runtime;
struct ct_vm *vm;
if (!apcm->substream)
return 0;
runtime = apcm->substream->runtime;
vm = atc->vm;
apcm->vm_block = vm->map(vm, apcm->substream, runtime->dma_bytes);
if (!apcm->vm_block)
return -ENOENT;
return 0;
}
static void ct_unmap_audio_buffer(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct ct_vm *vm;
if (!apcm->vm_block)
return;
vm = atc->vm;
vm->unmap(vm, apcm->vm_block);
apcm->vm_block = NULL;
}
static unsigned long atc_get_ptp_phys(struct ct_atc *atc, int index)
{
return atc->vm->get_ptp_phys(atc->vm, index);
}
static unsigned int convert_format(snd_pcm_format_t snd_format,
struct snd_card *card)
{
switch (snd_format) {
case SNDRV_PCM_FORMAT_U8:
return SRC_SF_U8;
case SNDRV_PCM_FORMAT_S16_LE:
return SRC_SF_S16;
case SNDRV_PCM_FORMAT_S24_3LE:
return SRC_SF_S24;
case SNDRV_PCM_FORMAT_S32_LE:
return SRC_SF_S32;
case SNDRV_PCM_FORMAT_FLOAT_LE:
return SRC_SF_F32;
default:
dev_err(card->dev, "not recognized snd format is %d\n",
snd_format);
return SRC_SF_S16;
}
}
static unsigned int
atc_get_pitch(unsigned int input_rate, unsigned int output_rate)
{
unsigned int pitch;
int b;
/* get pitch and convert to fixed-point 8.24 format. */
pitch = (input_rate / output_rate) << 24;
input_rate %= output_rate;
input_rate /= 100;
output_rate /= 100;
for (b = 31; ((b >= 0) && !(input_rate >> b)); )
b--;
if (b >= 0) {
input_rate <<= (31 - b);
input_rate /= output_rate;
b = 24 - (31 - b);
if (b >= 0)
input_rate <<= b;
else
input_rate >>= -b;
pitch |= input_rate;
}
return pitch;
}
static int select_rom(unsigned int pitch)
{
if (pitch > 0x00428f5c && pitch < 0x01b851ec) {
/* 0.26 <= pitch <= 1.72 */
return 1;
} else if (pitch == 0x01d66666 || pitch == 0x01d66667) {
/* pitch == 1.8375 */
return 2;
} else if (pitch == 0x02000000) {
/* pitch == 2 */
return 3;
} else if (pitch <= 0x08000000) {
/* 0 <= pitch <= 8 */
return 0;
} else {
return -ENOENT;
}
}
static int atc_pcm_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
struct src_desc desc = {0};
struct amixer_desc mix_dsc = {0};
struct src *src;
struct amixer *amixer;
int err;
int n_amixer = apcm->substream->runtime->channels, i = 0;
int device = apcm->substream->pcm->device;
unsigned int pitch;
/* first release old resources */
atc_pcm_release_resources(atc, apcm);
/* Get SRC resource */
desc.multi = apcm->substream->runtime->channels;
desc.msr = atc->msr;
desc.mode = MEMRD;
err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
if (err)
goto error1;
pitch = atc_get_pitch(apcm->substream->runtime->rate,
(atc->rsr * atc->msr));
src = apcm->src;
src->ops->set_pitch(src, pitch);
src->ops->set_rom(src, select_rom(pitch));
src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
atc->card));
src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
/* Get AMIXER resource */
n_amixer = (n_amixer < 2) ? 2 : n_amixer;
apcm->amixers = kcalloc(n_amixer, sizeof(void *), GFP_KERNEL);
if (!apcm->amixers) {
err = -ENOMEM;
goto error1;
}
mix_dsc.msr = atc->msr;
for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
(struct amixer **)&apcm->amixers[i]);
if (err)
goto error1;
apcm->n_amixer++;
}
/* Set up device virtual mem map */
err = ct_map_audio_buffer(atc, apcm);
if (err < 0)
goto error1;
/* Connect resources */
src = apcm->src;
for (i = 0; i < n_amixer; i++) {
amixer = apcm->amixers[i];
mutex_lock(&atc->atc_mutex);
amixer->ops->setup(amixer, &src->rsc,
INIT_VOL, atc->pcm[i+device*2]);
mutex_unlock(&atc->atc_mutex);
src = src->ops->next_interleave(src);
if (!src)
src = apcm->src;
}
ct_timer_prepare(apcm->timer);
return 0;
error1:
atc_pcm_release_resources(atc, apcm);
return err;
}
static int
atc_pcm_release_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
struct srcimp *srcimp;
int i;
if (apcm->srcimps) {
for (i = 0; i < apcm->n_srcimp; i++) {
srcimp = apcm->srcimps[i];
srcimp->ops->unmap(srcimp);
srcimp_mgr->put_srcimp(srcimp_mgr, srcimp);
apcm->srcimps[i] = NULL;
}
kfree(apcm->srcimps);
apcm->srcimps = NULL;
}
if (apcm->srccs) {
for (i = 0; i < apcm->n_srcc; i++) {
src_mgr->put_src(src_mgr, apcm->srccs[i]);
apcm->srccs[i] = NULL;
}
kfree(apcm->srccs);
apcm->srccs = NULL;
}
if (apcm->amixers) {
for (i = 0; i < apcm->n_amixer; i++) {
amixer_mgr->put_amixer(amixer_mgr, apcm->amixers[i]);
apcm->amixers[i] = NULL;
}
kfree(apcm->amixers);
apcm->amixers = NULL;
}
if (apcm->mono) {
sum_mgr->put_sum(sum_mgr, apcm->mono);
apcm->mono = NULL;
}
if (apcm->src) {
src_mgr->put_src(src_mgr, apcm->src);
apcm->src = NULL;
}
if (apcm->vm_block) {
/* Undo device virtual mem map */
ct_unmap_audio_buffer(atc, apcm);
apcm->vm_block = NULL;
}
return 0;
}
static int atc_pcm_playback_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
unsigned int max_cisz;
struct src *src = apcm->src;
if (apcm->started)
return 0;
apcm->started = 1;
max_cisz = src->multi * src->rsc.msr;
max_cisz = 0x80 * (max_cisz < 8 ? max_cisz : 8);
src->ops->set_sa(src, apcm->vm_block->addr);
src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
src->ops->set_ca(src, apcm->vm_block->addr + max_cisz);
src->ops->set_cisz(src, max_cisz);
src->ops->set_bm(src, 1);
src->ops->set_state(src, SRC_STATE_INIT);
src->ops->commit_write(src);
ct_timer_start(apcm->timer);
return 0;
}
static int atc_pcm_stop(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src *src;
int i;
ct_timer_stop(apcm->timer);
src = apcm->src;
src->ops->set_bm(src, 0);
src->ops->set_state(src, SRC_STATE_OFF);
src->ops->commit_write(src);
if (apcm->srccs) {
for (i = 0; i < apcm->n_srcc; i++) {
src = apcm->srccs[i];
src->ops->set_bm(src, 0);
src->ops->set_state(src, SRC_STATE_OFF);
src->ops->commit_write(src);
}
}
apcm->started = 0;
return 0;
}
static int
atc_pcm_playback_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src *src = apcm->src;
u32 size, max_cisz;
int position;
if (!src)
return 0;
position = src->ops->get_ca(src);
if (position < apcm->vm_block->addr) {
dev_dbg(atc->card->dev,
"bad ca - ca=0x%08x, vba=0x%08x, vbs=0x%08x\n",
position, apcm->vm_block->addr, apcm->vm_block->size);
position = apcm->vm_block->addr;
}
size = apcm->vm_block->size;
max_cisz = src->multi * src->rsc.msr;
max_cisz = 128 * (max_cisz < 8 ? max_cisz : 8);
return (position + size - max_cisz - apcm->vm_block->addr) % size;
}
struct src_node_conf_t {
unsigned int pitch;
unsigned int msr:8;
unsigned int mix_msr:8;
unsigned int imp_msr:8;
unsigned int vo:1;
};
static void setup_src_node_conf(struct ct_atc *atc, struct ct_atc_pcm *apcm,
struct src_node_conf_t *conf, int *n_srcc)
{
unsigned int pitch;
/* get pitch and convert to fixed-point 8.24 format. */
pitch = atc_get_pitch((atc->rsr * atc->msr),
apcm->substream->runtime->rate);
*n_srcc = 0;
if (1 == atc->msr) { /* FIXME: do we really need SRC here if pitch==1 */
*n_srcc = apcm->substream->runtime->channels;
conf[0].pitch = pitch;
conf[0].mix_msr = conf[0].imp_msr = conf[0].msr = 1;
conf[0].vo = 1;
} else if (2 <= atc->msr) {
if (0x8000000 < pitch) {
/* Need two-stage SRCs, SRCIMPs and
* AMIXERs for converting format */
conf[0].pitch = (atc->msr << 24);
conf[0].msr = conf[0].mix_msr = 1;
conf[0].imp_msr = atc->msr;
conf[0].vo = 0;
conf[1].pitch = atc_get_pitch(atc->rsr,
apcm->substream->runtime->rate);
conf[1].msr = conf[1].mix_msr = conf[1].imp_msr = 1;
conf[1].vo = 1;
*n_srcc = apcm->substream->runtime->channels * 2;
} else if (0x1000000 < pitch) {
/* Need one-stage SRCs, SRCIMPs and
* AMIXERs for converting format */
conf[0].pitch = pitch;
conf[0].msr = conf[0].mix_msr
= conf[0].imp_msr = atc->msr;
conf[0].vo = 1;
*n_srcc = apcm->substream->runtime->channels;
}
}
}
static int
atc_pcm_capture_get_resources(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
struct srcimp_mgr *srcimp_mgr = atc->rsc_mgrs[SRCIMP];
struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
struct sum_mgr *sum_mgr = atc->rsc_mgrs[SUM];
struct src_desc src_dsc = {0};
struct src *src;
struct srcimp_desc srcimp_dsc = {0};
struct srcimp *srcimp;
struct amixer_desc mix_dsc = {0};
struct sum_desc sum_dsc = {0};
unsigned int pitch;
int multi, err, i;
int n_srcimp, n_amixer, n_srcc, n_sum;
struct src_node_conf_t src_node_conf[2] = {{0} };
/* first release old resources */
atc_pcm_release_resources(atc, apcm);
/* The numbers of converting SRCs and SRCIMPs should be determined
* by pitch value. */
multi = apcm->substream->runtime->channels;
/* get pitch and convert to fixed-point 8.24 format. */
pitch = atc_get_pitch((atc->rsr * atc->msr),
apcm->substream->runtime->rate);
setup_src_node_conf(atc, apcm, src_node_conf, &n_srcc);
n_sum = (1 == multi) ? 1 : 0;
n_amixer = n_sum * 2 + n_srcc;
n_srcimp = n_srcc;
if ((multi > 1) && (0x8000000 >= pitch)) {
/* Need extra AMIXERs and SRCIMPs for special treatment
* of interleaved recording of conjugate channels */
n_amixer += multi * atc->msr;
n_srcimp += multi * atc->msr;
} else {
n_srcimp += multi;
}
if (n_srcc) {
apcm->srccs = kcalloc(n_srcc, sizeof(void *), GFP_KERNEL);
if (!apcm->srccs)
return -ENOMEM;
}
if (n_amixer) {
apcm->amixers = kcalloc(n_amixer, sizeof(void *), GFP_KERNEL);
if (!apcm->amixers) {
err = -ENOMEM;
goto error1;
}
}
apcm->srcimps = kcalloc(n_srcimp, sizeof(void *), GFP_KERNEL);
if (!apcm->srcimps) {
err = -ENOMEM;
goto error1;
}
/* Allocate SRCs for sample rate conversion if needed */
src_dsc.multi = 1;
src_dsc.mode = ARCRW;
for (i = 0, apcm->n_srcc = 0; i < n_srcc; i++) {
src_dsc.msr = src_node_conf[i/multi].msr;
err = src_mgr->get_src(src_mgr, &src_dsc,
(struct src **)&apcm->srccs[i]);
if (err)
goto error1;
src = apcm->srccs[i];
pitch = src_node_conf[i/multi].pitch;
src->ops->set_pitch(src, pitch);
src->ops->set_rom(src, select_rom(pitch));
src->ops->set_vo(src, src_node_conf[i/multi].vo);
apcm->n_srcc++;
}
/* Allocate AMIXERs for routing SRCs of conversion if needed */
for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
if (i < (n_sum*2))
mix_dsc.msr = atc->msr;
else if (i < (n_sum*2+n_srcc))
mix_dsc.msr = src_node_conf[(i-n_sum*2)/multi].mix_msr;
else
mix_dsc.msr = 1;
err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
(struct amixer **)&apcm->amixers[i]);
if (err)
goto error1;
apcm->n_amixer++;
}
/* Allocate a SUM resource to mix all input channels together */
sum_dsc.msr = atc->msr;
err = sum_mgr->get_sum(sum_mgr, &sum_dsc, (struct sum **)&apcm->mono);
if (err)
goto error1;
pitch = atc_get_pitch((atc->rsr * atc->msr),
apcm->substream->runtime->rate);
/* Allocate SRCIMP resources */
for (i = 0, apcm->n_srcimp = 0; i < n_srcimp; i++) {
if (i < (n_srcc))
srcimp_dsc.msr = src_node_conf[i/multi].imp_msr;
else if (1 == multi)
srcimp_dsc.msr = (pitch <= 0x8000000) ? atc->msr : 1;
else
srcimp_dsc.msr = 1;
err = srcimp_mgr->get_srcimp(srcimp_mgr, &srcimp_dsc, &srcimp);
if (err)
goto error1;
apcm->srcimps[i] = srcimp;
apcm->n_srcimp++;
}
/* Allocate a SRC for writing data to host memory */
src_dsc.multi = apcm->substream->runtime->channels;
src_dsc.msr = 1;
src_dsc.mode = MEMWR;
err = src_mgr->get_src(src_mgr, &src_dsc, (struct src **)&apcm->src);
if (err)
goto error1;
src = apcm->src;
src->ops->set_pitch(src, pitch);
/* Set up device virtual mem map */
err = ct_map_audio_buffer(atc, apcm);
if (err < 0)
goto error1;
return 0;
error1:
atc_pcm_release_resources(atc, apcm);
return err;
}
static int atc_pcm_capture_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src *src;
struct amixer *amixer;
struct srcimp *srcimp;
struct ct_mixer *mixer = atc->mixer;
struct sum *mono;
struct rsc *out_ports[8] = {NULL};
int err, i, j, n_sum, multi;
unsigned int pitch;
int mix_base = 0, imp_base = 0;
atc_pcm_release_resources(atc, apcm);
/* Get needed resources. */
err = atc_pcm_capture_get_resources(atc, apcm);
if (err)
return err;
/* Connect resources */
mixer->get_output_ports(mixer, MIX_PCMO_FRONT,
&out_ports[0], &out_ports[1]);
multi = apcm->substream->runtime->channels;
if (1 == multi) {
mono = apcm->mono;
for (i = 0; i < 2; i++) {
amixer = apcm->amixers[i];
amixer->ops->setup(amixer, out_ports[i],
MONO_SUM_SCALE, mono);
}
out_ports[0] = &mono->rsc;
n_sum = 1;
mix_base = n_sum * 2;
}
for (i = 0; i < apcm->n_srcc; i++) {
src = apcm->srccs[i];
srcimp = apcm->srcimps[imp_base+i];
amixer = apcm->amixers[mix_base+i];
srcimp->ops->map(srcimp, src, out_ports[i%multi]);
amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
out_ports[i%multi] = &amixer->rsc;
}
pitch = atc_get_pitch((atc->rsr * atc->msr),
apcm->substream->runtime->rate);
if ((multi > 1) && (pitch <= 0x8000000)) {
/* Special connection for interleaved
* recording with conjugate channels */
for (i = 0; i < multi; i++) {
out_ports[i]->ops->master(out_ports[i]);
for (j = 0; j < atc->msr; j++) {
amixer = apcm->amixers[apcm->n_srcc+j*multi+i];
amixer->ops->set_input(amixer, out_ports[i]);
amixer->ops->set_scale(amixer, INIT_VOL);
amixer->ops->set_sum(amixer, NULL);
amixer->ops->commit_raw_write(amixer);
out_ports[i]->ops->next_conj(out_ports[i]);
srcimp = apcm->srcimps[apcm->n_srcc+j*multi+i];
srcimp->ops->map(srcimp, apcm->src,
&amixer->rsc);
}
}
} else {
for (i = 0; i < multi; i++) {
srcimp = apcm->srcimps[apcm->n_srcc+i];
srcimp->ops->map(srcimp, apcm->src, out_ports[i]);
}
}
ct_timer_prepare(apcm->timer);
return 0;
}
static int atc_pcm_capture_start(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src *src;
struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
int i, multi;
if (apcm->started)
return 0;
apcm->started = 1;
multi = apcm->substream->runtime->channels;
/* Set up converting SRCs */
for (i = 0; i < apcm->n_srcc; i++) {
src = apcm->srccs[i];
src->ops->set_pm(src, ((i%multi) != (multi-1)));
src_mgr->src_disable(src_mgr, src);
}
/* Set up recording SRC */
src = apcm->src;
src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
atc->card));
src->ops->set_sa(src, apcm->vm_block->addr);
src->ops->set_la(src, apcm->vm_block->addr + apcm->vm_block->size);
src->ops->set_ca(src, apcm->vm_block->addr);
src_mgr->src_disable(src_mgr, src);
/* Disable relevant SRCs firstly */
src_mgr->commit_write(src_mgr);
/* Enable SRCs respectively */
for (i = 0; i < apcm->n_srcc; i++) {
src = apcm->srccs[i];
src->ops->set_state(src, SRC_STATE_RUN);
src->ops->commit_write(src);
src_mgr->src_enable_s(src_mgr, src);
}
src = apcm->src;
src->ops->set_bm(src, 1);
src->ops->set_state(src, SRC_STATE_RUN);
src->ops->commit_write(src);
src_mgr->src_enable_s(src_mgr, src);
/* Enable relevant SRCs synchronously */
src_mgr->commit_write(src_mgr);
ct_timer_start(apcm->timer);
return 0;
}
static int
atc_pcm_capture_position(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src *src = apcm->src;
if (!src)
return 0;
return src->ops->get_ca(src) - apcm->vm_block->addr;
}
static int spdif_passthru_playback_get_resources(struct ct_atc *atc,
struct ct_atc_pcm *apcm)
{
struct src_mgr *src_mgr = atc->rsc_mgrs[SRC];
struct amixer_mgr *amixer_mgr = atc->rsc_mgrs[AMIXER];
struct src_desc desc = {0};
struct amixer_desc mix_dsc = {0};
struct src *src;
int err;
int n_amixer = apcm->substream->runtime->channels, i;
unsigned int pitch, rsr = atc->pll_rate;
/* first release old resources */
atc_pcm_release_resources(atc, apcm);
/* Get SRC resource */
desc.multi = apcm->substream->runtime->channels;
desc.msr = 1;
while (apcm->substream->runtime->rate > (rsr * desc.msr))
desc.msr <<= 1;
desc.mode = MEMRD;
err = src_mgr->get_src(src_mgr, &desc, (struct src **)&apcm->src);
if (err)
goto error1;
pitch = atc_get_pitch(apcm->substream->runtime->rate, (rsr * desc.msr));
src = apcm->src;
src->ops->set_pitch(src, pitch);
src->ops->set_rom(src, select_rom(pitch));
src->ops->set_sf(src, convert_format(apcm->substream->runtime->format,
atc->card));
src->ops->set_pm(src, (src->ops->next_interleave(src) != NULL));
src->ops->set_bp(src, 1);
/* Get AMIXER resource */
n_amixer = (n_amixer < 2) ? 2 : n_amixer;
apcm->amixers = kcalloc(n_amixer, sizeof(void *), GFP_KERNEL);
if (!apcm->amixers) {
err = -ENOMEM;
goto error1;
}
mix_dsc.msr = desc.msr;
for (i = 0, apcm->n_amixer = 0; i < n_amixer; i++) {
err = amixer_mgr->get_amixer(amixer_mgr, &mix_dsc,
(struct amixer **)&apcm->amixers[i]);
if (err)
goto error1;
apcm->n_amixer++;
}
/* Set up device virtual mem map */
err = ct_map_audio_buffer(atc, apcm);
if (err < 0)
goto error1;
return 0;
error1:
atc_pcm_release_resources(atc, apcm);
return err;
}
static int atc_pll_init(struct ct_atc *atc, int rate)
{
struct hw *hw = atc->hw;
int err;
err = hw->pll_init(hw, rate);
atc->pll_rate = err ? 0 : rate;
return err;
}
static int
spdif_passthru_playback_setup(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct dao *dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
unsigned int rate = apcm->substream->runtime->rate;
unsigned int status;
int err = 0;
unsigned char iec958_con_fs;
switch (rate) {
case 48000:
iec958_con_fs = IEC958_AES3_CON_FS_48000;
break;
case 44100:
iec958_con_fs = IEC958_AES3_CON_FS_44100;
break;
case 32000:
iec958_con_fs = IEC958_AES3_CON_FS_32000;
break;
default:
return -ENOENT;
}
mutex_lock(&atc->atc_mutex);
dao->ops->get_spos(dao, &status);
if (((status >> 24) & IEC958_AES3_CON_FS) != iec958_con_fs) {
status &= ~(IEC958_AES3_CON_FS << 24);
status |= (iec958_con_fs << 24);
dao->ops->set_spos(dao, status);
dao->ops->commit_write(dao);
}
if ((rate != atc->pll_rate) && (32000 != rate))
err = atc_pll_init(atc, rate);
mutex_unlock(&atc->atc_mutex);
return err;
}
static int
spdif_passthru_playback_prepare(struct ct_atc *atc, struct ct_atc_pcm *apcm)
{
struct src *src;
struct amixer *amixer;
struct dao *dao;
int err;
int i;
atc_pcm_release_resources(atc, apcm);
/* Configure SPDIFOO and PLL to passthrough mode;
* determine pll_rate. */
err = spdif_passthru_playback_setup(atc, apcm);
if (err)
return err;
/* Get needed resources. */
err = spdif_passthru_playback_get_resources(atc, apcm);
if (err)
return err;
/* Connect resources */
src = apcm->src;
for (i = 0; i < apcm->n_amixer; i++) {
amixer = apcm->amixers[i];
amixer->ops->setup(amixer, &src->rsc, INIT_VOL, NULL);
src = src->ops->next_interleave(src);
if (!src)
src = apcm->src;
}
/* Connect to SPDIFOO */
mutex_lock(&atc->atc_mutex);
dao = container_of(atc->daios[SPDIFOO], struct dao, daio);
amixer = apcm->amixers[0];
dao->ops->set_left_input(dao, &amixer->rsc);
amixer = apcm->amixers[1];
dao->ops->set_right_input(dao, &amixer->rsc);
mutex_unlock(&atc->atc_mutex);
ct_timer_prepare(apcm->timer);
return 0;
}
static int atc_select_line_in(struct ct_atc *atc)
{
struct hw *hw = atc->hw;
struct ct_mixer *mixer = atc->mixer;
struct src *src;
if (hw->is_adc_source_selected(hw, ADC_LINEIN))
return 0;
mixer->set_input_left(mixer, MIX_MIC_IN, NULL);
mixer->set_input_right(mixer, MIX_MIC_IN, NULL);
hw->select_adc_source(hw, ADC_LINEIN);
src = atc->srcs[2];
mixer->set_input_left(mixer, MIX_LINE_IN, &src->rsc);
src = atc->srcs[3];
mixer->set_input_right(mixer, MIX_LINE_IN, &src->rsc);
return 0;
}
static int atc_select_mic_in(struct ct_atc *atc)
{
struct hw *hw = atc->hw;
struct ct_mixer *mixer = atc->mixer;
struct src *src;
if (hw->is_adc_source_selected(hw, ADC_MICIN))
return 0;
mixer->set_input_left(mixer, MIX_LINE_IN, NULL);
mixer->set_input_right(mixer, MIX_LINE_IN, NULL);
hw->select_adc_source(hw, ADC_MICIN);
src = atc->srcs[2];
mixer->set_input_left(mixer, MIX_MIC_IN, &src->rsc);
src = atc->srcs[3];
mixer->set_input_right(mixer, MIX_MIC_IN, &src->rsc);
return 0;
}
static struct capabilities atc_capabilities(struct ct_atc *atc)
{
struct hw *hw = atc->hw;
return hw->capabilities(hw);
}
static int atc_output_switch_get(struct ct_atc *atc)
{
struct hw *hw = atc->hw;
return hw->output_switch_get(hw);
}
static int atc_output_switch_put(struct ct_atc *atc, int position)
{
struct hw *hw = atc->hw;
return hw->output_switch_put(hw, position);