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/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>
20 #include <linux/regulator/consumer.h>
22 static struct device_type mfd_dev_type = {
26 int mfd_cell_enable(struct platform_device *pdev)
28 const struct mfd_cell *cell = mfd_get_cell(pdev);
31 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
35 return cell->enable(pdev);
37 EXPORT_SYMBOL(mfd_cell_enable);
39 int mfd_cell_disable(struct platform_device *pdev)
41 const struct mfd_cell *cell = mfd_get_cell(pdev);
44 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
48 return cell->disable(pdev);
50 EXPORT_SYMBOL(mfd_cell_disable);
52 static int mfd_platform_add_cell(struct platform_device *pdev,
53 const struct mfd_cell *cell)
58 pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
65 #if IS_ENABLED(CONFIG_ACPI)
66 static void mfd_acpi_add_device(const struct mfd_cell *cell,
67 struct platform_device *pdev)
69 const struct mfd_cell_acpi_match *match = cell->acpi_match;
70 struct acpi_device *parent, *child;
71 struct acpi_device *adev;
73 parent = ACPI_COMPANION(pdev->dev.parent);
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.
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
89 struct acpi_device_id ids[2] = {};
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)) {
99 unsigned long long adr;
102 list_for_each_entry(child, &parent->children, node) {
103 status = acpi_evaluate_integer(child->handle,
106 if (ACPI_SUCCESS(status) && match->adr == adr) {
114 ACPI_COMPANION_SET(&pdev->dev, adev);
117 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
118 struct platform_device *pdev)
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)
128 struct resource *res;
129 struct platform_device *pdev;
130 struct device_node *np = NULL;
135 if (id == PLATFORM_DEVID_AUTO)
138 platform_id = id + cell->id;
140 pdev = platform_device_alloc(cell->name, platform_id);
144 res = kcalloc(cell->num_resources, sizeof(*res), GFP_KERNEL);
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;
154 ret = regulator_bulk_register_supply_alias(
155 &pdev->dev, cell->parent_supplies,
156 parent, cell->parent_supplies,
157 cell->num_parent_supplies);
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;
171 mfd_acpi_add_device(cell, pdev);
173 if (cell->pdata_size) {
174 ret = platform_device_add_data(pdev,
175 cell->platform_data, cell->pdata_size);
180 if (cell->properties) {
181 ret = platform_device_add_properties(pdev, cell->properties);
186 ret = mfd_platform_add_cell(pdev, cell);
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;
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) {
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);
209 res[r].start = irq_base +
210 cell->resources[r].start;
211 res[r].end = irq_base +
212 cell->resources[r].end;
215 res[r].parent = cell->resources[r].parent;
216 res[r].start = cell->resources[r].start;
217 res[r].end = cell->resources[r].end;
220 if (!cell->ignore_resource_conflicts) {
221 if (has_acpi_companion(&pdev->dev)) {
222 ret = acpi_check_resource_conflict(&res[r]);
229 ret = platform_device_add_resources(pdev, res, cell->num_resources);
233 ret = platform_device_add(pdev);
237 if (cell->pm_runtime_no_callbacks)
238 pm_runtime_no_callbacks(&pdev->dev);
245 regulator_bulk_unregister_supply_alias(&pdev->dev,
246 cell->parent_supplies,
247 cell->num_parent_supplies);
251 platform_device_put(pdev);
257 * mfd_add_devices - register child devices
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.
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)
277 for (i = 0; i < n_devs; i++) {
278 ret = mfd_add_device(parent, id, cells + i, mem_base,
288 mfd_remove_devices(parent);
292 EXPORT_SYMBOL(mfd_add_devices);
294 static int mfd_remove_devices_fn(struct device *dev, void *data)
296 struct platform_device *pdev;
297 const struct mfd_cell *cell;
299 if (dev->type != &mfd_dev_type)
302 pdev = to_platform_device(dev);
303 cell = mfd_get_cell(pdev);
305 regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
306 cell->num_parent_supplies);
308 platform_device_unregister(pdev);
312 void mfd_remove_devices(struct device *parent)
314 device_for_each_child_reverse(parent, NULL, mfd_remove_devices_fn);
316 EXPORT_SYMBOL(mfd_remove_devices);
318 static void devm_mfd_dev_release(struct device *dev, void *res)
320 mfd_remove_devices(dev);
324 * devm_mfd_add_devices - Resource managed version of mfd_add_devices()
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
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)
338 ptr = devres_alloc(devm_mfd_dev_release, sizeof(*ptr), GFP_KERNEL);
342 ret = mfd_add_devices(dev, id, cells, n_devs, mem_base,
350 devres_add(dev, ptr);
354 EXPORT_SYMBOL(devm_mfd_add_devices);
356 MODULE_LICENSE("GPL");
357 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");