diff --git a/drivers/power/bq2425x.c b/drivers/power/bq2425x.c index 5039ad9ea92..025c3f980a9 100644 --- a/drivers/power/bq2425x.c +++ b/drivers/power/bq2425x.c @@ -99,7 +99,7 @@ struct bq2425x_dev_s { /* The common part of the battery driver visible to the upper-half driver */ - FAR const struct battery_charger_operations_s *ops; /* Battery operations */ + FAR const struct battery_charger_dev_s *dev; /* Battery device */ sem_t batsem; /* Enforce mutually exclusive access */ /* Data fields specific to the lower half BQ2425x driver follow */ @@ -781,48 +781,64 @@ FAR struct battery_charger_dev_s * FAR struct bq2425x_dev_s *priv; int ret; - /* Initialize the BQ2425x device structure */ + /* Allocate the BQ2425x device structure */ priv = (FAR struct bq2425x_dev_s *)kmm_zalloc(sizeof(struct bq2425x_dev_s)); - if (priv) + if (priv == NULL) { - /* Initialize the BQ2425x device structure */ + baterr("ERROR: Failed to allocate memory for bq2425x_dev_s!\n"); + return NULL; + } - nxsem_init(&priv->batsem, 0, 1); - priv->ops = &g_bq2425xops; - priv->i2c = i2c; - priv->addr = addr; - priv->frequency = frequency; + priv->dev = (FAR struct battery_charger_dev_s *) + kmm_zalloc(sizeof(struct battery_charger_dev_s)); - /* Reset the BQ2425x */ + if (priv->dev == NULL) + { + baterr("ERROR: Failed to allocate memory for battery_charger_dev_s!\n"); + kmm_free(priv); + return NULL; + } - ret = bq2425x_reset(priv); - if (ret < 0) - { - baterr("ERROR: Failed to reset the BQ2425x: %d\n", ret); - kmm_free(priv); - return NULL; - } + /* Initialize the BQ2425x device structure */ - /* Disable watchdog otherwise BQ2425x returns to StandAlone mode */ + nxsem_init(&priv->batsem, 0, 1); + priv->dev->ops = &g_bq2425xops; + priv->i2c = i2c; + priv->addr = addr; + priv->frequency = frequency; - ret = bq2425x_watchdog(priv, false); - if (ret < 0) - { - baterr("ERROR: Failed to disable BQ2425x watchdog: %d\n", ret); - kmm_free(priv); - return NULL; - } + /* Reset the BQ2425x */ - /* Define the current that our power supply can offer to the charger. */ + ret = bq2425x_reset(priv); + if (ret < 0) + { + baterr("ERROR: Failed to reset the BQ2425x: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; + } - ret = bq2425x_powersupply(priv, current); - if (ret < 0) - { - baterr("ERROR: Failed to set BQ2425x power supply input limit: %d\n", ret); - kmm_free(priv); - return NULL; - } + /* Disable watchdog otherwise BQ2425x returns to StandAlone mode */ + + ret = bq2425x_watchdog(priv, false); + if (ret < 0) + { + baterr("ERROR: Failed to disable BQ2425x watchdog: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; + } + + /* Define the current that our power supply can offer to the charger. */ + + ret = bq2425x_powersupply(priv, current); + if (ret < 0) + { + baterr("ERROR: Failed to set BQ2425x power supply input limit: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; } return (FAR struct battery_charger_dev_s *)priv; diff --git a/drivers/power/bq2429x.c b/drivers/power/bq2429x.c index 218f6f4541c..0c428a834a5 100644 --- a/drivers/power/bq2429x.c +++ b/drivers/power/bq2429x.c @@ -122,7 +122,7 @@ struct bq2429x_dev_s { /* The common part of the battery driver visible to the upper-half driver */ - FAR const struct battery_charger_operations_s *ops; /* Battery operations */ + FAR const struct battery_charger_dev_s *dev; /* Battery device */ sem_t batsem; /* Enforce mutually exclusive access */ /* Data fields specific to the lower half BQ2429X driver follow */ @@ -1251,58 +1251,74 @@ FAR struct battery_charger_dev_s * FAR struct bq2429x_dev_s *priv; int ret; - /* Initialize the BQ2429x device structure */ + /* Allocate the BQ2429x device structure */ priv = (FAR struct bq2429x_dev_s *)kmm_zalloc(sizeof(struct bq2429x_dev_s)); - if (priv) + if (priv == NULL) { - /* Initialize the BQ2429x device structure */ + baterr("ERROR: Failed to allocate memory for bq2429x_dev_s!\n"); + return NULL; + } - nxsem_init(&priv->batsem, 0, 1); - priv->ops = &g_bq2429xops; - priv->i2c = i2c; - priv->addr = addr; - priv->frequency = frequency; + priv->dev = (FAR struct battery_charger_dev_s *) + kmm_zalloc(sizeof(struct battery_charger_dev_s)); + if (priv->dev == NULL) + { + baterr("ERROR: Failed to allocate memory for battery_charger_dev_s!\n"); + kmm_free(priv); + return NULL; + } - /* Reset the BQ2429x */ + /* Initialize the BQ2429x device structure */ - ret = bq2429x_reset(priv); - if (ret < 0) - { - baterr("ERROR: Failed to reset the BQ2429x: %d\n", ret); - kmm_free(priv); - return NULL; - } + nxsem_init(&priv->batsem, 0, 1); + priv->dev->ops = &g_bq2429xops; + priv->i2c = i2c; + priv->addr = addr; + priv->frequency = frequency; - /* Disable watchdog otherwise BQ2429x returns to StandAlone mode */ + /* Reset the BQ2429x */ - ret = bq2429x_watchdog(priv, false); - if (ret < 0) - { - baterr("ERROR: Failed to disable BQ2429x watchdog: %d\n", ret); - kmm_free(priv); - return NULL; - } + ret = bq2429x_reset(priv); + if (ret < 0) + { + baterr("ERROR: Failed to reset the BQ2429x: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; + } - /* Define the current that our power supply can offer to the charger. */ + /* Disable watchdog otherwise BQ2429x returns to StandAlone mode */ - ret = bq2429x_powersupply(priv, current); - if (ret < 0) - { - baterr("ERROR: Failed to set BQ2429x power supply input limit: %d\n", ret); - kmm_free(priv); - return NULL; - } + ret = bq2429x_watchdog(priv, false); + if (ret < 0) + { + baterr("ERROR: Failed to disable BQ2429x watchdog: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; + } - /* Disable all interrupts. */ + /* Define the current that our power supply can offer to the charger. */ - ret = bq2429x_en_stat(priv, false); - if (ret < 0) - { - baterr("ERROR: Failed to disable BQ2429x interrupts: %d\n", ret); - kmm_free(priv); - return NULL; - } + ret = bq2429x_powersupply(priv, current); + if (ret < 0) + { + baterr("ERROR: Failed to set BQ2429x power supply input limit: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; + } + + /* Disable all interrupts. */ + + ret = bq2429x_en_stat(priv, false); + if (ret < 0) + { + baterr("ERROR: Failed to disable BQ2429x interrupts: %d\n", ret); + kmm_free(priv->dev); + kmm_free(priv); + return NULL; } return (FAR struct battery_charger_dev_s *)priv; diff --git a/drivers/power/mcp73871.c b/drivers/power/mcp73871.c index 74a5c2f483f..3b99c3b0dee 100644 --- a/drivers/power/mcp73871.c +++ b/drivers/power/mcp73871.c @@ -93,13 +93,14 @@ struct mcp73871_dev_s { + /* The common part of the battery driver visible to the upper-half driver */ + + FAR struct battery_charger_dev_s *dev; + /* MCP73871 configuration helpers */ FAR struct mcp73871_config_s *config; - /* The common part of the battery driver visible to the upper-half driver */ - - FAR const struct battery_charger_operations_s *ops; /* Battery operations */ sem_t batsem; /* Enforce mutually exclusive access */ }; @@ -399,24 +400,35 @@ FAR struct battery_charger_dev_s * { FAR struct mcp73871_dev_s *priv; - /* Initialize the BQ2425x device structure */ + /* Allocate the MCP73871 device structure */ - priv = (FAR struct mcp73871_dev_s *) - kmm_zalloc(sizeof(struct mcp73871_dev_s)); - - if (priv) + priv = (FAR struct mcp73871_dev_s *)kmm_zalloc(sizeof(struct mcp73871_dev_s)); + if (priv == NULL) { - /* Initialize the BQ2425x device structure */ - - nxsem_init(&priv->batsem, 0, 1); - priv->ops = &g_mcp73871ops; - priv->config = config; - - /* Enable the battery charge */ - - priv->config->set_chg_ce(true); + baterr("Error: Failed to allocate memory for mcp73871_dev_s!\n"); + return NULL; } + priv->dev = (FAR struct battery_charger_dev_s *) + kmm_zalloc(sizeof(struct battery_charger_dev_s)); + + if (priv->dev == NULL) + { + batterr("Error: Failed to allocate memory for battery_charger_dev_s!\n"); + kmm_free(priv); + return NULL; + } + + /* Initialize the MCP73871 device structure */ + + nxsem_init(&priv->batsem, 0, 1); + priv->config = config; + priv->dev->ops = &g_mcp73871ops; + + /* Enable the battery charge */ + + priv->config->set_chg_ce(true); + return (FAR struct battery_charger_dev_s *)priv; }