ce33b52328a145ed546fd2c142971009827d7a95
[openwrt/staging/ansuel.git] /
1 From fc29fd821d9ac2ae3d32a722fac39ce874efb883 Mon Sep 17 00:00:00 2001
2 From: Miquel Raynal <miquel.raynal@bootlin.com>
3 Date: Fri, 15 Dec 2023 11:15:32 +0000
4 Subject: [PATCH] nvmem: core: Rework layouts to become regular devices
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Current layout support was initially written without modules support in
10 mind. When the requirement for module support rose, the existing base
11 was improved to adopt modularization support, but kind of a design flaw
12 was introduced. With the existing implementation, when a storage device
13 registers into NVMEM, the core tries to hook a layout (if any) and
14 populates its cells immediately. This means, if the hardware description
15 expects a layout to be hooked up, but no driver was provided for that,
16 the storage medium will fail to probe and try later from
17 scratch. Even if we consider that the hardware description shall be
18 correct, we could still probe the storage device (especially if it
19 contains the rootfs).
20
21 One way to overcome this situation is to consider the layouts as
22 devices, and leverage the native notifier mechanism. When a new NVMEM
23 device is registered, we can populate its nvmem-layout child, if any,
24 and wait for the matching to be done in order to get the cells (the
25 waiting can be easily done with the NVMEM notifiers). If the layout
26 driver is compiled as a module, it should automatically be loaded. This
27 way, there is no strong order to enforce, any NVMEM device creation
28 or NVMEM layout driver insertion will be observed as a new event which
29 may lead to the creation of additional cells, without disturbing the
30 probes with costly (and sometimes endless) deferrals.
31
32 In order to achieve that goal we create a new bus for the nvmem-layouts
33 with minimal logic to match nvmem-layout devices with nvmem-layout
34 drivers. All this infrastructure code is created in the layouts.c file.
35
36 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
37 Tested-by: Rafał Miłecki <rafal@milecki.pl>
38 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
39 Link: https://lore.kernel.org/r/20231215111536.316972-7-srinivas.kandagatla@linaro.org
40 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
41 ---
42 drivers/nvmem/Kconfig | 1 +
43 drivers/nvmem/Makefile | 2 +
44 drivers/nvmem/core.c | 170 ++++++++++----------------
45 drivers/nvmem/internals.h | 21 ++++
46 drivers/nvmem/layouts.c | 201 +++++++++++++++++++++++++++++++
47 drivers/nvmem/layouts/Kconfig | 8 ++
48 drivers/nvmem/layouts/onie-tlv.c | 24 +++-
49 drivers/nvmem/layouts/sl28vpd.c | 24 +++-
50 include/linux/nvmem-provider.h | 38 +++---
51 9 files changed, 354 insertions(+), 135 deletions(-)
52 create mode 100644 drivers/nvmem/layouts.c
53
54 --- a/drivers/nvmem/Kconfig
55 +++ b/drivers/nvmem/Kconfig
56 @@ -1,6 +1,7 @@
57 # SPDX-License-Identifier: GPL-2.0-only
58 menuconfig NVMEM
59 bool "NVMEM Support"
60 + imply NVMEM_LAYOUTS
61 help
62 Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
63
64 --- a/drivers/nvmem/Makefile
65 +++ b/drivers/nvmem/Makefile
66 @@ -5,6 +5,8 @@
67
68 obj-$(CONFIG_NVMEM) += nvmem_core.o
69 nvmem_core-y := core.o
70 +obj-$(CONFIG_NVMEM_LAYOUTS) += nvmem_layouts.o
71 +nvmem_layouts-y := layouts.o
72 obj-y += layouts/
73
74 # Devices
75 --- a/drivers/nvmem/core.c
76 +++ b/drivers/nvmem/core.c
77 @@ -55,9 +55,6 @@ static LIST_HEAD(nvmem_lookup_list);
78
79 static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
80
81 -static DEFINE_SPINLOCK(nvmem_layout_lock);
82 -static LIST_HEAD(nvmem_layouts);
83 -
84 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
85 void *val, size_t bytes)
86 {
87 @@ -740,97 +737,22 @@ static int nvmem_add_cells_from_fixed_la
88 return err;
89 }
90
91 -int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner)
92 +int nvmem_layout_register(struct nvmem_layout *layout)
93 {
94 - layout->owner = owner;
95 -
96 - spin_lock(&nvmem_layout_lock);
97 - list_add(&layout->node, &nvmem_layouts);
98 - spin_unlock(&nvmem_layout_lock);
99 -
100 - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_ADD, layout);
101 + if (!layout->add_cells)
102 + return -EINVAL;
103
104 - return 0;
105 + /* Populate the cells */
106 + return layout->add_cells(&layout->nvmem->dev, layout->nvmem);
107 }
108 -EXPORT_SYMBOL_GPL(__nvmem_layout_register);
109 +EXPORT_SYMBOL_GPL(nvmem_layout_register);
110
111 void nvmem_layout_unregister(struct nvmem_layout *layout)
112 {
113 - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_LAYOUT_REMOVE, layout);
114 -
115 - spin_lock(&nvmem_layout_lock);
116 - list_del(&layout->node);
117 - spin_unlock(&nvmem_layout_lock);
118 + /* Keep the API even with an empty stub in case we need it later */
119 }
120 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
121
122 -static struct nvmem_layout *nvmem_layout_get(struct nvmem_device *nvmem)
123 -{
124 - struct device_node *layout_np;
125 - struct nvmem_layout *l, *layout = ERR_PTR(-EPROBE_DEFER);
126 -
127 - layout_np = of_nvmem_layout_get_container(nvmem);
128 - if (!layout_np)
129 - return NULL;
130 -
131 - /* Fixed layouts don't have a matching driver */
132 - if (of_device_is_compatible(layout_np, "fixed-layout")) {
133 - of_node_put(layout_np);
134 - return NULL;
135 - }
136 -
137 - /*
138 - * In case the nvmem device was built-in while the layout was built as a
139 - * module, we shall manually request the layout driver loading otherwise
140 - * we'll never have any match.
141 - */
142 - of_request_module(layout_np);
143 -
144 - spin_lock(&nvmem_layout_lock);
145 -
146 - list_for_each_entry(l, &nvmem_layouts, node) {
147 - if (of_match_node(l->of_match_table, layout_np)) {
148 - if (try_module_get(l->owner))
149 - layout = l;
150 -
151 - break;
152 - }
153 - }
154 -
155 - spin_unlock(&nvmem_layout_lock);
156 - of_node_put(layout_np);
157 -
158 - return layout;
159 -}
160 -
161 -static void nvmem_layout_put(struct nvmem_layout *layout)
162 -{
163 - if (layout)
164 - module_put(layout->owner);
165 -}
166 -
167 -static int nvmem_add_cells_from_layout(struct nvmem_device *nvmem)
168 -{
169 - struct nvmem_layout *layout = nvmem->layout;
170 - int ret;
171 -
172 - if (layout && layout->add_cells) {
173 - ret = layout->add_cells(&nvmem->dev, nvmem);
174 - if (ret)
175 - return ret;
176 - }
177 -
178 - return 0;
179 -}
180 -
181 -#if IS_ENABLED(CONFIG_OF)
182 -struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
183 -{
184 - return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
185 -}
186 -EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
187 -#endif
188 -
189 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
190 struct nvmem_layout *layout)
191 {
192 @@ -838,7 +760,7 @@ const void *nvmem_layout_get_match_data(
193 const struct of_device_id *match;
194
195 layout_np = of_nvmem_layout_get_container(nvmem);
196 - match = of_match_node(layout->of_match_table, layout_np);
197 + match = of_match_node(layout->dev.driver->of_match_table, layout_np);
198
199 return match ? match->data : NULL;
200 }
201 @@ -950,19 +872,6 @@ struct nvmem_device *nvmem_register(cons
202 goto err_put_device;
203 }
204
205 - /*
206 - * If the driver supplied a layout by config->layout, the module
207 - * pointer will be NULL and nvmem_layout_put() will be a noop.
208 - */
209 - nvmem->layout = config->layout ?: nvmem_layout_get(nvmem);
210 - if (IS_ERR(nvmem->layout)) {
211 - rval = PTR_ERR(nvmem->layout);
212 - nvmem->layout = NULL;
213 -
214 - if (rval == -EPROBE_DEFER)
215 - goto err_teardown_compat;
216 - }
217 -
218 if (config->cells) {
219 rval = nvmem_add_cells(nvmem, config->cells, config->ncells);
220 if (rval)
221 @@ -983,24 +892,24 @@ struct nvmem_device *nvmem_register(cons
222 if (rval)
223 goto err_remove_cells;
224
225 - rval = nvmem_add_cells_from_layout(nvmem);
226 - if (rval)
227 - goto err_remove_cells;
228 -
229 dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
230
231 rval = device_add(&nvmem->dev);
232 if (rval)
233 goto err_remove_cells;
234
235 + rval = nvmem_populate_layout(nvmem);
236 + if (rval)
237 + goto err_remove_dev;
238 +
239 blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
240
241 return nvmem;
242
243 +err_remove_dev:
244 + device_del(&nvmem->dev);
245 err_remove_cells:
246 nvmem_device_remove_all_cells(nvmem);
247 - nvmem_layout_put(nvmem->layout);
248 -err_teardown_compat:
249 if (config->compat)
250 nvmem_sysfs_remove_compat(nvmem, config);
251 err_put_device:
252 @@ -1022,7 +931,7 @@ static void nvmem_device_release(struct
253 device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
254
255 nvmem_device_remove_all_cells(nvmem);
256 - nvmem_layout_put(nvmem->layout);
257 + nvmem_destroy_layout(nvmem);
258 device_unregister(&nvmem->dev);
259 }
260
261 @@ -1324,6 +1233,12 @@ nvmem_cell_get_from_lookup(struct device
262 return cell;
263 }
264
265 +static void nvmem_layout_module_put(struct nvmem_device *nvmem)
266 +{
267 + if (nvmem->layout && nvmem->layout->dev.driver)
268 + module_put(nvmem->layout->dev.driver->owner);
269 +}
270 +
271 #if IS_ENABLED(CONFIG_OF)
272 static struct nvmem_cell_entry *
273 nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
274 @@ -1342,6 +1257,18 @@ nvmem_find_cell_entry_by_node(struct nvm
275 return cell;
276 }
277
278 +static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
279 +{
280 + if (!nvmem->layout)
281 + return 0;
282 +
283 + if (!nvmem->layout->dev.driver ||
284 + !try_module_get(nvmem->layout->dev.driver->owner))
285 + return -EPROBE_DEFER;
286 +
287 + return 0;
288 +}
289 +
290 /**
291 * of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
292 *
293 @@ -1404,16 +1331,29 @@ struct nvmem_cell *of_nvmem_cell_get(str
294 return ERR_CAST(nvmem);
295 }
296
297 + ret = nvmem_layout_module_get_optional(nvmem);
298 + if (ret) {
299 + of_node_put(cell_np);
300 + __nvmem_device_put(nvmem);
301 + return ERR_PTR(ret);
302 + }
303 +
304 cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
305 of_node_put(cell_np);
306 if (!cell_entry) {
307 __nvmem_device_put(nvmem);
308 - return ERR_PTR(-ENOENT);
309 + nvmem_layout_module_put(nvmem);
310 + if (nvmem->layout)
311 + return ERR_PTR(-EPROBE_DEFER);
312 + else
313 + return ERR_PTR(-ENOENT);
314 }
315
316 cell = nvmem_create_cell(cell_entry, id, cell_index);
317 - if (IS_ERR(cell))
318 + if (IS_ERR(cell)) {
319 __nvmem_device_put(nvmem);
320 + nvmem_layout_module_put(nvmem);
321 + }
322
323 return cell;
324 }
325 @@ -1527,6 +1467,7 @@ void nvmem_cell_put(struct nvmem_cell *c
326
327 kfree(cell);
328 __nvmem_device_put(nvmem);
329 + nvmem_layout_module_put(nvmem);
330 }
331 EXPORT_SYMBOL_GPL(nvmem_cell_put);
332
333 @@ -2104,11 +2045,22 @@ EXPORT_SYMBOL_GPL(nvmem_dev_name);
334
335 static int __init nvmem_init(void)
336 {
337 - return bus_register(&nvmem_bus_type);
338 + int ret;
339 +
340 + ret = bus_register(&nvmem_bus_type);
341 + if (ret)
342 + return ret;
343 +
344 + ret = nvmem_layout_bus_register();
345 + if (ret)
346 + bus_unregister(&nvmem_bus_type);
347 +
348 + return ret;
349 }
350
351 static void __exit nvmem_exit(void)
352 {
353 + nvmem_layout_bus_unregister();
354 bus_unregister(&nvmem_bus_type);
355 }
356
357 --- a/drivers/nvmem/internals.h
358 +++ b/drivers/nvmem/internals.h
359 @@ -34,4 +34,25 @@ struct nvmem_device {
360 void *priv;
361 };
362
363 +#if IS_ENABLED(CONFIG_OF)
364 +int nvmem_layout_bus_register(void);
365 +void nvmem_layout_bus_unregister(void);
366 +int nvmem_populate_layout(struct nvmem_device *nvmem);
367 +void nvmem_destroy_layout(struct nvmem_device *nvmem);
368 +#else /* CONFIG_OF */
369 +static inline int nvmem_layout_bus_register(void)
370 +{
371 + return 0;
372 +}
373 +
374 +static inline void nvmem_layout_bus_unregister(void) {}
375 +
376 +static inline int nvmem_populate_layout(struct nvmem_device *nvmem)
377 +{
378 + return 0;
379 +}
380 +
381 +static inline void nvmem_destroy_layout(struct nvmem_device *nvmem) { }
382 +#endif /* CONFIG_OF */
383 +
384 #endif /* ifndef _LINUX_NVMEM_INTERNALS_H */
385 --- /dev/null
386 +++ b/drivers/nvmem/layouts.c
387 @@ -0,0 +1,201 @@
388 +// SPDX-License-Identifier: GPL-2.0
389 +/*
390 + * NVMEM layout bus handling
391 + *
392 + * Copyright (C) 2023 Bootlin
393 + * Author: Miquel Raynal <miquel.raynal@bootlin.com
394 + */
395 +
396 +#include <linux/device.h>
397 +#include <linux/dma-mapping.h>
398 +#include <linux/nvmem-consumer.h>
399 +#include <linux/nvmem-provider.h>
400 +#include <linux/of.h>
401 +#include <linux/of_device.h>
402 +#include <linux/of_irq.h>
403 +
404 +#include "internals.h"
405 +
406 +#define to_nvmem_layout_driver(drv) \
407 + (container_of((drv), struct nvmem_layout_driver, driver))
408 +#define to_nvmem_layout_device(_dev) \
409 + container_of((_dev), struct nvmem_layout, dev)
410 +
411 +static int nvmem_layout_bus_match(struct device *dev, struct device_driver *drv)
412 +{
413 + return of_driver_match_device(dev, drv);
414 +}
415 +
416 +static int nvmem_layout_bus_probe(struct device *dev)
417 +{
418 + struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver);
419 + struct nvmem_layout *layout = to_nvmem_layout_device(dev);
420 +
421 + if (!drv->probe || !drv->remove)
422 + return -EINVAL;
423 +
424 + return drv->probe(layout);
425 +}
426 +
427 +static void nvmem_layout_bus_remove(struct device *dev)
428 +{
429 + struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver);
430 + struct nvmem_layout *layout = to_nvmem_layout_device(dev);
431 +
432 + return drv->remove(layout);
433 +}
434 +
435 +static struct bus_type nvmem_layout_bus_type = {
436 + .name = "nvmem-layout",
437 + .match = nvmem_layout_bus_match,
438 + .probe = nvmem_layout_bus_probe,
439 + .remove = nvmem_layout_bus_remove,
440 +};
441 +
442 +int nvmem_layout_driver_register(struct nvmem_layout_driver *drv)
443 +{
444 + drv->driver.bus = &nvmem_layout_bus_type;
445 +
446 + return driver_register(&drv->driver);
447 +}
448 +EXPORT_SYMBOL_GPL(nvmem_layout_driver_register);
449 +
450 +void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv)
451 +{
452 + driver_unregister(&drv->driver);
453 +}
454 +EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister);
455 +
456 +static void nvmem_layout_release_device(struct device *dev)
457 +{
458 + struct nvmem_layout *layout = to_nvmem_layout_device(dev);
459 +
460 + of_node_put(layout->dev.of_node);
461 + kfree(layout);
462 +}
463 +
464 +static int nvmem_layout_create_device(struct nvmem_device *nvmem,
465 + struct device_node *np)
466 +{
467 + struct nvmem_layout *layout;
468 + struct device *dev;
469 + int ret;
470 +
471 + layout = kzalloc(sizeof(*layout), GFP_KERNEL);
472 + if (!layout)
473 + return -ENOMEM;
474 +
475 + /* Create a bidirectional link */
476 + layout->nvmem = nvmem;
477 + nvmem->layout = layout;
478 +
479 + /* Device model registration */
480 + dev = &layout->dev;
481 + device_initialize(dev);
482 + dev->parent = &nvmem->dev;
483 + dev->bus = &nvmem_layout_bus_type;
484 + dev->release = nvmem_layout_release_device;
485 + dev->coherent_dma_mask = DMA_BIT_MASK(32);
486 + dev->dma_mask = &dev->coherent_dma_mask;
487 + device_set_node(dev, of_fwnode_handle(of_node_get(np)));
488 + of_device_make_bus_id(dev);
489 + of_msi_configure(dev, dev->of_node);
490 +
491 + ret = device_add(dev);
492 + if (ret) {
493 + put_device(dev);
494 + return ret;
495 + }
496 +
497 + return 0;
498 +}
499 +
500 +static const struct of_device_id of_nvmem_layout_skip_table[] = {
501 + { .compatible = "fixed-layout", },
502 + {}
503 +};
504 +
505 +static int nvmem_layout_bus_populate(struct nvmem_device *nvmem,
506 + struct device_node *layout_dn)
507 +{
508 + int ret;
509 +
510 + /* Make sure it has a compatible property */
511 + if (!of_get_property(layout_dn, "compatible", NULL)) {
512 + pr_debug("%s() - skipping %pOF, no compatible prop\n",
513 + __func__, layout_dn);
514 + return 0;
515 + }
516 +
517 + /* Fixed layouts are parsed manually somewhere else for now */
518 + if (of_match_node(of_nvmem_layout_skip_table, layout_dn)) {
519 + pr_debug("%s() - skipping %pOF node\n", __func__, layout_dn);
520 + return 0;
521 + }
522 +
523 + if (of_node_check_flag(layout_dn, OF_POPULATED_BUS)) {
524 + pr_debug("%s() - skipping %pOF, already populated\n",
525 + __func__, layout_dn);
526 +
527 + return 0;
528 + }
529 +
530 + /* NVMEM layout buses expect only a single device representing the layout */
531 + ret = nvmem_layout_create_device(nvmem, layout_dn);
532 + if (ret)
533 + return ret;
534 +
535 + of_node_set_flag(layout_dn, OF_POPULATED_BUS);
536 +
537 + return 0;
538 +}
539 +
540 +struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
541 +{
542 + return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
543 +}
544 +EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
545 +
546 +/*
547 + * Returns the number of devices populated, 0 if the operation was not relevant
548 + * for this nvmem device, an error code otherwise.
549 + */
550 +int nvmem_populate_layout(struct nvmem_device *nvmem)
551 +{
552 + struct device_node *layout_dn;
553 + int ret;
554 +
555 + layout_dn = of_nvmem_layout_get_container(nvmem);
556 + if (!layout_dn)
557 + return 0;
558 +
559 + /* Populate the layout device */
560 + device_links_supplier_sync_state_pause();
561 + ret = nvmem_layout_bus_populate(nvmem, layout_dn);
562 + device_links_supplier_sync_state_resume();
563 +
564 + of_node_put(layout_dn);
565 + return ret;
566 +}
567 +
568 +void nvmem_destroy_layout(struct nvmem_device *nvmem)
569 +{
570 + struct device *dev;
571 +
572 + if (!nvmem->layout)
573 + return;
574 +
575 + dev = &nvmem->layout->dev;
576 + of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
577 + device_unregister(dev);
578 +}
579 +
580 +int nvmem_layout_bus_register(void)
581 +{
582 + return bus_register(&nvmem_layout_bus_type);
583 +}
584 +
585 +void nvmem_layout_bus_unregister(void)
586 +{
587 + bus_unregister(&nvmem_layout_bus_type);
588 +}
589 --- a/drivers/nvmem/layouts/Kconfig
590 +++ b/drivers/nvmem/layouts/Kconfig
591 @@ -1,5 +1,11 @@
592 # SPDX-License-Identifier: GPL-2.0
593
594 +config NVMEM_LAYOUTS
595 + bool
596 + depends on OF
597 +
598 +if NVMEM_LAYOUTS
599 +
600 menu "Layout Types"
601
602 config NVMEM_LAYOUT_SL28_VPD
603 @@ -21,3 +27,5 @@ config NVMEM_LAYOUT_ONIE_TLV
604 If unsure, say N.
605
606 endmenu
607 +
608 +endif
609 --- a/drivers/nvmem/layouts/onie-tlv.c
610 +++ b/drivers/nvmem/layouts/onie-tlv.c
611 @@ -225,16 +225,32 @@ static int onie_tlv_parse_table(struct d
612 return 0;
613 }
614
615 +static int onie_tlv_probe(struct nvmem_layout *layout)
616 +{
617 + layout->add_cells = onie_tlv_parse_table;
618 +
619 + return nvmem_layout_register(layout);
620 +}
621 +
622 +static void onie_tlv_remove(struct nvmem_layout *layout)
623 +{
624 + nvmem_layout_unregister(layout);
625 +}
626 +
627 static const struct of_device_id onie_tlv_of_match_table[] = {
628 { .compatible = "onie,tlv-layout", },
629 {},
630 };
631 MODULE_DEVICE_TABLE(of, onie_tlv_of_match_table);
632
633 -static struct nvmem_layout onie_tlv_layout = {
634 - .name = "ONIE tlv layout",
635 - .of_match_table = onie_tlv_of_match_table,
636 - .add_cells = onie_tlv_parse_table,
637 +static struct nvmem_layout_driver onie_tlv_layout = {
638 + .driver = {
639 + .owner = THIS_MODULE,
640 + .name = "onie-tlv-layout",
641 + .of_match_table = onie_tlv_of_match_table,
642 + },
643 + .probe = onie_tlv_probe,
644 + .remove = onie_tlv_remove,
645 };
646 module_nvmem_layout_driver(onie_tlv_layout);
647
648 --- a/drivers/nvmem/layouts/sl28vpd.c
649 +++ b/drivers/nvmem/layouts/sl28vpd.c
650 @@ -134,16 +134,32 @@ static int sl28vpd_add_cells(struct devi
651 return 0;
652 }
653
654 +static int sl28vpd_probe(struct nvmem_layout *layout)
655 +{
656 + layout->add_cells = sl28vpd_add_cells;
657 +
658 + return nvmem_layout_register(layout);
659 +}
660 +
661 +static void sl28vpd_remove(struct nvmem_layout *layout)
662 +{
663 + nvmem_layout_unregister(layout);
664 +}
665 +
666 static const struct of_device_id sl28vpd_of_match_table[] = {
667 { .compatible = "kontron,sl28-vpd" },
668 {},
669 };
670 MODULE_DEVICE_TABLE(of, sl28vpd_of_match_table);
671
672 -static struct nvmem_layout sl28vpd_layout = {
673 - .name = "sl28-vpd",
674 - .of_match_table = sl28vpd_of_match_table,
675 - .add_cells = sl28vpd_add_cells,
676 +static struct nvmem_layout_driver sl28vpd_layout = {
677 + .driver = {
678 + .owner = THIS_MODULE,
679 + .name = "kontron-sl28vpd-layout",
680 + .of_match_table = sl28vpd_of_match_table,
681 + },
682 + .probe = sl28vpd_probe,
683 + .remove = sl28vpd_remove,
684 };
685 module_nvmem_layout_driver(sl28vpd_layout);
686
687 --- a/include/linux/nvmem-provider.h
688 +++ b/include/linux/nvmem-provider.h
689 @@ -9,6 +9,7 @@
690 #ifndef _LINUX_NVMEM_PROVIDER_H
691 #define _LINUX_NVMEM_PROVIDER_H
692
693 +#include <linux/device.h>
694 #include <linux/device/driver.h>
695 #include <linux/err.h>
696 #include <linux/errno.h>
697 @@ -158,12 +159,11 @@ struct nvmem_cell_table {
698 /**
699 * struct nvmem_layout - NVMEM layout definitions
700 *
701 - * @name: Layout name.
702 - * @of_match_table: Open firmware match table.
703 - * @add_cells: Called to populate the layout using
704 - * nvmem_add_one_cell().
705 - * @owner: Pointer to struct module.
706 - * @node: List node.
707 + * @dev: Device-model layout device.
708 + * @nvmem: The underlying NVMEM device
709 + * @add_cells: Will be called if a nvmem device is found which
710 + * has this layout. The function will add layout
711 + * specific cells with nvmem_add_one_cell().
712 *
713 * A nvmem device can hold a well defined structure which can just be
714 * evaluated during runtime. For example a TLV list, or a list of "name=val"
715 @@ -171,13 +171,15 @@ struct nvmem_cell_table {
716 * cells.
717 */
718 struct nvmem_layout {
719 - const char *name;
720 - const struct of_device_id *of_match_table;
721 + struct device dev;
722 + struct nvmem_device *nvmem;
723 int (*add_cells)(struct device *dev, struct nvmem_device *nvmem);
724 +};
725
726 - /* private */
727 - struct module *owner;
728 - struct list_head node;
729 +struct nvmem_layout_driver {
730 + struct device_driver driver;
731 + int (*probe)(struct nvmem_layout *layout);
732 + void (*remove)(struct nvmem_layout *layout);
733 };
734
735 #if IS_ENABLED(CONFIG_NVMEM)
736 @@ -194,11 +196,15 @@ void nvmem_del_cell_table(struct nvmem_c
737 int nvmem_add_one_cell(struct nvmem_device *nvmem,
738 const struct nvmem_cell_info *info);
739
740 -int __nvmem_layout_register(struct nvmem_layout *layout, struct module *owner);
741 -#define nvmem_layout_register(layout) \
742 - __nvmem_layout_register(layout, THIS_MODULE)
743 +int nvmem_layout_register(struct nvmem_layout *layout);
744 void nvmem_layout_unregister(struct nvmem_layout *layout);
745
746 +int nvmem_layout_driver_register(struct nvmem_layout_driver *drv);
747 +void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv);
748 +#define module_nvmem_layout_driver(__nvmem_layout_driver) \
749 + module_driver(__nvmem_layout_driver, nvmem_layout_driver_register, \
750 + nvmem_layout_driver_unregister)
751 +
752 const void *nvmem_layout_get_match_data(struct nvmem_device *nvmem,
753 struct nvmem_layout *layout);
754
755 @@ -262,8 +268,4 @@ static inline struct device_node *of_nvm
756
757 #endif /* CONFIG_NVMEM && CONFIG_OF */
758
759 -#define module_nvmem_layout_driver(__layout_driver) \
760 - module_driver(__layout_driver, nvmem_layout_register, \
761 - nvmem_layout_unregister)
762 -
763 #endif /* ifndef _LINUX_NVMEM_PROVIDER_H */