1 From 50014d659617dc58780a5d31ceb76c82779a9d8b Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Mon, 6 Feb 2023 13:43:50 +0000
4 Subject: [PATCH] nvmem: core: use nvmem_add_one_cell() in
5 nvmem_add_cells_from_of()
7 Convert nvmem_add_cells_from_of() to use the new nvmem_add_one_cell().
8 This will remove duplicate code and it will make it possible to add a
9 hook to a nvmem layout in between, which can change fields before the
10 cell is finally added.
12 Signed-off-by: Michael Walle <michael@walle.cc>
13 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
14 Link: https://lore.kernel.org/r/20230206134356.839737-17-srinivas.kandagatla@linaro.org
15 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
17 drivers/nvmem/core.c | 45 ++++++++++++++------------------------------
18 1 file changed, 14 insertions(+), 31 deletions(-)
20 --- a/drivers/nvmem/core.c
21 +++ b/drivers/nvmem/core.c
22 @@ -688,15 +688,14 @@ static int nvmem_validate_keepouts(struc
24 static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
26 - struct device_node *parent, *child;
27 struct device *dev = &nvmem->dev;
28 - struct nvmem_cell_entry *cell;
29 + struct device_node *child;
34 - parent = dev->of_node;
35 + for_each_child_of_node(dev->of_node, child) {
36 + struct nvmem_cell_info info = {0};
38 - for_each_child_of_node(parent, child) {
39 addr = of_get_property(child, "reg", &len);
42 @@ -706,40 +705,24 @@ static int nvmem_add_cells_from_of(struc
46 - cell = kzalloc(sizeof(*cell), GFP_KERNEL);
52 - cell->nvmem = nvmem;
53 - cell->offset = be32_to_cpup(addr++);
54 - cell->bytes = be32_to_cpup(addr);
55 - cell->name = kasprintf(GFP_KERNEL, "%pOFn", child);
56 + info.offset = be32_to_cpup(addr++);
57 + info.bytes = be32_to_cpup(addr);
58 + info.name = kasprintf(GFP_KERNEL, "%pOFn", child);
60 addr = of_get_property(child, "bits", &len);
61 if (addr && len == (2 * sizeof(u32))) {
62 - cell->bit_offset = be32_to_cpup(addr++);
63 - cell->nbits = be32_to_cpup(addr);
64 + info.bit_offset = be32_to_cpup(addr++);
65 + info.nbits = be32_to_cpup(addr);
69 - cell->bytes = DIV_ROUND_UP(
70 - cell->nbits + cell->bit_offset,
73 - if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
74 - dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
75 - cell->name, nvmem->stride);
76 - /* Cells already added will be freed later. */
77 - kfree_const(cell->name);
79 + info.np = of_node_get(child);
81 + ret = nvmem_add_one_cell(nvmem, &info);
89 - cell->np = of_node_get(child);
90 - nvmem_cell_entry_add(cell);