Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * NVMEM layout bus handling
4 *
5 * Copyright (C) 2023 Bootlin
6 * Author: Miquel Raynal <miquel.raynal@bootlin.com
7 */
8
9#include <linux/device.h>
10#include <linux/dma-mapping.h>
11#include <linux/nvmem-consumer.h>
12#include <linux/nvmem-provider.h>
13#include <linux/of.h>
14#include <linux/of_device.h>
15#include <linux/of_irq.h>
16
17#include "internals.h"
18
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)
23
24static int nvmem_layout_bus_match(struct device *dev, const struct device_driver *drv)
25{
26 return of_driver_match_device(dev, drv);
27}
28
29static int nvmem_layout_bus_probe(struct device *dev)
30{
31 struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver);
32 struct nvmem_layout *layout = to_nvmem_layout_device(dev);
33
34 if (!drv->probe || !drv->remove)
35 return -EINVAL;
36
37 return drv->probe(layout);
38}
39
40static void nvmem_layout_bus_remove(struct device *dev)
41{
42 struct nvmem_layout_driver *drv = to_nvmem_layout_driver(dev->driver);
43 struct nvmem_layout *layout = to_nvmem_layout_device(dev);
44
45 return drv->remove(layout);
46}
47
48static int nvmem_layout_bus_uevent(const struct device *dev,
49 struct kobj_uevent_env *env)
50{
51 int ret;
52
53 ret = of_device_uevent_modalias(dev, env);
54 if (ret != -ENODEV)
55 return ret;
56
57 return 0;
58}
59
60static const struct bus_type nvmem_layout_bus_type = {
61 .name = "nvmem-layout",
62 .match = nvmem_layout_bus_match,
63 .probe = nvmem_layout_bus_probe,
64 .remove = nvmem_layout_bus_remove,
65 .uevent = nvmem_layout_bus_uevent,
66};
67
68int __nvmem_layout_driver_register(struct nvmem_layout_driver *drv,
69 struct module *owner)
70{
71 drv->driver.bus = &nvmem_layout_bus_type;
72 drv->driver.owner = owner;
73
74 return driver_register(&drv->driver);
75}
76EXPORT_SYMBOL_GPL(__nvmem_layout_driver_register);
77
78void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv)
79{
80 driver_unregister(&drv->driver);
81}
82EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister);
83
84static void nvmem_layout_release_device(struct device *dev)
85{
86 struct nvmem_layout *layout = to_nvmem_layout_device(dev);
87
88 of_node_put(layout->dev.of_node);
89 kfree(layout);
90}
91
92static int nvmem_layout_create_device(struct nvmem_device *nvmem,
93 struct device_node *np)
94{
95 struct nvmem_layout *layout;
96 struct device *dev;
97 int ret;
98
99 layout = kzalloc(sizeof(*layout), GFP_KERNEL);
100 if (!layout)
101 return -ENOMEM;
102
103 /* Create a bidirectional link */
104 layout->nvmem = nvmem;
105 nvmem->layout = layout;
106
107 /* Device model registration */
108 dev = &layout->dev;
109 device_initialize(dev);
110 dev->parent = &nvmem->dev;
111 dev->bus = &nvmem_layout_bus_type;
112 dev->release = nvmem_layout_release_device;
113 dev->coherent_dma_mask = DMA_BIT_MASK(32);
114 dev->dma_mask = &dev->coherent_dma_mask;
115 device_set_node(dev, of_fwnode_handle(of_node_get(np)));
116 of_device_make_bus_id(dev);
117 of_msi_configure(dev, dev->of_node);
118
119 ret = device_add(dev);
120 if (ret) {
121 put_device(dev);
122 return ret;
123 }
124
125 return 0;
126}
127
128static const struct of_device_id of_nvmem_layout_skip_table[] = {
129 { .compatible = "fixed-layout", },
130 {}
131};
132
133static int nvmem_layout_bus_populate(struct nvmem_device *nvmem,
134 struct device_node *layout_dn)
135{
136 int ret;
137
138 /* Make sure it has a compatible property */
139 if (!of_property_present(layout_dn, "compatible")) {
140 pr_debug("%s() - skipping %pOF, no compatible prop\n",
141 __func__, layout_dn);
142 return 0;
143 }
144
145 /* Fixed layouts are parsed manually somewhere else for now */
146 if (of_match_node(of_nvmem_layout_skip_table, layout_dn)) {
147 pr_debug("%s() - skipping %pOF node\n", __func__, layout_dn);
148 return 0;
149 }
150
151 if (of_node_check_flag(layout_dn, OF_POPULATED_BUS)) {
152 pr_debug("%s() - skipping %pOF, already populated\n",
153 __func__, layout_dn);
154
155 return 0;
156 }
157
158 /* NVMEM layout buses expect only a single device representing the layout */
159 ret = nvmem_layout_create_device(nvmem, layout_dn);
160 if (ret)
161 return ret;
162
163 of_node_set_flag(layout_dn, OF_POPULATED_BUS);
164
165 return 0;
166}
167
168struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
169{
170 return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
171}
172EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
173
174/*
175 * Returns the number of devices populated, 0 if the operation was not relevant
176 * for this nvmem device, an error code otherwise.
177 */
178int nvmem_populate_layout(struct nvmem_device *nvmem)
179{
180 struct device_node *layout_dn;
181 int ret;
182
183 layout_dn = of_nvmem_layout_get_container(nvmem);
184 if (!layout_dn)
185 return 0;
186
187 /* Populate the layout device */
188 device_links_supplier_sync_state_pause();
189 ret = nvmem_layout_bus_populate(nvmem, layout_dn);
190 device_links_supplier_sync_state_resume();
191
192 of_node_put(layout_dn);
193 return ret;
194}
195
196void nvmem_destroy_layout(struct nvmem_device *nvmem)
197{
198 struct device *dev;
199
200 if (!nvmem->layout)
201 return;
202
203 dev = &nvmem->layout->dev;
204 of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
205 device_unregister(dev);
206}
207
208int nvmem_layout_bus_register(void)
209{
210 return bus_register(&nvmem_layout_bus_type);
211}
212
213void nvmem_layout_bus_unregister(void)
214{
215 bus_unregister(&nvmem_layout_bus_type);
216}