Skip to content

Commit 0749aa2

Browse files
Srinivas-Kandagatlagregkh
authored andcommitted
nvmem: core: fix regression in of_nvmem_cell_get()
NVMEM DT support seems to be totally broken after commit e888d44 ("nvmem: resolve cells from DT at registration time") Fix this! Index used in of_nvmem_cell_get() to find cell is specific to consumer, It can not be used for searching the cell in provider. Use device_node instead of this to find the matching cell in device tree case. Fixes: e888d44 ("nvmem: resolve cells from DT at registration time") Reported-by: Niklas Cassel <[email protected]> Signed-off-by: Srinivas Kandagatla <[email protected]> Tested-by: Niklas Cassel <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 7c97301 commit 0749aa2

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/nvmem/core.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct nvmem_cell {
4444
int bytes;
4545
int bit_offset;
4646
int nbits;
47+
struct device_node *np;
4748
struct nvmem_device *nvmem;
4849
struct list_head node;
4950
};
@@ -298,6 +299,7 @@ static void nvmem_cell_drop(struct nvmem_cell *cell)
298299
mutex_lock(&nvmem_mutex);
299300
list_del(&cell->node);
300301
mutex_unlock(&nvmem_mutex);
302+
of_node_put(cell->np);
301303
kfree(cell->name);
302304
kfree(cell);
303305
}
@@ -530,6 +532,7 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
530532
return -ENOMEM;
531533

532534
cell->nvmem = nvmem;
535+
cell->np = of_node_get(child);
533536
cell->offset = be32_to_cpup(addr++);
534537
cell->bytes = be32_to_cpup(addr);
535538
cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
@@ -960,14 +963,13 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
960963

961964
#if IS_ENABLED(CONFIG_OF)
962965
static struct nvmem_cell *
963-
nvmem_find_cell_by_index(struct nvmem_device *nvmem, int index)
966+
nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
964967
{
965968
struct nvmem_cell *cell = NULL;
966-
int i = 0;
967969

968970
mutex_lock(&nvmem_mutex);
969971
list_for_each_entry(cell, &nvmem->cells, node) {
970-
if (index == i++)
972+
if (np == cell->np)
971973
break;
972974
}
973975
mutex_unlock(&nvmem_mutex);
@@ -1011,7 +1013,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
10111013
if (IS_ERR(nvmem))
10121014
return ERR_CAST(nvmem);
10131015

1014-
cell = nvmem_find_cell_by_index(nvmem, index);
1016+
cell = nvmem_find_cell_by_node(nvmem, cell_np);
10151017
if (!cell) {
10161018
__nvmem_device_put(nvmem);
10171019
return ERR_PTR(-ENOENT);

0 commit comments

Comments
 (0)