[ARM] S3C: Move common GPIO code from plat-s3c24xx
[safe/jmp/linux-2.6] / arch / arm / plat-s3c / gpio.c
1 /* linux/arch/arm/plat-s3c/gpio.c
2  *
3  * Copyright 2008 Simtec Electronics
4  *      Ben Dooks <ben@simtec.co.uk>
5  *      http://armlinux.simtec.co.uk/
6  *
7  * S3C series GPIO core
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 version 2 as
11  * published by the Free Software Foundation.
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/gpio.h>
18
19 #include <plat/gpio-core.h>
20
21 /* Default routines for controlling GPIO, based on the original S3C24XX
22  * GPIO functions which deal with the case where each gpio bank of the
23  * chip is as following:
24  *
25  * base + 0x00: Control register, 2 bits per gpio
26  *              gpio n: 2 bits starting at (2*n)
27  *              00 = input, 01 = output, others mean special-function
28  * base + 0x04: Data register, 1 bit per gpio
29  *              bit n: data bit n
30 */
31
32 static int s3c_gpiolib_input(struct gpio_chip *chip, unsigned offset)
33 {
34         struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
35         void __iomem *base = ourchip->base;
36         unsigned long flags;
37         unsigned long con;
38
39         local_irq_save(flags);
40
41         con = __raw_readl(base + 0x00);
42         con &= ~(3 << (offset * 2));
43
44         __raw_writel(con, base + 0x00);
45
46         local_irq_restore(flags);
47         return 0;
48 }
49
50 static int s3c_gpiolib_output(struct gpio_chip *chip,
51                               unsigned offset, int value)
52 {
53         struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
54         void __iomem *base = ourchip->base;
55         unsigned long flags;
56         unsigned long dat;
57         unsigned long con;
58
59         local_irq_save(flags);
60
61         dat = __raw_readl(base + 0x04);
62         dat &= ~(1 << offset);
63         if (value)
64                 dat |= 1 << offset;
65         __raw_writel(dat, base + 0x04);
66
67         con = __raw_readl(base + 0x00);
68         con &= ~(3 << (offset * 2));
69         con |= 1 << (offset * 2);
70
71         __raw_writel(con, base + 0x00);
72         __raw_writel(dat, base + 0x04);
73
74         local_irq_restore(flags);
75         return 0;
76 }
77
78 static void s3c_gpiolib_set(struct gpio_chip *chip,
79                             unsigned offset, int value)
80 {
81         struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
82         void __iomem *base = ourchip->base;
83         unsigned long flags;
84         unsigned long dat;
85
86         local_irq_save(flags);
87
88         dat = __raw_readl(base + 0x04);
89         dat &= ~(1 << offset);
90         if (value)
91                 dat |= 1 << offset;
92         __raw_writel(dat, base + 0x04);
93
94         local_irq_restore(flags);
95 }
96
97 static int s3c_gpiolib_get(struct gpio_chip *chip, unsigned offset)
98 {
99         struct s3c_gpio_chip *ourchip = to_s3c_gpio(chip);
100         unsigned long val;
101
102         val = __raw_readl(ourchip->base + 0x04);
103         val >>= offset;
104         val &= 1;
105
106         return val;
107 }
108
109 __init void s3c_gpiolib_add(struct s3c_gpio_chip *chip)
110 {
111         struct gpio_chip *gc = &chip->chip;
112
113         BUG_ON(!chip->base);
114         BUG_ON(!gc->label);
115         BUG_ON(!gc->ngpio);
116
117         if (!gc->direction_input)
118                 gc->direction_input = s3c_gpiolib_input;
119         if (!gc->direction_output)
120                 gc->direction_output = s3c_gpiolib_output;
121         if (!gc->set)
122                 gc->set = s3c_gpiolib_set;
123         if (!gc->get)
124                 gc->get = s3c_gpiolib_get;
125
126         /* gpiochip_add() prints own failure message on error. */
127         gpiochip_add(gc);
128 }