diff mbox series

[v2,06/20] nvmem: core: add nvmem_add_one_cell()

Message ID 20220901221857.2600340-7-michael@walle.cc (mailing list archive)
State New, archived
Headers show
Series nvmem: core: introduce NVMEM layouts | expand

Commit Message

Michael Walle Sept. 1, 2022, 10:18 p.m. UTC
Add a new function to add exactly one cell. This will be used by the
nvmem layout drivers to add custom cells. In contrast to the
nvmem_add_cells(), this has the advantage that we don't have to assemble
a list of cells on runtime.

Signed-off-by: Michael Walle <michael@walle.cc>
---
changes since v1:
 - none

 drivers/nvmem/core.c           | 58 ++++++++++++++++++++--------------
 include/linux/nvmem-provider.h |  8 +++++
 2 files changed, 42 insertions(+), 24 deletions(-)

Comments

Srinivas Kandagatla Sept. 9, 2022, 8:52 a.m. UTC | #1
On 01/09/2022 23:18, Michael Walle wrote:
> Add a new function to add exactly one cell. This will be used by the
> nvmem layout drivers to add custom cells. In contrast to the
> nvmem_add_cells(), this has the advantage that we don't have to assemble
> a list of cells on runtime.
> 
> Signed-off-by: Michael Walle <michael@walle.cc>
> ---
> changes since v1:
>   - none
> 
>   drivers/nvmem/core.c           | 58 ++++++++++++++++++++--------------
>   include/linux/nvmem-provider.h |  8 +++++
>   2 files changed, 42 insertions(+), 24 deletions(-)
> 
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index be38e62fd190..3dfd149374a8 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
> @@ -501,6 +501,35 @@ static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
>   	return 0;
>   }
>   
> +/**
> + * nvmem_add_one_cell() - Add one cell information to an nvmem device
> + *
> + * @nvmem: nvmem device to add cells to.
> + * @info: nvmem cell info to add to the device
> + *
> + * Return: 0 or negative error code on failure.
> + */
> +int nvmem_add_one_cell(struct nvmem_device *nvmem,
> +		       const struct nvmem_cell_info *info)
> +{
> +	struct nvmem_cell_entry *cell;
> +	int rval;
> +
> +	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
> +	if (!cell)
> +		return -ENOMEM;
> +
> +	rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
> +	if (rval) {
> +		kfree(cell);
> +		return rval;
> +	}
> +
> +	nvmem_cell_entry_add(cell);
> +
> +	return 0;
> +}
> +

EXPORT_SYMBOL_GPL ???

>   /**
>    * nvmem_add_cells() - Add cell information to an nvmem device
>    *
> @@ -514,34 +543,15 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
>   		    const struct nvmem_cell_info *info,
>   		    int ncells)
>   {
> -	struct nvmem_cell_entry **cells;
> -	int i, rval = 0;
> -
> -	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
> -	if (!cells)
> -		return -ENOMEM;
> +	int i, rval;
>   
>   	for (i = 0; i < ncells; i++) {
> -		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
> -		if (!cells[i]) {
> -			rval = -ENOMEM;
> -			goto out;
> -		}
> -
> -		rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
> -		if (rval) {
> -			kfree(cells[i]);
> -			goto out;
> -		}
> -
> -		nvmem_cell_entry_add(cells[i]);
> +		rval = nvmem_add_one_cell(nvmem, &info[i]);
> +		if (rval)
> +			return rval;
>   	}
>   
> -out:
> -	/* remove tmp array */
> -	kfree(cells);
> -
> -	return rval;
> +	return 0;
>   }
>   
>   /**
> diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
> index 14a32a1bc249..385d29168008 100644
> --- a/include/linux/nvmem-provider.h
> +++ b/include/linux/nvmem-provider.h
> @@ -155,6 +155,9 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
>   void nvmem_add_cell_table(struct nvmem_cell_table *table);
>   void nvmem_del_cell_table(struct nvmem_cell_table *table);
>   
> +int nvmem_add_one_cell(struct nvmem_device *nvmem,
> +		       const struct nvmem_cell_info *info);
> +
>   #else
>   
>   static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
> @@ -172,6 +175,11 @@ devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
>   
>   static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
>   static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
> +static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
> +				     const struct nvmem_cell_info *info)
> +{
> +	return -EOPNOTSUPP;
> +}
>   
>   #endif /* CONFIG_NVMEM */
>   #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
diff mbox series

Patch

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index be38e62fd190..3dfd149374a8 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -501,6 +501,35 @@  static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
 	return 0;
 }
 
+/**
+ * nvmem_add_one_cell() - Add one cell information to an nvmem device
+ *
+ * @nvmem: nvmem device to add cells to.
+ * @info: nvmem cell info to add to the device
+ *
+ * Return: 0 or negative error code on failure.
+ */
+int nvmem_add_one_cell(struct nvmem_device *nvmem,
+		       const struct nvmem_cell_info *info)
+{
+	struct nvmem_cell_entry *cell;
+	int rval;
+
+	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
+	if (!cell)
+		return -ENOMEM;
+
+	rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
+	if (rval) {
+		kfree(cell);
+		return rval;
+	}
+
+	nvmem_cell_entry_add(cell);
+
+	return 0;
+}
+
 /**
  * nvmem_add_cells() - Add cell information to an nvmem device
  *
@@ -514,34 +543,15 @@  static int nvmem_add_cells(struct nvmem_device *nvmem,
 		    const struct nvmem_cell_info *info,
 		    int ncells)
 {
-	struct nvmem_cell_entry **cells;
-	int i, rval = 0;
-
-	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
-	if (!cells)
-		return -ENOMEM;
+	int i, rval;
 
 	for (i = 0; i < ncells; i++) {
-		cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
-		if (!cells[i]) {
-			rval = -ENOMEM;
-			goto out;
-		}
-
-		rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
-		if (rval) {
-			kfree(cells[i]);
-			goto out;
-		}
-
-		nvmem_cell_entry_add(cells[i]);
+		rval = nvmem_add_one_cell(nvmem, &info[i]);
+		if (rval)
+			return rval;
 	}
 
-out:
-	/* remove tmp array */
-	kfree(cells);
-
-	return rval;
+	return 0;
 }
 
 /**
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 14a32a1bc249..385d29168008 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -155,6 +155,9 @@  struct nvmem_device *devm_nvmem_register(struct device *dev,
 void nvmem_add_cell_table(struct nvmem_cell_table *table);
 void nvmem_del_cell_table(struct nvmem_cell_table *table);
 
+int nvmem_add_one_cell(struct nvmem_device *nvmem,
+		       const struct nvmem_cell_info *info);
+
 #else
 
 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
@@ -172,6 +175,11 @@  devm_nvmem_register(struct device *dev, const struct nvmem_config *c)
 
 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
+static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
+				     const struct nvmem_cell_info *info)
+{
+	return -EOPNOTSUPP;
+}
 
 #endif /* CONFIG_NVMEM */
 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */