1 From 7ae6478b304bc004c3139b422665b0e23b57f05c Mon Sep 17 00:00:00 2001
2 From: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
3 Date: Wed, 13 Oct 2021 14:19:55 +0100
4 Subject: [PATCH] nvmem: core: rework nvmem cell instance creation
6 In the existing design, we do not create a instance per nvmem cell consumer
7 but we directly refer cell from nvmem cell list that are added to provider.
9 However this design has some limitations when consumers want to assign name
10 or connection id the nvmem cell instance, ex: via "nvmem-cell-names" or
11 id in nvmem_cell_get(id).
13 Having a name associated with nvmem cell consumer instance will help
14 provider drivers in performing post processing of nvmem cell data if required
15 before data is seen by the consumers. This is pretty normal with some vendors
16 storing nvmem cells like mac-address in a vendor specific data layouts that
17 are not directly usable by the consumer drivers.
19 With this patch nvmem cell will be created dynamically during nvmem_cell_get
20 and destroyed in nvmem_cell_put, allowing consumers to associate name with
21 nvmem cell consumer instance.
23 With this patch a new struct nvmem_cell_entry replaces struct nvmem_cell
24 for storing nvmem cell information within the core.
25 This patch does not change nvmem-consumer interface based on nvmem_cell.
27 Tested-by: Joakim Zhang <qiangqing.zhang@nxp.com>
28 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
29 Link: https://lore.kernel.org/r/20211013131957.30271-2-srinivas.kandagatla@linaro.org
30 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
32 drivers/nvmem/core.c | 165 +++++++++++++++++++++++++++----------------
33 1 file changed, 105 insertions(+), 60 deletions(-)
35 --- a/drivers/nvmem/core.c
36 +++ b/drivers/nvmem/core.c
37 @@ -45,8 +45,7 @@ struct nvmem_device {
38 #define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
40 #define FLAG_COMPAT BIT(0)
43 +struct nvmem_cell_entry {
47 @@ -57,6 +56,11 @@ struct nvmem_cell {
48 struct list_head node;
52 + struct nvmem_cell_entry *entry;
56 static DEFINE_MUTEX(nvmem_mutex);
57 static DEFINE_IDA(nvmem_ida);
59 @@ -424,7 +428,7 @@ static struct bus_type nvmem_bus_type =
63 -static void nvmem_cell_drop(struct nvmem_cell *cell)
64 +static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
66 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
67 mutex_lock(&nvmem_mutex);
68 @@ -437,13 +441,13 @@ static void nvmem_cell_drop(struct nvmem
70 static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
72 - struct nvmem_cell *cell, *p;
73 + struct nvmem_cell_entry *cell, *p;
75 list_for_each_entry_safe(cell, p, &nvmem->cells, node)
76 - nvmem_cell_drop(cell);
77 + nvmem_cell_entry_drop(cell);
80 -static void nvmem_cell_add(struct nvmem_cell *cell)
81 +static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
83 mutex_lock(&nvmem_mutex);
84 list_add_tail(&cell->node, &cell->nvmem->cells);
85 @@ -451,9 +455,9 @@ static void nvmem_cell_add(struct nvmem_
86 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
89 -static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem,
90 - const struct nvmem_cell_info *info,
91 - struct nvmem_cell *cell)
92 +static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
93 + const struct nvmem_cell_info *info,
94 + struct nvmem_cell_entry *cell)
97 cell->offset = info->offset;
98 @@ -477,13 +481,13 @@ static int nvmem_cell_info_to_nvmem_cell
102 -static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem,
103 - const struct nvmem_cell_info *info,
104 - struct nvmem_cell *cell)
105 +static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
106 + const struct nvmem_cell_info *info,
107 + struct nvmem_cell_entry *cell)
111 - err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell);
112 + err = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, cell);
116 @@ -507,7 +511,7 @@ static int nvmem_add_cells(struct nvmem_
117 const struct nvmem_cell_info *info,
120 - struct nvmem_cell **cells;
121 + struct nvmem_cell_entry **cells;
124 cells = kcalloc(ncells, sizeof(*cells), GFP_KERNEL);
125 @@ -521,13 +525,13 @@ static int nvmem_add_cells(struct nvmem_
129 - rval = nvmem_cell_info_to_nvmem_cell(nvmem, &info[i], cells[i]);
130 + rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, &info[i], cells[i]);
136 - nvmem_cell_add(cells[i]);
137 + nvmem_cell_entry_add(cells[i]);
140 /* remove tmp array */
141 @@ -536,7 +540,7 @@ static int nvmem_add_cells(struct nvmem_
145 - nvmem_cell_drop(cells[i]);
146 + nvmem_cell_entry_drop(cells[i]);
150 @@ -573,7 +577,7 @@ static int nvmem_add_cells_from_table(st
152 const struct nvmem_cell_info *info;
153 struct nvmem_cell_table *table;
154 - struct nvmem_cell *cell;
155 + struct nvmem_cell_entry *cell;
158 mutex_lock(&nvmem_cell_mutex);
159 @@ -588,15 +592,13 @@ static int nvmem_add_cells_from_table(st
163 - rval = nvmem_cell_info_to_nvmem_cell(nvmem,
166 + rval = nvmem_cell_info_to_nvmem_cell_entry(nvmem, info, cell);
172 - nvmem_cell_add(cell);
173 + nvmem_cell_entry_add(cell);
177 @@ -606,10 +608,10 @@ out:
181 -static struct nvmem_cell *
182 -nvmem_find_cell_by_name(struct nvmem_device *nvmem, const char *cell_id)
183 +static struct nvmem_cell_entry *
184 +nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
186 - struct nvmem_cell *iter, *cell = NULL;
187 + struct nvmem_cell_entry *iter, *cell = NULL;
189 mutex_lock(&nvmem_mutex);
190 list_for_each_entry(iter, &nvmem->cells, node) {
191 @@ -680,7 +682,7 @@ static int nvmem_add_cells_from_of(struc
193 struct device_node *parent, *child;
194 struct device *dev = &nvmem->dev;
195 - struct nvmem_cell *cell;
196 + struct nvmem_cell_entry *cell;
200 @@ -729,7 +731,7 @@ static int nvmem_add_cells_from_of(struc
203 cell->np = of_node_get(child);
204 - nvmem_cell_add(cell);
205 + nvmem_cell_entry_add(cell);
209 @@ -1139,9 +1141,33 @@ struct nvmem_device *devm_nvmem_device_g
211 EXPORT_SYMBOL_GPL(devm_nvmem_device_get);
213 +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, const char *id)
215 + struct nvmem_cell *cell;
216 + const char *name = NULL;
218 + cell = kzalloc(sizeof(*cell), GFP_KERNEL);
220 + return ERR_PTR(-ENOMEM);
223 + name = kstrdup_const(id, GFP_KERNEL);
226 + return ERR_PTR(-ENOMEM);
231 + cell->entry = entry;
236 static struct nvmem_cell *
237 nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
239 + struct nvmem_cell_entry *cell_entry;
240 struct nvmem_cell *cell = ERR_PTR(-ENOENT);
241 struct nvmem_cell_lookup *lookup;
242 struct nvmem_device *nvmem;
243 @@ -1166,11 +1192,15 @@ nvmem_cell_get_from_lookup(struct device
247 - cell = nvmem_find_cell_by_name(nvmem,
248 - lookup->cell_name);
250 + cell_entry = nvmem_find_cell_entry_by_name(nvmem,
251 + lookup->cell_name);
253 __nvmem_device_put(nvmem);
254 cell = ERR_PTR(-ENOENT);
256 + cell = nvmem_create_cell(cell_entry, con_id);
258 + __nvmem_device_put(nvmem);
262 @@ -1181,10 +1211,10 @@ nvmem_cell_get_from_lookup(struct device
265 #if IS_ENABLED(CONFIG_OF)
266 -static struct nvmem_cell *
267 -nvmem_find_cell_by_node(struct nvmem_device *nvmem, struct device_node *np)
268 +static struct nvmem_cell_entry *
269 +nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
271 - struct nvmem_cell *iter, *cell = NULL;
272 + struct nvmem_cell_entry *iter, *cell = NULL;
274 mutex_lock(&nvmem_mutex);
275 list_for_each_entry(iter, &nvmem->cells, node) {
276 @@ -1214,6 +1244,7 @@ struct nvmem_cell *of_nvmem_cell_get(str
278 struct device_node *cell_np, *nvmem_np;
279 struct nvmem_device *nvmem;
280 + struct nvmem_cell_entry *cell_entry;
281 struct nvmem_cell *cell;
284 @@ -1234,12 +1265,16 @@ struct nvmem_cell *of_nvmem_cell_get(str
286 return ERR_CAST(nvmem);
288 - cell = nvmem_find_cell_by_node(nvmem, cell_np);
290 + cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
292 __nvmem_device_put(nvmem);
293 return ERR_PTR(-ENOENT);
296 + cell = nvmem_create_cell(cell_entry, id);
298 + __nvmem_device_put(nvmem);
302 EXPORT_SYMBOL_GPL(of_nvmem_cell_get);
303 @@ -1345,13 +1380,17 @@ EXPORT_SYMBOL(devm_nvmem_cell_put);
305 void nvmem_cell_put(struct nvmem_cell *cell)
307 - struct nvmem_device *nvmem = cell->nvmem;
308 + struct nvmem_device *nvmem = cell->entry->nvmem;
311 + kfree_const(cell->id);
314 __nvmem_device_put(nvmem);
316 EXPORT_SYMBOL_GPL(nvmem_cell_put);
318 -static void nvmem_shift_read_buffer_in_place(struct nvmem_cell *cell, void *buf)
319 +static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
322 int i, extra, bit_offset = cell->bit_offset;
323 @@ -1385,8 +1424,8 @@ static void nvmem_shift_read_buffer_in_p
326 static int __nvmem_cell_read(struct nvmem_device *nvmem,
327 - struct nvmem_cell *cell,
328 - void *buf, size_t *len)
329 + struct nvmem_cell_entry *cell,
330 + void *buf, size_t *len, const char *id)
334 @@ -1417,18 +1456,18 @@ static int __nvmem_cell_read(struct nvme
336 void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
338 - struct nvmem_device *nvmem = cell->nvmem;
339 + struct nvmem_device *nvmem = cell->entry->nvmem;
344 return ERR_PTR(-EINVAL);
346 - buf = kzalloc(cell->bytes, GFP_KERNEL);
347 + buf = kzalloc(cell->entry->bytes, GFP_KERNEL);
349 return ERR_PTR(-ENOMEM);
351 - rc = __nvmem_cell_read(nvmem, cell, buf, len);
352 + rc = __nvmem_cell_read(nvmem, cell->entry, buf, len, cell->id);
356 @@ -1438,7 +1477,7 @@ void *nvmem_cell_read(struct nvmem_cell
358 EXPORT_SYMBOL_GPL(nvmem_cell_read);
360 -static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell *cell,
361 +static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
364 struct nvmem_device *nvmem = cell->nvmem;
365 @@ -1491,16 +1530,7 @@ err:
370 - * nvmem_cell_write() - Write to a given nvmem cell
372 - * @cell: nvmem cell to be written.
373 - * @buf: Buffer to be written.
374 - * @len: length of buffer to be written to nvmem cell.
376 - * Return: length of bytes written or negative on failure.
378 -int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
379 +static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
381 struct nvmem_device *nvmem = cell->nvmem;
383 @@ -1526,6 +1556,21 @@ int nvmem_cell_write(struct nvmem_cell *
389 + * nvmem_cell_write() - Write to a given nvmem cell
391 + * @cell: nvmem cell to be written.
392 + * @buf: Buffer to be written.
393 + * @len: length of buffer to be written to nvmem cell.
395 + * Return: length of bytes written or negative on failure.
397 +int nvmem_cell_write(struct nvmem_cell *cell, void *buf, size_t len)
399 + return __nvmem_cell_entry_write(cell->entry, buf, len);
402 EXPORT_SYMBOL_GPL(nvmem_cell_write);
404 static int nvmem_cell_read_common(struct device *dev, const char *cell_id,
405 @@ -1628,7 +1673,7 @@ static const void *nvmem_cell_read_varia
409 - nbits = cell->nbits;
410 + nbits = cell->entry->nbits;
411 buf = nvmem_cell_read(cell, len);
412 nvmem_cell_put(cell);
414 @@ -1724,18 +1769,18 @@ EXPORT_SYMBOL_GPL(nvmem_cell_read_variab
415 ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
416 struct nvmem_cell_info *info, void *buf)
418 - struct nvmem_cell cell;
419 + struct nvmem_cell_entry cell;
426 - rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
427 + rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
431 - rc = __nvmem_cell_read(nvmem, &cell, buf, &len);
432 + rc = __nvmem_cell_read(nvmem, &cell, buf, &len, NULL);
436 @@ -1755,17 +1800,17 @@ EXPORT_SYMBOL_GPL(nvmem_device_cell_read
437 int nvmem_device_cell_write(struct nvmem_device *nvmem,
438 struct nvmem_cell_info *info, void *buf)
440 - struct nvmem_cell cell;
441 + struct nvmem_cell_entry cell;
447 - rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell);
448 + rc = nvmem_cell_info_to_nvmem_cell_entry_nodup(nvmem, info, &cell);
452 - return nvmem_cell_write(&cell, buf, cell.bytes);
453 + return __nvmem_cell_entry_write(&cell, buf, cell.bytes);
455 EXPORT_SYMBOL_GPL(nvmem_device_cell_write);