kernel: update NVMEM subsystem to the v6.3
[openwrt/staging/hauke.git] / target / linux / generic / backport-5.15 / 809-v6.3-0005-nvmem-core-add-nvmem_add_one_cell.patch
1 From 2ded6830d376d5e7bf43d59f7f7fdf1a59abc676 Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Mon, 6 Feb 2023 13:43:49 +0000
4 Subject: [PATCH] nvmem: core: add nvmem_add_one_cell()
5
6 Add a new function to add exactly one cell. This will be used by the
7 nvmem layout drivers to add custom cells. In contrast to the
8 nvmem_add_cells(), this has the advantage that we don't have to assemble
9 a list of cells on runtime.
10
11 Signed-off-by: Michael Walle <michael@walle.cc>
12 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
13 Link: https://lore.kernel.org/r/20230206134356.839737-16-srinivas.kandagatla@linaro.org
14 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
15 ---
16 drivers/nvmem/core.c | 59 ++++++++++++++++++++--------------
17 include/linux/nvmem-provider.h | 8 +++++
18 2 files changed, 43 insertions(+), 24 deletions(-)
19
20 --- a/drivers/nvmem/core.c
21 +++ b/drivers/nvmem/core.c
22 @@ -502,6 +502,36 @@ static int nvmem_cell_info_to_nvmem_cell
23 }
24
25 /**
26 + * nvmem_add_one_cell() - Add one cell information to an nvmem device
27 + *
28 + * @nvmem: nvmem device to add cells to.
29 + * @info: nvmem cell info to add to the device
30 + *
31 + * Return: 0 or negative error code on failure.
32 + */
33 +int nvmem_add_one_cell(struct nvmem_device *nvmem,
34 + const struct nvmem_cell_info *info)
35 +{
36 + struct nvmem_cell_entry *cell;
37 + int rval;
38 +
39 + cell = kzalloc(sizeof(*cell), GFP_KERNEL);
40 + if (!cell)
41 + return -ENOMEM;
42 +
43 + rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
44 + if (rval) {
45 + kfree(cell);
46 + return rval;
47 + }
48 +
49 + nvmem_cell_entry_add(cell);
50 +
51 + return 0;
52 +}
53 +EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
54 +
55 +/**
56 * nvmem_add_cells() - Add cell information to an nvmem device
57 *
58 * @nvmem: nvmem device to add cells to.
59 @@ -514,34 +544,15 @@ static int nvmem_add_cells(struct nvmem_
60 const struct nvmem_cell_info *info,
61 int ncells)
62 {
63 - struct nvmem_cell_entry **cells;
64 - int i, rval = 0;
65 -
66 - cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
67 - if (!cells)
68 - return -ENOMEM;
69 + int i, rval;
70
71 for (i = 0; i < ncells; i++) {
72 - cells[i] = kzalloc(sizeof(**cells), GFP_KERNEL);
73 - if (!cells[i]) {
74 - rval = -ENOMEM;
75 - goto out;
76 - }
77 -
78 - rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
79 - if (rval) {
80 - kfree(cells[i]);
81 - goto out;
82 - }
83 -
84 - nvmem_cell_entry_add(cells[i]);
85 + rval = nvmem_add_one_cell(nvmem, &info[i]);
86 + if (rval)
87 + return rval;
88 }
89
90 -out:
91 - /* remove tmp array */
92 - kfree(cells);
93 -
94 - return rval;
95 + return 0;
96 }
97
98 /**
99 --- a/include/linux/nvmem-provider.h
100 +++ b/include/linux/nvmem-provider.h
101 @@ -153,6 +153,9 @@ struct nvmem_device *devm_nvmem_register
102 void nvmem_add_cell_table(struct nvmem_cell_table *table);
103 void nvmem_del_cell_table(struct nvmem_cell_table *table);
104
105 +int nvmem_add_one_cell(struct nvmem_device *nvmem,
106 + const struct nvmem_cell_info *info);
107 +
108 #else
109
110 static inline struct nvmem_device *nvmem_register(const struct nvmem_config *c)
111 @@ -170,6 +173,11 @@ devm_nvmem_register(struct device *dev,
112
113 static inline void nvmem_add_cell_table(struct nvmem_cell_table *table) {}
114 static inline void nvmem_del_cell_table(struct nvmem_cell_table *table) {}
115 +static inline int nvmem_add_one_cell(struct nvmem_device *nvmem,
116 + const struct nvmem_cell_info *info)
117 +{
118 + return -EOPNOTSUPP;
119 +}
120
121 #endif /* CONFIG_NVMEM */
122 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */