[PATCH 16/26] dm: core: Split out alloc code into a new function

Simon Glass
2020-12-19T17:40:08Z

Add a new function to handle the allocation of private/platform data for
a device. This will make it easier to skip this feature when using the new
of-platdata.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

drivers/core/device.c | 89 +++++++++++++++++++++++++------------------
1 file changed, 52 insertions(+), 37 deletions(-)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index a4c8310f812..72169632c88 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -323,34 +323,17 @@ static void *alloc_priv(int size, uint flags)
return priv;
}

-int device_of_to_plat(struct udevice *dev)
+/**
+ * device_alloc_priv() - Allocate priv/plat data required by the device
+ *
+ * @dev: Device to process
+ * @return 0 if OK, -ENOMEM if out of memory
+ */
+static int device_alloc_priv(struct udevice *dev)
{
const struct driver *drv;
- int size = 0;
void *ptr;
- int ret;
-
- if (!dev)
- return -EINVAL;
-
- if (dev->flags & DM_FLAG_PLATDATA_VALID)
- return 0;
-
- /* Ensure all parents have ofdata */
- if (dev->parent) {
- ret = device_of_to_plat(dev->parent);
- if (ret)
- goto fail;
-
- /*
- * The device might have already been probed during
- * the call to device_probe() on its parent device
- * (e.g. PCI bridge devices). Test the flags again
- * so that we don't mess up the device.
- */
- if (dev->flags & DM_FLAG_PLATDATA_VALID)
- return 0;
- }
+ int size;

drv = dev->driver;
assert(drv);
@@ -358,20 +341,17 @@ int device_of_to_plat(struct udevice *dev)
/* Allocate private data if requested and not reentered */
if (drv->priv_auto && !dev_get_priv(dev)) {
ptr = alloc_priv(drv->priv_auto, drv->flags);
- if (!ptr) {
- ret = -ENOMEM;
- goto fail;
- }
+ if (!ptr)
+ return -ENOMEM;
dev_set_priv(dev, ptr);
}
+
/* Allocate private data if requested and not reentered */
size = dev->uclass->uc_drv->per_device_auto;
if (size && !dev_get_uclass_priv(dev)) {
ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
- if (!ptr) {
- ret = -ENOMEM;
- goto fail;
- }
+ if (!ptr)
+ return -ENOMEM;
dev_set_uclass_priv(dev, ptr);
}

@@ -382,14 +362,49 @@ int device_of_to_plat(struct udevice *dev)
size = dev->parent->uclass->uc_drv->per_child_auto;
if (size && !dev_get_parent_priv(dev)) {
ptr = alloc_priv(size, drv->flags);
- if (!ptr) {
- ret = -ENOMEM;
- goto fail;
- }
+ if (!ptr)
+ return -ENOMEM;
dev_set_parent_priv(dev, ptr);
}
}

+ return 0;
+}
+
+int device_of_to_plat(struct udevice *dev)
+{
+ const struct driver *drv;
+ int ret;
+
+ if (!dev)
+ return -EINVAL;
+
+ if (dev->flags & DM_FLAG_PLATDATA_VALID)
+ return 0;
+
+ /* Ensure all parents have ofdata */
+ if (dev->parent) {
+ ret = device_of_to_plat(dev->parent);
+ if (ret)
+ goto fail;
+
+ /*
+ * The device might have already been probed during
+ * the call to device_probe() on its parent device
+ * (e.g. PCI bridge devices). Test the flags again
+ * so that we don't mess up the device.
+ */
+ if (dev->flags & DM_FLAG_PLATDATA_VALID)
+ return 0;
+ }
+
+ ret = device_alloc_priv(dev);
+ if (ret)
+ goto fail;
+
+ drv = dev->driver;
+ assert(drv);
+
if (drv->of_to_plat &&
(CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
ret = drv->of_to_plat(dev);
--
2.29.2.684.gfbc64c5ab5-goog