[ARM] S3C24XX: Reduce code lineage of gpiolib.c
[safe/jmp/linux-2.6] / arch / arm / plat-s3c24xx / gpiolib.c
1 /* linux/arch/arm/plat-s3c24xx/gpiolib.c
2  *
3  * Copyright (c) 2008 Simtec Electronics
4  *      http://armlinux.simtec.co.uk/
5  *      Ben Dooks <ben@simtec.co.uk>
6  *
7  * S3C24XX GPIOlib support
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License.
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/io.h>
20 #include <linux/gpio.h>
21
22 #include <mach/hardware.h>
23 #include <asm/irq.h>
24
25 #include <mach/regs-gpio.h>
26
27 struct s3c24xx_gpio_chip {
28         struct gpio_chip        chip;
29         void __iomem            *base;
30 };
31
32 static inline struct s3c24xx_gpio_chip *to_s3c_chip(struct gpio_chip *gpc)
33 {
34         return container_of(gpc, struct s3c24xx_gpio_chip, chip);
35 }
36
37 /* these routines are exported for use by other parts of the platform
38  * and system support, but are not intended to be used directly by the
39  * drivers themsevles.
40  */
41
42 static int s3c24xx_gpiolib_input(struct gpio_chip *chip, unsigned offset)
43 {
44         struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
45         void __iomem *base = ourchip->base;
46         unsigned long flags;
47         unsigned long con;
48
49         local_irq_save(flags);
50
51         con = __raw_readl(base + 0x00);
52         con &= ~(3 << (offset * 2));
53         con |= (S3C2410_GPIO_OUTPUT & 0xf) << (offset * 2);
54
55         __raw_writel(con, base + 0x00);
56
57         local_irq_restore(flags);
58         return 0;
59 }
60
61 static int s3c24xx_gpiolib_output(struct gpio_chip *chip,
62                                   unsigned offset, int value)
63 {
64         struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
65         void __iomem *base = ourchip->base;
66         unsigned long flags;
67         unsigned long dat;
68         unsigned long con;
69
70         local_irq_save(flags);
71
72         dat = __raw_readl(base + 0x04);
73         dat &= ~(1 << offset);
74         if (value)
75                 dat |= 1 << offset;
76         __raw_writel(dat, base + 0x04);
77
78         con = __raw_readl(base + 0x00);
79         con &= ~(3 << (offset * 2));
80         con |= (S3C2410_GPIO_OUTPUT & 0xf) << (offset * 2);
81
82         __raw_writel(con, base + 0x00);
83         __raw_writel(dat, base + 0x04);
84
85         local_irq_restore(flags);
86         return 0;
87 }
88
89 static void s3c24xx_gpiolib_set(struct gpio_chip *chip,
90                                 unsigned offset, int value)
91 {
92         struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
93         void __iomem *base = ourchip->base;
94         unsigned long flags;
95         unsigned long dat;
96
97         local_irq_save(flags);
98
99         dat = __raw_readl(base + 0x04);
100         dat &= ~(1 << offset);
101         if (value)
102                 dat |= 1 << offset;
103         __raw_writel(dat, base + 0x04);
104
105         local_irq_restore(flags);
106 }
107
108 static int s3c24xx_gpiolib_get(struct gpio_chip *chip, unsigned offset)
109 {
110         struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
111         unsigned long val;
112
113         val = __raw_readl(ourchip->base + 0x04);
114         val >>= offset;
115         val &= 1;
116
117         return val;
118 }
119
120 static int s3c24xx_gpiolib_banka_input(struct gpio_chip *chip, unsigned offset)
121 {
122         return -EINVAL;
123 }
124
125 static int s3c24xx_gpiolib_banka_output(struct gpio_chip *chip,
126                                         unsigned offset, int value)
127 {
128         struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
129         void __iomem *base = ourchip->base;
130         unsigned long flags;
131         unsigned long dat;
132         unsigned long con;
133
134         local_irq_save(flags);
135
136         con = __raw_readl(base + 0x00);
137         dat = __raw_readl(base + 0x04);
138
139         dat &= ~(1 << offset);
140         if (value)
141                 dat |= 1 << offset;
142
143         __raw_writel(dat, base + 0x04);
144
145         con &= ~(1 << offset);
146
147         __raw_writel(con, base + 0x00);
148         __raw_writel(dat, base + 0x04);
149
150         local_irq_restore(flags);
151         return 0;
152 }
153
154 static struct s3c24xx_gpio_chip gpios[] = {
155         [0] = {
156                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPA0),
157                 .chip   = {
158                         .base                   = S3C2410_GPA0,
159                         .owner                  = THIS_MODULE,
160                         .label                  = "GPIOA",
161                         .ngpio                  = 24,
162                         .direction_input        = s3c24xx_gpiolib_banka_input,
163                         .direction_output       = s3c24xx_gpiolib_banka_output,
164                 },
165         },
166         [1] = {
167                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPB0),
168                 .chip   = {
169                         .base                   = S3C2410_GPB0,
170                         .owner                  = THIS_MODULE,
171                         .label                  = "GPIOB",
172                         .ngpio                  = 16,
173                 },
174         },
175         [2] = {
176                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPC0),
177                 .chip   = {
178                         .base                   = S3C2410_GPC0,
179                         .owner                  = THIS_MODULE,
180                         .label                  = "GPIOC",
181                         .ngpio                  = 16,
182                 },
183         },
184         [3] = {
185                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPD0),
186                 .chip   = {
187                         .base                   = S3C2410_GPD0,
188                         .owner                  = THIS_MODULE,
189                         .label                  = "GPIOD",
190                         .ngpio                  = 16,
191                 },
192         },
193         [4] = {
194                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPE0),
195                 .chip   = {
196                         .base                   = S3C2410_GPE0,
197                         .label                  = "GPIOE",
198                         .owner                  = THIS_MODULE,
199                         .ngpio                  = 16,
200                 },
201         },
202         [5] = {
203                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPF0),
204                 .chip   = {
205                         .base                   = S3C2410_GPF0,
206                         .owner                  = THIS_MODULE,
207                         .label                  = "GPIOF",
208                         .ngpio                  = 8,
209                 },
210         },
211         [6] = {
212                 .base   = S3C24XX_GPIO_BASE(S3C2410_GPG0),
213                 .chip   = {
214                         .base                   = S3C2410_GPG0,
215                         .owner                  = THIS_MODULE,
216                         .label                  = "GPIOG",
217                         .ngpio                  = 10,
218                 },
219         },
220 };
221
222 static __init void s3c24xx_gpiolib_add(struct s3c24xx_gpio_chip *chip)
223 {
224         struct gpio_chip *gc = &chip->chip;
225
226         BUG_ON(!chip->base);
227         BUG_ON(!gc->label);
228         BUG_ON(!gc->ngpio);
229
230         if (!gc->direction_input)
231                 gc->direction_input = s3c24xx_gpiolib_input;
232         if (!gc->direction_output)
233                 gc->direction_output = s3c24xx_gpiolib_output;
234         if (!gc->set)
235                 gc->set = s3c24xx_gpiolib_set;
236         if (!gc->get)
237                 gc->get = s3c24xx_gpiolib_get;
238
239         /* gpiochip_add() prints own failure message on error. */
240         gpiochip_add(gc);
241 }
242
243 static __init int s3c24xx_gpiolib_init(void)
244 {
245         struct s3c24xx_gpio_chip *chip = gpios;
246         int gpn;
247
248         for (gpn = 0; gpn < ARRAY_SIZE(gpios); gpn++, chip++)
249                 s3c24xx_gpiolib_add(chip);
250
251         return 0;
252 }
253
254 arch_initcall(s3c24xx_gpiolib_init);