Skip to content

Commit

Permalink
ARM: amba: add amba_device allocation/add/put functions
Browse files Browse the repository at this point in the history
Add functions to allocate and initialize AMBA device structures, and
add them to the Linux device manager.  This allows us to kill this
type of operation from individual platforms, moving it to core code.

Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
  • Loading branch information
Russell King committed Jan 25, 2012
1 parent dffcb9c commit d5dc927
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 25 deletions.
100 changes: 75 additions & 25 deletions drivers/amba/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,38 +497,20 @@ static void amba_device_release(struct device *dev)
}

/**
* amba_device_register - register an AMBA device
* @dev: AMBA device to register
* @parent: parent memory resource
* amba_device_add - add a previously allocated AMBA device structure
* @dev: AMBA device allocated by amba_device_alloc
* @parent: resource parent for this devices resources
*
* Setup the AMBA device, reading the cell ID if present.
* Claim the resource, and register the AMBA device with
* the Linux device manager.
* Claim the resource, and read the device cell ID if not already
* initialized. Register the AMBA device with the Linux device
* manager.
*/
int amba_device_register(struct amba_device *dev, struct resource *parent)
int amba_device_add(struct amba_device *dev, struct resource *parent)
{
u32 size;
void __iomem *tmp;
int i, ret;

device_initialize(&dev->dev);

/*
* Copy from device_add
*/
if (dev->dev.init_name) {
dev_set_name(&dev->dev, "%s", dev->dev.init_name);
dev->dev.init_name = NULL;
}

dev->dev.release = amba_device_release;
dev->dev.bus = &amba_bustype;
dev->dev.dma_mask = &dev->dma_mask;
dev->res.name = dev_name(&dev->dev);

if (!dev->dev.coherent_dma_mask && dev->dma_mask)
dev_warn(&dev->dev, "coherent dma mask is unset\n");

ret = request_resource(parent, &dev->res);
if (ret)
goto err_out;
Expand Down Expand Up @@ -596,6 +578,74 @@ int amba_device_register(struct amba_device *dev, struct resource *parent)
err_out:
return ret;
}
EXPORT_SYMBOL_GPL(amba_device_add);

static void amba_device_initialize(struct amba_device *dev, const char *name)
{
device_initialize(&dev->dev);
if (name)
dev_set_name(&dev->dev, "%s", name);
dev->dev.release = amba_device_release;
dev->dev.bus = &amba_bustype;
dev->dev.dma_mask = &dev->dma_mask;
dev->res.name = dev_name(&dev->dev);
}

/**
* amba_device_alloc - allocate an AMBA device
* @name: sysfs name of the AMBA device
* @base: base of AMBA device
* @size: size of AMBA device
*
* Allocate and initialize an AMBA device structure. Returns %NULL
* on failure.
*/
struct amba_device *amba_device_alloc(const char *name, resource_size_t base,
size_t size)
{
struct amba_device *dev;

dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (dev) {
amba_device_initialize(dev, name);
dev->res.start = base;
dev->res.end = base + size - 1;
dev->res.flags = IORESOURCE_MEM;
}

return dev;
}
EXPORT_SYMBOL_GPL(amba_device_alloc);

/**
* amba_device_register - register an AMBA device
* @dev: AMBA device to register
* @parent: parent memory resource
*
* Setup the AMBA device, reading the cell ID if present.
* Claim the resource, and register the AMBA device with
* the Linux device manager.
*/
int amba_device_register(struct amba_device *dev, struct resource *parent)
{
amba_device_initialize(dev, dev->dev.init_name);
dev->dev.init_name = NULL;

if (!dev->dev.coherent_dma_mask && dev->dma_mask)
dev_warn(&dev->dev, "coherent dma mask is unset\n");

return amba_device_add(dev, parent);
}

/**
* amba_device_put - put an AMBA device
* @dev: AMBA device to put
*/
void amba_device_put(struct amba_device *dev)
{
put_device(&dev->dev);
}
EXPORT_SYMBOL_GPL(amba_device_put);

/**
* amba_device_unregister - unregister an AMBA device
Expand Down
3 changes: 3 additions & 0 deletions include/linux/amba/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ extern struct bus_type amba_bustype;

int amba_driver_register(struct amba_driver *);
void amba_driver_unregister(struct amba_driver *);
struct amba_device *amba_device_alloc(const char *, resource_size_t, size_t);
void amba_device_put(struct amba_device *);
int amba_device_add(struct amba_device *, struct resource *);
int amba_device_register(struct amba_device *, struct resource *);
void amba_device_unregister(struct amba_device *);
struct amba_device *amba_find_device(const char *, struct device *, unsigned int, unsigned int);
Expand Down

0 comments on commit d5dc927

Please sign in to comment.