gpio-omap.c 47.0 KB
Newer Older
1 2 3
/*
 * Support functions for OMAP GPIO
 *
4
 * Copyright (C) 2003-2005 Nokia Corporation
5
 * Written by Juha Yrjölä <juha.yrjola@nokia.com>
6
 *
7 8 9
 * Copyright (C) 2009 Texas Instruments
 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
 *
10 11 12 13 14 15 16 17
 * 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>
18
#include <linux/syscore_ops.h>
19
#include <linux/err.h>
20
#include <linux/clk.h>
21
#include <linux/io.h>
22
#include <linux/cpu_pm.h>
23
#include <linux/device.h>
24
#include <linux/pm_runtime.h>
25
#include <linux/pm.h>
26 27
#include <linux/of.h>
#include <linux/of_device.h>
28
#include <linux/gpio/driver.h>
29
#include <linux/bitops.h>
30
#include <linux/platform_data/gpio-omap.h>
31

32
#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
33

34
#define OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER	BIT(2)
35

36 37 38 39 40 41 42 43 44 45 46
struct gpio_regs {
	u32 irqenable1;
	u32 irqenable2;
	u32 wake_en;
	u32 ctrl;
	u32 oe;
	u32 leveldetect0;
	u32 leveldetect1;
	u32 risingdetect;
	u32 fallingdetect;
	u32 dataout;
47 48
	u32 debounce;
	u32 debounce_en;
49 50
};

51 52 53 54 55 56 57
struct gpio_bank;

struct gpio_omap_funcs {
	void (*idle_enable_level_quirk)(struct gpio_bank *bank);
	void (*idle_disable_level_quirk)(struct gpio_bank *bank);
};

58
struct gpio_bank {
59
	struct list_head node;
60
	void __iomem *base;
61
	int irq;
62 63
	u32 non_wakeup_gpios;
	u32 enabled_non_wakeup_gpios;
64
	struct gpio_regs context;
65
	struct gpio_omap_funcs funcs;
66
	u32 saved_datain;
67
	u32 level_mask;
68
	u32 toggle_mask;
69
	raw_spinlock_t lock;
70
	raw_spinlock_t wa_lock;
D
David Brownell 已提交
71
	struct gpio_chip chip;
72
	struct clk *dbck;
73 74
	struct notifier_block nb;
	unsigned int is_suspended:1;
C
Charulatha V 已提交
75
	u32 mod_usage;
76
	u32 irq_usage;
77
	u32 dbck_enable_mask;
78
	bool dbck_enabled;
79
	bool is_mpuio;
80
	bool dbck_flag;
81
	bool loses_context;
82
	bool context_valid;
83
	int stride;
84
	u32 width;
85
	int context_loss_count;
86
	u32 quirks;
87

88
	void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
89 90
	void (*set_dataout_multiple)(struct gpio_bank *bank,
				     unsigned long *mask, unsigned long *bits);
91
	int (*get_context_loss_count)(struct device *dev);
92 93

	struct omap_gpio_reg_offs *regs;
94 95
};

96
#define GPIO_MOD_CTRL_BIT	BIT(0)
97

98
#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
99
#define LINE_USED(line, offset) (line & (BIT(offset)))
100

101 102
static void omap_gpio_unmask_irq(struct irq_data *d);

103
static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
104
{
105
	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
106
	return gpiochip_get_data(chip);
107 108
}

109 110
static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
				    int is_input)
111
{
112
	void __iomem *reg = bank->base;
113 114
	u32 l;

115
	reg += bank->regs->direction;
116
	l = readl_relaxed(reg);
117
	if (is_input)
118
		l |= BIT(gpio);
119
	else
120
		l &= ~(BIT(gpio));
121
	writel_relaxed(l, reg);
122
	bank->context.oe = l;
123 124
}

125 126

/* set data out value using dedicate set/clear register */
127
static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
128
				      int enable)
129
{
130
	void __iomem *reg = bank->base;
131
	u32 l = BIT(offset);
132

133
	if (enable) {
134
		reg += bank->regs->set_dataout;
135 136
		bank->context.dataout |= l;
	} else {
137
		reg += bank->regs->clr_dataout;
138 139
		bank->context.dataout &= ~l;
	}
140

141
	writel_relaxed(l, reg);
142 143
}

144
/* set data out value using mask register */
145
static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
146
				       int enable)
147
{
148
	void __iomem *reg = bank->base + bank->regs->dataout;
149
	u32 gpio_bit = BIT(offset);
150
	u32 l;
151

152
	l = readl_relaxed(reg);
153 154 155 156
	if (enable)
		l |= gpio_bit;
	else
		l &= ~gpio_bit;
157
	writel_relaxed(l, reg);
158
	bank->context.dataout = l;
159 160
}

161
static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
162
{
163
	void __iomem *reg = bank->base + bank->regs->datain;
164

165
	return (readl_relaxed(reg) & (BIT(offset))) != 0;
166
}
167

168
static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
169
{
170
	void __iomem *reg = bank->base + bank->regs->dataout;
171

172
	return (readl_relaxed(reg) & (BIT(offset))) != 0;
173 174
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/* set multiple data out values using dedicate set/clear register */
static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank *bank,
					       unsigned long *mask,
					       unsigned long *bits)
{
	void __iomem *reg = bank->base;
	u32 l;

	l = *bits & *mask;
	writel_relaxed(l, reg + bank->regs->set_dataout);
	bank->context.dataout |= l;

	l = ~*bits & *mask;
	writel_relaxed(l, reg + bank->regs->clr_dataout);
	bank->context.dataout &= ~l;
}

/* set multiple data out values using mask register */
static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank *bank,
						unsigned long *mask,
						unsigned long *bits)
{
	void __iomem *reg = bank->base + bank->regs->dataout;
	u32 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask);

	writel_relaxed(l, reg);
	bank->context.dataout = l;
}

static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank *bank,
					      unsigned long *mask)
{
	void __iomem *reg = bank->base + bank->regs->datain;

	return readl_relaxed(reg) & *mask;
}

static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank *bank,
					       unsigned long *mask)
{
	void __iomem *reg = bank->base + bank->regs->dataout;

	return readl_relaxed(reg) & *mask;
}

220
static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
221
{
222
	int l = readl_relaxed(base + reg);
223

224
	if (set)
225 226 227 228
		l |= mask;
	else
		l &= ~mask;

229
	writel_relaxed(l, base + reg);
230
}
231

232
static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
233 234
{
	if (bank->dbck_enable_mask && !bank->dbck_enabled) {
235
		clk_enable(bank->dbck);
236
		bank->dbck_enabled = true;
237

238
		writel_relaxed(bank->dbck_enable_mask,
239
			     bank->base + bank->regs->debounce_en);
240 241 242
	}
}

243
static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
244 245
{
	if (bank->dbck_enable_mask && bank->dbck_enabled) {
246 247 248 249 250
		/*
		 * Disable debounce before cutting it's clock. If debounce is
		 * enabled but the clock is not, GPIO module seems to be unable
		 * to detect events and generate interrupts at least on OMAP3.
		 */
251
		writel_relaxed(0, bank->base + bank->regs->debounce_en);
252

253
		clk_disable(bank->dbck);
254 255 256 257
		bank->dbck_enabled = false;
	}
}

258
/**
259
 * omap2_set_gpio_debounce - low level gpio debounce time
260
 * @bank: the gpio bank we're acting upon
261
 * @offset: the gpio number on this @bank
262 263
 * @debounce: debounce time to use
 *
264 265 266
 * OMAP's debounce time is in 31us steps
 *   <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31
 * so we need to convert and round up to the closest unit.
267 268
 *
 * Return: 0 on success, negative error otherwise.
269
 */
270 271
static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
				   unsigned debounce)
272
{
273
	void __iomem		*reg;
274 275
	u32			val;
	u32			l;
276
	bool			enable = !!debounce;
277

278
	if (!bank->dbck_flag)
279
		return -ENOTSUPP;
280

281 282
	if (enable) {
		debounce = DIV_ROUND_UP(debounce, 31) - 1;
283 284
		if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce)
			return -EINVAL;
285
	}
286

287
	l = BIT(offset);
288

289
	clk_enable(bank->dbck);
290
	reg = bank->base + bank->regs->debounce;
291
	writel_relaxed(debounce, reg);
292

293
	reg = bank->base + bank->regs->debounce_en;
294
	val = readl_relaxed(reg);
295

296
	if (enable)
297
		val |= l;
298
	else
299
		val &= ~l;
300
	bank->dbck_enable_mask = val;
301

302
	writel_relaxed(val, reg);
303
	clk_disable(bank->dbck);
304 305 306 307 308 309 310 311
	/*
	 * Enable debounce clock per module.
	 * This call is mandatory because in omap_gpio_request() when
	 * *_runtime_get_sync() is called,  _gpio_dbck_enable() within
	 * runtime callbck fails to turn on dbck because dbck_enable_mask
	 * used within _gpio_dbck_enable() is still not initialized at
	 * that point. Therefore we have to enable dbck here.
	 */
312
	omap_gpio_dbck_enable(bank);
313 314 315 316
	if (bank->dbck_enable_mask) {
		bank->context.debounce = debounce;
		bank->context.debounce_en = val;
	}
317 318

	return 0;
319 320
}

321
/**
322
 * omap_clear_gpio_debounce - clear debounce settings for a gpio
323
 * @bank: the gpio bank we're acting upon
324
 * @offset: the gpio number on this @bank
325 326 327 328 329 330
 *
 * If a gpio is using debounce, then clear the debounce enable bit and if
 * this is the only gpio in this bank using debounce, then clear the debounce
 * time too. The debounce clock will also be disabled when calling this function
 * if this is the only gpio in the bank using debounce.
 */
331
static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
332
{
333
	u32 gpio_bit = BIT(offset);
334 335 336 337 338 339 340 341 342

	if (!bank->dbck_flag)
		return;

	if (!(bank->dbck_enable_mask & gpio_bit))
		return;

	bank->dbck_enable_mask &= ~gpio_bit;
	bank->context.debounce_en &= ~gpio_bit;
343
        writel_relaxed(bank->context.debounce_en,
344 345 346 347
		     bank->base + bank->regs->debounce_en);

	if (!bank->dbck_enable_mask) {
		bank->context.debounce = 0;
348
		writel_relaxed(bank->context.debounce, bank->base +
349
			     bank->regs->debounce);
350
		clk_disable(bank->dbck);
351 352 353 354
		bank->dbck_enabled = false;
	}
}

355
static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
356
						unsigned trigger)
357
{
358
	void __iomem *base = bank->base;
359
	u32 gpio_bit = BIT(gpio);
360

361 362 363 364 365 366 367 368
	omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit,
		      trigger & IRQ_TYPE_LEVEL_LOW);
	omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit,
		      trigger & IRQ_TYPE_LEVEL_HIGH);
	omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit,
		      trigger & IRQ_TYPE_EDGE_RISING);
	omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit,
		      trigger & IRQ_TYPE_EDGE_FALLING);
369

370
	bank->context.leveldetect0 =
371
			readl_relaxed(bank->base + bank->regs->leveldetect0);
372
	bank->context.leveldetect1 =
373
			readl_relaxed(bank->base + bank->regs->leveldetect1);
374
	bank->context.risingdetect =
375
			readl_relaxed(bank->base + bank->regs->risingdetect);
376
	bank->context.fallingdetect =
377
			readl_relaxed(bank->base + bank->regs->fallingdetect);
378 379

	if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
380 381 382
		omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
		bank->context.wake_en =
			readl_relaxed(bank->base + bank->regs->wkup_en);
383
	}
384

385
	/* This part needs to be executed always for OMAP{34xx, 44xx} */
386 387 388 389 390 391 392
	if (!bank->regs->irqctrl) {
		/* On omap24xx proceed only when valid GPIO bit is set */
		if (bank->non_wakeup_gpios) {
			if (!(bank->non_wakeup_gpios & gpio_bit))
				goto exit;
		}

393 394 395 396 397 398 399
		/*
		 * 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)
400 401 402 403
			bank->enabled_non_wakeup_gpios |= gpio_bit;
		else
			bank->enabled_non_wakeup_gpios &= ~gpio_bit;
	}
404

405
exit:
406
	bank->level_mask =
407 408
		readl_relaxed(bank->base + bank->regs->leveldetect0) |
		readl_relaxed(bank->base + bank->regs->leveldetect1);
409 410
}

411
#ifdef CONFIG_ARCH_OMAP1
412 413 414 415
/*
 * 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.
 */
416
static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
417 418 419 420
{
	void __iomem *reg = bank->base;
	u32 l = 0;

421
	if (!bank->regs->irqctrl)
422
		return;
423 424

	reg += bank->regs->irqctrl;
425

426
	l = readl_relaxed(reg);
427
	if ((l >> gpio) & 1)
428
		l &= ~(BIT(gpio));
429
	else
430
		l |= BIT(gpio);
431

432
	writel_relaxed(l, reg);
433
}
434
#else
435
static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
436
#endif
437

438 439
static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
				    unsigned trigger)
440 441
{
	void __iomem *reg = bank->base;
442
	void __iomem *base = bank->base;
443
	u32 l = 0;
444

445
	if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
446
		omap_set_gpio_trigger(bank, gpio, trigger);
447 448 449
	} else if (bank->regs->irqctrl) {
		reg += bank->regs->irqctrl;

450
		l = readl_relaxed(reg);
451
		if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
452
			bank->toggle_mask |= BIT(gpio);
453
		if (trigger & IRQ_TYPE_EDGE_RISING)
454
			l |= BIT(gpio);
455
		else if (trigger & IRQ_TYPE_EDGE_FALLING)
456
			l &= ~(BIT(gpio));
457
		else
458 459
			return -EINVAL;

460
		writel_relaxed(l, reg);
461
	} else if (bank->regs->edgectrl1) {
462
		if (gpio & 0x08)
463
			reg += bank->regs->edgectrl2;
464
		else
465 466
			reg += bank->regs->edgectrl1;

467
		gpio &= 0x07;
468
		l = readl_relaxed(reg);
469
		l &= ~(3 << (gpio << 1));
470
		if (trigger & IRQ_TYPE_EDGE_RISING)
471
			l |= 2 << (gpio << 1);
472
		if (trigger & IRQ_TYPE_EDGE_FALLING)
473
			l |= BIT(gpio << 1);
474 475

		/* Enable wake-up during idle for dynamic tick */
476
		omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
477
		bank->context.wake_en =
478 479
			readl_relaxed(bank->base + bank->regs->wkup_en);
		writel_relaxed(l, reg);
480
	}
481
	return 0;
482 483
}

484
static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
485 486 487 488 489
{
	if (bank->regs->pinctrl) {
		void __iomem *reg = bank->base + bank->regs->pinctrl;

		/* Claim the pin for MPU */
490
		writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
491 492 493 494 495 496
	}

	if (bank->regs->ctrl && !BANK_USED(bank)) {
		void __iomem *reg = bank->base + bank->regs->ctrl;
		u32 ctrl;

497
		ctrl = readl_relaxed(reg);
498 499
		/* Module is enabled, clocks are not gated */
		ctrl &= ~GPIO_MOD_CTRL_BIT;
500
		writel_relaxed(ctrl, reg);
501 502 503 504
		bank->context.ctrl = ctrl;
	}
}

505
static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
506 507 508 509 510 511 512
{
	void __iomem *base = bank->base;

	if (bank->regs->wkup_en &&
	    !LINE_USED(bank->mod_usage, offset) &&
	    !LINE_USED(bank->irq_usage, offset)) {
		/* Disable wake-up during idle for dynamic tick */
513
		omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
514
		bank->context.wake_en =
515
			readl_relaxed(bank->base + bank->regs->wkup_en);
516 517 518 519 520 521
	}

	if (bank->regs->ctrl && !BANK_USED(bank)) {
		void __iomem *reg = bank->base + bank->regs->ctrl;
		u32 ctrl;

522
		ctrl = readl_relaxed(reg);
523 524
		/* Module is disabled, clocks are gated */
		ctrl |= GPIO_MOD_CTRL_BIT;
525
		writel_relaxed(ctrl, reg);
526 527 528 529
		bank->context.ctrl = ctrl;
	}
}

530
static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
531 532 533
{
	void __iomem *reg = bank->base + bank->regs->direction;

534
	return readl_relaxed(reg) & BIT(offset);
535 536
}

537
static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
538 539 540 541 542
{
	if (!LINE_USED(bank->mod_usage, offset)) {
		omap_enable_gpio_module(bank, offset);
		omap_set_gpio_direction(bank, offset, 1);
	}
543
	bank->irq_usage |= BIT(offset);
544 545
}

546
static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
547
{
548
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
549
	int retval;
D
David Brownell 已提交
550
	unsigned long flags;
551
	unsigned offset = d->hwirq;
552

553
	if (type & ~IRQ_TYPE_SENSE_MASK)
554
		return -EINVAL;
555

556 557
	if (!bank->regs->leveldetect0 &&
		(type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
558 559
		return -EINVAL;

560
	raw_spin_lock_irqsave(&bank->lock, flags);
561
	retval = omap_set_gpio_triggering(bank, offset, type);
562
	if (retval) {
563
		raw_spin_unlock_irqrestore(&bank->lock, flags);
564
		goto error;
565
	}
566
	omap_gpio_init_irq(bank, offset);
567
	if (!omap_gpio_is_input(bank, offset)) {
568
		raw_spin_unlock_irqrestore(&bank->lock, flags);
569 570
		retval = -EINVAL;
		goto error;
571
	}
572
	raw_spin_unlock_irqrestore(&bank->lock, flags);
573 574

	if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
575
		irq_set_handler_locked(d, handle_level_irq);
576
	else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
577 578 579 580 581 582 583
		/*
		 * Edge IRQs are already cleared/acked in irq_handler and
		 * not need to be masked, as result handle_edge_irq()
		 * logic is excessed here and may cause lose of interrupts.
		 * So just use handle_simple_irq.
		 */
		irq_set_handler_locked(d, handle_simple_irq);
584

585 586 587
	return 0;

error:
588
	return retval;
589 590
}

591
static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
592
{
593
	void __iomem *reg = bank->base;
594

595
	reg += bank->regs->irqstatus;
596
	writel_relaxed(gpio_mask, reg);
597 598

	/* Workaround for clearing DSP GPIO interrupts to allow retention */
599 600
	if (bank->regs->irqstatus2) {
		reg = bank->base + bank->regs->irqstatus2;
601
		writel_relaxed(gpio_mask, reg);
602
	}
603 604

	/* Flush posted write for the irq status to avoid spurious interrupts */
605
	readl_relaxed(reg);
606 607
}

608 609
static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
					     unsigned offset)
610
{
611
	omap_clear_gpio_irqbank(bank, BIT(offset));
612 613
}

614
static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
615 616
{
	void __iomem *reg = bank->base;
617
	u32 l;
618
	u32 mask = (BIT(bank->width)) - 1;
619

620
	reg += bank->regs->irqenable;
621
	l = readl_relaxed(reg);
622
	if (bank->regs->irqenable_inv)
623 624 625
		l = ~l;
	l &= mask;
	return l;
626 627
}

628
static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
629
{
630
	void __iomem *reg = bank->base;
631 632
	u32 l;

633 634 635
	if (bank->regs->set_irqenable) {
		reg += bank->regs->set_irqenable;
		l = gpio_mask;
636
		bank->context.irqenable1 |= gpio_mask;
637 638
	} else {
		reg += bank->regs->irqenable;
639
		l = readl_relaxed(reg);
640 641
		if (bank->regs->irqenable_inv)
			l &= ~gpio_mask;
642 643
		else
			l |= gpio_mask;
644
		bank->context.irqenable1 = l;
645 646
	}

647
	writel_relaxed(l, reg);
648 649
}

650
static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
651 652 653 654 655 656
{
	void __iomem *reg = bank->base;
	u32 l;

	if (bank->regs->clr_irqenable) {
		reg += bank->regs->clr_irqenable;
657
		l = gpio_mask;
658
		bank->context.irqenable1 &= ~gpio_mask;
659 660
	} else {
		reg += bank->regs->irqenable;
661
		l = readl_relaxed(reg);
662
		if (bank->regs->irqenable_inv)
663
			l |= gpio_mask;
664
		else
665
			l &= ~gpio_mask;
666
		bank->context.irqenable1 = l;
667
	}
668

669
	writel_relaxed(l, reg);
670 671
}

672 673
static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
					   unsigned offset, int enable)
674
{
675
	if (enable)
676
		omap_enable_gpio_irqbank(bank, BIT(offset));
677
	else
678
		omap_disable_gpio_irqbank(bank, BIT(offset));
679 680
}

681
/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
682
static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
683
{
684
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
685

686
	return irq_set_irq_wake(bank->irq, enable);
687 688
}

689
static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
690
{
691
	struct gpio_bank *bank = gpiochip_get_data(chip);
D
David Brownell 已提交
692
	unsigned long flags;
D
David Brownell 已提交
693

694
	pm_runtime_get_sync(chip->parent);
695

696
	raw_spin_lock_irqsave(&bank->lock, flags);
697
	omap_enable_gpio_module(bank, offset);
698
	bank->mod_usage |= BIT(offset);
699
	raw_spin_unlock_irqrestore(&bank->lock, flags);
700 701 702 703

	return 0;
}

704
static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
705
{
706
	struct gpio_bank *bank = gpiochip_get_data(chip);
D
David Brownell 已提交
707
	unsigned long flags;
708

709
	raw_spin_lock_irqsave(&bank->lock, flags);
710
	bank->mod_usage &= ~(BIT(offset));
711 712 713 714
	if (!LINE_USED(bank->irq_usage, offset)) {
		omap_set_gpio_direction(bank, offset, 1);
		omap_clear_gpio_debounce(bank, offset);
	}
715
	omap_disable_gpio_module(bank, offset);
716
	raw_spin_unlock_irqrestore(&bank->lock, flags);
717

718
	pm_runtime_put(chip->parent);
719 720 721 722 723 724 725 726 727 728 729
}

/*
 * 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.
 */
730
static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
731
{
732
	void __iomem *isr_reg = NULL;
733
	u32 enabled, isr, level_mask;
734
	unsigned int bit;
735 736
	struct gpio_bank *bank = gpiobank;
	unsigned long wa_lock_flags;
737
	unsigned long lock_flags;
738

739
	isr_reg = bank->base + bank->regs->irqstatus;
740 741 742
	if (WARN_ON(!isr_reg))
		goto exit;

743 744 745
	if (WARN_ONCE(!pm_runtime_active(bank->chip.parent),
		      "gpio irq%i while runtime suspended?\n", irq))
		return IRQ_NONE;
746

747
	while (1) {
748 749
		raw_spin_lock_irqsave(&bank->lock, lock_flags);

750
		enabled = omap_get_gpio_irqbank_mask(bank);
751
		isr = readl_relaxed(isr_reg) & enabled;
752

753
		if (bank->level_mask)
754
			level_mask = bank->level_mask & enabled;
755 756
		else
			level_mask = 0;
757 758 759 760

		/* clear edge sensitive interrupts before handler(s) are
		called so that we don't miss any interrupt occurred while
		executing them */
761 762
		if (isr & ~level_mask)
			omap_clear_gpio_irqbank(bank, isr & ~level_mask);
763

764 765
		raw_spin_unlock_irqrestore(&bank->lock, lock_flags);

766 767 768
		if (!isr)
			break;

769 770
		while (isr) {
			bit = __ffs(isr);
771
			isr &= ~(BIT(bit));
772

773
			raw_spin_lock_irqsave(&bank->lock, lock_flags);
774 775 776 777 778 779 780
			/*
			 * 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.
			 */
781
			if (bank->toggle_mask & (BIT(bit)))
782
				omap_toggle_gpio_edge_triggering(bank, bit);
783

784 785
			raw_spin_unlock_irqrestore(&bank->lock, lock_flags);

786 787
			raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);

788
			generic_handle_irq(irq_find_mapping(bank->chip.irq.domain,
789
							    bit));
790 791 792

			raw_spin_unlock_irqrestore(&bank->wa_lock,
						   wa_lock_flags);
793
		}
794
	}
795
exit:
796
	return IRQ_HANDLED;
797 798
}

799 800 801 802
static unsigned int omap_gpio_irq_startup(struct irq_data *d)
{
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
	unsigned long flags;
803
	unsigned offset = d->hwirq;
804

805
	raw_spin_lock_irqsave(&bank->lock, flags);
806 807 808 809 810 811 812 813

	if (!LINE_USED(bank->mod_usage, offset))
		omap_set_gpio_direction(bank, offset, 1);
	else if (!omap_gpio_is_input(bank, offset))
		goto err;
	omap_enable_gpio_module(bank, offset);
	bank->irq_usage |= BIT(offset);

814
	raw_spin_unlock_irqrestore(&bank->lock, flags);
815 816 817
	omap_gpio_unmask_irq(d);

	return 0;
818
err:
819
	raw_spin_unlock_irqrestore(&bank->lock, flags);
820
	return -EINVAL;
821 822
}

823
static void omap_gpio_irq_shutdown(struct irq_data *d)
824
{
825
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
826
	unsigned long flags;
827
	unsigned offset = d->hwirq;
828

829
	raw_spin_lock_irqsave(&bank->lock, flags);
830
	bank->irq_usage &= ~(BIT(offset));
831 832 833 834 835
	omap_set_gpio_irqenable(bank, offset, 0);
	omap_clear_gpio_irqstatus(bank, offset);
	omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
	if (!LINE_USED(bank->mod_usage, offset))
		omap_clear_gpio_debounce(bank, offset);
836
	omap_disable_gpio_module(bank, offset);
837
	raw_spin_unlock_irqrestore(&bank->lock, flags);
838 839 840 841 842 843
}

static void omap_gpio_irq_bus_lock(struct irq_data *data)
{
	struct gpio_bank *bank = omap_irq_data_get_bank(data);

844
	pm_runtime_get_sync(bank->chip.parent);
845 846 847 848 849
}

static void gpio_irq_bus_sync_unlock(struct irq_data *data)
{
	struct gpio_bank *bank = omap_irq_data_get_bank(data);
850

851
	pm_runtime_put(bank->chip.parent);
852 853
}

854
static void omap_gpio_ack_irq(struct irq_data *d)
855
{
856
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
857
	unsigned offset = d->hwirq;
858

859
	omap_clear_gpio_irqstatus(bank, offset);
860 861
}

862
static void omap_gpio_mask_irq(struct irq_data *d)
863
{
864
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
865
	unsigned offset = d->hwirq;
866
	unsigned long flags;
867

868
	raw_spin_lock_irqsave(&bank->lock, flags);
869 870
	omap_set_gpio_irqenable(bank, offset, 0);
	omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
871
	raw_spin_unlock_irqrestore(&bank->lock, flags);
872 873
}

874
static void omap_gpio_unmask_irq(struct irq_data *d)
875
{
876
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
877
	unsigned offset = d->hwirq;
878
	u32 trigger = irqd_get_trigger_type(d);
879
	unsigned long flags;
880

881
	raw_spin_lock_irqsave(&bank->lock, flags);
882
	if (trigger)
883
		omap_set_gpio_triggering(bank, offset, trigger);
884

885 886 887 888 889 890 891 892
	omap_set_gpio_irqenable(bank, offset, 1);

	/*
	 * For level-triggered GPIOs, clearing must be done after the source
	 * is cleared, thus after the handler has run. OMAP4 needs this done
	 * after enabing the interrupt to clear the wakeup status.
	 */
	if (bank->level_mask & BIT(offset))
893
		omap_clear_gpio_irqstatus(bank, offset);
894

895
	raw_spin_unlock_irqrestore(&bank->lock, flags);
896 897
}

898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
/*
 * Only edges can generate a wakeup event to the PRCM.
 *
 * Therefore, ensure any wake-up capable GPIOs have
 * edge-detection enabled before going idle to ensure a wakeup
 * to the PRCM is generated on a GPIO transition. (c.f. 34xx
 * NDA TRM 25.5.3.1)
 *
 * The normal values will be restored upon ->runtime_resume()
 * by writing back the values saved in bank->context.
 */
static void __maybe_unused
omap2_gpio_enable_level_quirk(struct gpio_bank *bank)
{
	u32 wake_low, wake_hi;

	/* Enable additional edge detection for level gpios for idle */
	wake_low = bank->context.leveldetect0 & bank->context.wake_en;
	if (wake_low)
		writel_relaxed(wake_low | bank->context.fallingdetect,
			       bank->base + bank->regs->fallingdetect);

	wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
	if (wake_hi)
		writel_relaxed(wake_hi | bank->context.risingdetect,
			       bank->base + bank->regs->risingdetect);
}

static void __maybe_unused
omap2_gpio_disable_level_quirk(struct gpio_bank *bank)
{
	/* Disable edge detection for level gpios after idle */
	writel_relaxed(bank->context.fallingdetect,
		       bank->base + bank->regs->fallingdetect);
	writel_relaxed(bank->context.risingdetect,
		       bank->base + bank->regs->risingdetect);
}

936 937
/*---------------------------------------------------------------------*/

938
static int omap_mpuio_suspend_noirq(struct device *dev)
D
David Brownell 已提交
939
{
940
	struct gpio_bank	*bank = dev_get_drvdata(dev);
941 942
	void __iomem		*mask_reg = bank->base +
					OMAP_MPUIO_GPIO_MASKIT / bank->stride;
D
David Brownell 已提交
943
	unsigned long		flags;
D
David Brownell 已提交
944

945
	raw_spin_lock_irqsave(&bank->lock, flags);
946
	writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
947
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
948 949 950 951

	return 0;
}

952
static int omap_mpuio_resume_noirq(struct device *dev)
D
David Brownell 已提交
953
{
954
	struct gpio_bank	*bank = dev_get_drvdata(dev);
955 956
	void __iomem		*mask_reg = bank->base +
					OMAP_MPUIO_GPIO_MASKIT / bank->stride;
D
David Brownell 已提交
957
	unsigned long		flags;
D
David Brownell 已提交
958

959
	raw_spin_lock_irqsave(&bank->lock, flags);
960
	writel_relaxed(bank->context.wake_en, mask_reg);
961
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
962 963 964 965

	return 0;
}

966
static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
967 968 969 970
	.suspend_noirq = omap_mpuio_suspend_noirq,
	.resume_noirq = omap_mpuio_resume_noirq,
};

971
/* use platform_driver for this. */
D
David Brownell 已提交
972 973 974
static struct platform_driver omap_mpuio_driver = {
	.driver		= {
		.name	= "mpuio",
975
		.pm	= &omap_mpuio_dev_pm_ops,
D
David Brownell 已提交
976 977 978 979 980 981 982 983 984 985 986 987
	},
};

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

988
static inline void omap_mpuio_init(struct gpio_bank *bank)
D
David Brownell 已提交
989
{
990
	platform_set_drvdata(&omap_mpuio_device, bank);
991

D
David Brownell 已提交
992 993 994 995
	if (platform_driver_register(&omap_mpuio_driver) == 0)
		(void) platform_device_register(&omap_mpuio_device);
}

996
/*---------------------------------------------------------------------*/
997

998
static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
999 1000 1001 1002 1003 1004
{
	struct gpio_bank *bank;
	unsigned long flags;
	void __iomem *reg;
	int dir;

1005
	bank = gpiochip_get_data(chip);
1006
	reg = bank->base + bank->regs->direction;
1007
	raw_spin_lock_irqsave(&bank->lock, flags);
1008
	dir = !!(readl_relaxed(reg) & BIT(offset));
1009
	raw_spin_unlock_irqrestore(&bank->lock, flags);
1010 1011 1012
	return dir;
}

1013
static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
D
David Brownell 已提交
1014 1015 1016 1017
{
	struct gpio_bank *bank;
	unsigned long flags;

1018
	bank = gpiochip_get_data(chip);
1019
	raw_spin_lock_irqsave(&bank->lock, flags);
1020
	omap_set_gpio_direction(bank, offset, 1);
1021
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
1022 1023 1024
	return 0;
}

1025
static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
D
David Brownell 已提交
1026
{
1027 1028
	struct gpio_bank *bank;

1029
	bank = gpiochip_get_data(chip);
1030

1031
	if (omap_gpio_is_input(bank, offset))
1032
		return omap_get_gpio_datain(bank, offset);
1033
	else
1034
		return omap_get_gpio_dataout(bank, offset);
D
David Brownell 已提交
1035 1036
}

1037
static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
D
David Brownell 已提交
1038 1039 1040 1041
{
	struct gpio_bank *bank;
	unsigned long flags;

1042
	bank = gpiochip_get_data(chip);
1043
	raw_spin_lock_irqsave(&bank->lock, flags);
1044
	bank->set_dataout(bank, offset, value);
1045
	omap_set_gpio_direction(bank, offset, 0);
1046
	raw_spin_unlock_irqrestore(&bank->lock, flags);
1047
	return 0;
D
David Brownell 已提交
1048 1049
}

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
				  unsigned long *bits)
{
	struct gpio_bank *bank = gpiochip_get_data(chip);
	void __iomem *reg = bank->base + bank->regs->direction;
	unsigned long in = readl_relaxed(reg), l;

	*bits = 0;

	l = in & *mask;
	if (l)
		*bits |= omap_get_gpio_datain_multiple(bank, &l);

	l = ~in & *mask;
	if (l)
		*bits |= omap_get_gpio_dataout_multiple(bank, &l);

	return 0;
}

1070 1071
static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
			      unsigned debounce)
1072 1073 1074
{
	struct gpio_bank *bank;
	unsigned long flags;
1075
	int ret;
1076

1077
	bank = gpiochip_get_data(chip);
1078

1079
	raw_spin_lock_irqsave(&bank->lock, flags);
1080
	ret = omap2_set_gpio_debounce(bank, offset, debounce);
1081
	raw_spin_unlock_irqrestore(&bank->lock, flags);
1082

1083 1084 1085 1086 1087 1088
	if (ret)
		dev_info(chip->parent,
			 "Could not set line %u debounce to %u microseconds (%d)",
			 offset, debounce, ret);

	return ret;
1089 1090
}

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset,
				unsigned long config)
{
	u32 debounce;

	if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
		return -ENOTSUPP;

	debounce = pinconf_to_config_argument(config);
	return omap_gpio_debounce(chip, offset, debounce);
}

1103
static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
D
David Brownell 已提交
1104 1105 1106 1107
{
	struct gpio_bank *bank;
	unsigned long flags;

1108
	bank = gpiochip_get_data(chip);
1109
	raw_spin_lock_irqsave(&bank->lock, flags);
1110
	bank->set_dataout(bank, offset, value);
1111
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
1112 1113
}

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
				   unsigned long *bits)
{
	struct gpio_bank *bank = gpiochip_get_data(chip);
	unsigned long flags;

	raw_spin_lock_irqsave(&bank->lock, flags);
	bank->set_dataout_multiple(bank, mask, bits);
	raw_spin_unlock_irqrestore(&bank->lock, flags);
}

D
David Brownell 已提交
1125 1126
/*---------------------------------------------------------------------*/

1127
static void omap_gpio_show_rev(struct gpio_bank *bank)
T
Tony Lindgren 已提交
1128
{
1129
	static bool called;
T
Tony Lindgren 已提交
1130 1131
	u32 rev;

1132
	if (called || bank->regs->revision == USHRT_MAX)
T
Tony Lindgren 已提交
1133 1134
		return;

1135
	rev = readw_relaxed(bank->base + bank->regs->revision);
1136
	pr_info("OMAP GPIO hardware version %d.%d\n",
T
Tony Lindgren 已提交
1137
		(rev >> 4) & 0x0f, rev & 0x0f);
1138 1139

	called = true;
T
Tony Lindgren 已提交
1140 1141
}

1142
static void omap_gpio_mod_init(struct gpio_bank *bank)
1143
{
1144 1145
	void __iomem *base = bank->base;
	u32 l = 0xffffffff;
1146

1147 1148 1149
	if (bank->width == 16)
		l = 0xffff;

1150
	if (bank->is_mpuio) {
1151
		writel_relaxed(l, bank->base + bank->regs->irqenable);
1152
		return;
1153
	}
1154

1155 1156 1157 1158
	omap_gpio_rmw(base, bank->regs->irqenable, l,
		      bank->regs->irqenable_inv);
	omap_gpio_rmw(base, bank->regs->irqstatus, l,
		      !bank->regs->irqenable_inv);
1159
	if (bank->regs->debounce_en)
1160
		writel_relaxed(0, base + bank->regs->debounce_en);
1161

1162
	/* Save OE default value (0xffffffff) in the context */
1163
	bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
1164 1165
	 /* Initialize interface clk ungated, module enabled */
	if (bank->regs->ctrl)
1166
		writel_relaxed(0, base + bank->regs->ctrl);
1167 1168
}

N
Nishanth Menon 已提交
1169
static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
1170
{
1171
	struct gpio_irq_chip *irq;
1172
	static int gpio;
1173
	const char *label;
1174
	int irq_base = 0;
1175
	int ret;
1176 1177 1178 1179 1180 1181 1182

	/*
	 * 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;
1183 1184 1185
	bank->chip.get_direction = omap_gpio_get_direction;
	bank->chip.direction_input = omap_gpio_input;
	bank->chip.get = omap_gpio_get;
1186
	bank->chip.get_multiple = omap_gpio_get_multiple;
1187
	bank->chip.direction_output = omap_gpio_output;
1188
	bank->chip.set_config = omap_gpio_set_config;
1189
	bank->chip.set = omap_gpio_set;
1190
	bank->chip.set_multiple = omap_gpio_set_multiple;
1191
	if (bank->is_mpuio) {
1192
		bank->chip.label = "mpuio";
1193
		if (bank->regs->wkup_en)
1194
			bank->chip.parent = &omap_mpuio_device.dev;
1195 1196
		bank->chip.base = OMAP_MPUIO(0);
	} else {
1197 1198 1199 1200 1201
		label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d",
				       gpio, gpio + bank->width - 1);
		if (!label)
			return -ENOMEM;
		bank->chip.label = label;
1202 1203
		bank->chip.base = gpio;
	}
1204
	bank->chip.ngpio = bank->width;
1205

1206 1207 1208 1209 1210
#ifdef CONFIG_ARCH_OMAP1
	/*
	 * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop
	 * irq_alloc_descs() since a base IRQ offset will no longer be needed.
	 */
1211 1212
	irq_base = devm_irq_alloc_descs(bank->chip.parent,
					-1, 0, bank->width, 0);
1213
	if (irq_base < 0) {
1214
		dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n");
1215 1216 1217 1218
		return -ENODEV;
	}
#endif

1219 1220 1221 1222 1223 1224 1225
	/* MPUIO is a bit different, reading IRQ status clears it */
	if (bank->is_mpuio) {
		irqc->irq_ack = dummy_irq_chip.irq_ack;
		if (!bank->regs->wkup_en)
			irqc->irq_set_wake = NULL;
	}

1226 1227 1228 1229 1230 1231 1232
	irq = &bank->chip.irq;
	irq->chip = irqc;
	irq->handler = handle_bad_irq;
	irq->default_type = IRQ_TYPE_NONE;
	irq->num_parents = 1;
	irq->parents = &bank->irq;
	irq->first = irq_base;
1233

1234
	ret = gpiochip_add_data(&bank->chip, bank);
1235
	if (ret) {
1236
		dev_err(bank->chip.parent,
1237 1238
			"Could not register gpio chip %d\n", ret);
		return ret;
1239 1240
	}

1241 1242 1243
	ret = devm_request_irq(bank->chip.parent, bank->irq,
			       omap_gpio_irq_handler,
			       0, dev_name(bank->chip.parent), bank);
1244 1245 1246
	if (ret)
		gpiochip_remove(&bank->chip);

1247 1248 1249
	if (!bank->is_mpuio)
		gpio += bank->width;

1250
	return ret;
1251 1252
}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context);
static void omap_gpio_unidle(struct gpio_bank *bank);

static int gpio_omap_cpu_notifier(struct notifier_block *nb,
				  unsigned long cmd, void *v)
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(nb, struct gpio_bank, nb);

	raw_spin_lock_irqsave(&bank->lock, flags);
	switch (cmd) {
	case CPU_CLUSTER_PM_ENTER:
		if (bank->is_suspended)
			break;
		omap_gpio_idle(bank, true);
		break;
	case CPU_CLUSTER_PM_ENTER_FAILED:
	case CPU_CLUSTER_PM_EXIT:
		if (bank->is_suspended)
			break;
		omap_gpio_unidle(bank);
		break;
	}
	raw_spin_unlock_irqrestore(&bank->lock, flags);

	return NOTIFY_OK;
}

1283 1284
static const struct of_device_id omap_gpio_match[];

B
Bill Pemberton 已提交
1285
static int omap_gpio_probe(struct platform_device *pdev)
1286
{
1287
	struct device *dev = &pdev->dev;
1288 1289
	struct device_node *node = dev->of_node;
	const struct of_device_id *match;
1290
	const struct omap_gpio_platform_data *pdata;
1291
	struct resource *res;
1292
	struct gpio_bank *bank;
N
Nishanth Menon 已提交
1293
	struct irq_chip *irqc;
1294
	int ret;
1295

1296 1297
	match = of_match_device(of_match_ptr(omap_gpio_match), dev);

J
Jingoo Han 已提交
1298
	pdata = match ? match->data : dev_get_platdata(dev);
1299
	if (!pdata)
1300
		return -EINVAL;
1301

1302
	bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL);
1303
	if (!bank)
1304
		return -ENOMEM;
1305

N
Nishanth Menon 已提交
1306 1307 1308 1309
	irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
	if (!irqc)
		return -ENOMEM;

1310
	irqc->irq_startup = omap_gpio_irq_startup,
N
Nishanth Menon 已提交
1311 1312 1313 1314 1315 1316
	irqc->irq_shutdown = omap_gpio_irq_shutdown,
	irqc->irq_ack = omap_gpio_ack_irq,
	irqc->irq_mask = omap_gpio_mask_irq,
	irqc->irq_unmask = omap_gpio_unmask_irq,
	irqc->irq_set_type = omap_gpio_irq_type,
	irqc->irq_set_wake = omap_gpio_wake_enable,
1317 1318
	irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
	irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
N
Nishanth Menon 已提交
1319
	irqc->name = dev_name(&pdev->dev);
1320
	irqc->flags = IRQCHIP_MASK_ON_SUSPEND;
1321
	irqc->parent_device = dev;
N
Nishanth Menon 已提交
1322

1323 1324 1325 1326 1327 1328 1329 1330
	bank->irq = platform_get_irq(pdev, 0);
	if (bank->irq <= 0) {
		if (!bank->irq)
			bank->irq = -ENXIO;
		if (bank->irq != -EPROBE_DEFER)
			dev_err(dev,
				"can't get irq resource ret=%d\n", bank->irq);
		return bank->irq;
1331
	}
1332

1333
	bank->chip.parent = dev;
1334
	bank->chip.owner = THIS_MODULE;
1335
	bank->dbck_flag = pdata->dbck_flag;
1336
	bank->quirks = pdata->quirks;
1337
	bank->stride = pdata->bank_stride;
1338
	bank->width = pdata->bank_width;
1339
	bank->is_mpuio = pdata->is_mpuio;
1340
	bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
1341
	bank->regs = pdata->regs;
1342 1343 1344
#ifdef CONFIG_OF_GPIO
	bank->chip.of_node = of_node_get(node);
#endif
1345

1346 1347 1348 1349 1350
	if (node) {
		if (!of_property_read_bool(node, "ti,gpio-always-on"))
			bank->loses_context = true;
	} else {
		bank->loses_context = pdata->loses_context;
1351 1352 1353 1354

		if (bank->loses_context)
			bank->get_context_loss_count =
				pdata->get_context_loss_count;
1355 1356
	}

1357
	if (bank->regs->set_dataout && bank->regs->clr_dataout) {
1358
		bank->set_dataout = omap_set_gpio_dataout_reg;
1359 1360
		bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple;
	} else {
1361
		bank->set_dataout = omap_set_gpio_dataout_mask;
1362 1363 1364
		bank->set_dataout_multiple =
				omap_set_gpio_dataout_mask_multiple;
	}
T
Tony Lindgren 已提交
1365

1366
	if (bank->quirks & OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER) {
1367 1368 1369 1370 1371 1372
		bank->funcs.idle_enable_level_quirk =
			omap2_gpio_enable_level_quirk;
		bank->funcs.idle_disable_level_quirk =
			omap2_gpio_disable_level_quirk;
	}

1373
	raw_spin_lock_init(&bank->lock);
1374
	raw_spin_lock_init(&bank->wa_lock);
T
Tony Lindgren 已提交
1375

1376 1377
	/* Static mapping, never released */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1378 1379 1380
	bank->base = devm_ioremap_resource(dev, res);
	if (IS_ERR(bank->base)) {
		return PTR_ERR(bank->base);
1381 1382
	}

1383
	if (bank->dbck_flag) {
1384
		bank->dbck = devm_clk_get(dev, "dbclk");
1385
		if (IS_ERR(bank->dbck)) {
1386
			dev_err(dev,
1387 1388 1389 1390 1391 1392 1393
				"Could not get gpio dbck. Disable debounce\n");
			bank->dbck_flag = false;
		} else {
			clk_prepare(bank->dbck);
		}
	}

1394 1395
	platform_set_drvdata(pdev, bank);

1396 1397
	pm_runtime_enable(dev);
	pm_runtime_get_sync(dev);
1398

1399
	if (bank->is_mpuio)
1400
		omap_mpuio_init(bank);
1401

1402
	omap_gpio_mod_init(bank);
1403

N
Nishanth Menon 已提交
1404
	ret = omap_gpio_chip_init(bank, irqc);
1405
	if (ret) {
1406 1407
		pm_runtime_put_sync(dev);
		pm_runtime_disable(dev);
1408 1409
		if (bank->dbck_flag)
			clk_unprepare(bank->dbck);
1410
		return ret;
1411
	}
1412

1413
	omap_gpio_show_rev(bank);
T
Tony Lindgren 已提交
1414

1415 1416 1417 1418 1419 1420
	if (bank->funcs.idle_enable_level_quirk &&
	    bank->funcs.idle_disable_level_quirk) {
		bank->nb.notifier_call = gpio_omap_cpu_notifier;
		cpu_pm_register_notifier(&bank->nb);
	}

1421
	pm_runtime_put(dev);
1422

1423
	return 0;
1424 1425
}

1426 1427 1428 1429
static int omap_gpio_remove(struct platform_device *pdev)
{
	struct gpio_bank *bank = platform_get_drvdata(pdev);

1430 1431
	if (bank->nb.notifier_call)
		cpu_pm_unregister_notifier(&bank->nb);
1432 1433
	list_del(&bank->node);
	gpiochip_remove(&bank->chip);
1434
	pm_runtime_disable(&pdev->dev);
1435 1436
	if (bank->dbck_flag)
		clk_unprepare(bank->dbck);
1437 1438 1439 1440

	return 0;
}

1441
static void omap_gpio_restore_context(struct gpio_bank *bank);
1442

1443
static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context)
1444
{
1445
	struct device *dev = bank->chip.parent;
1446 1447 1448 1449
	void __iomem *base = bank->base;
	u32 nowake;

	bank->saved_datain = readl_relaxed(base + bank->regs->datain);
1450

1451 1452
	if (bank->funcs.idle_enable_level_quirk)
		bank->funcs.idle_enable_level_quirk(bank);
1453

1454 1455 1456
	if (!bank->enabled_non_wakeup_gpios)
		goto update_gpio_context_count;

1457
	if (!may_lose_context)
1458
		goto update_gpio_context_count;
1459

1460
	/*
1461
	 * If going to OFF, remove triggering for all wkup domain
1462 1463 1464
	 * non-wakeup GPIOs.  Otherwise spurious IRQs will be
	 * generated.  See OMAP2420 Errata item 1.101.
	 */
1465 1466 1467 1468 1469
	if (!bank->loses_context && bank->enabled_non_wakeup_gpios) {
		nowake = bank->enabled_non_wakeup_gpios;
		omap_gpio_rmw(base, bank->regs->fallingdetect, nowake, ~nowake);
		omap_gpio_rmw(base, bank->regs->risingdetect, nowake, ~nowake);
	}
1470

1471
update_gpio_context_count:
1472 1473
	if (bank->get_context_loss_count)
		bank->context_loss_count =
1474
				bank->get_context_loss_count(dev);
1475

1476
	omap_gpio_dbck_disable(bank);
1477 1478
}

1479 1480
static void omap_gpio_init_context(struct gpio_bank *p);

1481
static void omap_gpio_unidle(struct gpio_bank *bank)
1482
{
1483
	struct device *dev = bank->chip.parent;
1484
	u32 l = 0, gen, gen0, gen1;
1485
	int c;
1486

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	/*
	 * On the first resume during the probe, the context has not
	 * been initialised and so initialise it now. Also initialise
	 * the context loss count.
	 */
	if (bank->loses_context && !bank->context_valid) {
		omap_gpio_init_context(bank);

		if (bank->get_context_loss_count)
			bank->context_loss_count =
1497
				bank->get_context_loss_count(dev);
1498 1499
	}

1500
	omap_gpio_dbck_enable(bank);
1501

1502 1503
	if (bank->funcs.idle_disable_level_quirk)
		bank->funcs.idle_disable_level_quirk(bank);
1504

1505 1506
	if (bank->loses_context) {
		if (!bank->get_context_loss_count) {
1507 1508
			omap_gpio_restore_context(bank);
		} else {
1509
			c = bank->get_context_loss_count(dev);
1510 1511 1512
			if (c != bank->context_loss_count) {
				omap_gpio_restore_context(bank);
			} else {
1513
				return;
1514
			}
1515
		}
1516 1517 1518 1519 1520 1521
	} else {
		/* Restore changes done for OMAP2420 errata 1.101 */
		writel_relaxed(bank->context.fallingdetect,
			       bank->base + bank->regs->fallingdetect);
		writel_relaxed(bank->context.risingdetect,
			       bank->base + bank->regs->risingdetect);
1522
	}
1523

1524
	l = readl_relaxed(bank->base + bank->regs->datain);
1525

1526 1527 1528 1529 1530 1531 1532 1533
	/*
	 * 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;
	l &= bank->enabled_non_wakeup_gpios;
1534

1535 1536 1537 1538
	/*
	 * No need to generate IRQs for the rising edge for gpio IRQs
	 * configured with falling edge only; and vice versa.
	 */
1539
	gen0 = l & bank->context.fallingdetect;
1540
	gen0 &= bank->saved_datain;
1541

1542
	gen1 = l & bank->context.risingdetect;
1543
	gen1 &= ~(bank->saved_datain);
1544

1545
	/* FIXME: Consider GPIO IRQs with level detections properly! */
1546 1547
	gen = l & (~(bank->context.fallingdetect) &
					 ~(bank->context.risingdetect));
1548 1549
	/* Consider all GPIO IRQs needed to be updated */
	gen |= gen0 | gen1;
1550

1551 1552
	if (gen) {
		u32 old0, old1;
1553

1554 1555
		old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
		old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
1556

1557
		if (!bank->regs->irqstatus_raw0) {
1558
			writel_relaxed(old0 | gen, bank->base +
1559
						bank->regs->leveldetect0);
1560
			writel_relaxed(old1 | gen, bank->base +
1561
						bank->regs->leveldetect1);
1562
		}
1563

1564
		if (bank->regs->irqstatus_raw0) {
1565
			writel_relaxed(old0 | l, bank->base +
1566
						bank->regs->leveldetect0);
1567
			writel_relaxed(old1 | l, bank->base +
1568
						bank->regs->leveldetect1);
1569
		}
1570 1571
		writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
		writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
1572 1573 1574
	}
}

1575 1576 1577 1578 1579
static void omap_gpio_init_context(struct gpio_bank *p)
{
	struct omap_gpio_reg_offs *regs = p->regs;
	void __iomem *base = p->base;

1580 1581 1582 1583 1584 1585 1586 1587 1588
	p->context.ctrl		= readl_relaxed(base + regs->ctrl);
	p->context.oe		= readl_relaxed(base + regs->direction);
	p->context.wake_en	= readl_relaxed(base + regs->wkup_en);
	p->context.leveldetect0	= readl_relaxed(base + regs->leveldetect0);
	p->context.leveldetect1	= readl_relaxed(base + regs->leveldetect1);
	p->context.risingdetect	= readl_relaxed(base + regs->risingdetect);
	p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect);
	p->context.irqenable1	= readl_relaxed(base + regs->irqenable);
	p->context.irqenable2	= readl_relaxed(base + regs->irqenable2);
1589 1590

	if (regs->set_dataout && p->regs->clr_dataout)
1591
		p->context.dataout = readl_relaxed(base + regs->set_dataout);
1592
	else
1593
		p->context.dataout = readl_relaxed(base + regs->dataout);
1594 1595 1596 1597

	p->context_valid = true;
}

1598
static void omap_gpio_restore_context(struct gpio_bank *bank)
1599
{
1600
	writel_relaxed(bank->context.wake_en,
1601
				bank->base + bank->regs->wkup_en);
1602 1603
	writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
	writel_relaxed(bank->context.leveldetect0,
1604
				bank->base + bank->regs->leveldetect0);
1605
	writel_relaxed(bank->context.leveldetect1,
1606
				bank->base + bank->regs->leveldetect1);
1607
	writel_relaxed(bank->context.risingdetect,
1608
				bank->base + bank->regs->risingdetect);
1609
	writel_relaxed(bank->context.fallingdetect,
1610
				bank->base + bank->regs->fallingdetect);
1611
	if (bank->regs->set_dataout && bank->regs->clr_dataout)
1612
		writel_relaxed(bank->context.dataout,
1613 1614
				bank->base + bank->regs->set_dataout);
	else
1615
		writel_relaxed(bank->context.dataout,
1616
				bank->base + bank->regs->dataout);
1617
	writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
1618

1619
	if (bank->dbck_enable_mask) {
1620
		writel_relaxed(bank->context.debounce, bank->base +
1621
					bank->regs->debounce);
1622
		writel_relaxed(bank->context.debounce_en,
1623 1624
					bank->base + bank->regs->debounce_en);
	}
1625

1626
	writel_relaxed(bank->context.irqenable1,
1627
				bank->base + bank->regs->irqenable);
1628
	writel_relaxed(bank->context.irqenable2,
1629
				bank->base + bank->regs->irqenable2);
1630 1631
}

1632 1633
static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev)
{
1634
	struct gpio_bank *bank = dev_get_drvdata(dev);
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
	unsigned long flags;
	int error = 0;

	raw_spin_lock_irqsave(&bank->lock, flags);
	/* Must be idled only by CPU_CLUSTER_PM_ENTER? */
	if (bank->irq_usage) {
		error = -EBUSY;
		goto unlock;
	}
	omap_gpio_idle(bank, true);
	bank->is_suspended = true;
unlock:
	raw_spin_unlock_irqrestore(&bank->lock, flags);

	return error;
}

static int __maybe_unused omap_gpio_runtime_resume(struct device *dev)
{
1654
	struct gpio_bank *bank = dev_get_drvdata(dev);
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
	unsigned long flags;
	int error = 0;

	raw_spin_lock_irqsave(&bank->lock, flags);
	/* Must be unidled only by CPU_CLUSTER_PM_ENTER? */
	if (bank->irq_usage) {
		error = -EBUSY;
		goto unlock;
	}
	omap_gpio_unidle(bank);
	bank->is_suspended = false;
unlock:
	raw_spin_unlock_irqrestore(&bank->lock, flags);

	return error;
}

#ifdef CONFIG_ARCH_OMAP2PLUS
1673
static const struct dev_pm_ops gpio_pm_ops = {
1674 1675
	SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
									NULL)
1676
};
1677 1678 1679
#else
static const struct dev_pm_ops gpio_pm_ops;
#endif	/* CONFIG_ARCH_OMAP2PLUS */
1680

1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
#if defined(CONFIG_OF)
static struct omap_gpio_reg_offs omap2_gpio_regs = {
	.revision =		OMAP24XX_GPIO_REVISION,
	.direction =		OMAP24XX_GPIO_OE,
	.datain =		OMAP24XX_GPIO_DATAIN,
	.dataout =		OMAP24XX_GPIO_DATAOUT,
	.set_dataout =		OMAP24XX_GPIO_SETDATAOUT,
	.clr_dataout =		OMAP24XX_GPIO_CLEARDATAOUT,
	.irqstatus =		OMAP24XX_GPIO_IRQSTATUS1,
	.irqstatus2 =		OMAP24XX_GPIO_IRQSTATUS2,
	.irqenable =		OMAP24XX_GPIO_IRQENABLE1,
	.irqenable2 =		OMAP24XX_GPIO_IRQENABLE2,
	.set_irqenable =	OMAP24XX_GPIO_SETIRQENABLE1,
	.clr_irqenable =	OMAP24XX_GPIO_CLEARIRQENABLE1,
	.debounce =		OMAP24XX_GPIO_DEBOUNCE_VAL,
	.debounce_en =		OMAP24XX_GPIO_DEBOUNCE_EN,
	.ctrl =			OMAP24XX_GPIO_CTRL,
	.wkup_en =		OMAP24XX_GPIO_WAKE_EN,
	.leveldetect0 =		OMAP24XX_GPIO_LEVELDETECT0,
	.leveldetect1 =		OMAP24XX_GPIO_LEVELDETECT1,
	.risingdetect =		OMAP24XX_GPIO_RISINGDETECT,
	.fallingdetect =	OMAP24XX_GPIO_FALLINGDETECT,
};

static struct omap_gpio_reg_offs omap4_gpio_regs = {
	.revision =		OMAP4_GPIO_REVISION,
	.direction =		OMAP4_GPIO_OE,
	.datain =		OMAP4_GPIO_DATAIN,
	.dataout =		OMAP4_GPIO_DATAOUT,
	.set_dataout =		OMAP4_GPIO_SETDATAOUT,
	.clr_dataout =		OMAP4_GPIO_CLEARDATAOUT,
	.irqstatus =		OMAP4_GPIO_IRQSTATUS0,
	.irqstatus2 =		OMAP4_GPIO_IRQSTATUS1,
	.irqenable =		OMAP4_GPIO_IRQSTATUSSET0,
	.irqenable2 =		OMAP4_GPIO_IRQSTATUSSET1,
	.set_irqenable =	OMAP4_GPIO_IRQSTATUSSET0,
	.clr_irqenable =	OMAP4_GPIO_IRQSTATUSCLR0,
	.debounce =		OMAP4_GPIO_DEBOUNCINGTIME,
	.debounce_en =		OMAP4_GPIO_DEBOUNCENABLE,
	.ctrl =			OMAP4_GPIO_CTRL,
	.wkup_en =		OMAP4_GPIO_IRQWAKEN0,
	.leveldetect0 =		OMAP4_GPIO_LEVELDETECT0,
	.leveldetect1 =		OMAP4_GPIO_LEVELDETECT1,
	.risingdetect =		OMAP4_GPIO_RISINGDETECT,
	.fallingdetect =	OMAP4_GPIO_FALLINGDETECT,
};

1728 1729 1730 1731 1732
/*
 * Note that omap2 does not currently support idle modes with context loss so
 * no need to add OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER quirk flag to save
 * and restore context.
 */
1733
static const struct omap_gpio_platform_data omap2_pdata = {
1734 1735 1736 1737 1738
	.regs = &omap2_gpio_regs,
	.bank_width = 32,
	.dbck_flag = false,
};

1739
static const struct omap_gpio_platform_data omap3_pdata = {
1740 1741 1742
	.regs = &omap2_gpio_regs,
	.bank_width = 32,
	.dbck_flag = true,
1743
	.quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
1744 1745
};

1746
static const struct omap_gpio_platform_data omap4_pdata = {
1747 1748 1749
	.regs = &omap4_gpio_regs,
	.bank_width = 32,
	.dbck_flag = true,
1750
	.quirks = OMAP_GPIO_QUIRK_IDLE_REMOVE_TRIGGER,
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
};

static const struct of_device_id omap_gpio_match[] = {
	{
		.compatible = "ti,omap4-gpio",
		.data = &omap4_pdata,
	},
	{
		.compatible = "ti,omap3-gpio",
		.data = &omap3_pdata,
	},
	{
		.compatible = "ti,omap2-gpio",
		.data = &omap2_pdata,
	},
	{ },
};
MODULE_DEVICE_TABLE(of, omap_gpio_match);
#endif

1771 1772
static struct platform_driver omap_gpio_driver = {
	.probe		= omap_gpio_probe,
1773
	.remove		= omap_gpio_remove,
1774 1775
	.driver		= {
		.name	= "omap_gpio",
1776
		.pm	= &gpio_pm_ops,
1777
		.of_match_table = of_match_ptr(omap_gpio_match),
1778 1779 1780
	},
};

1781
/*
1782 1783 1784
 * gpio driver register needs to be done before
 * machine_init functions access gpio APIs.
 * Hence omap_gpio_drv_reg() is a postcore_initcall.
1785
 */
1786
static int __init omap_gpio_drv_reg(void)
1787
{
1788
	return platform_driver_register(&omap_gpio_driver);
1789
}
1790
postcore_initcall(omap_gpio_drv_reg);
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800

static void __exit omap_gpio_exit(void)
{
	platform_driver_unregister(&omap_gpio_driver);
}
module_exit(omap_gpio_exit);

MODULE_DESCRIPTION("omap gpio driver");
MODULE_ALIAS("platform:gpio-omap");
MODULE_LICENSE("GPL v2");