xref: /linux/drivers/mfd/mfd-core.c (revision 765532c8aaac624b5f8687af6d319c6a1138a257)
1 /*
2  * drivers/mfd/mfd-core.c
3  *
4  * core MFD support
5  * Copyright (c) 2006 Ian Molton
6  * Copyright (c) 2007,2008 Dmitry Baryshkov
7  *
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.
11  *
12  */
13 
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/slab.h>
19 
20 static int mfd_add_device(struct device *parent, int id,
21 			  const struct mfd_cell *cell,
22 			  struct resource *mem_base,
23 			  int irq_base)
24 {
25 	struct resource *res;
26 	struct platform_device *pdev;
27 	int ret = -ENOMEM;
28 	int r;
29 
30 	pdev = platform_device_alloc(cell->name, id + cell->id);
31 	if (!pdev)
32 		goto fail_alloc;
33 
34 	res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
35 	if (!res)
36 		goto fail_device;
37 
38 	pdev->dev.parent = parent;
39 	platform_set_drvdata(pdev, cell->driver_data);
40 
41 	if (cell->data_size) {
42 		ret = platform_device_add_data(pdev,
43 					cell->platform_data, cell->data_size);
44 		if (ret)
45 			goto fail_res;
46 	}
47 
48 	for (r = 0; r < cell->num_resources; r++) {
49 		res[r].name = cell->resources[r].name;
50 		res[r].flags = cell->resources[r].flags;
51 
52 		/* Find out base to use */
53 		if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
54 			res[r].parent = mem_base;
55 			res[r].start = mem_base->start +
56 				cell->resources[r].start;
57 			res[r].end = mem_base->start +
58 				cell->resources[r].end;
59 		} else if (cell->resources[r].flags & IORESOURCE_IRQ) {
60 			res[r].start = irq_base +
61 				cell->resources[r].start;
62 			res[r].end   = irq_base +
63 				cell->resources[r].end;
64 		} else {
65 			res[r].parent = cell->resources[r].parent;
66 			res[r].start = cell->resources[r].start;
67 			res[r].end   = cell->resources[r].end;
68 		}
69 
70 		if (!cell->ignore_resource_conflicts) {
71 			ret = acpi_check_resource_conflict(res);
72 			if (ret)
73 				goto fail_res;
74 		}
75 	}
76 
77 	ret = platform_device_add_resources(pdev, res, cell->num_resources);
78 	if (ret)
79 		goto fail_res;
80 
81 	ret = platform_device_add(pdev);
82 	if (ret)
83 		goto fail_res;
84 
85 	kfree(res);
86 
87 	return 0;
88 
89 /*	platform_device_del(pdev); */
90 fail_res:
91 	kfree(res);
92 fail_device:
93 	platform_device_put(pdev);
94 fail_alloc:
95 	return ret;
96 }
97 
98 int mfd_add_devices(struct device *parent, int id,
99 		    const struct mfd_cell *cells, int n_devs,
100 		    struct resource *mem_base,
101 		    int irq_base)
102 {
103 	int i;
104 	int ret = 0;
105 
106 	for (i = 0; i < n_devs; i++) {
107 		ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
108 		if (ret)
109 			break;
110 	}
111 
112 	if (ret)
113 		mfd_remove_devices(parent);
114 
115 	return ret;
116 }
117 EXPORT_SYMBOL(mfd_add_devices);
118 
119 static int mfd_remove_devices_fn(struct device *dev, void *unused)
120 {
121 	platform_device_unregister(to_platform_device(dev));
122 	return 0;
123 }
124 
125 void mfd_remove_devices(struct device *parent)
126 {
127 	device_for_each_child(parent, NULL, mfd_remove_devices_fn);
128 }
129 EXPORT_SYMBOL(mfd_remove_devices);
130 
131 MODULE_LICENSE("GPL");
132 MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");
133