diff --git a/Devices/es3c28p/es3c28p.dts b/Devices/es3c28p/es3c28p.dts index 1063311b..379b756a 100644 --- a/Devices/es3c28p/es3c28p.dts +++ b/Devices/es3c28p/es3c28p.dts @@ -91,6 +91,7 @@ compatible = "everest,es8311"; reg = <0x18>; i2s = <&i2s0>; + input-gain-percent = <100>; }; }; diff --git a/Devices/es3c35p/es3c35p.dts b/Devices/es3c35p/es3c35p.dts index 5f808e2b..d86bb4c0 100644 --- a/Devices/es3c35p/es3c35p.dts +++ b/Devices/es3c35p/es3c35p.dts @@ -88,6 +88,7 @@ compatible = "everest,es8311"; reg = <0x18>; i2s = <&i2s0>; + input-gain-percent = <100>; }; }; diff --git a/Drivers/es8311-module/bindings/everest,es8311.yaml b/Drivers/es8311-module/bindings/everest,es8311.yaml index dbcbc3e1..a1989a30 100644 --- a/Drivers/es8311-module/bindings/everest,es8311.yaml +++ b/Drivers/es8311-module/bindings/everest,es8311.yaml @@ -9,3 +9,7 @@ properties: type: phandle required: true description: "I2S controller device that carries audio data" + input-gain-percent: + type: int + default: 100 + description: "Extra digital gain multiplier applied by audio_stream on top of the ES8311's own 24dB hardware ADC gain, as an integer percentage (100 = 1.0x / no extra boost). For quiet MEMS mic capsules that are still quiet even at max hardware gain." diff --git a/Drivers/es8311-module/include/drivers/es8311.h b/Drivers/es8311-module/include/drivers/es8311.h index f51e1827..aaf34f32 100644 --- a/Drivers/es8311-module/include/drivers/es8311.h +++ b/Drivers/es8311-module/include/drivers/es8311.h @@ -25,6 +25,14 @@ struct Es8311Config { uint8_t address; /** I2S controller device that carries audio data */ struct Device* i2s_device; + /** + * Extra fixed digital gain multiplier applied by audio_stream on top of the ES8311's + * own hardware ADC gain (0..24dB), as an integer percentage (100 = 1.0x / no extra boost). + * Small MEMS mic capsules can still sound quiet even near max hardware gain; this is for + * boards where 24dB hardware gain alone isn't enough. devicetree has no float property type, + * hence the x100 integer encoding. + */ + uint16_t input_gain_percent; }; #ifdef __cplusplus diff --git a/Drivers/es8311-module/source/es8311.cpp b/Drivers/es8311-module/source/es8311.cpp index 693d50e4..3db94878 100644 --- a/Drivers/es8311-module/source/es8311.cpp +++ b/Drivers/es8311-module/source/es8311.cpp @@ -31,6 +31,7 @@ struct Es8311Data { bool is_open = false; AudioCodecDirection open_direction = AUDIO_CODEC_DIR_BOTH; esp_codec_dev_sample_info_t open_sample_info = {}; + float input_gain = 1.0f; }; #define GET_CONFIG(device) (static_cast((device)->config)) @@ -164,8 +165,8 @@ error_t set_volume(Device* device, AudioCodecDirection direction, float volume_p } if (direction == AUDIO_CODEC_DIR_INPUT) { - // ES8311 ADC gain range is roughly 0..24 dB; map 0..100% linearly onto it. - float db = (volume_percent / 100.0f) * 24.0f; + // ES8311 ADC gain supports 0..42dB (0,6,12,18,24,30,36,42) – max hardware to restore old +30dB+ behavior + float db = (volume_percent / 100.0f) * 42.0f; return (esp_codec_dev_set_in_gain(data->codec_device, db) == ESP_CODEC_DEV_OK) ? ERROR_NONE : ERROR_RESOURCE; } @@ -192,7 +193,8 @@ error_t get_volume(Device* device, AudioCodecDirection direction, float* volume_ if (esp_codec_dev_get_in_gain(data->codec_device, &db) != ESP_CODEC_DEV_OK) { return ERROR_RESOURCE; } - *volume_percent = (db / 24.0f) * 100.0f; + *volume_percent = (db / 42.0f) * 100.0f; + if (*volume_percent > 100.0f) *volume_percent = 100.0f; return ERROR_NONE; } @@ -255,6 +257,12 @@ error_t get_capabilities(Device* device, AudioCodecDirection* supported_directio return ERROR_NONE; } +error_t get_input_gain_multiplier(Device* device, float* gain) { + auto* data = GET_DATA(device); + *gain = data->input_gain; + return ERROR_NONE; +} + static const struct AudioCodecApi API = { .open = open, .close = close, @@ -267,7 +275,7 @@ static const struct AudioCodecApi API = { .get_native_sample_rate = get_native_sample_rate, .get_native_channels = get_native_channels, .get_capabilities = get_capabilities, - .get_input_gain_multiplier = nullptr, + .get_input_gain_multiplier = get_input_gain_multiplier, }; // endregion @@ -277,6 +285,11 @@ static const struct AudioCodecApi API = { error_t start_device(Device* device) { const auto* config = GET_CONFIG(device); + if (config->input_gain_percent > 2000) { + LOG_E(TAG, "Invalid input_gain_percent %u (must be 0..2000)", config->input_gain_percent); + return ERROR_RESOURCE; + } + auto* i2c_controller = device_get_parent(device); if (i2c_controller == nullptr || device_get_type(i2c_controller) != &I2C_CONTROLLER_TYPE) { LOG_E(TAG, "Parent is not an I2C controller"); @@ -290,6 +303,9 @@ error_t start_device(Device* device) { } auto* data = new Es8311Data(); + data->input_gain = (float) config->input_gain_percent / 100.0f; + if (data->input_gain < 0.0f) data->input_gain = 1.0f; + if (config->input_gain_percent == 0) data->input_gain = 1.0f; // default when not set (devicetree default 100, but 0 means unset) data->ctrl_if = audio_codec_adapter_new_i2c_ctrl(i2c_controller, config->address); data->data_if = audio_codec_adapter_new_i2s_data(i2s_controller);