Skip to content

Commit 1e29af6

Browse files
dilingerSamuel Ortiz
authored andcommitted
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: Andres Salomon <dilinger@queued.net> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
1 parent dcb50e8 commit 1e29af6

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

‎drivers/mfd/mfd-core.c‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,43 @@
1818
#include <linux/pm_runtime.h>
1919
#include <linux/slab.h>
2020

21+
int mfd_shared_cell_enable(struct platform_device *pdev)
22+
{
23+
const struct mfd_cell *cell = mfd_get_cell(pdev);
24+
int err = 0;
25+
26+
/* only call enable hook if the cell wasn't previously enabled */
27+
if (atomic_inc_return(cell->usage_count) == 1)
28+
err = cell->enable(pdev);
29+
30+
/* if the enable hook failed, decrement counter to allow retries */
31+
if (err)
32+
atomic_dec(cell->usage_count);
33+
34+
return err;
35+
}
36+
EXPORT_SYMBOL(mfd_shared_cell_enable);
37+
38+
int mfd_shared_cell_disable(struct platform_device *pdev)
39+
{
40+
const struct mfd_cell *cell = mfd_get_cell(pdev);
41+
int err = 0;
42+
43+
/* only disable if no other clients are using it */
44+
if (atomic_dec_return(cell->usage_count) == 0)
45+
err = cell->disable(pdev);
46+
47+
/* if the disable hook failed, increment to allow retries */
48+
if (err)
49+
atomic_inc(cell->usage_count);
50+
51+
/* sanity check; did someone call disable too many times? */
52+
WARN_ON(atomic_read(cell->usage_count) < 0);
53+
54+
return err;
55+
}
56+
EXPORT_SYMBOL(mfd_shared_cell_disable);
57+
2158
static int mfd_add_device(struct device *parent, int id,
2259
const struct mfd_cell *cell,
2360
struct resource *mem_base,
@@ -96,14 +133,22 @@ static int mfd_add_device(struct device *parent, int id,
96133
}
97134

98135
int mfd_add_devices(struct device *parent, int id,
99-
const struct mfd_cell *cells, int n_devs,
136+
struct mfd_cell *cells, int n_devs,
100137
struct resource *mem_base,
101138
int irq_base)
102139
{
103140
int i;
104141
int ret = 0;
142+
atomic_t *cnts;
143+
144+
/* initialize reference counting for all cells */
145+
cnts = kcalloc(sizeof(*cnts), n_devs, GFP_KERNEL);
146+
if (!cnts)
147+
return -ENOMEM;
105148

106149
for (i = 0; i < n_devs; i++) {
150+
atomic_set(&cnts[i], 0);
151+
cells[i].usage_count = &cnts[i];
107152
ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
108153
if (ret)
109154
break;
@@ -116,15 +161,26 @@ int mfd_add_devices(struct device *parent, int id,
116161
}
117162
EXPORT_SYMBOL(mfd_add_devices);
118163

119-
static int mfd_remove_devices_fn(struct device *dev, void *unused)
164+
static int mfd_remove_devices_fn(struct device *dev, void *c)
120165
{
121-
platform_device_unregister(to_platform_device(dev));
166+
struct platform_device *pdev = to_platform_device(dev);
167+
const struct mfd_cell *cell = mfd_get_cell(pdev);
168+
atomic_t **usage_count = c;
169+
170+
/* find the base address of usage_count pointers (for freeing) */
171+
if (!*usage_count || (cell->usage_count < *usage_count))
172+
*usage_count = cell->usage_count;
173+
174+
platform_device_unregister(pdev);
122175
return 0;
123176
}
124177

125178
void mfd_remove_devices(struct device *parent)
126179
{
127-
device_for_each_child(parent, NULL, mfd_remove_devices_fn);
180+
atomic_t *cnts = NULL;
181+
182+
device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
183+
kfree(cnts);
128184
}
129185
EXPORT_SYMBOL(mfd_remove_devices);
130186

‎include/linux/mfd/core.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ struct mfd_cell {
2525
const char *name;
2626
int id;
2727

28+
/* refcounting for multiple drivers to use a single cell */
29+
atomic_t *usage_count;
2830
int (*enable)(struct platform_device *dev);
2931
int (*disable)(struct platform_device *dev);
32+
3033
int (*suspend)(struct platform_device *dev);
3134
int (*resume)(struct platform_device *dev);
3235

@@ -50,6 +53,15 @@ struct mfd_cell {
5053
bool pm_runtime_no_callbacks;
5154
};
5255

56+
/*
57+
* Convenience functions for clients using shared cells. Refcounting
58+
* happens automatically, with the cell's enable/disable callbacks
59+
* being called only when a device is first being enabled or no other
60+
* clients are making use of it.
61+
*/
62+
extern int mfd_shared_cell_enable(struct platform_device *pdev);
63+
extern int mfd_shared_cell_disable(struct platform_device *pdev);
64+
5365
/*
5466
* Given a platform device that's been created by mfd_add_devices(), fetch
5567
* the mfd_cell that created it.
@@ -69,7 +81,7 @@ static inline void *mfd_get_data(struct platform_device *pdev)
6981
}
7082

7183
extern int mfd_add_devices(struct device *parent, int id,
72-
const struct mfd_cell *cells, int n_devs,
84+
struct mfd_cell *cells, int n_devs,
7385
struct resource *mem_base,
7486
int irq_base);
7587

0 commit comments

Comments
 (0)