From 9c65f6c4482d0e3e3681c113f77f82ac571ceaa7 Mon Sep 17 00:00:00 2001 From: Timon Baetz Date: Mon, 9 Nov 2020 19:43:55 +0000 Subject: [PATCH] power: supply: max8997-charger: Improve getting charger status Detect charging and discharging state. Ported from downstream Samsung Galaxy S2 (i9100) kernel fork. Signed-off-by: Timon Baetz Signed-off-by: Sebastian Reichel --- drivers/power/supply/max8997_charger.c | 52 +++++++++++++++++--------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/power/supply/max8997_charger.c b/drivers/power/supply/max8997_charger.c index 8fccd58dac67..1947af25879a 100644 --- a/drivers/power/supply/max8997_charger.c +++ b/drivers/power/supply/max8997_charger.c @@ -13,6 +13,20 @@ #include #include +/* MAX8997_REG_STATUS4 */ +#define DCINOK_SHIFT 1 +#define DCINOK_MASK (1 << DCINOK_SHIFT) +#define DETBAT_SHIFT 2 +#define DETBAT_MASK (1 << DETBAT_SHIFT) + +/* MAX8997_REG_MBCCTRL1 */ +#define TFCH_SHIFT 4 +#define TFCH_MASK (7 << TFCH_SHIFT) + +/* MAX8997_REG_MBCCTRL5 */ +#define ITOPOFF_SHIFT 0 +#define ITOPOFF_MASK (0xF << ITOPOFF_SHIFT) + struct charger_data { struct device *dev; struct max8997_dev *iodev; @@ -20,7 +34,7 @@ struct charger_data { }; static enum power_supply_property max8997_battery_props[] = { - POWER_SUPPLY_PROP_STATUS, /* "FULL" or "NOT FULL" only. */ + POWER_SUPPLY_PROP_STATUS, /* "FULL", "CHARGING" or "DISCHARGING". */ POWER_SUPPLY_PROP_PRESENT, /* the presence of battery */ POWER_SUPPLY_PROP_ONLINE, /* charger is active or not */ }; @@ -43,6 +57,10 @@ static int max8997_battery_get_property(struct power_supply *psy, return ret; if ((reg & (1 << 0)) == 0x1) val->intval = POWER_SUPPLY_STATUS_FULL; + else if ((reg & DCINOK_MASK)) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; break; case POWER_SUPPLY_PROP_PRESENT: @@ -50,7 +68,7 @@ static int max8997_battery_get_property(struct power_supply *psy, ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, ®); if (ret) return ret; - if ((reg & (1 << 2)) == 0x0) + if ((reg & DETBAT_MASK) == 0x0) val->intval = 1; break; @@ -59,8 +77,7 @@ static int max8997_battery_get_property(struct power_supply *psy, ret = max8997_read_reg(i2c, MAX8997_REG_STATUS4, ®); if (ret) return ret; - /* DCINOK */ - if (reg & (1 << 1)) + if (reg & DCINOK_MASK) val->intval = 1; break; @@ -84,11 +101,14 @@ static int max8997_battery_probe(struct platform_device *pdev) int ret = 0; struct charger_data *charger; struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent); + struct i2c_client *i2c = iodev->i2c; struct max8997_platform_data *pdata = iodev->pdata; struct power_supply_config psy_cfg = {}; - if (!pdata) + if (!pdata) { + dev_err(&pdev->dev, "No platform data supplied.\n"); return -EINVAL; + } if (pdata->eoc_mA) { int val = (pdata->eoc_mA - 50) / 10; @@ -97,30 +117,29 @@ static int max8997_battery_probe(struct platform_device *pdev) if (val > 0xf) val = 0xf; - ret = max8997_update_reg(iodev->i2c, - MAX8997_REG_MBCCTRL5, val, 0xf); + ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL5, + val << ITOPOFF_SHIFT, ITOPOFF_MASK); if (ret < 0) { dev_err(&pdev->dev, "Cannot use i2c bus.\n"); return ret; } } - switch (pdata->timeout) { case 5: - ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1, - 0x2 << 4, 0x7 << 4); + ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, + 0x2 << TFCH_SHIFT, TFCH_MASK); break; case 6: - ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1, - 0x3 << 4, 0x7 << 4); + ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, + 0x3 << TFCH_SHIFT, TFCH_MASK); break; case 7: - ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1, - 0x4 << 4, 0x7 << 4); + ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, + 0x4 << TFCH_SHIFT, TFCH_MASK); break; case 0: - ret = max8997_update_reg(iodev->i2c, MAX8997_REG_MBCCTRL1, - 0x7 << 4, 0x7 << 4); + ret = max8997_update_reg(i2c, MAX8997_REG_MBCCTRL1, + 0x7 << TFCH_SHIFT, TFCH_MASK); break; default: dev_err(&pdev->dev, "incorrect timeout value (%d)\n", @@ -138,7 +157,6 @@ static int max8997_battery_probe(struct platform_device *pdev) platform_set_drvdata(pdev, charger); - charger->dev = &pdev->dev; charger->iodev = iodev;