boards/xtensa/esp32s3: Refactor ES8311 initialization to use I2C handle

Refactor esp32s3_es8311_initialize function to accept I2C handle instead
of port number, moving I2C bus initialization responsibility to the board
bringup code. This change improves flexibility by allowing boards to
manage I2C initialization while the ES8311 driver focuses on audio
codec configuration.

* Updated function signature to accept struct i2c_master_s *i2c parameter
* Removed I2C bus initialization from ES8311 driver
* Updated function documentation to reflect new parameter
* Modified esp32s3-lcd-ev and esp32s3-korvo-2 boards to initialize I2C
  before calling ES8311 initialization
* Added proper error handling for I2C initialization failures
* Moved variable declarations to function start for C89 compliance
* Fixed code style issues including line length and indentation

This change maintains backward compatibility for existing functionality
while providing better separation of concerns between I2C bus management
and audio codec configuration.

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi
2025-09-22 22:08:22 +08:00
committed by Alin Jerpelea
parent d598f97746
commit 98415626e3
5 changed files with 54 additions and 20 deletions
@@ -65,7 +65,7 @@ static struct es8311_lower_s g_es8311_lower[2];
* as /dev/audio/pcm[x] where x is determined by the I2S port number.
*
* Input Parameters:
* i2c_port - The I2C port used for the device
* i2c - The I2C handle used for the device
* i2c_addr - The I2C address used by the device
* i2c_freq - The I2C frequency used for the device
* i2s_port - The I2S port used for the device
@@ -76,17 +76,16 @@ static struct es8311_lower_s g_es8311_lower[2];
*
****************************************************************************/
int esp32s3_es8311_initialize(int i2c_port, uint8_t i2c_addr, int i2c_freq,
int i2s_port)
int esp32s3_es8311_initialize(struct i2c_master_s *i2c, uint8_t i2c_addr,
int i2c_freq, int i2s_port)
{
struct audio_lowerhalf_s *es8311;
struct i2s_dev_s *i2s;
struct i2c_master_s *i2c;
static bool initialized = false;
int ret;
audinfo("i2c_port %d, i2c_addr %d, i2c_freq %d\n",
i2c_port, i2c_addr, i2c_freq);
audinfo("i2c_addr %d, i2c_freq %d\n",
i2c_addr, i2c_freq);
/* Have we already initialized? Since we never uninitialize we must
* prevent multiple initializations. This is necessary, for example,
@@ -107,10 +106,9 @@ int esp32s3_es8311_initialize(int i2c_port, uint8_t i2c_addr, int i2c_freq,
goto errout;
}
i2c = esp32s3_i2cbus_initialize(i2c_port);
if (i2c == NULL)
{
auderr("ERROR: Failed to initialize I2C%d\n", i2c_port);
auderr("ERROR: I2C handle is NULL\n");
ret = -ENODEV;
goto errout;
}
@@ -30,6 +30,7 @@
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <stdint.h>
#include <nuttx/i2c/i2c_master.h>
/****************************************************************************
* Pre-processor Definitions
@@ -100,7 +101,7 @@ int board_spiflash_init(void);
* as /dev/audio/pcm[x] where x is determined by the I2S port number.
*
* Input Parameters:
* i2c_port - The I2C port used for the device
* i2c - The I2C handle used for the device
* i2c_addr - The I2C address used by the device
* i2c_freq - The I2C frequency used for the device
* i2s_port - The I2S port used for the device
@@ -112,8 +113,8 @@ int board_spiflash_init(void);
****************************************************************************/
#ifdef CONFIG_AUDIO_ES8311
int esp32s3_es8311_initialize(int i2c_port, uint8_t i2c_addr, int i2c_freq,
int i2s_port);
int esp32s3_es8311_initialize(struct i2c_master_s *i2c, uint8_t i2c_addr,
int i2c_freq, int i2s_port);
#endif
#endif /* __ASSEMBLY__ */
@@ -140,12 +140,15 @@
int esp32s3_bringup(void)
{
int ret;
int ret = OK;
#if (defined(CONFIG_ESPRESSIF_I2S0) && !defined(CONFIG_AUDIO_CS4344) && \
!defined(CONFIG_AUDIO_ES8311)) || defined(CONFIG_ESPRESSIF_I2S1)
bool i2s_enable_tx;
bool i2s_enable_rx;
#endif
#ifdef CONFIG_AUDIO_ES8311
struct i2c_master_s *i2c;
#endif
#if defined(CONFIG_ESP32S3_SPIRAM) && \
defined(CONFIG_ESP32S3_SPIRAM_BANKSWITCH_ENABLE)
@@ -311,8 +314,22 @@ int esp32s3_bringup(void)
esp32s3_configgpio(SPEAKER_ENABLE_GPIO, OUTPUT);
esp32s3_gpiowrite(SPEAKER_ENABLE_GPIO, true);
ret = esp32s3_es8311_initialize(ESP32S3_I2C0, ES8311_I2C_ADDR,
ES8311_I2C_FREQ, ESP32S3_I2S0);
i2c = esp32s3_i2cbus_initialize(ESP32S3_I2C0);
if (i2c == NULL)
{
syslog(LOG_ERR, "Failed to initialize I2C%d\n", ESP32S3_I2C0);
ret = -ENODEV;
}
else
{
ret = esp32s3_es8311_initialize(i2c, ES8311_I2C_ADDR,
ES8311_I2C_FREQ, ESP32S3_I2S0);
if (ret != OK)
{
syslog(LOG_ERR, "Failed to initialize ES8311 audio: %d\n", ret);
}
}
if (ret != OK)
{
syslog(LOG_ERR, "Failed to initialize ES8311 audio: %d\n", ret);
@@ -30,6 +30,7 @@
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <stdint.h>
#include <nuttx/i2c/i2c_master.h>
/****************************************************************************
* Pre-processor Definitions
@@ -207,7 +208,7 @@ int board_ioexpander_initialize(void);
* as /dev/audio/pcm[x] where x is determined by the I2S port number.
*
* Input Parameters:
* i2c_port - The I2C port used for the device
* i2c - The I2C handle used for the device
* i2c_addr - The I2C address used by the device
* i2c_freq - The I2C frequency used for the device
* i2s_port - The I2S port used for the device
@@ -219,8 +220,8 @@ int board_ioexpander_initialize(void);
****************************************************************************/
#ifdef CONFIG_AUDIO_ES8311
int esp32s3_es8311_initialize(int i2c_port, uint8_t i2c_addr, int i2c_freq,
int i2s_port);
int esp32s3_es8311_initialize(struct i2c_master_s *i2c, uint8_t i2c_addr,
int i2c_freq, int i2s_port);
#endif
#endif /* __ASSEMBLY__ */
@@ -111,12 +111,15 @@
int esp32s3_bringup(void)
{
int ret;
int ret = OK;
#if (defined(CONFIG_ESPRESSIF_I2S0) && !defined(CONFIG_AUDIO_CS4344) && \
!defined(CONFIG_AUDIO_ES8311)) || defined(CONFIG_ESPRESSIF_I2S1)
bool i2s_enable_tx;
bool i2s_enable_rx;
#endif
#ifdef CONFIG_AUDIO_ES8311
struct i2c_master_s *i2c;
#endif
#if defined(CONFIG_ESPRESSIF_EFUSE)
ret = esp_efuse_initialize("/dev/efuse");
@@ -195,8 +198,22 @@ int esp32s3_bringup(void)
esp32s3_configgpio(SPEAKER_ENABLE_GPIO, OUTPUT);
esp32s3_gpiowrite(SPEAKER_ENABLE_GPIO, true);
ret = esp32s3_es8311_initialize(ESP32S3_I2C0, ES8311_I2C_ADDR,
ES8311_I2C_FREQ, ESP32S3_I2S0);
i2c = esp32s3_i2cbus_initialize(ESP32S3_I2C0);
if (i2c == NULL)
{
syslog(LOG_ERR, "Failed to initialize I2C%d\n", ESP32S3_I2C0);
ret = -ENODEV;
}
else
{
ret = esp32s3_es8311_initialize(i2c, ES8311_I2C_ADDR,
ES8311_I2C_FREQ, ESP32S3_I2S0);
if (ret != OK)
{
syslog(LOG_ERR, "Failed to initialize ES8311 audio: %d\n", ret);
}
}
if (ret != OK)
{
syslog(LOG_ERR, "Failed to initialize ES8311 audio: %d\n", ret);