Linux Power Management development
 help / color / mirror / Atom feed
From: Hans de Goede <hansg@kernel.org>
To: amitsd@google.com, Sebastian Reichel <sre@kernel.org>,
	Badhri Jagan Sridharan <badhri@google.com>,
	Heikki Krogerus <heikki.krogerus@linux.intel.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Sebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>,
	Purism Kernel Team <kernel@puri.sm>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-usb@vger.kernel.org,
	"André Draszik" <andre.draszik@linaro.org>,
	"Tudor Ambarus" <tudor.ambarus@linaro.org>,
	"Peter Griffin" <peter.griffin@linaro.org>,
	"RD Babiera" <rdbabiera@google.com>,
	"Kyle Tso" <kyletso@google.com>
Subject: Re: [PATCH 3/3] power: supply: max17042: add handler for energy_now property
Date: Fri, 15 May 2026 09:25:30 +0200	[thread overview]
Message-ID: <17f49974-1f39-4b4c-8577-da33da7f1cc4@kernel.org> (raw)
In-Reply-To: <20260515-batt-status-v1-3-fed6b7d8cea7@google.com>

Hi,

On 15-May-26 07:48, Amit Sunil Dhamne via B4 Relay wrote:
> From: Amit Sunil Dhamne <amitsd@google.com>
> 
> Add handler to report power_supply_prop_energy_now so that users can get
> current SoC in uWH. Additionally, add helper functions to get avg_vcell
> and repcap values in uv and uah units respectively to avoid code
> duplication.
> 
> Signed-off-by: Amit Sunil Dhamne <amitsd@google.com>

NACK for multiple reasons:

1. We don't want to do this in all drivers which only support
charge_now and not energy_now, instead the TCPM driver should
convert charge_now to energy_now when necessary itself so that
the TCPM code will work with all battery type power-supply drivers
not just those which provide energy_now.

2. Having energy_now without energy_full is problematic and will
confuse userspace which prefers energy_* over charge_* since
userspace will now miss a reference value for full to report
a charging progress percentage. I also wonder how this works
on the TCPM side does the  Battery Status response message not
have a full value / percentage ?

3. IIRC userspace (upower) picks either energy_* or charge_*
values depending on which are present. I'm not sure if having
both will not confuse userspace. As mentioned in 2. having both
while one of them has an incomplete set of properties is sure
to confuse userspace.

Regards,

Hans





> ---
>  drivers/power/supply/max17042_battery.c | 60 ++++++++++++++++++++++++++-------
>  1 file changed, 47 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/power/supply/max17042_battery.c b/drivers/power/supply/max17042_battery.c
> index 167fb3fb3732..e16eb6985b70 100644
> --- a/drivers/power/supply/max17042_battery.c
> +++ b/drivers/power/supply/max17042_battery.c
> @@ -81,6 +81,7 @@ static enum power_supply_property max17042_battery_props[] = {
>  	POWER_SUPPLY_PROP_CHARGE_NOW,
>  	POWER_SUPPLY_PROP_CHARGE_COUNTER,
>  	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
> +	POWER_SUPPLY_PROP_ENERGY_NOW,
>  	POWER_SUPPLY_PROP_TEMP,
>  	POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
>  	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
> @@ -95,6 +96,36 @@ static enum power_supply_property max17042_battery_props[] = {
>  	POWER_SUPPLY_PROP_CURRENT_AVG,
>  };
>  
> +static int max17042_get_repcap_uah(struct max17042_chip *chip, u64 *rep_cap)
> +{
> +	u32 data;
> +	int ret;
> +
> +	ret = regmap_read(chip->regmap, MAX17042_RepCap, &data);
> +	if (ret < 0)
> +		return ret;
> +
> +	*rep_cap = data * 5000000ll;
> +	*rep_cap *= chip->task_period;
> +	do_div(*rep_cap, MAX17042_DEFAULT_TASK_PERIOD);
> +	do_div(*rep_cap, chip->pdata->r_sns);
> +
> +	return 0;
> +}
> +
> +static int max17042_get_avgvcell_uv(struct max17042_chip *chip, u32 *vcell)
> +{
> +	int ret;
> +
> +	ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, vcell);
> +	if (ret < 0)
> +		return ret;
> +
> +	*vcell = (*vcell * 625) / 8;
> +
> +	return 0;
> +}
> +
>  static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
>  {
>  	int ret;
> @@ -180,14 +211,12 @@ static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
>  	int temp, vavg, vbatt, ret;
>  	u32 val;
>  
> -	ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val);
> +	ret = max17042_get_avgvcell_uv(chip, &val);
>  	if (ret < 0)
>  		goto health_error;
>  
> -	/* bits [0-3] unused */
> -	vavg = val * 625 / 8;
>  	/* Convert to millivolts */
> -	vavg /= 1000;
> +	vavg = val / 1000;
>  
>  	ret = regmap_read(chip->regmap, MAX17042_VCELL, &val);
>  	if (ret < 0)
> @@ -304,11 +333,10 @@ static int max17042_get_property(struct power_supply *psy,
>  		val->intval = data * 625 / 8;
>  		break;
>  	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
> -		ret = regmap_read(map, MAX17042_AvgVCELL, &data);
> +		ret = max17042_get_avgvcell_uv(chip, &data);
>  		if (ret < 0)
>  			return ret;
> -
> -		val->intval = data * 625 / 8;
> +		val->intval = data;
>  		break;
>  	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
>  		ret = regmap_read(map, MAX17042_OCVInternal, &data);
> @@ -350,14 +378,9 @@ static int max17042_get_property(struct power_supply *psy,
>  		val->intval = data64;
>  		break;
>  	case POWER_SUPPLY_PROP_CHARGE_NOW:
> -		ret = regmap_read(map, MAX17042_RepCap, &data);
> +		ret = max17042_get_repcap_uah(chip, &data64);
>  		if (ret < 0)
>  			return ret;
> -
> -		data64 = data * 5000000ll;
> -		data64 *= chip->task_period;
> -		do_div(data64, MAX17042_DEFAULT_TASK_PERIOD);
> -		do_div(data64, chip->pdata->r_sns);
>  		val->intval = data64;
>  		break;
>  	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
> @@ -370,6 +393,17 @@ static int max17042_get_property(struct power_supply *psy,
>  		data64 = div_s64(data64, MAX17042_DEFAULT_TASK_PERIOD);
>  		val->intval = div_s64(data64, chip->pdata->r_sns);
>  		break;
> +	case POWER_SUPPLY_PROP_ENERGY_NOW:
> +		ret = max17042_get_repcap_uah(chip, &data64);
> +		if (ret < 0)
> +			return ret;
> +
> +		ret = max17042_get_avgvcell_uv(chip, &data);
> +		if (ret < 0)
> +			return ret;
> +
> +		val->intval = data64 * data / 1000000;
> +		break;
>  	case POWER_SUPPLY_PROP_TEMP:
>  		ret = max17042_get_temperature(chip, &val->intval);
>  		if (ret < 0)
> 


      reply	other threads:[~2026-05-15  7:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15  5:48 [PATCH 0/3] Add support for Battery Status AMS Amit Sunil Dhamne via B4 Relay
2026-05-15  5:48 ` [PATCH 1/3] power: Add power_supply_get_battery_all() to fetch battery psy handles Amit Sunil Dhamne via B4 Relay
2026-05-15  7:15   ` Hans de Goede
2026-05-15  5:48 ` [PATCH 2/3] usb: typec: tcpm: Add support for Battery Status response message Amit Sunil Dhamne via B4 Relay
2026-05-15  5:48 ` [PATCH 3/3] power: supply: max17042: add handler for energy_now property Amit Sunil Dhamne via B4 Relay
2026-05-15  7:25   ` Hans de Goede [this message]

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=17f49974-1f39-4b4c-8577-da33da7f1cc4@kernel.org \
    --to=hansg@kernel.org \
    --cc=amitsd@google.com \
    --cc=andre.draszik@linaro.org \
    --cc=badhri@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=kernel@puri.sm \
    --cc=krzk@kernel.org \
    --cc=kyletso@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=peter.griffin@linaro.org \
    --cc=rdbabiera@google.com \
    --cc=sebastian.krzyszkowiak@puri.sm \
    --cc=sre@kernel.org \
    --cc=tudor.ambarus@linaro.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