Merge branch 'nfs-for-2.6.35' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / arch / arm / plat-nomadik / gpio.c
index 092f380..5a6ef25 100644 (file)
 #include <linux/device.h>
 #include <linux/platform_device.h>
 #include <linux/io.h>
+#include <linux/clk.h>
+#include <linux/err.h>
 #include <linux/gpio.h>
 #include <linux/spinlock.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
+#include <linux/slab.h>
 
 #include <mach/hardware.h>
 #include <mach/gpio.h>
@@ -35,6 +38,7 @@
 struct nmk_gpio_chip {
        struct gpio_chip chip;
        void __iomem *addr;
+       struct clk *clk;
        unsigned int parent_irq;
        spinlock_t lock;
        /* Keep track of configured edges */
@@ -107,40 +111,37 @@ static void nmk_gpio_irq_ack(unsigned int irq)
        writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
 }
 
-static void nmk_gpio_irq_mask(unsigned int irq)
+static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
+                                 int gpio, bool enable)
 {
-       int gpio;
-       struct nmk_gpio_chip *nmk_chip;
-       unsigned long flags;
-       u32 bitmask, reg;
-
-       gpio = NOMADIK_IRQ_TO_GPIO(irq);
-       nmk_chip = get_irq_chip_data(irq);
-       bitmask = nmk_gpio_get_bitmask(gpio);
-       if (!nmk_chip)
-               return;
+       u32 bitmask = nmk_gpio_get_bitmask(gpio);
+       u32 reg;
 
-       /* we must individually clear the two edges */
-       spin_lock_irqsave(&nmk_chip->lock, flags);
+       /* we must individually set/clear the two edges */
        if (nmk_chip->edge_rising & bitmask) {
-               reg = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
-               reg &= ~bitmask;
-               writel(reg, nmk_chip->addr + NMK_GPIO_RWIMSC);
+               reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC);
+               if (enable)
+                       reg |= bitmask;
+               else
+                       reg &= ~bitmask;
+               writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC);
        }
        if (nmk_chip->edge_falling & bitmask) {
-               reg = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
-               reg &= ~bitmask;
-               writel(reg, nmk_chip->addr + NMK_GPIO_FWIMSC);
+               reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC);
+               if (enable)
+                       reg |= bitmask;
+               else
+                       reg &= ~bitmask;
+               writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC);
        }
-       spin_unlock_irqrestore(&nmk_chip->lock, flags);
-};
+}
 
-static void nmk_gpio_irq_unmask(unsigned int irq)
+static void nmk_gpio_irq_modify(unsigned int irq, bool enable)
 {
        int gpio;
        struct nmk_gpio_chip *nmk_chip;
        unsigned long flags;
-       u32 bitmask, reg;
+       u32 bitmask;
 
        gpio = NOMADIK_IRQ_TO_GPIO(irq);
        nmk_chip = get_irq_chip_data(irq);
@@ -148,23 +149,24 @@ static void nmk_gpio_irq_unmask(unsigned int irq)
        if (!nmk_chip)
                return;
 
-       /* we must individually set the two edges */
        spin_lock_irqsave(&nmk_chip->lock, flags);
-       if (nmk_chip->edge_rising & bitmask) {
-               reg = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
-               reg |= bitmask;
-               writel(reg, nmk_chip->addr + NMK_GPIO_RWIMSC);
-       }
-       if (nmk_chip->edge_falling & bitmask) {
-               reg = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
-               reg |= bitmask;
-               writel(reg, nmk_chip->addr + NMK_GPIO_FWIMSC);
-       }
+       __nmk_gpio_irq_modify(nmk_chip, gpio, enable);
        spin_unlock_irqrestore(&nmk_chip->lock, flags);
 }
 
+static void nmk_gpio_irq_mask(unsigned int irq)
+{
+       nmk_gpio_irq_modify(irq, false);
+};
+
+static void nmk_gpio_irq_unmask(unsigned int irq)
+{
+       nmk_gpio_irq_modify(irq, true);
+}
+
 static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
 {
+       bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED);
        int gpio;
        struct nmk_gpio_chip *nmk_chip;
        unsigned long flags;
@@ -183,19 +185,21 @@ static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
 
        spin_lock_irqsave(&nmk_chip->lock, flags);
 
+       if (enabled)
+               __nmk_gpio_irq_modify(nmk_chip, gpio, false);
+
        nmk_chip->edge_rising &= ~bitmask;
        if (type & IRQ_TYPE_EDGE_RISING)
                nmk_chip->edge_rising |= bitmask;
-       writel(nmk_chip->edge_rising, nmk_chip->addr + NMK_GPIO_RIMSC);
 
        nmk_chip->edge_falling &= ~bitmask;
        if (type & IRQ_TYPE_EDGE_FALLING)
                nmk_chip->edge_falling |= bitmask;
-       writel(nmk_chip->edge_falling, nmk_chip->addr + NMK_GPIO_FIMSC);
 
-       spin_unlock_irqrestore(&nmk_chip->lock, flags);
+       if (enabled)
+               __nmk_gpio_irq_modify(nmk_chip, gpio, true);
 
-       nmk_gpio_irq_unmask(irq);
+       spin_unlock_irqrestore(&nmk_chip->lock, flags);
 
        return 0;
 }
@@ -245,6 +249,7 @@ static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
                set_irq_handler(i, handle_edge_irq);
                set_irq_flags(i, IRQF_VALID);
                set_irq_chip_data(i, nmk_chip);
+               set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
        }
        set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
        set_irq_data(nmk_chip->parent_irq, nmk_chip);
@@ -309,6 +314,7 @@ static int __init nmk_gpio_probe(struct platform_device *dev)
        struct nmk_gpio_chip *nmk_chip;
        struct gpio_chip *chip;
        struct resource *res;
+       struct clk *clk;
        int irq;
        int ret;
 
@@ -333,15 +339,24 @@ static int __init nmk_gpio_probe(struct platform_device *dev)
                goto out;
        }
 
+       clk = clk_get(&dev->dev, NULL);
+       if (IS_ERR(clk)) {
+               ret = PTR_ERR(clk);
+               goto out_release;
+       }
+
+       clk_enable(clk);
+
        nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
        if (!nmk_chip) {
                ret = -ENOMEM;
-               goto out_release;
+               goto out_clk;
        }
        /*
         * The virt address in nmk_chip->addr is in the nomadik register space,
         * so we can simply convert the resource address, without remapping
         */
+       nmk_chip->clk = clk;
        nmk_chip->addr = io_p2v(res->start);
        nmk_chip->chip = nmk_gpio_template;
        nmk_chip->parent_irq = irq;
@@ -367,6 +382,9 @@ static int __init nmk_gpio_probe(struct platform_device *dev)
 
 out_free:
        kfree(nmk_chip);
+out_clk:
+       clk_disable(clk);
+       clk_put(clk);
 out_release:
        release_mem_region(res->start, resource_size(res));
 out:
@@ -384,6 +402,8 @@ static int __exit nmk_gpio_remove(struct platform_device *dev)
 
        nmk_chip = platform_get_drvdata(dev);
        gpiochip_remove(&nmk_chip->chip);
+       clk_disable(nmk_chip->clk);
+       clk_put(nmk_chip->clk);
        kfree(nmk_chip);
        release_mem_region(res->start, resource_size(res));
        return 0;