Skip to content

Commit

Permalink
drivers: charger: Updates drivers to use charge_enable handler
Browse files Browse the repository at this point in the history
Updates existing drivers to make use of the charge_enable handler.

Signed-off-by: Ricardo Rivera-Matos <[email protected]>
  • Loading branch information
rriveramcrus authored and MaureenHelm committed Dec 12, 2023
1 parent d4ed6ba commit 16519a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
11 changes: 1 addition & 10 deletions drivers/charger/charger_max20335.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,6 @@ static int max20335_set_prop(const struct device *dev, charger_prop_t prop,
const union charger_propval *val)
{
switch (prop) {
case CHARGER_PROP_STATUS:
switch (val->status) {
case CHARGER_STATUS_CHARGING:
return max20335_set_enabled(dev, true);
case CHARGER_STATUS_NOT_CHARGING:
return max20335_set_enabled(dev, false);
default:
return -ENOTSUP;
}
case CHARGER_PROP_CONSTANT_CHARGE_CURRENT_UA:
return max20335_set_constant_charge_current(dev,
val->const_charge_current_ua);
Expand All @@ -253,7 +244,6 @@ static int max20335_set_prop(const struct device *dev, charger_prop_t prop,
default:
return -ENOTSUP;
}

}

static int max20335_init(const struct device *dev)
Expand All @@ -270,6 +260,7 @@ static int max20335_init(const struct device *dev)
static const struct charger_driver_api max20335_driver_api = {
.get_property = max20335_get_prop,
.set_property = max20335_set_prop,
.charge_enable = max20335_set_enabled,
};

#define MAX20335_DEFINE(inst) \
Expand Down
16 changes: 14 additions & 2 deletions drivers/charger/emul_sbs_charger.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ struct sbs_charger_emul_cfg {
uint16_t addr;
};

/** Run-time data used by the emulator */
struct sbs_charger_emul_data {
uint16_t reg_charger_mode;
};

static int emul_sbs_charger_reg_write(const struct emul *target, int reg, int val)
{
struct sbs_charger_emul_data *data = target->data;

LOG_INF("write %x = %x", reg, val);
switch (reg) {
case SBS_CHARGER_REG_CHARGER_MODE:
data->reg_charger_mode = val;
break;
default:
LOG_ERR("Unknown write %x", reg);
return -EIO;
Expand Down Expand Up @@ -132,10 +142,12 @@ static int emul_sbs_sbs_charger_init(const struct emul *target, const struct dev
* Main instantiation macro. SBS Charger Emulator only implemented for I2C
*/
#define SBS_CHARGER_EMUL(n) \
static struct sbs_charger_emul_data sbs_charger_emul_data_##n; \
\
static const struct sbs_charger_emul_cfg sbs_charger_emul_cfg_##n = { \
.addr = DT_INST_REG_ADDR(n), \
}; \
EMUL_DT_INST_DEFINE(n, emul_sbs_sbs_charger_init, NULL, &sbs_charger_emul_cfg_##n, \
&sbs_charger_emul_api_i2c, NULL)
EMUL_DT_INST_DEFINE(n, emul_sbs_sbs_charger_init, &sbs_charger_emul_data_##n, \
&sbs_charger_emul_cfg_##n, &sbs_charger_emul_api_i2c, NULL)

DT_INST_FOREACH_STATUS_OKAY(SBS_CHARGER_EMUL)
28 changes: 16 additions & 12 deletions drivers/charger/sbs_charger.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ static int sbs_cmd_reg_update(const struct device *dev, uint8_t reg_addr, uint16
return sbs_cmd_reg_write(dev, reg_addr, new_val);
}

static int sbs_charger_charge_enable(const struct device *dev, const bool enable)
{
uint16_t reg_val;

if (!enable) {
reg_val = SBS_CHARGER_MODE_INHIBIT_CHARGE;
} else {
reg_val = 0;
}

return sbs_cmd_reg_update(dev, SBS_CHARGER_REG_CHARGER_MODE,
SBS_CHARGER_MODE_INHIBIT_CHARGE, reg_val);
}

static int sbs_charger_get_prop(const struct device *dev, const charger_prop_t prop,
union charger_propval *val)
{
Expand Down Expand Up @@ -123,18 +137,7 @@ static int sbs_charger_get_prop(const struct device *dev, const charger_prop_t p
static int sbs_charger_set_prop(const struct device *dev, const charger_prop_t prop,
const union charger_propval *val)
{
uint16_t reg_val = 0;

switch (prop) {
case CHARGER_PROP_STATUS:
if (val->status != CHARGER_STATUS_CHARGING) {
reg_val = SBS_CHARGER_MODE_INHIBIT_CHARGE;
}
return sbs_cmd_reg_update(dev, SBS_CHARGER_REG_CHARGER_MODE,
SBS_CHARGER_MODE_INHIBIT_CHARGE, reg_val);
default:
return -ENOTSUP;
}
return -ENOTSUP;
}

/**
Expand All @@ -157,6 +160,7 @@ static int sbs_charger_init(const struct device *dev)
static const struct charger_driver_api sbs_charger_driver_api = {
.get_property = &sbs_charger_get_prop,
.set_property = &sbs_charger_set_prop,
.charge_enable = &sbs_charger_charge_enable,
};

#define SBS_CHARGER_INIT(inst) \
Expand Down
9 changes: 3 additions & 6 deletions tests/drivers/charger/sbs_charger/src/test_sbs_charger.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ ZTEST_USER_F(sbs_charger, test_set_prop_failed_returns_negative)
zassert_equal(ret, -ENOTSUP, "Setting bad property %d has a good status.", prop);
}

ZTEST_USER_F(sbs_charger, test_set_prop_success_returns_zero)
ZTEST_USER_F(sbs_charger, test_charge_enable_success_returns_zero)
{
union charger_propval val = {.status = CHARGER_STATUS_NOT_CHARGING};
charger_prop_t prop = CHARGER_PROP_STATUS;
int ret = charger_charge_enable(fixture->dev, true);

int ret = charger_set_prop(fixture->dev, prop, &val);

zassert_equal(ret, 0, "Setting good property %d has a good status.", prop);
zassert_equal(ret, 0, "Enabling charge has a good status.");
}

ZTEST_SUITE(sbs_charger, NULL, sbs_charger_setup, NULL, NULL, NULL);

0 comments on commit 16519a5

Please sign in to comment.