1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/mfd/mfd-core.c
6 * Copyright (c) 2006 Ian Molton
7 * Copyright (c) 2007,2008 Dmitry Baryshkov
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>
21 #include <linux/of_address.h>
22 #include <linux/regulator/consumer.h>
24 static LIST_HEAD(mfd_of_node_list);
26 struct mfd_of_node_entry {
27 struct list_head list;
29 struct device_node *np;
32 static struct device_type mfd_dev_type = {
36 int mfd_cell_enable(struct platform_device *pdev)
38 const struct mfd_cell *cell = mfd_get_cell(pdev);
41 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
45 return cell->enable(pdev);
47 EXPORT_SYMBOL(mfd_cell_enable);
49 int mfd_cell_disable(struct platform_device *pdev)
51 const struct mfd_cell *cell = mfd_get_cell(pdev);
54 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
58 return cell->disable(pdev);
60 EXPORT_SYMBOL(mfd_cell_disable);
62 #if IS_ENABLED(CONFIG_ACPI)
63 struct match_ids_walk_data {
64 struct acpi_device_id *ids;
65 struct acpi_device *adev;
68 static int match_device_ids(struct acpi_device *adev, void *data)
70 struct match_ids_walk_data *wd = data;
72 if (!acpi_match_device_ids(adev, wd->ids)) {
80 static void mfd_acpi_add_device(const struct mfd_cell *cell,
81 struct platform_device *pdev)
83 const struct mfd_cell_acpi_match *match = cell->acpi_match;
84 struct acpi_device *adev = NULL;
85 struct acpi_device *parent;
87 parent = ACPI_COMPANION(pdev->dev.parent);
92 * MFD child device gets its ACPI handle either from the ACPI device
93 * directly under the parent that matches the either _HID or _CID, or
94 * _ADR or it will use the parent handle if is no ID is given.
96 * Note that use of _ADR is a grey area in the ACPI specification,
97 * though at least Intel Galileo Gen 2 is using it to distinguish
98 * the children devices.
102 struct acpi_device_id ids[2] = {};
103 struct match_ids_walk_data wd = {
108 strscpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
109 acpi_dev_for_each_child(parent, match_device_ids, &wd);
112 adev = acpi_find_child_device(parent, match->adr, false);
116 ACPI_COMPANION_SET(&pdev->dev, adev ?: parent);
119 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
120 struct platform_device *pdev)
125 static int mfd_match_of_node_to_dev(struct platform_device *pdev,
126 struct device_node *np,
127 const struct mfd_cell *cell)
129 #if IS_ENABLED(CONFIG_OF)
130 struct mfd_of_node_entry *of_entry;
134 /* Skip if OF node has previously been allocated to a device */
135 list_for_each_entry(of_entry, &mfd_of_node_list, list)
136 if (of_entry->np == np)
139 if (!cell->use_of_reg)
140 /* No of_reg defined - allocate first free compatible match */
141 goto allocate_of_node;
143 /* We only care about each node's first defined address */
144 reg = of_get_address(np, 0, NULL, NULL);
146 /* OF node does not contatin a 'reg' property to match to */
149 of_node_addr = of_read_number(reg, of_n_addr_cells(np));
151 if (cell->of_reg != of_node_addr)
156 of_entry = kzalloc(sizeof(*of_entry), GFP_KERNEL);
160 of_entry->dev = &pdev->dev;
162 list_add_tail(&of_entry->list, &mfd_of_node_list);
164 pdev->dev.of_node = np;
165 pdev->dev.fwnode = &np->fwnode;
170 static int mfd_add_device(struct device *parent, int id,
171 const struct mfd_cell *cell,
172 struct resource *mem_base,
173 int irq_base, struct irq_domain *domain)
175 struct resource *res;
176 struct platform_device *pdev;
177 struct device_node *np = NULL;
178 struct mfd_of_node_entry *of_entry, *tmp;
183 if (id == PLATFORM_DEVID_AUTO)
186 platform_id = id + cell->id;
188 pdev = platform_device_alloc(cell->name, platform_id);
192 pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
196 res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
200 pdev->dev.parent = parent;
201 pdev->dev.type = &mfd_dev_type;
202 pdev->dev.dma_mask = parent->dma_mask;
203 pdev->dev.dma_parms = parent->dma_parms;
204 pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
206 ret = regulator_bulk_register_supply_alias(
207 &pdev->dev, cell->parent_supplies,
208 parent, cell->parent_supplies,
209 cell->num_parent_supplies);
213 if (IS_ENABLED(CONFIG_OF) && parent->of_node && cell->of_compatible) {
214 for_each_child_of_node(parent->of_node, np) {
215 if (of_device_is_compatible(np, cell->of_compatible)) {
216 /* Ignore 'disabled' devices error free */
217 if (!of_device_is_available(np)) {
223 ret = mfd_match_of_node_to_dev(pdev, np, cell);
234 if (!pdev->dev.of_node)
235 pr_warn("%s: Failed to locate of_node [id: %d]\n",
236 cell->name, platform_id);
239 mfd_acpi_add_device(cell, pdev);
241 if (cell->pdata_size) {
242 ret = platform_device_add_data(pdev,
243 cell->platform_data, cell->pdata_size);
249 ret = device_add_software_node(&pdev->dev, cell->swnode);
254 for (r = 0; r < cell->num_resources; r++) {
255 res[r].name = cell->resources[r].name;
256 res[r].flags = cell->resources[r].flags;
258 /* Find out base to use */
259 if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
260 res[r].parent = mem_base;
261 res[r].start = mem_base->start +
262 cell->resources[r].start;
263 res[r].end = mem_base->start +
264 cell->resources[r].end;
265 } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
267 /* Unable to create mappings for IRQ ranges. */
268 WARN_ON(cell->resources[r].start !=
269 cell->resources[r].end);
270 res[r].start = res[r].end = irq_create_mapping(
271 domain, cell->resources[r].start);
273 res[r].start = irq_base +
274 cell->resources[r].start;
275 res[r].end = irq_base +
276 cell->resources[r].end;
279 res[r].parent = cell->resources[r].parent;
280 res[r].start = cell->resources[r].start;
281 res[r].end = cell->resources[r].end;
284 if (!cell->ignore_resource_conflicts) {
285 if (has_acpi_companion(&pdev->dev)) {
286 ret = acpi_check_resource_conflict(&res[r]);
288 goto fail_res_conflict;
293 ret = platform_device_add_resources(pdev, res, cell->num_resources);
295 goto fail_res_conflict;
297 ret = platform_device_add(pdev);
299 goto fail_res_conflict;
301 if (cell->pm_runtime_no_callbacks)
302 pm_runtime_no_callbacks(&pdev->dev);
310 device_remove_software_node(&pdev->dev);
312 list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
313 if (of_entry->dev == &pdev->dev) {
314 list_del(&of_entry->list);
318 regulator_bulk_unregister_supply_alias(&pdev->dev,
319 cell->parent_supplies,
320 cell->num_parent_supplies);
324 platform_device_put(pdev);
330 * mfd_add_devices - register child devices
332 * @parent: Pointer to parent device.
333 * @id: Can be PLATFORM_DEVID_AUTO to let the Platform API take care
334 * of device numbering, or will be added to a device's cell_id.
335 * @cells: Array of (struct mfd_cell)s describing child devices.
336 * @n_devs: Number of child devices to register.
337 * @mem_base: Parent register range resource for child devices.
338 * @irq_base: Base of the range of virtual interrupt numbers allocated for
339 * this MFD device. Unused if @domain is specified.
340 * @domain: Interrupt domain to create mappings for hardware interrupts.
342 int mfd_add_devices(struct device *parent, int id,
343 const struct mfd_cell *cells, int n_devs,
344 struct resource *mem_base,
345 int irq_base, struct irq_domain *domain)
350 for (i = 0; i < n_devs; i++) {
351 ret = mfd_add_device(parent, id, cells + i, mem_base,
361 mfd_remove_devices(parent);
365 EXPORT_SYMBOL(mfd_add_devices);
367 static int mfd_remove_devices_fn(struct device *dev, void *data)
369 struct platform_device *pdev;
370 const struct mfd_cell *cell;
371 struct mfd_of_node_entry *of_entry, *tmp;
374 if (dev->type != &mfd_dev_type)
377 pdev = to_platform_device(dev);
378 cell = mfd_get_cell(pdev);
380 if (level && cell->level > *level)
384 device_remove_software_node(&pdev->dev);
386 list_for_each_entry_safe(of_entry, tmp, &mfd_of_node_list, list)
387 if (of_entry->dev == &pdev->dev) {
388 list_del(&of_entry->list);
392 regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
393 cell->num_parent_supplies);
395 platform_device_unregister(pdev);
399 void mfd_remove_devices_late(struct device *parent)
401 int level = MFD_DEP_LEVEL_HIGH;
403 device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
405 EXPORT_SYMBOL(mfd_remove_devices_late);
407 void mfd_remove_devices(struct device *parent)
409 int level = MFD_DEP_LEVEL_NORMAL;
411 device_for_each_child_reverse(parent, &level, mfd_remove_devices_fn);
413 EXPORT_SYMBOL(mfd_remove_devices);
415 static void devm_mfd_dev_release(struct device *dev, void *res)
417 mfd_remove_devices(dev);
421 * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
423 * Returns 0 on success or an appropriate negative error number on failure.
424 * All child-devices of the MFD will automatically be removed when it gets
427 * @dev: Pointer to parent device.
428 * @id: Can be PLATFORM_DEVID_AUTO to let the Platform API take care
429 * of device numbering, or will be added to a device's cell_id.
430 * @cells: Array of (struct mfd_cell)s describing child devices.
431 * @n_devs: Number of child devices to register.
432 * @mem_base: Parent register range resource for child devices.
433 * @irq_base: Base of the range of virtual interrupt numbers allocated for
434 * this MFD device. Unused if @domain is specified.
435 * @domain: Interrupt domain to create mappings for hardware interrupts.
437 int devm_mfd_add_devices(struct device *dev, int id,
438 const struct mfd_cell *cells, int n_devs,
439 struct resource *mem_base,
440 int irq_base, struct irq_domain *domain)
445 ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
449 ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
457 devres_add(dev, ptr);
461 EXPORT_SYMBOL(devm_mfd_add_devices);
463 MODULE_LICENSE("GPL");
464 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");