[PATCH v3 4/5] power: supply: max17040: Clear ALRT bit when the SOC are above threshold

Matheus Castello
2019-05-27T02:46:28Z

In order to not generate duplicate interrupts we clear the ALRT bit when
the SOC is in a state that shows that the battery is charged above the set
threshold for the SOC low level alert.

Signed-off-by: Matheus Castello <matheus@castello.eng.br>
---
drivers/power/supply/max17040_battery.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/drivers/power/supply/max17040_battery.c b/drivers/power/supply/max17040_battery.c
index 2f4851608cfe..61e6fcfea8a1 100644
--- a/drivers/power/supply/max17040_battery.c
+++ b/drivers/power/supply/max17040_battery.c
@@ -48,6 +48,7 @@ struct max17040_chip {
int status;
/* Low alert threshold from 32% to 1% of the State of Charge */
u32 low_soc_alert_threshold;
+ int alert_bit;
};

static int max17040_get_property(struct power_supply *psy,
@@ -107,6 +108,7 @@ static void max17040_reset(struct i2c_client *client)
static int max17040_set_low_soc_threshold_alert(struct i2c_client *client,
u32 level)
{
+ struct max17040_chip *chip = i2c_get_clientdata(client);
int ret;
u16 data;

@@ -118,6 +120,7 @@ static int max17040_set_low_soc_threshold_alert(struct i2c_client *client,
data &= MAX17040_ATHD_MASK;
data |= level;
max17040_write_reg(client, MAX17040_RCOMP, data);
+ chip->alert_bit = 0;
ret = 0;
} else {
ret = -EINVAL;
@@ -144,6 +147,11 @@ static void max17040_get_soc(struct i2c_client *client)
soc = max17040_read_reg(client, MAX17040_SOC);

chip->soc = (soc >> 8);
+
+ /* check SOC level to clear ALRT bit */
+ if (chip->soc > chip->low_soc_alert_threshold && chip->alert_bit)
+ max17040_set_low_soc_threshold_alert(client,
+ chip->low_soc_alert_threshold);
}

static void max17040_get_version(struct i2c_client *client)
@@ -229,6 +237,9 @@ static irqreturn_t max17040_thread_handler(int id, void *dev)
/* send uevent */
power_supply_changed(chip->battery);

+ /* ALRT bit is seted */
+ chip->alert_bit = 1;
+
return IRQ_HANDLED;
}

--
2.20.1