diff --git a/drivers/power/bq2425x.c b/drivers/power/bq2425x.c index 025c3f980a..5039ad9ea9 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_dev_s *dev; /* Battery device */ + FAR const struct battery_charger_operations_s *ops; /* Battery operations */ sem_t batsem; /* Enforce mutually exclusive access */ /* Data fields specific to the lower half BQ2425x driver follow */ @@ -781,64 +781,48 @@ FAR struct battery_charger_dev_s * FAR struct bq2425x_dev_s *priv; int ret; - /* Allocate the BQ2425x device structure */ - - priv = (FAR struct bq2425x_dev_s *)kmm_zalloc(sizeof(struct bq2425x_dev_s)); - if (priv == NULL) - { - baterr("ERROR: Failed to allocate memory for bq2425x_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) - { - baterr("ERROR: Failed to allocate memory for battery_charger_dev_s!\n"); - kmm_free(priv); - return NULL; - } - /* Initialize the BQ2425x device structure */ - nxsem_init(&priv->batsem, 0, 1); - priv->dev->ops = &g_bq2425xops; - priv->i2c = i2c; - priv->addr = addr; - priv->frequency = frequency; - - /* Reset the BQ2425x */ - - ret = bq2425x_reset(priv); - if (ret < 0) + priv = (FAR struct bq2425x_dev_s *)kmm_zalloc(sizeof(struct bq2425x_dev_s)); + if (priv) { - baterr("ERROR: Failed to reset the BQ2425x: %d\n", ret); - kmm_free(priv->dev); - 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->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->dev); - 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); + 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->dev); - 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); + 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); + return NULL; + } } return (FAR struct battery_charger_dev_s *)priv; diff --git a/drivers/power/bq2429x.c b/drivers/power/bq2429x.c index 0c428a834a..218f6f4541 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_dev_s *dev; /* Battery device */ + FAR const struct battery_charger_operations_s *ops; /* Battery operations */ sem_t batsem; /* Enforce mutually exclusive access */ /* Data fields specific to the lower half BQ2429X driver follow */ @@ -1251,74 +1251,58 @@ FAR struct battery_charger_dev_s * FAR struct bq2429x_dev_s *priv; int ret; - /* Allocate the BQ2429x device structure */ - - priv = (FAR struct bq2429x_dev_s *)kmm_zalloc(sizeof(struct bq2429x_dev_s)); - if (priv == NULL) - { - baterr("ERROR: Failed to allocate memory for bq2429x_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) - { - baterr("ERROR: Failed to allocate memory for battery_charger_dev_s!\n"); - kmm_free(priv); - return NULL; - } - /* Initialize the BQ2429x device structure */ - nxsem_init(&priv->batsem, 0, 1); - priv->dev->ops = &g_bq2429xops; - priv->i2c = i2c; - priv->addr = addr; - priv->frequency = frequency; - - /* Reset the BQ2429x */ - - ret = bq2429x_reset(priv); - if (ret < 0) + priv = (FAR struct bq2429x_dev_s *)kmm_zalloc(sizeof(struct bq2429x_dev_s)); + if (priv) { - baterr("ERROR: Failed to reset the BQ2429x: %d\n", ret); - kmm_free(priv->dev); - kmm_free(priv); - return NULL; - } + /* Initialize the BQ2429x device structure */ - /* Disable watchdog otherwise BQ2429x returns to StandAlone mode */ + nxsem_init(&priv->batsem, 0, 1); + priv->ops = &g_bq2429xops; + priv->i2c = i2c; + priv->addr = addr; + priv->frequency = frequency; - 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; - } + /* Reset the BQ2429x */ - /* Define the current that our power supply can offer to the charger. */ + ret = bq2429x_reset(priv); + if (ret < 0) + { + baterr("ERROR: Failed to reset the BQ2429x: %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 watchdog otherwise BQ2429x returns to StandAlone mode */ - /* Disable all interrupts. */ + 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_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; + /* Define the current that our power supply can offer to the charger. */ + + 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; + } + + /* 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); + return NULL; + } } return (FAR struct battery_charger_dev_s *)priv; diff --git a/drivers/power/mcp73871.c b/drivers/power/mcp73871.c index 3b99c3b0de..74a5c2f483 100644 --- a/drivers/power/mcp73871.c +++ b/drivers/power/mcp73871.c @@ -93,14 +93,13 @@ 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 */ }; @@ -400,35 +399,24 @@ FAR struct battery_charger_dev_s * { FAR struct mcp73871_dev_s *priv; - /* Allocate the MCP73871 device structure */ + /* Initialize the BQ2425x device structure */ - priv = (FAR struct mcp73871_dev_s *)kmm_zalloc(sizeof(struct mcp73871_dev_s)); - if (priv == NULL) + priv = (FAR struct mcp73871_dev_s *) + kmm_zalloc(sizeof(struct mcp73871_dev_s)); + + if (priv) { - baterr("Error: Failed to allocate memory for mcp73871_dev_s!\n"); - return 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); } - 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; }