forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmic.c
More file actions
1472 lines (1280 loc) · 39.7 KB
/
dmic.c
File metadata and controls
1472 lines (1280 loc) · 39.7 KB
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) 2017, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Intel Corporation nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
*/
#include <sof/stream.h>
#include <sof/dmic.h>
#include <sof/interrupt.h>
#include <sof/math/numbers.h>
#include <sof/audio/format.h>
#if defined DMIC_HW_VERSION
#include <sof/audio/coefficients/pdm_decim/pdm_decim_table.h>
#if defined MODULE_TEST
#include <stdio.h>
#endif
#define DMIC_MAX_MODES 50
/* HW FIR pipeline needs 5 additional cycles per channel for internal
* operations. This is used in MAX filter length check.
*/
#define DMIC_FIR_PIPELINE_OVERHEAD 5
struct decim_modes {
int16_t clkdiv[DMIC_MAX_MODES];
int16_t mcic[DMIC_MAX_MODES];
int16_t mfir[DMIC_MAX_MODES];
int num_of_modes;
};
struct matched_modes {
int16_t clkdiv[DMIC_MAX_MODES];
int16_t mcic[DMIC_MAX_MODES];
int16_t mfir_a[DMIC_MAX_MODES];
int16_t mfir_b[DMIC_MAX_MODES];
int num_of_modes;
};
struct dmic_configuration {
struct pdm_decim *fir_a;
struct pdm_decim *fir_b;
int clkdiv;
int mcic;
int mfir_a;
int mfir_b;
int cic_shift;
int fir_a_shift;
int fir_b_shift;
int fir_a_length;
int fir_b_length;
int32_t fir_a_scale;
int32_t fir_b_scale;
};
struct pdm_controllers_configuration {
uint32_t cic_control;
uint32_t cic_config;
uint32_t mic_control;
uint32_t fir_control_a;
uint32_t fir_config_a;
uint32_t dc_offset_left_a;
uint32_t dc_offset_right_a;
uint32_t out_gain_left_a;
uint32_t out_gain_right_a;
uint32_t fir_control_b;
uint32_t fir_config_b;
uint32_t dc_offset_left_b;
uint32_t dc_offset_right_b;
uint32_t out_gain_left_b;
uint32_t out_gain_right_b;
};
/* Configuration ABI version, increment if not compatible with previous
* version.
*/
#define DMIC_IPC_VERSION 1
/* Minimum OSR is always applied for 48 kHz and less sample rates */
#define DMIC_MIN_OSR 50
/* These are used as guideline for configuring > 48 kHz sample rates. The
* minimum OSR can be relaxed down to 40 (use 3.84 MHz clock for 96 kHz).
*/
#define DMIC_HIGH_RATE_MIN_FS 64000
#define DMIC_HIGH_RATE_OSR_MIN 40
/* Used for scaling FIR coeffcients for HW */
#define DMIC_HW_FIR_COEF_MAX ((1 << (DMIC_HW_BITS_FIR_COEF - 1)) - 1)
#define DMIC_HW_FIR_COEF_Q (DMIC_HW_BITS_FIR_COEF - 1)
/* Internal precision in gains computation, e.g. Q4.28 in int32_t */
#define DMIC_FIR_SCALE_Q 28
/* Used in unmute ramp values calculation */
#define DMIC_HW_FIR_GAIN_MAX ((1 << (DMIC_HW_BITS_FIR_GAIN - 1)) - 1)
/* Hardwired log ramp parameters. The first value is the initial logarithmic
* gain and the second value is the multiplier for gain to achieve the linear
* decibels change over time. Currently the coefficient GM needs to be
* calculated manually. The 300 ms ramp should ensure clean sounding start with
* any microphone. However it is likely unnecessarily long for machine hearing.
* TODO: Add ramp characteristic passing via topology.
*/
#define LOGRAMP_GI 33954 /* -90 dB, Q2.30*/
#define LOGRAMP_GM 16959 /* Gives 300 ms ramp for -90..0 dB, Q2.14 */
/* tracing */
#define trace_dmic(__e) trace_event(TRACE_CLASS_DMIC, __e)
#define trace_dmic_error(__e) trace_error(TRACE_CLASS_DMIC, __e)
#define tracev_dmic(__e) tracev_event(TRACE_CLASS_DMIC, __e)
/* Base addresses (in PDM scope) of 2ch PDM controllers and coefficient RAM. */
static const uint32_t base[4] = {PDM0, PDM1, PDM2, PDM3};
static const uint32_t coef_base_a[4] = {PDM0_COEFFICIENT_A, PDM1_COEFFICIENT_A,
PDM2_COEFFICIENT_A, PDM3_COEFFICIENT_A};
static const uint32_t coef_base_b[4] = {PDM0_COEFFICIENT_B, PDM1_COEFFICIENT_B,
PDM2_COEFFICIENT_B, PDM3_COEFFICIENT_B};
#if defined MODULE_TEST
#define IO_BYTES_GLOBAL (PDM0 - OUTCONTROL0)
#define IO_BYTES_MIDDLE (PDM1 - PDM0)
#define IO_BYTES_LAST (PDM0_COEFFICIENT_B + PDM_COEF_RAM_B_LENGTH - PDM0)
#define IO_BYTES (((DMIC_HW_CONTROLLERS) - 1) * (IO_BYTES_MIDDLE) \
+ (IO_BYTES_LAST) + (IO_BYTES_GLOBAL))
#define IO_LENGTH ((IO_BYTES) >> 2)
static uint32_t dmic_io[IO_LENGTH];
static void dmic_write(struct dai *dai, uint32_t reg, uint32_t value)
{
printf("W %04x %08x\n", reg, value);
dmic_io[reg >> 2] = value;
}
static uint32_t dmic_read(struct dai *dai, uint32_t reg)
{
uint32_t value = dmic_io[reg >> 2];
printf("R %04x %08x\n", reg, value);
return value;
}
static void dmic_update_bits(struct dai *dai, uint32_t reg, uint32_t mask,
uint32_t value)
{
uint32_t new_value;
uint32_t old_value = dmic_io[reg >> 2];
new_value = (old_value & (~mask)) | value;
dmic_io[reg >> 2] = new_value;
printf("W %04x %08x\n", reg, new_value);
}
#else
static void dmic_write(struct dai *dai, uint32_t reg, uint32_t value)
{
io_reg_write(dai_base(dai) + reg, value);
}
static uint32_t dmic_read(struct dai *dai, uint32_t reg)
{
return io_reg_read(dai_base(dai) + reg);
}
static void dmic_update_bits(struct dai *dai, uint32_t reg, uint32_t mask,
uint32_t value)
{
io_reg_update_bits(dai_base(dai) + reg, mask, value);
}
#endif
/* this ramps volume changes over time */
static uint64_t dmic_work(void *data, uint64_t delay)
{
struct dai *dai = (struct dai *)data;
struct dmic_pdata *dmic = dai_get_drvdata(dai);
int32_t gval;
uint32_t val;
int i;
tracev_dmic("wrk");
spin_lock(&dmic->lock);
/* Increment gain with logaritmic step.
* Gain is Q2.30 and gain modifier is Q2.14.
*/
dmic->startcount++;
dmic->gain = Q_MULTSR_32X32((int64_t)dmic->gain,
LOGRAMP_GM, 30, 14, 30);
/* Gain is stored as Q2.30, while HW register is Q1.19 so shift
* the value right by 11.
*/
gval = dmic->gain >> 11;
/* Note that DMIC gain value zero has a special purpose. Value zero
* sets gain bypass mode in HW. Zero value will be applied after ramp
* is complete. It is because exact 1.0 gain is not possible with Q1.19.
*/
if (gval > DMIC_HW_FIR_GAIN_MAX)
gval = 0;
/* Write gain to registers */
for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
if (!dmic->enable[i])
continue;
if (dmic->startcount == DMIC_UNMUTE_CIC)
dmic_update_bits(dai, base[i] + CIC_CONTROL,
CIC_CONTROL_MIC_MUTE_BIT, 0);
if (dmic->startcount == DMIC_UNMUTE_FIR) {
if (dmic->fifo_a)
dmic_update_bits(dai, base[i] + FIR_CONTROL_A,
FIR_CONTROL_A_MUTE_BIT, 0);
if (dmic->fifo_b)
dmic_update_bits(dai, base[i] + FIR_CONTROL_B,
FIR_CONTROL_B_MUTE_BIT, 0);
}
if (dmic->fifo_a) {
val = OUT_GAIN_LEFT_A_GAIN(gval);
dmic_write(dai, base[i] + OUT_GAIN_LEFT_A, val);
dmic_write(dai, base[i] + OUT_GAIN_RIGHT_A, val);
}
if (dmic->fifo_b) {
val = OUT_GAIN_LEFT_B_GAIN(gval);
dmic_write(dai, base[i] + OUT_GAIN_LEFT_B, val);
dmic_write(dai, base[i] + OUT_GAIN_RIGHT_B, val);
}
}
spin_unlock(&dmic->lock);
if (gval)
return DMIC_UNMUTE_RAMP_US;
else
return 0;
}
/* This function returns a raw list of potential microphone clock and decimation
* modes for achieving requested sample rates. The search is constrained by
* decimation HW capabililies and setup parameters. The parameters such as
* microphone clock min/max and duty cycle requirements need be checked from
* used microphone component datasheet.
*/
static void find_modes(struct decim_modes *modes,
struct sof_ipc_dai_dmic_params *prm, uint32_t fs)
{
int clkdiv_min;
int clkdiv_max;
int clkdiv;
int c1;
int du_min;
int du_max;
int pdmclk;
int osr;
int mfir;
int mcic;
int ioclk_test;
int osr_min = DMIC_MIN_OSR;
int i = 0;
/* Defaults, empty result */
modes->num_of_modes = 0;
/* The FIFO is not requested if sample rate is set to zero. Just
* return in such case with num_of_modes as zero.
*/
if (fs == 0)
return;
/* Override DMIC_MIN_OSR for very high sample rates, use as minimum
* the nominal clock for the high rates.
*/
if (fs >= DMIC_HIGH_RATE_MIN_FS)
osr_min = DMIC_HIGH_RATE_OSR_MIN;
/* Check for sane pdm clock, min 100 kHz, max ioclk/2 */
if (prm->pdmclk_max < DMIC_HW_PDM_CLK_MIN ||
prm->pdmclk_max > DMIC_HW_IOCLK / 2) {
trace_dmic_error("pmx");
return;
}
if (prm->pdmclk_min < DMIC_HW_PDM_CLK_MIN ||
prm->pdmclk_min > prm->pdmclk_max) {
trace_dmic_error("pmn");
return;
}
/* Check for sane duty cycle */
if (prm->duty_min > prm->duty_max) {
trace_dmic_error("pdu");
return;
}
if (prm->duty_min < DMIC_HW_DUTY_MIN ||
prm->duty_min > DMIC_HW_DUTY_MAX) {
trace_dmic_error("pdn");
return;
}
if (prm->duty_max < DMIC_HW_DUTY_MIN ||
prm->duty_max > DMIC_HW_DUTY_MAX) {
trace_dmic_error("pdx");
return;
}
/* Min and max clock dividers */
clkdiv_min = ceil_divide(DMIC_HW_IOCLK, prm->pdmclk_max);
clkdiv_min = MAX(clkdiv_min, DMIC_HW_CIC_DECIM_MIN);
clkdiv_max = DMIC_HW_IOCLK / prm->pdmclk_min;
/* Loop possible clock dividers and check based on resulting
* oversampling ratio that CIC and FIR decimation ratios are
* feasible. The ratios need to be integers. Also the mic clock
* duty cycle need to be within limits.
*/
for (clkdiv = clkdiv_min; clkdiv <= clkdiv_max; clkdiv++) {
/* Calculate duty cycle for this clock divider. Note that
* odd dividers cause non-50% duty cycle.
*/
c1 = clkdiv >> 1;
du_min = 100 * c1 / clkdiv;
du_max = 100 - du_min;
/* Calculate PDM clock rate and oversampling ratio. */
pdmclk = DMIC_HW_IOCLK / clkdiv;
osr = pdmclk / fs;
/* Check that OSR constraints is met and clock duty cycle does
* not exceed microphone specification. If exceed proceed to
* next clkdiv.
*/
if (osr < osr_min || du_min < prm->duty_min ||
du_max > prm->duty_max)
continue;
/* Loop FIR decimation factors candidates. If the
* integer divided decimation factors and clock dividers
* as multiplied with sample rate match the IO clock
* rate the division was exact and such decimation mode
* is possible. Then check that CIC decimation constraints
* are met. The passed decimation modes are added to array.
*/
for (mfir = DMIC_HW_FIR_DECIM_MIN;
mfir <= DMIC_HW_FIR_DECIM_MAX; mfir++) {
mcic = osr / mfir;
ioclk_test = fs * mfir * mcic * clkdiv;
if (ioclk_test == DMIC_HW_IOCLK &&
mcic >= DMIC_HW_CIC_DECIM_MIN &&
mcic <= DMIC_HW_CIC_DECIM_MAX &&
i < DMIC_MAX_MODES) {
modes->clkdiv[i] = clkdiv;
modes->mcic[i] = mcic;
modes->mfir[i] = mfir;
i++;
modes->num_of_modes = i;
}
}
}
#if defined MODULE_TEST
printf("# Found %d modes\n", i);
#endif
}
/* The previous raw modes list contains sane configuration possibilities. When
* there is request for both FIFOs A and B operation this function returns
* list of compatible settings.
*/
static void match_modes(struct matched_modes *c, struct decim_modes *a,
struct decim_modes *b)
{
int16_t idx[DMIC_MAX_MODES];
int idx_length;
int i;
int n;
int m;
/* Check if previous search got results. */
c->num_of_modes = 0;
if (a->num_of_modes == 0 && b->num_of_modes == 0) {
/* Nothing to do */
return;
}
/* Ensure that num_of_modes is sane. */
if (a->num_of_modes > DMIC_MAX_MODES ||
b->num_of_modes > DMIC_MAX_MODES)
return;
/* Check for request only for FIFO A or B. In such case pass list for
* A or B as such.
*/
if (b->num_of_modes == 0) {
c->num_of_modes = a->num_of_modes;
for (i = 0; i < a->num_of_modes; i++) {
c->clkdiv[i] = a->clkdiv[i];
c->mcic[i] = a->mcic[i];
c->mfir_a[i] = a->mfir[i];
c->mfir_b[i] = 0; /* Mark FIR B as non-used */
}
return;
}
if (a->num_of_modes == 0) {
c->num_of_modes = b->num_of_modes;
for (i = 0; i < b->num_of_modes; i++) {
c->clkdiv[i] = b->clkdiv[i];
c->mcic[i] = b->mcic[i];
c->mfir_b[i] = b->mfir[i];
c->mfir_a[i] = 0; /* Mark FIR A as non-used */
}
return;
}
/* Merge a list of compatible modes */
i = 0;
for (n = 0; n < a->num_of_modes; n++) {
/* Find all indices of values a->clkdiv[n] in b->clkdiv[] */
idx_length = find_equal_int16(idx, b->clkdiv, a->clkdiv[n],
b->num_of_modes, 0);
for (m = 0; m < idx_length; m++) {
if (b->mcic[idx[m]] == a->mcic[n]) {
c->clkdiv[i] = a->clkdiv[n];
c->mcic[i] = a->mcic[n];
c->mfir_a[i] = a->mfir[n];
c->mfir_b[i] = b->mfir[idx[m]];
i++;
}
}
c->num_of_modes = i;
}
}
/* Finds a suitable FIR decimation filter from the included set */
static struct pdm_decim *get_fir(struct dmic_configuration *cfg, int mfir)
{
int i;
int fs;
int cic_fs;
int fir_max_length;
struct pdm_decim *fir = NULL;
if (mfir <= 0)
return fir;
cic_fs = DMIC_HW_IOCLK / cfg->clkdiv / cfg->mcic;
fs = cic_fs / mfir;
/* FIR max. length depends on available cycles and coef RAM
* length. Exceeding this length sets HW overrun status and
* overwrite of other register.
*/
fir_max_length = MIN(DMIC_HW_FIR_LENGTH_MAX,
DMIC_HW_IOCLK / fs / 2 - DMIC_FIR_PIPELINE_OVERHEAD);
for (i = 0; i < DMIC_FIR_LIST_LENGTH; i++) {
if (fir_list[i]->decim_factor == mfir &&
fir_list[i]->length <= fir_max_length) {
/* Store pointer, break from loop to avoid a
* Possible other mode with lower FIR length.
*/
fir = fir_list[i];
break;
}
}
return fir;
}
/* Calculate scale and shift to use for FIR coefficients. Scale is applied
* before write to HW coef RAM. Shift will be programmed to HW register.
*/
static int fir_coef_scale(int32_t *fir_scale, int *fir_shift, int add_shift,
const int32_t coef[], int coef_length, int32_t gain)
{
int32_t amax;
int32_t new_amax;
int32_t fir_gain;
int shift;
/* Multiply gain passed from CIC with output full scale. */
fir_gain = Q_MULTSR_32X32((int64_t)gain, DMIC_HW_SENS_Q28,
DMIC_FIR_SCALE_Q, 28, DMIC_FIR_SCALE_Q);
/* Find the largest FIR coefficient value. */
amax = find_max_abs_int32((int32_t *)coef, coef_length);
/* Scale max. tap value with FIR gain. */
new_amax = Q_MULTSR_32X32((int64_t)amax, fir_gain, 31,
DMIC_FIR_SCALE_Q, DMIC_FIR_SCALE_Q);
if (new_amax <= 0)
return -EINVAL;
/* Get left shifts count to normalize the fractional value as 32 bit.
* We need right shifts count for scaling so need to invert. The
* difference of Q31 vs. used Q format is added to get the correct
* normalization right shift value.
*/
shift = 31 - DMIC_FIR_SCALE_Q - norm_int32(new_amax);
/* Add to shift for coef raw Q31 format shift and store to
* configuration. Ensure range (fail should not happen with OK
* coefficient set).
*/
*fir_shift = -shift + add_shift;
if (*fir_shift < DMIC_HW_FIR_SHIFT_MIN ||
*fir_shift > DMIC_HW_FIR_SHIFT_MAX)
return -EINVAL;
/* Compensate shift into FIR coef scaler and store as Q4.20. */
if (shift < 0)
*fir_scale = (fir_gain << -shift);
else
*fir_scale = (fir_gain >> shift);
#if defined MODULE_TEST
printf("# FIR gain need Q28 = %d (%f)\n", fir_gain,
Q_CONVERT_QTOF(fir_gain, 28));
printf("# FIR max coef no gain Q31 = %d (%f)\n", amax,
Q_CONVERT_QTOF(amax, 31));
printf("# FIR max coef with gain Q28 = %d (%f)\n", new_amax,
Q_CONVERT_QTOF(new_amax, 28));
printf("# FIR coef norm rshift = %d\n", shift);
printf("# FIR coef old rshift = %d\n", add_shift);
printf("# FIR coef new rshift = %d\n", *fir_shift);
printf("# FIR coef scaler Q28 = %d (%f)\n", *fir_scale,
Q_CONVERT_QTOF(*fir_scale, 28));
#endif
return 0;
}
/* This function selects with a simple criteria one mode to set up the
* decimator. For the settings chosen for FIFOs A and B output a lookup
* is done for FIR coefficients from the included coefficients tables.
* For some decimation factors there may be several length coefficient sets.
* It is due to possible restruction of decimation engine cycles per given
* sample rate. If the coefficients length is exceeded the lookup continues.
* Therefore the list of coefficient set must present the filters for a
* decimation factor in decreasing length order.
*
* Note: If there is no filter available an error is returned. The parameters
* should be reviewed for such case. If still a filter is missing it should be
* added into the included set. FIR decimation with a high factor usually
* needs compromizes into specifications and is not desirable.
*/
static int select_mode(struct dmic_configuration *cfg,
struct matched_modes *modes)
{
int32_t g_cic;
int32_t fir_in_max;
int32_t cic_out_max;
int32_t gain_to_fir;
int16_t idx[DMIC_MAX_MODES];
int16_t *mfir;
int n = 1;
int mmin;
int count;
int mcic;
int bits_cic;
int ret;
/* If there are more than one possibilities select a mode with lowest
* FIR decimation factor. If there are several select mode with highest
* ioclk divider to minimize microphone power consumption. The highest
* clock divisors are in the end of list so select the last of list.
* The minimum OSR criteria used in previous ensures that quality in
* the candidates should be sufficient.
*/
if (modes->num_of_modes == 0) {
trace_dmic_error("nom");
return -EINVAL;
}
/* Valid modes presence is indicated with non-zero decimation
* factor in 1st element. If FIR A is not used get decimation factors
* from FIR B instead.
*/
if (modes->mfir_a[0] > 0)
mfir = modes->mfir_a;
else
mfir = modes->mfir_b;
mmin = find_min_int16(mfir, modes->num_of_modes);
count = find_equal_int16(idx, mfir, mmin, modes->num_of_modes, 0);
n = idx[count - 1];
/* Get microphone clock and decimation parameters for used mode from
* the list.
*/
cfg->clkdiv = modes->clkdiv[n];
cfg->mfir_a = modes->mfir_a[n];
cfg->mfir_b = modes->mfir_b[n];
cfg->mcic = modes->mcic[n];
cfg->fir_a = NULL;
cfg->fir_b = NULL;
/* Find raw FIR coefficients to match the decimation factors of FIR
* A and B.
*/
if (cfg->mfir_a > 0) {
cfg->fir_a = get_fir(cfg, cfg->mfir_a);
if (!cfg->fir_a) {
trace_dmic_error("fam");
trace_value(cfg->mfir_a);
return -EINVAL;
}
}
if (cfg->mfir_b > 0) {
cfg->fir_b = get_fir(cfg, cfg->mfir_b);
if (!cfg->fir_b) {
trace_dmic_error("fbm");
trace_value(cfg->mfir_b);
return -EINVAL;
}
}
/* Calculate CIC shift from the decimation factor specific gain. The
* gain of HW decimator equals decimation factor to power of 5.
*/
mcic = cfg->mcic;
g_cic = mcic * mcic * mcic * mcic * mcic;
if (g_cic < 0) {
/* Erroneous decimation factor and CIC gain */
trace_dmic_error("gci");
return -EINVAL;
}
bits_cic = 32 - norm_int32(g_cic);
cfg->cic_shift = bits_cic - DMIC_HW_BITS_FIR_INPUT;
/* Calculate remaining gain to FIR in Q format used for gain
* values.
*/
fir_in_max = (1 << (DMIC_HW_BITS_FIR_INPUT - 1));
if (cfg->cic_shift >= 0)
cic_out_max = g_cic >> cfg->cic_shift;
else
cic_out_max = g_cic << -cfg->cic_shift;
gain_to_fir = (int32_t)((((int64_t)fir_in_max) << DMIC_FIR_SCALE_Q) /
cic_out_max);
/* Calculate FIR scale and shift */
if (cfg->mfir_a > 0) {
cfg->fir_a_length = cfg->fir_a->length;
ret = fir_coef_scale(&cfg->fir_a_scale, &cfg->fir_a_shift,
cfg->fir_a->shift, cfg->fir_a->coef, cfg->fir_a->length,
gain_to_fir);
if (ret < 0) {
/* Invalid coefficient set found, should not happen. */
trace_dmic_error("ina");
return -EINVAL;
}
} else {
cfg->fir_a_scale = 0;
cfg->fir_a_shift = 0;
cfg->fir_a_length = 0;
}
if (cfg->mfir_b > 0) {
cfg->fir_b_length = cfg->fir_b->length;
ret = fir_coef_scale(&cfg->fir_b_scale, &cfg->fir_b_shift,
cfg->fir_b->shift, cfg->fir_b->coef, cfg->fir_b->length,
gain_to_fir);
if (ret < 0) {
/* Invalid coefficient set found, should not happen. */
trace_dmic_error("inb");
return -EINVAL;
}
} else {
cfg->fir_b_scale = 0;
cfg->fir_b_shift = 0;
cfg->fir_b_length = 0;
}
return 0;
}
/* The FIFO input packer mode (IPM) settings are somewhat different in
* HW versions. This helper function returns a suitable IPM bit field
* value to use.
*/
static inline void ipm_helper1(int *ipm, int stereo[], int swap[],
struct sof_ipc_dai_dmic_params *dmic)
{
int pdm[DMIC_HW_CONTROLLERS] = {0};
int cnt;
int i;
/* Loop number of PDM controllers in the configuration. If mic A
* or B is enabled then a pdm controller is marked as active. Also it
* is checked whether the controller should operate as stereo or mono
* left (A) or mono right (B) mode. Mono right mode is setup as channel
* swapped mono left.
*/
for (i = 0; i < dmic->num_pdm_active; i++) {
cnt = 0;
if (dmic->pdm[i].enable_mic_a > 0)
cnt++;
if (dmic->pdm[i].enable_mic_b > 0)
cnt++;
/* A PDM controller is used if at least one mic was enabled. */
pdm[i] = !!cnt;
/* Set stereo mode if both mic A anc B are enabled. */
cnt >>= 1;
stereo[i] = cnt;
/* Swap channels if only mic B is used for mono processing. */
swap[i] = dmic->pdm[i].enable_mic_b & !cnt;
}
/* IPM indicates active pdm controllers. */
*ipm = 0;
if (pdm[0] == 0 && pdm[1] > 0)
*ipm = 1;
if (pdm[0] > 0 && pdm[1] > 0)
*ipm = 2;
}
#if DMIC_HW_VERSION == 2
static inline void ipm_helper2(int source[], int *ipm, int stereo[],
int swap[], struct sof_ipc_dai_dmic_params *dmic)
{
int pdm[DMIC_HW_CONTROLLERS];
int i;
int n = 0;
/* Loop number of PDM controllers in the configuration. If mic A
* or B is enabled then a pdm controller is marked as active. Also it
* is checked whether the controller should operate as stereo or mono
* left (A) or mono right (B) mode. Mono right mode is setup as channel
* swapped mono left. The function returns also in array source[] the
* indice of enabled pdm controllers to be used for IPM configuration.
*/
for (i = 0; i < dmic->num_pdm_active; i++) {
if (dmic->pdm[i].enable_mic_a > 0 ||
dmic->pdm[i].enable_mic_b > 0) {
pdm[i] = 1;
source[n] = i;
n++;
} else {
pdm[i] = 0;
swap[i] = 0;
}
if (dmic->pdm[i].enable_mic_a > 0 &&
dmic->pdm[i].enable_mic_b > 0) {
stereo[i] = 1;
swap[i] = 0;
} else {
stereo[i] = 0;
if (dmic->pdm[i].enable_mic_a == 0)
swap[i] = 1;
else
swap[i] = 0;
}
}
/* IPM bit field is set to count of active pdm controllers. */
*ipm = pdm[0];
for (i = 1; i < dmic->num_pdm_active; i++)
*ipm += pdm[i];
}
#endif
static int configure_registers(struct dai *dai, struct dmic_configuration *cfg,
struct sof_ipc_dai_dmic_params *dmic)
{
int stereo[DMIC_HW_CONTROLLERS];
int swap[DMIC_HW_CONTROLLERS];
uint32_t val;
int32_t ci;
uint32_t cu;
int ipm;
int of0;
int of1;
int fir_decim;
int fir_length;
int length;
int edge;
int dccomp;
int cic_start_a;
int cic_start_b;
int fir_start_a;
int fir_start_b;
int soft_reset;
int i;
int j;
struct dmic_pdata *pdata = dai_get_drvdata(dai);
int array_a = 0;
int array_b = 0;
int cic_mute = 1;
int fir_mute = 1;
int bfth = 3; /* Should be 3 for 8 entries, 1 is 2 entries */
int th = 0; /* Used with TIE=1 */
/* Normal start sequence */
dccomp = 1;
soft_reset = 1;
cic_start_a = 0;
cic_start_b = 0;
fir_start_a = 0;
fir_start_b = 0;
#if DMIC_HW_VERSION == 2
int source[4] = {0, 0, 0, 0};
#endif
#if defined MODULE_TEST
int32_t fir_a_max = 0;
int32_t fir_a_min = 0;
int32_t fir_b_max = 0;
int32_t fir_b_min = 0;
#endif
/* pdata is set by dmic_probe(), error if it has not been set */
if (!pdata) {
trace_dmic_error("cfr");
return -EINVAL;
}
/* Sanity checks */
if (dmic->num_pdm_active > DMIC_HW_CONTROLLERS) {
trace_dmic_error("num");
return -EINVAL;
}
/* OUTCONTROL0 and OUTCONTROL1 */
trace_dmic("reg");
of0 = (dmic->fifo_bits_a == 32) ? 2 : 0;
of1 = (dmic->fifo_bits_b == 32) ? 2 : 0;
#if DMIC_HW_VERSION == 1
ipm_helper1(&ipm, stereo, swap, dmic);
val = OUTCONTROL0_TIE(0) |
OUTCONTROL0_SIP(0) |
OUTCONTROL0_FINIT(1) |
OUTCONTROL0_FCI(0) |
OUTCONTROL0_BFTH(bfth) |
OUTCONTROL0_OF(of0) |
OUTCONTROL0_IPM(ipm) |
OUTCONTROL0_TH(th);
dmic_write(dai, OUTCONTROL0, val);
trace_value(val);
val = OUTCONTROL1_TIE(0) |
OUTCONTROL1_SIP(0) |
OUTCONTROL1_FINIT(1) |
OUTCONTROL1_FCI(0) |
OUTCONTROL1_BFTH(bfth) |
OUTCONTROL1_OF(of1) |
OUTCONTROL1_IPM(ipm) |
OUTCONTROL1_TH(th);
dmic_write(dai, OUTCONTROL1, val);
trace_value(val);
#endif
#if DMIC_HW_VERSION == 2
#if DMIC_HW_CONTROLLERS > 2
ipm_helper2(source, &ipm, stereo, swap, dmic);
#else
ipm_helper1(&ipm, stereo, swap, dmic);
#endif
val = OUTCONTROL0_TIE(0) |
OUTCONTROL0_SIP(0) |
OUTCONTROL0_FINIT(1) |
OUTCONTROL0_FCI(0) |
OUTCONTROL0_BFTH(bfth) |
OUTCONTROL0_OF(of0) |
OUTCONTROL0_IPM(ipm) |
OUTCONTROL0_IPM_SOURCE_1(source[0]) |
OUTCONTROL0_IPM_SOURCE_2(source[1]) |
OUTCONTROL0_IPM_SOURCE_3(source[2]) |
OUTCONTROL0_IPM_SOURCE_4(source[3]) |
OUTCONTROL0_TH(th);
dmic_write(dai, OUTCONTROL0, val);
trace_value(val);
val = OUTCONTROL1_TIE(0) |
OUTCONTROL1_SIP(0) |
OUTCONTROL1_FINIT(1) |
OUTCONTROL1_FCI(0) |
OUTCONTROL1_BFTH(bfth) |
OUTCONTROL1_OF(of1) |
OUTCONTROL1_IPM(ipm) |
OUTCONTROL1_IPM_SOURCE_1(source[0]) |
OUTCONTROL1_IPM_SOURCE_2(source[1]) |
OUTCONTROL1_IPM_SOURCE_3(source[2]) |
OUTCONTROL1_IPM_SOURCE_4(source[3]) |
OUTCONTROL1_TH(th);
dmic_write(dai, OUTCONTROL1, val);
trace_value(val);
#endif
/* Mark enabled microphones into private data to be later used
* for starting correct parts of the HW.
*/
for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
pdata->enable[i] = (dmic->pdm[i].enable_mic_b << 1) |
dmic->pdm[i].enable_mic_a;
}
for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
/* CIC */
val = CIC_CONTROL_SOFT_RESET(soft_reset) |
CIC_CONTROL_CIC_START_B(cic_start_b) |
CIC_CONTROL_CIC_START_A(cic_start_a) |
CIC_CONTROL_MIC_B_POLARITY(dmic->pdm[i].polarity_mic_a) |
CIC_CONTROL_MIC_A_POLARITY(dmic->pdm[i].polarity_mic_b) |
CIC_CONTROL_MIC_MUTE(cic_mute) |
CIC_CONTROL_STEREO_MODE(stereo[i]);
dmic_write(dai, base[i] + CIC_CONTROL, val);
trace_value(val);
val = CIC_CONFIG_CIC_SHIFT(cfg->cic_shift + 8) |
CIC_CONFIG_COMB_COUNT(cfg->mcic - 1);
dmic_write(dai, base[i] + CIC_CONFIG, val);
trace_value(val);
/* Mono right channel mic usage requires swap of PDM channels
* since the mono decimation is done with only left channel
* processing active.
*/
edge = dmic->pdm[i].clk_edge;
if (swap[i])
edge = !edge;
val = MIC_CONTROL_PDM_CLKDIV(cfg->clkdiv - 2) |
MIC_CONTROL_PDM_SKEW(dmic->pdm[i].skew) |
MIC_CONTROL_CLK_EDGE(edge) |
MIC_CONTROL_PDM_EN_B(cic_start_b) |
MIC_CONTROL_PDM_EN_A(cic_start_a);
dmic_write(dai, base[i] + MIC_CONTROL, val);
trace_value(val);
/* FIR A */
fir_decim = MAX(cfg->mfir_a - 1, 0);
fir_length = MAX(cfg->fir_a_length - 1, 0);
val = FIR_CONTROL_A_START(fir_start_a) |
FIR_CONTROL_A_ARRAY_START_EN(array_a) |
FIR_CONTROL_A_DCCOMP(dccomp) |
FIR_CONTROL_A_MUTE(fir_mute) |
FIR_CONTROL_A_STEREO(stereo[i]);
dmic_write(dai, base[i] + FIR_CONTROL_A, val);
trace_value(val);
val = FIR_CONFIG_A_FIR_DECIMATION(fir_decim) |
FIR_CONFIG_A_FIR_SHIFT(cfg->fir_a_shift) |
FIR_CONFIG_A_FIR_LENGTH(fir_length);
dmic_write(dai, base[i] + FIR_CONFIG_A, val);
trace_value(val);
val = DC_OFFSET_LEFT_A_DC_OFFS(DCCOMP_TC0);
dmic_write(dai, base[i] + DC_OFFSET_LEFT_A, val);
trace_value(val);
val = DC_OFFSET_RIGHT_A_DC_OFFS(DCCOMP_TC0);
dmic_write(dai, base[i] + DC_OFFSET_RIGHT_A, val);
trace_value(val);
val = OUT_GAIN_LEFT_A_GAIN(0);
dmic_write(dai, base[i] + OUT_GAIN_LEFT_A, val);
trace_value(val);