1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/types.h>
32#include <linux/spinlock.h>
33#include <linux/platform_device.h>
34#include <linux/gpio.h>
35
36#include <asm/mach-rc32434/rb.h>
37#include <asm/mach-rc32434/gpio.h>
38
39struct rb532_gpio_chip {
40 struct gpio_chip chip;
41 void __iomem *regbase;
42};
43
44static struct resource rb532_gpio_reg0_res[] = {
45 {
46 .name = "gpio_reg0",
47 .start = REGBASE + GPIOBASE,
48 .end = REGBASE + GPIOBASE + sizeof(struct rb532_gpio_reg) - 1,
49 .flags = IORESOURCE_MEM,
50 }
51};
52
53
54
55
56
57
58
59static inline void rb532_set_bit(unsigned bitval,
60 unsigned offset, void __iomem *ioaddr)
61{
62 unsigned long flags;
63 u32 val;
64
65 local_irq_save(flags);
66
67 val = readl(ioaddr);
68 val &= ~(!bitval << offset);
69 val |= (!!bitval << offset);
70 writel(val, ioaddr);
71
72 local_irq_restore(flags);
73}
74
75
76
77
78
79static inline int rb532_get_bit(unsigned offset, void __iomem *ioaddr)
80{
81 return (readl(ioaddr) & (1 << offset));
82}
83
84
85
86static int rb532_gpio_get(struct gpio_chip *chip, unsigned offset)
87{
88 struct rb532_gpio_chip *gpch;
89
90 gpch = container_of(chip, struct rb532_gpio_chip, chip);
91 return rb532_get_bit(offset, gpch->regbase + GPIOD);
92}
93
94
95
96
97static void rb532_gpio_set(struct gpio_chip *chip,
98 unsigned offset, int value)
99{
100 struct rb532_gpio_chip *gpch;
101
102 gpch = container_of(chip, struct rb532_gpio_chip, chip);
103 rb532_set_bit(value, offset, gpch->regbase + GPIOD);
104}
105
106
107
108
109static int rb532_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
110{
111 struct rb532_gpio_chip *gpch;
112
113 gpch = container_of(chip, struct rb532_gpio_chip, chip);
114
115
116 rb532_set_bit(0, offset, gpch->regbase + GPIOFUNC);
117
118 rb532_set_bit(0, offset, gpch->regbase + GPIOCFG);
119 return 0;
120}
121
122
123
124
125static int rb532_gpio_direction_output(struct gpio_chip *chip,
126 unsigned offset, int value)
127{
128 struct rb532_gpio_chip *gpch;
129
130 gpch = container_of(chip, struct rb532_gpio_chip, chip);
131
132
133 rb532_set_bit(0, offset, gpch->regbase + GPIOFUNC);
134
135
136 rb532_set_bit(value, offset, gpch->regbase + GPIOD);
137
138 rb532_set_bit(1, offset, gpch->regbase + GPIOCFG);
139 return 0;
140}
141
142static struct rb532_gpio_chip rb532_gpio_chip[] = {
143 [0] = {
144 .chip = {
145 .label = "gpio0",
146 .direction_input = rb532_gpio_direction_input,
147 .direction_output = rb532_gpio_direction_output,
148 .get = rb532_gpio_get,
149 .set = rb532_gpio_set,
150 .base = 0,
151 .ngpio = 32,
152 },
153 },
154};
155
156
157
158
159void rb532_gpio_set_ilevel(int bit, unsigned gpio)
160{
161 rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOILEVEL);
162}
163EXPORT_SYMBOL(rb532_gpio_set_ilevel);
164
165
166
167
168void rb532_gpio_set_istat(int bit, unsigned gpio)
169{
170 rb532_set_bit(bit, gpio, rb532_gpio_chip->regbase + GPIOISTAT);
171}
172EXPORT_SYMBOL(rb532_gpio_set_istat);
173
174
175
176
177void rb532_gpio_set_func(unsigned gpio)
178{
179 rb532_set_bit(1, gpio, rb532_gpio_chip->regbase + GPIOFUNC);
180}
181EXPORT_SYMBOL(rb532_gpio_set_func);
182
183int __init rb532_gpio_init(void)
184{
185 struct resource *r;
186
187 r = rb532_gpio_reg0_res;
188 rb532_gpio_chip->regbase = ioremap_nocache(r->start, resource_size(r));
189
190 if (!rb532_gpio_chip->regbase) {
191 printk(KERN_ERR "rb532: cannot remap GPIO register 0\n");
192 return -ENXIO;
193 }
194
195
196 gpiochip_add(&rb532_gpio_chip->chip);
197
198 return 0;
199}
200arch_initcall(rb532_gpio_init);
201