gpio-omap.c 43.9 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/device.h>
23
#include <linux/pm_runtime.h>
24
#include <linux/pm.h>
25 26
#include <linux/of.h>
#include <linux/of_device.h>
27
#include <linux/gpio.h>
28
#include <linux/bitops.h>
29
#include <linux/platform_data/gpio-omap.h>
30

31
#define OFF_MODE	1
32
#define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF
33

34 35
static LIST_HEAD(omap_gpio_list);

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
struct gpio_bank {
52
	struct list_head node;
53
	void __iomem *base;
54
	u16 irq;
55 56
	u32 non_wakeup_gpios;
	u32 enabled_non_wakeup_gpios;
57
	struct gpio_regs context;
58
	u32 saved_datain;
59
	u32 level_mask;
60
	u32 toggle_mask;
61
	raw_spinlock_t lock;
62
	raw_spinlock_t wa_lock;
D
David Brownell 已提交
63
	struct gpio_chip chip;
64
	struct clk *dbck;
C
Charulatha V 已提交
65
	u32 mod_usage;
66
	u32 irq_usage;
67
	u32 dbck_enable_mask;
68
	bool dbck_enabled;
69
	struct device *dev;
70
	bool is_mpuio;
71
	bool dbck_flag;
72
	bool loses_context;
73
	bool context_valid;
74
	int stride;
75
	u32 width;
76
	int context_loss_count;
77 78
	int power_mode;
	bool workaround_enabled;
79

80
	void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable);
81
	int (*get_context_loss_count)(struct device *dev);
82 83

	struct omap_gpio_reg_offs *regs;
84 85
};

86
#define GPIO_MOD_CTRL_BIT	BIT(0)
87

88
#define BANK_USED(bank) (bank->mod_usage || bank->irq_usage)
89
#define LINE_USED(line, offset) (line & (BIT(offset)))
90

91 92
static void omap_gpio_unmask_irq(struct irq_data *d);

93
static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d)
94
{
95 96
	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
	return container_of(chip, struct gpio_bank, chip);
97 98
}

99 100
static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio,
				    int is_input)
101
{
102
	void __iomem *reg = bank->base;
103 104
	u32 l;

105
	reg += bank->regs->direction;
106
	l = readl_relaxed(reg);
107
	if (is_input)
108
		l |= BIT(gpio);
109
	else
110
		l &= ~(BIT(gpio));
111
	writel_relaxed(l, reg);
112
	bank->context.oe = l;
113 114
}

115 116

/* set data out value using dedicate set/clear register */
117
static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset,
118
				      int enable)
119
{
120
	void __iomem *reg = bank->base;
121
	u32 l = BIT(offset);
122

123
	if (enable) {
124
		reg += bank->regs->set_dataout;
125 126
		bank->context.dataout |= l;
	} else {
127
		reg += bank->regs->clr_dataout;
128 129
		bank->context.dataout &= ~l;
	}
130

131
	writel_relaxed(l, reg);
132 133
}

134
/* set data out value using mask register */
135
static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset,
136
				       int enable)
137
{
138
	void __iomem *reg = bank->base + bank->regs->dataout;
139
	u32 gpio_bit = BIT(offset);
140
	u32 l;
141

142
	l = readl_relaxed(reg);
143 144 145 146
	if (enable)
		l |= gpio_bit;
	else
		l &= ~gpio_bit;
147
	writel_relaxed(l, reg);
148
	bank->context.dataout = l;
149 150
}

151
static int omap_get_gpio_datain(struct gpio_bank *bank, int offset)
152
{
153
	void __iomem *reg = bank->base + bank->regs->datain;
154

155
	return (readl_relaxed(reg) & (BIT(offset))) != 0;
156
}
157

158
static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset)
159
{
160
	void __iomem *reg = bank->base + bank->regs->dataout;
161

162
	return (readl_relaxed(reg) & (BIT(offset))) != 0;
163 164
}

165
static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set)
166
{
167
	int l = readl_relaxed(base + reg);
168

169
	if (set)
170 171 172 173
		l |= mask;
	else
		l &= ~mask;

174
	writel_relaxed(l, base + reg);
175
}
176

177
static inline void omap_gpio_dbck_enable(struct gpio_bank *bank)
178 179
{
	if (bank->dbck_enable_mask && !bank->dbck_enabled) {
180
		clk_enable(bank->dbck);
181
		bank->dbck_enabled = true;
182

183
		writel_relaxed(bank->dbck_enable_mask,
184
			     bank->base + bank->regs->debounce_en);
185 186 187
	}
}

188
static inline void omap_gpio_dbck_disable(struct gpio_bank *bank)
189 190
{
	if (bank->dbck_enable_mask && bank->dbck_enabled) {
191 192 193 194 195
		/*
		 * 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.
		 */
196
		writel_relaxed(0, bank->base + bank->regs->debounce_en);
197

198
		clk_disable(bank->dbck);
199 200 201 202
		bank->dbck_enabled = false;
	}
}

203
/**
204
 * omap2_set_gpio_debounce - low level gpio debounce time
205
 * @bank: the gpio bank we're acting upon
206
 * @offset: the gpio number on this @bank
207 208
 * @debounce: debounce time to use
 *
209 210 211
 * 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.
212
 */
213
static void omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset,
214
				    unsigned debounce)
215
{
216
	void __iomem		*reg;
217 218
	u32			val;
	u32			l;
219
	bool			enable = !!debounce;
220

221 222 223
	if (!bank->dbck_flag)
		return;

224 225 226 227
	if (enable) {
		debounce = DIV_ROUND_UP(debounce, 31) - 1;
		debounce &= OMAP4_GPIO_DEBOUNCINGTIME_MASK;
	}
228

229
	l = BIT(offset);
230

231
	clk_enable(bank->dbck);
232
	reg = bank->base + bank->regs->debounce;
233
	writel_relaxed(debounce, reg);
234

235
	reg = bank->base + bank->regs->debounce_en;
236
	val = readl_relaxed(reg);
237

238
	if (enable)
239
		val |= l;
240
	else
241
		val &= ~l;
242
	bank->dbck_enable_mask = val;
243

244
	writel_relaxed(val, reg);
245
	clk_disable(bank->dbck);
246 247 248 249 250 251 252 253
	/*
	 * 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.
	 */
254
	omap_gpio_dbck_enable(bank);
255 256 257 258
	if (bank->dbck_enable_mask) {
		bank->context.debounce = debounce;
		bank->context.debounce_en = val;
	}
259 260
}

261
/**
262
 * omap_clear_gpio_debounce - clear debounce settings for a gpio
263
 * @bank: the gpio bank we're acting upon
264
 * @offset: the gpio number on this @bank
265 266 267 268 269 270
 *
 * 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.
 */
271
static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset)
272
{
273
	u32 gpio_bit = BIT(offset);
274 275 276 277 278 279 280 281 282

	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;
283
        writel_relaxed(bank->context.debounce_en,
284 285 286 287
		     bank->base + bank->regs->debounce_en);

	if (!bank->dbck_enable_mask) {
		bank->context.debounce = 0;
288
		writel_relaxed(bank->context.debounce, bank->base +
289
			     bank->regs->debounce);
290
		clk_disable(bank->dbck);
291 292 293 294
		bank->dbck_enabled = false;
	}
}

295
static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio,
296
						unsigned trigger)
297
{
298
	void __iomem *base = bank->base;
299
	u32 gpio_bit = BIT(gpio);
300

301 302 303 304 305 306 307 308
	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);
309

310
	bank->context.leveldetect0 =
311
			readl_relaxed(bank->base + bank->regs->leveldetect0);
312
	bank->context.leveldetect1 =
313
			readl_relaxed(bank->base + bank->regs->leveldetect1);
314
	bank->context.risingdetect =
315
			readl_relaxed(bank->base + bank->regs->risingdetect);
316
	bank->context.fallingdetect =
317
			readl_relaxed(bank->base + bank->regs->fallingdetect);
318 319

	if (likely(!(bank->non_wakeup_gpios & gpio_bit))) {
320
		omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0);
321
		bank->context.wake_en =
322
			readl_relaxed(bank->base + bank->regs->wkup_en);
323
	}
324

325
	/* This part needs to be executed always for OMAP{34xx, 44xx} */
326 327 328 329 330 331 332
	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;
		}

333 334 335 336 337 338 339
		/*
		 * 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)
340 341 342 343
			bank->enabled_non_wakeup_gpios |= gpio_bit;
		else
			bank->enabled_non_wakeup_gpios &= ~gpio_bit;
	}
344

345
exit:
346
	bank->level_mask =
347 348
		readl_relaxed(bank->base + bank->regs->leveldetect0) |
		readl_relaxed(bank->base + bank->regs->leveldetect1);
349 350
}

351
#ifdef CONFIG_ARCH_OMAP1
352 353 354 355
/*
 * 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.
 */
356
static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio)
357 358 359 360
{
	void __iomem *reg = bank->base;
	u32 l = 0;

361
	if (!bank->regs->irqctrl)
362
		return;
363 364

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

366
	l = readl_relaxed(reg);
367
	if ((l >> gpio) & 1)
368
		l &= ~(BIT(gpio));
369
	else
370
		l |= BIT(gpio);
371

372
	writel_relaxed(l, reg);
373
}
374
#else
375
static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {}
376
#endif
377

378 379
static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio,
				    unsigned trigger)
380 381
{
	void __iomem *reg = bank->base;
382
	void __iomem *base = bank->base;
383
	u32 l = 0;
384

385
	if (bank->regs->leveldetect0 && bank->regs->wkup_en) {
386
		omap_set_gpio_trigger(bank, gpio, trigger);
387 388 389
	} else if (bank->regs->irqctrl) {
		reg += bank->regs->irqctrl;

390
		l = readl_relaxed(reg);
391
		if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH)
392
			bank->toggle_mask |= BIT(gpio);
393
		if (trigger & IRQ_TYPE_EDGE_RISING)
394
			l |= BIT(gpio);
395
		else if (trigger & IRQ_TYPE_EDGE_FALLING)
396
			l &= ~(BIT(gpio));
397
		else
398 399
			return -EINVAL;

400
		writel_relaxed(l, reg);
401
	} else if (bank->regs->edgectrl1) {
402
		if (gpio & 0x08)
403
			reg += bank->regs->edgectrl2;
404
		else
405 406
			reg += bank->regs->edgectrl1;

407
		gpio &= 0x07;
408
		l = readl_relaxed(reg);
409
		l &= ~(3 << (gpio << 1));
410
		if (trigger & IRQ_TYPE_EDGE_RISING)
411
			l |= 2 << (gpio << 1);
412
		if (trigger & IRQ_TYPE_EDGE_FALLING)
413
			l |= BIT(gpio << 1);
414 415

		/* Enable wake-up during idle for dynamic tick */
416
		omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger);
417
		bank->context.wake_en =
418 419
			readl_relaxed(bank->base + bank->regs->wkup_en);
		writel_relaxed(l, reg);
420
	}
421
	return 0;
422 423
}

424
static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset)
425 426 427 428 429
{
	if (bank->regs->pinctrl) {
		void __iomem *reg = bank->base + bank->regs->pinctrl;

		/* Claim the pin for MPU */
430
		writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg);
431 432 433 434 435 436
	}

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

437
		ctrl = readl_relaxed(reg);
438 439
		/* Module is enabled, clocks are not gated */
		ctrl &= ~GPIO_MOD_CTRL_BIT;
440
		writel_relaxed(ctrl, reg);
441 442 443 444
		bank->context.ctrl = ctrl;
	}
}

445
static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset)
446 447 448 449 450 451 452
{
	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 */
453
		omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0);
454
		bank->context.wake_en =
455
			readl_relaxed(bank->base + bank->regs->wkup_en);
456 457 458 459 460 461
	}

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

462
		ctrl = readl_relaxed(reg);
463 464
		/* Module is disabled, clocks are gated */
		ctrl |= GPIO_MOD_CTRL_BIT;
465
		writel_relaxed(ctrl, reg);
466 467 468 469
		bank->context.ctrl = ctrl;
	}
}

470
static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset)
471 472 473
{
	void __iomem *reg = bank->base + bank->regs->direction;

474
	return readl_relaxed(reg) & BIT(offset);
475 476
}

477
static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset)
478 479 480 481 482
{
	if (!LINE_USED(bank->mod_usage, offset)) {
		omap_enable_gpio_module(bank, offset);
		omap_set_gpio_direction(bank, offset, 1);
	}
483
	bank->irq_usage |= BIT(offset);
484 485
}

486
static int omap_gpio_irq_type(struct irq_data *d, unsigned type)
487
{
488
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
489
	int retval;
D
David Brownell 已提交
490
	unsigned long flags;
491
	unsigned offset = d->hwirq;
492

493
	if (type & ~IRQ_TYPE_SENSE_MASK)
494
		return -EINVAL;
495

496 497
	if (!bank->regs->leveldetect0 &&
		(type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH)))
498 499
		return -EINVAL;

500
	raw_spin_lock_irqsave(&bank->lock, flags);
501
	retval = omap_set_gpio_triggering(bank, offset, type);
502
	if (retval) {
503
		raw_spin_unlock_irqrestore(&bank->lock, flags);
504
		goto error;
505
	}
506
	omap_gpio_init_irq(bank, offset);
507
	if (!omap_gpio_is_input(bank, offset)) {
508
		raw_spin_unlock_irqrestore(&bank->lock, flags);
509 510
		retval = -EINVAL;
		goto error;
511
	}
512
	raw_spin_unlock_irqrestore(&bank->lock, flags);
513 514

	if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
515
		irq_set_handler_locked(d, handle_level_irq);
516
	else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
517
		irq_set_handler_locked(d, handle_edge_irq);
518

519 520 521
	return 0;

error:
522
	return retval;
523 524
}

525
static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
526
{
527
	void __iomem *reg = bank->base;
528

529
	reg += bank->regs->irqstatus;
530
	writel_relaxed(gpio_mask, reg);
531 532

	/* Workaround for clearing DSP GPIO interrupts to allow retention */
533 534
	if (bank->regs->irqstatus2) {
		reg = bank->base + bank->regs->irqstatus2;
535
		writel_relaxed(gpio_mask, reg);
536
	}
537 538

	/* Flush posted write for the irq status to avoid spurious interrupts */
539
	readl_relaxed(reg);
540 541
}

542 543
static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank,
					     unsigned offset)
544
{
545
	omap_clear_gpio_irqbank(bank, BIT(offset));
546 547
}

548
static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank)
549 550
{
	void __iomem *reg = bank->base;
551
	u32 l;
552
	u32 mask = (BIT(bank->width)) - 1;
553

554
	reg += bank->regs->irqenable;
555
	l = readl_relaxed(reg);
556
	if (bank->regs->irqenable_inv)
557 558 559
		l = ~l;
	l &= mask;
	return l;
560 561
}

562
static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
563
{
564
	void __iomem *reg = bank->base;
565 566
	u32 l;

567 568 569
	if (bank->regs->set_irqenable) {
		reg += bank->regs->set_irqenable;
		l = gpio_mask;
570
		bank->context.irqenable1 |= gpio_mask;
571 572
	} else {
		reg += bank->regs->irqenable;
573
		l = readl_relaxed(reg);
574 575
		if (bank->regs->irqenable_inv)
			l &= ~gpio_mask;
576 577
		else
			l |= gpio_mask;
578
		bank->context.irqenable1 = l;
579 580
	}

581
	writel_relaxed(l, reg);
582 583
}

584
static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask)
585 586 587 588 589 590
{
	void __iomem *reg = bank->base;
	u32 l;

	if (bank->regs->clr_irqenable) {
		reg += bank->regs->clr_irqenable;
591
		l = gpio_mask;
592
		bank->context.irqenable1 &= ~gpio_mask;
593 594
	} else {
		reg += bank->regs->irqenable;
595
		l = readl_relaxed(reg);
596
		if (bank->regs->irqenable_inv)
597
			l |= gpio_mask;
598
		else
599
			l &= ~gpio_mask;
600
		bank->context.irqenable1 = l;
601
	}
602

603
	writel_relaxed(l, reg);
604 605
}

606 607
static inline void omap_set_gpio_irqenable(struct gpio_bank *bank,
					   unsigned offset, int enable)
608
{
609
	if (enable)
610
		omap_enable_gpio_irqbank(bank, BIT(offset));
611
	else
612
		omap_disable_gpio_irqbank(bank, BIT(offset));
613 614
}

615 616 617 618 619 620 621 622
/*
 * 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.
 */
623 624
static int omap_set_gpio_wakeup(struct gpio_bank *bank, unsigned offset,
				int enable)
625
{
626
	u32 gpio_bit = BIT(offset);
627
	unsigned long flags;
D
David Brownell 已提交
628

629
	if (bank->non_wakeup_gpios & gpio_bit) {
630
		dev_err(bank->dev,
631 632
			"Unable to modify wakeup on non-wakeup GPIO%d\n",
			offset);
633 634
		return -EINVAL;
	}
635

636
	raw_spin_lock_irqsave(&bank->lock, flags);
637
	if (enable)
638
		bank->context.wake_en |= gpio_bit;
639
	else
640
		bank->context.wake_en &= ~gpio_bit;
641

642
	writel_relaxed(bank->context.wake_en, bank->base + bank->regs->wkup_en);
643
	raw_spin_unlock_irqrestore(&bank->lock, flags);
644 645

	return 0;
646 647 648
}

/* Use disable_irq_wake() and enable_irq_wake() functions from drivers */
649
static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable)
650
{
651
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
652
	unsigned offset = d->hwirq;
653 654 655 656 657
	int ret;

	ret = omap_set_gpio_wakeup(bank, offset, enable);
	if (!ret)
		ret = irq_set_irq_wake(bank->irq, enable);
658

659
	return ret;
660 661
}

662
static int omap_gpio_request(struct gpio_chip *chip, unsigned offset)
663
{
664
	struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
D
David Brownell 已提交
665
	unsigned long flags;
D
David Brownell 已提交
666

667 668 669 670
	/*
	 * If this is the first gpio_request for the bank,
	 * enable the bank module.
	 */
671
	if (!BANK_USED(bank))
672
		pm_runtime_get_sync(bank->dev);
673

674
	raw_spin_lock_irqsave(&bank->lock, flags);
675
	omap_enable_gpio_module(bank, offset);
676
	bank->mod_usage |= BIT(offset);
677
	raw_spin_unlock_irqrestore(&bank->lock, flags);
678 679 680 681

	return 0;
}

682
static void omap_gpio_free(struct gpio_chip *chip, unsigned offset)
683
{
684
	struct gpio_bank *bank = container_of(chip, struct gpio_bank, chip);
D
David Brownell 已提交
685
	unsigned long flags;
686

687
	raw_spin_lock_irqsave(&bank->lock, flags);
688
	bank->mod_usage &= ~(BIT(offset));
689 690 691 692
	if (!LINE_USED(bank->irq_usage, offset)) {
		omap_set_gpio_direction(bank, offset, 1);
		omap_clear_gpio_debounce(bank, offset);
	}
693
	omap_disable_gpio_module(bank, offset);
694
	raw_spin_unlock_irqrestore(&bank->lock, flags);
695 696 697 698 699

	/*
	 * If this is the last gpio to be freed in the bank,
	 * disable the bank module.
	 */
700
	if (!BANK_USED(bank))
701
		pm_runtime_put(bank->dev);
702 703 704 705 706 707 708 709 710 711 712
}

/*
 * 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.
 */
713
static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank)
714
{
715
	void __iomem *isr_reg = NULL;
716
	u32 isr;
717
	unsigned int bit;
718 719
	struct gpio_bank *bank = gpiobank;
	unsigned long wa_lock_flags;
720
	unsigned long lock_flags;
721

722
	isr_reg = bank->base + bank->regs->irqstatus;
723 724 725
	if (WARN_ON(!isr_reg))
		goto exit;

726 727
	pm_runtime_get_sync(bank->dev);

728
	while (1) {
729
		u32 isr_saved, level_mask = 0;
730
		u32 enabled;
731

732 733
		raw_spin_lock_irqsave(&bank->lock, lock_flags);

734
		enabled = omap_get_gpio_irqbank_mask(bank);
735
		isr_saved = isr = readl_relaxed(isr_reg) & enabled;
736

737
		if (bank->level_mask)
738
			level_mask = bank->level_mask & enabled;
739 740 741 742

		/* clear edge sensitive interrupts before handler(s) are
		called so that we don't miss any interrupt occurred while
		executing them */
743 744 745
		omap_disable_gpio_irqbank(bank, isr_saved & ~level_mask);
		omap_clear_gpio_irqbank(bank, isr_saved & ~level_mask);
		omap_enable_gpio_irqbank(bank, isr_saved & ~level_mask);
746

747 748
		raw_spin_unlock_irqrestore(&bank->lock, lock_flags);

749 750 751
		if (!isr)
			break;

752 753
		while (isr) {
			bit = __ffs(isr);
754
			isr &= ~(BIT(bit));
755

756
			raw_spin_lock_irqsave(&bank->lock, lock_flags);
757 758 759 760 761 762 763
			/*
			 * 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.
			 */
764
			if (bank->toggle_mask & (BIT(bit)))
765
				omap_toggle_gpio_edge_triggering(bank, bit);
766

767 768
			raw_spin_unlock_irqrestore(&bank->lock, lock_flags);

769 770
			raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags);

771 772
			generic_handle_irq(irq_find_mapping(bank->chip.irqdomain,
							    bit));
773 774 775

			raw_spin_unlock_irqrestore(&bank->wa_lock,
						   wa_lock_flags);
776
		}
777
	}
778
exit:
779
	pm_runtime_put(bank->dev);
780
	return IRQ_HANDLED;
781 782
}

783 784 785 786
static unsigned int omap_gpio_irq_startup(struct irq_data *d)
{
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
	unsigned long flags;
787
	unsigned offset = d->hwirq;
788

789
	raw_spin_lock_irqsave(&bank->lock, flags);
790 791 792 793 794 795 796 797

	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);

798
	raw_spin_unlock_irqrestore(&bank->lock, flags);
799 800 801
	omap_gpio_unmask_irq(d);

	return 0;
802
err:
803
	raw_spin_unlock_irqrestore(&bank->lock, flags);
804
	return -EINVAL;
805 806
}

807
static void omap_gpio_irq_shutdown(struct irq_data *d)
808
{
809
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
810
	unsigned long flags;
811
	unsigned offset = d->hwirq;
812

813
	raw_spin_lock_irqsave(&bank->lock, flags);
814
	bank->irq_usage &= ~(BIT(offset));
815 816 817 818 819
	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);
820
	omap_disable_gpio_module(bank, offset);
821
	raw_spin_unlock_irqrestore(&bank->lock, flags);
822 823 824 825 826 827 828 829 830 831 832 833 834
}

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

	if (!BANK_USED(bank))
		pm_runtime_get_sync(bank->dev);
}

static void gpio_irq_bus_sync_unlock(struct irq_data *data)
{
	struct gpio_bank *bank = omap_irq_data_get_bank(data);
835 836 837 838 839 840 841

	/*
	 * If this is the last IRQ to be freed in the bank,
	 * disable the bank module.
	 */
	if (!BANK_USED(bank))
		pm_runtime_put(bank->dev);
842 843
}

844
static void omap_gpio_ack_irq(struct irq_data *d)
845
{
846
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
847
	unsigned offset = d->hwirq;
848

849
	omap_clear_gpio_irqstatus(bank, offset);
850 851
}

852
static void omap_gpio_mask_irq(struct irq_data *d)
853
{
854
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
855
	unsigned offset = d->hwirq;
856
	unsigned long flags;
857

858
	raw_spin_lock_irqsave(&bank->lock, flags);
859 860
	omap_set_gpio_irqenable(bank, offset, 0);
	omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE);
861
	raw_spin_unlock_irqrestore(&bank->lock, flags);
862 863
}

864
static void omap_gpio_unmask_irq(struct irq_data *d)
865
{
866
	struct gpio_bank *bank = omap_irq_data_get_bank(d);
867
	unsigned offset = d->hwirq;
868
	u32 trigger = irqd_get_trigger_type(d);
869
	unsigned long flags;
870

871
	raw_spin_lock_irqsave(&bank->lock, flags);
872
	if (trigger)
873
		omap_set_gpio_triggering(bank, offset, trigger);
874 875 876

	/* For level-triggered GPIOs, the clearing must be done after
	 * the HW source is cleared, thus after the handler has run */
877 878 879
	if (bank->level_mask & BIT(offset)) {
		omap_set_gpio_irqenable(bank, offset, 0);
		omap_clear_gpio_irqstatus(bank, offset);
880
	}
881

882
	omap_set_gpio_irqenable(bank, offset, 1);
883
	raw_spin_unlock_irqrestore(&bank->lock, flags);
884 885
}

886 887
/*---------------------------------------------------------------------*/

888
static int omap_mpuio_suspend_noirq(struct device *dev)
D
David Brownell 已提交
889
{
890
	struct platform_device *pdev = to_platform_device(dev);
D
David Brownell 已提交
891
	struct gpio_bank	*bank = platform_get_drvdata(pdev);
892 893
	void __iomem		*mask_reg = bank->base +
					OMAP_MPUIO_GPIO_MASKIT / bank->stride;
D
David Brownell 已提交
894
	unsigned long		flags;
D
David Brownell 已提交
895

896
	raw_spin_lock_irqsave(&bank->lock, flags);
897
	writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg);
898
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
899 900 901 902

	return 0;
}

903
static int omap_mpuio_resume_noirq(struct device *dev)
D
David Brownell 已提交
904
{
905
	struct platform_device *pdev = to_platform_device(dev);
D
David Brownell 已提交
906
	struct gpio_bank	*bank = platform_get_drvdata(pdev);
907 908
	void __iomem		*mask_reg = bank->base +
					OMAP_MPUIO_GPIO_MASKIT / bank->stride;
D
David Brownell 已提交
909
	unsigned long		flags;
D
David Brownell 已提交
910

911
	raw_spin_lock_irqsave(&bank->lock, flags);
912
	writel_relaxed(bank->context.wake_en, mask_reg);
913
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
914 915 916 917

	return 0;
}

918
static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
919 920 921 922
	.suspend_noirq = omap_mpuio_suspend_noirq,
	.resume_noirq = omap_mpuio_resume_noirq,
};

923
/* use platform_driver for this. */
D
David Brownell 已提交
924 925 926
static struct platform_driver omap_mpuio_driver = {
	.driver		= {
		.name	= "mpuio",
927
		.pm	= &omap_mpuio_dev_pm_ops,
D
David Brownell 已提交
928 929 930 931 932 933 934 935 936 937 938 939
	},
};

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

940
static inline void omap_mpuio_init(struct gpio_bank *bank)
D
David Brownell 已提交
941
{
942
	platform_set_drvdata(&omap_mpuio_device, bank);
943

D
David Brownell 已提交
944 945 946 947
	if (platform_driver_register(&omap_mpuio_driver) == 0)
		(void) platform_device_register(&omap_mpuio_device);
}

948
/*---------------------------------------------------------------------*/
949

950
static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
951 952 953 954 955 956 957 958
{
	struct gpio_bank *bank;
	unsigned long flags;
	void __iomem *reg;
	int dir;

	bank = container_of(chip, struct gpio_bank, chip);
	reg = bank->base + bank->regs->direction;
959
	raw_spin_lock_irqsave(&bank->lock, flags);
960
	dir = !!(readl_relaxed(reg) & BIT(offset));
961
	raw_spin_unlock_irqrestore(&bank->lock, flags);
962 963 964
	return dir;
}

965
static int omap_gpio_input(struct gpio_chip *chip, unsigned offset)
D
David Brownell 已提交
966 967 968 969 970
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
971
	raw_spin_lock_irqsave(&bank->lock, flags);
972
	omap_set_gpio_direction(bank, offset, 1);
973
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
974 975 976
	return 0;
}

977
static int omap_gpio_get(struct gpio_chip *chip, unsigned offset)
D
David Brownell 已提交
978
{
979 980
	struct gpio_bank *bank;

C
Charulatha V 已提交
981
	bank = container_of(chip, struct gpio_bank, chip);
982

983
	if (omap_gpio_is_input(bank, offset))
984
		return omap_get_gpio_datain(bank, offset);
985
	else
986
		return omap_get_gpio_dataout(bank, offset);
D
David Brownell 已提交
987 988
}

989
static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value)
D
David Brownell 已提交
990 991 992 993 994
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
995
	raw_spin_lock_irqsave(&bank->lock, flags);
996
	bank->set_dataout(bank, offset, value);
997
	omap_set_gpio_direction(bank, offset, 0);
998
	raw_spin_unlock_irqrestore(&bank->lock, flags);
999
	return 0;
D
David Brownell 已提交
1000 1001
}

1002 1003
static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset,
			      unsigned debounce)
1004 1005 1006 1007 1008
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
1009

1010
	raw_spin_lock_irqsave(&bank->lock, flags);
1011
	omap2_set_gpio_debounce(bank, offset, debounce);
1012
	raw_spin_unlock_irqrestore(&bank->lock, flags);
1013 1014 1015 1016

	return 0;
}

1017
static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
D
David Brownell 已提交
1018 1019 1020 1021 1022
{
	struct gpio_bank *bank;
	unsigned long flags;

	bank = container_of(chip, struct gpio_bank, chip);
1023
	raw_spin_lock_irqsave(&bank->lock, flags);
1024
	bank->set_dataout(bank, offset, value);
1025
	raw_spin_unlock_irqrestore(&bank->lock, flags);
D
David Brownell 已提交
1026 1027 1028 1029
}

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

1030
static void __init omap_gpio_show_rev(struct gpio_bank *bank)
T
Tony Lindgren 已提交
1031
{
1032
	static bool called;
T
Tony Lindgren 已提交
1033 1034
	u32 rev;

1035
	if (called || bank->regs->revision == USHRT_MAX)
T
Tony Lindgren 已提交
1036 1037
		return;

1038
	rev = readw_relaxed(bank->base + bank->regs->revision);
1039
	pr_info("OMAP GPIO hardware version %d.%d\n",
T
Tony Lindgren 已提交
1040
		(rev >> 4) & 0x0f, rev & 0x0f);
1041 1042

	called = true;
T
Tony Lindgren 已提交
1043 1044
}

1045
static void omap_gpio_mod_init(struct gpio_bank *bank)
1046
{
1047 1048
	void __iomem *base = bank->base;
	u32 l = 0xffffffff;
1049

1050 1051 1052
	if (bank->width == 16)
		l = 0xffff;

1053
	if (bank->is_mpuio) {
1054
		writel_relaxed(l, bank->base + bank->regs->irqenable);
1055
		return;
1056
	}
1057

1058 1059 1060 1061
	omap_gpio_rmw(base, bank->regs->irqenable, l,
		      bank->regs->irqenable_inv);
	omap_gpio_rmw(base, bank->regs->irqstatus, l,
		      !bank->regs->irqenable_inv);
1062
	if (bank->regs->debounce_en)
1063
		writel_relaxed(0, base + bank->regs->debounce_en);
1064

1065
	/* Save OE default value (0xffffffff) in the context */
1066
	bank->context.oe = readl_relaxed(bank->base + bank->regs->direction);
1067 1068
	 /* Initialize interface clk ungated, module enabled */
	if (bank->regs->ctrl)
1069
		writel_relaxed(0, base + bank->regs->ctrl);
1070 1071
}

N
Nishanth Menon 已提交
1072
static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc)
1073 1074
{
	static int gpio;
1075
	int irq_base = 0;
1076
	int ret;
1077 1078 1079 1080 1081 1082 1083

	/*
	 * 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;
1084 1085 1086 1087 1088 1089
	bank->chip.get_direction = omap_gpio_get_direction;
	bank->chip.direction_input = omap_gpio_input;
	bank->chip.get = omap_gpio_get;
	bank->chip.direction_output = omap_gpio_output;
	bank->chip.set_debounce = omap_gpio_debounce;
	bank->chip.set = omap_gpio_set;
1090
	if (bank->is_mpuio) {
1091
		bank->chip.label = "mpuio";
1092 1093
		if (bank->regs->wkup_en)
			bank->chip.dev = &omap_mpuio_device.dev;
1094 1095 1096 1097 1098
		bank->chip.base = OMAP_MPUIO(0);
	} else {
		bank->chip.label = "gpio";
		bank->chip.base = gpio;
	}
1099
	bank->chip.ngpio = bank->width;
1100

1101 1102
	ret = gpiochip_add(&bank->chip);
	if (ret) {
1103
		dev_err(bank->dev, "Could not register gpio chip %d\n", ret);
1104 1105
		return ret;
	}
1106

1107 1108 1109
	if (!bank->is_mpuio)
		gpio += bank->width;

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
#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.
	 */
	irq_base = irq_alloc_descs(-1, 0, bank->width, 0);
	if (irq_base < 0) {
		dev_err(bank->dev, "Couldn't allocate IRQ numbers\n");
		return -ENODEV;
	}
#endif

1122 1123 1124 1125 1126 1127 1128 1129 1130
	/* MPUIO is a bit different, reading IRQ status clears it */
	if (bank->is_mpuio) {
		irqc->irq_ack = dummy_irq_chip.irq_ack;
		irqc->irq_mask = irq_gc_mask_set_bit;
		irqc->irq_unmask = irq_gc_mask_clr_bit;
		if (!bank->regs->wkup_en)
			irqc->irq_set_wake = NULL;
	}

N
Nishanth Menon 已提交
1131
	ret = gpiochip_irqchip_add(&bank->chip, irqc,
1132
				   irq_base, handle_bad_irq,
1133 1134 1135 1136
				   IRQ_TYPE_NONE);

	if (ret) {
		dev_err(bank->dev, "Couldn't add irqchip to gpiochip %d\n", ret);
1137
		gpiochip_remove(&bank->chip);
1138 1139 1140
		return -ENODEV;
	}

1141
	gpiochip_set_chained_irqchip(&bank->chip, irqc, bank->irq, NULL);
1142

1143 1144 1145 1146 1147 1148
	ret = devm_request_irq(bank->dev, bank->irq, omap_gpio_irq_handler,
			       0, dev_name(bank->dev), bank);
	if (ret)
		gpiochip_remove(&bank->chip);

	return ret;
1149 1150
}

1151 1152
static const struct of_device_id omap_gpio_match[];

B
Bill Pemberton 已提交
1153
static int omap_gpio_probe(struct platform_device *pdev)
1154
{
1155
	struct device *dev = &pdev->dev;
1156 1157
	struct device_node *node = dev->of_node;
	const struct of_device_id *match;
1158
	const struct omap_gpio_platform_data *pdata;
1159
	struct resource *res;
1160
	struct gpio_bank *bank;
N
Nishanth Menon 已提交
1161
	struct irq_chip *irqc;
1162
	int ret;
1163

1164 1165
	match = of_match_device(of_match_ptr(omap_gpio_match), dev);

J
Jingoo Han 已提交
1166
	pdata = match ? match->data : dev_get_platdata(dev);
1167
	if (!pdata)
1168
		return -EINVAL;
1169

1170
	bank = devm_kzalloc(dev, sizeof(struct gpio_bank), GFP_KERNEL);
1171
	if (!bank) {
1172
		dev_err(dev, "Memory alloc failed\n");
1173
		return -ENOMEM;
1174
	}
1175

N
Nishanth Menon 已提交
1176 1177 1178 1179
	irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL);
	if (!irqc)
		return -ENOMEM;

1180
	irqc->irq_startup = omap_gpio_irq_startup,
N
Nishanth Menon 已提交
1181 1182 1183 1184 1185 1186
	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,
1187 1188
	irqc->irq_bus_lock = omap_gpio_irq_bus_lock,
	irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock,
N
Nishanth Menon 已提交
1189 1190
	irqc->name = dev_name(&pdev->dev);

1191 1192 1193 1194 1195 1196 1197 1198
	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;
1199
	}
1200

1201
	bank->dev = dev;
1202
	bank->chip.dev = dev;
1203
	bank->chip.owner = THIS_MODULE;
1204
	bank->dbck_flag = pdata->dbck_flag;
1205
	bank->stride = pdata->bank_stride;
1206
	bank->width = pdata->bank_width;
1207
	bank->is_mpuio = pdata->is_mpuio;
1208
	bank->non_wakeup_gpios = pdata->non_wakeup_gpios;
1209
	bank->regs = pdata->regs;
1210 1211 1212
#ifdef CONFIG_OF_GPIO
	bank->chip.of_node = of_node_get(node);
#endif
1213 1214 1215 1216 1217
	if (node) {
		if (!of_property_read_bool(node, "ti,gpio-always-on"))
			bank->loses_context = true;
	} else {
		bank->loses_context = pdata->loses_context;
1218 1219 1220 1221

		if (bank->loses_context)
			bank->get_context_loss_count =
				pdata->get_context_loss_count;
1222 1223
	}

1224
	if (bank->regs->set_dataout && bank->regs->clr_dataout)
1225
		bank->set_dataout = omap_set_gpio_dataout_reg;
1226
	else
1227
		bank->set_dataout = omap_set_gpio_dataout_mask;
T
Tony Lindgren 已提交
1228

1229
	raw_spin_lock_init(&bank->lock);
1230
	raw_spin_lock_init(&bank->wa_lock);
T
Tony Lindgren 已提交
1231

1232 1233
	/* Static mapping, never released */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1234 1235 1236
	bank->base = devm_ioremap_resource(dev, res);
	if (IS_ERR(bank->base)) {
		return PTR_ERR(bank->base);
1237 1238
	}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
	if (bank->dbck_flag) {
		bank->dbck = devm_clk_get(bank->dev, "dbclk");
		if (IS_ERR(bank->dbck)) {
			dev_err(bank->dev,
				"Could not get gpio dbck. Disable debounce\n");
			bank->dbck_flag = false;
		} else {
			clk_prepare(bank->dbck);
		}
	}

1250 1251
	platform_set_drvdata(pdev, bank);

1252
	pm_runtime_enable(bank->dev);
1253
	pm_runtime_irq_safe(bank->dev);
1254 1255
	pm_runtime_get_sync(bank->dev);

1256
	if (bank->is_mpuio)
1257
		omap_mpuio_init(bank);
1258

1259
	omap_gpio_mod_init(bank);
1260

N
Nishanth Menon 已提交
1261
	ret = omap_gpio_chip_init(bank, irqc);
1262 1263 1264
	if (ret) {
		pm_runtime_put_sync(bank->dev);
		pm_runtime_disable(bank->dev);
1265
		return ret;
1266
	}
1267

1268
	omap_gpio_show_rev(bank);
T
Tony Lindgren 已提交
1269

1270 1271
	pm_runtime_put(bank->dev);

1272
	list_add_tail(&bank->node, &omap_gpio_list);
1273

1274
	return 0;
1275 1276
}

1277 1278 1279 1280 1281 1282 1283
static int omap_gpio_remove(struct platform_device *pdev)
{
	struct gpio_bank *bank = platform_get_drvdata(pdev);

	list_del(&bank->node);
	gpiochip_remove(&bank->chip);
	pm_runtime_disable(bank->dev);
1284 1285
	if (bank->dbck_flag)
		clk_unprepare(bank->dbck);
1286 1287 1288 1289

	return 0;
}

1290 1291
#ifdef CONFIG_ARCH_OMAP2PLUS

1292
#if defined(CONFIG_PM)
1293
static void omap_gpio_restore_context(struct gpio_bank *bank);
1294

1295
static int omap_gpio_runtime_suspend(struct device *dev)
1296
{
1297 1298 1299 1300
	struct platform_device *pdev = to_platform_device(dev);
	struct gpio_bank *bank = platform_get_drvdata(pdev);
	u32 l1 = 0, l2 = 0;
	unsigned long flags;
1301
	u32 wake_low, wake_hi;
1302

1303
	raw_spin_lock_irqsave(&bank->lock, flags);
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

	/*
	 * 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.
	 */
	wake_low = bank->context.leveldetect0 & bank->context.wake_en;
	if (wake_low)
1318
		writel_relaxed(wake_low | bank->context.fallingdetect,
1319 1320 1321
			     bank->base + bank->regs->fallingdetect);
	wake_hi = bank->context.leveldetect1 & bank->context.wake_en;
	if (wake_hi)
1322
		writel_relaxed(wake_hi | bank->context.risingdetect,
1323 1324
			     bank->base + bank->regs->risingdetect);

1325 1326 1327
	if (!bank->enabled_non_wakeup_gpios)
		goto update_gpio_context_count;

1328 1329
	if (bank->power_mode != OFF_MODE) {
		bank->power_mode = 0;
1330
		goto update_gpio_context_count;
1331 1332 1333 1334 1335 1336
	}
	/*
	 * If going to OFF, remove triggering for all
	 * non-wakeup GPIOs.  Otherwise spurious IRQs will be
	 * generated.  See OMAP2420 Errata item 1.101.
	 */
1337
	bank->saved_datain = readl_relaxed(bank->base +
1338
						bank->regs->datain);
1339 1340
	l1 = bank->context.fallingdetect;
	l2 = bank->context.risingdetect;
1341

1342 1343
	l1 &= ~bank->enabled_non_wakeup_gpios;
	l2 &= ~bank->enabled_non_wakeup_gpios;
1344

1345 1346
	writel_relaxed(l1, bank->base + bank->regs->fallingdetect);
	writel_relaxed(l2, bank->base + bank->regs->risingdetect);
1347

1348
	bank->workaround_enabled = true;
1349

1350
update_gpio_context_count:
1351 1352
	if (bank->get_context_loss_count)
		bank->context_loss_count =
1353 1354
				bank->get_context_loss_count(bank->dev);

1355
	omap_gpio_dbck_disable(bank);
1356
	raw_spin_unlock_irqrestore(&bank->lock, flags);
1357

1358
	return 0;
1359 1360
}

1361 1362
static void omap_gpio_init_context(struct gpio_bank *p);

1363
static int omap_gpio_runtime_resume(struct device *dev)
1364
{
1365 1366 1367 1368
	struct platform_device *pdev = to_platform_device(dev);
	struct gpio_bank *bank = platform_get_drvdata(pdev);
	u32 l = 0, gen, gen0, gen1;
	unsigned long flags;
1369
	int c;
1370

1371
	raw_spin_lock_irqsave(&bank->lock, flags);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

	/*
	 * 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 =
				bank->get_context_loss_count(bank->dev);
	}

1386
	omap_gpio_dbck_enable(bank);
1387 1388 1389 1390 1391 1392 1393

	/*
	 * In ->runtime_suspend(), level-triggered, wakeup-enabled
	 * GPIOs were set to edge trigger also in order to be able to
	 * generate a PRCM wakeup.  Here we restore the
	 * pre-runtime_suspend() values for edge triggering.
	 */
1394
	writel_relaxed(bank->context.fallingdetect,
1395
		     bank->base + bank->regs->fallingdetect);
1396
	writel_relaxed(bank->context.risingdetect,
1397 1398
		     bank->base + bank->regs->risingdetect);

1399 1400
	if (bank->loses_context) {
		if (!bank->get_context_loss_count) {
1401 1402
			omap_gpio_restore_context(bank);
		} else {
1403 1404 1405 1406
			c = bank->get_context_loss_count(bank->dev);
			if (c != bank->context_loss_count) {
				omap_gpio_restore_context(bank);
			} else {
1407
				raw_spin_unlock_irqrestore(&bank->lock, flags);
1408 1409
				return 0;
			}
1410
		}
1411
	}
1412

1413
	if (!bank->workaround_enabled) {
1414
		raw_spin_unlock_irqrestore(&bank->lock, flags);
1415 1416 1417
		return 0;
	}

1418
	l = readl_relaxed(bank->base + bank->regs->datain);
1419

1420 1421 1422 1423 1424 1425 1426 1427
	/*
	 * 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;
1428

1429 1430 1431 1432
	/*
	 * No need to generate IRQs for the rising edge for gpio IRQs
	 * configured with falling edge only; and vice versa.
	 */
1433
	gen0 = l & bank->context.fallingdetect;
1434
	gen0 &= bank->saved_datain;
1435

1436
	gen1 = l & bank->context.risingdetect;
1437
	gen1 &= ~(bank->saved_datain);
1438

1439
	/* FIXME: Consider GPIO IRQs with level detections properly! */
1440 1441
	gen = l & (~(bank->context.fallingdetect) &
					 ~(bank->context.risingdetect));
1442 1443
	/* Consider all GPIO IRQs needed to be updated */
	gen |= gen0 | gen1;
1444

1445 1446
	if (gen) {
		u32 old0, old1;
1447

1448 1449
		old0 = readl_relaxed(bank->base + bank->regs->leveldetect0);
		old1 = readl_relaxed(bank->base + bank->regs->leveldetect1);
1450

1451
		if (!bank->regs->irqstatus_raw0) {
1452
			writel_relaxed(old0 | gen, bank->base +
1453
						bank->regs->leveldetect0);
1454
			writel_relaxed(old1 | gen, bank->base +
1455
						bank->regs->leveldetect1);
1456
		}
1457

1458
		if (bank->regs->irqstatus_raw0) {
1459
			writel_relaxed(old0 | l, bank->base +
1460
						bank->regs->leveldetect0);
1461
			writel_relaxed(old1 | l, bank->base +
1462
						bank->regs->leveldetect1);
1463
		}
1464 1465
		writel_relaxed(old0, bank->base + bank->regs->leveldetect0);
		writel_relaxed(old1, bank->base + bank->regs->leveldetect1);
1466 1467 1468
	}

	bank->workaround_enabled = false;
1469
	raw_spin_unlock_irqrestore(&bank->lock, flags);
1470 1471 1472

	return 0;
}
1473
#endif /* CONFIG_PM */
1474

1475
#if IS_BUILTIN(CONFIG_GPIO_OMAP)
1476 1477 1478 1479 1480
void omap2_gpio_prepare_for_idle(int pwr_mode)
{
	struct gpio_bank *bank;

	list_for_each_entry(bank, &omap_gpio_list, node) {
1481
		if (!BANK_USED(bank) || !bank->loses_context)
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
			continue;

		bank->power_mode = pwr_mode;

		pm_runtime_put_sync_suspend(bank->dev);
	}
}

void omap2_gpio_resume_after_idle(void)
{
	struct gpio_bank *bank;

	list_for_each_entry(bank, &omap_gpio_list, node) {
1495
		if (!BANK_USED(bank) || !bank->loses_context)
1496 1497 1498
			continue;

		pm_runtime_get_sync(bank->dev);
1499 1500
	}
}
1501
#endif
1502

1503
#if defined(CONFIG_PM)
1504 1505 1506 1507 1508
static void omap_gpio_init_context(struct gpio_bank *p)
{
	struct omap_gpio_reg_offs *regs = p->regs;
	void __iomem *base = p->base;

1509 1510 1511 1512 1513 1514 1515 1516 1517
	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);
1518 1519

	if (regs->set_dataout && p->regs->clr_dataout)
1520
		p->context.dataout = readl_relaxed(base + regs->set_dataout);
1521
	else
1522
		p->context.dataout = readl_relaxed(base + regs->dataout);
1523 1524 1525 1526

	p->context_valid = true;
}

1527
static void omap_gpio_restore_context(struct gpio_bank *bank)
1528
{
1529
	writel_relaxed(bank->context.wake_en,
1530
				bank->base + bank->regs->wkup_en);
1531 1532
	writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl);
	writel_relaxed(bank->context.leveldetect0,
1533
				bank->base + bank->regs->leveldetect0);
1534
	writel_relaxed(bank->context.leveldetect1,
1535
				bank->base + bank->regs->leveldetect1);
1536
	writel_relaxed(bank->context.risingdetect,
1537
				bank->base + bank->regs->risingdetect);
1538
	writel_relaxed(bank->context.fallingdetect,
1539
				bank->base + bank->regs->fallingdetect);
1540
	if (bank->regs->set_dataout && bank->regs->clr_dataout)
1541
		writel_relaxed(bank->context.dataout,
1542 1543
				bank->base + bank->regs->set_dataout);
	else
1544
		writel_relaxed(bank->context.dataout,
1545
				bank->base + bank->regs->dataout);
1546
	writel_relaxed(bank->context.oe, bank->base + bank->regs->direction);
1547

1548
	if (bank->dbck_enable_mask) {
1549
		writel_relaxed(bank->context.debounce, bank->base +
1550
					bank->regs->debounce);
1551
		writel_relaxed(bank->context.debounce_en,
1552 1553
					bank->base + bank->regs->debounce_en);
	}
1554

1555
	writel_relaxed(bank->context.irqenable1,
1556
				bank->base + bank->regs->irqenable);
1557
	writel_relaxed(bank->context.irqenable2,
1558
				bank->base + bank->regs->irqenable2);
1559
}
1560
#endif /* CONFIG_PM */
1561
#else
1562 1563
#define omap_gpio_runtime_suspend NULL
#define omap_gpio_runtime_resume NULL
1564
static inline void omap_gpio_init_context(struct gpio_bank *p) {}
1565 1566
#endif

1567
static const struct dev_pm_ops gpio_pm_ops = {
1568 1569
	SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume,
									NULL)
1570 1571
};

1572 1573 1574 1575 1576 1577 1578 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 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
#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,
};

1619
static const struct omap_gpio_platform_data omap2_pdata = {
1620 1621 1622 1623 1624
	.regs = &omap2_gpio_regs,
	.bank_width = 32,
	.dbck_flag = false,
};

1625
static const struct omap_gpio_platform_data omap3_pdata = {
1626 1627 1628 1629 1630
	.regs = &omap2_gpio_regs,
	.bank_width = 32,
	.dbck_flag = true,
};

1631
static const struct omap_gpio_platform_data omap4_pdata = {
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
	.regs = &omap4_gpio_regs,
	.bank_width = 32,
	.dbck_flag = true,
};

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

1655 1656
static struct platform_driver omap_gpio_driver = {
	.probe		= omap_gpio_probe,
1657
	.remove		= omap_gpio_remove,
1658 1659
	.driver		= {
		.name	= "omap_gpio",
1660
		.pm	= &gpio_pm_ops,
1661
		.of_match_table = of_match_ptr(omap_gpio_match),
1662 1663 1664
	},
};

1665
/*
1666 1667 1668
 * gpio driver register needs to be done before
 * machine_init functions access gpio APIs.
 * Hence omap_gpio_drv_reg() is a postcore_initcall.
1669
 */
1670
static int __init omap_gpio_drv_reg(void)
1671
{
1672
	return platform_driver_register(&omap_gpio_driver);
1673
}
1674
postcore_initcall(omap_gpio_drv_reg);
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684

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");