From: Tony Lindgren <tony@atomide.com>
To: Sebastian Reichel <sre@kernel.org>
Cc: linux-pm@vger.kernel.org, linux-omap@vger.kernel.org,
Merlijn Wajer <merlijn@wizzup.org>, Pavel Machek <pavel@ucw.cz>
Subject: [PATCH 3/3] power: supply: cpcap-charger: Adjust current based on charger interrupts
Date: Tue, 17 Sep 2019 14:52:53 -0700 [thread overview]
Message-ID: <20190917215253.17880-4-tony@atomide.com> (raw)
In-Reply-To: <20190917215253.17880-1-tony@atomide.com>
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.
We can take advantage of the chrgcurr1 interrupts for charger detection,
and retry charging at a lower rate if charging fails. When an acceptable
charge rate is found, the chrgcurr1 interrupts stop.
Cc: Merlijn Wajer <merlijn@wizzup.org>
Cc: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
drivers/power/supply/cpcap-charger.c | 45 ++++++++++++++++++++++++++++
1 file changed, 45 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
@@ -145,6 +145,12 @@ enum {
CPCAP_CHARGER_IIO_NR,
};
+enum {
+ CPCAP_CHARGER_DISCONNECTED,
+ CPCAP_CHARGER_DETECTING,
+ CPCAP_CHARGER_CONNECTED,
+};
+
struct cpcap_charger_ddata {
struct device *dev;
struct regmap *reg;
@@ -161,6 +167,9 @@ struct cpcap_charger_ddata {
unsigned int vbus_enabled:1;
unsigned int feeding_vbus:1;
int const_charge_voltage;
+ int state;
+ int last_current;
+ int last_current_retries;
atomic_t active;
int status;
@@ -551,6 +560,15 @@ static void cpcap_usb_detect(struct work_struct *work)
if (error)
return;
+ /* Just init the state if a charger is connected with no chrg_det set */
+ if (!ddata->feeding_vbus && !s.chrg_det && s.chrgcurr1 && s.vbusvld) {
+ ddata->state = CPCAP_CHARGER_DETECTING;
+ ddata->last_current = 0;
+
+ return;
+ }
+
+ /* Start charger on chrgcurr1, stop chrger otherwise */
if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
s.chrgcurr1) {
int max_current;
@@ -561,6 +579,32 @@ 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->state = CPCAP_CHARGER_CONNECTED;
+ 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;
+ }
+ ddata->state = CPCAP_CHARGER_CONNECTED;
+ 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;
cpcap_charger_match_voltage(ddata, ddata->const_charge_voltage,
&vchrg);
error = cpcap_charger_set_state(ddata,
@@ -569,6 +613,7 @@ static void cpcap_usb_detect(struct work_struct *work)
if (error)
goto out_err;
} else {
+ ddata->state = CPCAP_CHARGER_DISCONNECTED;
error = cpcap_charger_set_state(ddata, 0, 0, 0);
if (error)
goto out_err;
--
2.23.0
next prev parent reply other threads:[~2019-09-17 21:53 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-17 21:52 [PATCH 0/3] cpcap charger and battery changes to deal with dropped voltage Tony Lindgren
2019-09-17 21:52 ` [PATCH 1/3] power: supply: cpcap-battery: Fix handling of lowered charger voltage Tony Lindgren
2019-09-17 21:52 ` [PATCH 2/3] power: supply: cpcap-charger: Allow changing constant charge voltage Tony Lindgren
2019-09-19 9:26 ` Pavel Machek
2019-09-20 14:13 ` Tony Lindgren
2019-09-17 21:52 ` Tony Lindgren [this message]
2019-09-19 9:34 ` [PATCH 3/3] power: supply: cpcap-charger: Adjust current based on charger interrupts Pavel Machek
2019-09-20 14:15 ` Tony Lindgren
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190917215253.17880-4-tony@atomide.com \
--to=tony@atomide.com \
--cc=linux-omap@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=merlijn@wizzup.org \
--cc=pavel@ucw.cz \
--cc=sre@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).