diff mbox series

dm: core: introduce uclass_get_device_by_of_path()

Message ID 20230413151703.3567431-1-rasmus.villemoes@prevas.dk
State Accepted
Delegated to: Simon Glass
Headers show
Series dm: core: introduce uclass_get_device_by_of_path() | expand

Commit Message

Rasmus Villemoes April 13, 2023, 3:17 p.m. UTC
There's quite a few instances of board-specific code doing

  off = fdt_path_offset(gd->fdt_blob, ...);
  ...
  ret = uclass_get_device_by_of_offset(..., off, &dev);

looking for an eeprom or a pmic via some alias. Such code can be
simplified a little if we have a helper for directly getting a device
via device tree path (including being given as an alias).

Implement it in terms of ofnode rather than raw offsets so that this
will work whether live tree is enabled or not.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
---
 drivers/core/uclass.c |  6 ++++++
 include/dm/uclass.h   | 17 +++++++++++++++++
 2 files changed, 23 insertions(+)

Comments

Simon Glass April 19, 2023, 1:46 a.m. UTC | #1
Hi Rasmus,

On Thu, 13 Apr 2023 at 09:17, Rasmus Villemoes
<rasmus.villemoes@prevas.dk> wrote:
>
> There's quite a few instances of board-specific code doing
>
>   off = fdt_path_offset(gd->fdt_blob, ...);
>   ...
>   ret = uclass_get_device_by_of_offset(..., off, &dev);
>
> looking for an eeprom or a pmic via some alias. Such code can be
> simplified a little if we have a helper for directly getting a device
> via device tree path (including being given as an alias).
>
> Implement it in terms of ofnode rather than raw offsets so that this
> will work whether live tree is enabled or not.
>
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
> ---
>  drivers/core/uclass.c |  6 ++++++
>  include/dm/uclass.h   | 17 +++++++++++++++++
>  2 files changed, 23 insertions(+)

Looks fine but please add a test to ofnode.c

Regards,
Simon
Simon Glass April 28, 2023, 7:20 p.m. UTC | #2
Hi Rasmus,

On Thu, 13 Apr 2023 at 09:17, Rasmus Villemoes
<rasmus.villemoes@prevas.dk> wrote:
>
> There's quite a few instances of board-specific code doing
>
>   off = fdt_path_offset(gd->fdt_blob, ...);
>   ...
>   ret = uclass_get_device_by_of_offset(..., off, &dev);
>
> looking for an eeprom or a pmic via some alias. Such code can be
> simplified a little if we have a helper for directly getting a device
> via device tree path (including being given as an alias).
>
> Implement it in terms of ofnode rather than raw offsets so that this
> will work whether live tree is enabled or not.
>
> Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
> ---
>  drivers/core/uclass.c |  6 ++++++
>  include/dm/uclass.h   | 17 +++++++++++++++++
>  2 files changed, 23 insertions(+)

Looks fine but please add a test to ofnode.c

Regards,
Simon

Applied to u-boot-dm, thanks!
Simon Glass April 28, 2023, 7:21 p.m. UTC | #3
Hi Rasmus,

On Fri, 28 Apr 2023 at 13:20, Simon Glass <sjg@chromium.org> wrote:
>
> Hi Rasmus,
>
> On Thu, 13 Apr 2023 at 09:17, Rasmus Villemoes
> <rasmus.villemoes@prevas.dk> wrote:
> >
> > There's quite a few instances of board-specific code doing
> >
> >   off = fdt_path_offset(gd->fdt_blob, ...);
> >   ...
> >   ret = uclass_get_device_by_of_offset(..., off, &dev);
> >
> > looking for an eeprom or a pmic via some alias. Such code can be
> > simplified a little if we have a helper for directly getting a device
> > via device tree path (including being given as an alias).
> >
> > Implement it in terms of ofnode rather than raw offsets so that this
> > will work whether live tree is enabled or not.
> >
> > Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
> > ---
> >  drivers/core/uclass.c |  6 ++++++
> >  include/dm/uclass.h   | 17 +++++++++++++++++
> >  2 files changed, 23 insertions(+)
>
> Looks fine but please add a test to ofnode.c

Did you send a patch with a test? If so I missed it.

Also please check my tweak to this (OF_REAL)

Regards,
Simon
Rasmus Villemoes April 29, 2023, 9:33 a.m. UTC | #4
On 28/04/2023 21.21, Simon Glass wrote:

>> Looks fine but please add a test to ofnode.c
> 
> Did you send a patch with a test? If so I missed it.

No, sorry, haven't found the time yet.

> Also please check my tweak to this (OF_REAL)

Looks correct.

Rasmus
diff mbox series

Patch

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 3a919104a6..56ea878701 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -544,6 +544,12 @@  int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
 	return uclass_get_device_tail(dev, ret, devp);
 }
 
+int uclass_get_device_by_of_path(enum uclass_id id, const char *path,
+				 struct udevice **devp)
+{
+	return uclass_get_device_by_ofnode(id, ofnode_path(path), devp);
+}
+
 #if CONFIG_IS_ENABLED(OF_CONTROL)
 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
 				    struct udevice **devp)
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index ee15c92063..5c5fb9acac 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -264,6 +264,23 @@  int uclass_get_device_by_of_offset(enum uclass_id id, int node,
 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
 				struct udevice **devp);
 
+/**
+ * uclass_get_device_by_of_path() - Get a uclass device by device tree path
+ *
+ * This searches the devices in the uclass for one attached to the
+ * device tree node corresponding to the given path (which may also be
+ * an alias).
+ *
+ * The device is probed to activate it ready for use.
+ *
+ * @id: ID to look up
+ * @node: Device tree path to search for (if no such path then -ENODEV is returned)
+ * @devp: Returns pointer to device (there is only one for each node)
+ * Return: 0 if OK, -ve on error
+ */
+int uclass_get_device_by_of_path(enum uclass_id id, const char *path,
+				 struct udevice **devp);
+
 /**
  * uclass_get_device_by_phandle_id() - Get a uclass device by phandle id
  *