[PATCH 5/5] nvmem: core: add cell name based matching of DT cell nodes

From: Rafał Miłecki
Date: Thu Dec 23 2021 - 06:08:19 EST


From: Rafał Miłecki <rafal@xxxxxxxxxx>

When adding NVMEM cells defined by driver it's important to match them
with DT nodes that specify matching names. That way other bindings &
drivers can reference such "dynamic" NVMEM cells.

Signed-off-by: Rafał Miłecki <rafal@xxxxxxxxxx>
---
drivers/nvmem/core.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 45c39ac401bd..5fe92751645a 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -499,6 +499,33 @@ static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
return 0;
}

+/**
+ * nvmem_find_cell_of_node() - Find DT node matching nvmem cell
+ *
+ * @nvmem: nvmem device to add cells to.
+ * @name: nvmem cell name
+ *
+ * Runtime created nvmem cells (those not coming from DT) may still need to be
+ * referenced in DT. This function allows finding DT node referencing nvmem cell
+ * by its name. Such a DT node can be used by nvmem consumers.
+ *
+ * Return: NULL or pointer to DT node
+ */
+static struct device_node *nvmem_find_cell_of_node(struct nvmem_device *nvmem,
+ const char *name)
+{
+ struct device_node *child;
+ const char *label;
+
+ for_each_child_of_node(nvmem->dev.of_node, child) {
+ if (!of_property_read_string(child, "label", &label) &&
+ !strcmp(label, name))
+ return child;
+ }
+
+ return NULL;
+}
+
/**
* nvmem_add_cells() - Add cell information to an nvmem device
*
@@ -532,6 +559,8 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
goto err;
}

+ cells[i]->np = nvmem_find_cell_of_node(nvmem, cells[i]->name);
+
nvmem_cell_entry_add(cells[i]);
}

--
2.31.1