devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Auchter <michael.auchter@ni.com>
To: srinivas.kandagatla@linaro.org, pantelis.antoniou@konsulko.com,
	frowand.list@gmail.com
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	Michael Auchter <michael.auchter@ni.com>
Subject: [PATCH 2/2] nvmem: core: add OF_RECONFIG handler for nvmem cells
Date: Wed, 14 Oct 2020 16:41:28 -0500	[thread overview]
Message-ID: <20201014214128.1091738-3-michael.auchter@ni.com> (raw)
In-Reply-To: <20201014214128.1091738-1-michael.auchter@ni.com>

CONFIG_OF_DYNAMIC allows runtime changes to the device tree. This patch
adds a OF_RECONFIG handler to receive notifications on tree changes to
support adding/removing cells from an nvmem device based on these
changes.

Signed-off-by: Michael Auchter <michael.auchter@ni.com>
---
 drivers/nvmem/core.c | 61 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 91979529cb07..859431c15d5b 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1629,6 +1629,53 @@ void nvmem_del_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries)
 }
 EXPORT_SYMBOL_GPL(nvmem_del_cell_lookups);
 
+#if IS_ENABLED(CONFIG_OF_DYNAMIC)
+static int of_nvmem_notify(struct notifier_block *nb, unsigned long action,
+			   void *arg)
+{
+
+	struct of_reconfig_data *rd = arg;
+	struct nvmem_device *nvmem;
+	struct nvmem_cell *cell;
+	int rval;
+
+	switch (of_reconfig_get_state_change(action, rd)) {
+	case OF_RECONFIG_CHANGE_ADD:
+		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED))
+			return NOTIFY_OK;
+
+		nvmem = __nvmem_device_get(rd->dn->parent, device_match_of_node);
+		if (IS_ERR(nvmem))
+			return NOTIFY_OK;
+
+		rval = nvmem_add_cell_from_of(nvmem, rd->dn);
+		return notifier_from_errno(rval);
+		break;
+	case OF_RECONFIG_CHANGE_REMOVE:
+		if (!of_node_check_flag(rd->dn, OF_POPULATED))
+			return NOTIFY_OK;
+
+		nvmem = __nvmem_device_get(rd->dn->parent, device_match_of_node);
+		if (IS_ERR(nvmem))
+			return NOTIFY_OK;
+
+		cell = nvmem_find_cell_by_node(nvmem, rd->dn);
+		if (!cell)
+			return NOTIFY_OK;
+
+		nvmem_cell_drop(cell);
+		of_node_clear_flag(rd->dn, OF_POPULATED);
+		break;
+	}
+
+	return NOTIFY_OK;
+}
+
+struct notifier_block nvmem_of_notifier = {
+	.notifier_call = of_nvmem_notify,
+};
+#endif /* CONFIG_OF_DYNAMIC */
+
 /**
  * nvmem_dev_name() - Get the name of a given nvmem device.
  *
@@ -1644,11 +1691,23 @@ EXPORT_SYMBOL_GPL(nvmem_dev_name);
 
 static int __init nvmem_init(void)
 {
-	return bus_register(&nvmem_bus_type);
+	int rval;
+
+	rval = bus_register(&nvmem_bus_type);
+	if (rval)
+		return rval;
+
+	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
+		WARN_ON(of_reconfig_notifier_register(&nvmem_of_notifier));
+
+	return 0;
 }
 
 static void __exit nvmem_exit(void)
 {
+	if (IS_ENABLED(CONFIG_OF_DYNAMIC))
+		WARN_ON(of_reconfig_notifier_unregister(&nvmem_of_notifier));
+
 	bus_unregister(&nvmem_bus_type);
 }
 
-- 
2.25.4


  parent reply	other threads:[~2020-10-14 21:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-14 21:41 [PATCH 0/2] nvmem: add DT overlay support for cells Michael Auchter
2020-10-14 21:41 ` [PATCH 1/2] nvmem: core: extract function to add cell from OF Michael Auchter
2020-10-14 21:41 ` Michael Auchter [this message]
2020-10-15  0:13   ` [PATCH 2/2] nvmem: core: add OF_RECONFIG handler for nvmem cells kernel test robot
2020-10-15  0:13   ` [RFC PATCH] nvmem: core: nvmem_of_notifier can be static kernel test robot
2020-10-15  2:35   ` [PATCH 2/2] nvmem: core: add OF_RECONFIG handler for nvmem cells kernel test robot

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=20201014214128.1091738-3-michael.auchter@ni.com \
    --to=michael.auchter@ni.com \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pantelis.antoniou@konsulko.com \
    --cc=srinivas.kandagatla@linaro.org \
    /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).