blob: 1a65865fcfb65119968d09935cd0dda86069dd03 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/gpio.c
3 *
4 * Support functions for OMAP GPIO
5 *
Tony Lindgren92105bb2005-09-07 17:20:26 +01006 * Copyright (C) 2003-2005 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02007 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01008 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07009 * Copyright (C) 2009 Texas Instruments
10 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
11 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017#include <linux/init.h>
18#include <linux/module.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010019#include <linux/interrupt.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010020#include <linux/sysdev.h>
21#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000022#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010024
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/irqs.h>
28#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029#include <asm/mach/irq.h>
Kevin Hilman43ffcd92009-01-27 11:09:24 -080030#include <plat/powerdomain.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010032/*
33 * OMAP1510 GPIO registers
34 */
Tony Lindgren9f7065d2009-10-19 15:25:20 -070035#define OMAP1510_GPIO_BASE 0xfffce000
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036#define OMAP1510_GPIO_DATA_INPUT 0x00
37#define OMAP1510_GPIO_DATA_OUTPUT 0x04
38#define OMAP1510_GPIO_DIR_CONTROL 0x08
39#define OMAP1510_GPIO_INT_CONTROL 0x0c
40#define OMAP1510_GPIO_INT_MASK 0x10
41#define OMAP1510_GPIO_INT_STATUS 0x14
42#define OMAP1510_GPIO_PIN_CONTROL 0x18
43
44#define OMAP1510_IH_GPIO_BASE 64
45
46/*
47 * OMAP1610 specific GPIO registers
48 */
Tony Lindgren9f7065d2009-10-19 15:25:20 -070049#define OMAP1610_GPIO1_BASE 0xfffbe400
50#define OMAP1610_GPIO2_BASE 0xfffbec00
51#define OMAP1610_GPIO3_BASE 0xfffbb400
52#define OMAP1610_GPIO4_BASE 0xfffbbc00
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053#define OMAP1610_GPIO_REVISION 0x0000
54#define OMAP1610_GPIO_SYSCONFIG 0x0010
55#define OMAP1610_GPIO_SYSSTATUS 0x0014
56#define OMAP1610_GPIO_IRQSTATUS1 0x0018
57#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010058#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059#define OMAP1610_GPIO_DATAIN 0x002c
60#define OMAP1610_GPIO_DATAOUT 0x0030
61#define OMAP1610_GPIO_DIRECTION 0x0034
62#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
63#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
64#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010065#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010066#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
67#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010068#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010069#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
70
71/*
Alistair Buxton7c006922009-09-22 10:02:58 +010072 * OMAP7XX specific GPIO registers
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010073 */
Tony Lindgren9f7065d2009-10-19 15:25:20 -070074#define OMAP7XX_GPIO1_BASE 0xfffbc000
75#define OMAP7XX_GPIO2_BASE 0xfffbc800
76#define OMAP7XX_GPIO3_BASE 0xfffbd000
77#define OMAP7XX_GPIO4_BASE 0xfffbd800
78#define OMAP7XX_GPIO5_BASE 0xfffbe000
79#define OMAP7XX_GPIO6_BASE 0xfffbe800
Alistair Buxton7c006922009-09-22 10:02:58 +010080#define OMAP7XX_GPIO_DATA_INPUT 0x00
81#define OMAP7XX_GPIO_DATA_OUTPUT 0x04
82#define OMAP7XX_GPIO_DIR_CONTROL 0x08
83#define OMAP7XX_GPIO_INT_CONTROL 0x0c
84#define OMAP7XX_GPIO_INT_MASK 0x10
85#define OMAP7XX_GPIO_INT_STATUS 0x14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010086
Tony Lindgren9f7065d2009-10-19 15:25:20 -070087#define OMAP1_MPUIO_VBASE OMAP1_MPUIO_BASE
Tony Lindgren94113262009-08-28 10:50:33 -070088
Zebediah C. McClure56739a62009-03-23 18:07:40 -070089/*
Tony Lindgren92105bb2005-09-07 17:20:26 +010090 * omap24xx specific GPIO registers
91 */
Tony Lindgren9f7065d2009-10-19 15:25:20 -070092#define OMAP242X_GPIO1_BASE 0x48018000
93#define OMAP242X_GPIO2_BASE 0x4801a000
94#define OMAP242X_GPIO3_BASE 0x4801c000
95#define OMAP242X_GPIO4_BASE 0x4801e000
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -080096
Tony Lindgren9f7065d2009-10-19 15:25:20 -070097#define OMAP243X_GPIO1_BASE 0x4900C000
98#define OMAP243X_GPIO2_BASE 0x4900E000
99#define OMAP243X_GPIO3_BASE 0x49010000
100#define OMAP243X_GPIO4_BASE 0x49012000
101#define OMAP243X_GPIO5_BASE 0x480B6000
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800102
Tony Lindgren92105bb2005-09-07 17:20:26 +0100103#define OMAP24XX_GPIO_REVISION 0x0000
104#define OMAP24XX_GPIO_SYSCONFIG 0x0010
105#define OMAP24XX_GPIO_SYSSTATUS 0x0014
106#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300107#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
108#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +0100109#define OMAP24XX_GPIO_IRQENABLE1 0x001c
Tero Kristo723fdb72008-11-26 14:35:16 -0800110#define OMAP24XX_GPIO_WAKE_EN 0x0020
Tony Lindgren92105bb2005-09-07 17:20:26 +0100111#define OMAP24XX_GPIO_CTRL 0x0030
112#define OMAP24XX_GPIO_OE 0x0034
113#define OMAP24XX_GPIO_DATAIN 0x0038
114#define OMAP24XX_GPIO_DATAOUT 0x003c
115#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
116#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
117#define OMAP24XX_GPIO_RISINGDETECT 0x0048
118#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700119#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
120#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +0100121#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
122#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
123#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
124#define OMAP24XX_GPIO_SETWKUENA 0x0084
125#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
126#define OMAP24XX_GPIO_SETDATAOUT 0x0094
127
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530128#define OMAP4_GPIO_REVISION 0x0000
129#define OMAP4_GPIO_SYSCONFIG 0x0010
130#define OMAP4_GPIO_EOI 0x0020
131#define OMAP4_GPIO_IRQSTATUSRAW0 0x0024
132#define OMAP4_GPIO_IRQSTATUSRAW1 0x0028
133#define OMAP4_GPIO_IRQSTATUS0 0x002c
134#define OMAP4_GPIO_IRQSTATUS1 0x0030
135#define OMAP4_GPIO_IRQSTATUSSET0 0x0034
136#define OMAP4_GPIO_IRQSTATUSSET1 0x0038
137#define OMAP4_GPIO_IRQSTATUSCLR0 0x003c
138#define OMAP4_GPIO_IRQSTATUSCLR1 0x0040
139#define OMAP4_GPIO_IRQWAKEN0 0x0044
140#define OMAP4_GPIO_IRQWAKEN1 0x0048
Charulatha V9f096862010-05-14 12:05:27 -0700141#define OMAP4_GPIO_SYSSTATUS 0x0114
142#define OMAP4_GPIO_IRQENABLE1 0x011c
143#define OMAP4_GPIO_WAKE_EN 0x0120
144#define OMAP4_GPIO_IRQSTATUS2 0x0128
145#define OMAP4_GPIO_IRQENABLE2 0x012c
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530146#define OMAP4_GPIO_CTRL 0x0130
147#define OMAP4_GPIO_OE 0x0134
148#define OMAP4_GPIO_DATAIN 0x0138
149#define OMAP4_GPIO_DATAOUT 0x013c
150#define OMAP4_GPIO_LEVELDETECT0 0x0140
151#define OMAP4_GPIO_LEVELDETECT1 0x0144
152#define OMAP4_GPIO_RISINGDETECT 0x0148
153#define OMAP4_GPIO_FALLINGDETECT 0x014c
154#define OMAP4_GPIO_DEBOUNCENABLE 0x0150
155#define OMAP4_GPIO_DEBOUNCINGTIME 0x0154
Charulatha V9f096862010-05-14 12:05:27 -0700156#define OMAP4_GPIO_CLEARIRQENABLE1 0x0160
157#define OMAP4_GPIO_SETIRQENABLE1 0x0164
158#define OMAP4_GPIO_CLEARWKUENA 0x0180
159#define OMAP4_GPIO_SETWKUENA 0x0184
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530160#define OMAP4_GPIO_CLEARDATAOUT 0x0190
161#define OMAP4_GPIO_SETDATAOUT 0x0194
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800162/*
163 * omap34xx specific GPIO registers
164 */
165
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700166#define OMAP34XX_GPIO1_BASE 0x48310000
167#define OMAP34XX_GPIO2_BASE 0x49050000
168#define OMAP34XX_GPIO3_BASE 0x49052000
169#define OMAP34XX_GPIO4_BASE 0x49054000
170#define OMAP34XX_GPIO5_BASE 0x49056000
171#define OMAP34XX_GPIO6_BASE 0x49058000
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800172
Santosh Shilimkar44169072009-05-28 14:16:04 -0700173/*
174 * OMAP44XX specific GPIO registers
175 */
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700176#define OMAP44XX_GPIO1_BASE 0x4a310000
177#define OMAP44XX_GPIO2_BASE 0x48055000
178#define OMAP44XX_GPIO3_BASE 0x48057000
179#define OMAP44XX_GPIO4_BASE 0x48059000
180#define OMAP44XX_GPIO5_BASE 0x4805B000
181#define OMAP44XX_GPIO6_BASE 0x4805D000
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800182
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100183struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700184 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100185 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100186 u16 irq;
187 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100188 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -0800189#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100190 u32 suspend_wakeup;
191 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800192#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800193#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800194 u32 non_wakeup_gpios;
195 u32 enabled_non_wakeup_gpios;
196
197 u32 saved_datain;
198 u32 saved_fallingdetect;
199 u32 saved_risingdetect;
200#endif
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800201 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800202 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100203 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800204 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -0800205 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -0800206 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -0800207 u32 dbck_enable_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208};
209
210#define METHOD_MPUIO 0
211#define METHOD_GPIO_1510 1
212#define METHOD_GPIO_1610 2
Alistair Buxton7c006922009-09-22 10:02:58 +0100213#define METHOD_GPIO_7XX 3
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700214#define METHOD_GPIO_24XX 5
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800215#define METHOD_GPIO_44XX 6
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100216
Tony Lindgren92105bb2005-09-07 17:20:26 +0100217#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100218static struct gpio_bank gpio_bank_1610[5] = {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700219 { OMAP1_MPUIO_VBASE, NULL, INT_MPUIO, IH_MPUIO_BASE,
220 METHOD_MPUIO },
221 { OMAP1610_GPIO1_BASE, NULL, INT_GPIO_BANK1, IH_GPIO_BASE,
222 METHOD_GPIO_1610 },
223 { OMAP1610_GPIO2_BASE, NULL, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16,
224 METHOD_GPIO_1610 },
225 { OMAP1610_GPIO3_BASE, NULL, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32,
226 METHOD_GPIO_1610 },
227 { OMAP1610_GPIO4_BASE, NULL, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48,
228 METHOD_GPIO_1610 },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100229};
230#endif
231
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000232#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100233static struct gpio_bank gpio_bank_1510[2] = {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700234 { OMAP1_MPUIO_VBASE, NULL, INT_MPUIO, IH_MPUIO_BASE,
235 METHOD_MPUIO },
236 { OMAP1510_GPIO_BASE, NULL, INT_GPIO_BANK1, IH_GPIO_BASE,
237 METHOD_GPIO_1510 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100238};
239#endif
240
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100241#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100242static struct gpio_bank gpio_bank_7xx[7] = {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700243 { OMAP1_MPUIO_VBASE, NULL, INT_7XX_MPUIO, IH_MPUIO_BASE,
244 METHOD_MPUIO },
245 { OMAP7XX_GPIO1_BASE, NULL, INT_7XX_GPIO_BANK1, IH_GPIO_BASE,
246 METHOD_GPIO_7XX },
247 { OMAP7XX_GPIO2_BASE, NULL, INT_7XX_GPIO_BANK2, IH_GPIO_BASE + 32,
248 METHOD_GPIO_7XX },
249 { OMAP7XX_GPIO3_BASE, NULL, INT_7XX_GPIO_BANK3, IH_GPIO_BASE + 64,
250 METHOD_GPIO_7XX },
251 { OMAP7XX_GPIO4_BASE, NULL, INT_7XX_GPIO_BANK4, IH_GPIO_BASE + 96,
252 METHOD_GPIO_7XX },
253 { OMAP7XX_GPIO5_BASE, NULL, INT_7XX_GPIO_BANK5, IH_GPIO_BASE + 128,
254 METHOD_GPIO_7XX },
255 { OMAP7XX_GPIO6_BASE, NULL, INT_7XX_GPIO_BANK6, IH_GPIO_BASE + 160,
256 METHOD_GPIO_7XX },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100257};
258#endif
259
Tony Lindgren088ef952010-02-12 12:26:47 -0800260#ifdef CONFIG_ARCH_OMAP2
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800261
262static struct gpio_bank gpio_bank_242x[4] = {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700263 { OMAP242X_GPIO1_BASE, NULL, INT_24XX_GPIO_BANK1, IH_GPIO_BASE,
264 METHOD_GPIO_24XX },
265 { OMAP242X_GPIO2_BASE, NULL, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32,
266 METHOD_GPIO_24XX },
267 { OMAP242X_GPIO3_BASE, NULL, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64,
268 METHOD_GPIO_24XX },
269 { OMAP242X_GPIO4_BASE, NULL, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96,
270 METHOD_GPIO_24XX },
Tony Lindgren92105bb2005-09-07 17:20:26 +0100271};
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800272
273static struct gpio_bank gpio_bank_243x[5] = {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700274 { OMAP243X_GPIO1_BASE, NULL, INT_24XX_GPIO_BANK1, IH_GPIO_BASE,
275 METHOD_GPIO_24XX },
276 { OMAP243X_GPIO2_BASE, NULL, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32,
277 METHOD_GPIO_24XX },
278 { OMAP243X_GPIO3_BASE, NULL, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64,
279 METHOD_GPIO_24XX },
280 { OMAP243X_GPIO4_BASE, NULL, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96,
281 METHOD_GPIO_24XX },
282 { OMAP243X_GPIO5_BASE, NULL, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128,
283 METHOD_GPIO_24XX },
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800284};
285
Tony Lindgren92105bb2005-09-07 17:20:26 +0100286#endif
287
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800288#ifdef CONFIG_ARCH_OMAP3
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800289static struct gpio_bank gpio_bank_34xx[6] = {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700290 { OMAP34XX_GPIO1_BASE, NULL, INT_34XX_GPIO_BANK1, IH_GPIO_BASE,
291 METHOD_GPIO_24XX },
292 { OMAP34XX_GPIO2_BASE, NULL, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32,
293 METHOD_GPIO_24XX },
294 { OMAP34XX_GPIO3_BASE, NULL, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64,
295 METHOD_GPIO_24XX },
296 { OMAP34XX_GPIO4_BASE, NULL, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96,
297 METHOD_GPIO_24XX },
298 { OMAP34XX_GPIO5_BASE, NULL, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128,
299 METHOD_GPIO_24XX },
300 { OMAP34XX_GPIO6_BASE, NULL, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160,
301 METHOD_GPIO_24XX },
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800302};
303
Rajendra Nayak40c670f2008-09-26 17:47:48 +0530304struct omap3_gpio_regs {
305 u32 sysconfig;
306 u32 irqenable1;
307 u32 irqenable2;
308 u32 wake_en;
309 u32 ctrl;
310 u32 oe;
311 u32 leveldetect0;
312 u32 leveldetect1;
313 u32 risingdetect;
314 u32 fallingdetect;
315 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +0530316};
317
318static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800319#endif
320
Santosh Shilimkar44169072009-05-28 14:16:04 -0700321#ifdef CONFIG_ARCH_OMAP4
322static struct gpio_bank gpio_bank_44xx[6] = {
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530323 { OMAP44XX_GPIO1_BASE, NULL, OMAP44XX_IRQ_GPIO1, IH_GPIO_BASE,
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800324 METHOD_GPIO_44XX },
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530325 { OMAP44XX_GPIO2_BASE, NULL, OMAP44XX_IRQ_GPIO2, IH_GPIO_BASE + 32,
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800326 METHOD_GPIO_44XX },
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530327 { OMAP44XX_GPIO3_BASE, NULL, OMAP44XX_IRQ_GPIO3, IH_GPIO_BASE + 64,
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800328 METHOD_GPIO_44XX },
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530329 { OMAP44XX_GPIO4_BASE, NULL, OMAP44XX_IRQ_GPIO4, IH_GPIO_BASE + 96,
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800330 METHOD_GPIO_44XX },
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530331 { OMAP44XX_GPIO5_BASE, NULL, OMAP44XX_IRQ_GPIO5, IH_GPIO_BASE + 128,
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800332 METHOD_GPIO_44XX },
Santosh Shilimkar5772ca72010-02-18 03:14:12 +0530333 { OMAP44XX_GPIO6_BASE, NULL, OMAP44XX_IRQ_GPIO6, IH_GPIO_BASE + 160,
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800334 METHOD_GPIO_44XX },
Santosh Shilimkar44169072009-05-28 14:16:04 -0700335};
336
337#endif
338
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339static struct gpio_bank *gpio_bank;
340static int gpio_bank_count;
341
342static inline struct gpio_bank *get_gpio_bank(int gpio)
343{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100344 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 if (OMAP_GPIO_IS_MPUIO(gpio))
346 return &gpio_bank[0];
347 return &gpio_bank[1];
348 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349 if (cpu_is_omap16xx()) {
350 if (OMAP_GPIO_IS_MPUIO(gpio))
351 return &gpio_bank[0];
352 return &gpio_bank[1 + (gpio >> 4)];
353 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700354 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100355 if (OMAP_GPIO_IS_MPUIO(gpio))
356 return &gpio_bank[0];
357 return &gpio_bank[1 + (gpio >> 5)];
358 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100359 if (cpu_is_omap24xx())
360 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700361 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800362 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800363 BUG();
364 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365}
366
367static inline int get_gpio_index(int gpio)
368{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700369 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100370 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100371 if (cpu_is_omap24xx())
372 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700373 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800374 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100375 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376}
377
378static inline int gpio_valid(int gpio)
379{
380 if (gpio < 0)
381 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800382 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300383 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384 return -1;
385 return 0;
386 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100387 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100388 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 if ((cpu_is_omap16xx()) && gpio < 64)
390 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700391 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100392 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100393 if (cpu_is_omap24xx() && gpio < 128)
394 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700395 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800396 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397 return -1;
398}
399
400static int check_gpio(int gpio)
401{
Roel Kluind32b20f2009-11-17 14:39:03 -0800402 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
404 dump_stack();
405 return -1;
406 }
407 return 0;
408}
409
410static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
411{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100412 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100413 u32 l;
414
415 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800416#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100417 case METHOD_MPUIO:
418 reg += OMAP_MPUIO_IO_CNTL;
419 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800420#endif
421#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100422 case METHOD_GPIO_1510:
423 reg += OMAP1510_GPIO_DIR_CONTROL;
424 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800425#endif
426#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427 case METHOD_GPIO_1610:
428 reg += OMAP1610_GPIO_DIRECTION;
429 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800430#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100431#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100432 case METHOD_GPIO_7XX:
433 reg += OMAP7XX_GPIO_DIR_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700434 break;
435#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800436#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100437 case METHOD_GPIO_24XX:
438 reg += OMAP24XX_GPIO_OE;
439 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800440#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530441#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800442 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530443 reg += OMAP4_GPIO_OE;
444 break;
445#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800446 default:
447 WARN_ON(1);
448 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449 }
450 l = __raw_readl(reg);
451 if (is_input)
452 l |= 1 << gpio;
453 else
454 l &= ~(1 << gpio);
455 __raw_writel(l, reg);
456}
457
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
459{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100460 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100461 u32 l = 0;
462
463 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800464#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 case METHOD_MPUIO:
466 reg += OMAP_MPUIO_OUTPUT;
467 l = __raw_readl(reg);
468 if (enable)
469 l |= 1 << gpio;
470 else
471 l &= ~(1 << gpio);
472 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800473#endif
474#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 case METHOD_GPIO_1510:
476 reg += OMAP1510_GPIO_DATA_OUTPUT;
477 l = __raw_readl(reg);
478 if (enable)
479 l |= 1 << gpio;
480 else
481 l &= ~(1 << gpio);
482 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800483#endif
484#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485 case METHOD_GPIO_1610:
486 if (enable)
487 reg += OMAP1610_GPIO_SET_DATAOUT;
488 else
489 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
490 l = 1 << gpio;
491 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800492#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100493#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100494 case METHOD_GPIO_7XX:
495 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700496 l = __raw_readl(reg);
497 if (enable)
498 l |= 1 << gpio;
499 else
500 l &= ~(1 << gpio);
501 break;
502#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800503#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100504 case METHOD_GPIO_24XX:
505 if (enable)
506 reg += OMAP24XX_GPIO_SETDATAOUT;
507 else
508 reg += OMAP24XX_GPIO_CLEARDATAOUT;
509 l = 1 << gpio;
510 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800511#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530512#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800513 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530514 if (enable)
515 reg += OMAP4_GPIO_SETDATAOUT;
516 else
517 reg += OMAP4_GPIO_CLEARDATAOUT;
518 l = 1 << gpio;
519 break;
520#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100521 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800522 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100523 return;
524 }
525 __raw_writel(l, reg);
526}
527
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300528static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100530 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531
532 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800533 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534 reg = bank->base;
535 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800536#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537 case METHOD_MPUIO:
538 reg += OMAP_MPUIO_INPUT_LATCH;
539 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800540#endif
541#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100542 case METHOD_GPIO_1510:
543 reg += OMAP1510_GPIO_DATA_INPUT;
544 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800545#endif
546#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100547 case METHOD_GPIO_1610:
548 reg += OMAP1610_GPIO_DATAIN;
549 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800550#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100551#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100552 case METHOD_GPIO_7XX:
553 reg += OMAP7XX_GPIO_DATA_INPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700554 break;
555#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800556#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100557 case METHOD_GPIO_24XX:
558 reg += OMAP24XX_GPIO_DATAIN;
559 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800560#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530561#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800562 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530563 reg += OMAP4_GPIO_DATAIN;
564 break;
565#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800567 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100568 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100569 return (__raw_readl(reg)
570 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571}
572
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300573static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
574{
575 void __iomem *reg;
576
577 if (check_gpio(gpio) < 0)
578 return -EINVAL;
579 reg = bank->base;
580
581 switch (bank->method) {
582#ifdef CONFIG_ARCH_OMAP1
583 case METHOD_MPUIO:
584 reg += OMAP_MPUIO_OUTPUT;
585 break;
586#endif
587#ifdef CONFIG_ARCH_OMAP15XX
588 case METHOD_GPIO_1510:
589 reg += OMAP1510_GPIO_DATA_OUTPUT;
590 break;
591#endif
592#ifdef CONFIG_ARCH_OMAP16XX
593 case METHOD_GPIO_1610:
594 reg += OMAP1610_GPIO_DATAOUT;
595 break;
596#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100597#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100598 case METHOD_GPIO_7XX:
599 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300600 break;
601#endif
Charulatha V9f096862010-05-14 12:05:27 -0700602#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300603 case METHOD_GPIO_24XX:
604 reg += OMAP24XX_GPIO_DATAOUT;
605 break;
606#endif
Charulatha V9f096862010-05-14 12:05:27 -0700607#ifdef CONFIG_ARCH_OMAP4
608 case METHOD_GPIO_44XX:
609 reg += OMAP4_GPIO_DATAOUT;
610 break;
611#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300612 default:
613 return -EINVAL;
614 }
615
616 return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
617}
618
Tony Lindgren92105bb2005-09-07 17:20:26 +0100619#define MOD_REG_BIT(reg, bit_mask, set) \
620do { \
621 int l = __raw_readl(base + reg); \
622 if (set) l |= bit_mask; \
623 else l &= ~bit_mask; \
624 __raw_writel(l, base + reg); \
625} while(0)
626
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700627/**
628 * _set_gpio_debounce - low level gpio debounce time
629 * @bank: the gpio bank we're acting upon
630 * @gpio: the gpio number on this @gpio
631 * @debounce: debounce time to use
632 *
633 * OMAP's debounce time is in 31us steps so we need
634 * to convert and round up to the closest unit.
635 */
636static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
637 unsigned debounce)
638{
639 void __iomem *reg = bank->base;
640 u32 val;
641 u32 l;
642
643 if (debounce < 32)
644 debounce = 0x01;
645 else if (debounce > 7936)
646 debounce = 0xff;
647 else
648 debounce = (debounce / 0x1f) - 1;
649
650 l = 1 << get_gpio_index(gpio);
651
652 if (cpu_is_omap44xx())
653 reg += OMAP4_GPIO_DEBOUNCINGTIME;
654 else
655 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
656
657 __raw_writel(debounce, reg);
658
659 reg = bank->base;
660 if (cpu_is_omap44xx())
661 reg += OMAP4_GPIO_DEBOUNCENABLE;
662 else
663 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
664
665 val = __raw_readl(reg);
666
667 if (debounce) {
668 val |= l;
669 if (cpu_is_omap34xx() || cpu_is_omap44xx())
670 clk_enable(bank->dbck);
671 } else {
672 val &= ~l;
673 if (cpu_is_omap34xx() || cpu_is_omap44xx())
674 clk_disable(bank->dbck);
675 }
676
677 __raw_writel(val, reg);
678}
679
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700680void omap_set_gpio_debounce(int gpio, int enable)
681{
682 struct gpio_bank *bank;
683 void __iomem *reg;
David Brownelle031ab22008-12-10 17:35:27 -0800684 unsigned long flags;
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700685 u32 val, l = 1 << get_gpio_index(gpio);
686
687 if (cpu_class_is_omap1())
688 return;
689
690 bank = get_gpio_bank(gpio);
691 reg = bank->base;
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800692
693 if (cpu_is_omap44xx())
694 reg += OMAP4_GPIO_DEBOUNCENABLE;
695 else
696 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
697
Charulatha V058af1e2009-11-22 10:11:25 -0800698 if (!(bank->mod_usage & l)) {
699 printk(KERN_ERR "GPIO %d not requested\n", gpio);
700 return;
701 }
David Brownelle031ab22008-12-10 17:35:27 -0800702
703 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700704 val = __raw_readl(reg);
705
Jouni Hogander89db9482008-12-10 17:35:24 -0800706 if (enable && !(val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700707 val |= l;
David Brownelle031ab22008-12-10 17:35:27 -0800708 else if (!enable && (val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700709 val &= ~l;
Jouni Hogander89db9482008-12-10 17:35:24 -0800710 else
David Brownelle031ab22008-12-10 17:35:27 -0800711 goto done;
Jouni Hogander89db9482008-12-10 17:35:24 -0800712
Santosh Shilimkar44169072009-05-28 14:16:04 -0700713 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Kevin Hilman8865b9b2009-01-27 11:15:34 -0800714 bank->dbck_enable_mask = val;
David Brownelle031ab22008-12-10 17:35:27 -0800715 if (enable)
716 clk_enable(bank->dbck);
717 else
718 clk_disable(bank->dbck);
719 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700720
721 __raw_writel(val, reg);
David Brownelle031ab22008-12-10 17:35:27 -0800722done:
723 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700724}
725EXPORT_SYMBOL(omap_set_gpio_debounce);
726
727void omap_set_gpio_debounce_time(int gpio, int enc_time)
728{
729 struct gpio_bank *bank;
730 void __iomem *reg;
731
732 if (cpu_class_is_omap1())
733 return;
734
735 bank = get_gpio_bank(gpio);
736 reg = bank->base;
737
Charulatha V058af1e2009-11-22 10:11:25 -0800738 if (!bank->mod_usage) {
739 printk(KERN_ERR "GPIO not requested\n");
740 return;
741 }
742
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700743 enc_time &= 0xff;
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800744
745 if (cpu_is_omap44xx())
746 reg += OMAP4_GPIO_DEBOUNCINGTIME;
747 else
748 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
749
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700750 __raw_writel(enc_time, reg);
751}
752EXPORT_SYMBOL(omap_set_gpio_debounce_time);
753
Tony Lindgren140455f2010-02-12 12:26:48 -0800754#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700755static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
756 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100757{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800758 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100759 u32 gpio_bit = 1 << gpio;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530760 u32 val;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100761
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530762 if (cpu_is_omap44xx()) {
763 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
764 trigger & IRQ_TYPE_LEVEL_LOW);
765 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
766 trigger & IRQ_TYPE_LEVEL_HIGH);
767 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
768 trigger & IRQ_TYPE_EDGE_RISING);
769 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
770 trigger & IRQ_TYPE_EDGE_FALLING);
771 } else {
772 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
773 trigger & IRQ_TYPE_LEVEL_LOW);
774 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
775 trigger & IRQ_TYPE_LEVEL_HIGH);
776 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
777 trigger & IRQ_TYPE_EDGE_RISING);
778 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
779 trigger & IRQ_TYPE_EDGE_FALLING);
780 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800781 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530782 if (cpu_is_omap44xx()) {
783 if (trigger != 0)
784 __raw_writel(1 << gpio, bank->base+
785 OMAP4_GPIO_IRQWAKEN0);
786 else {
787 val = __raw_readl(bank->base +
788 OMAP4_GPIO_IRQWAKEN0);
789 __raw_writel(val & (~(1 << gpio)), bank->base +
790 OMAP4_GPIO_IRQWAKEN0);
791 }
792 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000793 /*
794 * GPIO wakeup request can only be generated on edge
795 * transitions
796 */
797 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530798 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700799 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530800 else
801 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700802 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530803 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200804 }
805 /* This part needs to be executed always for OMAP34xx */
806 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000807 /*
808 * Log the edge gpio and manually trigger the IRQ
809 * after resume if the input level changes
810 * to avoid irq lost during PER RET/OFF mode
811 * Applies for omap2 non-wakeup gpio and all omap3 gpios
812 */
813 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800814 bank->enabled_non_wakeup_gpios |= gpio_bit;
815 else
816 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
817 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700818
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530819 if (cpu_is_omap44xx()) {
820 bank->level_mask =
821 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
822 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
823 } else {
824 bank->level_mask =
825 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
826 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
827 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100828}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800829#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100830
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800831#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800832/*
833 * This only applies to chips that can't do both rising and falling edge
834 * detection at once. For all other chips, this function is a noop.
835 */
836static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
837{
838 void __iomem *reg = bank->base;
839 u32 l = 0;
840
841 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800842 case METHOD_MPUIO:
843 reg += OMAP_MPUIO_GPIO_INT_EDGE;
844 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800845#ifdef CONFIG_ARCH_OMAP15XX
846 case METHOD_GPIO_1510:
847 reg += OMAP1510_GPIO_INT_CONTROL;
848 break;
849#endif
850#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
851 case METHOD_GPIO_7XX:
852 reg += OMAP7XX_GPIO_INT_CONTROL;
853 break;
854#endif
855 default:
856 return;
857 }
858
859 l = __raw_readl(reg);
860 if ((l >> gpio) & 1)
861 l &= ~(1 << gpio);
862 else
863 l |= 1 << gpio;
864
865 __raw_writel(l, reg);
866}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800867#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800868
Tony Lindgren92105bb2005-09-07 17:20:26 +0100869static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
870{
871 void __iomem *reg = bank->base;
872 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100873
874 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800875#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100876 case METHOD_MPUIO:
877 reg += OMAP_MPUIO_GPIO_INT_EDGE;
878 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000879 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800880 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100881 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100882 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100883 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100884 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100885 else
886 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100887 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800888#endif
889#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100890 case METHOD_GPIO_1510:
891 reg += OMAP1510_GPIO_INT_CONTROL;
892 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000893 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800894 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100895 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100896 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100897 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100898 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100899 else
900 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100901 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800902#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800903#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100904 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905 if (gpio & 0x08)
906 reg += OMAP1610_GPIO_EDGE_CTRL2;
907 else
908 reg += OMAP1610_GPIO_EDGE_CTRL1;
909 gpio &= 0x07;
910 l = __raw_readl(reg);
911 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100912 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100913 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100914 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100915 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800916 if (trigger)
917 /* Enable wake-up during idle for dynamic tick */
918 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
919 else
920 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100921 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800922#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100923#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100924 case METHOD_GPIO_7XX:
925 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700926 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000927 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800928 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700929 if (trigger & IRQ_TYPE_EDGE_RISING)
930 l |= 1 << gpio;
931 else if (trigger & IRQ_TYPE_EDGE_FALLING)
932 l &= ~(1 << gpio);
933 else
934 goto bad;
935 break;
936#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800937#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100938 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800939 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800940 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100941 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800942#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100943 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100944 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100945 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100946 __raw_writel(l, reg);
947 return 0;
948bad:
949 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100950}
951
Tony Lindgren92105bb2005-09-07 17:20:26 +0100952static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100953{
954 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100955 unsigned gpio;
956 int retval;
David Brownella6472532008-03-03 04:33:30 -0800957 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100958
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800959 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100960 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
961 else
962 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100963
964 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100965 return -EINVAL;
966
David Brownelle5c56ed2006-12-06 17:13:59 -0800967 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100968 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800969
970 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800971 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800972 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100973 return -EINVAL;
974
David Brownell58781012006-12-06 17:14:10 -0800975 bank = get_irq_chip_data(irq);
David Brownella6472532008-03-03 04:33:30 -0800976 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100977 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800978 if (retval == 0) {
979 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
980 irq_desc[irq].status |= type;
981 }
David Brownella6472532008-03-03 04:33:30 -0800982 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800983
984 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
985 __set_irq_handler_unlocked(irq, handle_level_irq);
986 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
987 __set_irq_handler_unlocked(irq, handle_edge_irq);
988
Tony Lindgren92105bb2005-09-07 17:20:26 +0100989 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100990}
991
992static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
993{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100994 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100995
996 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800997#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100998 case METHOD_MPUIO:
999 /* MPUIO irqstatus is reset by reading the status register,
1000 * so do nothing here */
1001 return;
David Brownelle5c56ed2006-12-06 17:13:59 -08001002#endif
1003#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001004 case METHOD_GPIO_1510:
1005 reg += OMAP1510_GPIO_INT_STATUS;
1006 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001007#endif
1008#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001009 case METHOD_GPIO_1610:
1010 reg += OMAP1610_GPIO_IRQSTATUS1;
1011 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001012#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001013#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001014 case METHOD_GPIO_7XX:
1015 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001016 break;
1017#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001018#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001019 case METHOD_GPIO_24XX:
1020 reg += OMAP24XX_GPIO_IRQSTATUS1;
1021 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001022#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301023#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001024 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301025 reg += OMAP4_GPIO_IRQSTATUS0;
1026 break;
1027#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001028 default:
David Brownelle5c56ed2006-12-06 17:13:59 -08001029 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001030 return;
1031 }
1032 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +03001033
1034 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001035 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1036 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
1037 else if (cpu_is_omap44xx())
1038 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
1039
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301040 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Roger Quadrosbedfd152009-04-23 11:10:50 -07001041 __raw_writel(gpio_mask, reg);
1042
1043 /* Flush posted write for the irq status to avoid spurious interrupts */
1044 __raw_readl(reg);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301045 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001046}
1047
1048static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
1049{
1050 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
1051}
1052
Imre Deakea6dedd2006-06-26 16:16:00 -07001053static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
1054{
1055 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -07001056 int inv = 0;
1057 u32 l;
1058 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -07001059
1060 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001061#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -07001062 case METHOD_MPUIO:
1063 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -07001064 mask = 0xffff;
1065 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -07001066 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001067#endif
1068#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -07001069 case METHOD_GPIO_1510:
1070 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -07001071 mask = 0xffff;
1072 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -07001073 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001074#endif
1075#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -07001076 case METHOD_GPIO_1610:
1077 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -07001078 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -07001079 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001080#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001081#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001082 case METHOD_GPIO_7XX:
1083 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001084 mask = 0xffffffff;
1085 inv = 1;
1086 break;
1087#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001088#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -07001089 case METHOD_GPIO_24XX:
1090 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -07001091 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -07001092 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001093#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301094#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001095 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301096 reg += OMAP4_GPIO_IRQSTATUSSET0;
1097 mask = 0xffffffff;
1098 break;
1099#endif
Imre Deakea6dedd2006-06-26 16:16:00 -07001100 default:
David Brownelle5c56ed2006-12-06 17:13:59 -08001101 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -07001102 return 0;
1103 }
1104
Imre Deak99c47702006-06-26 16:16:07 -07001105 l = __raw_readl(reg);
1106 if (inv)
1107 l = ~l;
1108 l &= mask;
1109 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -07001110}
1111
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001112static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
1113{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001114 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001115 u32 l;
1116
1117 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001118#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001119 case METHOD_MPUIO:
1120 reg += OMAP_MPUIO_GPIO_MASKIT;
1121 l = __raw_readl(reg);
1122 if (enable)
1123 l &= ~(gpio_mask);
1124 else
1125 l |= gpio_mask;
1126 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001127#endif
1128#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001129 case METHOD_GPIO_1510:
1130 reg += OMAP1510_GPIO_INT_MASK;
1131 l = __raw_readl(reg);
1132 if (enable)
1133 l &= ~(gpio_mask);
1134 else
1135 l |= gpio_mask;
1136 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001137#endif
1138#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001139 case METHOD_GPIO_1610:
1140 if (enable)
1141 reg += OMAP1610_GPIO_SET_IRQENABLE1;
1142 else
1143 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
1144 l = gpio_mask;
1145 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001146#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001147#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001148 case METHOD_GPIO_7XX:
1149 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001150 l = __raw_readl(reg);
1151 if (enable)
1152 l &= ~(gpio_mask);
1153 else
1154 l |= gpio_mask;
1155 break;
1156#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001157#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001158 case METHOD_GPIO_24XX:
1159 if (enable)
1160 reg += OMAP24XX_GPIO_SETIRQENABLE1;
1161 else
1162 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
1163 l = gpio_mask;
1164 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001165#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301166#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001167 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301168 if (enable)
1169 reg += OMAP4_GPIO_IRQSTATUSSET0;
1170 else
1171 reg += OMAP4_GPIO_IRQSTATUSCLR0;
1172 l = gpio_mask;
1173 break;
1174#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001175 default:
David Brownelle5c56ed2006-12-06 17:13:59 -08001176 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001177 return;
1178 }
1179 __raw_writel(l, reg);
1180}
1181
1182static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
1183{
1184 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
1185}
1186
Tony Lindgren92105bb2005-09-07 17:20:26 +01001187/*
1188 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
1189 * 1510 does not seem to have a wake-up register. If JTAG is connected
1190 * to the target, system will wake up always on GPIO events. While
1191 * system is running all registered GPIO interrupts need to have wake-up
1192 * enabled. When system is suspended, only selected GPIO interrupts need
1193 * to have wake-up enabled.
1194 */
1195static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
1196{
Tony Lindgren4cc64202010-01-08 10:29:05 -08001197 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -08001198
Tony Lindgren92105bb2005-09-07 17:20:26 +01001199 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001200#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -08001201 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +01001202 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -08001203 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001204 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001205 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001206 else
Tony Lindgren92105bb2005-09-07 17:20:26 +01001207 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -08001208 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001209 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001210#endif
Tony Lindgren140455f2010-02-12 12:26:48 -08001211#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001212 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001213 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -08001214 if (bank->non_wakeup_gpios & (1 << gpio)) {
1215 printk(KERN_ERR "Unable to modify wakeup on "
1216 "non-wakeup GPIO%d\n",
1217 (bank - gpio_bank) * 32 + gpio);
1218 return -EINVAL;
1219 }
David Brownella6472532008-03-03 04:33:30 -08001220 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001221 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001222 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001223 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001224 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -08001225 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001226 return 0;
1227#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001228 default:
1229 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
1230 bank->method);
1231 return -EINVAL;
1232 }
1233}
1234
Tony Lindgren4196dd62006-09-25 12:41:38 +03001235static void _reset_gpio(struct gpio_bank *bank, int gpio)
1236{
1237 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
1238 _set_gpio_irqenable(bank, gpio, 0);
1239 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +01001240 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001241}
1242
Tony Lindgren92105bb2005-09-07 17:20:26 +01001243/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
1244static int gpio_wake_enable(unsigned int irq, unsigned int enable)
1245{
1246 unsigned int gpio = irq - IH_GPIO_BASE;
1247 struct gpio_bank *bank;
1248 int retval;
1249
1250 if (check_gpio(gpio) < 0)
1251 return -ENODEV;
David Brownell58781012006-12-06 17:14:10 -08001252 bank = get_irq_chip_data(irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001253 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001254
1255 return retval;
1256}
1257
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001258static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001259{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001260 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001261 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001262
David Brownella6472532008-03-03 04:33:30 -08001263 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001264
Tony Lindgren4196dd62006-09-25 12:41:38 +03001265 /* Set trigger to none. You need to enable the desired trigger with
1266 * request_irq() or set_irq_type().
1267 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001268 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001269
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001270#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001271 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001272 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001273
Tony Lindgren92105bb2005-09-07 17:20:26 +01001274 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001275 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001276 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001277 }
1278#endif
Charulatha V058af1e2009-11-22 10:11:25 -08001279 if (!cpu_class_is_omap1()) {
1280 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -07001281 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -08001282 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -07001283
1284 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1285 reg += OMAP24XX_GPIO_CTRL;
1286 else if (cpu_is_omap44xx())
1287 reg += OMAP4_GPIO_CTRL;
1288 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001289 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -07001290 ctrl &= 0xFFFFFFFE;
1291 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001292 }
1293 bank->mod_usage |= 1 << offset;
1294 }
David Brownella6472532008-03-03 04:33:30 -08001295 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001296
1297 return 0;
1298}
1299
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001300static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001301{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001302 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001303 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001304
David Brownella6472532008-03-03 04:33:30 -08001305 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001306#ifdef CONFIG_ARCH_OMAP16XX
1307 if (bank->method == METHOD_GPIO_1610) {
1308 /* Disable wake-up during idle for dynamic tick */
1309 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001310 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001311 }
1312#endif
Charulatha V9f096862010-05-14 12:05:27 -07001313#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1314 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001315 /* Disable wake-up during idle for dynamic tick */
1316 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001317 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001318 }
1319#endif
Charulatha V9f096862010-05-14 12:05:27 -07001320#ifdef CONFIG_ARCH_OMAP4
1321 if (bank->method == METHOD_GPIO_44XX) {
1322 /* Disable wake-up during idle for dynamic tick */
1323 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
1324 __raw_writel(1 << offset, reg);
1325 }
1326#endif
Charulatha V058af1e2009-11-22 10:11:25 -08001327 if (!cpu_class_is_omap1()) {
1328 bank->mod_usage &= ~(1 << offset);
1329 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -07001330 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -08001331 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -07001332
1333 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1334 reg += OMAP24XX_GPIO_CTRL;
1335 else if (cpu_is_omap44xx())
1336 reg += OMAP4_GPIO_CTRL;
1337 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001338 /* Module is disabled, clocks are gated */
1339 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -07001340 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001341 }
1342 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001343 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -08001344 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001345}
1346
1347/*
1348 * We need to unmask the GPIO bank interrupt as soon as possible to
1349 * avoid missing GPIO interrupts for other lines in the bank.
1350 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1351 * in the bank to avoid missing nested interrupts for a GPIO line.
1352 * If we wait to unmask individual GPIO lines in the bank after the
1353 * line's interrupt handler has been run, we may miss some nested
1354 * interrupts.
1355 */
Russell King10dd5ce2006-11-23 11:41:32 +00001356static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001357{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001358 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001359 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -08001360 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001361 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001362 u32 retrigger = 0;
1363 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001364
1365 desc->chip->ack(irq);
1366
Thomas Gleixner418ca1f02006-07-01 22:32:41 +01001367 bank = get_irq_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001368#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001369 if (bank->method == METHOD_MPUIO)
1370 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
David Brownelle5c56ed2006-12-06 17:13:59 -08001371#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001372#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001373 if (bank->method == METHOD_GPIO_1510)
1374 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1375#endif
1376#if defined(CONFIG_ARCH_OMAP16XX)
1377 if (bank->method == METHOD_GPIO_1610)
1378 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1379#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001380#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001381 if (bank->method == METHOD_GPIO_7XX)
1382 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001383#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001384#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001385 if (bank->method == METHOD_GPIO_24XX)
1386 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1387#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301388#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001389 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301390 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1391#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001392 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001393 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001394 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001395
Imre Deakea6dedd2006-06-26 16:16:00 -07001396 enabled = _get_gpio_irqbank_mask(bank);
1397 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001398
1399 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1400 isr &= 0x0000ffff;
1401
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001402 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001403 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001404 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001405
1406 /* clear edge sensitive interrupts before handler(s) are
1407 called so that we don't miss any interrupt occurred while
1408 executing them */
1409 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1410 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1411 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1412
1413 /* if there is only edge sensitive GPIO pin interrupts
1414 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001415 if (!level_mask && !unmasked) {
1416 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001417 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -07001418 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001419
Imre Deakea6dedd2006-06-26 16:16:00 -07001420 isr |= retrigger;
1421 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001422 if (!isr)
1423 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001424
Tony Lindgren92105bb2005-09-07 17:20:26 +01001425 gpio_irq = bank->virtual_irq_start;
1426 for (; isr != 0; isr >>= 1, gpio_irq++) {
Cory Maccarrone4318f362010-01-08 10:29:04 -08001427 gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));
1428
Tony Lindgren92105bb2005-09-07 17:20:26 +01001429 if (!(isr & 1))
1430 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001431
Cory Maccarrone4318f362010-01-08 10:29:04 -08001432#ifdef CONFIG_ARCH_OMAP1
1433 /*
1434 * Some chips can't respond to both rising and falling
1435 * at the same time. If this irq was requested with
1436 * both flags, we need to flip the ICR data for the IRQ
1437 * to respond to the IRQ for the opposite direction.
1438 * This will be indicated in the bank toggle_mask.
1439 */
1440 if (bank->toggle_mask & (1 << gpio_index))
1441 _toggle_gpio_edge_triggering(bank, gpio_index);
1442#endif
1443
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001444 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001445 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001446 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001447 /* if bank has any level sensitive GPIO pin interrupt
1448 configured, we must unmask the bank interrupt only after
1449 handler(s) are executed in order to avoid spurious bank
1450 interrupt */
1451 if (!unmasked)
1452 desc->chip->unmask(irq);
1453
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001454}
1455
Tony Lindgren4196dd62006-09-25 12:41:38 +03001456static void gpio_irq_shutdown(unsigned int irq)
1457{
1458 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001459 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001460
1461 _reset_gpio(bank, gpio);
1462}
1463
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001464static void gpio_ack_irq(unsigned int irq)
1465{
1466 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001467 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001468
1469 _clear_gpio_irqstatus(bank, gpio);
1470}
1471
1472static void gpio_mask_irq(unsigned int irq)
1473{
1474 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001475 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001476
1477 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001478 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001479}
1480
1481static void gpio_unmask_irq(unsigned int irq)
1482{
1483 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001484 struct gpio_bank *bank = get_irq_chip_data(irq);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001485 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Kevin Hilman55b60192009-06-04 15:57:10 -07001486 struct irq_desc *desc = irq_to_desc(irq);
1487 u32 trigger = desc->status & IRQ_TYPE_SENSE_MASK;
1488
1489 if (trigger)
1490 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001491
1492 /* For level-triggered GPIOs, the clearing must be done after
1493 * the HW source is cleared, thus after the handler has run */
1494 if (bank->level_mask & irq_mask) {
1495 _set_gpio_irqenable(bank, gpio, 0);
1496 _clear_gpio_irqstatus(bank, gpio);
1497 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001498
Kevin Hilman4de8c752008-01-16 21:56:14 -08001499 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001500}
1501
David Brownelle5c56ed2006-12-06 17:13:59 -08001502static struct irq_chip gpio_irq_chip = {
1503 .name = "GPIO",
1504 .shutdown = gpio_irq_shutdown,
1505 .ack = gpio_ack_irq,
1506 .mask = gpio_mask_irq,
1507 .unmask = gpio_unmask_irq,
1508 .set_type = gpio_irq_type,
1509 .set_wake = gpio_wake_enable,
1510};
1511
1512/*---------------------------------------------------------------------*/
1513
1514#ifdef CONFIG_ARCH_OMAP1
1515
1516/* MPUIO uses the always-on 32k clock */
1517
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001518static void mpuio_ack_irq(unsigned int irq)
1519{
1520 /* The ISR is reset automatically, so do nothing here. */
1521}
1522
1523static void mpuio_mask_irq(unsigned int irq)
1524{
1525 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001526 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001527
1528 _set_gpio_irqenable(bank, gpio, 0);
1529}
1530
1531static void mpuio_unmask_irq(unsigned int irq)
1532{
1533 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001534 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001535
1536 _set_gpio_irqenable(bank, gpio, 1);
1537}
1538
David Brownelle5c56ed2006-12-06 17:13:59 -08001539static struct irq_chip mpuio_irq_chip = {
1540 .name = "MPUIO",
1541 .ack = mpuio_ack_irq,
1542 .mask = mpuio_mask_irq,
1543 .unmask = mpuio_unmask_irq,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001544 .set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001545#ifdef CONFIG_ARCH_OMAP16XX
1546 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1547 .set_wake = gpio_wake_enable,
1548#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001549};
1550
David Brownelle5c56ed2006-12-06 17:13:59 -08001551
1552#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1553
David Brownell11a78b72006-12-06 17:14:11 -08001554
1555#ifdef CONFIG_ARCH_OMAP16XX
1556
1557#include <linux/platform_device.h>
1558
Magnus Damm79ee0312009-07-08 13:22:04 +02001559static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001560{
Magnus Damm79ee0312009-07-08 13:22:04 +02001561 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001562 struct gpio_bank *bank = platform_get_drvdata(pdev);
1563 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001564 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001565
David Brownella6472532008-03-03 04:33:30 -08001566 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001567 bank->saved_wakeup = __raw_readl(mask_reg);
1568 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001569 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001570
1571 return 0;
1572}
1573
Magnus Damm79ee0312009-07-08 13:22:04 +02001574static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001575{
Magnus Damm79ee0312009-07-08 13:22:04 +02001576 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001577 struct gpio_bank *bank = platform_get_drvdata(pdev);
1578 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001579 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001580
David Brownella6472532008-03-03 04:33:30 -08001581 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001582 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001583 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001584
1585 return 0;
1586}
1587
Alexey Dobriyan47145212009-12-14 18:00:08 -08001588static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001589 .suspend_noirq = omap_mpuio_suspend_noirq,
1590 .resume_noirq = omap_mpuio_resume_noirq,
1591};
1592
David Brownell11a78b72006-12-06 17:14:11 -08001593/* use platform_driver for this, now that there's no longer any
1594 * point to sys_device (other than not disturbing old code).
1595 */
1596static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001597 .driver = {
1598 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001599 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001600 },
1601};
1602
1603static struct platform_device omap_mpuio_device = {
1604 .name = "mpuio",
1605 .id = -1,
1606 .dev = {
1607 .driver = &omap_mpuio_driver.driver,
1608 }
1609 /* could list the /proc/iomem resources */
1610};
1611
1612static inline void mpuio_init(void)
1613{
David Brownellfcf126d2007-04-02 12:46:47 -07001614 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1615
David Brownell11a78b72006-12-06 17:14:11 -08001616 if (platform_driver_register(&omap_mpuio_driver) == 0)
1617 (void) platform_device_register(&omap_mpuio_device);
1618}
1619
1620#else
1621static inline void mpuio_init(void) {}
1622#endif /* 16xx */
1623
David Brownelle5c56ed2006-12-06 17:13:59 -08001624#else
1625
1626extern struct irq_chip mpuio_irq_chip;
1627
1628#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001629static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001630
1631#endif
1632
1633/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001634
David Brownell52e31342008-03-03 12:43:23 -08001635/* REVISIT these are stupid implementations! replace by ones that
1636 * don't switch on METHOD_* and which mostly avoid spinlocks
1637 */
1638
1639static int gpio_input(struct gpio_chip *chip, unsigned offset)
1640{
1641 struct gpio_bank *bank;
1642 unsigned long flags;
1643
1644 bank = container_of(chip, struct gpio_bank, chip);
1645 spin_lock_irqsave(&bank->lock, flags);
1646 _set_gpio_direction(bank, offset, 1);
1647 spin_unlock_irqrestore(&bank->lock, flags);
1648 return 0;
1649}
1650
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001651static int gpio_is_input(struct gpio_bank *bank, int mask)
1652{
1653 void __iomem *reg = bank->base;
1654
1655 switch (bank->method) {
1656 case METHOD_MPUIO:
1657 reg += OMAP_MPUIO_IO_CNTL;
1658 break;
1659 case METHOD_GPIO_1510:
1660 reg += OMAP1510_GPIO_DIR_CONTROL;
1661 break;
1662 case METHOD_GPIO_1610:
1663 reg += OMAP1610_GPIO_DIRECTION;
1664 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001665 case METHOD_GPIO_7XX:
1666 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001667 break;
1668 case METHOD_GPIO_24XX:
1669 reg += OMAP24XX_GPIO_OE;
1670 break;
Charulatha V9f096862010-05-14 12:05:27 -07001671 case METHOD_GPIO_44XX:
1672 reg += OMAP4_GPIO_OE;
1673 break;
1674 default:
1675 WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
1676 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001677 }
1678 return __raw_readl(reg) & mask;
1679}
1680
David Brownell52e31342008-03-03 12:43:23 -08001681static int gpio_get(struct gpio_chip *chip, unsigned offset)
1682{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001683 struct gpio_bank *bank;
1684 void __iomem *reg;
1685 int gpio;
1686 u32 mask;
1687
1688 gpio = chip->base + offset;
1689 bank = get_gpio_bank(gpio);
1690 reg = bank->base;
1691 mask = 1 << get_gpio_index(gpio);
1692
1693 if (gpio_is_input(bank, mask))
1694 return _get_gpio_datain(bank, gpio);
1695 else
1696 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001697}
1698
1699static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1700{
1701 struct gpio_bank *bank;
1702 unsigned long flags;
1703
1704 bank = container_of(chip, struct gpio_bank, chip);
1705 spin_lock_irqsave(&bank->lock, flags);
1706 _set_gpio_dataout(bank, offset, value);
1707 _set_gpio_direction(bank, offset, 0);
1708 spin_unlock_irqrestore(&bank->lock, flags);
1709 return 0;
1710}
1711
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001712static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1713 unsigned debounce)
1714{
1715 struct gpio_bank *bank;
1716 unsigned long flags;
1717
1718 bank = container_of(chip, struct gpio_bank, chip);
1719 spin_lock_irqsave(&bank->lock, flags);
1720 _set_gpio_debounce(bank, offset, debounce);
1721 spin_unlock_irqrestore(&bank->lock, flags);
1722
1723 return 0;
1724}
1725
David Brownell52e31342008-03-03 12:43:23 -08001726static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1727{
1728 struct gpio_bank *bank;
1729 unsigned long flags;
1730
1731 bank = container_of(chip, struct gpio_bank, chip);
1732 spin_lock_irqsave(&bank->lock, flags);
1733 _set_gpio_dataout(bank, offset, value);
1734 spin_unlock_irqrestore(&bank->lock, flags);
1735}
1736
David Brownella007b702008-12-10 17:35:25 -08001737static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1738{
1739 struct gpio_bank *bank;
1740
1741 bank = container_of(chip, struct gpio_bank, chip);
1742 return bank->virtual_irq_start + offset;
1743}
1744
David Brownell52e31342008-03-03 12:43:23 -08001745/*---------------------------------------------------------------------*/
1746
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001747static int initialized;
Tony Lindgren56213ca2010-02-12 12:26:46 -08001748#if defined(CONFIG_ARCH_OMAP1) || defined(CONFIG_ARCH_OMAP2)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001749static struct clk * gpio_ick;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001750#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001751
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001752#if defined(CONFIG_ARCH_OMAP2)
1753static struct clk * gpio_fck;
1754#endif
1755
1756#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001757static struct clk * gpio5_ick;
1758static struct clk * gpio5_fck;
1759#endif
1760
Santosh Shilimkar44169072009-05-28 14:16:04 -07001761#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001762static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1763#endif
1764
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001765static void __init omap_gpio_show_rev(void)
1766{
1767 u32 rev;
1768
1769 if (cpu_is_omap16xx())
1770 rev = __raw_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
1771 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
1772 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
1773 else if (cpu_is_omap44xx())
1774 rev = __raw_readl(gpio_bank[0].base + OMAP4_GPIO_REVISION);
1775 else
1776 return;
1777
1778 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1779 (rev >> 4) & 0x0f, rev & 0x0f);
1780}
1781
David Brownell8ba55c52008-02-26 11:10:50 -08001782/* This lock class tells lockdep that GPIO irqs are in a different
1783 * category than their parents, so it won't report false recursion.
1784 */
1785static struct lock_class_key gpio_lock_class;
1786
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001787static int __init _omap_gpio_init(void)
1788{
1789 int i;
David Brownell52e31342008-03-03 12:43:23 -08001790 int gpio = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001791 struct gpio_bank *bank;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001792 int bank_size = SZ_8K; /* Module 4KB + L4 4KB except on omap1 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001793 char clk_name[11];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001794
1795 initialized = 1;
1796
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001797#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren6e60e792006-04-02 17:46:23 +01001798 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001799 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1800 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001801 printk("Could not get arm_gpio_ck\n");
1802 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001803 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001804 }
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001805#endif
1806#if defined(CONFIG_ARCH_OMAP2)
1807 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001808 gpio_ick = clk_get(NULL, "gpios_ick");
1809 if (IS_ERR(gpio_ick))
1810 printk("Could not get gpios_ick\n");
1811 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001812 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001813 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001814 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001815 printk("Could not get gpios_fck\n");
1816 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001817 clk_enable(gpio_fck);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001818
1819 /*
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001820 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001821 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001822#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001823 if (cpu_is_omap2430()) {
1824 gpio5_ick = clk_get(NULL, "gpio5_ick");
1825 if (IS_ERR(gpio5_ick))
1826 printk("Could not get gpio5_ick\n");
1827 else
1828 clk_enable(gpio5_ick);
1829 gpio5_fck = clk_get(NULL, "gpio5_fck");
1830 if (IS_ERR(gpio5_fck))
1831 printk("Could not get gpio5_fck\n");
1832 else
1833 clk_enable(gpio5_fck);
1834 }
1835#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001836 }
1837#endif
1838
Santosh Shilimkar44169072009-05-28 14:16:04 -07001839#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
1840 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001841 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1842 sprintf(clk_name, "gpio%d_ick", i + 1);
1843 gpio_iclks[i] = clk_get(NULL, clk_name);
1844 if (IS_ERR(gpio_iclks[i]))
1845 printk(KERN_ERR "Could not get %s\n", clk_name);
1846 else
1847 clk_enable(gpio_iclks[i]);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001848 }
1849 }
1850#endif
1851
Tony Lindgren92105bb2005-09-07 17:20:26 +01001852
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001853#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001854 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001855 gpio_bank_count = 2;
1856 gpio_bank = gpio_bank_1510;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001857 bank_size = SZ_2K;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001858 }
1859#endif
1860#if defined(CONFIG_ARCH_OMAP16XX)
1861 if (cpu_is_omap16xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001862 gpio_bank_count = 5;
1863 gpio_bank = gpio_bank_1610;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001864 bank_size = SZ_2K;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001865 }
1866#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001867#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
1868 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001869 gpio_bank_count = 7;
Alistair Buxton7c006922009-09-22 10:02:58 +01001870 gpio_bank = gpio_bank_7xx;
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001871 bank_size = SZ_2K;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001872 }
1873#endif
Tony Lindgren088ef952010-02-12 12:26:47 -08001874#ifdef CONFIG_ARCH_OMAP2
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001875 if (cpu_is_omap242x()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001876 gpio_bank_count = 4;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001877 gpio_bank = gpio_bank_242x;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001878 }
1879 if (cpu_is_omap243x()) {
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001880 gpio_bank_count = 5;
1881 gpio_bank = gpio_bank_243x;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001882 }
1883#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001884#ifdef CONFIG_ARCH_OMAP3
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001885 if (cpu_is_omap34xx()) {
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001886 gpio_bank_count = OMAP34XX_NR_GPIOS;
1887 gpio_bank = gpio_bank_34xx;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001888 }
1889#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001890#ifdef CONFIG_ARCH_OMAP4
1891 if (cpu_is_omap44xx()) {
Santosh Shilimkar44169072009-05-28 14:16:04 -07001892 gpio_bank_count = OMAP34XX_NR_GPIOS;
1893 gpio_bank = gpio_bank_44xx;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001894 }
1895#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001896 for (i = 0; i < gpio_bank_count; i++) {
1897 int j, gpio_count = 16;
1898
1899 bank = &gpio_bank[i];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001900 spin_lock_init(&bank->lock);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001901
1902 /* Static mapping, never released */
1903 bank->base = ioremap(bank->pbase, bank_size);
1904 if (!bank->base) {
1905 printk(KERN_ERR "Could not ioremap gpio bank%i\n", i);
1906 continue;
1907 }
1908
David Brownelle5c56ed2006-12-06 17:13:59 -08001909 if (bank_is_mpuio(bank))
Russell King7c7095a2008-09-05 15:49:14 +01001910 __raw_writew(0xffff, bank->base + OMAP_MPUIO_GPIO_MASKIT);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001911 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001912 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1913 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1914 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001915 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001916 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1917 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001918 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001919 }
Alistair Buxton7c006922009-09-22 10:02:58 +01001920 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1921 __raw_writel(0xffffffff, bank->base + OMAP7XX_GPIO_INT_MASK);
1922 __raw_writel(0x00000000, bank->base + OMAP7XX_GPIO_INT_STATUS);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001923
Alistair Buxton7c006922009-09-22 10:02:58 +01001924 gpio_count = 32; /* 7xx has 32-bit GPIOs */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001925 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001926
Tony Lindgren140455f2010-02-12 12:26:48 -08001927#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001928 if ((bank->method == METHOD_GPIO_24XX) ||
1929 (bank->method == METHOD_GPIO_44XX)) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001930 static const u32 non_wakeup_gpios[] = {
1931 0xe203ffc0, 0x08700040
1932 };
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001933
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001934 if (cpu_is_omap44xx()) {
1935 __raw_writel(0xffffffff, bank->base +
1936 OMAP4_GPIO_IRQSTATUSCLR0);
1937 __raw_writew(0x0015, bank->base +
1938 OMAP4_GPIO_SYSCONFIG);
1939 __raw_writel(0x00000000, bank->base +
1940 OMAP4_GPIO_DEBOUNCENABLE);
1941 /*
1942 * Initialize interface clock ungated,
1943 * module enabled
1944 */
1945 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1946 } else {
1947 __raw_writel(0x00000000, bank->base +
1948 OMAP24XX_GPIO_IRQENABLE1);
1949 __raw_writel(0xffffffff, bank->base +
1950 OMAP24XX_GPIO_IRQSTATUS1);
1951 __raw_writew(0x0015, bank->base +
1952 OMAP24XX_GPIO_SYSCONFIG);
1953 __raw_writel(0x00000000, bank->base +
1954 OMAP24XX_GPIO_DEBOUNCE_EN);
1955
1956 /*
1957 * Initialize interface clock ungated,
1958 * module enabled
1959 */
1960 __raw_writel(0, bank->base +
1961 OMAP24XX_GPIO_CTRL);
1962 }
Tero Kristoa118b5f2008-12-22 14:27:12 +02001963 if (cpu_is_omap24xx() &&
1964 i < ARRAY_SIZE(non_wakeup_gpios))
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001965 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001966 gpio_count = 32;
1967 }
1968#endif
Charulatha V058af1e2009-11-22 10:11:25 -08001969
1970 bank->mod_usage = 0;
David Brownell52e31342008-03-03 12:43:23 -08001971 /* REVISIT eventually switch from OMAP-specific gpio structs
1972 * over to the generic ones
1973 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001974 bank->chip.request = omap_gpio_request;
1975 bank->chip.free = omap_gpio_free;
David Brownell52e31342008-03-03 12:43:23 -08001976 bank->chip.direction_input = gpio_input;
1977 bank->chip.get = gpio_get;
1978 bank->chip.direction_output = gpio_output;
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001979 bank->chip.set_debounce = gpio_debounce;
David Brownell52e31342008-03-03 12:43:23 -08001980 bank->chip.set = gpio_set;
David Brownella007b702008-12-10 17:35:25 -08001981 bank->chip.to_irq = gpio_2irq;
David Brownell52e31342008-03-03 12:43:23 -08001982 if (bank_is_mpuio(bank)) {
1983 bank->chip.label = "mpuio";
Russell King69114a42008-09-03 10:15:26 +01001984#ifdef CONFIG_ARCH_OMAP16XX
David Brownelld8f388d82008-07-25 01:46:07 -07001985 bank->chip.dev = &omap_mpuio_device.dev;
1986#endif
David Brownell52e31342008-03-03 12:43:23 -08001987 bank->chip.base = OMAP_MPUIO(0);
1988 } else {
1989 bank->chip.label = "gpio";
1990 bank->chip.base = gpio;
1991 gpio += gpio_count;
1992 }
1993 bank->chip.ngpio = gpio_count;
1994
1995 gpiochip_add(&bank->chip);
1996
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001997 for (j = bank->virtual_irq_start;
1998 j < bank->virtual_irq_start + gpio_count; j++) {
David Brownell8ba55c52008-02-26 11:10:50 -08001999 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
David Brownell58781012006-12-06 17:14:10 -08002000 set_irq_chip_data(j, bank);
David Brownelle5c56ed2006-12-06 17:13:59 -08002001 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002002 set_irq_chip(j, &mpuio_irq_chip);
2003 else
2004 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00002005 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002006 set_irq_flags(j, IRQF_VALID);
2007 }
2008 set_irq_chained_handler(bank->irq, gpio_irq_handler);
2009 set_irq_data(bank->irq, bank);
Jouni Hogander89db9482008-12-10 17:35:24 -08002010
Santosh Shilimkar44169072009-05-28 14:16:04 -07002011 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jouni Hogander89db9482008-12-10 17:35:24 -08002012 sprintf(clk_name, "gpio%d_dbck", i + 1);
2013 bank->dbck = clk_get(NULL, clk_name);
2014 if (IS_ERR(bank->dbck))
2015 printk(KERN_ERR "Could not get %s\n", clk_name);
2016 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002017 }
2018
2019 /* Enable system clock for GPIO module.
2020 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01002021 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002022 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
2023
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08002024 /* Enable autoidle for the OCP interface */
2025 if (cpu_is_omap24xx())
2026 omap_writel(1 << 0, 0x48019010);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08002027 if (cpu_is_omap34xx())
2028 omap_writel(1 << 0, 0x48306814);
Tony Lindgrend11ac972008-01-12 15:35:04 -08002029
Tony Lindgren9f7065d2009-10-19 15:25:20 -07002030 omap_gpio_show_rev();
2031
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002032 return 0;
2033}
2034
Tony Lindgren140455f2010-02-12 12:26:48 -08002035#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +01002036static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
2037{
2038 int i;
2039
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08002040 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01002041 return 0;
2042
2043 for (i = 0; i < gpio_bank_count; i++) {
2044 struct gpio_bank *bank = &gpio_bank[i];
2045 void __iomem *wake_status;
2046 void __iomem *wake_clear;
2047 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08002048 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01002049
2050 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08002051#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01002052 case METHOD_GPIO_1610:
2053 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
2054 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
2055 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
2056 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08002057#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08002058#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01002059 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08002060 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01002061 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
2062 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
2063 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08002064#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302065#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002066 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302067 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
2068 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
2069 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
2070 break;
2071#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01002072 default:
2073 continue;
2074 }
2075
David Brownella6472532008-03-03 04:33:30 -08002076 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01002077 bank->saved_wakeup = __raw_readl(wake_status);
2078 __raw_writel(0xffffffff, wake_clear);
2079 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08002080 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01002081 }
2082
2083 return 0;
2084}
2085
2086static int omap_gpio_resume(struct sys_device *dev)
2087{
2088 int i;
2089
Tero Kristo723fdb72008-11-26 14:35:16 -08002090 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01002091 return 0;
2092
2093 for (i = 0; i < gpio_bank_count; i++) {
2094 struct gpio_bank *bank = &gpio_bank[i];
2095 void __iomem *wake_clear;
2096 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08002097 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01002098
2099 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08002100#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01002101 case METHOD_GPIO_1610:
2102 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
2103 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
2104 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08002105#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08002106#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01002107 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03002108 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
2109 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01002110 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08002111#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302112#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002113 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302114 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
2115 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
2116 break;
2117#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01002118 default:
2119 continue;
2120 }
2121
David Brownella6472532008-03-03 04:33:30 -08002122 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01002123 __raw_writel(0xffffffff, wake_clear);
2124 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08002125 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01002126 }
2127
2128 return 0;
2129}
2130
2131static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01002132 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01002133 .suspend = omap_gpio_suspend,
2134 .resume = omap_gpio_resume,
2135};
2136
2137static struct sys_device omap_gpio_device = {
2138 .id = 0,
2139 .cls = &omap_gpio_sysclass,
2140};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002141
2142#endif
2143
Tony Lindgren140455f2010-02-12 12:26:48 -08002144#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002145
2146static int workaround_enabled;
2147
Kevin Hilman43ffcd92009-01-27 11:09:24 -08002148void omap2_gpio_prepare_for_idle(int power_state)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002149{
2150 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02002151 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002152
Tero Kristoa118b5f2008-12-22 14:27:12 +02002153 if (cpu_is_omap34xx())
2154 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08002155
Tero Kristoa118b5f2008-12-22 14:27:12 +02002156 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002157 struct gpio_bank *bank = &gpio_bank[i];
2158 u32 l1, l2;
2159
Kevin Hilman8865b9b2009-01-27 11:15:34 -08002160 if (bank->dbck_enable_mask)
2161 clk_disable(bank->dbck);
2162
Kevin Hilman43ffcd92009-01-27 11:09:24 -08002163 if (power_state > PWRDM_POWER_OFF)
2164 continue;
2165
2166 /* If going to OFF, remove triggering for all
2167 * non-wakeup GPIOs. Otherwise spurious IRQs will be
2168 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002169 if (!(bank->enabled_non_wakeup_gpios))
2170 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002171
2172 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
2173 bank->saved_datain = __raw_readl(bank->base +
2174 OMAP24XX_GPIO_DATAIN);
2175 l1 = __raw_readl(bank->base +
2176 OMAP24XX_GPIO_FALLINGDETECT);
2177 l2 = __raw_readl(bank->base +
2178 OMAP24XX_GPIO_RISINGDETECT);
2179 }
2180
2181 if (cpu_is_omap44xx()) {
2182 bank->saved_datain = __raw_readl(bank->base +
2183 OMAP4_GPIO_DATAIN);
2184 l1 = __raw_readl(bank->base +
2185 OMAP4_GPIO_FALLINGDETECT);
2186 l2 = __raw_readl(bank->base +
2187 OMAP4_GPIO_RISINGDETECT);
2188 }
2189
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002190 bank->saved_fallingdetect = l1;
2191 bank->saved_risingdetect = l2;
2192 l1 &= ~bank->enabled_non_wakeup_gpios;
2193 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002194
2195 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
2196 __raw_writel(l1, bank->base +
2197 OMAP24XX_GPIO_FALLINGDETECT);
2198 __raw_writel(l2, bank->base +
2199 OMAP24XX_GPIO_RISINGDETECT);
2200 }
2201
2202 if (cpu_is_omap44xx()) {
2203 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
2204 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
2205 }
2206
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002207 c++;
2208 }
2209 if (!c) {
2210 workaround_enabled = 0;
2211 return;
2212 }
2213 workaround_enabled = 1;
2214}
2215
Kevin Hilman43ffcd92009-01-27 11:09:24 -08002216void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002217{
2218 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02002219 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002220
Tero Kristoa118b5f2008-12-22 14:27:12 +02002221 if (cpu_is_omap34xx())
2222 min = 1;
2223 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002224 struct gpio_bank *bank = &gpio_bank[i];
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07002225 u32 l, gen, gen0, gen1;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002226
Kevin Hilman8865b9b2009-01-27 11:15:34 -08002227 if (bank->dbck_enable_mask)
2228 clk_enable(bank->dbck);
2229
Kevin Hilman43ffcd92009-01-27 11:09:24 -08002230 if (!workaround_enabled)
2231 continue;
2232
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002233 if (!(bank->enabled_non_wakeup_gpios))
2234 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002235
2236 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
2237 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002238 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002239 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002240 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002241 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
2242 }
2243
2244 if (cpu_is_omap44xx()) {
2245 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302246 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002247 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302248 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002249 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
2250 }
2251
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002252 /* Check if any of the non-wakeup interrupt GPIOs have changed
2253 * state. If so, generate an IRQ by software. This is
2254 * horribly racy, but it's the best we can do to work around
2255 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002256 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02002257 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07002258
2259 /*
2260 * No need to generate IRQs for the rising edge for gpio IRQs
2261 * configured with falling edge only; and vice versa.
2262 */
2263 gen0 = l & bank->saved_fallingdetect;
2264 gen0 &= bank->saved_datain;
2265
2266 gen1 = l & bank->saved_risingdetect;
2267 gen1 &= ~(bank->saved_datain);
2268
2269 /* FIXME: Consider GPIO IRQs with level detections properly! */
2270 gen = l & (~(bank->saved_fallingdetect) &
2271 ~(bank->saved_risingdetect));
2272 /* Consider all GPIO IRQs needed to be updated */
2273 gen |= gen0 | gen1;
2274
2275 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002276 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002277
Sergio Aguirref00d6492010-03-03 16:21:08 +00002278 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002279 old0 = __raw_readl(bank->base +
2280 OMAP24XX_GPIO_LEVELDETECT0);
2281 old1 = __raw_readl(bank->base +
2282 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00002283 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07002284 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00002285 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07002286 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00002287 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002288 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00002289 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002290 OMAP24XX_GPIO_LEVELDETECT1);
2291 }
2292
2293 if (cpu_is_omap44xx()) {
2294 old0 = __raw_readl(bank->base +
2295 OMAP4_GPIO_LEVELDETECT0);
2296 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302297 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002298 __raw_writel(old0 | l, bank->base +
2299 OMAP4_GPIO_LEVELDETECT0);
2300 __raw_writel(old1 | l, bank->base +
2301 OMAP4_GPIO_LEVELDETECT1);
2302 __raw_writel(old0, bank->base +
2303 OMAP4_GPIO_LEVELDETECT0);
2304 __raw_writel(old1, bank->base +
2305 OMAP4_GPIO_LEVELDETECT1);
2306 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002307 }
2308 }
2309
2310}
2311
Tony Lindgren92105bb2005-09-07 17:20:26 +01002312#endif
2313
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08002314#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302315/* save the registers of bank 2-6 */
2316void omap_gpio_save_context(void)
2317{
2318 int i;
2319
2320 /* saving banks from 2-6 only since GPIO1 is in WKUP */
2321 for (i = 1; i < gpio_bank_count; i++) {
2322 struct gpio_bank *bank = &gpio_bank[i];
2323 gpio_context[i].sysconfig =
2324 __raw_readl(bank->base + OMAP24XX_GPIO_SYSCONFIG);
2325 gpio_context[i].irqenable1 =
2326 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
2327 gpio_context[i].irqenable2 =
2328 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
2329 gpio_context[i].wake_en =
2330 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
2331 gpio_context[i].ctrl =
2332 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
2333 gpio_context[i].oe =
2334 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
2335 gpio_context[i].leveldetect0 =
2336 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
2337 gpio_context[i].leveldetect1 =
2338 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
2339 gpio_context[i].risingdetect =
2340 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
2341 gpio_context[i].fallingdetect =
2342 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
2343 gpio_context[i].dataout =
2344 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302345 }
2346}
2347
2348/* restore the required registers of bank 2-6 */
2349void omap_gpio_restore_context(void)
2350{
2351 int i;
2352
2353 for (i = 1; i < gpio_bank_count; i++) {
2354 struct gpio_bank *bank = &gpio_bank[i];
2355 __raw_writel(gpio_context[i].sysconfig,
2356 bank->base + OMAP24XX_GPIO_SYSCONFIG);
2357 __raw_writel(gpio_context[i].irqenable1,
2358 bank->base + OMAP24XX_GPIO_IRQENABLE1);
2359 __raw_writel(gpio_context[i].irqenable2,
2360 bank->base + OMAP24XX_GPIO_IRQENABLE2);
2361 __raw_writel(gpio_context[i].wake_en,
2362 bank->base + OMAP24XX_GPIO_WAKE_EN);
2363 __raw_writel(gpio_context[i].ctrl,
2364 bank->base + OMAP24XX_GPIO_CTRL);
2365 __raw_writel(gpio_context[i].oe,
2366 bank->base + OMAP24XX_GPIO_OE);
2367 __raw_writel(gpio_context[i].leveldetect0,
2368 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
2369 __raw_writel(gpio_context[i].leveldetect1,
2370 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
2371 __raw_writel(gpio_context[i].risingdetect,
2372 bank->base + OMAP24XX_GPIO_RISINGDETECT);
2373 __raw_writel(gpio_context[i].fallingdetect,
2374 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
2375 __raw_writel(gpio_context[i].dataout,
2376 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302377 }
2378}
2379#endif
2380
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002381/*
2382 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002383 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002384 */
David Brownell277d58e2006-12-06 17:13:59 -08002385int __init omap_gpio_init(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002386{
2387 if (!initialized)
2388 return _omap_gpio_init();
2389 else
2390 return 0;
2391}
2392
Tony Lindgren92105bb2005-09-07 17:20:26 +01002393static int __init omap_gpio_sysinit(void)
2394{
2395 int ret = 0;
2396
2397 if (!initialized)
2398 ret = _omap_gpio_init();
2399
David Brownell11a78b72006-12-06 17:14:11 -08002400 mpuio_init();
2401
Tony Lindgren140455f2010-02-12 12:26:48 -08002402#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08002403 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01002404 if (ret == 0) {
2405 ret = sysdev_class_register(&omap_gpio_sysclass);
2406 if (ret == 0)
2407 ret = sysdev_register(&omap_gpio_device);
2408 }
2409 }
2410#endif
2411
2412 return ret;
2413}
2414
Tony Lindgren92105bb2005-09-07 17:20:26 +01002415arch_initcall(omap_gpio_sysinit);