Skip to contentSkip to conversation

drivers: nvmem: Export nvmem_add_cells()

PATCH In review Andrew Lunn 2 participants 2 messages May 8, 2018 – May 11, 2018 NVMEM framework

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drivers: nvmem: Export nvmem_add_cells()

Not all platforms use device tree. It is useful to be able to add cells to a NVMEM device from code. Export nvmem_add_cells() so making this possible.

This required changing the parameters a bit, so that just the cells and the number of cells are passed, not the whole nvmem config structure.

---
v2: Add documentation for nvmem_add_cells()

---
 drivers/nvmem/core.c           | 24 +++++++++++++++++-------
 include/linux/nvmem-provider.h | 11 +++++++++++
 2 files changed, 28 insertions(+), 7 deletions(-)
drivers/nvmem/core.c +17 -7
index b05aa8e81303..b1c95ef78544 100644
@@ -353,18 +353,27 @@ static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
 	return 0;
 }
 
-static int nvmem_add_cells(struct nvmem_device *nvmem,
-			   const struct nvmem_config *cfg)
+/**
+ * nvmem_add_cells() - Add cell information to an nvmem device
+ *
+ * @nvmem: nvmem device to add cells to.
+ * @info: nvmem cell info to add to the device
+ * @ncells: number of cells in info
+ *
+ * Return: 0 or negative error code on failure.
+ */
+int nvmem_add_cells(struct nvmem_device *nvmem,
+		    const struct nvmem_cell_info *info,
+		    int ncells)
 {
 	struct nvmem_cell **cells;
-	const struct nvmem_cell_info *info = cfg->cells;
 	int i, rval;
 
-	cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL);
+	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
 	if (!cells)
 		return -ENOMEM;
 
-	for (i = 0; i < cfg->ncells; i++) {
+	for (i = 0; i < ncells; i++) {
 		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
 		if (!cells[i]) {
 			rval = -ENOMEM;
@@ -380,7 +389,7 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
 		nvmem_cell_add(cells[i]);
 	}
 
-	nvmem->ncells = cfg->ncells;
+	nvmem->ncells = ncells;
 	/* remove tmp array */
 	kfree(cells);
 
@@ -393,6 +402,7 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
 
 	return rval;
 }
+EXPORT_SYMBOL_GPL(nvmem_add_cells);
 
 /*
  * nvmem_setup_compat() - Create an additional binary entry in
@@ -509,7 +519,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	}
 
 	if (config->cells)
-		nvmem_add_cells(nvmem, config);
+		nvmem_add_cells(nvmem, config->cells, config->ncells);
 
 	return nvmem;
 
include/linux/nvmem-provider.h +11 -0
index f89598bc4e1c..24def6ad09bb 100644
@@ -77,6 +77,9 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
 
 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
 
+int nvmem_add_cells(struct nvmem_device *nvmem,
+		    const struct nvmem_cell_info *info,
+		    int ncells);
 #else
 
 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
@@ -99,6 +102,14 @@ static inline int
 devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
 {
 	return nvmem_unregister(nvmem);
+
+}
+
+static inline int nvmem_add_cells(struct nvmem_device *nvmem,
+				  const struct nvmem_cell_info *info,
+				  int ncells)
+{
+	return -ENOSYS;
 }
 
 #endif /* CONFIG_NVMEM */
Signature · 1 line
2.17.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Andrew Lunn

Not all platforms use device tree. It is useful to be able to add cells to a NVMEM device from code. Export nvmem_add_cells() so making this possible.

This required changing the parameters a bit, so that just the cells and the number of cells are passed, not the whole nvmem config structure.

Quoted 102 lines

---
v2: Add documentation for nvmem_add_cells()

---
 drivers/nvmem/core.c           | 24 +++++++++++++++++-------
 include/linux/nvmem-provider.h | 11 +++++++++++
 2 files changed, 28 insertions(+), 7 deletions(-)
drivers/nvmem/core.c +17 -7
index b05aa8e81303..b1c95ef78544 100644
@@ -353,18 +353,27 @@ static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
        return 0;
 }
 
-static int nvmem_add_cells(struct nvmem_device *nvmem,
-                          const struct nvmem_config *cfg)
+/**
+ * nvmem_add_cells() - Add cell information to an nvmem device
+ *
+ * @nvmem: nvmem device to add cells to.
+ * @info: nvmem cell info to add to the device
+ * @ncells: number of cells in info
+ *
+ * Return: 0 or negative error code on failure.
+ */
+int nvmem_add_cells(struct nvmem_device *nvmem,
+                   const struct nvmem_cell_info *info,
+                   int ncells)
 {
        struct nvmem_cell **cells;
-       const struct nvmem_cell_info *info = cfg->cells;
        int i, rval;
 
-       cells = kcalloc(cfg->ncells, sizeof(*cells), GFP_KERNEL);
+       cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
        if (!cells)
                return -ENOMEM;
 
-       for (i = 0; i < cfg->ncells; i++) {
+       for (i = 0; i < ncells; i++) {
                cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
                if (!cells[i]) {
                        rval = -ENOMEM;
@@ -380,7 +389,7 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
                nvmem_cell_add(cells[i]);
        }
 
-       nvmem->ncells = cfg->ncells;
+       nvmem->ncells = ncells;
        /* remove tmp array */
        kfree(cells);
 
@@ -393,6 +402,7 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
 
        return rval;
 }
+EXPORT_SYMBOL_GPL(nvmem_add_cells);
 
 /*
  * nvmem_setup_compat() - Create an additional binary entry in
@@ -509,7 +519,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
        }
 
        if (config->cells)
-               nvmem_add_cells(nvmem, config);
+               nvmem_add_cells(nvmem, config->cells, config->ncells);
 
        return nvmem;
 
include/linux/nvmem-provider.h +11 -1
index f89598bc4e1c..24def6ad09bb 100644
@@ -77,6 +77,9 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
 
 int devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem);
 
+int nvmem_add_cells(struct nvmem_device *nvmem,
+                   const struct nvmem_cell_info *info,
+                   int ncells);
 #else
 
 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
@@ -99,6 +102,14 @@ static inline int
 devm_nvmem_unregister(struct device *dev, struct nvmem_device *nvmem)
 {
        return nvmem_unregister(nvmem);
+
+}
+
+static inline int nvmem_add_cells(struct nvmem_device *nvmem,
+                                 const struct nvmem_cell_info *info,
+                                 int ncells)
+{
+       return -ENOSYS;
 }
 
 #endif /* CONFIG_NVMEM */
--

2.17.0