gpio.c 52.8 KB
Newer Older
1 2 3 4 5
/*
 *  linux/arch/arm/plat-omap/gpio.c
 *
 * Support functions for OMAP GPIO
 *
6
 * Copyright (C) 2003-2005 Nokia Corporation
7
 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
8
 *
9 10 11
 * Copyright (C) 2009 Texas Instruments
 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
 *
12 13 14 15 16 17 18 19
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
20 21
#include <linux/sysdev.h>
#include <linux/err.h>
22
#include <linux/clk.h>
23
#include <linux/io.h>
24 25
#include <linux/slab.h>
#include <linux/pm_runtime.h>
26

27
#include <mach/hardware.h>
28
#include <asm/irq.h>
29 30
#include <mach/irqs.h>
#include <mach/gpio.h>
31
#include <asm/mach/irq.h>
32
#include <plat/powerdomain.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

/*
 * OMAP1510 GPIO registers
 */
#define OMAP1510_GPIO_DATA_INPUT	0x00
#define OMAP1510_GPIO_DATA_OUTPUT	0x04
#define OMAP1510_GPIO_DIR_CONTROL	0x08
#define OMAP1510_GPIO_INT_CONTROL	0x0c
#define OMAP1510_GPIO_INT_MASK		0x10
#define OMAP1510_GPIO_INT_STATUS	0x14
#define OMAP1510_GPIO_PIN_CONTROL	0x18

#define OMAP1510_IH_GPIO_BASE		64

/*
 * OMAP1610 specific GPIO registers
 */
#define OMAP1610_GPIO_REVISION		0x0000
#define OMAP1610_GPIO_SYSCONFIG		0x0010
#define OMAP1610_GPIO_SYSSTATUS		0x0014
#define OMAP1610_GPIO_IRQSTATUS1	0x0018
#define OMAP1610_GPIO_IRQENABLE1	0x001c
55
#define OMAP1610_GPIO_WAKEUPENABLE	0x0028
56 57 58 59 60 61
#define OMAP1610_GPIO_DATAIN		0x002c
#define OMAP1610_GPIO_DATAOUT		0x0030
#define OMAP1610_GPIO_DIRECTION		0x0034
#define OMAP1610_GPIO_EDGE_CTRL1	0x0038
#define OMAP1610_GPIO_EDGE_CTRL2	0x003c
#define OMAP1610_GPIO_CLEAR_IRQENABLE1	0x009c
62
#define OMAP1610_GPIO_CLEAR_WAKEUPENA	0x00a8
63 64
#define OMAP1610_GPIO_CLEAR_DATAOUT	0x00b0
#define OMAP1610_GPIO_SET_IRQENABLE1	0x00dc
65
#define OMAP1610_GPIO_SET_WAKEUPENA	0x00e8
66 67 68
#define OMAP1610_GPIO_SET_DATAOUT	0x00f0

/*
69
 * OMAP7XX specific GPIO registers
70
 */
71 72 73 74 75 76
#define OMAP7XX_GPIO_DATA_INPUT		0x00
#define OMAP7XX_GPIO_DATA_OUTPUT	0x04
#define OMAP7XX_GPIO_DIR_CONTROL	0x08
#define OMAP7XX_GPIO_INT_CONTROL	0x0c
#define OMAP7XX_GPIO_INT_MASK		0x10
#define OMAP7XX_GPIO_INT_STATUS		0x14
77

78
/*
79
 * omap2+ specific GPIO registers
80 81 82
 */
#define OMAP24XX_GPIO_REVISION		0x0000
#define OMAP24XX_GPIO_IRQSTATUS1	0x0018
83 84
#define OMAP24XX_GPIO_IRQSTATUS2	0x0028
#define OMAP24XX_GPIO_IRQENABLE2	0x002c
85
#define OMAP24XX_GPIO_IRQENABLE1	0x001c
86
#define OMAP24XX_GPIO_WAKE_EN		0x0020
87 88 89 90 91 92 93 94
#define OMAP24XX_GPIO_CTRL		0x0030
#define OMAP24XX_GPIO_OE		0x0034
#define OMAP24XX_GPIO_DATAIN		0x0038
#define OMAP24XX_GPIO_DATAOUT		0x003c
#define OMAP24XX_GPIO_LEVELDETECT0	0x0040
#define OMAP24XX_GPIO_LEVELDETECT1	0x0044
#define OMAP24XX_GPIO_RISINGDETECT	0x0048
#define OMAP24XX_GPIO_FALLINGDETECT	0x004c
95 96
#define OMAP24XX_GPIO_DEBOUNCE_EN	0x0050
#define OMAP24XX_GPIO_DEBOUNCE_VAL	0x0054
97 98 99 100 101 102 103
#define OMAP24XX_GPIO_CLEARIRQENABLE1	0x0060
#define OMAP24XX_GPIO_SETIRQENABLE1	0x0064
#define OMAP24XX_GPIO_CLEARWKUENA	0x0080
#define OMAP24XX_GPIO_SETWKUENA		0x0084
#define OMAP24XX_GPIO_CLEARDATAOUT	0x0090
#define OMAP24XX_GPIO_SETDATAOUT	0x0094

104 105 106 107 108 109 110 111 112 113 114 115
#define OMAP4_GPIO_REVISION		0x0000
#define OMAP4_GPIO_EOI			0x0020
#define OMAP4_GPIO_IRQSTATUSRAW0	0x0024
#define OMAP4_GPIO_IRQSTATUSRAW1	0x0028
#define OMAP4_GPIO_IRQSTATUS0		0x002c
#define OMAP4_GPIO_IRQSTATUS1		0x0030
#define OMAP4_GPIO_IRQSTATUSSET0	0x0034
#define OMAP4_GPIO_IRQSTATUSSET1	0x0038
#define OMAP4_GPIO_IRQSTATUSCLR0	0x003c
#define OMAP4_GPIO_IRQSTATUSCLR1	0x0040
#define OMAP4_GPIO_IRQWAKEN0		0x0044
#define OMAP4_GPIO_IRQWAKEN1		0x0048
116 117 118 119
#define OMAP4_GPIO_IRQENABLE1		0x011c
#define OMAP4_GPIO_WAKE_EN		0x0120
#define OMAP4_GPIO_IRQSTATUS2		0x0128
#define OMAP4_GPIO_IRQENABLE2		0x012c
120 121 122 123 124 125 126 127 128 129
#define OMAP4_GPIO_CTRL			0x0130
#define OMAP4_GPIO_OE			0x0134
#define OMAP4_GPIO_DATAIN		0x0138
#define OMAP4_GPIO_DATAOUT		0x013c
#define OMAP4_GPIO_LEVELDETECT0		0x0140
#define OMAP4_GPIO_LEVELDETECT1		0x0144
#define OMAP4_GPIO_RISINGDETECT		0x0148
#define OMAP4_GPIO_FALLINGDETECT	0x014c
#define OMAP4_GPIO_DEBOUNCENABLE	0x0150
#define OMAP4_GPIO_DEBOUNCINGTIME	0x0154
130 131 132 133
#define OMAP4_GPIO_CLEARIRQENABLE1	0x0160
#define OMAP4_GPIO_SETIRQENABLE1	0x0164
#define OMAP4_GPIO_CLEARWKUENA		0x0180
#define OMAP4_GPIO_SETWKUENA		0x0184
134 135
#define OMAP4_GPIO_CLEARDATAOUT		0x0190
#define OMAP4_GPIO_SETDATAOUT		0x0194
136

137
struct gpio_bank {
T
Tony Lindgren 已提交
138
	unsigned long pbase;
139
	void __iomem *base;
140 141
	u16 irq;
	u16 virtual_irq_start;
142
	int method;
143
#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
144 145
	u32 suspend_wakeup;
	u32 saved_wakeup;
146 147 148 149 150 151 152
#endif
	u32 non_wakeup_gpios;
	u32 enabled_non_wakeup_gpios;

	u32 saved_datain;
	u32 saved_fallingdetect;
	u32 saved_risingdetect;
153
	u32 level_mask;
154
	u32 toggle_mask;
155
	spinlock_t lock;
D
David Brownell 已提交
156
	struct gpio_chip chip;
157
	struct clk *dbck;
C
Charulatha V 已提交
158
	u32 mod_usage;
159
	u32 dbck_enable_mask;
160 161
	struct device *dev;
	bool dbck_flag;
162
	int stride;
163 164
};

165
#ifdef CONFIG_ARCH_OMAP3
166 167 168 169 170 171 172 173 174 175 176
struct omap3_gpio_regs {
	u32 irqenable1;
	u32 irqenable2;
	u32 wake_en;
	u32 ctrl;
	u32 oe;
	u32 leveldetect0;
	u32 leveldetect1;
	u32 risingdetect;
	u32 fallingdetect;
	u32 dataout;
177 178
};

179
static struct omap3_gpio_regs gpio_context[OMAP34XX_NR_GPIOS];
180 181
#endif

182 183 184 185 186
/*
 * TODO: Cleanup gpio_bank usage as it is having information
 * related to all instances of the device
 */
static struct gpio_bank *gpio_bank;
187

188
static int bank_width;
189

190 191
/* TODO: Analyze removing gpio_bank_count usage from driver code */
int gpio_bank_count;
192 193 194

static inline struct gpio_bank *get_gpio_bank(int gpio)
{
195
	if (cpu_is_omap15xx()) {
196 197 198 199 200 201 202 203 204
		if (OMAP_GPIO_IS_MPUIO(gpio))
			return &gpio_bank[0];
		return &gpio_bank[1];
	}
	if (cpu_is_omap16xx()) {
		if (OMAP_GPIO_IS_MPUIO(gpio))
			return &gpio_bank[0];
		return &gpio_bank[1 + (gpio >> 4)];
	}
205
	if (cpu_is_omap7xx()) {
206 207 208 209
		if (OMAP_GPIO_IS_MPUIO(gpio))
			return &gpio_bank[0];
		return &gpio_bank[1 + (gpio >> 5)];
	}
210 211
	if (cpu_is_omap24xx())
		return &gpio_bank[gpio >> 5];
212
	if (cpu_is_omap34xx() || cpu_is_omap44xx())
213
		return &gpio_bank[gpio >> 5];
D
David Brownell 已提交
214 215
	BUG();
	return NULL;
216 217 218 219
}

static inline int get_gpio_index(int gpio)
{
220
	if (cpu_is_omap7xx())
221
		return gpio & 0x1f;
222 223
	if (cpu_is_omap24xx())
		return gpio & 0x1f;
224
	if (cpu_is_omap34xx() || cpu_is_omap44xx())
225
		return gpio & 0x1f;
226
	return gpio & 0x0f;
227 228 229 230 231 232
}

static inline int gpio_valid(int gpio)
{
	if (gpio < 0)
		return -1;
233
	if (cpu_class_is_omap1() && OMAP_GPIO_IS_MPUIO(gpio)) {
234
		if (gpio >= OMAP_MAX_GPIO_LINES + 16)
235 236 237
			return -1;
		return 0;
	}
238
	if (cpu_is_omap15xx() && gpio < 16)
239 240 241
		return 0;
	if ((cpu_is_omap16xx()) && gpio < 64)
		return 0;
242
	if (cpu_is_omap7xx() && gpio < 192)
243
		return 0;
244 245 246
	if (cpu_is_omap2420() && gpio < 128)
		return 0;
	if (cpu_is_omap2430() && gpio < 160)
247
		return 0;
248
	if ((cpu_is_omap34xx() || cpu_is_omap44xx()) && gpio < 192)
249
		return 0;
250 251 252 253 254
	return -1;
}

static int check_gpio(int gpio)
{
R
Roel Kluin 已提交
255
	if (unlikely(gpio_valid(gpio) < 0)) {
256 257 258 259 260 261 262 263 264
		printk(KERN_ERR "omap-gpio: invalid GPIO %d\n", gpio);
		dump_stack();
		return -1;
	}
	return 0;
}

static void _set_gpio_direction(struct gpio_bank *bank, int gpio, int is_input)
{
265
	void __iomem *reg = bank->base;
266 267 268
	u32 l;

	switch (bank->method) {
269
#ifdef CONFIG_ARCH_OMAP1
270
	case METHOD_MPUIO:
271
		reg += OMAP_MPUIO_IO_CNTL / bank->stride;
272
		break;
273 274
#endif
#ifdef CONFIG_ARCH_OMAP15XX
275 276 277
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_DIR_CONTROL;
		break;
278 279
#endif
#ifdef CONFIG_ARCH_OMAP16XX
280 281 282
	case METHOD_GPIO_1610:
		reg += OMAP1610_GPIO_DIRECTION;
		break;
283
#endif
284
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
285 286
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_DIR_CONTROL;
287 288
		break;
#endif
289
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
290 291 292
	case METHOD_GPIO_24XX:
		reg += OMAP24XX_GPIO_OE;
		break;
293 294
#endif
#if defined(CONFIG_ARCH_OMAP4)
295
	case METHOD_GPIO_44XX:
296 297
		reg += OMAP4_GPIO_OE;
		break;
298 299 300 301
#endif
	default:
		WARN_ON(1);
		return;
302 303 304 305 306 307 308 309 310 311 312
	}
	l = __raw_readl(reg);
	if (is_input)
		l |= 1 << gpio;
	else
		l &= ~(1 << gpio);
	__raw_writel(l, reg);
}

static void _set_gpio_dataout(struct gpio_bank *bank, int gpio, int enable)
{
313
	void __iomem *reg = bank->base;
314 315 316
	u32 l = 0;

	switch (bank->method) {
317
#ifdef CONFIG_ARCH_OMAP1
318
	case METHOD_MPUIO:
319
		reg += OMAP_MPUIO_OUTPUT / bank->stride;
320 321 322 323 324 325
		l = __raw_readl(reg);
		if (enable)
			l |= 1 << gpio;
		else
			l &= ~(1 << gpio);
		break;
326 327
#endif
#ifdef CONFIG_ARCH_OMAP15XX
328 329 330 331 332 333 334 335
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_DATA_OUTPUT;
		l = __raw_readl(reg);
		if (enable)
			l |= 1 << gpio;
		else
			l &= ~(1 << gpio);
		break;
336 337
#endif
#ifdef CONFIG_ARCH_OMAP16XX
338 339 340 341 342 343 344
	case METHOD_GPIO_1610:
		if (enable)
			reg += OMAP1610_GPIO_SET_DATAOUT;
		else
			reg += OMAP1610_GPIO_CLEAR_DATAOUT;
		l = 1 << gpio;
		break;
345
#endif
346
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
347 348
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_DATA_OUTPUT;
349 350 351 352 353 354 355
		l = __raw_readl(reg);
		if (enable)
			l |= 1 << gpio;
		else
			l &= ~(1 << gpio);
		break;
#endif
356
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
357 358 359 360 361 362 363
	case METHOD_GPIO_24XX:
		if (enable)
			reg += OMAP24XX_GPIO_SETDATAOUT;
		else
			reg += OMAP24XX_GPIO_CLEARDATAOUT;
		l = 1 << gpio;
		break;
364 365
#endif
#ifdef CONFIG_ARCH_OMAP4
366
	case METHOD_GPIO_44XX:
367 368 369 370 371 372
		if (enable)
			reg += OMAP4_GPIO_SETDATAOUT;
		else
			reg += OMAP4_GPIO_CLEARDATAOUT;
		l = 1 << gpio;
		break;
373
#endif
374
	default:
375
		WARN_ON(1);
376 377 378 379 380
		return;
	}
	__raw_writel(l, reg);
}

381
static int _get_gpio_datain(struct gpio_bank *bank, int gpio)
382
{
383
	void __iomem *reg;
384 385

	if (check_gpio(gpio) < 0)
386
		return -EINVAL;
387 388
	reg = bank->base;
	switch (bank->method) {
389
#ifdef CONFIG_ARCH_OMAP1
390
	case METHOD_MPUIO:
391
		reg += OMAP_MPUIO_INPUT_LATCH / bank->stride;
392
		break;
393 394
#endif
#ifdef CONFIG_ARCH_OMAP15XX
395 396 397
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_DATA_INPUT;
		break;
398 399
#endif
#ifdef CONFIG_ARCH_OMAP16XX
400 401 402
	case METHOD_GPIO_1610:
		reg += OMAP1610_GPIO_DATAIN;
		break;
403
#endif
404
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
405 406
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_DATA_INPUT;
407 408
		break;
#endif
409
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
410 411 412
	case METHOD_GPIO_24XX:
		reg += OMAP24XX_GPIO_DATAIN;
		break;
413 414
#endif
#ifdef CONFIG_ARCH_OMAP4
415
	case METHOD_GPIO_44XX:
416 417
		reg += OMAP4_GPIO_DATAIN;
		break;
418
#endif
419
	default:
420
		return -EINVAL;
421
	}
422 423
	return (__raw_readl(reg)
			& (1 << get_gpio_index(gpio))) != 0;
424 425
}

426 427 428 429 430 431 432 433 434 435 436
static int _get_gpio_dataout(struct gpio_bank *bank, int gpio)
{
	void __iomem *reg;

	if (check_gpio(gpio) < 0)
		return -EINVAL;
	reg = bank->base;

	switch (bank->method) {
#ifdef CONFIG_ARCH_OMAP1
	case METHOD_MPUIO:
437
		reg += OMAP_MPUIO_OUTPUT / bank->stride;
438 439 440 441 442 443 444 445 446 447 448 449
		break;
#endif
#ifdef CONFIG_ARCH_OMAP15XX
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_DATA_OUTPUT;
		break;
#endif
#ifdef CONFIG_ARCH_OMAP16XX
	case METHOD_GPIO_1610:
		reg += OMAP1610_GPIO_DATAOUT;
		break;
#endif
450
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
451 452
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_DATA_OUTPUT;
453 454
		break;
#endif
455
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
456 457 458
	case METHOD_GPIO_24XX:
		reg += OMAP24XX_GPIO_DATAOUT;
		break;
459 460 461 462 463
#endif
#ifdef CONFIG_ARCH_OMAP4
	case METHOD_GPIO_44XX:
		reg += OMAP4_GPIO_DATAOUT;
		break;
464 465 466 467 468 469 470 471
#endif
	default:
		return -EINVAL;
	}

	return (__raw_readl(reg) & (1 << get_gpio_index(gpio))) != 0;
}

472 473 474 475 476 477 478 479
#define MOD_REG_BIT(reg, bit_mask, set)	\
do {	\
	int l = __raw_readl(base + reg); \
	if (set) l |= bit_mask; \
	else l &= ~bit_mask; \
	__raw_writel(l, base + reg); \
} while(0)

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
/**
 * _set_gpio_debounce - low level gpio debounce time
 * @bank: the gpio bank we're acting upon
 * @gpio: the gpio number on this @gpio
 * @debounce: debounce time to use
 *
 * OMAP's debounce time is in 31us steps so we need
 * to convert and round up to the closest unit.
 */
static void _set_gpio_debounce(struct gpio_bank *bank, unsigned gpio,
		unsigned debounce)
{
	void __iomem		*reg = bank->base;
	u32			val;
	u32			l;

496 497 498
	if (!bank->dbck_flag)
		return;

499 500 501 502 503 504 505 506 507
	if (debounce < 32)
		debounce = 0x01;
	else if (debounce > 7936)
		debounce = 0xff;
	else
		debounce = (debounce / 0x1f) - 1;

	l = 1 << get_gpio_index(gpio);

508
	if (bank->method == METHOD_GPIO_44XX)
509 510 511 512 513 514 515
		reg += OMAP4_GPIO_DEBOUNCINGTIME;
	else
		reg += OMAP24XX_GPIO_DEBOUNCE_VAL;

	__raw_writel(debounce, reg);

	reg = bank->base;
516
	if (bank->method == METHOD_GPIO_44XX)
517 518 519 520 521 522 523 524
		reg += OMAP4_GPIO_DEBOUNCENABLE;
	else
		reg += OMAP24XX_GPIO_DEBOUNCE_EN;

	val = __raw_readl(reg);

	if (debounce) {
		val |= l;
525
		clk_enable(bank->dbck);
526 527
	} else {
		val &= ~l;
528
		clk_disable(bank->dbck);
529
	}
530
	bank->dbck_enable_mask = val;
531 532 533 534

	__raw_writel(val, reg);
}

535
#ifdef CONFIG_ARCH_OMAP2PLUS
536 537
static inline void set_24xx_gpio_triggering(struct gpio_bank *bank, int gpio,
						int trigger)
538
{
539
	void __iomem *base = bank->base;
540
	u32 gpio_bit = 1 << gpio;
541
	u32 val;
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	if (cpu_is_omap44xx()) {
		MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT0, gpio_bit,
			trigger & IRQ_TYPE_LEVEL_LOW);
		MOD_REG_BIT(OMAP4_GPIO_LEVELDETECT1, gpio_bit,
			trigger & IRQ_TYPE_LEVEL_HIGH);
		MOD_REG_BIT(OMAP4_GPIO_RISINGDETECT, gpio_bit,
			trigger & IRQ_TYPE_EDGE_RISING);
		MOD_REG_BIT(OMAP4_GPIO_FALLINGDETECT, gpio_bit,
			trigger & IRQ_TYPE_EDGE_FALLING);
	} else {
		MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT0, gpio_bit,
			trigger & IRQ_TYPE_LEVEL_LOW);
		MOD_REG_BIT(OMAP24XX_GPIO_LEVELDETECT1, gpio_bit,
			trigger & IRQ_TYPE_LEVEL_HIGH);
		MOD_REG_BIT(OMAP24XX_GPIO_RISINGDETECT, gpio_bit,
			trigger & IRQ_TYPE_EDGE_RISING);
		MOD_REG_BIT(OMAP24XX_GPIO_FALLINGDETECT, gpio_bit,
			trigger & IRQ_TYPE_EDGE_FALLING);
	}
562
	if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
563 564 565 566 567 568 569 570 571 572 573
		if (cpu_is_omap44xx()) {
			if (trigger != 0)
				__raw_writel(1 << gpio, bank->base+
						OMAP4_GPIO_IRQWAKEN0);
			else {
				val = __raw_readl(bank->base +
							OMAP4_GPIO_IRQWAKEN0);
				__raw_writel(val & (~(1 << gpio)), bank->base +
							 OMAP4_GPIO_IRQWAKEN0);
			}
		} else {
574 575 576 577 578
			/*
			 * GPIO wakeup request can only be generated on edge
			 * transitions
			 */
			if (trigger & IRQ_TYPE_EDGE_BOTH)
579
				__raw_writel(1 << gpio, bank->base
580
					+ OMAP24XX_GPIO_SETWKUENA);
581 582
			else
				__raw_writel(1 << gpio, bank->base
583
					+ OMAP24XX_GPIO_CLEARWKUENA);
584
		}
T
Tero Kristo 已提交
585 586 587
	}
	/* This part needs to be executed always for OMAP34xx */
	if (cpu_is_omap34xx() || (bank->non_wakeup_gpios & gpio_bit)) {
588 589 590 591 592 593 594
		/*
		 * Log the edge gpio and manually trigger the IRQ
		 * after resume if the input level changes
		 * to avoid irq lost during PER RET/OFF mode
		 * Applies for omap2 non-wakeup gpio and all omap3 gpios
		 */
		if (trigger & IRQ_TYPE_EDGE_BOTH)
595 596 597 598
			bank->enabled_non_wakeup_gpios |= gpio_bit;
		else
			bank->enabled_non_wakeup_gpios &= ~gpio_bit;
	}
599

600 601 602 603 604 605 606 607 608
	if (cpu_is_omap44xx()) {
		bank->level_mask =
			__raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT0) |
			__raw_readl(bank->base + OMAP4_GPIO_LEVELDETECT1);
	} else {
		bank->level_mask =
			__raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0) |
			__raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
	}
609
}
610
#endif
611

612
#ifdef CONFIG_ARCH_OMAP1
613 614 615 616 617 618 619 620 621 622 623
/*
 * This only applies to chips that can't do both rising and falling edge
 * detection at once.  For all other chips, this function is a noop.
 */
static void _toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
{
	void __iomem *reg = bank->base;
	u32 l = 0;

	switch (bank->method) {
	case METHOD_MPUIO:
624
		reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		break;
#ifdef CONFIG_ARCH_OMAP15XX
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_INT_CONTROL;
		break;
#endif
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_INT_CONTROL;
		break;
#endif
	default:
		return;
	}

	l = __raw_readl(reg);
	if ((l >> gpio) & 1)
		l &= ~(1 << gpio);
	else
		l |= 1 << gpio;

	__raw_writel(l, reg);
}
648
#endif
649

650 651 652 653
static int _set_gpio_triggering(struct gpio_bank *bank, int gpio, int trigger)
{
	void __iomem *reg = bank->base;
	u32 l = 0;
654 655

	switch (bank->method) {
656
#ifdef CONFIG_ARCH_OMAP1
657
	case METHOD_MPUIO:
658
		reg += OMAP_MPUIO_GPIO_INT_EDGE / bank->stride;
659
		l = __raw_readl(reg);
660
		if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
661
			bank->toggle_mask |= 1 << gpio;
662
		if (trigger & IRQ_TYPE_EDGE_RISING)
663
			l |= 1 << gpio;
664
		else if (trigger & IRQ_TYPE_EDGE_FALLING)
665
			l &= ~(1 << gpio);
666 667
		else
			goto bad;
668
		break;
669 670
#endif
#ifdef CONFIG_ARCH_OMAP15XX
671 672 673
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_INT_CONTROL;
		l = __raw_readl(reg);
674
		if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
675
			bank->toggle_mask |= 1 << gpio;
676
		if (trigger & IRQ_TYPE_EDGE_RISING)
677
			l |= 1 << gpio;
678
		else if (trigger & IRQ_TYPE_EDGE_FALLING)
679
			l &= ~(1 << gpio);
680 681
		else
			goto bad;
682
		break;
683
#endif
684
#ifdef CONFIG_ARCH_OMAP16XX
685 686 687 688 689 690 691 692
	case METHOD_GPIO_1610:
		if (gpio & 0x08)
			reg += OMAP1610_GPIO_EDGE_CTRL2;
		else
			reg += OMAP1610_GPIO_EDGE_CTRL1;
		gpio &= 0x07;
		l = __raw_readl(reg);
		l &= ~(3 << (gpio << 1));
693
		if (trigger & IRQ_TYPE_EDGE_RISING)
694
			l |= 2 << (gpio << 1);
695
		if (trigger & IRQ_TYPE_EDGE_FALLING)
696
			l |= 1 << (gpio << 1);
697 698 699 700 701
		if (trigger)
			/* Enable wake-up during idle for dynamic tick */
			__raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_SET_WAKEUPENA);
		else
			__raw_writel(1 << gpio, bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA);
702
		break;
703
#endif
704
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
705 706
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_INT_CONTROL;
707
		l = __raw_readl(reg);
708
		if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
709
			bank->toggle_mask |= 1 << gpio;
710 711 712 713 714 715 716 717
		if (trigger & IRQ_TYPE_EDGE_RISING)
			l |= 1 << gpio;
		else if (trigger & IRQ_TYPE_EDGE_FALLING)
			l &= ~(1 << gpio);
		else
			goto bad;
		break;
#endif
718
#ifdef CONFIG_ARCH_OMAP2PLUS
719
	case METHOD_GPIO_24XX:
720
	case METHOD_GPIO_44XX:
721
		set_24xx_gpio_triggering(bank, gpio, trigger);
722
		break;
723
#endif
724
	default:
725
		goto bad;
726
	}
727 728 729 730
	__raw_writel(l, reg);
	return 0;
bad:
	return -EINVAL;
731 732
}

733
static int gpio_irq_type(unsigned irq, unsigned type)
734 735
{
	struct gpio_bank *bank;
736 737
	unsigned gpio;
	int retval;
D
David Brownell 已提交
738
	unsigned long flags;
739

740
	if (!cpu_class_is_omap2() && irq > IH_MPUIO_BASE)
741 742 743
		gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
	else
		gpio = irq - IH_GPIO_BASE;
744 745

	if (check_gpio(gpio) < 0)
746 747
		return -EINVAL;

748
	if (type & ~IRQ_TYPE_SENSE_MASK)
749
		return -EINVAL;
750 751

	/* OMAP1 allows only only edge triggering */
752
	if (!cpu_class_is_omap2()
753
			&& (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
754 755
		return -EINVAL;

756
	bank = get_irq_chip_data(irq);
D
David Brownell 已提交
757
	spin_lock_irqsave(&bank->lock, flags);
758
	retval = _set_gpio_triggering(bank, get_gpio_index(gpio), type);
759 760 761 762
	if (retval == 0) {
		irq_desc[irq].status &= ~IRQ_TYPE_SENSE_MASK;
		irq_desc[irq].status |= type;
	}
D
David Brownell 已提交
763
	spin_unlock_irqrestore(&bank->lock, flags);
764 765 766 767 768 769

	if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
		__set_irq_handler_unlocked(irq, handle_level_irq);
	else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
		__set_irq_handler_unlocked(irq, handle_edge_irq);

770
	return retval;
771 772 773 774
}

static void _clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
{
775
	void __iomem *reg = bank->base;
776 777

	switch (bank->method) {
778
#ifdef CONFIG_ARCH_OMAP1
779 780 781 782
	case METHOD_MPUIO:
		/* MPUIO irqstatus is reset by reading the status register,
		 * so do nothing here */
		return;
783 784
#endif
#ifdef CONFIG_ARCH_OMAP15XX
785 786 787
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_INT_STATUS;
		break;
788 789
#endif
#ifdef CONFIG_ARCH_OMAP16XX
790 791 792
	case METHOD_GPIO_1610:
		reg += OMAP1610_GPIO_IRQSTATUS1;
		break;
793
#endif
794
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
795 796
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_INT_STATUS;
797 798
		break;
#endif
799
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
800 801 802
	case METHOD_GPIO_24XX:
		reg += OMAP24XX_GPIO_IRQSTATUS1;
		break;
803 804
#endif
#if defined(CONFIG_ARCH_OMAP4)
805
	case METHOD_GPIO_44XX:
806 807
		reg += OMAP4_GPIO_IRQSTATUS0;
		break;
808
#endif
809
	default:
810
		WARN_ON(1);
811 812 813
		return;
	}
	__raw_writel(gpio_mask, reg);
814 815

	/* Workaround for clearing DSP GPIO interrupts to allow retention */
816 817 818 819 820
	if (cpu_is_omap24xx() || cpu_is_omap34xx())
		reg = bank->base + OMAP24XX_GPIO_IRQSTATUS2;
	else if (cpu_is_omap44xx())
		reg = bank->base + OMAP4_GPIO_IRQSTATUS1;

821
	if (cpu_is_omap24xx() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
822 823 824 825
		__raw_writel(gpio_mask, reg);

	/* Flush posted write for the irq status to avoid spurious interrupts */
	__raw_readl(reg);
826
	}
827 828 829 830 831 832 833
}

static inline void _clear_gpio_irqstatus(struct gpio_bank *bank, int gpio)
{
	_clear_gpio_irqbank(bank, 1 << get_gpio_index(gpio));
}

834 835 836
static u32 _get_gpio_irqbank_mask(struct gpio_bank *bank)
{
	void __iomem *reg = bank->base;
837 838 839
	int inv = 0;
	u32 l;
	u32 mask;
840 841

	switch (bank->method) {
842
#ifdef CONFIG_ARCH_OMAP1
843
	case METHOD_MPUIO:
844
		reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
845 846
		mask = 0xffff;
		inv = 1;
847
		break;
848 849
#endif
#ifdef CONFIG_ARCH_OMAP15XX
850 851
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_INT_MASK;
852 853
		mask = 0xffff;
		inv = 1;
854
		break;
855 856
#endif
#ifdef CONFIG_ARCH_OMAP16XX
857 858
	case METHOD_GPIO_1610:
		reg += OMAP1610_GPIO_IRQENABLE1;
859
		mask = 0xffff;
860
		break;
861
#endif
862
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
863 864
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_INT_MASK;
865 866 867 868
		mask = 0xffffffff;
		inv = 1;
		break;
#endif
869
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
870 871
	case METHOD_GPIO_24XX:
		reg += OMAP24XX_GPIO_IRQENABLE1;
872
		mask = 0xffffffff;
873
		break;
874 875
#endif
#if defined(CONFIG_ARCH_OMAP4)
876
	case METHOD_GPIO_44XX:
877 878 879
		reg += OMAP4_GPIO_IRQSTATUSSET0;
		mask = 0xffffffff;
		break;
880
#endif
881
	default:
882
		WARN_ON(1);
883 884 885
		return 0;
	}

886 887 888 889 890
	l = __raw_readl(reg);
	if (inv)
		l = ~l;
	l &= mask;
	return l;
891 892
}

893 894
static void _enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask, int enable)
{
895
	void __iomem *reg = bank->base;
896 897 898
	u32 l;

	switch (bank->method) {
899
#ifdef CONFIG_ARCH_OMAP1
900
	case METHOD_MPUIO:
901
		reg += OMAP_MPUIO_GPIO_MASKIT / bank->stride;
902 903 904 905 906 907
		l = __raw_readl(reg);
		if (enable)
			l &= ~(gpio_mask);
		else
			l |= gpio_mask;
		break;
908 909
#endif
#ifdef CONFIG_ARCH_OMAP15XX
910 911 912 913 914 915 916 917
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_INT_MASK;
		l = __raw_readl(reg);
		if (enable)
			l &= ~(gpio_mask);
		else
			l |= gpio_mask;
		break;
918 919
#endif
#ifdef CONFIG_ARCH_OMAP16XX
920 921 922 923 924 925 926
	case METHOD_GPIO_1610:
		if (enable)
			reg += OMAP1610_GPIO_SET_IRQENABLE1;
		else
			reg += OMAP1610_GPIO_CLEAR_IRQENABLE1;
		l = gpio_mask;
		break;
927
#endif
928
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
929 930
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_INT_MASK;
931 932 933 934 935 936 937
		l = __raw_readl(reg);
		if (enable)
			l &= ~(gpio_mask);
		else
			l |= gpio_mask;
		break;
#endif
938
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
939 940 941 942 943 944 945
	case METHOD_GPIO_24XX:
		if (enable)
			reg += OMAP24XX_GPIO_SETIRQENABLE1;
		else
			reg += OMAP24XX_GPIO_CLEARIRQENABLE1;
		l = gpio_mask;
		break;
946 947
#endif
#ifdef CONFIG_ARCH_OMAP4
948
	case METHOD_GPIO_44XX:
949 950 951 952 953 954
		if (enable)
			reg += OMAP4_GPIO_IRQSTATUSSET0;
		else
			reg += OMAP4_GPIO_IRQSTATUSCLR0;
		l = gpio_mask;
		break;
955
#endif
956
	default:
957
		WARN_ON(1);
958 959 960 961 962 963 964 965 966 967
		return;
	}
	__raw_writel(l, reg);
}

static inline void _set_gpio_irqenable(struct gpio_bank *bank, int gpio, int enable)
{
	_enable_gpio_irqbank(bank, 1 << get_gpio_index(gpio), enable);
}

968 969 970 971 972 973 974 975 976 977
/*
 * Note that ENAWAKEUP needs to be enabled in GPIO_SYSCONFIG register.
 * 1510 does not seem to have a wake-up register. If JTAG is connected
 * to the target, system will wake up always on GPIO events. While
 * system is running all registered GPIO interrupts need to have wake-up
 * enabled. When system is suspended, only selected GPIO interrupts need
 * to have wake-up enabled.
 */
static int _set_gpio_wakeup(struct gpio_bank *bank, int gpio, int enable)
{
978
	unsigned long uninitialized_var(flags);
D
David Brownell 已提交
979

980
	switch (bank->method) {
981
#ifdef CONFIG_ARCH_OMAP16XX
D
David Brownell 已提交
982
	case METHOD_MPUIO:
983
	case METHOD_GPIO_1610:
D
David Brownell 已提交
984
		spin_lock_irqsave(&bank->lock, flags);
985
		if (enable)
986
			bank->suspend_wakeup |= (1 << gpio);
987
		else
988
			bank->suspend_wakeup &= ~(1 << gpio);
D
David Brownell 已提交
989
		spin_unlock_irqrestore(&bank->lock, flags);
990
		return 0;
991
#endif
992
#ifdef CONFIG_ARCH_OMAP2PLUS
993
	case METHOD_GPIO_24XX:
994
	case METHOD_GPIO_44XX:
D
David Brownell 已提交
995 996 997 998 999 1000
		if (bank->non_wakeup_gpios & (1 << gpio)) {
			printk(KERN_ERR "Unable to modify wakeup on "
					"non-wakeup GPIO%d\n",
					(bank - gpio_bank) * 32 + gpio);
			return -EINVAL;
		}
D
David Brownell 已提交
1001
		spin_lock_irqsave(&bank->lock, flags);
1002
		if (enable)
1003
			bank->suspend_wakeup |= (1 << gpio);
1004
		else
1005
			bank->suspend_wakeup &= ~(1 << gpio);
D
David Brownell 已提交
1006
		spin_unlock_irqrestore(&bank->lock, flags);
1007 1008
		return 0;
#endif
1009 1010 1011 1012 1013 1014 1015
	default:
		printk(KERN_ERR "Can't enable GPIO wakeup for method %i\n",
		       bank->method);
		return -EINVAL;
	}
}

1016 1017 1018 1019 1020
static void _reset_gpio(struct gpio_bank *bank, int gpio)
{
	_set_gpio_direction(bank, get_gpio_index(gpio), 1);
	_set_gpio_irqenable(bank, gpio, 0);
	_clear_gpio_irqstatus(bank, gpio);
1021
	_set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
1022 1023
}

1024 1025 1026 1027 1028 1029 1030 1031 1032
/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
static int gpio_wake_enable(unsigned int irq, unsigned int enable)
{
	unsigned int gpio = irq - IH_GPIO_BASE;
	struct gpio_bank *bank;
	int retval;

	if (check_gpio(gpio) < 0)
		return -ENODEV;
1033
	bank = get_irq_chip_data(irq);
1034 1035 1036 1037 1038
	retval = _set_gpio_wakeup(bank, get_gpio_index(gpio), enable);

	return retval;
}

1039
static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
1040
{
1041
	struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
D
David Brownell 已提交
1042
	unsigned long flags;
D
David Brownell 已提交
1043

D
David Brownell 已提交
1044
	spin_lock_irqsave(&bank->lock, flags);
1045

1046 1047 1048
	/* Set trigger to none. You need to enable the desired trigger with
	 * request_irq() or set_irq_type().
	 */
1049
	_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
1050

1051
#ifdef CONFIG_ARCH_OMAP15XX
1052
	if (bank->method == METHOD_GPIO_1510) {
1053
		void __iomem *reg;
1054

1055
		/* Claim the pin for MPU */
1056
		reg = bank->base + OMAP1510_GPIO_PIN_CONTROL;
1057
		__raw_writel(__raw_readl(reg) | (1 << offset), reg);
1058 1059
	}
#endif
C
Charulatha V 已提交
1060 1061
	if (!cpu_class_is_omap1()) {
		if (!bank->mod_usage) {
1062
			void __iomem *reg = bank->base;
C
Charulatha V 已提交
1063
			u32 ctrl;
1064 1065 1066 1067 1068 1069

			if (cpu_is_omap24xx() || cpu_is_omap34xx())
				reg += OMAP24XX_GPIO_CTRL;
			else if (cpu_is_omap44xx())
				reg += OMAP4_GPIO_CTRL;
			ctrl = __raw_readl(reg);
C
Charulatha V 已提交
1070
			/* Module is enabled, clocks are not gated */
1071 1072
			ctrl &= 0xFFFFFFFE;
			__raw_writel(ctrl, reg);
C
Charulatha V 已提交
1073 1074 1075
		}
		bank->mod_usage |= 1 << offset;
	}
D
David Brownell 已提交
1076
	spin_unlock_irqrestore(&bank->lock, flags);
1077 1078 1079 1080

	return 0;
}

1081
static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
1082
{
1083
	struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
D
David Brownell 已提交
1084
	unsigned long flags;
1085

D
David Brownell 已提交
1086
	spin_lock_irqsave(&bank->lock, flags);
1087 1088 1089 1090
#ifdef CONFIG_ARCH_OMAP16XX
	if (bank->method == METHOD_GPIO_1610) {
		/* Disable wake-up during idle for dynamic tick */
		void __iomem *reg = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
1091
		__raw_writel(1 << offset, reg);
1092 1093
	}
#endif
1094 1095
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
	if (bank->method == METHOD_GPIO_24XX) {
1096 1097
		/* Disable wake-up during idle for dynamic tick */
		void __iomem *reg = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
1098
		__raw_writel(1 << offset, reg);
1099
	}
1100 1101 1102 1103 1104 1105 1106
#endif
#ifdef CONFIG_ARCH_OMAP4
	if (bank->method == METHOD_GPIO_44XX) {
		/* Disable wake-up during idle for dynamic tick */
		void __iomem *reg = bank->base + OMAP4_GPIO_IRQWAKEN0;
		__raw_writel(1 << offset, reg);
	}
1107
#endif
C
Charulatha V 已提交
1108 1109 1110
	if (!cpu_class_is_omap1()) {
		bank->mod_usage &= ~(1 << offset);
		if (!bank->mod_usage) {
1111
			void __iomem *reg = bank->base;
C
Charulatha V 已提交
1112
			u32 ctrl;
1113 1114 1115 1116 1117 1118

			if (cpu_is_omap24xx() || cpu_is_omap34xx())
				reg += OMAP24XX_GPIO_CTRL;
			else if (cpu_is_omap44xx())
				reg += OMAP4_GPIO_CTRL;
			ctrl = __raw_readl(reg);
C
Charulatha V 已提交
1119 1120
			/* Module is disabled, clocks are gated */
			ctrl |= 1;
1121
			__raw_writel(ctrl, reg);
C
Charulatha V 已提交
1122 1123
		}
	}
1124
	_reset_gpio(bank, bank->chip.base + offset);
D
David Brownell 已提交
1125
	spin_unlock_irqrestore(&bank->lock, flags);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
}

/*
 * We need to unmask the GPIO bank interrupt as soon as possible to
 * avoid missing GPIO interrupts for other lines in the bank.
 * Then we need to mask-read-clear-unmask the triggered GPIO lines
 * in the bank to avoid missing nested interrupts for a GPIO line.
 * If we wait to unmask individual GPIO lines in the bank after the
 * line's interrupt handler has been run, we may miss some nested
 * interrupts.
 */
1137
static void gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
1138
{
1139
	void __iomem *isr_reg = NULL;
1140
	u32 isr;
1141
	unsigned int gpio_irq, gpio_index;
1142
	struct gpio_bank *bank;
1143 1144
	u32 retrigger = 0;
	int unmasked = 0;
1145 1146 1147

	desc->chip->ack(irq);

1148
	bank = get_irq_data(irq);
1149
#ifdef CONFIG_ARCH_OMAP1
1150
	if (bank->method == METHOD_MPUIO)
1151 1152
		isr_reg = bank->base +
				OMAP_MPUIO_GPIO_INT / bank->stride;
1153
#endif
1154
#ifdef CONFIG_ARCH_OMAP15XX
1155 1156 1157 1158 1159 1160 1161
	if (bank->method == METHOD_GPIO_1510)
		isr_reg = bank->base + OMAP1510_GPIO_INT_STATUS;
#endif
#if defined(CONFIG_ARCH_OMAP16XX)
	if (bank->method == METHOD_GPIO_1610)
		isr_reg = bank->base + OMAP1610_GPIO_IRQSTATUS1;
#endif
1162
#if defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP850)
1163 1164
	if (bank->method == METHOD_GPIO_7XX)
		isr_reg = bank->base + OMAP7XX_GPIO_INT_STATUS;
1165
#endif
1166
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1167 1168
	if (bank->method == METHOD_GPIO_24XX)
		isr_reg = bank->base + OMAP24XX_GPIO_IRQSTATUS1;
1169 1170
#endif
#if defined(CONFIG_ARCH_OMAP4)
1171
	if (bank->method == METHOD_GPIO_44XX)
1172
		isr_reg = bank->base + OMAP4_GPIO_IRQSTATUS0;
1173
#endif
1174 1175 1176 1177

	if (WARN_ON(!isr_reg))
		goto exit;

1178
	while(1) {
1179
		u32 isr_saved, level_mask = 0;
1180
		u32 enabled;
1181

1182 1183
		enabled = _get_gpio_irqbank_mask(bank);
		isr_saved = isr = __raw_readl(isr_reg) & enabled;
1184 1185 1186 1187

		if (cpu_is_omap15xx() && (bank->method == METHOD_MPUIO))
			isr &= 0x0000ffff;

1188
		if (cpu_class_is_omap2()) {
1189
			level_mask = bank->level_mask & enabled;
1190
		}
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

		/* clear edge sensitive interrupts before handler(s) are
		called so that we don't miss any interrupt occurred while
		executing them */
		_enable_gpio_irqbank(bank, isr_saved & ~level_mask, 0);
		_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
		_enable_gpio_irqbank(bank, isr_saved & ~level_mask, 1);

		/* if there is only edge sensitive GPIO pin interrupts
		configured, we could unmask GPIO bank interrupt immediately */
1201 1202
		if (!level_mask && !unmasked) {
			unmasked = 1;
1203
			desc->chip->unmask(irq);
1204
		}
1205

1206 1207
		isr |= retrigger;
		retrigger = 0;
1208 1209 1210 1211 1212
		if (!isr)
			break;

		gpio_irq = bank->virtual_irq_start;
		for (; isr != 0; isr >>= 1, gpio_irq++) {
1213 1214
			gpio_index = get_gpio_index(irq_to_gpio(gpio_irq));

1215 1216
			if (!(isr & 1))
				continue;
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
#ifdef CONFIG_ARCH_OMAP1
			/*
			 * Some chips can't respond to both rising and falling
			 * at the same time.  If this irq was requested with
			 * both flags, we need to flip the ICR data for the IRQ
			 * to respond to the IRQ for the opposite direction.
			 * This will be indicated in the bank toggle_mask.
			 */
			if (bank->toggle_mask & (1 << gpio_index))
				_toggle_gpio_edge_triggering(bank, gpio_index);
#endif

1230
			generic_handle_irq(gpio_irq);
1231
		}
1232
	}
1233 1234 1235 1236
	/* if bank has any level sensitive GPIO pin interrupt
	configured, we must unmask the bank interrupt only after
	handler(s) are executed in order to avoid spurious bank
	interrupt */
1237
exit:
1238 1239 1240
	if (!unmasked)
		desc->chip->unmask(irq);

1241 1242
}

1243 1244 1245
static void gpio_irq_shutdown(unsigned int irq)
{
	unsigned int gpio = irq - IH_GPIO_BASE;
1246
	struct gpio_bank *bank = get_irq_chip_data(irq);
1247 1248 1249 1250

	_reset_gpio(bank, gpio);
}

1251 1252 1253
static void gpio_ack_irq(unsigned int irq)
{
	unsigned int gpio = irq - IH_GPIO_BASE;
1254
	struct gpio_bank *bank = get_irq_chip_data(irq);
1255 1256 1257 1258 1259 1260 1261

	_clear_gpio_irqstatus(bank, gpio);
}

static void gpio_mask_irq(unsigned int irq)
{
	unsigned int gpio = irq - IH_GPIO_BASE;
1262
	struct gpio_bank *bank = get_irq_chip_data(irq);
1263 1264

	_set_gpio_irqenable(bank, gpio, 0);
1265
	_set_gpio_triggering(bank, get_gpio_index(gpio), IRQ_TYPE_NONE);
1266 1267 1268 1269 1270
}

static void gpio_unmask_irq(unsigned int irq)
{
	unsigned int gpio = irq - IH_GPIO_BASE;
1271
	struct gpio_bank *bank = get_irq_chip_data(irq);
1272
	unsigned int irq_mask = 1 << get_gpio_index(gpio);
1273 1274 1275 1276 1277
	struct irq_desc *desc = irq_to_desc(irq);
	u32 trigger = desc->status & IRQ_TYPE_SENSE_MASK;

	if (trigger)
		_set_gpio_triggering(bank, get_gpio_index(gpio), trigger);
1278 1279 1280 1281 1282 1283 1284

	/* For level-triggered GPIOs, the clearing must be done after
	 * the HW source is cleared, thus after the handler has run */
	if (bank->level_mask & irq_mask) {
		_set_gpio_irqenable(bank, gpio, 0);
		_clear_gpio_irqstatus(bank, gpio);
	}
1285

K
Kevin Hilman 已提交
1286
	_set_gpio_irqenable(bank, gpio, 1);
1287 1288
}

1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
static struct irq_chip gpio_irq_chip = {
	.name		= "GPIO",
	.shutdown	= gpio_irq_shutdown,
	.ack		= gpio_ack_irq,
	.mask		= gpio_mask_irq,
	.unmask		= gpio_unmask_irq,
	.set_type	= gpio_irq_type,
	.set_wake	= gpio_wake_enable,
};

/*---------------------------------------------------------------------*/

#ifdef CONFIG_ARCH_OMAP1

/* MPUIO uses the always-on 32k clock */

1305 1306 1307 1308 1309 1310 1311 1312
static void mpuio_ack_irq(unsigned int irq)
{
	/* The ISR is reset automatically, so do nothing here. */
}

static void mpuio_mask_irq(unsigned int irq)
{
	unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1313
	struct gpio_bank *bank = get_irq_chip_data(irq);
1314 1315 1316 1317 1318 1319 1320

	_set_gpio_irqenable(bank, gpio, 0);
}

static void mpuio_unmask_irq(unsigned int irq)
{
	unsigned int gpio = OMAP_MPUIO(irq - IH_MPUIO_BASE);
1321
	struct gpio_bank *bank = get_irq_chip_data(irq);
1322 1323 1324 1325

	_set_gpio_irqenable(bank, gpio, 1);
}

1326 1327 1328 1329 1330
static struct irq_chip mpuio_irq_chip = {
	.name		= "MPUIO",
	.ack		= mpuio_ack_irq,
	.mask		= mpuio_mask_irq,
	.unmask		= mpuio_unmask_irq,
1331
	.set_type	= gpio_irq_type,
D
David Brownell 已提交
1332 1333 1334 1335
#ifdef CONFIG_ARCH_OMAP16XX
	/* REVISIT: assuming only 16xx supports MPUIO wake events */
	.set_wake	= gpio_wake_enable,
#endif
1336 1337
};

1338 1339 1340

#define bank_is_mpuio(bank)	((bank)->method == METHOD_MPUIO)

D
David Brownell 已提交
1341 1342 1343 1344 1345

#ifdef CONFIG_ARCH_OMAP16XX

#include <linux/platform_device.h>

1346
static int omap_mpuio_suspend_noirq(struct device *dev)
D
David Brownell 已提交
1347
{
1348
	struct platform_device *pdev = to_platform_device(dev);
D
David Brownell 已提交
1349
	struct gpio_bank	*bank = platform_get_drvdata(pdev);
1350 1351
	void __iomem		*mask_reg = bank->base +
					OMAP_MPUIO_GPIO_MASKIT / bank->stride;
D
David Brownell 已提交
1352
	unsigned long		flags;
D
David Brownell 已提交
1353

D
David Brownell 已提交
1354
	spin_lock_irqsave(&bank->lock, flags);
D
David Brownell 已提交
1355 1356
	bank->saved_wakeup = __raw_readl(mask_reg);
	__raw_writel(0xffff & ~bank->suspend_wakeup, mask_reg);
D
David Brownell 已提交
1357
	spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
1358 1359 1360 1361

	return 0;
}

1362
static int omap_mpuio_resume_noirq(struct device *dev)
D
David Brownell 已提交
1363
{
1364
	struct platform_device *pdev = to_platform_device(dev);
D
David Brownell 已提交
1365
	struct gpio_bank	*bank = platform_get_drvdata(pdev);
1366 1367
	void __iomem		*mask_reg = bank->base +
					OMAP_MPUIO_GPIO_MASKIT / bank->stride;
D
David Brownell 已提交
1368
	unsigned long		flags;
D
David Brownell 已提交
1369

D
David Brownell 已提交
1370
	spin_lock_irqsave(&bank->lock, flags);
D
David Brownell 已提交
1371
	__raw_writel(bank->saved_wakeup, mask_reg);
D
David Brownell 已提交
1372
	spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
1373 1374 1375 1376

	return 0;
}

1377
static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
1378 1379 1380 1381
	.suspend_noirq = omap_mpuio_suspend_noirq,
	.resume_noirq = omap_mpuio_resume_noirq,
};

D
David Brownell 已提交
1382 1383 1384 1385 1386 1387
/* use platform_driver for this, now that there's no longer any
 * point to sys_device (other than not disturbing old code).
 */
static struct platform_driver omap_mpuio_driver = {
	.driver		= {
		.name	= "mpuio",
1388
		.pm	= &omap_mpuio_dev_pm_ops,
D
David Brownell 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
	},
};

static struct platform_device omap_mpuio_device = {
	.name		= "mpuio",
	.id		= -1,
	.dev = {
		.driver = &omap_mpuio_driver.driver,
	}
	/* could list the /proc/iomem resources */
};

static inline void mpuio_init(void)
{
1403 1404
	struct gpio_bank *bank = get_gpio_bank(OMAP_MPUIO(0));
	platform_set_drvdata(&omap_mpuio_device, bank);
1405

D
David Brownell 已提交
1406 1407 1408 1409 1410 1411 1412 1413
	if (platform_driver_register(&omap_mpuio_driver) == 0)
		(void) platform_device_register(&omap_mpuio_device);
}

#else
static inline void mpuio_init(void) {}
#endif	/* 16xx */

1414 1415 1416 1417 1418
#else

extern struct irq_chip mpuio_irq_chip;

#define bank_is_mpuio(bank)	0
D
David Brownell 已提交
1419
static inline void mpuio_init(void) {}
1420 1421 1422 1423

#endif

/*---------------------------------------------------------------------*/
1424

D
David Brownell 已提交
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
/* REVISIT these are stupid implementations!  replace by ones that
 * don't switch on METHOD_* and which mostly avoid spinlocks
 */

static int gpio_input(struct gpio_chip *chip, unsigned offset)
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
	spin_lock_irqsave(&bank->lock, flags);
	_set_gpio_direction(bank, offset, 1);
	spin_unlock_irqrestore(&bank->lock, flags);
	return 0;
}

1441 1442 1443 1444 1445 1446
static int gpio_is_input(struct gpio_bank *bank, int mask)
{
	void __iomem *reg = bank->base;

	switch (bank->method) {
	case METHOD_MPUIO:
1447
		reg += OMAP_MPUIO_IO_CNTL / bank->stride;
1448 1449 1450 1451 1452 1453 1454
		break;
	case METHOD_GPIO_1510:
		reg += OMAP1510_GPIO_DIR_CONTROL;
		break;
	case METHOD_GPIO_1610:
		reg += OMAP1610_GPIO_DIRECTION;
		break;
1455 1456
	case METHOD_GPIO_7XX:
		reg += OMAP7XX_GPIO_DIR_CONTROL;
1457 1458 1459 1460
		break;
	case METHOD_GPIO_24XX:
		reg += OMAP24XX_GPIO_OE;
		break;
1461 1462 1463 1464 1465 1466
	case METHOD_GPIO_44XX:
		reg += OMAP4_GPIO_OE;
		break;
	default:
		WARN_ONCE(1, "gpio_is_input: incorrect OMAP GPIO method");
		return -EINVAL;
1467 1468 1469 1470
	}
	return __raw_readl(reg) & mask;
}

D
David Brownell 已提交
1471 1472
static int gpio_get(struct gpio_chip *chip, unsigned offset)
{
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
	struct gpio_bank *bank;
	void __iomem *reg;
	int gpio;
	u32 mask;

	gpio = chip->base + offset;
	bank = get_gpio_bank(gpio);
	reg = bank->base;
	mask = 1 << get_gpio_index(gpio);

	if (gpio_is_input(bank, mask))
		return _get_gpio_datain(bank, gpio);
	else
		return _get_gpio_dataout(bank, gpio);
D
David Brownell 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
}

static int gpio_output(struct gpio_chip *chip, unsigned offset, int value)
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
	spin_lock_irqsave(&bank->lock, flags);
	_set_gpio_dataout(bank, offset, value);
	_set_gpio_direction(bank, offset, 0);
	spin_unlock_irqrestore(&bank->lock, flags);
	return 0;
}

1502 1503 1504 1505 1506 1507 1508
static int gpio_debounce(struct gpio_chip *chip, unsigned offset,
		unsigned debounce)
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
1509 1510 1511 1512 1513 1514 1515

	if (!bank->dbck) {
		bank->dbck = clk_get(bank->dev, "dbclk");
		if (IS_ERR(bank->dbck))
			dev_err(bank->dev, "Could not get gpio dbck\n");
	}

1516 1517 1518 1519 1520 1521 1522
	spin_lock_irqsave(&bank->lock, flags);
	_set_gpio_debounce(bank, offset, debounce);
	spin_unlock_irqrestore(&bank->lock, flags);

	return 0;
}

D
David Brownell 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
static void gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
	spin_lock_irqsave(&bank->lock, flags);
	_set_gpio_dataout(bank, offset, value);
	spin_unlock_irqrestore(&bank->lock, flags);
}

1534 1535 1536 1537 1538 1539 1540 1541
static int gpio_2irq(struct gpio_chip *chip, unsigned offset)
{
	struct gpio_bank *bank;

	bank = container_of(chip, struct gpio_bank, chip);
	return bank->virtual_irq_start + offset;
}

D
David Brownell 已提交
1542 1543
/*---------------------------------------------------------------------*/

1544
static void __init omap_gpio_show_rev(struct gpio_bank *bank)
T
Tony Lindgren 已提交
1545 1546 1547
{
	u32 rev;

1548 1549
	if (cpu_is_omap16xx() && !(bank->method != METHOD_MPUIO))
		rev = __raw_readw(bank->base + OMAP1610_GPIO_REVISION);
T
Tony Lindgren 已提交
1550
	else if (cpu_is_omap24xx() || cpu_is_omap34xx())
1551
		rev = __raw_readl(bank->base + OMAP24XX_GPIO_REVISION);
T
Tony Lindgren 已提交
1552
	else if (cpu_is_omap44xx())
1553
		rev = __raw_readl(bank->base + OMAP4_GPIO_REVISION);
T
Tony Lindgren 已提交
1554 1555 1556 1557 1558 1559 1560
	else
		return;

	printk(KERN_INFO "OMAP GPIO hardware version %d.%d\n",
		(rev >> 4) & 0x0f, rev & 0x0f);
}

1561 1562 1563 1564 1565
/* This lock class tells lockdep that GPIO irqs are in a different
 * category than their parents, so it won't report false recursion.
 */
static struct lock_class_key gpio_lock_class;

1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
static inline int init_gpio_info(struct platform_device *pdev)
{
	/* TODO: Analyze removing gpio_bank_count usage from driver code */
	gpio_bank = kzalloc(gpio_bank_count * sizeof(struct gpio_bank),
				GFP_KERNEL);
	if (!gpio_bank) {
		dev_err(&pdev->dev, "Memory alloc failed for gpio_bank\n");
		return -ENOMEM;
	}
	return 0;
}

/* TODO: Cleanup cpu_is_* checks */
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
static void omap_gpio_mod_init(struct gpio_bank *bank, int id)
{
	if (cpu_class_is_omap2()) {
		if (cpu_is_omap44xx()) {
			__raw_writel(0xffffffff, bank->base +
					OMAP4_GPIO_IRQSTATUSCLR0);
			__raw_writel(0x00000000, bank->base +
					 OMAP4_GPIO_DEBOUNCENABLE);
			/* Initialize interface clk ungated, module enabled */
			__raw_writel(0, bank->base + OMAP4_GPIO_CTRL);
		} else if (cpu_is_omap34xx()) {
			__raw_writel(0x00000000, bank->base +
					OMAP24XX_GPIO_IRQENABLE1);
			__raw_writel(0xffffffff, bank->base +
					OMAP24XX_GPIO_IRQSTATUS1);
			__raw_writel(0x00000000, bank->base +
					OMAP24XX_GPIO_DEBOUNCE_EN);

			/* Initialize interface clk ungated, module enabled */
			__raw_writel(0, bank->base + OMAP24XX_GPIO_CTRL);
		} else if (cpu_is_omap24xx()) {
			static const u32 non_wakeup_gpios[] = {
				0xe203ffc0, 0x08700040
			};
			if (id < ARRAY_SIZE(non_wakeup_gpios))
				bank->non_wakeup_gpios = non_wakeup_gpios[id];
		}
	} else if (cpu_class_is_omap1()) {
		if (bank_is_mpuio(bank))
1608 1609
			__raw_writew(0xffff, bank->base +
				OMAP_MPUIO_GPIO_MASKIT / bank->stride);
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
		if (cpu_is_omap15xx() && bank->method == METHOD_GPIO_1510) {
			__raw_writew(0xffff, bank->base
						+ OMAP1510_GPIO_INT_MASK);
			__raw_writew(0x0000, bank->base
						+ OMAP1510_GPIO_INT_STATUS);
		}
		if (cpu_is_omap16xx() && bank->method == METHOD_GPIO_1610) {
			__raw_writew(0x0000, bank->base
						+ OMAP1610_GPIO_IRQENABLE1);
			__raw_writew(0xffff, bank->base
						+ OMAP1610_GPIO_IRQSTATUS1);
			__raw_writew(0x0014, bank->base
						+ OMAP1610_GPIO_SYSCONFIG);

			/*
			 * Enable system clock for GPIO module.
			 * The CAM_CLK_CTRL *is* really the right place.
			 */
			omap_writel(omap_readl(ULPD_CAM_CLK_CTRL) | 0x04,
						ULPD_CAM_CLK_CTRL);
		}
		if (cpu_is_omap7xx() && bank->method == METHOD_GPIO_7XX) {
			__raw_writel(0xffffffff, bank->base
						+ OMAP7XX_GPIO_INT_MASK);
			__raw_writel(0x00000000, bank->base
						+ OMAP7XX_GPIO_INT_STATUS);
		}
	}
}

static void __init omap_gpio_chip_init(struct gpio_bank *bank)
{
1642
	int j;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	static int gpio;

	bank->mod_usage = 0;
	/*
	 * REVISIT eventually switch from OMAP-specific gpio structs
	 * over to the generic ones
	 */
	bank->chip.request = omap_gpio_request;
	bank->chip.free = omap_gpio_free;
	bank->chip.direction_input = gpio_input;
	bank->chip.get = gpio_get;
	bank->chip.direction_output = gpio_output;
	bank->chip.set_debounce = gpio_debounce;
	bank->chip.set = gpio_set;
	bank->chip.to_irq = gpio_2irq;
	if (bank_is_mpuio(bank)) {
		bank->chip.label = "mpuio";
#ifdef CONFIG_ARCH_OMAP16XX
		bank->chip.dev = &omap_mpuio_device.dev;
#endif
		bank->chip.base = OMAP_MPUIO(0);
	} else {
		bank->chip.label = "gpio";
		bank->chip.base = gpio;
		gpio += bank_width;
	}
	bank->chip.ngpio = bank_width;

	gpiochip_add(&bank->chip);

	for (j = bank->virtual_irq_start;
		     j < bank->virtual_irq_start + bank_width; j++) {
		lockdep_set_class(&irq_desc[j].lock, &gpio_lock_class);
		set_irq_chip_data(j, bank);
		if (bank_is_mpuio(bank))
			set_irq_chip(j, &mpuio_irq_chip);
		else
			set_irq_chip(j, &gpio_irq_chip);
		set_irq_handler(j, handle_simple_irq);
		set_irq_flags(j, IRQF_VALID);
	}
	set_irq_chained_handler(bank->irq, gpio_irq_handler);
	set_irq_data(bank->irq, bank);
}

1688
static int __devinit omap_gpio_probe(struct platform_device *pdev)
1689
{
1690 1691 1692 1693
	static int gpio_init_done;
	struct omap_gpio_platform_data *pdata;
	struct resource *res;
	int id;
1694 1695
	struct gpio_bank *bank;

1696 1697
	if (!pdev->dev.platform_data)
		return -EINVAL;
1698

1699
	pdata = pdev->dev.platform_data;
1700

1701 1702
	if (!gpio_init_done) {
		int ret;
1703

1704 1705 1706
		ret = init_gpio_info(pdev);
		if (ret)
			return ret;
1707 1708
	}

1709 1710
	id = pdev->id;
	bank = &gpio_bank[id];
1711

1712 1713 1714 1715
	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (unlikely(!res)) {
		dev_err(&pdev->dev, "GPIO Bank %i Invalid IRQ resource\n", id);
		return -ENODEV;
1716
	}
1717

1718 1719 1720 1721 1722
	bank->irq = res->start;
	bank->virtual_irq_start = pdata->virtual_irq_start;
	bank->method = pdata->bank_type;
	bank->dev = &pdev->dev;
	bank->dbck_flag = pdata->dbck_flag;
1723
	bank->stride = pdata->bank_stride;
1724
	bank_width = pdata->bank_width;
T
Tony Lindgren 已提交
1725

1726
	spin_lock_init(&bank->lock);
T
Tony Lindgren 已提交
1727

1728 1729 1730 1731 1732 1733
	/* Static mapping, never released */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (unlikely(!res)) {
		dev_err(&pdev->dev, "GPIO Bank %i Invalid mem resource\n", id);
		return -ENODEV;
	}
1734

1735 1736 1737 1738
	bank->base = ioremap(res->start, resource_size(res));
	if (!bank->base) {
		dev_err(&pdev->dev, "Could not ioremap gpio bank%i\n", id);
		return -ENOMEM;
1739 1740
	}

1741 1742 1743 1744 1745
	pm_runtime_enable(bank->dev);
	pm_runtime_get_sync(bank->dev);

	omap_gpio_mod_init(bank, id);
	omap_gpio_chip_init(bank);
1746
	omap_gpio_show_rev(bank);
T
Tony Lindgren 已提交
1747

1748 1749 1750
	if (!gpio_init_done)
		gpio_init_done = 1;

1751 1752 1753
	return 0;
}

1754
#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
1755 1756 1757 1758
static int omap_gpio_suspend(struct sys_device *dev, pm_message_t mesg)
{
	int i;

1759
	if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
1760 1761 1762 1763 1764 1765 1766
		return 0;

	for (i = 0; i < gpio_bank_count; i++) {
		struct gpio_bank *bank = &gpio_bank[i];
		void __iomem *wake_status;
		void __iomem *wake_clear;
		void __iomem *wake_set;
D
David Brownell 已提交
1767
		unsigned long flags;
1768 1769

		switch (bank->method) {
1770
#ifdef CONFIG_ARCH_OMAP16XX
1771 1772 1773 1774 1775
		case METHOD_GPIO_1610:
			wake_status = bank->base + OMAP1610_GPIO_WAKEUPENABLE;
			wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
			wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
			break;
1776
#endif
1777
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1778
		case METHOD_GPIO_24XX:
1779
			wake_status = bank->base + OMAP24XX_GPIO_WAKE_EN;
1780 1781 1782
			wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
			wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
			break;
1783 1784
#endif
#ifdef CONFIG_ARCH_OMAP4
1785
		case METHOD_GPIO_44XX:
1786 1787 1788 1789
			wake_status = bank->base + OMAP4_GPIO_IRQWAKEN0;
			wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
			wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
			break;
1790
#endif
1791 1792 1793 1794
		default:
			continue;
		}

D
David Brownell 已提交
1795
		spin_lock_irqsave(&bank->lock, flags);
1796 1797 1798
		bank->saved_wakeup = __raw_readl(wake_status);
		__raw_writel(0xffffffff, wake_clear);
		__raw_writel(bank->suspend_wakeup, wake_set);
D
David Brownell 已提交
1799
		spin_unlock_irqrestore(&bank->lock, flags);
1800 1801 1802 1803 1804 1805 1806 1807 1808
	}

	return 0;
}

static int omap_gpio_resume(struct sys_device *dev)
{
	int i;

1809
	if (!cpu_class_is_omap2() && !cpu_is_omap16xx())
1810 1811 1812 1813 1814 1815
		return 0;

	for (i = 0; i < gpio_bank_count; i++) {
		struct gpio_bank *bank = &gpio_bank[i];
		void __iomem *wake_clear;
		void __iomem *wake_set;
D
David Brownell 已提交
1816
		unsigned long flags;
1817 1818

		switch (bank->method) {
1819
#ifdef CONFIG_ARCH_OMAP16XX
1820 1821 1822 1823
		case METHOD_GPIO_1610:
			wake_clear = bank->base + OMAP1610_GPIO_CLEAR_WAKEUPENA;
			wake_set = bank->base + OMAP1610_GPIO_SET_WAKEUPENA;
			break;
1824
#endif
1825
#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1826
		case METHOD_GPIO_24XX:
1827 1828
			wake_clear = bank->base + OMAP24XX_GPIO_CLEARWKUENA;
			wake_set = bank->base + OMAP24XX_GPIO_SETWKUENA;
1829
			break;
1830 1831
#endif
#ifdef CONFIG_ARCH_OMAP4
1832
		case METHOD_GPIO_44XX:
1833 1834 1835
			wake_clear = bank->base + OMAP4_GPIO_IRQWAKEN0;
			wake_set = bank->base + OMAP4_GPIO_IRQWAKEN0;
			break;
1836
#endif
1837 1838 1839 1840
		default:
			continue;
		}

D
David Brownell 已提交
1841
		spin_lock_irqsave(&bank->lock, flags);
1842 1843
		__raw_writel(0xffffffff, wake_clear);
		__raw_writel(bank->saved_wakeup, wake_set);
D
David Brownell 已提交
1844
		spin_unlock_irqrestore(&bank->lock, flags);
1845 1846 1847 1848 1849 1850
	}

	return 0;
}

static struct sysdev_class omap_gpio_sysclass = {
1851
	.name		= "gpio",
1852 1853 1854 1855 1856 1857 1858 1859
	.suspend	= omap_gpio_suspend,
	.resume		= omap_gpio_resume,
};

static struct sys_device omap_gpio_device = {
	.id		= 0,
	.cls		= &omap_gpio_sysclass,
};
1860 1861 1862

#endif

1863
#ifdef CONFIG_ARCH_OMAP2PLUS
1864 1865 1866

static int workaround_enabled;

1867
void omap2_gpio_prepare_for_idle(int power_state)
1868 1869
{
	int i, c = 0;
T
Tero Kristo 已提交
1870
	int min = 0;
1871

T
Tero Kristo 已提交
1872 1873
	if (cpu_is_omap34xx())
		min = 1;
1874

T
Tero Kristo 已提交
1875
	for (i = min; i < gpio_bank_count; i++) {
1876
		struct gpio_bank *bank = &gpio_bank[i];
1877
		u32 l1 = 0, l2 = 0;
1878
		int j;
1879

1880
		for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
1881 1882
			clk_disable(bank->dbck);

1883 1884 1885 1886 1887 1888
		if (power_state > PWRDM_POWER_OFF)
			continue;

		/* If going to OFF, remove triggering for all
		 * non-wakeup GPIOs.  Otherwise spurious IRQs will be
		 * generated.  See OMAP2420 Errata item 1.101. */
1889 1890
		if (!(bank->enabled_non_wakeup_gpios))
			continue;
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909

		if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
			bank->saved_datain = __raw_readl(bank->base +
					OMAP24XX_GPIO_DATAIN);
			l1 = __raw_readl(bank->base +
					OMAP24XX_GPIO_FALLINGDETECT);
			l2 = __raw_readl(bank->base +
					OMAP24XX_GPIO_RISINGDETECT);
		}

		if (cpu_is_omap44xx()) {
			bank->saved_datain = __raw_readl(bank->base +
						OMAP4_GPIO_DATAIN);
			l1 = __raw_readl(bank->base +
						OMAP4_GPIO_FALLINGDETECT);
			l2 = __raw_readl(bank->base +
						OMAP4_GPIO_RISINGDETECT);
		}

1910 1911 1912 1913
		bank->saved_fallingdetect = l1;
		bank->saved_risingdetect = l2;
		l1 &= ~bank->enabled_non_wakeup_gpios;
		l2 &= ~bank->enabled_non_wakeup_gpios;
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

		if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
			__raw_writel(l1, bank->base +
					OMAP24XX_GPIO_FALLINGDETECT);
			__raw_writel(l2, bank->base +
					OMAP24XX_GPIO_RISINGDETECT);
		}

		if (cpu_is_omap44xx()) {
			__raw_writel(l1, bank->base + OMAP4_GPIO_FALLINGDETECT);
			__raw_writel(l2, bank->base + OMAP4_GPIO_RISINGDETECT);
		}

1927 1928 1929 1930 1931 1932 1933 1934 1935
		c++;
	}
	if (!c) {
		workaround_enabled = 0;
		return;
	}
	workaround_enabled = 1;
}

1936
void omap2_gpio_resume_after_idle(void)
1937 1938
{
	int i;
T
Tero Kristo 已提交
1939
	int min = 0;
1940

T
Tero Kristo 已提交
1941 1942 1943
	if (cpu_is_omap34xx())
		min = 1;
	for (i = min; i < gpio_bank_count; i++) {
1944
		struct gpio_bank *bank = &gpio_bank[i];
1945
		u32 l = 0, gen, gen0, gen1;
1946
		int j;
1947

1948
		for (j = 0; j < hweight_long(bank->dbck_enable_mask); j++)
1949 1950
			clk_enable(bank->dbck);

1951 1952 1953
		if (!workaround_enabled)
			continue;

1954 1955
		if (!(bank->enabled_non_wakeup_gpios))
			continue;
1956 1957 1958

		if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
			__raw_writel(bank->saved_fallingdetect,
1959
				 bank->base + OMAP24XX_GPIO_FALLINGDETECT);
1960
			__raw_writel(bank->saved_risingdetect,
1961
				 bank->base + OMAP24XX_GPIO_RISINGDETECT);
1962 1963 1964 1965 1966
			l = __raw_readl(bank->base + OMAP24XX_GPIO_DATAIN);
		}

		if (cpu_is_omap44xx()) {
			__raw_writel(bank->saved_fallingdetect,
1967
				 bank->base + OMAP4_GPIO_FALLINGDETECT);
1968
			__raw_writel(bank->saved_risingdetect,
1969
				 bank->base + OMAP4_GPIO_RISINGDETECT);
1970 1971 1972
			l = __raw_readl(bank->base + OMAP4_GPIO_DATAIN);
		}

1973 1974 1975 1976 1977
		/* Check if any of the non-wakeup interrupt GPIOs have changed
		 * state.  If so, generate an IRQ by software.  This is
		 * horribly racy, but it's the best we can do to work around
		 * this silicon bug. */
		l ^= bank->saved_datain;
T
Tero Kristo 已提交
1978
		l &= bank->enabled_non_wakeup_gpios;
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996

		/*
		 * No need to generate IRQs for the rising edge for gpio IRQs
		 * configured with falling edge only; and vice versa.
		 */
		gen0 = l & bank->saved_fallingdetect;
		gen0 &= bank->saved_datain;

		gen1 = l & bank->saved_risingdetect;
		gen1 &= ~(bank->saved_datain);

		/* FIXME: Consider GPIO IRQs with level detections properly! */
		gen = l & (~(bank->saved_fallingdetect) &
				~(bank->saved_risingdetect));
		/* Consider all GPIO IRQs needed to be updated */
		gen |= gen0 | gen1;

		if (gen) {
1997
			u32 old0, old1;
1998

1999
			if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
2000 2001 2002 2003
				old0 = __raw_readl(bank->base +
					OMAP24XX_GPIO_LEVELDETECT0);
				old1 = __raw_readl(bank->base +
					OMAP24XX_GPIO_LEVELDETECT1);
2004
				__raw_writel(old0 | gen, bank->base +
2005
					OMAP24XX_GPIO_LEVELDETECT0);
2006
				__raw_writel(old1 | gen, bank->base +
2007
					OMAP24XX_GPIO_LEVELDETECT1);
2008
				__raw_writel(old0, bank->base +
2009
					OMAP24XX_GPIO_LEVELDETECT0);
2010
				__raw_writel(old1, bank->base +
2011 2012 2013 2014 2015
					OMAP24XX_GPIO_LEVELDETECT1);
			}

			if (cpu_is_omap44xx()) {
				old0 = __raw_readl(bank->base +
2016
						OMAP4_GPIO_LEVELDETECT0);
2017
				old1 = __raw_readl(bank->base +
2018
						OMAP4_GPIO_LEVELDETECT1);
2019
				__raw_writel(old0 | l, bank->base +
2020
						OMAP4_GPIO_LEVELDETECT0);
2021
				__raw_writel(old1 | l, bank->base +
2022
						OMAP4_GPIO_LEVELDETECT1);
2023
				__raw_writel(old0, bank->base +
2024
						OMAP4_GPIO_LEVELDETECT0);
2025
				__raw_writel(old1, bank->base +
2026
						OMAP4_GPIO_LEVELDETECT1);
2027
			}
2028 2029 2030 2031 2032
		}
	}

}

2033 2034
#endif

2035
#ifdef CONFIG_ARCH_OMAP3
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
/* save the registers of bank 2-6 */
void omap_gpio_save_context(void)
{
	int i;

	/* saving banks from 2-6 only since GPIO1 is in WKUP */
	for (i = 1; i < gpio_bank_count; i++) {
		struct gpio_bank *bank = &gpio_bank[i];
		gpio_context[i].irqenable1 =
			__raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE1);
		gpio_context[i].irqenable2 =
			__raw_readl(bank->base + OMAP24XX_GPIO_IRQENABLE2);
		gpio_context[i].wake_en =
			__raw_readl(bank->base + OMAP24XX_GPIO_WAKE_EN);
		gpio_context[i].ctrl =
			__raw_readl(bank->base + OMAP24XX_GPIO_CTRL);
		gpio_context[i].oe =
			__raw_readl(bank->base + OMAP24XX_GPIO_OE);
		gpio_context[i].leveldetect0 =
			__raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT0);
		gpio_context[i].leveldetect1 =
			__raw_readl(bank->base + OMAP24XX_GPIO_LEVELDETECT1);
		gpio_context[i].risingdetect =
			__raw_readl(bank->base + OMAP24XX_GPIO_RISINGDETECT);
		gpio_context[i].fallingdetect =
			__raw_readl(bank->base + OMAP24XX_GPIO_FALLINGDETECT);
		gpio_context[i].dataout =
			__raw_readl(bank->base + OMAP24XX_GPIO_DATAOUT);
	}
}

/* restore the required registers of bank 2-6 */
void omap_gpio_restore_context(void)
{
	int i;

	for (i = 1; i < gpio_bank_count; i++) {
		struct gpio_bank *bank = &gpio_bank[i];
		__raw_writel(gpio_context[i].irqenable1,
				bank->base + OMAP24XX_GPIO_IRQENABLE1);
		__raw_writel(gpio_context[i].irqenable2,
				bank->base + OMAP24XX_GPIO_IRQENABLE2);
		__raw_writel(gpio_context[i].wake_en,
				bank->base + OMAP24XX_GPIO_WAKE_EN);
		__raw_writel(gpio_context[i].ctrl,
				bank->base + OMAP24XX_GPIO_CTRL);
		__raw_writel(gpio_context[i].oe,
				bank->base + OMAP24XX_GPIO_OE);
		__raw_writel(gpio_context[i].leveldetect0,
				bank->base + OMAP24XX_GPIO_LEVELDETECT0);
		__raw_writel(gpio_context[i].leveldetect1,
				bank->base + OMAP24XX_GPIO_LEVELDETECT1);
		__raw_writel(gpio_context[i].risingdetect,
				bank->base + OMAP24XX_GPIO_RISINGDETECT);
		__raw_writel(gpio_context[i].fallingdetect,
				bank->base + OMAP24XX_GPIO_FALLINGDETECT);
		__raw_writel(gpio_context[i].dataout,
				bank->base + OMAP24XX_GPIO_DATAOUT);
	}
}
#endif

2098 2099 2100 2101 2102 2103 2104
static struct platform_driver omap_gpio_driver = {
	.probe		= omap_gpio_probe,
	.driver		= {
		.name	= "omap_gpio",
	},
};

2105
/*
2106 2107 2108
 * gpio driver register needs to be done before
 * machine_init functions access gpio APIs.
 * Hence omap_gpio_drv_reg() is a postcore_initcall.
2109
 */
2110
static int __init omap_gpio_drv_reg(void)
2111
{
2112
	return platform_driver_register(&omap_gpio_driver);
2113
}
2114
postcore_initcall(omap_gpio_drv_reg);
2115

2116 2117 2118 2119
static int __init omap_gpio_sysinit(void)
{
	int ret = 0;

D
David Brownell 已提交
2120 2121
	mpuio_init();

2122
#if defined(CONFIG_ARCH_OMAP16XX) || defined(CONFIG_ARCH_OMAP2PLUS)
2123
	if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
		if (ret == 0) {
			ret = sysdev_class_register(&omap_gpio_sysclass);
			if (ret == 0)
				ret = sysdev_register(&omap_gpio_device);
		}
	}
#endif

	return ret;
}

arch_initcall(omap_gpio_sysinit);