]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
aa613de6 DB |
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 | |
aa613de6 DB |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/platform_device.h> | |
91fedede | 12 | #include <linux/acpi.h> |
466a62d7 | 13 | #include <linux/list.h> |
4d215cab | 14 | #include <linux/property.h> |
aa613de6 | 15 | #include <linux/mfd/core.h> |
4c90aa94 | 16 | #include <linux/pm_runtime.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
4e36dd33 | 18 | #include <linux/module.h> |
c94bb233 LJ |
19 | #include <linux/irqdomain.h> |
20 | #include <linux/of.h> | |
466a62d7 | 21 | #include <linux/of_address.h> |
7fcd4274 | 22 | #include <linux/regulator/consumer.h> |
aa613de6 | 23 | |
466a62d7 LJ |
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 | ||
b9fbb62e CK |
32 | static struct device_type mfd_dev_type = { |
33 | .name = "mfd_device", | |
34 | }; | |
35 | ||
f77289ac | 36 | int mfd_cell_enable(struct platform_device *pdev) |
1e29af62 AS |
37 | { |
38 | const struct mfd_cell *cell = mfd_get_cell(pdev); | |
1e29af62 | 39 | |
b195e101 LJ |
40 | if (!cell->enable) { |
41 | dev_dbg(&pdev->dev, "No .enable() call-back registered\n"); | |
42 | return 0; | |
43 | } | |
44 | ||
5a47c0fb | 45 | return cell->enable(pdev); |
1e29af62 | 46 | } |
f77289ac | 47 | EXPORT_SYMBOL(mfd_cell_enable); |
1e29af62 | 48 | |
f77289ac | 49 | int mfd_cell_disable(struct platform_device *pdev) |
1e29af62 AS |
50 | { |
51 | const struct mfd_cell *cell = mfd_get_cell(pdev); | |
1e29af62 | 52 | |
b195e101 LJ |
53 | if (!cell->disable) { |
54 | dev_dbg(&pdev->dev, "No .disable() call-back registered\n"); | |
55 | return 0; | |
56 | } | |
57 | ||
5a47c0fb | 58 | return cell->disable(pdev); |
1e29af62 | 59 | } |
f77289ac | 60 | EXPORT_SYMBOL(mfd_cell_disable); |
1e29af62 | 61 | |
6ab34301 MW |
62 | #if IS_ENABLED(CONFIG_ACPI) |
63 | static void mfd_acpi_add_device(const struct mfd_cell *cell, | |
64 | struct platform_device *pdev) | |
65 | { | |
98a3be44 AS |
66 | const struct mfd_cell_acpi_match *match = cell->acpi_match; |
67 | struct acpi_device *parent, *child; | |
6ab34301 MW |
68 | struct acpi_device *adev; |
69 | ||
98a3be44 AS |
70 | parent = ACPI_COMPANION(pdev->dev.parent); |
71 | if (!parent) | |
6ab34301 MW |
72 | return; |
73 | ||
74 | /* | |
98a3be44 AS |
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. | |
6ab34301 | 82 | */ |
98a3be44 AS |
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) { | |
ee414de5 | 90 | if (!acpi_match_device_ids(child, ids)) { |
98a3be44 AS |
91 | adev = child; |
92 | break; | |
93 | } | |
6ab34301 | 94 | } |
98a3be44 AS |
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 | } | |
6ab34301 MW |
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 | ||
466a62d7 LJ |
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 | ||
424f525a | 169 | static int mfd_add_device(struct device *parent, int id, |
5a47c0fb | 170 | const struct mfd_cell *cell, |
7f71ac93 | 171 | struct resource *mem_base, |
0848c94f | 172 | int irq_base, struct irq_domain *domain) |
aa613de6 | 173 | { |
a87903f3 | 174 | struct resource *res; |
aa613de6 | 175 | struct platform_device *pdev; |
c94bb233 | 176 | struct device_node *np = NULL; |
466a62d7 | 177 | struct mfd_of_node_entry *of_entry, *tmp; |
aa613de6 | 178 | int ret = -ENOMEM; |
6e3f62f0 | 179 | int platform_id; |
aa613de6 DB |
180 | int r; |
181 | ||
a77c50b4 | 182 | if (id == PLATFORM_DEVID_AUTO) |
6e3f62f0 JH |
183 | platform_id = id; |
184 | else | |
185 | platform_id = id + cell->id; | |
186 | ||
187 | pdev = platform_device_alloc(cell->name, platform_id); | |
aa613de6 DB |
188 | if (!pdev) |
189 | goto fail_alloc; | |
190 | ||
b944a688 LJ |
191 | pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL); |
192 | if (!pdev->mfd_cell) | |
193 | goto fail_device; | |
194 | ||
6396bb22 | 195 | res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL); |
a87903f3 IM |
196 | if (!res) |
197 | goto fail_device; | |
198 | ||
424f525a | 199 | pdev->dev.parent = parent; |
b9fbb62e | 200 | pdev->dev.type = &mfd_dev_type; |
b018e136 BS |
201 | pdev->dev.dma_mask = parent->dma_mask; |
202 | pdev->dev.dma_parms = parent->dma_parms; | |
4f08df1b | 203 | pdev->dev.coherent_dma_mask = parent->coherent_dma_mask; |
aa613de6 | 204 | |
d137be00 | 205 | ret = regulator_bulk_register_supply_alias( |
7fcd4274 CK |
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 | ||
466a62d7 | 212 | if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) { |
c94bb233 LJ |
213 | for_each_child_of_node(parent->of_node, np) { |
214 | if (of_device_is_compatible(np, cell->of_compatible)) { | |
466a62d7 LJ |
215 | ret = mfd_match_of_node_to_dev(pdev, np, cell); |
216 | if (ret == -EAGAIN) | |
217 | continue; | |
218 | if (ret) | |
6b5c3506 | 219 | goto fail_alias; |
466a62d7 | 220 | |
c94bb233 LJ |
221 | break; |
222 | } | |
223 | } | |
466a62d7 LJ |
224 | |
225 | if (!pdev->dev.of_node) | |
226 | pr_warn("%s: Failed to locate of_node [id: %d]\n", | |
227 | cell->name, platform_id); | |
c94bb233 LJ |
228 | } |
229 | ||
6ab34301 MW |
230 | mfd_acpi_add_device(cell, pdev); |
231 | ||
eb895607 SO |
232 | if (cell->pdata_size) { |
233 | ret = platform_device_add_data(pdev, | |
234 | cell->platform_data, cell->pdata_size); | |
235 | if (ret) | |
466a62d7 | 236 | goto fail_of_entry; |
eb895607 SO |
237 | } |
238 | ||
f4d05266 HK |
239 | if (cell->properties) { |
240 | ret = platform_device_add_properties(pdev, cell->properties); | |
4d215cab | 241 | if (ret) |
466a62d7 | 242 | goto fail_of_entry; |
4d215cab AS |
243 | } |
244 | ||
aa613de6 DB |
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 */ | |
f03cfcbc | 250 | if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) { |
aa613de6 DB |
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) { | |
c94bb233 LJ |
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 | } | |
aa613de6 DB |
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 | } | |
91fedede | 274 | |
5f2545fa | 275 | if (!cell->ignore_resource_conflicts) { |
ec40c606 LP |
276 | if (has_acpi_companion(&pdev->dev)) { |
277 | ret = acpi_check_resource_conflict(&res[r]); | |
278 | if (ret) | |
466a62d7 | 279 | goto fail_of_entry; |
ec40c606 | 280 | } |
5f2545fa | 281 | } |
aa613de6 DB |
282 | } |
283 | ||
8af5fe3b AL |
284 | ret = platform_device_add_resources(pdev, res, cell->num_resources); |
285 | if (ret) | |
466a62d7 | 286 | goto fail_of_entry; |
aa613de6 DB |
287 | |
288 | ret = platform_device_add(pdev); | |
289 | if (ret) | |
466a62d7 | 290 | goto fail_of_entry; |
a87903f3 | 291 | |
4c90aa94 MB |
292 | if (cell->pm_runtime_no_callbacks) |
293 | pm_runtime_no_callbacks(&pdev->dev); | |
294 | ||
a87903f3 | 295 | kfree(res); |
aa613de6 DB |
296 | |
297 | return 0; | |
298 | ||
466a62d7 LJ |
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 | } | |
7fcd4274 | 305 | fail_alias: |
d137be00 CK |
306 | regulator_bulk_unregister_supply_alias(&pdev->dev, |
307 | cell->parent_supplies, | |
308 | cell->num_parent_supplies); | |
a87903f3 IM |
309 | fail_res: |
310 | kfree(res); | |
aa613de6 DB |
311 | fail_device: |
312 | platform_device_put(pdev); | |
313 | fail_alloc: | |
314 | return ret; | |
315 | } | |
316 | ||
1946f996 BG |
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 | */ | |
424f525a | 330 | int mfd_add_devices(struct device *parent, int id, |
03e361b2 | 331 | const struct mfd_cell *cells, int n_devs, |
7f71ac93 | 332 | struct resource *mem_base, |
0848c94f | 333 | int irq_base, struct irq_domain *domain) |
aa613de6 DB |
334 | { |
335 | int i; | |
0b208e41 | 336 | int ret; |
aa613de6 DB |
337 | |
338 | for (i = 0; i < n_devs; i++) { | |
5a47c0fb | 339 | ret = mfd_add_device(parent, id, cells + i, mem_base, |
0848c94f | 340 | irq_base, domain); |
aa613de6 | 341 | if (ret) |
0b208e41 | 342 | goto fail; |
aa613de6 DB |
343 | } |
344 | ||
0b208e41 | 345 | return 0; |
aa613de6 | 346 | |
0b208e41 GU |
347 | fail: |
348 | if (i) | |
349 | mfd_remove_devices(parent); | |
5a47c0fb | 350 | |
aa613de6 DB |
351 | return ret; |
352 | } | |
353 | EXPORT_SYMBOL(mfd_add_devices); | |
354 | ||
5a47c0fb | 355 | static int mfd_remove_devices_fn(struct device *dev, void *data) |
aa613de6 | 356 | { |
b9fbb62e CK |
357 | struct platform_device *pdev; |
358 | const struct mfd_cell *cell; | |
114294d2 | 359 | int *level = data; |
1e29af62 | 360 | |
b9fbb62e CK |
361 | if (dev->type != &mfd_dev_type) |
362 | return 0; | |
363 | ||
364 | pdev = to_platform_device(dev); | |
365 | cell = mfd_get_cell(pdev); | |
366 | ||
114294d2 CK |
367 | if (level && cell->level > *level) |
368 | return 0; | |
369 | ||
d137be00 CK |
370 | regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies, |
371 | cell->num_parent_supplies); | |
372 | ||
1e29af62 | 373 | platform_device_unregister(pdev); |
aa613de6 DB |
374 | return 0; |
375 | } | |
376 | ||
114294d2 CK |
377 | void mfd_remove_devices_late(struct device *parent) |
378 | { | |
379 | int level = MFD_DEP_LEVEL_HIGH; | |
380 | ||
381 | device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn); | |
382 | } | |
383 | EXPORT_SYMBOL(mfd_remove_devices_late); | |
384 | ||
424f525a | 385 | void mfd_remove_devices(struct device *parent) |
aa613de6 | 386 | { |
114294d2 CK |
387 | int level = MFD_DEP_LEVEL_NORMAL; |
388 | ||
389 | device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn); | |
aa613de6 DB |
390 | } |
391 | EXPORT_SYMBOL(mfd_remove_devices); | |
392 | ||
a8f447be LD |
393 | static void devm_mfd_dev_release(struct device *dev, void *res) |
394 | { | |
395 | mfd_remove_devices(dev); | |
396 | } | |
397 | ||
398 | /** | |
399 | * devm_mfd_add_devices - Resource managed version of mfd_add_devices() | |
400 | * | |
401 | * Returns 0 on success or an appropriate negative error number on failure. | |
402 | * All child-devices of the MFD will automatically be removed when it gets | |
403 | * unbinded. | |
5a0ffef8 LJ |
404 | * |
405 | * @dev: Pointer to parent device. | |
406 | * @id: Can be PLATFORM_DEVID_AUTO to let the Platform API take care | |
407 | * of device numbering, or will be added to a device's cell_id. | |
408 | * @cells: Array of (struct mfd_cell)s describing child devices. | |
409 | * @n_devs: Number of child devices to register. | |
410 | * @mem_base: Parent register range resource for child devices. | |
411 | * @irq_base: Base of the range of virtual interrupt numbers allocated for | |
412 | * this MFD device. Unused if @domain is specified. | |
413 | * @domain: Interrupt domain to create mappings for hardware interrupts. | |
a8f447be LD |
414 | */ |
415 | int devm_mfd_add_devices(struct device *dev, int id, | |
416 | const struct mfd_cell *cells, int n_devs, | |
417 | struct resource *mem_base, | |
418 | int irq_base, struct irq_domain *domain) | |
419 | { | |
420 | struct device **ptr; | |
421 | int ret; | |
422 | ||
423 | ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL); | |
424 | if (!ptr) | |
425 | return -ENOMEM; | |
426 | ||
427 | ret = mfd_add_devices(dev, id, cells, n_devs, mem_base, | |
428 | irq_base, domain); | |
429 | if (ret < 0) { | |
430 | devres_free(ptr); | |
431 | return ret; | |
432 | } | |
433 | ||
434 | *ptr = dev; | |
435 | devres_add(dev, ptr); | |
436 | ||
437 | return ret; | |
438 | } | |
439 | EXPORT_SYMBOL(devm_mfd_add_devices); | |
440 | ||
aa613de6 DB |
441 | MODULE_LICENSE("GPL"); |
442 | MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov"); |