blob: b70715720350a4f61b490931a7bc04a6f9ee8999 [file] [log] [blame]
Juergen Beisert07bd1a62008-07-05 10:02:49 +02001/*
2 * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
3 * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
4 *
5 * Based on code from Freescale,
6 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/io.h>
24#include <linux/irq.h>
25#include <linux/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/hardware.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020027#include <asm-generic/bug.h>
28
29static struct mxc_gpio_port *mxc_gpio_ports;
30static int gpio_table_size;
31
32/* Note: This driver assumes 32 GPIOs are handled in one register */
33
34static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
35{
36 __raw_writel(1 << index, port->base + GPIO_ISR);
37}
38
39static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
40 int enable)
41{
42 u32 l;
43
44 l = __raw_readl(port->base + GPIO_IMR);
45 l = (l & (~(1 << index))) | (!!enable << index);
46 __raw_writel(l, port->base + GPIO_IMR);
47}
48
49static void gpio_ack_irq(u32 irq)
50{
51 u32 gpio = irq_to_gpio(irq);
52 _clear_gpio_irqstatus(&mxc_gpio_ports[gpio / 32], gpio & 0x1f);
53}
54
55static void gpio_mask_irq(u32 irq)
56{
57 u32 gpio = irq_to_gpio(irq);
58 _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 0);
59}
60
61static void gpio_unmask_irq(u32 irq)
62{
63 u32 gpio = irq_to_gpio(irq);
64 _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 1);
65}
66
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +010067static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset);
68
Juergen Beisert07bd1a62008-07-05 10:02:49 +020069static int gpio_set_irq_type(u32 irq, u32 type)
70{
71 u32 gpio = irq_to_gpio(irq);
72 struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
73 u32 bit, val;
74 int edge;
75 void __iomem *reg = port->base;
76
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +010077 port->both_edges &= ~(1 << (gpio & 31));
Juergen Beisert07bd1a62008-07-05 10:02:49 +020078 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010079 case IRQ_TYPE_EDGE_RISING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +020080 edge = GPIO_INT_RISE_EDGE;
81 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010082 case IRQ_TYPE_EDGE_FALLING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +020083 edge = GPIO_INT_FALL_EDGE;
84 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +010085 case IRQ_TYPE_EDGE_BOTH:
86 val = mxc_gpio_get(&port->chip, gpio & 31);
87 if (val) {
88 edge = GPIO_INT_LOW_LEV;
89 pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
90 } else {
91 edge = GPIO_INT_HIGH_LEV;
92 pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
93 }
94 port->both_edges |= 1 << (gpio & 31);
95 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010096 case IRQ_TYPE_LEVEL_LOW:
Juergen Beisert07bd1a62008-07-05 10:02:49 +020097 edge = GPIO_INT_LOW_LEV;
98 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010099 case IRQ_TYPE_LEVEL_HIGH:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200100 edge = GPIO_INT_HIGH_LEV;
101 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100102 default:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200103 return -EINVAL;
104 }
105
106 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
107 bit = gpio & 0xf;
108 val = __raw_readl(reg) & ~(0x3 << (bit << 1));
109 __raw_writel(val | (edge << (bit << 1)), reg);
110 _clear_gpio_irqstatus(port, gpio & 0x1f);
111
112 return 0;
113}
114
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100115static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
116{
117 void __iomem *reg = port->base;
118 u32 bit, val;
119 int edge;
120
121 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
122 bit = gpio & 0xf;
123 val = __raw_readl(reg);
124 edge = (val >> (bit << 1)) & 3;
125 val &= ~(0x3 << (bit << 1));
126 switch (edge) {
127 case GPIO_INT_HIGH_LEV:
128 edge = GPIO_INT_LOW_LEV;
129 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
130 break;
131 case GPIO_INT_LOW_LEV:
132 edge = GPIO_INT_HIGH_LEV;
133 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
134 break;
135 default:
136 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
137 gpio, edge);
138 return;
139 }
140 __raw_writel(val | (edge << (bit << 1)), reg);
141}
142
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200143/* handle n interrupts in one status register */
144static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
145{
146 u32 gpio_irq_no;
147
148 gpio_irq_no = port->virtual_irq_start;
149 for (; irq_stat != 0; irq_stat >>= 1, gpio_irq_no++) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100150 u32 gpio = irq_to_gpio(gpio_irq_no);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200151
152 if ((irq_stat & 1) == 0)
153 continue;
154
155 BUG_ON(!(irq_desc[gpio_irq_no].handle_irq));
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100156
157 if (port->both_edges & (1 << (gpio & 31)))
158 mxc_flip_edge(port, gpio);
159
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200160 irq_desc[gpio_irq_no].handle_irq(gpio_irq_no,
161 &irq_desc[gpio_irq_no]);
162 }
163}
164
Paulius Zaleckascfca8b52008-11-14 11:01:38 +0100165/* MX1 and MX3 has one interrupt *per* gpio port */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200166static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
167{
168 u32 irq_stat;
169 struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
170
171 irq_stat = __raw_readl(port->base + GPIO_ISR) &
172 __raw_readl(port->base + GPIO_IMR);
Sascha Hauere2c97e72009-04-21 12:39:59 +0200173
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200174 mxc_gpio_irq_handler(port, irq_stat);
175}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200176
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200177/* MX2 has one interrupt *for all* gpio ports */
178static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
179{
180 int i;
181 u32 irq_msk, irq_stat;
182 struct mxc_gpio_port *port = (struct mxc_gpio_port *)get_irq_data(irq);
183
184 /* walk through all interrupt status registers */
185 for (i = 0; i < gpio_table_size; i++) {
186 irq_msk = __raw_readl(port[i].base + GPIO_IMR);
187 if (!irq_msk)
188 continue;
189
190 irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
191 if (irq_stat)
192 mxc_gpio_irq_handler(&port[i], irq_stat);
193 }
194}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200195
196static struct irq_chip gpio_irq_chip = {
197 .ack = gpio_ack_irq,
198 .mask = gpio_mask_irq,
199 .unmask = gpio_unmask_irq,
200 .set_type = gpio_set_irq_type,
201};
202
203static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
204 int dir)
205{
206 struct mxc_gpio_port *port =
207 container_of(chip, struct mxc_gpio_port, chip);
208 u32 l;
209
210 l = __raw_readl(port->base + GPIO_GDIR);
211 if (dir)
212 l |= 1 << offset;
213 else
214 l &= ~(1 << offset);
215 __raw_writel(l, port->base + GPIO_GDIR);
216}
217
218static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
219{
220 struct mxc_gpio_port *port =
221 container_of(chip, struct mxc_gpio_port, chip);
222 void __iomem *reg = port->base + GPIO_DR;
223 u32 l;
224
225 l = (__raw_readl(reg) & (~(1 << offset))) | (value << offset);
226 __raw_writel(l, reg);
227}
228
229static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
230{
231 struct mxc_gpio_port *port =
232 container_of(chip, struct mxc_gpio_port, chip);
233
Darius Augulis5cac9d62008-10-15 10:38:30 +0200234 return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200235}
236
237static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
238{
239 _set_gpio_direction(chip, offset, 0);
240 return 0;
241}
242
243static int mxc_gpio_direction_output(struct gpio_chip *chip,
244 unsigned offset, int value)
245{
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200246 mxc_gpio_set(chip, offset, value);
Guennadi Liakhovetski999981d2009-02-12 14:27:22 +0100247 _set_gpio_direction(chip, offset, 1);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200248 return 0;
249}
250
251int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
252{
253 int i, j;
254
255 /* save for local usage */
256 mxc_gpio_ports = port;
257 gpio_table_size = cnt;
258
259 printk(KERN_INFO "MXC GPIO hardware\n");
260
261 for (i = 0; i < cnt; i++) {
262 /* disable the interrupt and clear the status */
263 __raw_writel(0, port[i].base + GPIO_IMR);
264 __raw_writel(~0, port[i].base + GPIO_ISR);
265 for (j = port[i].virtual_irq_start;
266 j < port[i].virtual_irq_start + 32; j++) {
267 set_irq_chip(j, &gpio_irq_chip);
268 set_irq_handler(j, handle_edge_irq);
269 set_irq_flags(j, IRQF_VALID);
270 }
271
272 /* register gpio chip */
273 port[i].chip.direction_input = mxc_gpio_direction_input;
274 port[i].chip.direction_output = mxc_gpio_direction_output;
275 port[i].chip.get = mxc_gpio_get;
276 port[i].chip.set = mxc_gpio_set;
277 port[i].chip.base = i * 32;
278 port[i].chip.ngpio = 32;
279
280 /* its a serious configuration bug when it fails */
281 BUG_ON( gpiochip_add(&port[i].chip) < 0 );
282
Sascha Hauer8afaada2009-06-15 12:36:25 +0200283 if (cpu_is_mx1() || cpu_is_mx3()) {
284 /* setup one handler for each entry */
285 set_irq_chained_handler(port[i].irq, mx3_gpio_irq_handler);
286 set_irq_data(port[i].irq, &port[i]);
287 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200288 }
289
Sascha Hauer8afaada2009-06-15 12:36:25 +0200290 if (cpu_is_mx2()) {
291 /* setup one handler for all GPIO interrupts */
292 set_irq_chained_handler(port[0].irq, mx2_gpio_irq_handler);
293 set_irq_data(port[0].irq, port);
294 }
295
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200296 return 0;
297}