Commit 2ded6830 authored by Michael Walle's avatar Michael Walle Committed by Greg Kroah-Hartman
Browse files

nvmem: core: add nvmem_add_one_cell()



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: default avatarMichael Walle <michael@walle.cc>
Signed-off-by: default avatarSrinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20230206134356.839737-16-srinivas.kandagatla@linaro.org


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent cc5bdd32
Loading
Loading
Loading
Loading
+35 −24
Original line number Diff line number Diff line
@@ -502,48 +502,59 @@ static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
}

/**
 * nvmem_add_cells() - Add cell information to an nvmem device
 * 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
 * @ncells: number of cells in info
 *
 * Return: 0 or negative error code on failure.
 */
static int nvmem_add_cells(struct nvmem_device *nvmem,
		    const struct nvmem_cell_info *info,
		    int ncells)
int nvmem_add_one_cell(struct nvmem_device *nvmem,
		       const struct nvmem_cell_info *info)
{
	struct nvmem_cell_entry **cells;
	int i, rval = 0;
	struct nvmem_cell_entry *cell;
	int rval;

	cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
	if (!cells)
	cell = kzalloc(sizeof(*cell), GFP_KERNEL);
	if (!cell)
		return -ENOMEM;

	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]);
	rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
	if (rval) {
			kfree(cells[i]);
			goto out;
		kfree(cell);
		return rval;
	}

		nvmem_cell_entry_add(cells[i]);
	nvmem_cell_entry_add(cell);

	return 0;
}
EXPORT_SYMBOL_GPL(nvmem_add_one_cell);

out:
	/* remove tmp array */
	kfree(cells);
/**
 * 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.
 */
static int nvmem_add_cells(struct nvmem_device *nvmem,
		    const struct nvmem_cell_info *info,
		    int ncells)
{
	int i, rval;

	for (i = 0; i < ncells; i++) {
		rval = nvmem_add_one_cell(nvmem, &info[i]);
		if (rval)
			return rval;
	}

	return 0;
}

/**
 * nvmem_register_notifier() - Register a notifier block for nvmem events.
 *
+8 −0
Original line number Diff line number Diff line
@@ -153,6 +153,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)
@@ -170,6 +173,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 */