Skip to content

Commit

Permalink
charger: bq25180: implement online and status properties
Browse files Browse the repository at this point in the history
Implement CHARGER_PROP_ONLINE and CHARGER_PROP_STATUS for the bq25180
driver.

Signed-off-by: Fabio Baltieri <[email protected]>
  • Loading branch information
fabiobaltieri authored and MaureenHelm committed Jan 31, 2024
1 parent 63cdde1 commit 044529d
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions drivers/charger/charger_bq25180.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ LOG_MODULE_REGISTER(bq25180, CONFIG_CHARGER_LOG_LEVEL);
#define BQ25180_SHIP_RST 0x09
#define BQ25180_MASK_ID 0x0c

#define BQ25180_STAT0_CHG_STAT_MASK GENMASK(6, 5)
#define BQ25180_STAT0_CHG_STAT_NOT_CHARGING 0x00
#define BQ25180_STAT0_CHG_STAT_CONSTANT_CURRENT 0x01
#define BQ25180_STAT0_CHG_STAT_CONSTANT_VOLTAGE 0x02
#define BQ25180_STAT0_CHG_STAT_DONE 0x03
#define BQ25180_STAT0_VIN_PGOOD_STAT BIT(0)
#define BQ25180_ICHG_CHG_DIS BIT(7)
#define BQ25180_ICHG_MSK GENMASK(6, 0)
#define BQ25180_WATCHDOG_SEL_1_MSK GENMASK(1, 0)
Expand Down Expand Up @@ -134,10 +140,79 @@ static int bq25180_get_charge_current(const struct device *dev,
return 0;
}

static int bq25180_get_online(const struct device *dev,
enum charger_online *online)
{
const struct bq25180_config *cfg = dev->config;
uint8_t val;
int ret;

ret = i2c_reg_read_byte_dt(&cfg->i2c, BQ25180_STAT0, &val);
if (ret < 0) {
return ret;
}

if ((val & BQ25180_STAT0_VIN_PGOOD_STAT) != 0x00) {
*online = CHARGER_ONLINE_FIXED;
} else {
*online = CHARGER_ONLINE_OFFLINE;
}

return 0;
}

static int bq25180_get_status(const struct device *dev,
enum charger_status *status)
{
const struct bq25180_config *cfg = dev->config;
uint8_t stat0;
uint8_t ichg_ctrl;
int ret;

ret = i2c_reg_read_byte_dt(&cfg->i2c, BQ25180_STAT0, &stat0);
if (ret < 0) {
return ret;
}

if ((stat0 & BQ25180_STAT0_VIN_PGOOD_STAT) == 0x00) {
*status = CHARGER_STATUS_DISCHARGING;
return 0;
}

ret = i2c_reg_read_byte_dt(&cfg->i2c, BQ25180_ICHG_CTRL, &ichg_ctrl);
if (ret < 0) {
return ret;
}

if ((ichg_ctrl & BQ25180_ICHG_CHG_DIS) != 0x00) {
*status = CHARGER_STATUS_NOT_CHARGING;
return 0;
}

switch (FIELD_GET(BQ25180_STAT0_CHG_STAT_MASK, stat0)) {
case BQ25180_STAT0_CHG_STAT_NOT_CHARGING:
*status = CHARGER_STATUS_NOT_CHARGING;
break;
case BQ25180_STAT0_CHG_STAT_CONSTANT_CURRENT:
case BQ25180_STAT0_CHG_STAT_CONSTANT_VOLTAGE:
*status = CHARGER_STATUS_CHARGING;
break;
case BQ25180_STAT0_CHG_STAT_DONE:
*status = CHARGER_STATUS_FULL;
break;
}

return 0;
}

static int bq25180_get_prop(const struct device *dev, charger_prop_t prop,
union charger_propval *val)
{
switch (prop) {
case CHARGER_PROP_ONLINE:
return bq25180_get_online(dev, &val->online);
case CHARGER_PROP_STATUS:
return bq25180_get_status(dev, &val->status);
case CHARGER_PROP_CONSTANT_CHARGE_CURRENT_UA:
return bq25180_get_charge_current(dev, &val->const_charge_current_ua);
default:
Expand Down

0 comments on commit 044529d

Please sign in to comment.