mfd-core.c 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * drivers/mfd/mfd-core.c
 *
 * core MFD support
 * Copyright (c) 2006 Ian Molton
 * Copyright (c) 2007,2008 Dmitry Baryshkov
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include <linux/kernel.h>
#include <linux/platform_device.h>
S
Samuel Ortiz 已提交
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,
B
Ben Dooks 已提交
21 22 23
			  const struct mfd_cell *cell,
			  struct resource *mem_base,
			  int irq_base)
24
{
I
Ian Molton 已提交
25
	struct resource *res;
26 27 28 29
	struct platform_device *pdev;
	int ret = -ENOMEM;
	int r;

30
	pdev = platform_device_alloc(cell->name, id + cell->id);
31 32 33
	if (!pdev)
		goto fail_alloc;

I
Ian Molton 已提交
34 35 36 37
	res = kzalloc(sizeof(*res) * cell->num_resources, GFP_KERNEL);
	if (!res)
		goto fail_device;

38
	pdev->dev.parent = parent;
39
	platform_set_drvdata(pdev, cell->driver_data);
40 41

	ret = platform_device_add_data(pdev,
42
			cell->platform_data, cell->data_size);
43
	if (ret)
I
Ian Molton 已提交
44
		goto fail_res;
45 46 47 48 49 50

	for (r = 0; r < cell->num_resources; r++) {
		res[r].name = cell->resources[r].name;
		res[r].flags = cell->resources[r].flags;

		/* Find out base to use */
51
		if ((cell->resources[r].flags & IORESOURCE_MEM) && mem_base) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
			res[r].parent = mem_base;
			res[r].start = mem_base->start +
				cell->resources[r].start;
			res[r].end = mem_base->start +
				cell->resources[r].end;
		} else if (cell->resources[r].flags & IORESOURCE_IRQ) {
			res[r].start = irq_base +
				cell->resources[r].start;
			res[r].end   = irq_base +
				cell->resources[r].end;
		} else {
			res[r].parent = cell->resources[r].parent;
			res[r].start = cell->resources[r].start;
			res[r].end   = cell->resources[r].end;
		}
S
Samuel Ortiz 已提交
67 68 69 70

		ret = acpi_check_resource_conflict(res);
		if (ret)
			goto fail_res;
71 72 73 74 75 76
	}

	platform_device_add_resources(pdev, res, cell->num_resources);

	ret = platform_device_add(pdev);
	if (ret)
I
Ian Molton 已提交
77 78 79
		goto fail_res;

	kfree(res);
80 81 82 83

	return 0;

/*	platform_device_del(pdev); */
I
Ian Molton 已提交
84 85
fail_res:
	kfree(res);
86 87 88 89 90 91
fail_device:
	platform_device_put(pdev);
fail_alloc:
	return ret;
}

92
int mfd_add_devices(struct device *parent, int id,
B
Ben Dooks 已提交
93 94 95
		    const struct mfd_cell *cells, int n_devs,
		    struct resource *mem_base,
		    int irq_base)
96 97 98 99 100
{
	int i;
	int ret = 0;

	for (i = 0; i < n_devs; i++) {
101
		ret = mfd_add_device(parent, id, cells + i, mem_base, irq_base);
102 103 104 105 106 107 108 109 110 111 112 113 114
		if (ret)
			break;
	}

	if (ret)
		mfd_remove_devices(parent);

	return ret;
}
EXPORT_SYMBOL(mfd_add_devices);

static int mfd_remove_devices_fn(struct device *dev, void *unused)
{
115
	platform_device_unregister(to_platform_device(dev));
116 117 118
	return 0;
}

119
void mfd_remove_devices(struct device *parent)
120
{
121
	device_for_each_child(parent, NULL, mfd_remove_devices_fn);
122 123 124 125 126
}
EXPORT_SYMBOL(mfd_remove_devices);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Ian Molton, Dmitry Baryshkov");