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.c131
1 files changed, 120 insertions, 11 deletions
diff --git a/drivers/mfd/mfd-core.c b/drivers/mfd/mfd-core.c
index 1823a57b7d8f..0902523af62d 100644
--- a/drivers/mfd/mfd-core.c
+++ b/drivers/mfd/mfd-core.c
@@ -15,8 +15,59 @@
15#include <linux/platform_device.h> 15#include <linux/platform_device.h>
16#include <linux/acpi.h> 16#include <linux/acpi.h>
17#include <linux/mfd/core.h> 17#include <linux/mfd/core.h>
18#include <linux/pm_runtime.h>
18#include <linux/slab.h> 19#include <linux/slab.h>
19 20
21int mfd_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_cell_enable);
37
38int mfd_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_cell_disable);
57
58static int mfd_platform_add_cell(struct platform_device *pdev,
59 const struct mfd_cell *cell)
60{
61 if (!cell)
62 return 0;
63
64 pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
65 if (!pdev->mfd_cell)
66 return -ENOMEM;
67
68 return 0;
69}
70
20static int mfd_add_device(struct device *parent, int id, 71static int mfd_add_device(struct device *parent, int id,
21 const struct mfd_cell *cell, 72 const struct mfd_cell *cell,
22 struct resource *mem_base, 73 struct resource *mem_base,
@@ -36,10 +87,15 @@ static int mfd_add_device(struct device *parent, int id,
36 goto fail_device; 87 goto fail_device;
37 88
38 pdev->dev.parent = parent; 89 pdev->dev.parent = parent;
39 platform_set_drvdata(pdev, cell->driver_data);
40 90
41 ret = platform_device_add_data(pdev, 91 if (cell->pdata_size) {
42 cell->platform_data, cell->data_size); 92 ret = platform_device_add_data(pdev,
93 cell->platform_data, cell->pdata_size);
94 if (ret)
95 goto fail_res;
96 }
97
98 ret = mfd_platform_add_cell(pdev, cell);
43 if (ret) 99 if (ret)
44 goto fail_res; 100 goto fail_res;
45 101
@@ -65,9 +121,11 @@ static int mfd_add_device(struct device *parent, int id,
65 res[r].end = cell->resources[r].end; 121 res[r].end = cell->resources[r].end;
66 } 122 }
67 123
68 ret = acpi_check_resource_conflict(res); 124 if (!cell->ignore_resource_conflicts) {
69 if (ret) 125 ret = acpi_check_resource_conflict(res);
70 goto fail_res; 126 if (ret)
127 goto fail_res;
128 }
71 } 129 }
72 130
73 ret = platform_device_add_resources(pdev, res, cell->num_resources); 131 ret = platform_device_add_resources(pdev, res, cell->num_resources);
@@ -78,11 +136,13 @@ static int mfd_add_device(struct device *parent, int id,
78 if (ret) 136 if (ret)
79 goto fail_res; 137 goto fail_res;
80 138
139 if (cell->pm_runtime_no_callbacks)
140 pm_runtime_no_callbacks(&pdev->dev);
141
81 kfree(res); 142 kfree(res);
82 143
83 return 0; 144 return 0;
84 145
85/* platform_device_del(pdev); */
86fail_res: 146fail_res:
87 kfree(res); 147 kfree(res);
88fail_device: 148fail_device:
@@ -92,14 +152,22 @@ fail_alloc:
92} 152}
93 153
94int mfd_add_devices(struct device *parent, int id, 154int mfd_add_devices(struct device *parent, int id,
95 const struct mfd_cell *cells, int n_devs, 155 struct mfd_cell *cells, int n_devs,
96 struct resource *mem_base, 156 struct resource *mem_base,
97 int irq_base) 157 int irq_base)
98{ 158{
99 int i; 159 int i;
100 int ret = 0; 160 int ret = 0;
161 atomic_t *cnts;
162
163 /* initialize reference counting for all cells */
164 cnts = kcalloc(sizeof(*cnts), n_devs, GFP_KERNEL);
165 if (!cnts)
166 return -ENOMEM;
101 167
102 for (i = 0; i < n_devs; i++) { 168 for (i = 0; i < n_devs; i++) {
169 atomic_set(&cnts[i], 0);
170 cells[i].usage_count = &cnts[i];
103 ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base); 171 ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
104 if (ret) 172 if (ret)
105 break; 173 break;
@@ -112,17 +180,58 @@ int mfd_add_devices(struct device *parent, int id,
112} 180}
113EXPORT_SYMBOL(mfd_add_devices); 181EXPORT_SYMBOL(mfd_add_devices);
114 182
115static int mfd_remove_devices_fn(struct device *dev, void *unused) 183static int mfd_remove_devices_fn(struct device *dev, void *c)
116{ 184{
117 platform_device_unregister(to_platform_device(dev)); 185 struct platform_device *pdev = to_platform_device(dev);
186 const struct mfd_cell *cell = mfd_get_cell(pdev);
187 atomic_t **usage_count = c;
188
189 /* find the base address of usage_count pointers (for freeing) */
190 if (!*usage_count || (cell->usage_count < *usage_count))
191 *usage_count = cell->usage_count;
192
193 platform_device_unregister(pdev);
118 return 0; 194 return 0;
119} 195}
120 196
121void mfd_remove_devices(struct device *parent) 197void mfd_remove_devices(struct device *parent)
122{ 198{
123 device_for_each_child(parent, NULL, mfd_remove_devices_fn); 199 atomic_t *cnts = NULL;
200
201 device_for_each_child(parent, &cnts, mfd_remove_devices_fn);
202 kfree(cnts);
124} 203}
125EXPORT_SYMBOL(mfd_remove_devices); 204EXPORT_SYMBOL(mfd_remove_devices);
126 205
206int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
207{
208 struct mfd_cell cell_entry;
209 struct device *dev;
210 struct platform_device *pdev;
211 int i;
212
213 /* fetch the parent cell's device (should already be registered!) */
214 dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
215 if (!dev) {
216 printk(KERN_ERR "failed to find device for cell %s\n", cell);
217 return -ENODEV;
218 }
219 pdev = to_platform_device(dev);
220 memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
221
222 WARN_ON(!cell_entry.enable);
223
224 for (i = 0; i < n_clones; i++) {
225 cell_entry.name = clones[i];
226 /* don't give up if a single call fails; just report error */
227 if (mfd_add_device(pdev->dev.parent, -1, &cell_entry, NULL, 0))
228 dev_err(dev, "failed to create platform device '%s'\n",
229 clones[i]);
230 }
231
232 return 0;
233}
234EXPORT_SYMBOL(mfd_clone_cell);
235
127MODULE_LICENSE("GPL"); 236MODULE_LICENSE("GPL");
128MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov"); 237MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");