mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] power: supply: qcom_battmgr: Apply the USB host current limit early
@ 2026-11-15 20:20 Venkat Yadlapati via B4 Relay
  2026-11-15 20:20 ` [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early Venkat Yadlapati via B4 Relay
  2026-11-15 20:20 ` [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers Venkat Yadlapati via B4 Relay
  0 siblings, 2 replies; 7+ messages in thread
From: Venkat Yadlapati via B4 Relay @ 2026-11-15 20:20 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim, Venkat Yadlapati

On the Motorola Signature the battery manager firmware keeps the input
current limit of a USB host port at the 100 mA allowed before
enumeration, so the phone discharges while it is connected to a
computer. The dwc3 controller reports the current the host grants
through usb-psy-name, and commit be5e7367bceb made the limit writable.

The controller reports it before the battery manager service is up
though, so the first patch keeps the limit and applies it once the
service comes up. The second one applies it only to SDP, CDP and USB PD
ports, like the downstream driver, so a dedicated charger is never
throttled to what the controller reports.

Tested on a Motorola Signature: connected to a computer it charges after
enumeration instead of discharging.

I used an LLM (Claude) to help write these patches and their commit
messages. I reviewed all of them and tested the result on the device.

---
Venkat Yadlapati (2):
      power: supply: qcom_battmgr: Apply a USB input current limit set early
      power: supply: qcom_battmgr: Don't limit the input current of chargers

 drivers/power/supply/qcom_battmgr.c | 61 ++++++++++++++++++++++++++++++++-----
 1 file changed, 53 insertions(+), 8 deletions(-)
---
base-commit: 7071d4750856abbc68dfe431acc58a1d45dd42e8
change-id: 20261116-vantage-battmgr-945e626783c4

Best regards,
--  
Venkat Yadlapati <venkat.yadlapati@yahoo.com>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early
  2026-11-15 20:20 [PATCH 0/2] power: supply: qcom_battmgr: Apply the USB host current limit early Venkat Yadlapati via B4 Relay
@ 2026-11-15 20:20 ` Venkat Yadlapati via B4 Relay
  2026-12-01 10:44   ` Konrad Dybcio
  2026-11-15 20:20 ` [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers Venkat Yadlapati via B4 Relay
  1 sibling, 1 reply; 7+ messages in thread
From: Venkat Yadlapati via B4 Relay @ 2026-11-15 20:20 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim, Venkat Yadlapati

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.

Assisted-by: LLM
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.0



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers
  2026-11-15 20:20 [PATCH 0/2] power: supply: qcom_battmgr: Apply the USB host current limit early Venkat Yadlapati via B4 Relay
  2026-11-15 20:20 ` [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early Venkat Yadlapati via B4 Relay
@ 2026-11-15 20:20 ` Venkat Yadlapati via B4 Relay
  2026-12-01 11:09   ` Konrad Dybcio
  1 sibling, 1 reply; 7+ messages in thread
From: Venkat Yadlapati via B4 Relay @ 2026-11-15 20:20 UTC (permalink / raw)
  To: Sebastian Reichel
  Cc: linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim, Venkat Yadlapati

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

The input current limit of the USB power supply is meant for the current
a USB host grants, e.g. after enumeration. Like the downstream driver,
only apply it to SDP, CDP and USB PD ports, so that a dedicated charger
is never throttled to what a USB controller reports.

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

diff --git a/drivers/power/supply/qcom_battmgr.c b/drivers/power/supply/qcom_battmgr.c
index fde9704d0acd..db117cb64a17 100644
--- a/drivers/power/supply/qcom_battmgr.c
+++ b/drivers/power/supply/qcom_battmgr.c
@@ -1152,6 +1152,24 @@ static int qcom_battmgr_usb_set_icl(struct qcom_battmgr *battmgr, u32 icl)
 {
 	int ret;
 
+	ret = qcom_battmgr_usb_sm8350_update(battmgr, POWER_SUPPLY_PROP_USB_TYPE);
+	if (ret)
+		return ret;
+
+	/*
+	 * The limit is meant for the current a USB host grants, e.g. after
+	 * enumeration, like the downstream driver don't let it throttle
+	 * dedicated chargers.
+	 */
+	switch (battmgr->usb.usb_type) {
+	case POWER_SUPPLY_USB_TYPE_SDP:
+	case POWER_SUPPLY_USB_TYPE_CDP:
+	case POWER_SUPPLY_USB_TYPE_PD:
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	mutex_lock(&battmgr->lock);
 	ret = qcom_battmgr_request_property(battmgr, BATTMGR_USB_PROPERTY_SET,
 					    USB_INPUT_CURR_LIMIT, icl);

-- 
2.56.0



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early
  2026-11-15 20:20 ` [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early Venkat Yadlapati via B4 Relay
@ 2026-12-01 10:44   ` Konrad Dybcio
  2026-12-01 16:08     ` Venkat Yadlapati
  0 siblings, 1 reply; 7+ messages in thread
From: Konrad Dybcio @ 2026-12-01 10:44 UTC (permalink / raw)
  To: venkat.yadlapati, Sebastian Reichel
  Cc: linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim

On 11/15/26 9:20 PM, Venkat Yadlapati via B4 Relay wrote:
> 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.
> 
> Assisted-by: LLM

This tag is no longer valid, please switch to linux-next/master as
your base and run checkpatch

> Signed-off-by: Venkat Yadlapati <venkat.yadlapati@yahoo.com>
> ---

[...]

> +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;

guard(mutex)(&battmgr->lock)

return qcom_battmgr_...


> +}
> +
>  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;
> +	}

This is the easiest workaround, but I fear this may spiral out of
control, as the same would apply to essentially any parameter
that's writeable.

I'm thinking about whether we should perhaps defer the
registration of the power supplies until the service first appears.
I'm not sure of how much collateral damage that would cause. Upon
a "service goes down" event, we could keep the "return -EAGAIN"
behavior

Konrad

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers
  2026-11-15 20:20 ` [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers Venkat Yadlapati via B4 Relay
@ 2026-12-01 11:09   ` Konrad Dybcio
  2026-12-01 16:08     ` Venkat Yadlapati
  0 siblings, 1 reply; 7+ messages in thread
From: Konrad Dybcio @ 2026-12-01 11:09 UTC (permalink / raw)
  To: venkat.yadlapati, Sebastian Reichel
  Cc: linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim

On 11/15/26 9:20 PM, Venkat Yadlapati via B4 Relay wrote:
> From: Venkat Yadlapati <venkat.yadlapati@yahoo.com>
> 
> The input current limit of the USB power supply is meant for the current
> a USB host grants, e.g. after enumeration. Like the downstream driver,
> only apply it to SDP, CDP and USB PD ports, so that a dedicated charger
> is never throttled to what a USB controller reports.
> 
> Assisted-by: LLM
> Signed-off-by: Venkat Yadlapati <venkat.yadlapati@yahoo.com>
> ---
>  drivers/power/supply/qcom_battmgr.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
> 
> diff --git a/drivers/power/supply/qcom_battmgr.c b/drivers/power/supply/qcom_battmgr.c
> index fde9704d0acd..db117cb64a17 100644
> --- a/drivers/power/supply/qcom_battmgr.c
> +++ b/drivers/power/supply/qcom_battmgr.c
> @@ -1152,6 +1152,24 @@ static int qcom_battmgr_usb_set_icl(struct qcom_battmgr *battmgr, u32 icl)
>  {
>  	int ret;
>  
> +	ret = qcom_battmgr_usb_sm8350_update(battmgr, POWER_SUPPLY_PROP_USB_TYPE);
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * The limit is meant for the current a USB host grants, e.g. after
> +	 * enumeration, like the downstream driver don't let it throttle
> +	 * dedicated chargers.
> +	 */
> +	switch (battmgr->usb.usb_type) {
> +	case POWER_SUPPLY_USB_TYPE_SDP:
> +	case POWER_SUPPLY_USB_TYPE_CDP:
> +	case POWER_SUPPLY_USB_TYPE_PD:
> +		break;
> +	default:
> +		return -EINVAL;

msm-5.10 doesn't allow SDP here. What's your downstream tree?

It also has a provision to reset this limit whenever the psy
type changes

Konrad

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early
  2026-12-01 10:44   ` Konrad Dybcio
@ 2026-12-01 16:08     ` Venkat Yadlapati
  0 siblings, 0 replies; 7+ messages in thread
From: Venkat Yadlapati @ 2026-12-01 16:08 UTC (permalink / raw)
  To: Konrad Dybcio
  Cc: Sebastian Reichel, linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim

On 12/1/26 4:14 PM, Konrad Dybcio wrote:
> On 11/15/26 9:20 PM, Venkat Yadlapati via B4 Relay wrote:
>> Assisted-by: LLM
>
> This tag is no longer valid, please switch to linux-next/master as
> your base and run checkpatch

Thanks for the review. That line is already gone in v2 [1], which is
based on next-20261120 and otherwise has the same code as v1, so the
comments below apply to v2 as well. v3 will be on a current linux-next
and checked with checkpatch again.

>> +	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;
>
> guard(mutex)(&battmgr->lock)
>
> return qcom_battmgr_...

Will do.

>> +	if (!battmgr->service_up) {
>> +		battmgr->usb_icl = pval->intval;
>> +		battmgr->usb_icl_pending = true;
>> +		return 0;
>> +	}
>
> This is the easiest workaround, but I fear this may spiral out of
> control, as the same would apply to essentially any parameter
> that's writeable.
>
> I'm thinking about whether we should perhaps defer the
> registration of the power supplies until the service first appears.
> I'm not sure of how much collateral damage that would cause. Upon
> a "service goes down" event, we could keep the "return -EAGAIN"
> behavior

Agreed, and for the dwc3 case it looks like the cleaner fix. dwc3
already copes with a supply that shows up late: since 7c87ef27af79
("usb: dwc3: avoid probe deferral when USB power supply is not
available") it waits on the power supply notifier instead of deferring
its probe, and when the supply appears it sends the current it has
stored in the meantime. So with the supplies registered from the first
SERVREG_SERVICE_STATE_UP, the limit reaches the firmware without any
caching in battmgr, and dwc3 is not held back while the ADSP boots.

The other users I can think of (the userspace battery tools, power
supply links from other drivers) already have to deal with supplies
that appear later, but I may be missing a user on the laptops.

I would do that in v3: register the power supplies from the
enable work on the first "up" notification instead of in probe, keep
returning -EAGAIN while the service is down after that, and drop the
usb_icl_pending caching from this patch.

One thing I noticed in the current version: the pending limit is
applied right after the notifications are enabled, and with 2/2 the
charger type is read at that point. It can still be unknown then, in
which case 2/2 rejects the limit. The downstream driver sleeps 200 ms
before re-applying the limit for that reason. With late registration
dwc3 sets the limit only after the supply is registered, but the type
may still not be known yet, so I will look at that in v3 too.

v3 will take a while, as the phone is going in for repair and I
don't want to send it untested.

[1] https://lore.kernel.org/all/20261123-vantage-battmgr-v2-0-03a419078d0a@yahoo.com/

Thanks,
Venkat

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers
  2026-12-01 11:09   ` Konrad Dybcio
@ 2026-12-01 16:08     ` Venkat Yadlapati
  0 siblings, 0 replies; 7+ messages in thread
From: Venkat Yadlapati @ 2026-12-01 16:08 UTC (permalink / raw)
  To: Konrad Dybcio
  Cc: Sebastian Reichel, linux-arm-msm, linux-pm, linux-kernel, Joonhoe Kim

On 12/1/26 4:39 PM, Konrad Dybcio wrote:
> On 11/15/26 9:20 PM, Venkat Yadlapati via B4 Relay wrote:
>> +	switch (battmgr->usb.usb_type) {
>> +	case POWER_SUPPLY_USB_TYPE_SDP:
>> +	case POWER_SUPPLY_USB_TYPE_CDP:
>> +	case POWER_SUPPLY_USB_TYPE_PD:
>> +		break;
>> +	default:
>> +		return -EINVAL;
>
> msm-5.10 doesn't allow SDP here. What's your downstream tree?

The Motorola kernel for the Signature (SM8845):

https://github.com/MotorolaMobilityLLC/kernel-msm/blob/MMI-A171WVH.21-16-16/drivers/power/supply/qti_battery_charger.c

Its usb_psy_set_icl() reads USB_ADAP_TYPE and allows SDP, PD and CDP:

	/* Allow this only for SDP, CDP or USB_PD and not for other charger types */
	switch (pst->prop[USB_ADAP_TYPE]) {
	case POWER_SUPPLY_USB_TYPE_SDP:
	case POWER_SUPPLY_USB_TYPE_PD:
	case POWER_SUPPLY_USB_TYPE_CDP:
		break;

SDP is also the case this series is about: the phone connected to a
computer, where dwc3 reports 500 mA (USB 2.0) or 900 mA (USB 3.x)
after enumeration and the firmware otherwise stays at 100 mA.

> It also has a provision to reset this limit whenever the psy
> type changes

Right. As far as I can see, the reset there only clears the cached
value (bcdev->usb_icl_ua), in usb_psy_set_icl() for other types and in
the notification handler when the type stops being SDP/CDP/PD. That
cache is only used to re-apply the last limit after the firmware
restarts. Nothing is written to the firmware when the type changes.

v2 keeps no cached value once the service is up, so the reset has
nothing to clear yet. If v3 goes with the late registration you
suggested on 1/2, it keeps no cache at all. Re-applying the limit
after a firmware restart, the way downstream does, would need the
cache, and then the reset on a type change as well. Do you want that
in this series, or as a separate change?

Thanks,
Venkat

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-01 16:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 20:20 [PATCH 0/2] power: supply: qcom_battmgr: Apply the USB host current limit early Venkat Yadlapati via B4 Relay
2026-11-15 20:20 ` [PATCH 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early Venkat Yadlapati via B4 Relay
2026-12-01 10:44   ` Konrad Dybcio
2026-12-01 16:08     ` Venkat Yadlapati
2026-11-15 20:20 ` [PATCH 2/2] power: supply: qcom_battmgr: Don't limit the input current of chargers Venkat Yadlapati via B4 Relay
2026-12-01 11:09   ` Konrad Dybcio
2026-12-01 16:08     ` Venkat Yadlapati

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®