GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / mfd / mfd-core.c
1 /*
2  * drivers/mfd/mfd-core.c
3  *
4  * core MFD support
5  * Copyright (c) 2006 Ian Molton
6  * Copyright (c) 2007,2008 Dmitry Baryshkov
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/acpi.h>
17 #include <linux/mfd/core.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/irqdomain.h>
22 #include <linux/of.h>
23 #include <linux/regulator/consumer.h>
24
25 static struct device_type mfd_dev_type = {
26         .name   = "mfd_device",
27 };
28
29 int mfd_cell_enable(struct platform_device *pdev)
30 {
31         const struct mfd_cell *cell = mfd_get_cell(pdev);
32         int err = 0;
33
34         if (!cell->enable) {
35                 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
36                 return 0;
37         }
38
39         /* only call enable hook if the cell wasn't previously enabled */
40         if (atomic_inc_return(cell->usage_count) == 1)
41                 err = cell->enable(pdev);
42
43         /* if the enable hook failed, decrement counter to allow retries */
44         if (err)
45                 atomic_dec(cell->usage_count);
46
47         return err;
48 }
49 EXPORT_SYMBOL(mfd_cell_enable);
50
51 int mfd_cell_disable(struct platform_device *pdev)
52 {
53         const struct mfd_cell *cell = mfd_get_cell(pdev);
54         int err = 0;
55
56         if (!cell->disable) {
57                 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
58                 return 0;
59         }
60
61         /* only disable if no other clients are using it */
62         if (atomic_dec_return(cell->usage_count) == 0)
63                 err = cell->disable(pdev);
64
65         /* if the disable hook failed, increment to allow retries */
66         if (err)
67                 atomic_inc(cell->usage_count);
68
69         /* sanity check; did someone call disable too many times? */
70         WARN_ON(atomic_read(cell->usage_count) < 0);
71
72         return err;
73 }
74 EXPORT_SYMBOL(mfd_cell_disable);
75
76 static int mfd_platform_add_cell(struct platform_device *pdev,
77                                  const struct mfd_cell *cell,
78                                  atomic_t *usage_count)
79 {
80         if (!cell)
81                 return 0;
82
83         pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
84         if (!pdev->mfd_cell)
85                 return -ENOMEM;
86
87         pdev->mfd_cell->usage_count = usage_count;
88         return 0;
89 }
90
91 #if IS_ENABLED(CONFIG_ACPI)
92 static void mfd_acpi_add_device(const struct mfd_cell *cell,
93                                 struct platform_device *pdev)
94 {
95         const struct mfd_cell_acpi_match *match = cell->acpi_match;
96         struct acpi_device *parent, *child;
97         struct acpi_device *adev;
98
99         parent = ACPI_COMPANION(pdev->dev.parent);
100         if (!parent)
101                 return;
102
103         /*
104          * MFD child device gets its ACPI handle either from the ACPI device
105          * directly under the parent that matches the either _HID or _CID, or
106          * _ADR or it will use the parent handle if is no ID is given.
107          *
108          * Note that use of _ADR is a grey area in the ACPI specification,
109          * though Intel Galileo Gen2 is using it to distinguish the children
110          * devices.
111          */
112         adev = parent;
113         if (match) {
114                 if (match->pnpid) {
115                         struct acpi_device_id ids[2] = {};
116
117                         strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
118                         list_for_each_entry(child, &parent->children, node) {
119                                 if (acpi_match_device_ids(child, ids)) {
120                                         adev = child;
121                                         break;
122                                 }
123                         }
124                 } else {
125                         unsigned long long adr;
126                         acpi_status status;
127
128                         list_for_each_entry(child, &parent->children, node) {
129                                 status = acpi_evaluate_integer(child->handle,
130                                                                "_ADR", NULL,
131                                                                &adr);
132                                 if (ACPI_SUCCESS(status) && match->adr == adr) {
133                                         adev = child;
134                                         break;
135                                 }
136                         }
137                 }
138         }
139
140         ACPI_COMPANION_SET(&pdev->dev, adev);
141 }
142 #else
143 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
144                                        struct platform_device *pdev)
145 {
146 }
147 #endif
148
149 static int mfd_add_device(struct device *parent, int id,
150                           const struct mfd_cell *cell, atomic_t *usage_count,
151                           struct resource *mem_base,
152                           int irq_base, struct irq_domain *domain)
153 {
154         struct resource *res;
155         struct platform_device *pdev;
156         struct device_node *np = NULL;
157         int ret = -ENOMEM;
158         int platform_id;
159         int r;
160
161         if (id == PLATFORM_DEVID_AUTO)
162                 platform_id = id;
163         else
164                 platform_id = id + cell->id;
165
166         pdev = platform_device_alloc(cell->name, platform_id);
167         if (!pdev)
168                 goto fail_alloc;
169
170         res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
171         if (!res)
172                 goto fail_device;
173
174         pdev->dev.parent = parent;
175         pdev->dev.type = &mfd_dev_type;
176         pdev->dev.dma_mask = parent->dma_mask;
177         pdev->dev.dma_parms = parent->dma_parms;
178         pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
179
180         ret = regulator_bulk_register_supply_alias(
181                         &pdev->dev, cell->parent_supplies,
182                         parent, cell->parent_supplies,
183                         cell->num_parent_supplies);
184         if (ret < 0)
185                 goto fail_res;
186
187         if (parent->of_node && cell->of_compatible) {
188                 for_each_child_of_node(parent->of_node, np) {
189                         if (of_device_is_compatible(np, cell->of_compatible)) {
190                                 pdev->dev.of_node = np;
191                                 pdev->dev.fwnode = &np->fwnode;
192                                 break;
193                         }
194                 }
195         }
196
197         mfd_acpi_add_device(cell, pdev);
198
199         if (cell->pdata_size) {
200                 ret = platform_device_add_data(pdev,
201                                         cell->platform_data, cell->pdata_size);
202                 if (ret)
203                         goto fail_alias;
204         }
205
206         ret = mfd_platform_add_cell(pdev, cell, usage_count);
207         if (ret)
208                 goto fail_alias;
209
210         for (r = 0; r < cell->num_resources; r++) {
211                 res[r].name = cell->resources[r].name;
212                 res[r].flags = cell->resources[r].flags;
213
214                 /* Find out base to use */
215                 if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
216                         res[r].parent = mem_base;
217                         res[r].start = mem_base->start +
218                                 cell->resources[r].start;
219                         res[r].end = mem_base->start +
220                                 cell->resources[r].end;
221                 } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
222                         if (domain) {
223                                 /* Unable to create mappings for IRQ ranges. */
224                                 WARN_ON(cell->resources[r].start !=
225                                         cell->resources[r].end);
226                                 res[r].start = res[r].end = irq_create_mapping(
227                                         domain, cell->resources[r].start);
228                         } else {
229                                 res[r].start = irq_base +
230                                         cell->resources[r].start;
231                                 res[r].end   = irq_base +
232                                         cell->resources[r].end;
233                         }
234                 } else {
235                         res[r].parent = cell->resources[r].parent;
236                         res[r].start = cell->resources[r].start;
237                         res[r].end   = cell->resources[r].end;
238                 }
239
240                 if (!cell->ignore_resource_conflicts) {
241                         if (has_acpi_companion(&pdev->dev)) {
242                                 ret = acpi_check_resource_conflict(&res[r]);
243                                 if (ret)
244                                         goto fail_alias;
245                         }
246                 }
247         }
248
249         ret = platform_device_add_resources(pdev, res, cell->num_resources);
250         if (ret)
251                 goto fail_alias;
252
253         ret = platform_device_add(pdev);
254         if (ret)
255                 goto fail_alias;
256
257         if (cell->pm_runtime_no_callbacks)
258                 pm_runtime_no_callbacks(&pdev->dev);
259
260         kfree(res);
261
262         return 0;
263
264 fail_alias:
265         regulator_bulk_unregister_supply_alias(&pdev->dev,
266                                                cell->parent_supplies,
267                                                cell->num_parent_supplies);
268 fail_res:
269         kfree(res);
270 fail_device:
271         platform_device_put(pdev);
272 fail_alloc:
273         return ret;
274 }
275
276 int mfd_add_devices(struct device *parent, int id,
277                     const struct mfd_cell *cells, int n_devs,
278                     struct resource *mem_base,
279                     int irq_base, struct irq_domain *domain)
280 {
281         int i;
282         int ret;
283         atomic_t *cnts;
284
285         /* initialize reference counting for all cells */
286         cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
287         if (!cnts)
288                 return -ENOMEM;
289
290         for (i = 0; i < n_devs; i++) {
291                 atomic_set(&cnts[i], 0);
292                 ret = mfd_add_device(parent, id, cells + i, cnts + i, mem_base,
293                                      irq_base, domain);
294                 if (ret)
295                         goto fail;
296         }
297
298         return 0;
299
300 fail:
301         if (i)
302                 mfd_remove_devices(parent);
303         else
304                 kfree(cnts);
305         return ret;
306 }
307 EXPORT_SYMBOL(mfd_add_devices);
308
309 static int mfd_remove_devices_fn(struct device *dev, void *c)
310 {
311         struct platform_device *pdev;
312         const struct mfd_cell *cell;
313         atomic_t **usage_count = c;
314
315         if (dev->type != &mfd_dev_type)
316                 return 0;
317
318         pdev = to_platform_device(dev);
319         cell = mfd_get_cell(pdev);
320
321         regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
322                                                cell->num_parent_supplies);
323
324         /* find the base address of usage_count pointers (for freeing) */
325         if (!*usage_count || (cell->usage_count < *usage_count))
326                 *usage_count = cell->usage_count;
327
328         platform_device_unregister(pdev);
329         return 0;
330 }
331
332 void mfd_remove_devices(struct device *parent)
333 {
334         atomic_t *cnts = NULL;
335
336         device_for_each_child_reverse(parent, &cnts, mfd_remove_devices_fn);
337         kfree(cnts);
338 }
339 EXPORT_SYMBOL(mfd_remove_devices);
340
341 int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
342 {
343         struct mfd_cell cell_entry;
344         struct device *dev;
345         struct platform_device *pdev;
346         int i;
347
348         /* fetch the parent cell's device (should already be registered!) */
349         dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
350         if (!dev) {
351                 printk(KERN_ERR "failed to find device for cell %s\n", cell);
352                 return -ENODEV;
353         }
354         pdev = to_platform_device(dev);
355         memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
356
357         WARN_ON(!cell_entry.enable);
358
359         for (i = 0; i < n_clones; i++) {
360                 cell_entry.name = clones[i];
361                 /* don't give up if a single call fails; just report error */
362                 if (mfd_add_device(pdev->dev.parent, -1, &cell_entry,
363                                    cell_entry.usage_count, NULL, 0, NULL))
364                         dev_err(dev, "failed to create platform device '%s'\n",
365                                         clones[i]);
366         }
367
368         put_device(dev);
369
370         return 0;
371 }
372 EXPORT_SYMBOL(mfd_clone_cell);
373
374 MODULE_LICENSE("GPL");
375 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");