2008-02-04 23:28:22 -07:00
|
|
|
/*
|
|
|
|
* linux/arch/arm/mach-pxa/gpio.c
|
|
|
|
*
|
|
|
|
* Generic PXA GPIO handling
|
|
|
|
*
|
|
|
|
* Author: Nicolas Pitre
|
|
|
|
* Created: Jun 15, 2001
|
|
|
|
* Copyright: MontaVista Software Inc.
|
|
|
|
*
|
|
|
|
* 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>
|
2008-03-03 20:42:26 -07:00
|
|
|
#include <linux/irq.h>
|
2008-03-04 01:13:58 -07:00
|
|
|
#include <linux/sysdev.h>
|
2008-09-06 05:10:45 -06:00
|
|
|
#include <linux/io.h>
|
2008-02-04 23:28:22 -07:00
|
|
|
|
|
|
|
#include <asm/gpio.h>
|
2008-08-05 09:14:15 -06:00
|
|
|
#include <mach/hardware.h>
|
|
|
|
#include <mach/pxa-regs.h>
|
2008-02-04 23:28:22 -07:00
|
|
|
|
|
|
|
#include "generic.h"
|
|
|
|
|
2008-11-27 23:54:39 -07:00
|
|
|
#define GPIO0_BASE ((void __iomem *)io_p2v(0x40E00000))
|
|
|
|
#define GPIO1_BASE ((void __iomem *)io_p2v(0x40E00004))
|
|
|
|
#define GPIO2_BASE ((void __iomem *)io_p2v(0x40E00008))
|
|
|
|
#define GPIO3_BASE ((void __iomem *)io_p2v(0x40E00100))
|
|
|
|
|
|
|
|
#define GPLR_OFFSET 0x00
|
|
|
|
#define GPDR_OFFSET 0x0C
|
|
|
|
#define GPSR_OFFSET 0x18
|
|
|
|
#define GPCR_OFFSET 0x24
|
|
|
|
#define GRER_OFFSET 0x30
|
|
|
|
#define GFER_OFFSET 0x3C
|
|
|
|
#define GEDR_OFFSET 0x48
|
2008-02-04 23:28:22 -07:00
|
|
|
|
|
|
|
struct pxa_gpio_chip {
|
|
|
|
struct gpio_chip chip;
|
|
|
|
void __iomem *regbase;
|
|
|
|
};
|
|
|
|
|
|
|
|
int pxa_last_gpio;
|
|
|
|
|
|
|
|
static int pxa_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
u32 mask = 1 << offset;
|
|
|
|
u32 value;
|
|
|
|
struct pxa_gpio_chip *pxa;
|
|
|
|
void __iomem *gpdr;
|
|
|
|
|
|
|
|
pxa = container_of(chip, struct pxa_gpio_chip, chip);
|
|
|
|
gpdr = pxa->regbase + GPDR_OFFSET;
|
|
|
|
local_irq_save(flags);
|
|
|
|
value = __raw_readl(gpdr);
|
2008-11-26 03:12:04 -07:00
|
|
|
if (__gpio_is_inverted(chip->base + offset))
|
|
|
|
value |= mask;
|
|
|
|
else
|
|
|
|
value &= ~mask;
|
2008-02-04 23:28:22 -07:00
|
|
|
__raw_writel(value, gpdr);
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pxa_gpio_direction_output(struct gpio_chip *chip,
|
|
|
|
unsigned offset, int value)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
u32 mask = 1 << offset;
|
|
|
|
u32 tmp;
|
|
|
|
struct pxa_gpio_chip *pxa;
|
|
|
|
void __iomem *gpdr;
|
|
|
|
|
|
|
|
pxa = container_of(chip, struct pxa_gpio_chip, chip);
|
|
|
|
__raw_writel(mask,
|
|
|
|
pxa->regbase + (value ? GPSR_OFFSET : GPCR_OFFSET));
|
|
|
|
gpdr = pxa->regbase + GPDR_OFFSET;
|
|
|
|
local_irq_save(flags);
|
|
|
|
tmp = __raw_readl(gpdr);
|
2008-11-26 03:12:04 -07:00
|
|
|
if (__gpio_is_inverted(chip->base + offset))
|
|
|
|
tmp &= ~mask;
|
|
|
|
else
|
|
|
|
tmp |= mask;
|
2008-02-04 23:28:22 -07:00
|
|
|
__raw_writel(tmp, gpdr);
|
|
|
|
local_irq_restore(flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return GPIO level
|
|
|
|
*/
|
|
|
|
static int pxa_gpio_get(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
|
|
|
u32 mask = 1 << offset;
|
|
|
|
struct pxa_gpio_chip *pxa;
|
|
|
|
|
|
|
|
pxa = container_of(chip, struct pxa_gpio_chip, chip);
|
|
|
|
return __raw_readl(pxa->regbase + GPLR_OFFSET) & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set output GPIO level
|
|
|
|
*/
|
|
|
|
static void pxa_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
|
|
|
|
{
|
|
|
|
u32 mask = 1 << offset;
|
|
|
|
struct pxa_gpio_chip *pxa;
|
|
|
|
|
|
|
|
pxa = container_of(chip, struct pxa_gpio_chip, chip);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
__raw_writel(mask, pxa->regbase + GPSR_OFFSET);
|
|
|
|
else
|
|
|
|
__raw_writel(mask, pxa->regbase + GPCR_OFFSET);
|
|
|
|
}
|
|
|
|
|
2008-03-02 22:20:20 -07:00
|
|
|
#define GPIO_CHIP(_n) \
|
|
|
|
[_n] = { \
|
|
|
|
.regbase = GPIO##_n##_BASE, \
|
|
|
|
.chip = { \
|
|
|
|
.label = "gpio-" #_n, \
|
|
|
|
.direction_input = pxa_gpio_direction_input, \
|
|
|
|
.direction_output = pxa_gpio_direction_output, \
|
|
|
|
.get = pxa_gpio_get, \
|
|
|
|
.set = pxa_gpio_set, \
|
|
|
|
.base = (_n) * 32, \
|
|
|
|
.ngpio = 32, \
|
|
|
|
}, \
|
|
|
|
}
|
|
|
|
|
2008-02-04 23:28:22 -07:00
|
|
|
static struct pxa_gpio_chip pxa_gpio_chip[] = {
|
2008-03-02 22:20:20 -07:00
|
|
|
GPIO_CHIP(0),
|
|
|
|
GPIO_CHIP(1),
|
|
|
|
GPIO_CHIP(2),
|
2008-02-04 23:28:22 -07:00
|
|
|
#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
|
2008-03-02 22:20:20 -07:00
|
|
|
GPIO_CHIP(3),
|
2008-02-04 23:28:22 -07:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2009-01-06 02:37:37 -07:00
|
|
|
static void __init pxa_init_gpio_chip(int gpio_nr)
|
|
|
|
{
|
|
|
|
int i, gpio;
|
|
|
|
|
|
|
|
/* add a GPIO chip for each register bank.
|
|
|
|
* the last PXA25x register only contains 21 GPIOs
|
|
|
|
*/
|
|
|
|
for (gpio = 0, i = 0; gpio < gpio_nr; gpio += 32, i++) {
|
|
|
|
if (gpio + 32 > gpio_nr)
|
|
|
|
pxa_gpio_chip[i].chip.ngpio = gpio_nr - gpio;
|
|
|
|
gpiochip_add(&pxa_gpio_chip[i].chip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-03 20:42:26 -07:00
|
|
|
/*
|
|
|
|
* PXA GPIO edge detection for IRQs:
|
|
|
|
* IRQs are generated on Falling-Edge, Rising-Edge, or both.
|
|
|
|
* Use this instead of directly setting GRER/GFER.
|
|
|
|
*/
|
|
|
|
|
2008-04-19 03:42:18 -06:00
|
|
|
static unsigned long GPIO_IRQ_rising_edge[4];
|
|
|
|
static unsigned long GPIO_IRQ_falling_edge[4];
|
|
|
|
static unsigned long GPIO_IRQ_mask[4];
|
2008-03-03 20:42:26 -07:00
|
|
|
|
|
|
|
static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
|
|
|
|
{
|
|
|
|
int gpio, idx;
|
|
|
|
|
|
|
|
gpio = IRQ_TO_GPIO(irq);
|
|
|
|
idx = gpio >> 5;
|
|
|
|
|
|
|
|
if (type == IRQ_TYPE_PROBE) {
|
|
|
|
/* Don't mess with enabled GPIOs using preconfigured edges or
|
|
|
|
* GPIOs set to alternate function or to output during probe
|
|
|
|
*/
|
2008-11-26 03:12:04 -07:00
|
|
|
if ((GPIO_IRQ_rising_edge[idx] & GPIO_bit(gpio)) ||
|
|
|
|
(GPIO_IRQ_falling_edge[idx] & GPIO_bit(gpio)))
|
2008-03-03 20:42:26 -07:00
|
|
|
return 0;
|
2008-03-04 02:18:38 -07:00
|
|
|
|
|
|
|
if (__gpio_is_occupied(gpio))
|
2008-03-03 20:42:26 -07:00
|
|
|
return 0;
|
2008-03-04 02:18:38 -07:00
|
|
|
|
2008-03-03 20:42:26 -07:00
|
|
|
type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
|
|
|
|
}
|
|
|
|
|
2008-11-26 03:12:04 -07:00
|
|
|
if (__gpio_is_inverted(gpio))
|
|
|
|
GPDR(gpio) |= GPIO_bit(gpio);
|
|
|
|
else
|
|
|
|
GPDR(gpio) &= ~GPIO_bit(gpio);
|
2008-03-03 20:42:26 -07:00
|
|
|
|
|
|
|
if (type & IRQ_TYPE_EDGE_RISING)
|
|
|
|
__set_bit(gpio, GPIO_IRQ_rising_edge);
|
|
|
|
else
|
|
|
|
__clear_bit(gpio, GPIO_IRQ_rising_edge);
|
|
|
|
|
|
|
|
if (type & IRQ_TYPE_EDGE_FALLING)
|
|
|
|
__set_bit(gpio, GPIO_IRQ_falling_edge);
|
|
|
|
else
|
|
|
|
__clear_bit(gpio, GPIO_IRQ_falling_edge);
|
|
|
|
|
|
|
|
GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
|
|
|
|
GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
|
|
|
|
|
|
|
|
pr_debug("%s: IRQ%d (GPIO%d) - edge%s%s\n", __func__, irq, gpio,
|
|
|
|
((type & IRQ_TYPE_EDGE_RISING) ? " rising" : ""),
|
|
|
|
((type & IRQ_TYPE_EDGE_FALLING) ? " falling" : ""));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Demux handler for GPIO>=2 edge detect interrupts
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
|
|
|
|
|
|
|
|
static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
|
|
|
|
{
|
|
|
|
int loop, bit, n;
|
|
|
|
unsigned long gedr[4];
|
|
|
|
|
|
|
|
do {
|
|
|
|
gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
|
|
|
|
gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
|
|
|
|
gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
|
|
|
|
gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
|
|
|
|
|
|
|
|
GEDR0 = gedr[0]; GEDR1 = gedr[1];
|
|
|
|
GEDR2 = gedr[2]; GEDR3 = gedr[3];
|
|
|
|
|
|
|
|
loop = 0;
|
|
|
|
bit = find_first_bit(gedr, GEDR_BITS);
|
|
|
|
while (bit < GEDR_BITS) {
|
|
|
|
loop = 1;
|
|
|
|
|
|
|
|
n = PXA_GPIO_IRQ_BASE + bit;
|
2008-10-09 06:36:24 -06:00
|
|
|
generic_handle_irq(n);
|
2008-03-03 20:42:26 -07:00
|
|
|
|
|
|
|
bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
|
|
|
|
}
|
|
|
|
} while (loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pxa_ack_muxed_gpio(unsigned int irq)
|
|
|
|
{
|
|
|
|
int gpio = irq - IRQ_GPIO(2) + 2;
|
|
|
|
GEDR(gpio) = GPIO_bit(gpio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pxa_mask_muxed_gpio(unsigned int irq)
|
|
|
|
{
|
|
|
|
int gpio = irq - IRQ_GPIO(2) + 2;
|
|
|
|
__clear_bit(gpio, GPIO_IRQ_mask);
|
|
|
|
GRER(gpio) &= ~GPIO_bit(gpio);
|
|
|
|
GFER(gpio) &= ~GPIO_bit(gpio);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pxa_unmask_muxed_gpio(unsigned int irq)
|
|
|
|
{
|
|
|
|
int gpio = irq - IRQ_GPIO(2) + 2;
|
|
|
|
int idx = gpio >> 5;
|
|
|
|
__set_bit(gpio, GPIO_IRQ_mask);
|
|
|
|
GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
|
|
|
|
GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct irq_chip pxa_muxed_gpio_chip = {
|
|
|
|
.name = "GPIO",
|
|
|
|
.ack = pxa_ack_muxed_gpio,
|
|
|
|
.mask = pxa_mask_muxed_gpio,
|
|
|
|
.unmask = pxa_unmask_muxed_gpio,
|
|
|
|
.set_type = pxa_gpio_irq_type,
|
|
|
|
};
|
|
|
|
|
2009-01-06 02:37:37 -07:00
|
|
|
void __init pxa_init_gpio(int mux_irq, int start, int end, set_wake_t fn)
|
2008-03-03 20:42:26 -07:00
|
|
|
{
|
2009-01-06 02:37:37 -07:00
|
|
|
int irq, i;
|
2008-03-03 20:42:26 -07:00
|
|
|
|
2009-01-06 02:37:37 -07:00
|
|
|
pxa_last_gpio = end;
|
2008-03-03 20:42:26 -07:00
|
|
|
|
|
|
|
/* clear all GPIO edge detects */
|
2009-01-06 02:37:37 -07:00
|
|
|
for (i = start; i <= end; i += 32) {
|
|
|
|
GFER(i) &= ~GPIO_IRQ_mask[i];
|
|
|
|
GRER(i) &= ~GPIO_IRQ_mask[i];
|
|
|
|
GEDR(i) = GPIO_IRQ_mask[i];
|
2008-03-03 20:42:26 -07:00
|
|
|
}
|
|
|
|
|
2009-01-06 02:37:37 -07:00
|
|
|
for (irq = gpio_to_irq(start); irq <= gpio_to_irq(end); irq++) {
|
2008-03-03 20:42:26 -07:00
|
|
|
set_irq_chip(irq, &pxa_muxed_gpio_chip);
|
|
|
|
set_irq_handler(irq, handle_edge_irq);
|
|
|
|
set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Install handler for GPIO>=2 edge detect interrupts */
|
2009-01-06 02:37:37 -07:00
|
|
|
set_irq_chained_handler(mux_irq, pxa_gpio_demux_handler);
|
2008-03-03 23:19:58 -07:00
|
|
|
pxa_muxed_gpio_chip.set_wake = fn;
|
2008-03-03 20:42:26 -07:00
|
|
|
|
2009-01-06 02:37:37 -07:00
|
|
|
/* Initialize GPIO chips */
|
|
|
|
pxa_init_gpio_chip(end + 1);
|
2008-03-03 20:42:26 -07:00
|
|
|
}
|
2008-03-04 01:13:58 -07:00
|
|
|
|
|
|
|
#ifdef CONFIG_PM
|
|
|
|
|
|
|
|
static unsigned long saved_gplr[4];
|
|
|
|
static unsigned long saved_gpdr[4];
|
|
|
|
static unsigned long saved_grer[4];
|
|
|
|
static unsigned long saved_gfer[4];
|
|
|
|
|
|
|
|
static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
|
|
|
|
{
|
|
|
|
int i, gpio;
|
|
|
|
|
|
|
|
for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
|
|
|
|
saved_gplr[i] = GPLR(gpio);
|
|
|
|
saved_gpdr[i] = GPDR(gpio);
|
|
|
|
saved_grer[i] = GRER(gpio);
|
|
|
|
saved_gfer[i] = GFER(gpio);
|
|
|
|
|
|
|
|
/* Clear GPIO transition detect bits */
|
|
|
|
GEDR(gpio) = GEDR(gpio);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pxa_gpio_resume(struct sys_device *dev)
|
|
|
|
{
|
|
|
|
int i, gpio;
|
|
|
|
|
|
|
|
for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
|
|
|
|
/* restore level with set/clear */
|
|
|
|
GPSR(gpio) = saved_gplr[i];
|
|
|
|
GPCR(gpio) = ~saved_gplr[i];
|
|
|
|
|
|
|
|
GRER(gpio) = saved_grer[i];
|
|
|
|
GFER(gpio) = saved_gfer[i];
|
|
|
|
GPDR(gpio) = saved_gpdr[i];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define pxa_gpio_suspend NULL
|
|
|
|
#define pxa_gpio_resume NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sysdev_class pxa_gpio_sysclass = {
|
|
|
|
.name = "gpio",
|
|
|
|
.suspend = pxa_gpio_suspend,
|
|
|
|
.resume = pxa_gpio_resume,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init pxa_gpio_init(void)
|
|
|
|
{
|
|
|
|
return sysdev_class_register(&pxa_gpio_sysclass);
|
|
|
|
}
|
|
|
|
|
|
|
|
core_initcall(pxa_gpio_init);
|