[PATCH] power: supply: cpcap-charger: Adjust current based on charger interrupts

From: Tony Lindgren
Date: Sun Feb 16 2020 - 19:59:06 EST


When debugging why higher than 500 mA charge current does not work, I
noticed that we start getting lots of chrgcurr1 interrupts if we attempt
to charge at rates higher than the charger can provide.

Cc: Merlijn Wajer <merlijn@xxxxxxxxxx>
Cc: Pavel Machek <pavel@xxxxxx>
Signed-off-by: Tony Lindgren <tony@xxxxxxxxxxx>
---
drivers/power/supply/cpcap-charger.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/drivers/power/supply/cpcap-charger.c b/drivers/power/supply/cpcap-charger.c
--- a/drivers/power/supply/cpcap-charger.c
+++ b/drivers/power/supply/cpcap-charger.c
@@ -147,6 +147,8 @@ struct cpcap_charger_ddata {
int status;
int state;
int voltage;
+ int last_current;
+ int last_current_retries;
};

struct cpcap_interrupt_desc {
@@ -616,6 +618,7 @@ static void cpcap_usb_detect(struct work_struct *work)
/* Just init the state if a charger is connected with no chrg_det set */
if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);
+ ddata->last_current = 0;

return;
}
@@ -662,6 +665,30 @@ static void cpcap_usb_detect(struct work_struct *work)
else
max_current = CPCAP_REG_CRM_ICHRG_0A532;

+ switch (ddata->state) {
+ case CPCAP_CHARGER_DETECTING:
+ ddata->last_current_retries = 0;
+ break;
+ case CPCAP_CHARGER_DISCONNECTED:
+ if (ddata->last_current > CPCAP_REG_CRM_ICHRG_0A532) {
+ /* Attempt current 3 times before lowering */
+ if (ddata->last_current_retries++ >= 3) {
+ ddata->last_current--;
+ ddata->last_current_retries = 0;
+ /* Wait a bit for voltage to ramp up */
+ usleep_range(40000, 50000);
+ }
+ max_current = ddata->last_current;
+ }
+ dev_info(ddata->dev, "enabling charger with current %i\n",
+ max_current);
+ break;
+ default:
+ ddata->last_current_retries = 0;
+ break;
+ }
+
+ ddata->last_current = max_current;
vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
error = cpcap_charger_set_state(ddata,
CPCAP_REG_CRM_VCHRG(vchrg),
--
2.26.2