blob: b0c73613a4e92cbbc2813dc53996e14c5f9a572c [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>
30
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031/*
32 * OMAP1510 GPIO registers
33 */
Tony Lindgren94113262009-08-28 10:50:33 -070034#define OMAP1510_GPIO_BASE OMAP1_IO_ADDRESS(0xfffce000)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010035#define OMAP1510_GPIO_DATA_INPUT 0x00
36#define OMAP1510_GPIO_DATA_OUTPUT 0x04
37#define OMAP1510_GPIO_DIR_CONTROL 0x08
38#define OMAP1510_GPIO_INT_CONTROL 0x0c
39#define OMAP1510_GPIO_INT_MASK 0x10
40#define OMAP1510_GPIO_INT_STATUS 0x14
41#define OMAP1510_GPIO_PIN_CONTROL 0x18
42
43#define OMAP1510_IH_GPIO_BASE 64
44
45/*
46 * OMAP1610 specific GPIO registers
47 */
Tony Lindgren94113262009-08-28 10:50:33 -070048#define OMAP1610_GPIO1_BASE OMAP1_IO_ADDRESS(0xfffbe400)
49#define OMAP1610_GPIO2_BASE OMAP1_IO_ADDRESS(0xfffbec00)
50#define OMAP1610_GPIO3_BASE OMAP1_IO_ADDRESS(0xfffbb400)
51#define OMAP1610_GPIO4_BASE OMAP1_IO_ADDRESS(0xfffbbc00)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010052#define OMAP1610_GPIO_REVISION 0x0000
53#define OMAP1610_GPIO_SYSCONFIG 0x0010
54#define OMAP1610_GPIO_SYSSTATUS 0x0014
55#define OMAP1610_GPIO_IRQSTATUS1 0x0018
56#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010057#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058#define OMAP1610_GPIO_DATAIN 0x002c
59#define OMAP1610_GPIO_DATAOUT 0x0030
60#define OMAP1610_GPIO_DIRECTION 0x0034
61#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
62#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
63#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010064#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
66#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010067#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010068#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
69
70/*
Alistair Buxton7c006922009-09-22 10:02:58 +010071 * OMAP7XX specific GPIO registers
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010072 */
Alistair Buxton7c006922009-09-22 10:02:58 +010073#define OMAP7XX_GPIO1_BASE OMAP1_IO_ADDRESS(0xfffbc000)
74#define OMAP7XX_GPIO2_BASE OMAP1_IO_ADDRESS(0xfffbc800)
75#define OMAP7XX_GPIO3_BASE OMAP1_IO_ADDRESS(0xfffbd000)
76#define OMAP7XX_GPIO4_BASE OMAP1_IO_ADDRESS(0xfffbd800)
77#define OMAP7XX_GPIO5_BASE OMAP1_IO_ADDRESS(0xfffbe000)
78#define OMAP7XX_GPIO6_BASE OMAP1_IO_ADDRESS(0xfffbe800)
79#define OMAP7XX_GPIO_DATA_INPUT 0x00
80#define OMAP7XX_GPIO_DATA_OUTPUT 0x04
81#define OMAP7XX_GPIO_DIR_CONTROL 0x08
82#define OMAP7XX_GPIO_INT_CONTROL 0x0c
83#define OMAP7XX_GPIO_INT_MASK 0x10
84#define OMAP7XX_GPIO_INT_STATUS 0x14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085
Tony Lindgren61755562009-08-28 10:50:34 -070086#define OMAP1_MPUIO_VBASE OMAP1_IO_ADDRESS(OMAP1_MPUIO_BASE)
Tony Lindgren94113262009-08-28 10:50:33 -070087
Zebediah C. McClure56739a62009-03-23 18:07:40 -070088/*
Tony Lindgren92105bb2005-09-07 17:20:26 +010089 * omap24xx specific GPIO registers
90 */
Tony Lindgren94113262009-08-28 10:50:33 -070091#define OMAP242X_GPIO1_BASE OMAP2_IO_ADDRESS(0x48018000)
92#define OMAP242X_GPIO2_BASE OMAP2_IO_ADDRESS(0x4801a000)
93#define OMAP242X_GPIO3_BASE OMAP2_IO_ADDRESS(0x4801c000)
94#define OMAP242X_GPIO4_BASE OMAP2_IO_ADDRESS(0x4801e000)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -080095
Tony Lindgren94113262009-08-28 10:50:33 -070096#define OMAP243X_GPIO1_BASE OMAP2_IO_ADDRESS(0x4900C000)
97#define OMAP243X_GPIO2_BASE OMAP2_IO_ADDRESS(0x4900E000)
98#define OMAP243X_GPIO3_BASE OMAP2_IO_ADDRESS(0x49010000)
99#define OMAP243X_GPIO4_BASE OMAP2_IO_ADDRESS(0x49012000)
100#define OMAP243X_GPIO5_BASE OMAP2_IO_ADDRESS(0x480B6000)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800101
Tony Lindgren92105bb2005-09-07 17:20:26 +0100102#define OMAP24XX_GPIO_REVISION 0x0000
103#define OMAP24XX_GPIO_SYSCONFIG 0x0010
104#define OMAP24XX_GPIO_SYSSTATUS 0x0014
105#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300106#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
107#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +0100108#define OMAP24XX_GPIO_IRQENABLE1 0x001c
Tero Kristo723fdb72008-11-26 14:35:16 -0800109#define OMAP24XX_GPIO_WAKE_EN 0x0020
Tony Lindgren92105bb2005-09-07 17:20:26 +0100110#define OMAP24XX_GPIO_CTRL 0x0030
111#define OMAP24XX_GPIO_OE 0x0034
112#define OMAP24XX_GPIO_DATAIN 0x0038
113#define OMAP24XX_GPIO_DATAOUT 0x003c
114#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
115#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
116#define OMAP24XX_GPIO_RISINGDETECT 0x0048
117#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700118#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
119#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +0100120#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
121#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
122#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
123#define OMAP24XX_GPIO_SETWKUENA 0x0084
124#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
125#define OMAP24XX_GPIO_SETDATAOUT 0x0094
126
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530127#define OMAP4_GPIO_REVISION 0x0000
128#define OMAP4_GPIO_SYSCONFIG 0x0010
129#define OMAP4_GPIO_EOI 0x0020
130#define OMAP4_GPIO_IRQSTATUSRAW0 0x0024
131#define OMAP4_GPIO_IRQSTATUSRAW1 0x0028
132#define OMAP4_GPIO_IRQSTATUS0 0x002c
133#define OMAP4_GPIO_IRQSTATUS1 0x0030
134#define OMAP4_GPIO_IRQSTATUSSET0 0x0034
135#define OMAP4_GPIO_IRQSTATUSSET1 0x0038
136#define OMAP4_GPIO_IRQSTATUSCLR0 0x003c
137#define OMAP4_GPIO_IRQSTATUSCLR1 0x0040
138#define OMAP4_GPIO_IRQWAKEN0 0x0044
139#define OMAP4_GPIO_IRQWAKEN1 0x0048
140#define OMAP4_GPIO_SYSSTATUS 0x0104
141#define OMAP4_GPIO_CTRL 0x0130
142#define OMAP4_GPIO_OE 0x0134
143#define OMAP4_GPIO_DATAIN 0x0138
144#define OMAP4_GPIO_DATAOUT 0x013c
145#define OMAP4_GPIO_LEVELDETECT0 0x0140
146#define OMAP4_GPIO_LEVELDETECT1 0x0144
147#define OMAP4_GPIO_RISINGDETECT 0x0148
148#define OMAP4_GPIO_FALLINGDETECT 0x014c
149#define OMAP4_GPIO_DEBOUNCENABLE 0x0150
150#define OMAP4_GPIO_DEBOUNCINGTIME 0x0154
151#define OMAP4_GPIO_CLEARDATAOUT 0x0190
152#define OMAP4_GPIO_SETDATAOUT 0x0194
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800153/*
154 * omap34xx specific GPIO registers
155 */
156
Tony Lindgren94113262009-08-28 10:50:33 -0700157#define OMAP34XX_GPIO1_BASE OMAP2_IO_ADDRESS(0x48310000)
158#define OMAP34XX_GPIO2_BASE OMAP2_IO_ADDRESS(0x49050000)
159#define OMAP34XX_GPIO3_BASE OMAP2_IO_ADDRESS(0x49052000)
160#define OMAP34XX_GPIO4_BASE OMAP2_IO_ADDRESS(0x49054000)
161#define OMAP34XX_GPIO5_BASE OMAP2_IO_ADDRESS(0x49056000)
162#define OMAP34XX_GPIO6_BASE OMAP2_IO_ADDRESS(0x49058000)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800163
Santosh Shilimkar44169072009-05-28 14:16:04 -0700164/*
165 * OMAP44XX specific GPIO registers
166 */
Tony Lindgren94113262009-08-28 10:50:33 -0700167#define OMAP44XX_GPIO1_BASE OMAP2_IO_ADDRESS(0x4a310000)
168#define OMAP44XX_GPIO2_BASE OMAP2_IO_ADDRESS(0x48055000)
169#define OMAP44XX_GPIO3_BASE OMAP2_IO_ADDRESS(0x48057000)
170#define OMAP44XX_GPIO4_BASE OMAP2_IO_ADDRESS(0x48059000)
171#define OMAP44XX_GPIO5_BASE OMAP2_IO_ADDRESS(0x4805B000)
172#define OMAP44XX_GPIO6_BASE OMAP2_IO_ADDRESS(0x4805D000)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800173
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100174struct gpio_bank {
Tony Lindgren92105bb2005-09-07 17:20:26 +0100175 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100176 u16 irq;
177 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100178 int method;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700179#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
180 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100181 u32 suspend_wakeup;
182 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800183#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700184#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
185 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800186 u32 non_wakeup_gpios;
187 u32 enabled_non_wakeup_gpios;
188
189 u32 saved_datain;
190 u32 saved_fallingdetect;
191 u32 saved_risingdetect;
192#endif
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800193 u32 level_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100194 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800195 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -0800196 struct clk *dbck;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100197};
198
199#define METHOD_MPUIO 0
200#define METHOD_GPIO_1510 1
201#define METHOD_GPIO_1610 2
Alistair Buxton7c006922009-09-22 10:02:58 +0100202#define METHOD_GPIO_7XX 3
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700203#define METHOD_GPIO_24XX 5
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100204
Tony Lindgren92105bb2005-09-07 17:20:26 +0100205#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100206static struct gpio_bank gpio_bank_1610[5] = {
Tony Lindgren94113262009-08-28 10:50:33 -0700207 { OMAP1_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO},
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208 { OMAP1610_GPIO1_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1610 },
209 { OMAP1610_GPIO2_BASE, INT_1610_GPIO_BANK2, IH_GPIO_BASE + 16, METHOD_GPIO_1610 },
210 { OMAP1610_GPIO3_BASE, INT_1610_GPIO_BANK3, IH_GPIO_BASE + 32, METHOD_GPIO_1610 },
211 { OMAP1610_GPIO4_BASE, INT_1610_GPIO_BANK4, IH_GPIO_BASE + 48, METHOD_GPIO_1610 },
212};
213#endif
214
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000215#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100216static struct gpio_bank gpio_bank_1510[2] = {
Tony Lindgren94113262009-08-28 10:50:33 -0700217 { OMAP1_MPUIO_VBASE, INT_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100218 { OMAP1510_GPIO_BASE, INT_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_1510 }
219};
220#endif
221
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100222#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100223static struct gpio_bank gpio_bank_7xx[7] = {
Alistair Buxton372b1c32009-09-18 04:09:39 +0100224 { OMAP1_MPUIO_VBASE, INT_7XX_MPUIO, IH_MPUIO_BASE, METHOD_MPUIO },
Alistair Buxton7c006922009-09-22 10:02:58 +0100225 { OMAP7XX_GPIO1_BASE, INT_7XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_7XX },
226 { OMAP7XX_GPIO2_BASE, INT_7XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_7XX },
227 { OMAP7XX_GPIO3_BASE, INT_7XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_7XX },
228 { OMAP7XX_GPIO4_BASE, INT_7XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_7XX },
229 { OMAP7XX_GPIO5_BASE, INT_7XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_7XX },
230 { OMAP7XX_GPIO6_BASE, INT_7XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_7XX },
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231};
232#endif
233
Tony Lindgren92105bb2005-09-07 17:20:26 +0100234#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800235
236static struct gpio_bank gpio_bank_242x[4] = {
237 { OMAP242X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
238 { OMAP242X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
239 { OMAP242X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
240 { OMAP242X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
Tony Lindgren92105bb2005-09-07 17:20:26 +0100241};
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800242
243static struct gpio_bank gpio_bank_243x[5] = {
244 { OMAP243X_GPIO1_BASE, INT_24XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
245 { OMAP243X_GPIO2_BASE, INT_24XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
246 { OMAP243X_GPIO3_BASE, INT_24XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
247 { OMAP243X_GPIO4_BASE, INT_24XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
248 { OMAP243X_GPIO5_BASE, INT_24XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
249};
250
Tony Lindgren92105bb2005-09-07 17:20:26 +0100251#endif
252
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800253#ifdef CONFIG_ARCH_OMAP34XX
254static struct gpio_bank gpio_bank_34xx[6] = {
255 { OMAP34XX_GPIO1_BASE, INT_34XX_GPIO_BANK1, IH_GPIO_BASE, METHOD_GPIO_24XX },
256 { OMAP34XX_GPIO2_BASE, INT_34XX_GPIO_BANK2, IH_GPIO_BASE + 32, METHOD_GPIO_24XX },
257 { OMAP34XX_GPIO3_BASE, INT_34XX_GPIO_BANK3, IH_GPIO_BASE + 64, METHOD_GPIO_24XX },
258 { OMAP34XX_GPIO4_BASE, INT_34XX_GPIO_BANK4, IH_GPIO_BASE + 96, METHOD_GPIO_24XX },
259 { OMAP34XX_GPIO5_BASE, INT_34XX_GPIO_BANK5, IH_GPIO_BASE + 128, METHOD_GPIO_24XX },
260 { OMAP34XX_GPIO6_BASE, INT_34XX_GPIO_BANK6, IH_GPIO_BASE + 160, METHOD_GPIO_24XX },
261};
262
263#endif
264
Santosh Shilimkar44169072009-05-28 14:16:04 -0700265#ifdef CONFIG_ARCH_OMAP4
266static struct gpio_bank gpio_bank_44xx[6] = {
267 { OMAP44XX_GPIO1_BASE, INT_44XX_GPIO_BANK1, IH_GPIO_BASE, \
268 METHOD_GPIO_24XX },
269 { OMAP44XX_GPIO2_BASE, INT_44XX_GPIO_BANK2, IH_GPIO_BASE + 32, \
270 METHOD_GPIO_24XX },
271 { OMAP44XX_GPIO3_BASE, INT_44XX_GPIO_BANK3, IH_GPIO_BASE + 64, \
272 METHOD_GPIO_24XX },
273 { OMAP44XX_GPIO4_BASE, INT_44XX_GPIO_BANK4, IH_GPIO_BASE + 96, \
274 METHOD_GPIO_24XX },
275 { OMAP44XX_GPIO5_BASE, INT_44XX_GPIO_BANK5, IH_GPIO_BASE + 128, \
276 METHOD_GPIO_24XX },
277 { OMAP44XX_GPIO6_BASE, INT_44XX_GPIO_BANK6, IH_GPIO_BASE + 160, \
278 METHOD_GPIO_24XX },
279};
280
281#endif
282
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283static struct gpio_bank *gpio_bank;
284static int gpio_bank_count;
285
286static inline struct gpio_bank *get_gpio_bank(int gpio)
287{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100288 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289 if (OMAP_GPIO_IS_MPUIO(gpio))
290 return &gpio_bank[0];
291 return &gpio_bank[1];
292 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100293 if (cpu_is_omap16xx()) {
294 if (OMAP_GPIO_IS_MPUIO(gpio))
295 return &gpio_bank[0];
296 return &gpio_bank[1 + (gpio >> 4)];
297 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700298 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100299 if (OMAP_GPIO_IS_MPUIO(gpio))
300 return &gpio_bank[0];
301 return &gpio_bank[1 + (gpio >> 5)];
302 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100303 if (cpu_is_omap24xx())
304 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700305 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800306 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800307 BUG();
308 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100309}
310
311static inline int get_gpio_index(int gpio)
312{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700313 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100314 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100315 if (cpu_is_omap24xx())
316 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700317 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800318 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100319 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320}
321
322static inline int gpio_valid(int gpio)
323{
324 if (gpio < 0)
325 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800326 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300327 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328 return -1;
329 return 0;
330 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100331 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100333 if ((cpu_is_omap16xx()) && gpio < 64)
334 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700335 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100336 return 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100337 if (cpu_is_omap24xx() && gpio < 128)
338 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700339 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800340 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100341 return -1;
342}
343
344static int check_gpio(int gpio)
345{
346 if (unlikely(gpio_valid(gpio)) < 0) {
347 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
348 dump_stack();
349 return -1;
350 }
351 return 0;
352}
353
354static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
355{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100356 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100357 u32 l;
358
359 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800360#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100361 case METHOD_MPUIO:
362 reg += OMAP_MPUIO_IO_CNTL;
363 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800364#endif
365#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366 case METHOD_GPIO_1510:
367 reg += OMAP1510_GPIO_DIR_CONTROL;
368 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800369#endif
370#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100371 case METHOD_GPIO_1610:
372 reg += OMAP1610_GPIO_DIRECTION;
373 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800374#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100375#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100376 case METHOD_GPIO_7XX:
377 reg += OMAP7XX_GPIO_DIR_CONTROL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100378 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800379#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530380#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100381 case METHOD_GPIO_24XX:
382 reg += OMAP24XX_GPIO_OE;
383 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800384#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530385#if defined(CONFIG_ARCH_OMAP4)
386 case METHOD_GPIO_24XX:
387 reg += OMAP4_GPIO_OE;
388 break;
389#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800390 default:
391 WARN_ON(1);
392 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100393 }
394 l = __raw_readl(reg);
395 if (is_input)
396 l |= 1 << gpio;
397 else
398 l &= ~(1 << gpio);
399 __raw_writel(l, reg);
400}
401
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100402static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
403{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100404 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405 u32 l = 0;
406
407 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800408#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100409 case METHOD_MPUIO:
410 reg += OMAP_MPUIO_OUTPUT;
411 l = __raw_readl(reg);
412 if (enable)
413 l |= 1 << gpio;
414 else
415 l &= ~(1 << gpio);
416 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800417#endif
418#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100419 case METHOD_GPIO_1510:
420 reg += OMAP1510_GPIO_DATA_OUTPUT;
421 l = __raw_readl(reg);
422 if (enable)
423 l |= 1 << gpio;
424 else
425 l &= ~(1 << gpio);
426 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800427#endif
428#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429 case METHOD_GPIO_1610:
430 if (enable)
431 reg += OMAP1610_GPIO_SET_DATAOUT;
432 else
433 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
434 l = 1 << gpio;
435 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800436#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100437#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100438 case METHOD_GPIO_7XX:
439 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100440 l = __raw_readl(reg);
441 if (enable)
442 l |= 1 << gpio;
443 else
444 l &= ~(1 << gpio);
445 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800446#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530447#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100448 case METHOD_GPIO_24XX:
449 if (enable)
450 reg += OMAP24XX_GPIO_SETDATAOUT;
451 else
452 reg += OMAP24XX_GPIO_CLEARDATAOUT;
453 l = 1 << gpio;
454 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800455#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530456#ifdef CONFIG_ARCH_OMAP4
457 case METHOD_GPIO_24XX:
458 if (enable)
459 reg += OMAP4_GPIO_SETDATAOUT;
460 else
461 reg += OMAP4_GPIO_CLEARDATAOUT;
462 l = 1 << gpio;
463 break;
464#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800466 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467 return;
468 }
469 __raw_writel(l, reg);
470}
471
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300472static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100474 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475
476 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800477 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 reg = bank->base;
479 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800480#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481 case METHOD_MPUIO:
482 reg += OMAP_MPUIO_INPUT_LATCH;
483 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800484#endif
485#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486 case METHOD_GPIO_1510:
487 reg += OMAP1510_GPIO_DATA_INPUT;
488 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800489#endif
490#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491 case METHOD_GPIO_1610:
492 reg += OMAP1610_GPIO_DATAIN;
493 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800494#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100495#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100496 case METHOD_GPIO_7XX:
497 reg += OMAP7XX_GPIO_DATA_INPUT;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800499#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530500#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100501 case METHOD_GPIO_24XX:
502 reg += OMAP24XX_GPIO_DATAIN;
503 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800504#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530505#ifdef CONFIG_ARCH_OMAP4
506 case METHOD_GPIO_24XX:
507 reg += OMAP4_GPIO_DATAIN;
508 break;
509#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800511 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100513 return (__raw_readl(reg)
514 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100515}
516
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300517static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
518{
519 void __iomem *reg;
520
521 if (check_gpio(gpio) < 0)
522 return -EINVAL;
523 reg = bank->base;
524
525 switch (bank->method) {
526#ifdef CONFIG_ARCH_OMAP1
527 case METHOD_MPUIO:
528 reg += OMAP_MPUIO_OUTPUT;
529 break;
530#endif
531#ifdef CONFIG_ARCH_OMAP15XX
532 case METHOD_GPIO_1510:
533 reg += OMAP1510_GPIO_DATA_OUTPUT;
534 break;
535#endif
536#ifdef CONFIG_ARCH_OMAP16XX
537 case METHOD_GPIO_1610:
538 reg += OMAP1610_GPIO_DATAOUT;
539 break;
540#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100541#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100542 case METHOD_GPIO_7XX:
543 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300544 break;
545#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300546#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
547 defined(CONFIG_ARCH_OMAP4)
548 case METHOD_GPIO_24XX:
549 reg += OMAP24XX_GPIO_DATAOUT;
550 break;
551#endif
552 default:
553 return -EINVAL;
554 }
555
556 return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
557}
558
Tony Lindgren92105bb2005-09-07 17:20:26 +0100559#define MOD_REG_BIT(reg, bit_mask, set) \
560do { \
561 int l = __raw_readl(base + reg); \
562 if (set) l |= bit_mask; \
563 else l &= ~bit_mask; \
564 __raw_writel(l, base + reg); \
565} while(0)
566
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700567void omap_set_gpio_debounce(int gpio, int enable)
568{
569 struct gpio_bank *bank;
570 void __iomem *reg;
David Brownelle031ab22008-12-10 17:35:27 -0800571 unsigned long flags;
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700572 u32 val, l = 1 << get_gpio_index(gpio);
573
574 if (cpu_class_is_omap1())
575 return;
576
577 bank = get_gpio_bank(gpio);
578 reg = bank->base;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530579#ifdef CONFIG_ARCH_OMAP4
580 reg += OMAP4_GPIO_DEBOUNCENABLE;
581#else
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700582 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530583#endif
David Brownelle031ab22008-12-10 17:35:27 -0800584
585 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700586 val = __raw_readl(reg);
587
Jouni Hogander89db9482008-12-10 17:35:24 -0800588 if (enable && !(val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700589 val |= l;
David Brownelle031ab22008-12-10 17:35:27 -0800590 else if (!enable && (val & l))
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700591 val &= ~l;
Jouni Hogander89db9482008-12-10 17:35:24 -0800592 else
David Brownelle031ab22008-12-10 17:35:27 -0800593 goto done;
Jouni Hogander89db9482008-12-10 17:35:24 -0800594
Santosh Shilimkar44169072009-05-28 14:16:04 -0700595 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
David Brownelle031ab22008-12-10 17:35:27 -0800596 if (enable)
597 clk_enable(bank->dbck);
598 else
599 clk_disable(bank->dbck);
600 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700601
602 __raw_writel(val, reg);
David Brownelle031ab22008-12-10 17:35:27 -0800603done:
604 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700605}
606EXPORT_SYMBOL(omap_set_gpio_debounce);
607
608void omap_set_gpio_debounce_time(int gpio, int enc_time)
609{
610 struct gpio_bank *bank;
611 void __iomem *reg;
612
613 if (cpu_class_is_omap1())
614 return;
615
616 bank = get_gpio_bank(gpio);
617 reg = bank->base;
618
619 enc_time &= 0xff;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530620#ifdef CONFIG_ARCH_OMAP4
621 reg += OMAP4_GPIO_DEBOUNCINGTIME;
622#else
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700623 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530624#endif
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700625 __raw_writel(enc_time, reg);
626}
627EXPORT_SYMBOL(omap_set_gpio_debounce_time);
628
Santosh Shilimkar44169072009-05-28 14:16:04 -0700629#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
630 defined(CONFIG_ARCH_OMAP4)
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700631static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
632 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100633{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800634 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100635 u32 gpio_bit = 1 << gpio;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530636 u32 val;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100637
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530638 if (cpu_is_omap44xx()) {
639 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
640 trigger & IRQ_TYPE_LEVEL_LOW);
641 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
642 trigger & IRQ_TYPE_LEVEL_HIGH);
643 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
644 trigger & IRQ_TYPE_EDGE_RISING);
645 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
646 trigger & IRQ_TYPE_EDGE_FALLING);
647 } else {
648 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
649 trigger & IRQ_TYPE_LEVEL_LOW);
650 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
651 trigger & IRQ_TYPE_LEVEL_HIGH);
652 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
653 trigger & IRQ_TYPE_EDGE_RISING);
654 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
655 trigger & IRQ_TYPE_EDGE_FALLING);
656 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800657 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530658 if (cpu_is_omap44xx()) {
659 if (trigger != 0)
660 __raw_writel(1 << gpio, bank->base+
661 OMAP4_GPIO_IRQWAKEN0);
662 else {
663 val = __raw_readl(bank->base +
664 OMAP4_GPIO_IRQWAKEN0);
665 __raw_writel(val & (~(1 << gpio)), bank->base +
666 OMAP4_GPIO_IRQWAKEN0);
667 }
668 } else {
669 if (trigger != 0)
670 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700671 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530672 else
673 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700674 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530675 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800676 } else {
677 if (trigger != 0)
678 bank->enabled_non_wakeup_gpios |= gpio_bit;
679 else
680 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
681 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700682
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530683 if (cpu_is_omap44xx()) {
684 bank->level_mask =
685 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
686 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
687 } else {
688 bank->level_mask =
689 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
690 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
691 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100692}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800693#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100694
695static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
696{
697 void __iomem *reg = bank->base;
698 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100699
700 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800701#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702 case METHOD_MPUIO:
703 reg += OMAP_MPUIO_GPIO_INT_EDGE;
704 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100705 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100707 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100708 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100709 else
710 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100711 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800712#endif
713#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100714 case METHOD_GPIO_1510:
715 reg += OMAP1510_GPIO_INT_CONTROL;
716 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100717 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100719 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100721 else
722 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800724#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800725#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100727 if (gpio & 0x08)
728 reg += OMAP1610_GPIO_EDGE_CTRL2;
729 else
730 reg += OMAP1610_GPIO_EDGE_CTRL1;
731 gpio &= 0x07;
732 l = __raw_readl(reg);
733 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100734 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100735 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100736 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100737 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800738 if (trigger)
739 /* Enable wake-up during idle for dynamic tick */
740 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
741 else
742 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800744#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100745#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100746 case METHOD_GPIO_7XX:
747 reg += OMAP7XX_GPIO_INT_CONTROL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748 l = __raw_readl(reg);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100749 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100751 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100753 else
754 goto bad;
755 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800756#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -0700757#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
758 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100759 case METHOD_GPIO_24XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800760 set_24xx_gpio_triggering(bank, gpio, trigger);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800762#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100764 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100765 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100766 __raw_writel(l, reg);
767 return 0;
768bad:
769 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100770}
771
Tony Lindgren92105bb2005-09-07 17:20:26 +0100772static int gpio_irq_type(unsigned irq, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773{
774 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100775 unsigned gpio;
776 int retval;
David Brownella6472532008-03-03 04:33:30 -0800777 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100778
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800779 if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100780 gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
781 else
782 gpio = irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100783
784 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100785 return -EINVAL;
786
David Brownelle5c56ed2006-12-06 17:13:59 -0800787 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100788 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800789
790 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800791 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800792 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100793 return -EINVAL;
794
David Brownell58781012006-12-06 17:14:10 -0800795 bank = get_irq_chip_data(irq);
David Brownella6472532008-03-03 04:33:30 -0800796 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100797 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownellb9772a22006-12-06 17:13:53 -0800798 if (retval == 0) {
799 irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
800 irq_desc[irq].status |= type;
801 }
David Brownella6472532008-03-03 04:33:30 -0800802 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800803
804 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
805 __set_irq_handler_unlocked(irq, handle_level_irq);
806 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
807 __set_irq_handler_unlocked(irq, handle_edge_irq);
808
Tony Lindgren92105bb2005-09-07 17:20:26 +0100809 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100810}
811
812static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
813{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100814 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100815
816 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800817#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100818 case METHOD_MPUIO:
819 /* MPUIO irqstatus is reset by reading the status register,
820 * so do nothing here */
821 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800822#endif
823#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100824 case METHOD_GPIO_1510:
825 reg += OMAP1510_GPIO_INT_STATUS;
826 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800827#endif
828#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100829 case METHOD_GPIO_1610:
830 reg += OMAP1610_GPIO_IRQSTATUS1;
831 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800832#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100833#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100834 case METHOD_GPIO_7XX:
835 reg += OMAP7XX_GPIO_INT_STATUS;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100836 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800837#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530838#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100839 case METHOD_GPIO_24XX:
840 reg += OMAP24XX_GPIO_IRQSTATUS1;
841 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800842#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530843#if defined(CONFIG_ARCH_OMAP4)
844 case METHOD_GPIO_24XX:
845 reg += OMAP4_GPIO_IRQSTATUS0;
846 break;
847#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100848 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800849 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850 return;
851 }
852 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300853
854 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800855#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Roger Quadrosbedfd152009-04-23 11:10:50 -0700856 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530857#endif
858#if defined(CONFIG_ARCH_OMAP4)
859 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
860#endif
861 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Roger Quadrosbedfd152009-04-23 11:10:50 -0700862 __raw_writel(gpio_mask, reg);
863
864 /* Flush posted write for the irq status to avoid spurious interrupts */
865 __raw_readl(reg);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530866 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100867}
868
869static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
870{
871 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
872}
873
Imre Deakea6dedd2006-06-26 16:16:00 -0700874static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
875{
876 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700877 int inv = 0;
878 u32 l;
879 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700880
881 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800882#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700883 case METHOD_MPUIO:
884 reg += OMAP_MPUIO_GPIO_MASKIT;
Imre Deak99c47702006-06-26 16:16:07 -0700885 mask = 0xffff;
886 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700887 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800888#endif
889#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700890 case METHOD_GPIO_1510:
891 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700892 mask = 0xffff;
893 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700894 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800895#endif
896#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700897 case METHOD_GPIO_1610:
898 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700899 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700900 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800901#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100902#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100903 case METHOD_GPIO_7XX:
904 reg += OMAP7XX_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700905 mask = 0xffffffff;
906 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700907 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800908#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530909#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Imre Deakea6dedd2006-06-26 16:16:00 -0700910 case METHOD_GPIO_24XX:
911 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700912 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700913 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800914#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530915#if defined(CONFIG_ARCH_OMAP4)
916 case METHOD_GPIO_24XX:
917 reg += OMAP4_GPIO_IRQSTATUSSET0;
918 mask = 0xffffffff;
919 break;
920#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700921 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800922 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700923 return 0;
924 }
925
Imre Deak99c47702006-06-26 16:16:07 -0700926 l = __raw_readl(reg);
927 if (inv)
928 l = ~l;
929 l &= mask;
930 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700931}
932
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100933static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
934{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100935 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100936 u32 l;
937
938 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800939#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100940 case METHOD_MPUIO:
941 reg += OMAP_MPUIO_GPIO_MASKIT;
942 l = __raw_readl(reg);
943 if (enable)
944 l &= ~(gpio_mask);
945 else
946 l |= gpio_mask;
947 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800948#endif
949#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100950 case METHOD_GPIO_1510:
951 reg += OMAP1510_GPIO_INT_MASK;
952 l = __raw_readl(reg);
953 if (enable)
954 l &= ~(gpio_mask);
955 else
956 l |= gpio_mask;
957 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800958#endif
959#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100960 case METHOD_GPIO_1610:
961 if (enable)
962 reg += OMAP1610_GPIO_SET_IRQENABLE1;
963 else
964 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
965 l = gpio_mask;
966 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800967#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100968#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100969 case METHOD_GPIO_7XX:
970 reg += OMAP7XX_GPIO_INT_MASK;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100971 l = __raw_readl(reg);
972 if (enable)
973 l &= ~(gpio_mask);
974 else
975 l |= gpio_mask;
976 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800977#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530978#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100979 case METHOD_GPIO_24XX:
980 if (enable)
981 reg += OMAP24XX_GPIO_SETIRQENABLE1;
982 else
983 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
984 l = gpio_mask;
985 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800986#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530987#ifdef CONFIG_ARCH_OMAP4
988 case METHOD_GPIO_24XX:
989 if (enable)
990 reg += OMAP4_GPIO_IRQSTATUSSET0;
991 else
992 reg += OMAP4_GPIO_IRQSTATUSCLR0;
993 l = gpio_mask;
994 break;
995#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100996 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800997 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100998 return;
999 }
1000 __raw_writel(l, reg);
1001}
1002
1003static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
1004{
1005 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
1006}
1007
Tony Lindgren92105bb2005-09-07 17:20:26 +01001008/*
1009 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
1010 * 1510 does not seem to have a wake-up register. If JTAG is connected
1011 * to the target, system will wake up always on GPIO events. While
1012 * system is running all registered GPIO interrupts need to have wake-up
1013 * enabled. When system is suspended, only selected GPIO interrupts need
1014 * to have wake-up enabled.
1015 */
1016static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
1017{
David Brownella6472532008-03-03 04:33:30 -08001018 unsigned long flags;
1019
Tony Lindgren92105bb2005-09-07 17:20:26 +01001020 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001021#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -08001022 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +01001023 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -08001024 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001025 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001026 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001027 else
Tony Lindgren92105bb2005-09-07 17:20:26 +01001028 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -08001029 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001030 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001031#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001032#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1033 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001034 case METHOD_GPIO_24XX:
David Brownell11a78b72006-12-06 17:14:11 -08001035 if (bank->non_wakeup_gpios & (1 << gpio)) {
1036 printk(KERN_ERR "Unable to modify wakeup on "
1037 "non-wakeup GPIO%d\n",
1038 (bank - gpio_bank) * 32 + gpio);
1039 return -EINVAL;
1040 }
David Brownella6472532008-03-03 04:33:30 -08001041 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001042 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001043 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -07001044 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001045 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -08001046 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001047 return 0;
1048#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001049 default:
1050 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
1051 bank->method);
1052 return -EINVAL;
1053 }
1054}
1055
Tony Lindgren4196dd62006-09-25 12:41:38 +03001056static void _reset_gpio(struct gpio_bank *bank, int gpio)
1057{
1058 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
1059 _set_gpio_irqenable(bank, gpio, 0);
1060 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +01001061 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001062}
1063
Tony Lindgren92105bb2005-09-07 17:20:26 +01001064/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
1065static int gpio_wake_enable(unsigned int irq, unsigned int enable)
1066{
1067 unsigned int gpio = irq - IH_GPIO_BASE;
1068 struct gpio_bank *bank;
1069 int retval;
1070
1071 if (check_gpio(gpio) < 0)
1072 return -ENODEV;
David Brownell58781012006-12-06 17:14:10 -08001073 bank = get_irq_chip_data(irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001074 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001075
1076 return retval;
1077}
1078
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001079static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001080{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001081 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001082 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001083
David Brownella6472532008-03-03 04:33:30 -08001084 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001085
Tony Lindgren4196dd62006-09-25 12:41:38 +03001086 /* Set trigger to none. You need to enable the desired trigger with
1087 * request_irq() or set_irq_type().
1088 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001089 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001090
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001091#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001092 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001093 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001094
Tony Lindgren92105bb2005-09-07 17:20:26 +01001095 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001097 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001098 }
1099#endif
David Brownella6472532008-03-03 04:33:30 -08001100 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001101
1102 return 0;
1103}
1104
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001105static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001106{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001107 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001108 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001109
David Brownella6472532008-03-03 04:33:30 -08001110 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001111#ifdef CONFIG_ARCH_OMAP16XX
1112 if (bank->method == METHOD_GPIO_1610) {
1113 /* Disable wake-up during idle for dynamic tick */
1114 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001115 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001116 }
1117#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001118#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1119 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001120 if (bank->method == METHOD_GPIO_24XX) {
1121 /* Disable wake-up during idle for dynamic tick */
1122 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001123 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001124 }
1125#endif
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001126 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -08001127 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001128}
1129
1130/*
1131 * We need to unmask the GPIO bank interrupt as soon as possible to
1132 * avoid missing GPIO interrupts for other lines in the bank.
1133 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1134 * in the bank to avoid missing nested interrupts for a GPIO line.
1135 * If we wait to unmask individual GPIO lines in the bank after the
1136 * line's interrupt handler has been run, we may miss some nested
1137 * interrupts.
1138 */
Russell King10dd5ce2006-11-23 11:41:32 +00001139static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001141 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001142 u32 isr;
1143 unsigned int gpio_irq;
1144 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001145 u32 retrigger = 0;
1146 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001147
1148 desc->chip->ack(irq);
1149
Thomas Gleixner418ca1f2006-07-01 22:32:41 +01001150 bank = get_irq_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001151#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152 if (bank->method == METHOD_MPUIO)
1153 isr_reg = bank->base + OMAP_MPUIO_GPIO_INT;
David Brownelle5c56ed2006-12-06 17:13:59 -08001154#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001155#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001156 if (bank->method == METHOD_GPIO_1510)
1157 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1158#endif
1159#if defined(CONFIG_ARCH_OMAP16XX)
1160 if (bank->method == METHOD_GPIO_1610)
1161 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1162#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001163#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001164 if (bank->method == METHOD_GPIO_7XX)
1165 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301167#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001168 if (bank->method == METHOD_GPIO_24XX)
1169 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1170#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301171#if defined(CONFIG_ARCH_OMAP4)
1172 if (bank->method == METHOD_GPIO_24XX)
1173 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1174#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001175 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001176 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001177 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001178
Imre Deakea6dedd2006-06-26 16:16:00 -07001179 enabled = _get_gpio_irqbank_mask(bank);
1180 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001181
1182 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1183 isr &= 0x0000ffff;
1184
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001185 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001186 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001187 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001188
1189 /* clear edge sensitive interrupts before handler(s) are
1190 called so that we don't miss any interrupt occurred while
1191 executing them */
1192 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1193 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1194 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1195
1196 /* if there is only edge sensitive GPIO pin interrupts
1197 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001198 if (!level_mask && !unmasked) {
1199 unmasked = 1;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001200 desc->chip->unmask(irq);
Imre Deakea6dedd2006-06-26 16:16:00 -07001201 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001202
Imre Deakea6dedd2006-06-26 16:16:00 -07001203 isr |= retrigger;
1204 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001205 if (!isr)
1206 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001207
Tony Lindgren92105bb2005-09-07 17:20:26 +01001208 gpio_irq = bank->virtual_irq_start;
1209 for (; isr != 0; isr >>= 1, gpio_irq++) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001210 if (!(isr & 1))
1211 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001212
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001213 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001214 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001215 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001216 /* if bank has any level sensitive GPIO pin interrupt
1217 configured, we must unmask the bank interrupt only after
1218 handler(s) are executed in order to avoid spurious bank
1219 interrupt */
1220 if (!unmasked)
1221 desc->chip->unmask(irq);
1222
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001223}
1224
Tony Lindgren4196dd62006-09-25 12:41:38 +03001225static void gpio_irq_shutdown(unsigned int irq)
1226{
1227 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001228 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001229
1230 _reset_gpio(bank, gpio);
1231}
1232
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001233static void gpio_ack_irq(unsigned int irq)
1234{
1235 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001236 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001237
1238 _clear_gpio_irqstatus(bank, gpio);
1239}
1240
1241static void gpio_mask_irq(unsigned int irq)
1242{
1243 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001244 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001245
1246 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001247 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001248}
1249
1250static void gpio_unmask_irq(unsigned int irq)
1251{
1252 unsigned int gpio = irq - IH_GPIO_BASE;
David Brownell58781012006-12-06 17:14:10 -08001253 struct gpio_bank *bank = get_irq_chip_data(irq);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001254 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Kevin Hilman55b60192009-06-04 15:57:10 -07001255 struct irq_desc *desc = irq_to_desc(irq);
1256 u32 trigger = desc->status & IRQ_TYPE_SENSE_MASK;
1257
1258 if (trigger)
1259 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001260
1261 /* For level-triggered GPIOs, the clearing must be done after
1262 * the HW source is cleared, thus after the handler has run */
1263 if (bank->level_mask & irq_mask) {
1264 _set_gpio_irqenable(bank, gpio, 0);
1265 _clear_gpio_irqstatus(bank, gpio);
1266 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001267
Kevin Hilman4de8c752008-01-16 21:56:14 -08001268 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001269}
1270
David Brownelle5c56ed2006-12-06 17:13:59 -08001271static struct irq_chip gpio_irq_chip = {
1272 .name = "GPIO",
1273 .shutdown = gpio_irq_shutdown,
1274 .ack = gpio_ack_irq,
1275 .mask = gpio_mask_irq,
1276 .unmask = gpio_unmask_irq,
1277 .set_type = gpio_irq_type,
1278 .set_wake = gpio_wake_enable,
1279};
1280
1281/*---------------------------------------------------------------------*/
1282
1283#ifdef CONFIG_ARCH_OMAP1
1284
1285/* MPUIO uses the always-on 32k clock */
1286
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001287static void mpuio_ack_irq(unsigned int irq)
1288{
1289 /* The ISR is reset automatically, so do nothing here. */
1290}
1291
1292static void mpuio_mask_irq(unsigned int irq)
1293{
1294 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001295 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001296
1297 _set_gpio_irqenable(bank, gpio, 0);
1298}
1299
1300static void mpuio_unmask_irq(unsigned int irq)
1301{
1302 unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
David Brownell58781012006-12-06 17:14:10 -08001303 struct gpio_bank *bank = get_irq_chip_data(irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001304
1305 _set_gpio_irqenable(bank, gpio, 1);
1306}
1307
David Brownelle5c56ed2006-12-06 17:13:59 -08001308static struct irq_chip mpuio_irq_chip = {
1309 .name = "MPUIO",
1310 .ack = mpuio_ack_irq,
1311 .mask = mpuio_mask_irq,
1312 .unmask = mpuio_unmask_irq,
Tony Lindgren92105bb2005-09-07 17:20:26 +01001313 .set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001314#ifdef CONFIG_ARCH_OMAP16XX
1315 /* REVISIT: assuming only 16xx supports MPUIO wake events */
1316 .set_wake = gpio_wake_enable,
1317#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001318};
1319
David Brownelle5c56ed2006-12-06 17:13:59 -08001320
1321#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1322
David Brownell11a78b72006-12-06 17:14:11 -08001323
1324#ifdef CONFIG_ARCH_OMAP16XX
1325
1326#include <linux/platform_device.h>
1327
Magnus Damm79ee0312009-07-08 13:22:04 +02001328static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001329{
Magnus Damm79ee0312009-07-08 13:22:04 +02001330 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001331 struct gpio_bank *bank = platform_get_drvdata(pdev);
1332 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001333 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001334
David Brownella6472532008-03-03 04:33:30 -08001335 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001336 bank->saved_wakeup = __raw_readl(mask_reg);
1337 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001338 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001339
1340 return 0;
1341}
1342
Magnus Damm79ee0312009-07-08 13:22:04 +02001343static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001344{
Magnus Damm79ee0312009-07-08 13:22:04 +02001345 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001346 struct gpio_bank *bank = platform_get_drvdata(pdev);
1347 void __iomem *mask_reg = bank->base + OMAP_MPUIO_GPIO_MASKIT;
David Brownella6472532008-03-03 04:33:30 -08001348 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001349
David Brownella6472532008-03-03 04:33:30 -08001350 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001351 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001352 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001353
1354 return 0;
1355}
1356
Magnus Damm79ee0312009-07-08 13:22:04 +02001357static struct dev_pm_ops omap_mpuio_dev_pm_ops = {
1358 .suspend_noirq = omap_mpuio_suspend_noirq,
1359 .resume_noirq = omap_mpuio_resume_noirq,
1360};
1361
David Brownell11a78b72006-12-06 17:14:11 -08001362/* use platform_driver for this, now that there's no longer any
1363 * point to sys_device (other than not disturbing old code).
1364 */
1365static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001366 .driver = {
1367 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001368 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001369 },
1370};
1371
1372static struct platform_device omap_mpuio_device = {
1373 .name = "mpuio",
1374 .id = -1,
1375 .dev = {
1376 .driver = &omap_mpuio_driver.driver,
1377 }
1378 /* could list the /proc/iomem resources */
1379};
1380
1381static inline void mpuio_init(void)
1382{
David Brownellfcf126d2007-04-02 12:46:47 -07001383 platform_set_drvdata(&omap_mpuio_device, &gpio_bank_1610[0]);
1384
David Brownell11a78b72006-12-06 17:14:11 -08001385 if (platform_driver_register(&omap_mpuio_driver) == 0)
1386 (void) platform_device_register(&omap_mpuio_device);
1387}
1388
1389#else
1390static inline void mpuio_init(void) {}
1391#endif /* 16xx */
1392
David Brownelle5c56ed2006-12-06 17:13:59 -08001393#else
1394
1395extern struct irq_chip mpuio_irq_chip;
1396
1397#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001398static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001399
1400#endif
1401
1402/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001403
David Brownell52e31342008-03-03 12:43:23 -08001404/* REVISIT these are stupid implementations! replace by ones that
1405 * don't switch on METHOD_* and which mostly avoid spinlocks
1406 */
1407
1408static int gpio_input(struct gpio_chip *chip, unsigned offset)
1409{
1410 struct gpio_bank *bank;
1411 unsigned long flags;
1412
1413 bank = container_of(chip, struct gpio_bank, chip);
1414 spin_lock_irqsave(&bank->lock, flags);
1415 _set_gpio_direction(bank, offset, 1);
1416 spin_unlock_irqrestore(&bank->lock, flags);
1417 return 0;
1418}
1419
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001420static int gpio_is_input(struct gpio_bank *bank, int mask)
1421{
1422 void __iomem *reg = bank->base;
1423
1424 switch (bank->method) {
1425 case METHOD_MPUIO:
1426 reg += OMAP_MPUIO_IO_CNTL;
1427 break;
1428 case METHOD_GPIO_1510:
1429 reg += OMAP1510_GPIO_DIR_CONTROL;
1430 break;
1431 case METHOD_GPIO_1610:
1432 reg += OMAP1610_GPIO_DIRECTION;
1433 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001434 case METHOD_GPIO_7XX:
1435 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001436 break;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001437 case METHOD_GPIO_24XX:
1438 reg += OMAP24XX_GPIO_OE;
1439 break;
1440 }
1441 return __raw_readl(reg) & mask;
1442}
1443
David Brownell52e31342008-03-03 12:43:23 -08001444static int gpio_get(struct gpio_chip *chip, unsigned offset)
1445{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001446 struct gpio_bank *bank;
1447 void __iomem *reg;
1448 int gpio;
1449 u32 mask;
1450
1451 gpio = chip->base + offset;
1452 bank = get_gpio_bank(gpio);
1453 reg = bank->base;
1454 mask = 1 << get_gpio_index(gpio);
1455
1456 if (gpio_is_input(bank, mask))
1457 return _get_gpio_datain(bank, gpio);
1458 else
1459 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001460}
1461
1462static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1463{
1464 struct gpio_bank *bank;
1465 unsigned long flags;
1466
1467 bank = container_of(chip, struct gpio_bank, chip);
1468 spin_lock_irqsave(&bank->lock, flags);
1469 _set_gpio_dataout(bank, offset, value);
1470 _set_gpio_direction(bank, offset, 0);
1471 spin_unlock_irqrestore(&bank->lock, flags);
1472 return 0;
1473}
1474
1475static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1476{
1477 struct gpio_bank *bank;
1478 unsigned long flags;
1479
1480 bank = container_of(chip, struct gpio_bank, chip);
1481 spin_lock_irqsave(&bank->lock, flags);
1482 _set_gpio_dataout(bank, offset, value);
1483 spin_unlock_irqrestore(&bank->lock, flags);
1484}
1485
David Brownella007b702008-12-10 17:35:25 -08001486static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1487{
1488 struct gpio_bank *bank;
1489
1490 bank = container_of(chip, struct gpio_bank, chip);
1491 return bank->virtual_irq_start + offset;
1492}
1493
David Brownell52e31342008-03-03 12:43:23 -08001494/*---------------------------------------------------------------------*/
1495
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001496static int initialized;
Santosh Shilimkar44169072009-05-28 14:16:04 -07001497#if !(defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001498static struct clk * gpio_ick;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001499#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001500
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001501#if defined(CONFIG_ARCH_OMAP2)
1502static struct clk * gpio_fck;
1503#endif
1504
1505#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001506static struct clk * gpio5_ick;
1507static struct clk * gpio5_fck;
1508#endif
1509
Santosh Shilimkar44169072009-05-28 14:16:04 -07001510#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001511static struct clk *gpio_iclks[OMAP34XX_NR_GPIOS];
1512#endif
1513
David Brownell8ba55c52008-02-26 11:10:50 -08001514/* This lock class tells lockdep that GPIO irqs are in a different
1515 * category than their parents, so it won't report false recursion.
1516 */
1517static struct lock_class_key gpio_lock_class;
1518
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001519static int __init _omap_gpio_init(void)
1520{
1521 int i;
David Brownell52e31342008-03-03 12:43:23 -08001522 int gpio = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001523 struct gpio_bank *bank;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001524 char clk_name[11];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001525
1526 initialized = 1;
1527
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001528#if defined(CONFIG_ARCH_OMAP1)
Tony Lindgren6e60e792006-04-02 17:46:23 +01001529 if (cpu_is_omap15xx()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001530 gpio_ick = clk_get(NULL, "arm_gpio_ck");
1531 if (IS_ERR(gpio_ick))
Tony Lindgren92105bb2005-09-07 17:20:26 +01001532 printk("Could not get arm_gpio_ck\n");
1533 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001534 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001535 }
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001536#endif
1537#if defined(CONFIG_ARCH_OMAP2)
1538 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001539 gpio_ick = clk_get(NULL, "gpios_ick");
1540 if (IS_ERR(gpio_ick))
1541 printk("Could not get gpios_ick\n");
1542 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001543 clk_enable(gpio_ick);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001544 gpio_fck = clk_get(NULL, "gpios_fck");
Komal Shah1630b522006-09-25 12:51:08 +03001545 if (IS_ERR(gpio_fck))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001546 printk("Could not get gpios_fck\n");
1547 else
Tony Lindgren30ff7202006-01-17 15:33:51 -08001548 clk_enable(gpio_fck);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001549
1550 /*
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001551 * On 2430 & 3430 GPIO 5 uses CORE L4 ICLK
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001552 */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001553#if defined(CONFIG_ARCH_OMAP2430)
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001554 if (cpu_is_omap2430()) {
1555 gpio5_ick = clk_get(NULL, "gpio5_ick");
1556 if (IS_ERR(gpio5_ick))
1557 printk("Could not get gpio5_ick\n");
1558 else
1559 clk_enable(gpio5_ick);
1560 gpio5_fck = clk_get(NULL, "gpio5_fck");
1561 if (IS_ERR(gpio5_fck))
1562 printk("Could not get gpio5_fck\n");
1563 else
1564 clk_enable(gpio5_fck);
1565 }
1566#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001567 }
1568#endif
1569
Santosh Shilimkar44169072009-05-28 14:16:04 -07001570#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
1571 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001572 for (i = 0; i < OMAP34XX_NR_GPIOS; i++) {
1573 sprintf(clk_name, "gpio%d_ick", i + 1);
1574 gpio_iclks[i] = clk_get(NULL, clk_name);
1575 if (IS_ERR(gpio_iclks[i]))
1576 printk(KERN_ERR "Could not get %s\n", clk_name);
1577 else
1578 clk_enable(gpio_iclks[i]);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001579 }
1580 }
1581#endif
1582
Tony Lindgren92105bb2005-09-07 17:20:26 +01001583
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001584#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren6e60e792006-04-02 17:46:23 +01001585 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001586 printk(KERN_INFO "OMAP1510 GPIO hardware\n");
1587 gpio_bank_count = 2;
1588 gpio_bank = gpio_bank_1510;
1589 }
1590#endif
1591#if defined(CONFIG_ARCH_OMAP16XX)
1592 if (cpu_is_omap16xx()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001593 u32 rev;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001594
1595 gpio_bank_count = 5;
1596 gpio_bank = gpio_bank_1610;
Russell King7c7095a2008-09-05 15:49:14 +01001597 rev = __raw_readw(gpio_bank[1].base + OMAP1610_GPIO_REVISION);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001598 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1599 (rev >> 4) & 0x0f, rev & 0x0f);
1600 }
1601#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001602#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
1603 if (cpu_is_omap7xx()) {
1604 printk(KERN_INFO "OMAP7XX GPIO hardware\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001605 gpio_bank_count = 7;
Alistair Buxton7c006922009-09-22 10:02:58 +01001606 gpio_bank = gpio_bank_7xx;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001607 }
1608#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001609#ifdef CONFIG_ARCH_OMAP24XX
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001610 if (cpu_is_omap242x()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001611 int rev;
1612
1613 gpio_bank_count = 4;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001614 gpio_bank = gpio_bank_242x;
Russell King7c7095a2008-09-05 15:49:14 +01001615 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001616 printk(KERN_INFO "OMAP242x GPIO hardware version %d.%d\n",
1617 (rev >> 4) & 0x0f, rev & 0x0f);
1618 }
1619 if (cpu_is_omap243x()) {
1620 int rev;
1621
1622 gpio_bank_count = 5;
1623 gpio_bank = gpio_bank_243x;
Russell King7c7095a2008-09-05 15:49:14 +01001624 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001625 printk(KERN_INFO "OMAP243x GPIO hardware version %d.%d\n",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001626 (rev >> 4) & 0x0f, rev & 0x0f);
1627 }
1628#endif
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001629#ifdef CONFIG_ARCH_OMAP34XX
1630 if (cpu_is_omap34xx()) {
1631 int rev;
1632
1633 gpio_bank_count = OMAP34XX_NR_GPIOS;
1634 gpio_bank = gpio_bank_34xx;
Russell King7c7095a2008-09-05 15:49:14 +01001635 rev = __raw_readl(gpio_bank[0].base + OMAP24XX_GPIO_REVISION);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001636 printk(KERN_INFO "OMAP34xx GPIO hardware version %d.%d\n",
1637 (rev >> 4) & 0x0f, rev & 0x0f);
1638 }
1639#endif
Santosh Shilimkar44169072009-05-28 14:16:04 -07001640#ifdef CONFIG_ARCH_OMAP4
1641 if (cpu_is_omap44xx()) {
1642 int rev;
1643
1644 gpio_bank_count = OMAP34XX_NR_GPIOS;
1645 gpio_bank = gpio_bank_44xx;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301646 rev = __raw_readl(gpio_bank[0].base + OMAP4_GPIO_REVISION);
Santosh Shilimkar44169072009-05-28 14:16:04 -07001647 printk(KERN_INFO "OMAP44xx GPIO hardware version %d.%d\n",
1648 (rev >> 4) & 0x0f, rev & 0x0f);
1649 }
1650#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001651 for (i = 0; i < gpio_bank_count; i++) {
1652 int j, gpio_count = 16;
1653
1654 bank = &gpio_bank[i];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001655 spin_lock_init(&bank->lock);
David Brownelle5c56ed2006-12-06 17:13:59 -08001656 if (bank_is_mpuio(bank))
Russell King7c7095a2008-09-05 15:49:14 +01001657 __raw_writew(0xffff, bank->base + OMAP_MPUIO_GPIO_MASKIT);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001658 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001659 __raw_writew(0xffff, bank->base + OMAP1510_GPIO_INT_MASK);
1660 __raw_writew(0x0000, bank->base + OMAP1510_GPIO_INT_STATUS);
1661 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001662 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001663 __raw_writew(0x0000, bank->base + OMAP1610_GPIO_IRQENABLE1);
1664 __raw_writew(0xffff, bank->base + OMAP1610_GPIO_IRQSTATUS1);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001665 __raw_writew(0x0014, bank->base + OMAP1610_GPIO_SYSCONFIG);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001666 }
Alistair Buxton7c006922009-09-22 10:02:58 +01001667 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1668 __raw_writel(0xffffffff, bank->base + OMAP7XX_GPIO_INT_MASK);
1669 __raw_writel(0x00000000, bank->base + OMAP7XX_GPIO_INT_STATUS);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001670
Alistair Buxton7c006922009-09-22 10:02:58 +01001671 gpio_count = 32; /* 7xx has 32-bit GPIOs */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001672 }
Tony Lindgrend11ac972008-01-12 15:35:04 -08001673
Santosh Shilimkar44169072009-05-28 14:16:04 -07001674#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1675 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001676 if (bank->method == METHOD_GPIO_24XX) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001677 static const u32 non_wakeup_gpios[] = {
1678 0xe203ffc0, 0x08700040
1679 };
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301680 if (cpu_is_omap44xx()) {
1681 __raw_writel(0xffffffff, bank->base +
1682 OMAP4_GPIO_IRQSTATUSCLR0);
1683 __raw_writew(0x0015, bank->base +
1684 OMAP4_GPIO_SYSCONFIG);
1685 __raw_writel(0x00000000, bank->base +
1686 OMAP4_GPIO_DEBOUNCENABLE);
1687 /* Initialize interface clock ungated, module enabled */
1688 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1689 } else {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001690 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_IRQENABLE1);
1691 __raw_writel(0xffffffff, bank->base + OMAP24XX_GPIO_IRQSTATUS1);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001692 __raw_writew(0x0015, bank->base + OMAP24XX_GPIO_SYSCONFIG);
janboecb5793d2009-06-23 13:30:25 +03001693 __raw_writel(0x00000000, bank->base + OMAP24XX_GPIO_DEBOUNCE_EN);
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001694
1695 /* Initialize interface clock ungated, module enabled */
1696 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301697 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001698 if (i < ARRAY_SIZE(non_wakeup_gpios))
1699 bank->non_wakeup_gpios = non_wakeup_gpios[i];
Tony Lindgren92105bb2005-09-07 17:20:26 +01001700 gpio_count = 32;
1701 }
1702#endif
David Brownell52e31342008-03-03 12:43:23 -08001703 /* REVISIT eventually switch from OMAP-specific gpio structs
1704 * over to the generic ones
1705 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001706 bank->chip.request = omap_gpio_request;
1707 bank->chip.free = omap_gpio_free;
David Brownell52e31342008-03-03 12:43:23 -08001708 bank->chip.direction_input = gpio_input;
1709 bank->chip.get = gpio_get;
1710 bank->chip.direction_output = gpio_output;
1711 bank->chip.set = gpio_set;
David Brownella007b702008-12-10 17:35:25 -08001712 bank->chip.to_irq = gpio_2irq;
David Brownell52e31342008-03-03 12:43:23 -08001713 if (bank_is_mpuio(bank)) {
1714 bank->chip.label = "mpuio";
Russell King69114a42008-09-03 10:15:26 +01001715#ifdef CONFIG_ARCH_OMAP16XX
David Brownelld8f388d2008-07-25 01:46:07 -07001716 bank->chip.dev = &omap_mpuio_device.dev;
1717#endif
David Brownell52e31342008-03-03 12:43:23 -08001718 bank->chip.base = OMAP_MPUIO(0);
1719 } else {
1720 bank->chip.label = "gpio";
1721 bank->chip.base = gpio;
1722 gpio += gpio_count;
1723 }
1724 bank->chip.ngpio = gpio_count;
1725
1726 gpiochip_add(&bank->chip);
1727
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001728 for (j = bank->virtual_irq_start;
1729 j < bank->virtual_irq_start + gpio_count; j++) {
David Brownell8ba55c52008-02-26 11:10:50 -08001730 lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
David Brownell58781012006-12-06 17:14:10 -08001731 set_irq_chip_data(j, bank);
David Brownelle5c56ed2006-12-06 17:13:59 -08001732 if (bank_is_mpuio(bank))
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001733 set_irq_chip(j, &mpuio_irq_chip);
1734 else
1735 set_irq_chip(j, &gpio_irq_chip);
Russell King10dd5ce2006-11-23 11:41:32 +00001736 set_irq_handler(j, handle_simple_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001737 set_irq_flags(j, IRQF_VALID);
1738 }
1739 set_irq_chained_handler(bank->irq, gpio_irq_handler);
1740 set_irq_data(bank->irq, bank);
Jouni Hogander89db9482008-12-10 17:35:24 -08001741
Santosh Shilimkar44169072009-05-28 14:16:04 -07001742 if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
Jouni Hogander89db9482008-12-10 17:35:24 -08001743 sprintf(clk_name, "gpio%d_dbck", i + 1);
1744 bank->dbck = clk_get(NULL, clk_name);
1745 if (IS_ERR(bank->dbck))
1746 printk(KERN_ERR "Could not get %s\n", clk_name);
1747 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001748 }
1749
1750 /* Enable system clock for GPIO module.
1751 * The CAM_CLK_CTRL *is* really the right place. */
Tony Lindgren92105bb2005-09-07 17:20:26 +01001752 if (cpu_is_omap16xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001753 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04, ULPD_CAM_CLK_CTRL);
1754
Juha Yrjola14f1c3b2006-12-06 17:13:48 -08001755 /* Enable autoidle for the OCP interface */
1756 if (cpu_is_omap24xx())
1757 omap_writel(1 << 0, 0x48019010);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001758 if (cpu_is_omap34xx())
1759 omap_writel(1 << 0, 0x48306814);
Tony Lindgrend11ac972008-01-12 15:35:04 -08001760
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001761 return 0;
1762}
1763
Santosh Shilimkar44169072009-05-28 14:16:04 -07001764#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
1765 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001766static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1767{
1768 int i;
1769
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001770 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001771 return 0;
1772
1773 for (i = 0; i < gpio_bank_count; i++) {
1774 struct gpio_bank *bank = &gpio_bank[i];
1775 void __iomem *wake_status;
1776 void __iomem *wake_clear;
1777 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001778 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001779
1780 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001781#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001782 case METHOD_GPIO_1610:
1783 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1784 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1785 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1786 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001787#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301788#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001789 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001790 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001791 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1792 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1793 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001794#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301795#ifdef CONFIG_ARCH_OMAP4
1796 case METHOD_GPIO_24XX:
1797 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1798 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1799 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1800 break;
1801#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001802 default:
1803 continue;
1804 }
1805
David Brownella6472532008-03-03 04:33:30 -08001806 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001807 bank->saved_wakeup = __raw_readl(wake_status);
1808 __raw_writel(0xffffffff, wake_clear);
1809 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001810 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001811 }
1812
1813 return 0;
1814}
1815
1816static int omap_gpio_resume(struct sys_device *dev)
1817{
1818 int i;
1819
Tero Kristo723fdb72008-11-26 14:35:16 -08001820 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001821 return 0;
1822
1823 for (i = 0; i < gpio_bank_count; i++) {
1824 struct gpio_bank *bank = &gpio_bank[i];
1825 void __iomem *wake_clear;
1826 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001827 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001828
1829 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001830#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001831 case METHOD_GPIO_1610:
1832 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1833 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1834 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001835#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301836#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001837 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001838 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1839 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001840 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001841#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301842#ifdef CONFIG_ARCH_OMAP4
1843 case METHOD_GPIO_24XX:
1844 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1845 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1846 break;
1847#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001848 default:
1849 continue;
1850 }
1851
David Brownella6472532008-03-03 04:33:30 -08001852 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001853 __raw_writel(0xffffffff, wake_clear);
1854 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001855 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001856 }
1857
1858 return 0;
1859}
1860
1861static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001862 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001863 .suspend = omap_gpio_suspend,
1864 .resume = omap_gpio_resume,
1865};
1866
1867static struct sys_device omap_gpio_device = {
1868 .id = 0,
1869 .cls = &omap_gpio_sysclass,
1870};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001871
1872#endif
1873
Santosh Shilimkar44169072009-05-28 14:16:04 -07001874#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX) || \
1875 defined(CONFIG_ARCH_OMAP4)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001876
1877static int workaround_enabled;
1878
1879void omap2_gpio_prepare_for_retention(void)
1880{
1881 int i, c = 0;
1882
1883 /* Remove triggering for all non-wakeup GPIOs. Otherwise spurious
1884 * IRQs will be generated. See OMAP2420 Errata item 1.101. */
1885 for (i = 0; i < gpio_bank_count; i++) {
1886 struct gpio_bank *bank = &gpio_bank[i];
1887 u32 l1, l2;
1888
1889 if (!(bank->enabled_non_wakeup_gpios))
1890 continue;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301891#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001892 bank->saved_datain = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1893 l1 = __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1894 l2 = __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001895#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301896#ifdef CONFIG_ARCH_OMAP4
1897 bank->saved_datain = __raw_readl(bank->base +
1898 OMAP4_GPIO_DATAIN);
1899 l1 = __raw_readl(bank->base + OMAP4_GPIO_FALLINGDETECT);
1900 l2 = __raw_readl(bank->base + OMAP4_GPIO_RISINGDETECT);
1901#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001902 bank->saved_fallingdetect = l1;
1903 bank->saved_risingdetect = l2;
1904 l1 &= ~bank->enabled_non_wakeup_gpios;
1905 l2 &= ~bank->enabled_non_wakeup_gpios;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301906#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001907 __raw_writel(l1, bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1908 __raw_writel(l2, bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001909#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301910#ifdef CONFIG_ARCH_OMAP4
1911 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1912 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1913#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001914 c++;
1915 }
1916 if (!c) {
1917 workaround_enabled = 0;
1918 return;
1919 }
1920 workaround_enabled = 1;
1921}
1922
1923void omap2_gpio_resume_after_retention(void)
1924{
1925 int i;
1926
1927 if (!workaround_enabled)
1928 return;
1929 for (i = 0; i < gpio_bank_count; i++) {
1930 struct gpio_bank *bank = &gpio_bank[i];
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001931 u32 l, gen, gen0, gen1;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001932
1933 if (!(bank->enabled_non_wakeup_gpios))
1934 continue;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301935#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001936 __raw_writel(bank->saved_fallingdetect,
1937 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1938 __raw_writel(bank->saved_risingdetect,
1939 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301940 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1941#endif
1942#ifdef CONFIG_ARCH_OMAP4
1943 __raw_writel(bank->saved_fallingdetect,
1944 bank->base + OMAP4_GPIO_FALLINGDETECT);
1945 __raw_writel(bank->saved_risingdetect,
1946 bank->base + OMAP4_GPIO_RISINGDETECT);
1947 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001948#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001949 /* Check if any of the non-wakeup interrupt GPIOs have changed
1950 * state. If so, generate an IRQ by software. This is
1951 * horribly racy, but it's the best we can do to work around
1952 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001953 l ^= bank->saved_datain;
1954 l &= bank->non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001955
1956 /*
1957 * No need to generate IRQs for the rising edge for gpio IRQs
1958 * configured with falling edge only; and vice versa.
1959 */
1960 gen0 = l & bank->saved_fallingdetect;
1961 gen0 &= bank->saved_datain;
1962
1963 gen1 = l & bank->saved_risingdetect;
1964 gen1 &= ~(bank->saved_datain);
1965
1966 /* FIXME: Consider GPIO IRQs with level detections properly! */
1967 gen = l & (~(bank->saved_fallingdetect) &
1968 ~(bank->saved_risingdetect));
1969 /* Consider all GPIO IRQs needed to be updated */
1970 gen |= gen0 | gen1;
1971
1972 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001973 u32 old0, old1;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301974#if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001975 old0 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1976 old1 = __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001977 __raw_writel(old0 | gen, bank->base +
1978 OMAP24XX_GPIO_LEVELDETECT0);
1979 __raw_writel(old1 | gen, bank->base +
1980 OMAP24XX_GPIO_LEVELDETECT1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001981 __raw_writel(old0, bank->base + OMAP24XX_GPIO_LEVELDETECT0);
1982 __raw_writel(old1, bank->base + OMAP24XX_GPIO_LEVELDETECT1);
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001983#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301984#ifdef CONFIG_ARCH_OMAP4
1985 old0 = __raw_readl(bank->base +
1986 OMAP4_GPIO_LEVELDETECT0);
1987 old1 = __raw_readl(bank->base +
1988 OMAP4_GPIO_LEVELDETECT1);
1989 __raw_writel(old0 | l, bank->base +
1990 OMAP4_GPIO_LEVELDETECT0);
1991 __raw_writel(old1 | l, bank->base +
1992 OMAP4_GPIO_LEVELDETECT1);
1993 __raw_writel(old0, bank->base +
1994 OMAP4_GPIO_LEVELDETECT0);
1995 __raw_writel(old1, bank->base +
1996 OMAP4_GPIO_LEVELDETECT1);
1997#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001998 }
1999 }
2000
2001}
2002
Tony Lindgren92105bb2005-09-07 17:20:26 +01002003#endif
2004
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002005/*
2006 * This may get called early from board specific init
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002007 * for boards that have interrupts routed via FPGA.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002008 */
David Brownell277d58e2006-12-06 17:13:59 -08002009int __init omap_gpio_init(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002010{
2011 if (!initialized)
2012 return _omap_gpio_init();
2013 else
2014 return 0;
2015}
2016
Tony Lindgren92105bb2005-09-07 17:20:26 +01002017static int __init omap_gpio_sysinit(void)
2018{
2019 int ret = 0;
2020
2021 if (!initialized)
2022 ret = _omap_gpio_init();
2023
David Brownell11a78b72006-12-06 17:14:11 -08002024 mpuio_init();
2025
Santosh Shilimkar44169072009-05-28 14:16:04 -07002026#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
2027 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08002028 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01002029 if (ret == 0) {
2030 ret = sysdev_class_register(&omap_gpio_sysclass);
2031 if (ret == 0)
2032 ret = sysdev_register(&omap_gpio_device);
2033 }
2034 }
2035#endif
2036
2037 return ret;
2038}
2039
Tony Lindgren92105bb2005-09-07 17:20:26 +01002040arch_initcall(omap_gpio_sysinit);
David Brownellb9772a22006-12-06 17:13:53 -08002041
2042
2043#ifdef CONFIG_DEBUG_FS
2044
2045#include <linux/debugfs.h>
2046#include <linux/seq_file.h>
2047
David Brownellb9772a22006-12-06 17:13:53 -08002048static int dbg_gpio_show(struct seq_file *s, void *unused)
2049{
2050 unsigned i, j, gpio;
2051
2052 for (i = 0, gpio = 0; i < gpio_bank_count; i++) {
2053 struct gpio_bank *bank = gpio_bank + i;
2054 unsigned bankwidth = 16;
2055 u32 mask = 1;
2056
David Brownelle5c56ed2006-12-06 17:13:59 -08002057 if (bank_is_mpuio(bank))
David Brownellb9772a22006-12-06 17:13:53 -08002058 gpio = OMAP_MPUIO(0);
Alistair Buxtonb718aa82009-09-23 18:56:19 +01002059 else if (cpu_class_is_omap2() || cpu_is_omap7xx())
David Brownellb9772a22006-12-06 17:13:53 -08002060 bankwidth = 32;
2061
2062 for (j = 0; j < bankwidth; j++, gpio++, mask <<= 1) {
2063 unsigned irq, value, is_in, irqstat;
David Brownell52e31342008-03-03 12:43:23 -08002064 const char *label;
David Brownellb9772a22006-12-06 17:13:53 -08002065
David Brownell52e31342008-03-03 12:43:23 -08002066 label = gpiochip_is_requested(&bank->chip, j);
2067 if (!label)
David Brownellb9772a22006-12-06 17:13:53 -08002068 continue;
2069
2070 irq = bank->virtual_irq_start + j;
David Brownell0b84b5c2008-12-10 17:35:25 -08002071 value = gpio_get_value(gpio);
David Brownellb9772a22006-12-06 17:13:53 -08002072 is_in = gpio_is_input(bank, mask);
2073
David Brownelle5c56ed2006-12-06 17:13:59 -08002074 if (bank_is_mpuio(bank))
David Brownell52e31342008-03-03 12:43:23 -08002075 seq_printf(s, "MPUIO %2d ", j);
David Brownellb9772a22006-12-06 17:13:53 -08002076 else
David Brownell52e31342008-03-03 12:43:23 -08002077 seq_printf(s, "GPIO %3d ", gpio);
Jarkko Nikula21c867f2008-12-10 17:35:24 -08002078 seq_printf(s, "(%-20.20s): %s %s",
David Brownell52e31342008-03-03 12:43:23 -08002079 label,
David Brownellb9772a22006-12-06 17:13:53 -08002080 is_in ? "in " : "out",
2081 value ? "hi" : "lo");
2082
David Brownell52e31342008-03-03 12:43:23 -08002083/* FIXME for at least omap2, show pullup/pulldown state */
2084
David Brownellb9772a22006-12-06 17:13:53 -08002085 irqstat = irq_desc[irq].status;
Tony Lindgren3a26e332009-01-15 13:09:53 +02002086#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP24XX) || \
Santosh Shilimkar44169072009-05-28 14:16:04 -07002087 defined(CONFIG_ARCH_OMAP34XX) || defined(CONFIG_ARCH_OMAP4)
David Brownellb9772a22006-12-06 17:13:53 -08002088 if (is_in && ((bank->suspend_wakeup & mask)
2089 || irqstat & IRQ_TYPE_SENSE_MASK)) {
2090 char *trigger = NULL;
2091
2092 switch (irqstat & IRQ_TYPE_SENSE_MASK) {
2093 case IRQ_TYPE_EDGE_FALLING:
2094 trigger = "falling";
2095 break;
2096 case IRQ_TYPE_EDGE_RISING:
2097 trigger = "rising";
2098 break;
2099 case IRQ_TYPE_EDGE_BOTH:
2100 trigger = "bothedge";
2101 break;
2102 case IRQ_TYPE_LEVEL_LOW:
2103 trigger = "low";
2104 break;
2105 case IRQ_TYPE_LEVEL_HIGH:
2106 trigger = "high";
2107 break;
2108 case IRQ_TYPE_NONE:
David Brownell52e31342008-03-03 12:43:23 -08002109 trigger = "(?)";
David Brownellb9772a22006-12-06 17:13:53 -08002110 break;
2111 }
David Brownell52e31342008-03-03 12:43:23 -08002112 seq_printf(s, ", irq-%d %-8s%s",
David Brownellb9772a22006-12-06 17:13:53 -08002113 irq, trigger,
2114 (bank->suspend_wakeup & mask)
2115 ? " wakeup" : "");
2116 }
Tony Lindgren3a26e332009-01-15 13:09:53 +02002117#endif
David Brownellb9772a22006-12-06 17:13:53 -08002118 seq_printf(s, "\n");
2119 }
2120
David Brownelle5c56ed2006-12-06 17:13:59 -08002121 if (bank_is_mpuio(bank)) {
David Brownellb9772a22006-12-06 17:13:53 -08002122 seq_printf(s, "\n");
2123 gpio = 0;
2124 }
2125 }
2126 return 0;
2127}
2128
2129static int dbg_gpio_open(struct inode *inode, struct file *file)
2130{
David Brownelle5c56ed2006-12-06 17:13:59 -08002131 return single_open(file, dbg_gpio_show, &inode->i_private);
David Brownellb9772a22006-12-06 17:13:53 -08002132}
2133
2134static const struct file_operations debug_fops = {
2135 .open = dbg_gpio_open,
2136 .read = seq_read,
2137 .llseek = seq_lseek,
2138 .release = single_release,
2139};
2140
2141static int __init omap_gpio_debuginit(void)
2142{
David Brownelle5c56ed2006-12-06 17:13:59 -08002143 (void) debugfs_create_file("omap_gpio", S_IRUGO,
2144 NULL, NULL, &debug_fops);
David Brownellb9772a22006-12-06 17:13:53 -08002145 return 0;
2146}
2147late_initcall(omap_gpio_debuginit);
2148#endif