mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 04/16] driver: bus: add helpers for finding devices in busses
Date: Thu,  5 Jun 2025 23:07:14 +0200	[thread overview]
Message-ID: <20250605210726.1916656-5-a.fatoum@barebox.org> (raw)
In-Reply-To: <20250605210726.1916656-1-a.fatoum@barebox.org>

To make porting Linux drivers easier, let's import some bus lookup
functions that are going to be used in later commits.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/base/bus.c         | 56 ++++++++++++++++++++++++++++++++++++++
 include/linux/device.h     | 22 ---------------
 include/linux/device/bus.h | 38 ++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 22 deletions(-)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index fde27dd008e8..8edd8cb01fae 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -95,3 +95,59 @@ int device_match_of_modalias(struct device *dev, const struct driver *drv)
 
 	return false;
 }
+
+static struct device *__bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data,
+					 int (*fn)(struct device *dev, void *data), int *result)
+{
+	struct device *dev;
+	int ret;
+
+	bus_for_each_device(bus, dev) {
+		if (start) {
+			if (dev == start)
+				start = NULL;
+			continue;
+		}
+
+		ret = fn(dev, data);
+		if (ret) {
+			if (result)
+				*result = ret;
+			return dev;
+		}
+	}
+
+	return NULL;
+}
+
+int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data,
+		     int (*fn)(struct device *dev, void *data))
+{
+	int ret = 0;
+	__bus_for_each_dev(bus, start, data, fn, &ret);
+	return ret;
+}
+
+struct check_match_data {
+	device_match_t match;
+};
+
+struct device *bus_find_device(const struct bus_type *bus, struct device *start,
+			       const void *data, device_match_t match)
+{
+	return __bus_for_each_dev(bus, start, (void *)data,
+				  (int (*)(struct device *dev, void *data))match,
+				  NULL);
+}
+
+int device_match_name(struct device *dev, const void *name)
+{
+	return !strcmp(dev_name(dev), name);
+}
+EXPORT_SYMBOL_GPL(device_match_name);
+
+int device_match_of_node(struct device *dev, const void *np)
+{
+	return np && dev->of_node == np;
+}
+EXPORT_SYMBOL_GPL(device_match_of_node);
diff --git a/include/linux/device.h b/include/linux/device.h
index d4098b85239f..e3934b505fe0 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -52,28 +52,6 @@ static inline void __iomem *devm_ioremap(struct device *dev,
 	return IOMEM(start);
 }
 
-static inline int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data,
-				   int (*fn)(struct device *dev, void *data))
-{
-	struct device *dev;
-	int ret;
-
-	bus_for_each_device(bus, dev) {
-		if (start) {
-			if (dev == start)
-				start = NULL;
-			continue;
-		}
-
-		ret = fn(dev, data);
-		if (ret)
-			return ret;
-	}
-
-	return 0;
-}
-
-
 /**
  * dev_set_drvdata - set driver private data for device
  * @dev: device instance
diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h
index 8837ce3c2ed2..2364645bad4a 100644
--- a/include/linux/device/bus.h
+++ b/include/linux/device/bus.h
@@ -37,6 +37,44 @@ extern struct class bus_class;
  */
 #define bus_for_each_driver(bus, drv) list_for_each_entry(drv, &(bus)->driver_list, bus_list)
 
+int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data,
+		     int (*fn)(struct device *dev, void *data));
+
+/* Generic device matching functions that all busses can use to match with */
+int device_match_name(struct device *dev, const void *name);
+int device_match_of_node(struct device *dev, const void *np);
+
+/* Matching function type for drivers/base APIs to find a specific device */
+typedef int (*device_match_t)(struct device *dev, const void *data);
+
+struct device *bus_find_device(const struct bus_type *bus, struct device *start,
+			       const void *data, device_match_t match);
+/**
+ * bus_find_device_by_name - device iterator for locating a particular device
+ * of a specific name.
+ * @bus: bus type
+ * @start: Device to begin with
+ * @name: name of the device to match
+ */
+static inline struct device *bus_find_device_by_name(const struct bus_type *bus,
+						     struct device *start,
+						     const char *name)
+{
+	return bus_find_device(bus, start, name, device_match_name);
+}
+
+/**
+ * bus_find_device_by_of_node : device iterator for locating a particular device
+ * matching the of_node.
+ * @bus: bus type
+ * @np: of_node of the device to match.
+ */
+static inline struct device *
+bus_find_device_by_of_node(const struct bus_type *bus, const struct device_node *np)
+{
+	return bus_find_device(bus, NULL, np, device_match_of_node);
+}
+
 extern struct bus_type platform_bus;
 
 #endif
-- 
2.39.5




  parent reply	other threads:[~2025-06-05 21:14 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-05 21:07 [PATCH 00/16] ARM: stm32mp: add MIPI DSI support Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 01/16] driver: bus: embed bus driver node into bus Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 02/16] driver: switch busses to device class Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 03/16] driver: factor out bus definitions into separate header Ahmad Fatoum
2025-06-05 21:07 ` Ahmad Fatoum [this message]
2025-06-05 21:07 ` [PATCH 05/16] drive: bus: make use of new bus_find_device helper Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 06/16] of: implement of_alias_from_compatible Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 07/16] video: vpl: fix potential read of uninitialized variable Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 08/16] video: vpl: factor out vpl_for_each Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 09/16] video: vpl: handle missing struct vpl::ioctl gracefully Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 10/16] video: vpl: add vpl_bridge abstraction Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 11/16] video: factor out drm_mode_vrefresh Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 12/16] video: add base MIPI DSI support Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 13/16] video: add Designware MIPI-DSI support Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 14/16] video: add STM32 MIPI DSI video driver Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 15/16] video: add support for Orise Technology otm8009a panel Ahmad Fatoum
2025-06-05 21:07 ` [PATCH 16/16] ARM: stm32mp: dk2: enable MIPI-DSI display by default Ahmad Fatoum

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=20250605210726.1916656-5-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.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