| 1 | |
| 2 | |
| 3 | * NVMEM layout bus handling
|
| 4 | *
|
| 5 | * Copyright (C) 2023 Bootlin
|
| 6 | * Author: Miquel Raynal <[email protected]
|
| 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 |
|
| 24 | static int nvmem_layout_bus_match(struct device *dev, const struct device_driver *drv)
|
| 25 | {
|
| 26 | return of_driver_match_device(dev, drv);
|
| 27 | }
|
| 28 |
|
| 29 | static 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 |
|
| 40 | static 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 |
|
| 48 | static 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 |
|
| 60 | static 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 |
|
| 68 | int __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 | }
|
| 76 | EXPORT_SYMBOL_GPL(__nvmem_layout_driver_register);
|
| 77 |
|
| 78 | void nvmem_layout_driver_unregister(struct nvmem_layout_driver *drv)
|
| 79 | {
|
| 80 | driver_unregister(&drv->driver);
|
| 81 | }
|
| 82 | EXPORT_SYMBOL_GPL(nvmem_layout_driver_unregister);
|
| 83 |
|
| 84 | static 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 |
|
| 92 | static 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_obj(*layout);
|
| 100 | if (!layout)
|
| 101 | return -ENOMEM;
|
| 102 |
|
| 103 |
|
| 104 | layout->nvmem = nvmem;
|
| 105 | nvmem->layout = layout;
|
| 106 |
|
| 107 |
|
| 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 |
|
| 128 | static int nvmem_layout_bus_populate(struct nvmem_device *nvmem,
|
| 129 | struct device_node *layout_dn)
|
| 130 | {
|
| 131 | int ret;
|
| 132 |
|
| 133 |
|
| 134 | if (!of_property_present(layout_dn, "compatible")) {
|
| 135 | pr_debug("%s() - skipping %pOF, no compatible prop\n",
|
| 136 | __func__, layout_dn);
|
| 137 | return 0;
|
| 138 | }
|
| 139 |
|
| 140 | if (of_node_check_flag(layout_dn, OF_POPULATED_BUS)) {
|
| 141 | pr_debug("%s() - skipping %pOF, already populated\n",
|
| 142 | __func__, layout_dn);
|
| 143 |
|
| 144 | return 0;
|
| 145 | }
|
| 146 |
|
| 147 |
|
| 148 | ret = nvmem_layout_create_device(nvmem, layout_dn);
|
| 149 | if (ret)
|
| 150 | return ret;
|
| 151 |
|
| 152 | of_node_set_flag(layout_dn, OF_POPULATED_BUS);
|
| 153 |
|
| 154 | return 0;
|
| 155 | }
|
| 156 |
|
| 157 | struct device_node *of_nvmem_layout_get_container(struct nvmem_device *nvmem)
|
| 158 | {
|
| 159 | return of_get_child_by_name(nvmem->dev.of_node, "nvmem-layout");
|
| 160 | }
|
| 161 | EXPORT_SYMBOL_GPL(of_nvmem_layout_get_container);
|
| 162 |
|
| 163 | |
| 164 | * Returns the number of devices populated, 0 if the operation was not relevant
|
| 165 | * for this nvmem device, an error code otherwise.
|
| 166 |
|
| 167 | int nvmem_populate_layout(struct nvmem_device *nvmem)
|
| 168 | {
|
| 169 | struct device_node *layout_dn;
|
| 170 | int ret;
|
| 171 |
|
| 172 | layout_dn = of_nvmem_layout_get_container(nvmem);
|
| 173 | if (!layout_dn)
|
| 174 | return 0;
|
| 175 |
|
| 176 |
|
| 177 | device_links_supplier_sync_state_pause();
|
| 178 | ret = nvmem_layout_bus_populate(nvmem, layout_dn);
|
| 179 | device_links_supplier_sync_state_resume();
|
| 180 |
|
| 181 | of_node_put(layout_dn);
|
| 182 | return ret;
|
| 183 | }
|
| 184 |
|
| 185 | void nvmem_destroy_layout(struct nvmem_device *nvmem)
|
| 186 | {
|
| 187 | struct device *dev;
|
| 188 |
|
| 189 | if (!nvmem->layout)
|
| 190 | return;
|
| 191 |
|
| 192 | dev = &nvmem->layout->dev;
|
| 193 | of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
|
| 194 | device_unregister(dev);
|
| 195 | }
|
| 196 |
|
| 197 | int nvmem_layout_bus_register(void)
|
| 198 | {
|
| 199 | return bus_register(&nvmem_layout_bus_type);
|
| 200 | }
|
| 201 |
|
| 202 | void nvmem_layout_bus_unregister(void)
|
| 203 | {
|
| 204 | bus_unregister(&nvmem_layout_bus_type);
|
| 205 | }
|