1 /* linux/arch/arm/plat-s3c24xx/gpiolib.c
3 * Copyright (c) 2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
7 * S3C24XX GPIOlib support
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.
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>
20 #include <linux/gpio.h>
22 #include <mach/hardware.h>
25 #include <mach/regs-gpio.h>
27 struct s3c24xx_gpio_chip {
28 struct gpio_chip chip;
32 static inline struct s3c24xx_gpio_chip *to_s3c_chip(struct gpio_chip *gpc)
34 return container_of(gpc, struct s3c24xx_gpio_chip, chip);
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
42 static int s3c24xx_gpiolib_input(struct gpio_chip *chip, unsigned offset)
44 struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
45 void __iomem *base = ourchip->base;
49 local_irq_save(flags);
51 con = __raw_readl(base + 0x00);
52 con &= ~(3 << (offset * 2));
53 con |= (S3C2410_GPIO_OUTPUT & 0xf) << (offset * 2);
55 __raw_writel(con, base + 0x00);
57 local_irq_restore(flags);
61 static int s3c24xx_gpiolib_output(struct gpio_chip *chip,
62 unsigned offset, int value)
64 struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
65 void __iomem *base = ourchip->base;
70 local_irq_save(flags);
72 dat = __raw_readl(base + 0x04);
73 dat &= ~(1 << offset);
76 __raw_writel(dat, base + 0x04);
78 con = __raw_readl(base + 0x00);
79 con &= ~(3 << (offset * 2));
80 con |= (S3C2410_GPIO_OUTPUT & 0xf) << (offset * 2);
82 __raw_writel(con, base + 0x00);
83 __raw_writel(dat, base + 0x04);
85 local_irq_restore(flags);
89 static void s3c24xx_gpiolib_set(struct gpio_chip *chip,
90 unsigned offset, int value)
92 struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
93 void __iomem *base = ourchip->base;
97 local_irq_save(flags);
99 dat = __raw_readl(base + 0x04);
100 dat &= ~(1 << offset);
103 __raw_writel(dat, base + 0x04);
105 local_irq_restore(flags);
108 static int s3c24xx_gpiolib_get(struct gpio_chip *chip, unsigned offset)
110 struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
113 val = __raw_readl(ourchip->base + 0x04);
120 static int s3c24xx_gpiolib_banka_input(struct gpio_chip *chip, unsigned offset)
125 static int s3c24xx_gpiolib_banka_output(struct gpio_chip *chip,
126 unsigned offset, int value)
128 struct s3c24xx_gpio_chip *ourchip = to_s3c_chip(chip);
129 void __iomem *base = ourchip->base;
134 local_irq_save(flags);
136 con = __raw_readl(base + 0x00);
137 dat = __raw_readl(base + 0x04);
139 dat &= ~(1 << offset);
143 __raw_writel(dat, base + 0x04);
145 con &= ~(1 << offset);
147 __raw_writel(con, base + 0x00);
148 __raw_writel(dat, base + 0x04);
150 local_irq_restore(flags);
154 static struct s3c24xx_gpio_chip gpios[] = {
156 .base = S3C24XX_GPIO_BASE(S3C2410_GPA0),
158 .base = S3C2410_GPA0,
159 .owner = THIS_MODULE,
162 .direction_input = s3c24xx_gpiolib_banka_input,
163 .direction_output = s3c24xx_gpiolib_banka_output,
167 .base = S3C24XX_GPIO_BASE(S3C2410_GPB0),
169 .base = S3C2410_GPB0,
170 .owner = THIS_MODULE,
176 .base = S3C24XX_GPIO_BASE(S3C2410_GPC0),
178 .base = S3C2410_GPC0,
179 .owner = THIS_MODULE,
185 .base = S3C24XX_GPIO_BASE(S3C2410_GPD0),
187 .base = S3C2410_GPD0,
188 .owner = THIS_MODULE,
194 .base = S3C24XX_GPIO_BASE(S3C2410_GPE0),
196 .base = S3C2410_GPE0,
198 .owner = THIS_MODULE,
203 .base = S3C24XX_GPIO_BASE(S3C2410_GPF0),
205 .base = S3C2410_GPF0,
206 .owner = THIS_MODULE,
212 .base = S3C24XX_GPIO_BASE(S3C2410_GPG0),
214 .base = S3C2410_GPG0,
215 .owner = THIS_MODULE,
222 static __init void s3c24xx_gpiolib_add(struct s3c24xx_gpio_chip *chip)
224 struct gpio_chip *gc = &chip->chip;
230 if (!gc->direction_input)
231 gc->direction_input = s3c24xx_gpiolib_input;
232 if (!gc->direction_output)
233 gc->direction_output = s3c24xx_gpiolib_output;
235 gc->set = s3c24xx_gpiolib_set;
237 gc->get = s3c24xx_gpiolib_get;
239 /* gpiochip_add() prints own failure message on error. */
243 static __init int s3c24xx_gpiolib_init(void)
245 struct s3c24xx_gpio_chip *chip = gpios;
248 for (gpn = 0; gpn < ARRAY_SIZE(gpios); gpn++, chip++)
249 s3c24xx_gpiolib_add(chip);
254 arch_initcall(s3c24xx_gpiolib_init);