2008-06-27 17:37:19 +08:00
|
|
|
/*
|
|
|
|
* 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>
|
2010-02-19 18:07:59 +08:00
|
|
|
#include <linux/acpi.h>
|
2008-06-27 17:37:19 +08:00
|
|
|
#include <linux/mfd/core.h>
|
2010-11-27 01:19:34 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2011-07-04 03:13:27 +08:00
|
|
|
#include <linux/module.h>
|
2008-06-27 17:37:19 +08:00
|
|
|
|
2011-03-04 01:51:58 +08:00
|
|
|
int mfd_cell_enable(struct platform_device *pdev)
|
2011-02-18 11:07:34 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2011-03-04 01:51:58 +08:00
|
|
|
EXPORT_SYMBOL(mfd_cell_enable);
|
2011-02-18 11:07:34 +08:00
|
|
|
|
2011-03-04 01:51:58 +08:00
|
|
|
int mfd_cell_disable(struct platform_device *pdev)
|
2011-02-18 11:07:34 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2011-03-04 01:51:58 +08:00
|
|
|
EXPORT_SYMBOL(mfd_cell_disable);
|
2011-02-18 11:07:34 +08:00
|
|
|
|
2011-04-08 06:43:01 +08:00
|
|
|
static int mfd_platform_add_cell(struct platform_device *pdev,
|
|
|
|
const struct mfd_cell *cell)
|
|
|
|
{
|
|
|
|
if (!cell)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
|
|
|
|
if (!pdev->mfd_cell)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-29 07:30:26 +08:00
|
|
|
static int mfd_add_device(struct device *parent, int id,
|
2008-07-29 00:29:09 +08:00
|
|
|
const struct mfd_cell *cell,
|
|
|
|
struct resource *mem_base,
|
|
|
|
int irq_base)
|
2008-06-27 17:37:19 +08:00
|
|
|
{
|
2008-07-26 05:59:29 +08:00
|
|
|
struct resource *res;
|
2008-06-27 17:37:19 +08:00
|
|
|
struct platform_device *pdev;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
int r;
|
|
|
|
|
2009-07-27 21:45:51 +08:00
|
|
|
pdev = platform_device_alloc(cell->name, id + cell->id);
|
2008-06-27 17:37:19 +08:00
|
|
|
if (!pdev)
|
|
|
|
goto fail_alloc;
|
|
|
|
|
2008-07-26 05:59:29 +08:00
|
|
|
res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
|
|
|
|
if (!res)
|
|
|
|
goto fail_device;
|
|
|
|
|
2008-07-29 07:30:26 +08:00
|
|
|
pdev->dev.parent = parent;
|
2008-06-27 17:37:19 +08:00
|
|
|
|
2011-04-06 22:52:52 +08:00
|
|
|
if (cell->pdata_size) {
|
|
|
|
ret = platform_device_add_data(pdev,
|
|
|
|
cell->platform_data, cell->pdata_size);
|
|
|
|
if (ret)
|
|
|
|
goto fail_res;
|
|
|
|
}
|
|
|
|
|
2011-04-08 06:43:01 +08:00
|
|
|
ret = mfd_platform_add_cell(pdev, cell);
|
2011-02-18 11:07:09 +08:00
|
|
|
if (ret)
|
|
|
|
goto fail_res;
|
2008-06-27 17:37:19 +08:00
|
|
|
|
|
|
|
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 */
|
2010-03-26 08:09:04 +08:00
|
|
|
if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
|
2008-06-27 17:37:19 +08:00
|
|
|
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;
|
|
|
|
}
|
2010-02-19 18:07:59 +08:00
|
|
|
|
2010-10-01 04:55:36 +08:00
|
|
|
if (!cell->ignore_resource_conflicts) {
|
2012-02-19 00:54:23 +08:00
|
|
|
ret = acpi_check_resource_conflict(&res[r]);
|
2010-10-01 04:55:36 +08:00
|
|
|
if (ret)
|
|
|
|
goto fail_res;
|
|
|
|
}
|
2008-06-27 17:37:19 +08:00
|
|
|
}
|
|
|
|
|
2010-05-31 17:30:55 +08:00
|
|
|
ret = platform_device_add_resources(pdev, res, cell->num_resources);
|
|
|
|
if (ret)
|
|
|
|
goto fail_res;
|
2008-06-27 17:37:19 +08:00
|
|
|
|
|
|
|
ret = platform_device_add(pdev);
|
|
|
|
if (ret)
|
2008-07-26 05:59:29 +08:00
|
|
|
goto fail_res;
|
|
|
|
|
2010-11-27 01:19:34 +08:00
|
|
|
if (cell->pm_runtime_no_callbacks)
|
|
|
|
pm_runtime_no_callbacks(&pdev->dev);
|
|
|
|
|
2008-07-26 05:59:29 +08:00
|
|
|
kfree(res);
|
2008-06-27 17:37:19 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2008-07-26 05:59:29 +08:00
|
|
|
fail_res:
|
|
|
|
kfree(res);
|
2008-06-27 17:37:19 +08:00
|
|
|
fail_device:
|
|
|
|
platform_device_put(pdev);
|
|
|
|
fail_alloc:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-07-29 07:30:26 +08:00
|
|
|
int mfd_add_devices(struct device *parent, int id,
|
2011-02-18 11:07:34 +08:00
|
|
|
struct mfd_cell *cells, int n_devs,
|
2008-07-29 00:29:09 +08:00
|
|
|
struct resource *mem_base,
|
|
|
|
int irq_base)
|
2008-06-27 17:37:19 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2011-02-18 11:07:34 +08:00
|
|
|
atomic_t *cnts;
|
|
|
|
|
|
|
|
/* initialize reference counting for all cells */
|
2012-02-12 17:43:22 +08:00
|
|
|
cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
|
2011-02-18 11:07:34 +08:00
|
|
|
if (!cnts)
|
|
|
|
return -ENOMEM;
|
2008-06-27 17:37:19 +08:00
|
|
|
|
|
|
|
for (i = 0; i < n_devs; i++) {
|
2011-02-18 11:07:34 +08:00
|
|
|
atomic_set(&cnts[i], 0);
|
|
|
|
cells[i].usage_count = &cnts[i];
|
2008-07-29 07:30:26 +08:00
|
|
|
ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
|
2008-06-27 17:37:19 +08:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
mfd_remove_devices(parent);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mfd_add_devices);
|
|
|
|
|
2011-02-18 11:07:34 +08:00
|
|
|
static int mfd_remove_devices_fn(struct device *dev, void *c)
|
2008-06-27 17:37:19 +08:00
|
|
|
{
|
2011-02-18 11:07:34 +08:00
|
|
|
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);
|
2008-06-27 17:37:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-29 07:30:26 +08:00
|
|
|
void mfd_remove_devices(struct device *parent)
|
2008-06-27 17:37:19 +08:00
|
|
|
{
|
2011-02-18 11:07:34 +08:00
|
|
|
atomic_t *cnts = NULL;
|
|
|
|
|
|
|
|
device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
|
|
|
|
kfree(cnts);
|
2008-06-27 17:37:19 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(mfd_remove_devices);
|
|
|
|
|
2011-03-22 10:19:35 +08:00
|
|
|
int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
|
2011-02-18 11:07:35 +08:00
|
|
|
{
|
|
|
|
struct mfd_cell cell_entry;
|
|
|
|
struct device *dev;
|
|
|
|
struct platform_device *pdev;
|
2011-03-22 10:19:35 +08:00
|
|
|
int i;
|
2011-02-18 11:07:35 +08:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2011-03-22 10:19:35 +08:00
|
|
|
for (i = 0; i < n_clones; i++) {
|
|
|
|
cell_entry.name = clones[i];
|
|
|
|
/* don't give up if a single call fails; just report error */
|
|
|
|
if (mfd_add_device(pdev->dev.parent, -1, &cell_entry, NULL, 0))
|
|
|
|
dev_err(dev, "failed to create platform device '%s'\n",
|
|
|
|
clones[i]);
|
|
|
|
}
|
2011-02-18 11:07:35 +08:00
|
|
|
|
2011-03-22 10:19:35 +08:00
|
|
|
return 0;
|
2011-02-18 11:07:35 +08:00
|
|
|
}
|
2011-03-22 10:19:35 +08:00
|
|
|
EXPORT_SYMBOL(mfd_clone_cell);
|
2011-02-18 11:07:35 +08:00
|
|
|
|
2008-06-27 17:37:19 +08:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");
|