blob: 69ee2fda80715ab82ea20d5f9cb16974da9900c8 [file] [log] [blame]
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001/*
2 * Copyright (c) 2017, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Intel Corporation nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
29 */
30
31#include <sof/stream.h>
32#include <sof/dmic.h>
33#include <sof/interrupt.h>
34#include <sof/math/numbers.h>
35#include <sof/audio/format.h>
36
37#if defined DMIC_HW_VERSION
38
39#include <sof/audio/coefficients/pdm_decim/pdm_decim_table.h>
40
41#if defined MODULE_TEST
42#include <stdio.h>
43#endif
44
45#define DMIC_MAX_MODES 50
46
47/* HW FIR pipeline needs 5 additional cycles per channel for internal
48 * operations. This is used in MAX filter length check.
49 */
50#define DMIC_FIR_PIPELINE_OVERHEAD 5
51
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +030052struct decim_modes {
53 int16_t clkdiv[DMIC_MAX_MODES];
54 int16_t mcic[DMIC_MAX_MODES];
55 int16_t mfir[DMIC_MAX_MODES];
56 int num_of_modes;
57};
58
59struct matched_modes {
60 int16_t clkdiv[DMIC_MAX_MODES];
61 int16_t mcic[DMIC_MAX_MODES];
62 int16_t mfir_a[DMIC_MAX_MODES];
63 int16_t mfir_b[DMIC_MAX_MODES];
64 int num_of_modes;
65};
66
67struct dmic_configuration {
68 struct pdm_decim *fir_a;
69 struct pdm_decim *fir_b;
70 int clkdiv;
71 int mcic;
72 int mfir_a;
73 int mfir_b;
74 int cic_shift;
75 int fir_a_shift;
76 int fir_b_shift;
77 int fir_a_length;
78 int fir_b_length;
79 int32_t fir_a_scale;
80 int32_t fir_b_scale;
81};
82
83struct pdm_controllers_configuration {
84 uint32_t cic_control;
85 uint32_t cic_config;
86 uint32_t mic_control;
87 uint32_t fir_control_a;
88 uint32_t fir_config_a;
89 uint32_t dc_offset_left_a;
90 uint32_t dc_offset_right_a;
91 uint32_t out_gain_left_a;
92 uint32_t out_gain_right_a;
93 uint32_t fir_control_b;
94 uint32_t fir_config_b;
95 uint32_t dc_offset_left_b;
96 uint32_t dc_offset_right_b;
97 uint32_t out_gain_left_b;
98 uint32_t out_gain_right_b;
99};
100
101/* Configuration ABI version, increment if not compatible with previous
102 * version.
103 */
104#define DMIC_IPC_VERSION 1
105
106/* Minimum OSR is always applied for 48 kHz and less sample rates */
107#define DMIC_MIN_OSR 50
108
109/* These are used as guideline for configuring > 48 kHz sample rates. The
110 * minimum OSR can be relaxed down to 40 (use 3.84 MHz clock for 96 kHz).
111 */
112#define DMIC_HIGH_RATE_MIN_FS 64000
113#define DMIC_HIGH_RATE_OSR_MIN 40
114
115/* Used for scaling FIR coeffcients for HW */
116#define DMIC_HW_FIR_COEF_MAX ((1 << (DMIC_HW_BITS_FIR_COEF - 1)) - 1)
117#define DMIC_HW_FIR_COEF_Q (DMIC_HW_BITS_FIR_COEF - 1)
118
119/* Internal precision in gains computation, e.g. Q4.28 in int32_t */
120#define DMIC_FIR_SCALE_Q 28
121
Seppo Ingalsuofa48a052018-06-19 18:14:53 +0300122/* Used in unmute ramp values calculation */
123#define DMIC_HW_FIR_GAIN_MAX ((1 << (DMIC_HW_BITS_FIR_GAIN - 1)) - 1)
124
125/* Hardwired log ramp parameters. The first value is the initial logarithmic
126 * gain and the second value is the multiplier for gain to achieve the linear
127 * decibels change over time. Currently the coefficient GM needs to be
128 * calculated manually. The 300 ms ramp should ensure clean sounding start with
129 * any microphone. However it is likely unnecessarily long for machine hearing.
130 * TODO: Add ramp characteristic passing via topology.
131 */
132#define LOGRAMP_GI 33954 /* -90 dB, Q2.30*/
133#define LOGRAMP_GM 16959 /* Gives 300 ms ramp for -90..0 dB, Q2.14 */
134
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300135/* tracing */
136#define trace_dmic(__e) trace_event(TRACE_CLASS_DMIC, __e)
137#define trace_dmic_error(__e) trace_error(TRACE_CLASS_DMIC, __e)
138#define tracev_dmic(__e) tracev_event(TRACE_CLASS_DMIC, __e)
139
140/* Base addresses (in PDM scope) of 2ch PDM controllers and coefficient RAM. */
141static const uint32_t base[4] = {PDM0, PDM1, PDM2, PDM3};
142static const uint32_t coef_base_a[4] = {PDM0_COEFFICIENT_A, PDM1_COEFFICIENT_A,
143 PDM2_COEFFICIENT_A, PDM3_COEFFICIENT_A};
144static const uint32_t coef_base_b[4] = {PDM0_COEFFICIENT_B, PDM1_COEFFICIENT_B,
145 PDM2_COEFFICIENT_B, PDM3_COEFFICIENT_B};
146
147#if defined MODULE_TEST
148#define IO_BYTES_GLOBAL (PDM0 - OUTCONTROL0)
149#define IO_BYTES_MIDDLE (PDM1 - PDM0)
150#define IO_BYTES_LAST (PDM0_COEFFICIENT_B + PDM_COEF_RAM_B_LENGTH - PDM0)
151#define IO_BYTES (((DMIC_HW_CONTROLLERS) - 1) * (IO_BYTES_MIDDLE) \
152 + (IO_BYTES_LAST) + (IO_BYTES_GLOBAL))
153#define IO_LENGTH ((IO_BYTES) >> 2)
154
155static uint32_t dmic_io[IO_LENGTH];
156
157static void dmic_write(struct dai *dai, uint32_t reg, uint32_t value)
158{
159 printf("W %04x %08x\n", reg, value);
160 dmic_io[reg >> 2] = value;
161}
162
163static uint32_t dmic_read(struct dai *dai, uint32_t reg)
164{
165 uint32_t value = dmic_io[reg >> 2];
166
167 printf("R %04x %08x\n", reg, value);
168 return value;
169}
170
171static void dmic_update_bits(struct dai *dai, uint32_t reg, uint32_t mask,
172 uint32_t value)
173{
174 uint32_t new_value;
175 uint32_t old_value = dmic_io[reg >> 2];
176
177 new_value = (old_value & (~mask)) | value;
178 dmic_io[reg >> 2] = new_value;
179 printf("W %04x %08x\n", reg, new_value);
180}
181#else
182
183static void dmic_write(struct dai *dai, uint32_t reg, uint32_t value)
184{
185 io_reg_write(dai_base(dai) + reg, value);
186}
187
188static uint32_t dmic_read(struct dai *dai, uint32_t reg)
189{
190 return io_reg_read(dai_base(dai) + reg);
191}
192
193static void dmic_update_bits(struct dai *dai, uint32_t reg, uint32_t mask,
194 uint32_t value)
195{
196 io_reg_update_bits(dai_base(dai) + reg, mask, value);
197}
198#endif
199
Seppo Ingalsuofa48a052018-06-19 18:14:53 +0300200/* this ramps volume changes over time */
201static uint64_t dmic_work(void *data, uint64_t delay)
202{
203 struct dai *dai = (struct dai *)data;
204 struct dmic_pdata *dmic = dai_get_drvdata(dai);
205 int32_t gval;
206 uint32_t val;
207 int i;
208
209 tracev_dmic("wrk");
210 spin_lock(&dmic->lock);
211
212 /* Increment gain with logaritmic step.
213 * Gain is Q2.30 and gain modifier is Q2.14.
214 */
215 dmic->startcount++;
216 dmic->gain = Q_MULTSR_32X32((int64_t)dmic->gain,
217 LOGRAMP_GM, 30, 14, 30);
218
219 /* Gain is stored as Q2.30, while HW register is Q1.19 so shift
220 * the value right by 11.
221 */
222 gval = dmic->gain >> 11;
223
224 /* Note that DMIC gain value zero has a special purpose. Value zero
225 * sets gain bypass mode in HW. Zero value will be applied after ramp
226 * is complete. It is because exact 1.0 gain is not possible with Q1.19.
227 */
228 if (gval > DMIC_HW_FIR_GAIN_MAX)
229 gval = 0;
230
231 /* Write gain to registers */
232 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
233 if (!dmic->enable[i])
234 continue;
235
236 if (dmic->startcount == DMIC_UNMUTE_CIC)
237 dmic_update_bits(dai, base[i] + CIC_CONTROL,
238 CIC_CONTROL_MIC_MUTE_BIT, 0);
239
240 if (dmic->startcount == DMIC_UNMUTE_FIR) {
241 if (dmic->fifo_a)
242 dmic_update_bits(dai, base[i] + FIR_CONTROL_A,
243 FIR_CONTROL_A_MUTE_BIT, 0);
244
245 if (dmic->fifo_b)
246 dmic_update_bits(dai, base[i] + FIR_CONTROL_B,
247 FIR_CONTROL_B_MUTE_BIT, 0);
248 }
249 if (dmic->fifo_a) {
250 val = OUT_GAIN_LEFT_A_GAIN(gval);
251 dmic_write(dai, base[i] + OUT_GAIN_LEFT_A, val);
252 dmic_write(dai, base[i] + OUT_GAIN_RIGHT_A, val);
253 }
254 if (dmic->fifo_b) {
255 val = OUT_GAIN_LEFT_B_GAIN(gval);
256 dmic_write(dai, base[i] + OUT_GAIN_LEFT_B, val);
257 dmic_write(dai, base[i] + OUT_GAIN_RIGHT_B, val);
258 }
259 }
260 spin_unlock(&dmic->lock);
261
262 if (gval)
263 return DMIC_UNMUTE_RAMP_US;
264 else
265 return 0;
266}
267
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300268/* This function returns a raw list of potential microphone clock and decimation
269 * modes for achieving requested sample rates. The search is constrained by
270 * decimation HW capabililies and setup parameters. The parameters such as
271 * microphone clock min/max and duty cycle requirements need be checked from
272 * used microphone component datasheet.
273 */
274static void find_modes(struct decim_modes *modes,
275 struct sof_ipc_dai_dmic_params *prm, uint32_t fs)
276{
277 int clkdiv_min;
278 int clkdiv_max;
279 int clkdiv;
280 int c1;
281 int du_min;
282 int du_max;
283 int pdmclk;
284 int osr;
285 int mfir;
286 int mcic;
287 int ioclk_test;
288 int osr_min = DMIC_MIN_OSR;
289 int i = 0;
290
291 /* Defaults, empty result */
292 modes->num_of_modes = 0;
293
294 /* The FIFO is not requested if sample rate is set to zero. Just
295 * return in such case with num_of_modes as zero.
296 */
297 if (fs == 0)
298 return;
299
300 /* Override DMIC_MIN_OSR for very high sample rates, use as minimum
301 * the nominal clock for the high rates.
302 */
303 if (fs >= DMIC_HIGH_RATE_MIN_FS)
304 osr_min = DMIC_HIGH_RATE_OSR_MIN;
305
306 /* Check for sane pdm clock, min 100 kHz, max ioclk/2 */
307 if (prm->pdmclk_max < DMIC_HW_PDM_CLK_MIN ||
308 prm->pdmclk_max > DMIC_HW_IOCLK / 2) {
309 trace_dmic_error("pmx");
310 return;
311 }
312 if (prm->pdmclk_min < DMIC_HW_PDM_CLK_MIN ||
313 prm->pdmclk_min > prm->pdmclk_max) {
314 trace_dmic_error("pmn");
315 return;
316 }
317
318 /* Check for sane duty cycle */
319 if (prm->duty_min > prm->duty_max) {
320 trace_dmic_error("pdu");
321 return;
322 }
323 if (prm->duty_min < DMIC_HW_DUTY_MIN ||
324 prm->duty_min > DMIC_HW_DUTY_MAX) {
325 trace_dmic_error("pdn");
326 return;
327 }
328 if (prm->duty_max < DMIC_HW_DUTY_MIN ||
329 prm->duty_max > DMIC_HW_DUTY_MAX) {
330 trace_dmic_error("pdx");
331 return;
332 }
333
334 /* Min and max clock dividers */
335 clkdiv_min = ceil_divide(DMIC_HW_IOCLK, prm->pdmclk_max);
336 clkdiv_min = MAX(clkdiv_min, DMIC_HW_CIC_DECIM_MIN);
337 clkdiv_max = DMIC_HW_IOCLK / prm->pdmclk_min;
338
339 /* Loop possible clock dividers and check based on resulting
340 * oversampling ratio that CIC and FIR decimation ratios are
341 * feasible. The ratios need to be integers. Also the mic clock
342 * duty cycle need to be within limits.
343 */
344 for (clkdiv = clkdiv_min; clkdiv <= clkdiv_max; clkdiv++) {
345 /* Calculate duty cycle for this clock divider. Note that
346 * odd dividers cause non-50% duty cycle.
347 */
348 c1 = clkdiv >> 1;
349 du_min = 100 * c1 / clkdiv;
350 du_max = 100 - du_min;
351
352 /* Calculate PDM clock rate and oversampling ratio. */
353 pdmclk = DMIC_HW_IOCLK / clkdiv;
354 osr = pdmclk / fs;
355
356 /* Check that OSR constraints is met and clock duty cycle does
357 * not exceed microphone specification. If exceed proceed to
358 * next clkdiv.
359 */
360 if (osr < osr_min || du_min < prm->duty_min ||
361 du_max > prm->duty_max)
362 continue;
363
364 /* Loop FIR decimation factors candidates. If the
365 * integer divided decimation factors and clock dividers
366 * as multiplied with sample rate match the IO clock
367 * rate the division was exact and such decimation mode
368 * is possible. Then check that CIC decimation constraints
369 * are met. The passed decimation modes are added to array.
370 */
371 for (mfir = DMIC_HW_FIR_DECIM_MIN;
372 mfir <= DMIC_HW_FIR_DECIM_MAX; mfir++) {
373 mcic = osr / mfir;
374 ioclk_test = fs * mfir * mcic * clkdiv;
375
376 if (ioclk_test == DMIC_HW_IOCLK &&
377 mcic >= DMIC_HW_CIC_DECIM_MIN &&
378 mcic <= DMIC_HW_CIC_DECIM_MAX &&
379 i < DMIC_MAX_MODES) {
380 modes->clkdiv[i] = clkdiv;
381 modes->mcic[i] = mcic;
382 modes->mfir[i] = mfir;
383 i++;
384 modes->num_of_modes = i;
385 }
386 }
387 }
388#if defined MODULE_TEST
389 printf("# Found %d modes\n", i);
390#endif
391}
392
393/* The previous raw modes list contains sane configuration possibilities. When
394 * there is request for both FIFOs A and B operation this function returns
395 * list of compatible settings.
396 */
397static void match_modes(struct matched_modes *c, struct decim_modes *a,
398 struct decim_modes *b)
399{
400 int16_t idx[DMIC_MAX_MODES];
401 int idx_length;
402 int i;
403 int n;
404 int m;
405
406 /* Check if previous search got results. */
407 c->num_of_modes = 0;
408 if (a->num_of_modes == 0 && b->num_of_modes == 0) {
409 /* Nothing to do */
410 return;
411 }
412
Seppo Ingalsuo2979baa2018-05-22 20:07:08 +0300413 /* Ensure that num_of_modes is sane. */
414 if (a->num_of_modes > DMIC_MAX_MODES ||
415 b->num_of_modes > DMIC_MAX_MODES)
416 return;
417
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300418 /* Check for request only for FIFO A or B. In such case pass list for
419 * A or B as such.
420 */
421 if (b->num_of_modes == 0) {
422 c->num_of_modes = a->num_of_modes;
423 for (i = 0; i < a->num_of_modes; i++) {
424 c->clkdiv[i] = a->clkdiv[i];
425 c->mcic[i] = a->mcic[i];
426 c->mfir_a[i] = a->mfir[i];
427 c->mfir_b[i] = 0; /* Mark FIR B as non-used */
428 }
429 return;
430 }
431
432 if (a->num_of_modes == 0) {
433 c->num_of_modes = b->num_of_modes;
434 for (i = 0; i < b->num_of_modes; i++) {
435 c->clkdiv[i] = b->clkdiv[i];
436 c->mcic[i] = b->mcic[i];
437 c->mfir_b[i] = b->mfir[i];
438 c->mfir_a[i] = 0; /* Mark FIR A as non-used */
439 }
440 return;
441 }
442
443 /* Merge a list of compatible modes */
444 i = 0;
445 for (n = 0; n < a->num_of_modes; n++) {
446 /* Find all indices of values a->clkdiv[n] in b->clkdiv[] */
447 idx_length = find_equal_int16(idx, b->clkdiv, a->clkdiv[n],
448 b->num_of_modes, 0);
449 for (m = 0; m < idx_length; m++) {
450 if (b->mcic[idx[m]] == a->mcic[n]) {
451 c->clkdiv[i] = a->clkdiv[n];
452 c->mcic[i] = a->mcic[n];
453 c->mfir_a[i] = a->mfir[n];
454 c->mfir_b[i] = b->mfir[idx[m]];
455 i++;
456 }
457 }
458 c->num_of_modes = i;
459 }
460}
461
462/* Finds a suitable FIR decimation filter from the included set */
463static struct pdm_decim *get_fir(struct dmic_configuration *cfg, int mfir)
464{
465 int i;
466 int fs;
467 int cic_fs;
468 int fir_max_length;
469 struct pdm_decim *fir = NULL;
470
471 if (mfir <= 0)
472 return fir;
473
474 cic_fs = DMIC_HW_IOCLK / cfg->clkdiv / cfg->mcic;
475 fs = cic_fs / mfir;
476 /* FIR max. length depends on available cycles and coef RAM
477 * length. Exceeding this length sets HW overrun status and
478 * overwrite of other register.
479 */
480 fir_max_length = MIN(DMIC_HW_FIR_LENGTH_MAX,
481 DMIC_HW_IOCLK / fs / 2 - DMIC_FIR_PIPELINE_OVERHEAD);
482
483 for (i = 0; i < DMIC_FIR_LIST_LENGTH; i++) {
484 if (fir_list[i]->decim_factor == mfir &&
485 fir_list[i]->length <= fir_max_length) {
486 /* Store pointer, break from loop to avoid a
487 * Possible other mode with lower FIR length.
488 */
489 fir = fir_list[i];
490 break;
491 }
492 }
493
494 return fir;
495}
496
497/* Calculate scale and shift to use for FIR coefficients. Scale is applied
498 * before write to HW coef RAM. Shift will be programmed to HW register.
499 */
500static int fir_coef_scale(int32_t *fir_scale, int *fir_shift, int add_shift,
501 const int32_t coef[], int coef_length, int32_t gain)
502{
503 int32_t amax;
504 int32_t new_amax;
505 int32_t fir_gain;
506 int shift;
507
508 /* Multiply gain passed from CIC with output full scale. */
509 fir_gain = Q_MULTSR_32X32((int64_t)gain, DMIC_HW_SENS_Q28,
510 DMIC_FIR_SCALE_Q, 28, DMIC_FIR_SCALE_Q);
511
512 /* Find the largest FIR coefficient value. */
513 amax = find_max_abs_int32((int32_t *)coef, coef_length);
514
515 /* Scale max. tap value with FIR gain. */
516 new_amax = Q_MULTSR_32X32((int64_t)amax, fir_gain, 31,
517 DMIC_FIR_SCALE_Q, DMIC_FIR_SCALE_Q);
518 if (new_amax <= 0)
519 return -EINVAL;
520
521 /* Get left shifts count to normalize the fractional value as 32 bit.
522 * We need right shifts count for scaling so need to invert. The
523 * difference of Q31 vs. used Q format is added to get the correct
524 * normalization right shift value.
525 */
526 shift = 31 - DMIC_FIR_SCALE_Q - norm_int32(new_amax);
527
528 /* Add to shift for coef raw Q31 format shift and store to
529 * configuration. Ensure range (fail should not happen with OK
530 * coefficient set).
531 */
532 *fir_shift = -shift + add_shift;
533 if (*fir_shift < DMIC_HW_FIR_SHIFT_MIN ||
534 *fir_shift > DMIC_HW_FIR_SHIFT_MAX)
535 return -EINVAL;
536
537 /* Compensate shift into FIR coef scaler and store as Q4.20. */
538 if (shift < 0)
539 *fir_scale = (fir_gain << -shift);
540 else
541 *fir_scale = (fir_gain >> shift);
542
543#if defined MODULE_TEST
544 printf("# FIR gain need Q28 = %d (%f)\n", fir_gain,
545 Q_CONVERT_QTOF(fir_gain, 28));
546 printf("# FIR max coef no gain Q31 = %d (%f)\n", amax,
547 Q_CONVERT_QTOF(amax, 31));
548 printf("# FIR max coef with gain Q28 = %d (%f)\n", new_amax,
549 Q_CONVERT_QTOF(new_amax, 28));
550 printf("# FIR coef norm rshift = %d\n", shift);
551 printf("# FIR coef old rshift = %d\n", add_shift);
552 printf("# FIR coef new rshift = %d\n", *fir_shift);
553 printf("# FIR coef scaler Q28 = %d (%f)\n", *fir_scale,
554 Q_CONVERT_QTOF(*fir_scale, 28));
555#endif
556
557 return 0;
558}
559
560/* This function selects with a simple criteria one mode to set up the
561 * decimator. For the settings chosen for FIFOs A and B output a lookup
562 * is done for FIR coefficients from the included coefficients tables.
563 * For some decimation factors there may be several length coefficient sets.
564 * It is due to possible restruction of decimation engine cycles per given
565 * sample rate. If the coefficients length is exceeded the lookup continues.
566 * Therefore the list of coefficient set must present the filters for a
567 * decimation factor in decreasing length order.
568 *
569 * Note: If there is no filter available an error is returned. The parameters
570 * should be reviewed for such case. If still a filter is missing it should be
571 * added into the included set. FIR decimation with a high factor usually
572 * needs compromizes into specifications and is not desirable.
573 */
574static int select_mode(struct dmic_configuration *cfg,
575 struct matched_modes *modes)
576{
577 int32_t g_cic;
578 int32_t fir_in_max;
579 int32_t cic_out_max;
580 int32_t gain_to_fir;
581 int16_t idx[DMIC_MAX_MODES];
582 int16_t *mfir;
583 int n = 1;
584 int mmin;
585 int count;
586 int mcic;
587 int bits_cic;
588 int ret;
589
590 /* If there are more than one possibilities select a mode with lowest
591 * FIR decimation factor. If there are several select mode with highest
592 * ioclk divider to minimize microphone power consumption. The highest
593 * clock divisors are in the end of list so select the last of list.
594 * The minimum OSR criteria used in previous ensures that quality in
595 * the candidates should be sufficient.
596 */
597 if (modes->num_of_modes == 0) {
598 trace_dmic_error("nom");
599 return -EINVAL;
600 }
601
602 /* Valid modes presence is indicated with non-zero decimation
603 * factor in 1st element. If FIR A is not used get decimation factors
604 * from FIR B instead.
605 */
606 if (modes->mfir_a[0] > 0)
607 mfir = modes->mfir_a;
608 else
609 mfir = modes->mfir_b;
610
611 mmin = find_min_int16(mfir, modes->num_of_modes);
612 count = find_equal_int16(idx, mfir, mmin, modes->num_of_modes, 0);
613 n = idx[count - 1];
614
615 /* Get microphone clock and decimation parameters for used mode from
616 * the list.
617 */
618 cfg->clkdiv = modes->clkdiv[n];
619 cfg->mfir_a = modes->mfir_a[n];
620 cfg->mfir_b = modes->mfir_b[n];
621 cfg->mcic = modes->mcic[n];
622 cfg->fir_a = NULL;
623 cfg->fir_b = NULL;
624
625 /* Find raw FIR coefficients to match the decimation factors of FIR
626 * A and B.
627 */
628 if (cfg->mfir_a > 0) {
629 cfg->fir_a = get_fir(cfg, cfg->mfir_a);
630 if (!cfg->fir_a) {
631 trace_dmic_error("fam");
632 trace_value(cfg->mfir_a);
633 return -EINVAL;
634 }
635 }
636
637 if (cfg->mfir_b > 0) {
638 cfg->fir_b = get_fir(cfg, cfg->mfir_b);
639 if (!cfg->fir_b) {
640 trace_dmic_error("fbm");
641 trace_value(cfg->mfir_b);
642 return -EINVAL;
643 }
644 }
645
646 /* Calculate CIC shift from the decimation factor specific gain. The
647 * gain of HW decimator equals decimation factor to power of 5.
648 */
649 mcic = cfg->mcic;
650 g_cic = mcic * mcic * mcic * mcic * mcic;
651 if (g_cic < 0) {
652 /* Erroneous decimation factor and CIC gain */
653 trace_dmic_error("gci");
654 return -EINVAL;
655 }
656
657 bits_cic = 32 - norm_int32(g_cic);
658 cfg->cic_shift = bits_cic - DMIC_HW_BITS_FIR_INPUT;
659
660 /* Calculate remaining gain to FIR in Q format used for gain
661 * values.
662 */
663 fir_in_max = (1 << (DMIC_HW_BITS_FIR_INPUT - 1));
664 if (cfg->cic_shift >= 0)
665 cic_out_max = g_cic >> cfg->cic_shift;
666 else
667 cic_out_max = g_cic << -cfg->cic_shift;
668
669 gain_to_fir = (int32_t)((((int64_t)fir_in_max) << DMIC_FIR_SCALE_Q) /
670 cic_out_max);
671
672 /* Calculate FIR scale and shift */
673 if (cfg->mfir_a > 0) {
674 cfg->fir_a_length = cfg->fir_a->length;
675 ret = fir_coef_scale(&cfg->fir_a_scale, &cfg->fir_a_shift,
676 cfg->fir_a->shift, cfg->fir_a->coef, cfg->fir_a->length,
677 gain_to_fir);
678 if (ret < 0) {
679 /* Invalid coefficient set found, should not happen. */
680 trace_dmic_error("ina");
681 return -EINVAL;
682 }
683 } else {
684 cfg->fir_a_scale = 0;
685 cfg->fir_a_shift = 0;
686 cfg->fir_a_length = 0;
687 }
688
689 if (cfg->mfir_b > 0) {
690 cfg->fir_b_length = cfg->fir_b->length;
691 ret = fir_coef_scale(&cfg->fir_b_scale, &cfg->fir_b_shift,
692 cfg->fir_b->shift, cfg->fir_b->coef, cfg->fir_b->length,
693 gain_to_fir);
694 if (ret < 0) {
695 /* Invalid coefficient set found, should not happen. */
696 trace_dmic_error("inb");
697 return -EINVAL;
698 }
699 } else {
700 cfg->fir_b_scale = 0;
701 cfg->fir_b_shift = 0;
702 cfg->fir_b_length = 0;
703 }
704
705 return 0;
706}
707
708/* The FIFO input packer mode (IPM) settings are somewhat different in
709 * HW versions. This helper function returns a suitable IPM bit field
710 * value to use.
711 */
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300712
Seppo Ingalsuo06570f22018-06-19 18:14:52 +0300713static inline void ipm_helper1(int *ipm, int stereo[], int swap[],
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300714 struct sof_ipc_dai_dmic_params *dmic)
715{
Seppo Ingalsuo2979baa2018-05-22 20:07:08 +0300716 int pdm[DMIC_HW_CONTROLLERS] = {0};
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300717 int cnt;
718 int i;
719
720 /* Loop number of PDM controllers in the configuration. If mic A
721 * or B is enabled then a pdm controller is marked as active. Also it
722 * is checked whether the controller should operate as stereo or mono
723 * left (A) or mono right (B) mode. Mono right mode is setup as channel
724 * swapped mono left.
725 */
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -0700726 for (i = 0; i < dmic->num_pdm_active; i++) {
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300727 cnt = 0;
728 if (dmic->pdm[i].enable_mic_a > 0)
729 cnt++;
730
731 if (dmic->pdm[i].enable_mic_b > 0)
732 cnt++;
733
734 /* A PDM controller is used if at least one mic was enabled. */
735 pdm[i] = !!cnt;
736
737 /* Set stereo mode if both mic A anc B are enabled. */
738 cnt >>= 1;
739 stereo[i] = cnt;
740
741 /* Swap channels if only mic B is used for mono processing. */
742 swap[i] = dmic->pdm[i].enable_mic_b & !cnt;
743 }
744
745 /* IPM indicates active pdm controllers. */
746 *ipm = 0;
747
748 if (pdm[0] == 0 && pdm[1] > 0)
749 *ipm = 1;
750
751 if (pdm[0] > 0 && pdm[1] > 0)
752 *ipm = 2;
753}
754
Seppo Ingalsuo06570f22018-06-19 18:14:52 +0300755#if DMIC_HW_VERSION == 2
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300756
Seppo Ingalsuo06570f22018-06-19 18:14:52 +0300757static inline void ipm_helper2(int source[], int *ipm, int stereo[],
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300758 int swap[], struct sof_ipc_dai_dmic_params *dmic)
759{
760 int pdm[DMIC_HW_CONTROLLERS];
761 int i;
762 int n = 0;
763
764 /* Loop number of PDM controllers in the configuration. If mic A
765 * or B is enabled then a pdm controller is marked as active. Also it
766 * is checked whether the controller should operate as stereo or mono
767 * left (A) or mono right (B) mode. Mono right mode is setup as channel
768 * swapped mono left. The function returns also in array source[] the
769 * indice of enabled pdm controllers to be used for IPM configuration.
770 */
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -0700771 for (i = 0; i < dmic->num_pdm_active; i++) {
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300772 if (dmic->pdm[i].enable_mic_a > 0 ||
773 dmic->pdm[i].enable_mic_b > 0) {
774 pdm[i] = 1;
775 source[n] = i;
776 n++;
777 } else {
778 pdm[i] = 0;
779 swap[i] = 0;
780 }
781
782 if (dmic->pdm[i].enable_mic_a > 0 &&
783 dmic->pdm[i].enable_mic_b > 0) {
784 stereo[i] = 1;
785 swap[i] = 0;
786 } else {
787 stereo[i] = 0;
788 if (dmic->pdm[i].enable_mic_a == 0)
789 swap[i] = 1;
790 else
791 swap[i] = 0;
792 }
793 }
794
795 /* IPM bit field is set to count of active pdm controllers. */
796 *ipm = pdm[0];
Seppo Ingalsuob2200bf2018-06-19 18:14:49 +0300797 for (i = 1; i < dmic->num_pdm_active; i++)
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300798 *ipm += pdm[i];
799}
800#endif
801
802static int configure_registers(struct dai *dai, struct dmic_configuration *cfg,
803 struct sof_ipc_dai_dmic_params *dmic)
804{
805 int stereo[DMIC_HW_CONTROLLERS];
806 int swap[DMIC_HW_CONTROLLERS];
807 uint32_t val;
808 int32_t ci;
809 uint32_t cu;
810 int ipm;
811 int of0;
812 int of1;
813 int fir_decim;
814 int fir_length;
815 int length;
816 int edge;
817 int dccomp;
818 int cic_start_a;
819 int cic_start_b;
820 int fir_start_a;
821 int fir_start_b;
822 int soft_reset;
823 int i;
824 int j;
825
826 struct dmic_pdata *pdata = dai_get_drvdata(dai);
827 int array_a = 0;
828 int array_b = 0;
Seppo Ingalsuofa48a052018-06-19 18:14:53 +0300829 int cic_mute = 1;
830 int fir_mute = 1;
Seppo Ingalsuobe4bf562018-06-20 19:07:58 +0300831 int bfth = 3; /* Should be 3 for 8 entries, 1 is 2 entries */
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300832 int th = 0; /* Used with TIE=1 */
833
834 /* Normal start sequence */
835 dccomp = 1;
836 soft_reset = 1;
837 cic_start_a = 0;
838 cic_start_b = 0;
839 fir_start_a = 0;
840 fir_start_b = 0;
841
842#if DMIC_HW_VERSION == 2
843 int source[4] = {0, 0, 0, 0};
844#endif
845
846#if defined MODULE_TEST
847 int32_t fir_a_max = 0;
848 int32_t fir_a_min = 0;
849 int32_t fir_b_max = 0;
850 int32_t fir_b_min = 0;
851#endif
852
853 /* pdata is set by dmic_probe(), error if it has not been set */
854 if (!pdata) {
855 trace_dmic_error("cfr");
856 return -EINVAL;
857 }
858
859 /* Sanity checks */
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -0700860 if (dmic->num_pdm_active > DMIC_HW_CONTROLLERS) {
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300861 trace_dmic_error("num");
862 return -EINVAL;
863 }
864
865 /* OUTCONTROL0 and OUTCONTROL1 */
866 trace_dmic("reg");
867 of0 = (dmic->fifo_bits_a == 32) ? 2 : 0;
868 of1 = (dmic->fifo_bits_b == 32) ? 2 : 0;
869
870#if DMIC_HW_VERSION == 1
Seppo Ingalsuo06570f22018-06-19 18:14:52 +0300871 ipm_helper1(&ipm, stereo, swap, dmic);
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300872 val = OUTCONTROL0_TIE(0) |
873 OUTCONTROL0_SIP(0) |
874 OUTCONTROL0_FINIT(1) |
875 OUTCONTROL0_FCI(0) |
876 OUTCONTROL0_BFTH(bfth) |
877 OUTCONTROL0_OF(of0) |
878 OUTCONTROL0_IPM(ipm) |
879 OUTCONTROL0_TH(th);
880 dmic_write(dai, OUTCONTROL0, val);
881 trace_value(val);
882
883 val = OUTCONTROL1_TIE(0) |
884 OUTCONTROL1_SIP(0) |
885 OUTCONTROL1_FINIT(1) |
886 OUTCONTROL1_FCI(0) |
887 OUTCONTROL1_BFTH(bfth) |
888 OUTCONTROL1_OF(of1) |
889 OUTCONTROL1_IPM(ipm) |
890 OUTCONTROL1_TH(th);
891 dmic_write(dai, OUTCONTROL1, val);
892 trace_value(val);
893#endif
894
895#if DMIC_HW_VERSION == 2
Seppo Ingalsuo06570f22018-06-19 18:14:52 +0300896#if DMIC_HW_CONTROLLERS > 2
897 ipm_helper2(source, &ipm, stereo, swap, dmic);
898#else
899 ipm_helper1(&ipm, stereo, swap, dmic);
900#endif
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300901 val = OUTCONTROL0_TIE(0) |
902 OUTCONTROL0_SIP(0) |
903 OUTCONTROL0_FINIT(1) |
904 OUTCONTROL0_FCI(0) |
Seppo Ingalsuof886d3d2018-06-19 18:14:50 +0300905 OUTCONTROL0_BFTH(bfth) |
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300906 OUTCONTROL0_OF(of0) |
Seppo Ingalsuo24ee0432018-06-19 18:14:51 +0300907 OUTCONTROL0_IPM(ipm) |
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300908 OUTCONTROL0_IPM_SOURCE_1(source[0]) |
909 OUTCONTROL0_IPM_SOURCE_2(source[1]) |
910 OUTCONTROL0_IPM_SOURCE_3(source[2]) |
911 OUTCONTROL0_IPM_SOURCE_4(source[3]) |
Seppo Ingalsuof886d3d2018-06-19 18:14:50 +0300912 OUTCONTROL0_TH(th);
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300913 dmic_write(dai, OUTCONTROL0, val);
914 trace_value(val);
915
916 val = OUTCONTROL1_TIE(0) |
917 OUTCONTROL1_SIP(0) |
918 OUTCONTROL1_FINIT(1) |
919 OUTCONTROL1_FCI(0) |
Seppo Ingalsuof886d3d2018-06-19 18:14:50 +0300920 OUTCONTROL1_BFTH(bfth) |
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300921 OUTCONTROL1_OF(of1) |
Seppo Ingalsuo24ee0432018-06-19 18:14:51 +0300922 OUTCONTROL1_IPM(ipm) |
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300923 OUTCONTROL1_IPM_SOURCE_1(source[0]) |
924 OUTCONTROL1_IPM_SOURCE_2(source[1]) |
925 OUTCONTROL1_IPM_SOURCE_3(source[2]) |
926 OUTCONTROL1_IPM_SOURCE_4(source[3]) |
Seppo Ingalsuof886d3d2018-06-19 18:14:50 +0300927 OUTCONTROL1_TH(th);
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +0300928 dmic_write(dai, OUTCONTROL1, val);
929 trace_value(val);
930#endif
931
932 /* Mark enabled microphones into private data to be later used
933 * for starting correct parts of the HW.
934 */
935 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
936 pdata->enable[i] = (dmic->pdm[i].enable_mic_b << 1) |
937 dmic->pdm[i].enable_mic_a;
938 }
939
940 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
941 /* CIC */
942 val = CIC_CONTROL_SOFT_RESET(soft_reset) |
943 CIC_CONTROL_CIC_START_B(cic_start_b) |
944 CIC_CONTROL_CIC_START_A(cic_start_a) |
945 CIC_CONTROL_MIC_B_POLARITY(dmic->pdm[i].polarity_mic_a) |
946 CIC_CONTROL_MIC_A_POLARITY(dmic->pdm[i].polarity_mic_b) |
947 CIC_CONTROL_MIC_MUTE(cic_mute) |
948 CIC_CONTROL_STEREO_MODE(stereo[i]);
949 dmic_write(dai, base[i] + CIC_CONTROL, val);
950 trace_value(val);
951
952 val = CIC_CONFIG_CIC_SHIFT(cfg->cic_shift + 8) |
953 CIC_CONFIG_COMB_COUNT(cfg->mcic - 1);
954 dmic_write(dai, base[i] + CIC_CONFIG, val);
955 trace_value(val);
956
957 /* Mono right channel mic usage requires swap of PDM channels
958 * since the mono decimation is done with only left channel
959 * processing active.
960 */
961 edge = dmic->pdm[i].clk_edge;
962 if (swap[i])
963 edge = !edge;
964
965 val = MIC_CONTROL_PDM_CLKDIV(cfg->clkdiv - 2) |
966 MIC_CONTROL_PDM_SKEW(dmic->pdm[i].skew) |
967 MIC_CONTROL_CLK_EDGE(edge) |
968 MIC_CONTROL_PDM_EN_B(cic_start_b) |
969 MIC_CONTROL_PDM_EN_A(cic_start_a);
970 dmic_write(dai, base[i] + MIC_CONTROL, val);
971 trace_value(val);
972
973 /* FIR A */
974 fir_decim = MAX(cfg->mfir_a - 1, 0);
975 fir_length = MAX(cfg->fir_a_length - 1, 0);
976 val = FIR_CONTROL_A_START(fir_start_a) |
977 FIR_CONTROL_A_ARRAY_START_EN(array_a) |
978 FIR_CONTROL_A_DCCOMP(dccomp) |
979 FIR_CONTROL_A_MUTE(fir_mute) |
980 FIR_CONTROL_A_STEREO(stereo[i]);
981 dmic_write(dai, base[i] + FIR_CONTROL_A, val);
982 trace_value(val);
983
984 val = FIR_CONFIG_A_FIR_DECIMATION(fir_decim) |
985 FIR_CONFIG_A_FIR_SHIFT(cfg->fir_a_shift) |
986 FIR_CONFIG_A_FIR_LENGTH(fir_length);
987 dmic_write(dai, base[i] + FIR_CONFIG_A, val);
988 trace_value(val);
989
990 val = DC_OFFSET_LEFT_A_DC_OFFS(DCCOMP_TC0);
991 dmic_write(dai, base[i] + DC_OFFSET_LEFT_A, val);
992 trace_value(val);
993
994 val = DC_OFFSET_RIGHT_A_DC_OFFS(DCCOMP_TC0);
995 dmic_write(dai, base[i] + DC_OFFSET_RIGHT_A, val);
996 trace_value(val);
997
998 val = OUT_GAIN_LEFT_A_GAIN(0);
999 dmic_write(dai, base[i] + OUT_GAIN_LEFT_A, val);
1000 trace_value(val);
1001
1002 val = OUT_GAIN_RIGHT_A_GAIN(0);
1003 dmic_write(dai, base[i] + OUT_GAIN_RIGHT_A, val);
1004 trace_value(val);
1005
1006 /* FIR B */
1007 fir_decim = MAX(cfg->mfir_b - 1, 0);
1008 fir_length = MAX(cfg->fir_b_length - 1, 0);
1009 val = FIR_CONTROL_B_START(fir_start_b) |
1010 FIR_CONTROL_B_ARRAY_START_EN(array_b) |
1011 FIR_CONTROL_B_DCCOMP(dccomp) |
1012 FIR_CONTROL_B_MUTE(fir_mute) |
1013 FIR_CONTROL_B_STEREO(stereo[i]);
1014 dmic_write(dai, base[i] + FIR_CONTROL_B, val);
1015 trace_value(val);
1016
1017 val = FIR_CONFIG_B_FIR_DECIMATION(fir_decim) |
1018 FIR_CONFIG_B_FIR_SHIFT(cfg->fir_b_shift) |
1019 FIR_CONFIG_B_FIR_LENGTH(fir_length);
1020 dmic_write(dai, base[i] + FIR_CONFIG_B, val);
1021 trace_value(val);
1022
1023 val = DC_OFFSET_LEFT_B_DC_OFFS(DCCOMP_TC0);
1024 dmic_write(dai, base[i] + DC_OFFSET_LEFT_B, val);
1025 trace_value(val);
1026 trace_value(val);
1027
1028 val = DC_OFFSET_RIGHT_B_DC_OFFS(DCCOMP_TC0);
1029 dmic_write(dai, base[i] + DC_OFFSET_RIGHT_B, val);
1030 trace_value(val);
1031
1032 val = OUT_GAIN_LEFT_B_GAIN(0);
1033 dmic_write(dai, base[i] + OUT_GAIN_LEFT_B, val);
1034 trace_value(val);
1035
1036 val = OUT_GAIN_RIGHT_B_GAIN(0);
1037 dmic_write(dai, base[i] + OUT_GAIN_RIGHT_B, val);
1038 trace_value(val);
1039
1040 /* Write coef RAM A with scaled coefficient in reverse order */
1041 length = cfg->fir_a_length;
1042 for (j = 0; j < length; j++) {
1043 ci = (int32_t)Q_MULTSR_32X32(
1044 (int64_t)cfg->fir_a->coef[j], cfg->fir_a_scale,
1045 31, DMIC_FIR_SCALE_Q, DMIC_HW_FIR_COEF_Q);
1046 cu = FIR_COEF_A(ci);
1047 dmic_write(dai, coef_base_a[i]
1048 + ((length - j - 1) << 2), cu);
1049#if defined MODULE_TEST
1050 fir_a_max = MAX(fir_a_max, ci);
1051 fir_a_min = MIN(fir_a_min, ci);
1052#endif
1053 }
1054
1055 /* Write coef RAM B with scaled coefficient in reverse order */
1056 length = cfg->fir_b_length;
1057 for (j = 0; j < length; j++) {
1058 ci = (int32_t)Q_MULTSR_32X32(
1059 (int64_t)cfg->fir_b->coef[j], cfg->fir_b_scale,
1060 31, DMIC_FIR_SCALE_Q, DMIC_HW_FIR_COEF_Q);
1061 cu = FIR_COEF_B(ci);
1062 dmic_write(dai, coef_base_b[i]
1063 + ((length - j - 1) << 2), cu);
1064#if defined MODULE_TEST
1065 fir_b_max = MAX(fir_b_max, ci);
1066 fir_b_min = MIN(fir_b_min, ci);
1067#endif
1068 }
1069 }
1070
1071#if defined MODULE_TEST
1072 printf("# FIR A max Q19 = %d (%f)\n", fir_a_max,
1073 Q_CONVERT_QTOF(fir_a_max, 19));
1074 printf("# FIR A min Q19 = %d (%f)\n", fir_a_min,
1075 Q_CONVERT_QTOF(fir_a_min, 19));
1076 printf("# FIR B max Q19 = %d (%f)\n", fir_b_max,
1077 Q_CONVERT_QTOF(fir_b_max, 19));
1078 printf("# FIR B min Q19 = %d (%f)\n", fir_b_min,
1079 Q_CONVERT_QTOF(fir_b_min, 19));
1080#endif
1081
1082 /* Function dmic_start() uses these to start the used FIFOs */
1083 if (cfg->mfir_a > 0)
1084 pdata->fifo_a = 1;
1085 else
1086 pdata->fifo_a = 0;
1087
1088 if (cfg->mfir_b > 0)
1089 pdata->fifo_b = 1;
1090 else
1091 pdata->fifo_b = 0;
1092
1093 return 0;
1094}
1095
1096static int dmic_set_config(struct dai *dai, struct sof_ipc_dai_config *config)
1097{
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001098 struct dmic_pdata *dmic = dai_get_drvdata(dai);
1099 struct sof_ipc_dai_dmic_params *prm;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001100 struct matched_modes modes_ab;
1101 struct dmic_configuration cfg;
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001102 struct decim_modes modes_a;
1103 struct decim_modes modes_b;
1104 int num_pdm_active = config->dmic.num_pdm_active;
1105 size_t size;
1106 int i, j, ret = 0;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001107
1108 trace_dmic("dsc");
1109
Seppo Ingalsuofa48a052018-06-19 18:14:53 +03001110 /* Initialize start sequence handler */
1111 work_init(&dmic->dmicwork, dmic_work, dai, WORK_ASYNC);
1112
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001113 /*
1114 * "config" might contain pdm controller params for only
1115 * the active controllers
1116 * "prm" is initialized with default params for all HW controllers
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001117 */
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001118 size = sizeof(*prm) + DMIC_HW_CONTROLLERS
1119 * sizeof(struct sof_ipc_dai_dmic_pdm_ctrl);
1120 prm = rzalloc(RZONE_SYS, SOF_MEM_CAPS_RAM, size);
1121
Ranjani Sridharan48d2a1c2018-06-14 10:45:57 -07001122 /* copy the DMIC params */
1123 memcpy(prm, &config->dmic, sizeof(struct sof_ipc_dai_dmic_params));
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001124
1125 /* copy the pdm controller params from ipc */
1126 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
1127 prm->pdm[i].id = i;
1128 for (j = 0; j < num_pdm_active; j++) {
1129 /* copy the pdm controller params id the id's match */
1130 if (prm->pdm[i].id == config->dmic.pdm[j].id)
1131 memcpy(&prm->pdm[i], &config->dmic.pdm[j],
1132 sizeof(prm->pdm[i]));
1133 }
1134 }
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001135
1136 trace_value(prm->driver_ipc_version);
1137 trace_value(prm->pdmclk_min);
1138 trace_value(prm->pdmclk_max);
1139 trace_value(prm->fifo_fs_a);
1140 trace_value(prm->fifo_fs_b);
1141 trace_value(prm->fifo_bits_a);
1142 trace_value(prm->fifo_bits_b);
1143 trace_value(prm->duty_min);
1144 trace_value(prm->duty_max);
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001145 trace_value(prm->num_pdm_active);
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001146
1147 if (prm->driver_ipc_version != DMIC_IPC_VERSION) {
1148 trace_dmic_error("ver");
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001149 ret = -EINVAL;
1150 goto finish;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001151 }
1152
Seppo Ingalsuo4e06d572018-07-26 18:42:28 +03001153 if (prm->fifo_bits_a != 16 && prm->fifo_bits_a != 32) {
1154 trace_dmic_error("fba");
1155 ret = -EINVAL;
1156 goto finish;
1157 }
1158
1159 if (prm->fifo_bits_b != 16 && prm->fifo_bits_b != 32) {
1160 trace_dmic_error("fbb");
1161 ret = -EINVAL;
1162 goto finish;
1163 }
1164
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001165 /* Match and select optimal decimators configuration for FIFOs A and B
1166 * paths. This setup phase is still abstract. Successful completion
1167 * points struct cfg to FIR coefficients and contains the scale value
1168 * to use for FIR coefficient RAM write as well as the CIC and FIR
1169 * shift values.
1170 */
1171 find_modes(&modes_a, prm, prm->fifo_fs_a);
1172 if (modes_a.num_of_modes == 0 && prm->fifo_fs_a > 0) {
1173 trace_dmic_error("amo");
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001174 ret = -EINVAL;
1175 goto finish;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001176 }
1177
1178 find_modes(&modes_b, prm, prm->fifo_fs_b);
1179 if (modes_b.num_of_modes == 0 && prm->fifo_fs_b > 0) {
1180 trace_dmic_error("bmo");
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001181 ret = -EINVAL;
1182 goto finish;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001183 }
1184
1185 match_modes(&modes_ab, &modes_a, &modes_b);
1186 ret = select_mode(&cfg, &modes_ab);
1187 if (ret < 0) {
1188 trace_dmic_error("smo");
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001189 ret = -EINVAL;
1190 goto finish;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001191 }
1192
1193 trace_dmic("cfg");
1194 trace_value(cfg.clkdiv);
1195 trace_value(cfg.mcic);
1196 trace_value(cfg.mfir_a);
1197 trace_value(cfg.mfir_b);
1198 trace_value(cfg.fir_a_length);
1199 trace_value(cfg.fir_b_length);
1200 trace_value(cfg.cic_shift);
1201 trace_value(cfg.fir_a_shift);
1202 trace_value(cfg.fir_b_shift);
1203
1204 /* Struct reg contains a mirror of actual HW registers. Determine
1205 * register bits configuration from decimator configuration and the
1206 * requested parameters.
1207 */
1208 ret = configure_registers(dai, &cfg, prm);
1209 if (ret < 0) {
1210 trace_dmic_error("cor");
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001211 ret = -EINVAL;
1212 goto finish;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001213 }
1214
1215 dmic->state = COMP_STATE_PREPARE;
1216
Ranjani Sridharanc02e34f2018-05-26 23:22:44 -07001217finish:
1218 /* free config params */
1219 rfree(prm);
1220
1221 return ret;
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001222}
1223
1224/* start the DMIC for capture */
1225static void dmic_start(struct dai *dai)
1226{
1227 struct dmic_pdata *dmic = dai_get_drvdata(dai);
1228 int i;
1229 int mic_a;
1230 int mic_b;
1231 int fir_a;
1232 int fir_b;
1233
1234 /* enable port */
1235 spin_lock(&dmic->lock);
1236 trace_dmic("sta");
1237 dmic->state = COMP_STATE_ACTIVE;
Seppo Ingalsuofa48a052018-06-19 18:14:53 +03001238 dmic->startcount = 0;
1239 dmic->gain = LOGRAMP_GI; /* Initial gain value */
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001240
1241 if (dmic->fifo_a) {
1242 trace_dmic("ffa");
1243 /* Clear FIFO A initialize, Enable interrupts to DSP,
1244 * Start FIFO A packer.
1245 */
1246 dmic_update_bits(dai, OUTCONTROL0,
1247 OUTCONTROL0_FINIT_BIT | OUTCONTROL0_SIP_BIT,
1248 OUTCONTROL0_SIP_BIT);
1249 }
1250 if (dmic->fifo_b) {
1251 trace_dmic("ffb");
1252 /* Clear FIFO B initialize, Enable interrupts to DSP,
1253 * Start FIFO B packer.
1254 */
1255 dmic_update_bits(dai, OUTCONTROL1,
1256 OUTCONTROL1_FINIT_BIT | OUTCONTROL1_SIP_BIT,
1257 OUTCONTROL1_SIP_BIT);
1258 }
1259
1260 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
1261 mic_a = dmic->enable[i] & 1;
1262 mic_b = (dmic->enable[i] & 2) >> 1;
1263 if (dmic->fifo_a)
1264 fir_a = (dmic->enable[i] > 0) ? 1 : 0;
1265 else
1266 fir_a = 0;
1267 if (dmic->fifo_b)
1268 fir_b = (dmic->enable[i] > 0) ? 1 : 0;
1269 else
1270 fir_b = 0;
1271
1272 trace_dmic("mfn");
1273 trace_value(mic_a);
1274 trace_value(mic_b);
1275 trace_value(fir_a);
1276 trace_value(fir_b);
1277
1278 dmic_update_bits(dai, base[i] + CIC_CONTROL,
1279 CIC_CONTROL_CIC_START_A_BIT |
1280 CIC_CONTROL_CIC_START_B_BIT,
1281 CIC_CONTROL_CIC_START_A(mic_a) |
1282 CIC_CONTROL_CIC_START_B(mic_b));
1283 dmic_update_bits(dai, base[i] + MIC_CONTROL,
1284 MIC_CONTROL_PDM_EN_A_BIT |
1285 MIC_CONTROL_PDM_EN_B_BIT,
1286 MIC_CONTROL_PDM_EN_A(mic_a) |
1287 MIC_CONTROL_PDM_EN_B(mic_b));
1288
1289 dmic_update_bits(dai, base[i] + FIR_CONTROL_A,
1290 FIR_CONTROL_A_START_BIT, FIR_CONTROL_A_START(fir_a));
1291 dmic_update_bits(dai, base[i] + FIR_CONTROL_B,
1292 FIR_CONTROL_B_START_BIT, FIR_CONTROL_B_START(fir_b));
1293 }
1294
1295 /* Clear soft reset for all/used PDM controllers. This should
1296 * start capture in sync.
1297 */
1298 trace_dmic("unr");
1299 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
1300 dmic_update_bits(dai, base[i] + CIC_CONTROL,
1301 CIC_CONTROL_SOFT_RESET_BIT, 0);
1302 }
1303
1304 spin_unlock(&dmic->lock);
1305
1306 /* Currently there's no DMIC HW internal mutings and wait times
1307 * applied into this start sequence. It can be implemented here if
1308 * start of audio capture would contain clicks and/or noise and it
1309 * is not suppressed by gain ramp somewhere in the capture pipe.
1310 */
Seppo Ingalsuofa48a052018-06-19 18:14:53 +03001311
1312 work_schedule_default(&dmic->dmicwork, DMIC_UNMUTE_RAMP_US);
1313
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001314 trace_dmic("run");
1315}
1316/* stop the DMIC for capture */
1317static void dmic_stop(struct dai *dai)
1318{
1319 struct dmic_pdata *dmic = dai_get_drvdata(dai);
1320 int i;
1321
1322 trace_dmic("sto")
1323 spin_lock(&dmic->lock);
1324 dmic->state = COMP_STATE_PREPARE;
1325
1326 /* Stop FIFO packers and set FIFO initialize bits */
1327 dmic_update_bits(dai, OUTCONTROL0,
1328 OUTCONTROL0_SIP_BIT | OUTCONTROL0_FINIT_BIT,
1329 OUTCONTROL0_FINIT_BIT);
1330 dmic_update_bits(dai, OUTCONTROL1,
1331 OUTCONTROL1_SIP_BIT | OUTCONTROL1_FINIT_BIT,
1332 OUTCONTROL1_FINIT_BIT);
1333
Seppo Ingalsuofa48a052018-06-19 18:14:53 +03001334 /* Set soft reset and mute on for all PDM controllers.
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001335 */
1336 trace_dmic("sre");
1337 for (i = 0; i < DMIC_HW_CONTROLLERS; i++) {
1338 dmic_update_bits(dai, base[i] + CIC_CONTROL,
Seppo Ingalsuofa48a052018-06-19 18:14:53 +03001339 CIC_CONTROL_SOFT_RESET_BIT |
1340 CIC_CONTROL_MIC_MUTE_BIT,
1341 CIC_CONTROL_SOFT_RESET_BIT |
1342 CIC_CONTROL_MIC_MUTE_BIT);
1343 dmic_update_bits(dai, base[i] + FIR_CONTROL_A,
1344 FIR_CONTROL_A_MUTE_BIT,
1345 FIR_CONTROL_A_MUTE_BIT);
1346 dmic_update_bits(dai, base[i] + FIR_CONTROL_B,
1347 FIR_CONTROL_B_MUTE_BIT,
1348 FIR_CONTROL_B_MUTE_BIT);
Seppo Ingalsuofdc0cf92018-05-11 17:03:02 +03001349 }
1350
1351 spin_unlock(&dmic->lock);
1352}
1353
1354/* save DMIC context prior to entering D3 */
1355static int dmic_context_store(struct dai *dai)
1356{
1357 /* TODO: Nothing stored at the moment. Could read the registers into
1358 * persisten memory if needed but the large coef RAM is not desirable
1359 * to copy. It would be better to store selected mode parametesr from
1360 * previous configuration request and re-program registers from
1361 * scratch.
1362 */
1363 return 0;
1364}
1365
1366/* restore DMIC context after leaving D3 */
1367static int dmic_context_restore(struct dai *dai)
1368{
1369 /* Nothing restored at the moment. */
1370 return 0;
1371}
1372
1373static int dmic_trigger(struct dai *dai, int cmd, int direction)
1374{
1375 struct dmic_pdata *dmic = dai_get_drvdata(dai);
1376
1377 trace_dmic("tri");
1378
1379 /* dai private is set in dmic_probe(), error if not set */
1380 if (!dmic) {
1381 trace_dmic_error("trn");
1382 return -EINVAL;
1383 }
1384
1385 if (direction != DAI_DIR_CAPTURE) {
1386 trace_dmic_error("cap");
1387 return -EINVAL;
1388 }
1389
1390 switch (cmd) {
1391 case COMP_TRIGGER_RELEASE:
1392 case COMP_TRIGGER_START:
1393 if (dmic->state == COMP_STATE_PREPARE ||
1394 dmic->state == COMP_STATE_PAUSED) {
1395 dmic_start(dai);
1396 } else {
1397 trace_dmic_error("cst");
1398 trace_value(dmic->state);
1399 }
1400 break;
1401 case COMP_TRIGGER_STOP:
1402 case COMP_TRIGGER_PAUSE:
1403 dmic_stop(dai);
1404 break;
1405 case COMP_TRIGGER_RESUME:
1406 dmic_context_restore(dai);
1407 break;
1408 case COMP_TRIGGER_SUSPEND:
1409 dmic_context_store(dai);
1410 break;
1411 default:
1412 break;
1413 }
1414
1415 return 0;
1416}
1417
1418/* On DMIC IRQ event trace the status register that contains the status and
1419 * error bit fields.
1420 */
1421static void dmic_irq_handler(void *data)
1422{
1423 struct dai *dai = data;
1424 uint32_t val;
1425
1426 /* Trace OUTSTAT0 register */
1427 val = dmic_read(dai, OUTSTAT0);
1428 trace_dmic("irq");
1429
1430 if (val & OUTSTAT0_ROR_BIT)
1431 trace_dmic_error("eor"); /* Full fifo or PDM overrrun */
1432
1433 trace_value(val);
1434
1435 /* clear IRQ */
1436 platform_interrupt_clear(dmic_irq(dai), 1);
1437}
1438
1439static int dmic_probe(struct dai *dai)
1440{
1441 struct dmic_pdata *dmic;
1442
1443 trace_dmic("pro");
1444
1445 /* allocate private data */
1446 dmic = rzalloc(RZONE_SYS, SOF_MEM_CAPS_RAM, sizeof(*dmic));
1447 dai_set_drvdata(dai, dmic);
1448
1449 spinlock_init(&dmic->lock);
1450
1451 /* Set state, note there is no playback direction support */
1452 dmic->state = COMP_STATE_READY;
1453
1454 /* register our IRQ handler */
1455 interrupt_register(dmic_irq(dai), dmic_irq_handler, dai);
1456
1457 platform_interrupt_unmask(dmic_irq(dai), 1);
1458 interrupt_enable(dmic_irq(dai));
1459
1460 return 0;
1461}
1462
1463/* DMIC has no loopback support */
1464static inline int dmic_set_loopback_mode(struct dai *dai, uint32_t lbm)
1465{
1466 return -EINVAL;
1467}
1468
1469const struct dai_ops dmic_ops = {
1470 .trigger = dmic_trigger,
1471 .set_config = dmic_set_config,
1472 .pm_context_store = dmic_context_store,
1473 .pm_context_restore = dmic_context_restore,
1474 .probe = dmic_probe,
1475 .set_loopback_mode = dmic_set_loopback_mode,
1476};
1477
1478#endif