crypto: ccree - fix runtime PM imbalance on error
[linux-2.6-microblaze.git] / drivers / mfd / mfd-core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/mfd/mfd-core.c
4  *
5  * core MFD support
6  * Copyright (c) 2006 Ian Molton
7  * Copyright (c) 2007,2008 Dmitry Baryshkov
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
12 #include <linux/acpi.h>
13 #include <linux/list.h>
14 #include <linux/property.h>
15 #include <linux/mfd/core.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/irqdomain.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/regulator/consumer.h>
23
24 static LIST_HEAD(mfd_of_node_list);
25
26 struct mfd_of_node_entry {
27         struct list_head list;
28         struct device *dev;
29         struct device_node *np;
30 };
31
32 static struct device_type mfd_dev_type = {
33         .name   = "mfd_device",
34 };
35
36 int mfd_cell_enable(struct platform_device *pdev)
37 {
38         const struct mfd_cell *cell = mfd_get_cell(pdev);
39
40         if (!cell->enable) {
41                 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
42                 return 0;
43         }
44
45         return cell->enable(pdev);
46 }
47 EXPORT_SYMBOL(mfd_cell_enable);
48
49 int mfd_cell_disable(struct platform_device *pdev)
50 {
51         const struct mfd_cell *cell = mfd_get_cell(pdev);
52
53         if (!cell->disable) {
54                 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
55                 return 0;
56         }
57
58         return cell->disable(pdev);
59 }
60 EXPORT_SYMBOL(mfd_cell_disable);
61
62 #if IS_ENABLED(CONFIG_ACPI)
63 static void mfd_acpi_add_device(const struct mfd_cell *cell,
64                                 struct platform_device *pdev)
65 {
66         const struct mfd_cell_acpi_match *match = cell->acpi_match;
67         struct acpi_device *parent, *child;
68         struct acpi_device *adev;
69
70         parent = ACPI_COMPANION(pdev->dev.parent);
71         if (!parent)
72                 return;
73
74         /*
75          * MFD child device gets its ACPI handle either from the ACPI device
76          * directly under the parent that matches the either _HID or _CID, or
77          * _ADR or it will use the parent handle if is no ID is given.
78          *
79          * Note that use of _ADR is a grey area in the ACPI specification,
80          * though Intel Galileo Gen2 is using it to distinguish the children
81          * devices.
82          */
83         adev = parent;
84         if (match) {
85                 if (match->pnpid) {
86                         struct acpi_device_id ids[2] = {};
87
88                         strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
89                         list_for_each_entry(child, &parent->children, node) {
90                                 if (!acpi_match_device_ids(child, ids)) {
91                                         adev = child;
92                                         break;
93                                 }
94                         }
95                 } else {
96                         unsigned long long adr;
97                         acpi_status status;
98
99                         list_for_each_entry(child, &parent->children, node) {
100                                 status = acpi_evaluate_integer(child->handle,
101                                                                "_ADR", NULL,
102                                                                &adr);
103                                 if (ACPI_SUCCESS(status) && match->adr == adr) {
104                                         adev = child;
105                                         break;
106                                 }
107                         }
108                 }
109         }
110
111         ACPI_COMPANION_SET(&pdev->dev, adev);
112 }
113 #else
114 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
115                                        struct platform_device *pdev)
116 {
117 }
118 #endif
119
120 static int mfd_match_of_node_to_dev(struct platform_device *pdev,
121                                     struct device_node *np,
122                                     const struct mfd_cell *cell)
123 {
124 #if IS_ENABLED(CONFIG_OF)
125         struct mfd_of_node_entry *of_entry;
126         const __be32 *reg;
127         u64 of_node_addr;
128
129         /* Skip devices 'disabled' by Device Tree */
130         if (!of_device_is_available(np))
131                 return -ENODEV;
132
133         /* Skip if OF node has previously been allocated to a device */
134         list_for_each_entry(of_entry, &mfd_of_node_list, list)
135                 if (of_entry->np == np)
136                         return -EAGAIN;
137
138         if (!cell->use_of_reg)
139                 /* No of_reg defined - allocate first free compatible match */
140                 goto allocate_of_node;
141
142         /* We only care about each node's first defined address */
143         reg = of_get_address(np, 0, NULL, NULL);
144         if (!reg)
145                 /* OF node does not contatin a 'reg' property to match to */
146                 return -EAGAIN;
147
148         of_node_addr = of_read_number(reg, of_n_addr_cells(np));
149
150         if (cell->of_reg != of_node_addr)
151                 /* No match */
152                 return -EAGAIN;
153
154 allocate_of_node:
155         of_entry = kzalloc(sizeof(*of_entry), GFP_KERNEL);
156         if (!of_entry)
157                 return -ENOMEM;
158
159         of_entry->dev = &pdev->dev;
160         of_entry->np = np;
161         list_add_tail(&of_entry->list, &mfd_of_node_list);
162
163         pdev->dev.of_node = np;
164         pdev->dev.fwnode = &np->fwnode;
165 #endif
166         return 0;
167 }
168
169 static int mfd_add_device(struct device *parent, int id,
170                           const struct mfd_cell *cell,
171                           struct resource *mem_base,
172                           int irq_base, struct irq_domain *domain)
173 {
174         struct resource *res;
175         struct platform_device *pdev;
176         struct device_node *np = NULL;
177         struct mfd_of_node_entry *of_entry, *tmp;
178         int ret = -ENOMEM;
179         int platform_id;
180         int r;
181
182         if (id == PLATFORM_DEVID_AUTO)
183                 platform_id = id;
184         else
185                 platform_id = id + cell->id;
186
187         pdev = platform_device_alloc(cell->name, platform_id);
188         if (!pdev)
189                 goto fail_alloc;
190
191         pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
192         if (!pdev->mfd_cell)
193                 goto fail_device;
194
195         res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
196         if (!res)
197                 goto fail_device;
198
199         pdev->dev.parent = parent;
200         pdev->dev.type = &mfd_dev_type;
201         pdev->dev.dma_mask = parent->dma_mask;
202         pdev->dev.dma_parms = parent->dma_parms;
203         pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
204
205         ret = regulator_bulk_register_supply_alias(
206                         &pdev->dev, cell->parent_supplies,
207                         parent, cell->parent_supplies,
208                         cell->num_parent_supplies);
209         if (ret < 0)
210                 goto fail_res;
211
212         if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) {
213                 for_each_child_of_node(parent->of_node, np) {
214                         if (of_device_is_compatible(np, cell->of_compatible)) {
215                                 ret = mfd_match_of_node_to_dev(pdev, np, cell);
216                                 if (ret == -EAGAIN)
217                                         continue;
218                                 if (ret)
219                                         goto fail_alias;
220
221                                 break;
222                         }
223                 }
224
225                 if (!pdev->dev.of_node)
226                         pr_warn("%s: Failed to locate of_node [id: %d]\n",
227                                 cell->name, platform_id);
228         }
229
230         mfd_acpi_add_device(cell, pdev);
231
232         if (cell->pdata_size) {
233                 ret = platform_device_add_data(pdev,
234                                         cell->platform_data, cell->pdata_size);
235                 if (ret)
236                         goto fail_of_entry;
237         }
238
239         if (cell->properties) {
240                 ret = platform_device_add_properties(pdev, cell->properties);
241                 if (ret)
242                         goto fail_of_entry;
243         }
244
245         for (r = 0; r < cell->num_resources; r++) {
246                 res[r].name = cell->resources[r].name;
247                 res[r].flags = cell->resources[r].flags;
248
249                 /* Find out base to use */
250                 if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
251                         res[r].parent = mem_base;
252                         res[r].start = mem_base->start +
253                                 cell->resources[r].start;
254                         res[r].end = mem_base->start +
255                                 cell->resources[r].end;
256                 } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
257                         if (domain) {
258                                 /* Unable to create mappings for IRQ ranges. */
259                                 WARN_ON(cell->resources[r].start !=
260                                         cell->resources[r].end);
261                                 res[r].start = res[r].end = irq_create_mapping(
262                                         domain, cell->resources[r].start);
263                         } else {
264                                 res[r].start = irq_base +
265                                         cell->resources[r].start;
266                                 res[r].end   = irq_base +
267                                         cell->resources[r].end;
268                         }
269                 } else {
270                         res[r].parent = cell->resources[r].parent;
271                         res[r].start = cell->resources[r].start;
272                         res[r].end   = cell->resources[r].end;
273                 }
274
275                 if (!cell->ignore_resource_conflicts) {
276                         if (has_acpi_companion(&pdev->dev)) {
277                                 ret = acpi_check_resource_conflict(&res[r]);
278                                 if (ret)
279                                         goto fail_of_entry;
280                         }
281                 }
282         }
283
284         ret = platform_device_add_resources(pdev, res, cell->num_resources);
285         if (ret)
286                 goto fail_of_entry;
287
288         ret = platform_device_add(pdev);
289         if (ret)
290                 goto fail_of_entry;
291
292         if (cell->pm_runtime_no_callbacks)
293                 pm_runtime_no_callbacks(&pdev->dev);
294
295         kfree(res);
296
297         return 0;
298
299 fail_of_entry:
300         list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
301                 if (of_entry->dev == &pdev->dev) {
302                         list_del(&of_entry->list);
303                         kfree(of_entry);
304                 }
305 fail_alias:
306         regulator_bulk_unregister_supply_alias(&pdev->dev,
307                                                cell->parent_supplies,
308                                                cell->num_parent_supplies);
309 fail_res:
310         kfree(res);
311 fail_device:
312         platform_device_put(pdev);
313 fail_alloc:
314         return ret;
315 }
316
317 /**
318  * mfd_add_devices - register child devices
319  *
320  * @parent:     Pointer to parent device.
321  * @id:         Can be PLATFORM_DEVID_AUTO to let the Platform API take care
322  *              of device numbering, or will be added to a device's cell_id.
323  * @cells:      Array of (struct mfd_cell)s describing child devices.
324  * @n_devs:     Number of child devices to register.
325  * @mem_base:   Parent register range resource for child devices.
326  * @irq_base:   Base of the range of virtual interrupt numbers allocated for
327  *              this MFD device. Unused if @domain is specified.
328  * @domain:     Interrupt domain to create mappings for hardware interrupts.
329  */
330 int mfd_add_devices(struct device *parent, int id,
331                     const struct mfd_cell *cells, int n_devs,
332                     struct resource *mem_base,
333                     int irq_base, struct irq_domain *domain)
334 {
335         int i;
336         int ret;
337
338         for (i = 0; i < n_devs; i++) {
339                 ret = mfd_add_device(parent, id, cells + i, mem_base,
340                                      irq_base, domain);
341                 if (ret)
342                         goto fail;
343         }
344
345         return 0;
346
347 fail:
348         if (i)
349                 mfd_remove_devices(parent);
350
351         return ret;
352 }
353 EXPORT_SYMBOL(mfd_add_devices);
354
355 static int mfd_remove_devices_fn(struct device *dev, void *data)
356 {
357         struct platform_device *pdev;
358         const struct mfd_cell *cell;
359         int *level = data;
360
361         if (dev->type != &mfd_dev_type)
362                 return 0;
363
364         pdev = to_platform_device(dev);
365         cell = mfd_get_cell(pdev);
366
367         if (level && cell->level > *level)
368                 return 0;
369
370         regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
371                                                cell->num_parent_supplies);
372
373         kfree(cell);
374
375         platform_device_unregister(pdev);
376         return 0;
377 }
378
379 void mfd_remove_devices_late(struct device *parent)
380 {
381         int level = MFD_DEP_LEVEL_HIGH;
382
383         device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
384 }
385 EXPORT_SYMBOL(mfd_remove_devices_late);
386
387 void mfd_remove_devices(struct device *parent)
388 {
389         int level = MFD_DEP_LEVEL_NORMAL;
390
391         device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
392 }
393 EXPORT_SYMBOL(mfd_remove_devices);
394
395 static void devm_mfd_dev_release(struct device *dev, void *res)
396 {
397         mfd_remove_devices(dev);
398 }
399
400 /**
401  * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
402  *
403  * Returns 0 on success or an appropriate negative error number on failure.
404  * All child-devices of the MFD will automatically be removed when it gets
405  * unbinded.
406  *
407  * @dev:        Pointer to parent device.
408  * @id:         Can be PLATFORM_DEVID_AUTO to let the Platform API take care
409  *              of device numbering, or will be added to a device's cell_id.
410  * @cells:      Array of (struct mfd_cell)s describing child devices.
411  * @n_devs:     Number of child devices to register.
412  * @mem_base:   Parent register range resource for child devices.
413  * @irq_base:   Base of the range of virtual interrupt numbers allocated for
414  *              this MFD device. Unused if @domain is specified.
415  * @domain:     Interrupt domain to create mappings for hardware interrupts.
416  */
417 int devm_mfd_add_devices(struct device *dev, int id,
418                          const struct mfd_cell *cells, int n_devs,
419                          struct resource *mem_base,
420                          int irq_base, struct irq_domain *domain)
421 {
422         struct device **ptr;
423         int ret;
424
425         ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
426         if (!ptr)
427                 return -ENOMEM;
428
429         ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
430                               irq_base, domain);
431         if (ret < 0) {
432                 devres_free(ptr);
433                 return ret;
434         }
435
436         *ptr = dev;
437         devres_add(dev, ptr);
438
439         return ret;
440 }
441 EXPORT_SYMBOL(devm_mfd_add_devices);
442
443 MODULE_LICENSE("GPL");
444 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");