From patchwork Sat Jan 16 12:40:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Biju Das X-Patchwork-Id: 1427489 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=bp.renesas.com Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DHyMj4ddWz9sVs for ; Sat, 16 Jan 2021 23:41:05 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0B7F3827CF; Sat, 16 Jan 2021 13:40:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=bp.renesas.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 2AD0A827D2; Sat, 16 Jan 2021 13:40:54 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=AC_FROM_MANY_DOTS,BAYES_00, KHOP_HELO_FCRDNS,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 Received: from relmlie5.idc.renesas.com (relmlor1.renesas.com [210.160.252.171]) by phobos.denx.de (Postfix) with ESMTP id D264C827C1 for ; Sat, 16 Jan 2021 13:40:48 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=bp.renesas.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=biju.das.jz@bp.renesas.com X-IronPort-AV: E=Sophos;i="5.79,352,1602514800"; d="scan'208";a="69195233" Received: from unknown (HELO relmlir6.idc.renesas.com) ([10.200.68.152]) by relmlie5.idc.renesas.com with ESMTP; 16 Jan 2021 21:40:46 +0900 Received: from localhost.localdomain (unknown [172.29.53.99]) by relmlir6.idc.renesas.com (Postfix) with ESMTP id E9EFF425ACC3; Sat, 16 Jan 2021 21:40:43 +0900 (JST) From: Biju Das To: Simon Glass Cc: Biju Das , u-boot@lists.denx.de, Tom Rini , Adam Ford , Marek Vasut , Nobuhiro Iwamatsu , Chris Paterson , Prabhakar Mahadev Lad Subject: [PATCH v5] dm: core: Add of_match_node helper function Date: Sat, 16 Jan 2021 12:40:42 +0000 Message-Id: <20210116124042.24007-1-biju.das.jz@bp.renesas.com> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean Add of_match_node() helper function to iterate over the device tree and tell if a device_node has a matching of_match structure. Signed-off-by: Biju Das Reviewed-by: Lad Prabhakar Reviewed-by: Simon Glass --- v4->v5: * Rebased to latest master v3->v4: No change * Added Simon's Rb tag. v2->v3: * Added a test case for of_match_node helper function. (Ref: https://patchwork.ozlabs.org/project/uboot/patch/20201102150959.4793-2-biju.das.jz@bp.renesas.com/) v1->v2: * No Change v1: * New Patch --- drivers/core/device.c | 21 +++++++++++++++++++++ include/dm/device.h | 13 +++++++++++++ test/dm/core.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/drivers/core/device.c b/drivers/core/device.c index aeab3836ed..4ff708fd38 100644 --- a/drivers/core/device.c +++ b/drivers/core/device.c @@ -1022,6 +1022,27 @@ bool of_machine_is_compatible(const char *compat) return !fdt_node_check_compatible(fdt, 0, compat); } +static +const struct udevice_id *__of_match_node(const struct udevice_id *matches, + const ofnode node) +{ + if (!matches) + return NULL; + + for (; matches->compatible; matches++) { + if (ofnode_device_is_compatible(node, matches->compatible)) + return matches; + } + + return NULL; +} + +const struct udevice_id *of_match_node(const struct udevice_id *matches, + const ofnode node) +{ + return __of_match_node(matches, node); +} + int dev_disable_by_path(const char *path) { struct uclass *uc; diff --git a/include/dm/device.h b/include/dm/device.h index f5b4cd6876..950fc78184 100644 --- a/include/dm/device.h +++ b/include/dm/device.h @@ -802,6 +802,19 @@ bool device_is_compatible(const struct udevice *dev, const char *compat); */ bool of_machine_is_compatible(const char *compat); +/** + * of_match_node() - Tell if a device_node has a matching of_match structure + * + * + * Low level utility function used by device matching. + * + * @matches: array of of device match structures to search in + * @node: the of device structure to match against + * @return matching structure on success, NULL if the match is not found + */ +const struct udevice_id *of_match_node(const struct udevice_id *matches, + const ofnode node); + /** * dev_disable_by_path() - Disable a device given its device tree path * diff --git a/test/dm/core.c b/test/dm/core.c index 1f5ca570dc..aae4d8427d 100644 --- a/test/dm/core.c +++ b/test/dm/core.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1067,6 +1068,36 @@ static int dm_test_inactive_child(struct unit_test_state *uts) } DM_TEST(dm_test_inactive_child, UT_TESTF_SCAN_PDATA); +static int dm_test_of_match_node(struct unit_test_state *uts) +{ + const ulong test_data_expected = 0x1234; + ofnode root_node = ofnode_path("/"); + const struct udevice_id *match; + unsigned long match_data; + + const struct udevice_id soc_device_ids[] = { + { .compatible = "sandbox", .data = test_data_expected, }, + { /* sentinel */ } + }; + + const struct udevice_id soc_device_nomatch_ids[] = { + { .compatible = "sandbox123", .data = test_data_expected, }, + { /* sentinel */ } + }; + + match = of_match_node(soc_device_ids, root_node); + ut_assert(match); + + match_data = match->data; + ut_asserteq(match_data, test_data_expected); + + match = of_match_node(soc_device_nomatch_ids, root_node); + ut_asserteq_ptr(match, NULL); + + return 0; +} +DM_TEST(dm_test_of_match_node, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); + /* Make sure all bound devices have a sequence number */ static int dm_test_all_have_seq(struct unit_test_state *uts) {