blob: d2adcdda23cf41c351fe418e31097fe2e4773df3 [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>
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080024#include <linux/slab.h>
25#include <linux/pm_runtime.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010028#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/irqs.h>
30#include <mach/gpio.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031#include <asm/mach/irq.h>
32
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033/*
34 * OMAP1510 GPIO registers
35 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036#define OMAP1510_GPIO_DATA_INPUT 0x00
37#define OMAP1510_GPIO_DATA_OUTPUT 0x04
38#define OMAP1510_GPIO_DIR_CONTROL 0x08
39#define OMAP1510_GPIO_INT_CONTROL 0x0c
40#define OMAP1510_GPIO_INT_MASK 0x10
41#define OMAP1510_GPIO_INT_STATUS 0x14
42#define OMAP1510_GPIO_PIN_CONTROL 0x18
43
44#define OMAP1510_IH_GPIO_BASE 64
45
46/*
47 * OMAP1610 specific GPIO registers
48 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049#define OMAP1610_GPIO_REVISION 0x0000
50#define OMAP1610_GPIO_SYSCONFIG 0x0010
51#define OMAP1610_GPIO_SYSSTATUS 0x0014
52#define OMAP1610_GPIO_IRQSTATUS1 0x0018
53#define OMAP1610_GPIO_IRQENABLE1 0x001c
Tony Lindgren92105bb2005-09-07 17:20:26 +010054#define OMAP1610_GPIO_WAKEUPENABLE 0x0028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010055#define OMAP1610_GPIO_DATAIN 0x002c
56#define OMAP1610_GPIO_DATAOUT 0x0030
57#define OMAP1610_GPIO_DIRECTION 0x0034
58#define OMAP1610_GPIO_EDGE_CTRL1 0x0038
59#define OMAP1610_GPIO_EDGE_CTRL2 0x003c
60#define OMAP1610_GPIO_CLEAR_IRQENABLE1 0x009c
Tony Lindgren92105bb2005-09-07 17:20:26 +010061#define OMAP1610_GPIO_CLEAR_WAKEUPENA 0x00a8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062#define OMAP1610_GPIO_CLEAR_DATAOUT 0x00b0
63#define OMAP1610_GPIO_SET_IRQENABLE1 0x00dc
Tony Lindgren92105bb2005-09-07 17:20:26 +010064#define OMAP1610_GPIO_SET_WAKEUPENA 0x00e8
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065#define OMAP1610_GPIO_SET_DATAOUT 0x00f0
66
67/*
Alistair Buxton7c006922009-09-22 10:02:58 +010068 * OMAP7XX specific GPIO registers
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010069 */
Alistair Buxton7c006922009-09-22 10:02:58 +010070#define OMAP7XX_GPIO_DATA_INPUT 0x00
71#define OMAP7XX_GPIO_DATA_OUTPUT 0x04
72#define OMAP7XX_GPIO_DIR_CONTROL 0x08
73#define OMAP7XX_GPIO_INT_CONTROL 0x0c
74#define OMAP7XX_GPIO_INT_MASK 0x10
75#define OMAP7XX_GPIO_INT_STATUS 0x14
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010076
Zebediah C. McClure56739a62009-03-23 18:07:40 -070077/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -080078 * omap2+ specific GPIO registers
Tony Lindgren92105bb2005-09-07 17:20:26 +010079 */
Tony Lindgren92105bb2005-09-07 17:20:26 +010080#define OMAP24XX_GPIO_REVISION 0x0000
Tony Lindgren92105bb2005-09-07 17:20:26 +010081#define OMAP24XX_GPIO_IRQSTATUS1 0x0018
Hiroshi DOYUbee79302006-09-25 12:41:46 +030082#define OMAP24XX_GPIO_IRQSTATUS2 0x0028
83#define OMAP24XX_GPIO_IRQENABLE2 0x002c
Tony Lindgren92105bb2005-09-07 17:20:26 +010084#define OMAP24XX_GPIO_IRQENABLE1 0x001c
Tero Kristo723fdb72008-11-26 14:35:16 -080085#define OMAP24XX_GPIO_WAKE_EN 0x0020
Tony Lindgren92105bb2005-09-07 17:20:26 +010086#define OMAP24XX_GPIO_CTRL 0x0030
87#define OMAP24XX_GPIO_OE 0x0034
88#define OMAP24XX_GPIO_DATAIN 0x0038
89#define OMAP24XX_GPIO_DATAOUT 0x003c
90#define OMAP24XX_GPIO_LEVELDETECT0 0x0040
91#define OMAP24XX_GPIO_LEVELDETECT1 0x0044
92#define OMAP24XX_GPIO_RISINGDETECT 0x0048
93#define OMAP24XX_GPIO_FALLINGDETECT 0x004c
Kevin Hilman5eb3bb92007-05-05 11:40:29 -070094#define OMAP24XX_GPIO_DEBOUNCE_EN 0x0050
95#define OMAP24XX_GPIO_DEBOUNCE_VAL 0x0054
Tony Lindgren92105bb2005-09-07 17:20:26 +010096#define OMAP24XX_GPIO_CLEARIRQENABLE1 0x0060
97#define OMAP24XX_GPIO_SETIRQENABLE1 0x0064
98#define OMAP24XX_GPIO_CLEARWKUENA 0x0080
99#define OMAP24XX_GPIO_SETWKUENA 0x0084
100#define OMAP24XX_GPIO_CLEARDATAOUT 0x0090
101#define OMAP24XX_GPIO_SETDATAOUT 0x0094
102
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530103#define OMAP4_GPIO_REVISION 0x0000
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530104#define OMAP4_GPIO_EOI 0x0020
105#define OMAP4_GPIO_IRQSTATUSRAW0 0x0024
106#define OMAP4_GPIO_IRQSTATUSRAW1 0x0028
107#define OMAP4_GPIO_IRQSTATUS0 0x002c
108#define OMAP4_GPIO_IRQSTATUS1 0x0030
109#define OMAP4_GPIO_IRQSTATUSSET0 0x0034
110#define OMAP4_GPIO_IRQSTATUSSET1 0x0038
111#define OMAP4_GPIO_IRQSTATUSCLR0 0x003c
112#define OMAP4_GPIO_IRQSTATUSCLR1 0x0040
113#define OMAP4_GPIO_IRQWAKEN0 0x0044
114#define OMAP4_GPIO_IRQWAKEN1 0x0048
Charulatha V9f096862010-05-14 12:05:27 -0700115#define OMAP4_GPIO_IRQENABLE1 0x011c
116#define OMAP4_GPIO_WAKE_EN 0x0120
117#define OMAP4_GPIO_IRQSTATUS2 0x0128
118#define OMAP4_GPIO_IRQENABLE2 0x012c
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530119#define OMAP4_GPIO_CTRL 0x0130
120#define OMAP4_GPIO_OE 0x0134
121#define OMAP4_GPIO_DATAIN 0x0138
122#define OMAP4_GPIO_DATAOUT 0x013c
123#define OMAP4_GPIO_LEVELDETECT0 0x0140
124#define OMAP4_GPIO_LEVELDETECT1 0x0144
125#define OMAP4_GPIO_RISINGDETECT 0x0148
126#define OMAP4_GPIO_FALLINGDETECT 0x014c
127#define OMAP4_GPIO_DEBOUNCENABLE 0x0150
128#define OMAP4_GPIO_DEBOUNCINGTIME 0x0154
Charulatha V9f096862010-05-14 12:05:27 -0700129#define OMAP4_GPIO_CLEARIRQENABLE1 0x0160
130#define OMAP4_GPIO_SETIRQENABLE1 0x0164
131#define OMAP4_GPIO_CLEARWKUENA 0x0180
132#define OMAP4_GPIO_SETWKUENA 0x0184
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530133#define OMAP4_GPIO_CLEARDATAOUT 0x0190
134#define OMAP4_GPIO_SETDATAOUT 0x0194
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800135
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136struct gpio_bank {
Tony Lindgren9f7065d2009-10-19 15:25:20 -0700137 unsigned long pbase;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100138 void __iomem *base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139 u16 irq;
140 u16 virtual_irq_start;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100141 int method;
Tony Lindgren140455f2010-02-12 12:26:48 -0800142#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100143 u32 suspend_wakeup;
144 u32 saved_wakeup;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800145#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800146 u32 non_wakeup_gpios;
147 u32 enabled_non_wakeup_gpios;
148
149 u32 saved_datain;
150 u32 saved_fallingdetect;
151 u32 saved_risingdetect;
Kevin Hilmanb144ff62008-01-16 21:56:15 -0800152 u32 level_mask;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800153 u32 toggle_mask;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154 spinlock_t lock;
David Brownell52e31342008-03-03 12:43:23 -0800155 struct gpio_chip chip;
Jouni Hogander89db9482008-12-10 17:35:24 -0800156 struct clk *dbck;
Charulatha V058af1e2009-11-22 10:11:25 -0800157 u32 mod_usage;
Kevin Hilman8865b9b2009-01-27 11:15:34 -0800158 u32 dbck_enable_mask;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800159 struct device *dev;
160 bool dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -0800161 int stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100162};
163
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800164#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +0530165struct omap3_gpio_regs {
Rajendra Nayak40c670f2008-09-26 17:47:48 +0530166 u32 irqenable1;
167 u32 irqenable2;
168 u32 wake_en;
169 u32 ctrl;
170 u32 oe;
171 u32 leveldetect0;
172 u32 leveldetect1;
173 u32 risingdetect;
174 u32 fallingdetect;
175 u32 dataout;
Rajendra Nayak40c670f2008-09-26 17:47:48 +0530176};
177
178static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800179#endif
180
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800181/*
182 * TODO: Cleanup gpio_bank usage as it is having information
183 * related to all instances of the device
184 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100185static struct gpio_bank *gpio_bank;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800186
187static int bank_width;
188
Varadarajan, Charulathac95d10b2010-12-07 16:26:56 -0800189/* TODO: Analyze removing gpio_bank_count usage from driver code */
190int gpio_bank_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100191
192static inline struct gpio_bank *get_gpio_bank(int gpio)
193{
Tony Lindgren6e60e792006-04-02 17:46:23 +0100194 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100195 if (OMAP_GPIO_IS_MPUIO(gpio))
196 return &gpio_bank[0];
197 return &gpio_bank[1];
198 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100199 if (cpu_is_omap16xx()) {
200 if (OMAP_GPIO_IS_MPUIO(gpio))
201 return &gpio_bank[0];
202 return &gpio_bank[1 + (gpio >> 4)];
203 }
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700204 if (cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100205 if (OMAP_GPIO_IS_MPUIO(gpio))
206 return &gpio_bank[0];
207 return &gpio_bank[1 + (gpio >> 5)];
208 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100209 if (cpu_is_omap24xx())
210 return &gpio_bank[gpio >> 5];
Santosh Shilimkar44169072009-05-28 14:16:04 -0700211 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800212 return &gpio_bank[gpio >> 5];
David Brownelle031ab22008-12-10 17:35:27 -0800213 BUG();
214 return NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100215}
216
217static inline int get_gpio_index(int gpio)
218{
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700219 if (cpu_is_omap7xx())
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100220 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100221 if (cpu_is_omap24xx())
222 return gpio & 0x1f;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700223 if (cpu_is_omap34xx() || cpu_is_omap44xx())
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800224 return gpio & 0x1f;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100225 return gpio & 0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100226}
227
228static inline int gpio_valid(int gpio)
229{
230 if (gpio < 0)
231 return -1;
Tony Lindgrend11ac972008-01-12 15:35:04 -0800232 if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
Jonathan McDowell193e68b2006-09-25 12:41:30 +0300233 if (gpio >= OMAP_MAX_GPIO_LINES + 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234 return -1;
235 return 0;
236 }
Tony Lindgren6e60e792006-04-02 17:46:23 +0100237 if (cpu_is_omap15xx() && gpio < 16)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100238 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100239 if ((cpu_is_omap16xx()) && gpio < 64)
240 return 0;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700241 if (cpu_is_omap7xx() && gpio < 192)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242 return 0;
Tony Lindgren25d6f632010-08-02 14:21:39 +0300243 if (cpu_is_omap2420() && gpio < 128)
244 return 0;
245 if (cpu_is_omap2430() && gpio < 160)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100246 return 0;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700247 if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800248 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100249 return -1;
250}
251
252static int check_gpio(int gpio)
253{
Roel Kluind32b20f2009-11-17 14:39:03 -0800254 if (unlikely(gpio_valid(gpio) < 0)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100255 printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
256 dump_stack();
257 return -1;
258 }
259 return 0;
260}
261
262static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
263{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100264 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100265 u32 l;
266
267 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800268#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100269 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800270 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100271 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800272#endif
273#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100274 case METHOD_GPIO_1510:
275 reg += OMAP1510_GPIO_DIR_CONTROL;
276 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800277#endif
278#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100279 case METHOD_GPIO_1610:
280 reg += OMAP1610_GPIO_DIRECTION;
281 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800282#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100283#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100284 case METHOD_GPIO_7XX:
285 reg += OMAP7XX_GPIO_DIR_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700286 break;
287#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800288#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100289 case METHOD_GPIO_24XX:
290 reg += OMAP24XX_GPIO_OE;
291 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800292#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530293#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800294 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530295 reg += OMAP4_GPIO_OE;
296 break;
297#endif
David Brownelle5c56ed2006-12-06 17:13:59 -0800298 default:
299 WARN_ON(1);
300 return;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100301 }
302 l = __raw_readl(reg);
303 if (is_input)
304 l |= 1 << gpio;
305 else
306 l &= ~(1 << gpio);
307 __raw_writel(l, reg);
308}
309
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100310static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
311{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100312 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313 u32 l = 0;
314
315 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800316#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800318 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100319 l = __raw_readl(reg);
320 if (enable)
321 l |= 1 << gpio;
322 else
323 l &= ~(1 << gpio);
324 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800325#endif
326#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327 case METHOD_GPIO_1510:
328 reg += OMAP1510_GPIO_DATA_OUTPUT;
329 l = __raw_readl(reg);
330 if (enable)
331 l |= 1 << gpio;
332 else
333 l &= ~(1 << gpio);
334 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800335#endif
336#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100337 case METHOD_GPIO_1610:
338 if (enable)
339 reg += OMAP1610_GPIO_SET_DATAOUT;
340 else
341 reg += OMAP1610_GPIO_CLEAR_DATAOUT;
342 l = 1 << gpio;
343 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800344#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100345#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100346 case METHOD_GPIO_7XX:
347 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700348 l = __raw_readl(reg);
349 if (enable)
350 l |= 1 << gpio;
351 else
352 l &= ~(1 << gpio);
353 break;
354#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800355#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100356 case METHOD_GPIO_24XX:
357 if (enable)
358 reg += OMAP24XX_GPIO_SETDATAOUT;
359 else
360 reg += OMAP24XX_GPIO_CLEARDATAOUT;
361 l = 1 << gpio;
362 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800363#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530364#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800365 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530366 if (enable)
367 reg += OMAP4_GPIO_SETDATAOUT;
368 else
369 reg += OMAP4_GPIO_CLEARDATAOUT;
370 l = 1 << gpio;
371 break;
372#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800374 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100375 return;
376 }
377 __raw_writel(l, reg);
378}
379
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300380static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100381{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100382 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383
384 if (check_gpio(gpio) < 0)
David Brownelle5c56ed2006-12-06 17:13:59 -0800385 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386 reg = bank->base;
387 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800388#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800390 reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100391 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800392#endif
393#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394 case METHOD_GPIO_1510:
395 reg += OMAP1510_GPIO_DATA_INPUT;
396 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800397#endif
398#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100399 case METHOD_GPIO_1610:
400 reg += OMAP1610_GPIO_DATAIN;
401 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800402#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100403#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100404 case METHOD_GPIO_7XX:
405 reg += OMAP7XX_GPIO_DATA_INPUT;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700406 break;
407#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800408#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100409 case METHOD_GPIO_24XX:
410 reg += OMAP24XX_GPIO_DATAIN;
411 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800412#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530413#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800414 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530415 reg += OMAP4_GPIO_DATAIN;
416 break;
417#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100418 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800419 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100420 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100421 return (__raw_readl(reg)
422 & (1 << get_gpio_index(gpio))) != 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100423}
424
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300425static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
426{
427 void __iomem *reg;
428
429 if (check_gpio(gpio) < 0)
430 return -EINVAL;
431 reg = bank->base;
432
433 switch (bank->method) {
434#ifdef CONFIG_ARCH_OMAP1
435 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800436 reg += OMAP_MPUIO_OUTPUT / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300437 break;
438#endif
439#ifdef CONFIG_ARCH_OMAP15XX
440 case METHOD_GPIO_1510:
441 reg += OMAP1510_GPIO_DATA_OUTPUT;
442 break;
443#endif
444#ifdef CONFIG_ARCH_OMAP16XX
445 case METHOD_GPIO_1610:
446 reg += OMAP1610_GPIO_DATAOUT;
447 break;
448#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100449#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100450 case METHOD_GPIO_7XX:
451 reg += OMAP7XX_GPIO_DATA_OUTPUT;
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300452 break;
453#endif
Charulatha V9f096862010-05-14 12:05:27 -0700454#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300455 case METHOD_GPIO_24XX:
456 reg += OMAP24XX_GPIO_DATAOUT;
457 break;
458#endif
Charulatha V9f096862010-05-14 12:05:27 -0700459#ifdef CONFIG_ARCH_OMAP4
460 case METHOD_GPIO_44XX:
461 reg += OMAP4_GPIO_DATAOUT;
462 break;
463#endif
Roger Quadrosb37c45b2009-08-05 16:53:24 +0300464 default:
465 return -EINVAL;
466 }
467
468 return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
469}
470
Tony Lindgren92105bb2005-09-07 17:20:26 +0100471#define MOD_REG_BIT(reg, bit_mask, set) \
472do { \
473 int l = __raw_readl(base + reg); \
474 if (set) l |= bit_mask; \
475 else l &= ~bit_mask; \
476 __raw_writel(l, base + reg); \
477} while(0)
478
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700479/**
480 * _set_gpio_debounce - low level gpio debounce time
481 * @bank: the gpio bank we're acting upon
482 * @gpio: the gpio number on this @gpio
483 * @debounce: debounce time to use
484 *
485 * OMAP's debounce time is in 31us steps so we need
486 * to convert and round up to the closest unit.
487 */
488static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
489 unsigned debounce)
490{
491 void __iomem *reg = bank->base;
492 u32 val;
493 u32 l;
494
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800495 if (!bank->dbck_flag)
496 return;
497
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700498 if (debounce < 32)
499 debounce = 0x01;
500 else if (debounce > 7936)
501 debounce = 0xff;
502 else
503 debounce = (debounce / 0x1f) - 1;
504
505 l = 1 << get_gpio_index(gpio);
506
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800507 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700508 reg += OMAP4_GPIO_DEBOUNCINGTIME;
509 else
510 reg += OMAP24XX_GPIO_DEBOUNCE_VAL;
511
512 __raw_writel(debounce, reg);
513
514 reg = bank->base;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800515 if (bank->method == METHOD_GPIO_44XX)
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700516 reg += OMAP4_GPIO_DEBOUNCENABLE;
517 else
518 reg += OMAP24XX_GPIO_DEBOUNCE_EN;
519
520 val = __raw_readl(reg);
521
522 if (debounce) {
523 val |= l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800524 clk_enable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700525 } else {
526 val &= ~l;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -0800527 clk_disable(bank->dbck);
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700528 }
Kevin Hilmanf7ec0b02010-06-09 13:53:07 +0300529 bank->dbck_enable_mask = val;
Felipe Balbi168ef3d2010-05-26 14:42:23 -0700530
531 __raw_writel(val, reg);
532}
533
Tony Lindgren140455f2010-02-12 12:26:48 -0800534#ifdef CONFIG_ARCH_OMAP2PLUS
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700535static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
536 int trigger)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537{
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800538 void __iomem *base = bank->base;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100539 u32 gpio_bit = 1 << gpio;
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530540 u32 val;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100541
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530542 if (cpu_is_omap44xx()) {
543 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
544 trigger & IRQ_TYPE_LEVEL_LOW);
545 MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
546 trigger & IRQ_TYPE_LEVEL_HIGH);
547 MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
548 trigger & IRQ_TYPE_EDGE_RISING);
549 MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
550 trigger & IRQ_TYPE_EDGE_FALLING);
551 } else {
552 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
553 trigger & IRQ_TYPE_LEVEL_LOW);
554 MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
555 trigger & IRQ_TYPE_LEVEL_HIGH);
556 MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
557 trigger & IRQ_TYPE_EDGE_RISING);
558 MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
559 trigger & IRQ_TYPE_EDGE_FALLING);
560 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800561 if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530562 if (cpu_is_omap44xx()) {
563 if (trigger != 0)
564 __raw_writel(1 << gpio, bank->base+
565 OMAP4_GPIO_IRQWAKEN0);
566 else {
567 val = __raw_readl(bank->base +
568 OMAP4_GPIO_IRQWAKEN0);
569 __raw_writel(val & (~(1 << gpio)), bank->base +
570 OMAP4_GPIO_IRQWAKEN0);
571 }
572 } else {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000573 /*
574 * GPIO wakeup request can only be generated on edge
575 * transitions
576 */
577 if (trigger & IRQ_TYPE_EDGE_BOTH)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530578 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700579 + OMAP24XX_GPIO_SETWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530580 else
581 __raw_writel(1 << gpio, bank->base
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700582 + OMAP24XX_GPIO_CLEARWKUENA);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530583 }
Tero Kristoa118b5f2008-12-22 14:27:12 +0200584 }
585 /* This part needs to be executed always for OMAP34xx */
586 if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
Chunqiu Wang699117a62009-06-24 17:13:39 +0000587 /*
588 * Log the edge gpio and manually trigger the IRQ
589 * after resume if the input level changes
590 * to avoid irq lost during PER RET/OFF mode
591 * Applies for omap2 non-wakeup gpio and all omap3 gpios
592 */
593 if (trigger & IRQ_TYPE_EDGE_BOTH)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800594 bank->enabled_non_wakeup_gpios |= gpio_bit;
595 else
596 bank->enabled_non_wakeup_gpios &= ~gpio_bit;
597 }
Kevin Hilman5eb3bb92007-05-05 11:40:29 -0700598
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530599 if (cpu_is_omap44xx()) {
600 bank->level_mask =
601 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
602 __raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
603 } else {
604 bank->level_mask =
605 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
606 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
607 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100608}
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800609#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +0100610
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800611#ifdef CONFIG_ARCH_OMAP1
Cory Maccarrone4318f362010-01-08 10:29:04 -0800612/*
613 * This only applies to chips that can't do both rising and falling edge
614 * detection at once. For all other chips, this function is a noop.
615 */
616static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
617{
618 void __iomem *reg = bank->base;
619 u32 l = 0;
620
621 switch (bank->method) {
Cory Maccarrone4318f362010-01-08 10:29:04 -0800622 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800623 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800624 break;
Cory Maccarrone4318f362010-01-08 10:29:04 -0800625#ifdef CONFIG_ARCH_OMAP15XX
626 case METHOD_GPIO_1510:
627 reg += OMAP1510_GPIO_INT_CONTROL;
628 break;
629#endif
630#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
631 case METHOD_GPIO_7XX:
632 reg += OMAP7XX_GPIO_INT_CONTROL;
633 break;
634#endif
635 default:
636 return;
637 }
638
639 l = __raw_readl(reg);
640 if ((l >> gpio) & 1)
641 l &= ~(1 << gpio);
642 else
643 l |= 1 << gpio;
644
645 __raw_writel(l, reg);
646}
Uwe Kleine-König9198bcd2010-01-29 14:20:05 -0800647#endif
Cory Maccarrone4318f362010-01-08 10:29:04 -0800648
Tony Lindgren92105bb2005-09-07 17:20:26 +0100649static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
650{
651 void __iomem *reg = bank->base;
652 u32 l = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653
654 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800655#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800657 reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100658 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000659 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800660 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100661 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100662 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100663 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100665 else
666 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800668#endif
669#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100670 case METHOD_GPIO_1510:
671 reg += OMAP1510_GPIO_INT_CONTROL;
672 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000673 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800674 bank->toggle_mask |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100675 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100676 l |= 1 << gpio;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100677 else if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100678 l &= ~(1 << gpio);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100679 else
680 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800682#endif
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800683#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100684 case METHOD_GPIO_1610:
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685 if (gpio & 0x08)
686 reg += OMAP1610_GPIO_EDGE_CTRL2;
687 else
688 reg += OMAP1610_GPIO_EDGE_CTRL1;
689 gpio &= 0x07;
690 l = __raw_readl(reg);
691 l &= ~(3 << (gpio << 1));
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100692 if (trigger & IRQ_TYPE_EDGE_RISING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100693 l |= 2 << (gpio << 1);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +0100694 if (trigger & IRQ_TYPE_EDGE_FALLING)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100695 l |= 1 << (gpio << 1);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800696 if (trigger)
697 /* Enable wake-up during idle for dynamic tick */
698 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
699 else
700 __raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701 break;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800702#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100703#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100704 case METHOD_GPIO_7XX:
705 reg += OMAP7XX_GPIO_INT_CONTROL;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700706 l = __raw_readl(reg);
Janusz Krzysztofik29501572010-04-05 11:38:06 +0000707 if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
Cory Maccarrone4318f362010-01-08 10:29:04 -0800708 bank->toggle_mask |= 1 << gpio;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700709 if (trigger & IRQ_TYPE_EDGE_RISING)
710 l |= 1 << gpio;
711 else if (trigger & IRQ_TYPE_EDGE_FALLING)
712 l &= ~(1 << gpio);
713 else
714 goto bad;
715 break;
716#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800717#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren92105bb2005-09-07 17:20:26 +0100718 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800719 case METHOD_GPIO_44XX:
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800720 set_24xx_gpio_triggering(bank, gpio, trigger);
Mika Westerbergf7c5cc42010-12-29 13:01:31 +0200721 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800722#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723 default:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100724 goto bad;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100725 }
Tony Lindgren92105bb2005-09-07 17:20:26 +0100726 __raw_writel(l, reg);
727 return 0;
728bad:
729 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730}
731
Lennert Buytenheke9191022010-11-29 11:17:17 +0100732static int gpio_irq_type(struct irq_data *d, unsigned type)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733{
734 struct gpio_bank *bank;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100735 unsigned gpio;
736 int retval;
David Brownella6472532008-03-03 04:33:30 -0800737 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +0100738
Lennert Buytenheke9191022010-11-29 11:17:17 +0100739 if (!cpu_class_is_omap2() && d->irq > IH_MPUIO_BASE)
740 gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100741 else
Lennert Buytenheke9191022010-11-29 11:17:17 +0100742 gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743
744 if (check_gpio(gpio) < 0)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100745 return -EINVAL;
746
David Brownelle5c56ed2006-12-06 17:13:59 -0800747 if (type & ~IRQ_TYPE_SENSE_MASK)
Tony Lindgren6e60e792006-04-02 17:46:23 +0100748 return -EINVAL;
David Brownelle5c56ed2006-12-06 17:13:59 -0800749
750 /* OMAP1 allows only only edge triggering */
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -0800751 if (!cpu_class_is_omap2()
David Brownelle5c56ed2006-12-06 17:13:59 -0800752 && (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
Tony Lindgren92105bb2005-09-07 17:20:26 +0100753 return -EINVAL;
754
Lennert Buytenheke9191022010-11-29 11:17:17 +0100755 bank = irq_data_get_irq_chip_data(d);
David Brownella6472532008-03-03 04:33:30 -0800756 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100757 retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
David Brownella6472532008-03-03 04:33:30 -0800758 spin_unlock_irqrestore(&bank->lock, flags);
Kevin Hilman672e3022008-01-16 21:56:16 -0800759
760 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100761 __irq_set_handler_locked(d->irq, handle_level_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800762 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100763 __irq_set_handler_locked(d->irq, handle_edge_irq);
Kevin Hilman672e3022008-01-16 21:56:16 -0800764
Tony Lindgren92105bb2005-09-07 17:20:26 +0100765 return retval;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766}
767
768static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
769{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100770 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771
772 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800773#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100774 case METHOD_MPUIO:
775 /* MPUIO irqstatus is reset by reading the status register,
776 * so do nothing here */
777 return;
David Brownelle5c56ed2006-12-06 17:13:59 -0800778#endif
779#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780 case METHOD_GPIO_1510:
781 reg += OMAP1510_GPIO_INT_STATUS;
782 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800783#endif
784#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100785 case METHOD_GPIO_1610:
786 reg += OMAP1610_GPIO_IRQSTATUS1;
787 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800788#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100789#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100790 case METHOD_GPIO_7XX:
791 reg += OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700792 break;
793#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800794#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100795 case METHOD_GPIO_24XX:
796 reg += OMAP24XX_GPIO_IRQSTATUS1;
797 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800798#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530799#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800800 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530801 reg += OMAP4_GPIO_IRQSTATUS0;
802 break;
803#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100804 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800805 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100806 return;
807 }
808 __raw_writel(gpio_mask, reg);
Hiroshi DOYUbee79302006-09-25 12:41:46 +0300809
810 /* Workaround for clearing DSP GPIO interrupts to allow retention */
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800811 if (cpu_is_omap24xx() || cpu_is_omap34xx())
812 reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
813 else if (cpu_is_omap44xx())
814 reg = bank->base + OMAP4_GPIO_IRQSTATUS1;
815
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530816 if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
Roger Quadrosbedfd152009-04-23 11:10:50 -0700817 __raw_writel(gpio_mask, reg);
818
819 /* Flush posted write for the irq status to avoid spurious interrupts */
820 __raw_readl(reg);
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530821 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100822}
823
824static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
825{
826 _clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
827}
828
Imre Deakea6dedd2006-06-26 16:16:00 -0700829static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
830{
831 void __iomem *reg = bank->base;
Imre Deak99c47702006-06-26 16:16:07 -0700832 int inv = 0;
833 u32 l;
834 u32 mask;
Imre Deakea6dedd2006-06-26 16:16:00 -0700835
836 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800837#ifdef CONFIG_ARCH_OMAP1
Imre Deakea6dedd2006-06-26 16:16:00 -0700838 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800839 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Imre Deak99c47702006-06-26 16:16:07 -0700840 mask = 0xffff;
841 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700842 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800843#endif
844#ifdef CONFIG_ARCH_OMAP15XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700845 case METHOD_GPIO_1510:
846 reg += OMAP1510_GPIO_INT_MASK;
Imre Deak99c47702006-06-26 16:16:07 -0700847 mask = 0xffff;
848 inv = 1;
Imre Deakea6dedd2006-06-26 16:16:00 -0700849 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800850#endif
851#ifdef CONFIG_ARCH_OMAP16XX
Imre Deakea6dedd2006-06-26 16:16:00 -0700852 case METHOD_GPIO_1610:
853 reg += OMAP1610_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700854 mask = 0xffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700855 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800856#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100857#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100858 case METHOD_GPIO_7XX:
859 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700860 mask = 0xffffffff;
861 inv = 1;
862 break;
863#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800864#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Imre Deakea6dedd2006-06-26 16:16:00 -0700865 case METHOD_GPIO_24XX:
866 reg += OMAP24XX_GPIO_IRQENABLE1;
Imre Deak99c47702006-06-26 16:16:07 -0700867 mask = 0xffffffff;
Imre Deakea6dedd2006-06-26 16:16:00 -0700868 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800869#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530870#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800871 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530872 reg += OMAP4_GPIO_IRQSTATUSSET0;
873 mask = 0xffffffff;
874 break;
875#endif
Imre Deakea6dedd2006-06-26 16:16:00 -0700876 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800877 WARN_ON(1);
Imre Deakea6dedd2006-06-26 16:16:00 -0700878 return 0;
879 }
880
Imre Deak99c47702006-06-26 16:16:07 -0700881 l = __raw_readl(reg);
882 if (inv)
883 l = ~l;
884 l &= mask;
885 return l;
Imre Deakea6dedd2006-06-26 16:16:00 -0700886}
887
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
889{
Tony Lindgren92105bb2005-09-07 17:20:26 +0100890 void __iomem *reg = bank->base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100891 u32 l;
892
893 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -0800894#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100895 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -0800896 reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100897 l = __raw_readl(reg);
898 if (enable)
899 l &= ~(gpio_mask);
900 else
901 l |= gpio_mask;
902 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800903#endif
904#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905 case METHOD_GPIO_1510:
906 reg += OMAP1510_GPIO_INT_MASK;
907 l = __raw_readl(reg);
908 if (enable)
909 l &= ~(gpio_mask);
910 else
911 l |= gpio_mask;
912 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800913#endif
914#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100915 case METHOD_GPIO_1610:
916 if (enable)
917 reg += OMAP1610_GPIO_SET_IRQENABLE1;
918 else
919 reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
920 l = gpio_mask;
921 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800922#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +0100923#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +0100924 case METHOD_GPIO_7XX:
925 reg += OMAP7XX_GPIO_INT_MASK;
Zebediah C. McClure56739a62009-03-23 18:07:40 -0700926 l = __raw_readl(reg);
927 if (enable)
928 l &= ~(gpio_mask);
929 else
930 l |= gpio_mask;
931 break;
932#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -0800933#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100934 case METHOD_GPIO_24XX:
935 if (enable)
936 reg += OMAP24XX_GPIO_SETIRQENABLE1;
937 else
938 reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
939 l = gpio_mask;
940 break;
David Brownelle5c56ed2006-12-06 17:13:59 -0800941#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530942#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800943 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +0530944 if (enable)
945 reg += OMAP4_GPIO_IRQSTATUSSET0;
946 else
947 reg += OMAP4_GPIO_IRQSTATUSCLR0;
948 l = gpio_mask;
949 break;
950#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100951 default:
David Brownelle5c56ed2006-12-06 17:13:59 -0800952 WARN_ON(1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100953 return;
954 }
955 __raw_writel(l, reg);
956}
957
958static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
959{
960 _enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
961}
962
Tony Lindgren92105bb2005-09-07 17:20:26 +0100963/*
964 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
965 * 1510 does not seem to have a wake-up register. If JTAG is connected
966 * to the target, system will wake up always on GPIO events. While
967 * system is running all registered GPIO interrupts need to have wake-up
968 * enabled. When system is suspended, only selected GPIO interrupts need
969 * to have wake-up enabled.
970 */
971static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
972{
Tony Lindgren4cc64202010-01-08 10:29:05 -0800973 unsigned long uninitialized_var(flags);
David Brownella6472532008-03-03 04:33:30 -0800974
Tony Lindgren92105bb2005-09-07 17:20:26 +0100975 switch (bank->method) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800976#ifdef CONFIG_ARCH_OMAP16XX
David Brownell11a78b72006-12-06 17:14:11 -0800977 case METHOD_MPUIO:
Tony Lindgren92105bb2005-09-07 17:20:26 +0100978 case METHOD_GPIO_1610:
David Brownella6472532008-03-03 04:33:30 -0800979 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700980 if (enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +0100981 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700982 else
Tony Lindgren92105bb2005-09-07 17:20:26 +0100983 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -0800984 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100985 return 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800986#endif
Tony Lindgren140455f2010-02-12 12:26:48 -0800987#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800988 case METHOD_GPIO_24XX:
Tony Lindgren3f1686a92010-02-15 09:27:25 -0800989 case METHOD_GPIO_44XX:
David Brownell11a78b72006-12-06 17:14:11 -0800990 if (bank->non_wakeup_gpios & (1 << gpio)) {
991 printk(KERN_ERR "Unable to modify wakeup on "
992 "non-wakeup GPIO%d\n",
993 (bank - gpio_bank) * 32 + gpio);
994 return -EINVAL;
995 }
David Brownella6472532008-03-03 04:33:30 -0800996 spin_lock_irqsave(&bank->lock, flags);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700997 if (enable)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -0800998 bank->suspend_wakeup |= (1 << gpio);
Kevin Hilmanb3bb4f62009-04-23 11:10:49 -0700999 else
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001000 bank->suspend_wakeup &= ~(1 << gpio);
David Brownella6472532008-03-03 04:33:30 -08001001 spin_unlock_irqrestore(&bank->lock, flags);
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001002 return 0;
1003#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001004 default:
1005 printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
1006 bank->method);
1007 return -EINVAL;
1008 }
1009}
1010
Tony Lindgren4196dd62006-09-25 12:41:38 +03001011static void _reset_gpio(struct gpio_bank *bank, int gpio)
1012{
1013 _set_gpio_direction(bank, get_gpio_index(gpio), 1);
1014 _set_gpio_irqenable(bank, gpio, 0);
1015 _clear_gpio_irqstatus(bank, gpio);
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +01001016 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001017}
1018
Tony Lindgren92105bb2005-09-07 17:20:26 +01001019/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001020static int gpio_wake_enable(struct irq_data *d, unsigned int enable)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001021{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001022 unsigned int gpio = d->irq - IH_GPIO_BASE;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001023 struct gpio_bank *bank;
1024 int retval;
1025
1026 if (check_gpio(gpio) < 0)
1027 return -ENODEV;
Lennert Buytenheke9191022010-11-29 11:17:17 +01001028 bank = irq_data_get_irq_chip_data(d);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001029 retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001030
1031 return retval;
1032}
1033
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001034static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001035{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001036 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001037 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001038
David Brownella6472532008-03-03 04:33:30 -08001039 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001040
Tony Lindgren4196dd62006-09-25 12:41:38 +03001041 /* Set trigger to none. You need to enable the desired trigger with
1042 * request_irq() or set_irq_type().
1043 */
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001044 _set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001045
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001046#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001047 if (bank->method == METHOD_GPIO_1510) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001048 void __iomem *reg;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001049
Tony Lindgren92105bb2005-09-07 17:20:26 +01001050 /* Claim the pin for MPU */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001051 reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001052 __raw_writel(__raw_readl(reg) | (1 << offset), reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001053 }
1054#endif
Charulatha V058af1e2009-11-22 10:11:25 -08001055 if (!cpu_class_is_omap1()) {
1056 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -07001057 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -08001058 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -07001059
1060 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1061 reg += OMAP24XX_GPIO_CTRL;
1062 else if (cpu_is_omap44xx())
1063 reg += OMAP4_GPIO_CTRL;
1064 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001065 /* Module is enabled, clocks are not gated */
Charulatha V9f096862010-05-14 12:05:27 -07001066 ctrl &= 0xFFFFFFFE;
1067 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001068 }
1069 bank->mod_usage |= 1 << offset;
1070 }
David Brownella6472532008-03-03 04:33:30 -08001071 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001072
1073 return 0;
1074}
1075
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001076static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001077{
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001078 struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
David Brownella6472532008-03-03 04:33:30 -08001079 unsigned long flags;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001080
David Brownella6472532008-03-03 04:33:30 -08001081 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001082#ifdef CONFIG_ARCH_OMAP16XX
1083 if (bank->method == METHOD_GPIO_1610) {
1084 /* Disable wake-up during idle for dynamic tick */
1085 void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001086 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001087 }
1088#endif
Charulatha V9f096862010-05-14 12:05:27 -07001089#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1090 if (bank->method == METHOD_GPIO_24XX) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01001091 /* Disable wake-up during idle for dynamic tick */
1092 void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001093 __raw_writel(1 << offset, reg);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001094 }
1095#endif
Charulatha V9f096862010-05-14 12:05:27 -07001096#ifdef CONFIG_ARCH_OMAP4
1097 if (bank->method == METHOD_GPIO_44XX) {
1098 /* Disable wake-up during idle for dynamic tick */
1099 void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
1100 __raw_writel(1 << offset, reg);
1101 }
1102#endif
Charulatha V058af1e2009-11-22 10:11:25 -08001103 if (!cpu_class_is_omap1()) {
1104 bank->mod_usage &= ~(1 << offset);
1105 if (!bank->mod_usage) {
Charulatha V9f096862010-05-14 12:05:27 -07001106 void __iomem *reg = bank->base;
Charulatha V058af1e2009-11-22 10:11:25 -08001107 u32 ctrl;
Charulatha V9f096862010-05-14 12:05:27 -07001108
1109 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1110 reg += OMAP24XX_GPIO_CTRL;
1111 else if (cpu_is_omap44xx())
1112 reg += OMAP4_GPIO_CTRL;
1113 ctrl = __raw_readl(reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001114 /* Module is disabled, clocks are gated */
1115 ctrl |= 1;
Charulatha V9f096862010-05-14 12:05:27 -07001116 __raw_writel(ctrl, reg);
Charulatha V058af1e2009-11-22 10:11:25 -08001117 }
1118 }
Jarkko Nikula3ff164e2008-12-10 17:35:27 -08001119 _reset_gpio(bank, bank->chip.base + offset);
David Brownella6472532008-03-03 04:33:30 -08001120 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001121}
1122
1123/*
1124 * We need to unmask the GPIO bank interrupt as soon as possible to
1125 * avoid missing GPIO interrupts for other lines in the bank.
1126 * Then we need to mask-read-clear-unmask the triggered GPIO lines
1127 * in the bank to avoid missing nested interrupts for a GPIO line.
1128 * If we wait to unmask individual GPIO lines in the bank after the
1129 * line's interrupt handler has been run, we may miss some nested
1130 * interrupts.
1131 */
Russell King10dd5ce2006-11-23 11:41:32 +00001132static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001133{
Tony Lindgren92105bb2005-09-07 17:20:26 +01001134 void __iomem *isr_reg = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001135 u32 isr;
Cory Maccarrone4318f362010-01-08 10:29:04 -08001136 unsigned int gpio_irq, gpio_index;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137 struct gpio_bank *bank;
Imre Deakea6dedd2006-06-26 16:16:00 -07001138 u32 retrigger = 0;
1139 int unmasked = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140
Lennert Buytenheke9191022010-11-29 11:17:17 +01001141 desc->irq_data.chip->irq_ack(&desc->irq_data);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001142
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001143 bank = irq_get_handler_data(irq);
David Brownelle5c56ed2006-12-06 17:13:59 -08001144#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145 if (bank->method == METHOD_MPUIO)
Tony Lindgren5de62b82010-12-07 16:26:58 -08001146 isr_reg = bank->base +
1147 OMAP_MPUIO_GPIO_INT / bank->stride;
David Brownelle5c56ed2006-12-06 17:13:59 -08001148#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001149#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001150 if (bank->method == METHOD_GPIO_1510)
1151 isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
1152#endif
1153#if defined(CONFIG_ARCH_OMAP16XX)
1154 if (bank->method == METHOD_GPIO_1610)
1155 isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
1156#endif
Alistair Buxtonb718aa82009-09-23 18:56:19 +01001157#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
Alistair Buxton7c006922009-09-22 10:02:58 +01001158 if (bank->method == METHOD_GPIO_7XX)
1159 isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
Zebediah C. McClure56739a62009-03-23 18:07:40 -07001160#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001161#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001162 if (bank->method == METHOD_GPIO_24XX)
1163 isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1164#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301165#if defined(CONFIG_ARCH_OMAP4)
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001166 if (bank->method == METHOD_GPIO_44XX)
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301167 isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1168#endif
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001169
1170 if (WARN_ON(!isr_reg))
1171 goto exit;
1172
Tony Lindgren92105bb2005-09-07 17:20:26 +01001173 while(1) {
Tony Lindgren6e60e792006-04-02 17:46:23 +01001174 u32 isr_saved, level_mask = 0;
Imre Deakea6dedd2006-06-26 16:16:00 -07001175 u32 enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001176
Imre Deakea6dedd2006-06-26 16:16:00 -07001177 enabled = _get_gpio_irqbank_mask(bank);
1178 isr_saved = isr = __raw_readl(isr_reg) & enabled;
Tony Lindgren6e60e792006-04-02 17:46:23 +01001179
1180 if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
1181 isr &= 0x0000ffff;
1182
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001183 if (cpu_class_is_omap2()) {
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001184 level_mask = bank->level_mask & enabled;
Imre Deakea6dedd2006-06-26 16:16:00 -07001185 }
Tony Lindgren6e60e792006-04-02 17:46:23 +01001186
1187 /* clear edge sensitive interrupts before handler(s) are
1188 called so that we don't miss any interrupt occurred while
1189 executing them */
1190 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
1191 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
1192 _enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);
1193
1194 /* if there is only edge sensitive GPIO pin interrupts
1195 configured, we could unmask GPIO bank interrupt immediately */
Imre Deakea6dedd2006-06-26 16:16:00 -07001196 if (!level_mask && !unmasked) {
1197 unmasked = 1;
Lennert Buytenheke9191022010-11-29 11:17:17 +01001198 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Imre Deakea6dedd2006-06-26 16:16:00 -07001199 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001200
Imre Deakea6dedd2006-06-26 16:16:00 -07001201 isr |= retrigger;
1202 retrigger = 0;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001203 if (!isr)
1204 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001205
Tony Lindgren92105bb2005-09-07 17:20:26 +01001206 gpio_irq = bank->virtual_irq_start;
1207 for (; isr != 0; isr >>= 1, gpio_irq++) {
Cory Maccarrone4318f362010-01-08 10:29:04 -08001208 gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));
1209
Tony Lindgren92105bb2005-09-07 17:20:26 +01001210 if (!(isr & 1))
1211 continue;
Thomas Gleixner29454dd2006-07-03 02:22:22 +02001212
Cory Maccarrone4318f362010-01-08 10:29:04 -08001213#ifdef CONFIG_ARCH_OMAP1
1214 /*
1215 * Some chips can't respond to both rising and falling
1216 * at the same time. If this irq was requested with
1217 * both flags, we need to flip the ICR data for the IRQ
1218 * to respond to the IRQ for the opposite direction.
1219 * This will be indicated in the bank toggle_mask.
1220 */
1221 if (bank->toggle_mask & (1 << gpio_index))
1222 _toggle_gpio_edge_triggering(bank, gpio_index);
1223#endif
1224
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +01001225 generic_handle_irq(gpio_irq);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001226 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001227 }
Imre Deakea6dedd2006-06-26 16:16:00 -07001228 /* if bank has any level sensitive GPIO pin interrupt
1229 configured, we must unmask the bank interrupt only after
1230 handler(s) are executed in order to avoid spurious bank
1231 interrupt */
Evgeny Kuznetsovb1cc4c52010-12-07 16:25:40 -08001232exit:
Imre Deakea6dedd2006-06-26 16:16:00 -07001233 if (!unmasked)
Lennert Buytenheke9191022010-11-29 11:17:17 +01001234 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001235}
1236
Lennert Buytenheke9191022010-11-29 11:17:17 +01001237static void gpio_irq_shutdown(struct irq_data *d)
Tony Lindgren4196dd62006-09-25 12:41:38 +03001238{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001239 unsigned int gpio = d->irq - IH_GPIO_BASE;
1240 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren4196dd62006-09-25 12:41:38 +03001241
1242 _reset_gpio(bank, gpio);
1243}
1244
Lennert Buytenheke9191022010-11-29 11:17:17 +01001245static void gpio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001246{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001247 unsigned int gpio = d->irq - IH_GPIO_BASE;
1248 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001249
1250 _clear_gpio_irqstatus(bank, gpio);
1251}
1252
Lennert Buytenheke9191022010-11-29 11:17:17 +01001253static void gpio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001254{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001255 unsigned int gpio = d->irq - IH_GPIO_BASE;
1256 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001257
1258 _set_gpio_irqenable(bank, gpio, 0);
Kevin Hilman55b60192009-06-04 15:57:10 -07001259 _set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001260}
1261
Lennert Buytenheke9191022010-11-29 11:17:17 +01001262static void gpio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001263{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001264 unsigned int gpio = d->irq - IH_GPIO_BASE;
1265 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001266 unsigned int irq_mask = 1 << get_gpio_index(gpio);
Thomas Gleixner8c04a172011-03-24 12:40:15 +01001267 u32 trigger = irqd_get_trigger_type(d);
Kevin Hilman55b60192009-06-04 15:57:10 -07001268
1269 if (trigger)
1270 _set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
Kevin Hilmanb144ff62008-01-16 21:56:15 -08001271
1272 /* For level-triggered GPIOs, the clearing must be done after
1273 * the HW source is cleared, thus after the handler has run */
1274 if (bank->level_mask & irq_mask) {
1275 _set_gpio_irqenable(bank, gpio, 0);
1276 _clear_gpio_irqstatus(bank, gpio);
1277 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001278
Kevin Hilman4de8c752008-01-16 21:56:14 -08001279 _set_gpio_irqenable(bank, gpio, 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001280}
1281
David Brownelle5c56ed2006-12-06 17:13:59 -08001282static struct irq_chip gpio_irq_chip = {
1283 .name = "GPIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001284 .irq_shutdown = gpio_irq_shutdown,
1285 .irq_ack = gpio_ack_irq,
1286 .irq_mask = gpio_mask_irq,
1287 .irq_unmask = gpio_unmask_irq,
1288 .irq_set_type = gpio_irq_type,
1289 .irq_set_wake = gpio_wake_enable,
David Brownelle5c56ed2006-12-06 17:13:59 -08001290};
1291
1292/*---------------------------------------------------------------------*/
1293
1294#ifdef CONFIG_ARCH_OMAP1
1295
1296/* MPUIO uses the always-on 32k clock */
1297
Lennert Buytenheke9191022010-11-29 11:17:17 +01001298static void mpuio_ack_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001299{
1300 /* The ISR is reset automatically, so do nothing here. */
1301}
1302
Lennert Buytenheke9191022010-11-29 11:17:17 +01001303static void mpuio_mask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001304{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001305 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1306 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001307
1308 _set_gpio_irqenable(bank, gpio, 0);
1309}
1310
Lennert Buytenheke9191022010-11-29 11:17:17 +01001311static void mpuio_unmask_irq(struct irq_data *d)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001312{
Lennert Buytenheke9191022010-11-29 11:17:17 +01001313 unsigned int gpio = OMAP_MPUIO(d->irq - IH_MPUIO_BASE);
1314 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001315
1316 _set_gpio_irqenable(bank, gpio, 1);
1317}
1318
David Brownelle5c56ed2006-12-06 17:13:59 -08001319static struct irq_chip mpuio_irq_chip = {
1320 .name = "MPUIO",
Lennert Buytenheke9191022010-11-29 11:17:17 +01001321 .irq_ack = mpuio_ack_irq,
1322 .irq_mask = mpuio_mask_irq,
1323 .irq_unmask = mpuio_unmask_irq,
1324 .irq_set_type = gpio_irq_type,
David Brownell11a78b72006-12-06 17:14:11 -08001325#ifdef CONFIG_ARCH_OMAP16XX
1326 /* REVISIT: assuming only 16xx supports MPUIO wake events */
Lennert Buytenheke9191022010-11-29 11:17:17 +01001327 .irq_set_wake = gpio_wake_enable,
David Brownell11a78b72006-12-06 17:14:11 -08001328#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001329};
1330
David Brownelle5c56ed2006-12-06 17:13:59 -08001331
1332#define bank_is_mpuio(bank) ((bank)->method == METHOD_MPUIO)
1333
David Brownell11a78b72006-12-06 17:14:11 -08001334
1335#ifdef CONFIG_ARCH_OMAP16XX
1336
1337#include <linux/platform_device.h>
1338
Magnus Damm79ee0312009-07-08 13:22:04 +02001339static int omap_mpuio_suspend_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001340{
Magnus Damm79ee0312009-07-08 13:22:04 +02001341 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001342 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001343 void __iomem *mask_reg = bank->base +
1344 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001345 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001346
David Brownella6472532008-03-03 04:33:30 -08001347 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001348 bank->saved_wakeup = __raw_readl(mask_reg);
1349 __raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001350 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001351
1352 return 0;
1353}
1354
Magnus Damm79ee0312009-07-08 13:22:04 +02001355static int omap_mpuio_resume_noirq(struct device *dev)
David Brownell11a78b72006-12-06 17:14:11 -08001356{
Magnus Damm79ee0312009-07-08 13:22:04 +02001357 struct platform_device *pdev = to_platform_device(dev);
David Brownell11a78b72006-12-06 17:14:11 -08001358 struct gpio_bank *bank = platform_get_drvdata(pdev);
Tony Lindgren5de62b82010-12-07 16:26:58 -08001359 void __iomem *mask_reg = bank->base +
1360 OMAP_MPUIO_GPIO_MASKIT / bank->stride;
David Brownella6472532008-03-03 04:33:30 -08001361 unsigned long flags;
David Brownell11a78b72006-12-06 17:14:11 -08001362
David Brownella6472532008-03-03 04:33:30 -08001363 spin_lock_irqsave(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001364 __raw_writel(bank->saved_wakeup, mask_reg);
David Brownella6472532008-03-03 04:33:30 -08001365 spin_unlock_irqrestore(&bank->lock, flags);
David Brownell11a78b72006-12-06 17:14:11 -08001366
1367 return 0;
1368}
1369
Alexey Dobriyan47145212009-12-14 18:00:08 -08001370static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
Magnus Damm79ee0312009-07-08 13:22:04 +02001371 .suspend_noirq = omap_mpuio_suspend_noirq,
1372 .resume_noirq = omap_mpuio_resume_noirq,
1373};
1374
David Brownell11a78b72006-12-06 17:14:11 -08001375/* use platform_driver for this, now that there's no longer any
1376 * point to sys_device (other than not disturbing old code).
1377 */
1378static struct platform_driver omap_mpuio_driver = {
David Brownell11a78b72006-12-06 17:14:11 -08001379 .driver = {
1380 .name = "mpuio",
Magnus Damm79ee0312009-07-08 13:22:04 +02001381 .pm = &omap_mpuio_dev_pm_ops,
David Brownell11a78b72006-12-06 17:14:11 -08001382 },
1383};
1384
1385static struct platform_device omap_mpuio_device = {
1386 .name = "mpuio",
1387 .id = -1,
1388 .dev = {
1389 .driver = &omap_mpuio_driver.driver,
1390 }
1391 /* could list the /proc/iomem resources */
1392};
1393
1394static inline void mpuio_init(void)
1395{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001396 struct gpio_bank *bank = get_gpio_bank(OMAP_MPUIO(0));
1397 platform_set_drvdata(&omap_mpuio_device, bank);
David Brownellfcf126d2007-04-02 12:46:47 -07001398
David Brownell11a78b72006-12-06 17:14:11 -08001399 if (platform_driver_register(&omap_mpuio_driver) == 0)
1400 (void) platform_device_register(&omap_mpuio_device);
1401}
1402
1403#else
1404static inline void mpuio_init(void) {}
1405#endif /* 16xx */
1406
David Brownelle5c56ed2006-12-06 17:13:59 -08001407#else
1408
1409extern struct irq_chip mpuio_irq_chip;
1410
1411#define bank_is_mpuio(bank) 0
David Brownell11a78b72006-12-06 17:14:11 -08001412static inline void mpuio_init(void) {}
David Brownelle5c56ed2006-12-06 17:13:59 -08001413
1414#endif
1415
1416/*---------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001417
David Brownell52e31342008-03-03 12:43:23 -08001418/* REVISIT these are stupid implementations! replace by ones that
1419 * don't switch on METHOD_* and which mostly avoid spinlocks
1420 */
1421
1422static int gpio_input(struct gpio_chip *chip, unsigned offset)
1423{
1424 struct gpio_bank *bank;
1425 unsigned long flags;
1426
1427 bank = container_of(chip, struct gpio_bank, chip);
1428 spin_lock_irqsave(&bank->lock, flags);
1429 _set_gpio_direction(bank, offset, 1);
1430 spin_unlock_irqrestore(&bank->lock, flags);
1431 return 0;
1432}
1433
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001434static int gpio_is_input(struct gpio_bank *bank, int mask)
1435{
1436 void __iomem *reg = bank->base;
1437
1438 switch (bank->method) {
1439 case METHOD_MPUIO:
Tony Lindgren5de62b82010-12-07 16:26:58 -08001440 reg += OMAP_MPUIO_IO_CNTL / bank->stride;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001441 break;
1442 case METHOD_GPIO_1510:
1443 reg += OMAP1510_GPIO_DIR_CONTROL;
1444 break;
1445 case METHOD_GPIO_1610:
1446 reg += OMAP1610_GPIO_DIRECTION;
1447 break;
Alistair Buxton7c006922009-09-22 10:02:58 +01001448 case METHOD_GPIO_7XX:
1449 reg += OMAP7XX_GPIO_DIR_CONTROL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001450 break;
1451 case METHOD_GPIO_24XX:
1452 reg += OMAP24XX_GPIO_OE;
1453 break;
Charulatha V9f096862010-05-14 12:05:27 -07001454 case METHOD_GPIO_44XX:
1455 reg += OMAP4_GPIO_OE;
1456 break;
1457 default:
1458 WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
1459 return -EINVAL;
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001460 }
1461 return __raw_readl(reg) & mask;
1462}
1463
David Brownell52e31342008-03-03 12:43:23 -08001464static int gpio_get(struct gpio_chip *chip, unsigned offset)
1465{
Roger Quadrosb37c45b2009-08-05 16:53:24 +03001466 struct gpio_bank *bank;
1467 void __iomem *reg;
1468 int gpio;
1469 u32 mask;
1470
1471 gpio = chip->base + offset;
1472 bank = get_gpio_bank(gpio);
1473 reg = bank->base;
1474 mask = 1 << get_gpio_index(gpio);
1475
1476 if (gpio_is_input(bank, mask))
1477 return _get_gpio_datain(bank, gpio);
1478 else
1479 return _get_gpio_dataout(bank, gpio);
David Brownell52e31342008-03-03 12:43:23 -08001480}
1481
1482static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
1483{
1484 struct gpio_bank *bank;
1485 unsigned long flags;
1486
1487 bank = container_of(chip, struct gpio_bank, chip);
1488 spin_lock_irqsave(&bank->lock, flags);
1489 _set_gpio_dataout(bank, offset, value);
1490 _set_gpio_direction(bank, offset, 0);
1491 spin_unlock_irqrestore(&bank->lock, flags);
1492 return 0;
1493}
1494
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001495static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
1496 unsigned debounce)
1497{
1498 struct gpio_bank *bank;
1499 unsigned long flags;
1500
1501 bank = container_of(chip, struct gpio_bank, chip);
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001502
1503 if (!bank->dbck) {
1504 bank->dbck = clk_get(bank->dev, "dbclk");
1505 if (IS_ERR(bank->dbck))
1506 dev_err(bank->dev, "Could not get gpio dbck\n");
1507 }
1508
Felipe Balbi168ef3d2010-05-26 14:42:23 -07001509 spin_lock_irqsave(&bank->lock, flags);
1510 _set_gpio_debounce(bank, offset, debounce);
1511 spin_unlock_irqrestore(&bank->lock, flags);
1512
1513 return 0;
1514}
1515
David Brownell52e31342008-03-03 12:43:23 -08001516static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1517{
1518 struct gpio_bank *bank;
1519 unsigned long flags;
1520
1521 bank = container_of(chip, struct gpio_bank, chip);
1522 spin_lock_irqsave(&bank->lock, flags);
1523 _set_gpio_dataout(bank, offset, value);
1524 spin_unlock_irqrestore(&bank->lock, flags);
1525}
1526
David Brownella007b702008-12-10 17:35:25 -08001527static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
1528{
1529 struct gpio_bank *bank;
1530
1531 bank = container_of(chip, struct gpio_bank, chip);
1532 return bank->virtual_irq_start + offset;
1533}
1534
David Brownell52e31342008-03-03 12:43:23 -08001535/*---------------------------------------------------------------------*/
1536
Tony Lindgren9a748052010-12-07 16:26:56 -08001537static void __init omap_gpio_show_rev(struct gpio_bank *bank)
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001538{
1539 u32 rev;
1540
Tony Lindgren9a748052010-12-07 16:26:56 -08001541 if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
1542 rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001543 else if (cpu_is_omap24xx() || cpu_is_omap34xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001544 rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001545 else if (cpu_is_omap44xx())
Tony Lindgren9a748052010-12-07 16:26:56 -08001546 rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001547 else
1548 return;
1549
1550 printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
1551 (rev >> 4) & 0x0f, rev & 0x0f);
1552}
1553
David Brownell8ba55c52008-02-26 11:10:50 -08001554/* This lock class tells lockdep that GPIO irqs are in a different
1555 * category than their parents, so it won't report false recursion.
1556 */
1557static struct lock_class_key gpio_lock_class;
1558
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001559static inline int init_gpio_info(struct platform_device *pdev)
1560{
1561 /* TODO: Analyze removing gpio_bank_count usage from driver code */
1562 gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
1563 GFP_KERNEL);
1564 if (!gpio_bank) {
1565 dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
1566 return -ENOMEM;
1567 }
1568 return 0;
1569}
1570
1571/* TODO: Cleanup cpu_is_* checks */
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001572static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
1573{
1574 if (cpu_class_is_omap2()) {
1575 if (cpu_is_omap44xx()) {
1576 __raw_writel(0xffffffff, bank->base +
1577 OMAP4_GPIO_IRQSTATUSCLR0);
1578 __raw_writel(0x00000000, bank->base +
1579 OMAP4_GPIO_DEBOUNCENABLE);
1580 /* Initialize interface clk ungated, module enabled */
1581 __raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
1582 } else if (cpu_is_omap34xx()) {
1583 __raw_writel(0x00000000, bank->base +
1584 OMAP24XX_GPIO_IRQENABLE1);
1585 __raw_writel(0xffffffff, bank->base +
1586 OMAP24XX_GPIO_IRQSTATUS1);
1587 __raw_writel(0x00000000, bank->base +
1588 OMAP24XX_GPIO_DEBOUNCE_EN);
1589
1590 /* Initialize interface clk ungated, module enabled */
1591 __raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
1592 } else if (cpu_is_omap24xx()) {
1593 static const u32 non_wakeup_gpios[] = {
1594 0xe203ffc0, 0x08700040
1595 };
1596 if (id < ARRAY_SIZE(non_wakeup_gpios))
1597 bank->non_wakeup_gpios = non_wakeup_gpios[id];
1598 }
1599 } else if (cpu_class_is_omap1()) {
1600 if (bank_is_mpuio(bank))
Tony Lindgren5de62b82010-12-07 16:26:58 -08001601 __raw_writew(0xffff, bank->base +
1602 OMAP_MPUIO_GPIO_MASKIT / bank->stride);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001603 if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
1604 __raw_writew(0xffff, bank->base
1605 + OMAP1510_GPIO_INT_MASK);
1606 __raw_writew(0x0000, bank->base
1607 + OMAP1510_GPIO_INT_STATUS);
1608 }
1609 if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
1610 __raw_writew(0x0000, bank->base
1611 + OMAP1610_GPIO_IRQENABLE1);
1612 __raw_writew(0xffff, bank->base
1613 + OMAP1610_GPIO_IRQSTATUS1);
1614 __raw_writew(0x0014, bank->base
1615 + OMAP1610_GPIO_SYSCONFIG);
1616
1617 /*
1618 * Enable system clock for GPIO module.
1619 * The CAM_CLK_CTRL *is* really the right place.
1620 */
1621 omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
1622 ULPD_CAM_CLK_CTRL);
1623 }
1624 if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
1625 __raw_writel(0xffffffff, bank->base
1626 + OMAP7XX_GPIO_INT_MASK);
1627 __raw_writel(0x00000000, bank->base
1628 + OMAP7XX_GPIO_INT_STATUS);
1629 }
1630 }
1631}
1632
1633static void __init omap_gpio_chip_init(struct gpio_bank *bank)
1634{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001635 int j;
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001636 static int gpio;
1637
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001638 bank->mod_usage = 0;
1639 /*
1640 * REVISIT eventually switch from OMAP-specific gpio structs
1641 * over to the generic ones
1642 */
1643 bank->chip.request = omap_gpio_request;
1644 bank->chip.free = omap_gpio_free;
1645 bank->chip.direction_input = gpio_input;
1646 bank->chip.get = gpio_get;
1647 bank->chip.direction_output = gpio_output;
1648 bank->chip.set_debounce = gpio_debounce;
1649 bank->chip.set = gpio_set;
1650 bank->chip.to_irq = gpio_2irq;
1651 if (bank_is_mpuio(bank)) {
1652 bank->chip.label = "mpuio";
1653#ifdef CONFIG_ARCH_OMAP16XX
1654 bank->chip.dev = &omap_mpuio_device.dev;
1655#endif
1656 bank->chip.base = OMAP_MPUIO(0);
1657 } else {
1658 bank->chip.label = "gpio";
1659 bank->chip.base = gpio;
1660 gpio += bank_width;
1661 }
1662 bank->chip.ngpio = bank_width;
1663
1664 gpiochip_add(&bank->chip);
1665
1666 for (j = bank->virtual_irq_start;
1667 j < bank->virtual_irq_start + bank_width; j++) {
Thomas Gleixner1475b852011-03-22 17:11:09 +01001668 irq_set_lockdep_class(j, &gpio_lock_class);
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001669 irq_set_chip_data(j, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001670 if (bank_is_mpuio(bank))
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001671 irq_set_chip(j, &mpuio_irq_chip);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001672 else
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001673 irq_set_chip(j, &gpio_irq_chip);
1674 irq_set_handler(j, handle_simple_irq);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001675 set_irq_flags(j, IRQF_VALID);
1676 }
Thomas Gleixner6845664a2011-03-24 13:25:22 +01001677 irq_set_chained_handler(bank->irq, gpio_irq_handler);
1678 irq_set_handler_data(bank->irq, bank);
Varadarajan, Charulatha2fae7fb2010-12-07 16:26:55 -08001679}
1680
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001681static int __devinit omap_gpio_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001682{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001683 static int gpio_init_done;
1684 struct omap_gpio_platform_data *pdata;
1685 struct resource *res;
1686 int id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001687 struct gpio_bank *bank;
1688
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001689 if (!pdev->dev.platform_data)
1690 return -EINVAL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001691
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001692 pdata = pdev->dev.platform_data;
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -08001693
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001694 if (!gpio_init_done) {
1695 int ret;
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001696
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001697 ret = init_gpio_info(pdev);
1698 if (ret)
1699 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001700 }
1701
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001702 id = pdev->id;
1703 bank = &gpio_bank[id];
1704
1705 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1706 if (unlikely(!res)) {
1707 dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
1708 return -ENODEV;
1709 }
1710
1711 bank->irq = res->start;
1712 bank->virtual_irq_start = pdata->virtual_irq_start;
1713 bank->method = pdata->bank_type;
1714 bank->dev = &pdev->dev;
1715 bank->dbck_flag = pdata->dbck_flag;
Tony Lindgren5de62b82010-12-07 16:26:58 -08001716 bank->stride = pdata->bank_stride;
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001717 bank_width = pdata->bank_width;
1718
1719 spin_lock_init(&bank->lock);
1720
1721 /* Static mapping, never released */
1722 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1723 if (unlikely(!res)) {
1724 dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
1725 return -ENODEV;
1726 }
1727
1728 bank->base = ioremap(res->start, resource_size(res));
1729 if (!bank->base) {
1730 dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
1731 return -ENOMEM;
1732 }
1733
1734 pm_runtime_enable(bank->dev);
1735 pm_runtime_get_sync(bank->dev);
1736
1737 omap_gpio_mod_init(bank, id);
1738 omap_gpio_chip_init(bank);
Tony Lindgren9a748052010-12-07 16:26:56 -08001739 omap_gpio_show_rev(bank);
Tony Lindgren9f7065d2009-10-19 15:25:20 -07001740
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08001741 if (!gpio_init_done)
1742 gpio_init_done = 1;
1743
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001744 return 0;
1745}
1746
Tony Lindgren140455f2010-02-12 12:26:48 -08001747#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001748static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
1749{
1750 int i;
1751
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08001752 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001753 return 0;
1754
1755 for (i = 0; i < gpio_bank_count; i++) {
1756 struct gpio_bank *bank = &gpio_bank[i];
1757 void __iomem *wake_status;
1758 void __iomem *wake_clear;
1759 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001760 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001761
1762 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001763#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001764 case METHOD_GPIO_1610:
1765 wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
1766 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1767 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1768 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001769#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001770#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001771 case METHOD_GPIO_24XX:
Tero Kristo723fdb72008-11-26 14:35:16 -08001772 wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001773 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1774 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1775 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001776#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301777#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001778 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301779 wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
1780 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1781 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1782 break;
1783#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001784 default:
1785 continue;
1786 }
1787
David Brownella6472532008-03-03 04:33:30 -08001788 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001789 bank->saved_wakeup = __raw_readl(wake_status);
1790 __raw_writel(0xffffffff, wake_clear);
1791 __raw_writel(bank->suspend_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001792 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001793 }
1794
1795 return 0;
1796}
1797
1798static int omap_gpio_resume(struct sys_device *dev)
1799{
1800 int i;
1801
Tero Kristo723fdb72008-11-26 14:35:16 -08001802 if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
Tony Lindgren92105bb2005-09-07 17:20:26 +01001803 return 0;
1804
1805 for (i = 0; i < gpio_bank_count; i++) {
1806 struct gpio_bank *bank = &gpio_bank[i];
1807 void __iomem *wake_clear;
1808 void __iomem *wake_set;
David Brownella6472532008-03-03 04:33:30 -08001809 unsigned long flags;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001810
1811 switch (bank->method) {
David Brownelle5c56ed2006-12-06 17:13:59 -08001812#ifdef CONFIG_ARCH_OMAP16XX
Tony Lindgren92105bb2005-09-07 17:20:26 +01001813 case METHOD_GPIO_1610:
1814 wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1815 wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
1816 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001817#endif
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001818#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren92105bb2005-09-07 17:20:26 +01001819 case METHOD_GPIO_24XX:
Tony Lindgren0d9356c2006-09-25 12:41:45 +03001820 wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1821 wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
Tony Lindgren92105bb2005-09-07 17:20:26 +01001822 break;
David Brownelle5c56ed2006-12-06 17:13:59 -08001823#endif
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301824#ifdef CONFIG_ARCH_OMAP4
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001825 case METHOD_GPIO_44XX:
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301826 wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
1827 wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
1828 break;
1829#endif
Tony Lindgren92105bb2005-09-07 17:20:26 +01001830 default:
1831 continue;
1832 }
1833
David Brownella6472532008-03-03 04:33:30 -08001834 spin_lock_irqsave(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001835 __raw_writel(0xffffffff, wake_clear);
1836 __raw_writel(bank->saved_wakeup, wake_set);
David Brownella6472532008-03-03 04:33:30 -08001837 spin_unlock_irqrestore(&bank->lock, flags);
Tony Lindgren92105bb2005-09-07 17:20:26 +01001838 }
1839
1840 return 0;
1841}
1842
1843static struct sysdev_class omap_gpio_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001844 .name = "gpio",
Tony Lindgren92105bb2005-09-07 17:20:26 +01001845 .suspend = omap_gpio_suspend,
1846 .resume = omap_gpio_resume,
1847};
1848
1849static struct sys_device omap_gpio_device = {
1850 .id = 0,
1851 .cls = &omap_gpio_sysclass,
1852};
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001853
1854#endif
1855
Tony Lindgren140455f2010-02-12 12:26:48 -08001856#ifdef CONFIG_ARCH_OMAP2PLUS
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001857
1858static int workaround_enabled;
1859
Paul Walmsley72e06d02010-12-21 21:05:16 -07001860void omap2_gpio_prepare_for_idle(int off_mode)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001861{
1862 int i, c = 0;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001863 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001864
Tero Kristoa118b5f2008-12-22 14:27:12 +02001865 if (cpu_is_omap34xx())
1866 min = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001867
Tero Kristoa118b5f2008-12-22 14:27:12 +02001868 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001869 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001870 u32 l1 = 0, l2 = 0;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001871 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001872
Kevin Hilman0aed04352010-09-22 16:06:27 -07001873 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001874 clk_disable(bank->dbck);
1875
Paul Walmsley72e06d02010-12-21 21:05:16 -07001876 if (!off_mode)
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001877 continue;
1878
1879 /* If going to OFF, remove triggering for all
1880 * non-wakeup GPIOs. Otherwise spurious IRQs will be
1881 * generated. See OMAP2420 Errata item 1.101. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001882 if (!(bank->enabled_non_wakeup_gpios))
1883 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001884
1885 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1886 bank->saved_datain = __raw_readl(bank->base +
1887 OMAP24XX_GPIO_DATAIN);
1888 l1 = __raw_readl(bank->base +
1889 OMAP24XX_GPIO_FALLINGDETECT);
1890 l2 = __raw_readl(bank->base +
1891 OMAP24XX_GPIO_RISINGDETECT);
1892 }
1893
1894 if (cpu_is_omap44xx()) {
1895 bank->saved_datain = __raw_readl(bank->base +
1896 OMAP4_GPIO_DATAIN);
1897 l1 = __raw_readl(bank->base +
1898 OMAP4_GPIO_FALLINGDETECT);
1899 l2 = __raw_readl(bank->base +
1900 OMAP4_GPIO_RISINGDETECT);
1901 }
1902
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001903 bank->saved_fallingdetect = l1;
1904 bank->saved_risingdetect = l2;
1905 l1 &= ~bank->enabled_non_wakeup_gpios;
1906 l2 &= ~bank->enabled_non_wakeup_gpios;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001907
1908 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1909 __raw_writel(l1, bank->base +
1910 OMAP24XX_GPIO_FALLINGDETECT);
1911 __raw_writel(l2, bank->base +
1912 OMAP24XX_GPIO_RISINGDETECT);
1913 }
1914
1915 if (cpu_is_omap44xx()) {
1916 __raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
1917 __raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
1918 }
1919
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001920 c++;
1921 }
1922 if (!c) {
1923 workaround_enabled = 0;
1924 return;
1925 }
1926 workaround_enabled = 1;
1927}
1928
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001929void omap2_gpio_resume_after_idle(void)
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001930{
1931 int i;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001932 int min = 0;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001933
Tero Kristoa118b5f2008-12-22 14:27:12 +02001934 if (cpu_is_omap34xx())
1935 min = 1;
1936 for (i = min; i < gpio_bank_count; i++) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001937 struct gpio_bank *bank = &gpio_bank[i];
Sanjeev Premica828762010-09-23 18:27:18 -07001938 u32 l = 0, gen, gen0, gen1;
Kevin Hilman0aed04352010-09-22 16:06:27 -07001939 int j;
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001940
Kevin Hilman0aed04352010-09-22 16:06:27 -07001941 for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
Kevin Hilman8865b9b2009-01-27 11:15:34 -08001942 clk_enable(bank->dbck);
1943
Kevin Hilman43ffcd92009-01-27 11:09:24 -08001944 if (!workaround_enabled)
1945 continue;
1946
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001947 if (!(bank->enabled_non_wakeup_gpios))
1948 continue;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001949
1950 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1951 __raw_writel(bank->saved_fallingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001952 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001953 __raw_writel(bank->saved_risingdetect,
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001954 bank->base + OMAP24XX_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001955 l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
1956 }
1957
1958 if (cpu_is_omap44xx()) {
1959 __raw_writel(bank->saved_fallingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301960 bank->base + OMAP4_GPIO_FALLINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001961 __raw_writel(bank->saved_risingdetect,
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05301962 bank->base + OMAP4_GPIO_RISINGDETECT);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001963 l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
1964 }
1965
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001966 /* Check if any of the non-wakeup interrupt GPIOs have changed
1967 * state. If so, generate an IRQ by software. This is
1968 * horribly racy, but it's the best we can do to work around
1969 * this silicon bug. */
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001970 l ^= bank->saved_datain;
Tero Kristoa118b5f2008-12-22 14:27:12 +02001971 l &= bank->enabled_non_wakeup_gpios;
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001972
1973 /*
1974 * No need to generate IRQs for the rising edge for gpio IRQs
1975 * configured with falling edge only; and vice versa.
1976 */
1977 gen0 = l & bank->saved_fallingdetect;
1978 gen0 &= bank->saved_datain;
1979
1980 gen1 = l & bank->saved_risingdetect;
1981 gen1 &= ~(bank->saved_datain);
1982
1983 /* FIXME: Consider GPIO IRQs with level detections properly! */
1984 gen = l & (~(bank->saved_fallingdetect) &
1985 ~(bank->saved_risingdetect));
1986 /* Consider all GPIO IRQs needed to be updated */
1987 gen |= gen0 | gen1;
1988
1989 if (gen) {
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08001990 u32 old0, old1;
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001991
Sergio Aguirref00d6492010-03-03 16:21:08 +00001992 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Tony Lindgren3f1686a92010-02-15 09:27:25 -08001993 old0 = __raw_readl(bank->base +
1994 OMAP24XX_GPIO_LEVELDETECT0);
1995 old1 = __raw_readl(bank->base +
1996 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001997 __raw_writel(old0 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07001998 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00001999 __raw_writel(old1 | gen, bank->base +
Eero Nurkkala82dbb9d2009-08-28 10:51:36 -07002000 OMAP24XX_GPIO_LEVELDETECT1);
Sergio Aguirref00d6492010-03-03 16:21:08 +00002001 __raw_writel(old0, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002002 OMAP24XX_GPIO_LEVELDETECT0);
Sergio Aguirref00d6492010-03-03 16:21:08 +00002003 __raw_writel(old1, bank->base +
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002004 OMAP24XX_GPIO_LEVELDETECT1);
2005 }
2006
2007 if (cpu_is_omap44xx()) {
2008 old0 = __raw_readl(bank->base +
2009 OMAP4_GPIO_LEVELDETECT0);
2010 old1 = __raw_readl(bank->base +
Syed Rafiuddin78a1a6d2009-07-28 18:57:30 +05302011 OMAP4_GPIO_LEVELDETECT1);
Tony Lindgren3f1686a92010-02-15 09:27:25 -08002012 __raw_writel(old0 | l, bank->base +
2013 OMAP4_GPIO_LEVELDETECT0);
2014 __raw_writel(old1 | l, bank->base +
2015 OMAP4_GPIO_LEVELDETECT1);
2016 __raw_writel(old0, bank->base +
2017 OMAP4_GPIO_LEVELDETECT0);
2018 __raw_writel(old1, bank->base +
2019 OMAP4_GPIO_LEVELDETECT1);
2020 }
Juha Yrjola3ac4fa92006-12-06 17:13:52 -08002021 }
2022 }
2023
2024}
2025
Tony Lindgren92105bb2005-09-07 17:20:26 +01002026#endif
2027
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08002028#ifdef CONFIG_ARCH_OMAP3
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302029/* save the registers of bank 2-6 */
2030void omap_gpio_save_context(void)
2031{
2032 int i;
2033
2034 /* saving banks from 2-6 only since GPIO1 is in WKUP */
2035 for (i = 1; i < gpio_bank_count; i++) {
2036 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302037 gpio_context[i].irqenable1 =
2038 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
2039 gpio_context[i].irqenable2 =
2040 __raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
2041 gpio_context[i].wake_en =
2042 __raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
2043 gpio_context[i].ctrl =
2044 __raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
2045 gpio_context[i].oe =
2046 __raw_readl(bank->base + OMAP24XX_GPIO_OE);
2047 gpio_context[i].leveldetect0 =
2048 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
2049 gpio_context[i].leveldetect1 =
2050 __raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
2051 gpio_context[i].risingdetect =
2052 __raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
2053 gpio_context[i].fallingdetect =
2054 __raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
2055 gpio_context[i].dataout =
2056 __raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302057 }
2058}
2059
2060/* restore the required registers of bank 2-6 */
2061void omap_gpio_restore_context(void)
2062{
2063 int i;
2064
2065 for (i = 1; i < gpio_bank_count; i++) {
2066 struct gpio_bank *bank = &gpio_bank[i];
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302067 __raw_writel(gpio_context[i].irqenable1,
2068 bank->base + OMAP24XX_GPIO_IRQENABLE1);
2069 __raw_writel(gpio_context[i].irqenable2,
2070 bank->base + OMAP24XX_GPIO_IRQENABLE2);
2071 __raw_writel(gpio_context[i].wake_en,
2072 bank->base + OMAP24XX_GPIO_WAKE_EN);
2073 __raw_writel(gpio_context[i].ctrl,
2074 bank->base + OMAP24XX_GPIO_CTRL);
2075 __raw_writel(gpio_context[i].oe,
2076 bank->base + OMAP24XX_GPIO_OE);
2077 __raw_writel(gpio_context[i].leveldetect0,
2078 bank->base + OMAP24XX_GPIO_LEVELDETECT0);
2079 __raw_writel(gpio_context[i].leveldetect1,
2080 bank->base + OMAP24XX_GPIO_LEVELDETECT1);
2081 __raw_writel(gpio_context[i].risingdetect,
2082 bank->base + OMAP24XX_GPIO_RISINGDETECT);
2083 __raw_writel(gpio_context[i].fallingdetect,
2084 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
2085 __raw_writel(gpio_context[i].dataout,
2086 bank->base + OMAP24XX_GPIO_DATAOUT);
Rajendra Nayak40c670f2008-09-26 17:47:48 +05302087 }
2088}
2089#endif
2090
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08002091static struct platform_driver omap_gpio_driver = {
2092 .probe = omap_gpio_probe,
2093 .driver = {
2094 .name = "omap_gpio",
2095 },
2096};
2097
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002098/*
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08002099 * gpio driver register needs to be done before
2100 * machine_init functions access gpio APIs.
2101 * Hence omap_gpio_drv_reg() is a postcore_initcall.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002102 */
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08002103static int __init omap_gpio_drv_reg(void)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002104{
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08002105 return platform_driver_register(&omap_gpio_driver);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002106}
Varadarajan, Charulatha77640aa2010-12-07 16:26:57 -08002107postcore_initcall(omap_gpio_drv_reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002108
Tony Lindgren92105bb2005-09-07 17:20:26 +01002109static int __init omap_gpio_sysinit(void)
2110{
2111 int ret = 0;
2112
David Brownell11a78b72006-12-06 17:14:11 -08002113 mpuio_init();
2114
Tony Lindgren140455f2010-02-12 12:26:48 -08002115#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
Syed Mohammed, Khasim5492fb12007-11-29 16:15:11 -08002116 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
Tony Lindgren92105bb2005-09-07 17:20:26 +01002117 if (ret == 0) {
2118 ret = sysdev_class_register(&omap_gpio_sysclass);
2119 if (ret == 0)
2120 ret = sysdev_register(&omap_gpio_device);
2121 }
2122 }
2123#endif
2124
2125 return ret;
2126}
2127
Tony Lindgren92105bb2005-09-07 17:20:26 +01002128arch_initcall(omap_gpio_sysinit);