dm: core: Split out alloc code into a new function

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>
This commit is contained in:
Simon Glass
2020-12-19 10:40:08 -07:00
parent e7144b07e4
commit 82021e31a7

View File

@@ -323,11 +323,57 @@ static void *alloc_priv(int size, uint flags)
return priv;
}
/**
* 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;
void *ptr;
int size;
drv = dev->driver;
assert(drv);
/* 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)
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)
return -ENOMEM;
dev_set_uclass_priv(dev, ptr);
}
/* Allocate parent data for this child */
if (dev->parent) {
size = dev->parent->driver->per_child_auto;
if (!size)
size = dev->parent->uclass->uc_drv->per_child_auto;
if (size && !dev_get_parent_priv(dev)) {
ptr = alloc_priv(size, drv->flags);
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 size = 0;
void *ptr;
int ret;
if (!dev)
@@ -352,44 +398,13 @@ int device_of_to_plat(struct udevice *dev)
return 0;
}
ret = device_alloc_priv(dev);
if (ret)
goto fail;
drv = dev->driver;
assert(drv);
/* 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;
}
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;
}
dev_set_uclass_priv(dev, ptr);
}
/* Allocate parent data for this child */
if (dev->parent) {
size = dev->parent->driver->per_child_auto;
if (!size)
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;
}
dev_set_parent_priv(dev, ptr);
}
}
if (drv->of_to_plat &&
(CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
ret = drv->of_to_plat(dev);