mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	 BAREBOX <barebox@lists.infradead.org>
Cc: Marco Felsch <m.felsch@pengutronix.de>
Subject: [PATCH v2 11/15] nvmem: core: add an index parameter to the cell
Date: Mon, 18 Aug 2025 19:44:45 +0200	[thread overview]
Message-ID: <20250818-v2025-06-0-topic-nvmem-v2-11-b6d677f6c519@pengutronix.de> (raw)
In-Reply-To: <20250818-v2025-06-0-topic-nvmem-v2-0-b6d677f6c519@pengutronix.de>

To further  sync the nvmem code base port Linux commit:

| commit 5d8e6e6c10a3d37486d263b16ddc15991a7e4a88
| Author: Michael Walle <michael@walle.cc>
| Date:   Mon Feb 6 13:43:46 2023 +0000
|
|     nvmem: core: add an index parameter to the cell
|
|     Sometimes a cell can represend multiple values. For example, a base
|     ethernet address stored in the NVMEM can be expanded into multiple
|     discreet ones by adding an offset.
|
|     For this use case, introduce an index parameter which is then used to
|     distiguish between values. This parameter will then be passed to the
|     post process hook which can then use it to create different values
|     during reading.
|
|     At the moment, there is only support for the device tree path. You can
|     add the index to the phandle, e.g.
|
|       &net {
|               nvmem-cells = <&base_mac_address 2>;
|               nvmem-cell-names = "mac-address";
|       };
|
|       &nvmem_provider {
|               base_mac_address: base-mac-address@0 {
|                       #nvmem-cell-cells = <1>;
|                       reg = <0 6>;
|               };
|       };
|
|     Signed-off-by: Michael Walle <michael@walle.cc>
|     Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
|     Link: https://lore.kernel.org/r/20230206134356.839737-13-srinivas.kandagatla@linaro.org
|     Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
 drivers/nvmem/core.c           | 33 ++++++++++++++++++++++++---------
 drivers/nvmem/imx-ocotp-ele.c  |  4 ++--
 drivers/nvmem/ocotp.c          |  4 ++--
 include/linux/nvmem-provider.h |  2 +-
 4 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index b87dd9375c6f0a2b446e4f1c2e51c6bf042e550d..8973af5fba2d7c3ebb69eecd9f92c90b7543c3ff 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -32,6 +32,7 @@ struct nvmem_cell_entry {
 struct nvmem_cell {
 	struct nvmem_cell_entry *entry;
 	const char		*id;
+	int			index;
 };
 
 static LIST_HEAD(nvmem_devs);
@@ -632,7 +633,8 @@ void nvmem_device_put(struct nvmem_device *nvmem)
 }
 EXPORT_SYMBOL_GPL(nvmem_device_put);
 
-static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, const char *id)
+static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
+					    const char *id, int index)
 {
 	struct nvmem_cell *cell;
 	const char *name = NULL;
@@ -651,6 +653,7 @@ static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, cons
 
 	cell->id = name;
 	cell->entry = entry;
+	cell->index = index;
 
 	return cell;
 }
@@ -700,15 +703,27 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
 	struct nvmem_device *nvmem;
 	struct nvmem_cell_entry *cell_entry;
 	struct nvmem_cell *cell;
+	struct of_phandle_args cell_spec;
 	int index = 0;
+	int cell_index = 0;
+	int ret;
 
 	/* if cell name exists, find index to the name */
 	if (id)
 		index = of_property_match_string(np, "nvmem-cell-names", id);
 
-	cell_np = of_parse_phandle(np, "nvmem-cells", index);
-	if (!cell_np)
-		return ERR_PTR(-ENOENT);
+	ret = of_parse_phandle_with_optional_args(np, "nvmem-cells",
+						  "#nvmem-cell-cells",
+						  index, &cell_spec);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (cell_spec.args_count > 1)
+		return ERR_PTR(-EINVAL);
+
+	cell_np = cell_spec.np;
+	if (cell_spec.args_count)
+		cell_index = cell_spec.args[0];
 
 	nvmem_np = of_get_parent(cell_np);
 	if (!nvmem_np) {
@@ -746,7 +761,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
 		return ERR_PTR(-ENOENT);
 	}
 
-	cell = nvmem_create_cell(cell_entry, id);
+	cell = nvmem_create_cell(cell_entry, id, cell_index);
 	if (IS_ERR(cell))
 		__nvmem_device_put(nvmem);
 
@@ -832,7 +847,7 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
 
 static int __nvmem_cell_read(struct nvmem_device *nvmem,
 			     struct nvmem_cell_entry *cell,
-			     void *buf, size_t *len, const char *id)
+			     void *buf, size_t *len, const char *id, int index)
 {
 	int rc;
 
@@ -845,7 +860,7 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 		nvmem_shift_read_buffer_in_place(cell, buf);
 
 	if (nvmem->cell_post_process) {
-		rc = nvmem->cell_post_process(nvmem->priv, id,
+		rc = nvmem->cell_post_process(nvmem->priv, id, index,
 					      cell->offset, buf, cell->bytes);
 		if (rc)
 			return rc;
@@ -881,7 +896,7 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id);
+	rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id, cell->index);
 	if (rc) {
 		kfree(buf);
 		return ERR_PTR(rc);
@@ -1009,7 +1024,7 @@ ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
 	if (rc)
 		return rc;
 
-	rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL);
+	rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL, 0);
 	if (rc)
 		return rc;
 
diff --git a/drivers/nvmem/imx-ocotp-ele.c b/drivers/nvmem/imx-ocotp-ele.c
index 5fb5dfc87e221bc244f52a1fa3bf2d1342b606d5..8ac4e2a9a6c87f128ff9aaf16d1238ef129d91ca 100644
--- a/drivers/nvmem/imx-ocotp-ele.c
+++ b/drivers/nvmem/imx-ocotp-ele.c
@@ -109,8 +109,8 @@ static int imx_ocotp_reg_write(void *context, unsigned int offset, unsigned int
 	return ret;
 }
 
-static int imx_ocotp_cell_pp(void *context, const char *id, unsigned int offset,
-			     void *data, size_t bytes)
+static int imx_ocotp_cell_pp(void *context, const char *id, int index,
+			     unsigned int offset, void *data, size_t bytes)
 {
 	/* Deal with some post processing of nvmem cell data */
 	if (id && !strcmp(id, "mac-address")) {
diff --git a/drivers/nvmem/ocotp.c b/drivers/nvmem/ocotp.c
index 5d47377678665b7d5d48b58cb297fe784d27438c..d4510d4b89ba404c0b0537afc01075cac501042c 100644
--- a/drivers/nvmem/ocotp.c
+++ b/drivers/nvmem/ocotp.c
@@ -788,8 +788,8 @@ static struct regmap_bus imx_ocotp_regmap_bus = {
 	.reg_read = imx_ocotp_reg_read,
 };
 
-static int imx_ocotp_cell_pp(void *context, const char *id, unsigned int offset,
-			     void *data, size_t bytes)
+static int imx_ocotp_cell_pp(void *context, const char *id, int index,
+			     unsigned int offset, void *data, size_t bytes)
 {
 	/* Deal with some post processing of nvmem cell data */
 	if (id && !strcmp(id, "mac-address")) {
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 346355ad988470ce27e78a3a52e4dfcb437e2f18..e08c6a62590312376934aa58229beb39840efdf7 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -22,7 +22,7 @@ typedef int (*nvmem_reg_read_t)(void *priv, unsigned int offset,
 typedef int (*nvmem_reg_write_t)(void *priv, unsigned int offset,
 				 const void *val, size_t bytes);
 /* used for vendor specific post processing of cell data */
-typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id,
+typedef int (*nvmem_cell_post_process_t)(void *priv, const char *id, int index,
 					 unsigned int offset, void *buf,
 					 size_t bytes);
 

-- 
2.39.5




  parent reply	other threads:[~2025-08-18 18:31 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-18 17:44 [PATCH v2 00/15] NVMEM: Add support for layout drivers Marco Felsch
2025-08-18 17:44 ` [PATCH v2 01/15] of: sync of_*_phandle_with_args with Linux Marco Felsch
2025-08-18 17:44 ` [PATCH v2 02/15] of: base: add of_parse_phandle_with_optional_args() Marco Felsch
2025-08-18 17:44 ` [PATCH v2 03/15] of: device: Export of_device_make_bus_id() Marco Felsch
2025-08-18 17:44 ` [PATCH v2 04/15] nvmem: core: fix nvmem_register error path Marco Felsch
2025-08-18 17:44 ` [PATCH v2 05/15] nvmem: core: sync with Linux Marco Felsch
2025-08-20  7:32   ` Sascha Hauer
2025-08-20  9:09     ` Marco Felsch
2025-08-18 17:44 ` [PATCH v2 06/15] nvmem: core: expose nvmem cells as cdev Marco Felsch
2025-08-20  7:33   ` Sascha Hauer
2025-08-20  9:09     ` Marco Felsch
2025-08-18 17:44 ` [PATCH v2 07/15] nvmem: core: allow single and dynamic device ids Marco Felsch
2025-08-20  7:47   ` Sascha Hauer
2025-08-20  9:13     ` Marco Felsch
2025-08-18 17:44 ` [PATCH v2 08/15] eeprom: at24: fix device name handling Marco Felsch
2025-08-18 17:44 ` [PATCH v2 09/15] nvmem: core: create a header for internal sharing Marco Felsch
2025-08-18 17:44 ` [PATCH v2 10/15] nvmem: core: add nvmem-layout support Marco Felsch
2025-08-19 12:46   ` Sascha Hauer
2025-08-19 14:39     ` Marco Felsch
2025-08-20  7:41   ` Sascha Hauer
2025-08-20  9:11     ` Marco Felsch
2025-08-20  9:40       ` Sascha Hauer
2025-08-20  9:55         ` Marco Felsch
2025-08-18 17:44 ` Marco Felsch [this message]
2025-08-18 17:44 ` [PATCH v2 12/15] nvmem: core: add per-cell post processing Marco Felsch
2025-08-18 17:44 ` [PATCH v2 13/15] nvmem: core: add cell based fixup logic Marco Felsch
2025-08-18 17:44 ` [PATCH v2 14/15] nvmem: core: provide own priv pointer in post process callback Marco Felsch
2025-08-18 17:44 ` [PATCH v2 15/15] nvmem: core: drop global cell_post_process Marco Felsch
2025-08-19  7:23 ` [PATCH v2 00/15] NVMEM: Add support for layout drivers Sascha Hauer

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=20250818-v2025-06-0-topic-nvmem-v2-11-b6d677f6c519@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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