9376a246f862526bbd892882755196ce6aa90018
[linux-2.6.git] / arch / arm / mach-ux500 / devices-common.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  *
4  * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
5  * License terms: GNU General Public License (GPL), version 2.
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/irq.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/amba/bus.h>
15
16 #include <mach/hardware.h>
17
18 #include "devices-common.h"
19
20 struct amba_device *
21 dbx500_add_amba_device(const char *name, resource_size_t base,
22                        int irq, void *pdata, unsigned int periphid)
23 {
24         struct amba_device *dev;
25         int ret;
26
27         dev = kzalloc(sizeof *dev, GFP_KERNEL);
28         if (!dev)
29                 return ERR_PTR(-ENOMEM);
30
31         dev->dev.init_name = name;
32
33         dev->res.start = base;
34         dev->res.end = base + SZ_4K - 1;
35         dev->res.flags = IORESOURCE_MEM;
36
37         dev->dma_mask = DMA_BIT_MASK(32);
38         dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
39
40         dev->irq[0] = irq;
41         dev->irq[1] = NO_IRQ;
42
43         dev->periphid = periphid;
44
45         dev->dev.platform_data = pdata;
46
47         ret = amba_device_register(dev, &iomem_resource);
48         if (ret) {
49                 kfree(dev);
50                 return ERR_PTR(ret);
51         }
52
53         return dev;
54 }
55
56 static struct platform_device *
57 dbx500_add_platform_device(const char *name, int id, void *pdata,
58                            struct resource *res, int resnum)
59 {
60         struct platform_device *dev;
61         int ret;
62
63         dev = platform_device_alloc(name, id);
64         if (!dev)
65                 return ERR_PTR(-ENOMEM);
66
67         dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
68         dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
69
70         ret = platform_device_add_resources(dev, res, resnum);
71         if (ret)
72                 goto out_free;
73
74         dev->dev.platform_data = pdata;
75
76         ret = platform_device_add(dev);
77         if (ret)
78                 goto out_free;
79
80         return dev;
81
82 out_free:
83         platform_device_put(dev);
84         return ERR_PTR(ret);
85 }
86
87 struct platform_device *
88 dbx500_add_platform_device_4k1irq(const char *name, int id,
89                                   resource_size_t base,
90                                   int irq, void *pdata)
91 {
92         struct resource resources[] = {
93                 [0] = {
94                         .start  = base,
95                         .end    = base + SZ_4K - 1,
96                         .flags  = IORESOURCE_MEM,
97                 },
98                 [1] = {
99                         .start  = irq,
100                         .end    = irq,
101                         .flags  = IORESOURCE_IRQ,
102                 }
103         };
104
105         return dbx500_add_platform_device(name, id, pdata, resources,
106                                           ARRAY_SIZE(resources));
107 }