e1791e5c83cff038e256f728bbbc2f1878361235
[librecmc/librecmc.git] /
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()
6
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.
11
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>
16 ---
17  drivers/nvmem/core.c | 45 ++++++++++++++------------------------------
18  1 file changed, 14 insertions(+), 31 deletions(-)
19
20 --- a/drivers/nvmem/core.c
21 +++ b/drivers/nvmem/core.c
22 @@ -688,15 +688,14 @@ static int nvmem_validate_keepouts(struc
23  
24  static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
25  {
26 -       struct device_node *parent, *child;
27         struct device *dev = &nvmem->dev;
28 -       struct nvmem_cell_entry *cell;
29 +       struct device_node *child;
30         const __be32 *addr;
31 -       int len;
32 +       int len, ret;
33  
34 -       parent = dev->of_node;
35 +       for_each_child_of_node(dev->of_node, child) {
36 +               struct nvmem_cell_info info = {0};
37  
38 -       for_each_child_of_node(parent, child) {
39                 addr = of_get_property(child, "reg", &len);
40                 if (!addr)
41                         continue;
42 @@ -706,40 +705,24 @@ static int nvmem_add_cells_from_of(struc
43                         return -EINVAL;
44                 }
45  
46 -               cell = kzalloc(sizeof(*cell), GFP_KERNEL);
47 -               if (!cell) {
48 -                       of_node_put(child);
49 -                       return -ENOMEM;
50 -               }
51 -
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);
59  
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);
66                 }
67  
68 -               if (cell->nbits)
69 -                       cell->bytes = DIV_ROUND_UP(
70 -                                       cell->nbits + cell->bit_offset,
71 -                                       BITS_PER_BYTE);
72 -
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);
78 -                       kfree(cell);
79 +               info.np = of_node_get(child);
80 +
81 +               ret = nvmem_add_one_cell(nvmem, &info);
82 +               kfree(info.name);
83 +               if (ret) {
84                         of_node_put(child);
85 -                       return -EINVAL;
86 +                       return ret;
87                 }
88 -
89 -               cell->np = of_node_get(child);
90 -               nvmem_cell_entry_add(cell);
91         }
92  
93         return 0;