Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 09437ECAAA1 for ; Wed, 31 Aug 2022 03:10:28 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0D0F2849F8; Wed, 31 Aug 2022 05:09:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Ns309KuO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A31C782A33; Wed, 31 Aug 2022 05:09:38 +0200 (CEST) Received: from mail-oa1-x34.google.com (mail-oa1-x34.google.com [IPv6:2001:4860:4864:20::34]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DF17E849DE for ; Wed, 31 Aug 2022 05:09:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oa1-x34.google.com with SMTP id 586e51a60fabf-11f0fa892aeso13083310fac.7 for ; Tue, 30 Aug 2022 20:09:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=krTDv85lhA7X0MD/GPFxp369OnTIzswmvVGwie0jvVY=; b=Ns309KuOY2IT43Ti6Jmu0dSFKYqindCG501lg8CI7VbNYZT9nVaZeqG/lfVJnafJ3R H7DHb89wjQAI/cSIb8AH5cbeBfliIiKtuH3+DX4AqgQA02VZdA9cA5tJI27cOhNclxE/ mZvaxJ3AouLsExlND8n6w3jDbCKRjCKhLu8uI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=krTDv85lhA7X0MD/GPFxp369OnTIzswmvVGwie0jvVY=; b=BEAN4fB9qjEDObTSvsPxnRSFIH6HRfJU1JNdKO34OHJCJwYs6UW8vG20K4kuLtwls5 N8M7pHLnmPEt6ZW90M8+q73FjNAuwV63JtMQbKKbaUSX0GA4DrnsKoih+zmNz0B1UPKi dO+qfJt1M+AWY7jT1kJgarzxgkYNj05KTGfMKlz6N2EizEBMoOXQwVUxb4CKjRvcKuxC LTLOeAZbBr26pgyh90Fd02nbx7IbUIICrI8e02g0ZFN5//JF55D0a+8JHDskczFCghjf XlwldVfRl5YUBkrF82zUo/Vy+yd4l+ZvbDakO4y847zYoa0w4SAqxETgbWkUrCvV0pYR ReTA== X-Gm-Message-State: ACgBeo3k2ZUrhpYaAJ5+K0i9FibRD2W5lCZuUkRbNQy2Igz4exMuk4Vj BgQWgmpSc1ZuKiLQAaqlgzaNm+cUpt9hSQ== X-Google-Smtp-Source: AA6agR41J6UHl0sIqK8H+K8OpkafIJULdZHDIwvfs7zxLZ6IhK1Br7cH1odqZ8aN5KVr2d/WFRHCpQ== X-Received: by 2002:a05:6870:ea90:b0:101:fb35:883d with SMTP id s16-20020a056870ea9000b00101fb35883dmr515239oap.23.1661915347698; Tue, 30 Aug 2022 20:09:07 -0700 (PDT) Received: from sjg1.roam.corp.google.com (c-67-190-102-125.hsd1.co.comcast.net. [67.190.102.125]) by smtp.gmail.com with ESMTPSA id fp39-20020a05687065a700b00116d5103487sm9282605oab.31.2022.08.30.20.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 20:09:07 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Michal Simek , =?UTF-8?q?Marek=20Beh=C3=BAn?= , Kishon Vijay Abraham I , Tom Rini , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH 08/21] dm: core: Allow adding ofnode subnodes Date: Tue, 30 Aug 2022 21:08:04 -0600 Message-Id: <20220831030817.27804-9-sjg@chromium.org> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog In-Reply-To: <20220831030817.27804-1-sjg@chromium.org> References: <20220831030817.27804-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.6 at phobos.denx.de X-Virus-Status: Clean Add this feature to the ofnode interface, supporting both livetree and flattree. Signed-off-by: Simon Glass --- drivers/core/of_access.c | 50 ++++++++++++++++++++++++++++++++++++++++ drivers/core/ofnode.c | 30 ++++++++++++++++++++++++ include/dm/of_access.h | 12 ++++++++++ include/dm/ofnode.h | 12 ++++++++++ test/dm/ofnode.c | 17 ++++++++++++++ 5 files changed, 121 insertions(+) diff --git a/drivers/core/of_access.c b/drivers/core/of_access.c index ee09bbb7550..765b21cecdb 100644 --- a/drivers/core/of_access.c +++ b/drivers/core/of_access.c @@ -931,3 +931,53 @@ int of_write_prop(struct device_node *np, const char *propname, int len, return 0; } + +int of_add_subnode(struct device_node *parent, const char *name, int len, + struct device_node **childp) +{ + struct device_node *child, *new, *last_sibling = NULL; + char *new_name, *full_name; + int parent_fnl; + + __for_each_child_of_node(parent, child) { + if (!strncmp(child->name, name, len) && strlen(name) == len) + return -EEXIST; + last_sibling = child; + } + + /* Property does not exist -> append new property */ + new = calloc(1, sizeof(struct device_node)); + if (!new) + return -ENOMEM; + + new_name = malloc(len + 1); + if (!name) { + free(new); + return -ENOMEM; + } + strlcpy(new_name, name, len + 1); + new->name = new_name; + + parent_fnl = parent->name ? strlen(parent->full_name) : 0; + full_name = calloc(1, parent_fnl + 1 + len + 1); + if (!full_name) { + free(new_name); + free(new); + return -ENOMEM; + } + strcpy(full_name, parent->full_name); + full_name[parent_fnl] = '/'; + strlcpy(&full_name[parent_fnl + 1], name, len + 1); + new->full_name = full_name; + + /* Add as last sibling of the parent */ + if (last_sibling) + last_sibling->sibling = new; + if (!parent->child) + parent->child = new; + new->parent = parent; + + *childp = new; + + return 0; +} diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c index bf8eaf6d09a..f01bfac8f69 100644 --- a/drivers/core/ofnode.c +++ b/drivers/core/ofnode.c @@ -1227,3 +1227,33 @@ phy_interface_t ofnode_read_phy_mode(ofnode node) return PHY_INTERFACE_MODE_NA; } + +int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep) +{ + ofnode subnode; + int ret; + + assert(ofnode_valid(node)); + + if (ofnode_is_np(node)) { + struct device_node *np, *child; + + np = (struct device_node *)ofnode_to_np(node); + ret = of_add_subnode(np, name, strlen(name), &child); + if (ret) + return ret; + subnode = np_to_ofnode(child); + } else { + int offset; + + offset = fdt_add_subnode((void *)gd->fdt_blob, + ofnode_to_offset(node), name); + if (offset < 0) + return offset == -FDT_ERR_EXISTS ? -EEXIST : 0; + subnode = offset_to_ofnode(offset); + } + + *subnodep = subnode; + + return 0; +} diff --git a/include/dm/of_access.h b/include/dm/of_access.h index c0ac91a416a..20bf588be45 100644 --- a/include/dm/of_access.h +++ b/include/dm/of_access.h @@ -533,4 +533,16 @@ struct device_node *of_get_stdout(void); int of_write_prop(struct device_node *np, const char *propname, int len, const void *value); +/** + * of_add_subnode() - add a new subnode to a node + * + * @node: parent node to add to + * @name: name of subnode + * @len: length of name + * @subnodep: returns pointer to new subnode + * Returns 0 if OK, -ve on error + */ +int of_add_subnode(struct device_node *node, const char *name, int len, + struct device_node **subnodep); + #endif diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h index 018ee70c2ff..ebef8a6e2bb 100644 --- a/include/dm/ofnode.h +++ b/include/dm/ofnode.h @@ -1260,6 +1260,18 @@ static inline const char *ofnode_conf_read_str(const char *prop_name) { return NULL; } + #endif /* CONFIG_DM */ +/** + * of_add_subnode() - add a new subnode to a node + * + * @parent: parent node to add to + * @name: name of subnode + * @nodep: returns pointer to new subnode + * Returns 0 if OK, -EEXIST if already exists, -ENOMEM if out of memory, other + * -ve on other error + */ +int ofnode_add_subnode(ofnode parent, const char *name, ofnode *nodep); + #endif diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c index f80993f8927..f5aabf42dbb 100644 --- a/test/dm/ofnode.c +++ b/test/dm/ofnode.c @@ -601,3 +601,20 @@ static int dm_test_ofnode_u32(struct unit_test_state *uts) } DM_TEST(dm_test_ofnode_u32, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT); + +static int dm_test_ofnode_add_subnode(struct unit_test_state *uts) +{ + ofnode node, check, subnode; + + node = ofnode_path("/lcd"); + ut_assert(ofnode_valid(node)); + ut_assertok(ofnode_add_subnode(node, "edmund", &subnode)); + check = ofnode_path("/lcd/edmund"); + ut_asserteq(check.of_offset, subnode.of_offset); + + ut_asserteq(-EEXIST, ofnode_add_subnode(node, "edmund", &subnode)); + + return 0; +} +DM_TEST(dm_test_ofnode_add_subnode, + UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_LIVE_OR_FLAT); -- 2.37.2.672.g94769d06f0-goog