~aleteoryx/muditaos

ref: a2b6d8738df77de44c919ae49056991720f725ff muditaos/module-bsp/board/rt1051/bsp/audio/CodecMAX98090.cpp -rw-r--r-- 20.5 KiB
a2b6d873 — Jakub Pyszczak [EGD-7732] Fixed audio poor quality 4 years ago
                                                                                
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CodecMAX98090.hpp"
#include "max98090_regs.hpp"
#include "board/BoardDefinitions.hpp"

#include <Utils.hpp>

#include <map>

using namespace drivers;

CodecMAX98090::CodecMAX98090() : i2cAddr{}
{

    i2cAddr.deviceAddress  = MAX98090_I2C_ADDR;
    i2cAddr.subAddressSize = 1; // MAX98090 uses 1byte addressing
    i2c                    = DriverI2C::Create(
        static_cast<I2CInstances>(BoardDefinitions::AUDIOCODEC_I2C),
        DriverI2CParams{.baudrate = static_cast<uint32_t>(BoardDefinitions::AUDIOCODEC_I2C_BAUDRATE)});
    Reset();
}

CodecMAX98090::~CodecMAX98090()
{
    Reset();
}

CodecRetCode CodecMAX98090::Start(const CodecParams &param)
{

    const CodecParamsMAX98090 &params = static_cast<const CodecParamsMAX98090 &>(param);

    // Turn off device
    i2cAddr.subAddress                   = MAX98090_REG_DEVICE_SHUTDOWN;
    max98090_reg_shutdown_t dev_shutdown = {};
    i2c->Write(i2cAddr, (uint8_t *)&dev_shutdown, 1);

    max98090_reg_masterclock_quick_setup_t masterclock_setup = {};
    masterclock_setup.M12P288                                = 1;
    i2cAddr.subAddress                                       = MAX98090_REG_MASTER_CLOCK_QUICK_SETUP;
    i2c->Write(i2cAddr, (uint8_t *)&masterclock_setup, 1);

    max98090_reg_master_samplerate_quick_setup_t samplerate_setup = {};
    switch (params.sampleRate) {

    case CodecParamsMAX98090::SampleRate::Rate8KHz:
        samplerate_setup.SR_8K = 1;
        break;

    case CodecParamsMAX98090::SampleRate::Rate16KHz:
        samplerate_setup.SR_16K = 1;
        break;

    case CodecParamsMAX98090::SampleRate::Rate44K1Hz:
        samplerate_setup.SR_44K1 = 1;
        break;

    case CodecParamsMAX98090::SampleRate::Rate48KHz:
        samplerate_setup.SR_48K = 1;
        break;

    case CodecParamsMAX98090::SampleRate::Rate32KHz:
        samplerate_setup.SR_32K = 1;
        break;

    case CodecParamsMAX98090::SampleRate::Rate96KHz:
        samplerate_setup.SR_96K = 1;
        break;

    default:
        return CodecRetCode::InvalidSampleRate;
    }
    i2cAddr.subAddress = MAX98090_REG_MASTER_SAMPLE_RATE_QUICK_SETUP;
    i2c->Write(i2cAddr, (uint8_t *)&samplerate_setup, 1);

    // Sets up DAI for I2S master mode operation.
    max98090_reg_dai_quick_setup_t q_dai_setup = {};
    q_dai_setup.i2sm                           = 1;
    i2cAddr.subAddress                         = MAX98090_REG_DAI_QUICK_SETUP;
    i2c->Write(i2cAddr, (uint8_t *)&q_dai_setup, 1);

    // OUT configuration
    if (params.outputPath != CodecParamsMAX98090::OutputPath::None) {

        // Control HP performance
        max98090_reg_dachp_perfmode_t dacperf = {};
        dacperf.dachp                         = 1;
        dacperf.perfmode                      = 0;
        i2cAddr.subAddress                    = MAX98090_REG_DACHP_PERF_MODE;
        i2c->Write(i2cAddr, (uint8_t *)&dacperf, 1);

        switch (params.outputPath) {

        case CodecParamsMAX98090::OutputPath::HeadphonesMono: {
            // Set output route
            max98090_reg_playback_quick_setup_t q_playback_setup = {};
            q_playback_setup.dig2hp                              = 1;
            i2cAddr.subAddress                                   = MAX98090_REG_PLAYBACK_QUICK_SETUP;
            i2c->Write(i2cAddr, (uint8_t *)&q_playback_setup, 1);

            // Mix left DAC channel to left&right HP output
            max98090_reg_lhp_mixer_t lmixconf = {};
            lmixconf.mixhpl                   = 1;
            i2cAddr.subAddress                = MAX98090_REG_LHP_MIXER_CONF;
            i2c->Write(i2cAddr, (uint8_t *)&lmixconf, 1);

            max98090_reg_rhp_mixer_t rmixconf = {};
            rmixconf.mixhpr                   = 1;
            i2cAddr.subAddress                = MAX98090_REG_RHP_MIXER_CONF;
            i2c->Write(i2cAddr, (uint8_t *)&rmixconf, 1);

            // Use mixer outputs instead of direct DAC outputs
            max98090_reg_hpmix_conf_t mixconf = {};
            mixconf.mixhplsel                 = 1;
            mixconf.mixhprsel                 = 1;
            i2cAddr.subAddress                = MAX98090_REG_HP_MIX_CONF;
            i2c->Write(i2cAddr, (uint8_t *)&mixconf, 1);
            SetFilterMode(FilterMode::Voice);
        } break;

        case CodecParamsMAX98090::OutputPath::Headphones: {
            max98090_reg_playback_quick_setup_t q_playback_setup = {};
            q_playback_setup.dig2hp                              = 1;
            i2cAddr.subAddress                                   = MAX98090_REG_PLAYBACK_QUICK_SETUP;
            i2c->Write(i2cAddr, (uint8_t *)&q_playback_setup, 1);

        } break;

        case CodecParamsMAX98090::OutputPath::Earspeaker: {
            max98090_reg_playback_quick_setup_t q_playback_setup = {};
            q_playback_setup.dig2ear                             = 1;
            i2cAddr.subAddress                                   = MAX98090_REG_PLAYBACK_QUICK_SETUP;
            i2c->Write(i2cAddr, (uint8_t *)&q_playback_setup, 1);

            constexpr uint8_t coarse = 2;
            constexpr uint8_t fine   = 2;
            SetPlaybackPath(coarse, fine);
            SetFilterMode(FilterMode::Voice);
        } break;

        case CodecParamsMAX98090::OutputPath::Loudspeaker: {
            max98090_reg_playback_quick_setup_t q_playback_setup = {};
            q_playback_setup.dig2spk                             = 1;
            i2cAddr.subAddress                                   = MAX98090_REG_PLAYBACK_QUICK_SETUP;
            i2c->Write(i2cAddr, (uint8_t *)&q_playback_setup, 1);

            uint8_t mask       = 0x08; // Set 3th bit (dmono on)
            i2cAddr.subAddress = MAX98090_REG_INOUT_PATH_CONF;
            i2c->Modify(i2cAddr, mask, true, 1);

            // Turn off right speaker path
            max98090_reg_outputenable_t outputenable = {};
            outputenable.dalen                       = 1;
            outputenable.splen                       = 1;
            i2cAddr.subAddress                       = MAX98090_REG_OUTPUT_ENABLE;
            i2c->Write(i2cAddr, (uint8_t *)&outputenable, 1);

            constexpr uint8_t coarse = 0;
            constexpr uint8_t fine   = 2;
            SetPlaybackPath(coarse, fine);
        } break;

        case CodecParamsMAX98090::OutputPath::LoudspeakerMono: {
            max98090_reg_playback_quick_setup_t q_playback_setup = {};
            q_playback_setup.dig2spk                             = 1;
            i2cAddr.subAddress                                   = MAX98090_REG_PLAYBACK_QUICK_SETUP;
            i2c->Write(i2cAddr, (uint8_t *)&q_playback_setup, 1);

            // Turn off right speaker path
            max98090_reg_outputenable_t outputenable = {};
            outputenable.dalen                       = 1;
            outputenable.splen                       = 1;
            i2cAddr.subAddress                       = MAX98090_REG_OUTPUT_ENABLE;
            i2c->Write(i2cAddr, (uint8_t *)&outputenable, 1);

            constexpr uint8_t coarse = 0;
            constexpr uint8_t fine   = 5;
            SetPlaybackPath(coarse, fine);
            SetFilterMode(FilterMode::Voice);
        } break;

        default:
            return CodecRetCode::InvalidOutputPath;
        }
    }

    // IN configuration
    if (params.inputPath != CodecParamsMAX98090::InputPath::None) {
        // Set input path
        switch (params.inputPath) {

        case CodecParamsMAX98090::InputPath::Headphones: {
            max98090_reg_input_to_record_quick_t q_input_setup = {};
            q_input_setup.in34dan                              = 1;
            i2cAddr.subAddress                                 = MAX98090_REG_LINE_INPUT_TO_RECORD_QUICK;
            i2c->Write(i2cAddr, (uint8_t *)&q_input_setup, 1);
        } break;

        case CodecParamsMAX98090::InputPath::Microphone: {
            max98090_reg_input_to_record_quick_t q_input_setup = {};
            max98090_reg_digmic_enable_t digena                = {};
            max98090_reg_digmic_conf_t digconf                 = {};

            // Enable left and right digital mic interface
            digena.digmicl = 1;
            digena.digmicr = 1;
            // Harman Kardon dig microphones specify valid clock range as 1.024MHz - 4.8MHz, typical ~2.4MHz
            digena.dmicclk = 3; // fDMC = fPCLK/5 - > fPCLK=12.288MHz fDMC = 2.458MHz

            // Table 17 from datasheet
            digconf.dmicfreq = 0;
            switch (currentParams.GetSampleRateVal()) {
            case 8000:
                digconf.dmiccomp =
                    static_cast<uint8_t>(CodecParamsMAX98090::DigitalMicrophoneCompensationFilter::DMIC_COMP_7);
                break;
            case 16000:
                digconf.dmiccomp =
                    static_cast<uint8_t>(CodecParamsMAX98090::DigitalMicrophoneCompensationFilter::DMIC_COMP_8);
                break;
            case 32000:
            case 44100:
            case 48000:
                digconf.dmiccomp =
                    static_cast<uint8_t>(CodecParamsMAX98090::DigitalMicrophoneCompensationFilter::DMIC_COMP_6);
                break;
            case 96000:
            case 0:
            default:
                digconf.dmiccomp =
                    static_cast<uint8_t>(CodecParamsMAX98090::DigitalMicrophoneCompensationFilter::DMIC_COMP_6);
                break;
            }

            i2cAddr.subAddress = MAX98090_REG_DIG_MIC_ENABLE;
            i2c->Write(i2cAddr, (uint8_t *)&digena, 1);

            i2cAddr.subAddress = MAX98090_REG_DIG_MIC_CONF;
            i2c->Write(i2cAddr, (uint8_t *)&digconf, 1);

            q_input_setup.in12sab = 1;
            i2cAddr.subAddress    = MAX98090_REG_LINE_INPUT_TO_RECORD_QUICK;
            i2c->Write(i2cAddr, (uint8_t *)&q_input_setup, 1);

        } break;

        default:
            return CodecRetCode::InvalidInputPath;
        }
    }

    // Turn on DC blocking filters
    uint8_t mask       = (1 << 6) | (1 << 5); // set 6th and 7th bit (AHPF and DHPF)
    i2cAddr.subAddress = MAX98090_REG_PLAYBACK_DSP_FILTER_CONF;
    i2c->Modify(i2cAddr, mask, true, 1);

    // Store param configuration
    currentParams = params;

    // Set volume to 0 before enabling codec to avoid pops/clicks
    auto currVol = currentParams.outVolume;
    SetOutputVolume(0);

    SetInputGain(currentParams.inGain);

    // Turn on device
    dev_shutdown.shdn = 1;

    i2cAddr.subAddress = MAX98090_REG_DEVICE_SHUTDOWN;
    i2c->Write(i2cAddr, (uint8_t *)&dev_shutdown, 1);

    SetOutputVolume(currVol);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::Pause()
{
    // Turn off device
    max98090_reg_shutdown_t dev_shutdown = {};
    i2cAddr.subAddress                   = MAX98090_REG_DEVICE_SHUTDOWN;
    i2c->Write(i2cAddr, (uint8_t *)&dev_shutdown, 1);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::Resume()
{
    // Turn on device
    max98090_reg_shutdown_t dev_shutdown = {.unused = 0, .shdn = 1};
    i2cAddr.subAddress                   = MAX98090_REG_DEVICE_SHUTDOWN;
    i2c->Write(i2cAddr, (uint8_t *)&dev_shutdown, 1);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::Stop()
{
    Reset();
    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::Ioctrl(const CodecParams &param)
{

    const CodecParamsMAX98090 &params = static_cast<const CodecParamsMAX98090 &>(param);

    CodecRetCode ret = CodecRetCode::Success;

    switch (params.opCmd) {
    case CodecParamsMAX98090::Cmd::SetOutVolume:
        ret = SetOutputVolume(params.outVolume);
        break;

    case CodecParamsMAX98090::Cmd::SetInGain:
        ret = SetInputGain(params.inGain);
        break;
    case CodecParamsMAX98090::Cmd::SetInput:
        ret = SetInputPath(params.inputPath);
        break;
    case CodecParamsMAX98090::Cmd::SetOutput:
        ret = SetOutputPath(params.outputPath);
        break;
    case CodecParamsMAX98090::Cmd::MicBiasCtrl:
        ret = MicBias(params.micBiasEnable);
        break;
    case CodecParamsMAX98090::Cmd::Reset:
        ret = Reset();
        break;
    case CodecParamsMAX98090::Cmd::SetMute:
        ret = SetMute(params.muteEnable);
        break;
    default:
        break;
    }

    return ret;
}

void CodecMAX98090::SetEqualizerParameters(float b0, float b1, float b2, float a1, float a2, std::size_t band)
{
    std::map<std::size_t, std::array<uint8_t, 5>> filterRegs = {
        {0, {0x46, 0x49, 0x4C, 0x4F, 0x52}},
        {1, {0x55, 0x58, 0x5B, 0x5E, 0x61}},
        {2, {0x64, 0x67, 0x6A, 0x6D, 0x70}},
        {3, {0x73, 0x76, 0x79, 0x7C, 0x7F}},
        {4, {0x82, 0x85, 0x88, 0x8B, 0x8E}},
        {5, {0x91, 0x94, 0x97, 0x9A, 0x9D}},
        {6, {0xA0, 0xA3, 0xA6, 0xA9, 0xAC}},
    };
    WriteFilterCoeff(b0, filterRegs[band].at(0));
    WriteFilterCoeff(b1, filterRegs[band].at(1));
    WriteFilterCoeff(b2, filterRegs[band].at(2));
    WriteFilterCoeff(a1, filterRegs[band].at(3));
    WriteFilterCoeff(a2, filterRegs[band].at(4));
}

CodecRetCode CodecMAX98090::SetOutputVolume(const float vol)
{
    uint8_t mute = 0;

    // If volume set to 0 then mute output
    if (vol == 0) {
        mute = 1;
    }

    switch (currentParams.outputPath) {
    case CodecParamsMAX98090::OutputPath::Headphones:
    case CodecParamsMAX98090::OutputPath::HeadphonesMono: {
        // Scale input volume(range 0 - 10) to MAX98090 range(decibels hardcoded as specific hex values)
        // @note:
        // In order to pass certification max value is limited and taken from measurements 0x0B: -28dB
        // Be carefull when changing it !!!
        constexpr auto scale_factor      = 1.24f;
        uint8_t volume                   = static_cast<float>(vol * scale_factor);
        max98090_reg_lhp_vol_ctrl_t lvol = {};
        max98090_reg_rhp_vol_ctrl_t rvol = {};

        lvol.hplm   = mute;
        rvol.hprm   = mute;
        lvol.hpvoll = volume;
        rvol.hpvolr = volume;

        i2cAddr.subAddress = MAX98090_REG_LHP_VOL_CTRL;
        i2c->Write(i2cAddr, (uint8_t *)&lvol, 1);
        i2cAddr.subAddress = MAX98090_REG_RHP_VOL_CTRL;
        i2c->Write(i2cAddr, (uint8_t *)&rvol, 1);

    } break;

    case CodecParamsMAX98090::OutputPath::Earspeaker: {
        // Scale input volume(range 0 - 10) to MAX98090 range(decibels hardcoded as specific hex values)
        constexpr auto scale_factor      = 3.1f; // take the whole scale
        uint8_t volume                   = static_cast<float>(vol * scale_factor);
        max98090_reg_recv_vol_ctrl_t vol = {};

        vol.rcvlm   = mute;
        vol.rcvlvol = volume;

        i2cAddr.subAddress = MAX98090_REG_RECV_VOL_CTRL;
        i2c->Write(i2cAddr, (uint8_t *)&vol, 1);
    } break;

    case CodecParamsMAX98090::OutputPath::Loudspeaker:
    case CodecParamsMAX98090::OutputPath::LoudspeakerMono: {
        // Scale input volume(range 0 - 10) to MAX98090 range(decibels hardcoded as specific hex values)
        constexpr auto scale_factor = 3.9f;
        uint8_t volume              = static_cast<float>(vol * scale_factor) + 0x18;

        max98090_reg_lspk_vol_ctrl_t lvol = {};
        max98090_reg_rspk_vol_ctrl_t rvol = {};

        lvol.splm   = mute;
        rvol.sprm   = mute;
        lvol.spvoll = volume;
        rvol.spvolr = volume;

        i2cAddr.subAddress = MAX98090_REG_LSPK_VOL_CTRL;
        i2c->Write(i2cAddr, (uint8_t *)&lvol, 1);

        i2cAddr.subAddress = MAX98090_REG_RSPK_VOL_CTRL;
        i2c->Write(i2cAddr, (uint8_t *)&rvol, 1);
    } break;

    default:
        return CodecRetCode::InvalidArgument;
    }

    currentParams.outVolume = vol;
    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::SetInputGain(const float gain)
{
    constexpr float scaleFactor = .1f;
    float gainToSet             = gain * scaleFactor;

    if (gain > 10) {
        gainToSet = 10;
    }

    max98090_reg_lrec_dig_gain_t lgain = {};
    lgain.avl  = static_cast<uint8_t>(CodecParamsMAX98090::RecordPathDigitalFineGain::Gain_p3dB); // fine gain
    lgain.avlg = gainToSet * 0.7; // coarse gain (0.7 used as scaling factor)

    i2cAddr.subAddress = MAX98090_REG_LREC_DIG_GAIN;
    i2c->Write(i2cAddr, (uint8_t *)&lgain, 1);

    // coarse gain - 18dB, fine gain - 0dB
    max98090_reg_rrec_dig_gain_t rgain = {};
    rgain.avr  = static_cast<uint8_t>(CodecParamsMAX98090::RecordPathDigitalFineGain::Gain_p3dB); // fine gain
    rgain.avrg = gainToSet * 0.7; // coarse gain (0.7 used as scaling factor)

    i2cAddr.subAddress = MAX98090_REG_RREC_DIG_GAIN;
    i2c->Write(i2cAddr, (uint8_t *)&rgain, 1);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::WriteFilterCoeff(const float coeff, const uint8_t basereg)
{
    const auto [byte1, byte2, byte3] = utils::floatingPointConverter(coeff);

    i2cAddr.subAddress = basereg;
    i2c->Write(i2cAddr, (uint8_t *)&byte1, 1);
    i2cAddr.subAddress = basereg + 1;
    i2c->Write(i2cAddr, (uint8_t *)&byte2, 1);
    i2cAddr.subAddress = basereg + 2;
    i2c->Write(i2cAddr, (uint8_t *)&byte3, 1);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::MicBias(const bool enable)
{
    max98090_reg_bias_mode_t biasmode = {};
    // BIAS created by bandgap reference
    biasmode.biasmode = 1;

    i2cAddr.subAddress = MAX98090_REG_BIAS_CTRL;
    i2c->Write(i2cAddr, (uint8_t *)&biasmode, 1);

    // Turn on microphone bias(needed for jack detection and powering external analog headphones mic)
    uint8_t mask = 0x10; // Set 4th bit (mic bias enable/disable)

    i2cAddr.subAddress = MAX98090_REG_INPUT_ENABLE;
    i2c->Modify(i2cAddr, mask, enable, 1);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::Reset()
{

    // Turn off device
    max98090_reg_shutdown_t dev_shutdown = {.unused = 0, .shdn = 0};

    i2cAddr.subAddress = MAX98090_REG_DEVICE_SHUTDOWN;
    i2c->Write(i2cAddr, (uint8_t *)&dev_shutdown, 1);

    // Set all registers to default state
    max98090_reg_swreset_t reset = {};
    reset.swreset                = 1;
    i2cAddr.subAddress           = MAX98090_REG_SWRESET;
    i2c->Write(i2cAddr, (uint8_t *)&reset, 1);

    return CodecRetCode::Success;
}

void CodecMAX98090::SetPlaybackPath(uint8_t coarse, uint8_t fine)
{
    max98090_reg_playback_gainlevel_conf_t gain = {};
    gain.dvg                                    = coarse;
    gain.dv                                     = fine;
    i2cAddr.subAddress                          = MAX98090_REG_PLAYBACK_GAIN_LEVEL_CONF;
    i2c->Write(i2cAddr, (uint8_t *)&gain, 1);
}

void CodecMAX98090::SetFilterMode(FilterMode mode)
{
    constexpr uint8_t mask = (1 << 7);
    i2cAddr.subAddress     = MAX98090_REG_PLAYBACK_DSP_FILTER_CONF;
    i2c->Modify(i2cAddr, mask, static_cast<bool>(mode), 1);
}

void CodecMAX98090::EnableFilterBands(std::size_t bandsCount)
{
    max98090_reg_dsp_biquadfilter_enable_t filter = {};
    switch (bandsCount) {
    case 3:
        filter.eq3banden = 1;
        break;
    case 5:
        filter.eq5banden = 1;
        break;
    case 7:
        filter.eq7banden = 1;
        break;
    default:
        return;
    }
    i2cAddr.subAddress = MAX98090_REG_DSP_BIQUAD_FILTER_ENABLE;
    i2c->Write(i2cAddr, (uint8_t *)&filter, 1);
}

CodecRetCode CodecMAX98090::SetOutputPath(const CodecParamsMAX98090::OutputPath path)
{
    Reset();
    currentParams.outputPath = path;
    Start(currentParams);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::SetInputPath(const CodecParamsMAX98090::InputPath path)
{
    Reset();
    currentParams.inputPath = path;
    Start(currentParams);

    return CodecRetCode::Success;
}

CodecRetCode CodecMAX98090::SetMute(const bool enable)
{
    // Set/clr 7th bit in register which controls mute/unmute
    uint8_t mask = 0x80;
    uint8_t regl, regr = 0;

    switch (currentParams.outputPath) {
    case CodecParamsMAX98090::OutputPath::Headphones: {
        regl = MAX98090_REG_LHP_VOL_CTRL;
        regr = MAX98090_REG_RHP_VOL_CTRL;
    } break;

    case CodecParamsMAX98090::OutputPath::Earspeaker: {
        regl = MAX98090_REG_RECV_VOL_CTRL;
        regr = 0;
    } break;

    case CodecParamsMAX98090::OutputPath::Loudspeaker: {
        regl = MAX98090_REG_LSPK_VOL_CTRL;
        regr = MAX98090_REG_RSPK_VOL_CTRL;
    } break;

    default:
        return CodecRetCode::InvalidArgument;
    }

    if (regl) {
        i2cAddr.subAddress = regl;
        i2c->Modify(i2cAddr, mask, enable, 1);
    }
    if (regr) {
        i2cAddr.subAddress = regr;
        i2c->Modify(i2cAddr, mask, enable, 1);
    }
    return CodecRetCode::Success;
}

std::optional<uint32_t> CodecMAX98090::Probe()
{
    uint8_t id = 0;

    i2cAddr.subAddress = MAX98090_REG_REVID;
    i2c->Write(i2cAddr, (uint8_t *)&id, 1);
    return id;
}