[PATCH v2 1/2] nvmem: core: allow to register cells for existing device

From: Vadym Kochan
Date: Fri May 29 2020 - 19:05:23 EST


Current implementation does not allow to register nvmem cells for
existing device and requires that this will be done before device is
registered.

But there might a driver which provides only cells info which needs to
be added for already registered nvmem device.

Signed-off-by: Vadym Kochan <vadym.kochan@xxxxxxxxxxx>
---
drivers/nvmem/core.c | 59 +++++++++++++++++++++++++++-----------------
1 file changed, 37 insertions(+), 22 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 05c6ae4b0b97..39c9df9ae9a0 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -482,35 +482,43 @@ int nvmem_unregister_notifier(struct notifier_block *nb)
}
EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);

-static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
+static int __nvmem_add_cells_from_table(struct nvmem_device *nvmem,
+ struct nvmem_cell_table *table)
{
const struct nvmem_cell_info *info;
- struct nvmem_cell_table *table;
struct nvmem_cell *cell;
- int rval = 0, i;
+ int err, i;
+
+ for (i = 0; i < table->ncells; i++) {
+ info = &table->cells[i];
+
+ cell = kzalloc(sizeof(*cell), GFP_KERNEL);
+ if (!cell)
+ return -ENOMEM;
+
+ err = nvmem_cell_info_to_nvmem_cell(nvmem, info, cell);
+ if (err) {
+ kfree(cell);
+ return err;
+ }
+
+ nvmem_cell_add(cell);
+ }
+
+ return 0;
+}
+
+static int nvmem_add_cells_from_table(struct nvmem_device *nvmem)
+{
+ struct nvmem_cell_table *table;
+ int rval = 0;

mutex_lock(&nvmem_cell_mutex);
list_for_each_entry(table, &nvmem_cell_tables, node) {
if (strcmp(nvmem_dev_name(nvmem), table->nvmem_name) == 0) {
- for (i = 0; i < table->ncells; i++) {
- info = &table->cells[i];
-
- cell = kzalloc(sizeof(*cell), GFP_KERNEL);
- if (!cell) {
- rval = -ENOMEM;
- goto out;
- }
-
- rval = nvmem_cell_info_to_nvmem_cell(nvmem,
- info,
- cell);
- if (rval) {
- kfree(cell);
- goto out;
- }
-
- nvmem_cell_add(cell);
- }
+ rval = __nvmem_add_cells_from_table(nvmem, table);
+ if (rval)
+ goto out;
}
}

@@ -1560,6 +1568,13 @@ EXPORT_SYMBOL_GPL(nvmem_device_write);
*/
void nvmem_add_cell_table(struct nvmem_cell_table *table)
{
+ const char *dev_name = table->nvmem_name;
+ struct nvmem_device *nvmem;
+
+ nvmem = __nvmem_device_get((void *)dev_name, device_match_name);
+ if (nvmem)
+ __nvmem_add_cells_from_table(nvmem, table);
+
mutex_lock(&nvmem_cell_mutex);
list_add_tail(&table->node, &nvmem_cell_tables);
mutex_unlock(&nvmem_cell_mutex);
--
2.17.1