blob: 6cd6d7f686f639623da2c10b0bcc6e9213d4b4a1 [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,
Dinh Nguyene24798e2010-04-22 16:28:42 +03006 * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
Juergen Beisert07bd1a62008-07-05 10:02:49 +02007 *
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>
Dinh Nguyena3484ff2010-10-23 09:12:48 -050023#include <linux/interrupt.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020024#include <linux/io.h>
25#include <linux/irq.h>
26#include <linux/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Juergen Beisert07bd1a62008-07-05 10:02:49 +020028#include <asm-generic/bug.h>
29
30static struct mxc_gpio_port *mxc_gpio_ports;
31static int gpio_table_size;
32
Sascha Hauer494f22d2009-05-27 18:26:51 +020033#define cpu_is_mx1_mx2() (cpu_is_mx1() || cpu_is_mx2())
34
35#define GPIO_DR (cpu_is_mx1_mx2() ? 0x1c : 0x00)
36#define GPIO_GDIR (cpu_is_mx1_mx2() ? 0x00 : 0x04)
37#define GPIO_PSR (cpu_is_mx1_mx2() ? 0x24 : 0x08)
38#define GPIO_ICR1 (cpu_is_mx1_mx2() ? 0x28 : 0x0C)
39#define GPIO_ICR2 (cpu_is_mx1_mx2() ? 0x2C : 0x10)
40#define GPIO_IMR (cpu_is_mx1_mx2() ? 0x30 : 0x14)
41#define GPIO_ISR (cpu_is_mx1_mx2() ? 0x34 : 0x18)
Sascha Hauer494f22d2009-05-27 18:26:51 +020042
43#define GPIO_INT_LOW_LEV (cpu_is_mx1_mx2() ? 0x3 : 0x0)
44#define GPIO_INT_HIGH_LEV (cpu_is_mx1_mx2() ? 0x2 : 0x1)
45#define GPIO_INT_RISE_EDGE (cpu_is_mx1_mx2() ? 0x0 : 0x2)
46#define GPIO_INT_FALL_EDGE (cpu_is_mx1_mx2() ? 0x1 : 0x3)
47#define GPIO_INT_NONE 0x4
48
Juergen Beisert07bd1a62008-07-05 10:02:49 +020049/* Note: This driver assumes 32 GPIOs are handled in one register */
50
51static void _clear_gpio_irqstatus(struct mxc_gpio_port *port, u32 index)
52{
53 __raw_writel(1 << index, port->base + GPIO_ISR);
54}
55
56static void _set_gpio_irqenable(struct mxc_gpio_port *port, u32 index,
57 int enable)
58{
59 u32 l;
60
61 l = __raw_readl(port->base + GPIO_IMR);
62 l = (l & (~(1 << index))) | (!!enable << index);
63 __raw_writel(l, port->base + GPIO_IMR);
64}
65
Lennert Buytenhek4d935792010-11-29 11:16:23 +010066static void gpio_ack_irq(struct irq_data *d)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020067{
Lennert Buytenhek4d935792010-11-29 11:16:23 +010068 u32 gpio = irq_to_gpio(d->irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020069 _clear_gpio_irqstatus(&mxc_gpio_ports[gpio / 32], gpio & 0x1f);
70}
71
Lennert Buytenhek4d935792010-11-29 11:16:23 +010072static void gpio_mask_irq(struct irq_data *d)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020073{
Lennert Buytenhek4d935792010-11-29 11:16:23 +010074 u32 gpio = irq_to_gpio(d->irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020075 _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 0);
76}
77
Lennert Buytenhek4d935792010-11-29 11:16:23 +010078static void gpio_unmask_irq(struct irq_data *d)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020079{
Lennert Buytenhek4d935792010-11-29 11:16:23 +010080 u32 gpio = irq_to_gpio(d->irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020081 _set_gpio_irqenable(&mxc_gpio_ports[gpio / 32], gpio & 0x1f, 1);
82}
83
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +010084static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset);
85
Lennert Buytenhek4d935792010-11-29 11:16:23 +010086static int gpio_set_irq_type(struct irq_data *d, u32 type)
Juergen Beisert07bd1a62008-07-05 10:02:49 +020087{
Lennert Buytenhek4d935792010-11-29 11:16:23 +010088 u32 gpio = irq_to_gpio(d->irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +020089 struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
90 u32 bit, val;
91 int edge;
92 void __iomem *reg = port->base;
93
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +010094 port->both_edges &= ~(1 << (gpio & 31));
Juergen Beisert07bd1a62008-07-05 10:02:49 +020095 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010096 case IRQ_TYPE_EDGE_RISING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +020097 edge = GPIO_INT_RISE_EDGE;
98 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010099 case IRQ_TYPE_EDGE_FALLING:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200100 edge = GPIO_INT_FALL_EDGE;
101 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100102 case IRQ_TYPE_EDGE_BOTH:
103 val = mxc_gpio_get(&port->chip, gpio & 31);
104 if (val) {
105 edge = GPIO_INT_LOW_LEV;
106 pr_debug("mxc: set GPIO %d to low trigger\n", gpio);
107 } else {
108 edge = GPIO_INT_HIGH_LEV;
109 pr_debug("mxc: set GPIO %d to high trigger\n", gpio);
110 }
111 port->both_edges |= 1 << (gpio & 31);
112 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100113 case IRQ_TYPE_LEVEL_LOW:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200114 edge = GPIO_INT_LOW_LEV;
115 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100116 case IRQ_TYPE_LEVEL_HIGH:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200117 edge = GPIO_INT_HIGH_LEV;
118 break;
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100119 default:
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200120 return -EINVAL;
121 }
122
123 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
124 bit = gpio & 0xf;
125 val = __raw_readl(reg) & ~(0x3 << (bit << 1));
126 __raw_writel(val | (edge << (bit << 1)), reg);
127 _clear_gpio_irqstatus(port, gpio & 0x1f);
128
129 return 0;
130}
131
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100132static void mxc_flip_edge(struct mxc_gpio_port *port, u32 gpio)
133{
134 void __iomem *reg = port->base;
135 u32 bit, val;
136 int edge;
137
138 reg += GPIO_ICR1 + ((gpio & 0x10) >> 2); /* lower or upper register */
139 bit = gpio & 0xf;
140 val = __raw_readl(reg);
141 edge = (val >> (bit << 1)) & 3;
142 val &= ~(0x3 << (bit << 1));
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100143 if (edge == GPIO_INT_HIGH_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100144 edge = GPIO_INT_LOW_LEV;
145 pr_debug("mxc: switch GPIO %d to low trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100146 } else if (edge == GPIO_INT_LOW_LEV) {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100147 edge = GPIO_INT_HIGH_LEV;
148 pr_debug("mxc: switch GPIO %d to high trigger\n", gpio);
Uwe Kleine-König3d40f7f2010-02-05 22:14:37 +0100149 } else {
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100150 pr_err("mxc: invalid configuration for GPIO %d: %x\n",
151 gpio, edge);
152 return;
153 }
154 __raw_writel(val | (edge << (bit << 1)), reg);
155}
156
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100157/* handle 32 interrupts in one status register */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200158static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat)
159{
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100160 u32 gpio_irq_no_base = port->virtual_irq_start;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200161
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100162 while (irq_stat != 0) {
163 int irqoffset = fls(irq_stat) - 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200164
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100165 if (port->both_edges & (1 << irqoffset))
166 mxc_flip_edge(port, irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100167
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100168 generic_handle_irq(gpio_irq_no_base + irqoffset);
Guennadi Liakhovetski910862e2009-03-12 12:46:41 +0100169
Uwe Kleine-König3621f182010-02-08 21:02:30 +0100170 irq_stat &= ~(1 << irqoffset);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200171 }
172}
173
Paulius Zaleckascfca8b52008-11-14 11:01:38 +0100174/* MX1 and MX3 has one interrupt *per* gpio port */
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200175static void mx3_gpio_irq_handler(u32 irq, struct irq_desc *desc)
176{
177 u32 irq_stat;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100178 struct mxc_gpio_port *port = irq_get_handler_data(irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200179
180 irq_stat = __raw_readl(port->base + GPIO_ISR) &
181 __raw_readl(port->base + GPIO_IMR);
Sascha Hauere2c97e72009-04-21 12:39:59 +0200182
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200183 mxc_gpio_irq_handler(port, irq_stat);
184}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200185
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200186/* MX2 has one interrupt *for all* gpio ports */
187static void mx2_gpio_irq_handler(u32 irq, struct irq_desc *desc)
188{
189 int i;
190 u32 irq_msk, irq_stat;
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100191 struct mxc_gpio_port *port = irq_get_handler_data(irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200192
193 /* walk through all interrupt status registers */
194 for (i = 0; i < gpio_table_size; i++) {
195 irq_msk = __raw_readl(port[i].base + GPIO_IMR);
196 if (!irq_msk)
197 continue;
198
199 irq_stat = __raw_readl(port[i].base + GPIO_ISR) & irq_msk;
200 if (irq_stat)
201 mxc_gpio_irq_handler(&port[i], irq_stat);
202 }
203}
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200204
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500205/*
206 * Set interrupt number "irq" in the GPIO as a wake-up source.
207 * While system is running, all registered GPIO interrupts need to have
208 * wake-up enabled. When system is suspended, only selected GPIO interrupts
209 * need to have wake-up enabled.
210 * @param irq interrupt source number
211 * @param enable enable as wake-up if equal to non-zero
212 * @return This function returns 0 on success.
213 */
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100214static int gpio_set_wake_irq(struct irq_data *d, u32 enable)
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500215{
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100216 u32 gpio = irq_to_gpio(d->irq);
Dinh Nguyena3484ff2010-10-23 09:12:48 -0500217 u32 gpio_idx = gpio & 0x1F;
218 struct mxc_gpio_port *port = &mxc_gpio_ports[gpio / 32];
219
220 if (enable) {
221 if (port->irq_high && (gpio_idx >= 16))
222 enable_irq_wake(port->irq_high);
223 else
224 enable_irq_wake(port->irq);
225 } else {
226 if (port->irq_high && (gpio_idx >= 16))
227 disable_irq_wake(port->irq_high);
228 else
229 disable_irq_wake(port->irq);
230 }
231
232 return 0;
233}
234
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200235static struct irq_chip gpio_irq_chip = {
Alexander Stein039c4642011-01-31 17:00:24 +0100236 .name = "GPIO",
Lennert Buytenhek4d935792010-11-29 11:16:23 +0100237 .irq_ack = gpio_ack_irq,
238 .irq_mask = gpio_mask_irq,
239 .irq_unmask = gpio_unmask_irq,
240 .irq_set_type = gpio_set_irq_type,
241 .irq_set_wake = gpio_set_wake_irq,
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200242};
243
244static void _set_gpio_direction(struct gpio_chip *chip, unsigned offset,
245 int dir)
246{
247 struct mxc_gpio_port *port =
248 container_of(chip, struct mxc_gpio_port, chip);
249 u32 l;
Baruch Siach14cb0de2010-07-06 14:03:22 +0300250 unsigned long flags;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200251
Baruch Siach14cb0de2010-07-06 14:03:22 +0300252 spin_lock_irqsave(&port->lock, flags);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200253 l = __raw_readl(port->base + GPIO_GDIR);
254 if (dir)
255 l |= 1 << offset;
256 else
257 l &= ~(1 << offset);
258 __raw_writel(l, port->base + GPIO_GDIR);
Baruch Siach14cb0de2010-07-06 14:03:22 +0300259 spin_unlock_irqrestore(&port->lock, flags);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200260}
261
262static void mxc_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
263{
264 struct mxc_gpio_port *port =
265 container_of(chip, struct mxc_gpio_port, chip);
266 void __iomem *reg = port->base + GPIO_DR;
267 u32 l;
Baruch Siach14cb0de2010-07-06 14:03:22 +0300268 unsigned long flags;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200269
Baruch Siach14cb0de2010-07-06 14:03:22 +0300270 spin_lock_irqsave(&port->lock, flags);
Peter Korsgaard886ab3d2010-10-11 15:02:06 +0200271 l = (__raw_readl(reg) & (~(1 << offset))) | (!!value << offset);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200272 __raw_writel(l, reg);
Baruch Siach14cb0de2010-07-06 14:03:22 +0300273 spin_unlock_irqrestore(&port->lock, flags);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200274}
275
276static int mxc_gpio_get(struct gpio_chip *chip, unsigned offset)
277{
278 struct mxc_gpio_port *port =
279 container_of(chip, struct mxc_gpio_port, chip);
280
Darius Augulis5cac9d62008-10-15 10:38:30 +0200281 return (__raw_readl(port->base + GPIO_PSR) >> offset) & 1;
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200282}
283
284static int mxc_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
285{
286 _set_gpio_direction(chip, offset, 0);
287 return 0;
288}
289
290static int mxc_gpio_direction_output(struct gpio_chip *chip,
291 unsigned offset, int value)
292{
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200293 mxc_gpio_set(chip, offset, value);
Guennadi Liakhovetski999981d2009-02-12 14:27:22 +0100294 _set_gpio_direction(chip, offset, 1);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200295 return 0;
296}
297
Thomas Gleixnerb5eee2f2011-04-04 14:29:58 +0200298/*
299 * This lock class tells lockdep that GPIO irqs are in a different
300 * category than their parents, so it won't report false recursion.
301 */
302static struct lock_class_key gpio_lock_class;
303
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200304int __init mxc_gpio_init(struct mxc_gpio_port *port, int cnt)
305{
306 int i, j;
307
308 /* save for local usage */
309 mxc_gpio_ports = port;
310 gpio_table_size = cnt;
311
312 printk(KERN_INFO "MXC GPIO hardware\n");
313
314 for (i = 0; i < cnt; i++) {
315 /* disable the interrupt and clear the status */
316 __raw_writel(0, port[i].base + GPIO_IMR);
317 __raw_writel(~0, port[i].base + GPIO_ISR);
318 for (j = port[i].virtual_irq_start;
319 j < port[i].virtual_irq_start + 32; j++) {
Thomas Gleixnerb5eee2f2011-04-04 14:29:58 +0200320 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100321 irq_set_chip_and_handler(j, &gpio_irq_chip,
322 handle_level_irq);
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200323 set_irq_flags(j, IRQF_VALID);
324 }
325
326 /* register gpio chip */
327 port[i].chip.direction_input = mxc_gpio_direction_input;
328 port[i].chip.direction_output = mxc_gpio_direction_output;
329 port[i].chip.get = mxc_gpio_get;
330 port[i].chip.set = mxc_gpio_set;
331 port[i].chip.base = i * 32;
332 port[i].chip.ngpio = 32;
333
Baruch Siach14cb0de2010-07-06 14:03:22 +0300334 spin_lock_init(&port[i].lock);
335
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200336 /* its a serious configuration bug when it fails */
337 BUG_ON( gpiochip_add(&port[i].chip) < 0 );
338
Dinh Nguyene24798e2010-04-22 16:28:42 +0300339 if (cpu_is_mx1() || cpu_is_mx3() || cpu_is_mx25() || cpu_is_mx51()) {
Sascha Hauer8afaada2009-06-15 12:36:25 +0200340 /* setup one handler for each entry */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100341 irq_set_chained_handler(port[i].irq,
342 mx3_gpio_irq_handler);
343 irq_set_handler_data(port[i].irq, &port[i]);
Eric Bénardaa872142010-07-21 14:46:11 +0200344 if (port[i].irq_high) {
345 /* setup handler for GPIO 16 to 31 */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100346 irq_set_chained_handler(port[i].irq_high,
347 mx3_gpio_irq_handler);
348 irq_set_handler_data(port[i].irq_high,
349 &port[i]);
Eric Bénardaa872142010-07-21 14:46:11 +0200350 }
Sascha Hauer8afaada2009-06-15 12:36:25 +0200351 }
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200352 }
353
Sascha Hauer8afaada2009-06-15 12:36:25 +0200354 if (cpu_is_mx2()) {
355 /* setup one handler for all GPIO interrupts */
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100356 irq_set_chained_handler(port[0].irq, mx2_gpio_irq_handler);
357 irq_set_handler_data(port[0].irq, port);
Sascha Hauer8afaada2009-06-15 12:36:25 +0200358 }
359
Juergen Beisert07bd1a62008-07-05 10:02:49 +0200360 return 0;
361}