aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mfd/mfd-core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mfd/mfd-core.c')
-rw-r--r--drivers/mfd/mfd-core.c64
1 files changed, 60 insertions, 4 deletions
diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c
index 01115f686dfa..ca789f882305 100644
--- a/drivers/mfd/mfd-core.c
+++ b/drivers/mfd/mfd-core.c
@@ -18,6 +18,43 @@
18#include <linux/pm_runtime.h> 18#include <linux/pm_runtime.h>
19#include <linux/slab.h> 19#include <linux/slab.h>
20 20
21int 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}
36EXPORT_SYMBOL(mfd_shared_cell_enable);
37
38int 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}
56EXPORT_SYMBOL(mfd_shared_cell_disable);
57
21static int mfd_add_device(struct device *parent, int id, 58static int mfd_add_device(struct device *parent, int id,
22 const struct mfd_cell *cell, 59 const struct mfd_cell *cell,
23 struct resource *mem_base, 60 struct resource *mem_base,
@@ -96,14 +133,22 @@ fail_alloc:
96} 133}
97 134
98int mfd_add_devices(struct device *parent, int id, 135int 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,
100 struct resource *mem_base, 137 struct resource *mem_base,
101 int irq_base) 138 int irq_base)
102{ 139{
103 int i; 140 int i;
104 int ret = 0; 141 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;
105 148
106 for (i = 0; i < n_devs; i++) { 149 for (i = 0; i < n_devs; i++) {
150 atomic_set(&cnts[i], 0);
151 cells[i].usage_count = &cnts[i];
107 ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base); 152 ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
108 if (ret) 153 if (ret)
109 break; 154 break;
@@ -116,15 +161,26 @@ int mfd_add_devices(struct device *parent, int id,
116} 161}
117EXPORT_SYMBOL(mfd_add_devices); 162EXPORT_SYMBOL(mfd_add_devices);
118 163
119static int mfd_remove_devices_fn(struct device *dev, void *unused) 164static int mfd_remove_devices_fn(struct device *dev, void *c)
120{ 165{
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);
122 return 0; 175 return 0;
123} 176}
124 177
125void mfd_remove_devices(struct device *parent) 178void mfd_remove_devices(struct device *parent)
126{ 179{
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);
128} 184}
129EXPORT_SYMBOL(mfd_remove_devices); 185EXPORT_SYMBOL(mfd_remove_devices);
130 186