2007-07-10 06:03:43 -06:00
|
|
|
/*
|
|
|
|
* TI DaVinci GPIO Support
|
|
|
|
*
|
2008-09-08 00:41:04 -06:00
|
|
|
* Copyright (c) 2006-2007 David Brownell
|
2007-07-10 06:03:43 -06:00
|
|
|
* Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
2011-07-26 03:53:52 -06:00
|
|
|
#include <linux/gpio.h>
|
2007-07-10 06:03:43 -06:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
2013-08-17 23:18:58 -06:00
|
|
|
#include <linux/irq.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/platform_data/gpio-davinci.h>
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2010-05-01 16:37:53 -06:00
|
|
|
struct davinci_gpio_regs {
|
|
|
|
u32 dir;
|
|
|
|
u32 out_data;
|
|
|
|
u32 set_data;
|
|
|
|
u32 clr_data;
|
|
|
|
u32 in_data;
|
|
|
|
u32 set_rising;
|
|
|
|
u32 clr_rising;
|
|
|
|
u32 set_falling;
|
|
|
|
u32 clr_falling;
|
|
|
|
u32 intstat;
|
|
|
|
};
|
|
|
|
|
2013-08-17 23:18:57 -06:00
|
|
|
#define BINTEN 0x8 /* GPIO Interrupt Per-Bank Enable Register */
|
|
|
|
|
2010-05-01 16:37:51 -06:00
|
|
|
#define chip2controller(chip) \
|
2010-05-01 16:37:52 -06:00
|
|
|
container_of(chip, struct davinci_gpio_controller, chip)
|
2010-05-01 16:37:51 -06:00
|
|
|
|
2010-05-07 15:06:32 -06:00
|
|
|
static void __iomem *gpio_base;
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
static struct davinci_gpio_regs __iomem *gpio2regs(unsigned gpio)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
2010-05-01 16:37:53 -06:00
|
|
|
void __iomem *ptr;
|
|
|
|
|
|
|
|
if (gpio < 32 * 1)
|
2010-05-07 15:06:32 -06:00
|
|
|
ptr = gpio_base + 0x10;
|
2010-05-01 16:37:53 -06:00
|
|
|
else if (gpio < 32 * 2)
|
2010-05-07 15:06:32 -06:00
|
|
|
ptr = gpio_base + 0x38;
|
2010-05-01 16:37:53 -06:00
|
|
|
else if (gpio < 32 * 3)
|
2010-05-07 15:06:32 -06:00
|
|
|
ptr = gpio_base + 0x60;
|
2010-05-01 16:37:53 -06:00
|
|
|
else if (gpio < 32 * 4)
|
2010-05-07 15:06:32 -06:00
|
|
|
ptr = gpio_base + 0x88;
|
2010-05-01 16:37:53 -06:00
|
|
|
else if (gpio < 32 * 5)
|
2010-05-07 15:06:32 -06:00
|
|
|
ptr = gpio_base + 0xb0;
|
2010-05-01 16:37:53 -06:00
|
|
|
else
|
|
|
|
ptr = NULL;
|
|
|
|
return ptr;
|
2007-07-10 06:03:43 -06:00
|
|
|
}
|
|
|
|
|
2010-05-01 16:37:52 -06:00
|
|
|
static inline struct davinci_gpio_regs __iomem *irq2regs(int irq)
|
2010-02-25 17:49:56 -07:00
|
|
|
{
|
2010-05-01 16:37:52 -06:00
|
|
|
struct davinci_gpio_regs __iomem *g;
|
2010-02-25 17:49:56 -07:00
|
|
|
|
2011-03-24 06:25:22 -06:00
|
|
|
g = (__force struct davinci_gpio_regs __iomem *)irq_get_chip_data(irq);
|
2010-02-25 17:49:56 -07:00
|
|
|
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
static int davinci_gpio_irq_setup(struct platform_device *pdev);
|
2008-09-08 00:41:04 -06:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
2010-05-01 16:38:27 -06:00
|
|
|
/* board setup code *MUST* setup pinmux and enable the GPIO clock. */
|
2010-05-01 16:37:51 -06:00
|
|
|
static inline int __davinci_direction(struct gpio_chip *chip,
|
|
|
|
unsigned offset, bool out, int value)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
2010-05-01 16:37:52 -06:00
|
|
|
struct davinci_gpio_controller *d = chip2controller(chip);
|
|
|
|
struct davinci_gpio_regs __iomem *g = d->regs;
|
2010-05-01 16:37:55 -06:00
|
|
|
unsigned long flags;
|
2008-09-08 00:41:04 -06:00
|
|
|
u32 temp;
|
2010-05-01 16:37:51 -06:00
|
|
|
u32 mask = 1 << offset;
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2010-05-01 16:37:55 -06:00
|
|
|
spin_lock_irqsave(&d->lock, flags);
|
2008-09-08 00:41:04 -06:00
|
|
|
temp = __raw_readl(&g->dir);
|
2010-05-01 16:37:51 -06:00
|
|
|
if (out) {
|
|
|
|
temp &= ~mask;
|
|
|
|
__raw_writel(mask, value ? &g->set_data : &g->clr_data);
|
|
|
|
} else {
|
|
|
|
temp |= mask;
|
|
|
|
}
|
2008-09-08 00:41:04 -06:00
|
|
|
__raw_writel(temp, &g->dir);
|
2010-05-01 16:37:55 -06:00
|
|
|
spin_unlock_irqrestore(&d->lock, flags);
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2008-09-08 00:41:04 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2010-05-01 16:37:51 -06:00
|
|
|
static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
|
|
|
return __davinci_direction(chip, offset, false, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
|
|
|
|
{
|
|
|
|
return __davinci_direction(chip, offset, true, value);
|
|
|
|
}
|
|
|
|
|
2007-07-10 06:03:43 -06:00
|
|
|
/*
|
|
|
|
* Read the pin's value (works even if it's set up as output);
|
|
|
|
* returns zero/nonzero.
|
|
|
|
*
|
|
|
|
* Note that changes are synched to the GPIO clock, so reading values back
|
|
|
|
* right after you've set them may give old values.
|
|
|
|
*/
|
2008-09-08 00:41:04 -06:00
|
|
|
static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
2010-05-01 16:37:52 -06:00
|
|
|
struct davinci_gpio_controller *d = chip2controller(chip);
|
|
|
|
struct davinci_gpio_regs __iomem *g = d->regs;
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2008-09-08 00:41:04 -06:00
|
|
|
return (1 << offset) & __raw_readl(&g->in_data);
|
2007-07-10 06:03:43 -06:00
|
|
|
}
|
|
|
|
|
2008-09-08 00:41:04 -06:00
|
|
|
/*
|
|
|
|
* Assuming the pin is muxed as a gpio output, set its output value.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
2010-05-01 16:37:52 -06:00
|
|
|
struct davinci_gpio_controller *d = chip2controller(chip);
|
|
|
|
struct davinci_gpio_regs __iomem *g = d->regs;
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2008-09-08 00:41:04 -06:00
|
|
|
__raw_writel((1 << offset), value ? &g->set_data : &g->clr_data);
|
|
|
|
}
|
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
static int davinci_gpio_probe(struct platform_device *pdev)
|
2008-09-08 00:41:04 -06:00
|
|
|
{
|
|
|
|
int i, base;
|
2009-04-15 13:40:35 -06:00
|
|
|
unsigned ngpio;
|
2013-08-17 23:18:58 -06:00
|
|
|
struct davinci_gpio_controller *chips;
|
|
|
|
struct davinci_gpio_platform_data *pdata;
|
|
|
|
struct davinci_gpio_regs __iomem *regs;
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct resource *res;
|
|
|
|
|
|
|
|
pdata = dev->platform_data;
|
|
|
|
if (!pdata) {
|
|
|
|
dev_err(dev, "No platform data found\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-05-01 16:37:54 -06:00
|
|
|
|
2009-04-15 13:40:35 -06:00
|
|
|
/*
|
|
|
|
* The gpio banks conceptually expose a segmented bitmap,
|
2008-12-07 12:46:23 -07:00
|
|
|
* and "ngpio" is one more than the largest zero-based
|
|
|
|
* bit index that's valid.
|
|
|
|
*/
|
2013-08-17 23:18:58 -06:00
|
|
|
ngpio = pdata->ngpio;
|
2009-04-15 13:40:35 -06:00
|
|
|
if (ngpio == 0) {
|
2013-08-17 23:18:58 -06:00
|
|
|
dev_err(dev, "How many GPIOs?\n");
|
2008-12-07 12:46:23 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WARN_ON(DAVINCI_N_GPIO < ngpio))
|
|
|
|
ngpio = DAVINCI_N_GPIO;
|
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
chips = devm_kzalloc(dev,
|
|
|
|
ngpio * sizeof(struct davinci_gpio_controller),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!chips) {
|
|
|
|
dev_err(dev, "Memory allocation failed\n");
|
2010-05-07 15:06:32 -06:00
|
|
|
return -ENOMEM;
|
2013-08-17 23:18:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
if (!res) {
|
|
|
|
dev_err(dev, "Invalid memory resource\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_base = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(gpio_base))
|
|
|
|
return PTR_ERR(gpio_base);
|
2010-05-07 15:06:32 -06:00
|
|
|
|
2008-12-07 12:46:23 -07:00
|
|
|
for (i = 0, base = 0; base < ngpio; i++, base += 32) {
|
2008-09-08 00:41:04 -06:00
|
|
|
chips[i].chip.label = "DaVinci";
|
|
|
|
|
|
|
|
chips[i].chip.direction_input = davinci_direction_in;
|
|
|
|
chips[i].chip.get = davinci_gpio_get;
|
|
|
|
chips[i].chip.direction_output = davinci_direction_out;
|
|
|
|
chips[i].chip.set = davinci_gpio_set;
|
|
|
|
|
|
|
|
chips[i].chip.base = base;
|
2008-12-07 12:46:23 -07:00
|
|
|
chips[i].chip.ngpio = ngpio - base;
|
2008-09-08 00:41:04 -06:00
|
|
|
if (chips[i].chip.ngpio > 32)
|
|
|
|
chips[i].chip.ngpio = 32;
|
|
|
|
|
2010-05-01 16:37:55 -06:00
|
|
|
spin_lock_init(&chips[i].lock);
|
|
|
|
|
2010-05-01 16:37:53 -06:00
|
|
|
regs = gpio2regs(base);
|
|
|
|
chips[i].regs = regs;
|
|
|
|
chips[i].set_data = ®s->set_data;
|
|
|
|
chips[i].clr_data = ®s->clr_data;
|
|
|
|
chips[i].in_data = ®s->in_data;
|
2008-09-08 00:41:04 -06:00
|
|
|
|
|
|
|
gpiochip_add(&chips[i].chip);
|
|
|
|
}
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
platform_set_drvdata(pdev, chips);
|
|
|
|
davinci_gpio_irq_setup(pdev);
|
2007-07-10 06:03:43 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-08 00:41:04 -06:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2007-07-10 06:03:43 -06:00
|
|
|
/*
|
|
|
|
* We expect irqs will normally be set up as input pins, but they can also be
|
|
|
|
* used as output pins ... which is convenient for testing.
|
|
|
|
*
|
2008-12-07 12:46:23 -07:00
|
|
|
* NOTE: The first few GPIOs also have direct INTC hookups in addition
|
2009-06-25 18:01:31 -06:00
|
|
|
* to their GPIOBNK0 irq, with a bit less overhead.
|
2007-07-10 06:03:43 -06:00
|
|
|
*
|
2008-12-07 12:46:23 -07:00
|
|
|
* All those INTC hookups (direct, plus several IRQ banks) can also
|
2007-07-10 06:03:43 -06:00
|
|
|
* serve as EDMA event triggers.
|
|
|
|
*/
|
|
|
|
|
2010-11-29 02:27:27 -07:00
|
|
|
static void gpio_irq_disable(struct irq_data *d)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
2010-11-29 02:27:27 -07:00
|
|
|
struct davinci_gpio_regs __iomem *g = irq2regs(d->irq);
|
2011-03-24 06:25:22 -06:00
|
|
|
u32 mask = (u32) irq_data_get_irq_handler_data(d);
|
2007-07-10 06:03:43 -06:00
|
|
|
|
|
|
|
__raw_writel(mask, &g->clr_falling);
|
|
|
|
__raw_writel(mask, &g->clr_rising);
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:27:27 -07:00
|
|
|
static void gpio_irq_enable(struct irq_data *d)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
2010-11-29 02:27:27 -07:00
|
|
|
struct davinci_gpio_regs __iomem *g = irq2regs(d->irq);
|
2011-03-24 06:25:22 -06:00
|
|
|
u32 mask = (u32) irq_data_get_irq_handler_data(d);
|
2011-03-24 05:47:04 -06:00
|
|
|
unsigned status = irqd_get_trigger_type(d);
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2009-05-04 14:14:27 -06:00
|
|
|
status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
|
|
|
|
if (!status)
|
|
|
|
status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
|
|
|
|
|
|
|
|
if (status & IRQ_TYPE_EDGE_FALLING)
|
2007-07-10 06:03:43 -06:00
|
|
|
__raw_writel(mask, &g->set_falling);
|
2009-05-04 14:14:27 -06:00
|
|
|
if (status & IRQ_TYPE_EDGE_RISING)
|
2007-07-10 06:03:43 -06:00
|
|
|
__raw_writel(mask, &g->set_rising);
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:27:27 -07:00
|
|
|
static int gpio_irq_type(struct irq_data *d, unsigned trigger)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
|
|
|
if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct irq_chip gpio_irqchip = {
|
|
|
|
.name = "GPIO",
|
2010-11-29 02:27:27 -07:00
|
|
|
.irq_enable = gpio_irq_enable,
|
|
|
|
.irq_disable = gpio_irq_disable,
|
|
|
|
.irq_set_type = gpio_irq_type,
|
2011-03-24 05:47:04 -06:00
|
|
|
.flags = IRQCHIP_SET_TYPE_MASKED,
|
2007-07-10 06:03:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
gpio_irq_handler(unsigned irq, struct irq_desc *desc)
|
|
|
|
{
|
2011-06-06 03:51:43 -06:00
|
|
|
struct davinci_gpio_regs __iomem *g;
|
2007-07-10 06:03:43 -06:00
|
|
|
u32 mask = 0xffff;
|
2011-07-11 15:03:11 -06:00
|
|
|
struct davinci_gpio_controller *d;
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2011-07-11 15:03:11 -06:00
|
|
|
d = (struct davinci_gpio_controller *)irq_desc_get_handler_data(desc);
|
|
|
|
g = (struct davinci_gpio_regs __iomem *)d->regs;
|
2011-06-06 03:51:43 -06:00
|
|
|
|
2007-07-10 06:03:43 -06:00
|
|
|
/* we only care about one bank */
|
|
|
|
if (irq & 1)
|
|
|
|
mask <<= 16;
|
|
|
|
|
|
|
|
/* temporarily mask (level sensitive) parent IRQ */
|
2010-11-29 02:27:27 -07:00
|
|
|
desc->irq_data.chip->irq_mask(&desc->irq_data);
|
|
|
|
desc->irq_data.chip->irq_ack(&desc->irq_data);
|
2007-07-10 06:03:43 -06:00
|
|
|
while (1) {
|
|
|
|
u32 status;
|
|
|
|
int n;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* ack any irqs */
|
|
|
|
status = __raw_readl(&g->intstat) & mask;
|
|
|
|
if (!status)
|
|
|
|
break;
|
|
|
|
__raw_writel(status, &g->intstat);
|
|
|
|
|
|
|
|
/* now demux them to the right lowlevel handler */
|
2011-07-11 15:03:11 -06:00
|
|
|
n = d->irq_base;
|
|
|
|
if (irq & 1) {
|
|
|
|
n += 16;
|
|
|
|
status >>= 16;
|
|
|
|
}
|
|
|
|
|
2007-07-10 06:03:43 -06:00
|
|
|
while (status) {
|
|
|
|
res = ffs(status);
|
|
|
|
n += res;
|
2008-10-09 06:36:24 -06:00
|
|
|
generic_handle_irq(n - 1);
|
2007-07-10 06:03:43 -06:00
|
|
|
status >>= res;
|
|
|
|
}
|
|
|
|
}
|
2010-11-29 02:27:27 -07:00
|
|
|
desc->irq_data.chip->irq_unmask(&desc->irq_data);
|
2007-07-10 06:03:43 -06:00
|
|
|
/* now it may re-trigger */
|
|
|
|
}
|
|
|
|
|
2009-06-25 18:01:31 -06:00
|
|
|
static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
2010-05-01 16:37:52 -06:00
|
|
|
struct davinci_gpio_controller *d = chip2controller(chip);
|
2009-06-25 18:01:31 -06:00
|
|
|
|
|
|
|
if (d->irq_base >= 0)
|
|
|
|
return d->irq_base + offset;
|
|
|
|
else
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
|
|
|
|
{
|
2013-08-17 23:18:58 -06:00
|
|
|
struct davinci_gpio_controller *d = chip2controller(chip);
|
2009-06-25 18:01:31 -06:00
|
|
|
|
2013-08-17 23:18:57 -06:00
|
|
|
/*
|
|
|
|
* NOTE: we assume for now that only irqs in the first gpio_chip
|
2009-06-25 18:01:31 -06:00
|
|
|
* can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
|
|
|
|
*/
|
2013-11-07 23:45:55 -07:00
|
|
|
if (offset < d->gpio_unbanked)
|
2013-08-17 23:18:58 -06:00
|
|
|
return d->gpio_irq + offset;
|
2009-06-25 18:01:31 -06:00
|
|
|
else
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2012-03-11 06:46:11 -06:00
|
|
|
static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger)
|
2009-06-25 18:01:31 -06:00
|
|
|
{
|
2012-03-11 06:46:11 -06:00
|
|
|
struct davinci_gpio_controller *d;
|
|
|
|
struct davinci_gpio_regs __iomem *g;
|
|
|
|
u32 mask;
|
|
|
|
|
|
|
|
d = (struct davinci_gpio_controller *)data->handler_data;
|
|
|
|
g = (struct davinci_gpio_regs __iomem *)d->regs;
|
2013-08-17 23:18:58 -06:00
|
|
|
mask = __gpio_mask(data->irq - d->gpio_irq);
|
2009-06-25 18:01:31 -06:00
|
|
|
|
|
|
|
if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
__raw_writel(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
|
|
|
|
? &g->set_falling : &g->clr_falling);
|
|
|
|
__raw_writel(mask, (trigger & IRQ_TYPE_EDGE_RISING)
|
|
|
|
? &g->set_rising : &g->clr_rising);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-10 06:03:43 -06:00
|
|
|
/*
|
2008-12-07 12:46:23 -07:00
|
|
|
* NOTE: for suspend/resume, probably best to make a platform_device with
|
|
|
|
* suspend_late/resume_resume calls hooking into results of the set_wake()
|
2007-07-10 06:03:43 -06:00
|
|
|
* calls ... so if no gpios are wakeup events the clock can be disabled,
|
|
|
|
* with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
|
2008-12-07 12:46:23 -07:00
|
|
|
* (dm6446) can be set appropriately for GPIOV33 pins.
|
2007-07-10 06:03:43 -06:00
|
|
|
*/
|
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
static int davinci_gpio_irq_setup(struct platform_device *pdev)
|
2007-07-10 06:03:43 -06:00
|
|
|
{
|
|
|
|
unsigned gpio, irq, bank;
|
|
|
|
struct clk *clk;
|
2008-12-07 12:46:23 -07:00
|
|
|
u32 binten = 0;
|
2009-04-15 13:40:35 -06:00
|
|
|
unsigned ngpio, bank_irq;
|
2013-08-17 23:18:58 -06:00
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct resource *res;
|
|
|
|
struct davinci_gpio_controller *chips = platform_get_drvdata(pdev);
|
|
|
|
struct davinci_gpio_platform_data *pdata = dev->platform_data;
|
|
|
|
struct davinci_gpio_regs __iomem *g;
|
2009-04-15 13:40:35 -06:00
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
ngpio = pdata->ngpio;
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
|
|
|
|
if (!res) {
|
|
|
|
dev_err(dev, "Invalid IRQ resource\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2008-12-07 12:46:23 -07:00
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
bank_irq = res->start;
|
|
|
|
|
|
|
|
if (!bank_irq) {
|
|
|
|
dev_err(dev, "Invalid IRQ resource\n");
|
|
|
|
return -ENODEV;
|
2008-12-07 12:46:23 -07:00
|
|
|
}
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2013-08-17 23:18:58 -06:00
|
|
|
clk = devm_clk_get(dev, "gpio");
|
2007-07-10 06:03:43 -06:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
printk(KERN_ERR "Error %ld getting gpio clock?\n",
|
|
|
|
PTR_ERR(clk));
|
2008-12-07 12:46:23 -07:00
|
|
|
return PTR_ERR(clk);
|
2007-07-10 06:03:43 -06:00
|
|
|
}
|
2012-08-30 12:03:57 -06:00
|
|
|
clk_prepare_enable(clk);
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2013-08-17 23:18:57 -06:00
|
|
|
/*
|
|
|
|
* Arrange gpio_to_irq() support, handling either direct IRQs or
|
2009-06-25 18:01:31 -06:00
|
|
|
* banked IRQs. Having GPIOs in the first GPIO bank use direct
|
|
|
|
* IRQs, while the others use banked IRQs, would need some setup
|
|
|
|
* tweaks to recognize hardware which can do that.
|
|
|
|
*/
|
|
|
|
for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 32) {
|
|
|
|
chips[bank].chip.to_irq = gpio_to_irq_banked;
|
2013-08-17 23:18:58 -06:00
|
|
|
chips[bank].irq_base = pdata->gpio_unbanked
|
2009-06-25 18:01:31 -06:00
|
|
|
? -EINVAL
|
2013-08-17 23:18:58 -06:00
|
|
|
: (pdata->intc_irq_num + gpio);
|
2009-06-25 18:01:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
|
|
|
|
* controller only handling trigger modes. We currently assume no
|
|
|
|
* IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
|
|
|
|
*/
|
2013-08-17 23:18:58 -06:00
|
|
|
if (pdata->gpio_unbanked) {
|
2012-03-11 06:46:12 -06:00
|
|
|
static struct irq_chip_type gpio_unbanked;
|
2009-06-25 18:01:31 -06:00
|
|
|
|
|
|
|
/* pass "bank 0" GPIO IRQs to AINTC */
|
|
|
|
chips[0].chip.to_irq = gpio_to_irq_unbanked;
|
2013-11-07 23:45:55 -07:00
|
|
|
chips[0].gpio_irq = bank_irq;
|
|
|
|
chips[0].gpio_unbanked = pdata->gpio_unbanked;
|
2009-06-25 18:01:31 -06:00
|
|
|
binten = BIT(0);
|
|
|
|
|
|
|
|
/* AINTC handles mask/unmask; GPIO handles triggering */
|
|
|
|
irq = bank_irq;
|
2012-03-11 06:46:12 -06:00
|
|
|
gpio_unbanked = *container_of(irq_get_chip(irq),
|
|
|
|
struct irq_chip_type, chip);
|
|
|
|
gpio_unbanked.chip.name = "GPIO-AINTC";
|
|
|
|
gpio_unbanked.chip.irq_set_type = gpio_irq_type_unbanked;
|
2009-06-25 18:01:31 -06:00
|
|
|
|
|
|
|
/* default trigger: both edges */
|
2010-05-01 16:37:52 -06:00
|
|
|
g = gpio2regs(0);
|
2009-06-25 18:01:31 -06:00
|
|
|
__raw_writel(~0, &g->set_falling);
|
|
|
|
__raw_writel(~0, &g->set_rising);
|
|
|
|
|
|
|
|
/* set the direct IRQs up to use that irqchip */
|
2013-08-17 23:18:58 -06:00
|
|
|
for (gpio = 0; gpio < pdata->gpio_unbanked; gpio++, irq++) {
|
2012-03-11 06:46:12 -06:00
|
|
|
irq_set_chip(irq, &gpio_unbanked.chip);
|
2012-03-11 06:46:11 -06:00
|
|
|
irq_set_handler_data(irq, &chips[gpio / 32]);
|
2011-03-24 05:47:04 -06:00
|
|
|
irq_set_status_flags(irq, IRQ_TYPE_EDGE_BOTH);
|
2009-06-25 18:01:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
|
|
|
|
* then chain through our own handler.
|
|
|
|
*/
|
2008-12-07 12:46:23 -07:00
|
|
|
for (gpio = 0, irq = gpio_to_irq(0), bank = 0;
|
|
|
|
gpio < ngpio;
|
|
|
|
bank++, bank_irq++) {
|
2007-07-10 06:03:43 -06:00
|
|
|
unsigned i;
|
|
|
|
|
2009-06-25 18:01:31 -06:00
|
|
|
/* disabled by default, enabled only as needed */
|
2010-05-01 16:37:52 -06:00
|
|
|
g = gpio2regs(gpio);
|
2007-07-10 06:03:43 -06:00
|
|
|
__raw_writel(~0, &g->clr_falling);
|
|
|
|
__raw_writel(~0, &g->clr_rising);
|
|
|
|
|
|
|
|
/* set up all irqs in this bank */
|
2011-03-24 06:25:22 -06:00
|
|
|
irq_set_chained_handler(bank_irq, gpio_irq_handler);
|
2011-07-11 15:03:11 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each chip handles 32 gpios, and each irq bank consists of 16
|
|
|
|
* gpio irqs. Pass the irq bank's corresponding controller to
|
|
|
|
* the chained irq handler.
|
|
|
|
*/
|
|
|
|
irq_set_handler_data(bank_irq, &chips[gpio / 32]);
|
2007-07-10 06:03:43 -06:00
|
|
|
|
2008-12-07 12:46:23 -07:00
|
|
|
for (i = 0; i < 16 && gpio < ngpio; i++, irq++, gpio++) {
|
2011-03-24 06:25:22 -06:00
|
|
|
irq_set_chip(irq, &gpio_irqchip);
|
|
|
|
irq_set_chip_data(irq, (__force void *)g);
|
|
|
|
irq_set_handler_data(irq, (void *)__gpio_mask(gpio));
|
|
|
|
irq_set_handler(irq, handle_simple_irq);
|
2007-07-10 06:03:43 -06:00
|
|
|
set_irq_flags(irq, IRQF_VALID);
|
|
|
|
}
|
2008-12-07 12:46:23 -07:00
|
|
|
|
|
|
|
binten |= BIT(bank);
|
2007-07-10 06:03:43 -06:00
|
|
|
}
|
|
|
|
|
2009-06-25 18:01:31 -06:00
|
|
|
done:
|
2013-08-17 23:18:57 -06:00
|
|
|
/*
|
|
|
|
* BINTEN -- per-bank interrupt enable. genirq would also let these
|
2007-07-10 06:03:43 -06:00
|
|
|
* bits be set/cleared dynamically.
|
|
|
|
*/
|
2013-08-17 23:18:57 -06:00
|
|
|
__raw_writel(binten, gpio_base + BINTEN);
|
2007-07-10 06:03:43 -06:00
|
|
|
|
|
|
|
printk(KERN_INFO "DaVinci: %d gpio irqs\n", irq - gpio_to_irq(0));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-17 23:18:58 -06:00
|
|
|
|
|
|
|
static struct platform_driver davinci_gpio_driver = {
|
|
|
|
.probe = davinci_gpio_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "davinci_gpio",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GPIO driver registration needs to be done before machine_init functions
|
|
|
|
* access GPIO. Hence davinci_gpio_drv_reg() is a postcore_initcall.
|
|
|
|
*/
|
|
|
|
static int __init davinci_gpio_drv_reg(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&davinci_gpio_driver);
|
|
|
|
}
|
|
|
|
postcore_initcall(davinci_gpio_drv_reg);
|