Commit 1e29af62 authored by Andres Salomon's avatar Andres Salomon Committed by Samuel Ortiz

mfd: Add refcounting support to mfd_cells

This provides convenience functions for sharing of cells across
multiple mfd clients.  Mfd drivers can provide enable/disable hooks
to actually tweak the hardware, and clients can call
mfd_shared_cell_{en,dis}able without having to worry about whether
or not another client happens to have enabled or disabled the
cell/hardware.

Note that this is purely optional; drivers can continue to use
the mfd_cell's enable/disable hooks for their own purposes, if
desired.
Signed-off-by: default avatarAndres Salomon <dilinger@queued.net>
Signed-off-by: default avatarSamuel Ortiz <sameo@linux.intel.com>
parent dcb50e83
...@@ -18,6 +18,43 @@ ...@@ -18,6 +18,43 @@
#include <linux/pm_runtime.h> #include <linux/pm_runtime.h>
#include <linux/slab.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, static int mfd_add_device(struct device *parent, int id,
const struct mfd_cell *cell, const struct mfd_cell *cell,
struct resource *mem_base, struct resource *mem_base,
...@@ -96,14 +133,22 @@ static int mfd_add_device(struct device *parent, int id, ...@@ -96,14 +133,22 @@ static int mfd_add_device(struct device *parent, int id,
} }
int mfd_add_devices(struct device *parent, int id, int mfd_add_devices(struct device *parent, int id,
const struct mfd_cell *cells, int n_devs, struct mfd_cell *cells, int n_devs,
struct resource *mem_base, struct resource *mem_base,
int irq_base) int irq_base)
{ {
int i; int i;
int ret = 0; 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++) { 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); ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
if (ret) if (ret)
break; break;
...@@ -116,15 +161,26 @@ int mfd_add_devices(struct device *parent, int id, ...@@ -116,15 +161,26 @@ int mfd_add_devices(struct device *parent, int id,
} }
EXPORT_SYMBOL(mfd_add_devices); EXPORT_SYMBOL(mfd_add_devices);
static int mfd_remove_devices_fn(struct device *dev, void *unused) static int mfd_remove_devices_fn(struct device *dev, void *c)
{ {
platform_device_unregister(to_platform_device(dev)); 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; return 0;
} }
void mfd_remove_devices(struct device *parent) void mfd_remove_devices(struct device *parent)
{ {
device_for_each_child(parent, NULL, mfd_remove_devices_fn); atomic_t *cnts = NULL;
device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
kfree(cnts);
} }
EXPORT_SYMBOL(mfd_remove_devices); EXPORT_SYMBOL(mfd_remove_devices);
......
...@@ -25,8 +25,11 @@ struct mfd_cell { ...@@ -25,8 +25,11 @@ struct mfd_cell {
const char *name; const char *name;
int id; int id;
/* refcounting for multiple drivers to use a single cell */
atomic_t *usage_count;
int (*enable)(struct platform_device *dev); int (*enable)(struct platform_device *dev);
int (*disable)(struct platform_device *dev); int (*disable)(struct platform_device *dev);
int (*suspend)(struct platform_device *dev); int (*suspend)(struct platform_device *dev);
int (*resume)(struct platform_device *dev); int (*resume)(struct platform_device *dev);
...@@ -50,6 +53,15 @@ struct mfd_cell { ...@@ -50,6 +53,15 @@ struct mfd_cell {
bool pm_runtime_no_callbacks; bool pm_runtime_no_callbacks;
}; };
/*
* Convenience functions for clients using shared cells. Refcounting
* happens automatically, with the cell's enable/disable callbacks
* being called only when a device is first being enabled or no other
* clients are making use of it.
*/
extern int mfd_shared_cell_enable(struct platform_device *pdev);
extern int mfd_shared_cell_disable(struct platform_device *pdev);
/* /*
* Given a platform device that's been created by mfd_add_devices(), fetch * Given a platform device that's been created by mfd_add_devices(), fetch
* the mfd_cell that created it. * the mfd_cell that created it.
...@@ -69,7 +81,7 @@ static inline void *mfd_get_data(struct platform_device *pdev) ...@@ -69,7 +81,7 @@ static inline void *mfd_get_data(struct platform_device *pdev)
} }
extern int mfd_add_devices(struct device *parent, int id, extern int mfd_add_devices(struct device *parent, int id,
const struct mfd_cell *cells, int n_devs, struct mfd_cell *cells, int n_devs,
struct resource *mem_base, struct resource *mem_base,
int irq_base); int irq_base);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment