From: Amit Sunil Dhamne via B4 Relay <devnull+amitsd.google.com@kernel.org>
To: Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Badhri Jagan Sridharan <badhri@google.com>,
Sebastian Reichel <sre@kernel.org>,
Heikki Krogerus <heikki.krogerus@linux.intel.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Len Brown <len.brown@intel.com>, Pavel Machek <pavel@kernel.org>
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-usb@vger.kernel.org, linux-pm@vger.kernel.org,
RD Babiera <rdbabiera@google.com>, Kyle Tso <kyletso@google.com>,
Amit Sunil Dhamne <amitsd@google.com>
Subject: [PATCH 2/5] power: supply: core: add function to get supplies from fwnode
Date: Wed, 12 Mar 2025 16:42:02 -0700 [thread overview]
Message-ID: <20250312-batt_ops-v1-2-88e0bb3129fd@google.com> (raw)
In-Reply-To: <20250312-batt_ops-v1-0-88e0bb3129fd@google.com>
From: Amit Sunil Dhamne <amitsd@google.com>
Add a new helper function power_supply_get_by_fwnode_reference_array()
to retrieve a list of power_supplies associated with the fwnode's
property. The property can contain multiple nodes where each node is
associated with a power_supply. The list of power_supply objects will be
stored in an array supplied by the caller and the return value will
indicate the size of the resulting array.
Signed-off-by: Amit Sunil Dhamne <amitsd@google.com>
---
drivers/power/supply/power_supply_core.c | 60 ++++++++++++++++++++++++++++++++
include/linux/power_supply.h | 5 +++
2 files changed, 65 insertions(+)
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index 76c340b38015af0a67a0d91305e6242a8646bf53..df1a52f85125748c4fdcb10687aa7ed2f626ded1 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -593,6 +593,66 @@ struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
#endif /* CONFIG_OF */
+static int power_supply_match_fwnode(struct device *dev, const void *data)
+{
+ return dev && dev->parent && dev->parent->fwnode == data;
+}
+
+/**
+ * power_supply_get_by_fwnode_reference_array() - Returns an array of power
+ * supply objects associated with each fwnode reference present in the property
+ * @fwnode: Pointer to fwnode to lookup property
+ * @property: Name of property holding references
+ * @psy: Resulting array of power_supply pointers. To be provided by the caller.
+ * @size: size of power_supply pointer array.
+ *
+ * If power supply was found, it increases reference count for the
+ * internal power supply's device. The user should power_supply_put()
+ * after usage.
+ *
+ * Return: On success returns the number of power supply objects filled
+ * in the @psy array.
+ * -EOVERFLOW when size of @psy array is not suffice.
+ * -EINVAL when @psy is NULL or @size is 0.
+ * -ENODATA when fwnode does not contain the given property
+ */
+int power_supply_get_by_fwnode_reference_array(struct fwnode_handle *fwnode,
+ const char *property,
+ struct power_supply **psy,
+ ssize_t size)
+{
+ int ret, index, count = 0;
+ struct fwnode_reference_args args;
+ struct device *dev;
+
+ if (!psy || !size)
+ return -EINVAL;
+
+ for (index = 0; index < size &&
+ !(ret = fwnode_property_get_reference_args(fwnode, property, NULL,
+ 0, index, &args));
+ ++index) {
+ dev = class_find_device(&power_supply_class, NULL, args.fwnode,
+ power_supply_match_fwnode);
+ fwnode_handle_put(args.fwnode);
+ if (!dev)
+ continue;
+
+ if (count > size)
+ return -EOVERFLOW;
+
+ psy[count] = dev_get_drvdata(dev);
+ atomic_inc(&psy[count]->use_cnt);
+ ++count;
+ }
+
+ if (ret != -ENOENT)
+ return ret;
+
+ return index ? count : -ENODATA;
+}
+EXPORT_SYMBOL_GPL(power_supply_get_by_fwnode_reference_array);
+
int power_supply_get_battery_info(struct power_supply *psy,
struct power_supply_battery_info **info_out)
{
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 6ed53b292162469d7b357734d5589bff18a201d0..3f062607e5cd7c7f04384e34128ae0953e25d981 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -820,6 +820,11 @@ devm_power_supply_get_by_phandle(struct device *dev, const char *property)
{ return NULL; }
#endif /* CONFIG_OF */
+extern int
+power_supply_get_by_fwnode_reference_array(struct fwnode_handle *fwnode,
+ const char *property,
+ struct power_supply **psy,
+ ssize_t size);
extern const enum power_supply_property power_supply_battery_info_properties[];
extern const size_t power_supply_battery_info_properties_size;
extern int power_supply_get_battery_info(struct power_supply *psy,
--
2.49.0.rc0.332.g42c0ae87b1-goog
next prev parent reply other threads:[~2025-03-12 23:43 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-12 23:42 [PATCH 0/5] Add support for Battery Status & Battery Caps AMS in TCPM Amit Sunil Dhamne via B4 Relay
2025-03-12 23:42 ` [PATCH 1/5] dt-bindings: connector: add fixed-batteries property Amit Sunil Dhamne via B4 Relay
2025-03-13 8:48 ` Krzysztof Kozlowski
2025-03-15 0:56 ` Amit Sunil Dhamne
2025-03-16 16:49 ` Krzysztof Kozlowski
2025-03-20 19:49 ` Amit Sunil Dhamne
2025-03-16 16:55 ` Krzysztof Kozlowski
2025-03-20 19:45 ` Amit Sunil Dhamne
2025-03-12 23:42 ` Amit Sunil Dhamne via B4 Relay [this message]
2025-03-19 13:54 ` [PATCH 2/5] power: supply: core: add function to get supplies from fwnode Heikki Krogerus
2025-04-08 19:54 ` Amit Sunil Dhamne
2025-03-12 23:42 ` [PATCH 3/5] usb: typec: tcpm: Add support for Battery Status response message Amit Sunil Dhamne via B4 Relay
2025-03-12 23:42 ` [PATCH 4/5] power: supply: core: add vendor and product id properties Amit Sunil Dhamne via B4 Relay
2025-03-12 23:42 ` [PATCH 5/5] usb: typec: tcpm: Add support for Battery Cap response message Amit Sunil Dhamne via B4 Relay
2025-03-13 8:50 ` [PATCH 0/5] Add support for Battery Status & Battery Caps AMS in TCPM Krzysztof Kozlowski
2025-03-15 0:49 ` Amit Sunil Dhamne
2025-03-16 16:52 ` Krzysztof Kozlowski
2025-03-20 21:11 ` Amit Sunil Dhamne
2025-03-21 7:51 ` Krzysztof Kozlowski
2025-04-03 3:41 ` Amit Sunil Dhamne
2025-04-03 8:00 ` Krzysztof Kozlowski
2025-04-03 8:02 ` Krzysztof Kozlowski
2025-04-08 19:50 ` 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=20250312-batt_ops-v1-2-88e0bb3129fd@google.com \
--to=devnull+amitsd.google.com@kernel.org \
--cc=amitsd@google.com \
--cc=badhri@google.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=krzk+dt@kernel.org \
--cc=kyletso@google.com \
--cc=len.brown@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=pavel@kernel.org \
--cc=rafael@kernel.org \
--cc=rdbabiera@google.com \
--cc=robh@kernel.org \
--cc=sre@kernel.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