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