-
Notifications
You must be signed in to change notification settings - Fork 4
/
audio_hw.c
2826 lines (2454 loc) · 93.5 KB
/
audio_hw.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
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "sun4i_audio_hardware"
#define LOG_NDEBUG 0
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <cutils/log.h>
#include <cutils/str_parms.h>
#include <cutils/properties.h>
#include <hardware/hardware.h>
#include <system/audio.h>
#include <hardware/audio.h>
#include <tinyalsa/asoundlib.h>
#include <audio_utils/resampler.h>
#include <audio_utils/echo_reference.h>
#include <hardware/audio_effect.h>
#include <audio_effects/effect_aec.h>
#include "ril_interface.h"
//#define F_LOG LOGV("%s, line: %d", __FUNCTION__, __LINE__);
#define F_LOG LOGV("########## %s ##########", __FUNCTION__);
/* Mixer control names */
#define MIXER_DL2_LEFT_EQUALIZER "DL2 Left Equalizer"
#define MIXER_DL2_RIGHT_EQUALIZER "DL2 Right Equalizer"
#define MIXER_DL1_MEDIA_PLAYBACK_VOLUME "DL1 Media Playback Volume"
#define MIXER_DL1_VOICE_PLAYBACK_VOLUME "DL1 Voice Playback Volume"
#define MIXER_DL2_MEDIA_PLAYBACK_VOLUME "DL2 Media Playback Volume"
#define MIXER_DL2_VOICE_PLAYBACK_VOLUME "DL2 Voice Playback Volume"
#define MIXER_SDT_DL_VOLUME "SDT DL Volume"
#define MIXER_SDT_UL_VOLUME "SDT UL Volume"
#define MIXER_HEADSET_PLAYBACK_VOLUME "Headset Playback Volume"
#define MIXER_HANDSFREE_PLAYBACK_VOLUME "Handsfree Playback Volume"
#define MIXER_EARPHONE_PLAYBACK_VOLUME "Earphone Playback Volume"
#define MIXER_BT_UL_VOLUME "BT UL Volume"
#define MIXER_DL1_EQUALIZER "DL1 Equalizer"
#define MIXER_DL1_MIXER_MULTIMEDIA "DL1 Mixer Multimedia"
#define MIXER_DL1_MIXER_VOICE "DL1 Mixer Voice"
#define MIXER_DL2_MIXER_MULTIMEDIA "DL2 Mixer Multimedia"
#define MIXER_DL2_MIXER_VOICE "DL2 Mixer Voice"
#define MIXER_SIDETONE_MIXER_PLAYBACK "Sidetone Mixer Playback"
#define MIXER_SIDETONE_MIXER_CAPTURE "Sidetone Mixer Capture"
#define MIXER_DL2_MONO_MIXER "DL2 Mono Mixer"
#define MIXER_DL1_PDM_SWITCH "DL1 PDM Switch"
#define MIXER_DL1_BT_VX_SWITCH "DL1 BT_VX Switch"
#define MIXER_VOICE_CAPTURE_MIXER_CAPTURE "Voice Capture Mixer Capture"
#define MIXER_HS_LEFT_PLAYBACK "HS Left Playback"
#define MIXER_HS_RIGHT_PLAYBACK "HS Right Playback"
#define MIXER_HF_LEFT_PLAYBACK "HF Left Playback"
#define MIXER_HF_RIGHT_PLAYBACK "HF Right Playback"
#define MIXER_EARPHONE_ENABLE_SWITCH "Earphone Enable Switch"
#define MIXER_ANALOG_LEFT_CAPTURE_ROUTE "Analog Left Capture Route"
#define MIXER_ANALOG_RIGHT_CAPTURE_ROUTE "Analog Right Capture Route"
#define MIXER_CAPTURE_PREAMPLIFIER_VOLUME "Capture Preamplifier Volume"
#define MIXER_CAPTURE_VOLUME "Capture Volume"
#define MIXER_AMIC_UL_VOLUME "AMIC UL Volume"
#define MIXER_AUDUL_VOICE_UL_VOLUME "AUDUL Voice UL Volume"
#define MIXER_MUX_VX0 "MUX_VX0"
#define MIXER_MUX_VX1 "MUX_VX1"
#define MIXER_MUX_UL10 "MUX_UL10"
#define MIXER_MUX_UL11 "MUX_UL11"
/* Sun4i Mixer Controls
ctl type num name value range
0 INT 1 Master Playback Volume (0->63)
1 BOOL 1 Playback Switch (0/1)
2 INT 1 Capture Volume (0->7)
3 INT 1 Fm Volume (0->7)
4 BOOL 1 Line Volume (0/1)
5 INT 1 MicL Volume (0->3)
6 INT 1 MicR Volume (0->3)
7 BOOL 1 FmL Switch (0/1)
8 BOOL 1 FmR Switch (0/1)
9 BOOL 1 LineL Switch (0/1)
10 BOOL 1 LineR Switch (0/1)
11 BOOL 1 Ldac Left Mixer (0/1)
12 BOOL 1 Rdac Right Mixer (0/1)
13 BOOL 1 Ldac Right Mixer (0/1)
14 INT 1 Mic Input Mux (0->15)
15 INT 1 ADC Input Mux (0->15)
*/
/* Mixer control gain and route values */
#define MIXER_ABE_GAIN_0DB 60
#define MIXER_PLAYBACK_HS_DAC "HS DAC"
#define MIXER_PLAYBACK_HF_DAC "HF DAC"
#define MIXER_MAIN_MIC "Main Mic"
#define MIXER_SUB_MIC "Sub Mic"
#define MIXER_HS_MIC "Headset Mic"
#define MIXER_AMIC0 "AMic0"
#define MIXER_AMIC1 "AMic1"
#define MIXER_BT_LEFT "BT Left"
#define MIXER_BT_RIGHT "BT Right"
#define MIXER_450HZ_HIGH_PASS "450Hz High-pass"
#define MIXER_FLAT_RESPONSE "Flat response"
#define MIXER_4KHZ_LPF_0DB "4Khz LPF 0dB"
/* ALSA cards for A10 */
#define CARD_A10_ABE 0
#define CARD_A10_HDMI 1
#define CARD_DEFAULT CARD_A10_ABE
/* ALSA ports for A10 */
#define PORT_MM 0
#define PORT_MM2_UL 0
#define PORT_VX 2
#define PORT_MODEM 5
#define PORT_MM_LP 6
#define PORT_SPDIF 9
#define PORT_HDMI 0
/* EXTERNAL USB DAC */
#define OUT_CARD_CID_PROPERTY "usb.audio.out.device"
#define OUT_CARD_FREQ_PROPERTY "usb.audio.out.freq"
/* First device after HDMI is default */
#define OUT_CARD_CID "pcmC2D0p"
#define CAP_CARD_CID_PROPERTY "usb.audio.cap.device"
/* Internal MIC is default */
#define CAP_CARD_CID "pcmC0D0c"
/* constraint imposed by ABE: all period sizes must be multiples of 24 */
#define ABE_BASE_FRAME_COUNT 24
/* number of base blocks in a short period (low latency) */
#define SHORT_PERIOD_MULTIPLIER 80 /* 40 ms */ //ex.44 /* 22 ms */
/* number of frames per short period (low latency) */
#define SHORT_PERIOD_SIZE (ABE_BASE_FRAME_COUNT * SHORT_PERIOD_MULTIPLIER)
/* number of short periods in a long period (low power) */
#define LONG_PERIOD_MULTIPLIER 6 /* 240 ms */ //ex.14 /* 308 ms */
/* number of frames per long period (low power) */
#define LONG_PERIOD_SIZE (SHORT_PERIOD_SIZE * LONG_PERIOD_MULTIPLIER)
/* number of periods for low power playback */
#define PLAYBACK_LONG_PERIOD_COUNT 2
/* number of pseudo periods for low latency playback */
#define PLAYBACK_SHORT_PERIOD_COUNT 4
/* minimum sleep time in out_write() when write threshold is not reached */
#define MIN_WRITE_SLEEP_US 5000
// add for capture
#define CAPTURE_PERIOD_SIZE 4096 // can not less than 8192
/* number of periods for capture */
#define CAPTURE_PERIOD_COUNT 4 //ex.2
#define RESAMPLER_BUFFER_FRAMES (SHORT_PERIOD_SIZE * 2)
#define RESAMPLER_BUFFER_SIZE (4 * RESAMPLER_BUFFER_FRAMES)
#define DEFAULT_OUT_SAMPLING_RATE 44100
/* sampling rate when using MM low power port */
#define MM_LOW_POWER_SAMPLING_RATE 44100
/* sampling rate when using MM full power port */
#define MM_FULL_POWER_SAMPLING_RATE 48000
/* sampling rate when using VX port for narrow band */
#define VX_NB_SAMPLING_RATE 8000
/* sampling rate when using VX port for wide band */
#define VX_WB_SAMPLING_RATE 16000
/* conversions from dB to ABE and codec gains */
#define DB_TO_ABE_GAIN(x) ((x) + MIXER_ABE_GAIN_0DB)
#define DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(x) (((x) + 6) / 6)
#define DB_TO_CAPTURE_VOLUME(x) (((x) - 6) / 6)
#define DB_TO_HEADSET_VOLUME(x) (((x) + 30) / 2)
#define DB_TO_SPEAKER_VOLUME(x) (((x) + 52) / 2)
#define DB_TO_EARPIECE_VOLUME(x) (((x) + 24) / 2)
/* conversions from codec and ABE gains to dB */
#define DB_FROM_SPEAKER_VOLUME(x) ((x) * 2 - 52)
/* use-case specific mic volumes, all in dB */
#define CAPTURE_MAIN_MIC_VOLUME 16
#define CAPTURE_SUB_MIC_VOLUME 18
#define CAPTURE_HEADSET_MIC_VOLUME 12
#define VOICE_RECOGNITION_MAIN_MIC_VOLUME 5
#define VOICE_RECOGNITION_SUB_MIC_VOLUME 18
#define VOICE_RECOGNITION_HEADSET_MIC_VOLUME 14
#define CAMCORDER_MAIN_MIC_VOLUME 13
#define CAMCORDER_SUB_MIC_VOLUME 10
#define CAMCORDER_HEADSET_MIC_VOLUME 12
#define VOIP_MAIN_MIC_VOLUME 13
#define VOIP_SUB_MIC_VOLUME 20
#define VOIP_HEADSET_MIC_VOLUME 12
#define VOICE_CALL_MAIN_MIC_VOLUME 0
#define VOICE_CALL_SUB_MIC_VOLUME_MAGURO -4
#define VOICE_CALL_SUB_MIC_VOLUME_TORO -2
#define VOICE_CALL_HEADSET_MIC_VOLUME 8
/* use-case specific output volumes */
#define NORMAL_SPEAKER_VOLUME_TORO 4
#define NORMAL_SPEAKER_VOLUME_MAGURO 2
#define NORMAL_HEADSET_VOLUME_TORO -12
#define NORMAL_HEADSET_VOLUME_MAGURO -12
#define NORMAL_HEADPHONE_VOLUME_TORO -6 /* allow louder output for headphones */
#define NORMAL_HEADPHONE_VOLUME_MAGURO -6
#define NORMAL_EARPIECE_VOLUME_TORO -2
#define NORMAL_EARPIECE_VOLUME_MAGURO -2
#define VOICE_CALL_SPEAKER_VOLUME_TORO 9
#define VOICE_CALL_SPEAKER_VOLUME_MAGURO 6
#define VOICE_CALL_HEADSET_VOLUME_TORO -6
#define VOICE_CALL_HEADSET_VOLUME_MAGURO 0
#define VOICE_CALL_EARPIECE_VOLUME_TORO 2
#define VOICE_CALL_EARPIECE_VOLUME_MAGURO 6
#define VOIP_SPEAKER_VOLUME_TORO 9
#define VOIP_SPEAKER_VOLUME_MAGURO 7
#define VOIP_HEADSET_VOLUME_TORO -6
#define VOIP_HEADSET_VOLUME_MAGURO -6
#define VOIP_EARPIECE_VOLUME_TORO 6
#define VOIP_EARPIECE_VOLUME_MAGURO 6
#define HEADPHONE_VOLUME_TTY -2
#define RINGTONE_HEADSET_VOLUME_OFFSET -14
/* product-specific defines */
#define PRODUCT_DEVICE_PROPERTY "ro.product.device"
#define PRODUCT_NAME_PROPERTY "ro.product.name"
#define PRODUCT_DEVICE_TORO "toro"
#define PRODUCT_NAME_YAKJU "yakju"
enum tty_modes {
TTY_MODE_OFF,
TTY_MODE_VCO,
TTY_MODE_HCO,
TTY_MODE_FULL
};
struct pcm_config pcm_config_mm = {
.channels = 2,
.rate = MM_FULL_POWER_SAMPLING_RATE,
// .period_size = LONG_PERIOD_SIZE,
// .period_count = PLAYBACK_LONG_PERIOD_COUNT,
.period_size = SHORT_PERIOD_SIZE,
.period_count = PLAYBACK_SHORT_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
};
struct pcm_config pcm_config_mm_ul = {
.channels = 2,
.rate = MM_FULL_POWER_SAMPLING_RATE,
.period_size = 1024,
.period_count = CAPTURE_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
};
struct pcm_config pcm_config_vx = {
.channels = 2,
.rate = VX_NB_SAMPLING_RATE,
.period_size = 160,
.period_count = 2,
.format = PCM_FORMAT_S16_LE,
};
#define MIN(x, y) ((x) > (y) ? (y) : (x))
struct route_setting
{
char *ctl_name;
int intval;
char *strval;
};
/* These are values that never change */
struct route_setting defaults[] = {
/* general */
{
.ctl_name = MIXER_DL2_LEFT_EQUALIZER,
.strval = MIXER_450HZ_HIGH_PASS,
},
{
.ctl_name = MIXER_DL2_RIGHT_EQUALIZER,
.strval = MIXER_450HZ_HIGH_PASS,
},
{
.ctl_name = MIXER_DL1_MEDIA_PLAYBACK_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = MIXER_DL2_MEDIA_PLAYBACK_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = MIXER_DL1_VOICE_PLAYBACK_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = MIXER_DL2_VOICE_PLAYBACK_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = MIXER_SDT_DL_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = MIXER_AUDUL_VOICE_UL_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = MIXER_CAPTURE_PREAMPLIFIER_VOLUME,
.intval = DB_TO_CAPTURE_PREAMPLIFIER_VOLUME(0),
},
{
.ctl_name = MIXER_CAPTURE_VOLUME,
.intval = DB_TO_CAPTURE_VOLUME(30),
},
{
.ctl_name = MIXER_SDT_UL_VOLUME,
.intval = MIXER_ABE_GAIN_0DB - 17,
},
{
.ctl_name = MIXER_SIDETONE_MIXER_CAPTURE,
.intval = 0,
},
/* headset */
{
.ctl_name = MIXER_SIDETONE_MIXER_PLAYBACK,
.intval = 1,
},
{
.ctl_name = MIXER_DL1_PDM_SWITCH,
.intval = 1,
},
/* bt */
{
.ctl_name = MIXER_BT_UL_VOLUME,
.intval = MIXER_ABE_GAIN_0DB,
},
{
.ctl_name = NULL,
},
};
struct route_setting hf_output[] = {
{
.ctl_name = MIXER_HF_LEFT_PLAYBACK,
.strval = MIXER_PLAYBACK_HF_DAC,
},
{
.ctl_name = MIXER_HF_RIGHT_PLAYBACK,
.strval = MIXER_PLAYBACK_HF_DAC,
},
{
.ctl_name = NULL,
},
};
struct route_setting hs_output[] = {
{
.ctl_name = MIXER_HS_LEFT_PLAYBACK,
.strval = MIXER_PLAYBACK_HS_DAC,
},
{
.ctl_name = MIXER_HS_RIGHT_PLAYBACK,
.strval = MIXER_PLAYBACK_HS_DAC,
},
{
.ctl_name = NULL,
},
};
/* MM UL front-end paths */
struct route_setting mm_ul2_bt[] = {
{
.ctl_name = MIXER_MUX_UL10,
.strval = MIXER_BT_LEFT,
},
{
.ctl_name = MIXER_MUX_UL11,
.strval = MIXER_BT_LEFT,
},
{
.ctl_name = NULL,
},
};
struct route_setting mm_ul2_amic_left[] = {
{
.ctl_name = MIXER_MUX_UL10,
.strval = MIXER_AMIC0,
},
{
.ctl_name = MIXER_MUX_UL11,
.strval = MIXER_AMIC0,
},
{
.ctl_name = NULL,
},
};
struct route_setting mm_ul2_amic_right[] = {
{
.ctl_name = MIXER_MUX_UL10,
.strval = MIXER_AMIC1,
},
{
.ctl_name = MIXER_MUX_UL11,
.strval = MIXER_AMIC1,
},
{
.ctl_name = NULL,
},
};
/* VX UL front-end paths */
struct route_setting vx_ul_amic_left[] = {
{
.ctl_name = MIXER_MUX_VX0,
.strval = MIXER_AMIC0,
},
{
.ctl_name = MIXER_MUX_VX1,
.strval = MIXER_AMIC0,
},
{
.ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE,
.intval = 1,
},
{
.ctl_name = NULL,
},
};
struct route_setting vx_ul_amic_right[] = {
{
.ctl_name = MIXER_MUX_VX0,
.strval = MIXER_AMIC1,
},
{
.ctl_name = MIXER_MUX_VX1,
.strval = MIXER_AMIC1,
},
{
.ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE,
.intval = 1,
},
{
.ctl_name = NULL,
},
};
struct route_setting vx_ul_bt[] = {
{
.ctl_name = MIXER_MUX_VX0,
.strval = MIXER_BT_LEFT,
},
{
.ctl_name = MIXER_MUX_VX1,
.strval = MIXER_BT_LEFT,
},
{
.ctl_name = MIXER_VOICE_CAPTURE_MIXER_CAPTURE,
.intval = 1,
},
{
.ctl_name = NULL,
},
};
struct mixer_ctls
{
struct mixer_ctl *dl1_eq;
struct mixer_ctl *mm_dl2_volume;
struct mixer_ctl *vx_dl2_volume;
struct mixer_ctl *mm_dl1;
struct mixer_ctl *mm_dl2;
struct mixer_ctl *vx_dl1;
struct mixer_ctl *vx_dl2;
struct mixer_ctl *earpiece_enable;
struct mixer_ctl *dl2_mono;
struct mixer_ctl *dl1_headset;
struct mixer_ctl *dl1_bt;
struct mixer_ctl *left_capture;
struct mixer_ctl *right_capture;
struct mixer_ctl *amic_ul_volume;
struct mixer_ctl *voice_ul_volume;
struct mixer_ctl *sidetone_capture;
struct mixer_ctl *headset_volume;
struct mixer_ctl *speaker_volume;
struct mixer_ctl *earpiece_volume;
};
struct sun4i_audio_device {
struct audio_hw_device hw_device;
pthread_mutex_t lock; /* see note below on mutex acquisition order */
struct mixer *mixer;
struct mixer_ctls mixer_ctls;
int mode;
int devices;
struct pcm *pcm_modem_dl;
struct pcm *pcm_modem_ul;
int in_call;
float voice_volume;
struct sun4i_stream_in *active_input;
struct sun4i_stream_out *active_output;
bool mic_mute;
int tty_mode;
struct echo_reference_itfe *echo_reference;
bool bluetooth_nrec;
bool device_is_toro;
int wb_amr;
bool low_power;
#ifdef __ENABLE_RIL
/* RIL */
struct ril_handle ril;
#endif
};
struct sun4i_stream_out {
struct audio_stream_out stream;
pthread_mutex_t lock; /* see note below on mutex acquisition order */
struct pcm_config config;
struct pcm *pcm;
struct resampler_itfe *resampler;
char *buffer;
int standby;
struct echo_reference_itfe *echo_reference;
struct sun4i_audio_device *dev;
int write_threshold;
bool low_power;
};
#define MAX_PREPROCESSORS 3 /* maximum one AGC + one NS + one AEC per input stream */
struct sun4i_stream_in {
struct audio_stream_in stream;
pthread_mutex_t lock; /* see note below on mutex acquisition order */
struct pcm_config config;
struct pcm *pcm;
int device;
struct resampler_itfe *resampler;
struct resampler_buffer_provider buf_provider;
int16_t *buffer;
size_t frames_in;
unsigned int requested_rate;
int standby;
int source;
struct echo_reference_itfe *echo_reference;
bool need_echo_reference;
effect_handle_t preprocessors[MAX_PREPROCESSORS];
int num_preprocessors;
int16_t *proc_buf;
size_t proc_buf_size;
size_t proc_frames_in;
int16_t *ref_buf;
size_t ref_buf_size;
size_t ref_frames_in;
int read_status;
struct sun4i_audio_device *dev;
};
/**
* NOTE: when multiple mutexes have to be acquired, always respect the following order:
* hw device > in stream > out stream
*/
static void select_output_device(struct sun4i_audio_device *adev);
static void select_input_device(struct sun4i_audio_device *adev);
static int adev_set_voice_volume(struct audio_hw_device *dev, float volume);
static int do_input_standby(struct sun4i_stream_in *in);
static int do_output_standby(struct sun4i_stream_out *out);
/* Returns true on devices that are toro, false otherwise */
static int is_device_toro(void)
{
char property[PROPERTY_VALUE_MAX];
property_get(PRODUCT_DEVICE_PROPERTY, property, PRODUCT_DEVICE_TORO);
/* return true if the property matches the given value */
return strcmp(property, PRODUCT_DEVICE_TORO) == 0;
}
/* Returns true for external DAC, false otherwise */
static int is_device_usb_dac(void)
{
char property[PROPERTY_VALUE_MAX];
property_get(OUT_CARD_CID_PROPERTY, property, OUT_CARD_CID);
struct stat info;
char path[18]="/dev/snd/";
strcat(path, property);
int ret = stat(path, &info);
LOGV("# is_device_usb_dac, dev: %s, ret: %d", property, ret);
return(ret == -1 ? 0 : 1);
}
/* Returns true for external DAC, false otherwise */
static int is_device_usb_cap(void)
{
char property[PROPERTY_VALUE_MAX];
property_get(CAP_CARD_CID_PROPERTY, property, CAP_CARD_CID);
struct stat info;
char path[18]="/dev/snd/";
strcat(path, property);
// internal MIC case
if (strcmp(property, "pcmC0D0c") == 0) {
LOGV("# internal mic input selected");
return(0);
} else {
int ret = stat(path, &info);
LOGV("# is_device_usb_cap, dev: %s, ret: %d", property, ret);
return(ret == -1 ? 0 : 1);
}
}
/* The enable flag when 0 makes the assumption that enums are disabled by
* "Off" and integers/booleans by 0 */
static int set_route_by_array(struct mixer *mixer, struct route_setting *route,
int enable)
{
struct mixer_ctl *ctl;
unsigned int i, j;
/* Go through the route array and set each value */
i = 0;
while (route[i].ctl_name) {
ctl = mixer_get_ctl_by_name(mixer, route[i].ctl_name);
if (!ctl)
return -EINVAL;
if (route[i].strval) {
if (enable)
mixer_ctl_set_enum_by_string(ctl, route[i].strval);
else
mixer_ctl_set_enum_by_string(ctl, "Off");
} else {
/* This ensures multiple (i.e. stereo) values are set jointly */
for (j = 0; j < mixer_ctl_get_num_values(ctl); j++) {
if (enable)
mixer_ctl_set_value(ctl, j, route[i].intval);
else
mixer_ctl_set_value(ctl, j, 0);
}
}
i++;
}
return 0;
}
static int start_call(struct sun4i_audio_device *adev)
{
LOGE("Opening modem PCMs");
pcm_config_vx.rate = adev->wb_amr ? VX_WB_SAMPLING_RATE : VX_NB_SAMPLING_RATE;
/* Open modem PCM channels */
if (adev->pcm_modem_dl == NULL) {
adev->pcm_modem_dl = pcm_open(0, PORT_MODEM, PCM_OUT, &pcm_config_vx);
if (!pcm_is_ready(adev->pcm_modem_dl)) {
LOGE("cannot open PCM modem DL stream: %s", pcm_get_error(adev->pcm_modem_dl));
goto err_open_dl;
}
}
if (adev->pcm_modem_ul == NULL) {
adev->pcm_modem_ul = pcm_open(0, PORT_MODEM, PCM_IN, &pcm_config_vx);
if (!pcm_is_ready(adev->pcm_modem_ul)) {
LOGE("cannot open PCM modem UL stream: %s", pcm_get_error(adev->pcm_modem_ul));
goto err_open_ul;
}
}
pcm_start(adev->pcm_modem_dl);
pcm_start(adev->pcm_modem_ul);
return 0;
err_open_ul:
pcm_close(adev->pcm_modem_ul);
adev->pcm_modem_ul = NULL;
err_open_dl:
pcm_close(adev->pcm_modem_dl);
adev->pcm_modem_dl = NULL;
return -ENOMEM;
}
static void end_call(struct sun4i_audio_device *adev)
{
LOGE("Closing modem PCMs");
pcm_stop(adev->pcm_modem_dl);
pcm_stop(adev->pcm_modem_ul);
pcm_close(adev->pcm_modem_dl);
pcm_close(adev->pcm_modem_ul);
adev->pcm_modem_dl = NULL;
adev->pcm_modem_ul = NULL;
}
static void set_eq_filter(struct sun4i_audio_device *adev)
{
/* DL1_EQ can't be used for bt */
int dl1_eq_applicable = adev->devices & (AUDIO_DEVICE_OUT_WIRED_HEADSET |
AUDIO_DEVICE_OUT_WIRED_HEADPHONE | AUDIO_DEVICE_OUT_EARPIECE);
/* 4Khz LPF is used only in NB-AMR voicecall */
if ((adev->mode == AUDIO_MODE_IN_CALL) && dl1_eq_applicable &&
(adev->tty_mode == TTY_MODE_OFF) && !adev->wb_amr)
mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_4KHZ_LPF_0DB);
else
mixer_ctl_set_enum_by_string(adev->mixer_ctls.dl1_eq, MIXER_FLAT_RESPONSE);
}
void audio_set_wb_amr_callback(void *data, int enable)
{
struct sun4i_audio_device *adev = (struct sun4i_audio_device *)data;
pthread_mutex_lock(&adev->lock);
if (adev->wb_amr != enable) {
adev->wb_amr = enable;
/* reopen the modem PCMs at the new rate */
if (adev->in_call) {
end_call(adev);
set_eq_filter(adev);
start_call(adev);
}
}
pthread_mutex_unlock(&adev->lock);
}
static void set_incall_device(struct sun4i_audio_device *adev)
{
int device_type;
switch(adev->devices & AUDIO_DEVICE_OUT_ALL) {
case AUDIO_DEVICE_OUT_EARPIECE:
device_type = SOUND_AUDIO_PATH_HANDSET;
break;
case AUDIO_DEVICE_OUT_SPEAKER:
case AUDIO_DEVICE_OUT_AUX_DIGITAL:
device_type = SOUND_AUDIO_PATH_SPEAKER;
break;
case AUDIO_DEVICE_OUT_WIRED_HEADSET:
device_type = SOUND_AUDIO_PATH_HEADSET;
break;
case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
device_type = SOUND_AUDIO_PATH_HEADPHONE;
break;
case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
if (adev->bluetooth_nrec)
device_type = SOUND_AUDIO_PATH_BLUETOOTH;
else
device_type = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR;
break;
default:
device_type = SOUND_AUDIO_PATH_HANDSET;
break;
}
#ifdef __ENABLE_RIL
/* if output device isn't supported, open modem side to handset by default */
ril_set_call_audio_path(&adev->ril, device_type);
#endif
}
static void set_input_volumes(struct sun4i_audio_device *adev, int main_mic_on,
int headset_mic_on, int sub_mic_on)
{
unsigned int channel;
int volume = MIXER_ABE_GAIN_0DB;
if (adev->mode == AUDIO_MODE_IN_CALL) {
int sub_mic_volume = is_device_toro() ? VOICE_CALL_SUB_MIC_VOLUME_TORO :
VOICE_CALL_SUB_MIC_VOLUME_MAGURO;
/* special case: don't look at input source for IN_CALL state */
volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_CALL_MAIN_MIC_VOLUME :
(headset_mic_on ? VOICE_CALL_HEADSET_MIC_VOLUME :
(sub_mic_on ? sub_mic_volume : 0)));
} else if (adev->active_input) {
/* determine input volume by use case */
switch (adev->active_input->source) {
case AUDIO_SOURCE_MIC: /* general capture */
volume = DB_TO_ABE_GAIN(main_mic_on ? CAPTURE_MAIN_MIC_VOLUME :
(headset_mic_on ? CAPTURE_HEADSET_MIC_VOLUME :
(sub_mic_on ? CAPTURE_SUB_MIC_VOLUME : 0)));
break;
case AUDIO_SOURCE_CAMCORDER:
volume = DB_TO_ABE_GAIN(main_mic_on ? CAMCORDER_MAIN_MIC_VOLUME :
(headset_mic_on ? CAMCORDER_HEADSET_MIC_VOLUME :
(sub_mic_on ? CAMCORDER_SUB_MIC_VOLUME : 0)));
break;
case AUDIO_SOURCE_VOICE_RECOGNITION:
volume = DB_TO_ABE_GAIN(main_mic_on ? VOICE_RECOGNITION_MAIN_MIC_VOLUME :
(headset_mic_on ? VOICE_RECOGNITION_HEADSET_MIC_VOLUME :
(sub_mic_on ? VOICE_RECOGNITION_SUB_MIC_VOLUME : 0)));
break;
case AUDIO_SOURCE_VOICE_COMMUNICATION: /* VoIP */
volume = DB_TO_ABE_GAIN(main_mic_on ? VOIP_MAIN_MIC_VOLUME :
(headset_mic_on ? VOIP_HEADSET_MIC_VOLUME :
(sub_mic_on ? VOIP_SUB_MIC_VOLUME : 0)));
break;
default:
/* nothing to do */
break;
}
}
for (channel = 0; channel < 2; channel++)
mixer_ctl_set_value(adev->mixer_ctls.amic_ul_volume, channel, volume);
}
static void set_output_volumes(struct sun4i_audio_device *adev, bool tty_volume)
{
unsigned int channel;
int speaker_volume;
int headset_volume;
int earpiece_volume;
bool toro = adev->device_is_toro;
int headphone_on = adev->devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
int speaker_on = adev->devices & AUDIO_DEVICE_OUT_SPEAKER;
int speaker_volume_overrange = MIXER_ABE_GAIN_0DB;
int speaker_max_db =
DB_FROM_SPEAKER_VOLUME(mixer_ctl_get_range_max(adev->mixer_ctls.speaker_volume));
struct mixer_ctl *mixer_ctl_overrange = adev->mixer_ctls.mm_dl2_volume;
if (adev->mode == AUDIO_MODE_IN_CALL) {
/* Voice call */
speaker_volume = toro ? VOICE_CALL_SPEAKER_VOLUME_TORO :
VOICE_CALL_SPEAKER_VOLUME_MAGURO;
headset_volume = toro ? VOICE_CALL_HEADSET_VOLUME_TORO :
VOICE_CALL_HEADSET_VOLUME_MAGURO;
earpiece_volume = toro ? VOICE_CALL_EARPIECE_VOLUME_TORO :
VOICE_CALL_EARPIECE_VOLUME_MAGURO;
mixer_ctl_overrange = adev->mixer_ctls.vx_dl2_volume;
} else if (adev->mode == AUDIO_MODE_IN_COMMUNICATION) {
/* VoIP */
speaker_volume = toro ? VOIP_SPEAKER_VOLUME_TORO :
VOIP_SPEAKER_VOLUME_MAGURO;
headset_volume = toro ? VOIP_HEADSET_VOLUME_TORO :
VOIP_HEADSET_VOLUME_MAGURO;
earpiece_volume = toro ? VOIP_EARPIECE_VOLUME_TORO :
VOIP_EARPIECE_VOLUME_MAGURO;
} else {
/* Media */
speaker_volume = toro ? NORMAL_SPEAKER_VOLUME_TORO :
NORMAL_SPEAKER_VOLUME_MAGURO;
if (headphone_on)
headset_volume = toro ? NORMAL_HEADPHONE_VOLUME_TORO :
NORMAL_HEADPHONE_VOLUME_MAGURO;
else
headset_volume = toro ? NORMAL_HEADSET_VOLUME_TORO :
NORMAL_HEADSET_VOLUME_MAGURO;
earpiece_volume = toro ? NORMAL_EARPIECE_VOLUME_TORO :
NORMAL_EARPIECE_VOLUME_MAGURO;
}
if (tty_volume)
headset_volume = HEADPHONE_VOLUME_TTY;
else if (adev->mode == AUDIO_MODE_RINGTONE)
headset_volume += RINGTONE_HEADSET_VOLUME_OFFSET;
/* If we have run out of range in the codec (analog) speaker volume,
we have to apply the remainder of the dB increase to the DL2
media/voice mixer volume, which is a digital gain */
if (speaker_volume > speaker_max_db) {
speaker_volume_overrange += (speaker_volume - speaker_max_db);
speaker_volume = speaker_max_db;
}
for (channel = 0; channel < 2; channel++) {
mixer_ctl_set_value(adev->mixer_ctls.speaker_volume, channel,
DB_TO_SPEAKER_VOLUME(speaker_volume));
mixer_ctl_set_value(adev->mixer_ctls.headset_volume, channel,
DB_TO_HEADSET_VOLUME(headset_volume));
}
if (speaker_on)
mixer_ctl_set_value(mixer_ctl_overrange, 0, speaker_volume_overrange);
else
mixer_ctl_set_value(mixer_ctl_overrange, 0, MIXER_ABE_GAIN_0DB);
mixer_ctl_set_value(adev->mixer_ctls.earpiece_volume, 0,
DB_TO_EARPIECE_VOLUME(earpiece_volume));
}
static void force_all_standby(struct sun4i_audio_device *adev)
{
struct sun4i_stream_in *in;
struct sun4i_stream_out *out;
if (adev->active_output) {
out = adev->active_output;
pthread_mutex_lock(&out->lock);
do_output_standby(out);
pthread_mutex_unlock(&out->lock);
}
if (adev->active_input) {
in = adev->active_input;
pthread_mutex_lock(&in->lock);
do_input_standby(in);
pthread_mutex_unlock(&in->lock);
}
}
static void select_mode(struct sun4i_audio_device *adev)
{
if (adev->mode == AUDIO_MODE_IN_CALL) {
LOGE("Entering IN_CALL state, in_call=%d", adev->in_call);
if (!adev->in_call) {
force_all_standby(adev);
/* force earpiece route for in call state if speaker is the
only currently selected route. This prevents having to tear
down the modem PCMs to change route from speaker to earpiece
after the ringtone is played, but doesn't cause a route
change if a headset or bt device is already connected. If
speaker is not the only thing active, just remove it from
the route. We'll assume it'll never be used initally during
a call. This works because we're sure that the audio policy
manager will update the output device after the audio mode
change, even if the device selection did not change. */
if ((adev->devices & AUDIO_DEVICE_OUT_ALL) == AUDIO_DEVICE_OUT_SPEAKER)
adev->devices = AUDIO_DEVICE_OUT_EARPIECE |
AUDIO_DEVICE_IN_BUILTIN_MIC;
else
adev->devices &= ~AUDIO_DEVICE_OUT_SPEAKER;
select_output_device(adev);
start_call(adev);
#ifdef __ENABLE_RIL
ril_set_call_clock_sync(&adev->ril, SOUND_CLOCK_START);
#endif
adev_set_voice_volume(&adev->hw_device, adev->voice_volume);
adev->in_call = 1;
}
} else {
LOGE("Leaving IN_CALL state, in_call=%d, mode=%d",
adev->in_call, adev->mode);
if (adev->in_call) {
adev->in_call = 0;
end_call(adev);
force_all_standby(adev);
select_output_device(adev);
select_input_device(adev);
}
}
}
static void select_output_device(struct sun4i_audio_device *adev)
{
int headset_on;
int headphone_on;
int speaker_on;
int earpiece_on;
int bt_on;
int dl1_on;
int sidetone_capture_on = 0;
bool tty_volume = false;
unsigned int channel;
/* Mute VX_UL to avoid pop noises in the tx path
* during call before switch changes.
*/
if (adev->mode == AUDIO_MODE_IN_CALL) {
for (channel = 0; channel < 2; channel++)
mixer_ctl_set_value(adev->mixer_ctls.voice_ul_volume,
channel, 0);
}