ux500: rework gpio registration
[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 <plat/gpio.h>
17
18 #include <mach/hardware.h>
19
20 #include "devices-common.h"
21
22 struct amba_device *
23 dbx500_add_amba_device(const char *name, resource_size_t base,
24                        int irq, void *pdata, unsigned int periphid)
25 {
26         struct amba_device *dev;
27         int ret;
28
29         dev = kzalloc(sizeof *dev, GFP_KERNEL);
30         if (!dev)
31                 return ERR_PTR(-ENOMEM);
32
33         dev->dev.init_name = name;
34
35         dev->res.start = base;
36         dev->res.end = base + SZ_4K - 1;
37         dev->res.flags = IORESOURCE_MEM;
38
39         dev->dma_mask = DMA_BIT_MASK(32);
40         dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
41
42         dev->irq[0] = irq;
43         dev->irq[1] = NO_IRQ;
44
45         dev->periphid = periphid;
46
47         dev->dev.platform_data = pdata;
48
49         ret = amba_device_register(dev, &iomem_resource);
50         if (ret) {
51                 kfree(dev);
52                 return ERR_PTR(ret);
53         }
54
55         return dev;
56 }
57
58 static struct platform_device *
59 dbx500_add_platform_device(const char *name, int id, void *pdata,
60                            struct resource *res, int resnum)
61 {
62         struct platform_device *dev;
63         int ret;
64
65         dev = platform_device_alloc(name, id);
66         if (!dev)
67                 return ERR_PTR(-ENOMEM);
68
69         dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
70         dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
71
72         ret = platform_device_add_resources(dev, res, resnum);
73         if (ret)
74                 goto out_free;
75
76         dev->dev.platform_data = pdata;
77
78         ret = platform_device_add(dev);
79         if (ret)
80                 goto out_free;
81
82         return dev;
83
84 out_free:
85         platform_device_put(dev);
86         return ERR_PTR(ret);
87 }
88
89 struct platform_device *
90 dbx500_add_platform_device_4k1irq(const char *name, int id,
91                                   resource_size_t base,
92                                   int irq, void *pdata)
93 {
94         struct resource resources[] = {
95                 [0] = {
96                         .start  = base,
97                         .end    = base + SZ_4K - 1,
98                         .flags  = IORESOURCE_MEM,
99                 },
100                 [1] = {
101                         .start  = irq,
102                         .end    = irq,
103                         .flags  = IORESOURCE_IRQ,
104                 }
105         };
106
107         return dbx500_add_platform_device(name, id, pdata, resources,
108                                           ARRAY_SIZE(resources));
109 }
110
111 static struct platform_device *
112 dbx500_add_gpio(int id, resource_size_t addr, int irq,
113                 struct nmk_gpio_platform_data *pdata)
114 {
115         struct resource resources[] = {
116                 {
117                         .start  = addr,
118                         .end    = addr + 127,
119                         .flags  = IORESOURCE_MEM,
120                 },
121                 {
122                         .start  = irq,
123                         .end    = irq,
124                         .flags  = IORESOURCE_IRQ,
125                 }
126         };
127
128         return platform_device_register_resndata(NULL, "gpio", id,
129                                 resources, ARRAY_SIZE(resources),
130                                 pdata, sizeof(*pdata));
131 }
132
133 void dbx500_add_gpios(resource_size_t *base, int num, int irq,
134                       struct nmk_gpio_platform_data *pdata)
135 {
136         int first = 0;
137         int i;
138
139         for (i = 0; i < num; i++, first += 32, irq++) {
140                 pdata->first_gpio = first;
141                 pdata->first_irq = NOMADIK_GPIO_TO_IRQ(first);
142
143                 dbx500_add_gpio(i, base[i], irq, pdata);
144         }
145 }