From: Mehmet Fide <[email protected]> The sequence number an aliases entry gives a node can only be looked up for a device, through dev_read_alias_seq(). A driver that has to know the number of a node it does not own, one it reached through a phandle for instance, has no livetree call for it and is left with fdtdec.
Move the body of dev_read_alias_seq() to the ofnode level and let both of its variants call it. No functional change, so the moved lines keep the ENOTSUPP return, the #if and the fdtdec call they had, which is what checkpatch complains about here. A test checks the lookup on the live and on the flat tree. Signed-off-by: Mehmet Fide <[email protected]> --- Changes in v4: - added a test, run on the live and on the flat tree - the kernel-doc no longer promises -ENOENT for a missing alias, the live tree returns -ENODEV drivers/core/ofnode.c | 20 ++++++++++++++++++++ drivers/core/read.c | 20 ++------------------ include/dm/ofnode.h | 13 +++++++++++++ include/dm/read.h | 8 ++------ test/dm/ofnode.c | 20 ++++++++++++++++++++ 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index b5d13c43db1..81d91ce5476 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -1233,6 +1233,26 @@ ofnode ofnode_get_aliases_node(const char *name) return ofnode_path(prop); } +int ofnode_get_alias_seq(ofnode node, const char *stem, int *seqp) +{ + int ret = -ENOTSUPP; + + if (ofnode_is_np(node)) { + ret = of_alias_get_id(ofnode_to_np(node), stem); + if (ret >= 0) { + *seqp = ret; + ret = 0; + } + } else { +#if CONFIG_IS_ENABLED(OF_CONTROL) + ret = fdtdec_get_alias_seq(ofnode_to_fdt(node), stem, + ofnode_to_offset(node), seqp); +#endif + } + + return ret; +} + int ofnode_get_child_count(ofnode parent) { ofnode child; diff --git a/drivers/core/read.c b/drivers/core/read.c index ba48862f44b..985f6128d97 100644 --- a/drivers/core/read.c +++ b/drivers/core/read.c @@ -356,24 +356,8 @@ const void *dev_read_prop_by_prop(struct ofprop *prop, int dev_read_alias_seq(const struct udevice *dev, int *devnump) { - ofnode node = dev_ofnode(dev); - const char *uc_name = dev->uclass->uc_drv->name; - int ret = -ENOTSUPP; - - if (ofnode_is_np(node)) { - ret = of_alias_get_id(ofnode_to_np(node), uc_name); - if (ret >= 0) { - *devnump = ret; - ret = 0; - } - } else { -#if CONFIG_IS_ENABLED(OF_CONTROL) - ret = fdtdec_get_alias_seq(gd->fdt_blob, uc_name, - ofnode_to_offset(node), devnump); -#endif - } - - return ret; + return ofnode_get_alias_seq(dev_ofnode(dev), + dev->uclass->uc_drv->name, devnump); } int dev_read_u32_array(const struct udevice *dev, const char *propname, diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h index c905e86b283..e13271a824f 100644 --- a/include/dm/ofnode.h +++ b/include/dm/ofnode.h @@ -1180,6 +1180,19 @@ const void *ofnode_read_aliases_prop(const char *propname, int *sizep); */ ofnode ofnode_get_aliases_node(const char *propname); +/** + * ofnode_get_alias_seq() - get the sequence number of a node from its alias + * + * The aliases node can name a node with a stem and a number, such as + * "serial2". This looks the node up and returns the number. + * + * @node: Node to look for + * @stem: Alias stem, e.g. "serial" + * @seqp: Returns the sequence number of the alias, if found + * Return: 0 if a sequence number was found, -ve if not + */ +int ofnode_get_alias_seq(ofnode node, const char *stem, int *seqp); + struct display_timing; /** * ofnode_decode_display_timing() - decode display timings diff --git a/include/dm/read.h b/include/dm/read.h index 12dcde6645c..d0c17c3d343 100644 --- a/include/dm/read.h +++ b/include/dm/read.h @@ -1168,12 +1168,8 @@ static inline const void *dev_read_prop_by_prop(struct ofprop *prop, static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump) { -#if CONFIG_IS_ENABLED(OF_CONTROL) - return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name, - dev_of_offset(dev), devnump); -#else - return -ENOTSUPP; -#endif + return ofnode_get_alias_seq(dev_ofnode(dev), + dev->uclass->uc_drv->name, devnump); } static inline int dev_read_u32_array(const struct udevice *dev, diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c index 0f60c2a6281..07c1965dd34 100644 --- a/test/dm/ofnode.c +++ b/test/dm/ofnode.c @@ -523,6 +523,26 @@ static int dm_test_ofnode_read_aliases(struct unit_test_state *uts) } DM_TEST(dm_test_ofnode_read_aliases, UTF_SCAN_PDATA | UTF_SCAN_FDT); +/* test ofnode_get_alias_seq() */ +static int dm_test_ofnode_get_alias_seq(struct unit_test_state *uts) +{ + int seq; + + ut_assertok(ofnode_get_alias_seq(ofnode_path("/i2c@0"), "i2c", &seq)); + ut_asserteq(0, seq); + + ut_assertok(ofnode_get_alias_seq(ofnode_path("/some-bus/c-test@1"), + "testfdt", &seq)); + ut_asserteq(12, seq); + + ut_assert(ofnode_get_alias_seq(ofnode_path("/i2c@0"), "spi", &seq) < 0); + ut_assert(ofnode_get_alias_seq(ofnode_path("/some-bus"), "testfdt", + &seq) < 0); + + return 0; +} +DM_TEST(dm_test_ofnode_get_alias_seq, UTF_SCAN_FDT); + static int dm_test_ofnode_get_child_count(struct unit_test_state *uts) { ofnode node, child_node; -- 2.55.0
