mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Venkat Yadlapati via B4 Relay <devnull+venkat.yadlapati.yahoo.com@kernel.org>
To: Sebastian Reichel <sre@kernel.org>
Cc: linux-arm-msm@vger.kernel.org, linux-pm@vger.kernel.org,
	 linux-kernel@vger.kernel.org, Joonhoe Kim <26rote@gmail.com>,
	 phone-devel@vger.kernel.org,
	Venkat Yadlapati <venkat.yadlapati@yahoo.com>
Subject: [PATCH v2 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early
Date: Mon, 23 Nov 2026 00:50:15 +0530	[thread overview]
Message-ID: <20261123-vantage-battmgr-v2-1-03a419078d0a@yahoo.com> (raw)
In-Reply-To: <20261123-vantage-battmgr-v2-0-03a419078d0a@yahoo.com>

From: Venkat Yadlapati <venkat.yadlapati@yahoo.com>

The USB controller can report the current a USB host grants, e.g. with
the usb-psy-name property of dwc3, before the battery manager service
is up. The input current limit is then rejected with -EAGAIN and the
firmware keeps the 100 mA allowed before enumeration, so a phone that is
in use discharges while it is connected to a computer.

Remember the limit while the service is down and send it once the
service comes up.

Signed-off-by: Venkat Yadlapati <venkat.yadlapati@yahoo.com>
---
 drivers/power/supply/qcom_battmgr.c | 45 +++++++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/drivers/power/supply/qcom_battmgr.c b/drivers/power/supply/qcom_battmgr.c
index 431a35bcac80..fde9704d0acd 100644
--- a/drivers/power/supply/qcom_battmgr.c
+++ b/drivers/power/supply/qcom_battmgr.c
@@ -329,6 +329,10 @@ struct qcom_battmgr {
 
 	bool service_up;
 
+	/* USB input current limit set before the service was up, in uA */
+	bool usb_icl_pending;
+	u32 usb_icl;
+
 	struct qcom_battmgr_info info;
 	struct qcom_battmgr_status status;
 	struct qcom_battmgr_ac ac;
@@ -1144,25 +1148,40 @@ static const struct power_supply_desc sc8280xp_usb_psy_desc = {
 		     BIT(POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID),
 };
 
+static int qcom_battmgr_usb_set_icl(struct qcom_battmgr *battmgr, u32 icl)
+{
+	int ret;
+
+	mutex_lock(&battmgr->lock);
+	ret = qcom_battmgr_request_property(battmgr, BATTMGR_USB_PROPERTY_SET,
+					    USB_INPUT_CURR_LIMIT, icl);
+	mutex_unlock(&battmgr->lock);
+
+	return ret;
+}
+
 static int qcom_battmgr_usb_set_property(struct power_supply *psy,
 					 enum power_supply_property psp,
 					 const union power_supply_propval *pval)
 {
 	struct qcom_battmgr *battmgr = power_supply_get_drvdata(psy);
-	int ret;
-
-	if (!battmgr->service_up)
-		return -EAGAIN;
 
 	if (psp != POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT || pval->intval < 0)
 		return -EINVAL;
 
-	mutex_lock(&battmgr->lock);
-	ret = qcom_battmgr_request_property(battmgr, BATTMGR_USB_PROPERTY_SET,
-					    USB_INPUT_CURR_LIMIT, pval->intval);
-	mutex_unlock(&battmgr->lock);
+	/*
+	 * The USB controller can report the current a host grants before the
+	 * firmware service is up, apply it once the service comes up.
+	 */
+	if (!battmgr->service_up) {
+		battmgr->usb_icl = pval->intval;
+		battmgr->usb_icl_pending = true;
+		return 0;
+	}
 
-	return ret;
+	battmgr->usb_icl_pending = false;
+
+	return qcom_battmgr_usb_set_icl(battmgr, pval->intval);
 }
 
 static int qcom_battmgr_usb_is_writeable(struct power_supply *psy,
@@ -1742,6 +1761,14 @@ static void qcom_battmgr_enable_worker(struct work_struct *work)
 	ret = qcom_battmgr_request(battmgr, &req, sizeof(req));
 	if (ret)
 		dev_err(battmgr->dev, "failed to request power notifications\n");
+
+	if (battmgr->usb_icl_pending) {
+		battmgr->usb_icl_pending = false;
+		ret = qcom_battmgr_usb_set_icl(battmgr, battmgr->usb_icl);
+		if (ret)
+			dev_warn(battmgr->dev,
+				 "failed to set the USB input current limit: %d\n", ret);
+	}
 }
 
 static void qcom_battmgr_pdr_notify(void *priv, int state)

-- 
2.56.1



  reply	other threads:[~2026-11-22 19:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 19:20 [PATCH v2 0/2] power: supply: qcom_battmgr: Apply the USB host current limit early Venkat Yadlapati via B4 Relay
2026-11-22 19:20 ` Venkat Yadlapati via B4 Relay [this message]
2026-11-22 19:20 ` [PATCH v2 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers Venkat Yadlapati via B4 Relay

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=20261123-vantage-battmgr-v2-1-03a419078d0a@yahoo.com \
    --to=devnull+venkat.yadlapati.yahoo.com@kernel.org \
    --cc=26rote@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=phone-devel@vger.kernel.org \
    --cc=sre@kernel.org \
    --cc=venkat.yadlapati@yahoo.com \
    /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

all inboxes | Powered by JetHome®