Some firmware-described consumers do not have a corresponding
driver-model device. They still need to resolve named NVMEM cells from
their own device-tree node.

Factor the existing index and name lookups through node-based
helpers, while retaining the device-based interfaces as wrappers.

Signed-off-by: James Hilliard <[email protected]>
---
 drivers/misc/nvmem.c | 36 ++++++++++++++++++++++++------------
 include/nvmem.h      | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/drivers/misc/nvmem.c b/drivers/misc/nvmem.c
index 33e80858565..736b59bffec 100644
--- a/drivers/misc/nvmem.c
+++ b/drivers/misc/nvmem.c
@@ -95,8 +95,8 @@ static int nvmem_get_device(ofnode node, struct nvmem_cell 
*cell)
        return -ENODEV;
 }
 
-int nvmem_cell_get_by_index(struct udevice *dev, int index,
-                           struct nvmem_cell *cell)
+int nvmem_cell_get_by_index_nodev(ofnode node, int index,
+                                 struct nvmem_cell *cell)
 {
        fdt_addr_t offset;
        fdt_size_t size = FDT_SIZE_T_NONE;
@@ -104,10 +104,8 @@ int nvmem_cell_get_by_index(struct udevice *dev, int index,
        struct ofnode_phandle_args args;
        ofnode par;
 
-       dev_dbg(dev, "%s: index=%d\n", __func__, index);
-
-       ret = dev_read_phandle_with_args(dev, "nvmem-cells", NULL, 0, index,
-                                        &args);
+       ret = ofnode_parse_phandle_with_args(node, "nvmem-cells", NULL, 0,
+                                            index, &args);
        if (ret)
                return ret;
 
@@ -131,16 +129,30 @@ int nvmem_cell_get_by_index(struct udevice *dev, int 
index,
        return 0;
 }
 
-int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
-                          struct nvmem_cell *cell)
+int nvmem_cell_get_by_index(struct udevice *dev, int index,
+                           struct nvmem_cell *cell)
 {
-       int index;
+       dev_dbg(dev, "%s: index=%d\n", __func__, index);
 
-       dev_dbg(dev, "%s, name=%s\n", __func__, name);
+       return nvmem_cell_get_by_index_nodev(dev_ofnode(dev), index, cell);
+}
+
+int nvmem_cell_get_by_name_nodev(ofnode node, const char *name,
+                                struct nvmem_cell *cell)
+{
+       int index;
 
-       index = dev_read_stringlist_search(dev, "nvmem-cell-names", name);
+       index = ofnode_stringlist_search(node, "nvmem-cell-names", name);
        if (index < 0)
                return index;
 
-       return nvmem_cell_get_by_index(dev, index, cell);
+       return nvmem_cell_get_by_index_nodev(node, index, cell);
+}
+
+int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
+                          struct nvmem_cell *cell)
+{
+       dev_dbg(dev, "%s, name=%s\n", __func__, name);
+
+       return nvmem_cell_get_by_name_nodev(dev_ofnode(dev), name, cell);
 }
diff --git a/include/nvmem.h b/include/nvmem.h
index e6a8a98828b..94205fc2763 100644
--- a/include/nvmem.h
+++ b/include/nvmem.h
@@ -6,6 +6,7 @@
 #ifndef NVMEM_H
 #define NVMEM_H
 
+#include <dm/ofnode.h>
 #include <linux/errno.h>
 
 /**
@@ -85,6 +86,20 @@ int nvmem_cell_write(struct nvmem_cell *cell, const void 
*buf, size_t size);
 int nvmem_cell_get_by_index(struct udevice *dev, int index,
                            struct nvmem_cell *cell);
 
+/**
+ * nvmem_cell_get_by_index_nodev() - Get an NVMEM cell from an ofnode
+ * @node: Device-tree node that uses the NVMEM cell
+ * @index: Index of the cell in nvmem-cells
+ * @cell: Cell to initialize
+ *
+ * This is the node-based form of nvmem_cell_get_by_index(), for consumers
+ * which do not have a corresponding driver-model device.
+ *
+ * Return: 0 on success or a negative error code on failure
+ */
+int nvmem_cell_get_by_index_nodev(ofnode node, int index,
+                                 struct nvmem_cell *cell);
+
 /**
  * nvmem_cell_get_by_name() - Get an nvmem cell from a given device and name
  * @dev: The device that uses the nvmem cell
@@ -106,6 +121,20 @@ int nvmem_cell_get_by_index(struct udevice *dev, int index,
 int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
                           struct nvmem_cell *cell);
 
+/**
+ * nvmem_cell_get_by_name_nodev() - Get a named NVMEM cell from an ofnode
+ * @node: Device-tree node that uses the NVMEM cell
+ * @name: Name in the nvmem-cell-names property
+ * @cell: Cell to initialize
+ *
+ * This is the node-based form of nvmem_cell_get_by_name(), for consumers
+ * which do not have a corresponding driver-model device.
+ *
+ * Return: 0 on success or a negative error code on failure
+ */
+int nvmem_cell_get_by_name_nodev(ofnode node, const char *name,
+                                struct nvmem_cell *cell);
+
 #else /* CONFIG_NVMEM */
 
 static inline int nvmem_cell_read(struct nvmem_cell *cell, void *buf, int size)
@@ -125,12 +154,24 @@ static inline int nvmem_cell_get_by_index(struct udevice 
*dev, int index,
        return -ENOSYS;
 }
 
+static inline int nvmem_cell_get_by_index_nodev(ofnode node, int index,
+                                               struct nvmem_cell *cell)
+{
+       return -ENOSYS;
+}
+
 static inline int nvmem_cell_get_by_name(struct udevice *dev, const char *name,
                                         struct nvmem_cell *cell)
 {
        return -ENOSYS;
 }
 
+static inline int nvmem_cell_get_by_name_nodev(ofnode node, const char *name,
+                                              struct nvmem_cell *cell)
+{
+       return -ENOSYS;
+}
+
 #endif /* CONFIG_NVMEM */
 
 #endif /* NVMEM_H */

-- 
2.53.0

Reply via email to