mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-12 13:55:32 +00:00
![Andres Salomon](/assets/img/avatar_default.png)
This adds functions to enable platform_device sharing for mfd clients. Each platform driver (mfd client) that wants to share an mfd_cell's platform_device uses the mfd_shared_platform_driver_{un,}register() functions instead of platform_driver_{un,}register(). Along with registering the platform driver, these also register a new platform device with the same characteristics as the original cell, but a different name. Given an mfd_cell with the name "foo", drivers that want to share access to its resources can call mfd_shared_platform_driver_register with platform drivers named (for example) "bar" and "baz". This will register two platform devices and drivers named "bar" and "baz" that share the same cell as the platform device "foo". The drivers can then call "foo" cell's enable hooks (or mfd_shared_cell_enable) to enable resources, and obtain platform resources as they normally would. This deals with platform handling only; mfd driver-specific details, hardware handling, refcounting, etc are all dealt with separately. Signed-off-by: Andres Salomon <dilinger@queued.net> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
249 lines
6 KiB
C
249 lines
6 KiB
C
/*
|
|
* drivers/mfd/mfd-core.c
|
|
*
|
|
* core MFD support
|
|
* Copyright (c) 2006 Ian Molton
|
|
* Copyright (c) 2007,2008 Dmitry Baryshkov
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/acpi.h>
|
|
#include <linux/mfd/core.h>
|
|
#include <linux/pm_runtime.h>
|
|
#include <linux/slab.h>
|
|
|
|
int mfd_shared_cell_enable(struct platform_device *pdev)
|
|
{
|
|
const struct mfd_cell *cell = mfd_get_cell(pdev);
|
|
int err = 0;
|
|
|
|
/* only call enable hook if the cell wasn't previously enabled */
|
|
if (atomic_inc_return(cell->usage_count) == 1)
|
|
err = cell->enable(pdev);
|
|
|
|
/* if the enable hook failed, decrement counter to allow retries */
|
|
if (err)
|
|
atomic_dec(cell->usage_count);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL(mfd_shared_cell_enable);
|
|
|
|
int mfd_shared_cell_disable(struct platform_device *pdev)
|
|
{
|
|
const struct mfd_cell *cell = mfd_get_cell(pdev);
|
|
int err = 0;
|
|
|
|
/* only disable if no other clients are using it */
|
|
if (atomic_dec_return(cell->usage_count) == 0)
|
|
err = cell->disable(pdev);
|
|
|
|
/* if the disable hook failed, increment to allow retries */
|
|
if (err)
|
|
atomic_inc(cell->usage_count);
|
|
|
|
/* sanity check; did someone call disable too many times? */
|
|
WARN_ON(atomic_read(cell->usage_count) < 0);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL(mfd_shared_cell_disable);
|
|
|
|
static int mfd_add_device(struct device *parent, int id,
|
|
const struct mfd_cell *cell,
|
|
struct resource *mem_base,
|
|
int irq_base)
|
|
{
|
|
struct resource *res;
|
|
struct platform_device *pdev;
|
|
int ret = -ENOMEM;
|
|
int r;
|
|
|
|
pdev = platform_device_alloc(cell->name, id + cell->id);
|
|
if (!pdev)
|
|
goto fail_alloc;
|
|
|
|
res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
|
|
if (!res)
|
|
goto fail_device;
|
|
|
|
pdev->dev.parent = parent;
|
|
|
|
ret = platform_device_add_data(pdev, cell, sizeof(*cell));
|
|
if (ret)
|
|
goto fail_res;
|
|
|
|
for (r = 0; r < cell->num_resources; r++) {
|
|
res[r].name = cell->resources[r].name;
|
|
res[r].flags = cell->resources[r].flags;
|
|
|
|
/* Find out base to use */
|
|
if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
|
|
res[r].parent = mem_base;
|
|
res[r].start = mem_base->start +
|
|
cell->resources[r].start;
|
|
res[r].end = mem_base->start +
|
|
cell->resources[r].end;
|
|
} else if (cell->resources[r].flags & IORESOURCE_IRQ) {
|
|
res[r].start = irq_base +
|
|
cell->resources[r].start;
|
|
res[r].end = irq_base +
|
|
cell->resources[r].end;
|
|
} else {
|
|
res[r].parent = cell->resources[r].parent;
|
|
res[r].start = cell->resources[r].start;
|
|
res[r].end = cell->resources[r].end;
|
|
}
|
|
|
|
if (!cell->ignore_resource_conflicts) {
|
|
ret = acpi_check_resource_conflict(res);
|
|
if (ret)
|
|
goto fail_res;
|
|
}
|
|
}
|
|
|
|
ret = platform_device_add_resources(pdev, res, cell->num_resources);
|
|
if (ret)
|
|
goto fail_res;
|
|
|
|
ret = platform_device_add(pdev);
|
|
if (ret)
|
|
goto fail_res;
|
|
|
|
if (cell->pm_runtime_no_callbacks)
|
|
pm_runtime_no_callbacks(&pdev->dev);
|
|
|
|
kfree(res);
|
|
|
|
return 0;
|
|
|
|
/* platform_device_del(pdev); */
|
|
fail_res:
|
|
kfree(res);
|
|
fail_device:
|
|
platform_device_put(pdev);
|
|
fail_alloc:
|
|
return ret;
|
|
}
|
|
|
|
int mfd_add_devices(struct device *parent, int id,
|
|
struct mfd_cell *cells, int n_devs,
|
|
struct resource *mem_base,
|
|
int irq_base)
|
|
{
|
|
int i;
|
|
int ret = 0;
|
|
atomic_t *cnts;
|
|
|
|
/* initialize reference counting for all cells */
|
|
cnts = kcalloc(sizeof(*cnts), n_devs, GFP_KERNEL);
|
|
if (!cnts)
|
|
return -ENOMEM;
|
|
|
|
for (i = 0; i < n_devs; i++) {
|
|
atomic_set(&cnts[i], 0);
|
|
cells[i].usage_count = &cnts[i];
|
|
ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
|
|
if (ret)
|
|
break;
|
|
}
|
|
|
|
if (ret)
|
|
mfd_remove_devices(parent);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL(mfd_add_devices);
|
|
|
|
static int mfd_remove_devices_fn(struct device *dev, void *c)
|
|
{
|
|
struct platform_device *pdev = to_platform_device(dev);
|
|
const struct mfd_cell *cell = mfd_get_cell(pdev);
|
|
atomic_t **usage_count = c;
|
|
|
|
/* find the base address of usage_count pointers (for freeing) */
|
|
if (!*usage_count || (cell->usage_count < *usage_count))
|
|
*usage_count = cell->usage_count;
|
|
|
|
platform_device_unregister(pdev);
|
|
return 0;
|
|
}
|
|
|
|
void mfd_remove_devices(struct device *parent)
|
|
{
|
|
atomic_t *cnts = NULL;
|
|
|
|
device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
|
|
kfree(cnts);
|
|
}
|
|
EXPORT_SYMBOL(mfd_remove_devices);
|
|
|
|
static int add_shared_platform_device(const char *cell, const char *name)
|
|
{
|
|
struct mfd_cell cell_entry;
|
|
struct device *dev;
|
|
struct platform_device *pdev;
|
|
int err;
|
|
|
|
/* check if we've already registered a device (don't fail if we have) */
|
|
if (bus_find_device_by_name(&platform_bus_type, NULL, name))
|
|
return 0;
|
|
|
|
/* fetch the parent cell's device (should already be registered!) */
|
|
dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
|
|
if (!dev) {
|
|
printk(KERN_ERR "failed to find device for cell %s\n", cell);
|
|
return -ENODEV;
|
|
}
|
|
pdev = to_platform_device(dev);
|
|
memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
|
|
|
|
WARN_ON(!cell_entry.enable);
|
|
|
|
cell_entry.name = name;
|
|
err = mfd_add_device(pdev->dev.parent, -1, &cell_entry, NULL, 0);
|
|
if (err)
|
|
dev_err(dev, "MFD add devices failed: %d\n", err);
|
|
return err;
|
|
}
|
|
|
|
int mfd_shared_platform_driver_register(struct platform_driver *drv,
|
|
const char *cellname)
|
|
{
|
|
int err;
|
|
|
|
err = add_shared_platform_device(cellname, drv->driver.name);
|
|
if (err)
|
|
printk(KERN_ERR "failed to add platform device %s\n",
|
|
drv->driver.name);
|
|
|
|
err = platform_driver_register(drv);
|
|
if (err)
|
|
printk(KERN_ERR "failed to add platform driver %s\n",
|
|
drv->driver.name);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL(mfd_shared_platform_driver_register);
|
|
|
|
void mfd_shared_platform_driver_unregister(struct platform_driver *drv)
|
|
{
|
|
struct device *dev;
|
|
|
|
dev = bus_find_device_by_name(&platform_bus_type, NULL,
|
|
drv->driver.name);
|
|
if (dev)
|
|
platform_device_unregister(to_platform_device(dev));
|
|
|
|
platform_driver_unregister(drv);
|
|
}
|
|
EXPORT_SYMBOL(mfd_shared_platform_driver_unregister);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");
|