2018-09-20 05:44:05

by Silesh C V

[permalink] [raw]
Subject: [PATCH] Driver core: add bus_find_device_by_of_node

Similar to bus_find_device_by_name, but finds the device having a
specific of_node.

Signed-off-by: Silesh C V <[email protected]>
---
drivers/base/bus.c | 21 +++++++++++++++++++++
include/linux/device.h | 3 +++
2 files changed, 24 insertions(+)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 8bfd27e..39f90d1 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -373,6 +373,27 @@ struct device *bus_find_device_by_name(struct bus_type *bus,
}
EXPORT_SYMBOL_GPL(bus_find_device_by_name);

+static int match_of_node(struct device *dev, void *data)
+{
+ struct device_node *node = data;
+
+ return dev->of_node == node;
+}
+
+/**
+ * bus_find_device_by_name - device iterator for locating a particular device
+ * having a specific device node
+ * @bus: bus type
+ * @start: Device to begin with
+ * @node: device node pointer
+ */
+struct device *bus_find_device_by_of_node(struct bus_type *bus,
+ struct device *start, struct device_node *node)
+{
+ return bus_find_device(bus, start, (void *)node, match_of_node);
+}
+EXPORT_SYMBOL_GPL(bus_find_device_by_of_node);
+
/**
* subsys_find_device_by_id - find a device with a specific enumeration number
* @subsys: subsystem
diff --git a/include/linux/device.h b/include/linux/device.h
index 055a69d..49006fd 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -171,6 +171,9 @@ struct device *bus_find_device(struct bus_type *bus, struct device *start,
struct device *bus_find_device_by_name(struct bus_type *bus,
struct device *start,
const char *name);
+struct device *bus_find_device_by_of_node(struct bus_type *bus,
+ struct device *start,
+ struct device_node *node);
struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
struct device *hint);
int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
--
1.9.1



2018-09-20 07:37:20

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] Driver core: add bus_find_device_by_of_node

On Thu, Sep 20, 2018 at 7:43 AM Silesh C V <[email protected]> wrote:
>
> Similar to bus_find_device_by_name, but finds the device having a
> specific of_node.

First, what do you need it for? Please describe your use case in the changelog.

Second, what about a more generic bus_find_device_by_fwnode() ?

Thanks,
Rafael

2018-09-21 03:47:13

by Silesh C V

[permalink] [raw]
Subject: Re: [PATCH] Driver core: add bus_find_device_by_of_node

Hello Rafael,

On Thu, Sep 20, 2018 at 1:06 PM Rafael J. Wysocki <[email protected]> wrote:
>
> On Thu, Sep 20, 2018 at 7:43 AM Silesh C V <[email protected]> wrote:
> >
> > Similar to bus_find_device_by_name, but finds the device having a
> > specific of_node.
>
> First, what do you need it for? Please describe your use case in the changelog.

This will be useful for drivers that do not register a platform_driver
(but retrieve the "simple-bus" device_node using
of_find_compatible_node etc.) but are still interested in the struct
device.

>
> Second, what about a more generic bus_find_device_by_fwnode() ?

If you think that the above requirement warrants the inclusion of
bus_find_device_by_of_node, I can try implementing this in v2.

Thanks.

Regards,
Silesh

2018-09-21 09:19:02

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH] Driver core: add bus_find_device_by_of_node

On Fri, Sep 21, 2018 at 5:45 AM Silesh C V <[email protected]> wrote:
>
> Hello Rafael,
>
> On Thu, Sep 20, 2018 at 1:06 PM Rafael J. Wysocki <[email protected]> wrote:
> >
> > On Thu, Sep 20, 2018 at 7:43 AM Silesh C V <[email protected]> wrote:
> > >
> > > Similar to bus_find_device_by_name, but finds the device having a
> > > specific of_node.
> >
> > First, what do you need it for? Please describe your use case in the changelog.
>
> This will be useful for drivers that do not register a platform_driver
> (but retrieve the "simple-bus" device_node using
> of_find_compatible_node etc.) but are still interested in the struct
> device.
>
> >
> > Second, what about a more generic bus_find_device_by_fwnode() ?
>
> If you think that the above requirement warrants the inclusion of
> bus_find_device_by_of_node, I can try implementing this in v2.

I'm not really sure what you mean, but if there is
bus_find_device_by_fwnode(), it will be useful not just for DT code,
but generally, and getting an of_node from fwnode is trivial.

Thanks,
Rafael

2018-09-21 09:47:21

by Silesh C V

[permalink] [raw]
Subject: Re: [PATCH] Driver core: add bus_find_device_by_of_node

Hello Rafael,

On Fri, Sep 21, 2018 at 2:47 PM Rafael J. Wysocki <[email protected]> wrote:
>
> >
> > >
> > > Second, what about a more generic bus_find_device_by_fwnode() ?
> >
> > If you think that the above requirement warrants the inclusion of
> > bus_find_device_by_of_node, I can try implementing this in v2.
>
> I'm not really sure what you mean, but if there is
> bus_find_device_by_fwnode(), it will be useful not just for DT code,
> but generally, and getting an of_node from fwnode is trivial.
>

OK. Will do this and send out a patch soon.

Thanks,
Silesh