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 v4 1/2] power: supply: Add helpers to get and put arrays of power supply handles
Date: Thu, 2 Jul 2026 14:42:07 +0200 [thread overview]
Message-ID: <9bbe1461-a731-4cbc-89db-2ff0866bd3d7@kernel.org> (raw)
In-Reply-To: <20260701-batt-status-v4-1-a31d97b1ae57@google.com>
Hi,
On 1-Jul-26 02:34, Amit Sunil Dhamne via B4 Relay wrote:
> From: Amit Sunil Dhamne <amitsd@google.com>
>
> Add power_supply_get_system_batteries() to allow drivers to obtain
> a list of registered battery type power supply references in the
> system. Also add power_supply_put_system_batteries() to perform
> cleanup after the former function is called.
>
> Signed-off-by: Amit Sunil Dhamne <amitsd@google.com>
Thanks, patch looks good to me:
Reviewed-by: Hans de Goede <johannes.goede@oss.qualcomm.com>
Regards,
Hans
> ---
> drivers/power/supply/power_supply_core.c | 153 +++++++++++++++++++++++++++++++
> include/linux/power_supply.h | 15 +++
> 2 files changed, 168 insertions(+)
>
> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
> index 2532e221b2e1..70f35f64cc72 100644
> --- a/drivers/power/supply/power_supply_core.c
> +++ b/drivers/power/supply/power_supply_core.c
> @@ -477,6 +477,159 @@ struct power_supply *power_supply_get_by_name(const char *name)
> }
> EXPORT_SYMBOL_GPL(power_supply_get_by_name);
>
> +static bool power_supply_is_system_battery(struct power_supply *psy)
> +{
> + union power_supply_propval val;
> +
> + if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
> + return false;
> +
> + if (!power_supply_get_property_direct(psy, POWER_SUPPLY_PROP_SCOPE,
> + &val))
> + if (val.intval == POWER_SUPPLY_SCOPE_DEVICE)
> + return false;
> +
> + return true;
> +}
> +
> +static int __power_supply_get_num_system_batteries(struct power_supply *epsy,
> + void *data)
> +{
> + int *count = data;
> +
> + if (power_supply_is_system_battery(epsy))
> + (*count)++;
> +
> + return 0;
> +}
> +
> +static int power_supply_get_num_system_batteries(struct device *dev)
> +{
> + int ret, count = 0;
> +
> + ret = power_supply_for_each_psy(&count,
> + __power_supply_get_num_system_batteries);
> +
> + 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_system_batteries_array(struct power_supply *epsy,
> + void *_data)
> +{
> + struct psy_get_supplies_data *data = _data;
> +
> + if (power_supply_is_system_battery(epsy)) {
> + 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_system_batteries_array(struct device *dev, int size,
> + struct power_supply **batteries)
> +{
> + int ret;
> +
> + struct psy_get_supplies_data data = {
> + .cnt = 0,
> + .size = size,
> + .psys = batteries,
> + };
> +
> + ret = power_supply_for_each_psy(&data,
> + __power_supply_populate_system_batteries_array);
> +
> + dev_dbg(dev, "%s Found %d batteries with array size %d ret %d\n",
> + __func__, data.cnt, data.size, ret);
> +
> + if (ret < 0 || !data.cnt) {
> + power_supply_put_system_batteries(batteries, data.cnt);
> + return ret;
> + }
> +
> + return data.cnt;
> +}
> +
> +/**
> + * power_supply_get_system_batteries() - Fetches references to battery type
> + * power supplies in the system.
> + * @dev: Pointer to device requesting the power supply refs.
> + * @psys: Pointer to an array of power supply refs.
> + *
> + * Helper function to get handles to battery type power supplies in the system.
> + * If acquiring a ref to a power supply fails, then the search for battery type
> + * power supplies will abort and the acquired power supply references will be
> + * released.
> + *
> + * Return: Indicates the number of battery type power supplies returned on
> + * success or a negative error code on failure.
> + *
> + * Call power_supply_put_system_batteries() after use to cleanup resources.
> + */
> +int __must_check power_supply_get_system_batteries(struct device *dev,
> + struct power_supply ***psys)
> +{
> + int ret;
> +
> + if (!psys)
> + return -EINVAL;
> +
> + ret = power_supply_get_num_system_batteries(dev);
> + if (ret <= 0) {
> + *psys = NULL;
> + return ret;
> + }
> +
> + *psys = kzalloc_objs(**psys, ret);
> + if (!*psys)
> + return -ENOMEM;
> +
> + ret = power_supply_populate_system_batteries_array(dev, ret, *psys);
> + if (ret <= 0)
> + *psys = NULL;
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(power_supply_get_system_batteries);
> +
> +/**
> + * power_supply_put_system_batteries() - Cleanup resources allocated by
> + * power_supply_get_system_batteries()
> + * @psys: Array of power supply references to release and free.
> + * @count: Number of elements in the array.
> + */
> +void power_supply_put_system_batteries(struct power_supply **psys, int count)
> +{
> + int i;
> +
> + for (i = 0; i < count; i++) {
> + if (psys[i])
> + power_supply_put(psys[i]);
> + }
> +
> + kfree(psys);
> +}
> +EXPORT_SYMBOL_GPL(power_supply_put_system_batteries);
> +
> /**
> * 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..9167a33d074b 100644
> --- a/include/linux/power_supply.h
> +++ b/include/linux/power_supply.h
> @@ -806,11 +806,26 @@ 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_system_batteries(struct device *dev,
> + struct power_supply ***psys);
> +extern void power_supply_put_system_batteries(struct power_supply **psys, int count);
> 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_system_batteries(struct device *dev,
> + struct power_supply ***psys)
> +{
> + if (psys)
> + *psys = NULL;
> + return 0;
> +}
> +
> +static inline void power_supply_put_system_batteries(struct power_supply **psys,
> + int count)
> +{
> +}
> #endif
> extern struct power_supply *power_supply_get_by_reference(struct fwnode_handle *fwnode,
> const char *property);
>
next prev parent reply other threads:[~2026-07-02 12:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-01 0:34 [PATCH v4 0/2] Add support for Battery Status AMS Amit Sunil Dhamne via B4 Relay
2026-07-01 0:34 ` [PATCH v4 1/2] power: supply: Add helpers to get and put arrays of power supply handles Amit Sunil Dhamne via B4 Relay
2026-07-02 12:42 ` Hans de Goede [this message]
2026-07-01 0:34 ` [PATCH v4 2/2] usb: typec: tcpm: Add support for Battery Status response message Amit Sunil Dhamne via B4 Relay
2026-07-01 21:49 ` 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=9bbe1461-a731-4cbc-89db-2ff0866bd3d7@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