1 // SPDX-License-Identifier: GPL-2.0
3 * NVMEM layout bus handling
5 * Copyright (C) 2023 Bootlin
6 * Author: Miquel Raynal <miquel.raynal@bootlin.com
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/nvmem-consumer.h>
12 #include <linux/nvmem-provider.h>
14 #include <linux/of_device.h>
15 #include <linux/of_irq.h>
17 #include "internals.h"
19 #define to_nvmem_layout_driver(drv) \
20 (container_of_const((drv), struct nvmem_layout_driver, driver))
21 #define to_nvmem_layout_device(_dev) \
22 container_of((_dev), struct nvmem_layout, dev)
24 static int nvmem_layout_bus_match(struct device
*dev
, const struct device_driver
*drv
)
26 return of_driver_match_device(dev
, drv
);
29 static int nvmem_layout_bus_probe(struct device
*dev
)
31 struct nvmem_layout_driver
*drv
= to_nvmem_layout_driver(dev
->driver
);
32 struct nvmem_layout
*layout
= to_nvmem_layout_device(dev
);
34 if (!drv
->probe
|| !drv
->remove
)
37 return drv
->probe(layout
);
40 static void nvmem_layout_bus_remove(struct device
*dev
)
42 struct nvmem_layout_driver
*drv
= to_nvmem_layout_driver(dev
->driver
);
43 struct nvmem_layout
*layout
= to_nvmem_layout_device(dev
);
45 return drv
->remove(layout
);
48 static const struct bus_type nvmem_layout_bus_type
= {
49 .name
= "nvmem-layout",
50 .match
= nvmem_layout_bus_match
,
51 .probe
= nvmem_layout_bus_probe
,
52 .remove
= nvmem_layout_bus_remove
,
55 int __nvmem_layout_driver_register(struct nvmem_layout_driver
*drv
,
58 drv
->driver
.bus
= &nvmem_layout_bus_type
;
59 drv
->driver
.owner
= owner
;
61 return driver_register(&drv
->driver
);
63 EXPORT_SYMBOL_GPL(__nvmem_layout_driver_register
);
65 void nvmem_layout_driver_unregister(struct nvmem_layout_driver
*drv
)
67 driver_unregister(&drv
->driver
);
69 EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister
);
71 static void nvmem_layout_release_device(struct device
*dev
)
73 struct nvmem_layout
*layout
= to_nvmem_layout_device(dev
);
75 of_node_put(layout
->dev
.of_node
);
79 static int nvmem_layout_create_device(struct nvmem_device
*nvmem
,
80 struct device_node
*np
)
82 struct nvmem_layout
*layout
;
86 layout
= kzalloc(sizeof(*layout
), GFP_KERNEL
);
90 /* Create a bidirectional link */
91 layout
->nvmem
= nvmem
;
92 nvmem
->layout
= layout
;
94 /* Device model registration */
96 device_initialize(dev
);
97 dev
->parent
= &nvmem
->dev
;
98 dev
->bus
= &nvmem_layout_bus_type
;
99 dev
->release
= nvmem_layout_release_device
;
100 dev
->coherent_dma_mask
= DMA_BIT_MASK(32);
101 dev
->dma_mask
= &dev
->coherent_dma_mask
;
102 device_set_node(dev
, of_fwnode_handle(of_node_get(np
)));
103 of_device_make_bus_id(dev
);
104 of_msi_configure(dev
, dev
->of_node
);
106 ret
= device_add(dev
);
115 static const struct of_device_id of_nvmem_layout_skip_table
[] = {
116 { .compatible
= "fixed-layout", },
120 static int nvmem_layout_bus_populate(struct nvmem_device
*nvmem
,
121 struct device_node
*layout_dn
)
125 /* Make sure it has a compatible property */
126 if (!of_property_present(layout_dn
, "compatible")) {
127 pr_debug("%s() - skipping %pOF, no compatible prop\n",
128 __func__
, layout_dn
);
132 /* Fixed layouts are parsed manually somewhere else for now */
133 if (of_match_node(of_nvmem_layout_skip_table
, layout_dn
)) {
134 pr_debug("%s() - skipping %pOF node\n", __func__
, layout_dn
);
138 if (of_node_check_flag(layout_dn
, OF_POPULATED_BUS
)) {
139 pr_debug("%s() - skipping %pOF, already populated\n",
140 __func__
, layout_dn
);
145 /* NVMEM layout buses expect only a single device representing the layout */
146 ret
= nvmem_layout_create_device(nvmem
, layout_dn
);
150 of_node_set_flag(layout_dn
, OF_POPULATED_BUS
);
155 struct device_node
*of_nvmem_layout_get_container(struct nvmem_device
*nvmem
)
157 return of_get_child_by_name(nvmem
->dev
.of_node
, "nvmem-layout");
159 EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container
);
162 * Returns the number of devices populated, 0 if the operation was not relevant
163 * for this nvmem device, an error code otherwise.
165 int nvmem_populate_layout(struct nvmem_device
*nvmem
)
167 struct device_node
*layout_dn
;
170 layout_dn
= of_nvmem_layout_get_container(nvmem
);
174 /* Populate the layout device */
175 device_links_supplier_sync_state_pause();
176 ret
= nvmem_layout_bus_populate(nvmem
, layout_dn
);
177 device_links_supplier_sync_state_resume();
179 of_node_put(layout_dn
);
183 void nvmem_destroy_layout(struct nvmem_device
*nvmem
)
190 dev
= &nvmem
->layout
->dev
;
191 of_node_clear_flag(dev
->of_node
, OF_POPULATED_BUS
);
192 device_unregister(dev
);
195 int nvmem_layout_bus_register(void)
197 return bus_register(&nvmem_layout_bus_type
);
200 void nvmem_layout_bus_unregister(void)
202 bus_unregister(&nvmem_layout_bus_type
);