Linux USB
 help / color / mirror / Atom feed
From: Amit Sunil Dhamne <amitsd@google.com>
To: Hans de Goede <hansg@kernel.org>,
	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 1/3] power: Add power_supply_get_battery_all() to fetch battery psy handles
Date: Fri, 15 May 2026 20:16:48 -0700	[thread overview]
Message-ID: <21100c24-1499-48c9-b8cc-0e82cf931c59@google.com> (raw)
In-Reply-To: <f34a38e9-c6bc-4ccb-9c9b-23288e09d734@kernel.org>

Hi Hans,

On 5/15/26 12:15 AM, Hans de Goede wrote:
> Hi,
>
> On 15-May-26 07:48, Amit Sunil Dhamne via B4 Relay wrote:
>> From: Amit Sunil Dhamne <amitsd@google.com>
>>
>> Add power_supply_get_battery_all() to allow drivers to obtain a list of
>> registered battery type power supply references in the system.
>>
>> Signed-off-by: Amit Sunil Dhamne <amitsd@google.com>
>> ---
>>  drivers/power/supply/power_supply_core.c | 122 +++++++++++++++++++++++++++++++
>>  include/linux/power_supply.h             |   9 +++
>>  2 files changed, 131 insertions(+)
>>
>> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
>> index a446d3d086fc..697645426fb1 100644
>> --- a/drivers/power/supply/power_supply_core.c
>> +++ b/drivers/power/supply/power_supply_core.c
>> @@ -482,6 +482,128 @@ struct power_supply *power_supply_get_by_name(const char *name)
>>  }
>>  EXPORT_SYMBOL_GPL(power_supply_get_by_name);
>>  
>> +static int __power_supply_get_num_battery(struct power_supply *epsy, void *data)
>> +{
>> +	int *count = data;
>> +
>> +	if (epsy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
>> +		(*count)++;
>> +
>> +	return 0;
>> +}
>> +
>> +static int power_supply_get_num_battery(struct device *dev)
>> +{
>> +	int ret, count = 0;
>> +
>> +	ret = power_supply_for_each_psy(&count, __power_supply_get_num_battery);
>> +
>> +	dev_dbg(dev, "%s: count: %d ret %d\n", __func__, count, ret);
>> +
>> +	if (ret)
>> +		return ret;
>> +
>> +	return count;
>> +}
>> +
>> +struct psy_get_supplies_data {
>> +	int cnt;
>> +	int size;
>> +	struct power_supply **psys;
>> +};
>> +
>> +static int __power_supply_populate_battery_array(struct power_supply *epsy,
>> +						 void *_data)
>> +{
>> +	struct psy_get_supplies_data *data = _data;
>> +
>> +	if (epsy->desc->type == POWER_SUPPLY_TYPE_BATTERY) {
>> +		if (data->size <= data->cnt)
>> +			return -EOVERFLOW;
>> +
>> +		get_device(&epsy->dev);
>> +		data->psys[data->cnt] = epsy;
>> +		atomic_inc(&epsy->use_cnt);
>> +		data->cnt++;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static int power_supply_populate_battery_array(struct device *dev, int size,
>> +					       struct power_supply **batteries)
>> +{
>> +	int ret, i;
>> +
>> +	struct psy_get_supplies_data data = {
>> +		.cnt = 0,
>> +		.size = size,
>> +		.psys = batteries,
>> +	};
>> +
>> +	ret = power_supply_for_each_psy(&data, __power_supply_populate_battery_array);
>> +
>> +	dev_dbg(dev, "%s Found %d batteries with array size %d ret %d\n",
>> +		__func__, data.cnt, data.size, ret);
>> +
>> +	if (ret < 0) {
>> +		for (i = 0; i < data.cnt; i++)
>> +			power_supply_put(batteries[i]);
>> +		return ret;
>> +	}
>> +
>> +	return data.cnt;
>> +}
>> +
>> +/**
>> + * power_supply_get_battery_all() - Searches for all battery type power supplies
>> + *				    and returns their references.
>> + * @dev: Pointer to device requesting the power supply refs.
>> + * @psys: Pointer to an array of power supply refs that will be filled by this
>> + *	  function.
>> + *
>> + * This function helps drivers get handles to all battery type power supplies.
>> + * If acquiring a ref to a power supply results in error, then the search for
>> + * battery type power supplies will abort and the acquired power supplies will
>> + * be "put".
>> + *
>> + * Return: Indicates the number of battery type power supplies returned on
>> + * success or the negative error code on failure.
>> + *
>> + * It's the responsibility of the caller to invoke power_supply_put() on the
>> + * individual psy refs and free the array returned by this function using kfree().
> Not a full review, just a quick remark: It seems to me that there should
> be some helper function e.g. :
>
> void power_supply_put_array(struct power_supply **psys, int count);
>
> Which can be called with the returned array + count to do this for the caller,
> rather then expecting all callers to open code this.

I see. I will add this as a new helper in the next revision.


Thanks,

Amit

>
> Regards,
>
> Hans
>
>
>> + */
>> +int __must_check power_supply_get_battery_all(struct device *dev,
>> +					      struct power_supply ***psys)
>> +{
>> +	int ret;
>> +
>> +	if (!psys)
>> +		return -EINVAL;
>> +
>> +	ret = power_supply_get_num_battery(dev);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	if (!ret) {
>> +		*psys = NULL;
>> +		return 0;
>> +	}
>> +
>> +	*psys = kzalloc_objs(**psys, ret);
>> +	if (!*psys)
>> +		return -ENOMEM;
>> +
>> +	ret = power_supply_populate_battery_array(dev, ret, *psys);
>> +	if (ret <= 0) {
>> +		kfree(*psys);
>> +		*psys = NULL;
>> +	}
>> +
>> +	return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(power_supply_get_battery_all);
>> +
>>  /**
>>   * power_supply_put() - Drop reference obtained with power_supply_get_by_name
>>   * @psy: Reference to put
>> diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
>> index 7a5e4c3242a0..2467530a5740 100644
>> --- a/include/linux/power_supply.h
>> +++ b/include/linux/power_supply.h
>> @@ -806,11 +806,20 @@ extern int power_supply_reg_notifier(struct notifier_block *nb);
>>  extern void power_supply_unreg_notifier(struct notifier_block *nb);
>>  #if IS_ENABLED(CONFIG_POWER_SUPPLY)
>>  extern struct power_supply *power_supply_get_by_name(const char *name);
>> +extern int __must_check power_supply_get_battery_all(struct device *dev,
>> +						     struct power_supply ***psys);
>>  extern void power_supply_put(struct power_supply *psy);
>>  #else
>>  static inline void power_supply_put(struct power_supply *psy) {}
>>  static inline struct power_supply *power_supply_get_by_name(const char *name)
>>  { return NULL; }
>> +static inline int __must_check power_supply_get_battery_all(struct device *dev,
>> +							    struct power_supply ***psys)
>> +{
>> +	if (psys)
>> +		*psys = NULL;
>> +	return 0;
>> +}
>>  #endif
>>  extern struct power_supply *power_supply_get_by_reference(struct fwnode_handle *fwnode,
>>  							  const char *property);
>>

  reply	other threads:[~2026-05-16  3:16 UTC|newest]

Thread overview: 8+ 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-16  3:16     ` Amit Sunil Dhamne [this message]
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
2026-05-15 22:22     ` Amit Sunil Dhamne

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=21100c24-1499-48c9-b8cc-0e82cf931c59@google.com \
    --to=amitsd@google.com \
    --cc=andre.draszik@linaro.org \
    --cc=badhri@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hansg@kernel.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