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 1/2] nvmem: core: extract function to add cell from OF
Date: Wed, 14 Oct 2020 16:41:27 -0500	[thread overview]
Message-ID: <20201014214128.1091738-2-michael.auchter@ni.com> (raw)
In-Reply-To: <20201014214128.1091738-1-michael.auchter@ni.com>

Extract the logic needed to add a single cell described by a device tree
node from nvmem_add_cells_from_of, and call this new function for each
child node.

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

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 6cd3edb2eaf6..91979529cb07 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -518,54 +518,66 @@ nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
 	return cell;
 }
 
-static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
+static int nvmem_add_cell_from_of(struct nvmem_device *nvmem,
+				  struct device_node *child)
 {
-	struct device_node *parent, *child;
 	struct device *dev = &nvmem->dev;
 	struct nvmem_cell *cell;
 	const __be32 *addr;
 	int len;
 
-	parent = dev->of_node;
+	addr = of_get_property(child, "reg", &len);
+	if (!addr || (len < 2 * sizeof(u32))) {
+		dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
+		return -EINVAL;
+	}
 
-	for_each_child_of_node(parent, child) {
-		addr = of_get_property(child, "reg", &len);
-		if (!addr || (len < 2 * sizeof(u32))) {
-			dev_err(dev, "nvmem: invalid reg on %pOF\n", child);
-			return -EINVAL;
-		}
+	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
+	if (!cell)
+		return -ENOMEM;
 
-		cell = kzalloc(sizeof(*cell), GFP_KERNEL);
-		if (!cell)
-			return -ENOMEM;
+	cell->nvmem = nvmem;
+	cell->np = of_node_get(child);
+	cell->offset = be32_to_cpup(addr++);
+	cell->bytes = be32_to_cpup(addr);
+	cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
+
+	addr = of_get_property(child, "bits", &len);
+	if (addr && len == (2 * sizeof(u32))) {
+		cell->bit_offset = be32_to_cpup(addr++);
+		cell->nbits = be32_to_cpup(addr);
+	}
 
-		cell->nvmem = nvmem;
-		cell->np = of_node_get(child);
-		cell->offset = be32_to_cpup(addr++);
-		cell->bytes = be32_to_cpup(addr);
-		cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
+	if (cell->nbits)
+		cell->bytes = DIV_ROUND_UP(
+				cell->nbits + cell->bit_offset,
+				BITS_PER_BYTE);
 
-		addr = of_get_property(child, "bits", &len);
-		if (addr && len == (2 * sizeof(u32))) {
-			cell->bit_offset = be32_to_cpup(addr++);
-			cell->nbits = be32_to_cpup(addr);
-		}
+	if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
+		dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
+			cell->name, nvmem->stride);
+		/* Cells already added will be freed later. */
+		kfree_const(cell->name);
+		kfree(cell);
+		return -EINVAL;
+	}
 
-		if (cell->nbits)
-			cell->bytes = DIV_ROUND_UP(
-					cell->nbits + cell->bit_offset,
-					BITS_PER_BYTE);
-
-		if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
-			dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
-				cell->name, nvmem->stride);
-			/* Cells already added will be freed later. */
-			kfree_const(cell->name);
-			kfree(cell);
-			return -EINVAL;
-		}
+	nvmem_cell_add(cell);
 
-		nvmem_cell_add(cell);
+	return 0;
+}
+
+static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
+{
+	struct device_node *parent, *child;
+	int rval;
+
+	parent = nvmem->dev.of_node;
+
+	for_each_child_of_node(parent, child) {
+		rval = nvmem_add_cell_from_of(nvmem, child);
+		if (rval)
+			return rval;
 	}
 
 	return 0;
-- 
2.25.4


  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 ` Michael Auchter [this message]
2020-10-14 21:41 ` [PATCH 2/2] nvmem: core: add OF_RECONFIG handler for nvmem cells Michael Auchter
2020-10-15  0:13   ` 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-2-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).