Linux Power Management development
 help / color / mirror / Atom feed
From: Sebastian Reichel <sebastian.reichel@collabora.com>
To: venkat.yadlapati@yahoo.com
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
Subject: Re: [PATCH v2 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early
Date: Fri, 4 Dec 2026 05:39:35 +0100	[thread overview]
Message-ID: <axJAJouH51Rymx_f@venus> (raw)
In-Reply-To: <20261123-vantage-battmgr-v2-1-03a419078d0a@yahoo.com>

[-- Attachment #1: Type: text/plain, Size: 4000 bytes --]

Hi,

On Mon, Nov 23, 2026 at 12:50:15AM +0530, 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.
> 
> 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;

usb_icl_pending can be implied by usb_icl != 0.

> +	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;
> +
This function should just use:

guard(mutex)(battmgr->lock);

for the mutex.

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

battmgr->usb_icl = pval->intval;

/* cached value is written when service is brought up */
if (!battmgr->service_up)
    return 0;

return qcom_battmgr_usb_set_icl(battmgr, battmgr->usb_icl);

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

if (battmgr->usb_icl) {
    ret = qcom_battmgr_usb_set_icl(battmgr, battmgr->usb_icl);
    if (ret)
        ...
}

>  }
>  
>  static void qcom_battmgr_pdr_notify(void *priv, int state)

Greetings,

-- Sebastian

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2026-12-04  4:39 UTC|newest]

Thread overview: 5+ 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 ` [PATCH v2 1/2] power: supply: qcom_battmgr: Apply a USB input current limit set early Venkat Yadlapati via B4 Relay
2026-12-04  4:39   ` Sebastian Reichel [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
2026-11-25  8:14 ` [PATCH v2 0/2] power: supply: qcom_battmgr: Apply the USB host current limit early Qianhui Xu

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=axJAJouH51Rymx_f@venus \
    --to=sebastian.reichel@collabora.com \
    --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=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