2 * drivers/mfd/mfd-core.c
5 * Copyright (c) 2006 Ian Molton
6 * Copyright (c) 2007,2008 Dmitry Baryshkov
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/acpi.h>
17 #include <linux/mfd/core.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/irqdomain.h>
23 #include <linux/regulator/consumer.h>
25 static struct device_type mfd_dev_type = {
29 int mfd_cell_enable(struct platform_device *pdev)
31 const struct mfd_cell *cell = mfd_get_cell(pdev);
35 dev_dbg(&pdev->dev, "No .enable() call-back registered\n");
39 /* only call enable hook if the cell wasn't previously enabled */
40 if (atomic_inc_return(cell->usage_count) == 1)
41 err = cell->enable(pdev);
43 /* if the enable hook failed, decrement counter to allow retries */
45 atomic_dec(cell->usage_count);
49 EXPORT_SYMBOL(mfd_cell_enable);
51 int mfd_cell_disable(struct platform_device *pdev)
53 const struct mfd_cell *cell = mfd_get_cell(pdev);
57 dev_dbg(&pdev->dev, "No .disable() call-back registered\n");
61 /* only disable if no other clients are using it */
62 if (atomic_dec_return(cell->usage_count) == 0)
63 err = cell->disable(pdev);
65 /* if the disable hook failed, increment to allow retries */
67 atomic_inc(cell->usage_count);
69 /* sanity check; did someone call disable too many times? */
70 WARN_ON(atomic_read(cell->usage_count) < 0);
74 EXPORT_SYMBOL(mfd_cell_disable);
76 static int mfd_platform_add_cell(struct platform_device *pdev,
77 const struct mfd_cell *cell,
78 atomic_t *usage_count)
83 pdev->mfd_cell = kmemdup(cell, sizeof(*cell), GFP_KERNEL);
87 pdev->mfd_cell->usage_count = usage_count;
91 #if IS_ENABLED(CONFIG_ACPI)
92 static void mfd_acpi_add_device(const struct mfd_cell *cell,
93 struct platform_device *pdev)
95 const struct mfd_cell_acpi_match *match = cell->acpi_match;
96 struct acpi_device *parent, *child;
97 struct acpi_device *adev;
99 parent = ACPI_COMPANION(pdev->dev.parent);
104 * MFD child device gets its ACPI handle either from the ACPI device
105 * directly under the parent that matches the either _HID or _CID, or
106 * _ADR or it will use the parent handle if is no ID is given.
108 * Note that use of _ADR is a grey area in the ACPI specification,
109 * though Intel Galileo Gen2 is using it to distinguish the children
115 struct acpi_device_id ids[2] = {};
117 strlcpy(ids[0].id, match->pnpid, sizeof(ids[0].id));
118 list_for_each_entry(child, &parent->children, node) {
119 if (acpi_match_device_ids(child, ids)) {
125 unsigned long long adr;
128 list_for_each_entry(child, &parent->children, node) {
129 status = acpi_evaluate_integer(child->handle,
132 if (ACPI_SUCCESS(status) && match->adr == adr) {
140 ACPI_COMPANION_SET(&pdev->dev, adev);
143 static inline void mfd_acpi_add_device(const struct mfd_cell *cell,
144 struct platform_device *pdev)
149 static int mfd_add_device(struct device *parent, int id,
150 const struct mfd_cell *cell, atomic_t *usage_count,
151 struct resource *mem_base,
152 int irq_base, struct irq_domain *domain)
154 struct resource *res;
155 struct platform_device *pdev;
156 struct device_node *np = NULL;
161 if (id == PLATFORM_DEVID_AUTO)
164 platform_id = id + cell->id;
166 pdev = platform_device_alloc(cell->name, platform_id);
170 res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
174 pdev->dev.parent = parent;
175 pdev->dev.type = &mfd_dev_type;
176 pdev->dev.dma_mask = parent->dma_mask;
177 pdev->dev.dma_parms = parent->dma_parms;
178 pdev->dev.coherent_dma_mask = parent->coherent_dma_mask;
180 ret = regulator_bulk_register_supply_alias(
181 &pdev->dev, cell->parent_supplies,
182 parent, cell->parent_supplies,
183 cell->num_parent_supplies);
187 if (parent->of_node && cell->of_compatible) {
188 for_each_child_of_node(parent->of_node, np) {
189 if (of_device_is_compatible(np, cell->of_compatible)) {
190 pdev->dev.of_node = np;
191 pdev->dev.fwnode = &np->fwnode;
197 mfd_acpi_add_device(cell, pdev);
199 if (cell->pdata_size) {
200 ret = platform_device_add_data(pdev,
201 cell->platform_data, cell->pdata_size);
206 ret = mfd_platform_add_cell(pdev, cell, usage_count);
210 for (r = 0; r < cell->num_resources; r++) {
211 res[r].name = cell->resources[r].name;
212 res[r].flags = cell->resources[r].flags;
214 /* Find out base to use */
215 if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
216 res[r].parent = mem_base;
217 res[r].start = mem_base->start +
218 cell->resources[r].start;
219 res[r].end = mem_base->start +
220 cell->resources[r].end;
221 } else if (cell->resources[r].flags & IORESOURCE_IRQ) {
223 /* Unable to create mappings for IRQ ranges. */
224 WARN_ON(cell->resources[r].start !=
225 cell->resources[r].end);
226 res[r].start = res[r].end = irq_create_mapping(
227 domain, cell->resources[r].start);
229 res[r].start = irq_base +
230 cell->resources[r].start;
231 res[r].end = irq_base +
232 cell->resources[r].end;
235 res[r].parent = cell->resources[r].parent;
236 res[r].start = cell->resources[r].start;
237 res[r].end = cell->resources[r].end;
240 if (!cell->ignore_resource_conflicts) {
241 if (has_acpi_companion(&pdev->dev)) {
242 ret = acpi_check_resource_conflict(&res[r]);
249 ret = platform_device_add_resources(pdev, res, cell->num_resources);
253 ret = platform_device_add(pdev);
257 if (cell->pm_runtime_no_callbacks)
258 pm_runtime_no_callbacks(&pdev->dev);
265 regulator_bulk_unregister_supply_alias(&pdev->dev,
266 cell->parent_supplies,
267 cell->num_parent_supplies);
271 platform_device_put(pdev);
276 int mfd_add_devices(struct device *parent, int id,
277 const struct mfd_cell *cells, int n_devs,
278 struct resource *mem_base,
279 int irq_base, struct irq_domain *domain)
285 /* initialize reference counting for all cells */
286 cnts = kcalloc(n_devs, sizeof(*cnts), GFP_KERNEL);
290 for (i = 0; i < n_devs; i++) {
291 atomic_set(&cnts[i], 0);
292 ret = mfd_add_device(parent, id, cells + i, cnts + i, mem_base,
302 mfd_remove_devices(parent);
307 EXPORT_SYMBOL(mfd_add_devices);
309 static int mfd_remove_devices_fn(struct device *dev, void *c)
311 struct platform_device *pdev;
312 const struct mfd_cell *cell;
313 atomic_t **usage_count = c;
315 if (dev->type != &mfd_dev_type)
318 pdev = to_platform_device(dev);
319 cell = mfd_get_cell(pdev);
321 regulator_bulk_unregister_supply_alias(dev, cell->parent_supplies,
322 cell->num_parent_supplies);
324 /* find the base address of usage_count pointers (for freeing) */
325 if (!*usage_count || (cell->usage_count < *usage_count))
326 *usage_count = cell->usage_count;
328 platform_device_unregister(pdev);
332 void mfd_remove_devices(struct device *parent)
334 atomic_t *cnts = NULL;
336 device_for_each_child_reverse(parent, &cnts, mfd_remove_devices_fn);
339 EXPORT_SYMBOL(mfd_remove_devices);
341 int mfd_clone_cell(const char *cell, const char **clones, size_t n_clones)
343 struct mfd_cell cell_entry;
345 struct platform_device *pdev;
348 /* fetch the parent cell's device (should already be registered!) */
349 dev = bus_find_device_by_name(&platform_bus_type, NULL, cell);
351 printk(KERN_ERR "failed to find device for cell %s\n", cell);
354 pdev = to_platform_device(dev);
355 memcpy(&cell_entry, mfd_get_cell(pdev), sizeof(cell_entry));
357 WARN_ON(!cell_entry.enable);
359 for (i = 0; i < n_clones; i++) {
360 cell_entry.name = clones[i];
361 /* don't give up if a single call fails; just report error */
362 if (mfd_add_device(pdev->dev.parent, -1, &cell_entry,
363 cell_entry.usage_count, NULL, 0, NULL))
364 dev_err(dev, "failed to create platform device '%s'\n",
372 EXPORT_SYMBOL(mfd_clone_cell);
374 MODULE_LICENSE("GPL");
375 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");