]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/mfd/mfd-core.c
mfd: mfd-core: Remove usage counting for .{en,dis}able() call-backs
[linux.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/property.h>
14 #include <linux/mfd/core.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/irqdomain.h>
19 #include <linux/of.h>
20 #include <linux/regulator/consumer.h>
21
22 static struct device_type mfd_dev_type = {
23         .name   = "mfd_device",
24 };
25
26 int mfd_cell_enable(struct platform_device *pdev)
27 {
28         const struct mfd_cell *cell = mfd_get_cell(pdev);
29
30         if (!cell->enable) {
31                 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
32                 return 0;
33         }
34
35         return cell->enable(pdev);
36 }
37 EXPORT_SYMBOL(mfd_cell_enable);
38
39 int mfd_cell_disable(struct platform_device *pdev)
40 {
41         const struct mfd_cell *cell = mfd_get_cell(pdev);
42
43         if (!cell->disable) {
44                 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
45                 return 0;
46         }
47
48         return cell->disable(pdev);
49 }
50 EXPORT_SYMBOL(mfd_cell_disable);
51
52 static int mfd_platform_add_cell(struct platform_device *pdev,
53                                  const struct mfd_cell *cell)
54 {
55         if (!cell)
56                 return 0;
57
58         pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
59         if (!pdev->mfd_cell)
60                 return -ENOMEM;
61
62         return 0;
63 }
64
65 #if IS_ENABLED(CONFIG_ACPI)
66 static void mfd_acpi_add_device(const struct mfd_cell *cell,
67                                 struct platform_device *pdev)
68 {
69         const struct mfd_cell_acpi_match *match = cell->acpi_match;
70         struct acpi_device *parent, *child;
71         struct acpi_device *adev;
72
73         parent = ACPI_COMPANION(pdev->dev.parent);
74         if (!parent)
75                 return;
76
77         /*
78          * MFD child device gets its ACPI handle either from the ACPI device
79          * directly under the parent that matches the either _HID or _CID, or
80          * _ADR or it will use the parent handle if is no ID is given.
81          *
82          * Note that use of _ADR is a grey area in the ACPI specification,
83          * though Intel Galileo Gen2 is using it to distinguish the children
84          * devices.
85          */
86         adev = parent;
87         if (match) {
88                 if (match->pnpid) {
89                         struct acpi_device_id ids[2] = {};
90
91                         strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
92                         list_for_each_entry(child, &parent->children, node) {
93                                 if (!acpi_match_device_ids(child, ids)) {
94                                         adev = child;
95                                         break;
96                                 }
97                         }
98                 } else {
99                         unsigned long long adr;
100                         acpi_status status;
101
102                         list_for_each_entry(child, &parent->children, node) {
103                                 status = acpi_evaluate_integer(child->handle,
104                                                                "_ADR", NULL,
105                                                                &adr);
106                                 if (ACPI_SUCCESS(status) && match->adr == adr) {
107                                         adev = child;
108                                         break;
109                                 }
110                         }
111                 }
112         }
113
114         ACPI_COMPANION_SET(&pdev->dev, adev);
115 }
116 #else
117 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
118                                        struct platform_device *pdev)
119 {
120 }
121 #endif
122
123 static int mfd_add_device(struct device *parent, int id,
124                           const struct mfd_cell *cell,
125                           struct resource *mem_base,
126                           int irq_base, struct irq_domain *domain)
127 {
128         struct resource *res;
129         struct platform_device *pdev;
130         struct device_node *np = NULL;
131         int ret = -ENOMEM;
132         int platform_id;
133         int r;
134
135         if (id == PLATFORM_DEVID_AUTO)
136                 platform_id = id;
137         else
138                 platform_id = id + cell->id;
139
140         pdev = platform_device_alloc(cell->name, platform_id);
141         if (!pdev)
142                 goto fail_alloc;
143
144         res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
145         if (!res)
146                 goto fail_device;
147
148         pdev->dev.parent = parent;
149         pdev->dev.type = &mfd_dev_type;
150         pdev->dev.dma_mask = parent->dma_mask;
151         pdev->dev.dma_parms = parent->dma_parms;
152         pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
153
154         ret = regulator_bulk_register_supply_alias(
155                         &pdev->dev, cell->parent_supplies,
156                         parent, cell->parent_supplies,
157                         cell->num_parent_supplies);
158         if (ret < 0)
159                 goto fail_res;
160
161         if (parent->of_node && cell->of_compatible) {
162                 for_each_child_of_node(parent->of_node, np) {
163                         if (of_device_is_compatible(np, cell->of_compatible)) {
164                                 pdev->dev.of_node = np;
165                                 pdev->dev.fwnode = &np->fwnode;
166                                 break;
167                         }
168                 }
169         }
170
171         mfd_acpi_add_device(cell, pdev);
172
173         if (cell->pdata_size) {
174                 ret = platform_device_add_data(pdev,
175                                         cell->platform_data, cell->pdata_size);
176                 if (ret)
177                         goto fail_alias;
178         }
179
180         if (cell->properties) {
181                 ret = platform_device_add_properties(pdev, cell->properties);
182                 if (ret)
183                         goto fail_alias;
184         }
185
186         ret = mfd_platform_add_cell(pdev, cell);
187         if (ret)
188                 goto fail_alias;
189
190         for (r = 0; r < cell->num_resources; r++) {
191                 res[r].name = cell->resources[r].name;
192                 res[r].flags = cell->resources[r].flags;
193
194                 /* Find out base to use */
195                 if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
196                         res[r].parent = mem_base;
197                         res[r].start = mem_base->start +
198                                 cell->resources[r].start;
199                         res[r].end = mem_base->start +
200                                 cell->resources[r].end;
201                 } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
202                         if (domain) {
203                                 /* Unable to create mappings for IRQ ranges. */
204                                 WARN_ON(cell->resources[r].start !=
205                                         cell->resources[r].end);
206                                 res[r].start = res[r].end = irq_create_mapping(
207                                         domain, cell->resources[r].start);
208                         } else {
209                                 res[r].start = irq_base +
210                                         cell->resources[r].start;
211                                 res[r].end   = irq_base +
212                                         cell->resources[r].end;
213                         }
214                 } else {
215                         res[r].parent = cell->resources[r].parent;
216                         res[r].start = cell->resources[r].start;
217                         res[r].end   = cell->resources[r].end;
218                 }
219
220                 if (!cell->ignore_resource_conflicts) {
221                         if (has_acpi_companion(&pdev->dev)) {
222                                 ret = acpi_check_resource_conflict(&res[r]);
223                                 if (ret)
224                                         goto fail_alias;
225                         }
226                 }
227         }
228
229         ret = platform_device_add_resources(pdev, res, cell->num_resources);
230         if (ret)
231                 goto fail_alias;
232
233         ret = platform_device_add(pdev);
234         if (ret)
235                 goto fail_alias;
236
237         if (cell->pm_runtime_no_callbacks)
238                 pm_runtime_no_callbacks(&pdev->dev);
239
240         kfree(res);
241
242         return 0;
243
244 fail_alias:
245         regulator_bulk_unregister_supply_alias(&pdev->dev,
246                                                cell->parent_supplies,
247                                                cell->num_parent_supplies);
248 fail_res:
249         kfree(res);
250 fail_device:
251         platform_device_put(pdev);
252 fail_alloc:
253         return ret;
254 }
255
256 /**
257  * mfd_add_devices - register child devices
258  *
259  * @parent:     Pointer to parent device.
260  * @id:         Can be PLATFORM_DEVID_AUTO to let the Platform API take care
261  *              of device numbering, or will be added to a device's cell_id.
262  * @cells:      Array of (struct mfd_cell)s describing child devices.
263  * @n_devs:     Number of child devices to register.
264  * @mem_base:   Parent register range resource for child devices.
265  * @irq_base:   Base of the range of virtual interrupt numbers allocated for
266  *              this MFD device. Unused if @domain is specified.
267  * @domain:     Interrupt domain to create mappings for hardware interrupts.
268  */
269 int mfd_add_devices(struct device *parent, int id,
270                     const struct mfd_cell *cells, int n_devs,
271                     struct resource *mem_base,
272                     int irq_base, struct irq_domain *domain)
273 {
274         int i;
275         int ret;
276
277         for (i = 0; i < n_devs; i++) {
278                 ret = mfd_add_device(parent, id, cells + i, mem_base,
279                                      irq_base, domain);
280                 if (ret)
281                         goto fail;
282         }
283
284         return 0;
285
286 fail:
287         if (i)
288                 mfd_remove_devices(parent);
289
290         return ret;
291 }
292 EXPORT_SYMBOL(mfd_add_devices);
293
294 static int mfd_remove_devices_fn(struct device *dev, void *data)
295 {
296         struct platform_device *pdev;
297         const struct mfd_cell *cell;
298
299         if (dev->type != &mfd_dev_type)
300                 return 0;
301
302         pdev = to_platform_device(dev);
303         cell = mfd_get_cell(pdev);
304
305         regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
306                                                cell->num_parent_supplies);
307
308         platform_device_unregister(pdev);
309         return 0;
310 }
311
312 void mfd_remove_devices(struct device *parent)
313 {
314         device_for_each_child_reverse(parent, NULL, mfd_remove_devices_fn);
315 }
316 EXPORT_SYMBOL(mfd_remove_devices);
317
318 static void devm_mfd_dev_release(struct device *dev, void *res)
319 {
320         mfd_remove_devices(dev);
321 }
322
323 /**
324  * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
325  *
326  * Returns 0 on success or an appropriate negative error number on failure.
327  * All child-devices of the MFD will automatically be removed when it gets
328  * unbinded.
329  */
330 int devm_mfd_add_devices(struct device *dev, 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         struct device **ptr;
336         int ret;
337
338         ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
339         if (!ptr)
340                 return -ENOMEM;
341
342         ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
343                               irq_base, domain);
344         if (ret < 0) {
345                 devres_free(ptr);
346                 return ret;
347         }
348
349         *ptr = dev;
350         devres_add(dev, ptr);
351
352         return ret;
353 }
354 EXPORT_SYMBOL(devm_mfd_add_devices);
355
356 MODULE_LICENSE("GPL");
357 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");