devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lizhi Hou <lizhi.hou@amd.com>
To: <linux-pci@vger.kernel.org>, <devicetree@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <robh@kernel.org>,
	<frowand.list@gmail.com>, <helgaas@kernel.org>
Cc: Lizhi Hou <lizhi.hou@amd.com>, <clement.leger@bootlin.com>,
	<max.zhen@amd.com>, <sonal.santan@amd.com>, <larry.liu@amd.com>,
	<brian.xu@amd.com>, <stefano.stabellini@xilinx.com>,
	<trix@redhat.com>
Subject: [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node()
Date: Wed, 12 Oct 2022 11:13:58 -0700	[thread overview]
Message-ID: <1665598440-47410-2-git-send-email-lizhi.hou@amd.com> (raw)
In-Reply-To: <1665598440-47410-1-git-send-email-lizhi.hou@amd.com>

of_create_node() creates device node and apply to base tree dynamically.
The parent device node and full name are required for creating the node.
And the caller can also provide a property array for the node.

Inside this function, it creates a changeset. Then the new device node
and properties are added to the changeset and applied to base tree. The
pointer of this changeset is saved in device node private data.

of_destroy_node() removes the node created by of_create_node() from the
base tree and free it. It gets the changeset pointer from device node
private data and call of_changeset_destroy() to free everything.

Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
Signed-off-by: Sonal Santan <sonal.santan@amd.com>
Signed-off-by: Max Zhen <max.zhen@amd.com>
Signed-off-by: Brian Xu <brian.xu@amd.com>
---
 drivers/of/dynamic.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of.h   |  4 +++
 2 files changed, 84 insertions(+)

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index cd3821a6444f..eca28b723706 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -934,3 +934,83 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action,
 	return 0;
 }
 EXPORT_SYMBOL_GPL(of_changeset_action);
+
+/**
+ * of_create_node - Dynamically create a device node and apply it to base tree
+ *
+ * @parent: Pointer to parent device node
+ * @full_name: Full name of device node
+ * @props: Pointer to property array
+ *
+ * Return: Pointer to the created device node or NULL in case of an error.
+ */
+struct device_node *of_create_node(struct device_node *parent,
+				   const char *full_name,
+				   struct property *props)
+{
+	struct of_changeset *cset;
+	struct property *new_pp;
+	struct device_node *np;
+	int ret, i;
+
+	cset = kzalloc(sizeof(*cset), GFP_KERNEL);
+	if (!cset)
+		return NULL;
+
+	of_changeset_init(cset);
+
+	np = __of_node_dup(NULL, full_name);
+	if (!np)
+		goto failed;
+	np->parent = parent;
+
+	ret = of_changeset_attach_node(cset, np);
+	if (ret)
+		goto failed;
+
+	if (props) {
+		for (i = 0; props[i].name; i++) {
+			new_pp = __of_prop_dup(&props[i], GFP_KERNEL);
+			if (!new_pp)
+				goto failed;
+			ret = of_changeset_add_property(cset, np, new_pp);
+			if (ret) {
+				kfree(new_pp->name);
+				kfree(new_pp->value);
+				kfree(new_pp);
+				goto failed;
+			}
+		}
+	}
+
+	ret = of_changeset_apply(cset);
+	if (ret)
+		goto failed;
+
+	np->data = cset;
+
+	return np;
+
+failed:
+	of_changeset_destroy(cset);
+	if (np)
+		of_node_put(np);
+
+	return NULL;
+}
+
+/**
+ * of_destroy_node - Destroy a dynamically created device node
+ *
+ * @np: Pointer to dynamically created device node
+ *
+ */
+void of_destroy_node(struct device_node *np)
+{
+	struct of_changeset *cset;
+
+	cset = (struct of_changeset *)np->data;
+	of_changeset_destroy(cset);
+	of_node_put(np);
+	kfree(cset);
+}
diff --git a/include/linux/of.h b/include/linux/of.h
index 766d002bddb9..493ef957c1a8 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -1475,6 +1475,10 @@ extern int of_changeset_revert(struct of_changeset *ocs);
 extern int of_changeset_action(struct of_changeset *ocs,
 		unsigned long action, struct device_node *np,
 		struct property *prop);
+struct device_node *of_create_node(struct device_node *parent,
+				   const char *full_name,
+				   struct property *props);
+void of_destroy_node(struct device_node *np);
 
 static inline int of_changeset_attach_node(struct of_changeset *ocs,
 		struct device_node *np)
-- 
2.17.1


  reply	other threads:[~2022-10-12 18:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-12 18:13 [PATCH RFC V2 0/3] Generate device tree node for pci devices Lizhi Hou
2022-10-12 18:13 ` Lizhi Hou [this message]
2022-11-01  7:47   ` [PATCH RFC V2 1/3] of: dynamic: Add of_create_node() and of_destroy_node() Christophe JAILLET
2022-11-01 18:16     ` Lizhi Hou
2022-10-12 18:13 ` [PATCH RFC V2 2/3] PCI: Create device tree node for selected devices Lizhi Hou
2022-10-12 18:14 ` [PATCH RFC V2 3/3] PCI: Add basic properties for dynamically generated PCI OF node Lizhi Hou
2022-10-26 19:39   ` Rob Herring
2022-10-31 23:21     ` Lizhi Hou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1665598440-47410-2-git-send-email-lizhi.hou@amd.com \
    --to=lizhi.hou@amd.com \
    --cc=brian.xu@amd.com \
    --cc=clement.leger@bootlin.com \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=helgaas@kernel.org \
    --cc=larry.liu@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=max.zhen@amd.com \
    --cc=robh@kernel.org \
    --cc=sonal.santan@amd.com \
    --cc=stefano.stabellini@xilinx.com \
    --cc=trix@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).